udp: avoid locking connected udp sessions on rx
[vpp.git] / src / vnet / udp / udp.c
1 /*
2  * Copyright (c) 2016-2020 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/udp/udp.h>
17 #include <vnet/session/session.h>
18 #include <vnet/dpo/load_balance.h>
19 #include <vnet/ip/ip4_inlines.h>
20 #include <vnet/ip/ip6_inlines.h>
21 #include <vppinfra/sparse_vec.h>
22
23 udp_main_t udp_main;
24
25 static void
26 udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
27 {
28   udp_main_t *um = &udp_main;
29   udp_dst_port_info_t *pi;
30   u16 *n;
31
32   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
33   if (!pi)
34     {
35       udp_add_dst_port (um, lcl_port, 0, is_ip4);
36       pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
37       pi->n_connections = 1;
38     }
39   else
40     {
41       pi->n_connections += 1;
42       /* Do not return. The fact that the pi is valid does not mean
43        * it's up to date */
44     }
45
46   pi->node_index = is_ip4 ? udp4_input_node.index : udp6_input_node.index;
47   pi->next_index = um->local_to_input_edge[is_ip4];
48
49   /* Setup udp protocol -> next index sparse vector mapping. */
50   if (is_ip4)
51     n = sparse_vec_validate (um->next_by_dst_port4,
52                              clib_host_to_net_u16 (lcl_port));
53   else
54     n = sparse_vec_validate (um->next_by_dst_port6,
55                              clib_host_to_net_u16 (lcl_port));
56
57   n[0] = pi->next_index;
58 }
59
60 static void
61 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
62 {
63   udp_main_t *um = &udp_main;
64   udp_dst_port_info_t *pi;
65
66   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
67   if (!pi)
68     return;
69
70   if (!pi->n_connections)
71     {
72       clib_warning ("no connections using port %u", lcl_port);
73       return;
74     }
75
76   if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
77     udp_unregister_dst_port (0, lcl_port, is_ip4);
78 }
79
80 void
81 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
82 {
83   udp_main_t *um = &udp_main;
84   udp_dst_port_info_t *pi;
85
86   /* Done without a lock but the operation is atomic. Writers to pi hash
87    * table and vector should be guarded by a barrier sync */
88   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
89   clib_atomic_fetch_add_rel (&pi->n_connections, 1);
90 }
91
92 udp_connection_t *
93 udp_connection_alloc (u32 thread_index)
94 {
95   udp_main_t *um = &udp_main;
96   udp_connection_t *uc;
97   u32 will_expand = 0;
98   pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
99                                 CLIB_CACHE_LINE_BYTES);
100
101   if (PREDICT_FALSE (will_expand))
102     {
103       clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
104                                   [thread_index]);
105       pool_get_aligned (udp_main.connections[thread_index], uc,
106                         CLIB_CACHE_LINE_BYTES);
107       clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
108                                     [thread_index]);
109     }
110   else
111     {
112       pool_get_aligned (um->connections[thread_index], uc,
113                         CLIB_CACHE_LINE_BYTES);
114     }
115   clib_memset (uc, 0, sizeof (*uc));
116   uc->c_c_index = uc - um->connections[thread_index];
117   uc->c_thread_index = thread_index;
118   uc->c_proto = TRANSPORT_PROTO_UDP;
119   clib_spinlock_init (&uc->rx_lock);
120   return uc;
121 }
122
123 void
124 udp_connection_free (udp_connection_t * uc)
125 {
126   u32 thread_index = uc->c_thread_index;
127   clib_spinlock_free (&uc->rx_lock);
128   if (CLIB_DEBUG)
129     clib_memset (uc, 0xFA, sizeof (*uc));
130   pool_put (udp_main.connections[thread_index], uc);
131 }
132
133 static void
134 udp_connection_cleanup (udp_connection_t * uc)
135 {
136   transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
137                               uc->c_lcl_port);
138   udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
139                                   uc->c_is_ip4);
140   udp_connection_free (uc);
141 }
142
143 void
144 udp_connection_delete (udp_connection_t * uc)
145 {
146   session_transport_delete_notify (&uc->connection);
147   udp_connection_cleanup (uc);
148 }
149
150 static u8
151 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
152 {
153   udp_main_t *um = vnet_get_udp_main ();
154   udp_dst_port_info_t *pi;
155
156   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
157   return (pi && !pi->n_connections
158           && udp_is_valid_dst_port (lcl_port, is_ip4));
159 }
160
161 static u16
162 udp_default_mtu (udp_main_t * um, u8 is_ip4)
163 {
164   u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
165   return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
166 }
167
168 static u32
169 udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
170 {
171   udp_main_t *um = vnet_get_udp_main ();
172   vlib_main_t *vm = vlib_get_main ();
173   transport_endpoint_cfg_t *lcl_ext;
174   udp_connection_t *listener;
175   u16 lcl_port_ho;
176   void *iface_ip;
177
178   lcl_port_ho = clib_net_to_host_u16 (lcl->port);
179
180   if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
181     {
182       clib_warning ("port already used");
183       return SESSION_E_PORTINUSE;
184     }
185
186   pool_get (um->listener_pool, listener);
187   clib_memset (listener, 0, sizeof (udp_connection_t));
188
189   listener->c_lcl_port = lcl->port;
190   listener->c_c_index = listener - um->listener_pool;
191
192   /* If we are provided a sw_if_index, bind using one of its ips */
193   if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
194     {
195       if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
196                                                  lcl->is_ip4)))
197         ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
198     }
199   ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
200   listener->c_is_ip4 = lcl->is_ip4;
201   listener->c_proto = TRANSPORT_PROTO_UDP;
202   listener->c_s_index = session_index;
203   listener->c_fib_index = lcl->fib_index;
204   listener->mss = udp_default_mtu (um, listener->c_is_ip4);
205   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
206   lcl_ext = (transport_endpoint_cfg_t *) lcl;
207   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
208     listener->flags |= UDP_CONN_F_CONNECTED;
209   else
210     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
211   clib_spinlock_init (&listener->rx_lock);
212
213   udp_connection_register_port (vm, lcl_port_ho, lcl->is_ip4);
214   return listener->c_c_index;
215 }
216
217 static u32
218 udp_session_unbind (u32 listener_index)
219 {
220   udp_main_t *um = &udp_main;
221   udp_connection_t *listener;
222
223   listener = udp_listener_get (listener_index);
224   udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
225                                   listener->c_is_ip4);
226   clib_spinlock_free (&listener->rx_lock);
227   pool_put (um->listener_pool, listener);
228   return 0;
229 }
230
231 static transport_connection_t *
232 udp_session_get_listener (u32 listener_index)
233 {
234   udp_connection_t *us;
235
236   us = udp_listener_get (listener_index);
237   return &us->connection;
238 }
239
240 static u32
241 udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
242 {
243   udp_connection_t *uc;
244   vlib_main_t *vm = vlib_get_main ();
245
246   uc = udp_connection_from_transport (tc);
247
248   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
249   if (tc->is_ip4)
250     vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
251                                  IP_PROTOCOL_UDP, 1 /* csum offload */ ,
252                                  0 /* is_df */ );
253   else
254     vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
255                           IP_PROTOCOL_UDP);
256   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
257   vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
258   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
259
260   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
261     {
262       if (!transport_max_tx_dequeue (&uc->connection))
263         udp_connection_delete (uc);
264     }
265
266   return 0;
267 }
268
269 static transport_connection_t *
270 udp_session_get (u32 connection_index, u32 thread_index)
271 {
272   udp_connection_t *uc;
273   uc = udp_connection_get (connection_index, thread_index);
274   if (uc)
275     return &uc->connection;
276   return 0;
277 }
278
279 static void
280 udp_session_close (u32 connection_index, u32 thread_index)
281 {
282   udp_connection_t *uc;
283
284   uc = udp_connection_get (connection_index, thread_index);
285   if (!uc)
286     return;
287
288   if (!transport_max_tx_dequeue (&uc->connection))
289     udp_connection_delete (uc);
290   else
291     uc->flags |= UDP_CONN_F_CLOSING;
292 }
293
294 static void
295 udp_session_cleanup (u32 connection_index, u32 thread_index)
296 {
297   udp_connection_t *uc;
298   uc = udp_connection_get (connection_index, thread_index);
299   if (!uc)
300     return;
301   if (uc->flags & UDP_CONN_F_MIGRATED)
302     udp_connection_free (uc);
303   else
304     udp_connection_cleanup (uc);
305 }
306
307 static int
308 udp_session_send_params (transport_connection_t * tconn,
309                          transport_send_params_t * sp)
310 {
311   udp_connection_t *uc;
312
313   uc = udp_connection_from_transport (tconn);
314
315   /* No constraint on TX window */
316   sp->snd_space = ~0;
317   /* TODO figure out MTU of output interface */
318   sp->snd_mss = uc->mss;
319   sp->tx_offset = 0;
320   sp->flags = 0;
321   return 0;
322 }
323
324 static int
325 udp_open_connection (transport_endpoint_cfg_t * rmt)
326 {
327   vlib_main_t *vm = vlib_get_main ();
328   u32 thread_index = vm->thread_index;
329   udp_main_t *um = &udp_main;
330   ip46_address_t lcl_addr;
331   udp_connection_t *uc;
332   u16 lcl_port;
333   int rv;
334
335   rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
336                                        &lcl_port);
337   if (rv)
338     {
339       if (rv != SESSION_E_PORTINUSE)
340         return rv;
341
342       if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
343         return SESSION_E_PORTINUSE;
344
345       /* If port in use, check if 5-tuple is also in use */
346       if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
347                                      lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
348                                      rmt->is_ip4))
349         return SESSION_E_PORTINUSE;
350
351       /* 5-tuple is available so increase lcl endpoint refcount and proceed
352        * with connection allocation */
353       transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
354                                       lcl_port);
355       goto conn_alloc;
356     }
357
358   if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
359     {
360       /* If specific source port was requested abort */
361       if (rmt->peer.port)
362         return SESSION_E_PORTINUSE;
363
364       /* Try to find a port that's not used */
365       while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
366         {
367           lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
368                                                  &lcl_addr);
369           if (lcl_port < 1)
370             return SESSION_E_PORTINUSE;
371         }
372     }
373
374 conn_alloc:
375
376   udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
377
378   /* We don't poll main thread if we have workers */
379   if (vlib_num_workers ())
380     thread_index = 1;
381
382   uc = udp_connection_alloc (thread_index);
383   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
384   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
385   uc->c_rmt_port = rmt->port;
386   uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
387   uc->c_is_ip4 = rmt->is_ip4;
388   uc->c_proto = TRANSPORT_PROTO_UDP;
389   uc->c_fib_index = rmt->fib_index;
390   uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
391   uc->flags |= UDP_CONN_F_OWNS_PORT;
392   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
393     uc->flags |= UDP_CONN_F_CONNECTED;
394   else
395     uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
396
397   return uc->c_c_index;
398 }
399
400 static transport_connection_t *
401 udp_session_get_half_open (u32 conn_index)
402 {
403   udp_connection_t *uc;
404   u32 thread_index;
405
406   /* We don't poll main thread if we have workers */
407   thread_index = vlib_num_workers ()? 1 : 0;
408   uc = udp_connection_get (conn_index, thread_index);
409   if (!uc)
410     return 0;
411   return &uc->connection;
412 }
413
414 static u8 *
415 format_udp_session (u8 * s, va_list * args)
416 {
417   u32 uci = va_arg (*args, u32);
418   u32 thread_index = va_arg (*args, u32);
419   u32 verbose = va_arg (*args, u32);
420   udp_connection_t *uc;
421
422   uc = udp_connection_get (uci, thread_index);
423   return format (s, "%U", format_udp_connection, uc, verbose);
424 }
425
426 static u8 *
427 format_udp_half_open_session (u8 * s, va_list * args)
428 {
429   u32 __clib_unused tci = va_arg (*args, u32);
430   u32 __clib_unused thread_index = va_arg (*args, u32);
431   clib_warning ("BUG");
432   return 0;
433 }
434
435 static u8 *
436 format_udp_listener_session (u8 * s, va_list * args)
437 {
438   u32 tci = va_arg (*args, u32);
439   u32 __clib_unused thread_index = va_arg (*args, u32);
440   u32 verbose = va_arg (*args, u32);
441   udp_connection_t *uc = udp_listener_get (tci);
442   return format (s, "%U", format_udp_connection, uc, verbose);
443 }
444
445 /* *INDENT-OFF* */
446 static const transport_proto_vft_t udp_proto = {
447   .start_listen = udp_session_bind,
448   .connect = udp_open_connection,
449   .stop_listen = udp_session_unbind,
450   .push_header = udp_push_header,
451   .get_connection = udp_session_get,
452   .get_listener = udp_session_get_listener,
453   .get_half_open = udp_session_get_half_open,
454   .close = udp_session_close,
455   .cleanup = udp_session_cleanup,
456   .send_params = udp_session_send_params,
457   .format_connection = format_udp_session,
458   .format_half_open = format_udp_half_open_session,
459   .format_listener = format_udp_listener_session,
460   .transport_options = {
461     .name = "udp",
462     .short_name = "U",
463     .tx_type = TRANSPORT_TX_DGRAM,
464     .service_type = TRANSPORT_SERVICE_CL,
465   },
466 };
467 /* *INDENT-ON* */
468
469 static clib_error_t *
470 udp_init (vlib_main_t * vm)
471 {
472   udp_main_t *um = vnet_get_udp_main ();
473   ip_main_t *im = &ip_main;
474   vlib_thread_main_t *tm = vlib_get_thread_main ();
475   u32 num_threads;
476   ip_protocol_info_t *pi;
477   int i;
478
479   /*
480    * Registrations
481    */
482
483   /* IP registration */
484   pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
485   if (pi == 0)
486     return clib_error_return (0, "UDP protocol info AWOL");
487   pi->format_header = format_udp_header;
488   pi->unformat_pg_edit = unformat_pg_udp_header;
489
490   /* Register as transport with URI */
491   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
492                                FIB_PROTOCOL_IP4, ip4_lookup_node.index);
493   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
494                                FIB_PROTOCOL_IP6, ip6_lookup_node.index);
495
496   /*
497    * Initialize data structures
498    */
499
500   num_threads = 1 /* main thread */  + tm->n_threads;
501   vec_validate (um->connections, num_threads - 1);
502   vec_validate (um->connection_peekers, num_threads - 1);
503   vec_validate (um->peekers_readers_locks, num_threads - 1);
504   vec_validate (um->peekers_write_locks, num_threads - 1);
505
506   if (num_threads > 1)
507     for (i = 0; i < num_threads; i++)
508       {
509         clib_spinlock_init (&um->peekers_readers_locks[i]);
510         clib_spinlock_init (&um->peekers_write_locks[i]);
511       }
512
513   um->local_to_input_edge[UDP_IP4] =
514     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
515   um->local_to_input_edge[UDP_IP6] =
516     vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
517
518   um->default_mtu = 1500;
519   return 0;
520 }
521
522 /* *INDENT-OFF* */
523 VLIB_INIT_FUNCTION (udp_init) =
524 {
525   .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
526                            "ip6_lookup_init"),
527 };
528 /* *INDENT-ON* */
529
530 /*
531  * fd.io coding-style-patch-verification: ON
532  *
533  * Local Variables:
534  * eval: (c-set-style "gnu")
535  * End:
536  */