blob: c333c4161d4f00b012beb5ef3ff1251d430e099e [file] [log] [blame]
Florin Coras3cbc04b2017-10-02 00:18:51 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/session/transport_interface.h>
17#include <vnet/session/session.h>
18#include <vnet/fib/fib.h>
19
20/**
21 * Per-type vector of transport protocol virtual function tables
22 */
23transport_proto_vft_t *tp_vfts;
24
25/*
26 * Port allocator seed
27 */
28static u32 port_allocator_seed;
29
30/*
31 * Local endpoints table
32 */
33static transport_endpoint_table_t local_endpoints_table;
34
35/*
36 * Pool of local endpoints
37 */
38static transport_endpoint_t *local_endpoints;
39
40/*
41 * Local endpoints pool lock
42 */
43static clib_spinlock_t local_endpoints_lock;
44
Florin Corasd67f1122018-05-21 17:47:40 -070045/*
46 * Period used by transport pacers. Initialized by session layer
47 */
48static double transport_pacer_period;
49
50#define TRANSPORT_PACER_MIN_MSS 1460
51
Florin Coras1c710452017-10-17 00:03:13 -070052u8 *
53format_transport_proto (u8 * s, va_list * args)
54{
55 u32 transport_proto = va_arg (*args, u32);
56 switch (transport_proto)
57 {
58 case TRANSPORT_PROTO_TCP:
59 s = format (s, "TCP");
60 break;
61 case TRANSPORT_PROTO_UDP:
62 s = format (s, "UDP");
63 break;
Marco Varlese191a5942017-10-30 18:17:21 +010064 case TRANSPORT_PROTO_SCTP:
65 s = format (s, "SCTP");
66 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070067 case TRANSPORT_PROTO_UDPC:
68 s = format (s, "UDPC");
69 break;
Florin Coras1c710452017-10-17 00:03:13 -070070 }
71 return s;
72}
73
Florin Coras561af9b2017-12-09 10:19:43 -080074u8 *
75format_transport_proto_short (u8 * s, va_list * args)
76{
77 u32 transport_proto = va_arg (*args, u32);
78 switch (transport_proto)
79 {
80 case TRANSPORT_PROTO_TCP:
81 s = format (s, "T");
82 break;
83 case TRANSPORT_PROTO_UDP:
84 s = format (s, "U");
85 break;
Florin Coras4399c2e2018-01-25 06:34:42 -080086 case TRANSPORT_PROTO_SCTP:
87 s = format (s, "S");
88 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070089 case TRANSPORT_PROTO_UDPC:
90 s = format (s, "U");
91 break;
Florin Coras561af9b2017-12-09 10:19:43 -080092 }
93 return s;
94}
95
Florin Coras1c710452017-10-17 00:03:13 -070096uword
97unformat_transport_proto (unformat_input_t * input, va_list * args)
98{
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;
Florin Coras4399c2e2018-01-25 06:34:42 -0800108 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100109 *proto = TRANSPORT_PROTO_SCTP;
110 else if (unformat (input, "SCTP"))
111 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800112 else if (unformat (input, "tls"))
113 *proto = TRANSPORT_PROTO_TLS;
114 else if (unformat (input, "TLS"))
115 *proto = TRANSPORT_PROTO_TLS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700116 else if (unformat (input, "udpc"))
117 *proto = TRANSPORT_PROTO_UDPC;
118 else if (unformat (input, "UDPC"))
119 *proto = TRANSPORT_PROTO_UDPC;
Florin Coras1c710452017-10-17 00:03:13 -0700120 else
121 return 0;
122 return 1;
123}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700124
125u32
126transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
127 ip46_address_t * ip, u16 port)
128{
129 clib_bihash_kv_24_8_t kv;
130 int rv;
131
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;
135
136 rv = clib_bihash_search_inline_24_8 (ht, &kv);
137 if (rv == 0)
138 return kv.value;
139
140 return ENDPOINT_INVALID_INDEX;
141}
142
143void
144transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
145 transport_endpoint_t * te, u32 value)
146{
147 clib_bihash_kv_24_8_t kv;
148
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;
152 kv.value = value;
153
154 clib_bihash_add_del_24_8 (ht, &kv, 1);
155}
156
157void
158transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
159 transport_endpoint_t * te)
160{
161 clib_bihash_kv_24_8_t kv;
162
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;
166
167 clib_bihash_add_del_24_8 (ht, &kv, 0);
168}
169
170/**
171 * Register transport virtual function table.
172 *
Florin Coras561af9b2017-12-09 10:19:43 -0800173 * @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
Florin Coras3cbc04b2017-10-02 00:18:51 -0700178 */
179void
Florin Coras561af9b2017-12-09 10:19:43 -0800180transport_register_protocol (transport_proto_t transport_proto,
181 const transport_proto_vft_t * vft,
182 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700183{
Florin Coras561af9b2017-12-09 10:19:43 -0800184 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700185
Florin Coras561af9b2017-12-09 10:19:43 -0800186 vec_validate (tp_vfts, transport_proto);
187 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700188
Florin Coras561af9b2017-12-09 10:19:43 -0800189 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700190}
191
192/**
193 * Get transport virtual function table
194 *
195 * @param type - session type (not protocol type)
196 */
197transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800198transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700199{
Florin Coras561af9b2017-12-09 10:19:43 -0800200 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800202 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203}
204
Florin Coras7fb0fe12018-04-09 09:24:52 -0700205transport_service_type_t
206transport_protocol_service_type (transport_proto_t tp)
207{
208 return tp_vfts[tp].service_type;
209}
210
Florin Corasf08f26d2018-05-10 13:20:47 -0700211transport_tx_fn_type_t
212transport_protocol_tx_fn_type (transport_proto_t tp)
213{
214 return tp_vfts[tp].tx_type;
215}
216
Florin Coras40903ac2018-06-10 14:41:23 -0700217u8
218transport_protocol_is_cl (transport_proto_t tp)
219{
220 return (tp_vfts[tp].service_type == TRANSPORT_SERVICE_CL);
221}
222
Florin Coras3cbc04b2017-10-02 00:18:51 -0700223#define PORT_MASK ((1 << 16)- 1)
224
225void
226transport_endpoint_del (u32 tepi)
227{
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);
231}
232
233always_inline transport_endpoint_t *
234transport_endpoint_new (void)
235{
236 transport_endpoint_t *tep;
237 pool_get (local_endpoints, tep);
238 return tep;
239}
240
241void
242transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
243{
244 u32 tepi;
245 transport_endpoint_t *tep;
246
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)
251 {
252 tep = pool_elt_at_index (local_endpoints, tepi);
253 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
254 transport_endpoint_del (tepi);
255 }
256}
257
258/**
259 * Allocate local port and add if successful add entry to local endpoint
260 * table to mark the pair as used.
261 */
262int
263transport_alloc_local_port (u8 proto, ip46_address_t * ip)
264{
265 transport_endpoint_t *tep;
266 u32 tei;
267 u16 min = 1024, max = 65535; /* XXX configurable ? */
268 int tries, limit;
269
270 limit = max - min;
271
272 /* Only support active opens from thread 0 */
273 ASSERT (vlib_get_thread_index () == 0);
274
275 /* Search for first free slot */
276 for (tries = 0; tries < limit; tries++)
277 {
278 u16 port = 0;
279
280 /* Find a port in the specified range */
281 while (1)
282 {
283 port = random_u32 (&port_allocator_seed) & PORT_MASK;
284 if (PREDICT_TRUE (port >= min && port < max))
285 break;
286 }
287
288 /* Look it up. If not found, we're done */
289 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
290 port);
291 if (tei == ENDPOINT_INVALID_INDEX)
292 {
293 clib_spinlock_lock_if_init (&local_endpoints_lock);
294 tep = transport_endpoint_new ();
295 clib_memcpy (&tep->ip, ip, sizeof (*ip));
296 tep->port = port;
297 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
298 tep - local_endpoints);
299 clib_spinlock_unlock_if_init (&local_endpoints_lock);
300
301 return tep->port;
302 }
303 }
304 return -1;
305}
306
307int
308transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
309 ip46_address_t * lcl_addr, u16 * lcl_port)
310{
311 fib_prefix_t prefix;
312 fib_node_index_t fei;
313 u32 sw_if_index;
314 int port;
315
316 /*
317 * Find the local address and allocate port
318 */
319
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;
324
325 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
326 fei = fib_table_lookup (rmt->fib_index, &prefix);
327
328 /* Couldn't find route to destination. Bail out. */
329 if (fei == FIB_NODE_INDEX_INVALID)
330 {
331 clib_warning ("no route to destination");
332 return -1;
333 }
334
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);
338
339 if (sw_if_index == ENDPOINT_INVALID_INDEX)
340 {
341 clib_warning ("no resolving interface for %U", format_ip46_address,
342 &rmt->ip, (rmt->is_ip4 == 0) + 1);
343 return -1;
344 }
345
Dave Barachb7b92992018-10-17 10:38:51 -0400346 clib_memset (lcl_addr, 0, sizeof (*lcl_addr));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700347
348 if (rmt->is_ip4)
349 {
350 ip4_address_t *ip4;
351 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800352 if (!ip4)
353 {
354 clib_warning ("no routable ip4 address on %U",
355 format_vnet_sw_if_index_name, vnet_get_main (),
356 sw_if_index);
357 return -1;
358 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700359 lcl_addr->ip4.as_u32 = ip4->as_u32;
360 }
361 else
362 {
363 ip6_address_t *ip6;
364 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
365 if (ip6 == 0)
366 {
367 clib_warning ("no routable ip6 addresses on %U",
368 format_vnet_sw_if_index_name, vnet_get_main (),
369 sw_if_index);
370 return -1;
371 }
372 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
373 }
374
375 /* Allocate source port */
376 port = transport_alloc_local_port (proto, lcl_addr);
377 if (port < 1)
378 {
379 clib_warning ("Failed to allocate src port");
380 return -1;
381 }
382 *lcl_port = port;
383 return 0;
384}
385
Florin Corasd67f1122018-05-21 17:47:40 -0700386#define SPACER_CPU_TICKS_PER_PERIOD_SHIFT 10
387#define SPACER_CPU_TICKS_PER_PERIOD (1 << SPACER_CPU_TICKS_PER_PERIOD_SHIFT)
388
389u8 *
390format_transport_pacer (u8 * s, va_list * args)
391{
392 spacer_t *pacer = va_arg (*args, spacer_t *);
393
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,
396 pacer->last_update);
397 return s;
398}
399
400static inline u32
401spacer_max_burst (spacer_t * pacer, u64 norm_time_now)
402{
403 u64 n_periods = norm_time_now - pacer->last_update;
404
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);
408}
409
410static inline void
411spacer_update_bucket (spacer_t * pacer, u32 bytes)
412{
413 ASSERT (pacer->bucket >= bytes);
414 pacer->bucket -= bytes;
415}
416
417static inline void
418spacer_update_max_burst_size (spacer_t * pacer, u32 max_burst_bytes)
419{
420 pacer->max_burst_size = clib_max (max_burst_bytes, TRANSPORT_PACER_MIN_MSS);
421}
422
423static inline void
424spacer_set_pace_rate (spacer_t * pacer, u64 rate_bytes_per_sec)
425{
426 ASSERT (rate_bytes_per_sec != 0);
427 pacer->tokens_per_period = rate_bytes_per_sec / transport_pacer_period;
428}
429
430void
431transport_connection_tx_pacer_init (transport_connection_t * tc,
432 u32 rate_bytes_per_sec, u32 burst_bytes)
433{
434 vlib_main_t *vm = vlib_get_main ();
435 u64 time_now = vm->clib_time.last_cpu_time;
436 spacer_t *pacer = &tc->pacer;
437
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;
443}
444
445void
446transport_connection_tx_pacer_update (transport_connection_t * tc,
447 u64 bytes_per_sec)
448{
449 u32 burst_size;
450
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);
454}
455
456u32
457transport_connection_max_tx_burst (transport_connection_t * tc, u64 time_now)
458{
459 u32 snd_space, max_paced_burst;
460 u32 mss;
461
462 snd_space = tp_vfts[tc->proto].send_space (tc);
463 if (transport_connection_is_tx_paced (tc))
464 {
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;
471 }
472 return snd_space;
473}
474
475void
476transport_connection_update_tx_stats (transport_connection_t * tc, u32 bytes)
477{
478 tc->stats.tx_bytes += bytes;
479 if (transport_connection_is_tx_paced (tc))
480 spacer_update_bucket (&tc->pacer, bytes);
481}
482
483void
484transport_init_tx_pacers_period (void)
485{
486 f64 cpu_freq = os_cpu_clock_frequency ();
487 transport_pacer_period = cpu_freq / SPACER_CPU_TICKS_PER_PERIOD;
488}
489
Florin Coras3cbc04b2017-10-02 00:18:51 -0700490void
Florin Coras561af9b2017-12-09 10:19:43 -0800491transport_update_time (f64 time_now, u8 thread_index)
492{
493 transport_proto_vft_t *vft;
494 vec_foreach (vft, tp_vfts)
495 {
496 if (vft->update_time)
497 (vft->update_time) (time_now, thread_index);
498 }
499}
500
501void
502transport_enable_disable (vlib_main_t * vm, u8 is_en)
503{
504 transport_proto_vft_t *vft;
505 vec_foreach (vft, tp_vfts)
506 {
507 if (vft->enable)
508 (vft->enable) (vm, is_en);
509 }
510}
511
512void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700513transport_init (void)
514{
515 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras93e65802017-11-29 00:07:11 -0500516 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700517 u32 num_threads;
518
Florin Coras93e65802017-11-29 00:07:11 -0500519 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;
523
Florin Coras3cbc04b2017-10-02 00:18:51 -0700524 /* Initialize [port-allocator] random number seed */
525 port_allocator_seed = (u32) clib_cpu_time_now ();
526
527 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500528 smm->local_endpoints_table_buckets,
529 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700530 num_threads = 1 /* main thread */ + vtm->n_threads;
531 if (num_threads > 1)
532 clib_spinlock_init (&local_endpoints_lock);
533}
534
535/*
536 * fd.io coding-style-patch-verification: ON
537 *
538 * Local Variables:
539 * eval: (c-set-style "gnu")
540 * End:
541 */