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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/tcp/tcp.h>
17 #include <vnet/session/session.h>
18 #include <vnet/fib/fib.h>
24 tcp_connection_bind (vlib_main_t * vm, u32 session_index, ip46_address_t * ip,
25 u16 port_host_byte_order, u8 is_ip4)
27 tcp_main_t *tm = &tcp_main;
28 tcp_connection_t *listener;
30 pool_get (tm->listener_pool, listener);
31 memset (listener, 0, sizeof (*listener));
33 listener->c_c_index = listener - tm->listener_pool;
34 listener->c_lcl_port = clib_host_to_net_u16 (port_host_byte_order);
37 listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32;
39 clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t));
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;
46 return listener->c_c_index;
50 tcp_session_bind_ip4 (vlib_main_t * vm, u32 session_index,
51 ip46_address_t * ip, u16 port_host_byte_order)
53 return tcp_connection_bind (vm, session_index, ip, port_host_byte_order, 1);
57 tcp_session_bind_ip6 (vlib_main_t * vm, u32 session_index,
58 ip46_address_t * ip, u16 port_host_byte_order)
60 return tcp_connection_bind (vm, session_index, ip, port_host_byte_order, 0);
65 tcp_session_unbind (u32 listener_index)
67 tcp_main_t *tm = vnet_get_tcp_main ();
68 pool_put_index (tm->listener_pool, listener_index);
72 tcp_session_unbind_ip4 (vlib_main_t * vm, u32 listener_index)
74 tcp_session_unbind (listener_index);
79 tcp_session_unbind_ip6 (vlib_main_t * vm, u32 listener_index)
81 tcp_session_unbind (listener_index);
85 transport_connection_t *
86 tcp_session_get_listener (u32 listener_index)
88 tcp_main_t *tm = vnet_get_tcp_main ();
90 tc = pool_elt_at_index (tm->listener_pool, listener_index);
91 return &tc->connection;
95 * Cleans up connection state.
100 tcp_connection_cleanup (tcp_connection_t * tc)
102 tcp_main_t *tm = &tcp_main;
104 transport_endpoint_t *tep;
106 /* Cleanup local endpoint if this was an active connect */
107 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
111 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
113 tep = pool_elt_at_index (tm->local_endpoints, tepi);
114 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
115 pool_put (tm->local_endpoints, tep);
118 /* Make sure all timers are cleared */
119 tcp_connection_timers_reset (tc);
121 /* Check if half-open */
122 if (tc->state == TCP_STATE_SYN_SENT)
123 pool_put (tm->half_open_connections, tc);
125 pool_put (tm->connections[tc->c_thread_index], tc);
129 * Connection removal.
131 * This should be called only once connection enters CLOSED state. Note
132 * that it notifies the session of the removal event, so if the goal is to
133 * just remove the connection, call tcp_connection_cleanup instead.
136 tcp_connection_del (tcp_connection_t * tc)
138 stream_session_delete_notify (&tc->connection);
139 tcp_connection_cleanup (tc);
143 * Begin connection closing procedure.
145 * If at the end the connection is not in CLOSED state, it is not removed.
146 * Instead, we rely on on TCP to advance through state machine to either
147 * 1) LAST_ACK (passive close) whereby when the last ACK is received
148 * tcp_connection_del is called. This notifies session of the delete and
150 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
151 * and cleanup is called.
154 tcp_connection_close (tcp_connection_t * tc)
156 /* Send FIN if needed */
157 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD
158 || tc->state == TCP_STATE_CLOSE_WAIT)
162 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD)
163 tc->state = TCP_STATE_FIN_WAIT_1;
164 else if (tc->state == TCP_STATE_SYN_SENT)
165 tc->state = TCP_STATE_CLOSED;
166 else if (tc->state == TCP_STATE_CLOSE_WAIT)
167 tc->state = TCP_STATE_LAST_ACK;
169 /* Half-close connections are not supported XXX */
171 if (tc->state == TCP_STATE_CLOSED)
172 tcp_connection_del (tc);
176 tcp_session_close (u32 conn_index, u32 thread_index)
178 tcp_connection_t *tc;
179 tc = tcp_connection_get (conn_index, thread_index);
180 tcp_connection_close (tc);
184 tcp_session_cleanup (u32 conn_index, u32 thread_index)
186 tcp_connection_t *tc;
187 tc = tcp_connection_get (conn_index, thread_index);
188 tcp_connection_cleanup (tc);
192 ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
194 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
195 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
196 ip_interface_address_t *ia = 0;
201 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
203 return ip_interface_address_get_address (lm4, ia);
210 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
212 return ip_interface_address_get_address (lm6, ia);
220 #define PORT_MASK ((1 << 16)- 1)
222 * Allocate local port and add if successful add entry to local endpoint
223 * table to mark the pair as used.
226 tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip)
228 transport_endpoint_t *tep;
230 u16 min = 1024, max = 65535, tries; /* XXX configurable ? */
233 time_now = tcp_time_now ();
235 /* Start at random point or max */
236 pool_get (tm->local_endpoints, tep);
237 clib_memcpy (&tep->ip, ip, sizeof (*ip));
239 /* Search for first free slot */
240 for (; tries >= 0; tries--)
244 /* Find a port in the specified range */
247 port = random_u32 (&time_now) & PORT_MASK;
248 if (PREDICT_TRUE (port >= min && port < max))
255 tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip,
257 /* If not found, we're done */
258 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
260 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
261 tep - tm->local_endpoints);
266 pool_put (tm->local_endpoints, tep);
271 * Initialize all connection timers as invalid
274 tcp_connection_timers_init (tcp_connection_t * tc)
278 /* Set all to invalid */
279 for (i = 0; i < TCP_N_TIMERS; i++)
281 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
284 tc->rto = TCP_RTO_INIT;
288 * Stop all connection timers
291 tcp_connection_timers_reset (tcp_connection_t * tc)
294 for (i = 0; i < TCP_N_TIMERS; i++)
296 tcp_timer_reset (tc, i);
300 /** Initialize tcp connection variables
302 * Should be called after having received a msg from the peer, i.e., a SYN or
303 * a SYNACK, such that connection options have already been exchanged. */
305 tcp_connection_init_vars (tcp_connection_t * tc)
307 tcp_connection_timers_init (tc);
308 tcp_set_snd_mss (tc);
309 tc->sack_sb.head = TCP_INVALID_SACK_HOLE_INDEX;
314 tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4)
316 tcp_main_t *tm = vnet_get_tcp_main ();
317 tcp_connection_t *tc;
319 u32 fei, sw_if_index;
320 ip46_address_t lcl_addr;
324 * Find the local address and allocate port
326 memset (&lcl_addr, 0, sizeof (lcl_addr));
328 /* Find a FIB path to the destination */
329 clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr));
330 prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
331 prefix.fp_len = is_ip4 ? 32 : 128;
333 fei = fib_table_lookup (0, &prefix);
335 /* Couldn't find route to destination. Bail out. */
336 if (fei == FIB_NODE_INDEX_INVALID)
339 sw_if_index = fib_entry_get_resolving_interface (fei);
341 if (sw_if_index == (u32) ~ 0)
347 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
348 lcl_addr.ip4.as_u32 = ip4->as_u32;
353 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
354 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
357 /* Allocate source port */
358 lcl_port = tcp_allocate_local_port (tm, &lcl_addr);
361 clib_warning ("Failed to allocate src port");
366 * Create connection and send SYN
369 pool_get (tm->half_open_connections, tc);
370 memset (tc, 0, sizeof (*tc));
372 clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t));
373 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
374 tc->c_rmt_port = clib_host_to_net_u16 (rmt_port);
375 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
376 tc->c_c_index = tc - tm->half_open_connections;
377 tc->c_is_ip4 = is_ip4;
379 /* The other connection vars will be initialized after SYN ACK */
380 tcp_connection_timers_init (tc);
384 tc->state = TCP_STATE_SYN_SENT;
386 return tc->c_c_index;
390 tcp_session_open_ip4 (ip46_address_t * addr, u16 port)
392 return tcp_connection_open (addr, port, 1);
396 tcp_session_open_ip6 (ip46_address_t * addr, u16 port)
398 return tcp_connection_open (addr, port, 0);
402 format_tcp_session_ip4 (u8 * s, va_list * args)
404 u32 tci = va_arg (*args, u32);
405 u32 thread_index = va_arg (*args, u32);
406 tcp_connection_t *tc;
408 tc = tcp_connection_get (tci, thread_index);
410 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address,
411 &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port),
412 format_ip4_address, &tc->c_rmt_ip4,
413 clib_net_to_host_u16 (tc->c_rmt_port));
419 format_tcp_session_ip6 (u8 * s, va_list * args)
421 u32 tci = va_arg (*args, u32);
422 u32 thread_index = va_arg (*args, u32);
423 tcp_connection_t *tc = tcp_connection_get (tci, thread_index);
424 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address,
425 &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port),
426 format_ip6_address, &tc->c_rmt_ip6,
427 clib_net_to_host_u16 (tc->c_rmt_port));
432 format_tcp_listener_session_ip4 (u8 * s, va_list * args)
434 u32 tci = va_arg (*args, u32);
435 tcp_connection_t *tc = tcp_listener_get (tci);
436 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address,
437 &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port),
438 format_ip4_address, &tc->c_rmt_ip4,
439 clib_net_to_host_u16 (tc->c_rmt_port));
444 format_tcp_listener_session_ip6 (u8 * s, va_list * args)
446 u32 tci = va_arg (*args, u32);
447 tcp_connection_t *tc = tcp_listener_get (tci);
448 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address,
449 &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port),
450 format_ip6_address, &tc->c_rmt_ip6,
451 clib_net_to_host_u16 (tc->c_rmt_port));
456 format_tcp_half_open_session_ip4 (u8 * s, va_list * args)
458 u32 tci = va_arg (*args, u32);
459 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
460 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip4_address,
461 &tc->c_lcl_ip4, clib_net_to_host_u16 (tc->c_lcl_port),
462 format_ip4_address, &tc->c_rmt_ip4,
463 clib_net_to_host_u16 (tc->c_rmt_port));
468 format_tcp_half_open_session_ip6 (u8 * s, va_list * args)
470 u32 tci = va_arg (*args, u32);
471 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
472 s = format (s, "[%s] %U:%d->%U:%d", "tcp", format_ip6_address,
473 &tc->c_lcl_ip6, clib_net_to_host_u16 (tc->c_lcl_port),
474 format_ip6_address, &tc->c_rmt_ip6,
475 clib_net_to_host_u16 (tc->c_rmt_port));
479 transport_connection_t *
480 tcp_session_get_transport (u32 conn_index, u32 thread_index)
482 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
483 return &tc->connection;
486 transport_connection_t *
487 tcp_half_open_session_get_transport (u32 conn_index)
489 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
490 return &tc->connection;
494 tcp_session_send_mss (transport_connection_t * trans_conn)
496 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
501 tcp_session_send_space (transport_connection_t * trans_conn)
503 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
504 return tcp_available_snd_space (tc);
508 tcp_session_rx_fifo_offset (transport_connection_t * trans_conn)
510 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
511 return (tc->snd_una_max - tc->snd_una);
515 const static transport_proto_vft_t tcp4_proto = {
516 .bind = tcp_session_bind_ip4,
517 .unbind = tcp_session_unbind_ip4,
518 .push_header = tcp_push_header,
519 .get_connection = tcp_session_get_transport,
520 .get_listener = tcp_session_get_listener,
521 .get_half_open = tcp_half_open_session_get_transport,
522 .open = tcp_session_open_ip4,
523 .close = tcp_session_close,
524 .cleanup = tcp_session_cleanup,
525 .send_mss = tcp_session_send_mss,
526 .send_space = tcp_session_send_space,
527 .rx_fifo_offset = tcp_session_rx_fifo_offset,
528 .format_connection = format_tcp_session_ip4,
529 .format_listener = format_tcp_listener_session_ip4,
530 .format_half_open = format_tcp_half_open_session_ip4
533 const static transport_proto_vft_t tcp6_proto = {
534 .bind = tcp_session_bind_ip6,
535 .unbind = tcp_session_unbind_ip6,
536 .push_header = tcp_push_header,
537 .get_connection = tcp_session_get_transport,
538 .get_listener = tcp_session_get_listener,
539 .get_half_open = tcp_half_open_session_get_transport,
540 .open = tcp_session_open_ip6,
541 .close = tcp_session_close,
542 .cleanup = tcp_session_cleanup,
543 .send_mss = tcp_session_send_mss,
544 .send_space = tcp_session_send_space,
545 .rx_fifo_offset = tcp_session_rx_fifo_offset,
546 .format_connection = format_tcp_session_ip6,
547 .format_listener = format_tcp_listener_session_ip6,
548 .format_half_open = format_tcp_half_open_session_ip6
553 tcp_timer_keep_handler (u32 conn_index)
555 u32 cpu_index = os_get_cpu_number ();
556 tcp_connection_t *tc;
558 tc = tcp_connection_get (conn_index, cpu_index);
559 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
561 tcp_connection_close (tc);
565 tcp_timer_establish_handler (u32 conn_index)
567 tcp_connection_t *tc;
570 tc = tcp_half_open_connection_get (conn_index);
571 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
573 ASSERT (tc->state == TCP_STATE_SYN_SENT);
575 sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
576 stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ );
578 tcp_connection_cleanup (tc);
582 tcp_timer_2msl_handler (u32 conn_index)
584 u32 cpu_index = os_get_cpu_number ();
585 tcp_connection_t *tc;
587 tc = tcp_connection_get (conn_index, cpu_index);
588 tc->timers[TCP_TIMER_2MSL] = TCP_TIMER_HANDLE_INVALID;
590 tcp_connection_del (tc);
594 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
596 tcp_timer_retransmit_handler,
597 tcp_timer_delack_handler,
599 tcp_timer_keep_handler,
600 tcp_timer_2msl_handler,
601 tcp_timer_retransmit_syn_handler,
602 tcp_timer_establish_handler
607 tcp_expired_timers_dispatch (u32 * expired_timers)
610 u32 connection_index, timer_id;
612 for (i = 0; i < vec_len (expired_timers); i++)
614 /* Get session index and timer id */
615 connection_index = expired_timers[i] & 0x0FFFFFFF;
616 timer_id = expired_timers[i] >> 28;
618 /* Handle expiration */
619 (*timer_expiration_handlers[timer_id]) (connection_index);
624 tcp_initialize_timer_wheels (tcp_main_t * tm)
626 tw_timer_wheel_16t_2w_512sl_t *tw;
627 vec_foreach (tw, tm->timer_wheels)
629 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
630 100e-3 /* timer period 100ms */ , ~0);
631 tw->last_run_time = vlib_time_now (tm->vlib_main);
636 tcp_main_enable (vlib_main_t * vm)
638 tcp_main_t *tm = vnet_get_tcp_main ();
639 ip_protocol_info_t *pi;
640 ip_main_t *im = &ip_main;
641 vlib_thread_main_t *vtm = vlib_get_thread_main ();
642 clib_error_t *error = 0;
645 if ((error = vlib_call_init_function (vm, ip_main_init)))
647 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
649 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
656 /* Register with IP */
657 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
659 return clib_error_return (0, "TCP protocol info AWOL");
660 pi->format_header = format_tcp_header;
661 pi->unformat_pg_edit = unformat_pg_tcp_header;
663 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
665 /* Register as transport with URI */
666 session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto);
667 session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto);
670 * Initialize data structures
673 num_threads = 1 /* main thread */ + vtm->n_threads;
674 vec_validate (tm->connections, num_threads - 1);
676 /* Initialize per worker thread tx buffers (used for control messages) */
677 vec_validate (tm->tx_buffers, num_threads - 1);
679 /* Initialize timer wheels */
680 vec_validate (tm->timer_wheels, num_threads - 1);
681 tcp_initialize_timer_wheels (tm);
683 vec_validate (tm->delack_connections, num_threads - 1);
685 /* Initialize clocks per tick for TCP timestamp. Used to compute
686 * monotonically increasing timestamps. */
687 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
688 / TCP_TSTAMP_RESOLUTION;
690 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
691 200000 /* $$$$ config parameter nbuckets */ ,
692 (64 << 20) /*$$$ config parameter table size */ );
698 vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
702 if (tcp_main.is_enabled)
705 return tcp_main_enable (vm);
709 tcp_main.is_enabled = 0;
716 tcp_init (vlib_main_t * vm)
718 tcp_main_t *tm = vnet_get_tcp_main ();
721 tm->vnet_main = vnet_get_main ();
727 VLIB_INIT_FUNCTION (tcp_init);
730 * fd.io coding-style-patch-verification: ON
733 * eval: (c-set-style "gnu")