blob: c2d21bf28f915cd9efa433e5f0f6c1c582fb1c04 [file] [log] [blame]
Florin Coras3cbc04b2017-10-02 00:18:51 -07001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * 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);
128 u32 listen_index = va_arg (*args, u32);
129 transport_proto_vft_t *tp_vft;
130
131 tp_vft = transport_protocol_get_vft (transport_proto);
132 if (!tp_vft)
133 return s;
134
135 s = format (s, "%U", tp_vft->format_listener, listen_index);
136 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;
162 else if (unformat (input, "udp"))
163 *proto = TRANSPORT_PROTO_UDP;
164 else if (unformat (input, "UDP"))
165 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -0800166 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100167 *proto = TRANSPORT_PROTO_SCTP;
168 else if (unformat (input, "SCTP"))
169 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800170 else if (unformat (input, "tls"))
171 *proto = TRANSPORT_PROTO_TLS;
172 else if (unformat (input, "TLS"))
173 *proto = TRANSPORT_PROTO_TLS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700174 else if (unformat (input, "udpc"))
175 *proto = TRANSPORT_PROTO_UDPC;
176 else if (unformat (input, "UDPC"))
177 *proto = TRANSPORT_PROTO_UDPC;
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 Coras40903ac2018-06-10 14:41:23 -0700275u8
276transport_protocol_is_cl (transport_proto_t tp)
277{
278 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
279}
280
Florin Coras3cbc04b2017-10-02 00:18:51 -0700281#define PORT_MASK ((1 << 16)- 1)
282
283void
284transport_endpoint_del (u32 tepi)
285{
286 clib_spinlock_lock_if_init (&local_endpoints_lock);
287 pool_put_index (local_endpoints, tepi);
288 clib_spinlock_unlock_if_init (&local_endpoints_lock);
289}
290
291always_inline transport_endpoint_t *
292transport_endpoint_new (void)
293{
294 transport_endpoint_t *tep;
Florin Coras5665ced2018-10-25 18:03:45 -0700295 pool_get_zero (local_endpoints, tep);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700296 return tep;
297}
298
299void
300transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
301{
302 u32 tepi;
303 transport_endpoint_t *tep;
304
305 /* Cleanup local endpoint if this was an active connect */
306 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
307 clib_net_to_host_u16 (port));
308 if (tepi != ENDPOINT_INVALID_INDEX)
309 {
310 tep = pool_elt_at_index (local_endpoints, tepi);
311 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
312 transport_endpoint_del (tepi);
313 }
314}
315
Florin Coras5665ced2018-10-25 18:03:45 -0700316static void
317transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
318{
319 transport_endpoint_t *tep;
320 clib_spinlock_lock_if_init (&local_endpoints_lock);
321 tep = transport_endpoint_new ();
Dave Barach178cf492018-11-13 16:34:13 -0500322 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700323 tep->port = port;
324 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
325 tep - local_endpoints);
326 clib_spinlock_unlock_if_init (&local_endpoints_lock);
327}
328
Florin Coras3cbc04b2017-10-02 00:18:51 -0700329/**
330 * Allocate local port and add if successful add entry to local endpoint
331 * table to mark the pair as used.
332 */
333int
334transport_alloc_local_port (u8 proto, ip46_address_t * ip)
335{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700336 u16 min = 1024, max = 65535; /* XXX configurable ? */
337 int tries, limit;
Florin Coras5665ced2018-10-25 18:03:45 -0700338 u32 tei;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700339
340 limit = max - min;
341
342 /* Only support active opens from thread 0 */
343 ASSERT (vlib_get_thread_index () == 0);
344
345 /* Search for first free slot */
346 for (tries = 0; tries < limit; tries++)
347 {
348 u16 port = 0;
349
350 /* Find a port in the specified range */
351 while (1)
352 {
353 port = random_u32 (&port_allocator_seed) & PORT_MASK;
354 if (PREDICT_TRUE (port >= min && port < max))
355 break;
356 }
357
358 /* Look it up. If not found, we're done */
359 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
360 port);
361 if (tei == ENDPOINT_INVALID_INDEX)
362 {
Florin Coras5665ced2018-10-25 18:03:45 -0700363 transport_endpoint_mark_used (proto, ip, port);
364 return port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700365 }
366 }
367 return -1;
368}
369
Florin Coras5665ced2018-10-25 18:03:45 -0700370static clib_error_t *
371transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700372{
Florin Coras5665ced2018-10-25 18:03:45 -0700373 if (is_ip4)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700374 {
375 ip4_address_t *ip4;
376 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800377 if (!ip4)
Florin Coras5665ced2018-10-25 18:03:45 -0700378 return clib_error_return (0, "no routable ip4 address on %U",
379 format_vnet_sw_if_index_name,
380 vnet_get_main (), sw_if_index);
381 addr->ip4.as_u32 = ip4->as_u32;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700382 }
383 else
384 {
385 ip6_address_t *ip6;
386 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
387 if (ip6 == 0)
Florin Coras5665ced2018-10-25 18:03:45 -0700388 return clib_error_return (0, "no routable ip6 addresses on %U",
389 format_vnet_sw_if_index_name,
390 vnet_get_main (), sw_if_index);
Dave Barach178cf492018-11-13 16:34:13 -0500391 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
Florin Coras5665ced2018-10-25 18:03:45 -0700392 }
393 return 0;
394}
395
396static clib_error_t *
397transport_find_local_ip_for_remote (u32 sw_if_index,
398 transport_endpoint_t * rmt,
399 ip46_address_t * lcl_addr)
400{
401 fib_node_index_t fei;
402 fib_prefix_t prefix;
403
404 if (sw_if_index == ENDPOINT_INVALID_INDEX)
405 {
406 /* Find a FIB path to the destination */
Dave Barach178cf492018-11-13 16:34:13 -0500407 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700408 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
409 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
410
411 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
412 fei = fib_table_lookup (rmt->fib_index, &prefix);
413
414 /* Couldn't find route to destination. Bail out. */
415 if (fei == FIB_NODE_INDEX_INVALID)
416 return clib_error_return (0, "no route to %U", format_ip46_address,
417 &rmt->ip, (rmt->is_ip4 == 0) + 1);
418
419 sw_if_index = fib_entry_get_resolving_interface (fei);
420 if (sw_if_index == ENDPOINT_INVALID_INDEX)
421 return clib_error_return (0, "no resolving interface for %U",
422 format_ip46_address, &rmt->ip,
423 (rmt->is_ip4 == 0) + 1);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700424 }
425
Florin Coras5665ced2018-10-25 18:03:45 -0700426 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
427 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
428}
429
430int
431transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
432 ip46_address_t * lcl_addr, u16 * lcl_port)
433{
434 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
435 clib_error_t *error;
436 int port;
437 u32 tei;
438
439 /*
440 * Find the local address
441 */
442 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700443 {
Florin Coras5665ced2018-10-25 18:03:45 -0700444 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
445 rmt, lcl_addr);
446 if (error)
Florin Corasdc2e2512018-12-03 17:47:26 -0800447 {
448 clib_error_report (error);
449 return -1;
450 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700451 }
Florin Coras5665ced2018-10-25 18:03:45 -0700452 else
453 {
454 /* Assume session layer vetted this address */
Dave Barach178cf492018-11-13 16:34:13 -0500455 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
456 sizeof (rmt_cfg->peer.ip));
Florin Coras5665ced2018-10-25 18:03:45 -0700457 }
458
459 /*
460 * Allocate source port
461 */
462 if (rmt_cfg->peer.port == 0)
463 {
464 port = transport_alloc_local_port (proto, lcl_addr);
465 if (port < 1)
466 {
467 clib_warning ("Failed to allocate src port");
468 return -1;
469 }
470 *lcl_port = port;
471 }
472 else
473 {
474 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
475 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
476 lcl_addr, port);
477 if (tei != ENDPOINT_INVALID_INDEX)
478 return -1;
479
480 transport_endpoint_mark_used (proto, lcl_addr, port);
481 *lcl_port = port;
482 }
483
Florin Coras3cbc04b2017-10-02 00:18:51 -0700484 return 0;
485}
486
Florin Corasd67f1122018-05-21 17:47:40 -0700487#define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
488#define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
489
490u8 *
491format_transport_pacer (u8 * s, va_list * args)
492{
493 spacer_t *pacer = va_arg (*args, spacer_t *);
494
495 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
496 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
497 pacer->last_update);
498 return s;
499}
500
501static inline u32
502spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
503{
504 u64 n_periods = norm_time_now - pacer->last_update;
Florin Corase55a6d72018-10-31 23:09:22 -0700505 u64 inc;
Florin Corasd67f1122018-05-21 17:47:40 -0700506
Florin Corase55a6d72018-10-31 23:09:22 -0700507 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
508 {
509 pacer->last_update = norm_time_now;
510 pacer->bucket += inc;
511 }
512
Florin Corasd67f1122018-05-21 17:47:40 -0700513 return clib_min (pacer->bucket, pacer->max_burst_size);
514}
515
516static inline void
517spacer_update_bucket (spacer_t * pacer, u32 bytes)
518{
519 ASSERT (pacer->bucket >= bytes);
520 pacer->bucket -= bytes;
521}
522
523static inline void
524spacer_update_max_burst_size (spacer_t * pacer, u32 max_burst_bytes)
525{
Florin Coras7ac053b2018-11-05 15:57:21 -0800526 pacer->max_burst_size = clib_max (max_burst_bytes,
527 TRANSPORT_PACER_MIN_BURST);
Florin Corasd67f1122018-05-21 17:47:40 -0700528}
529
530static inline void
531spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
532{
533 ASSERT (rate_bytes_per_sec != 0);
534 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
535}
536
537void
Florin Corasc44a5582018-11-01 16:30:54 -0700538transport_connection_tx_pacer_reset (transport_connection_t * tc,
539 u32 rate_bytes_per_sec,
540 u32 start_bucket, u64 time_now)
Florin Corasd67f1122018-05-21 17:47:40 -0700541{
Florin Corasd67f1122018-05-21 17:47:40 -0700542 spacer_t *pacer = &tc->pacer;
Florin Corase55a6d72018-10-31 23:09:22 -0700543 f64 dispatch_period;
544 u32 burst_size;
Florin Corasd67f1122018-05-21 17:47:40 -0700545
Florin Corase55a6d72018-10-31 23:09:22 -0700546 dispatch_period = transport_dispatch_period (tc->thread_index);
547 burst_size = rate_bytes_per_sec * dispatch_period;
548 spacer_update_max_burst_size (&tc->pacer, burst_size);
Florin Corasd67f1122018-05-21 17:47:40 -0700549 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
550 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
Florin Corasc44a5582018-11-01 16:30:54 -0700551 pacer->bucket = start_bucket;
552}
553
554void
555transport_connection_tx_pacer_init (transport_connection_t * tc,
556 u32 rate_bytes_per_sec,
557 u32 initial_bucket)
558{
559 vlib_main_t *vm = vlib_get_main ();
560 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
561 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
562 initial_bucket,
563 vm->clib_time.last_cpu_time);
Florin Corasd67f1122018-05-21 17:47:40 -0700564}
565
566void
567transport_connection_tx_pacer_update (transport_connection_t * tc,
568 u64 bytes_per_sec)
569{
Florin Coras7ac053b2018-11-05 15:57:21 -0800570 f64 dispatch_period = transport_dispatch_period (tc->thread_index);
571 u32 burst_size = 1.1 * bytes_per_sec * dispatch_period;
Florin Corasd67f1122018-05-21 17:47:40 -0700572 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
573 spacer_update_max_burst_size (&tc->pacer, burst_size);
574}
575
576u32
Florin Corase55a6d72018-10-31 23:09:22 -0700577transport_connection_tx_pacer_burst (transport_connection_t * tc,
578 u64 time_now)
579{
580 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
581 return spacer_max_burst (&tc->pacer, time_now);
582}
583
584u32
585transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
586 u16 mss)
Florin Corasd67f1122018-05-21 17:47:40 -0700587{
588 u32 snd_space, max_paced_burst;
Florin Corasd67f1122018-05-21 17:47:40 -0700589
590 snd_space = tp_vfts[tc->proto].send_space (tc);
591 if (transport_connection_is_tx_paced (tc))
592 {
593 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
594 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
Florin Corasd67f1122018-05-21 17:47:40 -0700595 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
596 snd_space = clib_min (snd_space, max_paced_burst);
597 snd_space = snd_space - snd_space % mss;
598 }
599 return snd_space;
600}
601
602void
603transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
604{
605 tc->stats.tx_bytes += bytes;
606 if (transport_connection_is_tx_paced (tc))
607 spacer_update_bucket (&tc->pacer, bytes);
608}
609
610void
Florin Corase55a6d72018-10-31 23:09:22 -0700611transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
612 u32 bytes)
613{
614 spacer_update_bucket (&tc->pacer, bytes);
615}
616
617void
Florin Corasd67f1122018-05-21 17:47:40 -0700618transport_init_tx_pacers_period (void)
619{
620 f64 cpu_freq = os_cpu_clock_frequency ();
621 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
622}
623
Florin Coras3cbc04b2017-10-02 00:18:51 -0700624void
Florin Coras561af9b2017-12-09 10:19:43 -0800625transport_update_time (f64 time_now, u8 thread_index)
626{
627 transport_proto_vft_t *vft;
628 vec_foreach (vft, tp_vfts)
629 {
630 if (vft->update_time)
631 (vft->update_time) (time_now, thread_index);
632 }
633}
634
635void
636transport_enable_disable (vlib_main_t * vm, u8 is_en)
637{
638 transport_proto_vft_t *vft;
639 vec_foreach (vft, tp_vfts)
640 {
641 if (vft->enable)
642 (vft->enable) (vm, is_en);
643 }
644}
645
646void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700647transport_init (void)
648{
649 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras93e65802017-11-29 00:07:11 -0500650 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700651 u32 num_threads;
652
Florin Coras93e65802017-11-29 00:07:11 -0500653 if (smm->local_endpoints_table_buckets == 0)
654 smm->local_endpoints_table_buckets = 250000;
655 if (smm->local_endpoints_table_memory == 0)
656 smm->local_endpoints_table_memory = 512 << 20;
657
Florin Coras3cbc04b2017-10-02 00:18:51 -0700658 /* Initialize [port-allocator] random number seed */
659 port_allocator_seed = (u32) clib_cpu_time_now ();
660
661 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500662 smm->local_endpoints_table_buckets,
663 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700664 num_threads = 1 /* main thread */ + vtm->n_threads;
665 if (num_threads > 1)
666 clib_spinlock_init (&local_endpoints_lock);
667}
668
669/*
670 * fd.io coding-style-patch-verification: ON
671 *
672 * Local Variables:
673 * eval: (c-set-style "gnu")
674 * End:
675 */