blob: 1d10f9bb81afa41825358d620ee2b337364d5a8d [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;
319 default:
320 clib_warning ("shouldn't be here");
321 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500322
Florin Coras6534b7a2017-07-18 05:38:03 -0400323 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500324
Florin Corasd79b41e2017-03-04 05:37:52 -0800325 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
326 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
327 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500328 tcp_connection_del (tc);
329}
330
331void
332tcp_session_close (u32 conn_index, u32 thread_index)
333{
334 tcp_connection_t *tc;
335 tc = tcp_connection_get (conn_index, thread_index);
336 tcp_connection_close (tc);
337}
338
339void
340tcp_session_cleanup (u32 conn_index, u32 thread_index)
341{
342 tcp_connection_t *tc;
343 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800344
345 /* Wait for the session tx events to clear */
346 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400347 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800348 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500349}
350
351void *
352ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
353{
354 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
355 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
356 ip_interface_address_t *ia = 0;
357
358 if (is_ip4)
359 {
360 /* *INDENT-OFF* */
361 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
362 ({
363 return ip_interface_address_get_address (lm4, ia);
364 }));
365 /* *INDENT-ON* */
366 }
367 else
368 {
369 /* *INDENT-OFF* */
370 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
371 ({
rootc9d1c5b2017-08-15 12:58:31 -0400372 ip6_address_t *rv;
373 rv = ip_interface_address_get_address (lm6, ia);
374 /* Trying to use a link-local ip6 src address is a fool's errand */
375 if (!ip6_address_is_link_local_unicast (rv))
376 return rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500377 }));
378 /* *INDENT-ON* */
379 }
380
381 return 0;
382}
383
Florin Corase04c2992017-03-01 08:17:34 -0800384#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500385/**
386 * Allocate local port and add if successful add entry to local endpoint
387 * table to mark the pair as used.
388 */
Florin Coras6534b7a2017-07-18 05:38:03 -0400389int
Florin Coras68810622017-07-24 17:40:28 -0700390tcp_allocate_local_port (ip46_address_t * ip)
Dave Barach68b0fb02017-02-28 15:15:56 -0500391{
Florin Coras68810622017-07-24 17:40:28 -0700392 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500393 transport_endpoint_t *tep;
Florin Coras66b11312017-07-31 17:18:03 -0700394 u32 tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800395 u16 min = 1024, max = 65535; /* XXX configurable ? */
Florin Coras66b11312017-07-31 17:18:03 -0700396 int tries, limit;
Dave Barach68b0fb02017-02-28 15:15:56 -0500397
Florin Coras66b11312017-07-31 17:18:03 -0700398 limit = max - min;
Dave Barach68b0fb02017-02-28 15:15:56 -0500399
Dave Barach2c25a622017-06-26 11:35:07 -0400400 /* Only support active opens from thread 0 */
401 ASSERT (vlib_get_thread_index () == 0);
402
Dave Barach68b0fb02017-02-28 15:15:56 -0500403 /* Search for first free slot */
Florin Coras66b11312017-07-31 17:18:03 -0700404 for (tries = 0; tries < limit; tries++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500405 {
Florin Corase04c2992017-03-01 08:17:34 -0800406 u16 port = 0;
407
408 /* Find a port in the specified range */
409 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500410 {
Florin Coras66b11312017-07-31 17:18:03 -0700411 port = random_u32 (&tm->port_allocator_seed) & PORT_MASK;
Florin Corase04c2992017-03-01 08:17:34 -0800412 if (PREDICT_TRUE (port >= min && port < max))
413 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500414 }
415
Florin Corase04c2992017-03-01 08:17:34 -0800416 /* Look it up */
Florin Coras68810622017-07-24 17:40:28 -0700417 tei = transport_endpoint_lookup (&tm->local_endpoints_table, ip, port);
Florin Corase04c2992017-03-01 08:17:34 -0800418 /* If not found, we're done */
419 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
420 {
Florin Coras68810622017-07-24 17:40:28 -0700421 clib_spinlock_lock_if_init (&tm->local_endpoints_lock);
422 tep = transport_endpoint_new ();
423 clib_memcpy (&tep->ip, ip, sizeof (*ip));
424 tep->port = port;
Florin Corase04c2992017-03-01 08:17:34 -0800425 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
426 tep - tm->local_endpoints);
Florin Coras68810622017-07-24 17:40:28 -0700427 clib_spinlock_unlock_if_init (&tm->local_endpoints_lock);
428
Florin Corase04c2992017-03-01 08:17:34 -0800429 return tep->port;
430 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500431 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500432 return -1;
433}
434
435/**
436 * Initialize all connection timers as invalid
437 */
438void
439tcp_connection_timers_init (tcp_connection_t * tc)
440{
441 int i;
442
443 /* Set all to invalid */
444 for (i = 0; i < TCP_N_TIMERS; i++)
445 {
446 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
447 }
448
449 tc->rto = TCP_RTO_INIT;
450}
451
452/**
453 * Stop all connection timers
454 */
455void
456tcp_connection_timers_reset (tcp_connection_t * tc)
457{
458 int i;
459 for (i = 0; i < TCP_N_TIMERS; i++)
460 {
461 tcp_timer_reset (tc, i);
462 }
463}
464
Dave Barach2c25a622017-06-26 11:35:07 -0400465#if 0
Florin Corasf6359c82017-06-19 12:26:09 -0400466typedef struct ip4_tcp_hdr
467{
468 ip4_header_t ip;
469 tcp_header_t tcp;
470} ip4_tcp_hdr_t;
471
472typedef struct ip6_tcp_hdr
473{
474 ip6_header_t ip;
475 tcp_header_t tcp;
476} ip6_tcp_hdr_t;
477
478static void
479tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
480 dpo_id_t * result)
481{
482 const dpo_id_t *choice;
483 load_balance_t *lb;
484 int hash;
485
486 lb = load_balance_get (dpo->dpoi_index);
487 if (tc->c_is_ip4)
488 {
489 ip4_tcp_hdr_t hdr;
490 memset (&hdr, 0, sizeof (hdr));
491 hdr.ip.protocol = IP_PROTOCOL_TCP;
492 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
493 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
494 hdr.tcp.src_port = tc->c_lcl_port;
495 hdr.tcp.dst_port = tc->c_rmt_port;
496 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
497 }
498 else
499 {
500 ip6_tcp_hdr_t hdr;
501 memset (&hdr, 0, sizeof (hdr));
502 hdr.ip.protocol = IP_PROTOCOL_TCP;
503 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
504 sizeof (ip6_address_t));
505 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
506 sizeof (ip6_address_t));
507 hdr.tcp.src_port = tc->c_lcl_port;
508 hdr.tcp.dst_port = tc->c_rmt_port;
509 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
510 }
511 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
512 dpo_copy (result, choice);
513}
514
515fib_node_index_t
516tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
517{
518 fib_prefix_t prefix;
Florin Coras04e53442017-07-16 17:12:15 -0700519 u32 fib_index;
Florin Corasf6359c82017-06-19 12:26:09 -0400520
521 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
522 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
523 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
Florin Coras04e53442017-07-16 17:12:15 -0700524 fib_index = fib_table_find (prefix.fp_proto, tc->c_vrf);
525 return fib_table_lookup (fib_index, &prefix);
Florin Corasf6359c82017-06-19 12:26:09 -0400526}
527
528static int
529tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
530{
531 dpo_id_t choice = DPO_INVALID;
532 u32 output_node_index;
533 fib_entry_t *fe;
534
535 fe = fib_entry_get (tc->c_rmt_fei);
536 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
537 return -1;
538
539 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
540
541 output_node_index =
542 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
543 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
544 return 0;
545}
546
547/** Stack tcp connection on peer's fib entry.
548 *
549 * This ultimately populates the dpo the connection will use to send packets.
550 */
551static void
552tcp_connection_fib_attach (tcp_connection_t * tc)
553{
554 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
555
556 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
557
558 tcp_connection_stack_on_fib_entry (tc);
559}
Dave Barach2c25a622017-06-26 11:35:07 -0400560#endif /* 0 */
Florin Corasf6359c82017-06-19 12:26:09 -0400561
Dave Barach68b0fb02017-02-28 15:15:56 -0500562/** Initialize tcp connection variables
563 *
564 * Should be called after having received a msg from the peer, i.e., a SYN or
565 * a SYNACK, such that connection options have already been exchanged. */
566void
567tcp_connection_init_vars (tcp_connection_t * tc)
568{
569 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700570 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700571 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 tcp_cc_init (tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400573 // tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500574}
575
576int
Florin Coras04e53442017-07-16 17:12:15 -0700577tcp_connection_open (transport_endpoint_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500578{
579 tcp_main_t *tm = vnet_get_tcp_main ();
580 tcp_connection_t *tc;
581 fib_prefix_t prefix;
Florin Corasf6359c82017-06-19 12:26:09 -0400582 fib_node_index_t fei;
Florin Coras04e53442017-07-16 17:12:15 -0700583 u32 sw_if_index, fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500584 ip46_address_t lcl_addr;
Florin Coras6534b7a2017-07-18 05:38:03 -0400585 int lcl_port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500586
587 /*
588 * Find the local address and allocate port
589 */
590 memset (&lcl_addr, 0, sizeof (lcl_addr));
591
592 /* Find a FIB path to the destination */
Florin Coras04e53442017-07-16 17:12:15 -0700593 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
594 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
595 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
Dave Barach68b0fb02017-02-28 15:15:56 -0500596
Florin Coras04e53442017-07-16 17:12:15 -0700597 fib_index = fib_table_find (prefix.fp_proto, rmt->vrf);
598 fei = fib_table_lookup (fib_index, &prefix);
Dave Barach68b0fb02017-02-28 15:15:56 -0500599
600 /* Couldn't find route to destination. Bail out. */
601 if (fei == FIB_NODE_INDEX_INVALID)
Florin Coras6534b7a2017-07-18 05:38:03 -0400602 {
603 clib_warning ("no route to destination");
604 return -1;
605 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500606
607 sw_if_index = fib_entry_get_resolving_interface (fei);
608
609 if (sw_if_index == (u32) ~ 0)
Florin Coras6534b7a2017-07-18 05:38:03 -0400610 {
611 clib_warning ("no resolving interface for %U", format_ip46_address,
Florin Coras04e53442017-07-16 17:12:15 -0700612 &rmt->ip, IP46_TYPE_IP4);
Florin Coras6534b7a2017-07-18 05:38:03 -0400613 return -1;
614 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500615
Florin Coras04e53442017-07-16 17:12:15 -0700616 if (rmt->is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 {
618 ip4_address_t *ip4;
Dave Barach2c25a622017-06-26 11:35:07 -0400619 int index;
620 if (vec_len (tm->ip4_src_addresses))
621 {
622 index = tm->last_v4_address_rotor++;
623 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
624 tm->last_v4_address_rotor = 0;
625 lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
626 }
627 else
628 {
629 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
630 lcl_addr.ip4.as_u32 = ip4->as_u32;
631 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500632 }
633 else
634 {
635 ip6_address_t *ip6;
Dave Barach2c25a622017-06-26 11:35:07 -0400636 int index;
637
638 if (vec_len (tm->ip6_src_addresses))
639 {
640 index = tm->last_v6_address_rotor++;
641 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
642 tm->last_v6_address_rotor = 0;
643 clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index],
644 sizeof (*ip6));
645 }
646 else
647 {
648 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
rootc9d1c5b2017-08-15 12:58:31 -0400649 if (ip6 == 0)
650 {
651 clib_warning ("no routable ip6 addresses on %U",
652 format_vnet_sw_if_index_name, vnet_get_main (),
653 sw_if_index);
654 return -1;
655 }
656
Dave Barach2c25a622017-06-26 11:35:07 -0400657 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
658 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 }
660
661 /* Allocate source port */
Florin Coras68810622017-07-24 17:40:28 -0700662 lcl_port = tcp_allocate_local_port (&lcl_addr);
Dave Barach68b0fb02017-02-28 15:15:56 -0500663 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800664 {
665 clib_warning ("Failed to allocate src port");
666 return -1;
667 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
669 /*
670 * Create connection and send SYN
671 */
Florin Coras68810622017-07-24 17:40:28 -0700672 clib_spinlock_lock_if_init (&tm->half_open_lock);
Florin Coras04e53442017-07-16 17:12:15 -0700673 tc = tcp_half_open_connection_new ();
Florin Coras04e53442017-07-16 17:12:15 -0700674 clib_memcpy (&tc->c_rmt_ip, &rmt->ip, sizeof (ip46_address_t));
Dave Barach68b0fb02017-02-28 15:15:56 -0500675 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
Florin Coras04e53442017-07-16 17:12:15 -0700676 tc->c_rmt_port = clib_host_to_net_u16 (rmt->port);
Dave Barach68b0fb02017-02-28 15:15:56 -0500677 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
Florin Coras04e53442017-07-16 17:12:15 -0700678 tc->c_is_ip4 = rmt->is_ip4;
Florin Coras68810622017-07-24 17:40:28 -0700679 tc->c_transport_proto = TRANSPORT_PROTO_TCP;
Florin Coras04e53442017-07-16 17:12:15 -0700680 tc->c_vrf = rmt->vrf;
Dave Barach68b0fb02017-02-28 15:15:56 -0500681 /* The other connection vars will be initialized after SYN ACK */
682 tcp_connection_timers_init (tc);
683
Florin Corase69f4952017-03-07 10:06:24 -0800684 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400685 tc->state = TCP_STATE_SYN_SENT;
686 tcp_send_syn (tc);
Florin Coras68810622017-07-24 17:40:28 -0700687 clib_spinlock_unlock_if_init (&tm->half_open_lock);
Florin Corase69f4952017-03-07 10:06:24 -0800688
Dave Barach68b0fb02017-02-28 15:15:56 -0500689 return tc->c_c_index;
690}
691
692int
Florin Coras04e53442017-07-16 17:12:15 -0700693tcp_session_open (transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500694{
Florin Coras04e53442017-07-16 17:12:15 -0700695 return tcp_connection_open (tep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500696}
697
Florin Corase69f4952017-03-07 10:06:24 -0800698const char *tcp_dbg_evt_str[] = {
699#define _(sym, str) str,
700 foreach_tcp_dbg_evt
701#undef _
702};
703
704const char *tcp_fsm_states[] = {
705#define _(sym, str) str,
706 foreach_tcp_fsm_state
707#undef _
708};
709
Dave Barach68b0fb02017-02-28 15:15:56 -0500710u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800711format_tcp_state (u8 * s, va_list * args)
712{
Florin Corasbb292f42017-05-19 09:49:19 -0700713 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800714
Florin Corasbb292f42017-05-19 09:49:19 -0700715 if (state < TCP_N_STATES)
716 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800717 else
Florin Corasbb292f42017-05-19 09:49:19 -0700718 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800719 return s;
720}
721
722const char *tcp_conn_timers[] = {
723#define _(sym, str) str,
724 foreach_tcp_timer
725#undef _
726};
727
728u8 *
729format_tcp_timers (u8 * s, va_list * args)
730{
731 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700732 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800733
734 for (i = 0; i < TCP_N_TIMERS; i++)
735 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
736 last = i;
737
738 s = format (s, "[");
739 for (i = 0; i < last; i++)
740 {
741 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
742 s = format (s, "%s,", tcp_conn_timers[i]);
743 }
744
Florin Coras93992a92017-05-24 18:03:56 -0700745 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800746 s = format (s, "%s]", tcp_conn_timers[i]);
747 else
748 s = format (s, "]");
749
750 return s;
751}
752
753u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700754format_tcp_congestion_status (u8 * s, va_list * args)
755{
756 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
757 if (tcp_in_recovery (tc))
758 s = format (s, "recovery");
759 else if (tcp_in_fastrecovery (tc))
760 s = format (s, "fastrecovery");
761 else
762 s = format (s, "none");
763 return s;
764}
765
766u8 *
767format_tcp_vars (u8 * s, va_list * args)
768{
769 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Dave Barach2c25a622017-06-26 11:35:07 -0400770 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
Florin Corasbb292f42017-05-19 09:49:19 -0700771 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
772 tc->snd_una_max - tc->iss);
773 s = format (s, " rcv_nxt %u rcv_las %u\n",
774 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
775 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
776 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
777 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700778 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
779 tcp_flight_size (tc), tcp_available_snd_space (tc),
780 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700781 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
782 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700783 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
Dave Barach2c25a622017-06-26 11:35:07 -0400784 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
Florin Coras93992a92017-05-24 18:03:56 -0700785 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
786 tc->rcv_dupacks);
Dave Barach2c25a622017-06-26 11:35:07 -0400787 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
788 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
789 tc->tsecr_last_ack);
Florin Corasbb292f42017-05-19 09:49:19 -0700790 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
791 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
792 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Dave Barach2c25a622017-06-26 11:35:07 -0400793 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
794 tcp_time_now () - tc->tsval_recent_age);
Florin Coras93992a92017-05-24 18:03:56 -0700795 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb);
Florin Corasbb292f42017-05-19 09:49:19 -0700796 if (vec_len (tc->snd_sacks))
797 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
798
799 return s;
800}
801
802u8 *
803format_tcp_connection_id (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800804{
805 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700806 if (!tc)
807 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800808 if (tc->c_is_ip4)
809 {
810 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
811 format_ip4_address, &tc->c_lcl_ip4,
812 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
813 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
814 }
815 else
816 {
817 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
818 format_ip6_address, &tc->c_lcl_ip6,
819 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
820 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
821 }
822
823 return s;
824}
825
826u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700827format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800828{
829 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700830 u32 verbose = va_arg (*args, u32);
831
832 s = format (s, "%-50U", format_tcp_connection_id, tc);
833 if (verbose)
834 {
835 s = format (s, "%-15U", format_tcp_state, tc->state);
836 if (verbose > 1)
837 s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc);
838 }
Florin Coras3eb50622017-07-13 01:24:57 -0400839
Florin Corase69f4952017-03-07 10:06:24 -0800840 return s;
841}
842
843u8 *
844format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500845{
846 u32 tci = va_arg (*args, u32);
847 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700848 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500849 tcp_connection_t *tc;
850
851 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700852 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700853 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700854 else
Florin Coras93992a92017-05-24 18:03:56 -0700855 s = format (s, "empty");
856 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500857}
858
859u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800860format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500861{
862 u32 tci = va_arg (*args, u32);
863 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700864 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500865}
866
867u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800868format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500869{
870 u32 tci = va_arg (*args, u32);
871 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700872 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500873}
874
Florin Coras06d11012017-05-17 14:21:51 -0700875u8 *
876format_tcp_sacks (u8 * s, va_list * args)
877{
878 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
879 sack_block_t *sacks = tc->snd_sacks;
880 sack_block_t *block;
Dave Barach2c25a622017-06-26 11:35:07 -0400881 int i, len = 0;
882
883 len = vec_len (sacks);
884 for (i = 0; i < len - 1; i++)
885 {
886 block = &sacks[i];
887 s = format (s, " start %u end %u\n", block->start - tc->irs,
888 block->end - tc->irs);
889 }
890 if (len)
891 {
892 block = &sacks[len - 1];
893 s = format (s, " start %u end %u", block->start - tc->irs,
894 block->end - tc->irs);
895 }
Florin Coras06d11012017-05-17 14:21:51 -0700896 return s;
897}
898
899u8 *
Florin Coras3eb50622017-07-13 01:24:57 -0400900format_tcp_rcv_sacks (u8 * s, va_list * args)
901{
902 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
903 sack_block_t *sacks = tc->rcv_opts.sacks;
904 sack_block_t *block;
905 int i, len = 0;
906
907 len = vec_len (sacks);
908 for (i = 0; i < len - 1; i++)
909 {
910 block = &sacks[i];
911 s = format (s, " start %u end %u\n", block->start - tc->iss,
912 block->end - tc->iss);
913 }
914 if (len)
915 {
916 block = &sacks[len - 1];
917 s = format (s, " start %u end %u", block->start - tc->iss,
918 block->end - tc->iss);
919 }
920 return s;
921}
922
923u8 *
Florin Coras06d11012017-05-17 14:21:51 -0700924format_tcp_sack_hole (u8 * s, va_list * args)
925{
926 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
927 s = format (s, "[%u, %u]", hole->start, hole->end);
928 return s;
929}
930
931u8 *
932format_tcp_scoreboard (u8 * s, va_list * args)
933{
934 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
935 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -0700936 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
937 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
938 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
939 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
940 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
941 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
942
Florin Coras06d11012017-05-17 14:21:51 -0700943 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -0700944 if (hole)
945 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
946
Florin Coras06d11012017-05-17 14:21:51 -0700947 while (hole)
948 {
949 s = format (s, "%U", format_tcp_sack_hole, hole);
950 hole = scoreboard_next_hole (sb, hole);
951 }
Florin Coras3eb50622017-07-13 01:24:57 -0400952
Florin Coras06d11012017-05-17 14:21:51 -0700953 return s;
954}
955
Dave Barach68b0fb02017-02-28 15:15:56 -0500956transport_connection_t *
957tcp_session_get_transport (u32 conn_index, u32 thread_index)
958{
959 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
960 return &tc->connection;
961}
962
963transport_connection_t *
964tcp_half_open_session_get_transport (u32 conn_index)
965{
966 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
967 return &tc->connection;
968}
969
Florin Corasc8343412017-05-04 14:25:50 -0700970/**
971 * Compute maximum segment size for session layer.
972 *
973 * Since the result needs to be the actual data length, it first computes
974 * the tcp options to be used in the next burst and subtracts their
975 * length from the connection's snd_mss.
976 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500977u16
978tcp_session_send_mss (transport_connection_t * trans_conn)
979{
980 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -0700981
982 /* Ensure snd_mss does accurately reflect the amount of data we can push
983 * in a segment. This also makes sure that options are updated according to
984 * the current state of the connection. */
985 tcp_update_snd_mss (tc);
986
Dave Barach68b0fb02017-02-28 15:15:56 -0500987 return tc->snd_mss;
988}
989
Florin Coras3af90fc2017-05-03 21:09:42 -0700990always_inline u32
991tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
992{
Dave Barach2c25a622017-06-26 11:35:07 -0400993 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Coras3af90fc2017-05-03 21:09:42 -0700994 {
Florin Corasdb84e572017-05-09 18:54:52 -0700995 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700996 }
997
998 /* If we can't write at least a segment, don't try at all */
Dave Barach2c25a622017-06-26 11:35:07 -0400999 if (PREDICT_FALSE (snd_space < tc->snd_mss))
1000 {
1001 if (snd_space > clib_min (tc->mss, tc->rcv_opts.mss) - TCP_HDR_LEN_MAX)
1002 return snd_space;
1003 return 0;
1004 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001005
1006 /* round down to mss multiple */
1007 return snd_space - (snd_space % tc->snd_mss);
1008}
1009
Florin Coras6792ec02017-03-13 03:49:51 -07001010/**
1011 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -07001012 *
1013 * Takes into account available send space, snd_mss and the congestion
1014 * state of the connection. If possible, the value returned is a multiple
1015 * of snd_mss.
1016 *
1017 * @param tc tcp connection
1018 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -07001019 */
Dave Barach68b0fb02017-02-28 15:15:56 -05001020u32
Florin Corasbb292f42017-05-19 09:49:19 -07001021tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -05001022{
Florin Corasf03a59a2017-06-09 21:07:32 -07001023 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -07001024
Florin Corasf03a59a2017-06-09 21:07:32 -07001025 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -07001026 {
1027 snd_space = tcp_available_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -07001028
1029 /* If we haven't gotten dupacks or if we did and have gotten sacked
1030 * bytes then we can still send as per Limited Transmit (RFC3042) */
1031 if (PREDICT_FALSE (tc->rcv_dupacks != 0
1032 && (tcp_opts_sack_permitted (tc)
1033 && tc->sack_sb.last_sacked_bytes == 0)))
1034 {
1035 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
1036 tc->limited_transmit = tc->snd_nxt;
1037 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
1038
1039 snt_limited = tc->snd_nxt - tc->limited_transmit;
1040 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
1041 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001042 return tcp_round_snd_space (tc, snd_space);
1043 }
Florin Coras6792ec02017-03-13 03:49:51 -07001044
Florin Coras3af90fc2017-05-03 21:09:42 -07001045 if (tcp_in_recovery (tc))
1046 {
1047 tc->snd_nxt = tc->snd_una_max;
Florin Coras93992a92017-05-24 18:03:56 -07001048 snd_space = tcp_available_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -07001049 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -07001050 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -07001051 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -07001052 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -07001053 }
1054
1055 /* If in fast recovery, send 1 SMSS if wnd allows */
Florin Coras93992a92017-05-24 18:03:56 -07001056 if (tcp_in_fastrecovery (tc)
1057 && tcp_available_snd_space (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -07001058 {
1059 tcp_fastrecovery_1_smss_on (tc);
1060 return tc->snd_mss;
1061 }
1062
1063 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001064}
1065
1066u32
Florin Corasbb292f42017-05-19 09:49:19 -07001067tcp_session_send_space (transport_connection_t * trans_conn)
1068{
1069 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1070 return tcp_snd_space (tc);
1071}
1072
Florin Coras93992a92017-05-24 18:03:56 -07001073i32
1074tcp_rcv_wnd_available (tcp_connection_t * tc)
1075{
1076 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1077}
1078
Florin Corasbb292f42017-05-19 09:49:19 -07001079u32
Florin Corasd79b41e2017-03-04 05:37:52 -08001080tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -05001081{
1082 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -07001083
1084 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1085
1086 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -08001087 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -05001088}
1089
1090/* *INDENT-OFF* */
Florin Coras04e53442017-07-16 17:12:15 -07001091const static transport_proto_vft_t tcp_proto = {
1092 .bind = tcp_session_bind,
Florin Corase69f4952017-03-07 10:06:24 -08001093 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001094 .push_header = tcp_push_header,
1095 .get_connection = tcp_session_get_transport,
1096 .get_listener = tcp_session_get_listener,
1097 .get_half_open = tcp_half_open_session_get_transport,
Florin Coras04e53442017-07-16 17:12:15 -07001098 .open = tcp_session_open,
Dave Barach68b0fb02017-02-28 15:15:56 -05001099 .close = tcp_session_close,
1100 .cleanup = tcp_session_cleanup,
1101 .send_mss = tcp_session_send_mss,
1102 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001103 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001104 .format_connection = format_tcp_session,
1105 .format_listener = format_tcp_listener_session,
1106 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001107};
1108/* *INDENT-ON* */
1109
1110void
1111tcp_timer_keep_handler (u32 conn_index)
1112{
Damjan Marion586afd72017-04-05 19:18:20 +02001113 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001114 tcp_connection_t *tc;
1115
Damjan Marion586afd72017-04-05 19:18:20 +02001116 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001117 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1118
1119 tcp_connection_close (tc);
1120}
1121
1122void
1123tcp_timer_establish_handler (u32 conn_index)
1124{
1125 tcp_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -05001126
1127 tc = tcp_half_open_connection_get (conn_index);
Florin Corasab0289a2017-08-14 11:25:25 -07001128 if (tc)
1129 {
1130 ASSERT (tc->state == TCP_STATE_SYN_SENT);
Florin Corasab0289a2017-08-14 11:25:25 -07001131 stream_session_connect_notify (&tc->connection, 1 /* fail */ );
1132 }
1133 else
1134 {
1135 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
1136 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
1137 }
rootc9d1c5b2017-08-15 12:58:31 -04001138 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
Dave Barach68b0fb02017-02-28 15:15:56 -05001139 tcp_connection_cleanup (tc);
1140}
1141
1142void
Florin Corasd79b41e2017-03-04 05:37:52 -08001143tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001144{
Damjan Marion586afd72017-04-05 19:18:20 +02001145 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001146 tcp_connection_t *tc;
1147
Damjan Marion586afd72017-04-05 19:18:20 +02001148 tc = tcp_connection_get (conn_index, thread_index);
Florin Coras68810622017-07-24 17:40:28 -07001149 if (!tc)
1150 return;
Florin Corasd79b41e2017-03-04 05:37:52 -08001151 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1152
1153 /* Session didn't come back with a close(). Send FIN either way
1154 * and switch to LAST_ACK. */
1155 if (tc->state == TCP_STATE_CLOSE_WAIT)
1156 {
1157 if (tc->flags & TCP_CONN_FINSNT)
1158 {
1159 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1160 }
1161
1162 tcp_send_fin (tc);
1163 tc->state = TCP_STATE_LAST_ACK;
1164
1165 /* Make sure we don't wait in LAST ACK forever */
1166 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1167
1168 /* Don't delete the connection yet */
1169 return;
1170 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001171
1172 tcp_connection_del (tc);
1173}
1174
1175/* *INDENT-OFF* */
1176static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1177{
1178 tcp_timer_retransmit_handler,
1179 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -07001180 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001181 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -08001182 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001183 tcp_timer_retransmit_syn_handler,
1184 tcp_timer_establish_handler
1185};
1186/* *INDENT-ON* */
1187
1188static void
1189tcp_expired_timers_dispatch (u32 * expired_timers)
1190{
1191 int i;
1192 u32 connection_index, timer_id;
1193
1194 for (i = 0; i < vec_len (expired_timers); i++)
1195 {
1196 /* Get session index and timer id */
1197 connection_index = expired_timers[i] & 0x0FFFFFFF;
1198 timer_id = expired_timers[i] >> 28;
1199
Florin Corase69f4952017-03-07 10:06:24 -08001200 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1201
Dave Barach68b0fb02017-02-28 15:15:56 -05001202 /* Handle expiration */
1203 (*timer_expiration_handlers[timer_id]) (connection_index);
1204 }
1205}
1206
1207void
1208tcp_initialize_timer_wheels (tcp_main_t * tm)
1209{
1210 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001211 /* *INDENT-OFF* */
1212 foreach_vlib_main (({
1213 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001214 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1215 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001216 tw->last_run_time = vlib_time_now (this_vlib_main);
1217 }));
1218 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001219}
1220
1221clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001222tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001223{
Dave Barach68b0fb02017-02-28 15:15:56 -05001224 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001225 ip_protocol_info_t *pi;
1226 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001227 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1228 clib_error_t *error = 0;
1229 u32 num_threads;
Florin Coras66b11312017-07-31 17:18:03 -07001230 int i, thread;
Dave Barach2c25a622017-06-26 11:35:07 -04001231 tcp_connection_t *tc __attribute__ ((unused));
Florin Coras66b11312017-07-31 17:18:03 -07001232 u32 preallocated_connections_per_thread;
Dave Barach68b0fb02017-02-28 15:15:56 -05001233
Dave Barach68b0fb02017-02-28 15:15:56 -05001234 if ((error = vlib_call_init_function (vm, ip_main_init)))
1235 return error;
1236 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1237 return error;
1238 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1239 return error;
1240
1241 /*
1242 * Registrations
1243 */
1244
1245 /* Register with IP */
1246 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1247 if (pi == 0)
1248 return clib_error_return (0, "TCP protocol info AWOL");
1249 pi->format_header = format_tcp_header;
1250 pi->unformat_pg_edit = unformat_pg_tcp_header;
1251
1252 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
rootc9d1c5b2017-08-15 12:58:31 -04001253 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001254
Florin Coras04e53442017-07-16 17:12:15 -07001255 /* Register as transport with session layer */
Florin Coras68810622017-07-24 17:40:28 -07001256 session_register_transport (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1257 session_register_transport (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
Dave Barach68b0fb02017-02-28 15:15:56 -05001258
1259 /*
1260 * Initialize data structures
1261 */
1262
1263 num_threads = 1 /* main thread */ + vtm->n_threads;
1264 vec_validate (tm->connections, num_threads - 1);
1265
Dave Barach2c25a622017-06-26 11:35:07 -04001266 /*
Florin Coras66b11312017-07-31 17:18:03 -07001267 * Preallocate connections. Assume that thread 0 won't
1268 * use preallocated threads when running multi-core
Dave Barach2c25a622017-06-26 11:35:07 -04001269 */
Florin Coras66b11312017-07-31 17:18:03 -07001270 if (num_threads == 1)
Dave Barach2c25a622017-06-26 11:35:07 -04001271 {
Florin Coras66b11312017-07-31 17:18:03 -07001272 thread = 0;
1273 preallocated_connections_per_thread = tm->preallocated_connections;
1274 }
1275 else
1276 {
1277 thread = 1;
1278 preallocated_connections_per_thread =
1279 tm->preallocated_connections / (num_threads - 1);
1280 }
1281 for (; thread < num_threads; thread++)
1282 {
1283 for (i = 0; i < preallocated_connections_per_thread; i++)
Dave Barach2c25a622017-06-26 11:35:07 -04001284 pool_get (tm->connections[thread], tc);
1285
Florin Coras66b11312017-07-31 17:18:03 -07001286 for (i = 0; i < preallocated_connections_per_thread; i++)
Dave Barach2c25a622017-06-26 11:35:07 -04001287 pool_put_index (tm->connections[thread], i);
1288 }
1289
1290 /*
1291 * Preallocate half-open connections
1292 */
1293 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1294 pool_get (tm->half_open_connections, tc);
1295
1296 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1297 pool_put_index (tm->half_open_connections, i);
1298
Dave Barach68b0fb02017-02-28 15:15:56 -05001299 /* Initialize per worker thread tx buffers (used for control messages) */
1300 vec_validate (tm->tx_buffers, num_threads - 1);
1301
1302 /* Initialize timer wheels */
1303 vec_validate (tm->timer_wheels, num_threads - 1);
1304 tcp_initialize_timer_wheels (tm);
1305
Dave Barach68b0fb02017-02-28 15:15:56 -05001306 /* Initialize clocks per tick for TCP timestamp. Used to compute
1307 * monotonically increasing timestamps. */
1308 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1309 / TCP_TSTAMP_RESOLUTION;
1310
1311 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
Florin Coras66b11312017-07-31 17:18:03 -07001312 1000000 /* $$$$ config parameter nbuckets */ ,
1313 (512 << 20) /*$$$ config parameter table size */ );
1314
1315 /* Initialize [port-allocator] random number seed */
1316 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
1317
Florin Coras04e53442017-07-16 17:12:15 -07001318 if (num_threads > 1)
Florin Coras68810622017-07-24 17:40:28 -07001319 {
1320 clib_spinlock_init (&tm->half_open_lock);
1321 clib_spinlock_init (&tm->local_endpoints_lock);
1322 }
Florin Coras66b11312017-07-31 17:18:03 -07001323
1324 vec_validate (tm->tx_frames[0], num_threads - 1);
1325 vec_validate (tm->tx_frames[1], num_threads - 1);
1326
Florin Corasb2215d62017-08-01 16:56:58 -07001327 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1328 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Coras82d3ec82017-08-14 08:10:42 -07001329
1330 vec_validate (tm->time_now, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001331 return error;
1332}
1333
Florin Corasa0b34a72017-03-07 01:20:52 -08001334clib_error_t *
1335vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1336{
1337 if (is_en)
1338 {
1339 if (tcp_main.is_enabled)
1340 return 0;
1341
1342 return tcp_main_enable (vm);
1343 }
1344 else
1345 {
1346 tcp_main.is_enabled = 0;
1347 }
1348
1349 return 0;
1350}
1351
1352clib_error_t *
1353tcp_init (vlib_main_t * vm)
1354{
1355 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001356 tm->is_enabled = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001357 tcp_api_reference ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001358 return 0;
1359}
1360
Dave Barach68b0fb02017-02-28 15:15:56 -05001361VLIB_INIT_FUNCTION (tcp_init);
1362
Dave Barach2c25a622017-06-26 11:35:07 -04001363static clib_error_t *
1364tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1365{
1366 tcp_main_t *tm = vnet_get_tcp_main ();
1367
1368 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1369 {
1370 if (unformat
1371 (input, "preallocated-connections %d",
1372 &tm->preallocated_connections))
1373 ;
1374 else if (unformat (input, "preallocated-half-open-connections %d",
1375 &tm->preallocated_half_open_connections))
1376 ;
1377 else
1378 return clib_error_return (0, "unknown input `%U'",
1379 format_unformat_error, input);
1380 }
1381 return 0;
1382}
1383
1384VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1385
Dave Barach3bbcfab2017-08-15 19:03:44 -04001386
1387/**
1388 * \brief Configure an ipv4 source address range
1389 * @param vm vlib_main_t pointer
1390 * @param start first ipv4 address in the source address range
1391 * @param end last ipv4 address in the source address range
1392 * @param table_id VRF / table ID, 0 for the default FIB
1393 * @return 0 if all OK, else an error indication from api_errno.h
1394 */
1395
1396int
1397tcp_configure_v4_source_address_range (vlib_main_t * vm,
1398 ip4_address_t * start,
1399 ip4_address_t * end, u32 table_id)
1400{
1401 tcp_main_t *tm = vnet_get_tcp_main ();
1402 vnet_main_t *vnm = vnet_get_main ();
1403 u32 start_host_byte_order, end_host_byte_order;
1404 fib_prefix_t prefix;
1405 vnet_sw_interface_t *si;
1406 fib_node_index_t fei;
1407 u32 fib_index = 0;
1408 u32 sw_if_index;
1409 int rv;
1410 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1411 ip4_address_t * hi_addr, u32 fib_index,
1412 int is_del);
1413
1414 memset (&prefix, 0, sizeof (prefix));
1415
1416 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1417
1418 if (fib_index == ~0)
1419 return VNET_API_ERROR_NO_SUCH_FIB;
1420
1421 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1422 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1423
1424 /* sanity check for reversed args or some such */
1425 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1426 return VNET_API_ERROR_INVALID_ARGUMENT;
1427
1428 /* Lookup the last address, to identify the interface involved */
1429 prefix.fp_len = 32;
1430 prefix.fp_proto = FIB_PROTOCOL_IP4;
1431 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1432
1433 fei = fib_table_lookup (fib_index, &prefix);
1434
1435 /* Couldn't find route to destination. Bail out. */
1436 if (fei == FIB_NODE_INDEX_INVALID)
1437 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1438
1439 sw_if_index = fib_entry_get_resolving_interface (fei);
1440
1441 /* Enable proxy arp on the interface */
1442 si = vnet_get_sw_interface (vnm, sw_if_index);
1443 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1444
1445 /* Configure proxy arp across the range */
1446 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1447
1448 if (rv)
1449 return rv;
1450
1451 do
1452 {
1453 dpo_id_t dpo = DPO_INVALID;
1454
1455 vec_add1 (tm->ip4_src_addresses, start[0]);
1456
1457 /* Add local adjacencies for the range */
1458
1459 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1460 NULL, &dpo);
1461 prefix.fp_len = 32;
1462 prefix.fp_proto = FIB_PROTOCOL_IP4;
1463 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1464
1465 fib_table_entry_special_dpo_update (fib_index,
1466 &prefix,
1467 FIB_SOURCE_API,
1468 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1469 dpo_reset (&dpo);
1470
1471 start_host_byte_order++;
1472 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1473 }
1474 while (start_host_byte_order <= end_host_byte_order);
1475
1476 return 0;
1477}
1478
1479/**
1480 * \brief Configure an ipv6 source address range
1481 * @param vm vlib_main_t pointer
1482 * @param start first ipv6 address in the source address range
1483 * @param end last ipv6 address in the source address range
1484 * @param table_id VRF / table ID, 0 for the default FIB
1485 * @return 0 if all OK, else an error indication from api_errno.h
1486 */
1487
1488int
1489tcp_configure_v6_source_address_range (vlib_main_t * vm,
1490 ip6_address_t * start,
1491 ip6_address_t * end, u32 table_id)
1492{
1493 tcp_main_t *tm = vnet_get_tcp_main ();
1494 fib_prefix_t prefix;
1495 u32 fib_index = 0;
1496 fib_node_index_t fei;
1497 u32 sw_if_index;
1498
1499 memset (&prefix, 0, sizeof (prefix));
1500
1501 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1502
1503 if (fib_index == ~0)
1504 return VNET_API_ERROR_NO_SUCH_FIB;
1505
1506 while (1)
1507 {
1508 int i;
1509 ip6_address_t tmp;
1510 dpo_id_t dpo = DPO_INVALID;
1511
1512 /* Remember this address */
1513 vec_add1 (tm->ip6_src_addresses, start[0]);
1514
1515 /* Lookup the prefix, to identify the interface involved */
1516 prefix.fp_len = 128;
1517 prefix.fp_proto = FIB_PROTOCOL_IP6;
1518 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1519
1520 fei = fib_table_lookup (fib_index, &prefix);
1521
1522 /* Couldn't find route to destination. Bail out. */
1523 if (fei == FIB_NODE_INDEX_INVALID)
1524 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1525
1526 sw_if_index = fib_entry_get_resolving_interface (fei);
1527
1528 if (sw_if_index == (u32) ~ 0)
1529 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1530
1531 /* Add a proxy neighbor discovery entry for this address */
1532 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1533
1534 /* Add a receive adjacency for this address */
1535 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1536 NULL, &dpo);
1537
1538 fib_table_entry_special_dpo_update (fib_index,
1539 &prefix,
1540 FIB_SOURCE_API,
1541 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1542 dpo_reset (&dpo);
1543
1544 /* Done with the entire range? */
1545 if (!memcmp (start, end, sizeof (start[0])))
1546 break;
1547
1548 /* Increment the address. DGMS. */
1549 tmp = start[0];
1550 for (i = 15; i >= 0; i--)
1551 {
1552 tmp.as_u8[i] += 1;
1553 if (tmp.as_u8[i] != 0)
1554 break;
1555 }
1556 start[0] = tmp;
1557 }
1558 return 0;
1559}
1560
Dave Barach2c25a622017-06-26 11:35:07 -04001561static clib_error_t *
1562tcp_src_address (vlib_main_t * vm,
1563 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1564{
Dave Barach2c25a622017-06-26 11:35:07 -04001565 ip4_address_t v4start, v4end;
1566 ip6_address_t v6start, v6end;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001567 u32 table_id = 0;
Dave Barach2c25a622017-06-26 11:35:07 -04001568 int v4set = 0;
1569 int v6set = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001570 int rv;
Dave Barach2c25a622017-06-26 11:35:07 -04001571
1572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1573 {
1574 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1575 unformat_ip4_address, &v4end))
1576 v4set = 1;
1577 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1578 {
1579 memcpy (&v4end, &v4start, sizeof (v4start));
1580 v4set = 1;
1581 }
1582 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
Dave Barach3bbcfab2017-08-15 19:03:44 -04001583 unformat_ip6_address, &v6end))
Dave Barach2c25a622017-06-26 11:35:07 -04001584 v6set = 1;
1585 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1586 {
1587 memcpy (&v6end, &v6start, sizeof (v4start));
1588 v6set = 1;
1589 }
Dave Barach3bbcfab2017-08-15 19:03:44 -04001590 else if (unformat (input, "fib-table %d", &table_id))
1591 ;
Dave Barach2c25a622017-06-26 11:35:07 -04001592 else
1593 break;
1594 }
1595
1596 if (!v4set && !v6set)
1597 return clib_error_return (0, "at least one v4 or v6 address required");
1598
1599 if (v4set)
1600 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001601 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1602 table_id);
1603 switch (rv)
Dave Barach2c25a622017-06-26 11:35:07 -04001604 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001605 case 0:
1606 break;
1607
1608 case VNET_API_ERROR_NO_SUCH_FIB:
1609 return clib_error_return (0, "Invalid table-id %d", table_id);
1610
1611 case VNET_API_ERROR_INVALID_ARGUMENT:
1612 return clib_error_return (0, "Invalid address range %U - %U",
1613 format_ip4_address, &v4start,
1614 format_ip4_address, &v4end);
1615 default:
1616 return clib_error_return (0, "error %d", rv);
1617 break;
Dave Barach2c25a622017-06-26 11:35:07 -04001618 }
Dave Barach2c25a622017-06-26 11:35:07 -04001619 }
1620 if (v6set)
1621 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001622 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1623 table_id);
1624 switch (rv)
1625 {
1626 case 0:
1627 break;
1628
1629 case VNET_API_ERROR_NO_SUCH_FIB:
1630 return clib_error_return (0, "Invalid table-id %d", table_id);
1631
1632 default:
1633 return clib_error_return (0, "error %d", rv);
1634 break;
1635 }
Dave Barach2c25a622017-06-26 11:35:07 -04001636 }
1637 return 0;
1638}
1639
1640/* *INDENT-OFF* */
1641VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1642{
1643 .path = "tcp src-address",
1644 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1645 .function = tcp_src_address,
1646};
1647/* *INDENT-ON* */
1648
Florin Coras3eb50622017-07-13 01:24:57 -04001649static u8 *
1650tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1651{
1652#if TCP_SCOREBOARD_TRACE
Dave Barach2c25a622017-06-26 11:35:07 -04001653
Florin Coras3eb50622017-07-13 01:24:57 -04001654 scoreboard_trace_elt_t *block;
1655 int i = 0;
1656
1657 if (!sb->trace)
1658 return s;
1659
1660 s = format (s, "scoreboard trace:");
1661 vec_foreach (block, sb->trace)
1662 {
1663 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1664 block->ack, block->snd_una_max, block->group);
1665 if ((++i % 3) == 0)
1666 s = format (s, "\n");
1667 }
1668 return s;
1669#else
1670 return 0;
1671#endif
1672}
1673
1674static clib_error_t *
1675tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1676 vlib_cli_command_t * cmd_arg)
1677{
1678 transport_connection_t *tconn = 0;
1679 tcp_connection_t *tc;
1680 u8 *s = 0;
1681 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1682 {
1683 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1684 TRANSPORT_PROTO_TCP))
1685 ;
1686 else
1687 return clib_error_return (0, "unknown input `%U'",
1688 format_unformat_error, input);
1689 }
1690
1691 if (!TCP_SCOREBOARD_TRACE)
1692 {
1693 vlib_cli_output (vm, "scoreboard tracing not enabled");
1694 return 0;
1695 }
1696
1697 tc = tcp_get_connection_from_transport (tconn);
1698 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1699 vlib_cli_output (vm, "%v", s);
1700 return 0;
1701}
1702
1703/* *INDENT-OFF* */
1704VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1705{
1706 .path = "show tcp scoreboard trace",
1707 .short_help = "show tcp scoreboard trace <connection>",
1708 .function = tcp_show_scoreboard_trace_fn,
1709};
1710/* *INDENT-ON* */
1711
1712u8 *
1713tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1714{
1715 int i, trace_len;
1716 scoreboard_trace_elt_t *trace;
1717 u32 next_ack, left, group, has_new_ack = 0;
1718 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1719 sack_block_t *block;
1720
1721 if (!tc)
1722 return s;
1723
1724 memset (dummy_tc, 0, sizeof (*dummy_tc));
1725 tcp_connection_timers_init (dummy_tc);
1726 scoreboard_init (&dummy_tc->sack_sb);
1727 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1728
1729#if TCP_SCOREBOARD_TRACE
1730 trace = tc->sack_sb.trace;
1731 trace_len = vec_len (tc->sack_sb.trace);
1732#else
1733 trace = 0;
1734 trace_len = 0;
1735#endif
1736
1737 for (i = 0; i < trace_len; i++)
1738 {
1739 if (trace[i].ack != 0)
1740 {
1741 dummy_tc->snd_una = trace[i].ack - 1448;
1742 dummy_tc->snd_una_max = trace[i].ack;
1743 }
1744 }
1745
1746 left = 0;
1747 while (left < trace_len)
1748 {
1749 group = trace[left].group;
1750 vec_reset_length (dummy_tc->rcv_opts.sacks);
1751 has_new_ack = 0;
1752 while (trace[left].group == group)
1753 {
1754 if (trace[left].ack != 0)
1755 {
1756 if (verbose)
1757 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1758 trace[left].ack, trace[left].snd_una_max);
1759 dummy_tc->snd_una_max = trace[left].snd_una_max;
1760 next_ack = trace[left].ack;
1761 has_new_ack = 1;
1762 }
1763 else
1764 {
1765 if (verbose)
1766 s = format (s, "[%u, %u], ", trace[left].start,
1767 trace[left].end);
1768 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1769 block->start = trace[left].start;
1770 block->end = trace[left].end;
1771 }
1772 left++;
1773 }
1774
1775 /* Push segments */
1776 tcp_rcv_sacks (dummy_tc, next_ack);
1777 if (has_new_ack)
1778 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1779
1780 if (verbose)
1781 s = format (s, "result: %U", format_tcp_scoreboard,
1782 &dummy_tc->sack_sb);
1783
1784 }
1785 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1786
1787 return s;
1788}
1789
1790static clib_error_t *
1791tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1792 vlib_cli_command_t * cmd_arg)
1793{
1794 transport_connection_t *tconn = 0;
1795 tcp_connection_t *tc = 0;
1796 u8 *str = 0;
1797 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1798 {
1799 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1800 TRANSPORT_PROTO_TCP))
1801 ;
1802 else
1803 return clib_error_return (0, "unknown input `%U'",
1804 format_unformat_error, input);
1805 }
1806
1807 if (!TCP_SCOREBOARD_TRACE)
1808 {
1809 vlib_cli_output (vm, "scoreboard tracing not enabled");
1810 return 0;
1811 }
1812
1813 tc = tcp_get_connection_from_transport (tconn);
1814 if (!tc)
1815 {
1816 vlib_cli_output (vm, "connection not found");
1817 return 0;
1818 }
1819 str = tcp_scoreboard_replay (str, tc, 1);
1820 vlib_cli_output (vm, "%v", str);
1821 return 0;
1822}
1823
1824/* *INDENT-OFF* */
1825VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1826{
1827 .path = "tcp replay scoreboard",
1828 .short_help = "tcp replay scoreboard <connection>",
1829 .function = tcp_scoreboard_trace_fn,
1830};
1831/* *INDENT-ON* */
Dave Barach2c25a622017-06-26 11:35:07 -04001832
Dave Barach68b0fb02017-02-28 15:15:56 -05001833/*
1834 * fd.io coding-style-patch-verification: ON
1835 *
1836 * Local Variables:
1837 * eval: (c-set-style "gnu")
1838 * End:
1839 */