blob: a4c13084413801e58762940da22b32ffe3ee6d37 [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
31static u32
Florin Coras04e53442017-07-16 17:12:15 -070032tcp_connection_bind (u32 session_index, transport_endpoint_t * lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -050033{
34 tcp_main_t *tm = &tcp_main;
35 tcp_connection_t *listener;
36
37 pool_get (tm->listener_pool, listener);
38 memset (listener, 0, sizeof (*listener));
39
40 listener->c_c_index = listener - tm->listener_pool;
Florin Coras04e53442017-07-16 17:12:15 -070041 listener->c_lcl_port = clib_host_to_net_u16 (lcl->port);
Dave Barach68b0fb02017-02-28 15:15:56 -050042
Florin Coras04e53442017-07-16 17:12:15 -070043 if (lcl->is_ip4)
Florin Coras6cf30ad2017-04-04 23:08:23 -070044 {
Florin Coras04e53442017-07-16 17:12:15 -070045 listener->c_lcl_ip4.as_u32 = lcl->ip.ip4.as_u32;
Florin Coras6cf30ad2017-04-04 23:08:23 -070046 listener->c_is_ip4 = 1;
Florin Coras6cf30ad2017-04-04 23:08:23 -070047 }
Dave Barach68b0fb02017-02-28 15:15:56 -050048 else
Florin Coras6cf30ad2017-04-04 23:08:23 -070049 {
Florin Coras04e53442017-07-16 17:12:15 -070050 clib_memcpy (&listener->c_lcl_ip6, &lcl->ip.ip6,
51 sizeof (ip6_address_t));
Dave Barach68b0fb02017-02-28 15:15:56 -050052
Florin Coras68810622017-07-24 17:40:28 -070053 }
54 listener->c_transport_proto = TRANSPORT_PROTO_TCP;
Dave Barach68b0fb02017-02-28 15:15:56 -050055 listener->c_s_index = session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050056 listener->state = TCP_STATE_LISTEN;
Dave Barach68b0fb02017-02-28 15:15:56 -050057
Florin Corase69f4952017-03-07 10:06:24 -080058 tcp_connection_timers_init (listener);
59
60 TCP_EVT_DBG (TCP_EVT_BIND, listener);
61
Dave Barach68b0fb02017-02-28 15:15:56 -050062 return listener->c_c_index;
63}
64
65u32
Florin Coras04e53442017-07-16 17:12:15 -070066tcp_session_bind (u32 session_index, transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -050067{
Florin Coras04e53442017-07-16 17:12:15 -070068 return tcp_connection_bind (session_index, tep);
Dave Barach68b0fb02017-02-28 15:15:56 -050069}
70
71static void
Florin Corase69f4952017-03-07 10:06:24 -080072tcp_connection_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050073{
74 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach2c25a622017-06-26 11:35:07 -040075 tcp_connection_t *tc;
76
77 tc = pool_elt_at_index (tm->listener_pool, listener_index);
78
79 TCP_EVT_DBG (TCP_EVT_UNBIND, tc);
80
81 /* Poison the entry */
82 if (CLIB_DEBUG > 0)
83 memset (tc, 0xFA, sizeof (*tc));
84
Dave Barach68b0fb02017-02-28 15:15:56 -050085 pool_put_index (tm->listener_pool, listener_index);
86}
87
88u32
Florin Corase69f4952017-03-07 10:06:24 -080089tcp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050090{
Florin Corase69f4952017-03-07 10:06:24 -080091 tcp_connection_unbind (listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050092 return 0;
93}
94
95transport_connection_t *
96tcp_session_get_listener (u32 listener_index)
97{
98 tcp_main_t *tm = vnet_get_tcp_main ();
99 tcp_connection_t *tc;
100 tc = pool_elt_at_index (tm->listener_pool, listener_index);
101 return &tc->connection;
102}
103
Florin Coras68810622017-07-24 17:40:28 -0700104always_inline void
105transport_endpoint_del (u32 tepi)
106{
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);
111}
112
113always_inline transport_endpoint_t *
114transport_endpoint_new (void)
115{
116 tcp_main_t *tm = vnet_get_tcp_main ();
117 transport_endpoint_t *tep;
118 pool_get (tm->local_endpoints, tep);
119 return tep;
120}
121
122/**
123 * Cleanup half-open connection
124 *
125 */
126void
127tcp_half_open_connection_del (tcp_connection_t * tc)
128{
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);
132 if (CLIB_DEBUG)
133 memset (tc, 0xFA, sizeof (*tc));
134 clib_spinlock_unlock_if_init (&tm->half_open_lock);
135}
136
137/**
138 * Try to cleanup half-open connection
139 *
140 * If called from a thread that doesn't own tc, the call won't have any
141 * effect.
142 *
143 * @param tc - connection to be cleaned up
144 * @return non-zero if cleanup failed.
145 */
146int
147tcp_half_open_connection_cleanup (tcp_connection_t * tc)
148{
149 /* Make sure this is the owning thread */
150 if (tc->c_thread_index != vlib_get_thread_index ())
151 return 1;
152 tcp_timer_reset (tc, TCP_TIMER_ESTABLISH);
153 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN);
154 tcp_half_open_connection_del (tc);
155 return 0;
156}
157
158tcp_connection_t *
159tcp_half_open_connection_new (void)
160{
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;
166 return tc;
167}
168
Dave Barach68b0fb02017-02-28 15:15:56 -0500169/**
170 * Cleans up connection state.
171 *
172 * No notifications.
173 */
174void
175tcp_connection_cleanup (tcp_connection_t * tc)
176{
177 tcp_main_t *tm = &tcp_main;
178 u32 tepi;
179 transport_endpoint_t *tep;
180
181 /* Cleanup local endpoint if this was an active connect */
182 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
Florin Coras66b11312017-07-31 17:18:03 -0700183 clib_net_to_host_u16 (tc->c_lcl_port));
Dave Barach68b0fb02017-02-28 15:15:56 -0500184 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
185 {
186 tep = pool_elt_at_index (tm->local_endpoints, tepi);
187 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
Florin Coras68810622017-07-24 17:40:28 -0700188 transport_endpoint_del (tepi);
Dave Barach68b0fb02017-02-28 15:15:56 -0500189 }
190
Florin Coras68810622017-07-24 17:40:28 -0700191 /* Check if connection is not yet fully established */
Dave Barach68b0fb02017-02-28 15:15:56 -0500192 if (tc->state == TCP_STATE_SYN_SENT)
Dave Barach2c25a622017-06-26 11:35:07 -0400193 {
Florin Coras68810622017-07-24 17:40:28 -0700194 /* 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);
Dave Barach2c25a622017-06-26 11:35:07 -0400198 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500199 else
Dave Barach2c25a622017-06-26 11:35:07 -0400200 {
201 int thread_index = tc->c_thread_index;
Florin Coras68810622017-07-24 17:40:28 -0700202
203 /* Make sure all timers are cleared */
204 tcp_connection_timers_reset (tc);
205
Dave Barach2c25a622017-06-26 11:35:07 -0400206 /* Poison the entry */
207 if (CLIB_DEBUG > 0)
208 memset (tc, 0xFA, sizeof (*tc));
209 pool_put (tm->connections[thread_index], tc);
210 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500211}
212
213/**
214 * Connection removal.
215 *
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.
219 */
220void
221tcp_connection_del (tcp_connection_t * tc)
222{
Florin Corase69f4952017-03-07 10:06:24 -0800223 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500224 stream_session_delete_notify (&tc->connection);
225 tcp_connection_cleanup (tc);
226}
227
Florin Coras6534b7a2017-07-18 05:38:03 -0400228tcp_connection_t *
229tcp_connection_new (u8 thread_index)
230{
231 tcp_main_t *tm = vnet_get_tcp_main ();
232 tcp_connection_t *tc;
233
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;
238 return tc;
239}
240
Florin Corasd79b41e2017-03-04 05:37:52 -0800241/** Notify session that connection has been reset.
242 *
243 * Switch state to closed and wait for session to call cleanup.
244 */
245void
246tcp_connection_reset (tcp_connection_t * tc)
247{
Florin Coras6534b7a2017-07-18 05:38:03 -0400248 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
Florin Coras11c05492017-05-10 12:29:14 -0700249 switch (tc->state)
250 {
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);
255 break;
256 case TCP_STATE_SYN_SENT:
Florin Coras68810622017-07-24 17:40:28 -0700257 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
Florin Coras6534b7a2017-07-18 05:38:03 -0400258 tcp_connection_cleanup (tc);
259 break;
Florin Coras11c05492017-05-10 12:29:14 -0700260 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;
Florin Coras6534b7a2017-07-18 05:38:03 -0400266 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800267
Florin Coras11c05492017-05-10 12:29:14 -0700268 /* Make sure all timers are cleared */
269 tcp_connection_timers_reset (tc);
Florin Coras11c05492017-05-10 12:29:14 -0700270 stream_session_reset_notify (&tc->connection);
Dave Barach2c25a622017-06-26 11:35:07 -0400271
272 /* Wait for cleanup from session layer but not forever */
Florin Coras68810622017-07-24 17:40:28 -0700273 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Florin Coras11c05492017-05-10 12:29:14 -0700274 break;
275 case TCP_STATE_CLOSED:
276 return;
277 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800278}
279
Dave Barach68b0fb02017-02-28 15:15:56 -0500280/**
281 * Begin connection closing procedure.
282 *
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
287 * calls cleanup.
288 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
289 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800290 *
291 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 */
293void
294tcp_connection_close (tcp_connection_t * tc)
295{
Florin Corase69f4952017-03-07 10:06:24 -0800296 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
297
Florin Corasb2215d62017-08-01 16:56:58 -0700298 /* Send/Program FIN if needed and switch state */
299 switch (tc->state)
300 {
301 case TCP_STATE_SYN_SENT:
302 tc->state = TCP_STATE_CLOSED;
303 break;
304 case TCP_STATE_SYN_RCVD:
305 tcp_send_fin (tc);
306 tc->state = TCP_STATE_FIN_WAIT_1;
307 break;
308 case TCP_STATE_ESTABLISHED:
309 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
310 tcp_send_fin (tc);
311 else
312 tc->flags |= TCP_CONN_FINPNDG;
313 tc->state = TCP_STATE_FIN_WAIT_1;
314 break;
315 case TCP_STATE_CLOSE_WAIT:
316 tcp_send_fin (tc);
317 tc->state = TCP_STATE_LAST_ACK;
318 break;
Florin Corasc87c91d2017-08-16 19:55:49 -0700319 case TCP_STATE_FIN_WAIT_1:
320 break;
Florin Corasb2215d62017-08-01 16:56:58 -0700321 default:
Florin Corasc87c91d2017-08-16 19:55:49 -0700322 clib_warning ("state: %u", tc->state);
Florin Corasb2215d62017-08-01 16:56:58 -0700323 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500324
Florin Coras6534b7a2017-07-18 05:38:03 -0400325 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500326
Florin Corasd79b41e2017-03-04 05:37:52 -0800327 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
328 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
329 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500330 tcp_connection_del (tc);
331}
332
333void
334tcp_session_close (u32 conn_index, u32 thread_index)
335{
336 tcp_connection_t *tc;
337 tc = tcp_connection_get (conn_index, thread_index);
338 tcp_connection_close (tc);
339}
340
341void
342tcp_session_cleanup (u32 conn_index, u32 thread_index)
343{
344 tcp_connection_t *tc;
345 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800346
347 /* Wait for the session tx events to clear */
348 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400349 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800350 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500351}
352
353void *
354ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
355{
356 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
357 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
358 ip_interface_address_t *ia = 0;
359
360 if (is_ip4)
361 {
362 /* *INDENT-OFF* */
363 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
364 ({
365 return ip_interface_address_get_address (lm4, ia);
366 }));
367 /* *INDENT-ON* */
368 }
369 else
370 {
371 /* *INDENT-OFF* */
372 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
373 ({
rootc9d1c5b2017-08-15 12:58:31 -0400374 ip6_address_t *rv;
375 rv = ip_interface_address_get_address (lm6, ia);
376 /* Trying to use a link-local ip6 src address is a fool's errand */
377 if (!ip6_address_is_link_local_unicast (rv))
378 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500379 }));
380 /* *INDENT-ON* */
381 }
382
383 return 0;
384}
385
Florin Corase04c2992017-03-01 08:17:34 -0800386#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500387/**
388 * Allocate local port and add if successful add entry to local endpoint
389 * table to mark the pair as used.
390 */
Florin Coras6534b7a2017-07-18 05:38:03 -0400391int
Florin Coras68810622017-07-24 17:40:28 -0700392tcp_allocate_local_port (ip46_address_t * ip)
Dave Barach68b0fb02017-02-28 15:15:56 -0500393{
Florin Coras68810622017-07-24 17:40:28 -0700394 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500395 transport_endpoint_t *tep;
Florin Coras66b11312017-07-31 17:18:03 -0700396 u32 tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800397 u16 min = 1024, max = 65535; /* XXX configurable ? */
Florin Coras66b11312017-07-31 17:18:03 -0700398 int tries, limit;
Dave Barach68b0fb02017-02-28 15:15:56 -0500399
Florin Coras66b11312017-07-31 17:18:03 -0700400 limit = max - min;
Dave Barach68b0fb02017-02-28 15:15:56 -0500401
Dave Barach2c25a622017-06-26 11:35:07 -0400402 /* Only support active opens from thread 0 */
403 ASSERT (vlib_get_thread_index () == 0);
404
Dave Barach68b0fb02017-02-28 15:15:56 -0500405 /* Search for first free slot */
Florin Coras66b11312017-07-31 17:18:03 -0700406 for (tries = 0; tries < limit; tries++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500407 {
Florin Corase04c2992017-03-01 08:17:34 -0800408 u16 port = 0;
409
410 /* Find a port in the specified range */
411 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500412 {
Florin Coras66b11312017-07-31 17:18:03 -0700413 port = random_u32 (&tm->port_allocator_seed) & PORT_MASK;
Florin Corase04c2992017-03-01 08:17:34 -0800414 if (PREDICT_TRUE (port >= min && port < max))
415 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 }
417
Florin Corase04c2992017-03-01 08:17:34 -0800418 /* Look it up */
Florin Coras68810622017-07-24 17:40:28 -0700419 tei = transport_endpoint_lookup (&tm->local_endpoints_table, ip, port);
Florin Corase04c2992017-03-01 08:17:34 -0800420 /* If not found, we're done */
421 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
422 {
Florin Coras68810622017-07-24 17:40:28 -0700423 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
424 tep = transport_endpoint_new ();
425 clib_memcpy (&tep->ip, ip, sizeof (*ip));
426 tep->port = port;
Florin Corase04c2992017-03-01 08:17:34 -0800427 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
428 tep - tm->local_endpoints);
Florin Coras68810622017-07-24 17:40:28 -0700429 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
430
Florin Corase04c2992017-03-01 08:17:34 -0800431 return tep->port;
432 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500433 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 return -1;
435}
436
437/**
438 * Initialize all connection timers as invalid
439 */
440void
441tcp_connection_timers_init (tcp_connection_t * tc)
442{
443 int i;
444
445 /* Set all to invalid */
446 for (i = 0; i < TCP_N_TIMERS; i++)
447 {
448 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
449 }
450
451 tc->rto = TCP_RTO_INIT;
452}
453
454/**
455 * Stop all connection timers
456 */
457void
458tcp_connection_timers_reset (tcp_connection_t * tc)
459{
460 int i;
461 for (i = 0; i < TCP_N_TIMERS; i++)
462 {
463 tcp_timer_reset (tc, i);
464 }
465}
466
Dave Barach2c25a622017-06-26 11:35:07 -0400467#if 0
Florin Corasf6359c82017-06-19 12:26:09 -0400468typedef struct ip4_tcp_hdr
469{
470 ip4_header_t ip;
471 tcp_header_t tcp;
472} ip4_tcp_hdr_t;
473
474typedef struct ip6_tcp_hdr
475{
476 ip6_header_t ip;
477 tcp_header_t tcp;
478} ip6_tcp_hdr_t;
479
480static void
481tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
482 dpo_id_t * result)
483{
484 const dpo_id_t *choice;
485 load_balance_t *lb;
486 int hash;
487
488 lb = load_balance_get (dpo->dpoi_index);
489 if (tc->c_is_ip4)
490 {
491 ip4_tcp_hdr_t hdr;
492 memset (&hdr, 0, sizeof (hdr));
493 hdr.ip.protocol = IP_PROTOCOL_TCP;
494 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
495 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
496 hdr.tcp.src_port = tc->c_lcl_port;
497 hdr.tcp.dst_port = tc->c_rmt_port;
498 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
499 }
500 else
501 {
502 ip6_tcp_hdr_t hdr;
503 memset (&hdr, 0, sizeof (hdr));
504 hdr.ip.protocol = IP_PROTOCOL_TCP;
505 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
506 sizeof (ip6_address_t));
507 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
508 sizeof (ip6_address_t));
509 hdr.tcp.src_port = tc->c_lcl_port;
510 hdr.tcp.dst_port = tc->c_rmt_port;
511 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
512 }
513 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
514 dpo_copy (result, choice);
515}
516
517fib_node_index_t
518tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
519{
520 fib_prefix_t prefix;
Florin Coras04e53442017-07-16 17:12:15 -0700521 u32 fib_index;
Florin Corasf6359c82017-06-19 12:26:09 -0400522
523 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
524 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
525 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
Florin Coras04e53442017-07-16 17:12:15 -0700526 fib_index = fib_table_find (prefix.fp_proto, tc->c_vrf);
527 return fib_table_lookup (fib_index, &prefix);
Florin Corasf6359c82017-06-19 12:26:09 -0400528}
529
530static int
531tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
532{
533 dpo_id_t choice = DPO_INVALID;
534 u32 output_node_index;
535 fib_entry_t *fe;
536
537 fe = fib_entry_get (tc->c_rmt_fei);
538 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
539 return -1;
540
541 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
542
543 output_node_index =
544 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
545 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
546 return 0;
547}
548
549/** Stack tcp connection on peer's fib entry.
550 *
551 * This ultimately populates the dpo the connection will use to send packets.
552 */
553static void
554tcp_connection_fib_attach (tcp_connection_t * tc)
555{
556 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
557
558 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
559
560 tcp_connection_stack_on_fib_entry (tc);
561}
Dave Barach2c25a622017-06-26 11:35:07 -0400562#endif /* 0 */
Florin Corasf6359c82017-06-19 12:26:09 -0400563
Dave Barach68b0fb02017-02-28 15:15:56 -0500564/** Initialize tcp connection variables
565 *
566 * Should be called after having received a msg from the peer, i.e., a SYN or
567 * a SYNACK, such that connection options have already been exchanged. */
568void
569tcp_connection_init_vars (tcp_connection_t * tc)
570{
571 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700572 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700573 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500574 tcp_cc_init (tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400575 // tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500576}
577
578int
Florin Coras04e53442017-07-16 17:12:15 -0700579tcp_connection_open (transport_endpoint_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500580{
581 tcp_main_t *tm = vnet_get_tcp_main ();
582 tcp_connection_t *tc;
583 fib_prefix_t prefix;
Florin Corasf6359c82017-06-19 12:26:09 -0400584 fib_node_index_t fei;
Florin Coras04e53442017-07-16 17:12:15 -0700585 u32 sw_if_index, fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500586 ip46_address_t lcl_addr;
Florin Coras6534b7a2017-07-18 05:38:03 -0400587 int lcl_port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
589 /*
590 * Find the local address and allocate port
591 */
592 memset (&lcl_addr, 0, sizeof (lcl_addr));
593
594 /* Find a FIB path to the destination */
Florin Coras04e53442017-07-16 17:12:15 -0700595 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
596 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
597 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
Dave Barach68b0fb02017-02-28 15:15:56 -0500598
Florin Coras04e53442017-07-16 17:12:15 -0700599 fib_index = fib_table_find (prefix.fp_proto, rmt->vrf);
tjancigaffef4042017-08-24 11:57:21 +0200600 if (fib_index == (u32) ~ 0)
601 {
602 clib_warning ("no fib table");
603 return -1;
604 }
605
Florin Coras04e53442017-07-16 17:12:15 -0700606 fei = fib_table_lookup (fib_index, &prefix);
Dave Barach68b0fb02017-02-28 15:15:56 -0500607
608 /* Couldn't find route to destination. Bail out. */
609 if (fei == FIB_NODE_INDEX_INVALID)
Florin Coras6534b7a2017-07-18 05:38:03 -0400610 {
611 clib_warning ("no route to destination");
612 return -1;
613 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500614
615 sw_if_index = fib_entry_get_resolving_interface (fei);
616
617 if (sw_if_index == (u32) ~ 0)
Florin Coras6534b7a2017-07-18 05:38:03 -0400618 {
619 clib_warning ("no resolving interface for %U", format_ip46_address,
Florin Coras04e53442017-07-16 17:12:15 -0700620 &rmt->ip, IP46_TYPE_IP4);
Florin Coras6534b7a2017-07-18 05:38:03 -0400621 return -1;
622 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500623
Florin Coras04e53442017-07-16 17:12:15 -0700624 if (rmt->is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500625 {
626 ip4_address_t *ip4;
Dave Barach2c25a622017-06-26 11:35:07 -0400627 int index;
628 if (vec_len (tm->ip4_src_addresses))
629 {
630 index = tm->last_v4_address_rotor++;
631 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
632 tm->last_v4_address_rotor = 0;
633 lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
634 }
635 else
636 {
637 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
638 lcl_addr.ip4.as_u32 = ip4->as_u32;
639 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500640 }
641 else
642 {
643 ip6_address_t *ip6;
Dave Barach2c25a622017-06-26 11:35:07 -0400644 int index;
645
646 if (vec_len (tm->ip6_src_addresses))
647 {
648 index = tm->last_v6_address_rotor++;
649 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
650 tm->last_v6_address_rotor = 0;
651 clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index],
652 sizeof (*ip6));
653 }
654 else
655 {
656 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
rootc9d1c5b2017-08-15 12:58:31 -0400657 if (ip6 == 0)
658 {
659 clib_warning ("no routable ip6 addresses on %U",
660 format_vnet_sw_if_index_name, vnet_get_main (),
661 sw_if_index);
662 return -1;
663 }
664
Dave Barach2c25a622017-06-26 11:35:07 -0400665 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
666 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500667 }
668
669 /* Allocate source port */
Florin Coras68810622017-07-24 17:40:28 -0700670 lcl_port = tcp_allocate_local_port (&lcl_addr);
Dave Barach68b0fb02017-02-28 15:15:56 -0500671 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800672 {
673 clib_warning ("Failed to allocate src port");
674 return -1;
675 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500676
677 /*
678 * Create connection and send SYN
679 */
Florin Coras68810622017-07-24 17:40:28 -0700680 clib_spinlock_lock_if_init (&tm->half_open_lock);
Florin Coras04e53442017-07-16 17:12:15 -0700681 tc = tcp_half_open_connection_new ();
Florin Coras04e53442017-07-16 17:12:15 -0700682 clib_memcpy (&tc->c_rmt_ip, &rmt->ip, sizeof (ip46_address_t));
Dave Barach68b0fb02017-02-28 15:15:56 -0500683 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
Florin Coras04e53442017-07-16 17:12:15 -0700684 tc->c_rmt_port = clib_host_to_net_u16 (rmt->port);
Dave Barach68b0fb02017-02-28 15:15:56 -0500685 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
Florin Coras04e53442017-07-16 17:12:15 -0700686 tc->c_is_ip4 = rmt->is_ip4;
Florin Coras68810622017-07-24 17:40:28 -0700687 tc->c_transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras04e53442017-07-16 17:12:15 -0700688 tc->c_vrf = rmt->vrf;
Dave Barach68b0fb02017-02-28 15:15:56 -0500689 /* The other connection vars will be initialized after SYN ACK */
690 tcp_connection_timers_init (tc);
691
Florin Corase69f4952017-03-07 10:06:24 -0800692 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400693 tc->state = TCP_STATE_SYN_SENT;
694 tcp_send_syn (tc);
Florin Coras68810622017-07-24 17:40:28 -0700695 clib_spinlock_unlock_if_init (&tm->half_open_lock);
Florin Corase69f4952017-03-07 10:06:24 -0800696
Dave Barach68b0fb02017-02-28 15:15:56 -0500697 return tc->c_c_index;
698}
699
700int
Florin Coras04e53442017-07-16 17:12:15 -0700701tcp_session_open (transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500702{
Florin Coras04e53442017-07-16 17:12:15 -0700703 return tcp_connection_open (tep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500704}
705
Florin Corase69f4952017-03-07 10:06:24 -0800706const char *tcp_dbg_evt_str[] = {
707#define _(sym, str) str,
708 foreach_tcp_dbg_evt
709#undef _
710};
711
712const char *tcp_fsm_states[] = {
713#define _(sym, str) str,
714 foreach_tcp_fsm_state
715#undef _
716};
717
Dave Barach68b0fb02017-02-28 15:15:56 -0500718u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800719format_tcp_state (u8 * s, va_list * args)
720{
Florin Corasbb292f42017-05-19 09:49:19 -0700721 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800722
Florin Corasbb292f42017-05-19 09:49:19 -0700723 if (state < TCP_N_STATES)
724 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800725 else
Florin Corasbb292f42017-05-19 09:49:19 -0700726 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800727 return s;
728}
729
730const char *tcp_conn_timers[] = {
731#define _(sym, str) str,
732 foreach_tcp_timer
733#undef _
734};
735
736u8 *
737format_tcp_timers (u8 * s, va_list * args)
738{
739 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700740 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800741
742 for (i = 0; i < TCP_N_TIMERS; i++)
743 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
744 last = i;
745
746 s = format (s, "[");
747 for (i = 0; i < last; i++)
748 {
749 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
750 s = format (s, "%s,", tcp_conn_timers[i]);
751 }
752
Florin Coras93992a92017-05-24 18:03:56 -0700753 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800754 s = format (s, "%s]", tcp_conn_timers[i]);
755 else
756 s = format (s, "]");
757
758 return s;
759}
760
761u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700762format_tcp_congestion_status (u8 * s, va_list * args)
763{
764 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
765 if (tcp_in_recovery (tc))
766 s = format (s, "recovery");
767 else if (tcp_in_fastrecovery (tc))
768 s = format (s, "fastrecovery");
769 else
770 s = format (s, "none");
771 return s;
772}
773
774u8 *
775format_tcp_vars (u8 * s, va_list * args)
776{
777 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Dave Barach2c25a622017-06-26 11:35:07 -0400778 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
Florin Corasbb292f42017-05-19 09:49:19 -0700779 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
780 tc->snd_una_max - tc->iss);
781 s = format (s, " rcv_nxt %u rcv_las %u\n",
782 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
783 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
784 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
785 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700786 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
787 tcp_flight_size (tc), tcp_available_snd_space (tc),
788 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700789 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
790 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700791 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
Dave Barach2c25a622017-06-26 11:35:07 -0400792 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
Florin Coras93992a92017-05-24 18:03:56 -0700793 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
794 tc->rcv_dupacks);
Dave Barach2c25a622017-06-26 11:35:07 -0400795 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
796 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
797 tc->tsecr_last_ack);
Florin Corasbb292f42017-05-19 09:49:19 -0700798 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
799 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
800 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Dave Barach2c25a622017-06-26 11:35:07 -0400801 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
802 tcp_time_now () - tc->tsval_recent_age);
Florin Coras1f152cd2017-08-18 19:28:03 -0700803 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
804 tc);
Florin Corasbb292f42017-05-19 09:49:19 -0700805 if (vec_len (tc->snd_sacks))
806 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
807
808 return s;
809}
810
811u8 *
812format_tcp_connection_id (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800813{
814 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700815 if (!tc)
816 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800817 if (tc->c_is_ip4)
818 {
819 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
820 format_ip4_address, &tc->c_lcl_ip4,
821 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
822 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
823 }
824 else
825 {
826 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
827 format_ip6_address, &tc->c_lcl_ip6,
828 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
829 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
830 }
831
832 return s;
833}
834
835u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700836format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800837{
838 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700839 u32 verbose = va_arg (*args, u32);
840
Florin Corasc87c91d2017-08-16 19:55:49 -0700841 if (!tc)
842 return s;
Florin Corasbb292f42017-05-19 09:49:19 -0700843 s = format (s, "%-50U", format_tcp_connection_id, tc);
844 if (verbose)
845 {
846 s = format (s, "%-15U", format_tcp_state, tc->state);
847 if (verbose > 1)
848 s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc);
849 }
Florin Coras3eb50622017-07-13 01:24:57 -0400850
Florin Corase69f4952017-03-07 10:06:24 -0800851 return s;
852}
853
854u8 *
855format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500856{
857 u32 tci = va_arg (*args, u32);
858 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700859 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500860 tcp_connection_t *tc;
861
862 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700863 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700864 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700865 else
Florin Coras1f152cd2017-08-18 19:28:03 -0700866 s = format (s, "empty\n");
Florin Coras93992a92017-05-24 18:03:56 -0700867 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500868}
869
870u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800871format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500872{
873 u32 tci = va_arg (*args, u32);
874 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700875 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500876}
877
878u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800879format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500880{
881 u32 tci = va_arg (*args, u32);
882 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700883 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500884}
885
Florin Coras06d11012017-05-17 14:21:51 -0700886u8 *
887format_tcp_sacks (u8 * s, va_list * args)
888{
889 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
890 sack_block_t *sacks = tc->snd_sacks;
891 sack_block_t *block;
Dave Barach2c25a622017-06-26 11:35:07 -0400892 int i, len = 0;
893
894 len = vec_len (sacks);
895 for (i = 0; i < len - 1; i++)
896 {
897 block = &sacks[i];
898 s = format (s, " start %u end %u\n", block->start - tc->irs,
899 block->end - tc->irs);
900 }
901 if (len)
902 {
903 block = &sacks[len - 1];
904 s = format (s, " start %u end %u", block->start - tc->irs,
905 block->end - tc->irs);
906 }
Florin Coras06d11012017-05-17 14:21:51 -0700907 return s;
908}
909
910u8 *
Florin Coras3eb50622017-07-13 01:24:57 -0400911format_tcp_rcv_sacks (u8 * s, va_list * args)
912{
913 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
914 sack_block_t *sacks = tc->rcv_opts.sacks;
915 sack_block_t *block;
916 int i, len = 0;
917
918 len = vec_len (sacks);
919 for (i = 0; i < len - 1; i++)
920 {
921 block = &sacks[i];
922 s = format (s, " start %u end %u\n", block->start - tc->iss,
923 block->end - tc->iss);
924 }
925 if (len)
926 {
927 block = &sacks[len - 1];
928 s = format (s, " start %u end %u", block->start - tc->iss,
929 block->end - tc->iss);
930 }
931 return s;
932}
933
934u8 *
Florin Coras06d11012017-05-17 14:21:51 -0700935format_tcp_sack_hole (u8 * s, va_list * args)
936{
937 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -0700938 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
939 if (tc)
940 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
941 else
942 s = format (s, " [%u, %u]", hole->start, hole->end);
Florin Coras06d11012017-05-17 14:21:51 -0700943 return s;
944}
945
946u8 *
947format_tcp_scoreboard (u8 * s, va_list * args)
948{
949 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -0700950 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras06d11012017-05-17 14:21:51 -0700951 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -0700952 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
953 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
954 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
955 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
956 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
957 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
958
Florin Coras06d11012017-05-17 14:21:51 -0700959 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -0700960 if (hole)
961 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
962
Florin Coras06d11012017-05-17 14:21:51 -0700963 while (hole)
964 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700965 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
Florin Coras06d11012017-05-17 14:21:51 -0700966 hole = scoreboard_next_hole (sb, hole);
967 }
Florin Coras3eb50622017-07-13 01:24:57 -0400968
Florin Coras06d11012017-05-17 14:21:51 -0700969 return s;
970}
971
Dave Barach68b0fb02017-02-28 15:15:56 -0500972transport_connection_t *
973tcp_session_get_transport (u32 conn_index, u32 thread_index)
974{
975 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
976 return &tc->connection;
977}
978
979transport_connection_t *
980tcp_half_open_session_get_transport (u32 conn_index)
981{
982 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
983 return &tc->connection;
984}
985
Florin Corasc8343412017-05-04 14:25:50 -0700986/**
987 * Compute maximum segment size for session layer.
988 *
989 * Since the result needs to be the actual data length, it first computes
990 * the tcp options to be used in the next burst and subtracts their
991 * length from the connection's snd_mss.
992 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500993u16
994tcp_session_send_mss (transport_connection_t * trans_conn)
995{
996 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -0700997
998 /* Ensure snd_mss does accurately reflect the amount of data we can push
999 * in a segment. This also makes sure that options are updated according to
1000 * the current state of the connection. */
1001 tcp_update_snd_mss (tc);
1002
Dave Barach68b0fb02017-02-28 15:15:56 -05001003 return tc->snd_mss;
1004}
1005
Florin Coras3af90fc2017-05-03 21:09:42 -07001006always_inline u32
1007tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
1008{
Dave Barach2c25a622017-06-26 11:35:07 -04001009 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Coras3af90fc2017-05-03 21:09:42 -07001010 {
Florin Corasdb84e572017-05-09 18:54:52 -07001011 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001012 }
1013
Florin Coras1f152cd2017-08-18 19:28:03 -07001014 /* If not snd_wnd constrained and we can't write at least a segment,
1015 * don't try at all */
Dave Barach2c25a622017-06-26 11:35:07 -04001016 if (PREDICT_FALSE (snd_space < tc->snd_mss))
Florin Coras1f152cd2017-08-18 19:28:03 -07001017 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001018
1019 /* round down to mss multiple */
1020 return snd_space - (snd_space % tc->snd_mss);
1021}
1022
Florin Coras6792ec02017-03-13 03:49:51 -07001023/**
1024 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -07001025 *
1026 * Takes into account available send space, snd_mss and the congestion
1027 * state of the connection. If possible, the value returned is a multiple
1028 * of snd_mss.
1029 *
1030 * @param tc tcp connection
1031 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -07001032 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001033u32
Florin Corasbb292f42017-05-19 09:49:19 -07001034tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001035{
Florin Corasf03a59a2017-06-09 21:07:32 -07001036 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -07001037
Florin Corasf03a59a2017-06-09 21:07:32 -07001038 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -07001039 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001040 snd_space = tcp_available_output_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -07001041
1042 /* If we haven't gotten dupacks or if we did and have gotten sacked
1043 * bytes then we can still send as per Limited Transmit (RFC3042) */
1044 if (PREDICT_FALSE (tc->rcv_dupacks != 0
1045 && (tcp_opts_sack_permitted (tc)
1046 && tc->sack_sb.last_sacked_bytes == 0)))
1047 {
1048 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
1049 tc->limited_transmit = tc->snd_nxt;
1050 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
1051
1052 snt_limited = tc->snd_nxt - tc->limited_transmit;
1053 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
1054 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001055 return tcp_round_snd_space (tc, snd_space);
1056 }
Florin Coras6792ec02017-03-13 03:49:51 -07001057
Florin Coras3af90fc2017-05-03 21:09:42 -07001058 if (tcp_in_recovery (tc))
1059 {
1060 tc->snd_nxt = tc->snd_una_max;
Florin Coras1f152cd2017-08-18 19:28:03 -07001061 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -07001062 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -07001063 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -07001064 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001065 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -07001066 }
1067
Florin Coras1f152cd2017-08-18 19:28:03 -07001068 /* RFC 5681: When previously unsent data is available and the new value of
1069 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
1070 * bytes of previously unsent data. */
1071 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -07001072 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001073 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
1074 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -07001075 tcp_fastrecovery_1_smss_on (tc);
1076 return tc->snd_mss;
1077 }
1078
1079 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001080}
1081
1082u32
Florin Corasbb292f42017-05-19 09:49:19 -07001083tcp_session_send_space (transport_connection_t * trans_conn)
1084{
1085 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras1f152cd2017-08-18 19:28:03 -07001086 return clib_min (tcp_snd_space (tc),
1087 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
Florin Corasbb292f42017-05-19 09:49:19 -07001088}
1089
Florin Coras93992a92017-05-24 18:03:56 -07001090i32
1091tcp_rcv_wnd_available (tcp_connection_t * tc)
1092{
1093 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1094}
1095
Florin Corasbb292f42017-05-19 09:49:19 -07001096u32
Florin Corasd79b41e2017-03-04 05:37:52 -08001097tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -05001098{
1099 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -07001100
1101 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1102
1103 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -08001104 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -05001105}
1106
1107/* *INDENT-OFF* */
Florin Coras04e53442017-07-16 17:12:15 -07001108const static transport_proto_vft_t tcp_proto = {
1109 .bind = tcp_session_bind,
Florin Corase69f4952017-03-07 10:06:24 -08001110 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001111 .push_header = tcp_push_header,
1112 .get_connection = tcp_session_get_transport,
1113 .get_listener = tcp_session_get_listener,
1114 .get_half_open = tcp_half_open_session_get_transport,
Florin Coras04e53442017-07-16 17:12:15 -07001115 .open = tcp_session_open,
Dave Barach68b0fb02017-02-28 15:15:56 -05001116 .close = tcp_session_close,
1117 .cleanup = tcp_session_cleanup,
1118 .send_mss = tcp_session_send_mss,
1119 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001120 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001121 .format_connection = format_tcp_session,
1122 .format_listener = format_tcp_listener_session,
1123 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001124};
1125/* *INDENT-ON* */
1126
1127void
1128tcp_timer_keep_handler (u32 conn_index)
1129{
Damjan Marion586afd72017-04-05 19:18:20 +02001130 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001131 tcp_connection_t *tc;
1132
Damjan Marion586afd72017-04-05 19:18:20 +02001133 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001134 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1135
1136 tcp_connection_close (tc);
1137}
1138
1139void
1140tcp_timer_establish_handler (u32 conn_index)
1141{
1142 tcp_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -05001143
1144 tc = tcp_half_open_connection_get (conn_index);
Florin Corasab0289a2017-08-14 11:25:25 -07001145 if (tc)
1146 {
1147 ASSERT (tc->state == TCP_STATE_SYN_SENT);
Florin Corasab0289a2017-08-14 11:25:25 -07001148 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
1149 }
1150 else
1151 {
1152 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
Dave Barachb7f1faa2017-08-29 11:43:37 -04001153 /* note: the connection may have already disappeared */
1154 if (PREDICT_FALSE (tc == 0))
1155 return;
1156
Florin Corasab0289a2017-08-14 11:25:25 -07001157 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
1158 }
rootc9d1c5b2017-08-15 12:58:31 -04001159 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
Dave Barach68b0fb02017-02-28 15:15:56 -05001160 tcp_connection_cleanup (tc);
1161}
1162
1163void
Florin Corasd79b41e2017-03-04 05:37:52 -08001164tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001165{
Damjan Marion586afd72017-04-05 19:18:20 +02001166 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001167 tcp_connection_t *tc;
1168
Damjan Marion586afd72017-04-05 19:18:20 +02001169 tc = tcp_connection_get (conn_index, thread_index);
Florin Coras68810622017-07-24 17:40:28 -07001170 if (!tc)
1171 return;
Florin Corasd79b41e2017-03-04 05:37:52 -08001172 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1173
1174 /* Session didn't come back with a close(). Send FIN either way
1175 * and switch to LAST_ACK. */
1176 if (tc->state == TCP_STATE_CLOSE_WAIT)
1177 {
1178 if (tc->flags & TCP_CONN_FINSNT)
1179 {
1180 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1181 }
1182
1183 tcp_send_fin (tc);
1184 tc->state = TCP_STATE_LAST_ACK;
1185
1186 /* Make sure we don't wait in LAST ACK forever */
1187 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1188
1189 /* Don't delete the connection yet */
1190 return;
1191 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001192
1193 tcp_connection_del (tc);
1194}
1195
1196/* *INDENT-OFF* */
1197static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1198{
1199 tcp_timer_retransmit_handler,
1200 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -07001201 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001202 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -08001203 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001204 tcp_timer_retransmit_syn_handler,
1205 tcp_timer_establish_handler
1206};
1207/* *INDENT-ON* */
1208
1209static void
1210tcp_expired_timers_dispatch (u32 * expired_timers)
1211{
1212 int i;
1213 u32 connection_index, timer_id;
1214
1215 for (i = 0; i < vec_len (expired_timers); i++)
1216 {
1217 /* Get session index and timer id */
1218 connection_index = expired_timers[i] & 0x0FFFFFFF;
1219 timer_id = expired_timers[i] >> 28;
1220
Florin Corase69f4952017-03-07 10:06:24 -08001221 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1222
Dave Barach68b0fb02017-02-28 15:15:56 -05001223 /* Handle expiration */
1224 (*timer_expiration_handlers[timer_id]) (connection_index);
1225 }
1226}
1227
1228void
1229tcp_initialize_timer_wheels (tcp_main_t * tm)
1230{
1231 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001232 /* *INDENT-OFF* */
1233 foreach_vlib_main (({
1234 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001235 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1236 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001237 tw->last_run_time = vlib_time_now (this_vlib_main);
1238 }));
1239 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001240}
1241
1242clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001243tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001244{
Dave Barach68b0fb02017-02-28 15:15:56 -05001245 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001246 ip_protocol_info_t *pi;
1247 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001248 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1249 clib_error_t *error = 0;
1250 u32 num_threads;
Dave Barachb7f1faa2017-08-29 11:43:37 -04001251 int thread;
Dave Barach2c25a622017-06-26 11:35:07 -04001252 tcp_connection_t *tc __attribute__ ((unused));
Florin Coras66b11312017-07-31 17:18:03 -07001253 u32 preallocated_connections_per_thread;
Dave Barach68b0fb02017-02-28 15:15:56 -05001254
Dave Barach68b0fb02017-02-28 15:15:56 -05001255 if ((error = vlib_call_init_function (vm, ip_main_init)))
1256 return error;
1257 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1258 return error;
1259 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1260 return error;
1261
1262 /*
1263 * Registrations
1264 */
1265
1266 /* Register with IP */
1267 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1268 if (pi == 0)
1269 return clib_error_return (0, "TCP protocol info AWOL");
1270 pi->format_header = format_tcp_header;
1271 pi->unformat_pg_edit = unformat_pg_tcp_header;
1272
1273 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
rootc9d1c5b2017-08-15 12:58:31 -04001274 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001275
Florin Coras04e53442017-07-16 17:12:15 -07001276 /* Register as transport with session layer */
Florin Coras68810622017-07-24 17:40:28 -07001277 session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1278 session_register_transport (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
Dave Barach68b0fb02017-02-28 15:15:56 -05001279
1280 /*
1281 * Initialize data structures
1282 */
1283
1284 num_threads = 1 /* main thread */ + vtm->n_threads;
1285 vec_validate (tm->connections, num_threads - 1);
1286
Dave Barach2c25a622017-06-26 11:35:07 -04001287 /*
Florin Coras66b11312017-07-31 17:18:03 -07001288 * Preallocate connections. Assume that thread 0 won't
1289 * use preallocated threads when running multi-core
Dave Barach2c25a622017-06-26 11:35:07 -04001290 */
Florin Coras66b11312017-07-31 17:18:03 -07001291 if (num_threads == 1)
Dave Barach2c25a622017-06-26 11:35:07 -04001292 {
Florin Coras66b11312017-07-31 17:18:03 -07001293 thread = 0;
1294 preallocated_connections_per_thread = tm->preallocated_connections;
1295 }
1296 else
1297 {
1298 thread = 1;
1299 preallocated_connections_per_thread =
1300 tm->preallocated_connections / (num_threads - 1);
1301 }
1302 for (; thread < num_threads; thread++)
1303 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001304 if (preallocated_connections_per_thread)
1305 pool_init_fixed (tm->connections[thread],
1306 preallocated_connections_per_thread);
Dave Barach2c25a622017-06-26 11:35:07 -04001307 }
1308
1309 /*
Dave Barachb7f1faa2017-08-29 11:43:37 -04001310 * Use a preallocated half-open connection pool?
Dave Barach2c25a622017-06-26 11:35:07 -04001311 */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001312 if (tm->preallocated_half_open_connections)
1313 pool_init_fixed (tm->half_open_connections,
1314 tm->preallocated_half_open_connections);
Dave Barach2c25a622017-06-26 11:35:07 -04001315
Dave Barach68b0fb02017-02-28 15:15:56 -05001316 /* Initialize per worker thread tx buffers (used for control messages) */
1317 vec_validate (tm->tx_buffers, num_threads - 1);
1318
1319 /* Initialize timer wheels */
1320 vec_validate (tm->timer_wheels, num_threads - 1);
1321 tcp_initialize_timer_wheels (tm);
1322
Dave Barach68b0fb02017-02-28 15:15:56 -05001323 /* Initialize clocks per tick for TCP timestamp. Used to compute
1324 * monotonically increasing timestamps. */
1325 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1326 / TCP_TSTAMP_RESOLUTION;
1327
Dave Barachd84ba852017-08-22 17:56:46 -04001328 if (tm->local_endpoints_table_buckets == 0)
1329 tm->local_endpoints_table_buckets = 250000;
1330 if (tm->local_endpoints_table_memory == 0)
1331 tm->local_endpoints_table_memory = 512 << 20;
1332
Dave Barach68b0fb02017-02-28 15:15:56 -05001333 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
Dave Barachd84ba852017-08-22 17:56:46 -04001334 tm->local_endpoints_table_buckets,
1335 tm->local_endpoints_table_memory);
Florin Coras66b11312017-07-31 17:18:03 -07001336
1337 /* Initialize [port-allocator] random number seed */
1338 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
1339
Florin Coras04e53442017-07-16 17:12:15 -07001340 if (num_threads > 1)
Florin Coras68810622017-07-24 17:40:28 -07001341 {
1342 clib_spinlock_init (&tm->half_open_lock);
1343 clib_spinlock_init (&tm->local_endpoints_lock);
1344 }
Florin Coras66b11312017-07-31 17:18:03 -07001345
1346 vec_validate (tm->tx_frames[0], num_threads - 1);
1347 vec_validate (tm->tx_frames[1], num_threads - 1);
1348
Florin Corasb2215d62017-08-01 16:56:58 -07001349 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1350 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Coras82d3ec82017-08-14 08:10:42 -07001351
1352 vec_validate (tm->time_now, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001353 return error;
1354}
1355
Florin Corasa0b34a72017-03-07 01:20:52 -08001356clib_error_t *
1357vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1358{
1359 if (is_en)
1360 {
1361 if (tcp_main.is_enabled)
1362 return 0;
1363
1364 return tcp_main_enable (vm);
1365 }
1366 else
1367 {
1368 tcp_main.is_enabled = 0;
1369 }
1370
1371 return 0;
1372}
1373
1374clib_error_t *
1375tcp_init (vlib_main_t * vm)
1376{
1377 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001378 tm->is_enabled = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001379 tcp_api_reference ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001380 return 0;
1381}
1382
Dave Barach68b0fb02017-02-28 15:15:56 -05001383VLIB_INIT_FUNCTION (tcp_init);
1384
Dave Barach2c25a622017-06-26 11:35:07 -04001385static clib_error_t *
1386tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1387{
1388 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barachd84ba852017-08-22 17:56:46 -04001389 u64 tmp;
Dave Barach2c25a622017-06-26 11:35:07 -04001390
1391 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1392 {
1393 if (unformat
1394 (input, "preallocated-connections %d",
1395 &tm->preallocated_connections))
1396 ;
1397 else if (unformat (input, "preallocated-half-open-connections %d",
1398 &tm->preallocated_half_open_connections))
1399 ;
Dave Barachd84ba852017-08-22 17:56:46 -04001400 else if (unformat (input, "local-endpoints-table-memory %U",
1401 unformat_memory_size, &tmp))
1402 {
1403 if (tmp >= 0x100000000)
1404 return clib_error_return (0, "memory size %llx (%lld) too large",
1405 tmp, tmp);
1406 tm->local_endpoints_table_memory = tmp;
1407 }
1408 else if (unformat (input, "local-endpoints-table-buckets %d",
1409 &tm->local_endpoints_table_buckets))
1410 ;
1411
1412
Dave Barach2c25a622017-06-26 11:35:07 -04001413 else
1414 return clib_error_return (0, "unknown input `%U'",
1415 format_unformat_error, input);
1416 }
1417 return 0;
1418}
1419
1420VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1421
Dave Barach3bbcfab2017-08-15 19:03:44 -04001422
1423/**
1424 * \brief Configure an ipv4 source address range
1425 * @param vm vlib_main_t pointer
1426 * @param start first ipv4 address in the source address range
1427 * @param end last ipv4 address in the source address range
1428 * @param table_id VRF / table ID, 0 for the default FIB
1429 * @return 0 if all OK, else an error indication from api_errno.h
1430 */
1431
1432int
1433tcp_configure_v4_source_address_range (vlib_main_t * vm,
1434 ip4_address_t * start,
1435 ip4_address_t * end, u32 table_id)
1436{
1437 tcp_main_t *tm = vnet_get_tcp_main ();
1438 vnet_main_t *vnm = vnet_get_main ();
1439 u32 start_host_byte_order, end_host_byte_order;
1440 fib_prefix_t prefix;
1441 vnet_sw_interface_t *si;
1442 fib_node_index_t fei;
1443 u32 fib_index = 0;
1444 u32 sw_if_index;
1445 int rv;
1446 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1447 ip4_address_t * hi_addr, u32 fib_index,
1448 int is_del);
1449
1450 memset (&prefix, 0, sizeof (prefix));
1451
1452 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1453
1454 if (fib_index == ~0)
1455 return VNET_API_ERROR_NO_SUCH_FIB;
1456
1457 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1458 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1459
1460 /* sanity check for reversed args or some such */
1461 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1462 return VNET_API_ERROR_INVALID_ARGUMENT;
1463
1464 /* Lookup the last address, to identify the interface involved */
1465 prefix.fp_len = 32;
1466 prefix.fp_proto = FIB_PROTOCOL_IP4;
1467 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1468
1469 fei = fib_table_lookup (fib_index, &prefix);
1470
1471 /* Couldn't find route to destination. Bail out. */
1472 if (fei == FIB_NODE_INDEX_INVALID)
1473 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1474
1475 sw_if_index = fib_entry_get_resolving_interface (fei);
1476
1477 /* Enable proxy arp on the interface */
1478 si = vnet_get_sw_interface (vnm, sw_if_index);
1479 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1480
1481 /* Configure proxy arp across the range */
1482 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1483
1484 if (rv)
1485 return rv;
1486
1487 do
1488 {
1489 dpo_id_t dpo = DPO_INVALID;
1490
1491 vec_add1 (tm->ip4_src_addresses, start[0]);
1492
1493 /* Add local adjacencies for the range */
1494
1495 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1496 NULL, &dpo);
1497 prefix.fp_len = 32;
1498 prefix.fp_proto = FIB_PROTOCOL_IP4;
1499 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1500
1501 fib_table_entry_special_dpo_update (fib_index,
1502 &prefix,
1503 FIB_SOURCE_API,
1504 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1505 dpo_reset (&dpo);
1506
1507 start_host_byte_order++;
1508 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1509 }
1510 while (start_host_byte_order <= end_host_byte_order);
1511
1512 return 0;
1513}
1514
1515/**
1516 * \brief Configure an ipv6 source address range
1517 * @param vm vlib_main_t pointer
1518 * @param start first ipv6 address in the source address range
1519 * @param end last ipv6 address in the source address range
1520 * @param table_id VRF / table ID, 0 for the default FIB
1521 * @return 0 if all OK, else an error indication from api_errno.h
1522 */
1523
1524int
1525tcp_configure_v6_source_address_range (vlib_main_t * vm,
1526 ip6_address_t * start,
1527 ip6_address_t * end, u32 table_id)
1528{
1529 tcp_main_t *tm = vnet_get_tcp_main ();
1530 fib_prefix_t prefix;
1531 u32 fib_index = 0;
1532 fib_node_index_t fei;
1533 u32 sw_if_index;
1534
1535 memset (&prefix, 0, sizeof (prefix));
1536
1537 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1538
1539 if (fib_index == ~0)
1540 return VNET_API_ERROR_NO_SUCH_FIB;
1541
1542 while (1)
1543 {
1544 int i;
1545 ip6_address_t tmp;
1546 dpo_id_t dpo = DPO_INVALID;
1547
1548 /* Remember this address */
1549 vec_add1 (tm->ip6_src_addresses, start[0]);
1550
1551 /* Lookup the prefix, to identify the interface involved */
1552 prefix.fp_len = 128;
1553 prefix.fp_proto = FIB_PROTOCOL_IP6;
1554 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1555
1556 fei = fib_table_lookup (fib_index, &prefix);
1557
1558 /* Couldn't find route to destination. Bail out. */
1559 if (fei == FIB_NODE_INDEX_INVALID)
1560 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1561
1562 sw_if_index = fib_entry_get_resolving_interface (fei);
1563
1564 if (sw_if_index == (u32) ~ 0)
1565 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1566
1567 /* Add a proxy neighbor discovery entry for this address */
1568 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1569
1570 /* Add a receive adjacency for this address */
1571 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1572 NULL, &dpo);
1573
1574 fib_table_entry_special_dpo_update (fib_index,
1575 &prefix,
1576 FIB_SOURCE_API,
1577 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1578 dpo_reset (&dpo);
1579
1580 /* Done with the entire range? */
1581 if (!memcmp (start, end, sizeof (start[0])))
1582 break;
1583
1584 /* Increment the address. DGMS. */
1585 tmp = start[0];
1586 for (i = 15; i >= 0; i--)
1587 {
1588 tmp.as_u8[i] += 1;
1589 if (tmp.as_u8[i] != 0)
1590 break;
1591 }
1592 start[0] = tmp;
1593 }
1594 return 0;
1595}
1596
Dave Barach2c25a622017-06-26 11:35:07 -04001597static clib_error_t *
1598tcp_src_address (vlib_main_t * vm,
1599 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1600{
Dave Barach2c25a622017-06-26 11:35:07 -04001601 ip4_address_t v4start, v4end;
1602 ip6_address_t v6start, v6end;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001603 u32 table_id = 0;
Dave Barach2c25a622017-06-26 11:35:07 -04001604 int v4set = 0;
1605 int v6set = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001606 int rv;
Dave Barach2c25a622017-06-26 11:35:07 -04001607
1608 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1609 {
1610 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1611 unformat_ip4_address, &v4end))
1612 v4set = 1;
1613 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1614 {
1615 memcpy (&v4end, &v4start, sizeof (v4start));
1616 v4set = 1;
1617 }
1618 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
Dave Barach3bbcfab2017-08-15 19:03:44 -04001619 unformat_ip6_address, &v6end))
Dave Barach2c25a622017-06-26 11:35:07 -04001620 v6set = 1;
1621 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1622 {
1623 memcpy (&v6end, &v6start, sizeof (v4start));
1624 v6set = 1;
1625 }
Dave Barach3bbcfab2017-08-15 19:03:44 -04001626 else if (unformat (input, "fib-table %d", &table_id))
1627 ;
Dave Barach2c25a622017-06-26 11:35:07 -04001628 else
1629 break;
1630 }
1631
1632 if (!v4set && !v6set)
1633 return clib_error_return (0, "at least one v4 or v6 address required");
1634
1635 if (v4set)
1636 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001637 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1638 table_id);
1639 switch (rv)
Dave Barach2c25a622017-06-26 11:35:07 -04001640 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001641 case 0:
1642 break;
1643
1644 case VNET_API_ERROR_NO_SUCH_FIB:
1645 return clib_error_return (0, "Invalid table-id %d", table_id);
1646
1647 case VNET_API_ERROR_INVALID_ARGUMENT:
1648 return clib_error_return (0, "Invalid address range %U - %U",
1649 format_ip4_address, &v4start,
1650 format_ip4_address, &v4end);
1651 default:
1652 return clib_error_return (0, "error %d", rv);
1653 break;
Dave Barach2c25a622017-06-26 11:35:07 -04001654 }
Dave Barach2c25a622017-06-26 11:35:07 -04001655 }
1656 if (v6set)
1657 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001658 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1659 table_id);
1660 switch (rv)
1661 {
1662 case 0:
1663 break;
1664
1665 case VNET_API_ERROR_NO_SUCH_FIB:
1666 return clib_error_return (0, "Invalid table-id %d", table_id);
1667
1668 default:
1669 return clib_error_return (0, "error %d", rv);
1670 break;
1671 }
Dave Barach2c25a622017-06-26 11:35:07 -04001672 }
1673 return 0;
1674}
1675
1676/* *INDENT-OFF* */
1677VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1678{
1679 .path = "tcp src-address",
1680 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1681 .function = tcp_src_address,
1682};
1683/* *INDENT-ON* */
1684
Florin Coras3eb50622017-07-13 01:24:57 -04001685static u8 *
1686tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1687{
1688#if TCP_SCOREBOARD_TRACE
Dave Barach2c25a622017-06-26 11:35:07 -04001689
Florin Coras3eb50622017-07-13 01:24:57 -04001690 scoreboard_trace_elt_t *block;
1691 int i = 0;
1692
1693 if (!sb->trace)
1694 return s;
1695
1696 s = format (s, "scoreboard trace:");
1697 vec_foreach (block, sb->trace)
1698 {
1699 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1700 block->ack, block->snd_una_max, block->group);
1701 if ((++i % 3) == 0)
1702 s = format (s, "\n");
1703 }
1704 return s;
1705#else
1706 return 0;
1707#endif
1708}
1709
1710static clib_error_t *
1711tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1712 vlib_cli_command_t * cmd_arg)
1713{
1714 transport_connection_t *tconn = 0;
1715 tcp_connection_t *tc;
1716 u8 *s = 0;
1717 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1718 {
1719 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1720 TRANSPORT_PROTO_TCP))
1721 ;
1722 else
1723 return clib_error_return (0, "unknown input `%U'",
1724 format_unformat_error, input);
1725 }
1726
1727 if (!TCP_SCOREBOARD_TRACE)
1728 {
1729 vlib_cli_output (vm, "scoreboard tracing not enabled");
1730 return 0;
1731 }
1732
1733 tc = tcp_get_connection_from_transport (tconn);
1734 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1735 vlib_cli_output (vm, "%v", s);
1736 return 0;
1737}
1738
1739/* *INDENT-OFF* */
1740VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1741{
1742 .path = "show tcp scoreboard trace",
1743 .short_help = "show tcp scoreboard trace <connection>",
1744 .function = tcp_show_scoreboard_trace_fn,
1745};
1746/* *INDENT-ON* */
1747
1748u8 *
1749tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1750{
1751 int i, trace_len;
1752 scoreboard_trace_elt_t *trace;
1753 u32 next_ack, left, group, has_new_ack = 0;
1754 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1755 sack_block_t *block;
1756
1757 if (!tc)
1758 return s;
1759
1760 memset (dummy_tc, 0, sizeof (*dummy_tc));
1761 tcp_connection_timers_init (dummy_tc);
1762 scoreboard_init (&dummy_tc->sack_sb);
1763 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1764
1765#if TCP_SCOREBOARD_TRACE
1766 trace = tc->sack_sb.trace;
1767 trace_len = vec_len (tc->sack_sb.trace);
1768#else
1769 trace = 0;
1770 trace_len = 0;
1771#endif
1772
1773 for (i = 0; i < trace_len; i++)
1774 {
1775 if (trace[i].ack != 0)
1776 {
1777 dummy_tc->snd_una = trace[i].ack - 1448;
1778 dummy_tc->snd_una_max = trace[i].ack;
1779 }
1780 }
1781
1782 left = 0;
1783 while (left < trace_len)
1784 {
1785 group = trace[left].group;
1786 vec_reset_length (dummy_tc->rcv_opts.sacks);
1787 has_new_ack = 0;
1788 while (trace[left].group == group)
1789 {
1790 if (trace[left].ack != 0)
1791 {
1792 if (verbose)
1793 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1794 trace[left].ack, trace[left].snd_una_max);
1795 dummy_tc->snd_una_max = trace[left].snd_una_max;
1796 next_ack = trace[left].ack;
1797 has_new_ack = 1;
1798 }
1799 else
1800 {
1801 if (verbose)
1802 s = format (s, "[%u, %u], ", trace[left].start,
1803 trace[left].end);
1804 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1805 block->start = trace[left].start;
1806 block->end = trace[left].end;
1807 }
1808 left++;
1809 }
1810
1811 /* Push segments */
1812 tcp_rcv_sacks (dummy_tc, next_ack);
1813 if (has_new_ack)
1814 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1815
1816 if (verbose)
1817 s = format (s, "result: %U", format_tcp_scoreboard,
1818 &dummy_tc->sack_sb);
1819
1820 }
1821 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1822
1823 return s;
1824}
1825
1826static clib_error_t *
1827tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1828 vlib_cli_command_t * cmd_arg)
1829{
1830 transport_connection_t *tconn = 0;
1831 tcp_connection_t *tc = 0;
1832 u8 *str = 0;
1833 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1834 {
1835 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1836 TRANSPORT_PROTO_TCP))
1837 ;
1838 else
1839 return clib_error_return (0, "unknown input `%U'",
1840 format_unformat_error, input);
1841 }
1842
1843 if (!TCP_SCOREBOARD_TRACE)
1844 {
1845 vlib_cli_output (vm, "scoreboard tracing not enabled");
1846 return 0;
1847 }
1848
1849 tc = tcp_get_connection_from_transport (tconn);
1850 if (!tc)
1851 {
1852 vlib_cli_output (vm, "connection not found");
1853 return 0;
1854 }
1855 str = tcp_scoreboard_replay (str, tc, 1);
1856 vlib_cli_output (vm, "%v", str);
1857 return 0;
1858}
1859
1860/* *INDENT-OFF* */
1861VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1862{
1863 .path = "tcp replay scoreboard",
1864 .short_help = "tcp replay scoreboard <connection>",
1865 .function = tcp_scoreboard_trace_fn,
1866};
1867/* *INDENT-ON* */
Dave Barach2c25a622017-06-26 11:35:07 -04001868
Dave Barach68b0fb02017-02-28 15:15:56 -05001869/*
1870 * fd.io coding-style-patch-verification: ON
1871 *
1872 * Local Variables:
1873 * eval: (c-set-style "gnu")
1874 * End:
1875 */