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 |
|
---|
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)
|
---|
62 | {
|
---|
63 | tqueue->conn = conn;
|
---|
64 | tqueue->timer = fibril_timer_create();
|
---|
65 | if (tqueue->timer == NULL)
|
---|
66 | return ENOMEM;
|
---|
67 |
|
---|
68 | list_initialize(&tqueue->list);
|
---|
69 |
|
---|
70 | return EOK;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void tcp_tqueue_clear(tcp_tqueue_t *tqueue)
|
---|
74 | {
|
---|
75 | tcp_tqueue_timer_clear(tqueue->conn);
|
---|
76 | }
|
---|
77 |
|
---|
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 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | void tcp_tqueue_ctrl_seg(tcp_conn_t *conn, tcp_control_t ctrl)
|
---|
87 | {
|
---|
88 | tcp_segment_t *seg;
|
---|
89 |
|
---|
90 | log_msg(LVL_DEBUG, "tcp_tqueue_ctrl_seg(%p, %u)", conn, ctrl);
|
---|
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 | {
|
---|
98 | tcp_segment_t *rt_seg;
|
---|
99 | tcp_tqueue_entry_t *tqe;
|
---|
100 |
|
---|
101 | log_msg(LVL_DEBUG, "%s: tcp_tqueue_seg(%p, %p)", conn->name, conn,
|
---|
102 | seg);
|
---|
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) {
|
---|
111 | log_msg(LVL_ERROR, "Memory allocation failed.");
|
---|
112 | /* XXX Handle properly */
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | tqe = calloc(1, sizeof(tcp_tqueue_entry_t));
|
---|
117 | if (tqe == NULL) {
|
---|
118 | log_msg(LVL_ERROR, "Memory allocation failed.");
|
---|
119 | /* XXX Handle properly */
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | tqe->conn = conn;
|
---|
124 | tqe->seg = rt_seg;
|
---|
125 | list_append(&tqe->link, &conn->retransmit.list);
|
---|
126 |
|
---|
127 | /* Set retransmission timer */
|
---|
128 | tcp_tqueue_timer_set(conn);
|
---|
129 | }
|
---|
130 |
|
---|
131 | tcp_prepare_transmit_segment(conn, seg);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void tcp_prepare_transmit_segment(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Always send ACK once we have received SYN, except for RST segments.
|
---|
138 | * (Spec says we should always send ACK once connection has been
|
---|
139 | * established.)
|
---|
140 | */
|
---|
141 | if (tcp_conn_got_syn(conn) && (seg->ctrl & CTL_RST) == 0)
|
---|
142 | seg->ctrl |= CTL_ACK;
|
---|
143 |
|
---|
144 | seg->seq = conn->snd_nxt;
|
---|
145 | conn->snd_nxt += seg->len;
|
---|
146 |
|
---|
147 | tcp_conn_transmit_segment(conn, seg);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Transmit data from the send buffer.
|
---|
151 | *
|
---|
152 | * @param conn Connection
|
---|
153 | */
|
---|
154 | void tcp_tqueue_new_data(tcp_conn_t *conn)
|
---|
155 | {
|
---|
156 | size_t avail_wnd;
|
---|
157 | size_t xfer_seqlen;
|
---|
158 | size_t snd_buf_seqlen;
|
---|
159 | size_t data_size;
|
---|
160 | tcp_control_t ctrl;
|
---|
161 | bool send_fin;
|
---|
162 |
|
---|
163 | tcp_segment_t *seg;
|
---|
164 |
|
---|
165 | log_msg(LVL_DEBUG, "%s: tcp_tqueue_new_data()", conn->name);
|
---|
166 |
|
---|
167 | /* Number of free sequence numbers in send window */
|
---|
168 | avail_wnd = (conn->snd_una + conn->snd_wnd) - conn->snd_nxt;
|
---|
169 | snd_buf_seqlen = conn->snd_buf_used + (conn->snd_buf_fin ? 1 : 0);
|
---|
170 |
|
---|
171 | xfer_seqlen = min(snd_buf_seqlen, avail_wnd);
|
---|
172 | log_msg(LVL_DEBUG, "%s: snd_buf_seqlen = %zu, SND.WND = %zu, "
|
---|
173 | "xfer_seqlen = %zu", conn->name, snd_buf_seqlen, conn->snd_wnd,
|
---|
174 | xfer_seqlen);
|
---|
175 |
|
---|
176 | if (xfer_seqlen == 0)
|
---|
177 | return;
|
---|
178 |
|
---|
179 | /* XXX Do not always send immediately */
|
---|
180 |
|
---|
181 | send_fin = conn->snd_buf_fin && xfer_seqlen == snd_buf_seqlen;
|
---|
182 | data_size = xfer_seqlen - (send_fin ? 1 : 0);
|
---|
183 |
|
---|
184 | if (send_fin) {
|
---|
185 | log_msg(LVL_DEBUG, "%s: Sending out FIN.", conn->name);
|
---|
186 | /* We are sending out FIN */
|
---|
187 | ctrl = CTL_FIN;
|
---|
188 | tcp_conn_fin_sent(conn);
|
---|
189 | } else {
|
---|
190 | ctrl = 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | seg = tcp_segment_make_data(ctrl, conn->snd_buf, data_size);
|
---|
194 | if (seg == NULL) {
|
---|
195 | log_msg(LVL_ERROR, "Memory allocation failure.");
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Remove data from send buffer */
|
---|
200 | memmove(conn->snd_buf, conn->snd_buf + data_size,
|
---|
201 | conn->snd_buf_used - data_size);
|
---|
202 | conn->snd_buf_used -= data_size;
|
---|
203 |
|
---|
204 | if (send_fin)
|
---|
205 | conn->snd_buf_fin = false;
|
---|
206 |
|
---|
207 | tcp_tqueue_seg(conn, seg);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Remove ACKed segments from retransmission queue and possibly transmit
|
---|
211 | * more data.
|
---|
212 | *
|
---|
213 | * This should be called when SND.UNA is updated due to incoming ACK.
|
---|
214 | */
|
---|
215 | void tcp_tqueue_ack_received(tcp_conn_t *conn)
|
---|
216 | {
|
---|
217 | link_t *cur, *next;
|
---|
218 |
|
---|
219 | log_msg(LVL_DEBUG, "%s: tcp_tqueue_ack_received(%p)", conn->name,
|
---|
220 | conn);
|
---|
221 |
|
---|
222 | cur = conn->retransmit.list.head.next;
|
---|
223 |
|
---|
224 | while (cur != &conn->retransmit.list.head) {
|
---|
225 | next = cur->next;
|
---|
226 |
|
---|
227 | tcp_tqueue_entry_t *tqe = list_get_instance(cur,
|
---|
228 | tcp_tqueue_entry_t, link);
|
---|
229 |
|
---|
230 | if (seq_no_segment_acked(conn, tqe->seg, conn->snd_una)) {
|
---|
231 | /* Remove acknowledged segment */
|
---|
232 | list_remove(cur);
|
---|
233 |
|
---|
234 | if ((tqe->seg->ctrl & CTL_FIN) != 0) {
|
---|
235 | /* Our FIN has been acked */
|
---|
236 | conn->fin_is_acked = true;
|
---|
237 | }
|
---|
238 |
|
---|
239 | tcp_segment_delete(tqe->seg);
|
---|
240 | free(tqe);
|
---|
241 |
|
---|
242 | /* Reset retransmission timer */
|
---|
243 | tcp_tqueue_timer_set(conn);
|
---|
244 | }
|
---|
245 |
|
---|
246 | cur = next;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /* Clear retransmission timer if the queue is empty. */
|
---|
250 | if (list_empty(&conn->retransmit.list))
|
---|
251 | tcp_tqueue_timer_clear(conn);
|
---|
252 |
|
---|
253 | /* Possibly transmit more data */
|
---|
254 | tcp_tqueue_new_data(conn);
|
---|
255 | }
|
---|
256 |
|
---|
257 | void tcp_conn_transmit_segment(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
258 | {
|
---|
259 | log_msg(LVL_DEBUG, "%s: tcp_conn_transmit_segment(%p, %p)",
|
---|
260 | conn->name, conn, seg);
|
---|
261 |
|
---|
262 | seg->wnd = conn->rcv_wnd;
|
---|
263 |
|
---|
264 | if ((seg->ctrl & CTL_ACK) != 0)
|
---|
265 | seg->ack = conn->rcv_nxt;
|
---|
266 | else
|
---|
267 | seg->ack = 0;
|
---|
268 |
|
---|
269 | tcp_transmit_segment(&conn->ident, seg);
|
---|
270 | }
|
---|
271 |
|
---|
272 | void tcp_transmit_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
273 | {
|
---|
274 | log_msg(LVL_DEBUG, "tcp_transmit_segment(f:(%x,%u),l:(%x,%u), %p)",
|
---|
275 | sp->foreign.addr.ipv4, sp->foreign.port,
|
---|
276 | sp->local.addr.ipv4, sp->local.port, seg);
|
---|
277 |
|
---|
278 | log_msg(LVL_DEBUG, "SEG.SEQ=%" PRIu32 ", SEG.WND=%" PRIu32,
|
---|
279 | seg->seq, seg->wnd);
|
---|
280 |
|
---|
281 | tcp_segment_dump(seg);
|
---|
282 | /*
|
---|
283 | tcp_pdu_prepare(conn, seg, &data, &len);
|
---|
284 | tcp_pdu_transmit(data, len);
|
---|
285 | */
|
---|
286 | // tcp_rqueue_bounce_seg(sp, seg);
|
---|
287 | // tcp_ncsim_bounce_seg(sp, seg);
|
---|
288 |
|
---|
289 | tcp_pdu_t *pdu;
|
---|
290 |
|
---|
291 | if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
|
---|
292 | log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
|
---|
293 | return;
|
---|
294 | }
|
---|
295 |
|
---|
296 | tcp_transmit_pdu(pdu);
|
---|
297 | tcp_pdu_delete(pdu);
|
---|
298 | }
|
---|
299 |
|
---|
300 | static void retransmit_timeout_func(void *arg)
|
---|
301 | {
|
---|
302 | tcp_conn_t *conn = (tcp_conn_t *) arg;
|
---|
303 | tcp_tqueue_entry_t *tqe;
|
---|
304 | tcp_segment_t *rt_seg;
|
---|
305 | link_t *link;
|
---|
306 |
|
---|
307 | log_msg(LVL_DEBUG, "### %s: retransmit_timeout_func(%p)", conn->name, conn);
|
---|
308 |
|
---|
309 | if (conn->cstate == st_closed) {
|
---|
310 | log_msg(LVL_DEBUG, "Connection already closed.");
|
---|
311 | return;
|
---|
312 | }
|
---|
313 |
|
---|
314 | link = list_first(&conn->retransmit.list);
|
---|
315 | if (link == NULL) {
|
---|
316 | log_msg(LVL_DEBUG, "Nothing to retransmit");
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | tqe = list_get_instance(link, tcp_tqueue_entry_t, link);
|
---|
321 |
|
---|
322 | rt_seg = tcp_segment_dup(tqe->seg);
|
---|
323 | if (rt_seg == NULL) {
|
---|
324 | log_msg(LVL_ERROR, "Memory allocation failed.");
|
---|
325 | /* XXX Handle properly */
|
---|
326 | return;
|
---|
327 | }
|
---|
328 |
|
---|
329 | log_msg(LVL_DEBUG, "### %s: retransmitting segment", conn->name);
|
---|
330 | tcp_conn_transmit_segment(tqe->conn, rt_seg);
|
---|
331 |
|
---|
332 | /* Reset retransmission timer */
|
---|
333 | tcp_tqueue_timer_set(tqe->conn);
|
---|
334 | }
|
---|
335 |
|
---|
336 | /** Set or re-set retransmission timer */
|
---|
337 | static void tcp_tqueue_timer_set(tcp_conn_t *conn)
|
---|
338 | {
|
---|
339 | log_msg(LVL_DEBUG, "### %s: tcp_tqueue_timer_set()", conn->name);
|
---|
340 |
|
---|
341 | (void) retransmit_timeout_func;
|
---|
342 | fibril_timer_set(conn->retransmit.timer, RETRANSMIT_TIMEOUT,
|
---|
343 | retransmit_timeout_func, (void *) conn);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Clear retransmission timer */
|
---|
347 | static void tcp_tqueue_timer_clear(tcp_conn_t *conn)
|
---|
348 | {
|
---|
349 | log_msg(LVL_DEBUG, "### %s: tcp_tqueue_timer_clear()", conn->name);
|
---|
350 |
|
---|
351 | fibril_timer_clear(conn->retransmit.timer);
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * @}
|
---|
356 | */
|
---|