source: mainline/uspace/srv/net/tcp/tqueue.c@ 36f0738

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36f0738 was 9520af7, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Allow TCP conn tests that involve transferring data by enabling an internal loopback. Add simple →SYN, ←RST test.

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