blob: b6c348288ece3811fe644de32759b312c4850b2e [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/tcp/tcp.h>
17#include <vnet/session/session.h>
18#include <vnet/fib/fib.h>
19#include <math.h>
20
21tcp_main_t tcp_main;
22
23static u32
Florin Corase69f4952017-03-07 10:06:24 -080024tcp_connection_bind (u32 session_index, ip46_address_t * ip,
Dave Barach68b0fb02017-02-28 15:15:56 -050025 u16 port_host_byte_order, u8 is_ip4)
26{
27 tcp_main_t *tm = &tcp_main;
28 tcp_connection_t *listener;
29
30 pool_get (tm->listener_pool, listener);
31 memset (listener, 0, sizeof (*listener));
32
33 listener->c_c_index = listener - tm->listener_pool;
34 listener->c_lcl_port = clib_host_to_net_u16 (port_host_byte_order);
35
36 if (is_ip4)
37 listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32;
38 else
39 clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t));
40
41 listener->c_s_index = session_index;
42 listener->c_proto = SESSION_TYPE_IP4_TCP;
43 listener->state = TCP_STATE_LISTEN;
44 listener->c_is_ip4 = 1;
45
Florin Corase69f4952017-03-07 10:06:24 -080046 tcp_connection_timers_init (listener);
47
48 TCP_EVT_DBG (TCP_EVT_BIND, listener);
49
Dave Barach68b0fb02017-02-28 15:15:56 -050050 return listener->c_c_index;
51}
52
53u32
Florin Corase69f4952017-03-07 10:06:24 -080054tcp_session_bind_ip4 (u32 session_index, ip46_address_t * ip,
55 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050056{
Florin Corase69f4952017-03-07 10:06:24 -080057 return tcp_connection_bind (session_index, ip, port_host_byte_order, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -050058}
59
60u32
Florin Corase69f4952017-03-07 10:06:24 -080061tcp_session_bind_ip6 (u32 session_index, ip46_address_t * ip,
62 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050063{
Florin Corase69f4952017-03-07 10:06:24 -080064 return tcp_connection_bind (session_index, ip, port_host_byte_order, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -050065
66}
67
68static void
Florin Corase69f4952017-03-07 10:06:24 -080069tcp_connection_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050070{
71 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corase69f4952017-03-07 10:06:24 -080072 TCP_EVT_DBG (TCP_EVT_UNBIND,
73 pool_elt_at_index (tm->listener_pool, listener_index));
Dave Barach68b0fb02017-02-28 15:15:56 -050074 pool_put_index (tm->listener_pool, listener_index);
75}
76
77u32
Florin Corase69f4952017-03-07 10:06:24 -080078tcp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050079{
Florin Corase69f4952017-03-07 10:06:24 -080080 tcp_connection_unbind (listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050081 return 0;
82}
83
84transport_connection_t *
85tcp_session_get_listener (u32 listener_index)
86{
87 tcp_main_t *tm = vnet_get_tcp_main ();
88 tcp_connection_t *tc;
89 tc = pool_elt_at_index (tm->listener_pool, listener_index);
90 return &tc->connection;
91}
92
93/**
94 * Cleans up connection state.
95 *
96 * No notifications.
97 */
98void
99tcp_connection_cleanup (tcp_connection_t * tc)
100{
101 tcp_main_t *tm = &tcp_main;
102 u32 tepi;
103 transport_endpoint_t *tep;
104
105 /* Cleanup local endpoint if this was an active connect */
106 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
107 tc->c_lcl_port);
108
109 /*XXX lock */
110 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
111 {
112 tep = pool_elt_at_index (tm->local_endpoints, tepi);
113 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
114 pool_put (tm->local_endpoints, tep);
115 }
116
117 /* Make sure all timers are cleared */
118 tcp_connection_timers_reset (tc);
119
120 /* Check if half-open */
121 if (tc->state == TCP_STATE_SYN_SENT)
122 pool_put (tm->half_open_connections, tc);
123 else
124 pool_put (tm->connections[tc->c_thread_index], tc);
125}
126
127/**
128 * Connection removal.
129 *
130 * This should be called only once connection enters CLOSED state. Note
131 * that it notifies the session of the removal event, so if the goal is to
132 * just remove the connection, call tcp_connection_cleanup instead.
133 */
134void
135tcp_connection_del (tcp_connection_t * tc)
136{
Florin Corase69f4952017-03-07 10:06:24 -0800137 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500138 stream_session_delete_notify (&tc->connection);
139 tcp_connection_cleanup (tc);
140}
141
Florin Corasd79b41e2017-03-04 05:37:52 -0800142/** Notify session that connection has been reset.
143 *
144 * Switch state to closed and wait for session to call cleanup.
145 */
146void
147tcp_connection_reset (tcp_connection_t * tc)
148{
149 if (tc->state == TCP_STATE_CLOSED)
150 return;
151
152 tc->state = TCP_STATE_CLOSED;
153 stream_session_reset_notify (&tc->connection);
154}
155
Dave Barach68b0fb02017-02-28 15:15:56 -0500156/**
157 * Begin connection closing procedure.
158 *
159 * If at the end the connection is not in CLOSED state, it is not removed.
160 * Instead, we rely on on TCP to advance through state machine to either
161 * 1) LAST_ACK (passive close) whereby when the last ACK is received
162 * tcp_connection_del is called. This notifies session of the delete and
163 * calls cleanup.
164 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
165 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800166 *
167 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500168 */
169void
170tcp_connection_close (tcp_connection_t * tc)
171{
Florin Corase69f4952017-03-07 10:06:24 -0800172 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
173
Dave Barach68b0fb02017-02-28 15:15:56 -0500174 /* Send FIN if needed */
175 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD
176 || tc->state == TCP_STATE_CLOSE_WAIT)
177 tcp_send_fin (tc);
178
179 /* Switch state */
180 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD)
181 tc->state = TCP_STATE_FIN_WAIT_1;
182 else if (tc->state == TCP_STATE_SYN_SENT)
183 tc->state = TCP_STATE_CLOSED;
184 else if (tc->state == TCP_STATE_CLOSE_WAIT)
185 tc->state = TCP_STATE_LAST_ACK;
186
Florin Corasd79b41e2017-03-04 05:37:52 -0800187 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
188 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
189 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500190 tcp_connection_del (tc);
191}
192
193void
194tcp_session_close (u32 conn_index, u32 thread_index)
195{
196 tcp_connection_t *tc;
197 tc = tcp_connection_get (conn_index, thread_index);
198 tcp_connection_close (tc);
199}
200
201void
202tcp_session_cleanup (u32 conn_index, u32 thread_index)
203{
204 tcp_connection_t *tc;
205 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800206
207 /* Wait for the session tx events to clear */
208 tc->state = TCP_STATE_CLOSED;
209 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500210}
211
212void *
213ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
214{
215 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
216 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
217 ip_interface_address_t *ia = 0;
218
219 if (is_ip4)
220 {
221 /* *INDENT-OFF* */
222 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
223 ({
224 return ip_interface_address_get_address (lm4, ia);
225 }));
226 /* *INDENT-ON* */
227 }
228 else
229 {
230 /* *INDENT-OFF* */
231 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
232 ({
233 return ip_interface_address_get_address (lm6, ia);
234 }));
235 /* *INDENT-ON* */
236 }
237
238 return 0;
239}
240
Florin Corase04c2992017-03-01 08:17:34 -0800241#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500242/**
243 * Allocate local port and add if successful add entry to local endpoint
244 * table to mark the pair as used.
245 */
246u16
247tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip)
248{
Dave Barach68b0fb02017-02-28 15:15:56 -0500249 transport_endpoint_t *tep;
250 u32 time_now, tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800251 u16 min = 1024, max = 65535; /* XXX configurable ? */
252 int tries;
Dave Barach68b0fb02017-02-28 15:15:56 -0500253
254 tries = max - min;
255 time_now = tcp_time_now ();
256
257 /* Start at random point or max */
258 pool_get (tm->local_endpoints, tep);
259 clib_memcpy (&tep->ip, ip, sizeof (*ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500260
261 /* Search for first free slot */
Florin Corase04c2992017-03-01 08:17:34 -0800262 for (; tries >= 0; tries--)
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 {
Florin Corase04c2992017-03-01 08:17:34 -0800264 u16 port = 0;
265
266 /* Find a port in the specified range */
267 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500268 {
Florin Corase04c2992017-03-01 08:17:34 -0800269 port = random_u32 (&time_now) & PORT_MASK;
270 if (PREDICT_TRUE (port >= min && port < max))
271 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500272 }
273
Florin Corase04c2992017-03-01 08:17:34 -0800274 tep->port = port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500275
Florin Corase04c2992017-03-01 08:17:34 -0800276 /* Look it up */
277 tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip,
278 tep->port);
279 /* If not found, we're done */
280 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
281 {
282 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
283 tep - tm->local_endpoints);
284 return tep->port;
285 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500286 }
Florin Corase04c2992017-03-01 08:17:34 -0800287 /* No free ports */
Dave Barach68b0fb02017-02-28 15:15:56 -0500288 pool_put (tm->local_endpoints, tep);
289 return -1;
290}
291
292/**
293 * Initialize all connection timers as invalid
294 */
295void
296tcp_connection_timers_init (tcp_connection_t * tc)
297{
298 int i;
299
300 /* Set all to invalid */
301 for (i = 0; i < TCP_N_TIMERS; i++)
302 {
303 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
304 }
305
306 tc->rto = TCP_RTO_INIT;
307}
308
309/**
310 * Stop all connection timers
311 */
312void
313tcp_connection_timers_reset (tcp_connection_t * tc)
314{
315 int i;
316 for (i = 0; i < TCP_N_TIMERS; i++)
317 {
318 tcp_timer_reset (tc, i);
319 }
320}
321
322/** Initialize tcp connection variables
323 *
324 * Should be called after having received a msg from the peer, i.e., a SYN or
325 * a SYNACK, such that connection options have already been exchanged. */
326void
327tcp_connection_init_vars (tcp_connection_t * tc)
328{
329 tcp_connection_timers_init (tc);
330 tcp_set_snd_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700331 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500332 tcp_cc_init (tc);
333}
334
335int
336tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4)
337{
338 tcp_main_t *tm = vnet_get_tcp_main ();
339 tcp_connection_t *tc;
340 fib_prefix_t prefix;
341 u32 fei, sw_if_index;
342 ip46_address_t lcl_addr;
343 u16 lcl_port;
344
345 /*
346 * Find the local address and allocate port
347 */
348 memset (&lcl_addr, 0, sizeof (lcl_addr));
349
350 /* Find a FIB path to the destination */
351 clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr));
352 prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
353 prefix.fp_len = is_ip4 ? 32 : 128;
354
355 fei = fib_table_lookup (0, &prefix);
356
357 /* Couldn't find route to destination. Bail out. */
358 if (fei == FIB_NODE_INDEX_INVALID)
359 return -1;
360
361 sw_if_index = fib_entry_get_resolving_interface (fei);
362
363 if (sw_if_index == (u32) ~ 0)
364 return -1;
365
366 if (is_ip4)
367 {
368 ip4_address_t *ip4;
369 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
370 lcl_addr.ip4.as_u32 = ip4->as_u32;
371 }
372 else
373 {
374 ip6_address_t *ip6;
375 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
376 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
377 }
378
379 /* Allocate source port */
380 lcl_port = tcp_allocate_local_port (tm, &lcl_addr);
381 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800382 {
383 clib_warning ("Failed to allocate src port");
384 return -1;
385 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500386
387 /*
388 * Create connection and send SYN
389 */
390
391 pool_get (tm->half_open_connections, tc);
392 memset (tc, 0, sizeof (*tc));
393
394 clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t));
395 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
396 tc->c_rmt_port = clib_host_to_net_u16 (rmt_port);
397 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
398 tc->c_c_index = tc - tm->half_open_connections;
399 tc->c_is_ip4 = is_ip4;
400
401 /* The other connection vars will be initialized after SYN ACK */
402 tcp_connection_timers_init (tc);
403
404 tcp_send_syn (tc);
405
406 tc->state = TCP_STATE_SYN_SENT;
407
Florin Corase69f4952017-03-07 10:06:24 -0800408 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
409
Dave Barach68b0fb02017-02-28 15:15:56 -0500410 return tc->c_c_index;
411}
412
413int
414tcp_session_open_ip4 (ip46_address_t * addr, u16 port)
415{
416 return tcp_connection_open (addr, port, 1);
417}
418
419int
420tcp_session_open_ip6 (ip46_address_t * addr, u16 port)
421{
422 return tcp_connection_open (addr, port, 0);
423}
424
Florin Corase69f4952017-03-07 10:06:24 -0800425const char *tcp_dbg_evt_str[] = {
426#define _(sym, str) str,
427 foreach_tcp_dbg_evt
428#undef _
429};
430
431const char *tcp_fsm_states[] = {
432#define _(sym, str) str,
433 foreach_tcp_fsm_state
434#undef _
435};
436
Dave Barach68b0fb02017-02-28 15:15:56 -0500437u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800438format_tcp_state (u8 * s, va_list * args)
439{
440 tcp_state_t *state = va_arg (*args, tcp_state_t *);
441
442 if (*state < TCP_N_STATES)
443 s = format (s, "%s", tcp_fsm_states[*state]);
444 else
445 s = format (s, "UNKNOWN");
446
447 return s;
448}
449
450const char *tcp_conn_timers[] = {
451#define _(sym, str) str,
452 foreach_tcp_timer
453#undef _
454};
455
456u8 *
457format_tcp_timers (u8 * s, va_list * args)
458{
459 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
460 int i, last = 0;
461
462 for (i = 0; i < TCP_N_TIMERS; i++)
463 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
464 last = i;
465
466 s = format (s, "[");
467 for (i = 0; i < last; i++)
468 {
469 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
470 s = format (s, "%s,", tcp_conn_timers[i]);
471 }
472
473 if (last > 0)
474 s = format (s, "%s]", tcp_conn_timers[i]);
475 else
476 s = format (s, "]");
477
478 return s;
479}
480
481u8 *
482format_tcp_connection (u8 * s, va_list * args)
483{
484 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
485
486 if (tc->c_is_ip4)
487 {
488 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
489 format_ip4_address, &tc->c_lcl_ip4,
490 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
491 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
492 }
493 else
494 {
495 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
496 format_ip6_address, &tc->c_lcl_ip6,
497 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
498 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
499 }
500
501 return s;
502}
503
504u8 *
505format_tcp_connection_verbose (u8 * s, va_list * args)
506{
507 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
508 s = format (s, "%U %U %U", format_tcp_connection, tc, format_tcp_state,
509 &tc->state, format_tcp_timers, tc);
510 return s;
511}
512
513u8 *
514format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500515{
516 u32 tci = va_arg (*args, u32);
517 u32 thread_index = va_arg (*args, u32);
518 tcp_connection_t *tc;
519
520 tc = tcp_connection_get (tci, thread_index);
Florin Corase69f4952017-03-07 10:06:24 -0800521 return format (s, "%U", format_tcp_connection, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500522}
523
524u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800525format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500526{
527 u32 tci = va_arg (*args, u32);
528 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corase69f4952017-03-07 10:06:24 -0800529 return format (s, "%U", format_tcp_connection, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500530}
531
532u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800533format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500534{
535 u32 tci = va_arg (*args, u32);
536 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corase69f4952017-03-07 10:06:24 -0800537 return format (s, "%U", format_tcp_connection, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500538}
539
540transport_connection_t *
541tcp_session_get_transport (u32 conn_index, u32 thread_index)
542{
543 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
544 return &tc->connection;
545}
546
547transport_connection_t *
548tcp_half_open_session_get_transport (u32 conn_index)
549{
550 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
551 return &tc->connection;
552}
553
554u16
555tcp_session_send_mss (transport_connection_t * trans_conn)
556{
557 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
558 return tc->snd_mss;
559}
560
Florin Coras6792ec02017-03-13 03:49:51 -0700561/**
562 * Compute tx window session is allowed to fill.
563 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500564u32
565tcp_session_send_space (transport_connection_t * trans_conn)
566{
Florin Coras6792ec02017-03-13 03:49:51 -0700567 u32 snd_space;
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -0700569
570 /* If we haven't gotten dupacks or if we did and have gotten sacked bytes
571 * then we can still send */
572 if (PREDICT_TRUE (tcp_in_fastrecovery (tc) == 0
573 && (tc->rcv_dupacks == 0
574 || tc->sack_sb.last_sacked_bytes)))
575 {
576 snd_space = tcp_available_snd_space (tc);
577
578 /* If we can't write at least a segment, don't try at all */
579 if (snd_space < tc->snd_mss)
580 return 0;
Florin Coras3e350af2017-03-30 02:54:28 -0700581
582 /* round down to mss multiple */
583 return snd_space - (snd_space % tc->snd_mss);
Florin Coras6792ec02017-03-13 03:49:51 -0700584 }
585
586 /* If in fast recovery, send 1 SMSS if wnd allows */
587 if (tcp_in_fastrecovery (tc) && tcp_available_snd_space (tc)
588 && tcp_fastrecovery_sent_1_smss (tc))
589 {
590 tcp_fastrecovery_1_smss_on (tc);
591 return tc->snd_mss;
592 }
593
594 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500595}
596
597u32
Florin Corasd79b41e2017-03-04 05:37:52 -0800598tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -0500599{
600 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -0700601
602 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
603
604 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -0800605 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -0500606}
607
608/* *INDENT-OFF* */
609const static transport_proto_vft_t tcp4_proto = {
610 .bind = tcp_session_bind_ip4,
Florin Corase69f4952017-03-07 10:06:24 -0800611 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500612 .push_header = tcp_push_header,
613 .get_connection = tcp_session_get_transport,
614 .get_listener = tcp_session_get_listener,
615 .get_half_open = tcp_half_open_session_get_transport,
616 .open = tcp_session_open_ip4,
617 .close = tcp_session_close,
618 .cleanup = tcp_session_cleanup,
619 .send_mss = tcp_session_send_mss,
620 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -0800621 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -0800622 .format_connection = format_tcp_session,
623 .format_listener = format_tcp_listener_session,
624 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -0500625};
626
627const static transport_proto_vft_t tcp6_proto = {
628 .bind = tcp_session_bind_ip6,
Florin Corase69f4952017-03-07 10:06:24 -0800629 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500630 .push_header = tcp_push_header,
631 .get_connection = tcp_session_get_transport,
632 .get_listener = tcp_session_get_listener,
633 .get_half_open = tcp_half_open_session_get_transport,
634 .open = tcp_session_open_ip6,
635 .close = tcp_session_close,
636 .cleanup = tcp_session_cleanup,
637 .send_mss = tcp_session_send_mss,
638 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -0800639 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -0800640 .format_connection = format_tcp_session,
641 .format_listener = format_tcp_listener_session,
642 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -0500643};
644/* *INDENT-ON* */
645
646void
647tcp_timer_keep_handler (u32 conn_index)
648{
Damjan Marion586afd72017-04-05 19:18:20 +0200649 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500650 tcp_connection_t *tc;
651
Damjan Marion586afd72017-04-05 19:18:20 +0200652 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500653 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
654
655 tcp_connection_close (tc);
656}
657
658void
659tcp_timer_establish_handler (u32 conn_index)
660{
661 tcp_connection_t *tc;
662 u8 sst;
663
664 tc = tcp_half_open_connection_get (conn_index);
665 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
666
667 ASSERT (tc->state == TCP_STATE_SYN_SENT);
668
669 sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
670 stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ );
671
672 tcp_connection_cleanup (tc);
673}
674
675void
Florin Corasd79b41e2017-03-04 05:37:52 -0800676tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500677{
Damjan Marion586afd72017-04-05 19:18:20 +0200678 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500679 tcp_connection_t *tc;
680
Damjan Marion586afd72017-04-05 19:18:20 +0200681 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800682 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
683
684 /* Session didn't come back with a close(). Send FIN either way
685 * and switch to LAST_ACK. */
686 if (tc->state == TCP_STATE_CLOSE_WAIT)
687 {
688 if (tc->flags & TCP_CONN_FINSNT)
689 {
690 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
691 }
692
693 tcp_send_fin (tc);
694 tc->state = TCP_STATE_LAST_ACK;
695
696 /* Make sure we don't wait in LAST ACK forever */
697 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
698
699 /* Don't delete the connection yet */
700 return;
701 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500702
703 tcp_connection_del (tc);
704}
705
706/* *INDENT-OFF* */
707static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
708{
709 tcp_timer_retransmit_handler,
710 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -0700711 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -0500712 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -0800713 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -0500714 tcp_timer_retransmit_syn_handler,
715 tcp_timer_establish_handler
716};
717/* *INDENT-ON* */
718
719static void
720tcp_expired_timers_dispatch (u32 * expired_timers)
721{
722 int i;
723 u32 connection_index, timer_id;
724
725 for (i = 0; i < vec_len (expired_timers); i++)
726 {
727 /* Get session index and timer id */
728 connection_index = expired_timers[i] & 0x0FFFFFFF;
729 timer_id = expired_timers[i] >> 28;
730
Florin Corase69f4952017-03-07 10:06:24 -0800731 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
732
Dave Barach68b0fb02017-02-28 15:15:56 -0500733 /* Handle expiration */
734 (*timer_expiration_handlers[timer_id]) (connection_index);
735 }
736}
737
738void
739tcp_initialize_timer_wheels (tcp_main_t * tm)
740{
741 tw_timer_wheel_16t_2w_512sl_t *tw;
742 vec_foreach (tw, tm->timer_wheels)
743 {
744 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
745 100e-3 /* timer period 100ms */ , ~0);
746 tw->last_run_time = vlib_time_now (tm->vlib_main);
747 }
748}
749
750clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -0800751tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -0500752{
Dave Barach68b0fb02017-02-28 15:15:56 -0500753 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -0800754 ip_protocol_info_t *pi;
755 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500756 vlib_thread_main_t *vtm = vlib_get_thread_main ();
757 clib_error_t *error = 0;
758 u32 num_threads;
759
Dave Barach68b0fb02017-02-28 15:15:56 -0500760 if ((error = vlib_call_init_function (vm, ip_main_init)))
761 return error;
762 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
763 return error;
764 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
765 return error;
766
767 /*
768 * Registrations
769 */
770
771 /* Register with IP */
772 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
773 if (pi == 0)
774 return clib_error_return (0, "TCP protocol info AWOL");
775 pi->format_header = format_tcp_header;
776 pi->unformat_pg_edit = unformat_pg_tcp_header;
777
778 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
779
780 /* Register as transport with URI */
781 session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto);
782 session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto);
783
784 /*
785 * Initialize data structures
786 */
787
788 num_threads = 1 /* main thread */ + vtm->n_threads;
789 vec_validate (tm->connections, num_threads - 1);
790
791 /* Initialize per worker thread tx buffers (used for control messages) */
792 vec_validate (tm->tx_buffers, num_threads - 1);
793
794 /* Initialize timer wheels */
795 vec_validate (tm->timer_wheels, num_threads - 1);
796 tcp_initialize_timer_wheels (tm);
797
Florin Coras6792ec02017-03-13 03:49:51 -0700798// vec_validate (tm->delack_connections, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500799
800 /* Initialize clocks per tick for TCP timestamp. Used to compute
801 * monotonically increasing timestamps. */
802 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
803 / TCP_TSTAMP_RESOLUTION;
804
805 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
806 200000 /* $$$$ config parameter nbuckets */ ,
807 (64 << 20) /*$$$ config parameter table size */ );
808
809 return error;
810}
811
Florin Corasa0b34a72017-03-07 01:20:52 -0800812clib_error_t *
813vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
814{
815 if (is_en)
816 {
817 if (tcp_main.is_enabled)
818 return 0;
819
820 return tcp_main_enable (vm);
821 }
822 else
823 {
824 tcp_main.is_enabled = 0;
825 }
826
827 return 0;
828}
829
830clib_error_t *
831tcp_init (vlib_main_t * vm)
832{
833 tcp_main_t *tm = vnet_get_tcp_main ();
834
835 tm->vlib_main = vm;
836 tm->vnet_main = vnet_get_main ();
837 tm->is_enabled = 0;
838
839 return 0;
840}
841
Dave Barach68b0fb02017-02-28 15:15:56 -0500842VLIB_INIT_FUNCTION (tcp_init);
843
844/*
845 * fd.io coding-style-patch-verification: ON
846 *
847 * Local Variables:
848 * eval: (c-set-style "gnu")
849 * End:
850 */