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 (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 tcp_connection_timers_init (listener);
48 TCP_EVT_DBG (TCP_EVT_BIND, listener);
50 return listener->c_c_index;
54 tcp_session_bind_ip4 (u32 session_index, ip46_address_t * ip,
55 u16 port_host_byte_order)
57 return tcp_connection_bind (session_index, ip, port_host_byte_order, 1);
61 tcp_session_bind_ip6 (u32 session_index, ip46_address_t * ip,
62 u16 port_host_byte_order)
64 return tcp_connection_bind (session_index, ip, port_host_byte_order, 0);
69 tcp_connection_unbind (u32 listener_index)
71 tcp_main_t *tm = vnet_get_tcp_main ();
72 TCP_EVT_DBG (TCP_EVT_UNBIND,
73 pool_elt_at_index (tm->listener_pool, listener_index));
74 pool_put_index (tm->listener_pool, listener_index);
78 tcp_session_unbind (u32 listener_index)
80 tcp_connection_unbind (listener_index);
84 transport_connection_t *
85 tcp_session_get_listener (u32 listener_index)
87 tcp_main_t *tm = vnet_get_tcp_main ();
89 tc = pool_elt_at_index (tm->listener_pool, listener_index);
90 return &tc->connection;
94 * Cleans up connection state.
99 tcp_connection_cleanup (tcp_connection_t * tc)
101 tcp_main_t *tm = &tcp_main;
103 transport_endpoint_t *tep;
105 /* Cleanup local endpoint if this was an active connect */
106 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
110 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
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);
117 /* Make sure all timers are cleared */
118 tcp_connection_timers_reset (tc);
120 /* Check if half-open */
121 if (tc->state == TCP_STATE_SYN_SENT)
122 pool_put (tm->half_open_connections, tc);
124 pool_put (tm->connections[tc->c_thread_index], tc);
128 * Connection removal.
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.
135 tcp_connection_del (tcp_connection_t * tc)
137 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
138 stream_session_delete_notify (&tc->connection);
139 tcp_connection_cleanup (tc);
142 /** Notify session that connection has been reset.
144 * Switch state to closed and wait for session to call cleanup.
147 tcp_connection_reset (tcp_connection_t * tc)
149 if (tc->state == TCP_STATE_CLOSED)
152 tc->state = TCP_STATE_CLOSED;
153 stream_session_reset_notify (&tc->connection);
157 * Begin connection closing procedure.
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
164 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
165 * and cleanup is called.
167 * N.B. Half-close connections are not supported
170 tcp_connection_close (tcp_connection_t * tc)
172 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
174 /* Send FIN if needed */
175 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD
176 || tc->state == TCP_STATE_CLOSE_WAIT)
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;
187 /* 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)
190 tcp_connection_del (tc);
194 tcp_session_close (u32 conn_index, u32 thread_index)
196 tcp_connection_t *tc;
197 tc = tcp_connection_get (conn_index, thread_index);
198 tcp_connection_close (tc);
202 tcp_session_cleanup (u32 conn_index, u32 thread_index)
204 tcp_connection_t *tc;
205 tc = tcp_connection_get (conn_index, thread_index);
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);
213 ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
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;
222 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
224 return ip_interface_address_get_address (lm4, ia);
231 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
233 return ip_interface_address_get_address (lm6, ia);
241 #define PORT_MASK ((1 << 16)- 1)
243 * Allocate local port and add if successful add entry to local endpoint
244 * table to mark the pair as used.
247 tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip)
249 transport_endpoint_t *tep;
251 u16 min = 1024, max = 65535; /* XXX configurable ? */
255 time_now = tcp_time_now ();
257 /* Start at random point or max */
258 pool_get (tm->local_endpoints, tep);
259 clib_memcpy (&tep->ip, ip, sizeof (*ip));
261 /* Search for first free slot */
262 for (; tries >= 0; tries--)
266 /* Find a port in the specified range */
269 port = random_u32 (&time_now) & PORT_MASK;
270 if (PREDICT_TRUE (port >= min && port < max))
277 tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip,
279 /* If not found, we're done */
280 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
282 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
283 tep - tm->local_endpoints);
288 pool_put (tm->local_endpoints, tep);
293 * Initialize all connection timers as invalid
296 tcp_connection_timers_init (tcp_connection_t * tc)
300 /* Set all to invalid */
301 for (i = 0; i < TCP_N_TIMERS; i++)
303 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
306 tc->rto = TCP_RTO_INIT;
310 * Stop all connection timers
313 tcp_connection_timers_reset (tcp_connection_t * tc)
316 for (i = 0; i < TCP_N_TIMERS; i++)
318 tcp_timer_reset (tc, i);
322 /** Initialize tcp connection variables
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. */
327 tcp_connection_init_vars (tcp_connection_t * tc)
329 tcp_connection_timers_init (tc);
330 tcp_set_snd_mss (tc);
331 tc->sack_sb.head = TCP_INVALID_SACK_HOLE_INDEX;
336 tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4)
338 tcp_main_t *tm = vnet_get_tcp_main ();
339 tcp_connection_t *tc;
341 u32 fei, sw_if_index;
342 ip46_address_t lcl_addr;
346 * Find the local address and allocate port
348 memset (&lcl_addr, 0, sizeof (lcl_addr));
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;
355 fei = fib_table_lookup (0, &prefix);
357 /* Couldn't find route to destination. Bail out. */
358 if (fei == FIB_NODE_INDEX_INVALID)
361 sw_if_index = fib_entry_get_resolving_interface (fei);
363 if (sw_if_index == (u32) ~ 0)
369 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
370 lcl_addr.ip4.as_u32 = ip4->as_u32;
375 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
376 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
379 /* Allocate source port */
380 lcl_port = tcp_allocate_local_port (tm, &lcl_addr);
383 clib_warning ("Failed to allocate src port");
388 * Create connection and send SYN
391 pool_get (tm->half_open_connections, tc);
392 memset (tc, 0, sizeof (*tc));
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;
401 /* The other connection vars will be initialized after SYN ACK */
402 tcp_connection_timers_init (tc);
406 tc->state = TCP_STATE_SYN_SENT;
408 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
410 return tc->c_c_index;
414 tcp_session_open_ip4 (ip46_address_t * addr, u16 port)
416 return tcp_connection_open (addr, port, 1);
420 tcp_session_open_ip6 (ip46_address_t * addr, u16 port)
422 return tcp_connection_open (addr, port, 0);
425 const char *tcp_dbg_evt_str[] = {
426 #define _(sym, str) str,
431 const char *tcp_fsm_states[] = {
432 #define _(sym, str) str,
433 foreach_tcp_fsm_state
438 format_tcp_state (u8 * s, va_list * args)
440 tcp_state_t *state = va_arg (*args, tcp_state_t *);
442 if (*state < TCP_N_STATES)
443 s = format (s, "%s", tcp_fsm_states[*state]);
445 s = format (s, "UNKNOWN");
450 const char *tcp_conn_timers[] = {
451 #define _(sym, str) str,
457 format_tcp_timers (u8 * s, va_list * args)
459 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
462 for (i = 0; i < TCP_N_TIMERS; i++)
463 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
467 for (i = 0; i < last; i++)
469 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
470 s = format (s, "%s,", tcp_conn_timers[i]);
474 s = format (s, "%s]", tcp_conn_timers[i]);
482 format_tcp_connection (u8 * s, va_list * args)
484 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
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));
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));
505 format_tcp_connection_verbose (u8 * s, va_list * args)
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);
514 format_tcp_session (u8 * s, va_list * args)
516 u32 tci = va_arg (*args, u32);
517 u32 thread_index = va_arg (*args, u32);
518 tcp_connection_t *tc;
520 tc = tcp_connection_get (tci, thread_index);
521 return format (s, "%U", format_tcp_connection, tc);
525 format_tcp_listener_session (u8 * s, va_list * args)
527 u32 tci = va_arg (*args, u32);
528 tcp_connection_t *tc = tcp_listener_get (tci);
529 return format (s, "%U", format_tcp_connection, tc);
533 format_tcp_half_open_session (u8 * s, va_list * args)
535 u32 tci = va_arg (*args, u32);
536 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
537 return format (s, "%U", format_tcp_connection, tc);
540 transport_connection_t *
541 tcp_session_get_transport (u32 conn_index, u32 thread_index)
543 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
544 return &tc->connection;
547 transport_connection_t *
548 tcp_half_open_session_get_transport (u32 conn_index)
550 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
551 return &tc->connection;
555 tcp_session_send_mss (transport_connection_t * trans_conn)
557 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
562 tcp_session_send_space (transport_connection_t * trans_conn)
564 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
565 return tcp_available_snd_space (tc);
569 tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
571 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
572 return (tc->snd_nxt - tc->snd_una);
576 const static transport_proto_vft_t tcp4_proto = {
577 .bind = tcp_session_bind_ip4,
578 .unbind = tcp_session_unbind,
579 .push_header = tcp_push_header,
580 .get_connection = tcp_session_get_transport,
581 .get_listener = tcp_session_get_listener,
582 .get_half_open = tcp_half_open_session_get_transport,
583 .open = tcp_session_open_ip4,
584 .close = tcp_session_close,
585 .cleanup = tcp_session_cleanup,
586 .send_mss = tcp_session_send_mss,
587 .send_space = tcp_session_send_space,
588 .tx_fifo_offset = tcp_session_tx_fifo_offset,
589 .format_connection = format_tcp_session,
590 .format_listener = format_tcp_listener_session,
591 .format_half_open = format_tcp_half_open_session,
594 const static transport_proto_vft_t tcp6_proto = {
595 .bind = tcp_session_bind_ip6,
596 .unbind = tcp_session_unbind,
597 .push_header = tcp_push_header,
598 .get_connection = tcp_session_get_transport,
599 .get_listener = tcp_session_get_listener,
600 .get_half_open = tcp_half_open_session_get_transport,
601 .open = tcp_session_open_ip6,
602 .close = tcp_session_close,
603 .cleanup = tcp_session_cleanup,
604 .send_mss = tcp_session_send_mss,
605 .send_space = tcp_session_send_space,
606 .tx_fifo_offset = tcp_session_tx_fifo_offset,
607 .format_connection = format_tcp_session,
608 .format_listener = format_tcp_listener_session,
609 .format_half_open = format_tcp_half_open_session,
614 tcp_timer_keep_handler (u32 conn_index)
616 u32 cpu_index = os_get_cpu_number ();
617 tcp_connection_t *tc;
619 tc = tcp_connection_get (conn_index, cpu_index);
620 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
622 tcp_connection_close (tc);
626 tcp_timer_establish_handler (u32 conn_index)
628 tcp_connection_t *tc;
631 tc = tcp_half_open_connection_get (conn_index);
632 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
634 ASSERT (tc->state == TCP_STATE_SYN_SENT);
636 sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
637 stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ );
639 tcp_connection_cleanup (tc);
643 tcp_timer_waitclose_handler (u32 conn_index)
645 u32 cpu_index = os_get_cpu_number ();
646 tcp_connection_t *tc;
648 tc = tcp_connection_get (conn_index, cpu_index);
649 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
651 /* Session didn't come back with a close(). Send FIN either way
652 * and switch to LAST_ACK. */
653 if (tc->state == TCP_STATE_CLOSE_WAIT)
655 if (tc->flags & TCP_CONN_FINSNT)
657 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
661 tc->state = TCP_STATE_LAST_ACK;
663 /* Make sure we don't wait in LAST ACK forever */
664 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
666 /* Don't delete the connection yet */
670 tcp_connection_del (tc);
674 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
676 tcp_timer_retransmit_handler,
677 tcp_timer_delack_handler,
679 tcp_timer_keep_handler,
680 tcp_timer_waitclose_handler,
681 tcp_timer_retransmit_syn_handler,
682 tcp_timer_establish_handler
687 tcp_expired_timers_dispatch (u32 * expired_timers)
690 u32 connection_index, timer_id;
692 for (i = 0; i < vec_len (expired_timers); i++)
694 /* Get session index and timer id */
695 connection_index = expired_timers[i] & 0x0FFFFFFF;
696 timer_id = expired_timers[i] >> 28;
698 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
700 /* Handle expiration */
701 (*timer_expiration_handlers[timer_id]) (connection_index);
706 tcp_initialize_timer_wheels (tcp_main_t * tm)
708 tw_timer_wheel_16t_2w_512sl_t *tw;
709 vec_foreach (tw, tm->timer_wheels)
711 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
712 100e-3 /* timer period 100ms */ , ~0);
713 tw->last_run_time = vlib_time_now (tm->vlib_main);
718 tcp_main_enable (vlib_main_t * vm)
720 tcp_main_t *tm = vnet_get_tcp_main ();
721 ip_protocol_info_t *pi;
722 ip_main_t *im = &ip_main;
723 vlib_thread_main_t *vtm = vlib_get_thread_main ();
724 clib_error_t *error = 0;
727 if ((error = vlib_call_init_function (vm, ip_main_init)))
729 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
731 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
738 /* Register with IP */
739 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
741 return clib_error_return (0, "TCP protocol info AWOL");
742 pi->format_header = format_tcp_header;
743 pi->unformat_pg_edit = unformat_pg_tcp_header;
745 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
747 /* Register as transport with URI */
748 session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto);
749 session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto);
752 * Initialize data structures
755 num_threads = 1 /* main thread */ + vtm->n_threads;
756 vec_validate (tm->connections, num_threads - 1);
758 /* Initialize per worker thread tx buffers (used for control messages) */
759 vec_validate (tm->tx_buffers, num_threads - 1);
761 /* Initialize timer wheels */
762 vec_validate (tm->timer_wheels, num_threads - 1);
763 tcp_initialize_timer_wheels (tm);
765 vec_validate (tm->delack_connections, num_threads - 1);
767 /* Initialize clocks per tick for TCP timestamp. Used to compute
768 * monotonically increasing timestamps. */
769 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
770 / TCP_TSTAMP_RESOLUTION;
772 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
773 200000 /* $$$$ config parameter nbuckets */ ,
774 (64 << 20) /*$$$ config parameter table size */ );
780 vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
784 if (tcp_main.is_enabled)
787 return tcp_main_enable (vm);
791 tcp_main.is_enabled = 0;
798 tcp_init (vlib_main_t * vm)
800 tcp_main_t *tm = vnet_get_tcp_main ();
803 tm->vnet_main = vnet_get_main ();
809 VLIB_INIT_FUNCTION (tcp_init);
812 * fd.io coding-style-patch-verification: ON
815 * eval: (c-set-style "gnu")