blob: 4e85eb3fc93bf3d7e5afb815c56f8272de99d346 [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>
Florin Corasf6359c82017-06-19 12:26:09 -040019#include <vnet/dpo/load_balance.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <math.h>
21
22tcp_main_t tcp_main;
23
24static u32
Florin Corase69f4952017-03-07 10:06:24 -080025tcp_connection_bind (u32 session_index, ip46_address_t * ip,
Dave Barach68b0fb02017-02-28 15:15:56 -050026 u16 port_host_byte_order, u8 is_ip4)
27{
28 tcp_main_t *tm = &tcp_main;
29 tcp_connection_t *listener;
30
31 pool_get (tm->listener_pool, listener);
32 memset (listener, 0, sizeof (*listener));
33
34 listener->c_c_index = listener - tm->listener_pool;
35 listener->c_lcl_port = clib_host_to_net_u16 (port_host_byte_order);
36
37 if (is_ip4)
Florin Coras6cf30ad2017-04-04 23:08:23 -070038 {
39 listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32;
40 listener->c_is_ip4 = 1;
41 listener->c_proto = SESSION_TYPE_IP4_TCP;
42 }
Dave Barach68b0fb02017-02-28 15:15:56 -050043 else
Florin Coras6cf30ad2017-04-04 23:08:23 -070044 {
45 clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t));
46 listener->c_proto = SESSION_TYPE_IP6_TCP;
47 }
Dave Barach68b0fb02017-02-28 15:15:56 -050048
49 listener->c_s_index = session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050050 listener->state = TCP_STATE_LISTEN;
Dave Barach68b0fb02017-02-28 15:15:56 -050051
Florin Corase69f4952017-03-07 10:06:24 -080052 tcp_connection_timers_init (listener);
53
54 TCP_EVT_DBG (TCP_EVT_BIND, listener);
55
Dave Barach68b0fb02017-02-28 15:15:56 -050056 return listener->c_c_index;
57}
58
59u32
Florin Corase69f4952017-03-07 10:06:24 -080060tcp_session_bind_ip4 (u32 session_index, ip46_address_t * ip,
61 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050062{
Florin Corase69f4952017-03-07 10:06:24 -080063 return tcp_connection_bind (session_index, ip, port_host_byte_order, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -050064}
65
66u32
Florin Corase69f4952017-03-07 10:06:24 -080067tcp_session_bind_ip6 (u32 session_index, ip46_address_t * ip,
68 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050069{
Florin Corase69f4952017-03-07 10:06:24 -080070 return tcp_connection_bind (session_index, ip, port_host_byte_order, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -050071}
72
73static void
Florin Corase69f4952017-03-07 10:06:24 -080074tcp_connection_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050075{
76 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corase69f4952017-03-07 10:06:24 -080077 TCP_EVT_DBG (TCP_EVT_UNBIND,
78 pool_elt_at_index (tm->listener_pool, listener_index));
Dave Barach68b0fb02017-02-28 15:15:56 -050079 pool_put_index (tm->listener_pool, listener_index);
80}
81
82u32
Florin Corase69f4952017-03-07 10:06:24 -080083tcp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050084{
Florin Corase69f4952017-03-07 10:06:24 -080085 tcp_connection_unbind (listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050086 return 0;
87}
88
89transport_connection_t *
90tcp_session_get_listener (u32 listener_index)
91{
92 tcp_main_t *tm = vnet_get_tcp_main ();
93 tcp_connection_t *tc;
94 tc = pool_elt_at_index (tm->listener_pool, listener_index);
95 return &tc->connection;
96}
97
98/**
99 * Cleans up connection state.
100 *
101 * No notifications.
102 */
103void
104tcp_connection_cleanup (tcp_connection_t * tc)
105{
106 tcp_main_t *tm = &tcp_main;
107 u32 tepi;
108 transport_endpoint_t *tep;
109
110 /* Cleanup local endpoint if this was an active connect */
111 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
112 tc->c_lcl_port);
113
114 /*XXX lock */
115 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
116 {
117 tep = pool_elt_at_index (tm->local_endpoints, tepi);
118 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
119 pool_put (tm->local_endpoints, tep);
120 }
121
122 /* Make sure all timers are cleared */
123 tcp_connection_timers_reset (tc);
124
125 /* Check if half-open */
126 if (tc->state == TCP_STATE_SYN_SENT)
127 pool_put (tm->half_open_connections, tc);
128 else
129 pool_put (tm->connections[tc->c_thread_index], tc);
130}
131
132/**
133 * Connection removal.
134 *
135 * This should be called only once connection enters CLOSED state. Note
136 * that it notifies the session of the removal event, so if the goal is to
137 * just remove the connection, call tcp_connection_cleanup instead.
138 */
139void
140tcp_connection_del (tcp_connection_t * tc)
141{
Florin Corase69f4952017-03-07 10:06:24 -0800142 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500143 stream_session_delete_notify (&tc->connection);
144 tcp_connection_cleanup (tc);
145}
146
Florin Corasd79b41e2017-03-04 05:37:52 -0800147/** Notify session that connection has been reset.
148 *
149 * Switch state to closed and wait for session to call cleanup.
150 */
151void
152tcp_connection_reset (tcp_connection_t * tc)
153{
Florin Coras11c05492017-05-10 12:29:14 -0700154 switch (tc->state)
155 {
156 case TCP_STATE_SYN_RCVD:
157 /* Cleanup everything. App wasn't notified yet */
158 stream_session_delete_notify (&tc->connection);
159 tcp_connection_cleanup (tc);
160 break;
161 case TCP_STATE_SYN_SENT:
162 case TCP_STATE_ESTABLISHED:
163 case TCP_STATE_CLOSE_WAIT:
164 case TCP_STATE_FIN_WAIT_1:
165 case TCP_STATE_FIN_WAIT_2:
166 case TCP_STATE_CLOSING:
167 tc->state = TCP_STATE_CLOSED;
Florin Corasd79b41e2017-03-04 05:37:52 -0800168
Florin Coras11c05492017-05-10 12:29:14 -0700169 /* Make sure all timers are cleared */
170 tcp_connection_timers_reset (tc);
Florin Corasdb84e572017-05-09 18:54:52 -0700171
Florin Coras11c05492017-05-10 12:29:14 -0700172 stream_session_reset_notify (&tc->connection);
173 break;
174 case TCP_STATE_CLOSED:
175 return;
176 }
Florin Corasdb84e572017-05-09 18:54:52 -0700177
Florin Corasd79b41e2017-03-04 05:37:52 -0800178}
179
Dave Barach68b0fb02017-02-28 15:15:56 -0500180/**
181 * Begin connection closing procedure.
182 *
183 * If at the end the connection is not in CLOSED state, it is not removed.
184 * Instead, we rely on on TCP to advance through state machine to either
185 * 1) LAST_ACK (passive close) whereby when the last ACK is received
186 * tcp_connection_del is called. This notifies session of the delete and
187 * calls cleanup.
188 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
189 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800190 *
191 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500192 */
193void
194tcp_connection_close (tcp_connection_t * tc)
195{
Florin Corase69f4952017-03-07 10:06:24 -0800196 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
197
Dave Barach68b0fb02017-02-28 15:15:56 -0500198 /* Send FIN if needed */
Florin Coras93992a92017-05-24 18:03:56 -0700199 if (tc->state == TCP_STATE_ESTABLISHED
200 || tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_CLOSE_WAIT)
Dave Barach68b0fb02017-02-28 15:15:56 -0500201 tcp_send_fin (tc);
202
203 /* Switch state */
204 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD)
205 tc->state = TCP_STATE_FIN_WAIT_1;
206 else if (tc->state == TCP_STATE_SYN_SENT)
207 tc->state = TCP_STATE_CLOSED;
208 else if (tc->state == TCP_STATE_CLOSE_WAIT)
209 tc->state = TCP_STATE_LAST_ACK;
210
Florin Corasd79b41e2017-03-04 05:37:52 -0800211 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
212 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
213 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500214 tcp_connection_del (tc);
215}
216
217void
218tcp_session_close (u32 conn_index, u32 thread_index)
219{
220 tcp_connection_t *tc;
221 tc = tcp_connection_get (conn_index, thread_index);
222 tcp_connection_close (tc);
223}
224
225void
226tcp_session_cleanup (u32 conn_index, u32 thread_index)
227{
228 tcp_connection_t *tc;
229 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800230
231 /* Wait for the session tx events to clear */
232 tc->state = TCP_STATE_CLOSED;
233 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500234}
235
236void *
237ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
238{
239 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
240 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
241 ip_interface_address_t *ia = 0;
242
243 if (is_ip4)
244 {
245 /* *INDENT-OFF* */
246 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
247 ({
248 return ip_interface_address_get_address (lm4, ia);
249 }));
250 /* *INDENT-ON* */
251 }
252 else
253 {
254 /* *INDENT-OFF* */
255 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
256 ({
257 return ip_interface_address_get_address (lm6, ia);
258 }));
259 /* *INDENT-ON* */
260 }
261
262 return 0;
263}
264
Florin Corase04c2992017-03-01 08:17:34 -0800265#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500266/**
267 * Allocate local port and add if successful add entry to local endpoint
268 * table to mark the pair as used.
269 */
270u16
271tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip)
272{
Dave Barach68b0fb02017-02-28 15:15:56 -0500273 transport_endpoint_t *tep;
274 u32 time_now, tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800275 u16 min = 1024, max = 65535; /* XXX configurable ? */
276 int tries;
Dave Barach68b0fb02017-02-28 15:15:56 -0500277
278 tries = max - min;
279 time_now = tcp_time_now ();
280
281 /* Start at random point or max */
282 pool_get (tm->local_endpoints, tep);
283 clib_memcpy (&tep->ip, ip, sizeof (*ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500284
285 /* Search for first free slot */
Florin Corase04c2992017-03-01 08:17:34 -0800286 for (; tries >= 0; tries--)
Dave Barach68b0fb02017-02-28 15:15:56 -0500287 {
Florin Corase04c2992017-03-01 08:17:34 -0800288 u16 port = 0;
289
290 /* Find a port in the specified range */
291 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 {
Florin Corase04c2992017-03-01 08:17:34 -0800293 port = random_u32 (&time_now) & PORT_MASK;
294 if (PREDICT_TRUE (port >= min && port < max))
295 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500296 }
297
Florin Corase04c2992017-03-01 08:17:34 -0800298 tep->port = port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500299
Florin Corase04c2992017-03-01 08:17:34 -0800300 /* Look it up */
301 tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip,
302 tep->port);
303 /* If not found, we're done */
304 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
305 {
306 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
307 tep - tm->local_endpoints);
308 return tep->port;
309 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500310 }
Florin Corase04c2992017-03-01 08:17:34 -0800311 /* No free ports */
Dave Barach68b0fb02017-02-28 15:15:56 -0500312 pool_put (tm->local_endpoints, tep);
313 return -1;
314}
315
316/**
317 * Initialize all connection timers as invalid
318 */
319void
320tcp_connection_timers_init (tcp_connection_t * tc)
321{
322 int i;
323
324 /* Set all to invalid */
325 for (i = 0; i < TCP_N_TIMERS; i++)
326 {
327 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
328 }
329
330 tc->rto = TCP_RTO_INIT;
331}
332
333/**
334 * Stop all connection timers
335 */
336void
337tcp_connection_timers_reset (tcp_connection_t * tc)
338{
339 int i;
340 for (i = 0; i < TCP_N_TIMERS; i++)
341 {
342 tcp_timer_reset (tc, i);
343 }
344}
345
Florin Corasf6359c82017-06-19 12:26:09 -0400346typedef struct ip4_tcp_hdr
347{
348 ip4_header_t ip;
349 tcp_header_t tcp;
350} ip4_tcp_hdr_t;
351
352typedef struct ip6_tcp_hdr
353{
354 ip6_header_t ip;
355 tcp_header_t tcp;
356} ip6_tcp_hdr_t;
357
358static void
359tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
360 dpo_id_t * result)
361{
362 const dpo_id_t *choice;
363 load_balance_t *lb;
364 int hash;
365
366 lb = load_balance_get (dpo->dpoi_index);
367 if (tc->c_is_ip4)
368 {
369 ip4_tcp_hdr_t hdr;
370 memset (&hdr, 0, sizeof (hdr));
371 hdr.ip.protocol = IP_PROTOCOL_TCP;
372 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
373 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
374 hdr.tcp.src_port = tc->c_lcl_port;
375 hdr.tcp.dst_port = tc->c_rmt_port;
376 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
377 }
378 else
379 {
380 ip6_tcp_hdr_t hdr;
381 memset (&hdr, 0, sizeof (hdr));
382 hdr.ip.protocol = IP_PROTOCOL_TCP;
383 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
384 sizeof (ip6_address_t));
385 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
386 sizeof (ip6_address_t));
387 hdr.tcp.src_port = tc->c_lcl_port;
388 hdr.tcp.dst_port = tc->c_rmt_port;
389 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
390 }
391 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
392 dpo_copy (result, choice);
393}
394
395fib_node_index_t
396tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
397{
398 fib_prefix_t prefix;
399
400 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
401 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
402 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
403 return fib_table_lookup (0, &prefix);
404}
405
406static int
407tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
408{
409 dpo_id_t choice = DPO_INVALID;
410 u32 output_node_index;
411 fib_entry_t *fe;
412
413 fe = fib_entry_get (tc->c_rmt_fei);
414 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
415 return -1;
416
417 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
418
419 output_node_index =
420 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
421 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
422 return 0;
423}
424
425/** Stack tcp connection on peer's fib entry.
426 *
427 * This ultimately populates the dpo the connection will use to send packets.
428 */
429static void
430tcp_connection_fib_attach (tcp_connection_t * tc)
431{
432 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
433
434 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
435
436 tcp_connection_stack_on_fib_entry (tc);
437}
438
Dave Barach68b0fb02017-02-28 15:15:56 -0500439/** Initialize tcp connection variables
440 *
441 * Should be called after having received a msg from the peer, i.e., a SYN or
442 * a SYNACK, such that connection options have already been exchanged. */
443void
444tcp_connection_init_vars (tcp_connection_t * tc)
445{
446 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700447 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700448 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500449 tcp_cc_init (tc);
Florin Corasf6359c82017-06-19 12:26:09 -0400450 tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500451}
452
453int
454tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4)
455{
456 tcp_main_t *tm = vnet_get_tcp_main ();
457 tcp_connection_t *tc;
458 fib_prefix_t prefix;
Florin Corasf6359c82017-06-19 12:26:09 -0400459 fib_node_index_t fei;
460 u32 sw_if_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500461 ip46_address_t lcl_addr;
462 u16 lcl_port;
463
464 /*
465 * Find the local address and allocate port
466 */
467 memset (&lcl_addr, 0, sizeof (lcl_addr));
468
469 /* Find a FIB path to the destination */
470 clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr));
471 prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
472 prefix.fp_len = is_ip4 ? 32 : 128;
473
474 fei = fib_table_lookup (0, &prefix);
475
476 /* Couldn't find route to destination. Bail out. */
477 if (fei == FIB_NODE_INDEX_INVALID)
478 return -1;
479
480 sw_if_index = fib_entry_get_resolving_interface (fei);
481
482 if (sw_if_index == (u32) ~ 0)
483 return -1;
484
485 if (is_ip4)
486 {
487 ip4_address_t *ip4;
488 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
489 lcl_addr.ip4.as_u32 = ip4->as_u32;
490 }
491 else
492 {
493 ip6_address_t *ip6;
494 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
495 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
496 }
497
498 /* Allocate source port */
499 lcl_port = tcp_allocate_local_port (tm, &lcl_addr);
500 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800501 {
502 clib_warning ("Failed to allocate src port");
503 return -1;
504 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500505
506 /*
507 * Create connection and send SYN
508 */
509
510 pool_get (tm->half_open_connections, tc);
511 memset (tc, 0, sizeof (*tc));
512
513 clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t));
514 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
515 tc->c_rmt_port = clib_host_to_net_u16 (rmt_port);
516 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
517 tc->c_c_index = tc - tm->half_open_connections;
518 tc->c_is_ip4 = is_ip4;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700519 tc->c_proto = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
Dave Barach68b0fb02017-02-28 15:15:56 -0500520
521 /* The other connection vars will be initialized after SYN ACK */
522 tcp_connection_timers_init (tc);
523
524 tcp_send_syn (tc);
525
526 tc->state = TCP_STATE_SYN_SENT;
527
Florin Corase69f4952017-03-07 10:06:24 -0800528 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
529
Dave Barach68b0fb02017-02-28 15:15:56 -0500530 return tc->c_c_index;
531}
532
533int
534tcp_session_open_ip4 (ip46_address_t * addr, u16 port)
535{
536 return tcp_connection_open (addr, port, 1);
537}
538
539int
540tcp_session_open_ip6 (ip46_address_t * addr, u16 port)
541{
542 return tcp_connection_open (addr, port, 0);
543}
544
Florin Corase69f4952017-03-07 10:06:24 -0800545const char *tcp_dbg_evt_str[] = {
546#define _(sym, str) str,
547 foreach_tcp_dbg_evt
548#undef _
549};
550
551const char *tcp_fsm_states[] = {
552#define _(sym, str) str,
553 foreach_tcp_fsm_state
554#undef _
555};
556
Dave Barach68b0fb02017-02-28 15:15:56 -0500557u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800558format_tcp_state (u8 * s, va_list * args)
559{
Florin Corasbb292f42017-05-19 09:49:19 -0700560 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800561
Florin Corasbb292f42017-05-19 09:49:19 -0700562 if (state < TCP_N_STATES)
563 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800564 else
Florin Corasbb292f42017-05-19 09:49:19 -0700565 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800566 return s;
567}
568
569const char *tcp_conn_timers[] = {
570#define _(sym, str) str,
571 foreach_tcp_timer
572#undef _
573};
574
575u8 *
576format_tcp_timers (u8 * s, va_list * args)
577{
578 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700579 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800580
581 for (i = 0; i < TCP_N_TIMERS; i++)
582 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
583 last = i;
584
585 s = format (s, "[");
586 for (i = 0; i < last; i++)
587 {
588 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
589 s = format (s, "%s,", tcp_conn_timers[i]);
590 }
591
Florin Coras93992a92017-05-24 18:03:56 -0700592 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800593 s = format (s, "%s]", tcp_conn_timers[i]);
594 else
595 s = format (s, "]");
596
597 return s;
598}
599
600u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700601format_tcp_congestion_status (u8 * s, va_list * args)
602{
603 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
604 if (tcp_in_recovery (tc))
605 s = format (s, "recovery");
606 else if (tcp_in_fastrecovery (tc))
607 s = format (s, "fastrecovery");
608 else
609 s = format (s, "none");
610 return s;
611}
612
613u8 *
614format_tcp_vars (u8 * s, va_list * args)
615{
616 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
617 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u\n",
618 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
619 tc->snd_una_max - tc->iss);
620 s = format (s, " rcv_nxt %u rcv_las %u\n",
621 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
622 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
623 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
624 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700625 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
626 tcp_flight_size (tc), tcp_available_snd_space (tc),
627 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700628 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
629 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700630 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
631 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u\n",
632 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
633 tc->rcv_dupacks);
Florin Corasbb292f42017-05-19 09:49:19 -0700634 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
635 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
636 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Florin Coras93992a92017-05-24 18:03:56 -0700637 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb);
Florin Corasbb292f42017-05-19 09:49:19 -0700638 if (vec_len (tc->snd_sacks))
639 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
640
641 return s;
642}
643
644u8 *
645format_tcp_connection_id (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800646{
647 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700648 if (!tc)
649 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800650 if (tc->c_is_ip4)
651 {
652 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
653 format_ip4_address, &tc->c_lcl_ip4,
654 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
655 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
656 }
657 else
658 {
659 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
660 format_ip6_address, &tc->c_lcl_ip6,
661 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
662 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
663 }
664
665 return s;
666}
667
668u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700669format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800670{
671 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700672 u32 verbose = va_arg (*args, u32);
673
674 s = format (s, "%-50U", format_tcp_connection_id, tc);
675 if (verbose)
676 {
677 s = format (s, "%-15U", format_tcp_state, tc->state);
678 if (verbose > 1)
679 s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc);
680 }
Florin Corase69f4952017-03-07 10:06:24 -0800681 return s;
682}
683
684u8 *
685format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500686{
687 u32 tci = va_arg (*args, u32);
688 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700689 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500690 tcp_connection_t *tc;
691
692 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700693 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700694 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700695 else
Florin Coras93992a92017-05-24 18:03:56 -0700696 s = format (s, "empty");
697 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500698}
699
700u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800701format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500702{
703 u32 tci = va_arg (*args, u32);
704 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700705 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500706}
707
708u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800709format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500710{
711 u32 tci = va_arg (*args, u32);
712 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700713 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500714}
715
Florin Coras06d11012017-05-17 14:21:51 -0700716u8 *
717format_tcp_sacks (u8 * s, va_list * args)
718{
719 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
720 sack_block_t *sacks = tc->snd_sacks;
721 sack_block_t *block;
722 vec_foreach (block, sacks)
723 {
724 s = format (s, " start %u end %u\n", block->start - tc->irs,
725 block->end - tc->irs);
726 }
727 return s;
728}
729
730u8 *
731format_tcp_sack_hole (u8 * s, va_list * args)
732{
733 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
734 s = format (s, "[%u, %u]", hole->start, hole->end);
735 return s;
736}
737
738u8 *
739format_tcp_scoreboard (u8 * s, va_list * args)
740{
741 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
742 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -0700743 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
744 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
745 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
746 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
747 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
748 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
749
Florin Coras06d11012017-05-17 14:21:51 -0700750 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -0700751 if (hole)
752 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
753
Florin Coras06d11012017-05-17 14:21:51 -0700754 while (hole)
755 {
756 s = format (s, "%U", format_tcp_sack_hole, hole);
757 hole = scoreboard_next_hole (sb, hole);
758 }
759 return s;
760}
761
Dave Barach68b0fb02017-02-28 15:15:56 -0500762transport_connection_t *
763tcp_session_get_transport (u32 conn_index, u32 thread_index)
764{
765 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
766 return &tc->connection;
767}
768
769transport_connection_t *
770tcp_half_open_session_get_transport (u32 conn_index)
771{
772 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
773 return &tc->connection;
774}
775
Florin Corasc8343412017-05-04 14:25:50 -0700776/**
777 * Compute maximum segment size for session layer.
778 *
779 * Since the result needs to be the actual data length, it first computes
780 * the tcp options to be used in the next burst and subtracts their
781 * length from the connection's snd_mss.
782 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500783u16
784tcp_session_send_mss (transport_connection_t * trans_conn)
785{
786 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -0700787
788 /* Ensure snd_mss does accurately reflect the amount of data we can push
789 * in a segment. This also makes sure that options are updated according to
790 * the current state of the connection. */
791 tcp_update_snd_mss (tc);
792
Dave Barach68b0fb02017-02-28 15:15:56 -0500793 return tc->snd_mss;
794}
795
Florin Coras3af90fc2017-05-03 21:09:42 -0700796always_inline u32
797tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
798{
799 if (tc->snd_wnd < tc->snd_mss)
800 {
Florin Corasdb84e572017-05-09 18:54:52 -0700801 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700802 }
803
804 /* If we can't write at least a segment, don't try at all */
805 if (snd_space < tc->snd_mss)
806 return 0;
807
808 /* round down to mss multiple */
809 return snd_space - (snd_space % tc->snd_mss);
810}
811
Florin Coras6792ec02017-03-13 03:49:51 -0700812/**
813 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -0700814 *
815 * Takes into account available send space, snd_mss and the congestion
816 * state of the connection. If possible, the value returned is a multiple
817 * of snd_mss.
818 *
819 * @param tc tcp connection
820 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -0700821 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500822u32
Florin Corasbb292f42017-05-19 09:49:19 -0700823tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500824{
Florin Corasf03a59a2017-06-09 21:07:32 -0700825 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -0700826
Florin Corasf03a59a2017-06-09 21:07:32 -0700827 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -0700828 {
829 snd_space = tcp_available_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -0700830
831 /* If we haven't gotten dupacks or if we did and have gotten sacked
832 * bytes then we can still send as per Limited Transmit (RFC3042) */
833 if (PREDICT_FALSE (tc->rcv_dupacks != 0
834 && (tcp_opts_sack_permitted (tc)
835 && tc->sack_sb.last_sacked_bytes == 0)))
836 {
837 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
838 tc->limited_transmit = tc->snd_nxt;
839 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
840
841 snt_limited = tc->snd_nxt - tc->limited_transmit;
842 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
843 }
Florin Coras3af90fc2017-05-03 21:09:42 -0700844 return tcp_round_snd_space (tc, snd_space);
845 }
Florin Coras6792ec02017-03-13 03:49:51 -0700846
Florin Coras3af90fc2017-05-03 21:09:42 -0700847 if (tcp_in_recovery (tc))
848 {
849 tc->snd_nxt = tc->snd_una_max;
Florin Coras93992a92017-05-24 18:03:56 -0700850 snd_space = tcp_available_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -0700851 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -0700852 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -0700853 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700854 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -0700855 }
856
857 /* If in fast recovery, send 1 SMSS if wnd allows */
Florin Coras93992a92017-05-24 18:03:56 -0700858 if (tcp_in_fastrecovery (tc)
859 && tcp_available_snd_space (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700860 {
861 tcp_fastrecovery_1_smss_on (tc);
862 return tc->snd_mss;
863 }
864
865 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500866}
867
868u32
Florin Corasbb292f42017-05-19 09:49:19 -0700869tcp_session_send_space (transport_connection_t * trans_conn)
870{
871 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
872 return tcp_snd_space (tc);
873}
874
Florin Coras93992a92017-05-24 18:03:56 -0700875i32
876tcp_rcv_wnd_available (tcp_connection_t * tc)
877{
878 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
879}
880
Florin Corasbb292f42017-05-19 09:49:19 -0700881u32
Florin Corasd79b41e2017-03-04 05:37:52 -0800882tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -0500883{
884 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -0700885
886 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
887
888 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -0800889 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -0500890}
891
892/* *INDENT-OFF* */
893const static transport_proto_vft_t tcp4_proto = {
894 .bind = tcp_session_bind_ip4,
Florin Corase69f4952017-03-07 10:06:24 -0800895 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500896 .push_header = tcp_push_header,
897 .get_connection = tcp_session_get_transport,
898 .get_listener = tcp_session_get_listener,
899 .get_half_open = tcp_half_open_session_get_transport,
900 .open = tcp_session_open_ip4,
901 .close = tcp_session_close,
902 .cleanup = tcp_session_cleanup,
903 .send_mss = tcp_session_send_mss,
904 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -0800905 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -0800906 .format_connection = format_tcp_session,
907 .format_listener = format_tcp_listener_session,
908 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -0500909};
910
911const static transport_proto_vft_t tcp6_proto = {
912 .bind = tcp_session_bind_ip6,
Florin Corase69f4952017-03-07 10:06:24 -0800913 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500914 .push_header = tcp_push_header,
915 .get_connection = tcp_session_get_transport,
916 .get_listener = tcp_session_get_listener,
917 .get_half_open = tcp_half_open_session_get_transport,
918 .open = tcp_session_open_ip6,
919 .close = tcp_session_close,
920 .cleanup = tcp_session_cleanup,
921 .send_mss = tcp_session_send_mss,
922 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -0800923 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -0800924 .format_connection = format_tcp_session,
925 .format_listener = format_tcp_listener_session,
926 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -0500927};
928/* *INDENT-ON* */
929
930void
931tcp_timer_keep_handler (u32 conn_index)
932{
Damjan Marion586afd72017-04-05 19:18:20 +0200933 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500934 tcp_connection_t *tc;
935
Damjan Marion586afd72017-04-05 19:18:20 +0200936 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500937 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
938
939 tcp_connection_close (tc);
940}
941
942void
943tcp_timer_establish_handler (u32 conn_index)
944{
945 tcp_connection_t *tc;
946 u8 sst;
947
948 tc = tcp_half_open_connection_get (conn_index);
949 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
950
951 ASSERT (tc->state == TCP_STATE_SYN_SENT);
952
953 sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
954 stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ );
955
956 tcp_connection_cleanup (tc);
957}
958
959void
Florin Corasd79b41e2017-03-04 05:37:52 -0800960tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500961{
Damjan Marion586afd72017-04-05 19:18:20 +0200962 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500963 tcp_connection_t *tc;
964
Damjan Marion586afd72017-04-05 19:18:20 +0200965 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800966 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
967
968 /* Session didn't come back with a close(). Send FIN either way
969 * and switch to LAST_ACK. */
970 if (tc->state == TCP_STATE_CLOSE_WAIT)
971 {
972 if (tc->flags & TCP_CONN_FINSNT)
973 {
974 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
975 }
976
977 tcp_send_fin (tc);
978 tc->state = TCP_STATE_LAST_ACK;
979
980 /* Make sure we don't wait in LAST ACK forever */
981 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
982
983 /* Don't delete the connection yet */
984 return;
985 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500986
987 tcp_connection_del (tc);
988}
989
990/* *INDENT-OFF* */
991static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
992{
993 tcp_timer_retransmit_handler,
994 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -0700995 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -0500996 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -0800997 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -0500998 tcp_timer_retransmit_syn_handler,
999 tcp_timer_establish_handler
1000};
1001/* *INDENT-ON* */
1002
1003static void
1004tcp_expired_timers_dispatch (u32 * expired_timers)
1005{
1006 int i;
1007 u32 connection_index, timer_id;
1008
1009 for (i = 0; i < vec_len (expired_timers); i++)
1010 {
1011 /* Get session index and timer id */
1012 connection_index = expired_timers[i] & 0x0FFFFFFF;
1013 timer_id = expired_timers[i] >> 28;
1014
Florin Corase69f4952017-03-07 10:06:24 -08001015 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1016
Dave Barach68b0fb02017-02-28 15:15:56 -05001017 /* Handle expiration */
1018 (*timer_expiration_handlers[timer_id]) (connection_index);
1019 }
1020}
1021
1022void
1023tcp_initialize_timer_wheels (tcp_main_t * tm)
1024{
1025 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001026 /* *INDENT-OFF* */
1027 foreach_vlib_main (({
1028 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001029 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1030 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001031 tw->last_run_time = vlib_time_now (this_vlib_main);
1032 }));
1033 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001034}
1035
1036clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001037tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001038{
Dave Barach68b0fb02017-02-28 15:15:56 -05001039 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001040 ip_protocol_info_t *pi;
1041 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001042 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1043 clib_error_t *error = 0;
1044 u32 num_threads;
1045
Dave Barach68b0fb02017-02-28 15:15:56 -05001046 if ((error = vlib_call_init_function (vm, ip_main_init)))
1047 return error;
1048 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1049 return error;
1050 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1051 return error;
1052
1053 /*
1054 * Registrations
1055 */
1056
1057 /* Register with IP */
1058 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1059 if (pi == 0)
1060 return clib_error_return (0, "TCP protocol info AWOL");
1061 pi->format_header = format_tcp_header;
1062 pi->unformat_pg_edit = unformat_pg_tcp_header;
1063
1064 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
1065
1066 /* Register as transport with URI */
1067 session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto);
1068 session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto);
1069
1070 /*
1071 * Initialize data structures
1072 */
1073
1074 num_threads = 1 /* main thread */ + vtm->n_threads;
1075 vec_validate (tm->connections, num_threads - 1);
1076
1077 /* Initialize per worker thread tx buffers (used for control messages) */
1078 vec_validate (tm->tx_buffers, num_threads - 1);
1079
1080 /* Initialize timer wheels */
1081 vec_validate (tm->timer_wheels, num_threads - 1);
1082 tcp_initialize_timer_wheels (tm);
1083
Dave Barach68b0fb02017-02-28 15:15:56 -05001084 /* Initialize clocks per tick for TCP timestamp. Used to compute
1085 * monotonically increasing timestamps. */
1086 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1087 / TCP_TSTAMP_RESOLUTION;
1088
1089 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
1090 200000 /* $$$$ config parameter nbuckets */ ,
1091 (64 << 20) /*$$$ config parameter table size */ );
1092
1093 return error;
1094}
1095
Florin Corasa0b34a72017-03-07 01:20:52 -08001096clib_error_t *
1097vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1098{
1099 if (is_en)
1100 {
1101 if (tcp_main.is_enabled)
1102 return 0;
1103
1104 return tcp_main_enable (vm);
1105 }
1106 else
1107 {
1108 tcp_main.is_enabled = 0;
1109 }
1110
1111 return 0;
1112}
1113
1114clib_error_t *
1115tcp_init (vlib_main_t * vm)
1116{
1117 tcp_main_t *tm = vnet_get_tcp_main ();
1118
1119 tm->vlib_main = vm;
1120 tm->vnet_main = vnet_get_main ();
1121 tm->is_enabled = 0;
1122
1123 return 0;
1124}
1125
Dave Barach68b0fb02017-02-28 15:15:56 -05001126VLIB_INIT_FUNCTION (tcp_init);
1127
1128/*
1129 * fd.io coding-style-patch-verification: ON
1130 *
1131 * Local Variables:
1132 * eval: (c-set-style "gnu")
1133 * End:
1134 */