source: mainline/uspace/srv/net/tcp/tqueue.c@ 42f61f01

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

Fix ia64/abs32le builds.

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