blob: bb4c6817345e4768d7451899ac5b27567f6aee4b [file] [log] [blame]
Florin Coras3cbc04b2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras3cbc04b2017-10-02 00:18:51 -07003 * 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
Florin Coras1ee78302019-02-05 15:51:15 -080016#include <vnet/session/transport.h>
Florin Coras3cbc04b2017-10-02 00:18:51 -070017#include <vnet/session/session.h>
18#include <vnet/fib/fib.h>
19
20/**
21 * Per-type vector of transport protocol virtual function tables
22 */
23transport_proto_vft_t *tp_vfts;
24
25/*
26 * Port allocator seed
27 */
28static u32 port_allocator_seed;
29
30/*
31 * Local endpoints table
32 */
33static transport_endpoint_table_t local_endpoints_table;
34
35/*
36 * Pool of local endpoints
37 */
38static transport_endpoint_t *local_endpoints;
39
40/*
41 * Local endpoints pool lock
42 */
43static clib_spinlock_t local_endpoints_lock;
44
Florin Corasd67f1122018-05-21 17:47:40 -070045/*
46 * Period used by transport pacers. Initialized by session layer
47 */
48static double transport_pacer_period;
49
Florin Coras7ac053b2018-11-05 15:57:21 -080050#define TRANSPORT_PACER_MIN_MSS 1460
51#define TRANSPORT_PACER_MIN_BURST TRANSPORT_PACER_MIN_MSS
Florin Corasf65074e2019-03-31 17:17:11 -070052#define TRANSPORT_PACER_MAX_BURST (32 * TRANSPORT_PACER_MIN_MSS)
Florin Corasd67f1122018-05-21 17:47:40 -070053
Florin Coras1c710452017-10-17 00:03:13 -070054u8 *
55format_transport_proto (u8 * s, va_list * args)
56{
57 u32 transport_proto = va_arg (*args, u32);
58 switch (transport_proto)
59 {
60 case TRANSPORT_PROTO_TCP:
61 s = format (s, "TCP");
62 break;
63 case TRANSPORT_PROTO_UDP:
64 s = format (s, "UDP");
65 break;
Marco Varlese191a5942017-10-30 18:17:21 +010066 case TRANSPORT_PROTO_SCTP:
67 s = format (s, "SCTP");
68 break;
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020069 case TRANSPORT_PROTO_NONE:
70 s = format (s, "NONE");
71 break;
72 case TRANSPORT_PROTO_TLS:
73 s = format (s, "TLS");
74 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070075 case TRANSPORT_PROTO_UDPC:
76 s = format (s, "UDPC");
77 break;
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +010078 case TRANSPORT_PROTO_QUIC:
79 s = format (s, "QUIC");
80 break;
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020081 default:
82 s = format (s, "UNKNOWN");
83 break;
Florin Coras1c710452017-10-17 00:03:13 -070084 }
85 return s;
86}
87
Florin Coras561af9b2017-12-09 10:19:43 -080088u8 *
89format_transport_proto_short (u8 * s, va_list * args)
90{
91 u32 transport_proto = va_arg (*args, u32);
92 switch (transport_proto)
93 {
94 case TRANSPORT_PROTO_TCP:
95 s = format (s, "T");
96 break;
97 case TRANSPORT_PROTO_UDP:
98 s = format (s, "U");
99 break;
Florin Coras4399c2e2018-01-25 06:34:42 -0800100 case TRANSPORT_PROTO_SCTP:
101 s = format (s, "S");
102 break;
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200103 case TRANSPORT_PROTO_NONE:
104 s = format (s, "N");
105 break;
106 case TRANSPORT_PROTO_TLS:
107 s = format (s, "J");
108 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700109 case TRANSPORT_PROTO_UDPC:
110 s = format (s, "U");
111 break;
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100112 case TRANSPORT_PROTO_QUIC:
113 s = format (s, "Q");
114 break;
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +0200115 default:
116 s = format (s, "?");
117 break;
Florin Coras561af9b2017-12-09 10:19:43 -0800118 }
119 return s;
120}
121
Florin Corasde9a8492018-10-24 22:18:58 -0700122u8 *
123format_transport_connection (u8 * s, va_list * args)
124{
125 u32 transport_proto = va_arg (*args, u32);
126 u32 conn_index = va_arg (*args, u32);
127 u32 thread_index = va_arg (*args, u32);
128 u32 verbose = va_arg (*args, u32);
129 transport_proto_vft_t *tp_vft;
130 transport_connection_t *tc;
131 u32 indent;
132
133 tp_vft = transport_protocol_get_vft (transport_proto);
134 if (!tp_vft)
135 return s;
136
137 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
138 verbose);
139 tc = tp_vft->get_connection (conn_index, thread_index);
140 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
141 {
142 indent = format_get_indent (s) + 1;
143 s = format (s, "%Upacer: %U\n", format_white_space, indent,
144 format_transport_pacer, &tc->pacer);
145 }
146 return s;
147}
148
149u8 *
150format_transport_listen_connection (u8 * s, va_list * args)
151{
152 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700153 transport_proto_vft_t *tp_vft;
154
155 tp_vft = transport_protocol_get_vft (transport_proto);
156 if (!tp_vft)
157 return s;
158
Florin Coras3389dd22019-02-01 18:00:05 -0800159 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700160 return s;
161}
162
163u8 *
164format_transport_half_open_connection (u8 * s, va_list * args)
165{
166 u32 transport_proto = va_arg (*args, u32);
167 u32 listen_index = va_arg (*args, u32);
168 transport_proto_vft_t *tp_vft;
169
170 tp_vft = transport_protocol_get_vft (transport_proto);
171 if (!tp_vft)
172 return s;
173
174 s = format (s, "%U", tp_vft->format_half_open, listen_index);
175 return s;
176}
177
Florin Coras1c710452017-10-17 00:03:13 -0700178uword
179unformat_transport_proto (unformat_input_t * input, va_list * args)
180{
181 u32 *proto = va_arg (*args, u32 *);
182 if (unformat (input, "tcp"))
183 *proto = TRANSPORT_PROTO_TCP;
184 else if (unformat (input, "TCP"))
185 *proto = TRANSPORT_PROTO_TCP;
Nathan Skrzypczakef71ef02019-03-25 10:20:56 +0100186 else if (unformat (input, "udpc"))
187 *proto = TRANSPORT_PROTO_UDPC;
188 else if (unformat (input, "UDPC"))
189 *proto = TRANSPORT_PROTO_UDPC;
Florin Coras1c710452017-10-17 00:03:13 -0700190 else if (unformat (input, "udp"))
191 *proto = TRANSPORT_PROTO_UDP;
192 else if (unformat (input, "UDP"))
193 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -0800194 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100195 *proto = TRANSPORT_PROTO_SCTP;
196 else if (unformat (input, "SCTP"))
197 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800198 else if (unformat (input, "tls"))
199 *proto = TRANSPORT_PROTO_TLS;
200 else if (unformat (input, "TLS"))
201 *proto = TRANSPORT_PROTO_TLS;
Nathan Skrzypczak60f3e652019-03-19 13:57:31 +0100202 else if (unformat (input, "quic"))
203 *proto = TRANSPORT_PROTO_QUIC;
204 else if (unformat (input, "QUIC"))
205 *proto = TRANSPORT_PROTO_QUIC;
Florin Coras1c710452017-10-17 00:03:13 -0700206 else
207 return 0;
208 return 1;
209}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210
211u32
212transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
213 ip46_address_t * ip, u16 port)
214{
215 clib_bihash_kv_24_8_t kv;
216 int rv;
217
218 kv.key[0] = ip->as_u64[0];
219 kv.key[1] = ip->as_u64[1];
220 kv.key[2] = (u64) port << 8 | (u64) proto;
221
222 rv = clib_bihash_search_inline_24_8 (ht, &kv);
223 if (rv == 0)
224 return kv.value;
225
226 return ENDPOINT_INVALID_INDEX;
227}
228
229void
230transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
231 transport_endpoint_t * te, u32 value)
232{
233 clib_bihash_kv_24_8_t kv;
234
235 kv.key[0] = te->ip.as_u64[0];
236 kv.key[1] = te->ip.as_u64[1];
237 kv.key[2] = (u64) te->port << 8 | (u64) proto;
238 kv.value = value;
239
240 clib_bihash_add_del_24_8 (ht, &kv, 1);
241}
242
243void
244transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
245 transport_endpoint_t * te)
246{
247 clib_bihash_kv_24_8_t kv;
248
249 kv.key[0] = te->ip.as_u64[0];
250 kv.key[1] = te->ip.as_u64[1];
251 kv.key[2] = (u64) te->port << 8 | (u64) proto;
252
253 clib_bihash_add_del_24_8 (ht, &kv, 0);
254}
255
256/**
257 * Register transport virtual function table.
258 *
Florin Coras561af9b2017-12-09 10:19:43 -0800259 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
260 * @param vft - virtual function table for transport proto
261 * @param fib_proto - network layer protocol
262 * @param output_node - output node index that session layer will hand off
263 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700264 */
265void
Florin Coras561af9b2017-12-09 10:19:43 -0800266transport_register_protocol (transport_proto_t transport_proto,
267 const transport_proto_vft_t * vft,
268 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700269{
Florin Coras561af9b2017-12-09 10:19:43 -0800270 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700271
Florin Coras561af9b2017-12-09 10:19:43 -0800272 vec_validate (tp_vfts, transport_proto);
273 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700274
Florin Coras561af9b2017-12-09 10:19:43 -0800275 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700276}
277
278/**
279 * Get transport virtual function table
280 *
281 * @param type - session type (not protocol type)
282 */
283transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800284transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700285{
Florin Coras561af9b2017-12-09 10:19:43 -0800286 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700287 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800288 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700289}
290
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200291u8
292transport_half_open_has_fifos (transport_proto_t tp)
293{
294 return tp_vfts[tp].transport_options.half_open_has_fifos;
295}
296
Florin Coras7fb0fe12018-04-09 09:24:52 -0700297transport_service_type_t
298transport_protocol_service_type (transport_proto_t tp)
299{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200300 return tp_vfts[tp].transport_options.service_type;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700301}
302
Florin Corasf08f26d2018-05-10 13:20:47 -0700303transport_tx_fn_type_t
304transport_protocol_tx_fn_type (transport_proto_t tp)
305{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200306 return tp_vfts[tp].transport_options.tx_type;
Florin Corasf08f26d2018-05-10 13:20:47 -0700307}
308
Florin Coras1ee78302019-02-05 15:51:15 -0800309void
310transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800311{
Florin Coras1ee78302019-02-05 15:51:15 -0800312 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800313}
314
Florin Coras1ee78302019-02-05 15:51:15 -0800315int
316transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800317{
Florin Coras1ee78302019-02-05 15:51:15 -0800318 return tp_vfts[tp].connect (tep);
319}
320
321void
322transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
323{
324 tp_vfts[tp].close (conn_index, thread_index);
325}
326
327u32
328transport_start_listen (transport_proto_t tp, u32 session_index,
329 transport_endpoint_t * tep)
330{
331 return tp_vfts[tp].start_listen (session_index, tep);
332}
333
334u32
335transport_stop_listen (transport_proto_t tp, u32 conn_index)
336{
337 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800338}
339
Florin Coras40903ac2018-06-10 14:41:23 -0700340u8
341transport_protocol_is_cl (transport_proto_t tp)
342{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200343 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
Florin Coras40903ac2018-06-10 14:41:23 -0700344}
345
Aloys Augustincdb71702019-04-08 17:54:39 +0200346always_inline void
347default_get_transport_endpoint (transport_connection_t * tc,
Florin Coras09d18c22019-04-24 11:10:02 -0700348 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200349{
350 if (is_lcl)
351 {
Florin Coras09d18c22019-04-24 11:10:02 -0700352 tep->port = tc->lcl_port;
353 tep->is_ip4 = tc->is_ip4;
354 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200355 }
356 else
357 {
Florin Coras09d18c22019-04-24 11:10:02 -0700358 tep->port = tc->rmt_port;
359 tep->is_ip4 = tc->is_ip4;
360 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200361 }
362}
363
364void
365transport_get_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700366 u32 thread_index, transport_endpoint_t * tep,
367 u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200368{
369 if (tp_vfts[tp].get_transport_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700370 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
371 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200372 else
373 {
374 transport_connection_t *tc;
375 tc = transport_get_connection (tp, conn_index, thread_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700376 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200377 }
378}
379
380void
381transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700382 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200383{
384 if (tp_vfts[tp].get_transport_listener_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700385 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200386 else
387 {
388 transport_connection_t *tc;
389 tc = transport_get_listener (tp, conn_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700390 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200391 }
392}
393
Florin Coras3cbc04b2017-10-02 00:18:51 -0700394#define PORT_MASK ((1 << 16)- 1)
395
396void
397transport_endpoint_del (u32 tepi)
398{
399 clib_spinlock_lock_if_init (&local_endpoints_lock);
400 pool_put_index (local_endpoints, tepi);
401 clib_spinlock_unlock_if_init (&local_endpoints_lock);
402}
403
404always_inline transport_endpoint_t *
405transport_endpoint_new (void)
406{
407 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700408 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700409 return tep;
410}
411
412void
413transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
414{
415 u32 tepi;
416 transport_endpoint_t *tep;
417
418 /* Cleanup local endpoint if this was an active connect */
419 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
420 clib_net_to_host_u16 (port));
421 if (tepi != ENDPOINT_INVALID_INDEX)
422 {
423 tep = pool_elt_at_index (local_endpoints, tepi);
424 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
425 transport_endpoint_del (tepi);
426 }
427}
428
Florin Coras5665ced2018-10-25 18:03:45 -0700429static void
430transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
431{
432 transport_endpoint_t *tep;
433 clib_spinlock_lock_if_init (&local_endpoints_lock);
434 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500435 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700436 tep->port = port;
437 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
438 tep - local_endpoints);
439 clib_spinlock_unlock_if_init (&local_endpoints_lock);
440}
441
Florin Coras3cbc04b2017-10-02 00:18:51 -0700442/**
443 * Allocate local port and add if successful add entry to local endpoint
444 * table to mark the pair as used.
445 */
446int
447transport_alloc_local_port (u8 proto, ip46_address_t * ip)
448{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700449 u16 min = 1024, max = 65535; /* XXX configurable ? */
450 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700451 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700452
453 limit = max - min;
454
455 /* Only support active opens from thread 0 */
456 ASSERT (vlib_get_thread_index () == 0);
457
458 /* Search for first free slot */
459 for (tries = 0; tries < limit; tries++)
460 {
461 u16 port = 0;
462
463 /* Find a port in the specified range */
464 while (1)
465 {
466 port = random_u32 (&port_allocator_seed) & PORT_MASK;
467 if (PREDICT_TRUE (port >= min && port < max))
468 break;
469 }
470
471 /* Look it up. If not found, we're done */
472 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
473 port);
474 if (tei == ENDPOINT_INVALID_INDEX)
475 {
Florin Coras5665ced2018-10-25 18:03:45 -0700476 transport_endpoint_mark_used (proto, ip, port);
477 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700478 }
479 }
480 return -1;
481}
482
Florin Coras5665ced2018-10-25 18:03:45 -0700483static clib_error_t *
484transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700485{
Florin Coras5665ced2018-10-25 18:03:45 -0700486 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700487 {
488 ip4_address_t *ip4;
489 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800490 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700491 return clib_error_return (0, "no routable ip4 address on %U",
492 format_vnet_sw_if_index_name,
493 vnet_get_main (), sw_if_index);
494 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700495 }
496 else
497 {
498 ip6_address_t *ip6;
499 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
500 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700501 return clib_error_return (0, "no routable ip6 addresses on %U",
502 format_vnet_sw_if_index_name,
503 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500504 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700505 }
506 return 0;
507}
508
509static clib_error_t *
510transport_find_local_ip_for_remote (u32 sw_if_index,
511 transport_endpoint_t * rmt,
512 ip46_address_t * lcl_addr)
513{
514 fib_node_index_t fei;
515 fib_prefix_t prefix;
516
517 if (sw_if_index == ENDPOINT_INVALID_INDEX)
518 {
519 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500520 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700521 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
522 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
523
524 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
525 fei = fib_table_lookup (rmt->fib_index, &prefix);
526
527 /* Couldn't find route to destination. Bail out. */
528 if (fei == FIB_NODE_INDEX_INVALID)
529 return clib_error_return (0, "no route to %U", format_ip46_address,
530 &rmt->ip, (rmt->is_ip4 == 0) + 1);
531
532 sw_if_index = fib_entry_get_resolving_interface (fei);
533 if (sw_if_index == ENDPOINT_INVALID_INDEX)
534 return clib_error_return (0, "no resolving interface for %U",
535 format_ip46_address, &rmt->ip,
536 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700537 }
538
Florin Coras5665ced2018-10-25 18:03:45 -0700539 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
540 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
541}
542
543int
544transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
545 ip46_address_t * lcl_addr, u16 * lcl_port)
546{
547 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
548 clib_error_t *error;
549 int port;
550 u32 tei;
551
552 /*
553 * Find the local address
554 */
555 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700556 {
Florin Coras5665ced2018-10-25 18:03:45 -0700557 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
558 rmt, lcl_addr);
559 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800560 {
561 clib_error_report (error);
562 return -1;
563 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700564 }
Florin Coras5665ced2018-10-25 18:03:45 -0700565 else
566 {
567 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500568 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
569 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700570 }
571
572 /*
573 * Allocate source port
574 */
575 if (rmt_cfg->peer.port == 0)
576 {
577 port = transport_alloc_local_port (proto, lcl_addr);
578 if (port < 1)
579 {
580 clib_warning ("Failed to allocate src port");
581 return -1;
582 }
583 *lcl_port = port;
584 }
585 else
586 {
587 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
588 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
589 lcl_addr, port);
590 if (tei != ENDPOINT_INVALID_INDEX)
591 return -1;
592
593 transport_endpoint_mark_used (proto, lcl_addr, port);
594 *lcl_port = port;
595 }
596
Florin Coras3cbc04b2017-10-02 00:18:51 -0700597 return 0;
598}
599
Florin Corasd67f1122018-05-21 17:47:40 -0700600#define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
601#define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
602
603u8 *
604format_transport_pacer (u8 * s, va_list * args)
605{
606 spacer_t *pacer = va_arg (*args, spacer_t *);
607
608 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
609 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
610 pacer->last_update);
611 return s;
612}
613
614static inline u32
615spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
616{
617 u64 n_periods = norm_time_now - pacer->last_update;
Florin Corase55a6d72018-10-31 23:09:22 -0700618 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700619
Florin Corase55a6d72018-10-31 23:09:22 -0700620 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
621 {
622 pacer->last_update = norm_time_now;
623 pacer->bucket += inc;
624 }
625
Florin Coras6bc6fd02019-03-27 18:55:11 -0700626 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700627}
628
629static inline void
630spacer_update_bucket (spacer_t * pacer, u32 bytes)
631{
632 ASSERT (pacer->bucket >= bytes);
633 pacer->bucket -= bytes;
634}
635
636static inline void
Florin Corasd67f1122018-05-21 17:47:40 -0700637spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
638{
639 ASSERT (rate_bytes_per_sec != 0);
640 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
641}
642
Florin Coras52814732019-06-12 15:38:19 -0700643static inline u64
644spacer_pace_rate (spacer_t * pacer)
645{
646 return pacer->tokens_per_period * transport_pacer_period;
647}
648
Florin Corasd67f1122018-05-21 17:47:40 -0700649void
Florin Corasc44a5582018-11-01 16:30:54 -0700650transport_connection_tx_pacer_reset (transport_connection_t * tc,
651 u32 rate_bytes_per_sec,
652 u32 start_bucket, u64 time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700653{
Florin Corasd67f1122018-05-21 17:47:40 -0700654 spacer_t *pacer = &tc->pacer;
Florin Corasd67f1122018-05-21 17:47:40 -0700655 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
656 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
Florin Corasc44a5582018-11-01 16:30:54 -0700657 pacer->bucket = start_bucket;
658}
659
660void
661transport_connection_tx_pacer_init (transport_connection_t * tc,
662 u32 rate_bytes_per_sec,
663 u32 initial_bucket)
664{
665 vlib_main_t *vm = vlib_get_main ();
666 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
667 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
668 initial_bucket,
669 vm->clib_time.last_cpu_time);
Florin Corasd67f1122018-05-21 17:47:40 -0700670}
671
672void
673transport_connection_tx_pacer_update (transport_connection_t * tc,
674 u64 bytes_per_sec)
675{
Florin Corasd67f1122018-05-21 17:47:40 -0700676 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
Florin Corasd67f1122018-05-21 17:47:40 -0700677}
678
679u32
Florin Corase55a6d72018-10-31 23:09:22 -0700680transport_connection_tx_pacer_burst (transport_connection_t * tc,
681 u64 time_now)
682{
683 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
684 return spacer_max_burst (&tc->pacer, time_now);
685}
686
687u32
688transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
689 u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700690{
691 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700692
693 snd_space = tp_vfts[tc->proto].send_space (tc);
694 if (transport_connection_is_tx_paced (tc))
695 {
696 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
697 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
Florin Corasd67f1122018-05-21 17:47:40 -0700698 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
699 snd_space = clib_min (snd_space, max_paced_burst);
700 snd_space = snd_space - snd_space % mss;
701 }
702 return snd_space;
703}
704
Florin Coras52814732019-06-12 15:38:19 -0700705u64
706transport_connection_tx_pacer_rate (transport_connection_t * tc)
707{
708 return spacer_pace_rate (&tc->pacer);
709}
710
Florin Corasd67f1122018-05-21 17:47:40 -0700711void
712transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
713{
714 tc->stats.tx_bytes += bytes;
715 if (transport_connection_is_tx_paced (tc))
716 spacer_update_bucket (&tc->pacer, bytes);
717}
718
719void
Florin Corase55a6d72018-10-31 23:09:22 -0700720transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
721 u32 bytes)
722{
723 spacer_update_bucket (&tc->pacer, bytes);
724}
725
726void
Florin Corasd67f1122018-05-21 17:47:40 -0700727transport_init_tx_pacers_period (void)
728{
729 f64 cpu_freq = os_cpu_clock_frequency ();
730 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
731}
732
Florin Coras3cbc04b2017-10-02 00:18:51 -0700733void
Florin Coras561af9b2017-12-09 10:19:43 -0800734transport_update_time (f64 time_now, u8 thread_index)
735{
736 transport_proto_vft_t *vft;
737 vec_foreach (vft, tp_vfts)
738 {
739 if (vft->update_time)
740 (vft->update_time) (time_now, thread_index);
741 }
742}
743
744void
745transport_enable_disable (vlib_main_t * vm, u8 is_en)
746{
747 transport_proto_vft_t *vft;
748 vec_foreach (vft, tp_vfts)
749 {
750 if (vft->enable)
751 (vft->enable) (vm, is_en);
752 }
753}
754
755void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700756transport_init (void)
757{
758 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800759 session_main_t *smm = vnet_get_session_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700760 u32 num_threads;
761
Florin Coras93e65802017-11-29 00:07:11 -0500762 if (smm->local_endpoints_table_buckets == 0)
763 smm->local_endpoints_table_buckets = 250000;
764 if (smm->local_endpoints_table_memory == 0)
765 smm->local_endpoints_table_memory = 512 << 20;
766
Florin Coras3cbc04b2017-10-02 00:18:51 -0700767 /* Initialize [port-allocator] random number seed */
768 port_allocator_seed = (u32) clib_cpu_time_now ();
769
770 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500771 smm->local_endpoints_table_buckets,
772 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700773 num_threads = 1 /* main thread */ + vtm->n_threads;
774 if (num_threads > 1)
775 clib_spinlock_init (&local_endpoints_lock);
776}
777
778/*
779 * fd.io coding-style-patch-verification: ON
780 *
781 * Local Variables:
782 * eval: (c-set-style "gnu")
783 * End:
784 */