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