[c5808b41] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[c5808b41] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup tcp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[032bbe7] | 34 | * @file Global segment receive queue
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/prodcons.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <io/log.h>
|
---|
[d14840d] | 40 | #include <stdbool.h>
|
---|
[c5808b41] | 41 | #include <stdlib.h>
|
---|
[88a6819] | 42 | #include <fibril.h>
|
---|
[d14840d] | 43 | #include <fibril_synch.h>
|
---|
[032bbe7] | 44 | #include "conn.h"
|
---|
[762b48a] | 45 | #include "pdu.h"
|
---|
[c5808b41] | 46 | #include "rqueue.h"
|
---|
[eea65f4] | 47 | #include "segment.h"
|
---|
[c5808b41] | 48 | #include "tcp_type.h"
|
---|
[762b48a] | 49 | #include "ucall.h"
|
---|
[c5808b41] | 50 |
|
---|
[eea65f4] | 51 | /** Transcode bounced segments.
|
---|
| 52 | *
|
---|
| 53 | * If defined, segments bounced via the internal debugging loopback will
|
---|
| 54 | * be encoded to a PDU and the decoded. Otherwise they will be bounced back
|
---|
| 55 | * directly without passing the encoder-decoder.
|
---|
| 56 | */
|
---|
| 57 | #define BOUNCE_TRANSCODE
|
---|
| 58 |
|
---|
[c5808b41] | 59 | static prodcons_t rqueue;
|
---|
[d14840d] | 60 | static bool fibril_active;
|
---|
| 61 | static fibril_mutex_t lock;
|
---|
| 62 | static fibril_condvar_t cv;
|
---|
| 63 | static tcp_rqueue_cb_t *rqueue_cb;
|
---|
[c5808b41] | 64 |
|
---|
[032bbe7] | 65 | /** Initialize segment receive queue. */
|
---|
[d14840d] | 66 | void tcp_rqueue_init(tcp_rqueue_cb_t *rcb)
|
---|
[c5808b41] | 67 | {
|
---|
| 68 | prodcons_initialize(&rqueue);
|
---|
[d14840d] | 69 | fibril_mutex_initialize(&lock);
|
---|
| 70 | fibril_condvar_initialize(&cv);
|
---|
| 71 | fibril_active = false;
|
---|
| 72 | rqueue_cb = rcb;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Finalize segment receive queue. */
|
---|
| 76 | void tcp_rqueue_fini(void)
|
---|
| 77 | {
|
---|
| 78 | inet_ep2_t epp;
|
---|
| 79 |
|
---|
| 80 | inet_ep2_init(&epp);
|
---|
| 81 | tcp_rqueue_insert_seg(&epp, NULL);
|
---|
| 82 |
|
---|
| 83 | fibril_mutex_lock(&lock);
|
---|
| 84 | while (fibril_active)
|
---|
| 85 | fibril_condvar_wait(&cv, &lock);
|
---|
| 86 | fibril_mutex_unlock(&lock);
|
---|
[c5808b41] | 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Bounce segment directy into receive queue without constructing the PDU.
|
---|
| 90 | *
|
---|
| 91 | * This is for testing purposes only.
|
---|
[032bbe7] | 92 | *
|
---|
[2f19103] | 93 | * @param sp Endpoint pair, oriented for transmission
|
---|
[032bbe7] | 94 | * @param seg Segment
|
---|
[c5808b41] | 95 | */
|
---|
[2f19103] | 96 | void tcp_rqueue_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
[c5808b41] | 97 | {
|
---|
[2f19103] | 98 | inet_ep2_t rident;
|
---|
[c5808b41] | 99 |
|
---|
[a1a101d] | 100 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_bounce_seg()");
|
---|
[c5808b41] | 101 |
|
---|
[eea65f4] | 102 | #ifdef BOUNCE_TRANSCODE
|
---|
| 103 | tcp_pdu_t *pdu;
|
---|
| 104 | tcp_segment_t *dseg;
|
---|
| 105 |
|
---|
[2f19103] | 106 | if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
|
---|
[a1a101d] | 107 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
[eea65f4] | 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
---|
[a1a101d] | 112 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
[eea65f4] | 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | tcp_pdu_delete(pdu);
|
---|
| 117 |
|
---|
| 118 | /** Insert decoded segment into rqueue */
|
---|
| 119 | tcp_rqueue_insert_seg(&rident, dseg);
|
---|
| 120 | tcp_segment_delete(seg);
|
---|
| 121 | #else
|
---|
[c5808b41] | 122 | /* Reverse the identification */
|
---|
[2f19103] | 123 | tcp_ep2_flipped(epp, &rident);
|
---|
[c5808b41] | 124 |
|
---|
[eea65f4] | 125 | /* Insert segment back into rqueue */
|
---|
[c5808b41] | 126 | tcp_rqueue_insert_seg(&rident, seg);
|
---|
[eea65f4] | 127 | #endif
|
---|
[c5808b41] | 128 | }
|
---|
| 129 |
|
---|
[032bbe7] | 130 | /** Insert segment into receive queue.
|
---|
| 131 | *
|
---|
[2f19103] | 132 | * @param epp Endpoint pair, oriented for reception
|
---|
[032bbe7] | 133 | * @param seg Segment
|
---|
| 134 | */
|
---|
[2f19103] | 135 | void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
[c5808b41] | 136 | {
|
---|
| 137 | tcp_rqueue_entry_t *rqe;
|
---|
| 138 |
|
---|
[d14840d] | 139 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_insert_seg()");
|
---|
| 140 |
|
---|
| 141 | if (seg != NULL)
|
---|
| 142 | tcp_segment_dump(seg);
|
---|
[6896409c] | 143 |
|
---|
[c5808b41] | 144 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
---|
| 145 | if (rqe == NULL) {
|
---|
[a1a101d] | 146 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating RQE.");
|
---|
[c5808b41] | 147 | return;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[2f19103] | 150 | rqe->epp = *epp;
|
---|
[c5808b41] | 151 | rqe->seg = seg;
|
---|
| 152 |
|
---|
| 153 | prodcons_produce(&rqueue, &rqe->link);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[88a6819] | 156 | /** Receive queue handler fibril. */
|
---|
| 157 | static int tcp_rqueue_fibril(void *arg)
|
---|
[c5808b41] | 158 | {
|
---|
| 159 | link_t *link;
|
---|
| 160 | tcp_rqueue_entry_t *rqe;
|
---|
| 161 |
|
---|
[a1a101d] | 162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril()");
|
---|
[c5808b41] | 163 |
|
---|
| 164 | while (true) {
|
---|
| 165 | link = prodcons_consume(&rqueue);
|
---|
| 166 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
---|
| 167 |
|
---|
[d14840d] | 168 | if (rqe->seg == NULL) {
|
---|
| 169 | free(rqe);
|
---|
| 170 | break;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | rqueue_cb->seg_received(&rqe->epp, rqe->seg);
|
---|
[2cb32f9] | 174 | free(rqe);
|
---|
[c5808b41] | 175 | }
|
---|
[88a6819] | 176 |
|
---|
[d14840d] | 177 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_fibril() exiting");
|
---|
| 178 |
|
---|
| 179 | /* Finished */
|
---|
| 180 | fibril_mutex_lock(&lock);
|
---|
| 181 | fibril_active = false;
|
---|
| 182 | fibril_mutex_unlock(&lock);
|
---|
| 183 | fibril_condvar_broadcast(&cv);
|
---|
| 184 |
|
---|
[88a6819] | 185 | return 0;
|
---|
[c5808b41] | 186 | }
|
---|
| 187 |
|
---|
[88a6819] | 188 | /** Start receive queue handler fibril. */
|
---|
| 189 | void tcp_rqueue_fibril_start(void)
|
---|
[c5808b41] | 190 | {
|
---|
[88a6819] | 191 | fid_t fid;
|
---|
[c5808b41] | 192 |
|
---|
[a1a101d] | 193 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril_start()");
|
---|
[c5808b41] | 194 |
|
---|
[88a6819] | 195 | fid = fibril_create(tcp_rqueue_fibril, NULL);
|
---|
| 196 | if (fid == 0) {
|
---|
[a1a101d] | 197 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating rqueue fibril.");
|
---|
[c5808b41] | 198 | return;
|
---|
| 199 | }
|
---|
[88a6819] | 200 |
|
---|
| 201 | fibril_add_ready(fid);
|
---|
[d14840d] | 202 | fibril_active = true;
|
---|
[c5808b41] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * @}
|
---|
| 207 | */
|
---|