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