blob: 3160a48d37d5b6db5c3139d99a581b03013645cd [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 Coras1c710452017-10-17 00:03:13 -070045u8 *
46format_transport_proto (u8 * s, va_list * args)
47{
48 u32 transport_proto = va_arg (*args, u32);
49 switch (transport_proto)
50 {
Florin Coras5bb23ec2019-08-31 09:45:13 -070051#define _(sym, str, sstr) \
52 case TRANSPORT_PROTO_ ## sym: \
53 s = format (s, str); \
Florin Coras1c710452017-10-17 00:03:13 -070054 break;
Florin Coras5bb23ec2019-08-31 09:45:13 -070055 foreach_transport_proto
56#undef _
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020057 default:
58 s = format (s, "UNKNOWN");
59 break;
Florin Coras1c710452017-10-17 00:03:13 -070060 }
61 return s;
62}
63
Florin Coras561af9b2017-12-09 10:19:43 -080064u8 *
65format_transport_proto_short (u8 * s, va_list * args)
66{
67 u32 transport_proto = va_arg (*args, u32);
68 switch (transport_proto)
69 {
Florin Coras5bb23ec2019-08-31 09:45:13 -070070#define _(sym, str, sstr) \
71 case TRANSPORT_PROTO_ ## sym: \
72 s = format (s, sstr); \
Florin Coras561af9b2017-12-09 10:19:43 -080073 break;
Florin Coras5bb23ec2019-08-31 09:45:13 -070074 foreach_transport_proto
75#undef _
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +020076 default:
77 s = format (s, "?");
78 break;
Florin Coras561af9b2017-12-09 10:19:43 -080079 }
80 return s;
81}
82
Florin Corasde9a8492018-10-24 22:18:58 -070083u8 *
84format_transport_connection (u8 * s, va_list * args)
85{
86 u32 transport_proto = va_arg (*args, u32);
87 u32 conn_index = va_arg (*args, u32);
88 u32 thread_index = va_arg (*args, u32);
89 u32 verbose = va_arg (*args, u32);
90 transport_proto_vft_t *tp_vft;
91 transport_connection_t *tc;
92 u32 indent;
93
94 tp_vft = transport_protocol_get_vft (transport_proto);
95 if (!tp_vft)
96 return s;
97
98 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
99 verbose);
100 tc = tp_vft->get_connection (conn_index, thread_index);
101 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
102 {
103 indent = format_get_indent (s) + 1;
104 s = format (s, "%Upacer: %U\n", format_white_space, indent,
Florin Corasa8e71c82019-10-22 19:01:39 -0700105 format_transport_pacer, &tc->pacer, tc->thread_index);
Florin Corasde9a8492018-10-24 22:18:58 -0700106 }
107 return s;
108}
109
110u8 *
111format_transport_listen_connection (u8 * s, va_list * args)
112{
113 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700114 transport_proto_vft_t *tp_vft;
115
116 tp_vft = transport_protocol_get_vft (transport_proto);
117 if (!tp_vft)
118 return s;
119
Florin Coras3389dd22019-02-01 18:00:05 -0800120 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700121 return s;
122}
123
124u8 *
125format_transport_half_open_connection (u8 * s, va_list * args)
126{
127 u32 transport_proto = va_arg (*args, u32);
128 u32 listen_index = va_arg (*args, u32);
129 transport_proto_vft_t *tp_vft;
130
131 tp_vft = transport_protocol_get_vft (transport_proto);
132 if (!tp_vft)
133 return s;
134
135 s = format (s, "%U", tp_vft->format_half_open, listen_index);
136 return s;
137}
138
Florin Coras1c710452017-10-17 00:03:13 -0700139uword
140unformat_transport_proto (unformat_input_t * input, va_list * args)
141{
142 u32 *proto = va_arg (*args, u32 *);
Florin Coras5bb23ec2019-08-31 09:45:13 -0700143
144#define _(sym, str, sstr) \
145 if (unformat (input, str)) \
146 { \
147 *proto = TRANSPORT_PROTO_ ## sym; \
148 return 1; \
149 }
150 foreach_transport_proto
151#undef _
Florin Coras1c710452017-10-17 00:03:13 -0700152 return 0;
Florin Coras1c710452017-10-17 00:03:13 -0700153}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700154
155u32
156transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
157 ip46_address_t * ip, u16 port)
158{
159 clib_bihash_kv_24_8_t kv;
160 int rv;
161
162 kv.key[0] = ip->as_u64[0];
163 kv.key[1] = ip->as_u64[1];
164 kv.key[2] = (u64) port << 8 | (u64) proto;
165
166 rv = clib_bihash_search_inline_24_8 (ht, &kv);
167 if (rv == 0)
168 return kv.value;
169
170 return ENDPOINT_INVALID_INDEX;
171}
172
173void
174transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
175 transport_endpoint_t * te, u32 value)
176{
177 clib_bihash_kv_24_8_t kv;
178
179 kv.key[0] = te->ip.as_u64[0];
180 kv.key[1] = te->ip.as_u64[1];
181 kv.key[2] = (u64) te->port << 8 | (u64) proto;
182 kv.value = value;
183
184 clib_bihash_add_del_24_8 (ht, &kv, 1);
185}
186
187void
188transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
189 transport_endpoint_t * te)
190{
191 clib_bihash_kv_24_8_t kv;
192
193 kv.key[0] = te->ip.as_u64[0];
194 kv.key[1] = te->ip.as_u64[1];
195 kv.key[2] = (u64) te->port << 8 | (u64) proto;
196
197 clib_bihash_add_del_24_8 (ht, &kv, 0);
198}
199
200/**
201 * Register transport virtual function table.
202 *
Florin Coras561af9b2017-12-09 10:19:43 -0800203 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
204 * @param vft - virtual function table for transport proto
205 * @param fib_proto - network layer protocol
206 * @param output_node - output node index that session layer will hand off
207 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700208 */
209void
Florin Coras561af9b2017-12-09 10:19:43 -0800210transport_register_protocol (transport_proto_t transport_proto,
211 const transport_proto_vft_t * vft,
212 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700213{
Florin Coras561af9b2017-12-09 10:19:43 -0800214 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700215
Florin Coras561af9b2017-12-09 10:19:43 -0800216 vec_validate (tp_vfts, transport_proto);
217 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700218
Florin Coras561af9b2017-12-09 10:19:43 -0800219 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700220}
221
222/**
223 * Get transport virtual function table
224 *
225 * @param type - session type (not protocol type)
226 */
227transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800228transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700229{
Florin Coras561af9b2017-12-09 10:19:43 -0800230 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700231 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800232 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700233}
234
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200235u8
236transport_half_open_has_fifos (transport_proto_t tp)
237{
238 return tp_vfts[tp].transport_options.half_open_has_fifos;
239}
240
Florin Coras7fb0fe12018-04-09 09:24:52 -0700241transport_service_type_t
242transport_protocol_service_type (transport_proto_t tp)
243{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200244 return tp_vfts[tp].transport_options.service_type;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700245}
246
Florin Corasf08f26d2018-05-10 13:20:47 -0700247transport_tx_fn_type_t
248transport_protocol_tx_fn_type (transport_proto_t tp)
249{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200250 return tp_vfts[tp].transport_options.tx_type;
Florin Corasf08f26d2018-05-10 13:20:47 -0700251}
252
Florin Coras1ee78302019-02-05 15:51:15 -0800253void
254transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800255{
Florin Coras1ee78302019-02-05 15:51:15 -0800256 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800257}
258
Florin Coras1ee78302019-02-05 15:51:15 -0800259int
260transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800261{
Florin Coras1ee78302019-02-05 15:51:15 -0800262 return tp_vfts[tp].connect (tep);
263}
264
265void
266transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
267{
268 tp_vfts[tp].close (conn_index, thread_index);
269}
270
Florin Corasdfb3b872019-08-16 17:48:44 -0700271void
272transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
273{
274 if (tp_vfts[tp].reset)
275 tp_vfts[tp].reset (conn_index, thread_index);
276 else
277 tp_vfts[tp].close (conn_index, thread_index);
278}
279
Florin Coras1ee78302019-02-05 15:51:15 -0800280u32
281transport_start_listen (transport_proto_t tp, u32 session_index,
282 transport_endpoint_t * tep)
283{
284 return tp_vfts[tp].start_listen (session_index, tep);
285}
286
287u32
288transport_stop_listen (transport_proto_t tp, u32 conn_index)
289{
290 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800291}
292
Florin Coras40903ac2018-06-10 14:41:23 -0700293u8
294transport_protocol_is_cl (transport_proto_t tp)
295{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200296 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
Florin Coras40903ac2018-06-10 14:41:23 -0700297}
298
Aloys Augustincdb71702019-04-08 17:54:39 +0200299always_inline void
300default_get_transport_endpoint (transport_connection_t * tc,
Florin Coras09d18c22019-04-24 11:10:02 -0700301 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200302{
303 if (is_lcl)
304 {
Florin Coras09d18c22019-04-24 11:10:02 -0700305 tep->port = tc->lcl_port;
306 tep->is_ip4 = tc->is_ip4;
307 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200308 }
309 else
310 {
Florin Coras09d18c22019-04-24 11:10:02 -0700311 tep->port = tc->rmt_port;
312 tep->is_ip4 = tc->is_ip4;
313 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200314 }
315}
316
317void
318transport_get_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700319 u32 thread_index, transport_endpoint_t * tep,
320 u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200321{
322 if (tp_vfts[tp].get_transport_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700323 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
324 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200325 else
326 {
327 transport_connection_t *tc;
328 tc = transport_get_connection (tp, conn_index, thread_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700329 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200330 }
331}
332
333void
334transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700335 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200336{
337 if (tp_vfts[tp].get_transport_listener_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700338 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200339 else
340 {
341 transport_connection_t *tc;
342 tc = transport_get_listener (tp, conn_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700343 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200344 }
345}
346
Florin Coras3cbc04b2017-10-02 00:18:51 -0700347#define PORT_MASK ((1 << 16)- 1)
348
349void
350transport_endpoint_del (u32 tepi)
351{
352 clib_spinlock_lock_if_init (&local_endpoints_lock);
353 pool_put_index (local_endpoints, tepi);
354 clib_spinlock_unlock_if_init (&local_endpoints_lock);
355}
356
357always_inline transport_endpoint_t *
358transport_endpoint_new (void)
359{
360 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700361 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700362 return tep;
363}
364
365void
366transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
367{
368 u32 tepi;
369 transport_endpoint_t *tep;
370
371 /* Cleanup local endpoint if this was an active connect */
372 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
373 clib_net_to_host_u16 (port));
374 if (tepi != ENDPOINT_INVALID_INDEX)
375 {
376 tep = pool_elt_at_index (local_endpoints, tepi);
377 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
378 transport_endpoint_del (tepi);
379 }
380}
381
Florin Coras5665ced2018-10-25 18:03:45 -0700382static void
383transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
384{
385 transport_endpoint_t *tep;
386 clib_spinlock_lock_if_init (&local_endpoints_lock);
387 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500388 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700389 tep->port = port;
390 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
391 tep - local_endpoints);
392 clib_spinlock_unlock_if_init (&local_endpoints_lock);
393}
394
Florin Coras3cbc04b2017-10-02 00:18:51 -0700395/**
396 * Allocate local port and add if successful add entry to local endpoint
397 * table to mark the pair as used.
398 */
399int
400transport_alloc_local_port (u8 proto, ip46_address_t * ip)
401{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700402 u16 min = 1024, max = 65535; /* XXX configurable ? */
403 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700404 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700405
406 limit = max - min;
407
408 /* Only support active opens from thread 0 */
409 ASSERT (vlib_get_thread_index () == 0);
410
411 /* Search for first free slot */
412 for (tries = 0; tries < limit; tries++)
413 {
414 u16 port = 0;
415
416 /* Find a port in the specified range */
417 while (1)
418 {
419 port = random_u32 (&port_allocator_seed) & PORT_MASK;
420 if (PREDICT_TRUE (port >= min && port < max))
421 break;
422 }
423
424 /* Look it up. If not found, we're done */
425 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
426 port);
427 if (tei == ENDPOINT_INVALID_INDEX)
428 {
Florin Coras5665ced2018-10-25 18:03:45 -0700429 transport_endpoint_mark_used (proto, ip, port);
430 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700431 }
432 }
433 return -1;
434}
435
Florin Coras5665ced2018-10-25 18:03:45 -0700436static clib_error_t *
437transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700438{
Florin Coras5665ced2018-10-25 18:03:45 -0700439 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700440 {
441 ip4_address_t *ip4;
442 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800443 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700444 return clib_error_return (0, "no routable ip4 address on %U",
445 format_vnet_sw_if_index_name,
446 vnet_get_main (), sw_if_index);
447 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700448 }
449 else
450 {
451 ip6_address_t *ip6;
452 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
453 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700454 return clib_error_return (0, "no routable ip6 addresses on %U",
455 format_vnet_sw_if_index_name,
456 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500457 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700458 }
459 return 0;
460}
461
462static clib_error_t *
463transport_find_local_ip_for_remote (u32 sw_if_index,
464 transport_endpoint_t * rmt,
465 ip46_address_t * lcl_addr)
466{
467 fib_node_index_t fei;
468 fib_prefix_t prefix;
469
470 if (sw_if_index == ENDPOINT_INVALID_INDEX)
471 {
472 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500473 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700474 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
475 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
476
477 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
478 fei = fib_table_lookup (rmt->fib_index, &prefix);
479
480 /* Couldn't find route to destination. Bail out. */
481 if (fei == FIB_NODE_INDEX_INVALID)
482 return clib_error_return (0, "no route to %U", format_ip46_address,
483 &rmt->ip, (rmt->is_ip4 == 0) + 1);
484
485 sw_if_index = fib_entry_get_resolving_interface (fei);
486 if (sw_if_index == ENDPOINT_INVALID_INDEX)
487 return clib_error_return (0, "no resolving interface for %U",
488 format_ip46_address, &rmt->ip,
489 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700490 }
491
Florin Coras5665ced2018-10-25 18:03:45 -0700492 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
493 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
494}
495
496int
497transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
498 ip46_address_t * lcl_addr, u16 * lcl_port)
499{
500 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
501 clib_error_t *error;
502 int port;
503 u32 tei;
504
505 /*
506 * Find the local address
507 */
508 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700509 {
Florin Coras5665ced2018-10-25 18:03:45 -0700510 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
511 rmt, lcl_addr);
512 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800513 {
514 clib_error_report (error);
515 return -1;
516 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700517 }
Florin Coras5665ced2018-10-25 18:03:45 -0700518 else
519 {
520 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500521 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
522 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700523 }
524
525 /*
526 * Allocate source port
527 */
528 if (rmt_cfg->peer.port == 0)
529 {
530 port = transport_alloc_local_port (proto, lcl_addr);
531 if (port < 1)
532 {
533 clib_warning ("Failed to allocate src port");
534 return -1;
535 }
536 *lcl_port = port;
537 }
538 else
539 {
540 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
541 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
542 lcl_addr, port);
543 if (tei != ENDPOINT_INVALID_INDEX)
544 return -1;
545
546 transport_endpoint_mark_used (proto, lcl_addr, port);
547 *lcl_port = port;
548 }
549
Florin Coras3cbc04b2017-10-02 00:18:51 -0700550 return 0;
551}
552
Florin Corasa8e71c82019-10-22 19:01:39 -0700553u8 *
554format_clib_us_time (u8 * s, va_list * args)
555{
556 clib_us_time_t t = va_arg (*args, clib_us_time_t);
557 if (t < 1e3)
558 s = format (s, "%u us", t);
559 else
560 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
561 return s;
562}
Florin Corasd67f1122018-05-21 17:47:40 -0700563
564u8 *
565format_transport_pacer (u8 * s, va_list * args)
566{
567 spacer_t *pacer = va_arg (*args, spacer_t *);
Florin Corasa8e71c82019-10-22 19:01:39 -0700568 u32 thread_index = va_arg (*args, int);
569 clib_us_time_t now, diff;
Florin Corasd67f1122018-05-21 17:47:40 -0700570
Florin Corasa8e71c82019-10-22 19:01:39 -0700571 now = transport_us_time_now (thread_index);
572 diff = now - pacer->last_update;
573 s = format (s, "rate %lu bucket %lu t/p %.3f last_update %U",
Florin Corasbe237bf2019-09-27 08:16:40 -0700574 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
Florin Corasa8e71c82019-10-22 19:01:39 -0700575 format_clib_us_time, diff);
Florin Corasd67f1122018-05-21 17:47:40 -0700576 return s;
577}
578
579static inline u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700580spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700581{
Florin Corasa8e71c82019-10-22 19:01:39 -0700582 u64 n_periods = (time_now - pacer->last_update);
Florin Corase55a6d72018-10-31 23:09:22 -0700583 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700584
Florin Corasa8e71c82019-10-22 19:01:39 -0700585 if (PREDICT_FALSE (n_periods > 5e4))
Florin Corasc31dc312019-10-06 14:06:14 -0700586 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700587 pacer->last_update = time_now;
Florin Corasc31dc312019-10-06 14:06:14 -0700588 pacer->bucket = TRANSPORT_PACER_MIN_BURST;
589 return TRANSPORT_PACER_MIN_BURST;
590 }
591
Florin Coras36ebcff2019-09-12 18:36:44 -0700592 if (n_periods > 0
593 && (inc = (f32) n_periods * pacer->tokens_per_period) > 10)
Florin Corase55a6d72018-10-31 23:09:22 -0700594 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700595 pacer->last_update = time_now;
Florin Coras7c8f8282019-09-15 16:28:45 -0700596 pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
Florin Corase55a6d72018-10-31 23:09:22 -0700597 }
598
Florin Coras6bc6fd02019-03-27 18:55:11 -0700599 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700600}
601
602static inline void
603spacer_update_bucket (spacer_t * pacer, u32 bytes)
604{
605 ASSERT (pacer->bucket >= bytes);
606 pacer->bucket -= bytes;
607}
608
609static inline void
Florin Corasd67f1122018-05-21 17:47:40 -0700610spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
611{
612 ASSERT (rate_bytes_per_sec != 0);
Florin Coras7c8f8282019-09-15 16:28:45 -0700613 pacer->bytes_per_sec = rate_bytes_per_sec;
Florin Corasa8e71c82019-10-22 19:01:39 -0700614 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
Florin Corasd67f1122018-05-21 17:47:40 -0700615}
616
Florin Coras52814732019-06-12 15:38:19 -0700617static inline u64
618spacer_pace_rate (spacer_t * pacer)
619{
Florin Coras7c8f8282019-09-15 16:28:45 -0700620 return pacer->bytes_per_sec;
Florin Coras52814732019-06-12 15:38:19 -0700621}
622
Florin Coras36ebcff2019-09-12 18:36:44 -0700623static inline void
Florin Corasa8e71c82019-10-22 19:01:39 -0700624spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
Florin Coras36ebcff2019-09-12 18:36:44 -0700625{
Florin Corasa8e71c82019-10-22 19:01:39 -0700626 pacer->last_update = time_now;
627 pacer->bucket = bucket;
Florin Coras36ebcff2019-09-12 18:36:44 -0700628}
629
Florin Corasd67f1122018-05-21 17:47:40 -0700630void
Florin Corasc44a5582018-11-01 16:30:54 -0700631transport_connection_tx_pacer_reset (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700632 u64 rate_bytes_per_sec, u32 start_bucket)
Florin Corasd67f1122018-05-21 17:47:40 -0700633{
Florin Corasd67f1122018-05-21 17:47:40 -0700634 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
Florin Corasa8e71c82019-10-22 19:01:39 -0700635 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
636 start_bucket);
637}
638
639void
640transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc)
641{
642 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), 0);
Florin Corasc44a5582018-11-01 16:30:54 -0700643}
644
645void
646transport_connection_tx_pacer_init (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700647 u64 rate_bytes_per_sec,
Florin Corasc44a5582018-11-01 16:30:54 -0700648 u32 initial_bucket)
649{
Florin Corasc44a5582018-11-01 16:30:54 -0700650 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
651 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
Florin Corasa8e71c82019-10-22 19:01:39 -0700652 initial_bucket);
Florin Corasd67f1122018-05-21 17:47:40 -0700653}
654
655void
656transport_connection_tx_pacer_update (transport_connection_t * tc,
657 u64 bytes_per_sec)
658{
Florin Corasd67f1122018-05-21 17:47:40 -0700659 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
Florin Corasd67f1122018-05-21 17:47:40 -0700660}
661
662u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700663transport_connection_tx_pacer_burst (transport_connection_t * tc)
Florin Corase55a6d72018-10-31 23:09:22 -0700664{
Florin Corasa8e71c82019-10-22 19:01:39 -0700665 return spacer_max_burst (&tc->pacer,
666 transport_us_time_now (tc->thread_index));
Florin Coras36ebcff2019-09-12 18:36:44 -0700667}
668
Florin Corase55a6d72018-10-31 23:09:22 -0700669u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700670transport_connection_snd_space (transport_connection_t * tc, u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700671{
672 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700673
674 snd_space = tp_vfts[tc->proto].send_space (tc);
Florin Corasc31dc312019-10-06 14:06:14 -0700675 if (snd_space && transport_connection_is_tx_paced (tc))
Florin Corasd67f1122018-05-21 17:47:40 -0700676 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700677 clib_us_time_t now = transport_us_time_now (tc->thread_index);
678 max_paced_burst = spacer_max_burst (&tc->pacer, now);
Simon Zhang1146ff42019-09-02 22:54:00 +0800679 max_paced_burst =
680 (max_paced_burst < TRANSPORT_PACER_MIN_BURST) ? 0 : max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700681 snd_space = clib_min (snd_space, max_paced_burst);
Simon Zhang1146ff42019-09-02 22:54:00 +0800682 return snd_space >= mss ? snd_space - snd_space % mss : snd_space;
Florin Corasd67f1122018-05-21 17:47:40 -0700683 }
684 return snd_space;
685}
686
Florin Coras52814732019-06-12 15:38:19 -0700687u64
688transport_connection_tx_pacer_rate (transport_connection_t * tc)
689{
690 return spacer_pace_rate (&tc->pacer);
691}
692
Florin Corasd67f1122018-05-21 17:47:40 -0700693void
Florin Coras00482232019-08-02 12:52:00 -0700694transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
Florin Corasd67f1122018-05-21 17:47:40 -0700695{
Florin Corasd67f1122018-05-21 17:47:40 -0700696 if (transport_connection_is_tx_paced (tc))
697 spacer_update_bucket (&tc->pacer, bytes);
698}
699
700void
Florin Corase55a6d72018-10-31 23:09:22 -0700701transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
702 u32 bytes)
703{
704 spacer_update_bucket (&tc->pacer, bytes);
705}
706
707void
Florin Corasa8e71c82019-10-22 19:01:39 -0700708transport_update_time (clib_time_type_t time_now, u8 thread_index)
Florin Coras561af9b2017-12-09 10:19:43 -0800709{
710 transport_proto_vft_t *vft;
711 vec_foreach (vft, tp_vfts)
712 {
713 if (vft->update_time)
714 (vft->update_time) (time_now, thread_index);
715 }
716}
717
718void
719transport_enable_disable (vlib_main_t * vm, u8 is_en)
720{
721 transport_proto_vft_t *vft;
722 vec_foreach (vft, tp_vfts)
723 {
724 if (vft->enable)
725 (vft->enable) (vm, is_en);
726 }
727}
728
729void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700730transport_init (void)
731{
732 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800733 session_main_t *smm = vnet_get_session_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700734 u32 num_threads;
735
Florin Coras93e65802017-11-29 00:07:11 -0500736 if (smm->local_endpoints_table_buckets == 0)
737 smm->local_endpoints_table_buckets = 250000;
738 if (smm->local_endpoints_table_memory == 0)
739 smm->local_endpoints_table_memory = 512 << 20;
740
Florin Coras3cbc04b2017-10-02 00:18:51 -0700741 /* Initialize [port-allocator] random number seed */
742 port_allocator_seed = (u32) clib_cpu_time_now ();
743
744 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500745 smm->local_endpoints_table_buckets,
746 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700747 num_threads = 1 /* main thread */ + vtm->n_threads;
748 if (num_threads > 1)
749 clib_spinlock_init (&local_endpoints_lock);
750}
751
752/*
753 * fd.io coding-style-patch-verification: ON
754 *
755 * Local Variables:
756 * eval: (c-set-style "gnu")
757 * End:
758 */