blob: e453ee0187963ab7f7d174d0618285a4a3375584 [file] [log] [blame]
Florin Coras3cbc04b2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras3cbc04b2017-10-02 00:18:51 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras1ee78302019-02-05 15:51:15 -080016#include <vnet/session/transport.h>
Florin Coras3cbc04b2017-10-02 00:18:51 -070017#include <vnet/session/session.h>
18#include <vnet/fib/fib.h>
19
20/**
21 * Per-type vector of transport protocol virtual function tables
22 */
23transport_proto_vft_t *tp_vfts;
24
25/*
26 * Port allocator seed
27 */
28static u32 port_allocator_seed;
29
30/*
31 * Local endpoints table
32 */
33static transport_endpoint_table_t local_endpoints_table;
34
35/*
36 * Pool of local endpoints
37 */
38static transport_endpoint_t *local_endpoints;
39
40/*
41 * Local endpoints pool lock
42 */
43static clib_spinlock_t local_endpoints_lock;
44
Florin Corasd67f1122018-05-21 17:47:40 -070045/*
46 * Period used by transport pacers. Initialized by session layer
47 */
48static double transport_pacer_period;
49
Florin Coras7ac053b2018-11-05 15:57:21 -080050#define TRANSPORT_PACER_MIN_MSS 1460
51#define TRANSPORT_PACER_MIN_BURST TRANSPORT_PACER_MIN_MSS
Florin Coras6bc6fd02019-03-27 18:55:11 -070052#define TRANSPORT_PACER_MAX_BURST (48 * TRANSPORT_PACER_MIN_MSS)
Florin Corasd67f1122018-05-21 17:47:40 -070053
Florin Coras1c710452017-10-17 00:03:13 -070054u8 *
55format_transport_proto (u8 * s, va_list * args)
56{
57 u32 transport_proto = va_arg (*args, u32);
58 switch (transport_proto)
59 {
60 case TRANSPORT_PROTO_TCP:
61 s = format (s, "TCP");
62 break;
63 case TRANSPORT_PROTO_UDP:
64 s = format (s, "UDP");
65 break;
Marco Varlese191a5942017-10-30 18:17:21 +010066 case TRANSPORT_PROTO_SCTP:
67 s = format (s, "SCTP");
68 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070069 case TRANSPORT_PROTO_UDPC:
70 s = format (s, "UDPC");
71 break;
Florin Coras1c710452017-10-17 00:03:13 -070072 }
73 return s;
74}
75
Florin Coras561af9b2017-12-09 10:19:43 -080076u8 *
77format_transport_proto_short (u8 * s, va_list * args)
78{
79 u32 transport_proto = va_arg (*args, u32);
80 switch (transport_proto)
81 {
82 case TRANSPORT_PROTO_TCP:
83 s = format (s, "T");
84 break;
85 case TRANSPORT_PROTO_UDP:
86 s = format (s, "U");
87 break;
Florin Coras4399c2e2018-01-25 06:34:42 -080088 case TRANSPORT_PROTO_SCTP:
89 s = format (s, "S");
90 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070091 case TRANSPORT_PROTO_UDPC:
92 s = format (s, "U");
93 break;
Florin Coras561af9b2017-12-09 10:19:43 -080094 }
95 return s;
96}
97
Florin Corasde9a8492018-10-24 22:18:58 -070098u8 *
99format_transport_connection (u8 * s, va_list * args)
100{
101 u32 transport_proto = va_arg (*args, u32);
102 u32 conn_index = va_arg (*args, u32);
103 u32 thread_index = va_arg (*args, u32);
104 u32 verbose = va_arg (*args, u32);
105 transport_proto_vft_t *tp_vft;
106 transport_connection_t *tc;
107 u32 indent;
108
109 tp_vft = transport_protocol_get_vft (transport_proto);
110 if (!tp_vft)
111 return s;
112
113 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
114 verbose);
115 tc = tp_vft->get_connection (conn_index, thread_index);
116 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
117 {
118 indent = format_get_indent (s) + 1;
119 s = format (s, "%Upacer: %U\n", format_white_space, indent,
120 format_transport_pacer, &tc->pacer);
121 }
122 return s;
123}
124
125u8 *
126format_transport_listen_connection (u8 * s, va_list * args)
127{
128 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700129 transport_proto_vft_t *tp_vft;
130
131 tp_vft = transport_protocol_get_vft (transport_proto);
132 if (!tp_vft)
133 return s;
134
Florin Coras3389dd22019-02-01 18:00:05 -0800135 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700136 return s;
137}
138
139u8 *
140format_transport_half_open_connection (u8 * s, va_list * args)
141{
142 u32 transport_proto = va_arg (*args, u32);
143 u32 listen_index = va_arg (*args, u32);
144 transport_proto_vft_t *tp_vft;
145
146 tp_vft = transport_protocol_get_vft (transport_proto);
147 if (!tp_vft)
148 return s;
149
150 s = format (s, "%U", tp_vft->format_half_open, listen_index);
151 return s;
152}
153
Florin Coras1c710452017-10-17 00:03:13 -0700154uword
155unformat_transport_proto (unformat_input_t * input, va_list * args)
156{
157 u32 *proto = va_arg (*args, u32 *);
158 if (unformat (input, "tcp"))
159 *proto = TRANSPORT_PROTO_TCP;
160 else if (unformat (input, "TCP"))
161 *proto = TRANSPORT_PROTO_TCP;
Nathan Skrzypczakef71ef02019-03-25 10:20:56 +0100162 else if (unformat (input, "udpc"))
163 *proto = TRANSPORT_PROTO_UDPC;
164 else if (unformat (input, "UDPC"))
165 *proto = TRANSPORT_PROTO_UDPC;
Florin Coras1c710452017-10-17 00:03:13 -0700166 else if (unformat (input, "udp"))
167 *proto = TRANSPORT_PROTO_UDP;
168 else if (unformat (input, "UDP"))
169 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -0800170 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100171 *proto = TRANSPORT_PROTO_SCTP;
172 else if (unformat (input, "SCTP"))
173 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800174 else if (unformat (input, "tls"))
175 *proto = TRANSPORT_PROTO_TLS;
176 else if (unformat (input, "TLS"))
177 *proto = TRANSPORT_PROTO_TLS;
Florin Coras1c710452017-10-17 00:03:13 -0700178 else
179 return 0;
180 return 1;
181}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700182
183u32
184transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
185 ip46_address_t * ip, u16 port)
186{
187 clib_bihash_kv_24_8_t kv;
188 int rv;
189
190 kv.key[0] = ip->as_u64[0];
191 kv.key[1] = ip->as_u64[1];
192 kv.key[2] = (u64) port << 8 | (u64) proto;
193
194 rv = clib_bihash_search_inline_24_8 (ht, &kv);
195 if (rv == 0)
196 return kv.value;
197
198 return ENDPOINT_INVALID_INDEX;
199}
200
201void
202transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
203 transport_endpoint_t * te, u32 value)
204{
205 clib_bihash_kv_24_8_t kv;
206
207 kv.key[0] = te->ip.as_u64[0];
208 kv.key[1] = te->ip.as_u64[1];
209 kv.key[2] = (u64) te->port << 8 | (u64) proto;
210 kv.value = value;
211
212 clib_bihash_add_del_24_8 (ht, &kv, 1);
213}
214
215void
216transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
217 transport_endpoint_t * te)
218{
219 clib_bihash_kv_24_8_t kv;
220
221 kv.key[0] = te->ip.as_u64[0];
222 kv.key[1] = te->ip.as_u64[1];
223 kv.key[2] = (u64) te->port << 8 | (u64) proto;
224
225 clib_bihash_add_del_24_8 (ht, &kv, 0);
226}
227
228/**
229 * Register transport virtual function table.
230 *
Florin Coras561af9b2017-12-09 10:19:43 -0800231 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
232 * @param vft - virtual function table for transport proto
233 * @param fib_proto - network layer protocol
234 * @param output_node - output node index that session layer will hand off
235 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700236 */
237void
Florin Coras561af9b2017-12-09 10:19:43 -0800238transport_register_protocol (transport_proto_t transport_proto,
239 const transport_proto_vft_t * vft,
240 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700241{
Florin Coras561af9b2017-12-09 10:19:43 -0800242 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700243
Florin Coras561af9b2017-12-09 10:19:43 -0800244 vec_validate (tp_vfts, transport_proto);
245 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700246
Florin Coras561af9b2017-12-09 10:19:43 -0800247 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700248}
249
250/**
251 * Get transport virtual function table
252 *
253 * @param type - session type (not protocol type)
254 */
255transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800256transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700257{
Florin Coras561af9b2017-12-09 10:19:43 -0800258 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700259 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800260 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700261}
262
Florin Coras7fb0fe12018-04-09 09:24:52 -0700263transport_service_type_t
264transport_protocol_service_type (transport_proto_t tp)
265{
266 return tp_vfts[tp].service_type;
267}
268
Florin Corasf08f26d2018-05-10 13:20:47 -0700269transport_tx_fn_type_t
270transport_protocol_tx_fn_type (transport_proto_t tp)
271{
272 return tp_vfts[tp].tx_type;
273}
274
Florin Coras1ee78302019-02-05 15:51:15 -0800275void
276transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
Florin Coras4edc37e2019-02-04 23:01:34 -0800277{
Florin Coras1ee78302019-02-05 15:51:15 -0800278 tp_vfts[tp].cleanup (conn_index, thread_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800279}
280
Florin Coras1ee78302019-02-05 15:51:15 -0800281int
282transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
Florin Coras4edc37e2019-02-04 23:01:34 -0800283{
Florin Coras1ee78302019-02-05 15:51:15 -0800284 return tp_vfts[tp].connect (tep);
285}
286
287void
288transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
289{
290 tp_vfts[tp].close (conn_index, thread_index);
291}
292
293u32
294transport_start_listen (transport_proto_t tp, u32 session_index,
295 transport_endpoint_t * tep)
296{
297 return tp_vfts[tp].start_listen (session_index, tep);
298}
299
300u32
301transport_stop_listen (transport_proto_t tp, u32 conn_index)
302{
303 return tp_vfts[tp].stop_listen (conn_index);
Florin Coras4edc37e2019-02-04 23:01:34 -0800304}
305
Florin Coras40903ac2018-06-10 14:41:23 -0700306u8
307transport_protocol_is_cl (transport_proto_t tp)
308{
309 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
310}
311
Florin Coras3cbc04b2017-10-02 00:18:51 -0700312#define PORT_MASK ((1 << 16)- 1)
313
314void
315transport_endpoint_del (u32 tepi)
316{
317 clib_spinlock_lock_if_init (&local_endpoints_lock);
318 pool_put_index (local_endpoints, tepi);
319 clib_spinlock_unlock_if_init (&local_endpoints_lock);
320}
321
322always_inline transport_endpoint_t *
323transport_endpoint_new (void)
324{
325 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700326 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700327 return tep;
328}
329
330void
331transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
332{
333 u32 tepi;
334 transport_endpoint_t *tep;
335
336 /* Cleanup local endpoint if this was an active connect */
337 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
338 clib_net_to_host_u16 (port));
339 if (tepi != ENDPOINT_INVALID_INDEX)
340 {
341 tep = pool_elt_at_index (local_endpoints, tepi);
342 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
343 transport_endpoint_del (tepi);
344 }
345}
346
Florin Coras5665ced2018-10-25 18:03:45 -0700347static void
348transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
349{
350 transport_endpoint_t *tep;
351 clib_spinlock_lock_if_init (&local_endpoints_lock);
352 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500353 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700354 tep->port = port;
355 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
356 tep - local_endpoints);
357 clib_spinlock_unlock_if_init (&local_endpoints_lock);
358}
359
Florin Coras3cbc04b2017-10-02 00:18:51 -0700360/**
361 * Allocate local port and add if successful add entry to local endpoint
362 * table to mark the pair as used.
363 */
364int
365transport_alloc_local_port (u8 proto, ip46_address_t * ip)
366{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700367 u16 min = 1024, max = 65535; /* XXX configurable ? */
368 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700369 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700370
371 limit = max - min;
372
373 /* Only support active opens from thread 0 */
374 ASSERT (vlib_get_thread_index () == 0);
375
376 /* Search for first free slot */
377 for (tries = 0; tries < limit; tries++)
378 {
379 u16 port = 0;
380
381 /* Find a port in the specified range */
382 while (1)
383 {
384 port = random_u32 (&port_allocator_seed) & PORT_MASK;
385 if (PREDICT_TRUE (port >= min && port < max))
386 break;
387 }
388
389 /* Look it up. If not found, we're done */
390 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
391 port);
392 if (tei == ENDPOINT_INVALID_INDEX)
393 {
Florin Coras5665ced2018-10-25 18:03:45 -0700394 transport_endpoint_mark_used (proto, ip, port);
395 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700396 }
397 }
398 return -1;
399}
400
Florin Coras5665ced2018-10-25 18:03:45 -0700401static clib_error_t *
402transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700403{
Florin Coras5665ced2018-10-25 18:03:45 -0700404 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700405 {
406 ip4_address_t *ip4;
407 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800408 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700409 return clib_error_return (0, "no routable ip4 address on %U",
410 format_vnet_sw_if_index_name,
411 vnet_get_main (), sw_if_index);
412 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700413 }
414 else
415 {
416 ip6_address_t *ip6;
417 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
418 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700419 return clib_error_return (0, "no routable ip6 addresses on %U",
420 format_vnet_sw_if_index_name,
421 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500422 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700423 }
424 return 0;
425}
426
427static clib_error_t *
428transport_find_local_ip_for_remote (u32 sw_if_index,
429 transport_endpoint_t * rmt,
430 ip46_address_t * lcl_addr)
431{
432 fib_node_index_t fei;
433 fib_prefix_t prefix;
434
435 if (sw_if_index == ENDPOINT_INVALID_INDEX)
436 {
437 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500438 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700439 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
440 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
441
442 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
443 fei = fib_table_lookup (rmt->fib_index, &prefix);
444
445 /* Couldn't find route to destination. Bail out. */
446 if (fei == FIB_NODE_INDEX_INVALID)
447 return clib_error_return (0, "no route to %U", format_ip46_address,
448 &rmt->ip, (rmt->is_ip4 == 0) + 1);
449
450 sw_if_index = fib_entry_get_resolving_interface (fei);
451 if (sw_if_index == ENDPOINT_INVALID_INDEX)
452 return clib_error_return (0, "no resolving interface for %U",
453 format_ip46_address, &rmt->ip,
454 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700455 }
456
Florin Coras5665ced2018-10-25 18:03:45 -0700457 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
458 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
459}
460
461int
462transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
463 ip46_address_t * lcl_addr, u16 * lcl_port)
464{
465 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
466 clib_error_t *error;
467 int port;
468 u32 tei;
469
470 /*
471 * Find the local address
472 */
473 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700474 {
Florin Coras5665ced2018-10-25 18:03:45 -0700475 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
476 rmt, lcl_addr);
477 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800478 {
479 clib_error_report (error);
480 return -1;
481 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700482 }
Florin Coras5665ced2018-10-25 18:03:45 -0700483 else
484 {
485 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500486 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
487 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700488 }
489
490 /*
491 * Allocate source port
492 */
493 if (rmt_cfg->peer.port == 0)
494 {
495 port = transport_alloc_local_port (proto, lcl_addr);
496 if (port < 1)
497 {
498 clib_warning ("Failed to allocate src port");
499 return -1;
500 }
501 *lcl_port = port;
502 }
503 else
504 {
505 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
506 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
507 lcl_addr, port);
508 if (tei != ENDPOINT_INVALID_INDEX)
509 return -1;
510
511 transport_endpoint_mark_used (proto, lcl_addr, port);
512 *lcl_port = port;
513 }
514
Florin Coras3cbc04b2017-10-02 00:18:51 -0700515 return 0;
516}
517
Florin Corasd67f1122018-05-21 17:47:40 -0700518#define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
519#define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
520
521u8 *
522format_transport_pacer (u8 * s, va_list * args)
523{
524 spacer_t *pacer = va_arg (*args, spacer_t *);
525
526 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
527 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
528 pacer->last_update);
529 return s;
530}
531
532static inline u32
533spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
534{
535 u64 n_periods = norm_time_now - pacer->last_update;
Florin Corase55a6d72018-10-31 23:09:22 -0700536 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700537
Florin Corase55a6d72018-10-31 23:09:22 -0700538 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
539 {
540 pacer->last_update = norm_time_now;
541 pacer->bucket += inc;
542 }
543
Florin Coras6bc6fd02019-03-27 18:55:11 -0700544 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700545}
546
547static inline void
548spacer_update_bucket (spacer_t * pacer, u32 bytes)
549{
550 ASSERT (pacer->bucket >= bytes);
551 pacer->bucket -= bytes;
552}
553
554static inline void
Florin Corasd67f1122018-05-21 17:47:40 -0700555spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
556{
557 ASSERT (rate_bytes_per_sec != 0);
558 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
559}
560
561void
Florin Corasc44a5582018-11-01 16:30:54 -0700562transport_connection_tx_pacer_reset (transport_connection_t * tc,
563 u32 rate_bytes_per_sec,
564 u32 start_bucket, u64 time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700565{
Florin Corasd67f1122018-05-21 17:47:40 -0700566 spacer_t *pacer = &tc->pacer;
Florin Corasd67f1122018-05-21 17:47:40 -0700567 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
568 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
Florin Corasc44a5582018-11-01 16:30:54 -0700569 pacer->bucket = start_bucket;
570}
571
572void
573transport_connection_tx_pacer_init (transport_connection_t * tc,
574 u32 rate_bytes_per_sec,
575 u32 initial_bucket)
576{
577 vlib_main_t *vm = vlib_get_main ();
578 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
579 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
580 initial_bucket,
581 vm->clib_time.last_cpu_time);
Florin Corasd67f1122018-05-21 17:47:40 -0700582}
583
584void
585transport_connection_tx_pacer_update (transport_connection_t * tc,
586 u64 bytes_per_sec)
587{
Florin Corasd67f1122018-05-21 17:47:40 -0700588 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
Florin Corasd67f1122018-05-21 17:47:40 -0700589}
590
591u32
Florin Corase55a6d72018-10-31 23:09:22 -0700592transport_connection_tx_pacer_burst (transport_connection_t * tc,
593 u64 time_now)
594{
595 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
596 return spacer_max_burst (&tc->pacer, time_now);
597}
598
599u32
600transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
601 u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700602{
603 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700604
605 snd_space = tp_vfts[tc->proto].send_space (tc);
606 if (transport_connection_is_tx_paced (tc))
607 {
608 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
609 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
Florin Corasd67f1122018-05-21 17:47:40 -0700610 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
611 snd_space = clib_min (snd_space, max_paced_burst);
612 snd_space = snd_space - snd_space % mss;
613 }
614 return snd_space;
615}
616
617void
618transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
619{
620 tc->stats.tx_bytes += bytes;
621 if (transport_connection_is_tx_paced (tc))
622 spacer_update_bucket (&tc->pacer, bytes);
623}
624
625void
Florin Corase55a6d72018-10-31 23:09:22 -0700626transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
627 u32 bytes)
628{
629 spacer_update_bucket (&tc->pacer, bytes);
630}
631
632void
Florin Corasd67f1122018-05-21 17:47:40 -0700633transport_init_tx_pacers_period (void)
634{
635 f64 cpu_freq = os_cpu_clock_frequency ();
636 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
637}
638
Florin Coras3cbc04b2017-10-02 00:18:51 -0700639void
Florin Coras561af9b2017-12-09 10:19:43 -0800640transport_update_time (f64 time_now, u8 thread_index)
641{
642 transport_proto_vft_t *vft;
643 vec_foreach (vft, tp_vfts)
644 {
645 if (vft->update_time)
646 (vft->update_time) (time_now, thread_index);
647 }
648}
649
650void
651transport_enable_disable (vlib_main_t * vm, u8 is_en)
652{
653 transport_proto_vft_t *vft;
654 vec_foreach (vft, tp_vfts)
655 {
656 if (vft->enable)
657 (vft->enable) (vm, is_en);
658 }
659}
660
661void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700662transport_init (void)
663{
664 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras31c99552019-03-01 13:00:58 -0800665 session_main_t *smm = vnet_get_session_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700666 u32 num_threads;
667
Florin Coras93e65802017-11-29 00:07:11 -0500668 if (smm->local_endpoints_table_buckets == 0)
669 smm->local_endpoints_table_buckets = 250000;
670 if (smm->local_endpoints_table_memory == 0)
671 smm->local_endpoints_table_memory = 512 << 20;
672
Florin Coras3cbc04b2017-10-02 00:18:51 -0700673 /* Initialize [port-allocator] random number seed */
674 port_allocator_seed = (u32) clib_cpu_time_now ();
675
676 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500677 smm->local_endpoints_table_buckets,
678 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700679 num_threads = 1 /* main thread */ + vtm->n_threads;
680 if (num_threads > 1)
681 clib_spinlock_init (&local_endpoints_lock);
682}
683
684/*
685 * fd.io coding-style-patch-verification: ON
686 *
687 * Local Variables:
688 * eval: (c-set-style "gnu")
689 * End:
690 */