blob: 797bdad1eaa6335a7184a43a9af6e4ab5536a1e1 [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 Coras1c710452017-10-17 00:03:13 -070060 }
61 return s;
62}
63
Florin Coras561af9b2017-12-09 10:19:43 -080064u8 *
65format_transport_proto_short (u8 * s, va_list * args)
66{
67 u32 transport_proto = va_arg (*args, u32);
68 switch (transport_proto)
69 {
70 case TRANSPORT_PROTO_TCP:
71 s = format (s, "T");
72 break;
73 case TRANSPORT_PROTO_UDP:
74 s = format (s, "U");
75 break;
Florin Coras4399c2e2018-01-25 06:34:42 -080076 case TRANSPORT_PROTO_SCTP:
77 s = format (s, "S");
78 break;
Florin Coras561af9b2017-12-09 10:19:43 -080079 }
80 return s;
81}
82
Florin Coras1c710452017-10-17 00:03:13 -070083uword
84unformat_transport_proto (unformat_input_t * input, va_list * args)
85{
86 u32 *proto = va_arg (*args, u32 *);
87 if (unformat (input, "tcp"))
88 *proto = TRANSPORT_PROTO_TCP;
89 else if (unformat (input, "TCP"))
90 *proto = TRANSPORT_PROTO_TCP;
91 else if (unformat (input, "udp"))
92 *proto = TRANSPORT_PROTO_UDP;
93 else if (unformat (input, "UDP"))
94 *proto = TRANSPORT_PROTO_UDP;
Florin Coras4399c2e2018-01-25 06:34:42 -080095 else if (unformat (input, "sctp"))
Marco Varlese191a5942017-10-30 18:17:21 +010096 *proto = TRANSPORT_PROTO_SCTP;
97 else if (unformat (input, "SCTP"))
98 *proto = TRANSPORT_PROTO_SCTP;
Florin Coras371ca502018-02-21 12:07:41 -080099 else if (unformat (input, "tls"))
100 *proto = TRANSPORT_PROTO_TLS;
101 else if (unformat (input, "TLS"))
102 *proto = TRANSPORT_PROTO_TLS;
Florin Coras1c710452017-10-17 00:03:13 -0700103 else
104 return 0;
105 return 1;
106}
Florin Coras3cbc04b2017-10-02 00:18:51 -0700107
108u32
109transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
110 ip46_address_t * ip, u16 port)
111{
112 clib_bihash_kv_24_8_t kv;
113 int rv;
114
115 kv.key[0] = ip->as_u64[0];
116 kv.key[1] = ip->as_u64[1];
117 kv.key[2] = (u64) port << 8 | (u64) proto;
118
119 rv = clib_bihash_search_inline_24_8 (ht, &kv);
120 if (rv == 0)
121 return kv.value;
122
123 return ENDPOINT_INVALID_INDEX;
124}
125
126void
127transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
128 transport_endpoint_t * te, u32 value)
129{
130 clib_bihash_kv_24_8_t kv;
131
132 kv.key[0] = te->ip.as_u64[0];
133 kv.key[1] = te->ip.as_u64[1];
134 kv.key[2] = (u64) te->port << 8 | (u64) proto;
135 kv.value = value;
136
137 clib_bihash_add_del_24_8 (ht, &kv, 1);
138}
139
140void
141transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
142 transport_endpoint_t * te)
143{
144 clib_bihash_kv_24_8_t kv;
145
146 kv.key[0] = te->ip.as_u64[0];
147 kv.key[1] = te->ip.as_u64[1];
148 kv.key[2] = (u64) te->port << 8 | (u64) proto;
149
150 clib_bihash_add_del_24_8 (ht, &kv, 0);
151}
152
153/**
154 * Register transport virtual function table.
155 *
Florin Coras561af9b2017-12-09 10:19:43 -0800156 * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
157 * @param vft - virtual function table for transport proto
158 * @param fib_proto - network layer protocol
159 * @param output_node - output node index that session layer will hand off
160 * buffers to, for requested fib proto
Florin Coras3cbc04b2017-10-02 00:18:51 -0700161 */
162void
Florin Coras561af9b2017-12-09 10:19:43 -0800163transport_register_protocol (transport_proto_t transport_proto,
164 const transport_proto_vft_t * vft,
165 fib_protocol_t fib_proto, u32 output_node)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700166{
Florin Coras561af9b2017-12-09 10:19:43 -0800167 u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700168
Florin Coras561af9b2017-12-09 10:19:43 -0800169 vec_validate (tp_vfts, transport_proto);
170 tp_vfts[transport_proto] = *vft;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700171
Florin Coras561af9b2017-12-09 10:19:43 -0800172 session_register_transport (transport_proto, vft, is_ip4, output_node);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700173}
174
175/**
176 * Get transport virtual function table
177 *
178 * @param type - session type (not protocol type)
179 */
180transport_proto_vft_t *
Florin Coras561af9b2017-12-09 10:19:43 -0800181transport_protocol_get_vft (transport_proto_t transport_proto)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700182{
Florin Coras561af9b2017-12-09 10:19:43 -0800183 if (transport_proto >= vec_len (tp_vfts))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700184 return 0;
Florin Coras561af9b2017-12-09 10:19:43 -0800185 return &tp_vfts[transport_proto];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700186}
187
188#define PORT_MASK ((1 << 16)- 1)
189
190void
191transport_endpoint_del (u32 tepi)
192{
193 clib_spinlock_lock_if_init (&local_endpoints_lock);
194 pool_put_index (local_endpoints, tepi);
195 clib_spinlock_unlock_if_init (&local_endpoints_lock);
196}
197
198always_inline transport_endpoint_t *
199transport_endpoint_new (void)
200{
201 transport_endpoint_t *tep;
202 pool_get (local_endpoints, tep);
203 return tep;
204}
205
206void
207transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
208{
209 u32 tepi;
210 transport_endpoint_t *tep;
211
212 /* Cleanup local endpoint if this was an active connect */
213 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
214 clib_net_to_host_u16 (port));
215 if (tepi != ENDPOINT_INVALID_INDEX)
216 {
217 tep = pool_elt_at_index (local_endpoints, tepi);
218 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
219 transport_endpoint_del (tepi);
220 }
221}
222
223/**
224 * Allocate local port and add if successful add entry to local endpoint
225 * table to mark the pair as used.
226 */
227int
228transport_alloc_local_port (u8 proto, ip46_address_t * ip)
229{
230 transport_endpoint_t *tep;
231 u32 tei;
232 u16 min = 1024, max = 65535; /* XXX configurable ? */
233 int tries, limit;
234
235 limit = max - min;
236
237 /* Only support active opens from thread 0 */
238 ASSERT (vlib_get_thread_index () == 0);
239
240 /* Search for first free slot */
241 for (tries = 0; tries < limit; tries++)
242 {
243 u16 port = 0;
244
245 /* Find a port in the specified range */
246 while (1)
247 {
248 port = random_u32 (&port_allocator_seed) & PORT_MASK;
249 if (PREDICT_TRUE (port >= min && port < max))
250 break;
251 }
252
253 /* Look it up. If not found, we're done */
254 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
255 port);
256 if (tei == ENDPOINT_INVALID_INDEX)
257 {
258 clib_spinlock_lock_if_init (&local_endpoints_lock);
259 tep = transport_endpoint_new ();
260 clib_memcpy (&tep->ip, ip, sizeof (*ip));
261 tep->port = port;
262 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
263 tep - local_endpoints);
264 clib_spinlock_unlock_if_init (&local_endpoints_lock);
265
266 return tep->port;
267 }
268 }
269 return -1;
270}
271
272int
273transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
274 ip46_address_t * lcl_addr, u16 * lcl_port)
275{
276 fib_prefix_t prefix;
277 fib_node_index_t fei;
278 u32 sw_if_index;
279 int port;
280
281 /*
282 * Find the local address and allocate port
283 */
284
285 /* Find a FIB path to the destination */
286 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
287 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
288 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
289
290 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
291 fei = fib_table_lookup (rmt->fib_index, &prefix);
292
293 /* Couldn't find route to destination. Bail out. */
294 if (fei == FIB_NODE_INDEX_INVALID)
295 {
296 clib_warning ("no route to destination");
297 return -1;
298 }
299
300 sw_if_index = rmt->sw_if_index;
301 if (sw_if_index == ENDPOINT_INVALID_INDEX)
302 sw_if_index = fib_entry_get_resolving_interface (fei);
303
304 if (sw_if_index == ENDPOINT_INVALID_INDEX)
305 {
306 clib_warning ("no resolving interface for %U", format_ip46_address,
307 &rmt->ip, (rmt->is_ip4 == 0) + 1);
308 return -1;
309 }
310
311 memset (lcl_addr, 0, sizeof (*lcl_addr));
312
313 if (rmt->is_ip4)
314 {
315 ip4_address_t *ip4;
316 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
Florin Corasfc804d92018-01-26 01:27:01 -0800317 if (!ip4)
318 {
319 clib_warning ("no routable ip4 address on %U",
320 format_vnet_sw_if_index_name, vnet_get_main (),
321 sw_if_index);
322 return -1;
323 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700324 lcl_addr->ip4.as_u32 = ip4->as_u32;
325 }
326 else
327 {
328 ip6_address_t *ip6;
329 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
330 if (ip6 == 0)
331 {
332 clib_warning ("no routable ip6 addresses on %U",
333 format_vnet_sw_if_index_name, vnet_get_main (),
334 sw_if_index);
335 return -1;
336 }
337 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
338 }
339
340 /* Allocate source port */
341 port = transport_alloc_local_port (proto, lcl_addr);
342 if (port < 1)
343 {
344 clib_warning ("Failed to allocate src port");
345 return -1;
346 }
347 *lcl_port = port;
348 return 0;
349}
350
351void
Florin Coras561af9b2017-12-09 10:19:43 -0800352transport_update_time (f64 time_now, u8 thread_index)
353{
354 transport_proto_vft_t *vft;
355 vec_foreach (vft, tp_vfts)
356 {
357 if (vft->update_time)
358 (vft->update_time) (time_now, thread_index);
359 }
360}
361
362void
363transport_enable_disable (vlib_main_t * vm, u8 is_en)
364{
365 transport_proto_vft_t *vft;
366 vec_foreach (vft, tp_vfts)
367 {
368 if (vft->enable)
369 (vft->enable) (vm, is_en);
370 }
371}
372
373void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700374transport_init (void)
375{
376 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Florin Coras93e65802017-11-29 00:07:11 -0500377 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700378 u32 num_threads;
379
Florin Coras93e65802017-11-29 00:07:11 -0500380 if (smm->local_endpoints_table_buckets == 0)
381 smm->local_endpoints_table_buckets = 250000;
382 if (smm->local_endpoints_table_memory == 0)
383 smm->local_endpoints_table_memory = 512 << 20;
384
Florin Coras3cbc04b2017-10-02 00:18:51 -0700385 /* Initialize [port-allocator] random number seed */
386 port_allocator_seed = (u32) clib_cpu_time_now ();
387
388 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
Florin Coras93e65802017-11-29 00:07:11 -0500389 smm->local_endpoints_table_buckets,
390 smm->local_endpoints_table_memory);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700391 num_threads = 1 /* main thread */ + vtm->n_threads;
392 if (num_threads > 1)
393 clib_spinlock_init (&local_endpoints_lock);
394}
395
396/*
397 * fd.io coding-style-patch-verification: ON
398 *
399 * Local Variables:
400 * eval: (c-set-style "gnu")
401 * End:
402 */