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;
37 pool_get (tm->listener_pool, listener);
38 memset (listener, 0, sizeof (*listener));
40 listener->c_c_index = listener - tm->listener_pool;
41 listener->c_lcl_port = clib_host_to_net_u16 (lcl->port);
45 listener->c_lcl_ip4.as_u32 = lcl->ip.ip4.as_u32;
46 listener->c_is_ip4 = 1;
50 clib_memcpy (&listener->c_lcl_ip6, &lcl->ip.ip6,
51 sizeof (ip6_address_t));
54 listener->c_transport_proto = TRANSPORT_PROTO_TCP;
55 listener->c_s_index = session_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 transport_endpoint_del (u32 tepi)
107 tcp_main_t *tm = vnet_get_tcp_main ();
108 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
109 pool_put_index (tm->local_endpoints, tepi);
110 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
113 always_inline transport_endpoint_t *
114 transport_endpoint_new (void)
116 tcp_main_t *tm = vnet_get_tcp_main ();
117 transport_endpoint_t *tep;
118 pool_get (tm->local_endpoints, tep);
123 * Cleanup half-open connection
127 tcp_half_open_connection_del (tcp_connection_t * tc)
129 tcp_main_t *tm = vnet_get_tcp_main ();
130 clib_spinlock_lock_if_init (&tm->half_open_lock);
131 pool_put_index (tm->half_open_connections, tc->c_c_index);
133 memset (tc, 0xFA, sizeof (*tc));
134 clib_spinlock_unlock_if_init (&tm->half_open_lock);
138 * Try to cleanup half-open connection
140 * If called from a thread that doesn't own tc, the call won't have any
143 * @param tc - connection to be cleaned up
144 * @return non-zero if cleanup failed.
147 tcp_half_open_connection_cleanup (tcp_connection_t * tc)
149 /* Make sure this is the owning thread */
150 if (tc->c_thread_index != vlib_get_thread_index ())
152 tcp_timer_reset (tc, TCP_TIMER_ESTABLISH);
153 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN);
154 tcp_half_open_connection_del (tc);
159 tcp_half_open_connection_new (void)
161 tcp_main_t *tm = vnet_get_tcp_main ();
162 tcp_connection_t *tc = 0;
163 pool_get (tm->half_open_connections, tc);
164 memset (tc, 0, sizeof (*tc));
165 tc->c_c_index = tc - tm->half_open_connections;
170 * Cleans up connection state.
175 tcp_connection_cleanup (tcp_connection_t * tc)
177 tcp_main_t *tm = &tcp_main;
179 transport_endpoint_t *tep;
181 /* Cleanup local endpoint if this was an active connect */
182 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
183 clib_net_to_host_u16 (tc->c_lcl_port));
184 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
186 tep = pool_elt_at_index (tm->local_endpoints, tepi);
187 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
188 transport_endpoint_del (tepi);
191 /* Check if connection is not yet fully established */
192 if (tc->state == TCP_STATE_SYN_SENT)
194 /* Try to remove the half-open connection. If this is not the owning
195 * thread, tc won't be removed. Retransmit or establish timers will
196 * eventually expire and call again cleanup on the right thread. */
197 tcp_half_open_connection_cleanup (tc);
201 int thread_index = tc->c_thread_index;
203 /* Make sure all timers are cleared */
204 tcp_connection_timers_reset (tc);
206 /* Poison the entry */
208 memset (tc, 0xFA, sizeof (*tc));
209 pool_put (tm->connections[thread_index], tc);
214 * Connection removal.
216 * This should be called only once connection enters CLOSED state. Note
217 * that it notifies the session of the removal event, so if the goal is to
218 * just remove the connection, call tcp_connection_cleanup instead.
221 tcp_connection_del (tcp_connection_t * tc)
223 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
224 stream_session_delete_notify (&tc->connection);
225 tcp_connection_cleanup (tc);
229 tcp_connection_new (u8 thread_index)
231 tcp_main_t *tm = vnet_get_tcp_main ();
232 tcp_connection_t *tc;
234 pool_get (tm->connections[thread_index], tc);
235 memset (tc, 0, sizeof (*tc));
236 tc->c_c_index = tc - tm->connections[thread_index];
237 tc->c_thread_index = thread_index;
241 /** Notify session that connection has been reset.
243 * Switch state to closed and wait for session to call cleanup.
246 tcp_connection_reset (tcp_connection_t * tc)
248 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
251 case TCP_STATE_SYN_RCVD:
252 /* Cleanup everything. App wasn't notified yet */
253 stream_session_delete_notify (&tc->connection);
254 tcp_connection_cleanup (tc);
256 case TCP_STATE_SYN_SENT:
257 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
258 tcp_connection_cleanup (tc);
260 case TCP_STATE_ESTABLISHED:
261 case TCP_STATE_CLOSE_WAIT:
262 case TCP_STATE_FIN_WAIT_1:
263 case TCP_STATE_FIN_WAIT_2:
264 case TCP_STATE_CLOSING:
265 tc->state = TCP_STATE_CLOSED;
266 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
268 /* Make sure all timers are cleared */
269 tcp_connection_timers_reset (tc);
270 stream_session_reset_notify (&tc->connection);
272 /* Wait for cleanup from session layer but not forever */
273 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
275 case TCP_STATE_CLOSED:
281 * Begin connection closing procedure.
283 * If at the end the connection is not in CLOSED state, it is not removed.
284 * Instead, we rely on on TCP to advance through state machine to either
285 * 1) LAST_ACK (passive close) whereby when the last ACK is received
286 * tcp_connection_del is called. This notifies session of the delete and
288 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
289 * and cleanup is called.
291 * N.B. Half-close connections are not supported
294 tcp_connection_close (tcp_connection_t * tc)
296 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
298 /* Send/Program FIN if needed and switch state */
301 case TCP_STATE_SYN_SENT:
302 tc->state = TCP_STATE_CLOSED;
304 case TCP_STATE_SYN_RCVD:
306 tc->state = TCP_STATE_FIN_WAIT_1;
308 case TCP_STATE_ESTABLISHED:
309 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
312 tc->flags |= TCP_CONN_FINPNDG;
313 tc->state = TCP_STATE_FIN_WAIT_1;
315 case TCP_STATE_CLOSE_WAIT:
317 tc->state = TCP_STATE_LAST_ACK;
320 clib_warning ("shouldn't be here");
323 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
325 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
326 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
327 && tc->state == TCP_STATE_CLOSED)
328 tcp_connection_del (tc);
332 tcp_session_close (u32 conn_index, u32 thread_index)
334 tcp_connection_t *tc;
335 tc = tcp_connection_get (conn_index, thread_index);
336 tcp_connection_close (tc);
340 tcp_session_cleanup (u32 conn_index, u32 thread_index)
342 tcp_connection_t *tc;
343 tc = tcp_connection_get (conn_index, thread_index);
345 /* Wait for the session tx events to clear */
346 tc->state = TCP_STATE_CLOSED;
347 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
348 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
352 ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
354 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
355 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
356 ip_interface_address_t *ia = 0;
361 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
363 return ip_interface_address_get_address (lm4, ia);
370 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
373 rv = ip_interface_address_get_address (lm6, ia);
374 /* Trying to use a link-local ip6 src address is a fool's errand */
375 if (!ip6_address_is_link_local_unicast (rv))
384 #define PORT_MASK ((1 << 16)- 1)
386 * Allocate local port and add if successful add entry to local endpoint
387 * table to mark the pair as used.
390 tcp_allocate_local_port (ip46_address_t * ip)
392 tcp_main_t *tm = vnet_get_tcp_main ();
393 transport_endpoint_t *tep;
395 u16 min = 1024, max = 65535; /* XXX configurable ? */
400 /* Only support active opens from thread 0 */
401 ASSERT (vlib_get_thread_index () == 0);
403 /* Search for first free slot */
404 for (tries = 0; tries < limit; tries++)
408 /* Find a port in the specified range */
411 port = random_u32 (&tm->port_allocator_seed) & PORT_MASK;
412 if (PREDICT_TRUE (port >= min && port < max))
417 tei = transport_endpoint_lookup (&tm->local_endpoints_table, ip, port);
418 /* If not found, we're done */
419 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
421 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
422 tep = transport_endpoint_new ();
423 clib_memcpy (&tep->ip, ip, sizeof (*ip));
425 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
426 tep - tm->local_endpoints);
427 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
436 * Initialize all connection timers as invalid
439 tcp_connection_timers_init (tcp_connection_t * tc)
443 /* Set all to invalid */
444 for (i = 0; i < TCP_N_TIMERS; i++)
446 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
449 tc->rto = TCP_RTO_INIT;
453 * Stop all connection timers
456 tcp_connection_timers_reset (tcp_connection_t * tc)
459 for (i = 0; i < TCP_N_TIMERS; i++)
461 tcp_timer_reset (tc, i);
466 typedef struct ip4_tcp_hdr
472 typedef struct ip6_tcp_hdr
479 tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
482 const dpo_id_t *choice;
486 lb = load_balance_get (dpo->dpoi_index);
490 memset (&hdr, 0, sizeof (hdr));
491 hdr.ip.protocol = IP_PROTOCOL_TCP;
492 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
493 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
494 hdr.tcp.src_port = tc->c_lcl_port;
495 hdr.tcp.dst_port = tc->c_rmt_port;
496 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
501 memset (&hdr, 0, sizeof (hdr));
502 hdr.ip.protocol = IP_PROTOCOL_TCP;
503 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
504 sizeof (ip6_address_t));
505 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
506 sizeof (ip6_address_t));
507 hdr.tcp.src_port = tc->c_lcl_port;
508 hdr.tcp.dst_port = tc->c_rmt_port;
509 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
511 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
512 dpo_copy (result, choice);
516 tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
521 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
522 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
523 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
524 fib_index = fib_table_find (prefix.fp_proto, tc->c_vrf);
525 return fib_table_lookup (fib_index, &prefix);
529 tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
531 dpo_id_t choice = DPO_INVALID;
532 u32 output_node_index;
535 fe = fib_entry_get (tc->c_rmt_fei);
536 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
539 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
542 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
543 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
547 /** Stack tcp connection on peer's fib entry.
549 * This ultimately populates the dpo the connection will use to send packets.
552 tcp_connection_fib_attach (tcp_connection_t * tc)
554 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
556 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
558 tcp_connection_stack_on_fib_entry (tc);
562 /** Initialize tcp connection variables
564 * Should be called after having received a msg from the peer, i.e., a SYN or
565 * a SYNACK, such that connection options have already been exchanged. */
567 tcp_connection_init_vars (tcp_connection_t * tc)
569 tcp_connection_timers_init (tc);
571 scoreboard_init (&tc->sack_sb);
573 // tcp_connection_fib_attach (tc);
577 tcp_connection_open (transport_endpoint_t * rmt)
579 tcp_main_t *tm = vnet_get_tcp_main ();
580 tcp_connection_t *tc;
582 fib_node_index_t fei;
583 u32 sw_if_index, fib_index;
584 ip46_address_t lcl_addr;
588 * Find the local address and allocate port
590 memset (&lcl_addr, 0, sizeof (lcl_addr));
592 /* Find a FIB path to the destination */
593 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
594 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
595 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
597 fib_index = fib_table_find (prefix.fp_proto, rmt->vrf);
598 if (fib_index == (u32) ~ 0)
600 clib_warning ("no fib table");
604 fei = fib_table_lookup (fib_index, &prefix);
606 /* Couldn't find route to destination. Bail out. */
607 if (fei == FIB_NODE_INDEX_INVALID)
609 clib_warning ("no route to destination");
613 sw_if_index = fib_entry_get_resolving_interface (fei);
615 if (sw_if_index == (u32) ~ 0)
617 clib_warning ("no resolving interface for %U", format_ip46_address,
618 &rmt->ip, IP46_TYPE_IP4);
626 if (vec_len (tm->ip4_src_addresses))
628 index = tm->last_v4_address_rotor++;
629 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
630 tm->last_v4_address_rotor = 0;
631 lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
635 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
636 lcl_addr.ip4.as_u32 = ip4->as_u32;
644 if (vec_len (tm->ip6_src_addresses))
646 index = tm->last_v6_address_rotor++;
647 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
648 tm->last_v6_address_rotor = 0;
649 clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index],
654 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
657 clib_warning ("no routable ip6 addresses on %U",
658 format_vnet_sw_if_index_name, vnet_get_main (),
663 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
667 /* Allocate source port */
668 lcl_port = tcp_allocate_local_port (&lcl_addr);
671 clib_warning ("Failed to allocate src port");
676 * Create connection and send SYN
678 clib_spinlock_lock_if_init (&tm->half_open_lock);
679 tc = tcp_half_open_connection_new ();
680 clib_memcpy (&tc->c_rmt_ip, &rmt->ip, sizeof (ip46_address_t));
681 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
682 tc->c_rmt_port = clib_host_to_net_u16 (rmt->port);
683 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
684 tc->c_is_ip4 = rmt->is_ip4;
685 tc->c_transport_proto = TRANSPORT_PROTO_TCP;
686 tc->c_vrf = rmt->vrf;
687 /* The other connection vars will be initialized after SYN ACK */
688 tcp_connection_timers_init (tc);
690 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
691 tc->state = TCP_STATE_SYN_SENT;
693 clib_spinlock_unlock_if_init (&tm->half_open_lock);
695 return tc->c_c_index;
699 tcp_session_open (transport_endpoint_t * tep)
701 return tcp_connection_open (tep);
704 const char *tcp_dbg_evt_str[] = {
705 #define _(sym, str) str,
710 const char *tcp_fsm_states[] = {
711 #define _(sym, str) str,
712 foreach_tcp_fsm_state
717 format_tcp_state (u8 * s, va_list * args)
719 u32 state = va_arg (*args, u32);
721 if (state < TCP_N_STATES)
722 s = format (s, "%s", tcp_fsm_states[state]);
724 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
728 const char *tcp_conn_timers[] = {
729 #define _(sym, str) str,
735 format_tcp_timers (u8 * s, va_list * args)
737 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
740 for (i = 0; i < TCP_N_TIMERS; i++)
741 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
745 for (i = 0; i < last; i++)
747 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
748 s = format (s, "%s,", tcp_conn_timers[i]);
752 s = format (s, "%s]", tcp_conn_timers[i]);
760 format_tcp_congestion_status (u8 * s, va_list * args)
762 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
763 if (tcp_in_recovery (tc))
764 s = format (s, "recovery");
765 else if (tcp_in_fastrecovery (tc))
766 s = format (s, "fastrecovery");
768 s = format (s, "none");
773 format_tcp_vars (u8 * s, va_list * args)
775 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
776 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
777 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
778 tc->snd_una_max - tc->iss);
779 s = format (s, " rcv_nxt %u rcv_las %u\n",
780 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
781 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
782 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
783 tc->snd_wl2 - tc->iss);
784 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
785 tcp_flight_size (tc), tcp_available_snd_space (tc),
786 tcp_rcv_wnd_available (tc));
787 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
788 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
789 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
790 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
791 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
793 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
794 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
796 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
797 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
798 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
799 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
800 tcp_time_now () - tc->tsval_recent_age);
801 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
803 if (vec_len (tc->snd_sacks))
804 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
810 format_tcp_connection_id (u8 * s, va_list * args)
812 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
817 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
818 format_ip4_address, &tc->c_lcl_ip4,
819 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
820 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
824 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
825 format_ip6_address, &tc->c_lcl_ip6,
826 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
827 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
834 format_tcp_connection (u8 * s, va_list * args)
836 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
837 u32 verbose = va_arg (*args, u32);
839 s = format (s, "%-50U", format_tcp_connection_id, tc);
842 s = format (s, "%-15U", format_tcp_state, tc->state);
844 s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc);
851 format_tcp_session (u8 * s, va_list * args)
853 u32 tci = va_arg (*args, u32);
854 u32 thread_index = va_arg (*args, u32);
855 u32 verbose = va_arg (*args, u32);
856 tcp_connection_t *tc;
858 tc = tcp_connection_get (tci, thread_index);
860 s = format (s, "%U", format_tcp_connection, tc, verbose);
862 s = format (s, "empty\n");
867 format_tcp_listener_session (u8 * s, va_list * args)
869 u32 tci = va_arg (*args, u32);
870 tcp_connection_t *tc = tcp_listener_get (tci);
871 return format (s, "%U", format_tcp_connection_id, tc);
875 format_tcp_half_open_session (u8 * s, va_list * args)
877 u32 tci = va_arg (*args, u32);
878 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
879 return format (s, "%U", format_tcp_connection_id, tc);
883 format_tcp_sacks (u8 * s, va_list * args)
885 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
886 sack_block_t *sacks = tc->snd_sacks;
890 len = vec_len (sacks);
891 for (i = 0; i < len - 1; i++)
894 s = format (s, " start %u end %u\n", block->start - tc->irs,
895 block->end - tc->irs);
899 block = &sacks[len - 1];
900 s = format (s, " start %u end %u", block->start - tc->irs,
901 block->end - tc->irs);
907 format_tcp_rcv_sacks (u8 * s, va_list * args)
909 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
910 sack_block_t *sacks = tc->rcv_opts.sacks;
914 len = vec_len (sacks);
915 for (i = 0; i < len - 1; i++)
918 s = format (s, " start %u end %u\n", block->start - tc->iss,
919 block->end - tc->iss);
923 block = &sacks[len - 1];
924 s = format (s, " start %u end %u", block->start - tc->iss,
925 block->end - tc->iss);
931 format_tcp_sack_hole (u8 * s, va_list * args)
933 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
934 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
936 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
938 s = format (s, " [%u, %u]", hole->start, hole->end);
943 format_tcp_scoreboard (u8 * s, va_list * args)
945 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
946 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
947 sack_scoreboard_hole_t *hole;
948 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
949 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
950 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
951 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
952 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
953 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
955 hole = scoreboard_first_hole (sb);
957 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
961 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
962 hole = scoreboard_next_hole (sb, hole);
968 transport_connection_t *
969 tcp_session_get_transport (u32 conn_index, u32 thread_index)
971 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
972 return &tc->connection;
975 transport_connection_t *
976 tcp_half_open_session_get_transport (u32 conn_index)
978 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
979 return &tc->connection;
983 * Compute maximum segment size for session layer.
985 * Since the result needs to be the actual data length, it first computes
986 * the tcp options to be used in the next burst and subtracts their
987 * length from the connection's snd_mss.
990 tcp_session_send_mss (transport_connection_t * trans_conn)
992 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
994 /* Ensure snd_mss does accurately reflect the amount of data we can push
995 * in a segment. This also makes sure that options are updated according to
996 * the current state of the connection. */
997 tcp_update_snd_mss (tc);
1003 tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
1005 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
1007 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
1010 /* If not snd_wnd constrained and we can't write at least a segment,
1011 * don't try at all */
1012 if (PREDICT_FALSE (snd_space < tc->snd_mss))
1015 /* round down to mss multiple */
1016 return snd_space - (snd_space % tc->snd_mss);
1020 * Compute tx window session is allowed to fill.
1022 * Takes into account available send space, snd_mss and the congestion
1023 * state of the connection. If possible, the value returned is a multiple
1026 * @param tc tcp connection
1027 * @return number of bytes session is allowed to write
1030 tcp_snd_space (tcp_connection_t * tc)
1032 int snd_space, snt_limited;
1034 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
1036 snd_space = tcp_available_output_snd_space (tc);
1038 /* If we haven't gotten dupacks or if we did and have gotten sacked
1039 * bytes then we can still send as per Limited Transmit (RFC3042) */
1040 if (PREDICT_FALSE (tc->rcv_dupacks != 0
1041 && (tcp_opts_sack_permitted (tc)
1042 && tc->sack_sb.last_sacked_bytes == 0)))
1044 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
1045 tc->limited_transmit = tc->snd_nxt;
1046 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
1048 snt_limited = tc->snd_nxt - tc->limited_transmit;
1049 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
1051 return tcp_round_snd_space (tc, snd_space);
1054 if (tcp_in_recovery (tc))
1056 tc->snd_nxt = tc->snd_una_max;
1057 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
1058 - (tc->snd_una_max - tc->snd_congestion);
1059 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
1061 return tcp_round_snd_space (tc, snd_space);
1064 /* RFC 5681: When previously unsent data is available and the new value of
1065 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
1066 * bytes of previously unsent data. */
1067 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
1069 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
1071 tcp_fastrecovery_1_smss_on (tc);
1079 tcp_session_send_space (transport_connection_t * trans_conn)
1081 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1082 return clib_min (tcp_snd_space (tc),
1083 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
1087 tcp_rcv_wnd_available (tcp_connection_t * tc)
1089 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1093 tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
1095 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1097 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1099 /* This still works if fast retransmit is on */
1100 return (tc->snd_nxt - tc->snd_una);
1104 const static transport_proto_vft_t tcp_proto = {
1105 .bind = tcp_session_bind,
1106 .unbind = tcp_session_unbind,
1107 .push_header = tcp_push_header,
1108 .get_connection = tcp_session_get_transport,
1109 .get_listener = tcp_session_get_listener,
1110 .get_half_open = tcp_half_open_session_get_transport,
1111 .open = tcp_session_open,
1112 .close = tcp_session_close,
1113 .cleanup = tcp_session_cleanup,
1114 .send_mss = tcp_session_send_mss,
1115 .send_space = tcp_session_send_space,
1116 .tx_fifo_offset = tcp_session_tx_fifo_offset,
1117 .format_connection = format_tcp_session,
1118 .format_listener = format_tcp_listener_session,
1119 .format_half_open = format_tcp_half_open_session,
1124 tcp_timer_keep_handler (u32 conn_index)
1126 u32 thread_index = vlib_get_thread_index ();
1127 tcp_connection_t *tc;
1129 tc = tcp_connection_get (conn_index, thread_index);
1130 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1132 tcp_connection_close (tc);
1136 tcp_timer_establish_handler (u32 conn_index)
1138 tcp_connection_t *tc;
1140 tc = tcp_half_open_connection_get (conn_index);
1143 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1144 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
1148 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
1149 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
1151 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
1152 tcp_connection_cleanup (tc);
1156 tcp_timer_waitclose_handler (u32 conn_index)
1158 u32 thread_index = vlib_get_thread_index ();
1159 tcp_connection_t *tc;
1161 tc = tcp_connection_get (conn_index, thread_index);
1164 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1166 /* Session didn't come back with a close(). Send FIN either way
1167 * and switch to LAST_ACK. */
1168 if (tc->state == TCP_STATE_CLOSE_WAIT)
1170 if (tc->flags & TCP_CONN_FINSNT)
1172 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1176 tc->state = TCP_STATE_LAST_ACK;
1178 /* Make sure we don't wait in LAST ACK forever */
1179 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1181 /* Don't delete the connection yet */
1185 tcp_connection_del (tc);
1189 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1191 tcp_timer_retransmit_handler,
1192 tcp_timer_delack_handler,
1193 tcp_timer_persist_handler,
1194 tcp_timer_keep_handler,
1195 tcp_timer_waitclose_handler,
1196 tcp_timer_retransmit_syn_handler,
1197 tcp_timer_establish_handler
1202 tcp_expired_timers_dispatch (u32 * expired_timers)
1205 u32 connection_index, timer_id;
1207 for (i = 0; i < vec_len (expired_timers); i++)
1209 /* Get session index and timer id */
1210 connection_index = expired_timers[i] & 0x0FFFFFFF;
1211 timer_id = expired_timers[i] >> 28;
1213 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1215 /* Handle expiration */
1216 (*timer_expiration_handlers[timer_id]) (connection_index);
1221 tcp_initialize_timer_wheels (tcp_main_t * tm)
1223 tw_timer_wheel_16t_2w_512sl_t *tw;
1225 foreach_vlib_main (({
1226 tw = &tm->timer_wheels[ii];
1227 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1228 100e-3 /* timer period 100ms */ , ~0);
1229 tw->last_run_time = vlib_time_now (this_vlib_main);
1235 tcp_main_enable (vlib_main_t * vm)
1237 tcp_main_t *tm = vnet_get_tcp_main ();
1238 ip_protocol_info_t *pi;
1239 ip_main_t *im = &ip_main;
1240 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1241 clib_error_t *error = 0;
1244 tcp_connection_t *tc __attribute__ ((unused));
1245 u32 preallocated_connections_per_thread;
1247 if ((error = vlib_call_init_function (vm, ip_main_init)))
1249 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1251 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1258 /* Register with IP */
1259 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1261 return clib_error_return (0, "TCP protocol info AWOL");
1262 pi->format_header = format_tcp_header;
1263 pi->unformat_pg_edit = unformat_pg_tcp_header;
1265 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
1266 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
1268 /* Register as transport with session layer */
1269 session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1270 session_register_transport (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
1273 * Initialize data structures
1276 num_threads = 1 /* main thread */ + vtm->n_threads;
1277 vec_validate (tm->connections, num_threads - 1);
1280 * Preallocate connections. Assume that thread 0 won't
1281 * use preallocated threads when running multi-core
1283 if (num_threads == 1)
1286 preallocated_connections_per_thread = tm->preallocated_connections;
1291 preallocated_connections_per_thread =
1292 tm->preallocated_connections / (num_threads - 1);
1294 for (; thread < num_threads; thread++)
1296 for (i = 0; i < preallocated_connections_per_thread; i++)
1297 pool_get (tm->connections[thread], tc);
1299 for (i = 0; i < preallocated_connections_per_thread; i++)
1300 pool_put_index (tm->connections[thread], i);
1304 * Preallocate half-open connections
1306 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1307 pool_get (tm->half_open_connections, tc);
1309 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1310 pool_put_index (tm->half_open_connections, i);
1312 /* Initialize per worker thread tx buffers (used for control messages) */
1313 vec_validate (tm->tx_buffers, num_threads - 1);
1315 /* Initialize timer wheels */
1316 vec_validate (tm->timer_wheels, num_threads - 1);
1317 tcp_initialize_timer_wheels (tm);
1319 /* Initialize clocks per tick for TCP timestamp. Used to compute
1320 * monotonically increasing timestamps. */
1321 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1322 / TCP_TSTAMP_RESOLUTION;
1324 if (tm->local_endpoints_table_buckets == 0)
1325 tm->local_endpoints_table_buckets = 250000;
1326 if (tm->local_endpoints_table_memory == 0)
1327 tm->local_endpoints_table_memory = 512 << 20;
1329 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
1330 tm->local_endpoints_table_buckets,
1331 tm->local_endpoints_table_memory);
1333 /* Initialize [port-allocator] random number seed */
1334 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
1336 if (num_threads > 1)
1338 clib_spinlock_init (&tm->half_open_lock);
1339 clib_spinlock_init (&tm->local_endpoints_lock);
1342 vec_validate (tm->tx_frames[0], num_threads - 1);
1343 vec_validate (tm->tx_frames[1], num_threads - 1);
1345 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1346 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1348 vec_validate (tm->time_now, num_threads - 1);
1353 vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1357 if (tcp_main.is_enabled)
1360 return tcp_main_enable (vm);
1364 tcp_main.is_enabled = 0;
1371 tcp_init (vlib_main_t * vm)
1373 tcp_main_t *tm = vnet_get_tcp_main ();
1375 tcp_api_reference ();
1379 VLIB_INIT_FUNCTION (tcp_init);
1381 static clib_error_t *
1382 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1384 tcp_main_t *tm = vnet_get_tcp_main ();
1387 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1390 (input, "preallocated-connections %d",
1391 &tm->preallocated_connections))
1393 else if (unformat (input, "preallocated-half-open-connections %d",
1394 &tm->preallocated_half_open_connections))
1396 else if (unformat (input, "local-endpoints-table-memory %U",
1397 unformat_memory_size, &tmp))
1399 if (tmp >= 0x100000000)
1400 return clib_error_return (0, "memory size %llx (%lld) too large",
1402 tm->local_endpoints_table_memory = tmp;
1404 else if (unformat (input, "local-endpoints-table-buckets %d",
1405 &tm->local_endpoints_table_buckets))
1410 return clib_error_return (0, "unknown input `%U'",
1411 format_unformat_error, input);
1416 VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1420 * \brief Configure an ipv4 source address range
1421 * @param vm vlib_main_t pointer
1422 * @param start first ipv4 address in the source address range
1423 * @param end last ipv4 address in the source address range
1424 * @param table_id VRF / table ID, 0 for the default FIB
1425 * @return 0 if all OK, else an error indication from api_errno.h
1429 tcp_configure_v4_source_address_range (vlib_main_t * vm,
1430 ip4_address_t * start,
1431 ip4_address_t * end, u32 table_id)
1433 tcp_main_t *tm = vnet_get_tcp_main ();
1434 vnet_main_t *vnm = vnet_get_main ();
1435 u32 start_host_byte_order, end_host_byte_order;
1436 fib_prefix_t prefix;
1437 vnet_sw_interface_t *si;
1438 fib_node_index_t fei;
1442 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1443 ip4_address_t * hi_addr, u32 fib_index,
1446 memset (&prefix, 0, sizeof (prefix));
1448 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1450 if (fib_index == ~0)
1451 return VNET_API_ERROR_NO_SUCH_FIB;
1453 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1454 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1456 /* sanity check for reversed args or some such */
1457 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1458 return VNET_API_ERROR_INVALID_ARGUMENT;
1460 /* Lookup the last address, to identify the interface involved */
1462 prefix.fp_proto = FIB_PROTOCOL_IP4;
1463 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1465 fei = fib_table_lookup (fib_index, &prefix);
1467 /* Couldn't find route to destination. Bail out. */
1468 if (fei == FIB_NODE_INDEX_INVALID)
1469 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1471 sw_if_index = fib_entry_get_resolving_interface (fei);
1473 /* Enable proxy arp on the interface */
1474 si = vnet_get_sw_interface (vnm, sw_if_index);
1475 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1477 /* Configure proxy arp across the range */
1478 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1485 dpo_id_t dpo = DPO_INVALID;
1487 vec_add1 (tm->ip4_src_addresses, start[0]);
1489 /* Add local adjacencies for the range */
1491 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1494 prefix.fp_proto = FIB_PROTOCOL_IP4;
1495 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1497 fib_table_entry_special_dpo_update (fib_index,
1500 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1503 start_host_byte_order++;
1504 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1506 while (start_host_byte_order <= end_host_byte_order);
1512 * \brief Configure an ipv6 source address range
1513 * @param vm vlib_main_t pointer
1514 * @param start first ipv6 address in the source address range
1515 * @param end last ipv6 address in the source address range
1516 * @param table_id VRF / table ID, 0 for the default FIB
1517 * @return 0 if all OK, else an error indication from api_errno.h
1521 tcp_configure_v6_source_address_range (vlib_main_t * vm,
1522 ip6_address_t * start,
1523 ip6_address_t * end, u32 table_id)
1525 tcp_main_t *tm = vnet_get_tcp_main ();
1526 fib_prefix_t prefix;
1528 fib_node_index_t fei;
1531 memset (&prefix, 0, sizeof (prefix));
1533 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1535 if (fib_index == ~0)
1536 return VNET_API_ERROR_NO_SUCH_FIB;
1542 dpo_id_t dpo = DPO_INVALID;
1544 /* Remember this address */
1545 vec_add1 (tm->ip6_src_addresses, start[0]);
1547 /* Lookup the prefix, to identify the interface involved */
1548 prefix.fp_len = 128;
1549 prefix.fp_proto = FIB_PROTOCOL_IP6;
1550 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1552 fei = fib_table_lookup (fib_index, &prefix);
1554 /* Couldn't find route to destination. Bail out. */
1555 if (fei == FIB_NODE_INDEX_INVALID)
1556 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1558 sw_if_index = fib_entry_get_resolving_interface (fei);
1560 if (sw_if_index == (u32) ~ 0)
1561 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1563 /* Add a proxy neighbor discovery entry for this address */
1564 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1566 /* Add a receive adjacency for this address */
1567 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1570 fib_table_entry_special_dpo_update (fib_index,
1573 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1576 /* Done with the entire range? */
1577 if (!memcmp (start, end, sizeof (start[0])))
1580 /* Increment the address. DGMS. */
1582 for (i = 15; i >= 0; i--)
1585 if (tmp.as_u8[i] != 0)
1593 static clib_error_t *
1594 tcp_src_address (vlib_main_t * vm,
1595 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1597 ip4_address_t v4start, v4end;
1598 ip6_address_t v6start, v6end;
1604 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1606 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1607 unformat_ip4_address, &v4end))
1609 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1611 memcpy (&v4end, &v4start, sizeof (v4start));
1614 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
1615 unformat_ip6_address, &v6end))
1617 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1619 memcpy (&v6end, &v6start, sizeof (v4start));
1622 else if (unformat (input, "fib-table %d", &table_id))
1628 if (!v4set && !v6set)
1629 return clib_error_return (0, "at least one v4 or v6 address required");
1633 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1640 case VNET_API_ERROR_NO_SUCH_FIB:
1641 return clib_error_return (0, "Invalid table-id %d", table_id);
1643 case VNET_API_ERROR_INVALID_ARGUMENT:
1644 return clib_error_return (0, "Invalid address range %U - %U",
1645 format_ip4_address, &v4start,
1646 format_ip4_address, &v4end);
1648 return clib_error_return (0, "error %d", rv);
1654 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1661 case VNET_API_ERROR_NO_SUCH_FIB:
1662 return clib_error_return (0, "Invalid table-id %d", table_id);
1665 return clib_error_return (0, "error %d", rv);
1673 VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1675 .path = "tcp src-address",
1676 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1677 .function = tcp_src_address,
1682 tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1684 #if TCP_SCOREBOARD_TRACE
1686 scoreboard_trace_elt_t *block;
1692 s = format (s, "scoreboard trace:");
1693 vec_foreach (block, sb->trace)
1695 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1696 block->ack, block->snd_una_max, block->group);
1698 s = format (s, "\n");
1706 static clib_error_t *
1707 tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1708 vlib_cli_command_t * cmd_arg)
1710 transport_connection_t *tconn = 0;
1711 tcp_connection_t *tc;
1713 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1715 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1716 TRANSPORT_PROTO_TCP))
1719 return clib_error_return (0, "unknown input `%U'",
1720 format_unformat_error, input);
1723 if (!TCP_SCOREBOARD_TRACE)
1725 vlib_cli_output (vm, "scoreboard tracing not enabled");
1729 tc = tcp_get_connection_from_transport (tconn);
1730 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1731 vlib_cli_output (vm, "%v", s);
1736 VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1738 .path = "show tcp scoreboard trace",
1739 .short_help = "show tcp scoreboard trace <connection>",
1740 .function = tcp_show_scoreboard_trace_fn,
1745 tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1748 scoreboard_trace_elt_t *trace;
1749 u32 next_ack, left, group, has_new_ack = 0;
1750 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1751 sack_block_t *block;
1756 memset (dummy_tc, 0, sizeof (*dummy_tc));
1757 tcp_connection_timers_init (dummy_tc);
1758 scoreboard_init (&dummy_tc->sack_sb);
1759 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1761 #if TCP_SCOREBOARD_TRACE
1762 trace = tc->sack_sb.trace;
1763 trace_len = vec_len (tc->sack_sb.trace);
1769 for (i = 0; i < trace_len; i++)
1771 if (trace[i].ack != 0)
1773 dummy_tc->snd_una = trace[i].ack - 1448;
1774 dummy_tc->snd_una_max = trace[i].ack;
1779 while (left < trace_len)
1781 group = trace[left].group;
1782 vec_reset_length (dummy_tc->rcv_opts.sacks);
1784 while (trace[left].group == group)
1786 if (trace[left].ack != 0)
1789 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1790 trace[left].ack, trace[left].snd_una_max);
1791 dummy_tc->snd_una_max = trace[left].snd_una_max;
1792 next_ack = trace[left].ack;
1798 s = format (s, "[%u, %u], ", trace[left].start,
1800 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1801 block->start = trace[left].start;
1802 block->end = trace[left].end;
1808 tcp_rcv_sacks (dummy_tc, next_ack);
1810 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1813 s = format (s, "result: %U", format_tcp_scoreboard,
1814 &dummy_tc->sack_sb);
1817 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1822 static clib_error_t *
1823 tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1824 vlib_cli_command_t * cmd_arg)
1826 transport_connection_t *tconn = 0;
1827 tcp_connection_t *tc = 0;
1829 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1831 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1832 TRANSPORT_PROTO_TCP))
1835 return clib_error_return (0, "unknown input `%U'",
1836 format_unformat_error, input);
1839 if (!TCP_SCOREBOARD_TRACE)
1841 vlib_cli_output (vm, "scoreboard tracing not enabled");
1845 tc = tcp_get_connection_from_transport (tconn);
1848 vlib_cli_output (vm, "connection not found");
1851 str = tcp_scoreboard_replay (str, tc, 1);
1852 vlib_cli_output (vm, "%v", str);
1857 VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1859 .path = "tcp replay scoreboard",
1860 .short_help = "tcp replay scoreboard <connection>",
1861 .function = tcp_scoreboard_trace_fn,
1866 * fd.io coding-style-patch-verification: ON
1869 * eval: (c-set-style "gnu")