2 * Copyright (c) 2017 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_interface.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
53 format_transport_proto (u8 * s, va_list * args)
55 u32 transport_proto = va_arg (*args, u32);
56 switch (transport_proto)
58 case TRANSPORT_PROTO_TCP:
59 s = format (s, "TCP");
61 case TRANSPORT_PROTO_UDP:
62 s = format (s, "UDP");
64 case TRANSPORT_PROTO_SCTP:
65 s = format (s, "SCTP");
67 case TRANSPORT_PROTO_UDPC:
68 s = format (s, "UDPC");
75 format_transport_proto_short (u8 * s, va_list * args)
77 u32 transport_proto = va_arg (*args, u32);
78 switch (transport_proto)
80 case TRANSPORT_PROTO_TCP:
83 case TRANSPORT_PROTO_UDP:
86 case TRANSPORT_PROTO_SCTP:
89 case TRANSPORT_PROTO_UDPC:
97 unformat_transport_proto (unformat_input_t * input, va_list * args)
99 u32 *proto = va_arg (*args, u32 *);
100 if (unformat (input, "tcp"))
101 *proto = TRANSPORT_PROTO_TCP;
102 else if (unformat (input, "TCP"))
103 *proto = TRANSPORT_PROTO_TCP;
104 else if (unformat (input, "udp"))
105 *proto = TRANSPORT_PROTO_UDP;
106 else if (unformat (input, "UDP"))
107 *proto = TRANSPORT_PROTO_UDP;
108 else if (unformat (input, "sctp"))
109 *proto = TRANSPORT_PROTO_SCTP;
110 else if (unformat (input, "SCTP"))
111 *proto = TRANSPORT_PROTO_SCTP;
112 else if (unformat (input, "tls"))
113 *proto = TRANSPORT_PROTO_TLS;
114 else if (unformat (input, "TLS"))
115 *proto = TRANSPORT_PROTO_TLS;
116 else if (unformat (input, "udpc"))
117 *proto = TRANSPORT_PROTO_UDPC;
118 else if (unformat (input, "UDPC"))
119 *proto = TRANSPORT_PROTO_UDPC;
126 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
127 ip46_address_t * ip, u16 port)
129 clib_bihash_kv_24_8_t kv;
132 kv.key[0] = ip->as_u64[0];
133 kv.key[1] = ip->as_u64[1];
134 kv.key[2] = (u64) port << 8 | (u64) proto;
136 rv = clib_bihash_search_inline_24_8 (ht, &kv);
140 return ENDPOINT_INVALID_INDEX;
144 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
145 transport_endpoint_t * te, u32 value)
147 clib_bihash_kv_24_8_t kv;
149 kv.key[0] = te->ip.as_u64[0];
150 kv.key[1] = te->ip.as_u64[1];
151 kv.key[2] = (u64) te->port << 8 | (u64) proto;
154 clib_bihash_add_del_24_8 (ht, &kv, 1);
158 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
159 transport_endpoint_t * te)
161 clib_bihash_kv_24_8_t kv;
163 kv.key[0] = te->ip.as_u64[0];
164 kv.key[1] = te->ip.as_u64[1];
165 kv.key[2] = (u64) te->port << 8 | (u64) proto;
167 clib_bihash_add_del_24_8 (ht, &kv, 0);
171 * Register transport virtual function table.
173 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
174 * @param vft - virtual function table for transport proto
175 * @param fib_proto - network layer protocol
176 * @param output_node - output node index that session layer will hand off
177 * buffers to, for requested fib proto
180 transport_register_protocol (transport_proto_t transport_proto,
181 const transport_proto_vft_t * vft,
182 fib_protocol_t fib_proto, u32 output_node)
184 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
186 vec_validate (tp_vfts, transport_proto);
187 tp_vfts[transport_proto] = *vft;
189 session_register_transport (transport_proto, vft, is_ip4, output_node);
193 * Get transport virtual function table
195 * @param type - session type (not protocol type)
197 transport_proto_vft_t *
198 transport_protocol_get_vft (transport_proto_t transport_proto)
200 if (transport_proto >= vec_len (tp_vfts))
202 return &tp_vfts[transport_proto];
205 transport_service_type_t
206 transport_protocol_service_type (transport_proto_t tp)
208 return tp_vfts[tp].service_type;
211 transport_tx_fn_type_t
212 transport_protocol_tx_fn_type (transport_proto_t tp)
214 return tp_vfts[tp].tx_type;
218 transport_protocol_is_cl (transport_proto_t tp)
220 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
223 #define PORT_MASK ((1 << 16)- 1)
226 transport_endpoint_del (u32 tepi)
228 clib_spinlock_lock_if_init (&local_endpoints_lock);
229 pool_put_index (local_endpoints, tepi);
230 clib_spinlock_unlock_if_init (&local_endpoints_lock);
233 always_inline transport_endpoint_t *
234 transport_endpoint_new (void)
236 transport_endpoint_t *tep;
237 pool_get (local_endpoints, tep);
242 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
245 transport_endpoint_t *tep;
247 /* Cleanup local endpoint if this was an active connect */
248 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
249 clib_net_to_host_u16 (port));
250 if (tepi != ENDPOINT_INVALID_INDEX)
252 tep = pool_elt_at_index (local_endpoints, tepi);
253 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
254 transport_endpoint_del (tepi);
259 * Allocate local port and add if successful add entry to local endpoint
260 * table to mark the pair as used.
263 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
265 transport_endpoint_t *tep;
267 u16 min = 1024, max = 65535; /* XXX configurable ? */
272 /* Only support active opens from thread 0 */
273 ASSERT (vlib_get_thread_index () == 0);
275 /* Search for first free slot */
276 for (tries = 0; tries < limit; tries++)
280 /* Find a port in the specified range */
283 port = random_u32 (&port_allocator_seed) & PORT_MASK;
284 if (PREDICT_TRUE (port >= min && port < max))
288 /* Look it up. If not found, we're done */
289 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
291 if (tei == ENDPOINT_INVALID_INDEX)
293 clib_spinlock_lock_if_init (&local_endpoints_lock);
294 tep = transport_endpoint_new ();
295 clib_memcpy (&tep->ip, ip, sizeof (*ip));
297 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
298 tep - local_endpoints);
299 clib_spinlock_unlock_if_init (&local_endpoints_lock);
308 transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
309 ip46_address_t * lcl_addr, u16 * lcl_port)
312 fib_node_index_t fei;
317 * Find the local address and allocate port
320 /* Find a FIB path to the destination */
321 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
322 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
323 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
325 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
326 fei = fib_table_lookup (rmt->fib_index, &prefix);
328 /* Couldn't find route to destination. Bail out. */
329 if (fei == FIB_NODE_INDEX_INVALID)
331 clib_warning ("no route to destination");
335 sw_if_index = rmt->sw_if_index;
336 if (sw_if_index == ENDPOINT_INVALID_INDEX)
337 sw_if_index = fib_entry_get_resolving_interface (fei);
339 if (sw_if_index == ENDPOINT_INVALID_INDEX)
341 clib_warning ("no resolving interface for %U", format_ip46_address,
342 &rmt->ip, (rmt->is_ip4 == 0) + 1);
346 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
351 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
354 clib_warning ("no routable ip4 address on %U",
355 format_vnet_sw_if_index_name, vnet_get_main (),
359 lcl_addr->ip4.as_u32 = ip4->as_u32;
364 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
367 clib_warning ("no routable ip6 addresses on %U",
368 format_vnet_sw_if_index_name, vnet_get_main (),
372 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
375 /* Allocate source port */
376 port = transport_alloc_local_port (proto, lcl_addr);
379 clib_warning ("Failed to allocate src port");
386 #define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
387 #define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
390 format_transport_pacer (u8 * s, va_list * args)
392 spacer_t *pacer = va_arg (*args, spacer_t *);
394 s = format (s, "bucket %u max_burst %u tokens/period %.3f last_update %x",
395 pacer->bucket, pacer->max_burst_size, pacer->tokens_per_period,
401 spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
403 u64 n_periods = norm_time_now - pacer->last_update;
405 pacer->last_update = norm_time_now;
406 pacer->bucket += n_periods * pacer->tokens_per_period;
407 return clib_min (pacer->bucket, pacer->max_burst_size);
411 spacer_update_bucket (spacer_t * pacer, u32 bytes)
413 ASSERT (pacer->bucket >= bytes);
414 pacer->bucket -= bytes;
418 spacer_update_max_burst_size (spacer_t * pacer, u32 max_burst_bytes)
420 pacer->max_burst_size = clib_max (max_burst_bytes, TRANSPORT_PACER_MIN_MSS);
424 spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
426 ASSERT (rate_bytes_per_sec != 0);
427 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
431 transport_connection_tx_pacer_init (transport_connection_t * tc,
432 u32 rate_bytes_per_sec, u32 burst_bytes)
434 vlib_main_t *vm = vlib_get_main ();
435 u64 time_now = vm->clib_time.last_cpu_time;
436 spacer_t *pacer = &tc->pacer;
438 tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
439 spacer_update_max_burst_size (&tc->pacer, burst_bytes);
440 spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
441 pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
442 pacer->bucket = burst_bytes;
446 transport_connection_tx_pacer_update (transport_connection_t * tc,
451 burst_size = bytes_per_sec * transport_dispatch_period (tc->thread_index);
452 spacer_set_pace_rate (&tc->pacer, bytes_per_sec);
453 spacer_update_max_burst_size (&tc->pacer, burst_size);
457 transport_connection_max_tx_burst (transport_connection_t * tc, u64 time_now)
459 u32 snd_space, max_paced_burst;
462 snd_space = tp_vfts[tc->proto].send_space (tc);
463 if (transport_connection_is_tx_paced (tc))
465 time_now >>= SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
466 max_paced_burst = spacer_max_burst (&tc->pacer, time_now);
467 mss = tp_vfts[tc->proto].send_mss (tc);
468 max_paced_burst = (max_paced_burst < mss) ? 0 : max_paced_burst;
469 snd_space = clib_min (snd_space, max_paced_burst);
470 snd_space = snd_space - snd_space % mss;
476 transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
478 tc->stats.tx_bytes += bytes;
479 if (transport_connection_is_tx_paced (tc))
480 spacer_update_bucket (&tc->pacer, bytes);
484 transport_init_tx_pacers_period (void)
486 f64 cpu_freq = os_cpu_clock_frequency ();
487 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
491 transport_update_time (f64 time_now, u8 thread_index)
493 transport_proto_vft_t *vft;
494 vec_foreach (vft, tp_vfts)
496 if (vft->update_time)
497 (vft->update_time) (time_now, thread_index);
502 transport_enable_disable (vlib_main_t * vm, u8 is_en)
504 transport_proto_vft_t *vft;
505 vec_foreach (vft, tp_vfts)
508 (vft->enable) (vm, is_en);
513 transport_init (void)
515 vlib_thread_main_t *vtm = vlib_get_thread_main ();
516 session_manager_main_t *smm = vnet_get_session_manager_main ();
519 if (smm->local_endpoints_table_buckets == 0)
520 smm->local_endpoints_table_buckets = 250000;
521 if (smm->local_endpoints_table_memory == 0)
522 smm->local_endpoints_table_memory = 512 << 20;
524 /* Initialize [port-allocator] random number seed */
525 port_allocator_seed = (u32) clib_cpu_time_now ();
527 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
528 smm->local_endpoints_table_buckets,
529 smm->local_endpoints_table_memory);
530 num_threads = 1 /* main thread */ + vtm->n_threads;
532 clib_spinlock_init (&local_endpoints_lock);
536 * fd.io coding-style-patch-verification: ON
539 * eval: (c-set-style "gnu")