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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/transport.h>
17 #include <vnet/session/session.h>
18 #include <vnet/fib/fib.h>
21 * Per-type vector of transport protocol virtual function tables
23 transport_proto_vft_t *tp_vfts;
28 static u32 port_allocator_seed;
31 * Local endpoints table
33 static transport_endpoint_table_t local_endpoints_table;
36 * Pool of local endpoints
38 static transport_endpoint_t *local_endpoints;
41 * Local endpoints pool lock
43 static clib_spinlock_t local_endpoints_lock;
46 * Period used by transport pacers. Initialized by session layer
48 static double transport_pacer_period;
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)
55 format_transport_proto (u8 * s, va_list * args)
57 u32 transport_proto = va_arg (*args, u32);
58 switch (transport_proto)
60 case TRANSPORT_PROTO_TCP:
61 s = format (s, "TCP");
63 case TRANSPORT_PROTO_UDP:
64 s = format (s, "UDP");
66 case TRANSPORT_PROTO_SCTP:
67 s = format (s, "SCTP");
69 case TRANSPORT_PROTO_UDPC:
70 s = format (s, "UDPC");
72 case TRANSPORT_PROTO_QUIC:
73 s = format (s, "QUIC");
80 format_transport_proto_short (u8 * s, va_list * args)
82 u32 transport_proto = va_arg (*args, u32);
83 switch (transport_proto)
85 case TRANSPORT_PROTO_TCP:
88 case TRANSPORT_PROTO_UDP:
91 case TRANSPORT_PROTO_SCTP:
94 case TRANSPORT_PROTO_UDPC:
97 case TRANSPORT_PROTO_QUIC:
105 format_transport_connection (u8 * s, va_list * args)
107 u32 transport_proto = va_arg (*args, u32);
108 u32 conn_index = va_arg (*args, u32);
109 u32 thread_index = va_arg (*args, u32);
110 u32 verbose = va_arg (*args, u32);
111 transport_proto_vft_t *tp_vft;
112 transport_connection_t *tc;
115 tp_vft = transport_protocol_get_vft (transport_proto);
119 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
121 tc = tp_vft->get_connection (conn_index, thread_index);
122 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
124 indent = format_get_indent (s) + 1;
125 s = format (s, "%Upacer: %U\n", format_white_space, indent,
126 format_transport_pacer, &tc->pacer);
132 format_transport_listen_connection (u8 * s, va_list * args)
134 u32 transport_proto = va_arg (*args, u32);
135 transport_proto_vft_t *tp_vft;
137 tp_vft = transport_protocol_get_vft (transport_proto);
141 s = (tp_vft->format_listener) (s, args);
146 format_transport_half_open_connection (u8 * s, va_list * args)
148 u32 transport_proto = va_arg (*args, u32);
149 u32 listen_index = va_arg (*args, u32);
150 transport_proto_vft_t *tp_vft;
152 tp_vft = transport_protocol_get_vft (transport_proto);
156 s = format (s, "%U", tp_vft->format_half_open, listen_index);
161 unformat_transport_proto (unformat_input_t * input, va_list * args)
163 u32 *proto = va_arg (*args, u32 *);
164 if (unformat (input, "tcp"))
165 *proto = TRANSPORT_PROTO_TCP;
166 else if (unformat (input, "TCP"))
167 *proto = TRANSPORT_PROTO_TCP;
168 else if (unformat (input, "udpc"))
169 *proto = TRANSPORT_PROTO_UDPC;
170 else if (unformat (input, "UDPC"))
171 *proto = TRANSPORT_PROTO_UDPC;
172 else if (unformat (input, "udp"))
173 *proto = TRANSPORT_PROTO_UDP;
174 else if (unformat (input, "UDP"))
175 *proto = TRANSPORT_PROTO_UDP;
176 else if (unformat (input, "sctp"))
177 *proto = TRANSPORT_PROTO_SCTP;
178 else if (unformat (input, "SCTP"))
179 *proto = TRANSPORT_PROTO_SCTP;
180 else if (unformat (input, "tls"))
181 *proto = TRANSPORT_PROTO_TLS;
182 else if (unformat (input, "TLS"))
183 *proto = TRANSPORT_PROTO_TLS;
184 else if (unformat (input, "quic"))
185 *proto = TRANSPORT_PROTO_QUIC;
186 else if (unformat (input, "QUIC"))
187 *proto = TRANSPORT_PROTO_QUIC;
194 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
195 ip46_address_t * ip, u16 port)
197 clib_bihash_kv_24_8_t kv;
200 kv.key[0] = ip->as_u64[0];
201 kv.key[1] = ip->as_u64[1];
202 kv.key[2] = (u64) port << 8 | (u64) proto;
204 rv = clib_bihash_search_inline_24_8 (ht, &kv);
208 return ENDPOINT_INVALID_INDEX;
212 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
213 transport_endpoint_t * te, u32 value)
215 clib_bihash_kv_24_8_t kv;
217 kv.key[0] = te->ip.as_u64[0];
218 kv.key[1] = te->ip.as_u64[1];
219 kv.key[2] = (u64) te->port << 8 | (u64) proto;
222 clib_bihash_add_del_24_8 (ht, &kv, 1);
226 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
227 transport_endpoint_t * te)
229 clib_bihash_kv_24_8_t kv;
231 kv.key[0] = te->ip.as_u64[0];
232 kv.key[1] = te->ip.as_u64[1];
233 kv.key[2] = (u64) te->port << 8 | (u64) proto;
235 clib_bihash_add_del_24_8 (ht, &kv, 0);
239 * Register transport virtual function table.
241 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
242 * @param vft - virtual function table for transport proto
243 * @param fib_proto - network layer protocol
244 * @param output_node - output node index that session layer will hand off
245 * buffers to, for requested fib proto
248 transport_register_protocol (transport_proto_t transport_proto,
249 const transport_proto_vft_t * vft,
250 fib_protocol_t fib_proto, u32 output_node)
252 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
254 vec_validate (tp_vfts, transport_proto);
255 tp_vfts[transport_proto] = *vft;
257 session_register_transport (transport_proto, vft, is_ip4, output_node);
261 * Get transport virtual function table
263 * @param type - session type (not protocol type)
265 transport_proto_vft_t *
266 transport_protocol_get_vft (transport_proto_t transport_proto)
268 if (transport_proto >= vec_len (tp_vfts))
270 return &tp_vfts[transport_proto];
273 transport_service_type_t
274 transport_protocol_service_type (transport_proto_t tp)
276 return tp_vfts[tp].service_type;
279 transport_tx_fn_type_t
280 transport_protocol_tx_fn_type (transport_proto_t tp)
282 return tp_vfts[tp].tx_type;
286 transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
288 tp_vfts[tp].cleanup (conn_index, thread_index);
292 transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
294 return tp_vfts[tp].connect (tep);
298 transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
300 tp_vfts[tp].close (conn_index, thread_index);
304 transport_start_listen (transport_proto_t tp, u32 session_index,
305 transport_endpoint_t * tep)
307 return tp_vfts[tp].start_listen (session_index, tep);
311 transport_stop_listen (transport_proto_t tp, u32 conn_index)
313 return tp_vfts[tp].stop_listen (conn_index);
317 transport_protocol_is_cl (transport_proto_t tp)
319 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
323 default_get_transport_endpoint (transport_connection_t * tc,
324 ip46_address_t * ip, u16 * port, u8 * is_ip4,
329 *port = tc->lcl_port;
330 *is_ip4 = tc->is_ip4;
331 clib_memcpy_fast (ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
335 *port = tc->rmt_port;
336 *is_ip4 = tc->is_ip4;
337 clib_memcpy_fast (ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
342 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
343 u32 thread_index, ip46_address_t * ip, u16 * port,
344 u8 * is_ip4, u8 is_lcl)
346 if (tp_vfts[tp].get_transport_endpoint)
347 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, ip, port,
351 transport_connection_t *tc;
352 tc = transport_get_connection (tp, conn_index, thread_index);
353 default_get_transport_endpoint (tc, ip, port, is_ip4, is_lcl);
358 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
359 ip46_address_t * ip, u16 * port, u8 * is_ip4,
362 if (tp_vfts[tp].get_transport_listener_endpoint)
363 tp_vfts[tp].get_transport_listener_endpoint (conn_index, ip, port, is_ip4,
367 transport_connection_t *tc;
368 tc = transport_get_listener (tp, conn_index);
369 default_get_transport_endpoint (tc, ip, port, is_ip4, is_lcl);
373 #define PORT_MASK ((1 << 16)- 1)
376 transport_endpoint_del (u32 tepi)
378 clib_spinlock_lock_if_init (&local_endpoints_lock);
379 pool_put_index (local_endpoints, tepi);
380 clib_spinlock_unlock_if_init (&local_endpoints_lock);
383 always_inline transport_endpoint_t *
384 transport_endpoint_new (void)
386 transport_endpoint_t *tep;
387 pool_get_zero (local_endpoints, tep);
392 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
395 transport_endpoint_t *tep;
397 /* Cleanup local endpoint if this was an active connect */
398 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
399 clib_net_to_host_u16 (port));
400 if (tepi != ENDPOINT_INVALID_INDEX)
402 tep = pool_elt_at_index (local_endpoints, tepi);
403 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
404 transport_endpoint_del (tepi);
409 transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
411 transport_endpoint_t *tep;
412 clib_spinlock_lock_if_init (&local_endpoints_lock);
413 tep = transport_endpoint_new ();
414 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
416 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
417 tep - local_endpoints);
418 clib_spinlock_unlock_if_init (&local_endpoints_lock);
422 * Allocate local port and add if successful add entry to local endpoint
423 * table to mark the pair as used.
426 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
428 u16 min = 1024, max = 65535; /* XXX configurable ? */
434 /* Only support active opens from thread 0 */
435 ASSERT (vlib_get_thread_index () == 0);
437 /* Search for first free slot */
438 for (tries = 0; tries < limit; tries++)
442 /* Find a port in the specified range */
445 port = random_u32 (&port_allocator_seed) & PORT_MASK;
446 if (PREDICT_TRUE (port >= min && port < max))
450 /* Look it up. If not found, we're done */
451 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
453 if (tei == ENDPOINT_INVALID_INDEX)
455 transport_endpoint_mark_used (proto, ip, port);
462 static clib_error_t *
463 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
468 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
470 return clib_error_return (0, "no routable ip4 address on %U",
471 format_vnet_sw_if_index_name,
472 vnet_get_main (), sw_if_index);
473 addr->ip4.as_u32 = ip4->as_u32;
478 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
480 return clib_error_return (0, "no routable ip6 addresses on %U",
481 format_vnet_sw_if_index_name,
482 vnet_get_main (), sw_if_index);
483 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
488 static clib_error_t *
489 transport_find_local_ip_for_remote (u32 sw_if_index,
490 transport_endpoint_t * rmt,
491 ip46_address_t * lcl_addr)
493 fib_node_index_t fei;
496 if (sw_if_index == ENDPOINT_INVALID_INDEX)
498 /* Find a FIB path to the destination */
499 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
500 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
501 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
503 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
504 fei = fib_table_lookup (rmt->fib_index, &prefix);
506 /* Couldn't find route to destination. Bail out. */
507 if (fei == FIB_NODE_INDEX_INVALID)
508 return clib_error_return (0, "no route to %U", format_ip46_address,
509 &rmt->ip, (rmt->is_ip4 == 0) + 1);
511 sw_if_index = fib_entry_get_resolving_interface (fei);
512 if (sw_if_index == ENDPOINT_INVALID_INDEX)
513 return clib_error_return (0, "no resolving interface for %U",
514 format_ip46_address, &rmt->ip,
515 (rmt->is_ip4 == 0) + 1);
518 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
519 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
523 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
524 ip46_address_t * lcl_addr, u16 * lcl_port)
526 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
532 * Find the local address
534 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
536 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
540 clib_error_report (error);
546 /* Assume session layer vetted this address */
547 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
548 sizeof (rmt_cfg->peer.ip));
552 * Allocate source port
554 if (rmt_cfg->peer.port == 0)
556 port = transport_alloc_local_port (proto, lcl_addr);
559 clib_warning ("Failed to allocate src port");
566 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
567 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
569 if (tei != ENDPOINT_INVALID_INDEX)
572 transport_endpoint_mark_used (proto, lcl_addr, port);
579 #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
580 #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
583 format_transport_pacer (u8 * s, va_list * args)
585 spacer_t *pacer = va_arg (*args, spacer_t *);
587 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
588 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
594 spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
596 u64 n_periods = norm_time_now - pacer->last_update;
599 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
601 pacer->last_update = norm_time_now;
602 pacer->bucket += inc;
605 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
609 spacer_update_bucket (spacer_t * pacer, u32 bytes)
611 ASSERT (pacer->bucket >= bytes);
612 pacer->bucket -= bytes;
616 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
618 ASSERT (rate_bytes_per_sec != 0);
619 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
623 transport_connection_tx_pacer_reset (transport_connection_t * tc,
624 u32 rate_bytes_per_sec,
625 u32 start_bucket, u64 time_now)
627 spacer_t *pacer = &tc->pacer;
628 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
629 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
630 pacer->bucket = start_bucket;
634 transport_connection_tx_pacer_init (transport_connection_t * tc,
635 u32 rate_bytes_per_sec,
638 vlib_main_t *vm = vlib_get_main ();
639 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
640 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
642 vm->clib_time.last_cpu_time);
646 transport_connection_tx_pacer_update (transport_connection_t * tc,
649 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
653 transport_connection_tx_pacer_burst (transport_connection_t * tc,
656 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
657 return spacer_max_burst (&tc->pacer, time_now);
661 transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
664 u32 snd_space, max_paced_burst;
666 snd_space = tp_vfts[tc->proto].send_space (tc);
667 if (transport_connection_is_tx_paced (tc))
669 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
670 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
671 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
672 snd_space = clib_min (snd_space, max_paced_burst);
673 snd_space = snd_space - snd_space % mss;
679 transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
681 tc->stats.tx_bytes += bytes;
682 if (transport_connection_is_tx_paced (tc))
683 spacer_update_bucket (&tc->pacer, bytes);
687 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
690 spacer_update_bucket (&tc->pacer, bytes);
694 transport_init_tx_pacers_period (void)
696 f64 cpu_freq = os_cpu_clock_frequency ();
697 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
701 transport_update_time (f64 time_now, u8 thread_index)
703 transport_proto_vft_t *vft;
704 vec_foreach (vft, tp_vfts)
706 if (vft->update_time)
707 (vft->update_time) (time_now, thread_index);
712 transport_enable_disable (vlib_main_t * vm, u8 is_en)
714 transport_proto_vft_t *vft;
715 vec_foreach (vft, tp_vfts)
718 (vft->enable) (vm, is_en);
723 transport_init (void)
725 vlib_thread_main_t *vtm = vlib_get_thread_main ();
726 session_main_t *smm = vnet_get_session_main ();
729 if (smm->local_endpoints_table_buckets == 0)
730 smm->local_endpoints_table_buckets = 250000;
731 if (smm->local_endpoints_table_memory == 0)
732 smm->local_endpoints_table_memory = 512 << 20;
734 /* Initialize [port-allocator] random number seed */
735 port_allocator_seed = (u32) clib_cpu_time_now ();
737 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
738 smm->local_endpoints_table_buckets,
739 smm->local_endpoints_table_memory);
740 num_threads = 1 /* main thread */ + vtm->n_threads;
742 clib_spinlock_init (&local_endpoints_lock);
746 * fd.io coding-style-patch-verification: ON
749 * eval: (c-set-style "gnu")