blob: c18cf15974e76d4c449816447ba524578bd06a2f [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;
57 }
58 return s;
59}
60
61uword
62unformat_transport_proto (unformat_input_t * input, va_list * args)
63{
64 u32 *proto = va_arg (*args, u32 *);
65 if (unformat (input, "tcp"))
66 *proto = TRANSPORT_PROTO_TCP;
67 else if (unformat (input, "TCP"))
68 *proto = TRANSPORT_PROTO_TCP;
69 else if (unformat (input, "udp"))
70 *proto = TRANSPORT_PROTO_UDP;
71 else if (unformat (input, "UDP"))
72 *proto = TRANSPORT_PROTO_UDP;
73 else
74 return 0;
75 return 1;
76}
Florin Coras3cbc04b2017-10-02 00:18:51 -070077
78u32
79transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
80 ip46_address_t * ip, u16 port)
81{
82 clib_bihash_kv_24_8_t kv;
83 int rv;
84
85 kv.key[0] = ip->as_u64[0];
86 kv.key[1] = ip->as_u64[1];
87 kv.key[2] = (u64) port << 8 | (u64) proto;
88
89 rv = clib_bihash_search_inline_24_8 (ht, &kv);
90 if (rv == 0)
91 return kv.value;
92
93 return ENDPOINT_INVALID_INDEX;
94}
95
96void
97transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
98 transport_endpoint_t * te, u32 value)
99{
100 clib_bihash_kv_24_8_t kv;
101
102 kv.key[0] = te->ip.as_u64[0];
103 kv.key[1] = te->ip.as_u64[1];
104 kv.key[2] = (u64) te->port << 8 | (u64) proto;
105 kv.value = value;
106
107 clib_bihash_add_del_24_8 (ht, &kv, 1);
108}
109
110void
111transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
112 transport_endpoint_t * te)
113{
114 clib_bihash_kv_24_8_t kv;
115
116 kv.key[0] = te->ip.as_u64[0];
117 kv.key[1] = te->ip.as_u64[1];
118 kv.key[2] = (u64) te->port << 8 | (u64) proto;
119
120 clib_bihash_add_del_24_8 (ht, &kv, 0);
121}
122
123/**
124 * Register transport virtual function table.
125 *
126 * @param type - session type (not protocol type)
127 * @param vft - virtual function table
128 */
129void
130transport_register_protocol (transport_proto_t transport_proto, u8 is_ip4,
131 const transport_proto_vft_t * vft)
132{
133 u8 session_type;
134 session_type = session_type_from_proto_and_ip (transport_proto, is_ip4);
135
136 vec_validate (tp_vfts, session_type);
137 tp_vfts[session_type] = *vft;
138
139 /* If an offset function is provided, then peek instead of dequeue */
140 session_manager_set_transport_rx_fn (session_type,
141 vft->tx_fifo_offset != 0);
142}
143
144/**
145 * Get transport virtual function table
146 *
147 * @param type - session type (not protocol type)
148 */
149transport_proto_vft_t *
150transport_protocol_get_vft (u8 session_type)
151{
152 if (session_type >= vec_len (tp_vfts))
153 return 0;
154 return &tp_vfts[session_type];
155}
156
157#define PORT_MASK ((1 << 16)- 1)
158
159void
160transport_endpoint_del (u32 tepi)
161{
162 clib_spinlock_lock_if_init (&local_endpoints_lock);
163 pool_put_index (local_endpoints, tepi);
164 clib_spinlock_unlock_if_init (&local_endpoints_lock);
165}
166
167always_inline transport_endpoint_t *
168transport_endpoint_new (void)
169{
170 transport_endpoint_t *tep;
171 pool_get (local_endpoints, tep);
172 return tep;
173}
174
175void
176transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
177{
178 u32 tepi;
179 transport_endpoint_t *tep;
180
181 /* Cleanup local endpoint if this was an active connect */
182 tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
183 clib_net_to_host_u16 (port));
184 if (tepi != ENDPOINT_INVALID_INDEX)
185 {
186 tep = pool_elt_at_index (local_endpoints, tepi);
187 transport_endpoint_table_del (&local_endpoints_table, proto, tep);
188 transport_endpoint_del (tepi);
189 }
190}
191
192/**
193 * Allocate local port and add if successful add entry to local endpoint
194 * table to mark the pair as used.
195 */
196int
197transport_alloc_local_port (u8 proto, ip46_address_t * ip)
198{
199 transport_endpoint_t *tep;
200 u32 tei;
201 u16 min = 1024, max = 65535; /* XXX configurable ? */
202 int tries, limit;
203
204 limit = max - min;
205
206 /* Only support active opens from thread 0 */
207 ASSERT (vlib_get_thread_index () == 0);
208
209 /* Search for first free slot */
210 for (tries = 0; tries < limit; tries++)
211 {
212 u16 port = 0;
213
214 /* Find a port in the specified range */
215 while (1)
216 {
217 port = random_u32 (&port_allocator_seed) & PORT_MASK;
218 if (PREDICT_TRUE (port >= min && port < max))
219 break;
220 }
221
222 /* Look it up. If not found, we're done */
223 tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
224 port);
225 if (tei == ENDPOINT_INVALID_INDEX)
226 {
227 clib_spinlock_lock_if_init (&local_endpoints_lock);
228 tep = transport_endpoint_new ();
229 clib_memcpy (&tep->ip, ip, sizeof (*ip));
230 tep->port = port;
231 transport_endpoint_table_add (&local_endpoints_table, proto, tep,
232 tep - local_endpoints);
233 clib_spinlock_unlock_if_init (&local_endpoints_lock);
234
235 return tep->port;
236 }
237 }
238 return -1;
239}
240
241int
242transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
243 ip46_address_t * lcl_addr, u16 * lcl_port)
244{
245 fib_prefix_t prefix;
246 fib_node_index_t fei;
247 u32 sw_if_index;
248 int port;
249
250 /*
251 * Find the local address and allocate port
252 */
253
254 /* Find a FIB path to the destination */
255 clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
256 prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
257 prefix.fp_len = rmt->is_ip4 ? 32 : 128;
258
259 ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
260 fei = fib_table_lookup (rmt->fib_index, &prefix);
261
262 /* Couldn't find route to destination. Bail out. */
263 if (fei == FIB_NODE_INDEX_INVALID)
264 {
265 clib_warning ("no route to destination");
266 return -1;
267 }
268
269 sw_if_index = rmt->sw_if_index;
270 if (sw_if_index == ENDPOINT_INVALID_INDEX)
271 sw_if_index = fib_entry_get_resolving_interface (fei);
272
273 if (sw_if_index == ENDPOINT_INVALID_INDEX)
274 {
275 clib_warning ("no resolving interface for %U", format_ip46_address,
276 &rmt->ip, (rmt->is_ip4 == 0) + 1);
277 return -1;
278 }
279
280 memset (lcl_addr, 0, sizeof (*lcl_addr));
281
282 if (rmt->is_ip4)
283 {
284 ip4_address_t *ip4;
285 ip4 = ip_interface_get_first_ip (sw_if_index, 1);
286 lcl_addr->ip4.as_u32 = ip4->as_u32;
287 }
288 else
289 {
290 ip6_address_t *ip6;
291 ip6 = ip_interface_get_first_ip (sw_if_index, 0);
292 if (ip6 == 0)
293 {
294 clib_warning ("no routable ip6 addresses on %U",
295 format_vnet_sw_if_index_name, vnet_get_main (),
296 sw_if_index);
297 return -1;
298 }
299 clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
300 }
301
302 /* Allocate source port */
303 port = transport_alloc_local_port (proto, lcl_addr);
304 if (port < 1)
305 {
306 clib_warning ("Failed to allocate src port");
307 return -1;
308 }
309 *lcl_port = port;
310 return 0;
311}
312
313void
314transport_init (void)
315{
316 vlib_thread_main_t *vtm = vlib_get_thread_main ();
317 u32 local_endpoints_table_buckets = 250000;
318 u32 local_endpoints_table_memory = 512 << 20;
319 u32 num_threads;
320
321 /* Initialize [port-allocator] random number seed */
322 port_allocator_seed = (u32) clib_cpu_time_now ();
323
324 clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
325 local_endpoints_table_buckets,
326 local_endpoints_table_memory);
327 num_threads = 1 /* main thread */ + vtm->n_threads;
328 if (num_threads > 1)
329 clib_spinlock_init (&local_endpoints_lock);
330}
331
332/*
333 * fd.io coding-style-patch-verification: ON
334 *
335 * Local Variables:
336 * eval: (c-set-style "gnu")
337 * End:
338 */