blob: a2214158c1b3e1021ac9bc55b43ce5043acc802d [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
16#include <vnet/tcp/tcp.h>
17#include <vnet/session/session.h>
18#include <vnet/fib/fib.h>
Florin Corasf6359c82017-06-19 12:26:09 -040019#include <vnet/dpo/load_balance.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <math.h>
21
22tcp_main_t tcp_main;
23
24static u32
Florin Corase69f4952017-03-07 10:06:24 -080025tcp_connection_bind (u32 session_index, ip46_address_t * ip,
Dave Barach68b0fb02017-02-28 15:15:56 -050026 u16 port_host_byte_order, u8 is_ip4)
27{
28 tcp_main_t *tm = &tcp_main;
29 tcp_connection_t *listener;
30
31 pool_get (tm->listener_pool, listener);
32 memset (listener, 0, sizeof (*listener));
33
34 listener->c_c_index = listener - tm->listener_pool;
35 listener->c_lcl_port = clib_host_to_net_u16 (port_host_byte_order);
36
37 if (is_ip4)
Florin Coras6cf30ad2017-04-04 23:08:23 -070038 {
39 listener->c_lcl_ip4.as_u32 = ip->ip4.as_u32;
40 listener->c_is_ip4 = 1;
41 listener->c_proto = SESSION_TYPE_IP4_TCP;
42 }
Dave Barach68b0fb02017-02-28 15:15:56 -050043 else
Florin Coras6cf30ad2017-04-04 23:08:23 -070044 {
45 clib_memcpy (&listener->c_lcl_ip6, &ip->ip6, sizeof (ip6_address_t));
46 listener->c_proto = SESSION_TYPE_IP6_TCP;
47 }
Dave Barach68b0fb02017-02-28 15:15:56 -050048
49 listener->c_s_index = session_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050050 listener->state = TCP_STATE_LISTEN;
Dave Barach68b0fb02017-02-28 15:15:56 -050051
Florin Corase69f4952017-03-07 10:06:24 -080052 tcp_connection_timers_init (listener);
53
54 TCP_EVT_DBG (TCP_EVT_BIND, listener);
55
Dave Barach68b0fb02017-02-28 15:15:56 -050056 return listener->c_c_index;
57}
58
59u32
Florin Corase69f4952017-03-07 10:06:24 -080060tcp_session_bind_ip4 (u32 session_index, ip46_address_t * ip,
61 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050062{
Florin Corase69f4952017-03-07 10:06:24 -080063 return tcp_connection_bind (session_index, ip, port_host_byte_order, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -050064}
65
66u32
Florin Corase69f4952017-03-07 10:06:24 -080067tcp_session_bind_ip6 (u32 session_index, ip46_address_t * ip,
68 u16 port_host_byte_order)
Dave Barach68b0fb02017-02-28 15:15:56 -050069{
Florin Corase69f4952017-03-07 10:06:24 -080070 return tcp_connection_bind (session_index, ip, port_host_byte_order, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -050071}
72
73static void
Florin Corase69f4952017-03-07 10:06:24 -080074tcp_connection_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050075{
76 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach2c25a622017-06-26 11:35:07 -040077 tcp_connection_t *tc;
78
79 tc = pool_elt_at_index (tm->listener_pool, listener_index);
80
81 TCP_EVT_DBG (TCP_EVT_UNBIND, tc);
82
83 /* Poison the entry */
84 if (CLIB_DEBUG > 0)
85 memset (tc, 0xFA, sizeof (*tc));
86
Dave Barach68b0fb02017-02-28 15:15:56 -050087 pool_put_index (tm->listener_pool, listener_index);
88}
89
90u32
Florin Corase69f4952017-03-07 10:06:24 -080091tcp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -050092{
Florin Corase69f4952017-03-07 10:06:24 -080093 tcp_connection_unbind (listener_index);
Dave Barach68b0fb02017-02-28 15:15:56 -050094 return 0;
95}
96
97transport_connection_t *
98tcp_session_get_listener (u32 listener_index)
99{
100 tcp_main_t *tm = vnet_get_tcp_main ();
101 tcp_connection_t *tc;
102 tc = pool_elt_at_index (tm->listener_pool, listener_index);
103 return &tc->connection;
104}
105
106/**
107 * Cleans up connection state.
108 *
109 * No notifications.
110 */
111void
112tcp_connection_cleanup (tcp_connection_t * tc)
113{
114 tcp_main_t *tm = &tcp_main;
115 u32 tepi;
116 transport_endpoint_t *tep;
117
118 /* Cleanup local endpoint if this was an active connect */
119 tepi = transport_endpoint_lookup (&tm->local_endpoints_table, &tc->c_lcl_ip,
120 tc->c_lcl_port);
121
122 /*XXX lock */
123 if (tepi != TRANSPORT_ENDPOINT_INVALID_INDEX)
124 {
125 tep = pool_elt_at_index (tm->local_endpoints, tepi);
126 transport_endpoint_table_del (&tm->local_endpoints_table, tep);
127 pool_put (tm->local_endpoints, tep);
128 }
129
130 /* Make sure all timers are cleared */
131 tcp_connection_timers_reset (tc);
132
133 /* Check if half-open */
134 if (tc->state == TCP_STATE_SYN_SENT)
Dave Barach2c25a622017-06-26 11:35:07 -0400135 {
136 /* Poison the entry */
137 if (CLIB_DEBUG > 0)
138 memset (tc, 0xFA, sizeof (*tc));
139 pool_put (tm->half_open_connections, tc);
140 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500141 else
Dave Barach2c25a622017-06-26 11:35:07 -0400142 {
143 int thread_index = tc->c_thread_index;
144 /* Poison the entry */
145 if (CLIB_DEBUG > 0)
146 memset (tc, 0xFA, sizeof (*tc));
147 pool_put (tm->connections[thread_index], tc);
148 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500149}
150
151/**
152 * Connection removal.
153 *
154 * This should be called only once connection enters CLOSED state. Note
155 * that it notifies the session of the removal event, so if the goal is to
156 * just remove the connection, call tcp_connection_cleanup instead.
157 */
158void
159tcp_connection_del (tcp_connection_t * tc)
160{
Florin Corase69f4952017-03-07 10:06:24 -0800161 TCP_EVT_DBG (TCP_EVT_DELETE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500162 stream_session_delete_notify (&tc->connection);
163 tcp_connection_cleanup (tc);
164}
165
Florin Coras6534b7a2017-07-18 05:38:03 -0400166/**
167 * Cleanup half-open connection
168 */
169void
170tcp_half_open_connection_del (tcp_connection_t * tc)
171{
172 tcp_main_t *tm = vnet_get_tcp_main ();
173 if (CLIB_DEBUG)
174 memset (tc, 0xFA, sizeof (*tc));
175 clib_spinlock_lock (&tm->half_open_lock);
176 pool_put (tm->half_open_connections, tc);
177 clib_spinlock_unlock (&tm->half_open_lock);
178}
179
180tcp_connection_t *
181tcp_connection_new (u8 thread_index)
182{
183 tcp_main_t *tm = vnet_get_tcp_main ();
184 tcp_connection_t *tc;
185
186 pool_get (tm->connections[thread_index], tc);
187 memset (tc, 0, sizeof (*tc));
188 tc->c_c_index = tc - tm->connections[thread_index];
189 tc->c_thread_index = thread_index;
190 return tc;
191}
192
Florin Corasd79b41e2017-03-04 05:37:52 -0800193/** Notify session that connection has been reset.
194 *
195 * Switch state to closed and wait for session to call cleanup.
196 */
197void
198tcp_connection_reset (tcp_connection_t * tc)
199{
Florin Coras6534b7a2017-07-18 05:38:03 -0400200 TCP_EVT_DBG (TCP_EVT_RST_RCVD, tc);
Florin Coras11c05492017-05-10 12:29:14 -0700201 switch (tc->state)
202 {
203 case TCP_STATE_SYN_RCVD:
204 /* Cleanup everything. App wasn't notified yet */
205 stream_session_delete_notify (&tc->connection);
206 tcp_connection_cleanup (tc);
207 break;
208 case TCP_STATE_SYN_SENT:
Florin Coras6534b7a2017-07-18 05:38:03 -0400209 /* XXX remove sst from call */
210 stream_session_connect_notify (&tc->connection, tc->connection.proto,
211 1 /* fail */ );
212 tcp_connection_cleanup (tc);
213 break;
Florin Coras11c05492017-05-10 12:29:14 -0700214 case TCP_STATE_ESTABLISHED:
215 case TCP_STATE_CLOSE_WAIT:
216 case TCP_STATE_FIN_WAIT_1:
217 case TCP_STATE_FIN_WAIT_2:
218 case TCP_STATE_CLOSING:
219 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400220 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800221
Florin Coras11c05492017-05-10 12:29:14 -0700222 /* Make sure all timers are cleared */
223 tcp_connection_timers_reset (tc);
Florin Coras11c05492017-05-10 12:29:14 -0700224 stream_session_reset_notify (&tc->connection);
Dave Barach2c25a622017-06-26 11:35:07 -0400225
226 /* Wait for cleanup from session layer but not forever */
227 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Florin Coras11c05492017-05-10 12:29:14 -0700228 break;
229 case TCP_STATE_CLOSED:
230 return;
231 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800232}
233
Dave Barach68b0fb02017-02-28 15:15:56 -0500234/**
235 * Begin connection closing procedure.
236 *
237 * If at the end the connection is not in CLOSED state, it is not removed.
238 * Instead, we rely on on TCP to advance through state machine to either
239 * 1) LAST_ACK (passive close) whereby when the last ACK is received
240 * tcp_connection_del is called. This notifies session of the delete and
241 * calls cleanup.
242 * 2) TIME_WAIT (active close) whereby after 2MSL the 2MSL timer triggers
243 * and cleanup is called.
Florin Corasd79b41e2017-03-04 05:37:52 -0800244 *
245 * N.B. Half-close connections are not supported
Dave Barach68b0fb02017-02-28 15:15:56 -0500246 */
247void
248tcp_connection_close (tcp_connection_t * tc)
249{
Florin Corase69f4952017-03-07 10:06:24 -0800250 TCP_EVT_DBG (TCP_EVT_CLOSE, tc);
251
Dave Barach68b0fb02017-02-28 15:15:56 -0500252 /* Send FIN if needed */
Florin Coras93992a92017-05-24 18:03:56 -0700253 if (tc->state == TCP_STATE_ESTABLISHED
254 || tc->state == TCP_STATE_SYN_RCVD || tc->state == TCP_STATE_CLOSE_WAIT)
Dave Barach68b0fb02017-02-28 15:15:56 -0500255 tcp_send_fin (tc);
256
257 /* Switch state */
258 if (tc->state == TCP_STATE_ESTABLISHED || tc->state == TCP_STATE_SYN_RCVD)
259 tc->state = TCP_STATE_FIN_WAIT_1;
260 else if (tc->state == TCP_STATE_SYN_SENT)
261 tc->state = TCP_STATE_CLOSED;
262 else if (tc->state == TCP_STATE_CLOSE_WAIT)
263 tc->state = TCP_STATE_LAST_ACK;
Florin Coras6534b7a2017-07-18 05:38:03 -0400264 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500265
Florin Corasd79b41e2017-03-04 05:37:52 -0800266 /* If in CLOSED and WAITCLOSE timer is not set, delete connection now */
267 if (tc->timers[TCP_TIMER_WAITCLOSE] == TCP_TIMER_HANDLE_INVALID
268 && tc->state == TCP_STATE_CLOSED)
Dave Barach68b0fb02017-02-28 15:15:56 -0500269 tcp_connection_del (tc);
270}
271
272void
273tcp_session_close (u32 conn_index, u32 thread_index)
274{
275 tcp_connection_t *tc;
276 tc = tcp_connection_get (conn_index, thread_index);
277 tcp_connection_close (tc);
278}
279
280void
281tcp_session_cleanup (u32 conn_index, u32 thread_index)
282{
283 tcp_connection_t *tc;
284 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -0800285
286 /* Wait for the session tx events to clear */
287 tc->state = TCP_STATE_CLOSED;
Florin Coras6534b7a2017-07-18 05:38:03 -0400288 TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
Florin Corasd79b41e2017-03-04 05:37:52 -0800289 tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -0500290}
291
292void *
293ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
294{
295 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
296 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
297 ip_interface_address_t *ia = 0;
298
299 if (is_ip4)
300 {
301 /* *INDENT-OFF* */
302 foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
303 ({
304 return ip_interface_address_get_address (lm4, ia);
305 }));
306 /* *INDENT-ON* */
307 }
308 else
309 {
310 /* *INDENT-OFF* */
311 foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
312 ({
313 return ip_interface_address_get_address (lm6, ia);
314 }));
315 /* *INDENT-ON* */
316 }
317
318 return 0;
319}
320
Florin Corase04c2992017-03-01 08:17:34 -0800321#define PORT_MASK ((1 << 16)- 1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500322/**
323 * Allocate local port and add if successful add entry to local endpoint
324 * table to mark the pair as used.
325 */
Florin Coras6534b7a2017-07-18 05:38:03 -0400326int
Dave Barach68b0fb02017-02-28 15:15:56 -0500327tcp_allocate_local_port (tcp_main_t * tm, ip46_address_t * ip)
328{
Dave Barach68b0fb02017-02-28 15:15:56 -0500329 transport_endpoint_t *tep;
330 u32 time_now, tei;
Florin Corasd79b41e2017-03-04 05:37:52 -0800331 u16 min = 1024, max = 65535; /* XXX configurable ? */
332 int tries;
Dave Barach68b0fb02017-02-28 15:15:56 -0500333
334 tries = max - min;
335 time_now = tcp_time_now ();
336
Dave Barach2c25a622017-06-26 11:35:07 -0400337 /* Only support active opens from thread 0 */
338 ASSERT (vlib_get_thread_index () == 0);
339
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 /* Start at random point or max */
341 pool_get (tm->local_endpoints, tep);
342 clib_memcpy (&tep->ip, ip, sizeof (*ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500343
344 /* Search for first free slot */
Florin Corase04c2992017-03-01 08:17:34 -0800345 for (; tries >= 0; tries--)
Dave Barach68b0fb02017-02-28 15:15:56 -0500346 {
Florin Corase04c2992017-03-01 08:17:34 -0800347 u16 port = 0;
348
349 /* Find a port in the specified range */
350 while (1)
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 {
Florin Corase04c2992017-03-01 08:17:34 -0800352 port = random_u32 (&time_now) & PORT_MASK;
353 if (PREDICT_TRUE (port >= min && port < max))
354 break;
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 }
356
Florin Corase04c2992017-03-01 08:17:34 -0800357 tep->port = port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500358
Florin Corase04c2992017-03-01 08:17:34 -0800359 /* Look it up */
360 tei = transport_endpoint_lookup (&tm->local_endpoints_table, &tep->ip,
361 tep->port);
362 /* If not found, we're done */
363 if (tei == TRANSPORT_ENDPOINT_INVALID_INDEX)
364 {
365 transport_endpoint_table_add (&tm->local_endpoints_table, tep,
366 tep - tm->local_endpoints);
367 return tep->port;
368 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500369 }
Florin Corase04c2992017-03-01 08:17:34 -0800370 /* No free ports */
Dave Barach68b0fb02017-02-28 15:15:56 -0500371 pool_put (tm->local_endpoints, tep);
372 return -1;
373}
374
375/**
376 * Initialize all connection timers as invalid
377 */
378void
379tcp_connection_timers_init (tcp_connection_t * tc)
380{
381 int i;
382
383 /* Set all to invalid */
384 for (i = 0; i < TCP_N_TIMERS; i++)
385 {
386 tc->timers[i] = TCP_TIMER_HANDLE_INVALID;
387 }
388
389 tc->rto = TCP_RTO_INIT;
390}
391
392/**
393 * Stop all connection timers
394 */
395void
396tcp_connection_timers_reset (tcp_connection_t * tc)
397{
398 int i;
399 for (i = 0; i < TCP_N_TIMERS; i++)
400 {
401 tcp_timer_reset (tc, i);
402 }
403}
404
Dave Barach2c25a622017-06-26 11:35:07 -0400405#if 0
Florin Corasf6359c82017-06-19 12:26:09 -0400406typedef struct ip4_tcp_hdr
407{
408 ip4_header_t ip;
409 tcp_header_t tcp;
410} ip4_tcp_hdr_t;
411
412typedef struct ip6_tcp_hdr
413{
414 ip6_header_t ip;
415 tcp_header_t tcp;
416} ip6_tcp_hdr_t;
417
418static void
419tcp_connection_select_lb_bucket (tcp_connection_t * tc, const dpo_id_t * dpo,
420 dpo_id_t * result)
421{
422 const dpo_id_t *choice;
423 load_balance_t *lb;
424 int hash;
425
426 lb = load_balance_get (dpo->dpoi_index);
427 if (tc->c_is_ip4)
428 {
429 ip4_tcp_hdr_t hdr;
430 memset (&hdr, 0, sizeof (hdr));
431 hdr.ip.protocol = IP_PROTOCOL_TCP;
432 hdr.ip.address_pair.src.as_u32 = tc->c_lcl_ip.ip4.as_u32;
433 hdr.ip.address_pair.dst.as_u32 = tc->c_rmt_ip.ip4.as_u32;
434 hdr.tcp.src_port = tc->c_lcl_port;
435 hdr.tcp.dst_port = tc->c_rmt_port;
436 hash = ip4_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
437 }
438 else
439 {
440 ip6_tcp_hdr_t hdr;
441 memset (&hdr, 0, sizeof (hdr));
442 hdr.ip.protocol = IP_PROTOCOL_TCP;
443 clib_memcpy (&hdr.ip.src_address, &tc->c_lcl_ip.ip6,
444 sizeof (ip6_address_t));
445 clib_memcpy (&hdr.ip.dst_address, &tc->c_rmt_ip.ip6,
446 sizeof (ip6_address_t));
447 hdr.tcp.src_port = tc->c_lcl_port;
448 hdr.tcp.dst_port = tc->c_rmt_port;
449 hash = ip6_compute_flow_hash (&hdr.ip, lb->lb_hash_config);
450 }
451 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
452 dpo_copy (result, choice);
453}
454
455fib_node_index_t
456tcp_lookup_rmt_in_fib (tcp_connection_t * tc)
457{
458 fib_prefix_t prefix;
459
460 clib_memcpy (&prefix.fp_addr, &tc->c_rmt_ip, sizeof (prefix.fp_addr));
461 prefix.fp_proto = tc->c_is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
462 prefix.fp_len = tc->c_is_ip4 ? 32 : 128;
463 return fib_table_lookup (0, &prefix);
464}
465
466static int
467tcp_connection_stack_on_fib_entry (tcp_connection_t * tc)
468{
469 dpo_id_t choice = DPO_INVALID;
470 u32 output_node_index;
471 fib_entry_t *fe;
472
473 fe = fib_entry_get (tc->c_rmt_fei);
474 if (fe->fe_lb.dpoi_type != DPO_LOAD_BALANCE)
475 return -1;
476
477 tcp_connection_select_lb_bucket (tc, &fe->fe_lb, &choice);
478
479 output_node_index =
480 tc->c_is_ip4 ? tcp4_output_node.index : tcp6_output_node.index;
481 dpo_stack_from_node (output_node_index, &tc->c_rmt_dpo, &choice);
482 return 0;
483}
484
485/** Stack tcp connection on peer's fib entry.
486 *
487 * This ultimately populates the dpo the connection will use to send packets.
488 */
489static void
490tcp_connection_fib_attach (tcp_connection_t * tc)
491{
492 tc->c_rmt_fei = tcp_lookup_rmt_in_fib (tc);
493
494 ASSERT (tc->c_rmt_fei != FIB_NODE_INDEX_INVALID);
495
496 tcp_connection_stack_on_fib_entry (tc);
497}
Dave Barach2c25a622017-06-26 11:35:07 -0400498#endif /* 0 */
Florin Corasf6359c82017-06-19 12:26:09 -0400499
Dave Barach68b0fb02017-02-28 15:15:56 -0500500/** Initialize tcp connection variables
501 *
502 * Should be called after having received a msg from the peer, i.e., a SYN or
503 * a SYNACK, such that connection options have already been exchanged. */
504void
505tcp_connection_init_vars (tcp_connection_t * tc)
506{
507 tcp_connection_timers_init (tc);
Florin Corasc8343412017-05-04 14:25:50 -0700508 tcp_init_mss (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700509 scoreboard_init (&tc->sack_sb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500510 tcp_cc_init (tc);
Dave Barach2c25a622017-06-26 11:35:07 -0400511 // tcp_connection_fib_attach (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500512}
513
514int
515tcp_connection_open (ip46_address_t * rmt_addr, u16 rmt_port, u8 is_ip4)
516{
517 tcp_main_t *tm = vnet_get_tcp_main ();
518 tcp_connection_t *tc;
519 fib_prefix_t prefix;
Florin Corasf6359c82017-06-19 12:26:09 -0400520 fib_node_index_t fei;
521 u32 sw_if_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500522 ip46_address_t lcl_addr;
Florin Coras6534b7a2017-07-18 05:38:03 -0400523 int lcl_port;
Dave Barach68b0fb02017-02-28 15:15:56 -0500524
525 /*
526 * Find the local address and allocate port
527 */
528 memset (&lcl_addr, 0, sizeof (lcl_addr));
529
530 /* Find a FIB path to the destination */
531 clib_memcpy (&prefix.fp_addr, rmt_addr, sizeof (*rmt_addr));
532 prefix.fp_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
533 prefix.fp_len = is_ip4 ? 32 : 128;
534
535 fei = fib_table_lookup (0, &prefix);
536
537 /* Couldn't find route to destination. Bail out. */
538 if (fei == FIB_NODE_INDEX_INVALID)
Florin Coras6534b7a2017-07-18 05:38:03 -0400539 {
540 clib_warning ("no route to destination");
541 return -1;
542 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500543
544 sw_if_index = fib_entry_get_resolving_interface (fei);
545
546 if (sw_if_index == (u32) ~ 0)
Florin Coras6534b7a2017-07-18 05:38:03 -0400547 {
548 clib_warning ("no resolving interface for %U", format_ip46_address,
549 rmt_addr, IP46_TYPE_IP4);
550 return -1;
551 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500552
553 if (is_ip4)
554 {
555 ip4_address_t *ip4;
Dave Barach2c25a622017-06-26 11:35:07 -0400556 int index;
557 if (vec_len (tm->ip4_src_addresses))
558 {
559 index = tm->last_v4_address_rotor++;
560 if (tm->last_v4_address_rotor >= vec_len (tm->ip4_src_addresses))
561 tm->last_v4_address_rotor = 0;
562 lcl_addr.ip4.as_u32 = tm->ip4_src_addresses[index].as_u32;
563 }
564 else
565 {
566 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
567 lcl_addr.ip4.as_u32 = ip4->as_u32;
568 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500569 }
570 else
571 {
572 ip6_address_t *ip6;
Dave Barach2c25a622017-06-26 11:35:07 -0400573 int index;
574
575 if (vec_len (tm->ip6_src_addresses))
576 {
577 index = tm->last_v6_address_rotor++;
578 if (tm->last_v6_address_rotor >= vec_len (tm->ip6_src_addresses))
579 tm->last_v6_address_rotor = 0;
580 clib_memcpy (&lcl_addr.ip6, &tm->ip6_src_addresses[index],
581 sizeof (*ip6));
582 }
583 else
584 {
585 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
586 clib_memcpy (&lcl_addr.ip6, ip6, sizeof (*ip6));
587 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500588 }
589
590 /* Allocate source port */
591 lcl_port = tcp_allocate_local_port (tm, &lcl_addr);
592 if (lcl_port < 1)
Florin Corase04c2992017-03-01 08:17:34 -0800593 {
594 clib_warning ("Failed to allocate src port");
595 return -1;
596 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
598 /*
599 * Create connection and send SYN
600 */
601
602 pool_get (tm->half_open_connections, tc);
603 memset (tc, 0, sizeof (*tc));
604
605 clib_memcpy (&tc->c_rmt_ip, rmt_addr, sizeof (ip46_address_t));
606 clib_memcpy (&tc->c_lcl_ip, &lcl_addr, sizeof (ip46_address_t));
607 tc->c_rmt_port = clib_host_to_net_u16 (rmt_port);
608 tc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
609 tc->c_c_index = tc - tm->half_open_connections;
610 tc->c_is_ip4 = is_ip4;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700611 tc->c_proto = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
Dave Barach68b0fb02017-02-28 15:15:56 -0500612
613 /* The other connection vars will be initialized after SYN ACK */
614 tcp_connection_timers_init (tc);
615
Florin Corase69f4952017-03-07 10:06:24 -0800616 TCP_EVT_DBG (TCP_EVT_OPEN, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -0400617 tc->state = TCP_STATE_SYN_SENT;
618 tcp_send_syn (tc);
Florin Corase69f4952017-03-07 10:06:24 -0800619
Dave Barach68b0fb02017-02-28 15:15:56 -0500620 return tc->c_c_index;
621}
622
623int
624tcp_session_open_ip4 (ip46_address_t * addr, u16 port)
625{
626 return tcp_connection_open (addr, port, 1);
627}
628
629int
630tcp_session_open_ip6 (ip46_address_t * addr, u16 port)
631{
632 return tcp_connection_open (addr, port, 0);
633}
634
Florin Corase69f4952017-03-07 10:06:24 -0800635const char *tcp_dbg_evt_str[] = {
636#define _(sym, str) str,
637 foreach_tcp_dbg_evt
638#undef _
639};
640
641const char *tcp_fsm_states[] = {
642#define _(sym, str) str,
643 foreach_tcp_fsm_state
644#undef _
645};
646
Dave Barach68b0fb02017-02-28 15:15:56 -0500647u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800648format_tcp_state (u8 * s, va_list * args)
649{
Florin Corasbb292f42017-05-19 09:49:19 -0700650 u32 state = va_arg (*args, u32);
Florin Corase69f4952017-03-07 10:06:24 -0800651
Florin Corasbb292f42017-05-19 09:49:19 -0700652 if (state < TCP_N_STATES)
653 s = format (s, "%s", tcp_fsm_states[state]);
Florin Corase69f4952017-03-07 10:06:24 -0800654 else
Florin Corasbb292f42017-05-19 09:49:19 -0700655 s = format (s, "UNKNOWN (%d (0x%x))", state, state);
Florin Corase69f4952017-03-07 10:06:24 -0800656 return s;
657}
658
659const char *tcp_conn_timers[] = {
660#define _(sym, str) str,
661 foreach_tcp_timer
662#undef _
663};
664
665u8 *
666format_tcp_timers (u8 * s, va_list * args)
667{
668 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Coras93992a92017-05-24 18:03:56 -0700669 int i, last = -1;
Florin Corase69f4952017-03-07 10:06:24 -0800670
671 for (i = 0; i < TCP_N_TIMERS; i++)
672 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
673 last = i;
674
675 s = format (s, "[");
676 for (i = 0; i < last; i++)
677 {
678 if (tc->timers[i] != TCP_TIMER_HANDLE_INVALID)
679 s = format (s, "%s,", tcp_conn_timers[i]);
680 }
681
Florin Coras93992a92017-05-24 18:03:56 -0700682 if (last >= 0)
Florin Corase69f4952017-03-07 10:06:24 -0800683 s = format (s, "%s]", tcp_conn_timers[i]);
684 else
685 s = format (s, "]");
686
687 return s;
688}
689
690u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700691format_tcp_congestion_status (u8 * s, va_list * args)
692{
693 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
694 if (tcp_in_recovery (tc))
695 s = format (s, "recovery");
696 else if (tcp_in_fastrecovery (tc))
697 s = format (s, "fastrecovery");
698 else
699 s = format (s, "none");
700 return s;
701}
702
703u8 *
704format_tcp_vars (u8 * s, va_list * args)
705{
706 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Dave Barach2c25a622017-06-26 11:35:07 -0400707 s = format (s, " snd_una %u snd_nxt %u snd_una_max %u",
Florin Corasbb292f42017-05-19 09:49:19 -0700708 tc->snd_una - tc->iss, tc->snd_nxt - tc->iss,
709 tc->snd_una_max - tc->iss);
710 s = format (s, " rcv_nxt %u rcv_las %u\n",
711 tc->rcv_nxt - tc->irs, tc->rcv_las - tc->irs);
712 s = format (s, " snd_wnd %u rcv_wnd %u snd_wl1 %u snd_wl2 %u\n",
713 tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
714 tc->snd_wl2 - tc->iss);
Florin Coras93992a92017-05-24 18:03:56 -0700715 s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
716 tcp_flight_size (tc), tcp_available_snd_space (tc),
717 tcp_rcv_wnd_available (tc));
Florin Corasbb292f42017-05-19 09:49:19 -0700718 s = format (s, " cong %U ", format_tcp_congestion_status, tc);
719 s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
Florin Coras93992a92017-05-24 18:03:56 -0700720 tc->cwnd, tc->ssthresh, tc->snd_rxt_bytes, tc->bytes_acked);
Dave Barach2c25a622017-06-26 11:35:07 -0400721 s = format (s, " prev_ssthresh %u snd_congestion %u dupack %u",
Florin Coras93992a92017-05-24 18:03:56 -0700722 tc->prev_ssthresh, tc->snd_congestion - tc->iss,
723 tc->rcv_dupacks);
Dave Barach2c25a622017-06-26 11:35:07 -0400724 s = format (s, " limited_transmit %u\n", tc->limited_transmit - tc->iss);
725 s = format (s, " tsecr %u tsecr_last_ack %u\n", tc->rcv_opts.tsecr,
726 tc->tsecr_last_ack);
Florin Corasbb292f42017-05-19 09:49:19 -0700727 s = format (s, " rto %u rto_boff %u srtt %u rttvar %u rtt_ts %u ", tc->rto,
728 tc->rto_boff, tc->srtt, tc->rttvar, tc->rtt_ts);
729 s = format (s, "rtt_seq %u\n", tc->rtt_seq);
Dave Barach2c25a622017-06-26 11:35:07 -0400730 s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
731 tcp_time_now () - tc->tsval_recent_age);
Florin Coras93992a92017-05-24 18:03:56 -0700732 s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb);
Florin Corasbb292f42017-05-19 09:49:19 -0700733 if (vec_len (tc->snd_sacks))
734 s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
735
736 return s;
737}
738
739u8 *
740format_tcp_connection_id (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 Corasa5464812017-04-19 13:00:05 -0700743 if (!tc)
744 return s;
Florin Corase69f4952017-03-07 10:06:24 -0800745 if (tc->c_is_ip4)
746 {
747 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
748 format_ip4_address, &tc->c_lcl_ip4,
749 clib_net_to_host_u16 (tc->c_lcl_port), format_ip4_address,
750 &tc->c_rmt_ip4, clib_net_to_host_u16 (tc->c_rmt_port));
751 }
752 else
753 {
754 s = format (s, "[#%d][%s] %U:%d->%U:%d", tc->c_thread_index, "T",
755 format_ip6_address, &tc->c_lcl_ip6,
756 clib_net_to_host_u16 (tc->c_lcl_port), format_ip6_address,
757 &tc->c_rmt_ip6, clib_net_to_host_u16 (tc->c_rmt_port));
758 }
759
760 return s;
761}
762
763u8 *
Florin Corasbb292f42017-05-19 09:49:19 -0700764format_tcp_connection (u8 * s, va_list * args)
Florin Corase69f4952017-03-07 10:06:24 -0800765{
766 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
Florin Corasbb292f42017-05-19 09:49:19 -0700767 u32 verbose = va_arg (*args, u32);
768
769 s = format (s, "%-50U", format_tcp_connection_id, tc);
770 if (verbose)
771 {
772 s = format (s, "%-15U", format_tcp_state, tc->state);
773 if (verbose > 1)
774 s = format (s, " %U\n%U", format_tcp_timers, tc, format_tcp_vars, tc);
775 }
Florin Coras3eb50622017-07-13 01:24:57 -0400776
Florin Corase69f4952017-03-07 10:06:24 -0800777 return s;
778}
779
780u8 *
781format_tcp_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500782{
783 u32 tci = va_arg (*args, u32);
784 u32 thread_index = va_arg (*args, u32);
Florin Corasbb292f42017-05-19 09:49:19 -0700785 u32 verbose = va_arg (*args, u32);
Dave Barach68b0fb02017-02-28 15:15:56 -0500786 tcp_connection_t *tc;
787
788 tc = tcp_connection_get (tci, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700789 if (tc)
Florin Coras93992a92017-05-24 18:03:56 -0700790 s = format (s, "%U", format_tcp_connection, tc, verbose);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700791 else
Florin Coras93992a92017-05-24 18:03:56 -0700792 s = format (s, "empty");
793 return s;
Dave Barach68b0fb02017-02-28 15:15:56 -0500794}
795
796u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800797format_tcp_listener_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500798{
799 u32 tci = va_arg (*args, u32);
800 tcp_connection_t *tc = tcp_listener_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700801 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500802}
803
804u8 *
Florin Corase69f4952017-03-07 10:06:24 -0800805format_tcp_half_open_session (u8 * s, va_list * args)
Dave Barach68b0fb02017-02-28 15:15:56 -0500806{
807 u32 tci = va_arg (*args, u32);
808 tcp_connection_t *tc = tcp_half_open_connection_get (tci);
Florin Corasbb292f42017-05-19 09:49:19 -0700809 return format (s, "%U", format_tcp_connection_id, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500810}
811
Florin Coras06d11012017-05-17 14:21:51 -0700812u8 *
813format_tcp_sacks (u8 * s, va_list * args)
814{
815 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
816 sack_block_t *sacks = tc->snd_sacks;
817 sack_block_t *block;
Dave Barach2c25a622017-06-26 11:35:07 -0400818 int i, len = 0;
819
820 len = vec_len (sacks);
821 for (i = 0; i < len - 1; i++)
822 {
823 block = &sacks[i];
824 s = format (s, " start %u end %u\n", block->start - tc->irs,
825 block->end - tc->irs);
826 }
827 if (len)
828 {
829 block = &sacks[len - 1];
830 s = format (s, " start %u end %u", block->start - tc->irs,
831 block->end - tc->irs);
832 }
Florin Coras06d11012017-05-17 14:21:51 -0700833 return s;
834}
835
836u8 *
Florin Coras3eb50622017-07-13 01:24:57 -0400837format_tcp_rcv_sacks (u8 * s, va_list * args)
838{
839 tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
840 sack_block_t *sacks = tc->rcv_opts.sacks;
841 sack_block_t *block;
842 int i, len = 0;
843
844 len = vec_len (sacks);
845 for (i = 0; i < len - 1; i++)
846 {
847 block = &sacks[i];
848 s = format (s, " start %u end %u\n", block->start - tc->iss,
849 block->end - tc->iss);
850 }
851 if (len)
852 {
853 block = &sacks[len - 1];
854 s = format (s, " start %u end %u", block->start - tc->iss,
855 block->end - tc->iss);
856 }
857 return s;
858}
859
860u8 *
Florin Coras06d11012017-05-17 14:21:51 -0700861format_tcp_sack_hole (u8 * s, va_list * args)
862{
863 sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
864 s = format (s, "[%u, %u]", hole->start, hole->end);
865 return s;
866}
867
868u8 *
869format_tcp_scoreboard (u8 * s, va_list * args)
870{
871 sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
872 sack_scoreboard_hole_t *hole;
Florin Coras93992a92017-05-24 18:03:56 -0700873 s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
874 sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
875 s = format (s, " last_bytes_delivered %u high_sacked %u snd_una_adv %u\n",
876 sb->last_bytes_delivered, sb->high_sacked, sb->snd_una_adv);
877 s = format (s, " cur_rxt_hole %u high_rxt %u rescue_rxt %u",
878 sb->cur_rxt_hole, sb->high_rxt, sb->rescue_rxt);
879
Florin Coras06d11012017-05-17 14:21:51 -0700880 hole = scoreboard_first_hole (sb);
Florin Coras93992a92017-05-24 18:03:56 -0700881 if (hole)
882 s = format (s, "\n head %u tail %u holes:\n", sb->head, sb->tail);
883
Florin Coras06d11012017-05-17 14:21:51 -0700884 while (hole)
885 {
886 s = format (s, "%U", format_tcp_sack_hole, hole);
887 hole = scoreboard_next_hole (sb, hole);
888 }
Florin Coras3eb50622017-07-13 01:24:57 -0400889
Florin Coras06d11012017-05-17 14:21:51 -0700890 return s;
891}
892
Dave Barach68b0fb02017-02-28 15:15:56 -0500893transport_connection_t *
894tcp_session_get_transport (u32 conn_index, u32 thread_index)
895{
896 tcp_connection_t *tc = tcp_connection_get (conn_index, thread_index);
897 return &tc->connection;
898}
899
900transport_connection_t *
901tcp_half_open_session_get_transport (u32 conn_index)
902{
903 tcp_connection_t *tc = tcp_half_open_connection_get (conn_index);
904 return &tc->connection;
905}
906
Florin Corasc8343412017-05-04 14:25:50 -0700907/**
908 * Compute maximum segment size for session layer.
909 *
910 * Since the result needs to be the actual data length, it first computes
911 * the tcp options to be used in the next burst and subtracts their
912 * length from the connection's snd_mss.
913 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500914u16
915tcp_session_send_mss (transport_connection_t * trans_conn)
916{
917 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Corasc8343412017-05-04 14:25:50 -0700918
919 /* Ensure snd_mss does accurately reflect the amount of data we can push
920 * in a segment. This also makes sure that options are updated according to
921 * the current state of the connection. */
922 tcp_update_snd_mss (tc);
923
Dave Barach68b0fb02017-02-28 15:15:56 -0500924 return tc->snd_mss;
925}
926
Florin Coras3af90fc2017-05-03 21:09:42 -0700927always_inline u32
928tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
929{
Dave Barach2c25a622017-06-26 11:35:07 -0400930 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Coras3af90fc2017-05-03 21:09:42 -0700931 {
Florin Corasdb84e572017-05-09 18:54:52 -0700932 return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700933 }
934
935 /* If we can't write at least a segment, don't try at all */
Dave Barach2c25a622017-06-26 11:35:07 -0400936 if (PREDICT_FALSE (snd_space < tc->snd_mss))
937 {
938 if (snd_space > clib_min (tc->mss, tc->rcv_opts.mss) - TCP_HDR_LEN_MAX)
939 return snd_space;
940 return 0;
941 }
Florin Coras3af90fc2017-05-03 21:09:42 -0700942
943 /* round down to mss multiple */
944 return snd_space - (snd_space % tc->snd_mss);
945}
946
Florin Coras6792ec02017-03-13 03:49:51 -0700947/**
948 * Compute tx window session is allowed to fill.
Florin Corasbb292f42017-05-19 09:49:19 -0700949 *
950 * Takes into account available send space, snd_mss and the congestion
951 * state of the connection. If possible, the value returned is a multiple
952 * of snd_mss.
953 *
954 * @param tc tcp connection
955 * @return number of bytes session is allowed to write
Florin Coras6792ec02017-03-13 03:49:51 -0700956 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500957u32
Florin Corasbb292f42017-05-19 09:49:19 -0700958tcp_snd_space (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500959{
Florin Corasf03a59a2017-06-09 21:07:32 -0700960 int snd_space, snt_limited;
Florin Coras6792ec02017-03-13 03:49:51 -0700961
Florin Corasf03a59a2017-06-09 21:07:32 -0700962 if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
Florin Coras6792ec02017-03-13 03:49:51 -0700963 {
964 snd_space = tcp_available_snd_space (tc);
Florin Corasf03a59a2017-06-09 21:07:32 -0700965
966 /* If we haven't gotten dupacks or if we did and have gotten sacked
967 * bytes then we can still send as per Limited Transmit (RFC3042) */
968 if (PREDICT_FALSE (tc->rcv_dupacks != 0
969 && (tcp_opts_sack_permitted (tc)
970 && tc->sack_sb.last_sacked_bytes == 0)))
971 {
972 if (tc->rcv_dupacks == 1 && tc->limited_transmit != tc->snd_nxt)
973 tc->limited_transmit = tc->snd_nxt;
974 ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
975
976 snt_limited = tc->snd_nxt - tc->limited_transmit;
977 snd_space = clib_max (2 * tc->snd_mss - snt_limited, 0);
978 }
Florin Coras3af90fc2017-05-03 21:09:42 -0700979 return tcp_round_snd_space (tc, snd_space);
980 }
Florin Coras6792ec02017-03-13 03:49:51 -0700981
Florin Coras3af90fc2017-05-03 21:09:42 -0700982 if (tcp_in_recovery (tc))
983 {
984 tc->snd_nxt = tc->snd_una_max;
Florin Coras93992a92017-05-24 18:03:56 -0700985 snd_space = tcp_available_wnd (tc) - tc->snd_rxt_bytes
Florin Coras3af90fc2017-05-03 21:09:42 -0700986 - (tc->snd_una_max - tc->snd_congestion);
Florin Corasc8343412017-05-04 14:25:50 -0700987 if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
Florin Coras6792ec02017-03-13 03:49:51 -0700988 return 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700989 return tcp_round_snd_space (tc, snd_space);
Florin Coras6792ec02017-03-13 03:49:51 -0700990 }
991
992 /* If in fast recovery, send 1 SMSS if wnd allows */
Florin Coras93992a92017-05-24 18:03:56 -0700993 if (tcp_in_fastrecovery (tc)
994 && tcp_available_snd_space (tc) && !tcp_fastrecovery_sent_1_smss (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700995 {
996 tcp_fastrecovery_1_smss_on (tc);
997 return tc->snd_mss;
998 }
999
1000 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001001}
1002
1003u32
Florin Corasbb292f42017-05-19 09:49:19 -07001004tcp_session_send_space (transport_connection_t * trans_conn)
1005{
1006 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
1007 return tcp_snd_space (tc);
1008}
1009
Florin Coras93992a92017-05-24 18:03:56 -07001010i32
1011tcp_rcv_wnd_available (tcp_connection_t * tc)
1012{
1013 return (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
1014}
1015
Florin Corasbb292f42017-05-19 09:49:19 -07001016u32
Florin Corasd79b41e2017-03-04 05:37:52 -08001017tcp_session_tx_fifo_offset (transport_connection_t * trans_conn)
Dave Barach68b0fb02017-02-28 15:15:56 -05001018{
1019 tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
Florin Coras6792ec02017-03-13 03:49:51 -07001020
1021 ASSERT (seq_geq (tc->snd_nxt, tc->snd_una));
1022
1023 /* This still works if fast retransmit is on */
Florin Corasd79b41e2017-03-04 05:37:52 -08001024 return (tc->snd_nxt - tc->snd_una);
Dave Barach68b0fb02017-02-28 15:15:56 -05001025}
1026
1027/* *INDENT-OFF* */
1028const static transport_proto_vft_t tcp4_proto = {
1029 .bind = tcp_session_bind_ip4,
Florin Corase69f4952017-03-07 10:06:24 -08001030 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001031 .push_header = tcp_push_header,
1032 .get_connection = tcp_session_get_transport,
1033 .get_listener = tcp_session_get_listener,
1034 .get_half_open = tcp_half_open_session_get_transport,
1035 .open = tcp_session_open_ip4,
1036 .close = tcp_session_close,
1037 .cleanup = tcp_session_cleanup,
1038 .send_mss = tcp_session_send_mss,
1039 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001040 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001041 .format_connection = format_tcp_session,
1042 .format_listener = format_tcp_listener_session,
1043 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001044};
1045
1046const static transport_proto_vft_t tcp6_proto = {
1047 .bind = tcp_session_bind_ip6,
Florin Corase69f4952017-03-07 10:06:24 -08001048 .unbind = tcp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -05001049 .push_header = tcp_push_header,
1050 .get_connection = tcp_session_get_transport,
1051 .get_listener = tcp_session_get_listener,
1052 .get_half_open = tcp_half_open_session_get_transport,
1053 .open = tcp_session_open_ip6,
1054 .close = tcp_session_close,
1055 .cleanup = tcp_session_cleanup,
1056 .send_mss = tcp_session_send_mss,
1057 .send_space = tcp_session_send_space,
Florin Corasd79b41e2017-03-04 05:37:52 -08001058 .tx_fifo_offset = tcp_session_tx_fifo_offset,
Florin Corase69f4952017-03-07 10:06:24 -08001059 .format_connection = format_tcp_session,
1060 .format_listener = format_tcp_listener_session,
1061 .format_half_open = format_tcp_half_open_session,
Dave Barach68b0fb02017-02-28 15:15:56 -05001062};
1063/* *INDENT-ON* */
1064
1065void
1066tcp_timer_keep_handler (u32 conn_index)
1067{
Damjan Marion586afd72017-04-05 19:18:20 +02001068 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001069 tcp_connection_t *tc;
1070
Damjan Marion586afd72017-04-05 19:18:20 +02001071 tc = tcp_connection_get (conn_index, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05001072 tc->timers[TCP_TIMER_KEEP] = TCP_TIMER_HANDLE_INVALID;
1073
1074 tcp_connection_close (tc);
1075}
1076
1077void
1078tcp_timer_establish_handler (u32 conn_index)
1079{
1080 tcp_connection_t *tc;
1081 u8 sst;
1082
1083 tc = tcp_half_open_connection_get (conn_index);
1084 tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
1085
1086 ASSERT (tc->state == TCP_STATE_SYN_SENT);
1087
1088 sst = tc->c_is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1089 stream_session_connect_notify (&tc->connection, sst, 1 /* fail */ );
1090
1091 tcp_connection_cleanup (tc);
1092}
1093
1094void
Florin Corasd79b41e2017-03-04 05:37:52 -08001095tcp_timer_waitclose_handler (u32 conn_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001096{
Damjan Marion586afd72017-04-05 19:18:20 +02001097 u32 thread_index = vlib_get_thread_index ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001098 tcp_connection_t *tc;
1099
Damjan Marion586afd72017-04-05 19:18:20 +02001100 tc = tcp_connection_get (conn_index, thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001101 tc->timers[TCP_TIMER_WAITCLOSE] = TCP_TIMER_HANDLE_INVALID;
1102
1103 /* Session didn't come back with a close(). Send FIN either way
1104 * and switch to LAST_ACK. */
1105 if (tc->state == TCP_STATE_CLOSE_WAIT)
1106 {
1107 if (tc->flags & TCP_CONN_FINSNT)
1108 {
1109 clib_warning ("FIN was sent and still in CLOSE WAIT. Weird!");
1110 }
1111
1112 tcp_send_fin (tc);
1113 tc->state = TCP_STATE_LAST_ACK;
1114
1115 /* Make sure we don't wait in LAST ACK forever */
1116 tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
1117
1118 /* Don't delete the connection yet */
1119 return;
1120 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001121
1122 tcp_connection_del (tc);
1123}
1124
1125/* *INDENT-OFF* */
1126static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
1127{
1128 tcp_timer_retransmit_handler,
1129 tcp_timer_delack_handler,
Florin Coras3e350af2017-03-30 02:54:28 -07001130 tcp_timer_persist_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001131 tcp_timer_keep_handler,
Florin Corasd79b41e2017-03-04 05:37:52 -08001132 tcp_timer_waitclose_handler,
Dave Barach68b0fb02017-02-28 15:15:56 -05001133 tcp_timer_retransmit_syn_handler,
1134 tcp_timer_establish_handler
1135};
1136/* *INDENT-ON* */
1137
1138static void
1139tcp_expired_timers_dispatch (u32 * expired_timers)
1140{
1141 int i;
1142 u32 connection_index, timer_id;
1143
1144 for (i = 0; i < vec_len (expired_timers); i++)
1145 {
1146 /* Get session index and timer id */
1147 connection_index = expired_timers[i] & 0x0FFFFFFF;
1148 timer_id = expired_timers[i] >> 28;
1149
Florin Corase69f4952017-03-07 10:06:24 -08001150 TCP_EVT_DBG (TCP_EVT_TIMER_POP, connection_index, timer_id);
1151
Dave Barach68b0fb02017-02-28 15:15:56 -05001152 /* Handle expiration */
1153 (*timer_expiration_handlers[timer_id]) (connection_index);
1154 }
1155}
1156
1157void
1158tcp_initialize_timer_wheels (tcp_main_t * tm)
1159{
1160 tw_timer_wheel_16t_2w_512sl_t *tw;
Florin Corasa5464812017-04-19 13:00:05 -07001161 /* *INDENT-OFF* */
1162 foreach_vlib_main (({
1163 tw = &tm->timer_wheels[ii];
Dave Barach68b0fb02017-02-28 15:15:56 -05001164 tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
1165 100e-3 /* timer period 100ms */ , ~0);
Florin Corasa5464812017-04-19 13:00:05 -07001166 tw->last_run_time = vlib_time_now (this_vlib_main);
1167 }));
1168 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001169}
1170
1171clib_error_t *
Florin Corasa0b34a72017-03-07 01:20:52 -08001172tcp_main_enable (vlib_main_t * vm)
Dave Barach68b0fb02017-02-28 15:15:56 -05001173{
Dave Barach68b0fb02017-02-28 15:15:56 -05001174 tcp_main_t *tm = vnet_get_tcp_main ();
Florin Corasa0b34a72017-03-07 01:20:52 -08001175 ip_protocol_info_t *pi;
1176 ip_main_t *im = &ip_main;
Dave Barach68b0fb02017-02-28 15:15:56 -05001177 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1178 clib_error_t *error = 0;
1179 u32 num_threads;
Dave Barach2c25a622017-06-26 11:35:07 -04001180 int thread, i;
1181 tcp_connection_t *tc __attribute__ ((unused));
Dave Barach68b0fb02017-02-28 15:15:56 -05001182
Dave Barach68b0fb02017-02-28 15:15:56 -05001183 if ((error = vlib_call_init_function (vm, ip_main_init)))
1184 return error;
1185 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
1186 return error;
1187 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
1188 return error;
1189
1190 /*
1191 * Registrations
1192 */
1193
1194 /* Register with IP */
1195 pi = ip_get_protocol_info (im, IP_PROTOCOL_TCP);
1196 if (pi == 0)
1197 return clib_error_return (0, "TCP protocol info AWOL");
1198 pi->format_header = format_tcp_header;
1199 pi->unformat_pg_edit = unformat_pg_tcp_header;
1200
1201 ip4_register_protocol (IP_PROTOCOL_TCP, tcp4_input_node.index);
1202
1203 /* Register as transport with URI */
1204 session_register_transport (SESSION_TYPE_IP4_TCP, &tcp4_proto);
1205 session_register_transport (SESSION_TYPE_IP6_TCP, &tcp6_proto);
1206
1207 /*
1208 * Initialize data structures
1209 */
1210
1211 num_threads = 1 /* main thread */ + vtm->n_threads;
1212 vec_validate (tm->connections, num_threads - 1);
1213
Dave Barach2c25a622017-06-26 11:35:07 -04001214 /*
1215 * Preallocate connections
1216 */
1217 for (thread = 0; thread < num_threads; thread++)
1218 {
1219 for (i = 0; i < tm->preallocated_connections; i++)
1220 pool_get (tm->connections[thread], tc);
1221
1222 for (i = 0; i < tm->preallocated_connections; i++)
1223 pool_put_index (tm->connections[thread], i);
1224 }
1225
1226 /*
1227 * Preallocate half-open connections
1228 */
1229 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1230 pool_get (tm->half_open_connections, tc);
1231
1232 for (i = 0; i < tm->preallocated_half_open_connections; i++)
1233 pool_put_index (tm->half_open_connections, i);
1234
Dave Barach68b0fb02017-02-28 15:15:56 -05001235 /* Initialize per worker thread tx buffers (used for control messages) */
1236 vec_validate (tm->tx_buffers, num_threads - 1);
1237
1238 /* Initialize timer wheels */
1239 vec_validate (tm->timer_wheels, num_threads - 1);
1240 tcp_initialize_timer_wheels (tm);
1241
Dave Barach68b0fb02017-02-28 15:15:56 -05001242 /* Initialize clocks per tick for TCP timestamp. Used to compute
1243 * monotonically increasing timestamps. */
1244 tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
1245 / TCP_TSTAMP_RESOLUTION;
1246
1247 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
1248 200000 /* $$$$ config parameter nbuckets */ ,
1249 (64 << 20) /*$$$ config parameter table size */ );
Florin Coras6534b7a2017-07-18 05:38:03 -04001250 clib_spinlock_init (&tm->half_open_lock);
Dave Barach68b0fb02017-02-28 15:15:56 -05001251 return error;
1252}
1253
Florin Corasa0b34a72017-03-07 01:20:52 -08001254clib_error_t *
1255vnet_tcp_enable_disable (vlib_main_t * vm, u8 is_en)
1256{
1257 if (is_en)
1258 {
1259 if (tcp_main.is_enabled)
1260 return 0;
1261
1262 return tcp_main_enable (vm);
1263 }
1264 else
1265 {
1266 tcp_main.is_enabled = 0;
1267 }
1268
1269 return 0;
1270}
1271
1272clib_error_t *
1273tcp_init (vlib_main_t * vm)
1274{
1275 tcp_main_t *tm = vnet_get_tcp_main ();
1276
Florin Corasa0b34a72017-03-07 01:20:52 -08001277 tm->vnet_main = vnet_get_main ();
1278 tm->is_enabled = 0;
1279
1280 return 0;
1281}
1282
Dave Barach68b0fb02017-02-28 15:15:56 -05001283VLIB_INIT_FUNCTION (tcp_init);
1284
Dave Barach2c25a622017-06-26 11:35:07 -04001285
1286static clib_error_t *
1287tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
1288{
1289 tcp_main_t *tm = vnet_get_tcp_main ();
1290
1291 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1292 {
1293 if (unformat
1294 (input, "preallocated-connections %d",
1295 &tm->preallocated_connections))
1296 ;
1297 else if (unformat (input, "preallocated-half-open-connections %d",
1298 &tm->preallocated_half_open_connections))
1299 ;
1300 else
1301 return clib_error_return (0, "unknown input `%U'",
1302 format_unformat_error, input);
1303 }
1304 return 0;
1305}
1306
1307VLIB_CONFIG_FUNCTION (tcp_config_fn, "tcp");
1308
1309static clib_error_t *
1310tcp_src_address (vlib_main_t * vm,
1311 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
1312{
1313 tcp_main_t *tm = vnet_get_tcp_main ();
1314 ip4_address_t v4start, v4end;
1315 ip6_address_t v6start, v6end;
1316 int v4set = 0;
1317 int v6set = 0;
1318
1319 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1320 {
1321 if (unformat (input, "%U - %U", unformat_ip4_address, &v4start,
1322 unformat_ip4_address, &v4end))
1323 v4set = 1;
1324 else if (unformat (input, "%U", unformat_ip4_address, &v4start))
1325 {
1326 memcpy (&v4end, &v4start, sizeof (v4start));
1327 v4set = 1;
1328 }
1329 else if (unformat (input, "%U - %U", unformat_ip6_address, &v6start,
1330 unformat_ip4_address, &v6end))
1331 v6set = 1;
1332 else if (unformat (input, "%U", unformat_ip6_address, &v6start))
1333 {
1334 memcpy (&v6end, &v6start, sizeof (v4start));
1335 v6set = 1;
1336 }
1337 else
1338 break;
1339 }
1340
1341 if (!v4set && !v6set)
1342 return clib_error_return (0, "at least one v4 or v6 address required");
1343
1344 if (v4set)
1345 {
1346 u32 tmp;
1347
1348 do
1349 {
1350 vec_add1 (tm->ip4_src_addresses, v4start);
1351 tmp = clib_net_to_host_u32 (v4start.as_u32);
1352 tmp++;
1353 v4start.as_u32 = clib_host_to_net_u32 (tmp);
1354 }
1355 while (clib_host_to_net_u32 (v4start.as_u32) <=
1356 clib_host_to_net_u32 (v4end.as_u32));
1357 }
1358 if (v6set)
1359 {
1360 clib_warning ("v6 src address list unimplemented...");
1361 }
1362 return 0;
1363}
1364
1365/* *INDENT-OFF* */
1366VLIB_CLI_COMMAND (tcp_src_address_command, static) =
1367{
1368 .path = "tcp src-address",
1369 .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
1370 .function = tcp_src_address,
1371};
1372/* *INDENT-ON* */
1373
Florin Coras3eb50622017-07-13 01:24:57 -04001374static u8 *
1375tcp_scoreboard_dump_trace (u8 * s, sack_scoreboard_t * sb)
1376{
1377#if TCP_SCOREBOARD_TRACE
Dave Barach2c25a622017-06-26 11:35:07 -04001378
Florin Coras3eb50622017-07-13 01:24:57 -04001379 scoreboard_trace_elt_t *block;
1380 int i = 0;
1381
1382 if (!sb->trace)
1383 return s;
1384
1385 s = format (s, "scoreboard trace:");
1386 vec_foreach (block, sb->trace)
1387 {
1388 s = format (s, "{%u, %u, %u, %u, %u}, ", block->start, block->end,
1389 block->ack, block->snd_una_max, block->group);
1390 if ((++i % 3) == 0)
1391 s = format (s, "\n");
1392 }
1393 return s;
1394#else
1395 return 0;
1396#endif
1397}
1398
1399static clib_error_t *
1400tcp_show_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1401 vlib_cli_command_t * cmd_arg)
1402{
1403 transport_connection_t *tconn = 0;
1404 tcp_connection_t *tc;
1405 u8 *s = 0;
1406 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1407 {
1408 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1409 TRANSPORT_PROTO_TCP))
1410 ;
1411 else
1412 return clib_error_return (0, "unknown input `%U'",
1413 format_unformat_error, input);
1414 }
1415
1416 if (!TCP_SCOREBOARD_TRACE)
1417 {
1418 vlib_cli_output (vm, "scoreboard tracing not enabled");
1419 return 0;
1420 }
1421
1422 tc = tcp_get_connection_from_transport (tconn);
1423 s = tcp_scoreboard_dump_trace (s, &tc->sack_sb);
1424 vlib_cli_output (vm, "%v", s);
1425 return 0;
1426}
1427
1428/* *INDENT-OFF* */
1429VLIB_CLI_COMMAND (tcp_show_scoreboard_trace_command, static) =
1430{
1431 .path = "show tcp scoreboard trace",
1432 .short_help = "show tcp scoreboard trace <connection>",
1433 .function = tcp_show_scoreboard_trace_fn,
1434};
1435/* *INDENT-ON* */
1436
1437u8 *
1438tcp_scoreboard_replay (u8 * s, tcp_connection_t * tc, u8 verbose)
1439{
1440 int i, trace_len;
1441 scoreboard_trace_elt_t *trace;
1442 u32 next_ack, left, group, has_new_ack = 0;
1443 tcp_connection_t _dummy_tc, *dummy_tc = &_dummy_tc;
1444 sack_block_t *block;
1445
1446 if (!tc)
1447 return s;
1448
1449 memset (dummy_tc, 0, sizeof (*dummy_tc));
1450 tcp_connection_timers_init (dummy_tc);
1451 scoreboard_init (&dummy_tc->sack_sb);
1452 dummy_tc->rcv_opts.flags |= TCP_OPTS_FLAG_SACK;
1453
1454#if TCP_SCOREBOARD_TRACE
1455 trace = tc->sack_sb.trace;
1456 trace_len = vec_len (tc->sack_sb.trace);
1457#else
1458 trace = 0;
1459 trace_len = 0;
1460#endif
1461
1462 for (i = 0; i < trace_len; i++)
1463 {
1464 if (trace[i].ack != 0)
1465 {
1466 dummy_tc->snd_una = trace[i].ack - 1448;
1467 dummy_tc->snd_una_max = trace[i].ack;
1468 }
1469 }
1470
1471 left = 0;
1472 while (left < trace_len)
1473 {
1474 group = trace[left].group;
1475 vec_reset_length (dummy_tc->rcv_opts.sacks);
1476 has_new_ack = 0;
1477 while (trace[left].group == group)
1478 {
1479 if (trace[left].ack != 0)
1480 {
1481 if (verbose)
1482 s = format (s, "Adding ack %u, snd_una_max %u, segs: ",
1483 trace[left].ack, trace[left].snd_una_max);
1484 dummy_tc->snd_una_max = trace[left].snd_una_max;
1485 next_ack = trace[left].ack;
1486 has_new_ack = 1;
1487 }
1488 else
1489 {
1490 if (verbose)
1491 s = format (s, "[%u, %u], ", trace[left].start,
1492 trace[left].end);
1493 vec_add2 (dummy_tc->rcv_opts.sacks, block, 1);
1494 block->start = trace[left].start;
1495 block->end = trace[left].end;
1496 }
1497 left++;
1498 }
1499
1500 /* Push segments */
1501 tcp_rcv_sacks (dummy_tc, next_ack);
1502 if (has_new_ack)
1503 dummy_tc->snd_una = next_ack + dummy_tc->sack_sb.snd_una_adv;
1504
1505 if (verbose)
1506 s = format (s, "result: %U", format_tcp_scoreboard,
1507 &dummy_tc->sack_sb);
1508
1509 }
1510 s = format (s, "result: %U", format_tcp_scoreboard, &dummy_tc->sack_sb);
1511
1512 return s;
1513}
1514
1515static clib_error_t *
1516tcp_scoreboard_trace_fn (vlib_main_t * vm, unformat_input_t * input,
1517 vlib_cli_command_t * cmd_arg)
1518{
1519 transport_connection_t *tconn = 0;
1520 tcp_connection_t *tc = 0;
1521 u8 *str = 0;
1522 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1523 {
1524 if (unformat (input, "%U", unformat_transport_connection, &tconn,
1525 TRANSPORT_PROTO_TCP))
1526 ;
1527 else
1528 return clib_error_return (0, "unknown input `%U'",
1529 format_unformat_error, input);
1530 }
1531
1532 if (!TCP_SCOREBOARD_TRACE)
1533 {
1534 vlib_cli_output (vm, "scoreboard tracing not enabled");
1535 return 0;
1536 }
1537
1538 tc = tcp_get_connection_from_transport (tconn);
1539 if (!tc)
1540 {
1541 vlib_cli_output (vm, "connection not found");
1542 return 0;
1543 }
1544 str = tcp_scoreboard_replay (str, tc, 1);
1545 vlib_cli_output (vm, "%v", str);
1546 return 0;
1547}
1548
1549/* *INDENT-OFF* */
1550VLIB_CLI_COMMAND (tcp_replay_scoreboard_command, static) =
1551{
1552 .path = "tcp replay scoreboard",
1553 .short_help = "tcp replay scoreboard <connection>",
1554 .function = tcp_scoreboard_trace_fn,
1555};
1556/* *INDENT-ON* */
Dave Barach2c25a622017-06-26 11:35:07 -04001557
Dave Barach68b0fb02017-02-28 15:15:56 -05001558/*
1559 * fd.io coding-style-patch-verification: ON
1560 *
1561 * Local Variables:
1562 * eval: (c-set-style "gnu")
1563 * End:
1564 */