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 (43 * 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 #define _(sym, str, sstr) \
61 case TRANSPORT_PROTO_ ## sym: \
62 s = format (s, str); \
64 foreach_transport_proto
67 s = format (s, "UNKNOWN");
74 format_transport_proto_short (u8 * s, va_list * args)
76 u32 transport_proto = va_arg (*args, u32);
77 switch (transport_proto)
79 #define _(sym, str, sstr) \
80 case TRANSPORT_PROTO_ ## sym: \
81 s = format (s, sstr); \
83 foreach_transport_proto
93 format_transport_connection (u8 * s, va_list * args)
95 u32 transport_proto = va_arg (*args, u32);
96 u32 conn_index = va_arg (*args, u32);
97 u32 thread_index = va_arg (*args, u32);
98 u32 verbose = va_arg (*args, u32);
99 transport_proto_vft_t *tp_vft;
100 transport_connection_t *tc;
103 tp_vft = transport_protocol_get_vft (transport_proto);
107 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
109 tc = tp_vft->get_connection (conn_index, thread_index);
110 if (tc && transport_connection_is_tx_paced (tc) && verbose > 1)
112 indent = format_get_indent (s) + 1;
113 s = format (s, "%Upacer: %U\n", format_white_space, indent,
114 format_transport_pacer, &tc->pacer);
120 format_transport_listen_connection (u8 * s, va_list * args)
122 u32 transport_proto = va_arg (*args, u32);
123 transport_proto_vft_t *tp_vft;
125 tp_vft = transport_protocol_get_vft (transport_proto);
129 s = (tp_vft->format_listener) (s, args);
134 format_transport_half_open_connection (u8 * s, va_list * args)
136 u32 transport_proto = va_arg (*args, u32);
137 u32 listen_index = va_arg (*args, u32);
138 transport_proto_vft_t *tp_vft;
140 tp_vft = transport_protocol_get_vft (transport_proto);
144 s = format (s, "%U", tp_vft->format_half_open, listen_index);
149 unformat_transport_proto (unformat_input_t * input, va_list * args)
151 u32 *proto = va_arg (*args, u32 *);
153 #define _(sym, str, sstr) \
154 if (unformat (input, str)) \
156 *proto = TRANSPORT_PROTO_ ## sym; \
159 foreach_transport_proto
165 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
166 ip46_address_t * ip, u16 port)
168 clib_bihash_kv_24_8_t kv;
171 kv.key[0] = ip->as_u64[0];
172 kv.key[1] = ip->as_u64[1];
173 kv.key[2] = (u64) port << 8 | (u64) proto;
175 rv = clib_bihash_search_inline_24_8 (ht, &kv);
179 return ENDPOINT_INVALID_INDEX;
183 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
184 transport_endpoint_t * te, u32 value)
186 clib_bihash_kv_24_8_t kv;
188 kv.key[0] = te->ip.as_u64[0];
189 kv.key[1] = te->ip.as_u64[1];
190 kv.key[2] = (u64) te->port << 8 | (u64) proto;
193 clib_bihash_add_del_24_8 (ht, &kv, 1);
197 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
198 transport_endpoint_t * te)
200 clib_bihash_kv_24_8_t kv;
202 kv.key[0] = te->ip.as_u64[0];
203 kv.key[1] = te->ip.as_u64[1];
204 kv.key[2] = (u64) te->port << 8 | (u64) proto;
206 clib_bihash_add_del_24_8 (ht, &kv, 0);
210 * Register transport virtual function table.
212 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
213 * @param vft - virtual function table for transport proto
214 * @param fib_proto - network layer protocol
215 * @param output_node - output node index that session layer will hand off
216 * buffers to, for requested fib proto
219 transport_register_protocol (transport_proto_t transport_proto,
220 const transport_proto_vft_t * vft,
221 fib_protocol_t fib_proto, u32 output_node)
223 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
225 vec_validate (tp_vfts, transport_proto);
226 tp_vfts[transport_proto] = *vft;
228 session_register_transport (transport_proto, vft, is_ip4, output_node);
232 * Get transport virtual function table
234 * @param type - session type (not protocol type)
236 transport_proto_vft_t *
237 transport_protocol_get_vft (transport_proto_t transport_proto)
239 if (transport_proto >= vec_len (tp_vfts))
241 return &tp_vfts[transport_proto];
245 transport_half_open_has_fifos (transport_proto_t tp)
247 return tp_vfts[tp].transport_options.half_open_has_fifos;
250 transport_service_type_t
251 transport_protocol_service_type (transport_proto_t tp)
253 return tp_vfts[tp].transport_options.service_type;
256 transport_tx_fn_type_t
257 transport_protocol_tx_fn_type (transport_proto_t tp)
259 return tp_vfts[tp].transport_options.tx_type;
263 transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
265 tp_vfts[tp].cleanup (conn_index, thread_index);
269 transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
271 return tp_vfts[tp].connect (tep);
275 transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
277 tp_vfts[tp].close (conn_index, thread_index);
281 transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
283 if (tp_vfts[tp].reset)
284 tp_vfts[tp].reset (conn_index, thread_index);
286 tp_vfts[tp].close (conn_index, thread_index);
290 transport_start_listen (transport_proto_t tp, u32 session_index,
291 transport_endpoint_t * tep)
293 return tp_vfts[tp].start_listen (session_index, tep);
297 transport_stop_listen (transport_proto_t tp, u32 conn_index)
299 return tp_vfts[tp].stop_listen (conn_index);
303 transport_protocol_is_cl (transport_proto_t tp)
305 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
309 default_get_transport_endpoint (transport_connection_t * tc,
310 transport_endpoint_t * tep, u8 is_lcl)
314 tep->port = tc->lcl_port;
315 tep->is_ip4 = tc->is_ip4;
316 clib_memcpy_fast (&tep->ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
320 tep->port = tc->rmt_port;
321 tep->is_ip4 = tc->is_ip4;
322 clib_memcpy_fast (&tep->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
327 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
328 u32 thread_index, transport_endpoint_t * tep,
331 if (tp_vfts[tp].get_transport_endpoint)
332 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
336 transport_connection_t *tc;
337 tc = transport_get_connection (tp, conn_index, thread_index);
338 default_get_transport_endpoint (tc, tep, is_lcl);
343 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
344 transport_endpoint_t * tep, u8 is_lcl)
346 if (tp_vfts[tp].get_transport_listener_endpoint)
347 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
350 transport_connection_t *tc;
351 tc = transport_get_listener (tp, conn_index);
352 default_get_transport_endpoint (tc, tep, is_lcl);
356 #define PORT_MASK ((1 << 16)- 1)
359 transport_endpoint_del (u32 tepi)
361 clib_spinlock_lock_if_init (&local_endpoints_lock);
362 pool_put_index (local_endpoints, tepi);
363 clib_spinlock_unlock_if_init (&local_endpoints_lock);
366 always_inline transport_endpoint_t *
367 transport_endpoint_new (void)
369 transport_endpoint_t *tep;
370 pool_get_zero (local_endpoints, tep);
375 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
378 transport_endpoint_t *tep;
380 /* Cleanup local endpoint if this was an active connect */
381 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
382 clib_net_to_host_u16 (port));
383 if (tepi != ENDPOINT_INVALID_INDEX)
385 tep = pool_elt_at_index (local_endpoints, tepi);
386 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
387 transport_endpoint_del (tepi);
392 transport_endpoint_mark_used (u8 proto, ip46_address_t * ip, u16 port)
394 transport_endpoint_t *tep;
395 clib_spinlock_lock_if_init (&local_endpoints_lock);
396 tep = transport_endpoint_new ();
397 clib_memcpy_fast (&tep->ip, ip, sizeof (*ip));
399 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
400 tep - local_endpoints);
401 clib_spinlock_unlock_if_init (&local_endpoints_lock);
405 * Allocate local port and add if successful add entry to local endpoint
406 * table to mark the pair as used.
409 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
411 u16 min = 1024, max = 65535; /* XXX configurable ? */
417 /* Only support active opens from thread 0 */
418 ASSERT (vlib_get_thread_index () == 0);
420 /* Search for first free slot */
421 for (tries = 0; tries < limit; tries++)
425 /* Find a port in the specified range */
428 port = random_u32 (&port_allocator_seed) & PORT_MASK;
429 if (PREDICT_TRUE (port >= min && port < max))
433 /* Look it up. If not found, we're done */
434 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
436 if (tei == ENDPOINT_INVALID_INDEX)
438 transport_endpoint_mark_used (proto, ip, port);
445 static clib_error_t *
446 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
451 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
453 return clib_error_return (0, "no routable ip4 address on %U",
454 format_vnet_sw_if_index_name,
455 vnet_get_main (), sw_if_index);
456 addr->ip4.as_u32 = ip4->as_u32;
461 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
463 return clib_error_return (0, "no routable ip6 addresses on %U",
464 format_vnet_sw_if_index_name,
465 vnet_get_main (), sw_if_index);
466 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
471 static clib_error_t *
472 transport_find_local_ip_for_remote (u32 sw_if_index,
473 transport_endpoint_t * rmt,
474 ip46_address_t * lcl_addr)
476 fib_node_index_t fei;
479 if (sw_if_index == ENDPOINT_INVALID_INDEX)
481 /* Find a FIB path to the destination */
482 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
483 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
484 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
486 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
487 fei = fib_table_lookup (rmt->fib_index, &prefix);
489 /* Couldn't find route to destination. Bail out. */
490 if (fei == FIB_NODE_INDEX_INVALID)
491 return clib_error_return (0, "no route to %U", format_ip46_address,
492 &rmt->ip, (rmt->is_ip4 == 0) + 1);
494 sw_if_index = fib_entry_get_resolving_interface (fei);
495 if (sw_if_index == ENDPOINT_INVALID_INDEX)
496 return clib_error_return (0, "no resolving interface for %U",
497 format_ip46_address, &rmt->ip,
498 (rmt->is_ip4 == 0) + 1);
501 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
502 return transport_get_interface_ip (sw_if_index, rmt->is_ip4, lcl_addr);
506 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
507 ip46_address_t * lcl_addr, u16 * lcl_port)
509 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
515 * Find the local address
517 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
519 error = transport_find_local_ip_for_remote (rmt_cfg->peer.sw_if_index,
523 clib_error_report (error);
529 /* Assume session layer vetted this address */
530 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
531 sizeof (rmt_cfg->peer.ip));
535 * Allocate source port
537 if (rmt_cfg->peer.port == 0)
539 port = transport_alloc_local_port (proto, lcl_addr);
542 clib_warning ("Failed to allocate src port");
549 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
550 tei = transport_endpoint_lookup (&local_endpoints_table, proto,
552 if (tei != ENDPOINT_INVALID_INDEX)
555 transport_endpoint_mark_used (proto, lcl_addr, port);
562 #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
563 #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
566 format_transport_pacer (u8 * s, va_list * args)
568 spacer_t *pacer = va_arg (*args, spacer_t *);
570 s = format (s, "bucket %u tokens/period %.3f last_update %x",
571 pacer->bucket, pacer->tokens_per_period, pacer->last_update);
576 spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
578 u64 n_periods = norm_time_now - pacer->last_update;
582 && (inc = (f32) n_periods * pacer->tokens_per_period) > 10)
584 pacer->last_update = norm_time_now;
585 pacer->bucket = clib_min (pacer->bucket + inc, pacer->bytes_per_sec);
588 return clib_min (pacer->bucket, TRANSPORT_PACER_MAX_BURST);
592 spacer_update_bucket (spacer_t * pacer, u32 bytes)
594 ASSERT (pacer->bucket >= bytes);
595 pacer->bucket -= bytes;
599 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
601 ASSERT (rate_bytes_per_sec != 0);
602 pacer->bytes_per_sec = rate_bytes_per_sec;
603 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
607 spacer_pace_rate (spacer_t * pacer)
609 return pacer->bytes_per_sec;
613 spacer_reset_bucket (spacer_t * pacer, u64 norm_time_now)
615 pacer->last_update = norm_time_now;
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_tx_pacer_reset_bucket (transport_connection_t * tc,
661 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
662 spacer_reset_bucket (&tc->pacer, time_now);
666 transport_connection_snd_space (transport_connection_t * tc, u64 time_now,
669 u32 snd_space, max_paced_burst;
671 snd_space = tp_vfts[tc->proto].send_space (tc);
672 if (transport_connection_is_tx_paced (tc))
674 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
675 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
677 (max_paced_burst < TRANSPORT_PACER_MIN_BURST) ? 0 : max_paced_burst;
678 snd_space = clib_min (snd_space, max_paced_burst);
679 return snd_space >= mss ? snd_space - snd_space % mss : snd_space;
685 transport_connection_tx_pacer_rate (transport_connection_t * tc)
687 return spacer_pace_rate (&tc->pacer);
691 transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
693 if (transport_connection_is_tx_paced (tc))
694 spacer_update_bucket (&tc->pacer, bytes);
698 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
701 spacer_update_bucket (&tc->pacer, bytes);
705 transport_init_tx_pacers_period (void)
707 f64 cpu_freq = os_cpu_clock_frequency ();
708 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
712 transport_update_time (f64 time_now, u8 thread_index)
714 transport_proto_vft_t *vft;
715 vec_foreach (vft, tp_vfts)
717 if (vft->update_time)
718 (vft->update_time) (time_now, thread_index);
723 transport_enable_disable (vlib_main_t * vm, u8 is_en)
725 transport_proto_vft_t *vft;
726 vec_foreach (vft, tp_vfts)
729 (vft->enable) (vm, is_en);
734 transport_init (void)
736 vlib_thread_main_t *vtm = vlib_get_thread_main ();
737 session_main_t *smm = vnet_get_session_main ();
740 if (smm->local_endpoints_table_buckets == 0)
741 smm->local_endpoints_table_buckets = 250000;
742 if (smm->local_endpoints_table_memory == 0)
743 smm->local_endpoints_table_memory = 512 << 20;
745 /* Initialize [port-allocator] random number seed */
746 port_allocator_seed = (u32) clib_cpu_time_now ();
748 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
749 smm->local_endpoints_table_buckets,
750 smm->local_endpoints_table_memory);
751 num_threads = 1 /* main thread */ + vtm->n_threads;
753 clib_spinlock_init (&local_endpoints_lock);
757 * fd.io coding-style-patch-verification: ON
760 * eval: (c-set-style "gnu")