blob: a0a5f190fa4042a282c415cc92fc79753e2e3aac [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;
Florin Corascea194d2017-10-02 00:18:51 -070036 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -050037
38 pool_get (tm->listener_pool, listener);
39 memset (listener, 0, sizeof (*listener));
40
41 listener->c_c_index = listener - tm->listener_pool;
Florin Coras0e495682017-09-19 22:27:18 -070042 listener->c_lcl_port = lcl->port;
Dave Barach68b0fb02017-02-28 15:15:56 -050043
Florin Corascea194d2017-10-02 00:18:51 -070044 /* If we are provided a sw_if_index, bind using one of its ips */
45 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
Florin Coras6cf30ad2017-04-04 23:08:23 -070046 {
Florin Corascea194d2017-10-02 00:18:51 -070047 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
48 lcl->is_ip4)))
49 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
Florin Coras6cf30ad2017-04-04 23:08:23 -070050 }
Florin Corascea194d2017-10-02 00:18:51 -070051 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
52 listener->c_is_ip4 = lcl->is_ip4;
Florin Coras3cbc04b2017-10-02 00:18:51 -070053 listener->c_proto = TRANSPORT_PROTO_TCP;
Dave Barach68b0fb02017-02-28 15:15:56 -050054 listener->c_s_index = session_index;
Florin Corascea194d2017-10-02 00:18:51 -070055 listener->c_fib_index = lcl->fib_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 -0700104/**
105 * Cleanup half-open connection
106 *
107 */
108void
109tcp_half_open_connection_del (tcp_connection_t * tc)
110{
111 tcp_main_t *tm = vnet_get_tcp_main ();
112 clib_spinlock_lock_if_init (&tm->half_open_lock);
113 pool_put_index (tm->half_open_connections, tc->c_c_index);
114 if (CLIB_DEBUG)
115 memset (tc, 0xFA, sizeof (*tc));
116 clib_spinlock_unlock_if_init (&tm->half_open_lock);
117}
118
119/**
120 * Try to cleanup half-open connection
121 *
122 * If called from a thread that doesn't own tc, the call won't have any
123 * effect.
124 *
125 * @param tc - connection to be cleaned up
126 * @return non-zero if cleanup failed.
127 */
128int
129tcp_half_open_connection_cleanup (tcp_connection_t * tc)
130{
131 /* Make sure this is the owning thread */
132 if (tc->c_thread_index != vlib_get_thread_index ())
133 return 1;
134 tcp_timer_reset (tc, TCP_TIMER_ESTABLISH);
135 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT_SYN);
136 tcp_half_open_connection_del (tc);
137 return 0;
138}
139
140tcp_connection_t *
141tcp_half_open_connection_new (void)
142{
143 tcp_main_t *tm = vnet_get_tcp_main ();
144 tcp_connection_t *tc = 0;
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400145 ASSERT (vlib_get_thread_index () == 0);
Florin Coras68810622017-07-24 17:40:28 -0700146 pool_get (tm->half_open_connections, tc);
147 memset (tc, 0, sizeof (*tc));
148 tc->c_c_index = tc - tm->half_open_connections;
149 return tc;
150}
151
Dave Barach68b0fb02017-02-28 15:15:56 -0500152/**
153 * Cleans up connection state.
154 *
155 * No notifications.
156 */
157void
158tcp_connection_cleanup (tcp_connection_t * tc)
159{
160 tcp_main_t *tm = &tcp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500161
162 /* Cleanup local endpoint if this was an active connect */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700163 transport_endpoint_cleanup (TRANSPORT_PROTO_TCP, &tc->c_lcl_ip,
164 tc->c_lcl_port);
Dave Barach68b0fb02017-02-28 15:15:56 -0500165
Florin Coras68810622017-07-24 17:40:28 -0700166 /* Check if connection is not yet fully established */
Dave Barach68b0fb02017-02-28 15:15:56 -0500167 if (tc->state == TCP_STATE_SYN_SENT)
Dave Barach2c25a622017-06-26 11:35:07 -0400168 {
Florin Coras68810622017-07-24 17:40:28 -0700169 /* Try to remove the half-open connection. If this is not the owning
170 * thread, tc won't be removed. Retransmit or establish timers will
171 * eventually expire and call again cleanup on the right thread. */
172 tcp_half_open_connection_cleanup (tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400173 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500174 else
Dave Barach2c25a622017-06-26 11:35:07 -0400175 {
176 int thread_index = tc->c_thread_index;
Florin Coras68810622017-07-24 17:40:28 -0700177
178 /* Make sure all timers are cleared */
179 tcp_connection_timers_reset (tc);
180
Dave Barach2c25a622017-06-26 11:35:07 -0400181 /* Poison the entry */
182 if (CLIB_DEBUG > 0)
183 memset (tc, 0xFA, sizeof (*tc));
184 pool_put (tm->connections[thread_index], tc);
185 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500186}
187
188/**
189 * Connection removal.
190 *
191 * This should be called only once connection enters CLOSED state. Note
192 * that it notifies the session of the removal event, so if the goal is to
193 * just remove the connection, call tcp_connection_cleanup instead.
194 */
195void
196tcp_connection_del (tcp_connection_t * tc)
197{
Florin Corase69f4952017-03-07 10:06:24 -0800198 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500199 stream_session_delete_notify (&tc->connection);
200 tcp_connection_cleanup (tc);
201}
202
Florin Coras6534b7a2017-07-18 05:38:03 -0400203tcp_connection_t *
204tcp_connection_new (u8 thread_index)
205{
206 tcp_main_t *tm = vnet_get_tcp_main ();
207 tcp_connection_t *tc;
208
209 pool_get (tm->connections[thread_index], tc);
210 memset (tc, 0, sizeof (*tc));
211 tc->c_c_index = tc - tm->connections[thread_index];
212 tc->c_thread_index = thread_index;
213 return tc;
214}
215
Florin Corasd79b41e2017-03-04 05:37:52 -0800216/** Notify session that connection has been reset.
217 *
218 * Switch state to closed and wait for session to call cleanup.
219 */
220void
221tcp_connection_reset (tcp_connection_t * tc)
222{
Florin Coras6534b7a2017-07-18 05:38:03 -0400223 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
Florin Coras11c05492017-05-10 12:29:14 -0700224 switch (tc->state)
225 {
226 case TCP_STATE_SYN_RCVD:
227 /* Cleanup everything. App wasn't notified yet */
228 stream_session_delete_notify (&tc->connection);
229 tcp_connection_cleanup (tc);
230 break;
231 case TCP_STATE_SYN_SENT:
Florin Coras3cbc04b2017-10-02 00:18:51 -0700232 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
Florin Coras6534b7a2017-07-18 05:38:03 -0400233 tcp_connection_cleanup (tc);
234 break;
Florin Coras11c05492017-05-10 12:29:14 -0700235 case TCP_STATE_ESTABLISHED:
236 case TCP_STATE_CLOSE_WAIT:
237 case TCP_STATE_FIN_WAIT_1:
238 case TCP_STATE_FIN_WAIT_2:
239 case TCP_STATE_CLOSING:
240 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400241 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800242
Florin Coras11c05492017-05-10 12:29:14 -0700243 /* Make sure all timers are cleared */
244 tcp_connection_timers_reset (tc);
Florin Coras11c05492017-05-10 12:29:14 -0700245 stream_session_reset_notify (&tc->connection);
Dave Barach2c25a622017-06-26 11:35:07 -0400246
247 /* Wait for cleanup from session layer but not forever */
Florin Coras68810622017-07-24 17:40:28 -0700248 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Florin Coras11c05492017-05-10 12:29:14 -0700249 break;
250 case TCP_STATE_CLOSED:
251 return;
252 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800253}
254
Dave Barach68b0fb02017-02-28 15:15:56 -0500255/**
256 * Begin connection closing procedure.
257 *
258 * If at the end the connection is not in CLOSED state, it is not removed.
259 * Instead, we rely on on TCP to advance through state machine to either
260 * 1) LAST_ACK (passive close) whereby when the last ACK is received
261 * tcp_connection_del is called. This notifies session of the delete and
262 * calls cleanup.
263 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
264 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800265 *
266 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500267 */
268void
269tcp_connection_close (tcp_connection_t * tc)
270{
Florin Corase69f4952017-03-07 10:06:24 -0800271 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
272
Florin Corasb2215d62017-08-01 16:56:58 -0700273 /* Send/Program FIN if needed and switch state */
274 switch (tc->state)
275 {
276 case TCP_STATE_SYN_SENT:
277 tc->state = TCP_STATE_CLOSED;
278 break;
279 case TCP_STATE_SYN_RCVD:
280 tcp_send_fin (tc);
281 tc->state = TCP_STATE_FIN_WAIT_1;
282 break;
283 case TCP_STATE_ESTABLISHED:
284 if (!stream_session_tx_fifo_max_dequeue (&tc->connection))
285 tcp_send_fin (tc);
286 else
287 tc->flags |= TCP_CONN_FINPNDG;
288 tc->state = TCP_STATE_FIN_WAIT_1;
289 break;
290 case TCP_STATE_CLOSE_WAIT:
Florin Corasa096f2d2017-09-28 23:49:42 -0400291 tcp_connection_timers_reset (tc);
Florin Corasb2215d62017-08-01 16:56:58 -0700292 tcp_send_fin (tc);
293 tc->state = TCP_STATE_LAST_ACK;
Florin Corasa096f2d2017-09-28 23:49:42 -0400294 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Florin Corasb2215d62017-08-01 16:56:58 -0700295 break;
Florin Corasc87c91d2017-08-16 19:55:49 -0700296 case TCP_STATE_FIN_WAIT_1:
Florin Corasa096f2d2017-09-28 23:49:42 -0400297 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Florin Corasc87c91d2017-08-16 19:55:49 -0700298 break;
Florin Corasb2215d62017-08-01 16:56:58 -0700299 default:
Florin Corasa096f2d2017-09-28 23:49:42 -0400300 TCP_DBG ("state: %u", tc->state);
Florin Corasb2215d62017-08-01 16:56:58 -0700301 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500302
Florin Coras6534b7a2017-07-18 05:38:03 -0400303 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500304
Florin Corasd79b41e2017-03-04 05:37:52 -0800305 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
Florin Corasa096f2d2017-09-28 23:49:42 -0400306 if (!tcp_timer_is_active (tc, TCP_TIMER_WAITCLOSE)
Florin Corasd79b41e2017-03-04 05:37:52 -0800307 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500308 tcp_connection_del (tc);
309}
310
311void
312tcp_session_close (u32 conn_index, u32 thread_index)
313{
314 tcp_connection_t *tc;
315 tc = tcp_connection_get (conn_index, thread_index);
316 tcp_connection_close (tc);
317}
318
319void
320tcp_session_cleanup (u32 conn_index, u32 thread_index)
321{
322 tcp_connection_t *tc;
323 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasa096f2d2017-09-28 23:49:42 -0400324 tcp_connection_timers_reset (tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800325
326 /* Wait for the session tx events to clear */
327 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400328 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800329 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500330}
331
Dave Barach68b0fb02017-02-28 15:15:56 -0500332/**
333 * Initialize all connection timers as invalid
334 */
335void
336tcp_connection_timers_init (tcp_connection_t * tc)
337{
338 int i;
339
340 /* Set all to invalid */
341 for (i = 0; i < TCP_N_TIMERS; i++)
342 {
343 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
344 }
345
346 tc->rto = TCP_RTO_INIT;
347}
348
349/**
350 * Stop all connection timers
351 */
352void
353tcp_connection_timers_reset (tcp_connection_t * tc)
354{
355 int i;
356 for (i = 0; i < TCP_N_TIMERS; i++)
357 {
358 tcp_timer_reset (tc, i);
359 }
360}
361
Dave Barach2c25a622017-06-26 11:35:07 -0400362#if 0
Florin Corasf6359c82017-06-19 12:26:09 -0400363typedef struct ip4_tcp_hdr
364{
365 ip4_header_t ip;
366 tcp_header_t tcp;
367} ip4_tcp_hdr_t;
368
369typedef struct ip6_tcp_hdr
370{
371 ip6_header_t ip;
372 tcp_header_t tcp;
373} ip6_tcp_hdr_t;
374
375static void
376tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
377 dpo_id_t * result)
378{
379 const dpo_id_t *choice;
380 load_balance_t *lb;
381 int hash;
382
383 lb = load_balance_get (dpo->dpoi_index);
384 if (tc->c_is_ip4)
385 {
386 ip4_tcp_hdr_t hdr;
387 memset (&hdr, 0, sizeof (hdr));
388 hdr.ip.protocol = IP_PROTOCOL_TCP;
389 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
390 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
391 hdr.tcp.src_port = tc->c_lcl_port;
392 hdr.tcp.dst_port = tc->c_rmt_port;
393 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
394 }
395 else
396 {
397 ip6_tcp_hdr_t hdr;
398 memset (&hdr, 0, sizeof (hdr));
399 hdr.ip.protocol = IP_PROTOCOL_TCP;
400 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
401 sizeof (ip6_address_t));
402 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
403 sizeof (ip6_address_t));
404 hdr.tcp.src_port = tc->c_lcl_port;
405 hdr.tcp.dst_port = tc->c_rmt_port;
406 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
407 }
408 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
409 dpo_copy (result, choice);
410}
411
412fib_node_index_t
413tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
414{
415 fib_prefix_t prefix;
Florin Coras04e53442017-07-16 17:12:15 -0700416 u32 fib_index;
Florin Corasf6359c82017-06-19 12:26:09 -0400417
418 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
419 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
420 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
Florin Corascea194d2017-10-02 00:18:51 -0700421 fib_index = fib_table_find (prefix.fp_proto, tc->c_fib_index);
Florin Coras04e53442017-07-16 17:12:15 -0700422 return fib_table_lookup (fib_index, &prefix);
Florin Corasf6359c82017-06-19 12:26:09 -0400423}
424
425static int
426tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
427{
428 dpo_id_t choice = DPO_INVALID;
429 u32 output_node_index;
430 fib_entry_t *fe;
431
432 fe = fib_entry_get (tc->c_rmt_fei);
433 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
434 return -1;
435
436 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
437
438 output_node_index =
439 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
440 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
441 return 0;
442}
443
444/** Stack tcp connection on peer's fib entry.
445 *
446 * This ultimately populates the dpo the connection will use to send packets.
447 */
448static void
449tcp_connection_fib_attach (tcp_connection_t * tc)
450{
451 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
452
453 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
454
455 tcp_connection_stack_on_fib_entry (tc);
456}
Dave Barach2c25a622017-06-26 11:35:07 -0400457#endif /* 0 */
Florin Corasf6359c82017-06-19 12:26:09 -0400458
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400459/**
460 * Initialize connection send variables.
461 */
462void
463tcp_init_snd_vars (tcp_connection_t * tc)
464{
465 u32 time_now;
466
Florin Coras3cbc04b2017-10-02 00:18:51 -0700467 /*
468 * We use the time to randomize iss and for setting up the initial
469 * timestamp. Make sure it's updated otherwise syn and ack in the
470 * handshake may make it look as if time has flown in the opposite
471 * direction for us.
472 */
473 tcp_set_time_now (vlib_get_thread_index ());
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400474 time_now = tcp_time_now ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700475
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400476 tc->iss = random_u32 (&time_now);
477 tc->snd_una = tc->iss;
478 tc->snd_nxt = tc->iss + 1;
479 tc->snd_una_max = tc->snd_nxt;
480}
481
Dave Barach68b0fb02017-02-28 15:15:56 -0500482/** Initialize tcp connection variables
483 *
484 * Should be called after having received a msg from the peer, i.e., a SYN or
485 * a SYNACK, such that connection options have already been exchanged. */
486void
487tcp_connection_init_vars (tcp_connection_t * tc)
488{
489 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700490 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700491 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500492 tcp_cc_init (tc);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400493 if (tc->state == TCP_STATE_SYN_RCVD)
494 tcp_init_snd_vars (tc);
495
Dave Barach2c25a622017-06-26 11:35:07 -0400496 // tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500497}
498
Florin Coras3cbc04b2017-10-02 00:18:51 -0700499static int
500tcp_alloc_custom_local_endpoint (tcp_main_t * tm, ip46_address_t * lcl_addr,
501 u16 * lcl_port, u8 is_ip4)
502{
503 int index, port;
504 if (is_ip4)
505 {
506 index = tm->last_v4_address_rotor++;
507 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
508 tm->last_v4_address_rotor = 0;
509 lcl_addr->ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
510 }
511 else
512 {
513 index = tm->last_v6_address_rotor++;
514 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
515 tm->last_v6_address_rotor = 0;
516 clib_memcpy (&lcl_addr->ip6, &tm->ip6_src_addresses[index],
517 sizeof (ip6_address_t));
518 }
519 port = transport_alloc_local_port (TRANSPORT_PROTO_TCP, lcl_addr);
520 if (port < 1)
521 {
522 clib_warning ("Failed to allocate src port");
523 return -1;
524 }
525 *lcl_port = port;
526 return 0;
527}
528
Dave Barach68b0fb02017-02-28 15:15:56 -0500529int
Florin Coras04e53442017-07-16 17:12:15 -0700530tcp_connection_open (transport_endpoint_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500531{
532 tcp_main_t *tm = vnet_get_tcp_main ();
533 tcp_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -0500534 ip46_address_t lcl_addr;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700535 u16 lcl_port;
536 int rv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500537
538 /*
Florin Coras3cbc04b2017-10-02 00:18:51 -0700539 * Allocate local endpoint
Dave Barach68b0fb02017-02-28 15:15:56 -0500540 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700541 if ((rmt->is_ip4 && vec_len (tm->ip4_src_addresses))
542 || (!rmt->is_ip4 && vec_len (tm->ip6_src_addresses)))
543 rv = tcp_alloc_custom_local_endpoint (tm, &lcl_addr, &lcl_port,
544 rmt->is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 else
Florin Coras3cbc04b2017-10-02 00:18:51 -0700546 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_TCP,
547 rmt, &lcl_addr, &lcl_port);
Dave Barach2c25a622017-06-26 11:35:07 -0400548
Florin Coras3cbc04b2017-10-02 00:18:51 -0700549 if (rv)
550 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500551
552 /*
553 * Create connection and send SYN
554 */
Florin Coras68810622017-07-24 17:40:28 -0700555 clib_spinlock_lock_if_init (&tm->half_open_lock);
Florin Coras04e53442017-07-16 17:12:15 -0700556 tc = tcp_half_open_connection_new ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700557 ip_copy (&tc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
558 ip_copy (&tc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
Florin Coras0e495682017-09-19 22:27:18 -0700559 tc->c_rmt_port = rmt->port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500560 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
Florin Coras04e53442017-07-16 17:12:15 -0700561 tc->c_is_ip4 = rmt->is_ip4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700562 tc->c_proto = TRANSPORT_PROTO_TCP;
Florin Corascea194d2017-10-02 00:18:51 -0700563 tc->c_fib_index = rmt->fib_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 /* The other connection vars will be initialized after SYN ACK */
565 tcp_connection_timers_init (tc);
566
Florin Corase69f4952017-03-07 10:06:24 -0800567 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400568 tc->state = TCP_STATE_SYN_SENT;
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400569 tcp_init_snd_vars (tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400570 tcp_send_syn (tc);
Florin Coras68810622017-07-24 17:40:28 -0700571 clib_spinlock_unlock_if_init (&tm->half_open_lock);
Florin Corase69f4952017-03-07 10:06:24 -0800572
Dave Barach68b0fb02017-02-28 15:15:56 -0500573 return tc->c_c_index;
574}
575
576int
Florin Coras04e53442017-07-16 17:12:15 -0700577tcp_session_open (transport_endpoint_t * tep)
Dave Barach68b0fb02017-02-28 15:15:56 -0500578{
Florin Coras04e53442017-07-16 17:12:15 -0700579 return tcp_connection_open (tep);
Dave Barach68b0fb02017-02-28 15:15:56 -0500580}
581
Florin Corase69f4952017-03-07 10:06:24 -0800582const char *tcp_dbg_evt_str[] = {
583#define _(sym, str) str,
584 foreach_tcp_dbg_evt
585#undef _
586};
587
588const char *tcp_fsm_states[] = {
589#define _(sym, str) str,
590 foreach_tcp_fsm_state
591#undef _
592};
593
Dave Barach68b0fb02017-02-28 15:15:56 -0500594u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800595format_tcp_state (u8 * s, va_list * args)
596{
Florin Corasbb292f42017-05-19 09:49:19 -0700597 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800598
Florin Corasbb292f42017-05-19 09:49:19 -0700599 if (state < TCP_N_STATES)
600 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800601 else
Florin Corasbb292f42017-05-19 09:49:19 -0700602 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800603 return s;
604}
605
Florin Corasa096f2d2017-09-28 23:49:42 -0400606const char *tcp_connection_flags_str[] = {
607#define _(sym, str) str,
608 foreach_tcp_connection_flag
609#undef _
610};
611
612u8 *
613format_tcp_connection_flags (u8 * s, va_list * args)
614{
615 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
616 int i, last = -1;
617
618 for (i = 0; i < TCP_CONN_N_FLAG_BITS; i++)
619 if (tc->flags & (1 << i))
620 last = i;
621 for (i = 0; i < last; i++)
622 {
623 if (tc->flags & (1 << i))
624 s = format (s, "%s, ", tcp_connection_flags_str[i]);
625 }
626 if (last >= 0)
627 s = format (s, "%s", tcp_connection_flags_str[last]);
628 return s;
629}
630
Florin Corase69f4952017-03-07 10:06:24 -0800631const char *tcp_conn_timers[] = {
632#define _(sym, str) str,
633 foreach_tcp_timer
634#undef _
635};
636
637u8 *
638format_tcp_timers (u8 * s, va_list * args)
639{
640 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700641 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800642
643 for (i = 0; i < TCP_N_TIMERS; i++)
644 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
645 last = i;
646
647 s = format (s, "[");
648 for (i = 0; i < last; i++)
649 {
650 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
651 s = format (s, "%s,", tcp_conn_timers[i]);
652 }
653
Florin Coras93992a92017-05-24 18:03:56 -0700654 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800655 s = format (s, "%s]", tcp_conn_timers[i]);
656 else
657 s = format (s, "]");
658
659 return s;
660}
661
662u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700663format_tcp_congestion_status (u8 * s, va_list * args)
664{
665 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
666 if (tcp_in_recovery (tc))
667 s = format (s, "recovery");
668 else if (tcp_in_fastrecovery (tc))
669 s = format (s, "fastrecovery");
670 else
671 s = format (s, "none");
672 return s;
673}
674
675u8 *
676format_tcp_vars (u8 * s, va_list * args)
677{
678 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa096f2d2017-09-28 23:49:42 -0400679 s = format (s, " flags: %U timers: %U\n", format_tcp_connection_flags, tc,
680 format_tcp_timers, tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400681 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
Florin Corasbb292f42017-05-19 09:49:19 -0700682 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
683 tc->snd_una_max - tc->iss);
684 s = format (s, " rcv_nxt %u rcv_las %u\n",
685 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
686 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
687 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
688 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700689 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400690 tcp_flight_size (tc), tcp_available_output_snd_space (tc),
Florin Coras93992a92017-05-24 18:03:56 -0700691 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700692 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
693 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700694 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
Dave Barach2c25a622017-06-26 11:35:07 -0400695 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
Florin Coras93992a92017-05-24 18:03:56 -0700696 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
697 tc->rcv_dupacks);
Dave Barach2c25a622017-06-26 11:35:07 -0400698 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
699 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
700 tc->tsecr_last_ack);
Florin Corasbb292f42017-05-19 09:49:19 -0700701 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
702 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
703 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Dave Barach2c25a622017-06-26 11:35:07 -0400704 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
705 tcp_time_now () - tc->tsval_recent_age);
Florin Corasde706082017-10-11 01:43:15 -0700706 if (tc->state >= TCP_STATE_ESTABLISHED)
707 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
708 tc);
Florin Corasbb292f42017-05-19 09:49:19 -0700709 if (vec_len (tc->snd_sacks))
710 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
711
712 return s;
713}
714
715u8 *
716format_tcp_connection_id (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800717{
718 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasa5464812017-04-19 13:00:05 -0700719 if (!tc)
720 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800721 if (tc->c_is_ip4)
722 {
723 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
724 format_ip4_address, &tc->c_lcl_ip4,
725 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
726 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
727 }
728 else
729 {
730 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
731 format_ip6_address, &tc->c_lcl_ip6,
732 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
733 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
734 }
735
736 return s;
737}
738
739u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700740format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800741{
742 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700743 u32 verbose = va_arg (*args, u32);
744
Florin Corasc87c91d2017-08-16 19:55:49 -0700745 if (!tc)
746 return s;
Florin Corasbb292f42017-05-19 09:49:19 -0700747 s = format (s, "%-50U", format_tcp_connection_id, tc);
748 if (verbose)
749 {
750 s = format (s, "%-15U", format_tcp_state, tc->state);
751 if (verbose > 1)
Florin Corasa096f2d2017-09-28 23:49:42 -0400752 s = format (s, "\n%U", format_tcp_vars, tc);
Florin Corasbb292f42017-05-19 09:49:19 -0700753 }
Florin Coras3eb50622017-07-13 01:24:57 -0400754
Florin Corase69f4952017-03-07 10:06:24 -0800755 return s;
756}
757
758u8 *
759format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500760{
761 u32 tci = va_arg (*args, u32);
762 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700763 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500764 tcp_connection_t *tc;
765
766 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700767 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700768 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700769 else
Florin Coras1f152cd2017-08-18 19:28:03 -0700770 s = format (s, "empty\n");
Florin Coras93992a92017-05-24 18:03:56 -0700771 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500772}
773
774u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800775format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500776{
777 u32 tci = va_arg (*args, u32);
778 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700779 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500780}
781
782u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800783format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500784{
785 u32 tci = va_arg (*args, u32);
786 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700787 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500788}
789
Florin Coras06d11012017-05-17 14:21:51 -0700790u8 *
791format_tcp_sacks (u8 * s, va_list * args)
792{
793 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
794 sack_block_t *sacks = tc->snd_sacks;
795 sack_block_t *block;
Dave Barach2c25a622017-06-26 11:35:07 -0400796 int i, len = 0;
797
798 len = vec_len (sacks);
799 for (i = 0; i < len - 1; i++)
800 {
801 block = &sacks[i];
802 s = format (s, " start %u end %u\n", block->start - tc->irs,
803 block->end - tc->irs);
804 }
805 if (len)
806 {
807 block = &sacks[len - 1];
808 s = format (s, " start %u end %u", block->start - tc->irs,
809 block->end - tc->irs);
810 }
Florin Coras06d11012017-05-17 14:21:51 -0700811 return s;
812}
813
814u8 *
Florin Coras3eb50622017-07-13 01:24:57 -0400815format_tcp_rcv_sacks (u8 * s, va_list * args)
816{
817 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
818 sack_block_t *sacks = tc->rcv_opts.sacks;
819 sack_block_t *block;
820 int i, len = 0;
821
822 len = vec_len (sacks);
823 for (i = 0; i < len - 1; i++)
824 {
825 block = &sacks[i];
826 s = format (s, " start %u end %u\n", block->start - tc->iss,
827 block->end - tc->iss);
828 }
829 if (len)
830 {
831 block = &sacks[len - 1];
832 s = format (s, " start %u end %u", block->start - tc->iss,
833 block->end - tc->iss);
834 }
835 return s;
836}
837
838u8 *
Florin Coras06d11012017-05-17 14:21:51 -0700839format_tcp_sack_hole (u8 * s, va_list * args)
840{
841 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -0700842 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
843 if (tc)
844 s = format (s, " [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
845 else
846 s = format (s, " [%u, %u]", hole->start, hole->end);
Florin Coras06d11012017-05-17 14:21:51 -0700847 return s;
848}
849
850u8 *
851format_tcp_scoreboard (u8 * s, va_list * args)
852{
853 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
Florin Coras1f152cd2017-08-18 19:28:03 -0700854 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras06d11012017-05-17 14:21:51 -0700855 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -0700856 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
857 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
858 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
859 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
860 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
861 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
862
Florin Coras06d11012017-05-17 14:21:51 -0700863 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -0700864 if (hole)
865 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
866
Florin Coras06d11012017-05-17 14:21:51 -0700867 while (hole)
868 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700869 s = format (s, "%U", format_tcp_sack_hole, hole, tc);
Florin Coras06d11012017-05-17 14:21:51 -0700870 hole = scoreboard_next_hole (sb, hole);
871 }
Florin Coras3eb50622017-07-13 01:24:57 -0400872
Florin Coras06d11012017-05-17 14:21:51 -0700873 return s;
874}
875
Dave Barach68b0fb02017-02-28 15:15:56 -0500876transport_connection_t *
877tcp_session_get_transport (u32 conn_index, u32 thread_index)
878{
879 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
880 return &tc->connection;
881}
882
883transport_connection_t *
884tcp_half_open_session_get_transport (u32 conn_index)
885{
886 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
887 return &tc->connection;
888}
889
Florin Corasc8343412017-05-04 14:25:50 -0700890/**
891 * Compute maximum segment size for session layer.
892 *
893 * Since the result needs to be the actual data length, it first computes
894 * the tcp options to be used in the next burst and subtracts their
895 * length from the connection's snd_mss.
896 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500897u16
898tcp_session_send_mss (transport_connection_t * trans_conn)
899{
900 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -0700901
902 /* Ensure snd_mss does accurately reflect the amount of data we can push
903 * in a segment. This also makes sure that options are updated according to
904 * the current state of the connection. */
905 tcp_update_snd_mss (tc);
906
Dave Barach68b0fb02017-02-28 15:15:56 -0500907 return tc->snd_mss;
908}
909
Florin Coras3af90fc2017-05-03 21:09:42 -0700910always_inline u32
911tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
912{
Dave Barach2c25a622017-06-26 11:35:07 -0400913 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Coras3af90fc2017-05-03 21:09:42 -0700914 {
Florin Corasdb84e572017-05-09 18:54:52 -0700915 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700916 }
917
Florin Coras1f152cd2017-08-18 19:28:03 -0700918 /* If not snd_wnd constrained and we can't write at least a segment,
919 * don't try at all */
Dave Barach2c25a622017-06-26 11:35:07 -0400920 if (PREDICT_FALSE (snd_space < tc->snd_mss))
Florin Coras9d063042017-09-14 03:08:00 -0400921 return snd_space < tc->cwnd ? 0 : snd_space;
Florin Coras3af90fc2017-05-03 21:09:42 -0700922
923 /* round down to mss multiple */
924 return snd_space - (snd_space % tc->snd_mss);
925}
926
Florin Coras6792ec02017-03-13 03:49:51 -0700927/**
928 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -0700929 *
930 * Takes into account available send space, snd_mss and the congestion
931 * state of the connection. If possible, the value returned is a multiple
932 * of snd_mss.
933 *
934 * @param tc tcp connection
935 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -0700936 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500937u32
Florin Corasbb292f42017-05-19 09:49:19 -0700938tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500939{
Florin Corasf03a59a2017-06-09 21:07:32 -0700940 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -0700941
Florin Corasf03a59a2017-06-09 21:07:32 -0700942 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -0700943 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700944 snd_space = tcp_available_output_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -0700945
946 /* If we haven't gotten dupacks or if we did and have gotten sacked
947 * bytes then we can still send as per Limited Transmit (RFC3042) */
948 if (PREDICT_FALSE (tc->rcv_dupacks != 0
949 && (tcp_opts_sack_permitted (tc)
950 && tc->sack_sb.last_sacked_bytes == 0)))
951 {
952 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
953 tc->limited_transmit = tc->snd_nxt;
954 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
955
956 snt_limited = tc->snd_nxt - tc->limited_transmit;
957 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
958 }
Florin Coras3af90fc2017-05-03 21:09:42 -0700959 return tcp_round_snd_space (tc, snd_space);
960 }
Florin Coras6792ec02017-03-13 03:49:51 -0700961
Florin Coras3af90fc2017-05-03 21:09:42 -0700962 if (tcp_in_recovery (tc))
963 {
964 tc->snd_nxt = tc->snd_una_max;
Florin Coras1f152cd2017-08-18 19:28:03 -0700965 snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -0700966 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -0700967 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -0700968 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700969 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -0700970 }
971
Florin Coras1f152cd2017-08-18 19:28:03 -0700972 /* RFC 5681: When previously unsent data is available and the new value of
973 * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
974 * bytes of previously unsent data. */
975 if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700976 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700977 if (tcp_available_output_snd_space (tc) < tc->snd_mss)
978 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700979 tcp_fastrecovery_1_smss_on (tc);
980 return tc->snd_mss;
981 }
982
983 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500984}
985
986u32
Florin Corasbb292f42017-05-19 09:49:19 -0700987tcp_session_send_space (transport_connection_t * trans_conn)
988{
989 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras1f152cd2017-08-18 19:28:03 -0700990 return clib_min (tcp_snd_space (tc),
991 tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
Florin Corasbb292f42017-05-19 09:49:19 -0700992}
993
Florin Coras93992a92017-05-24 18:03:56 -0700994i32
995tcp_rcv_wnd_available (tcp_connection_t * tc)
996{
997 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
998}
999
Florin Corasbb292f42017-05-19 09:49:19 -07001000u32
Florin Corasd79b41e2017-03-04 05:37:52 -08001001tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -05001002{
1003 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -07001004
1005 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1006
1007 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -08001008 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -05001009}
1010
1011/* *INDENT-OFF* */
Florin Coras04e53442017-07-16 17:12:15 -07001012const static transport_proto_vft_t tcp_proto = {
1013 .bind = tcp_session_bind,
Florin Corase69f4952017-03-07 10:06:24 -08001014 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001015 .push_header = tcp_push_header,
1016 .get_connection = tcp_session_get_transport,
1017 .get_listener = tcp_session_get_listener,
1018 .get_half_open = tcp_half_open_session_get_transport,
Florin Coras04e53442017-07-16 17:12:15 -07001019 .open = tcp_session_open,
Dave Barach68b0fb02017-02-28 15:15:56 -05001020 .close = tcp_session_close,
1021 .cleanup = tcp_session_cleanup,
1022 .send_mss = tcp_session_send_mss,
1023 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001024 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001025 .format_connection = format_tcp_session,
1026 .format_listener = format_tcp_listener_session,
1027 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001028};
1029/* *INDENT-ON* */
1030
1031void
1032tcp_timer_keep_handler (u32 conn_index)
1033{
Damjan Marion586afd72017-04-05 19:18:20 +02001034 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001035 tcp_connection_t *tc;
1036
Damjan Marion586afd72017-04-05 19:18:20 +02001037 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001038 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1039
1040 tcp_connection_close (tc);
1041}
1042
1043void
1044tcp_timer_establish_handler (u32 conn_index)
1045{
1046 tcp_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -05001047
1048 tc = tcp_half_open_connection_get (conn_index);
Florin Corasab0289a2017-08-14 11:25:25 -07001049 if (tc)
1050 {
1051 ASSERT (tc->state == TCP_STATE_SYN_SENT);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001052 session_stream_connect_notify (&tc->connection, 1 /* fail */ );
Florin Coras9d063042017-09-14 03:08:00 -04001053 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
Florin Corasab0289a2017-08-14 11:25:25 -07001054 }
1055 else
1056 {
1057 tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
Dave Barachb7f1faa2017-08-29 11:43:37 -04001058 /* note: the connection may have already disappeared */
1059 if (PREDICT_FALSE (tc == 0))
1060 return;
Florin Coras9d063042017-09-14 03:08:00 -04001061 TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
Florin Corasab0289a2017-08-14 11:25:25 -07001062 ASSERT (tc->state == TCP_STATE_SYN_RCVD);
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001063 /* Start cleanup. App wasn't notified yet so use delete notify as
1064 * opposed to delete to cleanup session layer state. */
1065 stream_session_delete_notify (&tc->connection);
Florin Corasab0289a2017-08-14 11:25:25 -07001066 }
rootc9d1c5b2017-08-15 12:58:31 -04001067 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
Dave Barach68b0fb02017-02-28 15:15:56 -05001068 tcp_connection_cleanup (tc);
1069}
1070
1071void
Florin Corasd79b41e2017-03-04 05:37:52 -08001072tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001073{
Damjan Marion586afd72017-04-05 19:18:20 +02001074 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001075 tcp_connection_t *tc;
1076
Damjan Marion586afd72017-04-05 19:18:20 +02001077 tc = tcp_connection_get (conn_index, thread_index);
Florin Coras68810622017-07-24 17:40:28 -07001078 if (!tc)
1079 return;
Florin Corasd79b41e2017-03-04 05:37:52 -08001080 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1081
1082 /* Session didn't come back with a close(). Send FIN either way
1083 * and switch to LAST_ACK. */
1084 if (tc->state == TCP_STATE_CLOSE_WAIT)
1085 {
1086 if (tc->flags & TCP_CONN_FINSNT)
1087 {
1088 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1089 }
1090
1091 tcp_send_fin (tc);
1092 tc->state = TCP_STATE_LAST_ACK;
1093
1094 /* Make sure we don't wait in LAST ACK forever */
1095 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1096
1097 /* Don't delete the connection yet */
1098 return;
1099 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001100
1101 tcp_connection_del (tc);
1102}
1103
1104/* *INDENT-OFF* */
1105static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1106{
1107 tcp_timer_retransmit_handler,
1108 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -07001109 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001110 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -08001111 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001112 tcp_timer_retransmit_syn_handler,
1113 tcp_timer_establish_handler
1114};
1115/* *INDENT-ON* */
1116
1117static void
1118tcp_expired_timers_dispatch (u32 * expired_timers)
1119{
1120 int i;
1121 u32 connection_index, timer_id;
1122
1123 for (i = 0; i < vec_len (expired_timers); i++)
1124 {
1125 /* Get session index and timer id */
1126 connection_index = expired_timers[i] & 0x0FFFFFFF;
1127 timer_id = expired_timers[i] >> 28;
1128
Florin Corase69f4952017-03-07 10:06:24 -08001129 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1130
Dave Barach68b0fb02017-02-28 15:15:56 -05001131 /* Handle expiration */
1132 (*timer_expiration_handlers[timer_id]) (connection_index);
1133 }
1134}
1135
1136void
1137tcp_initialize_timer_wheels (tcp_main_t * tm)
1138{
1139 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001140 /* *INDENT-OFF* */
1141 foreach_vlib_main (({
1142 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001143 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1144 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001145 tw->last_run_time = vlib_time_now (this_vlib_main);
1146 }));
1147 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001148}
1149
1150clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001151tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001152{
Dave Barach68b0fb02017-02-28 15:15:56 -05001153 tcp_main_t *tm = vnet_get_tcp_main ();
1154 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1155 clib_error_t *error = 0;
1156 u32 num_threads;
Dave Barachb7f1faa2017-08-29 11:43:37 -04001157 int thread;
Dave Barach2c25a622017-06-26 11:35:07 -04001158 tcp_connection_t *tc __attribute__ ((unused));
Florin Coras66b11312017-07-31 17:18:03 -07001159 u32 preallocated_connections_per_thread;
Dave Barach68b0fb02017-02-28 15:15:56 -05001160
Dave Barach68b0fb02017-02-28 15:15:56 -05001161 if ((error = vlib_call_init_function (vm, ip_main_init)))
1162 return error;
1163 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1164 return error;
1165 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1166 return error;
1167
1168 /*
1169 * Registrations
1170 */
1171
Dave Barach68b0fb02017-02-28 15:15:56 -05001172 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
rootc9d1c5b2017-08-15 12:58:31 -04001173 ip6_register_protocol (IP_PROTOCOL_TCP, tcp6_input_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001174
Florin Coras04e53442017-07-16 17:12:15 -07001175 /* Register as transport with session layer */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001176 transport_register_protocol (TRANSPORT_PROTO_TCP, 1, &tcp_proto);
1177 transport_register_protocol (TRANSPORT_PROTO_TCP, 0, &tcp_proto);
Dave Barach68b0fb02017-02-28 15:15:56 -05001178
1179 /*
1180 * Initialize data structures
1181 */
1182
1183 num_threads = 1 /* main thread */ + vtm->n_threads;
1184 vec_validate (tm->connections, num_threads - 1);
1185
Dave Barach2c25a622017-06-26 11:35:07 -04001186 /*
Florin Coras66b11312017-07-31 17:18:03 -07001187 * Preallocate connections. Assume that thread 0 won't
1188 * use preallocated threads when running multi-core
Dave Barach2c25a622017-06-26 11:35:07 -04001189 */
Florin Coras66b11312017-07-31 17:18:03 -07001190 if (num_threads == 1)
Dave Barach2c25a622017-06-26 11:35:07 -04001191 {
Florin Coras66b11312017-07-31 17:18:03 -07001192 thread = 0;
1193 preallocated_connections_per_thread = tm->preallocated_connections;
1194 }
1195 else
1196 {
1197 thread = 1;
1198 preallocated_connections_per_thread =
1199 tm->preallocated_connections / (num_threads - 1);
1200 }
1201 for (; thread < num_threads; thread++)
1202 {
Dave Barachb7f1faa2017-08-29 11:43:37 -04001203 if (preallocated_connections_per_thread)
1204 pool_init_fixed (tm->connections[thread],
1205 preallocated_connections_per_thread);
Dave Barach2c25a622017-06-26 11:35:07 -04001206 }
1207
1208 /*
Dave Barachb7f1faa2017-08-29 11:43:37 -04001209 * Use a preallocated half-open connection pool?
Dave Barach2c25a622017-06-26 11:35:07 -04001210 */
Dave Barachb7f1faa2017-08-29 11:43:37 -04001211 if (tm->preallocated_half_open_connections)
1212 pool_init_fixed (tm->half_open_connections,
1213 tm->preallocated_half_open_connections);
Dave Barach2c25a622017-06-26 11:35:07 -04001214
Dave Barach68b0fb02017-02-28 15:15:56 -05001215 /* Initialize per worker thread tx buffers (used for control messages) */
1216 vec_validate (tm->tx_buffers, num_threads - 1);
1217
1218 /* Initialize timer wheels */
1219 vec_validate (tm->timer_wheels, num_threads - 1);
1220 tcp_initialize_timer_wheels (tm);
1221
Dave Barach68b0fb02017-02-28 15:15:56 -05001222 /* Initialize clocks per tick for TCP timestamp. Used to compute
1223 * monotonically increasing timestamps. */
1224 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1225 / TCP_TSTAMP_RESOLUTION;
1226
Florin Coras04e53442017-07-16 17:12:15 -07001227 if (num_threads > 1)
Florin Coras68810622017-07-24 17:40:28 -07001228 {
1229 clib_spinlock_init (&tm->half_open_lock);
Florin Coras68810622017-07-24 17:40:28 -07001230 }
Florin Coras66b11312017-07-31 17:18:03 -07001231
1232 vec_validate (tm->tx_frames[0], num_threads - 1);
1233 vec_validate (tm->tx_frames[1], num_threads - 1);
Florin Coras9d063042017-09-14 03:08:00 -04001234 vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
1235 vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
Florin Coras66b11312017-07-31 17:18:03 -07001236
Florin Corasb2215d62017-08-01 16:56:58 -07001237 tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
1238 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Coras82d3ec82017-08-14 08:10:42 -07001239
1240 vec_validate (tm->time_now, num_threads - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001241 return error;
1242}
1243
Florin Corasa0b34a72017-03-07 01:20:52 -08001244clib_error_t *
1245vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1246{
1247 if (is_en)
1248 {
1249 if (tcp_main.is_enabled)
1250 return 0;
1251
1252 return tcp_main_enable (vm);
1253 }
1254 else
1255 {
1256 tcp_main.is_enabled = 0;
1257 }
1258
1259 return 0;
1260}
1261
Pierre Pfister7fe51f32017-09-20 08:48:36 +02001262void
1263tcp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add)
1264{
1265 tcp_main_t *tm = &tcp_main;
1266 if (is_ip4)
1267 tm->punt_unknown4 = is_add;
1268 else
1269 tm->punt_unknown6 = is_add;
1270}
1271
Florin Corasa0b34a72017-03-07 01:20:52 -08001272clib_error_t *
1273tcp_init (vlib_main_t * vm)
1274{
1275 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Coras40364272017-11-16 09:57:50 -08001276 ip_main_t *im = &ip_main;
1277 ip_protocol_info_t *pi;
1278
1279 /* Session layer, and by implication tcp, are disabled by default */
Florin Corasa0b34a72017-03-07 01:20:52 -08001280 tm->is_enabled = 0;
Florin Coras40364272017-11-16 09:57:50 -08001281
1282 /* Register with IP for header parsing */
1283 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1284 if (pi == 0)
1285 return clib_error_return (0, "TCP protocol info AWOL");
1286 pi->format_header = format_tcp_header;
1287 pi->unformat_pg_edit = unformat_pg_tcp_header;
1288
Dave Barach3bbcfab2017-08-15 19:03:44 -04001289 tcp_api_reference ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001290 return 0;
1291}
1292
Dave Barach68b0fb02017-02-28 15:15:56 -05001293VLIB_INIT_FUNCTION (tcp_init);
1294
Dave Barach2c25a622017-06-26 11:35:07 -04001295static clib_error_t *
1296tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1297{
1298 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barachd84ba852017-08-22 17:56:46 -04001299 u64 tmp;
Dave Barach2c25a622017-06-26 11:35:07 -04001300
1301 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1302 {
1303 if (unformat
1304 (input, "preallocated-connections %d",
1305 &tm->preallocated_connections))
1306 ;
1307 else if (unformat (input, "preallocated-half-open-connections %d",
1308 &tm->preallocated_half_open_connections))
1309 ;
Dave Barachd84ba852017-08-22 17:56:46 -04001310 else if (unformat (input, "local-endpoints-table-memory %U",
1311 unformat_memory_size, &tmp))
1312 {
1313 if (tmp >= 0x100000000)
1314 return clib_error_return (0, "memory size %llx (%lld) too large",
1315 tmp, tmp);
1316 tm->local_endpoints_table_memory = tmp;
1317 }
1318 else if (unformat (input, "local-endpoints-table-buckets %d",
1319 &tm->local_endpoints_table_buckets))
1320 ;
1321
1322
Dave Barach2c25a622017-06-26 11:35:07 -04001323 else
1324 return clib_error_return (0, "unknown input `%U'",
1325 format_unformat_error, input);
1326 }
1327 return 0;
1328}
1329
1330VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1331
Dave Barach3bbcfab2017-08-15 19:03:44 -04001332
1333/**
1334 * \brief Configure an ipv4 source address range
1335 * @param vm vlib_main_t pointer
1336 * @param start first ipv4 address in the source address range
1337 * @param end last ipv4 address in the source address range
1338 * @param table_id VRF / table ID, 0 for the default FIB
1339 * @return 0 if all OK, else an error indication from api_errno.h
1340 */
1341
1342int
1343tcp_configure_v4_source_address_range (vlib_main_t * vm,
1344 ip4_address_t * start,
1345 ip4_address_t * end, u32 table_id)
1346{
1347 tcp_main_t *tm = vnet_get_tcp_main ();
1348 vnet_main_t *vnm = vnet_get_main ();
1349 u32 start_host_byte_order, end_host_byte_order;
1350 fib_prefix_t prefix;
1351 vnet_sw_interface_t *si;
1352 fib_node_index_t fei;
1353 u32 fib_index = 0;
1354 u32 sw_if_index;
1355 int rv;
1356 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
1357 ip4_address_t * hi_addr, u32 fib_index,
1358 int is_del);
1359
1360 memset (&prefix, 0, sizeof (prefix));
1361
1362 fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id);
1363
1364 if (fib_index == ~0)
1365 return VNET_API_ERROR_NO_SUCH_FIB;
1366
1367 start_host_byte_order = clib_net_to_host_u32 (start->as_u32);
1368 end_host_byte_order = clib_net_to_host_u32 (end->as_u32);
1369
1370 /* sanity check for reversed args or some such */
1371 if ((end_host_byte_order - start_host_byte_order) > (10 << 10))
1372 return VNET_API_ERROR_INVALID_ARGUMENT;
1373
1374 /* Lookup the last address, to identify the interface involved */
1375 prefix.fp_len = 32;
1376 prefix.fp_proto = FIB_PROTOCOL_IP4;
1377 memcpy (&prefix.fp_addr.ip4, end, sizeof (ip4_address_t));
1378
1379 fei = fib_table_lookup (fib_index, &prefix);
1380
1381 /* Couldn't find route to destination. Bail out. */
1382 if (fei == FIB_NODE_INDEX_INVALID)
1383 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1384
1385 sw_if_index = fib_entry_get_resolving_interface (fei);
1386
1387 /* Enable proxy arp on the interface */
1388 si = vnet_get_sw_interface (vnm, sw_if_index);
1389 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
1390
1391 /* Configure proxy arp across the range */
1392 rv = vnet_proxy_arp_add_del (start, end, fib_index, 0 /* is_del */ );
1393
1394 if (rv)
1395 return rv;
1396
1397 do
1398 {
1399 dpo_id_t dpo = DPO_INVALID;
1400
1401 vec_add1 (tm->ip4_src_addresses, start[0]);
1402
1403 /* Add local adjacencies for the range */
1404
1405 receive_dpo_add_or_lock (DPO_PROTO_IP4, ~0 /* sw_if_index */ ,
1406 NULL, &dpo);
1407 prefix.fp_len = 32;
1408 prefix.fp_proto = FIB_PROTOCOL_IP4;
1409 prefix.fp_addr.ip4.as_u32 = start->as_u32;
1410
1411 fib_table_entry_special_dpo_update (fib_index,
1412 &prefix,
1413 FIB_SOURCE_API,
1414 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1415 dpo_reset (&dpo);
1416
1417 start_host_byte_order++;
1418 start->as_u32 = clib_host_to_net_u32 (start_host_byte_order);
1419 }
1420 while (start_host_byte_order <= end_host_byte_order);
1421
1422 return 0;
1423}
1424
1425/**
1426 * \brief Configure an ipv6 source address range
1427 * @param vm vlib_main_t pointer
1428 * @param start first ipv6 address in the source address range
1429 * @param end last ipv6 address in the source address range
1430 * @param table_id VRF / table ID, 0 for the default FIB
1431 * @return 0 if all OK, else an error indication from api_errno.h
1432 */
1433
1434int
1435tcp_configure_v6_source_address_range (vlib_main_t * vm,
1436 ip6_address_t * start,
1437 ip6_address_t * end, u32 table_id)
1438{
1439 tcp_main_t *tm = vnet_get_tcp_main ();
1440 fib_prefix_t prefix;
1441 u32 fib_index = 0;
1442 fib_node_index_t fei;
1443 u32 sw_if_index;
1444
1445 memset (&prefix, 0, sizeof (prefix));
1446
1447 fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
1448
1449 if (fib_index == ~0)
1450 return VNET_API_ERROR_NO_SUCH_FIB;
1451
1452 while (1)
1453 {
1454 int i;
1455 ip6_address_t tmp;
1456 dpo_id_t dpo = DPO_INVALID;
1457
1458 /* Remember this address */
1459 vec_add1 (tm->ip6_src_addresses, start[0]);
1460
1461 /* Lookup the prefix, to identify the interface involved */
1462 prefix.fp_len = 128;
1463 prefix.fp_proto = FIB_PROTOCOL_IP6;
1464 memcpy (&prefix.fp_addr.ip6, start, sizeof (ip6_address_t));
1465
1466 fei = fib_table_lookup (fib_index, &prefix);
1467
1468 /* Couldn't find route to destination. Bail out. */
1469 if (fei == FIB_NODE_INDEX_INVALID)
1470 return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1471
1472 sw_if_index = fib_entry_get_resolving_interface (fei);
1473
1474 if (sw_if_index == (u32) ~ 0)
1475 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1476
1477 /* Add a proxy neighbor discovery entry for this address */
1478 ip6_neighbor_proxy_add_del (sw_if_index, start, 0 /* is_del */ );
1479
1480 /* Add a receive adjacency for this address */
1481 receive_dpo_add_or_lock (DPO_PROTO_IP6, ~0 /* sw_if_index */ ,
1482 NULL, &dpo);
1483
1484 fib_table_entry_special_dpo_update (fib_index,
1485 &prefix,
1486 FIB_SOURCE_API,
1487 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1488 dpo_reset (&dpo);
1489
1490 /* Done with the entire range? */
1491 if (!memcmp (start, end, sizeof (start[0])))
1492 break;
1493
1494 /* Increment the address. DGMS. */
1495 tmp = start[0];
1496 for (i = 15; i >= 0; i--)
1497 {
1498 tmp.as_u8[i] += 1;
1499 if (tmp.as_u8[i] != 0)
1500 break;
1501 }
1502 start[0] = tmp;
1503 }
1504 return 0;
1505}
1506
Dave Barach2c25a622017-06-26 11:35:07 -04001507static clib_error_t *
1508tcp_src_address (vlib_main_t * vm,
1509 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1510{
Dave Barach2c25a622017-06-26 11:35:07 -04001511 ip4_address_t v4start, v4end;
1512 ip6_address_t v6start, v6end;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001513 u32 table_id = 0;
Dave Barach2c25a622017-06-26 11:35:07 -04001514 int v4set = 0;
1515 int v6set = 0;
Dave Barach3bbcfab2017-08-15 19:03:44 -04001516 int rv;
Dave Barach2c25a622017-06-26 11:35:07 -04001517
1518 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1519 {
1520 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1521 unformat_ip4_address, &v4end))
1522 v4set = 1;
1523 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1524 {
1525 memcpy (&v4end, &v4start, sizeof (v4start));
1526 v4set = 1;
1527 }
1528 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
Dave Barach3bbcfab2017-08-15 19:03:44 -04001529 unformat_ip6_address, &v6end))
Dave Barach2c25a622017-06-26 11:35:07 -04001530 v6set = 1;
1531 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1532 {
Yoann Desmouceaux6b297aa2017-09-20 10:34:22 +02001533 memcpy (&v6end, &v6start, sizeof (v6start));
Dave Barach2c25a622017-06-26 11:35:07 -04001534 v6set = 1;
1535 }
Dave Barach3bbcfab2017-08-15 19:03:44 -04001536 else if (unformat (input, "fib-table %d", &table_id))
1537 ;
Dave Barach2c25a622017-06-26 11:35:07 -04001538 else
1539 break;
1540 }
1541
1542 if (!v4set && !v6set)
1543 return clib_error_return (0, "at least one v4 or v6 address required");
1544
1545 if (v4set)
1546 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001547 rv = tcp_configure_v4_source_address_range (vm, &v4start, &v4end,
1548 table_id);
1549 switch (rv)
Dave Barach2c25a622017-06-26 11:35:07 -04001550 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001551 case 0:
1552 break;
1553
1554 case VNET_API_ERROR_NO_SUCH_FIB:
1555 return clib_error_return (0, "Invalid table-id %d", table_id);
1556
1557 case VNET_API_ERROR_INVALID_ARGUMENT:
1558 return clib_error_return (0, "Invalid address range %U - %U",
1559 format_ip4_address, &v4start,
1560 format_ip4_address, &v4end);
1561 default:
1562 return clib_error_return (0, "error %d", rv);
1563 break;
Dave Barach2c25a622017-06-26 11:35:07 -04001564 }
Dave Barach2c25a622017-06-26 11:35:07 -04001565 }
1566 if (v6set)
1567 {
Dave Barach3bbcfab2017-08-15 19:03:44 -04001568 rv = tcp_configure_v6_source_address_range (vm, &v6start, &v6end,
1569 table_id);
1570 switch (rv)
1571 {
1572 case 0:
1573 break;
1574
1575 case VNET_API_ERROR_NO_SUCH_FIB:
1576 return clib_error_return (0, "Invalid table-id %d", table_id);
1577
1578 default:
1579 return clib_error_return (0, "error %d", rv);
1580 break;
1581 }
Dave Barach2c25a622017-06-26 11:35:07 -04001582 }
1583 return 0;
1584}
1585
1586/* *INDENT-OFF* */
1587VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1588{
1589 .path = "tcp src-address",
1590 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1591 .function = tcp_src_address,
1592};
1593/* *INDENT-ON* */
1594
Florin Coras3eb50622017-07-13 01:24:57 -04001595static u8 *
1596tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1597{
1598#if TCP_SCOREBOARD_TRACE
Dave Barach2c25a622017-06-26 11:35:07 -04001599
Florin Coras3eb50622017-07-13 01:24:57 -04001600 scoreboard_trace_elt_t *block;
1601 int i = 0;
1602
1603 if (!sb->trace)
1604 return s;
1605
1606 s = format (s, "scoreboard trace:");
1607 vec_foreach (block, sb->trace)
1608 {
1609 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1610 block->ack, block->snd_una_max, block->group);
1611 if ((++i % 3) == 0)
1612 s = format (s, "\n");
1613 }
1614 return s;
1615#else
1616 return 0;
1617#endif
1618}
1619
1620static clib_error_t *
1621tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1622 vlib_cli_command_t * cmd_arg)
1623{
1624 transport_connection_t *tconn = 0;
1625 tcp_connection_t *tc;
1626 u8 *s = 0;
1627 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1628 {
1629 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1630 TRANSPORT_PROTO_TCP))
1631 ;
1632 else
1633 return clib_error_return (0, "unknown input `%U'",
1634 format_unformat_error, input);
1635 }
1636
1637 if (!TCP_SCOREBOARD_TRACE)
1638 {
1639 vlib_cli_output (vm, "scoreboard tracing not enabled");
1640 return 0;
1641 }
1642
1643 tc = tcp_get_connection_from_transport (tconn);
1644 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1645 vlib_cli_output (vm, "%v", s);
1646 return 0;
1647}
1648
1649/* *INDENT-OFF* */
1650VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1651{
1652 .path = "show tcp scoreboard trace",
1653 .short_help = "show tcp scoreboard trace <connection>",
1654 .function = tcp_show_scoreboard_trace_fn,
1655};
1656/* *INDENT-ON* */
1657
1658u8 *
1659tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1660{
1661 int i, trace_len;
1662 scoreboard_trace_elt_t *trace;
1663 u32 next_ack, left, group, has_new_ack = 0;
1664 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1665 sack_block_t *block;
1666
1667 if (!tc)
1668 return s;
1669
1670 memset (dummy_tc, 0, sizeof (*dummy_tc));
1671 tcp_connection_timers_init (dummy_tc);
1672 scoreboard_init (&dummy_tc->sack_sb);
1673 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1674
1675#if TCP_SCOREBOARD_TRACE
1676 trace = tc->sack_sb.trace;
1677 trace_len = vec_len (tc->sack_sb.trace);
1678#else
1679 trace = 0;
1680 trace_len = 0;
1681#endif
1682
1683 for (i = 0; i < trace_len; i++)
1684 {
1685 if (trace[i].ack != 0)
1686 {
1687 dummy_tc->snd_una = trace[i].ack - 1448;
1688 dummy_tc->snd_una_max = trace[i].ack;
1689 }
1690 }
1691
1692 left = 0;
1693 while (left < trace_len)
1694 {
1695 group = trace[left].group;
1696 vec_reset_length (dummy_tc->rcv_opts.sacks);
1697 has_new_ack = 0;
1698 while (trace[left].group == group)
1699 {
1700 if (trace[left].ack != 0)
1701 {
1702 if (verbose)
1703 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1704 trace[left].ack, trace[left].snd_una_max);
1705 dummy_tc->snd_una_max = trace[left].snd_una_max;
1706 next_ack = trace[left].ack;
1707 has_new_ack = 1;
1708 }
1709 else
1710 {
1711 if (verbose)
1712 s = format (s, "[%u, %u], ", trace[left].start,
1713 trace[left].end);
1714 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1715 block->start = trace[left].start;
1716 block->end = trace[left].end;
1717 }
1718 left++;
1719 }
1720
1721 /* Push segments */
1722 tcp_rcv_sacks (dummy_tc, next_ack);
1723 if (has_new_ack)
1724 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1725
1726 if (verbose)
1727 s = format (s, "result: %U", format_tcp_scoreboard,
1728 &dummy_tc->sack_sb);
1729
1730 }
1731 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1732
1733 return s;
1734}
1735
1736static clib_error_t *
1737tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1738 vlib_cli_command_t * cmd_arg)
1739{
1740 transport_connection_t *tconn = 0;
1741 tcp_connection_t *tc = 0;
1742 u8 *str = 0;
1743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1744 {
1745 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1746 TRANSPORT_PROTO_TCP))
1747 ;
1748 else
1749 return clib_error_return (0, "unknown input `%U'",
1750 format_unformat_error, input);
1751 }
1752
1753 if (!TCP_SCOREBOARD_TRACE)
1754 {
1755 vlib_cli_output (vm, "scoreboard tracing not enabled");
1756 return 0;
1757 }
1758
1759 tc = tcp_get_connection_from_transport (tconn);
1760 if (!tc)
1761 {
1762 vlib_cli_output (vm, "connection not found");
1763 return 0;
1764 }
1765 str = tcp_scoreboard_replay (str, tc, 1);
1766 vlib_cli_output (vm, "%v", str);
1767 return 0;
1768}
1769
1770/* *INDENT-OFF* */
1771VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1772{
1773 .path = "tcp replay scoreboard",
1774 .short_help = "tcp replay scoreboard <connection>",
1775 .function = tcp_scoreboard_trace_fn,
1776};
1777/* *INDENT-ON* */
Dave Barach2c25a622017-06-26 11:35:07 -04001778
Pierre Pfister7fe51f32017-09-20 08:48:36 +02001779static clib_error_t *
1780show_tcp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
1781 vlib_cli_command_t * cmd_arg)
1782{
1783 tcp_main_t *tm = vnet_get_tcp_main ();
1784 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1785 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
1786 input);
1787 vlib_cli_output (vm, "IPv4 TCP punt: %s",
1788 tm->punt_unknown4 ? "enabled" : "disabled");
1789 vlib_cli_output (vm, "IPv6 TCP punt: %s",
1790 tm->punt_unknown6 ? "enabled" : "disabled");
1791 return 0;
1792}
1793/* *INDENT-OFF* */
1794VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
1795{
1796 .path = "show tcp punt",
1797 .short_help = "show tcp punt",
1798 .function = show_tcp_punt_fn,
1799};
1800/* *INDENT-ON* */
1801
Dave Barach68b0fb02017-02-28 15:15:56 -05001802/*
1803 * fd.io coding-style-patch-verification: ON
1804 *
1805 * Local Variables:
1806 * eval: (c-set-style "gnu")
1807 * End:
1808 */