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;
25 typedef struct local_endpoint_
27 transport_endpoint_t ep;
31 typedef struct transport_main_
33 transport_endpoint_table_t local_endpoints_table;
34 local_endpoint_t *local_endpoints;
35 u32 port_allocator_seed;
36 clib_spinlock_t local_endpoints_lock;
39 static transport_main_t tp_main;
42 format_transport_proto (u8 * s, va_list * args)
44 u32 transport_proto = va_arg (*args, u32);
46 if (tp_vfts[transport_proto].transport_options.name)
47 s = format (s, "%s", tp_vfts[transport_proto].transport_options.name);
49 s = format (s, "n/a");
55 format_transport_proto_short (u8 * s, va_list * args)
57 u32 transport_proto = va_arg (*args, u32);
60 short_name = tp_vfts[transport_proto].transport_options.short_name;
62 s = format (s, "%s", short_name);
70 format_transport_connection (u8 * s, va_list * args)
72 u32 transport_proto = va_arg (*args, u32);
73 u32 conn_index = va_arg (*args, u32);
74 u32 thread_index = va_arg (*args, u32);
75 u32 verbose = va_arg (*args, u32);
76 transport_proto_vft_t *tp_vft;
77 transport_connection_t *tc;
80 tp_vft = transport_protocol_get_vft (transport_proto);
84 s = format (s, "%U", tp_vft->format_connection, conn_index, thread_index,
86 tc = tp_vft->get_connection (conn_index, thread_index);
87 if (tc && verbose > 1)
89 indent = format_get_indent (s) + 1;
90 if (transport_connection_is_tx_paced (tc))
91 s = format (s, "%Upacer: %U\n", format_white_space, indent,
92 format_transport_pacer, &tc->pacer, tc->thread_index);
93 s = format (s, "%Utransport: flags 0x%x\n", format_white_space, indent,
100 format_transport_listen_connection (u8 * s, va_list * args)
102 u32 transport_proto = va_arg (*args, u32);
103 transport_proto_vft_t *tp_vft;
105 tp_vft = transport_protocol_get_vft (transport_proto);
109 s = (tp_vft->format_listener) (s, args);
114 format_transport_half_open_connection (u8 * s, va_list * args)
116 u32 transport_proto = va_arg (*args, u32);
117 u32 ho_index = va_arg (*args, u32);
118 transport_proto_vft_t *tp_vft;
120 tp_vft = transport_protocol_get_vft (transport_proto);
124 s = format (s, "%U", tp_vft->format_half_open, ho_index);
129 unformat_transport_str_match (unformat_input_t * input, const char *str)
133 if (strlen (str) > vec_len (input->buffer) - input->index)
136 for (i = 0; i < strlen (str); i++)
138 if (input->buffer[i + input->index] != str[i])
145 unformat_transport_proto (unformat_input_t * input, va_list * args)
147 u32 *proto = va_arg (*args, u32 *);
148 transport_proto_vft_t *tp_vft;
149 u8 longest_match = 0, match;
150 char *str, *str_match = 0;
151 transport_proto_t tp;
153 for (tp = 0; tp < vec_len (tp_vfts); tp++)
155 tp_vft = &tp_vfts[tp];
156 str = tp_vft->transport_options.name;
159 if (unformat_transport_str_match (input, str))
161 match = strlen (str);
162 if (match > longest_match)
165 longest_match = match;
172 (void) unformat (input, str_match);
180 format_transport_protos (u8 * s, va_list * args)
182 transport_proto_vft_t *tp_vft;
184 vec_foreach (tp_vft, tp_vfts)
185 s = format (s, "%s\n", tp_vft->transport_options.name);
191 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
192 ip46_address_t * ip, u16 port)
194 clib_bihash_kv_24_8_t kv;
197 kv.key[0] = ip->as_u64[0];
198 kv.key[1] = ip->as_u64[1];
199 kv.key[2] = (u64) port << 8 | (u64) proto;
201 rv = clib_bihash_search_inline_24_8 (ht, &kv);
205 return ENDPOINT_INVALID_INDEX;
209 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
210 transport_endpoint_t * te, u32 value)
212 clib_bihash_kv_24_8_t kv;
214 kv.key[0] = te->ip.as_u64[0];
215 kv.key[1] = te->ip.as_u64[1];
216 kv.key[2] = (u64) te->port << 8 | (u64) proto;
219 clib_bihash_add_del_24_8 (ht, &kv, 1);
223 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
224 transport_endpoint_t * te)
226 clib_bihash_kv_24_8_t kv;
228 kv.key[0] = te->ip.as_u64[0];
229 kv.key[1] = te->ip.as_u64[1];
230 kv.key[2] = (u64) te->port << 8 | (u64) proto;
232 clib_bihash_add_del_24_8 (ht, &kv, 0);
236 transport_register_protocol (transport_proto_t transport_proto,
237 const transport_proto_vft_t * vft,
238 fib_protocol_t fib_proto, u32 output_node)
240 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
242 vec_validate (tp_vfts, transport_proto);
243 tp_vfts[transport_proto] = *vft;
245 session_register_transport (transport_proto, vft, is_ip4, output_node);
249 transport_register_new_protocol (const transport_proto_vft_t * vft,
250 fib_protocol_t fib_proto, u32 output_node)
252 transport_proto_t transport_proto;
255 transport_proto = session_add_transport_proto ();
256 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
258 vec_validate (tp_vfts, transport_proto);
259 tp_vfts[transport_proto] = *vft;
261 session_register_transport (transport_proto, vft, is_ip4, output_node);
263 return transport_proto;
267 * Get transport virtual function table
269 * @param type - session type (not protocol type)
271 transport_proto_vft_t *
272 transport_protocol_get_vft (transport_proto_t transport_proto)
274 if (transport_proto >= vec_len (tp_vfts))
276 return &tp_vfts[transport_proto];
279 transport_service_type_t
280 transport_protocol_service_type (transport_proto_t tp)
282 return tp_vfts[tp].transport_options.service_type;
285 transport_tx_fn_type_t
286 transport_protocol_tx_fn_type (transport_proto_t tp)
288 return tp_vfts[tp].transport_options.tx_type;
292 transport_cleanup (transport_proto_t tp, u32 conn_index, u8 thread_index)
294 tp_vfts[tp].cleanup (conn_index, thread_index);
298 transport_cleanup_half_open (transport_proto_t tp, u32 conn_index)
300 if (tp_vfts[tp].cleanup_ho)
301 tp_vfts[tp].cleanup_ho (conn_index);
305 transport_connect (transport_proto_t tp, transport_endpoint_cfg_t * tep)
307 if (PREDICT_FALSE (!tp_vfts[tp].connect))
308 return SESSION_E_TRANSPORT_NO_REG;
309 return tp_vfts[tp].connect (tep);
313 transport_half_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
315 if (tp_vfts[tp].half_close)
316 tp_vfts[tp].half_close (conn_index, thread_index);
320 transport_close (transport_proto_t tp, u32 conn_index, u8 thread_index)
322 tp_vfts[tp].close (conn_index, thread_index);
326 transport_reset (transport_proto_t tp, u32 conn_index, u8 thread_index)
328 if (tp_vfts[tp].reset)
329 tp_vfts[tp].reset (conn_index, thread_index);
331 tp_vfts[tp].close (conn_index, thread_index);
335 transport_start_listen (transport_proto_t tp, u32 session_index,
336 transport_endpoint_cfg_t *tep)
338 if (PREDICT_FALSE (!tp_vfts[tp].start_listen))
339 return SESSION_E_TRANSPORT_NO_REG;
340 return tp_vfts[tp].start_listen (session_index, tep);
344 transport_stop_listen (transport_proto_t tp, u32 conn_index)
346 return tp_vfts[tp].stop_listen (conn_index);
350 transport_protocol_is_cl (transport_proto_t tp)
352 return (tp_vfts[tp].transport_options.service_type == TRANSPORT_SERVICE_CL);
356 default_get_transport_endpoint (transport_connection_t * tc,
357 transport_endpoint_t * tep, u8 is_lcl)
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));
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));
374 transport_get_endpoint (transport_proto_t tp, u32 conn_index,
375 u32 thread_index, transport_endpoint_t * tep,
378 if (tp_vfts[tp].get_transport_endpoint)
379 tp_vfts[tp].get_transport_endpoint (conn_index, thread_index, tep,
383 transport_connection_t *tc;
384 tc = transport_get_connection (tp, conn_index, thread_index);
385 default_get_transport_endpoint (tc, tep, is_lcl);
390 transport_get_listener_endpoint (transport_proto_t tp, u32 conn_index,
391 transport_endpoint_t * tep, u8 is_lcl)
393 if (tp_vfts[tp].get_transport_listener_endpoint)
394 tp_vfts[tp].get_transport_listener_endpoint (conn_index, tep, is_lcl);
397 transport_connection_t *tc;
398 tc = transport_get_listener (tp, conn_index);
399 default_get_transport_endpoint (tc, tep, is_lcl);
404 transport_connection_attribute (transport_proto_t tp, u32 conn_index,
405 u8 thread_index, u8 is_get,
406 transport_endpt_attr_t *attr)
408 if (!tp_vfts[tp].attribute)
411 return tp_vfts[tp].attribute (conn_index, thread_index, is_get, attr);
414 #define PORT_MASK ((1 << 16)- 1)
417 transport_endpoint_free (u32 tepi)
419 transport_main_t *tm = &tp_main;
420 pool_put_index (tm->local_endpoints, tepi);
423 always_inline local_endpoint_t *
424 transport_endpoint_alloc (void)
426 transport_main_t *tm = &tp_main;
427 local_endpoint_t *lep;
429 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
430 pool_get_aligned_safe (tm->local_endpoints, lep, 0);
435 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
437 transport_main_t *tm = &tp_main;
438 local_endpoint_t *lep;
441 /* Cleanup local endpoint if this was an active connect */
442 lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
443 clib_net_to_host_u16 (port));
444 if (lepi == ENDPOINT_INVALID_INDEX)
447 lep = pool_elt_at_index (tm->local_endpoints, lepi);
448 if (!clib_atomic_sub_fetch (&lep->refcnt, 1))
450 transport_endpoint_table_del (&tm->local_endpoints_table, proto,
453 /* All workers can free connections. Synchronize access to pool */
454 clib_spinlock_lock (&tm->local_endpoints_lock);
455 transport_endpoint_free (lepi);
456 clib_spinlock_unlock (&tm->local_endpoints_lock);
461 transport_endpoint_mark_used (u8 proto, ip46_address_t *ip, u16 port)
463 transport_main_t *tm = &tp_main;
464 local_endpoint_t *lep;
467 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
470 transport_endpoint_lookup (&tm->local_endpoints_table, proto, ip, port);
471 if (tei != ENDPOINT_INVALID_INDEX)
472 return SESSION_E_PORTINUSE;
474 /* Pool reallocs with worker barrier */
475 lep = transport_endpoint_alloc ();
476 clib_memcpy_fast (&lep->ep.ip, ip, sizeof (*ip));
480 transport_endpoint_table_add (&tm->local_endpoints_table, proto, &lep->ep,
481 lep - tm->local_endpoints);
487 transport_share_local_endpoint (u8 proto, ip46_address_t * lcl_ip, u16 port)
489 transport_main_t *tm = &tp_main;
490 local_endpoint_t *lep;
493 lepi = transport_endpoint_lookup (&tm->local_endpoints_table, proto, lcl_ip,
494 clib_net_to_host_u16 (port));
495 if (lepi != ENDPOINT_INVALID_INDEX)
497 lep = pool_elt_at_index (tm->local_endpoints, lepi);
498 clib_atomic_add_fetch (&lep->refcnt, 1);
503 * Allocate local port and add if successful add entry to local endpoint
504 * table to mark the pair as used.
507 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
509 u16 min = 1024, max = 65535; /* XXX configurable ? */
514 /* Only support active opens from one of ctrl threads */
515 ASSERT (vlib_get_thread_index () <= transport_cl_thread ());
517 /* Search for first free slot */
518 for (tries = 0; tries < limit; tries++)
522 /* Find a port in the specified range */
525 port = random_u32 (&tp_main.port_allocator_seed) & PORT_MASK;
526 if (PREDICT_TRUE (port >= min && port < max))
530 if (!transport_endpoint_mark_used (proto, ip, port))
536 static session_error_t
537 transport_get_interface_ip (u32 sw_if_index, u8 is_ip4, ip46_address_t * addr)
542 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
544 return SESSION_E_NOIP;
545 addr->ip4.as_u32 = ip4->as_u32;
550 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
552 return SESSION_E_NOIP;
553 clib_memcpy_fast (&addr->ip6, ip6, sizeof (*ip6));
558 static session_error_t
559 transport_find_local_ip_for_remote (u32 *sw_if_index,
560 transport_endpoint_t *rmt,
561 ip46_address_t *lcl_addr)
563 fib_node_index_t fei;
566 if (*sw_if_index == ENDPOINT_INVALID_INDEX)
568 /* Find a FIB path to the destination */
569 clib_memcpy_fast (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
570 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
571 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
573 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
574 fei = fib_table_lookup (rmt->fib_index, &prefix);
576 /* Couldn't find route to destination. Bail out. */
577 if (fei == FIB_NODE_INDEX_INVALID)
578 return SESSION_E_NOROUTE;
580 *sw_if_index = fib_entry_get_resolving_interface (fei);
581 if (*sw_if_index == ENDPOINT_INVALID_INDEX)
582 return SESSION_E_NOINTF;
585 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
586 return transport_get_interface_ip (*sw_if_index, rmt->is_ip4, lcl_addr);
590 transport_alloc_local_endpoint (u8 proto, transport_endpoint_cfg_t * rmt_cfg,
591 ip46_address_t * lcl_addr, u16 * lcl_port)
593 transport_endpoint_t *rmt = (transport_endpoint_t *) rmt_cfg;
594 session_error_t error;
598 * Find the local address
600 if (ip_is_zero (&rmt_cfg->peer.ip, rmt_cfg->peer.is_ip4))
602 error = transport_find_local_ip_for_remote (&rmt_cfg->peer.sw_if_index,
609 /* Assume session layer vetted this address */
610 clib_memcpy_fast (lcl_addr, &rmt_cfg->peer.ip,
611 sizeof (rmt_cfg->peer.ip));
615 * Allocate source port
617 if (rmt_cfg->peer.port == 0)
619 port = transport_alloc_local_port (proto, lcl_addr);
621 return SESSION_E_NOPORT;
626 port = clib_net_to_host_u16 (rmt_cfg->peer.port);
629 return transport_endpoint_mark_used (proto, lcl_addr, port);
636 format_clib_us_time (u8 * s, va_list * args)
638 clib_us_time_t t = va_arg (*args, clib_us_time_t);
640 s = format (s, "%u us", t);
642 s = format (s, "%.3f s", (f64) t * CLIB_US_TIME_PERIOD);
647 format_transport_pacer (u8 * s, va_list * args)
649 spacer_t *pacer = va_arg (*args, spacer_t *);
650 u32 thread_index = va_arg (*args, int);
651 clib_us_time_t now, diff;
653 now = transport_us_time_now (thread_index);
654 diff = now - pacer->last_update;
655 s = format (s, "rate %lu bucket %ld t/p %.3f last_update %U burst %u",
656 pacer->bytes_per_sec, pacer->bucket, pacer->tokens_per_period,
657 format_clib_us_time, diff, pacer->max_burst);
662 spacer_max_burst (spacer_t * pacer, clib_us_time_t time_now)
664 u64 n_periods = (time_now - pacer->last_update);
667 if ((inc = (f32) n_periods * pacer->tokens_per_period) > 10)
669 pacer->last_update = time_now;
670 pacer->bucket = clib_min (pacer->bucket + inc, (i64) pacer->max_burst);
673 return pacer->bucket >= 0 ? pacer->max_burst : 0;
677 spacer_update_bucket (spacer_t * pacer, u32 bytes)
679 pacer->bucket -= bytes;
683 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec,
684 clib_us_time_t rtt, clib_time_type_t sec_per_loop)
686 clib_us_time_t max_time;
688 ASSERT (rate_bytes_per_sec != 0);
689 pacer->bytes_per_sec = rate_bytes_per_sec;
690 pacer->tokens_per_period = rate_bytes_per_sec * CLIB_US_TIME_PERIOD;
692 /* Allow a min number of bursts per rtt, if their size is acceptable. Goal
693 * is to spread the sending of data over the rtt but to also allow for some
694 * coalescing that can potentially
695 * 1) reduce load on session layer by reducing scheduling frequency for a
697 * 2) optimize sending when tso if available
699 * Max "time-length" of a burst cannot be less than 1us or more than 1ms.
701 max_time = clib_max (rtt / TRANSPORT_PACER_BURSTS_PER_RTT,
702 (clib_us_time_t) (sec_per_loop * CLIB_US_TIME_FREQ));
703 max_time = clib_clamp (max_time, 1 /* 1us */ , 1000 /* 1ms */ );
704 pacer->max_burst = (rate_bytes_per_sec * max_time) * CLIB_US_TIME_PERIOD;
705 pacer->max_burst = clib_clamp (pacer->max_burst, TRANSPORT_PACER_MIN_BURST,
706 TRANSPORT_PACER_MAX_BURST);
710 spacer_pace_rate (spacer_t * pacer)
712 return pacer->bytes_per_sec;
716 spacer_reset (spacer_t * pacer, clib_us_time_t time_now, u64 bucket)
718 pacer->last_update = time_now;
719 pacer->bucket = bucket;
723 transport_connection_tx_pacer_reset (transport_connection_t * tc,
724 u64 rate_bytes_per_sec, u32 start_bucket,
727 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec, rtt,
728 transport_seconds_per_loop (tc->thread_index));
729 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index),
734 transport_connection_tx_pacer_reset_bucket (transport_connection_t * tc,
737 spacer_reset (&tc->pacer, transport_us_time_now (tc->thread_index), bucket);
741 transport_connection_tx_pacer_init (transport_connection_t * tc,
742 u64 rate_bytes_per_sec,
745 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
746 transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
747 initial_bucket, 1e6);
751 transport_connection_tx_pacer_update (transport_connection_t * tc,
752 u64 bytes_per_sec, clib_us_time_t rtt)
754 spacer_set_pace_rate (&tc->pacer, bytes_per_sec, rtt,
755 transport_seconds_per_loop (tc->thread_index));
759 transport_connection_tx_pacer_burst (transport_connection_t * tc)
761 return spacer_max_burst (&tc->pacer,
762 transport_us_time_now (tc->thread_index));
766 transport_connection_tx_pacer_rate (transport_connection_t * tc)
768 return spacer_pace_rate (&tc->pacer);
772 transport_connection_update_tx_bytes (transport_connection_t * tc, u32 bytes)
774 if (transport_connection_is_tx_paced (tc))
775 spacer_update_bucket (&tc->pacer, bytes);
779 transport_connection_tx_pacer_update_bytes (transport_connection_t * tc,
782 spacer_update_bucket (&tc->pacer, bytes);
786 transport_update_pacer_time (u32 thread_index, clib_time_type_t now)
788 session_wrk_update_time (session_main_get_worker (thread_index), now);
792 transport_connection_reschedule (transport_connection_t * tc)
794 tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
795 transport_connection_tx_pacer_reset_bucket (tc, 0 /* bucket */);
796 if (transport_max_tx_dequeue (tc))
797 sesssion_reschedule_tx (tc);
800 session_t *s = session_get (tc->s_index, tc->thread_index);
801 svm_fifo_unset_event (s->tx_fifo);
802 if (svm_fifo_max_dequeue_cons (s->tx_fifo))
803 if (svm_fifo_set_event (s->tx_fifo))
804 sesssion_reschedule_tx (tc);
809 transport_fifos_init_ooo (transport_connection_t * tc)
811 session_t *s = session_get (tc->s_index, tc->thread_index);
812 svm_fifo_init_ooo_lookup (s->rx_fifo, 0 /* ooo enq */ );
813 svm_fifo_init_ooo_lookup (s->tx_fifo, 1 /* ooo deq */ );
817 transport_update_time (clib_time_type_t time_now, u8 thread_index)
819 transport_proto_vft_t *vft;
820 vec_foreach (vft, tp_vfts)
822 if (vft->update_time)
823 (vft->update_time) (time_now, thread_index);
828 transport_enable_disable (vlib_main_t * vm, u8 is_en)
830 transport_proto_vft_t *vft;
831 vec_foreach (vft, tp_vfts)
834 (vft->enable) (vm, is_en);
836 if (vft->update_time)
837 session_register_update_time_fn (vft->update_time, is_en);
842 transport_init (void)
844 vlib_thread_main_t *vtm = vlib_get_thread_main ();
845 session_main_t *smm = vnet_get_session_main ();
846 transport_main_t *tm = &tp_main;
849 if (smm->local_endpoints_table_buckets == 0)
850 smm->local_endpoints_table_buckets = 250000;
851 if (smm->local_endpoints_table_memory == 0)
852 smm->local_endpoints_table_memory = 512 << 20;
854 /* Initialize [port-allocator] random number seed */
855 tm->port_allocator_seed = (u32) clib_cpu_time_now ();
857 clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoints table",
858 smm->local_endpoints_table_buckets,
859 smm->local_endpoints_table_memory);
860 clib_spinlock_init (&tm->local_endpoints_lock);
862 num_threads = 1 /* main thread */ + vtm->n_threads;
865 /* Main not polled if there are workers */
866 smm->transport_cl_thread = 1;
871 * fd.io coding-style-patch-verification: ON
874 * eval: (c-set-style "gnu")