blob: 0f86f9e916a9157a9ec8c181418406757c3ae2c8 [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
16#include <vnet/session/transport_interface.h>
17#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 Corasd67f1122018-05-21 17:47:40 -070052
Florin Coras1c710452017-10-17 00:03:13 -070053u8 *
54format_transport_proto (u8 * s, va_list * args)
55{
56 u32 transport_proto = va_arg (*args, u32);
57 switch (transport_proto)
58 {
59 case TRANSPORT_PROTO_TCP:
60 s = format (s, "TCP");
61 break;
62 case TRANSPORT_PROTO_UDP:
63 s = format (s, "UDP");
64 break;
Marco Varlese191a5942017-10-30 18:17:21 +010065 case TRANSPORT_PROTO_SCTP:
66 s = format (s, "SCTP");
67 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070068 case TRANSPORT_PROTO_UDPC:
69 s = format (s, "UDPC");
70 break;
Florin Coras1c710452017-10-17 00:03:13 -070071 }
72 return s;
73}
74
Florin Coras561af9b2017-12-09 10:19:43 -080075u8 *
76format_transport_proto_short (u8 * s, va_list * args)
77{
78 u32 transport_proto = va_arg (*args, u32);
79 switch (transport_proto)
80 {
81 case TRANSPORT_PROTO_TCP:
82 s = format (s, "T");
83 break;
84 case TRANSPORT_PROTO_UDP:
85 s = format (s, "U");
86 break;
Florin Coras4399c2e2018-01-25 06:34:42 -080087 case TRANSPORT_PROTO_SCTP:
88 s = format (s, "S");
89 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070090 case TRANSPORT_PROTO_UDPC:
91 s = format (s, "U");
92 break;
Florin Coras561af9b2017-12-09 10:19:43 -080093 }
94 return s;
95}
96
Florin Corasde9a8492018-10-24 22:18:58 -070097u8 *
98format_transport_connection (u8 * s, va_list * args)
99{
100 u32 transport_proto = va_arg (*args, u32);
101 u32 conn_index = va_arg (*args, u32);
102 u32 thread_index = va_arg (*args, u32);
103 u32 verbose = va_arg (*args, u32);
104 transport_proto_vft_t *tp_vft;
105 transport_connection_t *tc;
106 u32 indent;
107
108 tp_vft = transport_protocol_get_vft (transport_proto);
109 if (!tp_vft)
110 return s;
111
112 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
113 verbose);
114 tc = tp_vft->get_connection (conn_index, thread_index);
115 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
116 {
117 indent = format_get_indent (s) + 1;
118 s = format (s, "%Upacer: %U\n", format_white_space, indent,
119 format_transport_pacer, &tc->pacer);
120 }
121 return s;
122}
123
124u8 *
125format_transport_listen_connection (u8 * s, va_list * args)
126{
127 u32 transport_proto = va_arg (*args, u32);
Florin Corasde9a8492018-10-24 22:18:58 -0700128 transport_proto_vft_t *tp_vft;
129
130 tp_vft = transport_protocol_get_vft (transport_proto);
131 if (!tp_vft)
132 return s;
133
Florin Coras3389dd22019-02-01 18:00:05 -0800134 s = (tp_vft->format_listener) (s, args);
Florin Corasde9a8492018-10-24 22:18:58 -0700135 return s;
136}
137
138u8 *
139format_transport_half_open_connection (u8 * s, va_list * args)
140{
141 u32 transport_proto = va_arg (*args, u32);
142 u32 listen_index = va_arg (*args, u32);
143 transport_proto_vft_t *tp_vft;
144
145 tp_vft = transport_protocol_get_vft (transport_proto);
146 if (!tp_vft)
147 return s;
148
149 s = format (s, "%U", tp_vft->format_half_open, listen_index);
150 return s;
151}
152
Florin Coras1c710452017-10-17 00:03:13 -0700153uword
154unformat_transport_proto (unformat_input_t * input, va_list * args)
155{
156 u32 *proto = va_arg (*args, u32 *);
157 if (unformat (input, "tcp"))
158 *proto = TRANSPORT_PROTO_TCP;
159 else if (unformat (input, "TCP"))
160 *proto = TRANSPORT_PROTO_TCP;
161 else if (unformat (input, "udp"))
162 *proto = TRANSPORT_PROTO_UDP;
163 else if (unformat (input, "UDP"))
164 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -0800165 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100166 *proto = TRANSPORT_PROTO_SCTP;
167 else if (unformat (input, "SCTP"))
168 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800169 else if (unformat (input, "tls"))
170 *proto = TRANSPORT_PROTO_TLS;
171 else if (unformat (input, "TLS"))
172 *proto = TRANSPORT_PROTO_TLS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700173 else if (unformat (input, "udpc"))
174 *proto = TRANSPORT_PROTO_UDPC;
175 else if (unformat (input, "UDPC"))
176 *proto = TRANSPORT_PROTO_UDPC;
Florin Coras1c710452017-10-17 00:03:13 -0700177 else
178 return 0;
179 return 1;
180}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700181
182u32
183transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
184 ip46_address_t * ip, u16 port)
185{
186 clib_bihash_kv_24_8_t kv;
187 int rv;
188
189 kv.key[0] = ip->as_u64[0];
190 kv.key[1] = ip->as_u64[1];
191 kv.key[2] = (u64) port << 8 | (u64) proto;
192
193 rv = clib_bihash_search_inline_24_8 (ht, &kv);
194 if (rv == 0)
195 return kv.value;
196
197 return ENDPOINT_INVALID_INDEX;
198}
199
200void
201transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
202 transport_endpoint_t * te, u32 value)
203{
204 clib_bihash_kv_24_8_t kv;
205
206 kv.key[0] = te->ip.as_u64[0];
207 kv.key[1] = te->ip.as_u64[1];
208 kv.key[2] = (u64) te->port << 8 | (u64) proto;
209 kv.value = value;
210
211 clib_bihash_add_del_24_8 (ht, &kv, 1);
212}
213
214void
215transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
216 transport_endpoint_t * te)
217{
218 clib_bihash_kv_24_8_t kv;
219
220 kv.key[0] = te->ip.as_u64[0];
221 kv.key[1] = te->ip.as_u64[1];
222 kv.key[2] = (u64) te->port << 8 | (u64) proto;
223
224 clib_bihash_add_del_24_8 (ht, &kv, 0);
225}
226
227/**
228 * Register transport virtual function table.
229 *
Florin Coras561af9b2017-12-09 10:19:43 -0800230 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
231 * @param vft - virtual function table for transport proto
232 * @param fib_proto - network layer protocol
233 * @param output_node - output node index that session layer will hand off
234 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700235 */
236void
Florin Coras561af9b2017-12-09 10:19:43 -0800237transport_register_protocol (transport_proto_t transport_proto,
238 const transport_proto_vft_t * vft,
239 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700240{
Florin Coras561af9b2017-12-09 10:19:43 -0800241 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700242
Florin Coras561af9b2017-12-09 10:19:43 -0800243 vec_validate (tp_vfts, transport_proto);
244 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700245
Florin Coras561af9b2017-12-09 10:19:43 -0800246 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700247}
248
249/**
250 * Get transport virtual function table
251 *
252 * @param type - session type (not protocol type)
253 */
254transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800255transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700256{
Florin Coras561af9b2017-12-09 10:19:43 -0800257 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700258 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800259 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700260}
261
Florin Coras7fb0fe12018-04-09 09:24:52 -0700262transport_service_type_t
263transport_protocol_service_type (transport_proto_t tp)
264{
265 return tp_vfts[tp].service_type;
266}
267
Florin Corasf08f26d2018-05-10 13:20:47 -0700268transport_tx_fn_type_t
269transport_protocol_tx_fn_type (transport_proto_t tp)
270{
271 return tp_vfts[tp].tx_type;
272}
273
Florin Coras40903ac2018-06-10 14:41:23 -0700274u8
275transport_protocol_is_cl (transport_proto_t tp)
276{
277 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
278}
279
Florin Coras3cbc04b2017-10-02 00:18:51 -0700280#define PORT_MASK ((1 << 16)- 1)
281
282void
283transport_endpoint_del (u32 tepi)
284{
285 clib_spinlock_lock_if_init (&local_endpoints_lock);
286 pool_put_index (local_endpoints, tepi);
287 clib_spinlock_unlock_if_init (&local_endpoints_lock);
288}
289
290always_inline transport_endpoint_t *
291transport_endpoint_new (void)
292{
293 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700294 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700295 return tep;
296}
297
298void
299transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
300{
301 u32 tepi;
302 transport_endpoint_t *tep;
303
304 /* Cleanup local endpoint if this was an active connect */
305 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
306 clib_net_to_host_u16 (port));
307 if (tepi != ENDPOINT_INVALID_INDEX)
308 {
309 tep = pool_elt_at_index (local_endpoints, tepi);
310 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
311 transport_endpoint_del (tepi);
312 }
313}
314
Florin Coras5665ced2018-10-25 18:03:45 -0700315static void
316transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
317{
318 transport_endpoint_t *tep;
319 clib_spinlock_lock_if_init (&local_endpoints_lock);
320 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500321 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700322 tep->port = port;
323 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
324 tep - local_endpoints);
325 clib_spinlock_unlock_if_init (&local_endpoints_lock);
326}
327
Florin Coras3cbc04b2017-10-02 00:18:51 -0700328/**
329 * Allocate local port and add if successful add entry to local endpoint
330 * table to mark the pair as used.
331 */
332int
333transport_alloc_local_port (u8 proto, ip46_address_t * ip)
334{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700335 u16 min = 1024, max = 65535; /* XXX configurable ? */
336 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700337 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700338
339 limit = max - min;
340
341 /* Only support active opens from thread 0 */
342 ASSERT (vlib_get_thread_index () == 0);
343
344 /* Search for first free slot */
345 for (tries = 0; tries < limit; tries++)
346 {
347 u16 port = 0;
348
349 /* Find a port in the specified range */
350 while (1)
351 {
352 port = random_u32 (&port_allocator_seed) & PORT_MASK;
353 if (PREDICT_TRUE (port >= min && port < max))
354 break;
355 }
356
357 /* Look it up. If not found, we're done */
358 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
359 port);
360 if (tei == ENDPOINT_INVALID_INDEX)
361 {
Florin Coras5665ced2018-10-25 18:03:45 -0700362 transport_endpoint_mark_used (proto, ip, port);
363 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700364 }
365 }
366 return -1;
367}
368
Florin Coras5665ced2018-10-25 18:03:45 -0700369static clib_error_t *
370transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700371{
Florin Coras5665ced2018-10-25 18:03:45 -0700372 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700373 {
374 ip4_address_t *ip4;
375 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800376 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700377 return clib_error_return (0, "no routable ip4 address on %U",
378 format_vnet_sw_if_index_name,
379 vnet_get_main (), sw_if_index);
380 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700381 }
382 else
383 {
384 ip6_address_t *ip6;
385 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
386 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700387 return clib_error_return (0, "no routable ip6 addresses on %U",
388 format_vnet_sw_if_index_name,
389 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500390 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700391 }
392 return 0;
393}
394
395static clib_error_t *
396transport_find_local_ip_for_remote (u32 sw_if_index,
397 transport_endpoint_t * rmt,
398 ip46_address_t * lcl_addr)
399{
400 fib_node_index_t fei;
401 fib_prefix_t prefix;
402
403 if (sw_if_index == ENDPOINT_INVALID_INDEX)
404 {
405 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500406 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700407 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
408 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
409
410 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
411 fei = fib_table_lookup (rmt->fib_index, &prefix);
412
413 /* Couldn't find route to destination. Bail out. */
414 if (fei == FIB_NODE_INDEX_INVALID)
415 return clib_error_return (0, "no route to %U", format_ip46_address,
416 &rmt->ip, (rmt->is_ip4 == 0) + 1);
417
418 sw_if_index = fib_entry_get_resolving_interface (fei);
419 if (sw_if_index == ENDPOINT_INVALID_INDEX)
420 return clib_error_return (0, "no resolving interface for %U",
421 format_ip46_address, &rmt->ip,
422 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700423 }
424
Florin Coras5665ced2018-10-25 18:03:45 -0700425 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
426 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
427}
428
429int
430transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
431 ip46_address_t * lcl_addr, u16 * lcl_port)
432{
433 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
434 clib_error_t *error;
435 int port;
436 u32 tei;
437
438 /*
439 * Find the local address
440 */
441 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700442 {
Florin Coras5665ced2018-10-25 18:03:45 -0700443 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
444 rmt, lcl_addr);
445 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800446 {
447 clib_error_report (error);
448 return -1;
449 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700450 }
Florin Coras5665ced2018-10-25 18:03:45 -0700451 else
452 {
453 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500454 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
455 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700456 }
457
458 /*
459 * Allocate source port
460 */
461 if (rmt_cfg->peer.port == 0)
462 {
463 port = transport_alloc_local_port (proto, lcl_addr);
464 if (port < 1)
465 {
466 clib_warning ("Failed to allocate src port");
467 return -1;
468 }
469 *lcl_port = port;
470 }
471 else
472 {
473 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
474 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
475 lcl_addr, port);
476 if (tei != ENDPOINT_INVALID_INDEX)
477 return -1;
478
479 transport_endpoint_mark_used (proto, lcl_addr, port);
480 *lcl_port = port;
481 }
482
Florin Coras3cbc04b2017-10-02 00:18:51 -0700483 return 0;
484}
485
Florin Corasd67f1122018-05-21 17:47:40 -0700486#define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
487#define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
488
489u8 *
490format_transport_pacer (u8 * s, va_list * args)
491{
492 spacer_t *pacer = va_arg (*args, spacer_t *);
493
494 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
495 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
496 pacer->last_update);
497 return s;
498}
499
500static inline u32
501spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
502{
503 u64 n_periods = norm_time_now - pacer->last_update;
Florin Corase55a6d72018-10-31 23:09:22 -0700504 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700505
Florin Corase55a6d72018-10-31 23:09:22 -0700506 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
507 {
508 pacer->last_update = norm_time_now;
509 pacer->bucket += inc;
510 }
511
Florin Corasd67f1122018-05-21 17:47:40 -0700512 return clib_min (pacer->bucket, pacer->max_burst_size);
513}
514
515static inline void
516spacer_update_bucket (spacer_t * pacer, u32 bytes)
517{
518 ASSERT (pacer->bucket >= bytes);
519 pacer->bucket -= bytes;
520}
521
522static inline void
523spacer_update_max_burst_size (spacer_t * pacer, u32 max_burst_bytes)
524{
Florin Coras7ac053b2018-11-05 15:57:21 -0800525 pacer->max_burst_size = clib_max (max_burst_bytes,
526 TRANSPORT_PACER_MIN_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700527}
528
529static inline void
530spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
531{
532 ASSERT (rate_bytes_per_sec != 0);
533 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
534}
535
536void
Florin Corasc44a5582018-11-01 16:30:54 -0700537transport_connection_tx_pacer_reset (transport_connection_t * tc,
538 u32 rate_bytes_per_sec,
539 u32 start_bucket, u64 time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700540{
Florin Corasd67f1122018-05-21 17:47:40 -0700541 spacer_t *pacer = &tc->pacer;
Florin Corase55a6d72018-10-31 23:09:22 -0700542 f64 dispatch_period;
543 u32 burst_size;
Florin Corasd67f1122018-05-21 17:47:40 -0700544
Florin Corase55a6d72018-10-31 23:09:22 -0700545 dispatch_period = transport_dispatch_period (tc->thread_index);
546 burst_size = rate_bytes_per_sec * dispatch_period;
547 spacer_update_max_burst_size (&tc->pacer, burst_size);
Florin Corasd67f1122018-05-21 17:47:40 -0700548 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
549 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
Florin Corasc44a5582018-11-01 16:30:54 -0700550 pacer->bucket = start_bucket;
551}
552
553void
554transport_connection_tx_pacer_init (transport_connection_t * tc,
555 u32 rate_bytes_per_sec,
556 u32 initial_bucket)
557{
558 vlib_main_t *vm = vlib_get_main ();
559 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
560 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
561 initial_bucket,
562 vm->clib_time.last_cpu_time);
Florin Corasd67f1122018-05-21 17:47:40 -0700563}
564
565void
566transport_connection_tx_pacer_update (transport_connection_t * tc,
567 u64 bytes_per_sec)
568{
Florin Coras7ac053b2018-11-05 15:57:21 -0800569 f64 dispatch_period = transport_dispatch_period (tc->thread_index);
570 u32 burst_size = 1.1 * bytes_per_sec * dispatch_period;
Florin Corasd67f1122018-05-21 17:47:40 -0700571 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
572 spacer_update_max_burst_size (&tc->pacer, burst_size);
573}
574
575u32
Florin Corase55a6d72018-10-31 23:09:22 -0700576transport_connection_tx_pacer_burst (transport_connection_t * tc,
577 u64 time_now)
578{
579 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
580 return spacer_max_burst (&tc->pacer, time_now);
581}
582
583u32
584transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
585 u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700586{
587 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700588
589 snd_space = tp_vfts[tc->proto].send_space (tc);
590 if (transport_connection_is_tx_paced (tc))
591 {
592 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
593 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
Florin Corasd67f1122018-05-21 17:47:40 -0700594 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
595 snd_space = clib_min (snd_space, max_paced_burst);
596 snd_space = snd_space - snd_space % mss;
597 }
598 return snd_space;
599}
600
601void
602transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
603{
604 tc->stats.tx_bytes += bytes;
605 if (transport_connection_is_tx_paced (tc))
606 spacer_update_bucket (&tc->pacer, bytes);
607}
608
609void
Florin Corase55a6d72018-10-31 23:09:22 -0700610transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
611 u32 bytes)
612{
613 spacer_update_bucket (&tc->pacer, bytes);
614}
615
616void
Florin Corasd67f1122018-05-21 17:47:40 -0700617transport_init_tx_pacers_period (void)
618{
619 f64 cpu_freq = os_cpu_clock_frequency ();
620 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
621}
622
Florin Coras3cbc04b2017-10-02 00:18:51 -0700623void
Florin Coras561af9b2017-12-09 10:19:43 -0800624transport_update_time (f64 time_now, u8 thread_index)
625{
626 transport_proto_vft_t *vft;
627 vec_foreach (vft, tp_vfts)
628 {
629 if (vft->update_time)
630 (vft->update_time) (time_now, thread_index);
631 }
632}
633
634void
635transport_enable_disable (vlib_main_t * vm, u8 is_en)
636{
637 transport_proto_vft_t *vft;
638 vec_foreach (vft, tp_vfts)
639 {
640 if (vft->enable)
641 (vft->enable) (vm, is_en);
642 }
643}
644
645void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700646transport_init (void)
647{
648 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras93e65802017-11-29 00:07:11 -0500649 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700650 u32 num_threads;
651
Florin Coras93e65802017-11-29 00:07:11 -0500652 if (smm->local_endpoints_table_buckets == 0)
653 smm->local_endpoints_table_buckets = 250000;
654 if (smm->local_endpoints_table_memory == 0)
655 smm->local_endpoints_table_memory = 512 << 20;
656
Florin Coras3cbc04b2017-10-02 00:18:51 -0700657 /* Initialize [port-allocator] random number seed */
658 port_allocator_seed = (u32) clib_cpu_time_now ();
659
660 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500661 smm->local_endpoints_table_buckets,
662 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700663 num_threads = 1 /* main thread */ + vtm->n_threads;
664 if (num_threads > 1)
665 clib_spinlock_init (&local_endpoints_lock);
666}
667
668/*
669 * fd.io coding-style-patch-verification: ON
670 *
671 * Local Variables:
672 * eval: (c-set-style "gnu")
673 * End:
674 */