blob: 20b912929b4444a500e2da4f60e256d5d2ef6ffb [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 Coras1c710452017-10-17 00:03:13 -070045u8 *
46format_transport_proto (u8 * s, va_list * args)
47{
48 u32 transport_proto = va_arg (*args, u32);
49 switch (transport_proto)
50 {
51 case TRANSPORT_PROTO_TCP:
52 s = format (s, "TCP");
53 break;
54 case TRANSPORT_PROTO_UDP:
55 s = format (s, "UDP");
56 break;
Marco Varlese191a5942017-10-30 18:17:21 +010057 case TRANSPORT_PROTO_SCTP:
58 s = format (s, "SCTP");
59 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070060 case TRANSPORT_PROTO_UDPC:
61 s = format (s, "UDPC");
62 break;
Florin Coras1c710452017-10-17 00:03:13 -070063 }
64 return s;
65}
66
Florin Coras561af9b2017-12-09 10:19:43 -080067u8 *
68format_transport_proto_short (u8 * s, va_list * args)
69{
70 u32 transport_proto = va_arg (*args, u32);
71 switch (transport_proto)
72 {
73 case TRANSPORT_PROTO_TCP:
74 s = format (s, "T");
75 break;
76 case TRANSPORT_PROTO_UDP:
77 s = format (s, "U");
78 break;
Florin Coras4399c2e2018-01-25 06:34:42 -080079 case TRANSPORT_PROTO_SCTP:
80 s = format (s, "S");
81 break;
Florin Coras7fb0fe12018-04-09 09:24:52 -070082 case TRANSPORT_PROTO_UDPC:
83 s = format (s, "U");
84 break;
Florin Coras561af9b2017-12-09 10:19:43 -080085 }
86 return s;
87}
88
Florin Coras1c710452017-10-17 00:03:13 -070089uword
90unformat_transport_proto (unformat_input_t * input, va_list * args)
91{
92 u32 *proto = va_arg (*args, u32 *);
93 if (unformat (input, "tcp"))
94 *proto = TRANSPORT_PROTO_TCP;
95 else if (unformat (input, "TCP"))
96 *proto = TRANSPORT_PROTO_TCP;
97 else if (unformat (input, "udp"))
98 *proto = TRANSPORT_PROTO_UDP;
99 else if (unformat (input, "UDP"))
100 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -0800101 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +0100102 *proto = TRANSPORT_PROTO_SCTP;
103 else if (unformat (input, "SCTP"))
104 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -0800105 else if (unformat (input, "tls"))
106 *proto = TRANSPORT_PROTO_TLS;
107 else if (unformat (input, "TLS"))
108 *proto = TRANSPORT_PROTO_TLS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700109 else if (unformat (input, "udpc"))
110 *proto = TRANSPORT_PROTO_UDPC;
111 else if (unformat (input, "UDPC"))
112 *proto = TRANSPORT_PROTO_UDPC;
Florin Coras1c710452017-10-17 00:03:13 -0700113 else
114 return 0;
115 return 1;
116}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700117
118u32
119transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
120 ip46_address_t * ip, u16 port)
121{
122 clib_bihash_kv_24_8_t kv;
123 int rv;
124
125 kv.key[0] = ip->as_u64[0];
126 kv.key[1] = ip->as_u64[1];
127 kv.key[2] = (u64) port << 8 | (u64) proto;
128
129 rv = clib_bihash_search_inline_24_8 (ht, &kv);
130 if (rv == 0)
131 return kv.value;
132
133 return ENDPOINT_INVALID_INDEX;
134}
135
136void
137transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
138 transport_endpoint_t * te, u32 value)
139{
140 clib_bihash_kv_24_8_t kv;
141
142 kv.key[0] = te->ip.as_u64[0];
143 kv.key[1] = te->ip.as_u64[1];
144 kv.key[2] = (u64) te->port << 8 | (u64) proto;
145 kv.value = value;
146
147 clib_bihash_add_del_24_8 (ht, &kv, 1);
148}
149
150void
151transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
152 transport_endpoint_t * te)
153{
154 clib_bihash_kv_24_8_t kv;
155
156 kv.key[0] = te->ip.as_u64[0];
157 kv.key[1] = te->ip.as_u64[1];
158 kv.key[2] = (u64) te->port << 8 | (u64) proto;
159
160 clib_bihash_add_del_24_8 (ht, &kv, 0);
161}
162
163/**
164 * Register transport virtual function table.
165 *
Florin Coras561af9b2017-12-09 10:19:43 -0800166 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
167 * @param vft - virtual function table for transport proto
168 * @param fib_proto - network layer protocol
169 * @param output_node - output node index that session layer will hand off
170 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700171 */
172void
Florin Coras561af9b2017-12-09 10:19:43 -0800173transport_register_protocol (transport_proto_t transport_proto,
174 const transport_proto_vft_t * vft,
175 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700176{
Florin Coras561af9b2017-12-09 10:19:43 -0800177 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700178
Florin Coras561af9b2017-12-09 10:19:43 -0800179 vec_validate (tp_vfts, transport_proto);
180 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700181
Florin Coras561af9b2017-12-09 10:19:43 -0800182 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700183}
184
185/**
186 * Get transport virtual function table
187 *
188 * @param type - session type (not protocol type)
189 */
190transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800191transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700192{
Florin Coras561af9b2017-12-09 10:19:43 -0800193 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700194 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800195 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700196}
197
Florin Coras7fb0fe12018-04-09 09:24:52 -0700198transport_service_type_t
199transport_protocol_service_type (transport_proto_t tp)
200{
201 return tp_vfts[tp].service_type;
202}
203
Florin Coras3cbc04b2017-10-02 00:18:51 -0700204#define PORT_MASK ((1 << 16)- 1)
205
206void
207transport_endpoint_del (u32 tepi)
208{
209 clib_spinlock_lock_if_init (&local_endpoints_lock);
210 pool_put_index (local_endpoints, tepi);
211 clib_spinlock_unlock_if_init (&local_endpoints_lock);
212}
213
214always_inline transport_endpoint_t *
215transport_endpoint_new (void)
216{
217 transport_endpoint_t *tep;
218 pool_get (local_endpoints, tep);
219 return tep;
220}
221
222void
223transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
224{
225 u32 tepi;
226 transport_endpoint_t *tep;
227
228 /* Cleanup local endpoint if this was an active connect */
229 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
230 clib_net_to_host_u16 (port));
231 if (tepi != ENDPOINT_INVALID_INDEX)
232 {
233 tep = pool_elt_at_index (local_endpoints, tepi);
234 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
235 transport_endpoint_del (tepi);
236 }
237}
238
239/**
240 * Allocate local port and add if successful add entry to local endpoint
241 * table to mark the pair as used.
242 */
243int
244transport_alloc_local_port (u8 proto, ip46_address_t * ip)
245{
246 transport_endpoint_t *tep;
247 u32 tei;
248 u16 min = 1024, max = 65535; /* XXX configurable ? */
249 int tries, limit;
250
251 limit = max - min;
252
253 /* Only support active opens from thread 0 */
254 ASSERT (vlib_get_thread_index () == 0);
255
256 /* Search for first free slot */
257 for (tries = 0; tries < limit; tries++)
258 {
259 u16 port = 0;
260
261 /* Find a port in the specified range */
262 while (1)
263 {
264 port = random_u32 (&port_allocator_seed) & PORT_MASK;
265 if (PREDICT_TRUE (port >= min && port < max))
266 break;
267 }
268
269 /* Look it up. If not found, we're done */
270 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
271 port);
272 if (tei == ENDPOINT_INVALID_INDEX)
273 {
274 clib_spinlock_lock_if_init (&local_endpoints_lock);
275 tep = transport_endpoint_new ();
276 clib_memcpy (&tep->ip, ip, sizeof (*ip));
277 tep->port = port;
278 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
279 tep - local_endpoints);
280 clib_spinlock_unlock_if_init (&local_endpoints_lock);
281
282 return tep->port;
283 }
284 }
285 return -1;
286}
287
288int
289transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
290 ip46_address_t * lcl_addr, u16 * lcl_port)
291{
292 fib_prefix_t prefix;
293 fib_node_index_t fei;
294 u32 sw_if_index;
295 int port;
296
297 /*
298 * Find the local address and allocate port
299 */
300
301 /* Find a FIB path to the destination */
302 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
303 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
304 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
305
306 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
307 fei = fib_table_lookup (rmt->fib_index, &prefix);
308
309 /* Couldn't find route to destination. Bail out. */
310 if (fei == FIB_NODE_INDEX_INVALID)
311 {
312 clib_warning ("no route to destination");
313 return -1;
314 }
315
316 sw_if_index = rmt->sw_if_index;
317 if (sw_if_index == ENDPOINT_INVALID_INDEX)
318 sw_if_index = fib_entry_get_resolving_interface (fei);
319
320 if (sw_if_index == ENDPOINT_INVALID_INDEX)
321 {
322 clib_warning ("no resolving interface for %U", format_ip46_address,
323 &rmt->ip, (rmt->is_ip4 == 0) + 1);
324 return -1;
325 }
326
327 memset (lcl_addr, 0, sizeof (*lcl_addr));
328
329 if (rmt->is_ip4)
330 {
331 ip4_address_t *ip4;
332 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800333 if (!ip4)
334 {
335 clib_warning ("no routable ip4 address on %U",
336 format_vnet_sw_if_index_name, vnet_get_main (),
337 sw_if_index);
338 return -1;
339 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700340 lcl_addr->ip4.as_u32 = ip4->as_u32;
341 }
342 else
343 {
344 ip6_address_t *ip6;
345 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
346 if (ip6 == 0)
347 {
348 clib_warning ("no routable ip6 addresses on %U",
349 format_vnet_sw_if_index_name, vnet_get_main (),
350 sw_if_index);
351 return -1;
352 }
353 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
354 }
355
356 /* Allocate source port */
357 port = transport_alloc_local_port (proto, lcl_addr);
358 if (port < 1)
359 {
360 clib_warning ("Failed to allocate src port");
361 return -1;
362 }
363 *lcl_port = port;
364 return 0;
365}
366
367void
Florin Coras561af9b2017-12-09 10:19:43 -0800368transport_update_time (f64 time_now, u8 thread_index)
369{
370 transport_proto_vft_t *vft;
371 vec_foreach (vft, tp_vfts)
372 {
373 if (vft->update_time)
374 (vft->update_time) (time_now, thread_index);
375 }
376}
377
378void
379transport_enable_disable (vlib_main_t * vm, u8 is_en)
380{
381 transport_proto_vft_t *vft;
382 vec_foreach (vft, tp_vfts)
383 {
384 if (vft->enable)
385 (vft->enable) (vm, is_en);
386 }
387}
388
389void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700390transport_init (void)
391{
392 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras93e65802017-11-29 00:07:11 -0500393 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700394 u32 num_threads;
395
Florin Coras93e65802017-11-29 00:07:11 -0500396 if (smm->local_endpoints_table_buckets == 0)
397 smm->local_endpoints_table_buckets = 250000;
398 if (smm->local_endpoints_table_memory == 0)
399 smm->local_endpoints_table_memory = 512 << 20;
400
Florin Coras3cbc04b2017-10-02 00:18:51 -0700401 /* Initialize [port-allocator] random number seed */
402 port_allocator_seed = (u32) clib_cpu_time_now ();
403
404 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500405 smm->local_endpoints_table_buckets,
406 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700407 num_threads = 1 /* main thread */ + vtm->n_threads;
408 if (num_threads > 1)
409 clib_spinlock_init (&local_endpoints_lock);
410}
411
412/*
413 * fd.io coding-style-patch-verification: ON
414 *
415 * Local Variables:
416 * eval: (c-set-style "gnu")
417 * End:
418 */