blob: 16f4f02cbe30dc0acddb3dc6dc9498f73dc94dfe [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 Coras70f879d2020-03-13 17:54:42 +0000106 s = format (s, "%Utransport: flags 0x%x\n", format_white_space, indent,
107 tc->flags);
Florin Corasde9a8492018-10-24 22:18:58 -0700108 }
109 return s;
110}
111
112u8 *
113format_transport_listen_connection (u8 * s, va_list * args)
114{
115 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700116 transport_proto_vft_t *tp_vft;
117
118 tp_vft = transport_protocol_get_vft (transport_proto);
119 if (!tp_vft)
120 return s;
121
Florin Coras3389dd22019-02-01 18:00:05 -0800122 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700123 return s;
124}
125
126u8 *
127format_transport_half_open_connection (u8 * s, va_list * args)
128{
129 u32 transport_proto = va_arg (*args, u32);
130 u32 listen_index = va_arg (*args, u32);
131 transport_proto_vft_t *tp_vft;
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_half_open, listen_index);
138 return s;
139}
140
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800141static u8
142unformat_transport_str_match (unformat_input_t * input, const char *str)
143{
144 int i;
145
146 if (strlen (str) > vec_len (input->buffer) - input->index)
147 return 0;
148
149 for (i = 0; i < strlen (str); i++)
150 {
151 if (input->buffer[i + input->index] != str[i])
152 return 0;
153 }
154 return 1;
155}
156
Florin Coras1c710452017-10-17 00:03:13 -0700157uword
158unformat_transport_proto (unformat_input_t * input, va_list * args)
159{
160 u32 *proto = va_arg (*args, u32 *);
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800161 u8 longest_match = 0, match;
162 char *str_match = 0;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700163
164#define _(sym, str, sstr) \
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800165 if (unformat_transport_str_match (input, str)) \
Florin Coras5bb23ec2019-08-31 09:45:13 -0700166 { \
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800167 match = strlen (str); \
168 if (match > longest_match) \
169 { \
170 *proto = TRANSPORT_PROTO_ ## sym; \
171 longest_match = match; \
172 str_match = str; \
173 } \
Florin Coras5bb23ec2019-08-31 09:45:13 -0700174 }
175 foreach_transport_proto
176#undef _
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800177 if (longest_match)
178 {
179 unformat (input, str_match);
180 return 1;
181 }
182
183 return 0;
Florin Coras1c710452017-10-17 00:03:13 -0700184}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700185
186u32
187transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
188 ip46_address_t * ip, u16 port)
189{
190 clib_bihash_kv_24_8_t kv;
191 int rv;
192
193 kv.key[0] = ip->as_u64[0];
194 kv.key[1] = ip->as_u64[1];
195 kv.key[2] = (u64) port << 8 | (u64) proto;
196
197 rv = clib_bihash_search_inline_24_8 (ht, &kv);
198 if (rv == 0)
199 return kv.value;
200
201 return ENDPOINT_INVALID_INDEX;
202}
203
204void
205transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
206 transport_endpoint_t * te, u32 value)
207{
208 clib_bihash_kv_24_8_t kv;
209
210 kv.key[0] = te->ip.as_u64[0];
211 kv.key[1] = te->ip.as_u64[1];
212 kv.key[2] = (u64) te->port << 8 | (u64) proto;
213 kv.value = value;
214
215 clib_bihash_add_del_24_8 (ht, &kv, 1);
216}
217
218void
219transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
220 transport_endpoint_t * te)
221{
222 clib_bihash_kv_24_8_t kv;
223
224 kv.key[0] = te->ip.as_u64[0];
225 kv.key[1] = te->ip.as_u64[1];
226 kv.key[2] = (u64) te->port << 8 | (u64) proto;
227
228 clib_bihash_add_del_24_8 (ht, &kv, 0);
229}
230
231/**
232 * Register transport virtual function table.
233 *
Florin Coras561af9b2017-12-09 10:19:43 -0800234 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
235 * @param vft - virtual function table for transport proto
236 * @param fib_proto - network layer protocol
237 * @param output_node - output node index that session layer will hand off
238 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700239 */
240void
Florin Coras561af9b2017-12-09 10:19:43 -0800241transport_register_protocol (transport_proto_t transport_proto,
242 const transport_proto_vft_t * vft,
243 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700244{
Florin Coras561af9b2017-12-09 10:19:43 -0800245 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700246
Florin Coras561af9b2017-12-09 10:19:43 -0800247 vec_validate (tp_vfts, transport_proto);
248 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700249
Florin Coras561af9b2017-12-09 10:19:43 -0800250 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700251}
252
253/**
254 * Get transport virtual function table
255 *
256 * @param type - session type (not protocol type)
257 */
258transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800259transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700260{
Florin Coras561af9b2017-12-09 10:19:43 -0800261 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700262 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800263 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700264}
265
Nathan Skrzypczaka26349d2019-06-26 13:53:08 +0200266u8
267transport_half_open_has_fifos (transport_proto_t tp)
268{
269 return tp_vfts[tp].transport_options.half_open_has_fifos;
270}
271
Florin Coras7fb0fe12018-04-09 09:24:52 -0700272transport_service_type_t
273transport_protocol_service_type (transport_proto_t tp)
274{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200275 return tp_vfts[tp].transport_options.service_type;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700276}
277
Florin Corasf08f26d2018-05-10 13:20:47 -0700278transport_tx_fn_type_t
279transport_protocol_tx_fn_type (transport_proto_t tp)
280{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200281 return tp_vfts[tp].transport_options.tx_type;
Florin Corasf08f26d2018-05-10 13:20:47 -0700282}
283
Florin Coras1ee78302019-02-05 15:51:15 -0800284void
285transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800286{
Florin Coras1ee78302019-02-05 15:51:15 -0800287 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800288}
289
Florin Coras1ee78302019-02-05 15:51:15 -0800290int
291transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800292{
Florin Coras1ee78302019-02-05 15:51:15 -0800293 return tp_vfts[tp].connect (tep);
294}
295
296void
297transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
298{
299 tp_vfts[tp].close (conn_index, thread_index);
300}
301
Florin Corasdfb3b872019-08-16 17:48:44 -0700302void
303transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
304{
305 if (tp_vfts[tp].reset)
306 tp_vfts[tp].reset (conn_index, thread_index);
307 else
308 tp_vfts[tp].close (conn_index, thread_index);
309}
310
Florin Coras1ee78302019-02-05 15:51:15 -0800311u32
312transport_start_listen (transport_proto_t tp, u32 session_index,
313 transport_endpoint_t * tep)
314{
315 return tp_vfts[tp].start_listen (session_index, tep);
316}
317
318u32
319transport_stop_listen (transport_proto_t tp, u32 conn_index)
320{
321 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800322}
323
Florin Coras40903ac2018-06-10 14:41:23 -0700324u8
325transport_protocol_is_cl (transport_proto_t tp)
326{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200327 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
Florin Coras40903ac2018-06-10 14:41:23 -0700328}
329
Aloys Augustincdb71702019-04-08 17:54:39 +0200330always_inline void
331default_get_transport_endpoint (transport_connection_t * tc,
Florin Coras09d18c22019-04-24 11:10:02 -0700332 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200333{
334 if (is_lcl)
335 {
Florin Coras09d18c22019-04-24 11:10:02 -0700336 tep->port = tc->lcl_port;
337 tep->is_ip4 = tc->is_ip4;
338 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200339 }
340 else
341 {
Florin Coras09d18c22019-04-24 11:10:02 -0700342 tep->port = tc->rmt_port;
343 tep->is_ip4 = tc->is_ip4;
344 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200345 }
346}
347
348void
349transport_get_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700350 u32 thread_index, transport_endpoint_t * tep,
351 u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200352{
353 if (tp_vfts[tp].get_transport_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700354 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
355 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200356 else
357 {
358 transport_connection_t *tc;
359 tc = transport_get_connection (tp, conn_index, thread_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700360 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200361 }
362}
363
364void
365transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700366 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200367{
368 if (tp_vfts[tp].get_transport_listener_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700369 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200370 else
371 {
372 transport_connection_t *tc;
373 tc = transport_get_listener (tp, conn_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700374 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200375 }
376}
377
Florin Coras3cbc04b2017-10-02 00:18:51 -0700378#define PORT_MASK ((1 << 16)- 1)
379
380void
381transport_endpoint_del (u32 tepi)
382{
383 clib_spinlock_lock_if_init (&local_endpoints_lock);
384 pool_put_index (local_endpoints, tepi);
385 clib_spinlock_unlock_if_init (&local_endpoints_lock);
386}
387
388always_inline transport_endpoint_t *
389transport_endpoint_new (void)
390{
391 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700392 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700393 return tep;
394}
395
396void
397transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
398{
399 u32 tepi;
400 transport_endpoint_t *tep;
401
402 /* Cleanup local endpoint if this was an active connect */
403 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
404 clib_net_to_host_u16 (port));
405 if (tepi != ENDPOINT_INVALID_INDEX)
406 {
407 tep = pool_elt_at_index (local_endpoints, tepi);
408 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
409 transport_endpoint_del (tepi);
410 }
411}
412
Florin Coras5665ced2018-10-25 18:03:45 -0700413static void
414transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
415{
416 transport_endpoint_t *tep;
417 clib_spinlock_lock_if_init (&local_endpoints_lock);
418 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500419 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700420 tep->port = port;
421 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
422 tep - local_endpoints);
423 clib_spinlock_unlock_if_init (&local_endpoints_lock);
424}
425
Florin Coras3cbc04b2017-10-02 00:18:51 -0700426/**
427 * Allocate local port and add if successful add entry to local endpoint
428 * table to mark the pair as used.
429 */
430int
431transport_alloc_local_port (u8 proto, ip46_address_t * ip)
432{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700433 u16 min = 1024, max = 65535; /* XXX configurable ? */
434 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700435 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700436
437 limit = max - min;
438
439 /* Only support active opens from thread 0 */
440 ASSERT (vlib_get_thread_index () == 0);
441
442 /* Search for first free slot */
443 for (tries = 0; tries < limit; tries++)
444 {
445 u16 port = 0;
446
447 /* Find a port in the specified range */
448 while (1)
449 {
450 port = random_u32 (&port_allocator_seed) & PORT_MASK;
451 if (PREDICT_TRUE (port >= min && port < max))
452 break;
453 }
454
455 /* Look it up. If not found, we're done */
456 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
457 port);
458 if (tei == ENDPOINT_INVALID_INDEX)
459 {
Florin Coras5665ced2018-10-25 18:03:45 -0700460 transport_endpoint_mark_used (proto, ip, port);
461 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700462 }
463 }
464 return -1;
465}
466
Florin Coras5665ced2018-10-25 18:03:45 -0700467static clib_error_t *
468transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700469{
Florin Coras5665ced2018-10-25 18:03:45 -0700470 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700471 {
472 ip4_address_t *ip4;
473 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800474 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700475 return clib_error_return (0, "no routable ip4 address on %U",
476 format_vnet_sw_if_index_name,
477 vnet_get_main (), sw_if_index);
478 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700479 }
480 else
481 {
482 ip6_address_t *ip6;
483 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
484 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700485 return clib_error_return (0, "no routable ip6 addresses on %U",
486 format_vnet_sw_if_index_name,
487 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500488 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700489 }
490 return 0;
491}
492
493static clib_error_t *
494transport_find_local_ip_for_remote (u32 sw_if_index,
495 transport_endpoint_t * rmt,
496 ip46_address_t * lcl_addr)
497{
498 fib_node_index_t fei;
499 fib_prefix_t prefix;
500
501 if (sw_if_index == ENDPOINT_INVALID_INDEX)
502 {
503 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500504 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700505 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
506 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
507
508 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
509 fei = fib_table_lookup (rmt->fib_index, &prefix);
510
511 /* Couldn't find route to destination. Bail out. */
512 if (fei == FIB_NODE_INDEX_INVALID)
513 return clib_error_return (0, "no route to %U", format_ip46_address,
514 &rmt->ip, (rmt->is_ip4 == 0) + 1);
515
516 sw_if_index = fib_entry_get_resolving_interface (fei);
517 if (sw_if_index == ENDPOINT_INVALID_INDEX)
518 return clib_error_return (0, "no resolving interface for %U",
519 format_ip46_address, &rmt->ip,
520 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700521 }
522
Florin Coras5665ced2018-10-25 18:03:45 -0700523 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
524 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
525}
526
527int
528transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
529 ip46_address_t * lcl_addr, u16 * lcl_port)
530{
531 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
532 clib_error_t *error;
533 int port;
534 u32 tei;
535
536 /*
537 * Find the local address
538 */
539 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700540 {
Florin Coras5665ced2018-10-25 18:03:45 -0700541 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
542 rmt, lcl_addr);
543 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800544 {
545 clib_error_report (error);
546 return -1;
547 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700548 }
Florin Coras5665ced2018-10-25 18:03:45 -0700549 else
550 {
551 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500552 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
553 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700554 }
555
556 /*
557 * Allocate source port
558 */
559 if (rmt_cfg->peer.port == 0)
560 {
561 port = transport_alloc_local_port (proto, lcl_addr);
562 if (port < 1)
563 {
564 clib_warning ("Failed to allocate src port");
565 return -1;
566 }
567 *lcl_port = port;
568 }
569 else
570 {
571 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
572 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
573 lcl_addr, port);
574 if (tei != ENDPOINT_INVALID_INDEX)
575 return -1;
576
577 transport_endpoint_mark_used (proto, lcl_addr, port);
578 *lcl_port = port;
579 }
580
Florin Coras3cbc04b2017-10-02 00:18:51 -0700581 return 0;
582}
583
Florin Corasa8e71c82019-10-22 19:01:39 -0700584u8 *
585format_clib_us_time (u8 * s, va_list * args)
586{
587 clib_us_time_t t = va_arg (*args, clib_us_time_t);
588 if (t < 1e3)
589 s = format (s, "%u us", t);
590 else
591 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
592 return s;
593}
Florin Corasd67f1122018-05-21 17:47:40 -0700594
595u8 *
596format_transport_pacer (u8 * s, va_list * args)
597{
598 spacer_t *pacer = va_arg (*args, spacer_t *);
Florin Corasa8e71c82019-10-22 19:01:39 -0700599 u32 thread_index = va_arg (*args, int);
600 clib_us_time_t now, diff;
Florin Corasd67f1122018-05-21 17:47:40 -0700601
Florin Corasa8e71c82019-10-22 19:01:39 -0700602 now = transport_us_time_now (thread_index);
603 diff = now - pacer->last_update;
Florin Coras11e9e352019-11-13 19:09:47 -0800604 s = format (s, "rate %lu bucket %lu t/p %.3f last_update %U idle %u",
Florin Corasbe237bf2019-09-27 08:16:40 -0700605 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
Florin Coras11e9e352019-11-13 19:09:47 -0800606 format_clib_us_time, diff, pacer->idle_timeout_us);
Florin Corasd67f1122018-05-21 17:47:40 -0700607 return s;
608}
609
610static inline u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700611spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700612{
Florin Corasa8e71c82019-10-22 19:01:39 -0700613 u64 n_periods = (time_now - pacer->last_update);
Florin Corase55a6d72018-10-31 23:09:22 -0700614 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700615
Florin Coras11e9e352019-11-13 19:09:47 -0800616 if (PREDICT_FALSE (n_periods > pacer->idle_timeout_us))
Florin Corasc31dc312019-10-06 14:06:14 -0700617 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700618 pacer->last_update = time_now;
Florin Corasc31dc312019-10-06 14:06:14 -0700619 pacer->bucket = TRANSPORT_PACER_MIN_BURST;
620 return TRANSPORT_PACER_MIN_BURST;
621 }
622
Florin Coras11e9e352019-11-13 19:09:47 -0800623 if ((inc = (f32) n_periods * pacer->tokens_per_period) > 10)
Florin Corase55a6d72018-10-31 23:09:22 -0700624 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700625 pacer->last_update = time_now;
Florin Coras7c8f8282019-09-15 16:28:45 -0700626 pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
Florin Corase55a6d72018-10-31 23:09:22 -0700627 }
628
Florin Coras6bc6fd02019-03-27 18:55:11 -0700629 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700630}
631
632static inline void
633spacer_update_bucket (spacer_t * pacer, u32 bytes)
634{
635 ASSERT (pacer->bucket >= bytes);
636 pacer->bucket -= bytes;
637}
638
639static inline void
Florin Coras11e9e352019-11-13 19:09:47 -0800640spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec,
641 clib_us_time_t rtt)
Florin Corasd67f1122018-05-21 17:47:40 -0700642{
643 ASSERT (rate_bytes_per_sec != 0);
Florin Coras7c8f8282019-09-15 16:28:45 -0700644 pacer->bytes_per_sec = rate_bytes_per_sec;
Florin Corasa8e71c82019-10-22 19:01:39 -0700645 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
Florin Coras11e9e352019-11-13 19:09:47 -0800646 pacer->idle_timeout_us = clib_max (rtt * TRANSPORT_PACER_IDLE_FACTOR,
647 TRANSPORT_PACER_MIN_IDLE);
Florin Corasd67f1122018-05-21 17:47:40 -0700648}
649
Florin Coras52814732019-06-12 15:38:19 -0700650static inline u64
651spacer_pace_rate (spacer_t * pacer)
652{
Florin Coras7c8f8282019-09-15 16:28:45 -0700653 return pacer->bytes_per_sec;
Florin Coras52814732019-06-12 15:38:19 -0700654}
655
Florin Coras36ebcff2019-09-12 18:36:44 -0700656static inline void
Florin Corasa8e71c82019-10-22 19:01:39 -0700657spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
Florin Coras36ebcff2019-09-12 18:36:44 -0700658{
Florin Corasa8e71c82019-10-22 19:01:39 -0700659 pacer->last_update = time_now;
660 pacer->bucket = bucket;
Florin Coras36ebcff2019-09-12 18:36:44 -0700661}
662
Florin Corasd67f1122018-05-21 17:47:40 -0700663void
Florin Corasc44a5582018-11-01 16:30:54 -0700664transport_connection_tx_pacer_reset (transport_connection_t * tc,
Florin Coras11e9e352019-11-13 19:09:47 -0800665 u64 rate_bytes_per_sec, u32 start_bucket,
666 clib_us_time_t rtt)
Florin Corasd67f1122018-05-21 17:47:40 -0700667{
Florin Coras11e9e352019-11-13 19:09:47 -0800668 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec, rtt);
Florin Corasa8e71c82019-10-22 19:01:39 -0700669 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
670 start_bucket);
671}
672
673void
Florin Coras11e9e352019-11-13 19:09:47 -0800674transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc,
675 u32 bucket)
Florin Corasa8e71c82019-10-22 19:01:39 -0700676{
Florin Coras11e9e352019-11-13 19:09:47 -0800677 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), bucket);
Florin Corasc44a5582018-11-01 16:30:54 -0700678}
679
680void
681transport_connection_tx_pacer_init (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700682 u64 rate_bytes_per_sec,
Florin Corasc44a5582018-11-01 16:30:54 -0700683 u32 initial_bucket)
684{
Florin Corasc44a5582018-11-01 16:30:54 -0700685 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
686 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
Florin Coras11e9e352019-11-13 19:09:47 -0800687 initial_bucket, 1e6);
Florin Corasd67f1122018-05-21 17:47:40 -0700688}
689
690void
691transport_connection_tx_pacer_update (transport_connection_t * tc,
Florin Coras11e9e352019-11-13 19:09:47 -0800692 u64 bytes_per_sec, clib_us_time_t rtt)
Florin Corasd67f1122018-05-21 17:47:40 -0700693{
Florin Coras11e9e352019-11-13 19:09:47 -0800694 spacer_set_pace_rate (&tc->pacer, bytes_per_sec, rtt);
Florin Corasd67f1122018-05-21 17:47:40 -0700695}
696
697u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700698transport_connection_tx_pacer_burst (transport_connection_t * tc)
Florin Corase55a6d72018-10-31 23:09:22 -0700699{
Florin Corasa8e71c82019-10-22 19:01:39 -0700700 return spacer_max_burst (&tc->pacer,
701 transport_us_time_now (tc->thread_index));
Florin Coras36ebcff2019-09-12 18:36:44 -0700702}
703
Florin Coras52814732019-06-12 15:38:19 -0700704u64
705transport_connection_tx_pacer_rate (transport_connection_t * tc)
706{
707 return spacer_pace_rate (&tc->pacer);
708}
709
Florin Corasd67f1122018-05-21 17:47:40 -0700710void
Florin Coras00482232019-08-02 12:52:00 -0700711transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
Florin Corasd67f1122018-05-21 17:47:40 -0700712{
Florin Corasd67f1122018-05-21 17:47:40 -0700713 if (transport_connection_is_tx_paced (tc))
714 spacer_update_bucket (&tc->pacer, bytes);
715}
716
717void
Florin Corase55a6d72018-10-31 23:09:22 -0700718transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
719 u32 bytes)
720{
721 spacer_update_bucket (&tc->pacer, bytes);
722}
723
724void
Florin Coras70f879d2020-03-13 17:54:42 +0000725transport_connection_reschedule (transport_connection_t * tc)
726{
727 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras6080e0d2020-03-13 20:39:43 +0000728 transport_connection_tx_pacer_reset_bucket (tc, TRANSPORT_PACER_MIN_BURST);
Florin Coras70f879d2020-03-13 17:54:42 +0000729 if (transport_max_tx_dequeue (tc))
730 sesssion_reschedule_tx (tc);
731 else
732 {
733 session_t *s = session_get (tc->s_index, tc->thread_index);
734 svm_fifo_unset_event (s->tx_fifo);
735 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
736 if (svm_fifo_set_event (s->tx_fifo))
737 sesssion_reschedule_tx (tc);
738 }
739}
740
741void
Florin Corasa8e71c82019-10-22 19:01:39 -0700742transport_update_time (clib_time_type_t time_now, u8 thread_index)
Florin Coras561af9b2017-12-09 10:19:43 -0800743{
744 transport_proto_vft_t *vft;
745 vec_foreach (vft, tp_vfts)
746 {
747 if (vft->update_time)
748 (vft->update_time) (time_now, thread_index);
749 }
750}
751
752void
753transport_enable_disable (vlib_main_t * vm, u8 is_en)
754{
755 transport_proto_vft_t *vft;
756 vec_foreach (vft, tp_vfts)
757 {
758 if (vft->enable)
759 (vft->enable) (vm, is_en);
760 }
761}
762
763void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700764transport_init (void)
765{
766 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800767 session_main_t *smm = vnet_get_session_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700768 u32 num_threads;
769
Florin Coras93e65802017-11-29 00:07:11 -0500770 if (smm->local_endpoints_table_buckets == 0)
771 smm->local_endpoints_table_buckets = 250000;
772 if (smm->local_endpoints_table_memory == 0)
773 smm->local_endpoints_table_memory = 512 << 20;
774
Florin Coras3cbc04b2017-10-02 00:18:51 -0700775 /* Initialize [port-allocator] random number seed */
776 port_allocator_seed = (u32) clib_cpu_time_now ();
777
778 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500779 smm->local_endpoints_table_buckets,
780 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700781 num_threads = 1 /* main thread */ + vtm->n_threads;
782 if (num_threads > 1)
783 clib_spinlock_init (&local_endpoints_lock);
784}
785
786/*
787 * fd.io coding-style-patch-verification: ON
788 *
789 * Local Variables:
790 * eval: (c-set-style "gnu")
791 * End:
792 */