aa8deac54007352822c6937abbd0b7668b0363bc
[vpp.git] / src / vnet / session / transport.c
1 /*
2  * Copyright (c) 2017-2019 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.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  */
23 transport_proto_vft_t *tp_vfts;
24
25 /*
26  * Port allocator seed
27  */
28 static u32 port_allocator_seed;
29
30 /*
31  * Local endpoints table
32  */
33 static transport_endpoint_table_t local_endpoints_table;
34
35 /*
36  * Pool of local endpoints
37  */
38 static transport_endpoint_t *local_endpoints;
39
40 /*
41  * Local endpoints pool lock
42  */
43 static clib_spinlock_t local_endpoints_lock;
44
45 /*
46  * Period used by transport pacers. Initialized by session layer
47  */
48 static double transport_pacer_period;
49
50 #define TRANSPORT_PACER_MIN_MSS         1460
51 #define TRANSPORT_PACER_MIN_BURST       TRANSPORT_PACER_MIN_MSS
52 #define TRANSPORT_PACER_MAX_BURST       (32 * TRANSPORT_PACER_MIN_MSS)
53
54 u8 *
55 format_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;
66     case TRANSPORT_PROTO_SCTP:
67       s = format (s, "SCTP");
68       break;
69     case TRANSPORT_PROTO_NONE:
70       s = format (s, "NONE");
71       break;
72     case TRANSPORT_PROTO_TLS:
73       s = format (s, "TLS");
74       break;
75     case TRANSPORT_PROTO_UDPC:
76       s = format (s, "UDPC");
77       break;
78     case TRANSPORT_PROTO_QUIC:
79       s = format (s, "QUIC");
80       break;
81     default:
82       s = format (s, "UNKNOWN");
83       break;
84     }
85   return s;
86 }
87
88 u8 *
89 format_transport_proto_short (u8 * s, va_list * args)
90 {
91   u32 transport_proto = va_arg (*args, u32);
92   switch (transport_proto)
93     {
94     case TRANSPORT_PROTO_TCP:
95       s = format (s, "T");
96       break;
97     case TRANSPORT_PROTO_UDP:
98       s = format (s, "U");
99       break;
100     case TRANSPORT_PROTO_SCTP:
101       s = format (s, "S");
102       break;
103     case TRANSPORT_PROTO_NONE:
104       s = format (s, "N");
105       break;
106     case TRANSPORT_PROTO_TLS:
107       s = format (s, "J");
108       break;
109     case TRANSPORT_PROTO_UDPC:
110       s = format (s, "U");
111       break;
112     case TRANSPORT_PROTO_QUIC:
113       s = format (s, "Q");
114       break;
115     default:
116       s = format (s, "?");
117       break;
118     }
119   return s;
120 }
121
122 u8 *
123 format_transport_connection (u8 * s, va_list * args)
124 {
125   u32 transport_proto = va_arg (*args, u32);
126   u32 conn_index = va_arg (*args, u32);
127   u32 thread_index = va_arg (*args, u32);
128   u32 verbose = va_arg (*args, u32);
129   transport_proto_vft_t *tp_vft;
130   transport_connection_t *tc;
131   u32 indent;
132
133   tp_vft = transport_protocol_get_vft (transport_proto);
134   if (!tp_vft)
135     return s;
136
137   s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
138               verbose);
139   tc = tp_vft->get_connection (conn_index, thread_index);
140   if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
141     {
142       indent = format_get_indent (s) + 1;
143       s = format (s, "%Upacer: %U\n", format_white_space, indent,
144                   format_transport_pacer, &tc->pacer);
145     }
146   return s;
147 }
148
149 u8 *
150 format_transport_listen_connection (u8 * s, va_list * args)
151 {
152   u32 transport_proto = va_arg (*args, u32);
153   transport_proto_vft_t *tp_vft;
154
155   tp_vft = transport_protocol_get_vft (transport_proto);
156   if (!tp_vft)
157     return s;
158
159   s = (tp_vft->format_listener) (s, args);
160   return s;
161 }
162
163 u8 *
164 format_transport_half_open_connection (u8 * s, va_list * args)
165 {
166   u32 transport_proto = va_arg (*args, u32);
167   u32 listen_index = va_arg (*args, u32);
168   transport_proto_vft_t *tp_vft;
169
170   tp_vft = transport_protocol_get_vft (transport_proto);
171   if (!tp_vft)
172     return s;
173
174   s = format (s, "%U", tp_vft->format_half_open, listen_index);
175   return s;
176 }
177
178 uword
179 unformat_transport_proto (unformat_input_t * input, va_list * args)
180 {
181   u32 *proto = va_arg (*args, u32 *);
182   if (unformat (input, "tcp"))
183     *proto = TRANSPORT_PROTO_TCP;
184   else if (unformat (input, "TCP"))
185     *proto = TRANSPORT_PROTO_TCP;
186   else if (unformat (input, "udpc"))
187     *proto = TRANSPORT_PROTO_UDPC;
188   else if (unformat (input, "UDPC"))
189     *proto = TRANSPORT_PROTO_UDPC;
190   else if (unformat (input, "udp"))
191     *proto = TRANSPORT_PROTO_UDP;
192   else if (unformat (input, "UDP"))
193     *proto = TRANSPORT_PROTO_UDP;
194   else if (unformat (input, "sctp"))
195     *proto = TRANSPORT_PROTO_SCTP;
196   else if (unformat (input, "SCTP"))
197     *proto = TRANSPORT_PROTO_SCTP;
198   else if (unformat (input, "tls"))
199     *proto = TRANSPORT_PROTO_TLS;
200   else if (unformat (input, "TLS"))
201     *proto = TRANSPORT_PROTO_TLS;
202   else if (unformat (input, "quic"))
203     *proto = TRANSPORT_PROTO_QUIC;
204   else if (unformat (input, "QUIC"))
205     *proto = TRANSPORT_PROTO_QUIC;
206   else
207     return 0;
208   return 1;
209 }
210
211 u32
212 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
213                            ip46_address_t * ip, u16 port)
214 {
215   clib_bihash_kv_24_8_t kv;
216   int rv;
217
218   kv.key[0] = ip->as_u64[0];
219   kv.key[1] = ip->as_u64[1];
220   kv.key[2] = (u64) port << 8 | (u64) proto;
221
222   rv = clib_bihash_search_inline_24_8 (ht, &kv);
223   if (rv == 0)
224     return kv.value;
225
226   return ENDPOINT_INVALID_INDEX;
227 }
228
229 void
230 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
231                               transport_endpoint_t * te, u32 value)
232 {
233   clib_bihash_kv_24_8_t kv;
234
235   kv.key[0] = te->ip.as_u64[0];
236   kv.key[1] = te->ip.as_u64[1];
237   kv.key[2] = (u64) te->port << 8 | (u64) proto;
238   kv.value = value;
239
240   clib_bihash_add_del_24_8 (ht, &kv, 1);
241 }
242
243 void
244 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
245                               transport_endpoint_t * te)
246 {
247   clib_bihash_kv_24_8_t kv;
248
249   kv.key[0] = te->ip.as_u64[0];
250   kv.key[1] = te->ip.as_u64[1];
251   kv.key[2] = (u64) te->port << 8 | (u64) proto;
252
253   clib_bihash_add_del_24_8 (ht, &kv, 0);
254 }
255
256 /**
257  * Register transport virtual function table.
258  *
259  * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
260  * @param vft - virtual function table for transport proto
261  * @param fib_proto - network layer protocol
262  * @param output_node - output node index that session layer will hand off
263  *                      buffers to, for requested fib proto
264  */
265 void
266 transport_register_protocol (transport_proto_t transport_proto,
267                              const transport_proto_vft_t * vft,
268                              fib_protocol_t fib_proto, u32 output_node)
269 {
270   u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
271
272   vec_validate (tp_vfts, transport_proto);
273   tp_vfts[transport_proto] = *vft;
274
275   session_register_transport (transport_proto, vft, is_ip4, output_node);
276 }
277
278 /**
279  * Get transport virtual function table
280  *
281  * @param type - session type (not protocol type)
282  */
283 transport_proto_vft_t *
284 transport_protocol_get_vft (transport_proto_t transport_proto)
285 {
286   if (transport_proto >= vec_len (tp_vfts))
287     return 0;
288   return &tp_vfts[transport_proto];
289 }
290
291 u8
292 transport_half_open_has_fifos (transport_proto_t tp)
293 {
294   return tp_vfts[tp].transport_options.half_open_has_fifos;
295 }
296
297 transport_service_type_t
298 transport_protocol_service_type (transport_proto_t tp)
299 {
300   return tp_vfts[tp].transport_options.service_type;
301 }
302
303 transport_tx_fn_type_t
304 transport_protocol_tx_fn_type (transport_proto_t tp)
305 {
306   return tp_vfts[tp].transport_options.tx_type;
307 }
308
309 void
310 transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
311 {
312   tp_vfts[tp].cleanup (conn_index, thread_index);
313 }
314
315 int
316 transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
317 {
318   return tp_vfts[tp].connect (tep);
319 }
320
321 void
322 transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
323 {
324   tp_vfts[tp].close (conn_index, thread_index);
325 }
326
327 void
328 transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
329 {
330   if (tp_vfts[tp].reset)
331     tp_vfts[tp].reset (conn_index, thread_index);
332   else
333     tp_vfts[tp].close (conn_index, thread_index);
334 }
335
336 u32
337 transport_start_listen (transport_proto_t tp, u32 session_index,
338                         transport_endpoint_t * tep)
339 {
340   return tp_vfts[tp].start_listen (session_index, tep);
341 }
342
343 u32
344 transport_stop_listen (transport_proto_t tp, u32 conn_index)
345 {
346   return tp_vfts[tp].stop_listen (conn_index);
347 }
348
349 u8
350 transport_protocol_is_cl (transport_proto_t tp)
351 {
352   return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
353 }
354
355 always_inline void
356 default_get_transport_endpoint (transport_connection_t * tc,
357                                 transport_endpoint_t * tep, u8 is_lcl)
358 {
359   if (is_lcl)
360     {
361       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));
364     }
365   else
366     {
367       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));
370     }
371 }
372
373 void
374 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
375                         u32 thread_index, transport_endpoint_t * tep,
376                         u8 is_lcl)
377 {
378   if (tp_vfts[tp].get_transport_endpoint)
379     tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
380                                         is_lcl);
381   else
382     {
383       transport_connection_t *tc;
384       tc = transport_get_connection (tp, conn_index, thread_index);
385       default_get_transport_endpoint (tc, tep, is_lcl);
386     }
387 }
388
389 void
390 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
391                                  transport_endpoint_t * tep, u8 is_lcl)
392 {
393   if (tp_vfts[tp].get_transport_listener_endpoint)
394     tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
395   else
396     {
397       transport_connection_t *tc;
398       tc = transport_get_listener (tp, conn_index);
399       default_get_transport_endpoint (tc, tep, is_lcl);
400     }
401 }
402
403 #define PORT_MASK ((1 << 16)- 1)
404
405 void
406 transport_endpoint_del (u32 tepi)
407 {
408   clib_spinlock_lock_if_init (&local_endpoints_lock);
409   pool_put_index (local_endpoints, tepi);
410   clib_spinlock_unlock_if_init (&local_endpoints_lock);
411 }
412
413 always_inline transport_endpoint_t *
414 transport_endpoint_new (void)
415 {
416   transport_endpoint_t *tep;
417   pool_get_zero (local_endpoints, tep);
418   return tep;
419 }
420
421 void
422 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
423 {
424   u32 tepi;
425   transport_endpoint_t *tep;
426
427   /* Cleanup local endpoint if this was an active connect */
428   tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
429                                     clib_net_to_host_u16 (port));
430   if (tepi != ENDPOINT_INVALID_INDEX)
431     {
432       tep = pool_elt_at_index (local_endpoints, tepi);
433       transport_endpoint_table_del (&local_endpoints_table, proto, tep);
434       transport_endpoint_del (tepi);
435     }
436 }
437
438 static void
439 transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
440 {
441   transport_endpoint_t *tep;
442   clib_spinlock_lock_if_init (&local_endpoints_lock);
443   tep = transport_endpoint_new ();
444   clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
445   tep->port = port;
446   transport_endpoint_table_add (&local_endpoints_table, proto, tep,
447                                 tep - local_endpoints);
448   clib_spinlock_unlock_if_init (&local_endpoints_lock);
449 }
450
451 /**
452  * Allocate local port and add if successful add entry to local endpoint
453  * table to mark the pair as used.
454  */
455 int
456 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
457 {
458   u16 min = 1024, max = 65535;  /* XXX configurable ? */
459   int tries, limit;
460   u32 tei;
461
462   limit = max - min;
463
464   /* Only support active opens from thread 0 */
465   ASSERT (vlib_get_thread_index () == 0);
466
467   /* Search for first free slot */
468   for (tries = 0; tries < limit; tries++)
469     {
470       u16 port = 0;
471
472       /* Find a port in the specified range */
473       while (1)
474         {
475           port = random_u32 (&port_allocator_seed) & PORT_MASK;
476           if (PREDICT_TRUE (port >= min && port < max))
477             break;
478         }
479
480       /* Look it up. If not found, we're done */
481       tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
482                                        port);
483       if (tei == ENDPOINT_INVALID_INDEX)
484         {
485           transport_endpoint_mark_used (proto, ip, port);
486           return port;
487         }
488     }
489   return -1;
490 }
491
492 static clib_error_t *
493 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
494 {
495   if (is_ip4)
496     {
497       ip4_address_t *ip4;
498       ip4 = ip_interface_get_first_ip (sw_if_index, 1);
499       if (!ip4)
500         return clib_error_return (0, "no routable ip4 address on %U",
501                                   format_vnet_sw_if_index_name,
502                                   vnet_get_main (), sw_if_index);
503       addr->ip4.as_u32 = ip4->as_u32;
504     }
505   else
506     {
507       ip6_address_t *ip6;
508       ip6 = ip_interface_get_first_ip (sw_if_index, 0);
509       if (ip6 == 0)
510         return clib_error_return (0, "no routable ip6 addresses on %U",
511                                   format_vnet_sw_if_index_name,
512                                   vnet_get_main (), sw_if_index);
513       clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
514     }
515   return 0;
516 }
517
518 static clib_error_t *
519 transport_find_local_ip_for_remote (u32 sw_if_index,
520                                     transport_endpoint_t * rmt,
521                                     ip46_address_t * lcl_addr)
522 {
523   fib_node_index_t fei;
524   fib_prefix_t prefix;
525
526   if (sw_if_index == ENDPOINT_INVALID_INDEX)
527     {
528       /* Find a FIB path to the destination */
529       clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
530       prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
531       prefix.fp_len = rmt->is_ip4 ? 32 : 128;
532
533       ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
534       fei = fib_table_lookup (rmt->fib_index, &prefix);
535
536       /* Couldn't find route to destination. Bail out. */
537       if (fei == FIB_NODE_INDEX_INVALID)
538         return clib_error_return (0, "no route to %U", format_ip46_address,
539                                   &rmt->ip, (rmt->is_ip4 == 0) + 1);
540
541       sw_if_index = fib_entry_get_resolving_interface (fei);
542       if (sw_if_index == ENDPOINT_INVALID_INDEX)
543         return clib_error_return (0, "no resolving interface for %U",
544                                   format_ip46_address, &rmt->ip,
545                                   (rmt->is_ip4 == 0) + 1);
546     }
547
548   clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
549   return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
550 }
551
552 int
553 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
554                                 ip46_address_t * lcl_addr, u16 * lcl_port)
555 {
556   transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
557   clib_error_t *error;
558   int port;
559   u32 tei;
560
561   /*
562    * Find the local address
563    */
564   if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
565     {
566       error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
567                                                   rmt, lcl_addr);
568       if (error)
569         {
570           clib_error_report (error);
571           return -1;
572         }
573     }
574   else
575     {
576       /* Assume session layer vetted this address */
577       clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
578                         sizeof (rmt_cfg->peer.ip));
579     }
580
581   /*
582    * Allocate source port
583    */
584   if (rmt_cfg->peer.port == 0)
585     {
586       port = transport_alloc_local_port (proto, lcl_addr);
587       if (port < 1)
588         {
589           clib_warning ("Failed to allocate src port");
590           return -1;
591         }
592       *lcl_port = port;
593     }
594   else
595     {
596       port = clib_net_to_host_u16 (rmt_cfg->peer.port);
597       tei = transport_endpoint_lookup (&local_endpoints_table, proto,
598                                        lcl_addr, port);
599       if (tei != ENDPOINT_INVALID_INDEX)
600         return -1;
601
602       transport_endpoint_mark_used (proto, lcl_addr, port);
603       *lcl_port = port;
604     }
605
606   return 0;
607 }
608
609 #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
610 #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
611
612 u8 *
613 format_transport_pacer (u8 * s, va_list * args)
614 {
615   spacer_t *pacer = va_arg (*args, spacer_t *);
616
617   s = format (s, "bucket %u tokens/period %.3f last_update %x",
618               pacer->bucket, pacer->tokens_per_period, pacer->last_update);
619   return s;
620 }
621
622 static inline u32
623 spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
624 {
625   u64 n_periods = norm_time_now - pacer->last_update;
626   u64 inc;
627
628   if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
629     {
630       pacer->last_update = norm_time_now;
631       pacer->bucket += inc;
632     }
633
634   return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
635 }
636
637 static inline void
638 spacer_update_bucket (spacer_t * pacer, u32 bytes)
639 {
640   ASSERT (pacer->bucket >= bytes);
641   pacer->bucket -= bytes;
642 }
643
644 static inline void
645 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
646 {
647   ASSERT (rate_bytes_per_sec != 0);
648   pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
649 }
650
651 static inline u64
652 spacer_pace_rate (spacer_t * pacer)
653 {
654   return pacer->tokens_per_period * transport_pacer_period;
655 }
656
657 void
658 transport_connection_tx_pacer_reset (transport_connection_t * tc,
659                                      u32 rate_bytes_per_sec,
660                                      u32 start_bucket, u64 time_now)
661 {
662   spacer_t *pacer = &tc->pacer;
663   spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
664   pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
665   pacer->bucket = start_bucket;
666 }
667
668 void
669 transport_connection_tx_pacer_init (transport_connection_t * tc,
670                                     u32 rate_bytes_per_sec,
671                                     u32 initial_bucket)
672 {
673   vlib_main_t *vm = vlib_get_main ();
674   tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
675   transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
676                                        initial_bucket,
677                                        vm->clib_time.last_cpu_time);
678 }
679
680 void
681 transport_connection_tx_pacer_update (transport_connection_t * tc,
682                                       u64 bytes_per_sec)
683 {
684   spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
685 }
686
687 u32
688 transport_connection_tx_pacer_burst (transport_connection_t * tc,
689                                      u64 time_now)
690 {
691   time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
692   return spacer_max_burst (&tc->pacer, time_now);
693 }
694
695 u32
696 transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
697                                 u16 mss)
698 {
699   u32 snd_space, max_paced_burst;
700
701   snd_space = tp_vfts[tc->proto].send_space (tc);
702   if (transport_connection_is_tx_paced (tc))
703     {
704       time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
705       max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
706       max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
707       snd_space = clib_min (snd_space, max_paced_burst);
708       snd_space = snd_space - snd_space % mss;
709     }
710   return snd_space;
711 }
712
713 u64
714 transport_connection_tx_pacer_rate (transport_connection_t * tc)
715 {
716   return spacer_pace_rate (&tc->pacer);
717 }
718
719 void
720 transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
721 {
722   if (transport_connection_is_tx_paced (tc))
723     spacer_update_bucket (&tc->pacer, bytes);
724 }
725
726 void
727 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
728                                             u32 bytes)
729 {
730   spacer_update_bucket (&tc->pacer, bytes);
731 }
732
733 void
734 transport_init_tx_pacers_period (void)
735 {
736   f64 cpu_freq = os_cpu_clock_frequency ();
737   transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
738 }
739
740 void
741 transport_update_time (f64 time_now, u8 thread_index)
742 {
743   transport_proto_vft_t *vft;
744   vec_foreach (vft, tp_vfts)
745   {
746     if (vft->update_time)
747       (vft->update_time) (time_now, thread_index);
748   }
749 }
750
751 void
752 transport_enable_disable (vlib_main_t * vm, u8 is_en)
753 {
754   transport_proto_vft_t *vft;
755   vec_foreach (vft, tp_vfts)
756   {
757     if (vft->enable)
758       (vft->enable) (vm, is_en);
759   }
760 }
761
762 void
763 transport_init (void)
764 {
765   vlib_thread_main_t *vtm = vlib_get_thread_main ();
766   session_main_t *smm = vnet_get_session_main ();
767   u32 num_threads;
768
769   if (smm->local_endpoints_table_buckets == 0)
770     smm->local_endpoints_table_buckets = 250000;
771   if (smm->local_endpoints_table_memory == 0)
772     smm->local_endpoints_table_memory = 512 << 20;
773
774   /* Initialize [port-allocator] random number seed */
775   port_allocator_seed = (u32) clib_cpu_time_now ();
776
777   clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
778                          smm->local_endpoints_table_buckets,
779                          smm->local_endpoints_table_memory);
780   num_threads = 1 /* main thread */  + vtm->n_threads;
781   if (num_threads > 1)
782     clib_spinlock_init (&local_endpoints_lock);
783 }
784
785 /*
786  * fd.io coding-style-patch-verification: ON
787  *
788  * Local Variables:
789  * eval: (c-set-style "gnu")
790  * End:
791  */