blob: 554508672e7ccb446f5f212a967a117f3a94eb42 [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
Florin Coras0b466ad2022-11-03 12:50:13 -070020/**
21 * Per-type vector of transport protocol virtual function tables
22 */
23transport_proto_vft_t *tp_vfts;
24
Florin Coras57660d92020-04-04 22:45:34 +000025typedef struct local_endpoint_
26{
27 transport_endpoint_t ep;
28 int refcnt;
29} local_endpoint_t;
30
Florin Coras0b466ad2022-11-03 12:50:13 -070031typedef struct transport_main_
32{
33 transport_endpoint_table_t local_endpoints_table;
34 local_endpoint_t *local_endpoints;
35 u32 port_allocator_seed;
36 clib_spinlock_t local_endpoints_lock;
37} transport_main_t;
Florin Coras3cbc04b2017-10-02 00:18:51 -070038
Florin Coras0b466ad2022-11-03 12:50:13 -070039static transport_main_t tp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -070040
Florin Coras1c710452017-10-17 00:03:13 -070041u8 *
42format_transport_proto (u8 * s, va_list * args)
43{
44 u32 transport_proto = va_arg (*args, u32);
Florin Coras07063b82020-03-13 04:44:51 +000045
46 if (tp_vfts[transport_proto].transport_options.name)
47 s = format (s, "%s", tp_vfts[transport_proto].transport_options.name);
48 else
49 s = format (s, "n/a");
50
Florin Coras1c710452017-10-17 00:03:13 -070051 return s;
52}
53
Florin Coras561af9b2017-12-09 10:19:43 -080054u8 *
55format_transport_proto_short (u8 * s, va_list * args)
56{
57 u32 transport_proto = va_arg (*args, u32);
Florin Coras07063b82020-03-13 04:44:51 +000058 char *short_name;
59
60 short_name = tp_vfts[transport_proto].transport_options.short_name;
61 if (short_name)
62 s = format (s, "%s", short_name);
63 else
64 s = format (s, "NA");
65
Florin Coras561af9b2017-12-09 10:19:43 -080066 return s;
67}
68
Florin Corasde9a8492018-10-24 22:18:58 -070069u8 *
70format_transport_connection (u8 * s, va_list * args)
71{
72 u32 transport_proto = va_arg (*args, u32);
73 u32 conn_index = va_arg (*args, u32);
74 u32 thread_index = va_arg (*args, u32);
75 u32 verbose = va_arg (*args, u32);
76 transport_proto_vft_t *tp_vft;
77 transport_connection_t *tc;
78 u32 indent;
79
80 tp_vft = transport_protocol_get_vft (transport_proto);
81 if (!tp_vft)
82 return s;
83
84 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
85 verbose);
86 tc = tp_vft->get_connection (conn_index, thread_index);
Florin Coras36d49392020-04-24 23:00:11 +000087 if (tc && verbose > 1)
Florin Corasde9a8492018-10-24 22:18:58 -070088 {
89 indent = format_get_indent (s) + 1;
Florin Coras36d49392020-04-24 23:00:11 +000090 if (transport_connection_is_tx_paced (tc))
91 s = format (s, "%Upacer: %U\n", format_white_space, indent,
92 format_transport_pacer, &tc->pacer, tc->thread_index);
Florin Coras70f879d2020-03-13 17:54:42 +000093 s = format (s, "%Utransport: flags 0x%x\n", format_white_space, indent,
94 tc->flags);
Florin Corasde9a8492018-10-24 22:18:58 -070095 }
96 return s;
97}
98
99u8 *
100format_transport_listen_connection (u8 * s, va_list * args)
101{
102 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700103 transport_proto_vft_t *tp_vft;
104
105 tp_vft = transport_protocol_get_vft (transport_proto);
106 if (!tp_vft)
107 return s;
108
Florin Coras3389dd22019-02-01 18:00:05 -0800109 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700110 return s;
111}
112
113u8 *
114format_transport_half_open_connection (u8 * s, va_list * args)
115{
116 u32 transport_proto = va_arg (*args, u32);
Florin Corasea727642021-05-07 19:39:43 -0700117 u32 ho_index = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700118 transport_proto_vft_t *tp_vft;
119
120 tp_vft = transport_protocol_get_vft (transport_proto);
121 if (!tp_vft)
122 return s;
123
Florin Corasea727642021-05-07 19:39:43 -0700124 s = format (s, "%U", tp_vft->format_half_open, ho_index);
Florin Corasde9a8492018-10-24 22:18:58 -0700125 return s;
126}
127
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800128static u8
129unformat_transport_str_match (unformat_input_t * input, const char *str)
130{
131 int i;
132
133 if (strlen (str) > vec_len (input->buffer) - input->index)
134 return 0;
135
136 for (i = 0; i < strlen (str); i++)
137 {
138 if (input->buffer[i + input->index] != str[i])
139 return 0;
140 }
141 return 1;
142}
143
Florin Coras1c710452017-10-17 00:03:13 -0700144uword
145unformat_transport_proto (unformat_input_t * input, va_list * args)
146{
147 u32 *proto = va_arg (*args, u32 *);
Florin Coras07063b82020-03-13 04:44:51 +0000148 transport_proto_vft_t *tp_vft;
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800149 u8 longest_match = 0, match;
Florin Coras07063b82020-03-13 04:44:51 +0000150 char *str, *str_match = 0;
151 transport_proto_t tp;
Florin Coras5bb23ec2019-08-31 09:45:13 -0700152
Florin Coras07063b82020-03-13 04:44:51 +0000153 for (tp = 0; tp < vec_len (tp_vfts); tp++)
154 {
155 tp_vft = &tp_vfts[tp];
156 str = tp_vft->transport_options.name;
157 if (!str)
158 continue;
159 if (unformat_transport_str_match (input, str))
160 {
161 match = strlen (str);
162 if (match > longest_match)
163 {
164 *proto = tp;
165 longest_match = match;
166 str_match = str;
167 }
168 }
Florin Coras5bb23ec2019-08-31 09:45:13 -0700169 }
Florin Coras07063b82020-03-13 04:44:51 +0000170 if (longest_match)
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800171 {
Dave Barach4897d772020-03-26 10:56:13 -0400172 (void) unformat (input, str_match);
Florin Coras3bbbf0d2019-11-19 17:23:22 -0800173 return 1;
174 }
175
176 return 0;
Florin Coras1c710452017-10-17 00:03:13 -0700177}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700178
Florin Coras07063b82020-03-13 04:44:51 +0000179u8 *
180format_transport_protos (u8 * s, va_list * args)
181{
182 transport_proto_vft_t *tp_vft;
183
184 vec_foreach (tp_vft, tp_vfts)
185 s = format (s, "%s\n", tp_vft->transport_options.name);
186
187 return s;
188}
189
Florin Coras3cbc04b2017-10-02 00:18:51 -0700190u32
191transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
192 ip46_address_t * ip, u16 port)
193{
194 clib_bihash_kv_24_8_t kv;
195 int rv;
196
197 kv.key[0] = ip->as_u64[0];
198 kv.key[1] = ip->as_u64[1];
199 kv.key[2] = (u64) port << 8 | (u64) proto;
200
201 rv = clib_bihash_search_inline_24_8 (ht, &kv);
202 if (rv == 0)
203 return kv.value;
204
205 return ENDPOINT_INVALID_INDEX;
206}
207
208void
209transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
210 transport_endpoint_t * te, u32 value)
211{
212 clib_bihash_kv_24_8_t kv;
213
214 kv.key[0] = te->ip.as_u64[0];
215 kv.key[1] = te->ip.as_u64[1];
216 kv.key[2] = (u64) te->port << 8 | (u64) proto;
217 kv.value = value;
218
219 clib_bihash_add_del_24_8 (ht, &kv, 1);
220}
221
222void
223transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
224 transport_endpoint_t * te)
225{
226 clib_bihash_kv_24_8_t kv;
227
228 kv.key[0] = te->ip.as_u64[0];
229 kv.key[1] = te->ip.as_u64[1];
230 kv.key[2] = (u64) te->port << 8 | (u64) proto;
231
232 clib_bihash_add_del_24_8 (ht, &kv, 0);
233}
234
Florin Coras3cbc04b2017-10-02 00:18:51 -0700235void
Florin Coras561af9b2017-12-09 10:19:43 -0800236transport_register_protocol (transport_proto_t transport_proto,
237 const transport_proto_vft_t * vft,
238 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700239{
Florin Coras561af9b2017-12-09 10:19:43 -0800240 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700241
Florin Coras561af9b2017-12-09 10:19:43 -0800242 vec_validate (tp_vfts, transport_proto);
243 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700244
Florin Coras561af9b2017-12-09 10:19:43 -0800245 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700246}
247
Florin Coras07063b82020-03-13 04:44:51 +0000248transport_proto_t
249transport_register_new_protocol (const transport_proto_vft_t * vft,
250 fib_protocol_t fib_proto, u32 output_node)
251{
252 transport_proto_t transport_proto;
253 u8 is_ip4;
254
255 transport_proto = session_add_transport_proto ();
256 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
257
258 vec_validate (tp_vfts, transport_proto);
259 tp_vfts[transport_proto] = *vft;
260
261 session_register_transport (transport_proto, vft, is_ip4, output_node);
262
263 return transport_proto;
264}
265
Florin Coras3cbc04b2017-10-02 00:18:51 -0700266/**
267 * Get transport virtual function table
268 *
269 * @param type - session type (not protocol type)
270 */
271transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800272transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700273{
Florin Coras561af9b2017-12-09 10:19:43 -0800274 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700275 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800276 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700277}
278
Florin Coras7fb0fe12018-04-09 09:24:52 -0700279transport_service_type_t
280transport_protocol_service_type (transport_proto_t tp)
281{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200282 return tp_vfts[tp].transport_options.service_type;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700283}
284
Florin Corasf08f26d2018-05-10 13:20:47 -0700285transport_tx_fn_type_t
286transport_protocol_tx_fn_type (transport_proto_t tp)
287{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200288 return tp_vfts[tp].transport_options.tx_type;
Florin Corasf08f26d2018-05-10 13:20:47 -0700289}
290
Florin Coras1ee78302019-02-05 15:51:15 -0800291void
292transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800293{
Florin Coras1ee78302019-02-05 15:51:15 -0800294 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800295}
296
Florin Corasd50ff7f2020-04-16 04:30:22 +0000297void
298transport_cleanup_half_open (transport_proto_t tp, u32 conn_index)
299{
Florin Coras05bc33c2021-05-19 19:33:19 -0700300 if (tp_vfts[tp].cleanup_ho)
Florin Corasd50ff7f2020-04-16 04:30:22 +0000301 tp_vfts[tp].cleanup_ho (conn_index);
302}
303
Florin Coras1ee78302019-02-05 15:51:15 -0800304int
305transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800306{
Filip Tehlar92d29652022-07-28 08:39:13 +0000307 if (PREDICT_FALSE (!tp_vfts[tp].connect))
308 return SESSION_E_TRANSPORT_NO_REG;
Florin Coras1ee78302019-02-05 15:51:15 -0800309 return tp_vfts[tp].connect (tep);
310}
311
312void
liuyacan534468e2021-05-09 03:50:40 +0000313transport_half_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
314{
315 if (tp_vfts[tp].half_close)
316 tp_vfts[tp].half_close (conn_index, thread_index);
317}
318
319void
Florin Coras1ee78302019-02-05 15:51:15 -0800320transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
321{
322 tp_vfts[tp].close (conn_index, thread_index);
323}
324
Florin Corasdfb3b872019-08-16 17:48:44 -0700325void
326transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
327{
328 if (tp_vfts[tp].reset)
329 tp_vfts[tp].reset (conn_index, thread_index);
330 else
331 tp_vfts[tp].close (conn_index, thread_index);
332}
333
Florin Coras1ee78302019-02-05 15:51:15 -0800334u32
335transport_start_listen (transport_proto_t tp, u32 session_index,
Florin Coras0bce71e2022-02-10 11:57:06 -0800336 transport_endpoint_cfg_t *tep)
Florin Coras1ee78302019-02-05 15:51:15 -0800337{
Filip Tehlar92d29652022-07-28 08:39:13 +0000338 if (PREDICT_FALSE (!tp_vfts[tp].start_listen))
339 return SESSION_E_TRANSPORT_NO_REG;
Florin Coras1ee78302019-02-05 15:51:15 -0800340 return tp_vfts[tp].start_listen (session_index, tep);
341}
342
343u32
344transport_stop_listen (transport_proto_t tp, u32 conn_index)
345{
346 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800347}
348
Florin Coras40903ac2018-06-10 14:41:23 -0700349u8
350transport_protocol_is_cl (transport_proto_t tp)
351{
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200352 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
Florin Coras40903ac2018-06-10 14:41:23 -0700353}
354
Aloys Augustincdb71702019-04-08 17:54:39 +0200355always_inline void
356default_get_transport_endpoint (transport_connection_t * tc,
Florin Coras09d18c22019-04-24 11:10:02 -0700357 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200358{
359 if (is_lcl)
360 {
Florin Coras09d18c22019-04-24 11:10:02 -0700361 tep->port = tc->lcl_port;
362 tep->is_ip4 = tc->is_ip4;
363 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200364 }
365 else
366 {
Florin Coras09d18c22019-04-24 11:10:02 -0700367 tep->port = tc->rmt_port;
368 tep->is_ip4 = tc->is_ip4;
369 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Aloys Augustincdb71702019-04-08 17:54:39 +0200370 }
371}
372
373void
374transport_get_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700375 u32 thread_index, transport_endpoint_t * tep,
376 u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200377{
378 if (tp_vfts[tp].get_transport_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700379 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
380 is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200381 else
382 {
383 transport_connection_t *tc;
384 tc = transport_get_connection (tp, conn_index, thread_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700385 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200386 }
387}
388
389void
390transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
Florin Coras09d18c22019-04-24 11:10:02 -0700391 transport_endpoint_t * tep, u8 is_lcl)
Aloys Augustincdb71702019-04-08 17:54:39 +0200392{
393 if (tp_vfts[tp].get_transport_listener_endpoint)
Florin Coras09d18c22019-04-24 11:10:02 -0700394 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200395 else
396 {
397 transport_connection_t *tc;
398 tc = transport_get_listener (tp, conn_index);
Florin Coras09d18c22019-04-24 11:10:02 -0700399 default_get_transport_endpoint (tc, tep, is_lcl);
Aloys Augustincdb71702019-04-08 17:54:39 +0200400 }
401}
402
Florin Coras04ae8272021-04-12 19:55:37 -0700403int
404transport_connection_attribute (transport_proto_t tp, u32 conn_index,
405 u8 thread_index, u8 is_get,
406 transport_endpt_attr_t *attr)
407{
408 if (!tp_vfts[tp].attribute)
409 return -1;
410
411 return tp_vfts[tp].attribute (conn_index, thread_index, is_get, attr);
412}
413
Florin Coras3cbc04b2017-10-02 00:18:51 -0700414#define PORT_MASK ((1 << 16)- 1)
415
416void
Florin Coras05ead782022-03-23 16:35:05 -0700417transport_endpoint_free (u32 tepi)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700418{
Florin Coras0b466ad2022-11-03 12:50:13 -0700419 transport_main_t *tm = &tp_main;
420 pool_put_index (tm->local_endpoints, tepi);
Florin Coras05ead782022-03-23 16:35:05 -0700421}
422
Florin Coras57660d92020-04-04 22:45:34 +0000423always_inline local_endpoint_t *
Florin Coras05ead782022-03-23 16:35:05 -0700424transport_endpoint_alloc (void)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700425{
Florin Coras0b466ad2022-11-03 12:50:13 -0700426 transport_main_t *tm = &tp_main;
Florin Coras57660d92020-04-04 22:45:34 +0000427 local_endpoint_t *lep;
Florin Coras05ead782022-03-23 16:35:05 -0700428
429 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
Florin Coras0b466ad2022-11-03 12:50:13 -0700430 pool_get_aligned_safe (tm->local_endpoints, lep, 0);
Florin Coras57660d92020-04-04 22:45:34 +0000431 return lep;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700432}
433
434void
435transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
436{
Florin Coras0b466ad2022-11-03 12:50:13 -0700437 transport_main_t *tm = &tp_main;
Florin Coras57660d92020-04-04 22:45:34 +0000438 local_endpoint_t *lep;
439 u32 lepi;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700440
441 /* Cleanup local endpoint if this was an active connect */
Florin Coras0b466ad2022-11-03 12:50:13 -0700442 lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700443 clib_net_to_host_u16 (port));
Florin Corasf55183a2022-03-24 17:02:08 -0700444 if (lepi == ENDPOINT_INVALID_INDEX)
445 return;
446
Florin Coras0b466ad2022-11-03 12:50:13 -0700447 lep = pool_elt_at_index (tm->local_endpoints, lepi);
Florin Corasf55183a2022-03-24 17:02:08 -0700448 if (!clib_atomic_sub_fetch (&lep->refcnt, 1))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700449 {
Florin Coras0b466ad2022-11-03 12:50:13 -0700450 transport_endpoint_table_del (&tm->local_endpoints_table, proto,
451 &lep->ep);
Florin Corasf55183a2022-03-24 17:02:08 -0700452
453 /* All workers can free connections. Synchronize access to pool */
Florin Coras0b466ad2022-11-03 12:50:13 -0700454 clib_spinlock_lock (&tm->local_endpoints_lock);
Florin Corasf55183a2022-03-24 17:02:08 -0700455 transport_endpoint_free (lepi);
Florin Coras0b466ad2022-11-03 12:50:13 -0700456 clib_spinlock_unlock (&tm->local_endpoints_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700457 }
458}
459
Florin Corasf55183a2022-03-24 17:02:08 -0700460static int
461transport_endpoint_mark_used (u8 proto, ip46_address_t *ip, u16 port)
Florin Coras5665ced2018-10-25 18:03:45 -0700462{
Florin Coras0b466ad2022-11-03 12:50:13 -0700463 transport_main_t *tm = &tp_main;
Florin Coras57660d92020-04-04 22:45:34 +0000464 local_endpoint_t *lep;
Florin Corasf55183a2022-03-24 17:02:08 -0700465 u32 tei;
Florin Coras05ead782022-03-23 16:35:05 -0700466
467 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
468
Florin Coras0b466ad2022-11-03 12:50:13 -0700469 tei =
470 transport_endpoint_lookup (&tm->local_endpoints_table, proto, ip, port);
Florin Corasf55183a2022-03-24 17:02:08 -0700471 if (tei != ENDPOINT_INVALID_INDEX)
472 return SESSION_E_PORTINUSE;
473
Florin Coras05ead782022-03-23 16:35:05 -0700474 /* Pool reallocs with worker barrier */
475 lep = transport_endpoint_alloc ();
Florin Coras57660d92020-04-04 22:45:34 +0000476 clib_memcpy_fast (&lep->ep.ip, ip, sizeof (*ip));
477 lep->ep.port = port;
478 lep->refcnt = 1;
Florin Corasf55183a2022-03-24 17:02:08 -0700479
Florin Coras0b466ad2022-11-03 12:50:13 -0700480 transport_endpoint_table_add (&tm->local_endpoints_table, proto, &lep->ep,
481 lep - tm->local_endpoints);
Florin Corasf55183a2022-03-24 17:02:08 -0700482
483 return 0;
Florin Coras5665ced2018-10-25 18:03:45 -0700484}
485
Florin Coras57660d92020-04-04 22:45:34 +0000486void
487transport_share_local_endpoint (u8 proto, ip46_address_t * lcl_ip, u16 port)
488{
Florin Coras0b466ad2022-11-03 12:50:13 -0700489 transport_main_t *tm = &tp_main;
Florin Coras57660d92020-04-04 22:45:34 +0000490 local_endpoint_t *lep;
491 u32 lepi;
492
Florin Coras0b466ad2022-11-03 12:50:13 -0700493 lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
Florin Coras57660d92020-04-04 22:45:34 +0000494 clib_net_to_host_u16 (port));
495 if (lepi != ENDPOINT_INVALID_INDEX)
496 {
Florin Coras0b466ad2022-11-03 12:50:13 -0700497 lep = pool_elt_at_index (tm->local_endpoints, lepi);
Florin Coras57660d92020-04-04 22:45:34 +0000498 clib_atomic_add_fetch (&lep->refcnt, 1);
499 }
500}
501
Florin Coras3cbc04b2017-10-02 00:18:51 -0700502/**
503 * Allocate local port and add if successful add entry to local endpoint
504 * table to mark the pair as used.
505 */
506int
507transport_alloc_local_port (u8 proto, ip46_address_t * ip)
508{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700509 u16 min = 1024, max = 65535; /* XXX configurable ? */
510 int tries, limit;
511
512 limit = max - min;
513
Florin Coras05ead782022-03-23 16:35:05 -0700514 /* Only support active opens from one of ctrl threads */
515 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
Florin Coras3cbc04b2017-10-02 00:18:51 -0700516
517 /* Search for first free slot */
518 for (tries = 0; tries < limit; tries++)
519 {
520 u16 port = 0;
521
522 /* Find a port in the specified range */
523 while (1)
524 {
Florin Coras0b466ad2022-11-03 12:50:13 -0700525 port = random_u32 (&tp_main.port_allocator_seed) & PORT_MASK;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700526 if (PREDICT_TRUE (port >= min && port < max))
527 break;
528 }
529
Florin Corasf55183a2022-03-24 17:02:08 -0700530 if (!transport_endpoint_mark_used (proto, ip, port))
531 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700532 }
533 return -1;
534}
535
Florin Coras00e01d32019-10-21 16:07:46 -0700536static session_error_t
Florin Coras5665ced2018-10-25 18:03:45 -0700537transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700538{
Florin Coras5665ced2018-10-25 18:03:45 -0700539 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700540 {
541 ip4_address_t *ip4;
542 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800543 if (!ip4)
Florin Coras00e01d32019-10-21 16:07:46 -0700544 return SESSION_E_NOIP;
Florin Coras5665ced2018-10-25 18:03:45 -0700545 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700546 }
547 else
548 {
549 ip6_address_t *ip6;
550 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
551 if (ip6 == 0)
Florin Coras00e01d32019-10-21 16:07:46 -0700552 return SESSION_E_NOIP;
Dave Barach178cf492018-11-13 16:34:13 -0500553 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700554 }
555 return 0;
556}
557
Florin Coras00e01d32019-10-21 16:07:46 -0700558static session_error_t
Florin Coras596c45b2021-09-09 12:04:17 -0700559transport_find_local_ip_for_remote (u32 *sw_if_index,
560 transport_endpoint_t *rmt,
561 ip46_address_t *lcl_addr)
Florin Coras5665ced2018-10-25 18:03:45 -0700562{
563 fib_node_index_t fei;
564 fib_prefix_t prefix;
565
Florin Coras596c45b2021-09-09 12:04:17 -0700566 if (*sw_if_index == ENDPOINT_INVALID_INDEX)
Florin Coras5665ced2018-10-25 18:03:45 -0700567 {
568 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500569 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700570 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
571 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
572
573 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
574 fei = fib_table_lookup (rmt->fib_index, &prefix);
575
576 /* Couldn't find route to destination. Bail out. */
577 if (fei == FIB_NODE_INDEX_INVALID)
Florin Coras00e01d32019-10-21 16:07:46 -0700578 return SESSION_E_NOROUTE;
Florin Coras5665ced2018-10-25 18:03:45 -0700579
Florin Coras596c45b2021-09-09 12:04:17 -0700580 *sw_if_index = fib_entry_get_resolving_interface (fei);
581 if (*sw_if_index == ENDPOINT_INVALID_INDEX)
Florin Coras00e01d32019-10-21 16:07:46 -0700582 return SESSION_E_NOINTF;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700583 }
584
Florin Coras5665ced2018-10-25 18:03:45 -0700585 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
Florin Coras596c45b2021-09-09 12:04:17 -0700586 return transport_get_interface_ip (*sw_if_index, rmt->is_ip4, lcl_addr);
Florin Coras5665ced2018-10-25 18:03:45 -0700587}
588
589int
590transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
591 ip46_address_t * lcl_addr, u16 * lcl_port)
592{
593 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
Florin Coras00e01d32019-10-21 16:07:46 -0700594 session_error_t error;
Florin Coras5665ced2018-10-25 18:03:45 -0700595 int port;
Florin Coras5665ced2018-10-25 18:03:45 -0700596
597 /*
598 * Find the local address
599 */
600 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700601 {
Florin Coras596c45b2021-09-09 12:04:17 -0700602 error = transport_find_local_ip_for_remote (&rmt_cfg->peer.sw_if_index,
Florin Coras5665ced2018-10-25 18:03:45 -0700603 rmt, lcl_addr);
604 if (error)
Florin Coras00e01d32019-10-21 16:07:46 -0700605 return error;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700606 }
Florin Coras5665ced2018-10-25 18:03:45 -0700607 else
608 {
609 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500610 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
611 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700612 }
613
614 /*
615 * Allocate source port
616 */
617 if (rmt_cfg->peer.port == 0)
618 {
619 port = transport_alloc_local_port (proto, lcl_addr);
620 if (port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700621 return SESSION_E_NOPORT;
Florin Coras5665ced2018-10-25 18:03:45 -0700622 *lcl_port = port;
623 }
624 else
625 {
626 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
Florin Coras57660d92020-04-04 22:45:34 +0000627 *lcl_port = port;
Florin Coras5665ced2018-10-25 18:03:45 -0700628
Florin Corasf55183a2022-03-24 17:02:08 -0700629 return transport_endpoint_mark_used (proto, lcl_addr, port);
Florin Coras5665ced2018-10-25 18:03:45 -0700630 }
631
Florin Coras3cbc04b2017-10-02 00:18:51 -0700632 return 0;
633}
634
Florin Corasa8e71c82019-10-22 19:01:39 -0700635u8 *
636format_clib_us_time (u8 * s, va_list * args)
637{
638 clib_us_time_t t = va_arg (*args, clib_us_time_t);
639 if (t < 1e3)
640 s = format (s, "%u us", t);
641 else
642 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
643 return s;
644}
Florin Corasd67f1122018-05-21 17:47:40 -0700645
646u8 *
647format_transport_pacer (u8 * s, va_list * args)
648{
649 spacer_t *pacer = va_arg (*args, spacer_t *);
Florin Corasa8e71c82019-10-22 19:01:39 -0700650 u32 thread_index = va_arg (*args, int);
651 clib_us_time_t now, diff;
Florin Corasd67f1122018-05-21 17:47:40 -0700652
Florin Corasa8e71c82019-10-22 19:01:39 -0700653 now = transport_us_time_now (thread_index);
654 diff = now - pacer->last_update;
Florin Corasa39f4722020-11-12 10:20:05 -0800655 s = format (s, "rate %lu bucket %ld t/p %.3f last_update %U burst %u",
Florin Corasbe237bf2019-09-27 08:16:40 -0700656 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
Florin Coras7808df22020-11-02 18:00:32 -0800657 format_clib_us_time, diff, pacer->max_burst);
Florin Corasd67f1122018-05-21 17:47:40 -0700658 return s;
659}
660
661static inline u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700662spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700663{
Florin Corasa8e71c82019-10-22 19:01:39 -0700664 u64 n_periods = (time_now - pacer->last_update);
Florin Coras93dd58c2022-01-09 17:20:28 -0800665 i64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700666
Florin Coras11e9e352019-11-13 19:09:47 -0800667 if ((inc = (f32) n_periods * pacer->tokens_per_period) > 10)
Florin Corase55a6d72018-10-31 23:09:22 -0700668 {
Florin Corasa8e71c82019-10-22 19:01:39 -0700669 pacer->last_update = time_now;
Florin Coras93dd58c2022-01-09 17:20:28 -0800670 pacer->bucket = clib_min (pacer->bucket + inc, (i64) pacer->max_burst);
Florin Corase55a6d72018-10-31 23:09:22 -0700671 }
672
Florin Coras2b4f74f2022-01-09 19:03:09 -0800673 return pacer->bucket >= 0 ? pacer->max_burst : 0;
Florin Corasd67f1122018-05-21 17:47:40 -0700674}
675
676static inline void
677spacer_update_bucket (spacer_t * pacer, u32 bytes)
678{
Florin Corasd67f1122018-05-21 17:47:40 -0700679 pacer->bucket -= bytes;
680}
681
682static inline void
Florin Coras11e9e352019-11-13 19:09:47 -0800683spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec,
Florin Corasa39f4722020-11-12 10:20:05 -0800684 clib_us_time_t rtt, clib_time_type_t sec_per_loop)
Florin Corasd67f1122018-05-21 17:47:40 -0700685{
Florin Coras7808df22020-11-02 18:00:32 -0800686 clib_us_time_t max_time;
687
Florin Corasd67f1122018-05-21 17:47:40 -0700688 ASSERT (rate_bytes_per_sec != 0);
Florin Coras7c8f8282019-09-15 16:28:45 -0700689 pacer->bytes_per_sec = rate_bytes_per_sec;
Florin Corasa8e71c82019-10-22 19:01:39 -0700690 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
Florin Coras7808df22020-11-02 18:00:32 -0800691
692 /* Allow a min number of bursts per rtt, if their size is acceptable. Goal
693 * is to spread the sending of data over the rtt but to also allow for some
694 * coalescing that can potentially
695 * 1) reduce load on session layer by reducing scheduling frequency for a
696 * session and
697 * 2) optimize sending when tso if available
698 *
699 * Max "time-length" of a burst cannot be less than 1us or more than 1ms.
700 */
Florin Corasa39f4722020-11-12 10:20:05 -0800701 max_time = clib_max (rtt / TRANSPORT_PACER_BURSTS_PER_RTT,
702 (clib_us_time_t) (sec_per_loop * CLIB_US_TIME_FREQ));
Florin Coras7808df22020-11-02 18:00:32 -0800703 max_time = clib_clamp (max_time, 1 /* 1us */ , 1000 /* 1ms */ );
704 pacer->max_burst = (rate_bytes_per_sec * max_time) * CLIB_US_TIME_PERIOD;
705 pacer->max_burst = clib_clamp (pacer->max_burst, TRANSPORT_PACER_MIN_BURST,
706 TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700707}
708
Florin Coras52814732019-06-12 15:38:19 -0700709static inline u64
710spacer_pace_rate (spacer_t * pacer)
711{
Florin Coras7c8f8282019-09-15 16:28:45 -0700712 return pacer->bytes_per_sec;
Florin Coras52814732019-06-12 15:38:19 -0700713}
714
Florin Coras36ebcff2019-09-12 18:36:44 -0700715static inline void
Florin Corasa8e71c82019-10-22 19:01:39 -0700716spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
Florin Coras36ebcff2019-09-12 18:36:44 -0700717{
Florin Corasa8e71c82019-10-22 19:01:39 -0700718 pacer->last_update = time_now;
719 pacer->bucket = bucket;
Florin Coras36ebcff2019-09-12 18:36:44 -0700720}
721
Florin Corasd67f1122018-05-21 17:47:40 -0700722void
Florin Corasc44a5582018-11-01 16:30:54 -0700723transport_connection_tx_pacer_reset (transport_connection_t * tc,
Florin Coras11e9e352019-11-13 19:09:47 -0800724 u64 rate_bytes_per_sec, u32 start_bucket,
725 clib_us_time_t rtt)
Florin Corasd67f1122018-05-21 17:47:40 -0700726{
Florin Corasa39f4722020-11-12 10:20:05 -0800727 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec, rtt,
728 transport_seconds_per_loop (tc->thread_index));
Florin Corasa8e71c82019-10-22 19:01:39 -0700729 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
730 start_bucket);
731}
732
733void
Florin Coras11e9e352019-11-13 19:09:47 -0800734transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc,
735 u32 bucket)
Florin Corasa8e71c82019-10-22 19:01:39 -0700736{
Florin Coras11e9e352019-11-13 19:09:47 -0800737 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), bucket);
Florin Corasc44a5582018-11-01 16:30:54 -0700738}
739
740void
741transport_connection_tx_pacer_init (transport_connection_t * tc,
Florin Corasa8e71c82019-10-22 19:01:39 -0700742 u64 rate_bytes_per_sec,
Florin Corasc44a5582018-11-01 16:30:54 -0700743 u32 initial_bucket)
744{
Florin Corasc44a5582018-11-01 16:30:54 -0700745 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
746 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
Florin Coras11e9e352019-11-13 19:09:47 -0800747 initial_bucket, 1e6);
Florin Corasd67f1122018-05-21 17:47:40 -0700748}
749
750void
751transport_connection_tx_pacer_update (transport_connection_t * tc,
Florin Coras11e9e352019-11-13 19:09:47 -0800752 u64 bytes_per_sec, clib_us_time_t rtt)
Florin Corasd67f1122018-05-21 17:47:40 -0700753{
Florin Corasa39f4722020-11-12 10:20:05 -0800754 spacer_set_pace_rate (&tc->pacer, bytes_per_sec, rtt,
755 transport_seconds_per_loop (tc->thread_index));
Florin Corasd67f1122018-05-21 17:47:40 -0700756}
757
758u32
Florin Corasa8e71c82019-10-22 19:01:39 -0700759transport_connection_tx_pacer_burst (transport_connection_t * tc)
Florin Corase55a6d72018-10-31 23:09:22 -0700760{
Florin Corasa8e71c82019-10-22 19:01:39 -0700761 return spacer_max_burst (&tc->pacer,
762 transport_us_time_now (tc->thread_index));
Florin Coras36ebcff2019-09-12 18:36:44 -0700763}
764
Florin Coras52814732019-06-12 15:38:19 -0700765u64
766transport_connection_tx_pacer_rate (transport_connection_t * tc)
767{
768 return spacer_pace_rate (&tc->pacer);
769}
770
Florin Corasd67f1122018-05-21 17:47:40 -0700771void
Florin Coras00482232019-08-02 12:52:00 -0700772transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
Florin Corasd67f1122018-05-21 17:47:40 -0700773{
Florin Corasd67f1122018-05-21 17:47:40 -0700774 if (transport_connection_is_tx_paced (tc))
775 spacer_update_bucket (&tc->pacer, bytes);
776}
777
778void
Florin Corase55a6d72018-10-31 23:09:22 -0700779transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
780 u32 bytes)
781{
782 spacer_update_bucket (&tc->pacer, bytes);
783}
784
785void
Florin Coras8f10b902021-04-02 18:32:00 -0700786transport_update_pacer_time (u32 thread_index, clib_time_type_t now)
787{
788 session_wrk_update_time (session_main_get_worker (thread_index), now);
789}
790
791void
Florin Coras70f879d2020-03-13 17:54:42 +0000792transport_connection_reschedule (transport_connection_t * tc)
793{
794 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
Florin Coras2b4f74f2022-01-09 19:03:09 -0800795 transport_connection_tx_pacer_reset_bucket (tc, 0 /* bucket */);
Florin Coras70f879d2020-03-13 17:54:42 +0000796 if (transport_max_tx_dequeue (tc))
797 sesssion_reschedule_tx (tc);
798 else
799 {
800 session_t *s = session_get (tc->s_index, tc->thread_index);
801 svm_fifo_unset_event (s->tx_fifo);
802 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
803 if (svm_fifo_set_event (s->tx_fifo))
804 sesssion_reschedule_tx (tc);
805 }
806}
807
808void
Florin Coras8c4fa012020-11-06 16:59:08 -0800809transport_fifos_init_ooo (transport_connection_t * tc)
810{
811 session_t *s = session_get (tc->s_index, tc->thread_index);
812 svm_fifo_init_ooo_lookup (s->rx_fifo, 0 /* ooo enq */ );
813 svm_fifo_init_ooo_lookup (s->tx_fifo, 1 /* ooo deq */ );
814}
815
816void
Florin Corasa8e71c82019-10-22 19:01:39 -0700817transport_update_time (clib_time_type_t time_now, u8 thread_index)
Florin Coras561af9b2017-12-09 10:19:43 -0800818{
819 transport_proto_vft_t *vft;
820 vec_foreach (vft, tp_vfts)
821 {
822 if (vft->update_time)
823 (vft->update_time) (time_now, thread_index);
824 }
825}
826
827void
828transport_enable_disable (vlib_main_t * vm, u8 is_en)
829{
830 transport_proto_vft_t *vft;
831 vec_foreach (vft, tp_vfts)
832 {
833 if (vft->enable)
834 (vft->enable) (vm, is_en);
Florin Coras5384cca2022-01-20 18:10:26 -0800835
836 if (vft->update_time)
837 session_register_update_time_fn (vft->update_time, is_en);
Florin Coras561af9b2017-12-09 10:19:43 -0800838 }
839}
840
841void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700842transport_init (void)
843{
844 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800845 session_main_t *smm = vnet_get_session_main ();
Florin Coras0b466ad2022-11-03 12:50:13 -0700846 transport_main_t *tm = &tp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700847 u32 num_threads;
848
Florin Coras93e65802017-11-29 00:07:11 -0500849 if (smm->local_endpoints_table_buckets == 0)
850 smm->local_endpoints_table_buckets = 250000;
851 if (smm->local_endpoints_table_memory == 0)
852 smm->local_endpoints_table_memory = 512 << 20;
853
Florin Coras3cbc04b2017-10-02 00:18:51 -0700854 /* Initialize [port-allocator] random number seed */
Florin Coras0b466ad2022-11-03 12:50:13 -0700855 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700856
Florin Coras0b466ad2022-11-03 12:50:13 -0700857 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500858 smm->local_endpoints_table_buckets,
859 smm->local_endpoints_table_memory);
Florin Coras0b466ad2022-11-03 12:50:13 -0700860 clib_spinlock_init (&tm->local_endpoints_lock);
Florin Coras05ead782022-03-23 16:35:05 -0700861
Florin Coras3cbc04b2017-10-02 00:18:51 -0700862 num_threads = 1 /* main thread */ + vtm->n_threads;
863 if (num_threads > 1)
Florin Corasfb50bc32021-05-18 00:28:59 -0700864 {
Florin Corasfb50bc32021-05-18 00:28:59 -0700865 /* Main not polled if there are workers */
866 smm->transport_cl_thread = 1;
867 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700868}
869
870/*
871 * fd.io coding-style-patch-verification: ON
872 *
873 * Local Variables:
874 * eval: (c-set-style "gnu")
875 * End:
876 */