blob: 902c7400af9b1716c5464ded5d94538432b1bfbd [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 Coras3bbbf0d2019-11-19 17:23:22 -0800139static u8
140unformat_transport_str_match (unformat_input_t * input, const char *str)
141{
142 int i;
143
144 if (strlen (str) > vec_len (input->buffer) - input->index)
145 return 0;
146
147 for (i = 0; i < strlen (str); i++)
148 {
149 if (input->buffer[i + input->index] != str[i])
150 return 0;
151 }
152 return 1;
153}
154
Florin Coras1c710452017-10-17 00:03:13 -0700155uword
156unformat_transport_proto (unformat_input_t * input, va_list * args)
157{
158 u32 *proto = va_arg (*args, u32 *);
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800159 u8 longest_match = 0, match;
160 char *str_match = 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700161
162#define _(sym, str, sstr) \
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800163 if (unformat_transport_str_match (input, str)) \
Florin Coras5bb23ec2019-08-31 09:45:13 -0700164 { \
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800165 match = strlen (str); \
166 if (match > longest_match) \
167 { \
168 *proto = TRANSPORT_PROTO_ ## sym; \
169 longest_match = match; \
170 str_match = str; \
171 } \
Florin Coras5bb23ec2019-08-31 09:45:13 -0700172 }
173 foreach_transport_proto
174#undef _
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800175 if (longest_match)
176 {
177 unformat (input, str_match);
178 return 1;
179 }
180
181 return 0;
Florin Coras1c710452017-10-17 00:03:13 -0700182}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700183
184u32
185transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
186 ip46_address_t * ip, u16 port)
187{
188 clib_bihash_kv_24_8_t kv;
189 int rv;
190
191 kv.key[0] = ip->as_u64[0];
192 kv.key[1] = ip->as_u64[1];
193 kv.key[2] = (u64) port << 8 | (u64) proto;
194
195 rv = clib_bihash_search_inline_24_8 (ht, &kv);
196 if (rv == 0)
197 return kv.value;
198
199 return ENDPOINT_INVALID_INDEX;
200}
201
202void
203transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
204 transport_endpoint_t * te, u32 value)
205{
206 clib_bihash_kv_24_8_t kv;
207
208 kv.key[0] = te->ip.as_u64[0];
209 kv.key[1] = te->ip.as_u64[1];
210 kv.key[2] = (u64) te->port << 8 | (u64) proto;
211 kv.value = value;
212
213 clib_bihash_add_del_24_8 (ht, &kv, 1);
214}
215
216void
217transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
218 transport_endpoint_t * te)
219{
220 clib_bihash_kv_24_8_t kv;
221
222 kv.key[0] = te->ip.as_u64[0];
223 kv.key[1] = te->ip.as_u64[1];
224 kv.key[2] = (u64) te->port << 8 | (u64) proto;
225
226 clib_bihash_add_del_24_8 (ht, &kv, 0);
227}
228
229/**
230 * Register transport virtual function table.
231 *
Florin Coras561af9b2017-12-09 10:19:43 -0800232 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
233 * @param vft - virtual function table for transport proto
234 * @param fib_proto - network layer protocol
235 * @param output_node - output node index that session layer will hand off
236 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700237 */
238void
Florin Coras561af9b2017-12-09 10:19:43 -0800239transport_register_protocol (transport_proto_t transport_proto,
240 const transport_proto_vft_t * vft,
241 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700242{
Florin Coras561af9b2017-12-09 10:19:43 -0800243 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700244
Florin Coras561af9b2017-12-09 10:19:43 -0800245 vec_validate (tp_vfts, transport_proto);
246 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700247
Florin Coras561af9b2017-12-09 10:19:43 -0800248 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700249}
250
251/**
252 * Get transport virtual function table
253 *
254 * @param type - session type (not protocol type)
255 */
256transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800257transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700258{
Florin Coras561af9b2017-12-09 10:19:43 -0800259 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700260 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800261 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700262}
263
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200264u8
265transport_half_open_has_fifos (transport_proto_t tp)
266{
267 return tp_vfts[tp].transport_options.half_open_has_fifos;
268}
269
Florin Coras7fb0fe12018-04-09 09:24:52 -0700270transport_service_type_t
271transport_protocol_service_type (transport_proto_t tp)
272{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200273 return tp_vfts[tp].transport_options.service_type;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700274}
275
Florin Corasf08f26d2018-05-10 13:20:47 -0700276transport_tx_fn_type_t
277transport_protocol_tx_fn_type (transport_proto_t tp)
278{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200279 return tp_vfts[tp].transport_options.tx_type;
Florin Corasf08f26d2018-05-10 13:20:47 -0700280}
281
Florin Coras1ee78302019-02-05 15:51:15 -0800282void
283transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800284{
Florin Coras1ee78302019-02-05 15:51:15 -0800285 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800286}
287
Florin Coras1ee78302019-02-05 15:51:15 -0800288int
289transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800290{
Florin Coras1ee78302019-02-05 15:51:15 -0800291 return tp_vfts[tp].connect (tep);
292}
293
294void
295transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
296{
297 tp_vfts[tp].close (conn_index, thread_index);
298}
299
Florin Corasdfb3b872019-08-16 17:48:44 -0700300void
301transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
302{
303 if (tp_vfts[tp].reset)
304 tp_vfts[tp].reset (conn_index, thread_index);
305 else
306 tp_vfts[tp].close (conn_index, thread_index);
307}
308
Florin Coras1ee78302019-02-05 15:51:15 -0800309u32
310transport_start_listen (transport_proto_t tp, u32 session_index,
311 transport_endpoint_t * tep)
312{
313 return tp_vfts[tp].start_listen (session_index, tep);
314}
315
316u32
317transport_stop_listen (transport_proto_t tp, u32 conn_index)
318{
319 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800320}
321
Florin Coras40903ac2018-06-10 14:41:23 -0700322u8
323transport_protocol_is_cl (transport_proto_t tp)
324{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200325 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
Florin Coras40903ac2018-06-10 14:41:23 -0700326}
327
Aloys Augustincdb71702019-04-08 17:54:39 +0200328always_inline void
329default_get_transport_endpoint (transport_connection_t * tc,
Florin Coras09d18c22019-04-24 11:10:02 -0700330 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200331{
332 if (is_lcl)
333 {
Florin Coras09d18c22019-04-24 11:10:02 -0700334 tep->port = tc->lcl_port;
335 tep->is_ip4 = tc->is_ip4;
336 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200337 }
338 else
339 {
Florin Coras09d18c22019-04-24 11:10:02 -0700340 tep->port = tc->rmt_port;
341 tep->is_ip4 = tc->is_ip4;
342 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200343 }
344}
345
346void
347transport_get_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700348 u32 thread_index, transport_endpoint_t * tep,
349 u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200350{
351 if (tp_vfts[tp].get_transport_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700352 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
353 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200354 else
355 {
356 transport_connection_t *tc;
357 tc = transport_get_connection (tp, conn_index, thread_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700358 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200359 }
360}
361
362void
363transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700364 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200365{
366 if (tp_vfts[tp].get_transport_listener_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700367 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200368 else
369 {
370 transport_connection_t *tc;
371 tc = transport_get_listener (tp, conn_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700372 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200373 }
374}
375
Florin Coras3cbc04b2017-10-02 00:18:51 -0700376#define PORT_MASK ((1 << 16)- 1)
377
378void
379transport_endpoint_del (u32 tepi)
380{
381 clib_spinlock_lock_if_init (&local_endpoints_lock);
382 pool_put_index (local_endpoints, tepi);
383 clib_spinlock_unlock_if_init (&local_endpoints_lock);
384}
385
386always_inline transport_endpoint_t *
387transport_endpoint_new (void)
388{
389 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700390 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700391 return tep;
392}
393
394void
395transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
396{
397 u32 tepi;
398 transport_endpoint_t *tep;
399
400 /* Cleanup local endpoint if this was an active connect */
401 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
402 clib_net_to_host_u16 (port));
403 if (tepi != ENDPOINT_INVALID_INDEX)
404 {
405 tep = pool_elt_at_index (local_endpoints, tepi);
406 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
407 transport_endpoint_del (tepi);
408 }
409}
410
Florin Coras5665ced2018-10-25 18:03:45 -0700411static void
412transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
413{
414 transport_endpoint_t *tep;
415 clib_spinlock_lock_if_init (&local_endpoints_lock);
416 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500417 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700418 tep->port = port;
419 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
420 tep - local_endpoints);
421 clib_spinlock_unlock_if_init (&local_endpoints_lock);
422}
423
Florin Coras3cbc04b2017-10-02 00:18:51 -0700424/**
425 * Allocate local port and add if successful add entry to local endpoint
426 * table to mark the pair as used.
427 */
428int
429transport_alloc_local_port (u8 proto, ip46_address_t * ip)
430{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700431 u16 min = 1024, max = 65535; /* XXX configurable ? */
432 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700433 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700434
435 limit = max - min;
436
437 /* Only support active opens from thread 0 */
438 ASSERT (vlib_get_thread_index () == 0);
439
440 /* Search for first free slot */
441 for (tries = 0; tries < limit; tries++)
442 {
443 u16 port = 0;
444
445 /* Find a port in the specified range */
446 while (1)
447 {
448 port = random_u32 (&port_allocator_seed) & PORT_MASK;
449 if (PREDICT_TRUE (port >= min && port < max))
450 break;
451 }
452
453 /* Look it up. If not found, we're done */
454 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
455 port);
456 if (tei == ENDPOINT_INVALID_INDEX)
457 {
Florin Coras5665ced2018-10-25 18:03:45 -0700458 transport_endpoint_mark_used (proto, ip, port);
459 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700460 }
461 }
462 return -1;
463}
464
Florin Coras5665ced2018-10-25 18:03:45 -0700465static clib_error_t *
466transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700467{
Florin Coras5665ced2018-10-25 18:03:45 -0700468 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700469 {
470 ip4_address_t *ip4;
471 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800472 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700473 return clib_error_return (0, "no routable ip4 address on %U",
474 format_vnet_sw_if_index_name,
475 vnet_get_main (), sw_if_index);
476 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700477 }
478 else
479 {
480 ip6_address_t *ip6;
481 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
482 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700483 return clib_error_return (0, "no routable ip6 addresses on %U",
484 format_vnet_sw_if_index_name,
485 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500486 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700487 }
488 return 0;
489}
490
491static clib_error_t *
492transport_find_local_ip_for_remote (u32 sw_if_index,
493 transport_endpoint_t * rmt,
494 ip46_address_t * lcl_addr)
495{
496 fib_node_index_t fei;
497 fib_prefix_t prefix;
498
499 if (sw_if_index == ENDPOINT_INVALID_INDEX)
500 {
501 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500502 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700503 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
504 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
505
506 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
507 fei = fib_table_lookup (rmt->fib_index, &prefix);
508
509 /* Couldn't find route to destination. Bail out. */
510 if (fei == FIB_NODE_INDEX_INVALID)
511 return clib_error_return (0, "no route to %U", format_ip46_address,
512 &rmt->ip, (rmt->is_ip4 == 0) + 1);
513
514 sw_if_index = fib_entry_get_resolving_interface (fei);
515 if (sw_if_index == ENDPOINT_INVALID_INDEX)
516 return clib_error_return (0, "no resolving interface for %U",
517 format_ip46_address, &rmt->ip,
518 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700519 }
520
Florin Coras5665ced2018-10-25 18:03:45 -0700521 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
522 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
523}
524
525int
526transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
527 ip46_address_t * lcl_addr, u16 * lcl_port)
528{
529 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
530 clib_error_t *error;
531 int port;
532 u32 tei;
533
534 /*
535 * Find the local address
536 */
537 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700538 {
Florin Coras5665ced2018-10-25 18:03:45 -0700539 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
540 rmt, lcl_addr);
541 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800542 {
543 clib_error_report (error);
544 return -1;
545 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700546 }
Florin Coras5665ced2018-10-25 18:03:45 -0700547 else
548 {
549 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500550 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
551 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700552 }
553
554 /*
555 * Allocate source port
556 */
557 if (rmt_cfg->peer.port == 0)
558 {
559 port = transport_alloc_local_port (proto, lcl_addr);
560 if (port < 1)
561 {
562 clib_warning ("Failed to allocate src port");
563 return -1;
564 }
565 *lcl_port = port;
566 }
567 else
568 {
569 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
570 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
571 lcl_addr, port);
572 if (tei != ENDPOINT_INVALID_INDEX)
573 return -1;
574
575 transport_endpoint_mark_used (proto, lcl_addr, port);
576 *lcl_port = port;
577 }
578
Florin Coras3cbc04b2017-10-02 00:18:51 -0700579 return 0;
580}
581
Florin Corasa8e71c82019-10-22 19:01:39 -0700582u8 *
583format_clib_us_time (u8 * s, va_list * args)
584{
585 clib_us_time_t t = va_arg (*args, clib_us_time_t);
586 if (t < 1e3)
587 s = format (s, "%u us", t);
588 else
589 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
590 return s;
591}
Florin Corasd67f1122018-05-21 17:47:40 -0700592
593u8 *
594format_transport_pacer (u8 * s, va_list * args)
595{
596 spacer_t *pacer = va_arg (*args, spacer_t *);
Florin Corasa8e71c82019-10-22 19:01:39 -0700597 u32 thread_index = va_arg (*args, int);
598 clib_us_time_t now, diff;
Florin Corasd67f1122018-05-21 17:47:40 -0700599
Florin Corasa8e71c82019-10-22 19:01:39 -0700600 now = transport_us_time_now (thread_index);
601 diff = now - pacer->last_update;
602 s = format (s, "rate %lu bucket %lu t/p %.3f last_update %U",
Florin Corasbe237bf2019-09-27 08:16:40 -0700603 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
Florin Corasa8e71c82019-10-22 19:01:39 -0700604 format_clib_us_time, diff);
Florin Corasd67f1122018-05-21 17:47:40 -0700605 return s;
606}
607
608static inline u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700609spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700610{
Florin Corasa8e71c82019-10-22 19:01:39 -0700611 u64 n_periods = (time_now - pacer->last_update);
Florin Corase55a6d72018-10-31 23:09:22 -0700612 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700613
Florin Corasa8e71c82019-10-22 19:01:39 -0700614 if (PREDICT_FALSE (n_periods > 5e4))
Florin Corasc31dc312019-10-06 14:06:14 -0700615 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700616 pacer->last_update = time_now;
Florin Corasc31dc312019-10-06 14:06:14 -0700617 pacer->bucket = TRANSPORT_PACER_MIN_BURST;
618 return TRANSPORT_PACER_MIN_BURST;
619 }
620
Florin Coras36ebcff2019-09-12 18:36:44 -0700621 if (n_periods > 0
622 && (inc = (f32) n_periods * pacer->tokens_per_period) > 10)
Florin Corase55a6d72018-10-31 23:09:22 -0700623 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700624 pacer->last_update = time_now;
Florin Coras7c8f8282019-09-15 16:28:45 -0700625 pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
Florin Corase55a6d72018-10-31 23:09:22 -0700626 }
627
Florin Coras6bc6fd02019-03-27 18:55:11 -0700628 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700629}
630
631static inline void
632spacer_update_bucket (spacer_t * pacer, u32 bytes)
633{
634 ASSERT (pacer->bucket >= bytes);
635 pacer->bucket -= bytes;
636}
637
638static inline void
Florin Corasd67f1122018-05-21 17:47:40 -0700639spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
640{
641 ASSERT (rate_bytes_per_sec != 0);
Florin Coras7c8f8282019-09-15 16:28:45 -0700642 pacer->bytes_per_sec = rate_bytes_per_sec;
Florin Corasa8e71c82019-10-22 19:01:39 -0700643 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
Florin Corasd67f1122018-05-21 17:47:40 -0700644}
645
Florin Coras52814732019-06-12 15:38:19 -0700646static inline u64
647spacer_pace_rate (spacer_t * pacer)
648{
Florin Coras7c8f8282019-09-15 16:28:45 -0700649 return pacer->bytes_per_sec;
Florin Coras52814732019-06-12 15:38:19 -0700650}
651
Florin Coras36ebcff2019-09-12 18:36:44 -0700652static inline void
Florin Corasa8e71c82019-10-22 19:01:39 -0700653spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
Florin Coras36ebcff2019-09-12 18:36:44 -0700654{
Florin Corasa8e71c82019-10-22 19:01:39 -0700655 pacer->last_update = time_now;
656 pacer->bucket = bucket;
Florin Coras36ebcff2019-09-12 18:36:44 -0700657}
658
Florin Corasd67f1122018-05-21 17:47:40 -0700659void
Florin Corasc44a5582018-11-01 16:30:54 -0700660transport_connection_tx_pacer_reset (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700661 u64 rate_bytes_per_sec, u32 start_bucket)
Florin Corasd67f1122018-05-21 17:47:40 -0700662{
Florin Corasd67f1122018-05-21 17:47:40 -0700663 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
Florin Corasa8e71c82019-10-22 19:01:39 -0700664 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
665 start_bucket);
666}
667
668void
669transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc)
670{
671 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), 0);
Florin Corasc44a5582018-11-01 16:30:54 -0700672}
673
674void
675transport_connection_tx_pacer_init (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700676 u64 rate_bytes_per_sec,
Florin Corasc44a5582018-11-01 16:30:54 -0700677 u32 initial_bucket)
678{
Florin Corasc44a5582018-11-01 16:30:54 -0700679 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
680 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
Florin Corasa8e71c82019-10-22 19:01:39 -0700681 initial_bucket);
Florin Corasd67f1122018-05-21 17:47:40 -0700682}
683
684void
685transport_connection_tx_pacer_update (transport_connection_t * tc,
686 u64 bytes_per_sec)
687{
Florin Corasd67f1122018-05-21 17:47:40 -0700688 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
Florin Corasd67f1122018-05-21 17:47:40 -0700689}
690
691u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700692transport_connection_tx_pacer_burst (transport_connection_t * tc)
Florin Corase55a6d72018-10-31 23:09:22 -0700693{
Florin Corasa8e71c82019-10-22 19:01:39 -0700694 return spacer_max_burst (&tc->pacer,
695 transport_us_time_now (tc->thread_index));
Florin Coras36ebcff2019-09-12 18:36:44 -0700696}
697
Florin Corase55a6d72018-10-31 23:09:22 -0700698u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700699transport_connection_snd_space (transport_connection_t * tc, u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700700{
701 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700702
703 snd_space = tp_vfts[tc->proto].send_space (tc);
Florin Corasc31dc312019-10-06 14:06:14 -0700704 if (snd_space && transport_connection_is_tx_paced (tc))
Florin Corasd67f1122018-05-21 17:47:40 -0700705 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700706 clib_us_time_t now = transport_us_time_now (tc->thread_index);
707 max_paced_burst = spacer_max_burst (&tc->pacer, now);
Simon Zhang1146ff42019-09-02 22:54:00 +0800708 max_paced_burst =
709 (max_paced_burst < TRANSPORT_PACER_MIN_BURST) ? 0 : max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700710 snd_space = clib_min (snd_space, max_paced_burst);
Simon Zhang1146ff42019-09-02 22:54:00 +0800711 return snd_space >= mss ? snd_space - snd_space % mss : snd_space;
Florin Corasd67f1122018-05-21 17:47:40 -0700712 }
713 return snd_space;
714}
715
Florin Coras52814732019-06-12 15:38:19 -0700716u64
717transport_connection_tx_pacer_rate (transport_connection_t * tc)
718{
719 return spacer_pace_rate (&tc->pacer);
720}
721
Florin Corasd67f1122018-05-21 17:47:40 -0700722void
Florin Coras00482232019-08-02 12:52:00 -0700723transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
Florin Corasd67f1122018-05-21 17:47:40 -0700724{
Florin Corasd67f1122018-05-21 17:47:40 -0700725 if (transport_connection_is_tx_paced (tc))
726 spacer_update_bucket (&tc->pacer, bytes);
727}
728
729void
Florin Corase55a6d72018-10-31 23:09:22 -0700730transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
731 u32 bytes)
732{
733 spacer_update_bucket (&tc->pacer, bytes);
734}
735
736void
Florin Corasa8e71c82019-10-22 19:01:39 -0700737transport_update_time (clib_time_type_t time_now, u8 thread_index)
Florin Coras561af9b2017-12-09 10:19:43 -0800738{
739 transport_proto_vft_t *vft;
740 vec_foreach (vft, tp_vfts)
741 {
742 if (vft->update_time)
743 (vft->update_time) (time_now, thread_index);
744 }
745}
746
747void
748transport_enable_disable (vlib_main_t * vm, u8 is_en)
749{
750 transport_proto_vft_t *vft;
751 vec_foreach (vft, tp_vfts)
752 {
753 if (vft->enable)
754 (vft->enable) (vm, is_en);
755 }
756}
757
758void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700759transport_init (void)
760{
761 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800762 session_main_t *smm = vnet_get_session_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700763 u32 num_threads;
764
Florin Coras93e65802017-11-29 00:07:11 -0500765 if (smm->local_endpoints_table_buckets == 0)
766 smm->local_endpoints_table_buckets = 250000;
767 if (smm->local_endpoints_table_memory == 0)
768 smm->local_endpoints_table_memory = 512 << 20;
769
Florin Coras3cbc04b2017-10-02 00:18:51 -0700770 /* Initialize [port-allocator] random number seed */
771 port_allocator_seed = (u32) clib_cpu_time_now ();
772
773 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500774 smm->local_endpoints_table_buckets,
775 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700776 num_threads = 1 /* main thread */ + vtm->n_threads;
777 if (num_threads > 1)
778 clib_spinlock_init (&local_endpoints_lock);
779}
780
781/*
782 * fd.io coding-style-patch-verification: ON
783 *
784 * Local Variables:
785 * eval: (c-set-style "gnu")
786 * End:
787 */