acbb4f65e7a42251660bcf37259c7cf7fe326569
[vpp.git] / src / vnet / session / transport.c
1 /*
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  */
23 transport_proto_vft_t *tp_vfts;
24
25 /*
26  * Port allocator seed
27  */
28 static u32 port_allocator_seed;
29
30 /*
31  * Local endpoints table
32  */
33 static transport_endpoint_table_t local_endpoints_table;
34
35 /*
36  * Pool of local endpoints
37  */
38 static transport_endpoint_t *local_endpoints;
39
40 /*
41  * Local endpoints pool lock
42  */
43 static clib_spinlock_t local_endpoints_lock;
44
45 u8 *
46 format_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     case TRANSPORT_PROTO_SCTP:
58       s = format (s, "SCTP");
59       break;
60     }
61   return s;
62 }
63
64 u8 *
65 format_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;
76     case TRANSPORT_PROTO_SCTP:
77       s = format (s, "S");
78       break;
79     }
80   return s;
81 }
82
83 uword
84 unformat_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;
95   else if (unformat (input, "sctp"))
96     *proto = TRANSPORT_PROTO_SCTP;
97   else if (unformat (input, "SCTP"))
98     *proto = TRANSPORT_PROTO_SCTP;
99   else
100     return 0;
101   return 1;
102 }
103
104 u32
105 transport_endpoint_lookup (transport_endpoint_table_t * ht, u8 proto,
106                            ip46_address_t * ip, u16 port)
107 {
108   clib_bihash_kv_24_8_t kv;
109   int rv;
110
111   kv.key[0] = ip->as_u64[0];
112   kv.key[1] = ip->as_u64[1];
113   kv.key[2] = (u64) port << 8 | (u64) proto;
114
115   rv = clib_bihash_search_inline_24_8 (ht, &kv);
116   if (rv == 0)
117     return kv.value;
118
119   return ENDPOINT_INVALID_INDEX;
120 }
121
122 void
123 transport_endpoint_table_add (transport_endpoint_table_t * ht, u8 proto,
124                               transport_endpoint_t * te, u32 value)
125 {
126   clib_bihash_kv_24_8_t kv;
127
128   kv.key[0] = te->ip.as_u64[0];
129   kv.key[1] = te->ip.as_u64[1];
130   kv.key[2] = (u64) te->port << 8 | (u64) proto;
131   kv.value = value;
132
133   clib_bihash_add_del_24_8 (ht, &kv, 1);
134 }
135
136 void
137 transport_endpoint_table_del (transport_endpoint_table_t * ht, u8 proto,
138                               transport_endpoint_t * te)
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
146   clib_bihash_add_del_24_8 (ht, &kv, 0);
147 }
148
149 /**
150  * Register transport virtual function table.
151  *
152  * @param transport_proto - transport protocol type (i.e., TCP, UDP ..)
153  * @param vft - virtual function table for transport proto
154  * @param fib_proto - network layer protocol
155  * @param output_node - output node index that session layer will hand off
156  *                      buffers to, for requested fib proto
157  */
158 void
159 transport_register_protocol (transport_proto_t transport_proto,
160                              const transport_proto_vft_t * vft,
161                              fib_protocol_t fib_proto, u32 output_node)
162 {
163   u8 is_ip4 = fib_proto == FIB_PROTOCOL_IP4;
164
165   vec_validate (tp_vfts, transport_proto);
166   tp_vfts[transport_proto] = *vft;
167
168   session_register_transport (transport_proto, vft, is_ip4, output_node);
169 }
170
171 /**
172  * Get transport virtual function table
173  *
174  * @param type - session type (not protocol type)
175  */
176 transport_proto_vft_t *
177 transport_protocol_get_vft (transport_proto_t transport_proto)
178 {
179   if (transport_proto >= vec_len (tp_vfts))
180     return 0;
181   return &tp_vfts[transport_proto];
182 }
183
184 #define PORT_MASK ((1 << 16)- 1)
185
186 void
187 transport_endpoint_del (u32 tepi)
188 {
189   clib_spinlock_lock_if_init (&local_endpoints_lock);
190   pool_put_index (local_endpoints, tepi);
191   clib_spinlock_unlock_if_init (&local_endpoints_lock);
192 }
193
194 always_inline transport_endpoint_t *
195 transport_endpoint_new (void)
196 {
197   transport_endpoint_t *tep;
198   pool_get (local_endpoints, tep);
199   return tep;
200 }
201
202 void
203 transport_endpoint_cleanup (u8 proto, ip46_address_t * lcl_ip, u16 port)
204 {
205   u32 tepi;
206   transport_endpoint_t *tep;
207
208   /* Cleanup local endpoint if this was an active connect */
209   tepi = transport_endpoint_lookup (&local_endpoints_table, proto, lcl_ip,
210                                     clib_net_to_host_u16 (port));
211   if (tepi != ENDPOINT_INVALID_INDEX)
212     {
213       tep = pool_elt_at_index (local_endpoints, tepi);
214       transport_endpoint_table_del (&local_endpoints_table, proto, tep);
215       transport_endpoint_del (tepi);
216     }
217 }
218
219 /**
220  * Allocate local port and add if successful add entry to local endpoint
221  * table to mark the pair as used.
222  */
223 int
224 transport_alloc_local_port (u8 proto, ip46_address_t * ip)
225 {
226   transport_endpoint_t *tep;
227   u32 tei;
228   u16 min = 1024, max = 65535;  /* XXX configurable ? */
229   int tries, limit;
230
231   limit = max - min;
232
233   /* Only support active opens from thread 0 */
234   ASSERT (vlib_get_thread_index () == 0);
235
236   /* Search for first free slot */
237   for (tries = 0; tries < limit; tries++)
238     {
239       u16 port = 0;
240
241       /* Find a port in the specified range */
242       while (1)
243         {
244           port = random_u32 (&port_allocator_seed) & PORT_MASK;
245           if (PREDICT_TRUE (port >= min && port < max))
246             break;
247         }
248
249       /* Look it up. If not found, we're done */
250       tei = transport_endpoint_lookup (&local_endpoints_table, proto, ip,
251                                        port);
252       if (tei == ENDPOINT_INVALID_INDEX)
253         {
254           clib_spinlock_lock_if_init (&local_endpoints_lock);
255           tep = transport_endpoint_new ();
256           clib_memcpy (&tep->ip, ip, sizeof (*ip));
257           tep->port = port;
258           transport_endpoint_table_add (&local_endpoints_table, proto, tep,
259                                         tep - local_endpoints);
260           clib_spinlock_unlock_if_init (&local_endpoints_lock);
261
262           return tep->port;
263         }
264     }
265   return -1;
266 }
267
268 int
269 transport_alloc_local_endpoint (u8 proto, transport_endpoint_t * rmt,
270                                 ip46_address_t * lcl_addr, u16 * lcl_port)
271 {
272   fib_prefix_t prefix;
273   fib_node_index_t fei;
274   u32 sw_if_index;
275   int port;
276
277   /*
278    * Find the local address and allocate port
279    */
280
281   /* Find a FIB path to the destination */
282   clib_memcpy (&prefix.fp_addr, &rmt->ip, sizeof (rmt->ip));
283   prefix.fp_proto = rmt->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
284   prefix.fp_len = rmt->is_ip4 ? 32 : 128;
285
286   ASSERT (rmt->fib_index != ENDPOINT_INVALID_INDEX);
287   fei = fib_table_lookup (rmt->fib_index, &prefix);
288
289   /* Couldn't find route to destination. Bail out. */
290   if (fei == FIB_NODE_INDEX_INVALID)
291     {
292       clib_warning ("no route to destination");
293       return -1;
294     }
295
296   sw_if_index = rmt->sw_if_index;
297   if (sw_if_index == ENDPOINT_INVALID_INDEX)
298     sw_if_index = fib_entry_get_resolving_interface (fei);
299
300   if (sw_if_index == ENDPOINT_INVALID_INDEX)
301     {
302       clib_warning ("no resolving interface for %U", format_ip46_address,
303                     &rmt->ip, (rmt->is_ip4 == 0) + 1);
304       return -1;
305     }
306
307   memset (lcl_addr, 0, sizeof (*lcl_addr));
308
309   if (rmt->is_ip4)
310     {
311       ip4_address_t *ip4;
312       ip4 = ip_interface_get_first_ip (sw_if_index, 1);
313       if (!ip4)
314         {
315           clib_warning ("no routable ip4 address on %U",
316                         format_vnet_sw_if_index_name, vnet_get_main (),
317                         sw_if_index);
318           return -1;
319         }
320       lcl_addr->ip4.as_u32 = ip4->as_u32;
321     }
322   else
323     {
324       ip6_address_t *ip6;
325       ip6 = ip_interface_get_first_ip (sw_if_index, 0);
326       if (ip6 == 0)
327         {
328           clib_warning ("no routable ip6 addresses on %U",
329                         format_vnet_sw_if_index_name, vnet_get_main (),
330                         sw_if_index);
331           return -1;
332         }
333       clib_memcpy (&lcl_addr->ip6, ip6, sizeof (*ip6));
334     }
335
336   /* Allocate source port */
337   port = transport_alloc_local_port (proto, lcl_addr);
338   if (port < 1)
339     {
340       clib_warning ("Failed to allocate src port");
341       return -1;
342     }
343   *lcl_port = port;
344   return 0;
345 }
346
347 void
348 transport_update_time (f64 time_now, u8 thread_index)
349 {
350   transport_proto_vft_t *vft;
351   vec_foreach (vft, tp_vfts)
352   {
353     if (vft->update_time)
354       (vft->update_time) (time_now, thread_index);
355   }
356 }
357
358 void
359 transport_enable_disable (vlib_main_t * vm, u8 is_en)
360 {
361   transport_proto_vft_t *vft;
362   vec_foreach (vft, tp_vfts)
363   {
364     if (vft->enable)
365       (vft->enable) (vm, is_en);
366   }
367 }
368
369 void
370 transport_init (void)
371 {
372   vlib_thread_main_t *vtm = vlib_get_thread_main ();
373   session_manager_main_t *smm = vnet_get_session_manager_main ();
374   u32 num_threads;
375
376   if (smm->local_endpoints_table_buckets == 0)
377     smm->local_endpoints_table_buckets = 250000;
378   if (smm->local_endpoints_table_memory == 0)
379     smm->local_endpoints_table_memory = 512 << 20;
380
381   /* Initialize [port-allocator] random number seed */
382   port_allocator_seed = (u32) clib_cpu_time_now ();
383
384   clib_bihash_init_24_8 (&local_endpoints_table, "local endpoints table",
385                          smm->local_endpoints_table_buckets,
386                          smm->local_endpoints_table_memory);
387   num_threads = 1 /* main thread */  + vtm->n_threads;
388   if (num_threads > 1)
389     clib_spinlock_init (&local_endpoints_lock);
390 }
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */