source: mainline/uspace/srv/net/tcp/tqueue.c@ 78192cc7

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

Fibril timer locking improvements.

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