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 format_transport_proto (u8 * s, va_list * args)
48 u32 transport_proto = va_arg (*args, u32);
49 switch (transport_proto)
51 #define _(sym, str, sstr) \
52 case TRANSPORT_PROTO_ ## sym: \
53 s = format (s, str); \
55 foreach_transport_proto
58 s = format (s, "UNKNOWN");
65 format_transport_proto_short (u8 * s, va_list * args)
67 u32 transport_proto = va_arg (*args, u32);
68 switch (transport_proto)
70 #define _(sym, str, sstr) \
71 case TRANSPORT_PROTO_ ## sym: \
72 s = format (s, sstr); \
74 foreach_transport_proto
84 format_transport_connection (u8 * s, va_list * args)
86 u32 transport_proto = va_arg (*args, u32);
87 u32 conn_index = va_arg (*args, u32);
88 u32 thread_index = va_arg (*args, u32);
89 u32 verbose = va_arg (*args, u32);
90 transport_proto_vft_t *tp_vft;
91 transport_connection_t *tc;
94 tp_vft = transport_protocol_get_vft (transport_proto);
98 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
100 tc = tp_vft->get_connection (conn_index, thread_index);
101 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
103 indent = format_get_indent (s) + 1;
104 s = format (s, "%Upacer: %U\n", format_white_space, indent,
105 format_transport_pacer, &tc->pacer, tc->thread_index);
111 format_transport_listen_connection (u8 * s, va_list * args)
113 u32 transport_proto = va_arg (*args, u32);
114 transport_proto_vft_t *tp_vft;
116 tp_vft = transport_protocol_get_vft (transport_proto);
120 s = (tp_vft->format_listener) (s, args);
125 format_transport_half_open_connection (u8 * s, va_list * args)
127 u32 transport_proto = va_arg (*args, u32);
128 u32 listen_index = va_arg (*args, u32);
129 transport_proto_vft_t *tp_vft;
131 tp_vft = transport_protocol_get_vft (transport_proto);
135 s = format (s, "%U", tp_vft->format_half_open, listen_index);
140 unformat_transport_str_match (unformat_input_t * input, const char *str)
144 if (strlen (str) > vec_len (input->buffer) - input->index)
147 for (i = 0; i < strlen (str); i++)
149 if (input->buffer[i + input->index] != str[i])
156 unformat_transport_proto (unformat_input_t * input, va_list * args)
158 u32 *proto = va_arg (*args, u32 *);
159 u8 longest_match = 0, match;
162 #define _(sym, str, sstr) \
163 if (unformat_transport_str_match (input, str)) \
165 match = strlen (str); \
166 if (match > longest_match) \
168 *proto = TRANSPORT_PROTO_ ## sym; \
169 longest_match = match; \
173 foreach_transport_proto
177 unformat (input, str_match);
185 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
186 ip46_address_t * ip, u16 port)
188 clib_bihash_kv_24_8_t kv;
191 kv.key[0] = ip->as_u64[0];
192 kv.key[1] = ip->as_u64[1];
193 kv.key[2] = (u64) port << 8 | (u64) proto;
195 rv = clib_bihash_search_inline_24_8 (ht, &kv);
199 return ENDPOINT_INVALID_INDEX;
203 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
204 transport_endpoint_t * te, u32 value)
206 clib_bihash_kv_24_8_t kv;
208 kv.key[0] = te->ip.as_u64[0];
209 kv.key[1] = te->ip.as_u64[1];
210 kv.key[2] = (u64) te->port << 8 | (u64) proto;
213 clib_bihash_add_del_24_8 (ht, &kv, 1);
217 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
218 transport_endpoint_t * te)
220 clib_bihash_kv_24_8_t kv;
222 kv.key[0] = te->ip.as_u64[0];
223 kv.key[1] = te->ip.as_u64[1];
224 kv.key[2] = (u64) te->port << 8 | (u64) proto;
226 clib_bihash_add_del_24_8 (ht, &kv, 0);
230 * Register transport virtual function table.
232 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
233 * @param vft - virtual function table for transport proto
234 * @param fib_proto - network layer protocol
235 * @param output_node - output node index that session layer will hand off
236 * buffers to, for requested fib proto
239 transport_register_protocol (transport_proto_t transport_proto,
240 const transport_proto_vft_t * vft,
241 fib_protocol_t fib_proto, u32 output_node)
243 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
245 vec_validate (tp_vfts, transport_proto);
246 tp_vfts[transport_proto] = *vft;
248 session_register_transport (transport_proto, vft, is_ip4, output_node);
252 * Get transport virtual function table
254 * @param type - session type (not protocol type)
256 transport_proto_vft_t *
257 transport_protocol_get_vft (transport_proto_t transport_proto)
259 if (transport_proto >= vec_len (tp_vfts))
261 return &tp_vfts[transport_proto];
265 transport_half_open_has_fifos (transport_proto_t tp)
267 return tp_vfts[tp].transport_options.half_open_has_fifos;
270 transport_service_type_t
271 transport_protocol_service_type (transport_proto_t tp)
273 return tp_vfts[tp].transport_options.service_type;
276 transport_tx_fn_type_t
277 transport_protocol_tx_fn_type (transport_proto_t tp)
279 return tp_vfts[tp].transport_options.tx_type;
283 transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
285 tp_vfts[tp].cleanup (conn_index, thread_index);
289 transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
291 return tp_vfts[tp].connect (tep);
295 transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
297 tp_vfts[tp].close (conn_index, thread_index);
301 transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
303 if (tp_vfts[tp].reset)
304 tp_vfts[tp].reset (conn_index, thread_index);
306 tp_vfts[tp].close (conn_index, thread_index);
310 transport_start_listen (transport_proto_t tp, u32 session_index,
311 transport_endpoint_t * tep)
313 return tp_vfts[tp].start_listen (session_index, tep);
317 transport_stop_listen (transport_proto_t tp, u32 conn_index)
319 return tp_vfts[tp].stop_listen (conn_index);
323 transport_protocol_is_cl (transport_proto_t tp)
325 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
329 default_get_transport_endpoint (transport_connection_t * tc,
330 transport_endpoint_t * tep, u8 is_lcl)
334 tep->port = tc->lcl_port;
335 tep->is_ip4 = tc->is_ip4;
336 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
340 tep->port = tc->rmt_port;
341 tep->is_ip4 = tc->is_ip4;
342 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
347 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
348 u32 thread_index, transport_endpoint_t * tep,
351 if (tp_vfts[tp].get_transport_endpoint)
352 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
356 transport_connection_t *tc;
357 tc = transport_get_connection (tp, conn_index, thread_index);
358 default_get_transport_endpoint (tc, tep, is_lcl);
363 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
364 transport_endpoint_t * tep, u8 is_lcl)
366 if (tp_vfts[tp].get_transport_listener_endpoint)
367 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
370 transport_connection_t *tc;
371 tc = transport_get_listener (tp, conn_index);
372 default_get_transport_endpoint (tc, tep, is_lcl);
376 #define PORT_MASK ((1 << 16)- 1)
379 transport_endpoint_del (u32 tepi)
381 clib_spinlock_lock_if_init (&local_endpoints_lock);
382 pool_put_index (local_endpoints, tepi);
383 clib_spinlock_unlock_if_init (&local_endpoints_lock);
386 always_inline transport_endpoint_t *
387 transport_endpoint_new (void)
389 transport_endpoint_t *tep;
390 pool_get_zero (local_endpoints, tep);
395 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
398 transport_endpoint_t *tep;
400 /* Cleanup local endpoint if this was an active connect */
401 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
402 clib_net_to_host_u16 (port));
403 if (tepi != ENDPOINT_INVALID_INDEX)
405 tep = pool_elt_at_index (local_endpoints, tepi);
406 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
407 transport_endpoint_del (tepi);
412 transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
414 transport_endpoint_t *tep;
415 clib_spinlock_lock_if_init (&local_endpoints_lock);
416 tep = transport_endpoint_new ();
417 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
419 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
420 tep - local_endpoints);
421 clib_spinlock_unlock_if_init (&local_endpoints_lock);
425 * Allocate local port and add if successful add entry to local endpoint
426 * table to mark the pair as used.
429 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
431 u16 min = 1024, max = 65535; /* XXX configurable ? */
437 /* Only support active opens from thread 0 */
438 ASSERT (vlib_get_thread_index () == 0);
440 /* Search for first free slot */
441 for (tries = 0; tries < limit; tries++)
445 /* Find a port in the specified range */
448 port = random_u32 (&port_allocator_seed) & PORT_MASK;
449 if (PREDICT_TRUE (port >= min && port < max))
453 /* Look it up. If not found, we're done */
454 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
456 if (tei == ENDPOINT_INVALID_INDEX)
458 transport_endpoint_mark_used (proto, ip, port);
465 static clib_error_t *
466 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
471 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
473 return clib_error_return (0, "no routable ip4 address on %U",
474 format_vnet_sw_if_index_name,
475 vnet_get_main (), sw_if_index);
476 addr->ip4.as_u32 = ip4->as_u32;
481 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
483 return clib_error_return (0, "no routable ip6 addresses on %U",
484 format_vnet_sw_if_index_name,
485 vnet_get_main (), sw_if_index);
486 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
491 static clib_error_t *
492 transport_find_local_ip_for_remote (u32 sw_if_index,
493 transport_endpoint_t * rmt,
494 ip46_address_t * lcl_addr)
496 fib_node_index_t fei;
499 if (sw_if_index == ENDPOINT_INVALID_INDEX)
501 /* Find a FIB path to the destination */
502 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
503 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
504 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
506 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
507 fei = fib_table_lookup (rmt->fib_index, &prefix);
509 /* Couldn't find route to destination. Bail out. */
510 if (fei == FIB_NODE_INDEX_INVALID)
511 return clib_error_return (0, "no route to %U", format_ip46_address,
512 &rmt->ip, (rmt->is_ip4 == 0) + 1);
514 sw_if_index = fib_entry_get_resolving_interface (fei);
515 if (sw_if_index == ENDPOINT_INVALID_INDEX)
516 return clib_error_return (0, "no resolving interface for %U",
517 format_ip46_address, &rmt->ip,
518 (rmt->is_ip4 == 0) + 1);
521 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
522 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
526 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
527 ip46_address_t * lcl_addr, u16 * lcl_port)
529 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
535 * Find the local address
537 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
539 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
543 clib_error_report (error);
549 /* Assume session layer vetted this address */
550 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
551 sizeof (rmt_cfg->peer.ip));
555 * Allocate source port
557 if (rmt_cfg->peer.port == 0)
559 port = transport_alloc_local_port (proto, lcl_addr);
562 clib_warning ("Failed to allocate src port");
569 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
570 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
572 if (tei != ENDPOINT_INVALID_INDEX)
575 transport_endpoint_mark_used (proto, lcl_addr, port);
583 format_clib_us_time (u8 * s, va_list * args)
585 clib_us_time_t t = va_arg (*args, clib_us_time_t);
587 s = format (s, "%u us", t);
589 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
594 format_transport_pacer (u8 * s, va_list * args)
596 spacer_t *pacer = va_arg (*args, spacer_t *);
597 u32 thread_index = va_arg (*args, int);
598 clib_us_time_t now, diff;
600 now = transport_us_time_now (thread_index);
601 diff = now - pacer->last_update;
602 s = format (s, "rate %lu bucket %lu t/p %.3f last_update %U idle %u",
603 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
604 format_clib_us_time, diff, pacer->idle_timeout_us);
609 spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
611 u64 n_periods = (time_now - pacer->last_update);
614 if (PREDICT_FALSE (n_periods > pacer->idle_timeout_us))
616 pacer->last_update = time_now;
617 pacer->bucket = TRANSPORT_PACER_MIN_BURST;
618 return TRANSPORT_PACER_MIN_BURST;
621 if ((inc = (f32) n_periods * pacer->tokens_per_period) > 10)
623 pacer->last_update = time_now;
624 pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
627 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
631 spacer_update_bucket (spacer_t * pacer, u32 bytes)
633 ASSERT (pacer->bucket >= bytes);
634 pacer->bucket -= bytes;
638 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec,
641 ASSERT (rate_bytes_per_sec != 0);
642 pacer->bytes_per_sec = rate_bytes_per_sec;
643 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
644 pacer->idle_timeout_us = clib_max (rtt * TRANSPORT_PACER_IDLE_FACTOR,
645 TRANSPORT_PACER_MIN_IDLE);
649 spacer_pace_rate (spacer_t * pacer)
651 return pacer->bytes_per_sec;
655 spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
657 pacer->last_update = time_now;
658 pacer->bucket = bucket;
662 transport_connection_tx_pacer_reset (transport_connection_t * tc,
663 u64 rate_bytes_per_sec, u32 start_bucket,
666 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec, rtt);
667 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
672 transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc,
675 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), bucket);
679 transport_connection_tx_pacer_init (transport_connection_t * tc,
680 u64 rate_bytes_per_sec,
683 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
684 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
685 initial_bucket, 1e6);
689 transport_connection_tx_pacer_update (transport_connection_t * tc,
690 u64 bytes_per_sec, clib_us_time_t rtt)
692 spacer_set_pace_rate (&tc->pacer, bytes_per_sec, rtt);
696 transport_connection_tx_pacer_burst (transport_connection_t * tc)
698 return spacer_max_burst (&tc->pacer,
699 transport_us_time_now (tc->thread_index));
703 transport_connection_tx_pacer_rate (transport_connection_t * tc)
705 return spacer_pace_rate (&tc->pacer);
709 transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
711 if (transport_connection_is_tx_paced (tc))
712 spacer_update_bucket (&tc->pacer, bytes);
716 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
719 spacer_update_bucket (&tc->pacer, bytes);
723 transport_update_time (clib_time_type_t time_now, u8 thread_index)
725 transport_proto_vft_t *vft;
726 vec_foreach (vft, tp_vfts)
728 if (vft->update_time)
729 (vft->update_time) (time_now, thread_index);
734 transport_enable_disable (vlib_main_t * vm, u8 is_en)
736 transport_proto_vft_t *vft;
737 vec_foreach (vft, tp_vfts)
740 (vft->enable) (vm, is_en);
745 transport_init (void)
747 vlib_thread_main_t *vtm = vlib_get_thread_main ();
748 session_main_t *smm = vnet_get_session_main ();
751 if (smm->local_endpoints_table_buckets == 0)
752 smm->local_endpoints_table_buckets = 250000;
753 if (smm->local_endpoints_table_memory == 0)
754 smm->local_endpoints_table_memory = 512 << 20;
756 /* Initialize [port-allocator] random number seed */
757 port_allocator_seed = (u32) clib_cpu_time_now ();
759 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
760 smm->local_endpoints_table_buckets,
761 smm->local_endpoints_table_memory);
762 num_threads = 1 /* main thread */ + vtm->n_threads;
764 clib_spinlock_init (&local_endpoints_lock);
768 * fd.io coding-style-patch-verification: ON
771 * eval: (c-set-style "gnu")