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 case TCP_STATE_CLOSE_WAIT:
237 case TCP_STATE_FIN_WAIT_1:
238 case TCP_STATE_FIN_WAIT_2:
239 case TCP_STATE_CLOSING:
240 tc->state = TCP_STATE_CLOSED;
241 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
243 /* Make sure all timers are cleared */
244 tcp_connection_timers_reset (tc);
245 stream_session_reset_notify (&tc->connection);
247 /* Wait for cleanup from session layer but not forever */
248 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
250 case TCP_STATE_CLOSED:
256 * Begin connection closing procedure.
258 * If at the end the connection is not in CLOSED state, it is not removed.
259 * Instead, we rely on on TCP to advance through state machine to either
260 * 1) LAST_ACK (passive close) whereby when the last ACK is received
261 * tcp_connection_del is called. This notifies session of the delete and
263 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
264 * and cleanup is called.
266 * N.B. Half-close connections are not supported
269 tcp_connection_close (tcp_connection_t * tc)
271 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
273 /* Send/Program FIN if needed and switch state */
276 case TCP_STATE_SYN_SENT:
277 tc->state = TCP_STATE_CLOSED;
279 case TCP_STATE_SYN_RCVD:
281 tc->state = TCP_STATE_FIN_WAIT_1;
283 case TCP_STATE_ESTABLISHED:
284 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
287 tc->flags |= TCP_CONN_FINPNDG;
288 tc->state = TCP_STATE_FIN_WAIT_1;
290 case TCP_STATE_CLOSE_WAIT:
291 tcp_connection_timers_reset (tc);
293 tc->state = TCP_STATE_LAST_ACK;
294 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
296 case TCP_STATE_FIN_WAIT_1:
297 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
300 TCP_DBG ("state: %u", tc->state);
303 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
305 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
306 if (!tcp_timer_is_active (tc, TCP_TIMER_WAITCLOSE)
307 && tc->state == TCP_STATE_CLOSED)
308 tcp_connection_del (tc);
312 tcp_session_close (u32 conn_index, u32 thread_index)
314 tcp_connection_t *tc;
315 tc = tcp_connection_get (conn_index, thread_index);
316 tcp_connection_close (tc);
320 tcp_session_cleanup (u32 conn_index, u32 thread_index)
322 tcp_connection_t *tc;
323 tc = tcp_connection_get (conn_index, thread_index);
324 tcp_connection_timers_reset (tc);
326 /* Wait for the session tx events to clear */
327 tc->state = TCP_STATE_CLOSED;
328 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
329 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
333 * Initialize all connection timers as invalid
336 tcp_connection_timers_init (tcp_connection_t * tc)
340 /* Set all to invalid */
341 for (i = 0; i < TCP_N_TIMERS; i++)
343 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
346 tc->rto = TCP_RTO_INIT;
350 * Stop all connection timers
353 tcp_connection_timers_reset (tcp_connection_t * tc)
356 for (i = 0; i < TCP_N_TIMERS; i++)
358 tcp_timer_reset (tc, i);
363 typedef struct ip4_tcp_hdr
369 typedef struct ip6_tcp_hdr
376 tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
379 const dpo_id_t *choice;
383 lb = load_balance_get (dpo->dpoi_index);
387 memset (&hdr, 0, sizeof (hdr));
388 hdr.ip.protocol = IP_PROTOCOL_TCP;
389 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
390 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
391 hdr.tcp.src_port = tc->c_lcl_port;
392 hdr.tcp.dst_port = tc->c_rmt_port;
393 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
398 memset (&hdr, 0, sizeof (hdr));
399 hdr.ip.protocol = IP_PROTOCOL_TCP;
400 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
401 sizeof (ip6_address_t));
402 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
403 sizeof (ip6_address_t));
404 hdr.tcp.src_port = tc->c_lcl_port;
405 hdr.tcp.dst_port = tc->c_rmt_port;
406 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
408 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
409 dpo_copy (result, choice);
413 tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
418 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
419 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
420 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
421 fib_index = fib_table_find (prefix.fp_proto, tc->c_fib_index);
422 return fib_table_lookup (fib_index, &prefix);
426 tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
428 dpo_id_t choice = DPO_INVALID;
429 u32 output_node_index;
432 fe = fib_entry_get (tc->c_rmt_fei);
433 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
436 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
439 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
440 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
444 /** Stack tcp connection on peer's fib entry.
446 * This ultimately populates the dpo the connection will use to send packets.
449 tcp_connection_fib_attach (tcp_connection_t * tc)
451 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
453 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
455 tcp_connection_stack_on_fib_entry (tc);
460 * Initialize connection send variables.
463 tcp_init_snd_vars (tcp_connection_t * tc)
468 * We use the time to randomize iss and for setting up the initial
469 * timestamp. Make sure it's updated otherwise syn and ack in the
470 * handshake may make it look as if time has flown in the opposite
473 tcp_set_time_now (vlib_get_thread_index ());
474 time_now = tcp_time_now ();
476 tc->iss = random_u32 (&time_now);
477 tc->snd_una = tc->iss;
478 tc->snd_nxt = tc->iss + 1;
479 tc->snd_una_max = tc->snd_nxt;
482 /** Initialize tcp connection variables
484 * Should be called after having received a msg from the peer, i.e., a SYN or
485 * a SYNACK, such that connection options have already been exchanged. */
487 tcp_connection_init_vars (tcp_connection_t * tc)
489 tcp_connection_timers_init (tc);
491 scoreboard_init (&tc->sack_sb);
493 if (tc->state == TCP_STATE_SYN_RCVD)
494 tcp_init_snd_vars (tc);
496 // tcp_connection_fib_attach (tc);
500 tcp_alloc_custom_local_endpoint (tcp_main_t * tm, ip46_address_t * lcl_addr,
501 u16 * lcl_port, u8 is_ip4)
506 index = tm->last_v4_address_rotor++;
507 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
508 tm->last_v4_address_rotor = 0;
509 lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
513 index = tm->last_v6_address_rotor++;
514 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
515 tm->last_v6_address_rotor = 0;
516 clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index],
517 sizeof (ip6_address_t));
519 port = transport_alloc_local_port (TRANSPORT_PROTO_TCP, lcl_addr);
522 clib_warning ("Failed to allocate src port");
530 tcp_connection_open (transport_endpoint_t * rmt)
532 tcp_main_t *tm = vnet_get_tcp_main ();
533 tcp_connection_t *tc;
534 ip46_address_t lcl_addr;
539 * Allocate local endpoint
541 if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
542 || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
543 rv = tcp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
546 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_TCP,
547 rmt, &lcl_addr, &lcl_port);
553 * Create connection and send SYN
555 clib_spinlock_lock_if_init (&tm->half_open_lock);
556 tc = tcp_half_open_connection_new ();
557 ip_copy (&tc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
558 ip_copy (&tc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
559 tc->c_rmt_port = rmt->port;
560 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
561 tc->c_is_ip4 = rmt->is_ip4;
562 tc->c_proto = TRANSPORT_PROTO_TCP;
563 tc->c_fib_index = rmt->fib_index;
564 /* The other connection vars will be initialized after SYN ACK */
565 tcp_connection_timers_init (tc);
567 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
568 tc->state = TCP_STATE_SYN_SENT;
569 tcp_init_snd_vars (tc);
571 clib_spinlock_unlock_if_init (&tm->half_open_lock);
573 return tc->c_c_index;
577 tcp_session_open (transport_endpoint_t * tep)
579 return tcp_connection_open (tep);
582 const char *tcp_dbg_evt_str[] = {
583 #define _(sym, str) str,
588 const char *tcp_fsm_states[] = {
589 #define _(sym, str) str,
590 foreach_tcp_fsm_state
595 format_tcp_state (u8 * s, va_list * args)
597 u32 state = va_arg (*args, u32);
599 if (state < TCP_N_STATES)
600 s = format (s, "%s", tcp_fsm_states[state]);
602 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
606 const char *tcp_connection_flags_str[] = {
607 #define _(sym, str) str,
608 foreach_tcp_connection_flag
613 format_tcp_connection_flags (u8 * s, va_list * args)
615 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
618 for (i = 0; i < TCP_CONN_N_FLAG_BITS; i++)
619 if (tc->flags & (1 << i))
621 for (i = 0; i < last; i++)
623 if (tc->flags & (1 << i))
624 s = format (s, "%s, ", tcp_connection_flags_str[i]);
627 s = format (s, "%s", tcp_connection_flags_str[last]);
631 const char *tcp_conn_timers[] = {
632 #define _(sym, str) str,
638 format_tcp_timers (u8 * s, va_list * args)
640 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
643 for (i = 0; i < TCP_N_TIMERS; i++)
644 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
648 for (i = 0; i < last; i++)
650 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
651 s = format (s, "%s,", tcp_conn_timers[i]);
655 s = format (s, "%s]", tcp_conn_timers[i]);
663 format_tcp_congestion_status (u8 * s, va_list * args)
665 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
666 if (tcp_in_recovery (tc))
667 s = format (s, "recovery");
668 else if (tcp_in_fastrecovery (tc))
669 s = format (s, "fastrecovery");
671 s = format (s, "none");
676 format_tcp_vars (u8 * s, va_list * args)
678 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
679 s = format (s, " flags: %U timers: %U\n", format_tcp_connection_flags, tc,
680 format_tcp_timers, tc);
681 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
682 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
683 tc->snd_una_max - tc->iss);
684 s = format (s, " rcv_nxt %u rcv_las %u\n",
685 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
686 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
687 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
688 tc->snd_wl2 - tc->iss);
689 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
690 tcp_flight_size (tc), tcp_available_output_snd_space (tc),
691 tcp_rcv_wnd_available (tc));
692 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
693 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
694 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
695 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
696 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
698 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
699 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
701 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
702 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
703 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
704 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
705 tcp_time_now () - tc->tsval_recent_age);
706 if (tc->state >= TCP_STATE_ESTABLISHED)
707 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
709 if (vec_len (tc->snd_sacks))
710 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
716 format_tcp_connection_id (u8 * s, va_list * args)
718 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
723 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
724 format_ip4_address, &tc->c_lcl_ip4,
725 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
726 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
730 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
731 format_ip6_address, &tc->c_lcl_ip6,
732 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
733 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
740 format_tcp_connection (u8 * s, va_list * args)
742 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
743 u32 verbose = va_arg (*args, u32);
747 s = format (s, "%-50U", format_tcp_connection_id, tc);
750 s = format (s, "%-15U", format_tcp_state, tc->state);
752 s = format (s, "\n%U", format_tcp_vars, tc);
759 format_tcp_session (u8 * s, va_list * args)
761 u32 tci = va_arg (*args, u32);
762 u32 thread_index = va_arg (*args, u32);
763 u32 verbose = va_arg (*args, u32);
764 tcp_connection_t *tc;
766 tc = tcp_connection_get (tci, thread_index);
768 s = format (s, "%U", format_tcp_connection, tc, verbose);
770 s = format (s, "empty\n");
775 format_tcp_listener_session (u8 * s, va_list * args)
777 u32 tci = va_arg (*args, u32);
778 tcp_connection_t *tc = tcp_listener_get (tci);
779 return format (s, "%U", format_tcp_connection_id, tc);
783 format_tcp_half_open_session (u8 * s, va_list * args)
785 u32 tci = va_arg (*args, u32);
786 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
787 return format (s, "%U", format_tcp_connection_id, tc);
791 format_tcp_sacks (u8 * s, va_list * args)
793 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
794 sack_block_t *sacks = tc->snd_sacks;
798 len = vec_len (sacks);
799 for (i = 0; i < len - 1; i++)
802 s = format (s, " start %u end %u\n", block->start - tc->irs,
803 block->end - tc->irs);
807 block = &sacks[len - 1];
808 s = format (s, " start %u end %u", block->start - tc->irs,
809 block->end - tc->irs);
815 format_tcp_rcv_sacks (u8 * s, va_list * args)
817 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
818 sack_block_t *sacks = tc->rcv_opts.sacks;
822 len = vec_len (sacks);
823 for (i = 0; i < len - 1; i++)
826 s = format (s, " start %u end %u\n", block->start - tc->iss,
827 block->end - tc->iss);
831 block = &sacks[len - 1];
832 s = format (s, " start %u end %u", block->start - tc->iss,
833 block->end - tc->iss);
839 format_tcp_sack_hole (u8 * s, va_list * args)
841 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
842 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
844 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
846 s = format (s, " [%u, %u]", hole->start, hole->end);
851 format_tcp_scoreboard (u8 * s, va_list * args)
853 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
854 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
855 sack_scoreboard_hole_t *hole;
856 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
857 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
858 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
859 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
860 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
861 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
863 hole = scoreboard_first_hole (sb);
865 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
869 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
870 hole = scoreboard_next_hole (sb, hole);
876 transport_connection_t *
877 tcp_session_get_transport (u32 conn_index, u32 thread_index)
879 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
880 return &tc->connection;
883 transport_connection_t *
884 tcp_half_open_session_get_transport (u32 conn_index)
886 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
887 return &tc->connection;
891 * Compute maximum segment size for session layer.
893 * Since the result needs to be the actual data length, it first computes
894 * the tcp options to be used in the next burst and subtracts their
895 * length from the connection's snd_mss.
898 tcp_session_send_mss (transport_connection_t * trans_conn)
900 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
902 /* Ensure snd_mss does accurately reflect the amount of data we can push
903 * in a segment. This also makes sure that options are updated according to
904 * the current state of the connection. */
905 tcp_update_snd_mss (tc);
911 tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
913 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
915 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
918 /* If not snd_wnd constrained and we can't write at least a segment,
919 * don't try at all */
920 if (PREDICT_FALSE (snd_space < tc->snd_mss))
921 return snd_space < tc->cwnd ? 0 : snd_space;
923 /* round down to mss multiple */
924 return snd_space - (snd_space % tc->snd_mss);
928 * Compute tx window session is allowed to fill.
930 * Takes into account available send space, snd_mss and the congestion
931 * state of the connection. If possible, the value returned is a multiple
934 * @param tc tcp connection
935 * @return number of bytes session is allowed to write
938 tcp_snd_space (tcp_connection_t * tc)
940 int snd_space, snt_limited;
942 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
944 snd_space = tcp_available_output_snd_space (tc);
946 /* If we haven't gotten dupacks or if we did and have gotten sacked
947 * bytes then we can still send as per Limited Transmit (RFC3042) */
948 if (PREDICT_FALSE (tc->rcv_dupacks != 0
949 && (tcp_opts_sack_permitted (tc)
950 && tc->sack_sb.last_sacked_bytes == 0)))
952 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
953 tc->limited_transmit = tc->snd_nxt;
954 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
956 snt_limited = tc->snd_nxt - tc->limited_transmit;
957 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
959 return tcp_round_snd_space (tc, snd_space);
962 if (tcp_in_recovery (tc))
964 tc->snd_nxt = tc->snd_una_max;
965 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
966 - (tc->snd_una_max - tc->snd_congestion);
967 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
969 return tcp_round_snd_space (tc, snd_space);
972 /* RFC 5681: When previously unsent data is available and the new value of
973 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
974 * bytes of previously unsent data. */
975 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
977 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
979 tcp_fastrecovery_1_smss_on (tc);
987 tcp_session_send_space (transport_connection_t * trans_conn)
989 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
990 return clib_min (tcp_snd_space (tc),
991 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
995 tcp_rcv_wnd_available (tcp_connection_t * tc)
997 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1001 tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
1003 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1005 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1007 /* This still works if fast retransmit is on */
1008 return (tc->snd_nxt - tc->snd_una);
1012 const static transport_proto_vft_t tcp_proto = {
1013 .bind = tcp_session_bind,
1014 .unbind = tcp_session_unbind,
1015 .push_header = tcp_push_header,
1016 .get_connection = tcp_session_get_transport,
1017 .get_listener = tcp_session_get_listener,
1018 .get_half_open = tcp_half_open_session_get_transport,
1019 .open = tcp_session_open,
1020 .close = tcp_session_close,
1021 .cleanup = tcp_session_cleanup,
1022 .send_mss = tcp_session_send_mss,
1023 .send_space = tcp_session_send_space,
1024 .tx_fifo_offset = tcp_session_tx_fifo_offset,
1025 .format_connection = format_tcp_session,
1026 .format_listener = format_tcp_listener_session,
1027 .format_half_open = format_tcp_half_open_session,
1032 tcp_timer_keep_handler (u32 conn_index)
1034 u32 thread_index = vlib_get_thread_index ();
1035 tcp_connection_t *tc;
1037 tc = tcp_connection_get (conn_index, thread_index);
1038 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1040 tcp_connection_close (tc);
1044 tcp_timer_establish_handler (u32 conn_index)
1046 tcp_connection_t *tc;
1048 tc = tcp_half_open_connection_get (conn_index);
1051 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1052 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
1053 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
1057 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
1058 /* note: the connection may have already disappeared */
1059 if (PREDICT_FALSE (tc == 0))
1061 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
1062 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
1063 /* Start cleanup. App wasn't notified yet so use delete notify as
1064 * opposed to delete to cleanup session layer state. */
1065 stream_session_delete_notify (&tc->connection);
1067 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
1068 tcp_connection_cleanup (tc);
1072 tcp_timer_waitclose_handler (u32 conn_index)
1074 u32 thread_index = vlib_get_thread_index ();
1075 tcp_connection_t *tc;
1077 tc = tcp_connection_get (conn_index, thread_index);
1080 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1082 /* Session didn't come back with a close(). Send FIN either way
1083 * and switch to LAST_ACK. */
1084 if (tc->state == TCP_STATE_CLOSE_WAIT)
1086 if (tc->flags & TCP_CONN_FINSNT)
1088 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1092 tc->state = TCP_STATE_LAST_ACK;
1094 /* Make sure we don't wait in LAST ACK forever */
1095 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1097 /* Don't delete the connection yet */
1101 tcp_connection_del (tc);
1105 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1107 tcp_timer_retransmit_handler,
1108 tcp_timer_delack_handler,
1109 tcp_timer_persist_handler,
1110 tcp_timer_keep_handler,
1111 tcp_timer_waitclose_handler,
1112 tcp_timer_retransmit_syn_handler,
1113 tcp_timer_establish_handler
1118 tcp_expired_timers_dispatch (u32 * expired_timers)
1121 u32 connection_index, timer_id;
1123 for (i = 0; i < vec_len (expired_timers); i++)
1125 /* Get session index and timer id */
1126 connection_index = expired_timers[i] & 0x0FFFFFFF;
1127 timer_id = expired_timers[i] >> 28;
1129 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1131 /* Handle expiration */
1132 (*timer_expiration_handlers[timer_id]) (connection_index);
1137 tcp_initialize_timer_wheels (tcp_main_t * tm)
1139 tw_timer_wheel_16t_2w_512sl_t *tw;
1141 foreach_vlib_main (({
1142 tw = &tm->timer_wheels[ii];
1143 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1144 100e-3 /* timer period 100ms */ , ~0);
1145 tw->last_run_time = vlib_time_now (this_vlib_main);
1151 tcp_main_enable (vlib_main_t * vm)
1153 tcp_main_t *tm = vnet_get_tcp_main ();
1154 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1155 clib_error_t *error = 0;
1158 tcp_connection_t *tc __attribute__ ((unused));
1159 u32 preallocated_connections_per_thread;
1161 if ((error = vlib_call_init_function (vm, ip_main_init)))
1163 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1165 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1172 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
1173 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
1175 /* Register as transport with session layer */
1176 transport_register_protocol (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1177 transport_register_protocol (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
1180 * Initialize data structures
1183 num_threads = 1 /* main thread */ + vtm->n_threads;
1184 vec_validate (tm->connections, num_threads - 1);
1187 * Preallocate connections. Assume that thread 0 won't
1188 * use preallocated threads when running multi-core
1190 if (num_threads == 1)
1193 preallocated_connections_per_thread = tm->preallocated_connections;
1198 preallocated_connections_per_thread =
1199 tm->preallocated_connections / (num_threads - 1);
1201 for (; thread < num_threads; thread++)
1203 if (preallocated_connections_per_thread)
1204 pool_init_fixed (tm->connections[thread],
1205 preallocated_connections_per_thread);
1209 * Use a preallocated half-open connection pool?
1211 if (tm->preallocated_half_open_connections)
1212 pool_init_fixed (tm->half_open_connections,
1213 tm->preallocated_half_open_connections);
1215 /* Initialize per worker thread tx buffers (used for control messages) */
1216 vec_validate (tm->tx_buffers, num_threads - 1);
1218 /* Initialize timer wheels */
1219 vec_validate (tm->timer_wheels, num_threads - 1);
1220 tcp_initialize_timer_wheels (tm);
1222 /* Initialize clocks per tick for TCP timestamp. Used to compute
1223 * monotonically increasing timestamps. */
1224 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1225 / TCP_TSTAMP_RESOLUTION;
1227 if (num_threads > 1)
1229 clib_spinlock_init (&tm->half_open_lock);
1232 vec_validate (tm->tx_frames[0], num_threads - 1);
1233 vec_validate (tm->tx_frames[1], num_threads - 1);
1234 vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
1235 vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
1237 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1238 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1240 vec_validate (tm->time_now, num_threads - 1);
1245 vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1249 if (tcp_main.is_enabled)
1252 return tcp_main_enable (vm);
1256 tcp_main.is_enabled = 0;
1263 tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
1265 tcp_main_t *tm = &tcp_main;
1267 tm->punt_unknown4 = is_add;
1269 tm->punt_unknown6 = is_add;
1273 tcp_init (vlib_main_t * vm)
1275 tcp_main_t *tm = vnet_get_tcp_main ();
1276 ip_main_t *im = &ip_main;
1277 ip_protocol_info_t *pi;
1279 /* Session layer, and by implication tcp, are disabled by default */
1282 /* Register with IP for header parsing */
1283 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1285 return clib_error_return (0, "TCP protocol info AWOL");
1286 pi->format_header = format_tcp_header;
1287 pi->unformat_pg_edit = unformat_pg_tcp_header;
1289 tcp_api_reference ();
1293 VLIB_INIT_FUNCTION (tcp_init);
1295 static clib_error_t *
1296 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1298 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, "local-endpoints-table-memory %U",
1311 unformat_memory_size, &tmp))
1313 if (tmp >= 0x100000000)
1314 return clib_error_return (0, "memory size %llx (%lld) too large",
1316 tm->local_endpoints_table_memory = tmp;
1318 else if (unformat (input, "local-endpoints-table-buckets %d",
1319 &tm->local_endpoints_table_buckets))
1324 return clib_error_return (0, "unknown input `%U'",
1325 format_unformat_error, input);
1330 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1334 * \brief Configure an ipv4 source address range
1335 * @param vm vlib_main_t pointer
1336 * @param start first ipv4 address in the source address range
1337 * @param end last ipv4 address in the source address range
1338 * @param table_id VRF / table ID, 0 for the default FIB
1339 * @return 0 if all OK, else an error indication from api_errno.h
1343 tcp_configure_v4_source_address_range (vlib_main_t * vm,
1344 ip4_address_t * start,
1345 ip4_address_t * end, u32 table_id)
1347 tcp_main_t *tm = vnet_get_tcp_main ();
1348 vnet_main_t *vnm = vnet_get_main ();
1349 u32 start_host_byte_order, end_host_byte_order;
1350 fib_prefix_t prefix;
1351 vnet_sw_interface_t *si;
1352 fib_node_index_t fei;
1356 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1357 ip4_address_t * hi_addr, u32 fib_index,
1360 memset (&prefix, 0, sizeof (prefix));
1362 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1364 if (fib_index == ~0)
1365 return VNET_API_ERROR_NO_SUCH_FIB;
1367 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1368 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1370 /* sanity check for reversed args or some such */
1371 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1372 return VNET_API_ERROR_INVALID_ARGUMENT;
1374 /* Lookup the last address, to identify the interface involved */
1376 prefix.fp_proto = FIB_PROTOCOL_IP4;
1377 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1379 fei = fib_table_lookup (fib_index, &prefix);
1381 /* Couldn't find route to destination. Bail out. */
1382 if (fei == FIB_NODE_INDEX_INVALID)
1383 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1385 sw_if_index = fib_entry_get_resolving_interface (fei);
1387 /* Enable proxy arp on the interface */
1388 si = vnet_get_sw_interface (vnm, sw_if_index);
1389 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1391 /* Configure proxy arp across the range */
1392 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1399 dpo_id_t dpo = DPO_INVALID;
1401 vec_add1 (tm->ip4_src_addresses, start[0]);
1403 /* Add local adjacencies for the range */
1405 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1408 prefix.fp_proto = FIB_PROTOCOL_IP4;
1409 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1411 fib_table_entry_special_dpo_update (fib_index,
1414 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1417 start_host_byte_order++;
1418 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1420 while (start_host_byte_order <= end_host_byte_order);
1426 * \brief Configure an ipv6 source address range
1427 * @param vm vlib_main_t pointer
1428 * @param start first ipv6 address in the source address range
1429 * @param end last ipv6 address in the source address range
1430 * @param table_id VRF / table ID, 0 for the default FIB
1431 * @return 0 if all OK, else an error indication from api_errno.h
1435 tcp_configure_v6_source_address_range (vlib_main_t * vm,
1436 ip6_address_t * start,
1437 ip6_address_t * end, u32 table_id)
1439 tcp_main_t *tm = vnet_get_tcp_main ();
1440 fib_prefix_t prefix;
1442 fib_node_index_t fei;
1445 memset (&prefix, 0, sizeof (prefix));
1447 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1449 if (fib_index == ~0)
1450 return VNET_API_ERROR_NO_SUCH_FIB;
1456 dpo_id_t dpo = DPO_INVALID;
1458 /* Remember this address */
1459 vec_add1 (tm->ip6_src_addresses, start[0]);
1461 /* Lookup the prefix, to identify the interface involved */
1462 prefix.fp_len = 128;
1463 prefix.fp_proto = FIB_PROTOCOL_IP6;
1464 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1466 fei = fib_table_lookup (fib_index, &prefix);
1468 /* Couldn't find route to destination. Bail out. */
1469 if (fei == FIB_NODE_INDEX_INVALID)
1470 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1472 sw_if_index = fib_entry_get_resolving_interface (fei);
1474 if (sw_if_index == (u32) ~ 0)
1475 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1477 /* Add a proxy neighbor discovery entry for this address */
1478 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1480 /* Add a receive adjacency for this address */
1481 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1484 fib_table_entry_special_dpo_update (fib_index,
1487 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1490 /* Done with the entire range? */
1491 if (!memcmp (start, end, sizeof (start[0])))
1494 /* Increment the address. DGMS. */
1496 for (i = 15; i >= 0; i--)
1499 if (tmp.as_u8[i] != 0)
1507 static clib_error_t *
1508 tcp_src_address (vlib_main_t * vm,
1509 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1511 ip4_address_t v4start, v4end;
1512 ip6_address_t v6start, v6end;
1518 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1520 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1521 unformat_ip4_address, &v4end))
1523 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1525 memcpy (&v4end, &v4start, sizeof (v4start));
1528 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
1529 unformat_ip6_address, &v6end))
1531 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1533 memcpy (&v6end, &v6start, sizeof (v6start));
1536 else if (unformat (input, "fib-table %d", &table_id))
1542 if (!v4set && !v6set)
1543 return clib_error_return (0, "at least one v4 or v6 address required");
1547 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1554 case VNET_API_ERROR_NO_SUCH_FIB:
1555 return clib_error_return (0, "Invalid table-id %d", table_id);
1557 case VNET_API_ERROR_INVALID_ARGUMENT:
1558 return clib_error_return (0, "Invalid address range %U - %U",
1559 format_ip4_address, &v4start,
1560 format_ip4_address, &v4end);
1562 return clib_error_return (0, "error %d", rv);
1568 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1575 case VNET_API_ERROR_NO_SUCH_FIB:
1576 return clib_error_return (0, "Invalid table-id %d", table_id);
1579 return clib_error_return (0, "error %d", rv);
1587 VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1589 .path = "tcp src-address",
1590 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1591 .function = tcp_src_address,
1596 tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1598 #if TCP_SCOREBOARD_TRACE
1600 scoreboard_trace_elt_t *block;
1606 s = format (s, "scoreboard trace:");
1607 vec_foreach (block, sb->trace)
1609 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1610 block->ack, block->snd_una_max, block->group);
1612 s = format (s, "\n");
1620 static clib_error_t *
1621 tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1622 vlib_cli_command_t * cmd_arg)
1624 transport_connection_t *tconn = 0;
1625 tcp_connection_t *tc;
1627 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1629 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1630 TRANSPORT_PROTO_TCP))
1633 return clib_error_return (0, "unknown input `%U'",
1634 format_unformat_error, input);
1637 if (!TCP_SCOREBOARD_TRACE)
1639 vlib_cli_output (vm, "scoreboard tracing not enabled");
1643 tc = tcp_get_connection_from_transport (tconn);
1644 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1645 vlib_cli_output (vm, "%v", s);
1650 VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1652 .path = "show tcp scoreboard trace",
1653 .short_help = "show tcp scoreboard trace <connection>",
1654 .function = tcp_show_scoreboard_trace_fn,
1659 tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1662 scoreboard_trace_elt_t *trace;
1663 u32 next_ack, left, group, has_new_ack = 0;
1664 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1665 sack_block_t *block;
1670 memset (dummy_tc, 0, sizeof (*dummy_tc));
1671 tcp_connection_timers_init (dummy_tc);
1672 scoreboard_init (&dummy_tc->sack_sb);
1673 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1675 #if TCP_SCOREBOARD_TRACE
1676 trace = tc->sack_sb.trace;
1677 trace_len = vec_len (tc->sack_sb.trace);
1683 for (i = 0; i < trace_len; i++)
1685 if (trace[i].ack != 0)
1687 dummy_tc->snd_una = trace[i].ack - 1448;
1688 dummy_tc->snd_una_max = trace[i].ack;
1693 while (left < trace_len)
1695 group = trace[left].group;
1696 vec_reset_length (dummy_tc->rcv_opts.sacks);
1698 while (trace[left].group == group)
1700 if (trace[left].ack != 0)
1703 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1704 trace[left].ack, trace[left].snd_una_max);
1705 dummy_tc->snd_una_max = trace[left].snd_una_max;
1706 next_ack = trace[left].ack;
1712 s = format (s, "[%u, %u], ", trace[left].start,
1714 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1715 block->start = trace[left].start;
1716 block->end = trace[left].end;
1722 tcp_rcv_sacks (dummy_tc, next_ack);
1724 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1727 s = format (s, "result: %U", format_tcp_scoreboard,
1728 &dummy_tc->sack_sb);
1731 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1736 static clib_error_t *
1737 tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1738 vlib_cli_command_t * cmd_arg)
1740 transport_connection_t *tconn = 0;
1741 tcp_connection_t *tc = 0;
1743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1745 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1746 TRANSPORT_PROTO_TCP))
1749 return clib_error_return (0, "unknown input `%U'",
1750 format_unformat_error, input);
1753 if (!TCP_SCOREBOARD_TRACE)
1755 vlib_cli_output (vm, "scoreboard tracing not enabled");
1759 tc = tcp_get_connection_from_transport (tconn);
1762 vlib_cli_output (vm, "connection not found");
1765 str = tcp_scoreboard_replay (str, tc, 1);
1766 vlib_cli_output (vm, "%v", str);
1771 VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1773 .path = "tcp replay scoreboard",
1774 .short_help = "tcp replay scoreboard <connection>",
1775 .function = tcp_scoreboard_trace_fn,
1779 static clib_error_t *
1780 show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
1781 vlib_cli_command_t * cmd_arg)
1783 tcp_main_t *tm = vnet_get_tcp_main ();
1784 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1785 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
1787 vlib_cli_output (vm, "IPv4 TCP punt: %s",
1788 tm->punt_unknown4 ? "enabled" : "disabled");
1789 vlib_cli_output (vm, "IPv6 TCP punt: %s",
1790 tm->punt_unknown6 ? "enabled" : "disabled");
1794 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
1796 .path = "show tcp punt",
1797 .short_help = "show tcp punt",
1798 .function = show_tcp_punt_fn,
1803 * fd.io coding-style-patch-verification: ON
1806 * eval: (c-set-style "gnu")