blob: 34c901eb1df58ef71207bd8a9d3f4c244aa1e8e4 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
Dave Barach3bbcfab2017-08-15 19:03:44 -040016/**
17 * @file
18 * @brief TCP host stack utilities
19 */
20
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/tcp/tcp.h>
22#include <vnet/session/session.h>
23#include <vnet/fib/fib.h>
Florin Corasf6359c82017-06-19 12:26:09 -040024#include <vnet/dpo/load_balance.h>
Dave Barach3bbcfab2017-08-15 19:03:44 -040025#include <vnet/dpo/receive_dpo.h>
26#include <vnet/ip/ip6_neighbor.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027#include <math.h>
28
29tcp_main_t tcp_main;
30
Florin Corascea194d2017-10-02 00:18:51 -070031void *
32ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
33{
34 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
35 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
36 ip_interface_address_t *ia = 0;
37
38 if (is_ip4)
39 {
40 /* *INDENT-OFF* */
41 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
42 ({
43 return ip_interface_address_get_address (lm4, ia);
44 }));
45 /* *INDENT-ON* */
46 }
47 else
48 {
49 /* *INDENT-OFF* */
50 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
51 ({
52 ip6_address_t *rv;
53 rv = ip_interface_address_get_address (lm6, ia);
54 /* Trying to use a link-local ip6 src address is a fool's errand */
55 if (!ip6_address_is_link_local_unicast (rv))
56 return rv;
57 }));
58 /* *INDENT-ON* */
59 }
60
61 return 0;
62}
63
Dave Barach68b0fb02017-02-28 15:15:56 -050064static u32
Florin Coras04e53442017-07-16 17:12:15 -070065tcp_connection_bind (u32 session_index, transport_endpoint_t * lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -050066{
67 tcp_main_t *tm = &tcp_main;
68 tcp_connection_t *listener;
Florin Corascea194d2017-10-02 00:18:51 -070069 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -050070
71 pool_get (tm->listener_pool, listener);
72 memset (listener, 0, sizeof (*listener));
73
74 listener->c_c_index = listener - tm->listener_pool;
Florin Coras0e495682017-09-19 22:27:18 -070075 listener->c_lcl_port = lcl->port;
Dave Barach68b0fb02017-02-28 15:15:56 -050076
Florin Corascea194d2017-10-02 00:18:51 -070077 /* If we are provided a sw_if_index, bind using one of its ips */
78 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
Florin Coras6cf30ad2017-04-04 23:08:23 -070079 {
Florin Corascea194d2017-10-02 00:18:51 -070080 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
81 lcl->is_ip4)))
82 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
Florin Coras6cf30ad2017-04-04 23:08:23 -070083 }
Florin Corascea194d2017-10-02 00:18:51 -070084 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
85 listener->c_is_ip4 = lcl->is_ip4;
Florin Coras68810622017-07-24 17:40:28 -070086 listener->c_transport_proto = TRANSPORT_PROTO_TCP;
Dave Barach68b0fb02017-02-28 15:15:56 -050087 listener->c_s_index = session_index;
Florin Corascea194d2017-10-02 00:18:51 -070088 listener->c_fib_index = lcl->fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050089 listener->state = TCP_STATE_LISTEN;
Dave Barach68b0fb02017-02-28 15:15:56 -050090
Florin Corase69f4952017-03-07 10:06:24 -080091 tcp_connection_timers_init (listener);
92
93 TCP_EVT_DBG (TCP_EVT_BIND, listener);
94
Dave Barach68b0fb02017-02-28 15:15:56 -050095 return listener->c_c_index;
96}
97
98u32
Florin Coras04e53442017-07-16 17:12:15 -070099tcp_session_bind (u32 session_index, transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500100{
Florin Coras04e53442017-07-16 17:12:15 -0700101 return tcp_connection_bind (session_index, tep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500102}
103
104static void
Florin Corase69f4952017-03-07 10:06:24 -0800105tcp_connection_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500106{
107 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach2c25a622017-06-26 11:35:07 -0400108 tcp_connection_t *tc;
109
110 tc = pool_elt_at_index (tm->listener_pool, listener_index);
111
112 TCP_EVT_DBG (TCP_EVT_UNBIND, tc);
113
114 /* Poison the entry */
115 if (CLIB_DEBUG > 0)
116 memset (tc, 0xFA, sizeof (*tc));
117
Dave Barach68b0fb02017-02-28 15:15:56 -0500118 pool_put_index (tm->listener_pool, listener_index);
119}
120
121u32
Florin Corase69f4952017-03-07 10:06:24 -0800122tcp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500123{
Florin Corase69f4952017-03-07 10:06:24 -0800124 tcp_connection_unbind (listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500125 return 0;
126}
127
128transport_connection_t *
129tcp_session_get_listener (u32 listener_index)
130{
131 tcp_main_t *tm = vnet_get_tcp_main ();
132 tcp_connection_t *tc;
133 tc = pool_elt_at_index (tm->listener_pool, listener_index);
134 return &tc->connection;
135}
136
Florin Coras68810622017-07-24 17:40:28 -0700137always_inline void
138transport_endpoint_del (u32 tepi)
139{
140 tcp_main_t *tm = vnet_get_tcp_main ();
141 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
142 pool_put_index (tm->local_endpoints, tepi);
143 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
144}
145
146always_inline transport_endpoint_t *
147transport_endpoint_new (void)
148{
149 tcp_main_t *tm = vnet_get_tcp_main ();
150 transport_endpoint_t *tep;
151 pool_get (tm->local_endpoints, tep);
152 return tep;
153}
154
155/**
156 * Cleanup half-open connection
157 *
158 */
159void
160tcp_half_open_connection_del (tcp_connection_t * tc)
161{
162 tcp_main_t *tm = vnet_get_tcp_main ();
163 clib_spinlock_lock_if_init (&tm->half_open_lock);
164 pool_put_index (tm->half_open_connections, tc->c_c_index);
165 if (CLIB_DEBUG)
166 memset (tc, 0xFA, sizeof (*tc));
167 clib_spinlock_unlock_if_init (&tm->half_open_lock);
168}
169
170/**
171 * Try to cleanup half-open connection
172 *
173 * If called from a thread that doesn't own tc, the call won't have any
174 * effect.
175 *
176 * @param tc - connection to be cleaned up
177 * @return non-zero if cleanup failed.
178 */
179int
180tcp_half_open_connection_cleanup (tcp_connection_t * tc)
181{
182 /* Make sure this is the owning thread */
183 if (tc->c_thread_index != vlib_get_thread_index ())
184 return 1;
185 tcp_timer_reset (tc, TCP_TIMER_ESTABLISH);
186 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN);
187 tcp_half_open_connection_del (tc);
188 return 0;
189}
190
191tcp_connection_t *
192tcp_half_open_connection_new (void)
193{
194 tcp_main_t *tm = vnet_get_tcp_main ();
195 tcp_connection_t *tc = 0;
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400196 ASSERT (vlib_get_thread_index () == 0);
Florin Coras68810622017-07-24 17:40:28 -0700197 pool_get (tm->half_open_connections, tc);
198 memset (tc, 0, sizeof (*tc));
199 tc->c_c_index = tc - tm->half_open_connections;
200 return tc;
201}
202
Dave Barach68b0fb02017-02-28 15:15:56 -0500203/**
204 * Cleans up connection state.
205 *
206 * No notifications.
207 */
208void
209tcp_connection_cleanup (tcp_connection_t * tc)
210{
211 tcp_main_t *tm = &tcp_main;
212 u32 tepi;
213 transport_endpoint_t *tep;
214
215 /* Cleanup local endpoint if this was an active connect */
216 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
Florin Coras66b11312017-07-31 17:18:03 -0700217 clib_net_to_host_u16 (tc->c_lcl_port));
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
219 {
220 tep = pool_elt_at_index (tm->local_endpoints, tepi);
221 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
Florin Coras68810622017-07-24 17:40:28 -0700222 transport_endpoint_del (tepi);
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 }
224
Florin Coras68810622017-07-24 17:40:28 -0700225 /* Check if connection is not yet fully established */
Dave Barach68b0fb02017-02-28 15:15:56 -0500226 if (tc->state == TCP_STATE_SYN_SENT)
Dave Barach2c25a622017-06-26 11:35:07 -0400227 {
Florin Coras68810622017-07-24 17:40:28 -0700228 /* Try to remove the half-open connection. If this is not the owning
229 * thread, tc won't be removed. Retransmit or establish timers will
230 * eventually expire and call again cleanup on the right thread. */
231 tcp_half_open_connection_cleanup (tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400232 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 else
Dave Barach2c25a622017-06-26 11:35:07 -0400234 {
235 int thread_index = tc->c_thread_index;
Florin Coras68810622017-07-24 17:40:28 -0700236
237 /* Make sure all timers are cleared */
238 tcp_connection_timers_reset (tc);
239
Dave Barach2c25a622017-06-26 11:35:07 -0400240 /* Poison the entry */
241 if (CLIB_DEBUG > 0)
242 memset (tc, 0xFA, sizeof (*tc));
243 pool_put (tm->connections[thread_index], tc);
244 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500245}
246
247/**
248 * Connection removal.
249 *
250 * This should be called only once connection enters CLOSED state. Note
251 * that it notifies the session of the removal event, so if the goal is to
252 * just remove the connection, call tcp_connection_cleanup instead.
253 */
254void
255tcp_connection_del (tcp_connection_t * tc)
256{
Florin Corase69f4952017-03-07 10:06:24 -0800257 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500258 stream_session_delete_notify (&tc->connection);
259 tcp_connection_cleanup (tc);
260}
261
Florin Coras6534b7a2017-07-18 05:38:03 -0400262tcp_connection_t *
263tcp_connection_new (u8 thread_index)
264{
265 tcp_main_t *tm = vnet_get_tcp_main ();
266 tcp_connection_t *tc;
267
268 pool_get (tm->connections[thread_index], tc);
269 memset (tc, 0, sizeof (*tc));
270 tc->c_c_index = tc - tm->connections[thread_index];
271 tc->c_thread_index = thread_index;
272 return tc;
273}
274
Florin Corasd79b41e2017-03-04 05:37:52 -0800275/** Notify session that connection has been reset.
276 *
277 * Switch state to closed and wait for session to call cleanup.
278 */
279void
280tcp_connection_reset (tcp_connection_t * tc)
281{
Florin Coras6534b7a2017-07-18 05:38:03 -0400282 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
Florin Coras11c05492017-05-10 12:29:14 -0700283 switch (tc->state)
284 {
285 case TCP_STATE_SYN_RCVD:
286 /* Cleanup everything. App wasn't notified yet */
287 stream_session_delete_notify (&tc->connection);
288 tcp_connection_cleanup (tc);
289 break;
290 case TCP_STATE_SYN_SENT:
Florin Coras68810622017-07-24 17:40:28 -0700291 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
Florin Coras6534b7a2017-07-18 05:38:03 -0400292 tcp_connection_cleanup (tc);
293 break;
Florin Coras11c05492017-05-10 12:29:14 -0700294 case TCP_STATE_ESTABLISHED:
295 case TCP_STATE_CLOSE_WAIT:
296 case TCP_STATE_FIN_WAIT_1:
297 case TCP_STATE_FIN_WAIT_2:
298 case TCP_STATE_CLOSING:
299 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400300 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800301
Florin Coras11c05492017-05-10 12:29:14 -0700302 /* Make sure all timers are cleared */
303 tcp_connection_timers_reset (tc);
Florin Coras11c05492017-05-10 12:29:14 -0700304 stream_session_reset_notify (&tc->connection);
Dave Barach2c25a622017-06-26 11:35:07 -0400305
306 /* Wait for cleanup from session layer but not forever */
Florin Coras68810622017-07-24 17:40:28 -0700307 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Florin Coras11c05492017-05-10 12:29:14 -0700308 break;
309 case TCP_STATE_CLOSED:
310 return;
311 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800312}
313
Dave Barach68b0fb02017-02-28 15:15:56 -0500314/**
315 * Begin connection closing procedure.
316 *
317 * If at the end the connection is not in CLOSED state, it is not removed.
318 * Instead, we rely on on TCP to advance through state machine to either
319 * 1) LAST_ACK (passive close) whereby when the last ACK is received
320 * tcp_connection_del is called. This notifies session of the delete and
321 * calls cleanup.
322 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
323 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800324 *
325 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500326 */
327void
328tcp_connection_close (tcp_connection_t * tc)
329{
Florin Corase69f4952017-03-07 10:06:24 -0800330 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
331
Florin Corasb2215d62017-08-01 16:56:58 -0700332 /* Send/Program FIN if needed and switch state */
333 switch (tc->state)
334 {
335 case TCP_STATE_SYN_SENT:
336 tc->state = TCP_STATE_CLOSED;
337 break;
338 case TCP_STATE_SYN_RCVD:
339 tcp_send_fin (tc);
340 tc->state = TCP_STATE_FIN_WAIT_1;
341 break;
342 case TCP_STATE_ESTABLISHED:
343 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
344 tcp_send_fin (tc);
345 else
346 tc->flags |= TCP_CONN_FINPNDG;
347 tc->state = TCP_STATE_FIN_WAIT_1;
348 break;
349 case TCP_STATE_CLOSE_WAIT:
Florin Corasa096f2d2017-09-28 23:49:42 -0400350 tcp_connection_timers_reset (tc);
Florin Corasb2215d62017-08-01 16:56:58 -0700351 tcp_send_fin (tc);
352 tc->state = TCP_STATE_LAST_ACK;
Florin Corasa096f2d2017-09-28 23:49:42 -0400353 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Florin Corasb2215d62017-08-01 16:56:58 -0700354 break;
Florin Corasc87c91d2017-08-16 19:55:49 -0700355 case TCP_STATE_FIN_WAIT_1:
Florin Corasa096f2d2017-09-28 23:49:42 -0400356 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Florin Corasc87c91d2017-08-16 19:55:49 -0700357 break;
Florin Corasb2215d62017-08-01 16:56:58 -0700358 default:
Florin Corasa096f2d2017-09-28 23:49:42 -0400359 TCP_DBG ("state: %u", tc->state);
Florin Corasb2215d62017-08-01 16:56:58 -0700360 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500361
Florin Coras6534b7a2017-07-18 05:38:03 -0400362 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500363
Florin Corasd79b41e2017-03-04 05:37:52 -0800364 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
Florin Corasa096f2d2017-09-28 23:49:42 -0400365 if (!tcp_timer_is_active (tc, TCP_TIMER_WAITCLOSE)
Florin Corasd79b41e2017-03-04 05:37:52 -0800366 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500367 tcp_connection_del (tc);
368}
369
370void
371tcp_session_close (u32 conn_index, u32 thread_index)
372{
373 tcp_connection_t *tc;
374 tc = tcp_connection_get (conn_index, thread_index);
375 tcp_connection_close (tc);
376}
377
378void
379tcp_session_cleanup (u32 conn_index, u32 thread_index)
380{
381 tcp_connection_t *tc;
382 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasa096f2d2017-09-28 23:49:42 -0400383 tcp_connection_timers_reset (tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800384
385 /* Wait for the session tx events to clear */
386 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400387 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800388 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500389}
390
Florin Corase04c2992017-03-01 08:17:34 -0800391#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500392/**
393 * Allocate local port and add if successful add entry to local endpoint
394 * table to mark the pair as used.
395 */
Florin Coras6534b7a2017-07-18 05:38:03 -0400396int
Florin Coras68810622017-07-24 17:40:28 -0700397tcp_allocate_local_port (ip46_address_t * ip)
Dave Barach68b0fb02017-02-28 15:15:56 -0500398{
Florin Coras68810622017-07-24 17:40:28 -0700399 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500400 transport_endpoint_t *tep;
Florin Coras66b11312017-07-31 17:18:03 -0700401 u32 tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800402 u16 min = 1024, max = 65535; /* XXX configurable ? */
Florin Coras66b11312017-07-31 17:18:03 -0700403 int tries, limit;
Dave Barach68b0fb02017-02-28 15:15:56 -0500404
Florin Coras66b11312017-07-31 17:18:03 -0700405 limit = max - min;
Dave Barach68b0fb02017-02-28 15:15:56 -0500406
Dave Barach2c25a622017-06-26 11:35:07 -0400407 /* Only support active opens from thread 0 */
408 ASSERT (vlib_get_thread_index () == 0);
409
Dave Barach68b0fb02017-02-28 15:15:56 -0500410 /* Search for first free slot */
Florin Coras66b11312017-07-31 17:18:03 -0700411 for (tries = 0; tries < limit; tries++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 {
Florin Corase04c2992017-03-01 08:17:34 -0800413 u16 port = 0;
414
415 /* Find a port in the specified range */
416 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 {
Florin Coras66b11312017-07-31 17:18:03 -0700418 port = random_u32 (&tm->port_allocator_seed) & PORT_MASK;
Florin Corase04c2992017-03-01 08:17:34 -0800419 if (PREDICT_TRUE (port >= min && port < max))
420 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500421 }
422
Florin Corase04c2992017-03-01 08:17:34 -0800423 /* Look it up */
Florin Coras68810622017-07-24 17:40:28 -0700424 tei = transport_endpoint_lookup (&tm->local_endpoints_table, ip, port);
Florin Corase04c2992017-03-01 08:17:34 -0800425 /* If not found, we're done */
426 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
427 {
Florin Coras68810622017-07-24 17:40:28 -0700428 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
429 tep = transport_endpoint_new ();
430 clib_memcpy (&tep->ip, ip, sizeof (*ip));
431 tep->port = port;
Florin Corase04c2992017-03-01 08:17:34 -0800432 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
433 tep - tm->local_endpoints);
Florin Coras68810622017-07-24 17:40:28 -0700434 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
435
Florin Corase04c2992017-03-01 08:17:34 -0800436 return tep->port;
437 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500438 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500439 return -1;
440}
441
442/**
443 * Initialize all connection timers as invalid
444 */
445void
446tcp_connection_timers_init (tcp_connection_t * tc)
447{
448 int i;
449
450 /* Set all to invalid */
451 for (i = 0; i < TCP_N_TIMERS; i++)
452 {
453 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
454 }
455
456 tc->rto = TCP_RTO_INIT;
457}
458
459/**
460 * Stop all connection timers
461 */
462void
463tcp_connection_timers_reset (tcp_connection_t * tc)
464{
465 int i;
466 for (i = 0; i < TCP_N_TIMERS; i++)
467 {
468 tcp_timer_reset (tc, i);
469 }
470}
471
Dave Barach2c25a622017-06-26 11:35:07 -0400472#if 0
Florin Corasf6359c82017-06-19 12:26:09 -0400473typedef struct ip4_tcp_hdr
474{
475 ip4_header_t ip;
476 tcp_header_t tcp;
477} ip4_tcp_hdr_t;
478
479typedef struct ip6_tcp_hdr
480{
481 ip6_header_t ip;
482 tcp_header_t tcp;
483} ip6_tcp_hdr_t;
484
485static void
486tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
487 dpo_id_t * result)
488{
489 const dpo_id_t *choice;
490 load_balance_t *lb;
491 int hash;
492
493 lb = load_balance_get (dpo->dpoi_index);
494 if (tc->c_is_ip4)
495 {
496 ip4_tcp_hdr_t hdr;
497 memset (&hdr, 0, sizeof (hdr));
498 hdr.ip.protocol = IP_PROTOCOL_TCP;
499 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
500 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
501 hdr.tcp.src_port = tc->c_lcl_port;
502 hdr.tcp.dst_port = tc->c_rmt_port;
503 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
504 }
505 else
506 {
507 ip6_tcp_hdr_t hdr;
508 memset (&hdr, 0, sizeof (hdr));
509 hdr.ip.protocol = IP_PROTOCOL_TCP;
510 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
511 sizeof (ip6_address_t));
512 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
513 sizeof (ip6_address_t));
514 hdr.tcp.src_port = tc->c_lcl_port;
515 hdr.tcp.dst_port = tc->c_rmt_port;
516 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
517 }
518 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
519 dpo_copy (result, choice);
520}
521
522fib_node_index_t
523tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
524{
525 fib_prefix_t prefix;
Florin Coras04e53442017-07-16 17:12:15 -0700526 u32 fib_index;
Florin Corasf6359c82017-06-19 12:26:09 -0400527
528 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
529 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
530 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
Florin Corascea194d2017-10-02 00:18:51 -0700531 fib_index = fib_table_find (prefix.fp_proto, tc->c_fib_index);
Florin Coras04e53442017-07-16 17:12:15 -0700532 return fib_table_lookup (fib_index, &prefix);
Florin Corasf6359c82017-06-19 12:26:09 -0400533}
534
535static int
536tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
537{
538 dpo_id_t choice = DPO_INVALID;
539 u32 output_node_index;
540 fib_entry_t *fe;
541
542 fe = fib_entry_get (tc->c_rmt_fei);
543 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
544 return -1;
545
546 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
547
548 output_node_index =
549 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
550 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
551 return 0;
552}
553
554/** Stack tcp connection on peer's fib entry.
555 *
556 * This ultimately populates the dpo the connection will use to send packets.
557 */
558static void
559tcp_connection_fib_attach (tcp_connection_t * tc)
560{
561 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
562
563 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
564
565 tcp_connection_stack_on_fib_entry (tc);
566}
Dave Barach2c25a622017-06-26 11:35:07 -0400567#endif /* 0 */
Florin Corasf6359c82017-06-19 12:26:09 -0400568
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400569/**
570 * Initialize connection send variables.
571 */
572void
573tcp_init_snd_vars (tcp_connection_t * tc)
574{
575 u32 time_now;
576
577 /* Set random initial sequence */
Florin Corascea194d2017-10-02 00:18:51 -0700578 tcp_set_time_now (0);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400579 time_now = tcp_time_now ();
580 tc->iss = random_u32 (&time_now);
581 tc->snd_una = tc->iss;
582 tc->snd_nxt = tc->iss + 1;
583 tc->snd_una_max = tc->snd_nxt;
584}
585
Dave Barach68b0fb02017-02-28 15:15:56 -0500586/** Initialize tcp connection variables
587 *
588 * Should be called after having received a msg from the peer, i.e., a SYN or
589 * a SYNACK, such that connection options have already been exchanged. */
590void
591tcp_connection_init_vars (tcp_connection_t * tc)
592{
593 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700594 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700595 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500596 tcp_cc_init (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400597 if (tc->state == TCP_STATE_SYN_RCVD)
598 tcp_init_snd_vars (tc);
599
Dave Barach2c25a622017-06-26 11:35:07 -0400600 // tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500601}
602
603int
Florin Coras04e53442017-07-16 17:12:15 -0700604tcp_connection_open (transport_endpoint_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500605{
606 tcp_main_t *tm = vnet_get_tcp_main ();
607 tcp_connection_t *tc;
608 fib_prefix_t prefix;
Florin Corasf6359c82017-06-19 12:26:09 -0400609 fib_node_index_t fei;
Florin Corascea194d2017-10-02 00:18:51 -0700610 u32 sw_if_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 ip46_address_t lcl_addr;
Florin Coras6534b7a2017-07-18 05:38:03 -0400612 int lcl_port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500613
614 /*
615 * Find the local address and allocate port
616 */
617 memset (&lcl_addr, 0, sizeof (lcl_addr));
618
619 /* Find a FIB path to the destination */
Florin Coras04e53442017-07-16 17:12:15 -0700620 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
621 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
622 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
Dave Barach68b0fb02017-02-28 15:15:56 -0500623
Florin Corascea194d2017-10-02 00:18:51 -0700624 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
625 fei = fib_table_lookup (rmt->fib_index, &prefix);
Dave Barach68b0fb02017-02-28 15:15:56 -0500626
627 /* Couldn't find route to destination. Bail out. */
628 if (fei == FIB_NODE_INDEX_INVALID)
Florin Coras6534b7a2017-07-18 05:38:03 -0400629 {
630 clib_warning ("no route to destination");
631 return -1;
632 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500633
Florin Corascea194d2017-10-02 00:18:51 -0700634 sw_if_index = rmt->sw_if_index;
635 if (sw_if_index == ENDPOINT_INVALID_INDEX)
636 sw_if_index = fib_entry_get_resolving_interface (fei);
Dave Barach68b0fb02017-02-28 15:15:56 -0500637
Florin Corascea194d2017-10-02 00:18:51 -0700638 if (sw_if_index == ENDPOINT_INVALID_INDEX)
Florin Coras6534b7a2017-07-18 05:38:03 -0400639 {
640 clib_warning ("no resolving interface for %U", format_ip46_address,
Florin Corascea194d2017-10-02 00:18:51 -0700641 &rmt->ip, (rmt->is_ip4 == 0) + 1);
Florin Coras6534b7a2017-07-18 05:38:03 -0400642 return -1;
643 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500644
Florin Coras04e53442017-07-16 17:12:15 -0700645 if (rmt->is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500646 {
647 ip4_address_t *ip4;
Dave Barach2c25a622017-06-26 11:35:07 -0400648 int index;
649 if (vec_len (tm->ip4_src_addresses))
650 {
651 index = tm->last_v4_address_rotor++;
652 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
653 tm->last_v4_address_rotor = 0;
654 lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
655 }
656 else
657 {
658 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
659 lcl_addr.ip4.as_u32 = ip4->as_u32;
660 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500661 }
662 else
663 {
664 ip6_address_t *ip6;
Dave Barach2c25a622017-06-26 11:35:07 -0400665 int index;
666
667 if (vec_len (tm->ip6_src_addresses))
668 {
669 index = tm->last_v6_address_rotor++;
670 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
671 tm->last_v6_address_rotor = 0;
672 clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index],
673 sizeof (*ip6));
674 }
675 else
676 {
677 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
rootc9d1c5b2017-08-15 12:58:31 -0400678 if (ip6 == 0)
679 {
680 clib_warning ("no routable ip6 addresses on %U",
681 format_vnet_sw_if_index_name, vnet_get_main (),
682 sw_if_index);
683 return -1;
684 }
685
Dave Barach2c25a622017-06-26 11:35:07 -0400686 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
687 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500688 }
689
690 /* Allocate source port */
Florin Coras68810622017-07-24 17:40:28 -0700691 lcl_port = tcp_allocate_local_port (&lcl_addr);
Dave Barach68b0fb02017-02-28 15:15:56 -0500692 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800693 {
694 clib_warning ("Failed to allocate src port");
695 return -1;
696 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500697
698 /*
699 * Create connection and send SYN
700 */
Florin Coras68810622017-07-24 17:40:28 -0700701 clib_spinlock_lock_if_init (&tm->half_open_lock);
Florin Coras04e53442017-07-16 17:12:15 -0700702 tc = tcp_half_open_connection_new ();
Florin Coras04e53442017-07-16 17:12:15 -0700703 clib_memcpy (&tc->c_rmt_ip, &rmt->ip, sizeof (ip46_address_t));
Dave Barach68b0fb02017-02-28 15:15:56 -0500704 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
Florin Coras0e495682017-09-19 22:27:18 -0700705 tc->c_rmt_port = rmt->port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500706 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
Florin Coras04e53442017-07-16 17:12:15 -0700707 tc->c_is_ip4 = rmt->is_ip4;
Florin Coras68810622017-07-24 17:40:28 -0700708 tc->c_transport_proto = TRANSPORT_PROTO_TCP;
Florin Corascea194d2017-10-02 00:18:51 -0700709 tc->c_fib_index = rmt->fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500710 /* The other connection vars will be initialized after SYN ACK */
711 tcp_connection_timers_init (tc);
712
Florin Corase69f4952017-03-07 10:06:24 -0800713 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400714 tc->state = TCP_STATE_SYN_SENT;
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400715 tcp_init_snd_vars (tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400716 tcp_send_syn (tc);
Florin Coras68810622017-07-24 17:40:28 -0700717 clib_spinlock_unlock_if_init (&tm->half_open_lock);
Florin Corase69f4952017-03-07 10:06:24 -0800718
Dave Barach68b0fb02017-02-28 15:15:56 -0500719 return tc->c_c_index;
720}
721
722int
Florin Coras04e53442017-07-16 17:12:15 -0700723tcp_session_open (transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500724{
Florin Coras04e53442017-07-16 17:12:15 -0700725 return tcp_connection_open (tep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500726}
727
Florin Corase69f4952017-03-07 10:06:24 -0800728const char *tcp_dbg_evt_str[] = {
729#define _(sym, str) str,
730 foreach_tcp_dbg_evt
731#undef _
732};
733
734const char *tcp_fsm_states[] = {
735#define _(sym, str) str,
736 foreach_tcp_fsm_state
737#undef _
738};
739
Dave Barach68b0fb02017-02-28 15:15:56 -0500740u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800741format_tcp_state (u8 * s, va_list * args)
742{
Florin Corasbb292f42017-05-19 09:49:19 -0700743 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800744
Florin Corasbb292f42017-05-19 09:49:19 -0700745 if (state < TCP_N_STATES)
746 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800747 else
Florin Corasbb292f42017-05-19 09:49:19 -0700748 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800749 return s;
750}
751
Florin Corasa096f2d2017-09-28 23:49:42 -0400752const char *tcp_connection_flags_str[] = {
753#define _(sym, str) str,
754 foreach_tcp_connection_flag
755#undef _
756};
757
758u8 *
759format_tcp_connection_flags (u8 * s, va_list * args)
760{
761 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
762 int i, last = -1;
763
764 for (i = 0; i < TCP_CONN_N_FLAG_BITS; i++)
765 if (tc->flags & (1 << i))
766 last = i;
767 for (i = 0; i < last; i++)
768 {
769 if (tc->flags & (1 << i))
770 s = format (s, "%s, ", tcp_connection_flags_str[i]);
771 }
772 if (last >= 0)
773 s = format (s, "%s", tcp_connection_flags_str[last]);
774 return s;
775}
776
Florin Corase69f4952017-03-07 10:06:24 -0800777const char *tcp_conn_timers[] = {
778#define _(sym, str) str,
779 foreach_tcp_timer
780#undef _
781};
782
783u8 *
784format_tcp_timers (u8 * s, va_list * args)
785{
786 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700787 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800788
789 for (i = 0; i < TCP_N_TIMERS; i++)
790 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
791 last = i;
792
793 s = format (s, "[");
794 for (i = 0; i < last; i++)
795 {
796 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
797 s = format (s, "%s,", tcp_conn_timers[i]);
798 }
799
Florin Coras93992a92017-05-24 18:03:56 -0700800 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800801 s = format (s, "%s]", tcp_conn_timers[i]);
802 else
803 s = format (s, "]");
804
805 return s;
806}
807
808u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700809format_tcp_congestion_status (u8 * s, va_list * args)
810{
811 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
812 if (tcp_in_recovery (tc))
813 s = format (s, "recovery");
814 else if (tcp_in_fastrecovery (tc))
815 s = format (s, "fastrecovery");
816 else
817 s = format (s, "none");
818 return s;
819}
820
821u8 *
822format_tcp_vars (u8 * s, va_list * args)
823{
824 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa096f2d2017-09-28 23:49:42 -0400825 s = format (s, " flags: %U timers: %U\n", format_tcp_connection_flags, tc,
826 format_tcp_timers, tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400827 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
Florin Corasbb292f42017-05-19 09:49:19 -0700828 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
829 tc->snd_una_max - tc->iss);
830 s = format (s, " rcv_nxt %u rcv_las %u\n",
831 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
832 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
833 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
834 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700835 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400836 tcp_flight_size (tc), tcp_available_output_snd_space (tc),
Florin Coras93992a92017-05-24 18:03:56 -0700837 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700838 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
839 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700840 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
Dave Barach2c25a622017-06-26 11:35:07 -0400841 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
Florin Coras93992a92017-05-24 18:03:56 -0700842 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
843 tc->rcv_dupacks);
Dave Barach2c25a622017-06-26 11:35:07 -0400844 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
845 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
846 tc->tsecr_last_ack);
Florin Corasbb292f42017-05-19 09:49:19 -0700847 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
848 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
849 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Dave Barach2c25a622017-06-26 11:35:07 -0400850 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
851 tcp_time_now () - tc->tsval_recent_age);
Florin Corasde706082017-10-11 01:43:15 -0700852 if (tc->state >= TCP_STATE_ESTABLISHED)
853 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
854 tc);
Florin Corasbb292f42017-05-19 09:49:19 -0700855 if (vec_len (tc->snd_sacks))
856 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
857
858 return s;
859}
860
861u8 *
862format_tcp_connection_id (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800863{
864 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700865 if (!tc)
866 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800867 if (tc->c_is_ip4)
868 {
869 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
870 format_ip4_address, &tc->c_lcl_ip4,
871 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
872 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
873 }
874 else
875 {
876 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
877 format_ip6_address, &tc->c_lcl_ip6,
878 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
879 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
880 }
881
882 return s;
883}
884
885u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700886format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800887{
888 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700889 u32 verbose = va_arg (*args, u32);
890
Florin Corasc87c91d2017-08-16 19:55:49 -0700891 if (!tc)
892 return s;
Florin Corasbb292f42017-05-19 09:49:19 -0700893 s = format (s, "%-50U", format_tcp_connection_id, tc);
894 if (verbose)
895 {
896 s = format (s, "%-15U", format_tcp_state, tc->state);
897 if (verbose > 1)
Florin Corasa096f2d2017-09-28 23:49:42 -0400898 s = format (s, "\n%U", format_tcp_vars, tc);
Florin Corasbb292f42017-05-19 09:49:19 -0700899 }
Florin Coras3eb50622017-07-13 01:24:57 -0400900
Florin Corase69f4952017-03-07 10:06:24 -0800901 return s;
902}
903
904u8 *
905format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500906{
907 u32 tci = va_arg (*args, u32);
908 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700909 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500910 tcp_connection_t *tc;
911
912 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700913 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700914 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700915 else
Florin Coras1f152cd2017-08-18 19:28:03 -0700916 s = format (s, "empty\n");
Florin Coras93992a92017-05-24 18:03:56 -0700917 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500918}
919
920u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800921format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500922{
923 u32 tci = va_arg (*args, u32);
924 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700925 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500926}
927
928u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800929format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500930{
931 u32 tci = va_arg (*args, u32);
932 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700933 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500934}
935
Florin Coras06d11012017-05-17 14:21:51 -0700936u8 *
937format_tcp_sacks (u8 * s, va_list * args)
938{
939 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
940 sack_block_t *sacks = tc->snd_sacks;
941 sack_block_t *block;
Dave Barach2c25a622017-06-26 11:35:07 -0400942 int i, len = 0;
943
944 len = vec_len (sacks);
945 for (i = 0; i < len - 1; i++)
946 {
947 block = &sacks[i];
948 s = format (s, " start %u end %u\n", block->start - tc->irs,
949 block->end - tc->irs);
950 }
951 if (len)
952 {
953 block = &sacks[len - 1];
954 s = format (s, " start %u end %u", block->start - tc->irs,
955 block->end - tc->irs);
956 }
Florin Coras06d11012017-05-17 14:21:51 -0700957 return s;
958}
959
960u8 *
Florin Coras3eb50622017-07-13 01:24:57 -0400961format_tcp_rcv_sacks (u8 * s, va_list * args)
962{
963 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
964 sack_block_t *sacks = tc->rcv_opts.sacks;
965 sack_block_t *block;
966 int i, len = 0;
967
968 len = vec_len (sacks);
969 for (i = 0; i < len - 1; i++)
970 {
971 block = &sacks[i];
972 s = format (s, " start %u end %u\n", block->start - tc->iss,
973 block->end - tc->iss);
974 }
975 if (len)
976 {
977 block = &sacks[len - 1];
978 s = format (s, " start %u end %u", block->start - tc->iss,
979 block->end - tc->iss);
980 }
981 return s;
982}
983
984u8 *
Florin Coras06d11012017-05-17 14:21:51 -0700985format_tcp_sack_hole (u8 * s, va_list * args)
986{
987 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -0700988 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
989 if (tc)
990 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
991 else
992 s = format (s, " [%u, %u]", hole->start, hole->end);
Florin Coras06d11012017-05-17 14:21:51 -0700993 return s;
994}
995
996u8 *
997format_tcp_scoreboard (u8 * s, va_list * args)
998{
999 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -07001000 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras06d11012017-05-17 14:21:51 -07001001 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -07001002 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
1003 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
1004 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
1005 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
1006 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
1007 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
1008
Florin Coras06d11012017-05-17 14:21:51 -07001009 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -07001010 if (hole)
1011 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
1012
Florin Coras06d11012017-05-17 14:21:51 -07001013 while (hole)
1014 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001015 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
Florin Coras06d11012017-05-17 14:21:51 -07001016 hole = scoreboard_next_hole (sb, hole);
1017 }
Florin Coras3eb50622017-07-13 01:24:57 -04001018
Florin Coras06d11012017-05-17 14:21:51 -07001019 return s;
1020}
1021
Dave Barach68b0fb02017-02-28 15:15:56 -05001022transport_connection_t *
1023tcp_session_get_transport (u32 conn_index, u32 thread_index)
1024{
1025 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
1026 return &tc->connection;
1027}
1028
1029transport_connection_t *
1030tcp_half_open_session_get_transport (u32 conn_index)
1031{
1032 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
1033 return &tc->connection;
1034}
1035
Florin Corasc8343412017-05-04 14:25:50 -07001036/**
1037 * Compute maximum segment size for session layer.
1038 *
1039 * Since the result needs to be the actual data length, it first computes
1040 * the tcp options to be used in the next burst and subtracts their
1041 * length from the connection's snd_mss.
1042 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001043u16
1044tcp_session_send_mss (transport_connection_t * trans_conn)
1045{
1046 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -07001047
1048 /* Ensure snd_mss does accurately reflect the amount of data we can push
1049 * in a segment. This also makes sure that options are updated according to
1050 * the current state of the connection. */
1051 tcp_update_snd_mss (tc);
1052
Dave Barach68b0fb02017-02-28 15:15:56 -05001053 return tc->snd_mss;
1054}
1055
Florin Coras3af90fc2017-05-03 21:09:42 -07001056always_inline u32
1057tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
1058{
Dave Barach2c25a622017-06-26 11:35:07 -04001059 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Coras3af90fc2017-05-03 21:09:42 -07001060 {
Florin Corasdb84e572017-05-09 18:54:52 -07001061 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001062 }
1063
Florin Coras1f152cd2017-08-18 19:28:03 -07001064 /* If not snd_wnd constrained and we can't write at least a segment,
1065 * don't try at all */
Dave Barach2c25a622017-06-26 11:35:07 -04001066 if (PREDICT_FALSE (snd_space < tc->snd_mss))
Florin Coras9d063042017-09-14 03:08:00 -04001067 return snd_space < tc->cwnd ? 0 : snd_space;
Florin Coras3af90fc2017-05-03 21:09:42 -07001068
1069 /* round down to mss multiple */
1070 return snd_space - (snd_space % tc->snd_mss);
1071}
1072
Florin Coras6792ec02017-03-13 03:49:51 -07001073/**
1074 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -07001075 *
1076 * Takes into account available send space, snd_mss and the congestion
1077 * state of the connection. If possible, the value returned is a multiple
1078 * of snd_mss.
1079 *
1080 * @param tc tcp connection
1081 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -07001082 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001083u32
Florin Corasbb292f42017-05-19 09:49:19 -07001084tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001085{
Florin Corasf03a59a2017-06-09 21:07:32 -07001086 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -07001087
Florin Corasf03a59a2017-06-09 21:07:32 -07001088 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -07001089 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001090 snd_space = tcp_available_output_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -07001091
1092 /* If we haven't gotten dupacks or if we did and have gotten sacked
1093 * bytes then we can still send as per Limited Transmit (RFC3042) */
1094 if (PREDICT_FALSE (tc->rcv_dupacks != 0
1095 && (tcp_opts_sack_permitted (tc)
1096 && tc->sack_sb.last_sacked_bytes == 0)))
1097 {
1098 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
1099 tc->limited_transmit = tc->snd_nxt;
1100 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
1101
1102 snt_limited = tc->snd_nxt - tc->limited_transmit;
1103 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
1104 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001105 return tcp_round_snd_space (tc, snd_space);
1106 }
Florin Coras6792ec02017-03-13 03:49:51 -07001107
Florin Coras3af90fc2017-05-03 21:09:42 -07001108 if (tcp_in_recovery (tc))
1109 {
1110 tc->snd_nxt = tc->snd_una_max;
Florin Coras1f152cd2017-08-18 19:28:03 -07001111 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -07001112 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -07001113 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -07001114 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001115 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -07001116 }
1117
Florin Coras1f152cd2017-08-18 19:28:03 -07001118 /* RFC 5681: When previously unsent data is available and the new value of
1119 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
1120 * bytes of previously unsent data. */
1121 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -07001122 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001123 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
1124 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -07001125 tcp_fastrecovery_1_smss_on (tc);
1126 return tc->snd_mss;
1127 }
1128
1129 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001130}
1131
1132u32
Florin Corasbb292f42017-05-19 09:49:19 -07001133tcp_session_send_space (transport_connection_t * trans_conn)
1134{
1135 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras1f152cd2017-08-18 19:28:03 -07001136 return clib_min (tcp_snd_space (tc),
1137 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
Florin Corasbb292f42017-05-19 09:49:19 -07001138}
1139
Florin Coras93992a92017-05-24 18:03:56 -07001140i32
1141tcp_rcv_wnd_available (tcp_connection_t * tc)
1142{
1143 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1144}
1145
Florin Corasbb292f42017-05-19 09:49:19 -07001146u32
Florin Corasd79b41e2017-03-04 05:37:52 -08001147tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -05001148{
1149 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -07001150
1151 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1152
1153 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -08001154 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -05001155}
1156
1157/* *INDENT-OFF* */
Florin Coras04e53442017-07-16 17:12:15 -07001158const static transport_proto_vft_t tcp_proto = {
1159 .bind = tcp_session_bind,
Florin Corase69f4952017-03-07 10:06:24 -08001160 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001161 .push_header = tcp_push_header,
1162 .get_connection = tcp_session_get_transport,
1163 .get_listener = tcp_session_get_listener,
1164 .get_half_open = tcp_half_open_session_get_transport,
Florin Coras04e53442017-07-16 17:12:15 -07001165 .open = tcp_session_open,
Dave Barach68b0fb02017-02-28 15:15:56 -05001166 .close = tcp_session_close,
1167 .cleanup = tcp_session_cleanup,
1168 .send_mss = tcp_session_send_mss,
1169 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001170 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001171 .format_connection = format_tcp_session,
1172 .format_listener = format_tcp_listener_session,
1173 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001174};
1175/* *INDENT-ON* */
1176
1177void
1178tcp_timer_keep_handler (u32 conn_index)
1179{
Damjan Marion586afd72017-04-05 19:18:20 +02001180 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001181 tcp_connection_t *tc;
1182
Damjan Marion586afd72017-04-05 19:18:20 +02001183 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001184 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1185
1186 tcp_connection_close (tc);
1187}
1188
1189void
1190tcp_timer_establish_handler (u32 conn_index)
1191{
1192 tcp_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -05001193
1194 tc = tcp_half_open_connection_get (conn_index);
Florin Corasab0289a2017-08-14 11:25:25 -07001195 if (tc)
1196 {
1197 ASSERT (tc->state == TCP_STATE_SYN_SENT);
Florin Corasab0289a2017-08-14 11:25:25 -07001198 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
Florin Coras9d063042017-09-14 03:08:00 -04001199 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
Florin Corasab0289a2017-08-14 11:25:25 -07001200 }
1201 else
1202 {
1203 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
Dave Barachb7f1faa2017-08-29 11:43:37 -04001204 /* note: the connection may have already disappeared */
1205 if (PREDICT_FALSE (tc == 0))
1206 return;
Florin Coras9d063042017-09-14 03:08:00 -04001207 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
Florin Corasab0289a2017-08-14 11:25:25 -07001208 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001209 /* Start cleanup. App wasn't notified yet so use delete notify as
1210 * opposed to delete to cleanup session layer state. */
1211 stream_session_delete_notify (&tc->connection);
Florin Corasab0289a2017-08-14 11:25:25 -07001212 }
rootc9d1c5b2017-08-15 12:58:31 -04001213 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
Dave Barach68b0fb02017-02-28 15:15:56 -05001214 tcp_connection_cleanup (tc);
1215}
1216
1217void
Florin Corasd79b41e2017-03-04 05:37:52 -08001218tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001219{
Damjan Marion586afd72017-04-05 19:18:20 +02001220 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001221 tcp_connection_t *tc;
1222
Damjan Marion586afd72017-04-05 19:18:20 +02001223 tc = tcp_connection_get (conn_index, thread_index);
Florin Coras68810622017-07-24 17:40:28 -07001224 if (!tc)
1225 return;
Florin Corasd79b41e2017-03-04 05:37:52 -08001226 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1227
1228 /* Session didn't come back with a close(). Send FIN either way
1229 * and switch to LAST_ACK. */
1230 if (tc->state == TCP_STATE_CLOSE_WAIT)
1231 {
1232 if (tc->flags & TCP_CONN_FINSNT)
1233 {
1234 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1235 }
1236
1237 tcp_send_fin (tc);
1238 tc->state = TCP_STATE_LAST_ACK;
1239
1240 /* Make sure we don't wait in LAST ACK forever */
1241 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1242
1243 /* Don't delete the connection yet */
1244 return;
1245 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001246
1247 tcp_connection_del (tc);
1248}
1249
1250/* *INDENT-OFF* */
1251static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1252{
1253 tcp_timer_retransmit_handler,
1254 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -07001255 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001256 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -08001257 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001258 tcp_timer_retransmit_syn_handler,
1259 tcp_timer_establish_handler
1260};
1261/* *INDENT-ON* */
1262
1263static void
1264tcp_expired_timers_dispatch (u32 * expired_timers)
1265{
1266 int i;
1267 u32 connection_index, timer_id;
1268
1269 for (i = 0; i < vec_len (expired_timers); i++)
1270 {
1271 /* Get session index and timer id */
1272 connection_index = expired_timers[i] & 0x0FFFFFFF;
1273 timer_id = expired_timers[i] >> 28;
1274
Florin Corase69f4952017-03-07 10:06:24 -08001275 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1276
Dave Barach68b0fb02017-02-28 15:15:56 -05001277 /* Handle expiration */
1278 (*timer_expiration_handlers[timer_id]) (connection_index);
1279 }
1280}
1281
1282void
1283tcp_initialize_timer_wheels (tcp_main_t * tm)
1284{
1285 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001286 /* *INDENT-OFF* */
1287 foreach_vlib_main (({
1288 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001289 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1290 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001291 tw->last_run_time = vlib_time_now (this_vlib_main);
1292 }));
1293 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001294}
1295
1296clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001297tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001298{
Dave Barach68b0fb02017-02-28 15:15:56 -05001299 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001300 ip_protocol_info_t *pi;
1301 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001302 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1303 clib_error_t *error = 0;
1304 u32 num_threads;
Dave Barachb7f1faa2017-08-29 11:43:37 -04001305 int thread;
Dave Barach2c25a622017-06-26 11:35:07 -04001306 tcp_connection_t *tc __attribute__ ((unused));
Florin Coras66b11312017-07-31 17:18:03 -07001307 u32 preallocated_connections_per_thread;
Dave Barach68b0fb02017-02-28 15:15:56 -05001308
Dave Barach68b0fb02017-02-28 15:15:56 -05001309 if ((error = vlib_call_init_function (vm, ip_main_init)))
1310 return error;
1311 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1312 return error;
1313 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1314 return error;
1315
1316 /*
1317 * Registrations
1318 */
1319
1320 /* Register with IP */
1321 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1322 if (pi == 0)
1323 return clib_error_return (0, "TCP protocol info AWOL");
1324 pi->format_header = format_tcp_header;
1325 pi->unformat_pg_edit = unformat_pg_tcp_header;
1326
1327 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
rootc9d1c5b2017-08-15 12:58:31 -04001328 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001329
Florin Coras04e53442017-07-16 17:12:15 -07001330 /* Register as transport with session layer */
Florin Coras68810622017-07-24 17:40:28 -07001331 session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1332 session_register_transport (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
Dave Barach68b0fb02017-02-28 15:15:56 -05001333
1334 /*
1335 * Initialize data structures
1336 */
1337
1338 num_threads = 1 /* main thread */ + vtm->n_threads;
1339 vec_validate (tm->connections, num_threads - 1);
1340
Dave Barach2c25a622017-06-26 11:35:07 -04001341 /*
Florin Coras66b11312017-07-31 17:18:03 -07001342 * Preallocate connections. Assume that thread 0 won't
1343 * use preallocated threads when running multi-core
Dave Barach2c25a622017-06-26 11:35:07 -04001344 */
Florin Coras66b11312017-07-31 17:18:03 -07001345 if (num_threads == 1)
Dave Barach2c25a622017-06-26 11:35:07 -04001346 {
Florin Coras66b11312017-07-31 17:18:03 -07001347 thread = 0;
1348 preallocated_connections_per_thread = tm->preallocated_connections;
1349 }
1350 else
1351 {
1352 thread = 1;
1353 preallocated_connections_per_thread =
1354 tm->preallocated_connections / (num_threads - 1);
1355 }
1356 for (; thread < num_threads; thread++)
1357 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001358 if (preallocated_connections_per_thread)
1359 pool_init_fixed (tm->connections[thread],
1360 preallocated_connections_per_thread);
Dave Barach2c25a622017-06-26 11:35:07 -04001361 }
1362
1363 /*
Dave Barachb7f1faa2017-08-29 11:43:37 -04001364 * Use a preallocated half-open connection pool?
Dave Barach2c25a622017-06-26 11:35:07 -04001365 */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001366 if (tm->preallocated_half_open_connections)
1367 pool_init_fixed (tm->half_open_connections,
1368 tm->preallocated_half_open_connections);
Dave Barach2c25a622017-06-26 11:35:07 -04001369
Dave Barach68b0fb02017-02-28 15:15:56 -05001370 /* Initialize per worker thread tx buffers (used for control messages) */
1371 vec_validate (tm->tx_buffers, num_threads - 1);
1372
1373 /* Initialize timer wheels */
1374 vec_validate (tm->timer_wheels, num_threads - 1);
1375 tcp_initialize_timer_wheels (tm);
1376
Dave Barach68b0fb02017-02-28 15:15:56 -05001377 /* Initialize clocks per tick for TCP timestamp. Used to compute
1378 * monotonically increasing timestamps. */
1379 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1380 / TCP_TSTAMP_RESOLUTION;
1381
Dave Barachd84ba852017-08-22 17:56:46 -04001382 if (tm->local_endpoints_table_buckets == 0)
1383 tm->local_endpoints_table_buckets = 250000;
1384 if (tm->local_endpoints_table_memory == 0)
1385 tm->local_endpoints_table_memory = 512 << 20;
1386
Dave Barach68b0fb02017-02-28 15:15:56 -05001387 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
Dave Barachd84ba852017-08-22 17:56:46 -04001388 tm->local_endpoints_table_buckets,
1389 tm->local_endpoints_table_memory);
Florin Coras66b11312017-07-31 17:18:03 -07001390
1391 /* Initialize [port-allocator] random number seed */
1392 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
1393
Florin Coras04e53442017-07-16 17:12:15 -07001394 if (num_threads > 1)
Florin Coras68810622017-07-24 17:40:28 -07001395 {
1396 clib_spinlock_init (&tm->half_open_lock);
1397 clib_spinlock_init (&tm->local_endpoints_lock);
1398 }
Florin Coras66b11312017-07-31 17:18:03 -07001399
1400 vec_validate (tm->tx_frames[0], num_threads - 1);
1401 vec_validate (tm->tx_frames[1], num_threads - 1);
Florin Coras9d063042017-09-14 03:08:00 -04001402 vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
1403 vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
Florin Coras66b11312017-07-31 17:18:03 -07001404
Florin Corasb2215d62017-08-01 16:56:58 -07001405 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1406 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Coras82d3ec82017-08-14 08:10:42 -07001407
1408 vec_validate (tm->time_now, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001409 return error;
1410}
1411
Florin Corasa0b34a72017-03-07 01:20:52 -08001412clib_error_t *
1413vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1414{
1415 if (is_en)
1416 {
1417 if (tcp_main.is_enabled)
1418 return 0;
1419
1420 return tcp_main_enable (vm);
1421 }
1422 else
1423 {
1424 tcp_main.is_enabled = 0;
1425 }
1426
1427 return 0;
1428}
1429
Pierre Pfister7fe51f32017-09-20 08:48:36 +02001430void
1431tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
1432{
1433 tcp_main_t *tm = &tcp_main;
1434 if (is_ip4)
1435 tm->punt_unknown4 = is_add;
1436 else
1437 tm->punt_unknown6 = is_add;
1438}
1439
Florin Corasa0b34a72017-03-07 01:20:52 -08001440clib_error_t *
1441tcp_init (vlib_main_t * vm)
1442{
1443 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001444 tm->is_enabled = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001445 tcp_api_reference ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001446 return 0;
1447}
1448
Dave Barach68b0fb02017-02-28 15:15:56 -05001449VLIB_INIT_FUNCTION (tcp_init);
1450
Dave Barach2c25a622017-06-26 11:35:07 -04001451static clib_error_t *
1452tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1453{
1454 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barachd84ba852017-08-22 17:56:46 -04001455 u64 tmp;
Dave Barach2c25a622017-06-26 11:35:07 -04001456
1457 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1458 {
1459 if (unformat
1460 (input, "preallocated-connections %d",
1461 &tm->preallocated_connections))
1462 ;
1463 else if (unformat (input, "preallocated-half-open-connections %d",
1464 &tm->preallocated_half_open_connections))
1465 ;
Dave Barachd84ba852017-08-22 17:56:46 -04001466 else if (unformat (input, "local-endpoints-table-memory %U",
1467 unformat_memory_size, &tmp))
1468 {
1469 if (tmp >= 0x100000000)
1470 return clib_error_return (0, "memory size %llx (%lld) too large",
1471 tmp, tmp);
1472 tm->local_endpoints_table_memory = tmp;
1473 }
1474 else if (unformat (input, "local-endpoints-table-buckets %d",
1475 &tm->local_endpoints_table_buckets))
1476 ;
1477
1478
Dave Barach2c25a622017-06-26 11:35:07 -04001479 else
1480 return clib_error_return (0, "unknown input `%U'",
1481 format_unformat_error, input);
1482 }
1483 return 0;
1484}
1485
1486VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1487
Dave Barach3bbcfab2017-08-15 19:03:44 -04001488
1489/**
1490 * \brief Configure an ipv4 source address range
1491 * @param vm vlib_main_t pointer
1492 * @param start first ipv4 address in the source address range
1493 * @param end last ipv4 address in the source address range
1494 * @param table_id VRF / table ID, 0 for the default FIB
1495 * @return 0 if all OK, else an error indication from api_errno.h
1496 */
1497
1498int
1499tcp_configure_v4_source_address_range (vlib_main_t * vm,
1500 ip4_address_t * start,
1501 ip4_address_t * end, u32 table_id)
1502{
1503 tcp_main_t *tm = vnet_get_tcp_main ();
1504 vnet_main_t *vnm = vnet_get_main ();
1505 u32 start_host_byte_order, end_host_byte_order;
1506 fib_prefix_t prefix;
1507 vnet_sw_interface_t *si;
1508 fib_node_index_t fei;
1509 u32 fib_index = 0;
1510 u32 sw_if_index;
1511 int rv;
1512 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1513 ip4_address_t * hi_addr, u32 fib_index,
1514 int is_del);
1515
1516 memset (&prefix, 0, sizeof (prefix));
1517
1518 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1519
1520 if (fib_index == ~0)
1521 return VNET_API_ERROR_NO_SUCH_FIB;
1522
1523 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1524 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1525
1526 /* sanity check for reversed args or some such */
1527 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1528 return VNET_API_ERROR_INVALID_ARGUMENT;
1529
1530 /* Lookup the last address, to identify the interface involved */
1531 prefix.fp_len = 32;
1532 prefix.fp_proto = FIB_PROTOCOL_IP4;
1533 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1534
1535 fei = fib_table_lookup (fib_index, &prefix);
1536
1537 /* Couldn't find route to destination. Bail out. */
1538 if (fei == FIB_NODE_INDEX_INVALID)
1539 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1540
1541 sw_if_index = fib_entry_get_resolving_interface (fei);
1542
1543 /* Enable proxy arp on the interface */
1544 si = vnet_get_sw_interface (vnm, sw_if_index);
1545 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1546
1547 /* Configure proxy arp across the range */
1548 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1549
1550 if (rv)
1551 return rv;
1552
1553 do
1554 {
1555 dpo_id_t dpo = DPO_INVALID;
1556
1557 vec_add1 (tm->ip4_src_addresses, start[0]);
1558
1559 /* Add local adjacencies for the range */
1560
1561 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1562 NULL, &dpo);
1563 prefix.fp_len = 32;
1564 prefix.fp_proto = FIB_PROTOCOL_IP4;
1565 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1566
1567 fib_table_entry_special_dpo_update (fib_index,
1568 &prefix,
1569 FIB_SOURCE_API,
1570 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1571 dpo_reset (&dpo);
1572
1573 start_host_byte_order++;
1574 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1575 }
1576 while (start_host_byte_order <= end_host_byte_order);
1577
1578 return 0;
1579}
1580
1581/**
1582 * \brief Configure an ipv6 source address range
1583 * @param vm vlib_main_t pointer
1584 * @param start first ipv6 address in the source address range
1585 * @param end last ipv6 address in the source address range
1586 * @param table_id VRF / table ID, 0 for the default FIB
1587 * @return 0 if all OK, else an error indication from api_errno.h
1588 */
1589
1590int
1591tcp_configure_v6_source_address_range (vlib_main_t * vm,
1592 ip6_address_t * start,
1593 ip6_address_t * end, u32 table_id)
1594{
1595 tcp_main_t *tm = vnet_get_tcp_main ();
1596 fib_prefix_t prefix;
1597 u32 fib_index = 0;
1598 fib_node_index_t fei;
1599 u32 sw_if_index;
1600
1601 memset (&prefix, 0, sizeof (prefix));
1602
1603 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1604
1605 if (fib_index == ~0)
1606 return VNET_API_ERROR_NO_SUCH_FIB;
1607
1608 while (1)
1609 {
1610 int i;
1611 ip6_address_t tmp;
1612 dpo_id_t dpo = DPO_INVALID;
1613
1614 /* Remember this address */
1615 vec_add1 (tm->ip6_src_addresses, start[0]);
1616
1617 /* Lookup the prefix, to identify the interface involved */
1618 prefix.fp_len = 128;
1619 prefix.fp_proto = FIB_PROTOCOL_IP6;
1620 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1621
1622 fei = fib_table_lookup (fib_index, &prefix);
1623
1624 /* Couldn't find route to destination. Bail out. */
1625 if (fei == FIB_NODE_INDEX_INVALID)
1626 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1627
1628 sw_if_index = fib_entry_get_resolving_interface (fei);
1629
1630 if (sw_if_index == (u32) ~ 0)
1631 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1632
1633 /* Add a proxy neighbor discovery entry for this address */
1634 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1635
1636 /* Add a receive adjacency for this address */
1637 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1638 NULL, &dpo);
1639
1640 fib_table_entry_special_dpo_update (fib_index,
1641 &prefix,
1642 FIB_SOURCE_API,
1643 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1644 dpo_reset (&dpo);
1645
1646 /* Done with the entire range? */
1647 if (!memcmp (start, end, sizeof (start[0])))
1648 break;
1649
1650 /* Increment the address. DGMS. */
1651 tmp = start[0];
1652 for (i = 15; i >= 0; i--)
1653 {
1654 tmp.as_u8[i] += 1;
1655 if (tmp.as_u8[i] != 0)
1656 break;
1657 }
1658 start[0] = tmp;
1659 }
1660 return 0;
1661}
1662
Dave Barach2c25a622017-06-26 11:35:07 -04001663static clib_error_t *
1664tcp_src_address (vlib_main_t * vm,
1665 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1666{
Dave Barach2c25a622017-06-26 11:35:07 -04001667 ip4_address_t v4start, v4end;
1668 ip6_address_t v6start, v6end;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001669 u32 table_id = 0;
Dave Barach2c25a622017-06-26 11:35:07 -04001670 int v4set = 0;
1671 int v6set = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001672 int rv;
Dave Barach2c25a622017-06-26 11:35:07 -04001673
1674 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1675 {
1676 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1677 unformat_ip4_address, &v4end))
1678 v4set = 1;
1679 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1680 {
1681 memcpy (&v4end, &v4start, sizeof (v4start));
1682 v4set = 1;
1683 }
1684 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
Dave Barach3bbcfab2017-08-15 19:03:44 -04001685 unformat_ip6_address, &v6end))
Dave Barach2c25a622017-06-26 11:35:07 -04001686 v6set = 1;
1687 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1688 {
Yoann Desmouceaux6b297aa2017-09-20 10:34:22 +02001689 memcpy (&v6end, &v6start, sizeof (v6start));
Dave Barach2c25a622017-06-26 11:35:07 -04001690 v6set = 1;
1691 }
Dave Barach3bbcfab2017-08-15 19:03:44 -04001692 else if (unformat (input, "fib-table %d", &table_id))
1693 ;
Dave Barach2c25a622017-06-26 11:35:07 -04001694 else
1695 break;
1696 }
1697
1698 if (!v4set && !v6set)
1699 return clib_error_return (0, "at least one v4 or v6 address required");
1700
1701 if (v4set)
1702 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001703 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1704 table_id);
1705 switch (rv)
Dave Barach2c25a622017-06-26 11:35:07 -04001706 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001707 case 0:
1708 break;
1709
1710 case VNET_API_ERROR_NO_SUCH_FIB:
1711 return clib_error_return (0, "Invalid table-id %d", table_id);
1712
1713 case VNET_API_ERROR_INVALID_ARGUMENT:
1714 return clib_error_return (0, "Invalid address range %U - %U",
1715 format_ip4_address, &v4start,
1716 format_ip4_address, &v4end);
1717 default:
1718 return clib_error_return (0, "error %d", rv);
1719 break;
Dave Barach2c25a622017-06-26 11:35:07 -04001720 }
Dave Barach2c25a622017-06-26 11:35:07 -04001721 }
1722 if (v6set)
1723 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001724 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1725 table_id);
1726 switch (rv)
1727 {
1728 case 0:
1729 break;
1730
1731 case VNET_API_ERROR_NO_SUCH_FIB:
1732 return clib_error_return (0, "Invalid table-id %d", table_id);
1733
1734 default:
1735 return clib_error_return (0, "error %d", rv);
1736 break;
1737 }
Dave Barach2c25a622017-06-26 11:35:07 -04001738 }
1739 return 0;
1740}
1741
1742/* *INDENT-OFF* */
1743VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1744{
1745 .path = "tcp src-address",
1746 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1747 .function = tcp_src_address,
1748};
1749/* *INDENT-ON* */
1750
Florin Coras3eb50622017-07-13 01:24:57 -04001751static u8 *
1752tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1753{
1754#if TCP_SCOREBOARD_TRACE
Dave Barach2c25a622017-06-26 11:35:07 -04001755
Florin Coras3eb50622017-07-13 01:24:57 -04001756 scoreboard_trace_elt_t *block;
1757 int i = 0;
1758
1759 if (!sb->trace)
1760 return s;
1761
1762 s = format (s, "scoreboard trace:");
1763 vec_foreach (block, sb->trace)
1764 {
1765 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1766 block->ack, block->snd_una_max, block->group);
1767 if ((++i % 3) == 0)
1768 s = format (s, "\n");
1769 }
1770 return s;
1771#else
1772 return 0;
1773#endif
1774}
1775
1776static clib_error_t *
1777tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1778 vlib_cli_command_t * cmd_arg)
1779{
1780 transport_connection_t *tconn = 0;
1781 tcp_connection_t *tc;
1782 u8 *s = 0;
1783 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1784 {
1785 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1786 TRANSPORT_PROTO_TCP))
1787 ;
1788 else
1789 return clib_error_return (0, "unknown input `%U'",
1790 format_unformat_error, input);
1791 }
1792
1793 if (!TCP_SCOREBOARD_TRACE)
1794 {
1795 vlib_cli_output (vm, "scoreboard tracing not enabled");
1796 return 0;
1797 }
1798
1799 tc = tcp_get_connection_from_transport (tconn);
1800 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1801 vlib_cli_output (vm, "%v", s);
1802 return 0;
1803}
1804
1805/* *INDENT-OFF* */
1806VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1807{
1808 .path = "show tcp scoreboard trace",
1809 .short_help = "show tcp scoreboard trace <connection>",
1810 .function = tcp_show_scoreboard_trace_fn,
1811};
1812/* *INDENT-ON* */
1813
1814u8 *
1815tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1816{
1817 int i, trace_len;
1818 scoreboard_trace_elt_t *trace;
1819 u32 next_ack, left, group, has_new_ack = 0;
1820 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1821 sack_block_t *block;
1822
1823 if (!tc)
1824 return s;
1825
1826 memset (dummy_tc, 0, sizeof (*dummy_tc));
1827 tcp_connection_timers_init (dummy_tc);
1828 scoreboard_init (&dummy_tc->sack_sb);
1829 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1830
1831#if TCP_SCOREBOARD_TRACE
1832 trace = tc->sack_sb.trace;
1833 trace_len = vec_len (tc->sack_sb.trace);
1834#else
1835 trace = 0;
1836 trace_len = 0;
1837#endif
1838
1839 for (i = 0; i < trace_len; i++)
1840 {
1841 if (trace[i].ack != 0)
1842 {
1843 dummy_tc->snd_una = trace[i].ack - 1448;
1844 dummy_tc->snd_una_max = trace[i].ack;
1845 }
1846 }
1847
1848 left = 0;
1849 while (left < trace_len)
1850 {
1851 group = trace[left].group;
1852 vec_reset_length (dummy_tc->rcv_opts.sacks);
1853 has_new_ack = 0;
1854 while (trace[left].group == group)
1855 {
1856 if (trace[left].ack != 0)
1857 {
1858 if (verbose)
1859 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1860 trace[left].ack, trace[left].snd_una_max);
1861 dummy_tc->snd_una_max = trace[left].snd_una_max;
1862 next_ack = trace[left].ack;
1863 has_new_ack = 1;
1864 }
1865 else
1866 {
1867 if (verbose)
1868 s = format (s, "[%u, %u], ", trace[left].start,
1869 trace[left].end);
1870 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1871 block->start = trace[left].start;
1872 block->end = trace[left].end;
1873 }
1874 left++;
1875 }
1876
1877 /* Push segments */
1878 tcp_rcv_sacks (dummy_tc, next_ack);
1879 if (has_new_ack)
1880 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1881
1882 if (verbose)
1883 s = format (s, "result: %U", format_tcp_scoreboard,
1884 &dummy_tc->sack_sb);
1885
1886 }
1887 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1888
1889 return s;
1890}
1891
1892static clib_error_t *
1893tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1894 vlib_cli_command_t * cmd_arg)
1895{
1896 transport_connection_t *tconn = 0;
1897 tcp_connection_t *tc = 0;
1898 u8 *str = 0;
1899 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1900 {
1901 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1902 TRANSPORT_PROTO_TCP))
1903 ;
1904 else
1905 return clib_error_return (0, "unknown input `%U'",
1906 format_unformat_error, input);
1907 }
1908
1909 if (!TCP_SCOREBOARD_TRACE)
1910 {
1911 vlib_cli_output (vm, "scoreboard tracing not enabled");
1912 return 0;
1913 }
1914
1915 tc = tcp_get_connection_from_transport (tconn);
1916 if (!tc)
1917 {
1918 vlib_cli_output (vm, "connection not found");
1919 return 0;
1920 }
1921 str = tcp_scoreboard_replay (str, tc, 1);
1922 vlib_cli_output (vm, "%v", str);
1923 return 0;
1924}
1925
1926/* *INDENT-OFF* */
1927VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1928{
1929 .path = "tcp replay scoreboard",
1930 .short_help = "tcp replay scoreboard <connection>",
1931 .function = tcp_scoreboard_trace_fn,
1932};
1933/* *INDENT-ON* */
Dave Barach2c25a622017-06-26 11:35:07 -04001934
Pierre Pfister7fe51f32017-09-20 08:48:36 +02001935static clib_error_t *
1936show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
1937 vlib_cli_command_t * cmd_arg)
1938{
1939 tcp_main_t *tm = vnet_get_tcp_main ();
1940 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1941 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
1942 input);
1943 vlib_cli_output (vm, "IPv4 TCP punt: %s",
1944 tm->punt_unknown4 ? "enabled" : "disabled");
1945 vlib_cli_output (vm, "IPv6 TCP punt: %s",
1946 tm->punt_unknown6 ? "enabled" : "disabled");
1947 return 0;
1948}
1949/* *INDENT-OFF* */
1950VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
1951{
1952 .path = "show tcp punt",
1953 .short_help = "show tcp punt",
1954 .function = show_tcp_punt_fn,
1955};
1956/* *INDENT-ON* */
1957
Dave Barach68b0fb02017-02-28 15:15:56 -05001958/*
1959 * fd.io coding-style-patch-verification: ON
1960 *
1961 * Local Variables:
1962 * eval: (c-set-style "gnu")
1963 * End:
1964 */