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.
18 * @brief TCP host stack utilities
21 #include <vnet/tcp/tcp.h>
22 #include <vnet/session/session.h>
23 #include <vnet/fib/fib.h>
24 #include <vnet/dpo/load_balance.h>
25 #include <vnet/dpo/receive_dpo.h>
26 #include <vnet/ip/ip6_neighbor.h>
32 tcp_connection_bind (u32 session_index, transport_endpoint_t * lcl)
34 tcp_main_t *tm = &tcp_main;
35 tcp_connection_t *listener;
38 pool_get (tm->listener_pool, listener);
39 memset (listener, 0, sizeof (*listener));
41 listener->c_c_index = listener - tm->listener_pool;
42 listener->c_lcl_port = lcl->port;
44 /* If we are provided a sw_if_index, bind using one of its ips */
45 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
47 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
49 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
51 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
52 listener->c_is_ip4 = lcl->is_ip4;
53 listener->c_proto = TRANSPORT_PROTO_TCP;
54 listener->c_s_index = session_index;
55 listener->c_fib_index = lcl->fib_index;
56 listener->state = TCP_STATE_LISTEN;
58 tcp_connection_timers_init (listener);
60 TCP_EVT_DBG (TCP_EVT_BIND, listener);
62 return listener->c_c_index;
66 tcp_session_bind (u32 session_index, transport_endpoint_t * tep)
68 return tcp_connection_bind (session_index, tep);
72 tcp_connection_unbind (u32 listener_index)
74 tcp_main_t *tm = vnet_get_tcp_main ();
77 tc = pool_elt_at_index (tm->listener_pool, listener_index);
79 TCP_EVT_DBG (TCP_EVT_UNBIND, tc);
81 /* Poison the entry */
83 memset (tc, 0xFA, sizeof (*tc));
85 pool_put_index (tm->listener_pool, listener_index);
89 tcp_session_unbind (u32 listener_index)
91 tcp_connection_unbind (listener_index);
95 transport_connection_t *
96 tcp_session_get_listener (u32 listener_index)
98 tcp_main_t *tm = vnet_get_tcp_main ();
100 tc = pool_elt_at_index (tm->listener_pool, listener_index);
101 return &tc->connection;
105 * Cleanup half-open connection
109 tcp_half_open_connection_del (tcp_connection_t * tc)
111 tcp_main_t *tm = vnet_get_tcp_main ();
112 clib_spinlock_lock_if_init (&tm->half_open_lock);
113 pool_put_index (tm->half_open_connections, tc->c_c_index);
115 memset (tc, 0xFA, sizeof (*tc));
116 clib_spinlock_unlock_if_init (&tm->half_open_lock);
120 * Try to cleanup half-open connection
122 * If called from a thread that doesn't own tc, the call won't have any
125 * @param tc - connection to be cleaned up
126 * @return non-zero if cleanup failed.
129 tcp_half_open_connection_cleanup (tcp_connection_t * tc)
131 /* Make sure this is the owning thread */
132 if (tc->c_thread_index != vlib_get_thread_index ())
134 tcp_timer_reset (tc, TCP_TIMER_ESTABLISH);
135 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN);
136 tcp_half_open_connection_del (tc);
141 tcp_half_open_connection_new (void)
143 tcp_main_t *tm = vnet_get_tcp_main ();
144 tcp_connection_t *tc = 0;
145 ASSERT (vlib_get_thread_index () == 0);
146 pool_get (tm->half_open_connections, tc);
147 memset (tc, 0, sizeof (*tc));
148 tc->c_c_index = tc - tm->half_open_connections;
153 * Cleans up connection state.
158 tcp_connection_cleanup (tcp_connection_t * tc)
160 tcp_main_t *tm = &tcp_main;
162 /* Cleanup local endpoint if this was an active connect */
163 transport_endpoint_cleanup (TRANSPORT_PROTO_TCP, &tc->c_lcl_ip,
166 /* Check if connection is not yet fully established */
167 if (tc->state == TCP_STATE_SYN_SENT)
169 /* Try to remove the half-open connection. If this is not the owning
170 * thread, tc won't be removed. Retransmit or establish timers will
171 * eventually expire and call again cleanup on the right thread. */
172 tcp_half_open_connection_cleanup (tc);
176 int thread_index = tc->c_thread_index;
178 /* Make sure all timers are cleared */
179 tcp_connection_timers_reset (tc);
181 /* Poison the entry */
183 memset (tc, 0xFA, sizeof (*tc));
184 pool_put (tm->connections[thread_index], tc);
189 * Connection removal.
191 * This should be called only once connection enters CLOSED state. Note
192 * that it notifies the session of the removal event, so if the goal is to
193 * just remove the connection, call tcp_connection_cleanup instead.
196 tcp_connection_del (tcp_connection_t * tc)
198 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
199 stream_session_delete_notify (&tc->connection);
200 tcp_connection_cleanup (tc);
204 tcp_connection_new (u8 thread_index)
206 tcp_main_t *tm = vnet_get_tcp_main ();
207 tcp_connection_t *tc;
209 pool_get (tm->connections[thread_index], tc);
210 memset (tc, 0, sizeof (*tc));
211 tc->c_c_index = tc - tm->connections[thread_index];
212 tc->c_thread_index = thread_index;
216 /** Notify session that connection has been reset.
218 * Switch state to closed and wait for session to call cleanup.
221 tcp_connection_reset (tcp_connection_t * tc)
223 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
226 case TCP_STATE_SYN_RCVD:
227 /* Cleanup everything. App wasn't notified yet */
228 stream_session_delete_notify (&tc->connection);
229 tcp_connection_cleanup (tc);
231 case TCP_STATE_SYN_SENT:
232 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
233 tcp_connection_cleanup (tc);
235 case TCP_STATE_ESTABLISHED:
236 stream_session_reset_notify (&tc->connection);
238 case TCP_STATE_CLOSE_WAIT:
239 case TCP_STATE_FIN_WAIT_1:
240 case TCP_STATE_FIN_WAIT_2:
241 case TCP_STATE_CLOSING:
242 tc->state = TCP_STATE_CLOSED;
243 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
245 /* Make sure all timers are cleared */
246 tcp_connection_timers_reset (tc);
248 /* Wait for cleanup from session layer but not forever */
249 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
251 case TCP_STATE_CLOSED:
257 * Begin connection closing procedure.
259 * If at the end the connection is not in CLOSED state, it is not removed.
260 * Instead, we rely on on TCP to advance through state machine to either
261 * 1) LAST_ACK (passive close) whereby when the last ACK is received
262 * tcp_connection_del is called. This notifies session of the delete and
264 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
265 * and cleanup is called.
267 * N.B. Half-close connections are not supported
270 tcp_connection_close (tcp_connection_t * tc)
272 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
274 /* Send/Program FIN if needed and switch state */
277 case TCP_STATE_SYN_SENT:
278 tc->state = TCP_STATE_CLOSED;
280 case TCP_STATE_SYN_RCVD:
282 tc->state = TCP_STATE_FIN_WAIT_1;
284 case TCP_STATE_ESTABLISHED:
285 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
288 tc->flags |= TCP_CONN_FINPNDG;
289 tc->state = TCP_STATE_FIN_WAIT_1;
291 case TCP_STATE_CLOSE_WAIT:
292 tcp_connection_timers_reset (tc);
294 tc->state = TCP_STATE_LAST_ACK;
295 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
297 case TCP_STATE_FIN_WAIT_1:
298 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
301 TCP_DBG ("state: %u", tc->state);
304 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
306 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
307 if (!tcp_timer_is_active (tc, TCP_TIMER_WAITCLOSE)
308 && tc->state == TCP_STATE_CLOSED)
309 tcp_connection_del (tc);
313 tcp_session_close (u32 conn_index, u32 thread_index)
315 tcp_connection_t *tc;
316 tc = tcp_connection_get (conn_index, thread_index);
317 tcp_connection_close (tc);
321 tcp_session_cleanup (u32 conn_index, u32 thread_index)
323 tcp_connection_t *tc;
324 tc = tcp_connection_get (conn_index, thread_index);
325 tcp_connection_timers_reset (tc);
327 /* Wait for the session tx events to clear */
328 tc->state = TCP_STATE_CLOSED;
329 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
330 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
334 * Initialize all connection timers as invalid
337 tcp_connection_timers_init (tcp_connection_t * tc)
341 /* Set all to invalid */
342 for (i = 0; i < TCP_N_TIMERS; i++)
344 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
347 tc->rto = TCP_RTO_INIT;
351 * Stop all connection timers
354 tcp_connection_timers_reset (tcp_connection_t * tc)
357 for (i = 0; i < TCP_N_TIMERS; i++)
359 tcp_timer_reset (tc, i);
364 typedef struct ip4_tcp_hdr
370 typedef struct ip6_tcp_hdr
377 tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
380 const dpo_id_t *choice;
384 lb = load_balance_get (dpo->dpoi_index);
388 memset (&hdr, 0, sizeof (hdr));
389 hdr.ip.protocol = IP_PROTOCOL_TCP;
390 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
391 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
392 hdr.tcp.src_port = tc->c_lcl_port;
393 hdr.tcp.dst_port = tc->c_rmt_port;
394 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
399 memset (&hdr, 0, sizeof (hdr));
400 hdr.ip.protocol = IP_PROTOCOL_TCP;
401 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
402 sizeof (ip6_address_t));
403 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
404 sizeof (ip6_address_t));
405 hdr.tcp.src_port = tc->c_lcl_port;
406 hdr.tcp.dst_port = tc->c_rmt_port;
407 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
409 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
410 dpo_copy (result, choice);
414 tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
419 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
420 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
421 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
422 fib_index = fib_table_find (prefix.fp_proto, tc->c_fib_index);
423 return fib_table_lookup (fib_index, &prefix);
427 tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
429 dpo_id_t choice = DPO_INVALID;
430 u32 output_node_index;
433 fe = fib_entry_get (tc->c_rmt_fei);
434 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
437 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
440 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
441 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
445 /** Stack tcp connection on peer's fib entry.
447 * This ultimately populates the dpo the connection will use to send packets.
450 tcp_connection_fib_attach (tcp_connection_t * tc)
452 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
454 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
456 tcp_connection_stack_on_fib_entry (tc);
461 * Initialize connection send variables.
464 tcp_init_snd_vars (tcp_connection_t * tc)
469 * We use the time to randomize iss and for setting up the initial
470 * timestamp. Make sure it's updated otherwise syn and ack in the
471 * handshake may make it look as if time has flown in the opposite
474 tcp_set_time_now (vlib_get_thread_index ());
475 time_now = tcp_time_now ();
477 tc->iss = random_u32 (&time_now);
478 tc->snd_una = tc->iss;
479 tc->snd_nxt = tc->iss + 1;
480 tc->snd_una_max = tc->snd_nxt;
483 /** Initialize tcp connection variables
485 * Should be called after having received a msg from the peer, i.e., a SYN or
486 * a SYNACK, such that connection options have already been exchanged. */
488 tcp_connection_init_vars (tcp_connection_t * tc)
490 tcp_connection_timers_init (tc);
492 scoreboard_init (&tc->sack_sb);
494 if (tc->state == TCP_STATE_SYN_RCVD)
495 tcp_init_snd_vars (tc);
497 // tcp_connection_fib_attach (tc);
501 tcp_alloc_custom_local_endpoint (tcp_main_t * tm, ip46_address_t * lcl_addr,
502 u16 * lcl_port, u8 is_ip4)
507 index = tm->last_v4_address_rotor++;
508 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
509 tm->last_v4_address_rotor = 0;
510 lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
514 index = tm->last_v6_address_rotor++;
515 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
516 tm->last_v6_address_rotor = 0;
517 clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index],
518 sizeof (ip6_address_t));
520 port = transport_alloc_local_port (TRANSPORT_PROTO_TCP, lcl_addr);
523 clib_warning ("Failed to allocate src port");
531 tcp_connection_open (transport_endpoint_t * rmt)
533 tcp_main_t *tm = vnet_get_tcp_main ();
534 tcp_connection_t *tc;
535 ip46_address_t lcl_addr;
540 * Allocate local endpoint
542 if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
543 || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
544 rv = tcp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
547 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_TCP,
548 rmt, &lcl_addr, &lcl_port);
554 * Create connection and send SYN
556 clib_spinlock_lock_if_init (&tm->half_open_lock);
557 tc = tcp_half_open_connection_new ();
558 ip_copy (&tc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
559 ip_copy (&tc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
560 tc->c_rmt_port = rmt->port;
561 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
562 tc->c_is_ip4 = rmt->is_ip4;
563 tc->c_proto = TRANSPORT_PROTO_TCP;
564 tc->c_fib_index = rmt->fib_index;
565 /* The other connection vars will be initialized after SYN ACK */
566 tcp_connection_timers_init (tc);
568 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
569 tc->state = TCP_STATE_SYN_SENT;
570 tcp_init_snd_vars (tc);
572 clib_spinlock_unlock_if_init (&tm->half_open_lock);
574 return tc->c_c_index;
578 tcp_session_open (transport_endpoint_t * tep)
580 return tcp_connection_open (tep);
583 const char *tcp_dbg_evt_str[] = {
584 #define _(sym, str) str,
589 const char *tcp_fsm_states[] = {
590 #define _(sym, str) str,
591 foreach_tcp_fsm_state
596 format_tcp_state (u8 * s, va_list * args)
598 u32 state = va_arg (*args, u32);
600 if (state < TCP_N_STATES)
601 s = format (s, "%s", tcp_fsm_states[state]);
603 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
607 const char *tcp_connection_flags_str[] = {
608 #define _(sym, str) str,
609 foreach_tcp_connection_flag
614 format_tcp_connection_flags (u8 * s, va_list * args)
616 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
619 for (i = 0; i < TCP_CONN_N_FLAG_BITS; i++)
620 if (tc->flags & (1 << i))
622 for (i = 0; i < last; i++)
624 if (tc->flags & (1 << i))
625 s = format (s, "%s, ", tcp_connection_flags_str[i]);
628 s = format (s, "%s", tcp_connection_flags_str[last]);
632 const char *tcp_conn_timers[] = {
633 #define _(sym, str) str,
639 format_tcp_timers (u8 * s, va_list * args)
641 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
644 for (i = 0; i < TCP_N_TIMERS; i++)
645 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
649 for (i = 0; i < last; i++)
651 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
652 s = format (s, "%s,", tcp_conn_timers[i]);
656 s = format (s, "%s]", tcp_conn_timers[i]);
664 format_tcp_congestion_status (u8 * s, va_list * args)
666 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
667 if (tcp_in_recovery (tc))
668 s = format (s, "recovery");
669 else if (tcp_in_fastrecovery (tc))
670 s = format (s, "fastrecovery");
672 s = format (s, "none");
677 format_tcp_vars (u8 * s, va_list * args)
679 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
680 s = format (s, " flags: %U timers: %U\n", format_tcp_connection_flags, tc,
681 format_tcp_timers, tc);
682 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
683 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
684 tc->snd_una_max - tc->iss);
685 s = format (s, " rcv_nxt %u rcv_las %u\n",
686 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
687 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
688 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
689 tc->snd_wl2 - tc->iss);
690 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
691 tcp_flight_size (tc), tcp_available_output_snd_space (tc),
692 tcp_rcv_wnd_available (tc));
693 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
694 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
695 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
696 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
697 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
699 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
700 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
702 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
703 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
704 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
705 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
706 tcp_time_now () - tc->tsval_recent_age);
707 if (tc->state >= TCP_STATE_ESTABLISHED)
708 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
710 if (vec_len (tc->snd_sacks))
711 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
717 format_tcp_connection_id (u8 * s, va_list * args)
719 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
724 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
725 format_ip4_address, &tc->c_lcl_ip4,
726 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
727 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
731 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
732 format_ip6_address, &tc->c_lcl_ip6,
733 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
734 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
741 format_tcp_connection (u8 * s, va_list * args)
743 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
744 u32 verbose = va_arg (*args, u32);
748 s = format (s, "%-50U", format_tcp_connection_id, tc);
751 s = format (s, "%-15U", format_tcp_state, tc->state);
753 s = format (s, "\n%U", format_tcp_vars, tc);
760 format_tcp_session (u8 * s, va_list * args)
762 u32 tci = va_arg (*args, u32);
763 u32 thread_index = va_arg (*args, u32);
764 u32 verbose = va_arg (*args, u32);
765 tcp_connection_t *tc;
767 tc = tcp_connection_get (tci, thread_index);
769 s = format (s, "%U", format_tcp_connection, tc, verbose);
771 s = format (s, "empty\n");
776 format_tcp_listener_session (u8 * s, va_list * args)
778 u32 tci = va_arg (*args, u32);
779 tcp_connection_t *tc = tcp_listener_get (tci);
780 return format (s, "%U", format_tcp_connection_id, tc);
784 format_tcp_half_open_session (u8 * s, va_list * args)
786 u32 tci = va_arg (*args, u32);
787 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
788 return format (s, "%U", format_tcp_connection_id, tc);
792 format_tcp_sacks (u8 * s, va_list * args)
794 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
795 sack_block_t *sacks = tc->snd_sacks;
799 len = vec_len (sacks);
800 for (i = 0; i < len - 1; i++)
803 s = format (s, " start %u end %u\n", block->start - tc->irs,
804 block->end - tc->irs);
808 block = &sacks[len - 1];
809 s = format (s, " start %u end %u", block->start - tc->irs,
810 block->end - tc->irs);
816 format_tcp_rcv_sacks (u8 * s, va_list * args)
818 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
819 sack_block_t *sacks = tc->rcv_opts.sacks;
823 len = vec_len (sacks);
824 for (i = 0; i < len - 1; i++)
827 s = format (s, " start %u end %u\n", block->start - tc->iss,
828 block->end - tc->iss);
832 block = &sacks[len - 1];
833 s = format (s, " start %u end %u", block->start - tc->iss,
834 block->end - tc->iss);
840 format_tcp_sack_hole (u8 * s, va_list * args)
842 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
843 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
845 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
847 s = format (s, " [%u, %u]", hole->start, hole->end);
852 format_tcp_scoreboard (u8 * s, va_list * args)
854 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
855 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
856 sack_scoreboard_hole_t *hole;
857 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
858 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
859 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
860 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
861 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
862 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
864 hole = scoreboard_first_hole (sb);
866 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
870 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
871 hole = scoreboard_next_hole (sb, hole);
877 transport_connection_t *
878 tcp_session_get_transport (u32 conn_index, u32 thread_index)
880 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
881 return &tc->connection;
884 transport_connection_t *
885 tcp_half_open_session_get_transport (u32 conn_index)
887 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
888 return &tc->connection;
892 * Compute maximum segment size for session layer.
894 * Since the result needs to be the actual data length, it first computes
895 * the tcp options to be used in the next burst and subtracts their
896 * length from the connection's snd_mss.
899 tcp_session_send_mss (transport_connection_t * trans_conn)
901 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
903 /* Ensure snd_mss does accurately reflect the amount of data we can push
904 * in a segment. This also makes sure that options are updated according to
905 * the current state of the connection. */
906 tcp_update_snd_mss (tc);
912 tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
914 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
916 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
919 /* If not snd_wnd constrained and we can't write at least a segment,
920 * don't try at all */
921 if (PREDICT_FALSE (snd_space < tc->snd_mss))
922 return snd_space < tc->cwnd ? 0 : snd_space;
924 /* round down to mss multiple */
925 return snd_space - (snd_space % tc->snd_mss);
929 * Compute tx window session is allowed to fill.
931 * Takes into account available send space, snd_mss and the congestion
932 * state of the connection. If possible, the value returned is a multiple
935 * @param tc tcp connection
936 * @return number of bytes session is allowed to write
939 tcp_snd_space (tcp_connection_t * tc)
941 int snd_space, snt_limited;
943 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
945 snd_space = tcp_available_output_snd_space (tc);
947 /* If we haven't gotten dupacks or if we did and have gotten sacked
948 * bytes then we can still send as per Limited Transmit (RFC3042) */
949 if (PREDICT_FALSE (tc->rcv_dupacks != 0
950 && (tcp_opts_sack_permitted (tc)
951 && tc->sack_sb.last_sacked_bytes == 0)))
953 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
954 tc->limited_transmit = tc->snd_nxt;
955 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
957 snt_limited = tc->snd_nxt - tc->limited_transmit;
958 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
960 return tcp_round_snd_space (tc, snd_space);
963 if (tcp_in_recovery (tc))
965 tc->snd_nxt = tc->snd_una_max;
966 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
967 - (tc->snd_una_max - tc->snd_congestion);
968 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
970 return tcp_round_snd_space (tc, snd_space);
973 /* RFC 5681: When previously unsent data is available and the new value of
974 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
975 * bytes of previously unsent data. */
976 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
978 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
980 tcp_fastrecovery_1_smss_on (tc);
988 tcp_session_send_space (transport_connection_t * trans_conn)
990 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
991 return clib_min (tcp_snd_space (tc),
992 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
996 tcp_rcv_wnd_available (tcp_connection_t * tc)
998 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1002 tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
1004 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1006 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1008 /* This still works if fast retransmit is on */
1009 return (tc->snd_nxt - tc->snd_una);
1013 const static transport_proto_vft_t tcp_proto = {
1014 .bind = tcp_session_bind,
1015 .unbind = tcp_session_unbind,
1016 .push_header = tcp_push_header,
1017 .get_connection = tcp_session_get_transport,
1018 .get_listener = tcp_session_get_listener,
1019 .get_half_open = tcp_half_open_session_get_transport,
1020 .open = tcp_session_open,
1021 .close = tcp_session_close,
1022 .cleanup = tcp_session_cleanup,
1023 .send_mss = tcp_session_send_mss,
1024 .send_space = tcp_session_send_space,
1025 .tx_fifo_offset = tcp_session_tx_fifo_offset,
1026 .format_connection = format_tcp_session,
1027 .format_listener = format_tcp_listener_session,
1028 .format_half_open = format_tcp_half_open_session,
1033 tcp_timer_keep_handler (u32 conn_index)
1035 u32 thread_index = vlib_get_thread_index ();
1036 tcp_connection_t *tc;
1038 tc = tcp_connection_get (conn_index, thread_index);
1039 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1041 tcp_connection_close (tc);
1045 tcp_timer_establish_handler (u32 conn_index)
1047 tcp_connection_t *tc;
1049 tc = tcp_half_open_connection_get (conn_index);
1052 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1053 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
1054 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
1058 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
1059 /* note: the connection may have already disappeared */
1060 if (PREDICT_FALSE (tc == 0))
1062 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
1063 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
1064 /* Start cleanup. App wasn't notified yet so use delete notify as
1065 * opposed to delete to cleanup session layer state. */
1066 stream_session_delete_notify (&tc->connection);
1068 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
1069 tcp_connection_cleanup (tc);
1073 tcp_timer_waitclose_handler (u32 conn_index)
1075 u32 thread_index = vlib_get_thread_index ();
1076 tcp_connection_t *tc;
1078 tc = tcp_connection_get (conn_index, thread_index);
1081 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1083 /* Session didn't come back with a close(). Send FIN either way
1084 * and switch to LAST_ACK. */
1085 if (tc->state == TCP_STATE_CLOSE_WAIT)
1087 if (tc->flags & TCP_CONN_FINSNT)
1089 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1093 tc->state = TCP_STATE_LAST_ACK;
1095 /* Make sure we don't wait in LAST ACK forever */
1096 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1098 /* Don't delete the connection yet */
1102 tcp_connection_del (tc);
1106 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1108 tcp_timer_retransmit_handler,
1109 tcp_timer_delack_handler,
1110 tcp_timer_persist_handler,
1111 tcp_timer_keep_handler,
1112 tcp_timer_waitclose_handler,
1113 tcp_timer_retransmit_syn_handler,
1114 tcp_timer_establish_handler
1119 tcp_expired_timers_dispatch (u32 * expired_timers)
1122 u32 connection_index, timer_id;
1124 for (i = 0; i < vec_len (expired_timers); i++)
1126 /* Get session index and timer id */
1127 connection_index = expired_timers[i] & 0x0FFFFFFF;
1128 timer_id = expired_timers[i] >> 28;
1130 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1132 /* Handle expiration */
1133 (*timer_expiration_handlers[timer_id]) (connection_index);
1138 tcp_initialize_timer_wheels (tcp_main_t * tm)
1140 tw_timer_wheel_16t_2w_512sl_t *tw;
1142 foreach_vlib_main (({
1143 tw = &tm->timer_wheels[ii];
1144 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1145 100e-3 /* timer period 100ms */ , ~0);
1146 tw->last_run_time = vlib_time_now (this_vlib_main);
1152 tcp_main_enable (vlib_main_t * vm)
1154 tcp_main_t *tm = vnet_get_tcp_main ();
1155 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1156 clib_error_t *error = 0;
1159 tcp_connection_t *tc __attribute__ ((unused));
1160 u32 preallocated_connections_per_thread;
1162 if ((error = vlib_call_init_function (vm, ip_main_init)))
1164 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1166 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1173 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
1174 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
1176 /* Register as transport with session layer */
1177 transport_register_protocol (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1178 transport_register_protocol (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
1181 * Initialize data structures
1184 num_threads = 1 /* main thread */ + vtm->n_threads;
1185 vec_validate (tm->connections, num_threads - 1);
1188 * Preallocate connections. Assume that thread 0 won't
1189 * use preallocated threads when running multi-core
1191 if (num_threads == 1)
1194 preallocated_connections_per_thread = tm->preallocated_connections;
1199 preallocated_connections_per_thread =
1200 tm->preallocated_connections / (num_threads - 1);
1202 for (; thread < num_threads; thread++)
1204 if (preallocated_connections_per_thread)
1205 pool_init_fixed (tm->connections[thread],
1206 preallocated_connections_per_thread);
1210 * Use a preallocated half-open connection pool?
1212 if (tm->preallocated_half_open_connections)
1213 pool_init_fixed (tm->half_open_connections,
1214 tm->preallocated_half_open_connections);
1216 /* Initialize per worker thread tx buffers (used for control messages) */
1217 vec_validate (tm->tx_buffers, num_threads - 1);
1219 /* Initialize timer wheels */
1220 vec_validate (tm->timer_wheels, num_threads - 1);
1221 tcp_initialize_timer_wheels (tm);
1223 /* Initialize clocks per tick for TCP timestamp. Used to compute
1224 * monotonically increasing timestamps. */
1225 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1226 / TCP_TSTAMP_RESOLUTION;
1228 if (num_threads > 1)
1230 clib_spinlock_init (&tm->half_open_lock);
1233 vec_validate (tm->tx_frames[0], num_threads - 1);
1234 vec_validate (tm->tx_frames[1], num_threads - 1);
1235 vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
1236 vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
1238 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1239 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1241 vec_validate (tm->time_now, num_threads - 1);
1246 vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1250 if (tcp_main.is_enabled)
1253 return tcp_main_enable (vm);
1257 tcp_main.is_enabled = 0;
1264 tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
1266 tcp_main_t *tm = &tcp_main;
1268 tm->punt_unknown4 = is_add;
1270 tm->punt_unknown6 = is_add;
1274 tcp_init (vlib_main_t * vm)
1276 tcp_main_t *tm = vnet_get_tcp_main ();
1277 ip_main_t *im = &ip_main;
1278 ip_protocol_info_t *pi;
1280 /* Session layer, and by implication tcp, are disabled by default */
1283 /* Register with IP for header parsing */
1284 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1286 return clib_error_return (0, "TCP protocol info AWOL");
1287 pi->format_header = format_tcp_header;
1288 pi->unformat_pg_edit = unformat_pg_tcp_header;
1290 tcp_api_reference ();
1294 VLIB_INIT_FUNCTION (tcp_init);
1296 static clib_error_t *
1297 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1299 tcp_main_t *tm = vnet_get_tcp_main ();
1301 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1304 (input, "preallocated-connections %d",
1305 &tm->preallocated_connections))
1307 else if (unformat (input, "preallocated-half-open-connections %d",
1308 &tm->preallocated_half_open_connections))
1310 else if (unformat (input, "buffer-fail-fraction %f",
1311 &tm->buffer_fail_fraction))
1314 return clib_error_return (0, "unknown input `%U'",
1315 format_unformat_error, input);
1320 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1324 * \brief Configure an ipv4 source address range
1325 * @param vm vlib_main_t pointer
1326 * @param start first ipv4 address in the source address range
1327 * @param end last ipv4 address in the source address range
1328 * @param table_id VRF / table ID, 0 for the default FIB
1329 * @return 0 if all OK, else an error indication from api_errno.h
1333 tcp_configure_v4_source_address_range (vlib_main_t * vm,
1334 ip4_address_t * start,
1335 ip4_address_t * end, u32 table_id)
1337 tcp_main_t *tm = vnet_get_tcp_main ();
1338 vnet_main_t *vnm = vnet_get_main ();
1339 u32 start_host_byte_order, end_host_byte_order;
1340 fib_prefix_t prefix;
1341 vnet_sw_interface_t *si;
1342 fib_node_index_t fei;
1346 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1347 ip4_address_t * hi_addr, u32 fib_index,
1350 memset (&prefix, 0, sizeof (prefix));
1352 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1354 if (fib_index == ~0)
1355 return VNET_API_ERROR_NO_SUCH_FIB;
1357 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1358 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1360 /* sanity check for reversed args or some such */
1361 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1362 return VNET_API_ERROR_INVALID_ARGUMENT;
1364 /* Lookup the last address, to identify the interface involved */
1366 prefix.fp_proto = FIB_PROTOCOL_IP4;
1367 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1369 fei = fib_table_lookup (fib_index, &prefix);
1371 /* Couldn't find route to destination. Bail out. */
1372 if (fei == FIB_NODE_INDEX_INVALID)
1373 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1375 sw_if_index = fib_entry_get_resolving_interface (fei);
1377 /* Enable proxy arp on the interface */
1378 si = vnet_get_sw_interface (vnm, sw_if_index);
1379 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1381 /* Configure proxy arp across the range */
1382 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1389 dpo_id_t dpo = DPO_INVALID;
1391 vec_add1 (tm->ip4_src_addresses, start[0]);
1393 /* Add local adjacencies for the range */
1395 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1398 prefix.fp_proto = FIB_PROTOCOL_IP4;
1399 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1401 fib_table_entry_special_dpo_update (fib_index,
1404 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1407 start_host_byte_order++;
1408 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1410 while (start_host_byte_order <= end_host_byte_order);
1416 * \brief Configure an ipv6 source address range
1417 * @param vm vlib_main_t pointer
1418 * @param start first ipv6 address in the source address range
1419 * @param end last ipv6 address in the source address range
1420 * @param table_id VRF / table ID, 0 for the default FIB
1421 * @return 0 if all OK, else an error indication from api_errno.h
1425 tcp_configure_v6_source_address_range (vlib_main_t * vm,
1426 ip6_address_t * start,
1427 ip6_address_t * end, u32 table_id)
1429 tcp_main_t *tm = vnet_get_tcp_main ();
1430 fib_prefix_t prefix;
1432 fib_node_index_t fei;
1435 memset (&prefix, 0, sizeof (prefix));
1437 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1439 if (fib_index == ~0)
1440 return VNET_API_ERROR_NO_SUCH_FIB;
1446 dpo_id_t dpo = DPO_INVALID;
1448 /* Remember this address */
1449 vec_add1 (tm->ip6_src_addresses, start[0]);
1451 /* Lookup the prefix, to identify the interface involved */
1452 prefix.fp_len = 128;
1453 prefix.fp_proto = FIB_PROTOCOL_IP6;
1454 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1456 fei = fib_table_lookup (fib_index, &prefix);
1458 /* Couldn't find route to destination. Bail out. */
1459 if (fei == FIB_NODE_INDEX_INVALID)
1460 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1462 sw_if_index = fib_entry_get_resolving_interface (fei);
1464 if (sw_if_index == (u32) ~ 0)
1465 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1467 /* Add a proxy neighbor discovery entry for this address */
1468 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1470 /* Add a receive adjacency for this address */
1471 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1474 fib_table_entry_special_dpo_update (fib_index,
1477 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1480 /* Done with the entire range? */
1481 if (!memcmp (start, end, sizeof (start[0])))
1484 /* Increment the address. DGMS. */
1486 for (i = 15; i >= 0; i--)
1489 if (tmp.as_u8[i] != 0)
1497 static clib_error_t *
1498 tcp_src_address (vlib_main_t * vm,
1499 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1501 ip4_address_t v4start, v4end;
1502 ip6_address_t v6start, v6end;
1508 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1510 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1511 unformat_ip4_address, &v4end))
1513 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1515 memcpy (&v4end, &v4start, sizeof (v4start));
1518 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
1519 unformat_ip6_address, &v6end))
1521 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1523 memcpy (&v6end, &v6start, sizeof (v6start));
1526 else if (unformat (input, "fib-table %d", &table_id))
1532 if (!v4set && !v6set)
1533 return clib_error_return (0, "at least one v4 or v6 address required");
1537 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1544 case VNET_API_ERROR_NO_SUCH_FIB:
1545 return clib_error_return (0, "Invalid table-id %d", table_id);
1547 case VNET_API_ERROR_INVALID_ARGUMENT:
1548 return clib_error_return (0, "Invalid address range %U - %U",
1549 format_ip4_address, &v4start,
1550 format_ip4_address, &v4end);
1552 return clib_error_return (0, "error %d", rv);
1558 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1565 case VNET_API_ERROR_NO_SUCH_FIB:
1566 return clib_error_return (0, "Invalid table-id %d", table_id);
1569 return clib_error_return (0, "error %d", rv);
1577 VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1579 .path = "tcp src-address",
1580 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1581 .function = tcp_src_address,
1586 tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1588 #if TCP_SCOREBOARD_TRACE
1590 scoreboard_trace_elt_t *block;
1596 s = format (s, "scoreboard trace:");
1597 vec_foreach (block, sb->trace)
1599 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1600 block->ack, block->snd_una_max, block->group);
1602 s = format (s, "\n");
1610 static clib_error_t *
1611 tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1612 vlib_cli_command_t * cmd_arg)
1614 transport_connection_t *tconn = 0;
1615 tcp_connection_t *tc;
1617 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1619 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1620 TRANSPORT_PROTO_TCP))
1623 return clib_error_return (0, "unknown input `%U'",
1624 format_unformat_error, input);
1627 if (!TCP_SCOREBOARD_TRACE)
1629 vlib_cli_output (vm, "scoreboard tracing not enabled");
1633 tc = tcp_get_connection_from_transport (tconn);
1634 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1635 vlib_cli_output (vm, "%v", s);
1640 VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1642 .path = "show tcp scoreboard trace",
1643 .short_help = "show tcp scoreboard trace <connection>",
1644 .function = tcp_show_scoreboard_trace_fn,
1649 tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1652 scoreboard_trace_elt_t *trace;
1653 u32 next_ack, left, group, has_new_ack = 0;
1654 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1655 sack_block_t *block;
1660 memset (dummy_tc, 0, sizeof (*dummy_tc));
1661 tcp_connection_timers_init (dummy_tc);
1662 scoreboard_init (&dummy_tc->sack_sb);
1663 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1665 #if TCP_SCOREBOARD_TRACE
1666 trace = tc->sack_sb.trace;
1667 trace_len = vec_len (tc->sack_sb.trace);
1673 for (i = 0; i < trace_len; i++)
1675 if (trace[i].ack != 0)
1677 dummy_tc->snd_una = trace[i].ack - 1448;
1678 dummy_tc->snd_una_max = trace[i].ack;
1683 while (left < trace_len)
1685 group = trace[left].group;
1686 vec_reset_length (dummy_tc->rcv_opts.sacks);
1688 while (trace[left].group == group)
1690 if (trace[left].ack != 0)
1693 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1694 trace[left].ack, trace[left].snd_una_max);
1695 dummy_tc->snd_una_max = trace[left].snd_una_max;
1696 next_ack = trace[left].ack;
1702 s = format (s, "[%u, %u], ", trace[left].start,
1704 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1705 block->start = trace[left].start;
1706 block->end = trace[left].end;
1712 tcp_rcv_sacks (dummy_tc, next_ack);
1714 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1717 s = format (s, "result: %U", format_tcp_scoreboard,
1718 &dummy_tc->sack_sb);
1721 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1726 static clib_error_t *
1727 tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1728 vlib_cli_command_t * cmd_arg)
1730 transport_connection_t *tconn = 0;
1731 tcp_connection_t *tc = 0;
1733 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1735 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1736 TRANSPORT_PROTO_TCP))
1739 return clib_error_return (0, "unknown input `%U'",
1740 format_unformat_error, input);
1743 if (!TCP_SCOREBOARD_TRACE)
1745 vlib_cli_output (vm, "scoreboard tracing not enabled");
1749 tc = tcp_get_connection_from_transport (tconn);
1752 vlib_cli_output (vm, "connection not found");
1755 str = tcp_scoreboard_replay (str, tc, 1);
1756 vlib_cli_output (vm, "%v", str);
1761 VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1763 .path = "tcp replay scoreboard",
1764 .short_help = "tcp replay scoreboard <connection>",
1765 .function = tcp_scoreboard_trace_fn,
1769 static clib_error_t *
1770 show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
1771 vlib_cli_command_t * cmd_arg)
1773 tcp_main_t *tm = vnet_get_tcp_main ();
1774 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1775 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
1777 vlib_cli_output (vm, "IPv4 TCP punt: %s",
1778 tm->punt_unknown4 ? "enabled" : "disabled");
1779 vlib_cli_output (vm, "IPv6 TCP punt: %s",
1780 tm->punt_unknown6 ? "enabled" : "disabled");
1784 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
1786 .path = "show tcp punt",
1787 .short_help = "show tcp punt",
1788 .function = show_tcp_punt_fn,
1793 * fd.io coding-style-patch-verification: ON
1796 * eval: (c-set-style "gnu")