[c5808b41] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 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 TCP transmission queue
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
[6df418c4] | 37 | #include <adt/list.h>
|
---|
[7cf7ded] | 38 | #include <errno.h>
|
---|
| 39 | #include <fibril_synch.h>
|
---|
[c5808b41] | 40 | #include <byteorder.h>
|
---|
| 41 | #include <io/log.h>
|
---|
[32105348] | 42 | #include <macros.h>
|
---|
| 43 | #include <mem.h>
|
---|
[6df418c4] | 44 | #include <stdlib.h>
|
---|
[32105348] | 45 | #include "conn.h"
|
---|
[8218b6b] | 46 | #include "ncsim.h"
|
---|
[762b48a] | 47 | #include "pdu.h"
|
---|
[c5808b41] | 48 | #include "rqueue.h"
|
---|
| 49 | #include "segment.h"
|
---|
[6df418c4] | 50 | #include "seq_no.h"
|
---|
[c5808b41] | 51 | #include "tqueue.h"
|
---|
[1812a0d] | 52 | #include "tcp.h"
|
---|
[c5808b41] | 53 | #include "tcp_type.h"
|
---|
| 54 |
|
---|
[7cf7ded] | 55 | #define RETRANSMIT_TIMEOUT (2*1000*1000)
|
---|
| 56 |
|
---|
| 57 | static void retransmit_timeout_func(void *arg);
|
---|
| 58 | static void tcp_tqueue_timer_set(tcp_conn_t *conn);
|
---|
| 59 | static void tcp_tqueue_timer_clear(tcp_conn_t *conn);
|
---|
| 60 |
|
---|
| 61 | int tcp_tqueue_init(tcp_tqueue_t *tqueue, tcp_conn_t *conn)
|
---|
[6df418c4] | 62 | {
|
---|
| 63 | tqueue->conn = conn;
|
---|
[7cf7ded] | 64 | tqueue->timer = fibril_timer_create();
|
---|
| 65 | if (tqueue->timer == NULL)
|
---|
| 66 | return ENOMEM;
|
---|
| 67 |
|
---|
[6df418c4] | 68 | list_initialize(&tqueue->list);
|
---|
[7cf7ded] | 69 |
|
---|
| 70 | return EOK;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[704586fb] | 73 | void tcp_tqueue_clear(tcp_tqueue_t *tqueue)
|
---|
| 74 | {
|
---|
| 75 | tcp_tqueue_timer_clear(tqueue->conn);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[7cf7ded] | 78 | void tcp_tqueue_fini(tcp_tqueue_t *tqueue)
|
---|
| 79 | {
|
---|
| 80 | if (tqueue->timer != NULL) {
|
---|
| 81 | fibril_timer_destroy(tqueue->timer);
|
---|
| 82 | tqueue->timer = NULL;
|
---|
| 83 | }
|
---|
[6df418c4] | 84 | }
|
---|
| 85 |
|
---|
[c5808b41] | 86 | void tcp_tqueue_ctrl_seg(tcp_conn_t *conn, tcp_control_t ctrl)
|
---|
| 87 | {
|
---|
| 88 | tcp_segment_t *seg;
|
---|
| 89 |
|
---|
[a1a101d] | 90 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_tqueue_ctrl_seg(%p, %u)", conn, ctrl);
|
---|
[c5808b41] | 91 |
|
---|
| 92 | seg = tcp_segment_make_ctrl(ctrl);
|
---|
| 93 | tcp_tqueue_seg(conn, seg);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void tcp_tqueue_seg(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 97 | {
|
---|
[6df418c4] | 98 | tcp_segment_t *rt_seg;
|
---|
| 99 | tcp_tqueue_entry_t *tqe;
|
---|
| 100 |
|
---|
[a1a101d] | 101 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_tqueue_seg(%p, %p)", conn->name, conn,
|
---|
[23fe06c] | 102 | seg);
|
---|
[6df418c4] | 103 |
|
---|
| 104 | /*
|
---|
| 105 | * Add segment to retransmission queue
|
---|
| 106 | */
|
---|
| 107 |
|
---|
| 108 | if (seg->len > 0) {
|
---|
| 109 | rt_seg = tcp_segment_dup(seg);
|
---|
| 110 | if (rt_seg == NULL) {
|
---|
[a1a101d] | 111 | log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed.");
|
---|
[6df418c4] | 112 | /* XXX Handle properly */
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | tqe = calloc(1, sizeof(tcp_tqueue_entry_t));
|
---|
| 117 | if (tqe == NULL) {
|
---|
[a1a101d] | 118 | log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed.");
|
---|
[6df418c4] | 119 | /* XXX Handle properly */
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[7cf7ded] | 123 | tqe->conn = conn;
|
---|
[6df418c4] | 124 | tqe->seg = rt_seg;
|
---|
[74c99b5] | 125 | rt_seg->seq = conn->snd_nxt;
|
---|
| 126 |
|
---|
[6df418c4] | 127 | list_append(&tqe->link, &conn->retransmit.list);
|
---|
[7cf7ded] | 128 |
|
---|
| 129 | /* Set retransmission timer */
|
---|
| 130 | tcp_tqueue_timer_set(conn);
|
---|
[6df418c4] | 131 | }
|
---|
[c5808b41] | 132 |
|
---|
[7cf7ded] | 133 | tcp_prepare_transmit_segment(conn, seg);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void tcp_prepare_transmit_segment(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 137 | {
|
---|
[32105348] | 138 | /*
|
---|
| 139 | * Always send ACK once we have received SYN, except for RST segments.
|
---|
| 140 | * (Spec says we should always send ACK once connection has been
|
---|
| 141 | * established.)
|
---|
| 142 | */
|
---|
| 143 | if (tcp_conn_got_syn(conn) && (seg->ctrl & CTL_RST) == 0)
|
---|
| 144 | seg->ctrl |= CTL_ACK;
|
---|
| 145 |
|
---|
| 146 | seg->seq = conn->snd_nxt;
|
---|
[c5808b41] | 147 | conn->snd_nxt += seg->len;
|
---|
[32105348] | 148 |
|
---|
[4f3f6285] | 149 | tcp_conn_transmit_segment(conn, seg);
|
---|
[c5808b41] | 150 | }
|
---|
| 151 |
|
---|
[32105348] | 152 | /** Transmit data from the send buffer.
|
---|
| 153 | *
|
---|
| 154 | * @param conn Connection
|
---|
| 155 | */
|
---|
| 156 | void tcp_tqueue_new_data(tcp_conn_t *conn)
|
---|
| 157 | {
|
---|
[d9ce049] | 158 | size_t avail_wnd;
|
---|
[8c7a054] | 159 | size_t xfer_seqlen;
|
---|
| 160 | size_t snd_buf_seqlen;
|
---|
[32105348] | 161 | size_t data_size;
|
---|
[8c7a054] | 162 | tcp_control_t ctrl;
|
---|
[6896409c] | 163 | bool send_fin;
|
---|
[8c7a054] | 164 |
|
---|
[32105348] | 165 | tcp_segment_t *seg;
|
---|
| 166 |
|
---|
[a1a101d] | 167 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_tqueue_new_data()", conn->name);
|
---|
[32105348] | 168 |
|
---|
[d9ce049] | 169 | /* Number of free sequence numbers in send window */
|
---|
| 170 | avail_wnd = (conn->snd_una + conn->snd_wnd) - conn->snd_nxt;
|
---|
[8c7a054] | 171 | snd_buf_seqlen = conn->snd_buf_used + (conn->snd_buf_fin ? 1 : 0);
|
---|
[d9ce049] | 172 |
|
---|
[8c7a054] | 173 | xfer_seqlen = min(snd_buf_seqlen, avail_wnd);
|
---|
[70253688] | 174 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: snd_buf_seqlen = %zu, SND.WND = %" PRIu32 ", "
|
---|
[6896409c] | 175 | "xfer_seqlen = %zu", conn->name, snd_buf_seqlen, conn->snd_wnd,
|
---|
[8c7a054] | 176 | xfer_seqlen);
|
---|
[32105348] | 177 |
|
---|
[0edaf0f6] | 178 | if (xfer_seqlen == 0)
|
---|
[32105348] | 179 | return;
|
---|
| 180 |
|
---|
| 181 | /* XXX Do not always send immediately */
|
---|
| 182 |
|
---|
[6896409c] | 183 | send_fin = conn->snd_buf_fin && xfer_seqlen == snd_buf_seqlen;
|
---|
| 184 | data_size = xfer_seqlen - (send_fin ? 1 : 0);
|
---|
| 185 |
|
---|
| 186 | if (send_fin) {
|
---|
[a1a101d] | 187 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Sending out FIN.", conn->name);
|
---|
[8c7a054] | 188 | /* We are sending out FIN */
|
---|
| 189 | ctrl = CTL_FIN;
|
---|
| 190 | } else {
|
---|
| 191 | ctrl = 0;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | seg = tcp_segment_make_data(ctrl, conn->snd_buf, data_size);
|
---|
[32105348] | 195 | if (seg == NULL) {
|
---|
[a1a101d] | 196 | log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failure.");
|
---|
[32105348] | 197 | return;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /* Remove data from send buffer */
|
---|
| 201 | memmove(conn->snd_buf, conn->snd_buf + data_size,
|
---|
| 202 | conn->snd_buf_used - data_size);
|
---|
| 203 | conn->snd_buf_used -= data_size;
|
---|
[6896409c] | 204 |
|
---|
| 205 | if (send_fin)
|
---|
| 206 | conn->snd_buf_fin = false;
|
---|
[32105348] | 207 |
|
---|
[bbf159a] | 208 | fibril_condvar_broadcast(&conn->snd_buf_cv);
|
---|
| 209 |
|
---|
| 210 | if (send_fin)
|
---|
| 211 | tcp_conn_fin_sent(conn);
|
---|
| 212 |
|
---|
[32105348] | 213 | tcp_tqueue_seg(conn, seg);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[d9ce049] | 216 | /** Remove ACKed segments from retransmission queue and possibly transmit
|
---|
| 217 | * more data.
|
---|
[4c55a64] | 218 | *
|
---|
| 219 | * This should be called when SND.UNA is updated due to incoming ACK.
|
---|
| 220 | */
|
---|
[d9ce049] | 221 | void tcp_tqueue_ack_received(tcp_conn_t *conn)
|
---|
[4c55a64] | 222 | {
|
---|
[6df418c4] | 223 | link_t *cur, *next;
|
---|
| 224 |
|
---|
[a1a101d] | 225 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_tqueue_ack_received(%p)", conn->name,
|
---|
[23fe06c] | 226 | conn);
|
---|
[6df418c4] | 227 |
|
---|
| 228 | cur = conn->retransmit.list.head.next;
|
---|
| 229 |
|
---|
| 230 | while (cur != &conn->retransmit.list.head) {
|
---|
| 231 | next = cur->next;
|
---|
| 232 |
|
---|
| 233 | tcp_tqueue_entry_t *tqe = list_get_instance(cur,
|
---|
| 234 | tcp_tqueue_entry_t, link);
|
---|
| 235 |
|
---|
| 236 | if (seq_no_segment_acked(conn, tqe->seg, conn->snd_una)) {
|
---|
| 237 | /* Remove acknowledged segment */
|
---|
| 238 | list_remove(cur);
|
---|
| 239 |
|
---|
| 240 | if ((tqe->seg->ctrl & CTL_FIN) != 0) {
|
---|
[a1a101d] | 241 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Fin has been acked");
|
---|
| 242 | log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.UNA=%" PRIu32
|
---|
[74c99b5] | 243 | " SEG.SEQ=%" PRIu32 " SEG.LEN=%" PRIu32,
|
---|
| 244 | conn->snd_una, tqe->seg->seq, tqe->seg->len);
|
---|
[6df418c4] | 245 | /* Our FIN has been acked */
|
---|
| 246 | conn->fin_is_acked = true;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | tcp_segment_delete(tqe->seg);
|
---|
| 250 | free(tqe);
|
---|
[7cf7ded] | 251 |
|
---|
| 252 | /* Reset retransmission timer */
|
---|
| 253 | tcp_tqueue_timer_set(conn);
|
---|
[6df418c4] | 254 | }
|
---|
| 255 |
|
---|
| 256 | cur = next;
|
---|
| 257 | }
|
---|
[d9ce049] | 258 |
|
---|
[7cf7ded] | 259 | /* Clear retransmission timer if the queue is empty. */
|
---|
| 260 | if (list_empty(&conn->retransmit.list))
|
---|
| 261 | tcp_tqueue_timer_clear(conn);
|
---|
| 262 |
|
---|
[6df418c4] | 263 | /* Possibly transmit more data */
|
---|
[d9ce049] | 264 | tcp_tqueue_new_data(conn);
|
---|
[4c55a64] | 265 | }
|
---|
| 266 |
|
---|
[7cf7ded] | 267 | void tcp_conn_transmit_segment(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 268 | {
|
---|
[a1a101d] | 269 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_transmit_segment(%p, %p)",
|
---|
[23fe06c] | 270 | conn->name, conn, seg);
|
---|
[7cf7ded] | 271 |
|
---|
| 272 | seg->wnd = conn->rcv_wnd;
|
---|
| 273 |
|
---|
| 274 | if ((seg->ctrl & CTL_ACK) != 0)
|
---|
| 275 | seg->ack = conn->rcv_nxt;
|
---|
| 276 | else
|
---|
| 277 | seg->ack = 0;
|
---|
| 278 |
|
---|
| 279 | tcp_transmit_segment(&conn->ident, seg);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[c5808b41] | 282 | void tcp_transmit_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 283 | {
|
---|
[a1a101d] | 284 | log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.SEQ=%" PRIu32 ", SEG.WND=%" PRIu32,
|
---|
[7cf7ded] | 285 | seg->seq, seg->wnd);
|
---|
[6896409c] | 286 |
|
---|
| 287 | tcp_segment_dump(seg);
|
---|
[c5808b41] | 288 | /*
|
---|
| 289 | tcp_pdu_prepare(conn, seg, &data, &len);
|
---|
| 290 | tcp_pdu_transmit(data, len);
|
---|
| 291 | */
|
---|
[1812a0d] | 292 | // tcp_rqueue_bounce_seg(sp, seg);
|
---|
[7cf7ded] | 293 | // tcp_ncsim_bounce_seg(sp, seg);
|
---|
[1812a0d] | 294 |
|
---|
| 295 | tcp_pdu_t *pdu;
|
---|
| 296 |
|
---|
| 297 | if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
|
---|
[a1a101d] | 298 | log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
[1812a0d] | 299 | return;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | tcp_transmit_pdu(pdu);
|
---|
| 303 | tcp_pdu_delete(pdu);
|
---|
[7cf7ded] | 304 | }
|
---|
| 305 |
|
---|
| 306 | static void retransmit_timeout_func(void *arg)
|
---|
| 307 | {
|
---|
| 308 | tcp_conn_t *conn = (tcp_conn_t *) arg;
|
---|
| 309 | tcp_tqueue_entry_t *tqe;
|
---|
| 310 | tcp_segment_t *rt_seg;
|
---|
| 311 | link_t *link;
|
---|
| 312 |
|
---|
[a1a101d] | 313 | log_msg(LOG_DEFAULT, LVL_DEBUG, "### %s: retransmit_timeout_func(%p)", conn->name, conn);
|
---|
[704586fb] | 314 |
|
---|
[0edaf0f6] | 315 | fibril_mutex_lock(&conn->lock);
|
---|
| 316 |
|
---|
[704586fb] | 317 | if (conn->cstate == st_closed) {
|
---|
[a1a101d] | 318 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
|
---|
[0edaf0f6] | 319 | fibril_mutex_unlock(&conn->lock);
|
---|
| 320 | tcp_conn_delref(conn);
|
---|
[704586fb] | 321 | return;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[7cf7ded] | 324 | link = list_first(&conn->retransmit.list);
|
---|
| 325 | if (link == NULL) {
|
---|
[a1a101d] | 326 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Nothing to retransmit");
|
---|
[0edaf0f6] | 327 | fibril_mutex_unlock(&conn->lock);
|
---|
| 328 | tcp_conn_delref(conn);
|
---|
[7cf7ded] | 329 | return;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | tqe = list_get_instance(link, tcp_tqueue_entry_t, link);
|
---|
| 333 |
|
---|
| 334 | rt_seg = tcp_segment_dup(tqe->seg);
|
---|
| 335 | if (rt_seg == NULL) {
|
---|
[a1a101d] | 336 | log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed.");
|
---|
[0edaf0f6] | 337 | fibril_mutex_unlock(&conn->lock);
|
---|
| 338 | tcp_conn_delref(conn);
|
---|
[7cf7ded] | 339 | /* XXX Handle properly */
|
---|
| 340 | return;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[a1a101d] | 343 | log_msg(LOG_DEFAULT, LVL_DEBUG, "### %s: retransmitting segment", conn->name);
|
---|
[7cf7ded] | 344 | tcp_conn_transmit_segment(tqe->conn, rt_seg);
|
---|
| 345 |
|
---|
| 346 | /* Reset retransmission timer */
|
---|
| 347 | tcp_tqueue_timer_set(tqe->conn);
|
---|
[0edaf0f6] | 348 |
|
---|
| 349 | fibril_mutex_unlock(&conn->lock);
|
---|
| 350 | tcp_conn_delref(conn);
|
---|
[7cf7ded] | 351 | }
|
---|
| 352 |
|
---|
| 353 | /** Set or re-set retransmission timer */
|
---|
| 354 | static void tcp_tqueue_timer_set(tcp_conn_t *conn)
|
---|
| 355 | {
|
---|
[a1a101d] | 356 | log_msg(LOG_DEFAULT, LVL_DEBUG, "### %s: tcp_tqueue_timer_set()", conn->name);
|
---|
[7cf7ded] | 357 |
|
---|
[0edaf0f6] | 358 | /* Clear first to make sure we update refcnt correctly */
|
---|
| 359 | tcp_tqueue_timer_clear(conn);
|
---|
| 360 |
|
---|
| 361 | tcp_conn_addref(conn);
|
---|
[7cf7ded] | 362 | fibril_timer_set(conn->retransmit.timer, RETRANSMIT_TIMEOUT,
|
---|
| 363 | retransmit_timeout_func, (void *) conn);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /** Clear retransmission timer */
|
---|
| 367 | static void tcp_tqueue_timer_clear(tcp_conn_t *conn)
|
---|
| 368 | {
|
---|
[a1a101d] | 369 | log_msg(LOG_DEFAULT, LVL_DEBUG, "### %s: tcp_tqueue_timer_clear()", conn->name);
|
---|
[7cf7ded] | 370 |
|
---|
[0edaf0f6] | 371 | if (fibril_timer_clear(conn->retransmit.timer) == fts_active)
|
---|
| 372 | tcp_conn_delref(conn);
|
---|
[c5808b41] | 373 | }
|
---|
| 374 |
|
---|
| 375 | /**
|
---|
| 376 | * @}
|
---|
| 377 | */
|
---|