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 transport_endpoint_t * tep, u8 is_lcl)
328 tep->port = tc->lcl_port;
329 tep->is_ip4 = tc->is_ip4;
330 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
334 tep->port = tc->rmt_port;
335 tep->is_ip4 = tc->is_ip4;
336 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
341 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
342 u32 thread_index, transport_endpoint_t * tep,
345 if (tp_vfts[tp].get_transport_endpoint)
346 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
350 transport_connection_t *tc;
351 tc = transport_get_connection (tp, conn_index, thread_index);
352 default_get_transport_endpoint (tc, tep, is_lcl);
357 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
358 transport_endpoint_t * tep, u8 is_lcl)
360 if (tp_vfts[tp].get_transport_listener_endpoint)
361 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
364 transport_connection_t *tc;
365 tc = transport_get_listener (tp, conn_index);
366 default_get_transport_endpoint (tc, tep, is_lcl);
370 #define PORT_MASK ((1 << 16)- 1)
373 transport_endpoint_del (u32 tepi)
375 clib_spinlock_lock_if_init (&local_endpoints_lock);
376 pool_put_index (local_endpoints, tepi);
377 clib_spinlock_unlock_if_init (&local_endpoints_lock);
380 always_inline transport_endpoint_t *
381 transport_endpoint_new (void)
383 transport_endpoint_t *tep;
384 pool_get_zero (local_endpoints, tep);
389 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
392 transport_endpoint_t *tep;
394 /* Cleanup local endpoint if this was an active connect */
395 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
396 clib_net_to_host_u16 (port));
397 if (tepi != ENDPOINT_INVALID_INDEX)
399 tep = pool_elt_at_index (local_endpoints, tepi);
400 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
401 transport_endpoint_del (tepi);
406 transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
408 transport_endpoint_t *tep;
409 clib_spinlock_lock_if_init (&local_endpoints_lock);
410 tep = transport_endpoint_new ();
411 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
413 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
414 tep - local_endpoints);
415 clib_spinlock_unlock_if_init (&local_endpoints_lock);
419 * Allocate local port and add if successful add entry to local endpoint
420 * table to mark the pair as used.
423 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
425 u16 min = 1024, max = 65535; /* XXX configurable ? */
431 /* Only support active opens from thread 0 */
432 ASSERT (vlib_get_thread_index () == 0);
434 /* Search for first free slot */
435 for (tries = 0; tries < limit; tries++)
439 /* Find a port in the specified range */
442 port = random_u32 (&port_allocator_seed) & PORT_MASK;
443 if (PREDICT_TRUE (port >= min && port < max))
447 /* Look it up. If not found, we're done */
448 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
450 if (tei == ENDPOINT_INVALID_INDEX)
452 transport_endpoint_mark_used (proto, ip, port);
459 static clib_error_t *
460 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
465 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
467 return clib_error_return (0, "no routable ip4 address on %U",
468 format_vnet_sw_if_index_name,
469 vnet_get_main (), sw_if_index);
470 addr->ip4.as_u32 = ip4->as_u32;
475 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
477 return clib_error_return (0, "no routable ip6 addresses on %U",
478 format_vnet_sw_if_index_name,
479 vnet_get_main (), sw_if_index);
480 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
485 static clib_error_t *
486 transport_find_local_ip_for_remote (u32 sw_if_index,
487 transport_endpoint_t * rmt,
488 ip46_address_t * lcl_addr)
490 fib_node_index_t fei;
493 if (sw_if_index == ENDPOINT_INVALID_INDEX)
495 /* Find a FIB path to the destination */
496 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
497 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
498 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
500 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
501 fei = fib_table_lookup (rmt->fib_index, &prefix);
503 /* Couldn't find route to destination. Bail out. */
504 if (fei == FIB_NODE_INDEX_INVALID)
505 return clib_error_return (0, "no route to %U", format_ip46_address,
506 &rmt->ip, (rmt->is_ip4 == 0) + 1);
508 sw_if_index = fib_entry_get_resolving_interface (fei);
509 if (sw_if_index == ENDPOINT_INVALID_INDEX)
510 return clib_error_return (0, "no resolving interface for %U",
511 format_ip46_address, &rmt->ip,
512 (rmt->is_ip4 == 0) + 1);
515 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
516 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
520 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
521 ip46_address_t * lcl_addr, u16 * lcl_port)
523 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
529 * Find the local address
531 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
533 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
537 clib_error_report (error);
543 /* Assume session layer vetted this address */
544 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
545 sizeof (rmt_cfg->peer.ip));
549 * Allocate source port
551 if (rmt_cfg->peer.port == 0)
553 port = transport_alloc_local_port (proto, lcl_addr);
556 clib_warning ("Failed to allocate src port");
563 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
564 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
566 if (tei != ENDPOINT_INVALID_INDEX)
569 transport_endpoint_mark_used (proto, lcl_addr, port);
576 #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
577 #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
580 format_transport_pacer (u8 * s, va_list * args)
582 spacer_t *pacer = va_arg (*args, spacer_t *);
584 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
585 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
591 spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
593 u64 n_periods = norm_time_now - pacer->last_update;
596 if (n_periods > 0 && (inc = n_periods * pacer->tokens_per_period) > 10)
598 pacer->last_update = norm_time_now;
599 pacer->bucket += inc;
602 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
606 spacer_update_bucket (spacer_t * pacer, u32 bytes)
608 ASSERT (pacer->bucket >= bytes);
609 pacer->bucket -= bytes;
613 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
615 ASSERT (rate_bytes_per_sec != 0);
616 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
620 transport_connection_tx_pacer_reset (transport_connection_t * tc,
621 u32 rate_bytes_per_sec,
622 u32 start_bucket, u64 time_now)
624 spacer_t *pacer = &tc->pacer;
625 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
626 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
627 pacer->bucket = start_bucket;
631 transport_connection_tx_pacer_init (transport_connection_t * tc,
632 u32 rate_bytes_per_sec,
635 vlib_main_t *vm = vlib_get_main ();
636 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
637 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
639 vm->clib_time.last_cpu_time);
643 transport_connection_tx_pacer_update (transport_connection_t * tc,
646 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
650 transport_connection_tx_pacer_burst (transport_connection_t * tc,
653 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
654 return spacer_max_burst (&tc->pacer, time_now);
658 transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
661 u32 snd_space, max_paced_burst;
663 snd_space = tp_vfts[tc->proto].send_space (tc);
664 if (transport_connection_is_tx_paced (tc))
666 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
667 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
668 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
669 snd_space = clib_min (snd_space, max_paced_burst);
670 snd_space = snd_space - snd_space % mss;
676 transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
678 tc->stats.tx_bytes += bytes;
679 if (transport_connection_is_tx_paced (tc))
680 spacer_update_bucket (&tc->pacer, bytes);
684 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
687 spacer_update_bucket (&tc->pacer, bytes);
691 transport_init_tx_pacers_period (void)
693 f64 cpu_freq = os_cpu_clock_frequency ();
694 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
698 transport_update_time (f64 time_now, u8 thread_index)
700 transport_proto_vft_t *vft;
701 vec_foreach (vft, tp_vfts)
703 if (vft->update_time)
704 (vft->update_time) (time_now, thread_index);
709 transport_enable_disable (vlib_main_t * vm, u8 is_en)
711 transport_proto_vft_t *vft;
712 vec_foreach (vft, tp_vfts)
715 (vft->enable) (vm, is_en);
720 transport_init (void)
722 vlib_thread_main_t *vtm = vlib_get_thread_main ();
723 session_main_t *smm = vnet_get_session_main ();
726 if (smm->local_endpoints_table_buckets == 0)
727 smm->local_endpoints_table_buckets = 250000;
728 if (smm->local_endpoints_table_memory == 0)
729 smm->local_endpoints_table_memory = 512 << 20;
731 /* Initialize [port-allocator] random number seed */
732 port_allocator_seed = (u32) clib_cpu_time_now ();
734 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
735 smm->local_endpoints_table_buckets,
736 smm->local_endpoints_table_memory);
737 num_threads = 1 /* main thread */ + vtm->n_threads;
739 clib_spinlock_init (&local_endpoints_lock);
743 * fd.io coding-style-patch-verification: ON
746 * eval: (c-set-style "gnu")