session: transport endpt cleanup on owner thread
[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 (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_worker_t *wrk = udp_worker_get (thread_index);
96   udp_connection_t *uc;
97
98   pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES);
99
100   clib_memset (uc, 0, sizeof (*uc));
101   uc->c_c_index = uc - wrk->connections;
102   uc->c_thread_index = thread_index;
103   uc->c_proto = TRANSPORT_PROTO_UDP;
104   return uc;
105 }
106
107 void
108 udp_connection_free (udp_connection_t * uc)
109 {
110   udp_worker_t *wrk = udp_worker_get (uc->c_thread_index);
111
112   clib_spinlock_free (&uc->rx_lock);
113   if (CLIB_DEBUG)
114     clib_memset (uc, 0xFA, sizeof (*uc));
115   pool_put (wrk->connections, uc);
116 }
117
118 static void
119 udp_connection_cleanup (udp_connection_t * uc)
120 {
121   transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
122                                     uc->c_lcl_port);
123   udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
124                                   uc->c_is_ip4);
125   udp_connection_free (uc);
126 }
127
128 void
129 udp_connection_delete (udp_connection_t * uc)
130 {
131   session_transport_delete_notify (&uc->connection);
132   udp_connection_cleanup (uc);
133 }
134
135 static void
136 udp_handle_cleanups (void *args)
137 {
138   u32 thread_index = (u32) pointer_to_uword (args);
139   udp_connection_t *uc;
140   udp_worker_t *wrk;
141   u32 *uc_index;
142
143   wrk = udp_worker_get (thread_index);
144   vec_foreach (uc_index, wrk->pending_cleanups)
145     {
146       uc = udp_connection_get (*uc_index, thread_index);
147       udp_connection_delete (uc);
148     }
149   vec_reset_length (wrk->pending_cleanups);
150 }
151
152 static void
153 udp_connection_program_cleanup (udp_connection_t *uc)
154 {
155   uword thread_index = uc->c_thread_index;
156   udp_worker_t *wrk;
157
158   wrk = udp_worker_get (uc->c_thread_index);
159   vec_add1 (wrk->pending_cleanups, uc->c_c_index);
160
161   if (vec_len (wrk->pending_cleanups) == 1)
162     session_send_rpc_evt_to_thread_force (
163       thread_index, udp_handle_cleanups,
164       uword_to_pointer (thread_index, void *));
165 }
166
167 static u8
168 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
169 {
170   udp_main_t *um = vnet_get_udp_main ();
171   udp_dst_port_info_t *pi;
172
173   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
174   return (pi && !pi->n_connections
175           && udp_is_valid_dst_port (lcl_port, is_ip4));
176 }
177
178 static u16
179 udp_default_mtu (udp_main_t * um, u8 is_ip4)
180 {
181   u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
182   return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
183 }
184
185 static u32
186 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
187 {
188   udp_main_t *um = vnet_get_udp_main ();
189   transport_endpoint_cfg_t *lcl_ext;
190   udp_connection_t *listener;
191   u16 lcl_port_ho;
192   void *iface_ip;
193
194   lcl_port_ho = clib_net_to_host_u16 (lcl->port);
195
196   if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
197     {
198       clib_warning ("port already used");
199       return SESSION_E_PORTINUSE;
200     }
201
202   pool_get (um->listener_pool, listener);
203   clib_memset (listener, 0, sizeof (udp_connection_t));
204
205   listener->c_lcl_port = lcl->port;
206   listener->c_c_index = listener - um->listener_pool;
207
208   /* If we are provided a sw_if_index, bind using one of its ips */
209   if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
210     {
211       if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
212                                                  lcl->is_ip4)))
213         ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
214     }
215   ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
216   listener->c_is_ip4 = lcl->is_ip4;
217   listener->c_proto = TRANSPORT_PROTO_UDP;
218   listener->c_s_index = session_index;
219   listener->c_fib_index = lcl->fib_index;
220   listener->mss =
221     lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
222   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
223   lcl_ext = (transport_endpoint_cfg_t *) lcl;
224   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
225     listener->flags |= UDP_CONN_F_CONNECTED;
226   else
227     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
228   clib_spinlock_init (&listener->rx_lock);
229   if (!um->csum_offload)
230     listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
231
232   udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
233   return listener->c_c_index;
234 }
235
236 static u32
237 udp_session_unbind (u32 listener_index)
238 {
239   udp_main_t *um = &udp_main;
240   udp_connection_t *listener;
241
242   listener = udp_listener_get (listener_index);
243   udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
244                                   listener->c_is_ip4);
245   clib_spinlock_free (&listener->rx_lock);
246   pool_put (um->listener_pool, listener);
247   return 0;
248 }
249
250 static transport_connection_t *
251 udp_session_get_listener (u32 listener_index)
252 {
253   udp_connection_t *us;
254
255   us = udp_listener_get (listener_index);
256   return &us->connection;
257 }
258
259 always_inline u32
260 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
261 {
262   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
263                         udp_csum_offload (uc));
264   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
265   /* reuse tcp medatada for now */
266   vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
267
268   return 0;
269 }
270
271 static u32
272 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
273 {
274   vlib_main_t *vm = vlib_get_main ();
275   udp_connection_t *uc;
276
277   uc = udp_connection_from_transport (tc);
278
279   while (n_bufs >= 4)
280     {
281       vlib_prefetch_buffer_header (bs[2], STORE);
282       vlib_prefetch_buffer_header (bs[3], STORE);
283
284       udp_push_one_header (vm, uc, bs[0]);
285       udp_push_one_header (vm, uc, bs[1]);
286
287       n_bufs -= 2;
288       bs += 2;
289     }
290   while (n_bufs)
291     {
292       if (n_bufs > 1)
293         vlib_prefetch_buffer_header (bs[1], STORE);
294
295       udp_push_one_header (vm, uc, bs[0]);
296
297       n_bufs -= 1;
298       bs += 1;
299     }
300
301   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
302     {
303       if (!transport_max_tx_dequeue (&uc->connection))
304         udp_connection_program_cleanup (uc);
305     }
306
307   return 0;
308 }
309
310 static transport_connection_t *
311 udp_session_get (u32 connection_index, u32 thread_index)
312 {
313   udp_connection_t *uc;
314   uc = udp_connection_get (connection_index, thread_index);
315   if (uc)
316     return &uc->connection;
317   return 0;
318 }
319
320 static void
321 udp_session_close (u32 connection_index, u32 thread_index)
322 {
323   udp_connection_t *uc;
324
325   uc = udp_connection_get (connection_index, thread_index);
326   if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
327     return;
328
329   if (!transport_max_tx_dequeue (&uc->connection))
330     udp_connection_program_cleanup (uc);
331   else
332     uc->flags |= UDP_CONN_F_CLOSING;
333 }
334
335 static void
336 udp_session_cleanup (u32 connection_index, u32 thread_index)
337 {
338   udp_connection_t *uc;
339   uc = udp_connection_get (connection_index, thread_index);
340   if (!uc)
341     return;
342   if (uc->flags & UDP_CONN_F_MIGRATED)
343     udp_connection_free (uc);
344   else
345     udp_connection_cleanup (uc);
346 }
347
348 static int
349 udp_session_send_params (transport_connection_t * tconn,
350                          transport_send_params_t * sp)
351 {
352   udp_connection_t *uc;
353
354   uc = udp_connection_from_transport (tconn);
355
356   /* No constraint on TX window */
357   sp->snd_space = ~0;
358   /* TODO figure out MTU of output interface */
359   sp->snd_mss = uc->mss;
360   sp->tx_offset = 0;
361   sp->flags = 0;
362   return 0;
363 }
364
365 static int
366 udp_open_connection (transport_endpoint_cfg_t * rmt)
367 {
368   udp_main_t *um = &udp_main;
369   ip46_address_t lcl_addr;
370   udp_connection_t *uc;
371   u32 thread_index;
372   u16 lcl_port;
373   int rv;
374
375   rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
376                                        &lcl_port);
377   if (rv)
378     {
379       if (rv != SESSION_E_PORTINUSE)
380         return rv;
381
382       if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
383         return SESSION_E_PORTINUSE;
384
385       /* If port in use, check if 5-tuple is also in use */
386       if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
387                                      lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
388                                      rmt->is_ip4))
389         return SESSION_E_PORTINUSE;
390
391       /* 5-tuple is available so increase lcl endpoint refcount and proceed
392        * with connection allocation */
393       transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
394                                       lcl_port);
395       goto conn_alloc;
396     }
397
398   if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
399     {
400       /* If specific source port was requested abort */
401       if (rmt->peer.port)
402         return SESSION_E_PORTINUSE;
403
404       /* Try to find a port that's not used */
405       while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
406         {
407           lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
408                                                  &lcl_addr);
409           if (lcl_port < 1)
410             return SESSION_E_PORTINUSE;
411         }
412     }
413
414 conn_alloc:
415
416   udp_connection_register_port (lcl_port, rmt->is_ip4);
417
418   /* We don't poll main thread if we have workers */
419   thread_index = transport_cl_thread ();
420
421   uc = udp_connection_alloc (thread_index);
422   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
423   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
424   uc->c_rmt_port = rmt->port;
425   uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
426   uc->c_is_ip4 = rmt->is_ip4;
427   uc->c_proto = TRANSPORT_PROTO_UDP;
428   uc->c_fib_index = rmt->fib_index;
429   uc->c_dscp = rmt->dscp;
430   uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
431   if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
432     uc->sw_if_index = rmt->peer.sw_if_index;
433   uc->flags |= UDP_CONN_F_OWNS_PORT;
434   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
435     {
436       uc->flags |= UDP_CONN_F_CONNECTED;
437     }
438   else
439     {
440       clib_spinlock_init (&uc->rx_lock);
441       uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
442     }
443   if (!um->csum_offload)
444     uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
445   uc->next_node_index = rmt->next_node_index;
446   uc->next_node_opaque = rmt->next_node_opaque;
447
448   return uc->c_c_index;
449 }
450
451 static transport_connection_t *
452 udp_session_get_half_open (u32 conn_index)
453 {
454   udp_connection_t *uc;
455   u32 thread_index;
456
457   /* We don't poll main thread if we have workers */
458   thread_index = transport_cl_thread ();
459   uc = udp_connection_get (conn_index, thread_index);
460   if (!uc)
461     return 0;
462   return &uc->connection;
463 }
464
465 static u8 *
466 format_udp_session (u8 * s, va_list * args)
467 {
468   u32 uci = va_arg (*args, u32);
469   u32 thread_index = va_arg (*args, u32);
470   u32 verbose = va_arg (*args, u32);
471   udp_connection_t *uc;
472
473   uc = udp_connection_get (uci, thread_index);
474   return format (s, "%U", format_udp_connection, uc, verbose);
475 }
476
477 static u8 *
478 format_udp_half_open_session (u8 * s, va_list * args)
479 {
480   u32 __clib_unused tci = va_arg (*args, u32);
481   u32 __clib_unused thread_index = va_arg (*args, u32);
482   clib_warning ("BUG");
483   return 0;
484 }
485
486 static u8 *
487 format_udp_listener_session (u8 * s, va_list * args)
488 {
489   u32 tci = va_arg (*args, u32);
490   u32 __clib_unused thread_index = va_arg (*args, u32);
491   u32 verbose = va_arg (*args, u32);
492   udp_connection_t *uc = udp_listener_get (tci);
493   return format (s, "%U", format_udp_connection, uc, verbose);
494 }
495
496 static void
497 udp_realloc_ports_sv (u16 **ports_nh_svp)
498 {
499   u16 port, port_no, *ports_nh_sv, *mc;
500   u32 *ports = 0, *nh = 0, msum, i;
501   sparse_vec_header_t *h;
502   uword sv_index, *mb;
503
504   ports_nh_sv = *ports_nh_svp;
505
506   for (port = 1; port < 65535; port++)
507     {
508       port_no = clib_host_to_net_u16 (port);
509
510       sv_index = sparse_vec_index (ports_nh_sv, port_no);
511       if (sv_index != SPARSE_VEC_INVALID_INDEX)
512         {
513           vec_add1 (ports, port_no);
514           vec_add1 (nh, ports_nh_sv[sv_index]);
515         }
516     }
517
518   sparse_vec_free (ports_nh_sv);
519
520   ports_nh_sv =
521     sparse_vec_new (/* elt bytes */ sizeof (ports_nh_sv[0]),
522                     /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
523
524   vec_resize (ports_nh_sv, 65535);
525
526   for (port = 1; port < 65535; port++)
527     ports_nh_sv[port] = ~0;
528
529   for (i = 0; i < vec_len (ports); i++)
530     ports_nh_sv[ports[i]] = nh[i];
531
532   h = sparse_vec_header (ports_nh_sv);
533   vec_foreach (mb, h->is_member_bitmap)
534     *mb = (uword) ~0;
535
536   msum = 0;
537   vec_foreach (mc, h->member_counts)
538     {
539       *mc = msum;
540       msum += msum == 0 ? 63 : 64;
541     }
542
543   vec_free (ports);
544   vec_free (nh);
545
546   *ports_nh_svp = ports_nh_sv;
547 }
548
549 static clib_error_t *
550 udp_enable_disable (vlib_main_t *vm, u8 is_en)
551 {
552   udp_main_t *um = &udp_main;
553
554   /* Not ideal. The sparse vector used to map ports to next nodes assumes
555    * only a few ports are ever used. When udp transport is enabled this does
556    * not hold and, to make matters worse, ports are consumed in a random
557    * order.
558    *
559    * This can lead to a lot of slow updates to internal data structures
560    * which in turn can slow udp connection allocations until all ports are
561    * eventually consumed.
562    *
563    * Consequently, reallocate sparse vector, preallocate all ports and have
564    * them point to UDP_NO_NODE_SET. We could consider switching the sparse
565    * vector to a preallocated vector but that would increase memory
566    * consumption for vpp deployments that do not rely on host stack.
567    */
568
569   udp_realloc_ports_sv (&um->next_by_dst_port4);
570   udp_realloc_ports_sv (&um->next_by_dst_port6);
571
572   return 0;
573 }
574
575 static const transport_proto_vft_t udp_proto = {
576   .enable = udp_enable_disable,
577   .start_listen = udp_session_bind,
578   .connect = udp_open_connection,
579   .stop_listen = udp_session_unbind,
580   .push_header = udp_push_header,
581   .get_connection = udp_session_get,
582   .get_listener = udp_session_get_listener,
583   .get_half_open = udp_session_get_half_open,
584   .close = udp_session_close,
585   .cleanup = udp_session_cleanup,
586   .send_params = udp_session_send_params,
587   .format_connection = format_udp_session,
588   .format_half_open = format_udp_half_open_session,
589   .format_listener = format_udp_listener_session,
590   .transport_options = {
591     .name = "udp",
592     .short_name = "U",
593     .tx_type = TRANSPORT_TX_DGRAM,
594     .service_type = TRANSPORT_SERVICE_CL,
595   },
596 };
597
598 static clib_error_t *
599 udp_init (vlib_main_t * vm)
600 {
601   udp_main_t *um = vnet_get_udp_main ();
602   ip_main_t *im = &ip_main;
603   vlib_thread_main_t *tm = vlib_get_thread_main ();
604   u32 num_threads;
605   ip_protocol_info_t *pi;
606
607   /*
608    * Registrations
609    */
610
611   /* IP registration */
612   pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
613   if (pi == 0)
614     return clib_error_return (0, "UDP protocol info AWOL");
615   pi->format_header = format_udp_header;
616   pi->unformat_pg_edit = unformat_pg_udp_header;
617
618   /* Register as transport with session layer */
619   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
620                                FIB_PROTOCOL_IP4, udp4_output_node.index);
621   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
622                                FIB_PROTOCOL_IP6, udp6_output_node.index);
623
624   /*
625    * Initialize data structures
626    */
627
628   num_threads = 1 /* main thread */  + tm->n_threads;
629   vec_validate (um->wrk, num_threads - 1);
630
631   um->local_to_input_edge[UDP_IP4] =
632     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
633   um->local_to_input_edge[UDP_IP6] =
634     vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
635
636   um->default_mtu = 1500;
637   um->csum_offload = 1;
638   return 0;
639 }
640
641 /* *INDENT-OFF* */
642 VLIB_INIT_FUNCTION (udp_init) =
643 {
644   .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
645                            "ip6_lookup_init"),
646 };
647 /* *INDENT-ON* */
648
649 /*
650  * fd.io coding-style-patch-verification: ON
651  *
652  * Local Variables:
653  * eval: (c-set-style "gnu")
654  * End:
655  */