udp: track connection port sharing
[vpp.git] / src / vnet / udp / udp.c
1 /*
2  * Copyright (c) 2016-2019 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 /** @file
17     udp state machine, etc.
18 */
19
20 #include <vnet/udp/udp.h>
21 #include <vnet/session/session.h>
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/sparse_vec.h>
25
26 udp_main_t udp_main;
27
28 static void
29 udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
30 {
31   udp_main_t *um = &udp_main;
32   udp_dst_port_info_t *pi;
33   u16 *n;
34
35   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
36   if (!pi)
37     {
38       udp_add_dst_port (um, lcl_port, 0, is_ip4);
39       pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
40       pi->n_connections = 1;
41     }
42   else
43     {
44       pi->n_connections += 1;
45       /* Do not return. The fact that the pi is valid does not mean
46        * it's up to date */
47     }
48
49   pi->node_index = is_ip4 ? udp4_input_node.index : udp6_input_node.index;
50   pi->next_index = um->local_to_input_edge[is_ip4];
51
52   /* Setup udp protocol -> next index sparse vector mapping. */
53   if (is_ip4)
54     n = sparse_vec_validate (um->next_by_dst_port4,
55                              clib_host_to_net_u16 (lcl_port));
56   else
57     n = sparse_vec_validate (um->next_by_dst_port6,
58                              clib_host_to_net_u16 (lcl_port));
59
60   n[0] = pi->next_index;
61 }
62
63 static void
64 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
65 {
66   udp_main_t *um = &udp_main;
67   udp_dst_port_info_t *pi;
68
69   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
70   if (!pi)
71     return;
72
73   if (!pi->n_connections)
74     {
75       clib_warning ("no connections using port %u", lcl_port);
76       return;
77     }
78
79   if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
80     udp_unregister_dst_port (0, lcl_port, is_ip4);
81 }
82
83 void
84 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
85 {
86   udp_main_t *um = &udp_main;
87   udp_dst_port_info_t *pi;
88
89   /* Done without a lock but the operation is atomic. Writers to pi hash
90    * table and vector should be guarded by a barrier sync */
91   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
92   clib_atomic_fetch_add_rel (&pi->n_connections, 1);
93 }
94
95 udp_connection_t *
96 udp_connection_alloc (u32 thread_index)
97 {
98   udp_main_t *um = &udp_main;
99   udp_connection_t *uc;
100   u32 will_expand = 0;
101   pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
102                                 CLIB_CACHE_LINE_BYTES);
103
104   if (PREDICT_FALSE (will_expand))
105     {
106       clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
107                                   [thread_index]);
108       pool_get_aligned (udp_main.connections[thread_index], uc,
109                         CLIB_CACHE_LINE_BYTES);
110       clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
111                                     [thread_index]);
112     }
113   else
114     {
115       pool_get_aligned (um->connections[thread_index], uc,
116                         CLIB_CACHE_LINE_BYTES);
117     }
118   clib_memset (uc, 0, sizeof (*uc));
119   uc->c_c_index = uc - um->connections[thread_index];
120   uc->c_thread_index = thread_index;
121   uc->c_proto = TRANSPORT_PROTO_UDP;
122   clib_spinlock_init (&uc->rx_lock);
123   return uc;
124 }
125
126 void
127 udp_connection_free (udp_connection_t * uc)
128 {
129   u32 thread_index = uc->c_thread_index;
130   if (CLIB_DEBUG)
131     clib_memset (uc, 0xFA, sizeof (*uc));
132   pool_put (udp_main.connections[thread_index], uc);
133 }
134
135 void
136 udp_connection_delete (udp_connection_t * uc)
137 {
138   udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
139                                   uc->c_is_ip4);
140   session_transport_delete_notify (&uc->connection);
141   udp_connection_free (uc);
142 }
143
144 u32
145 udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
146 {
147   udp_main_t *um = vnet_get_udp_main ();
148   vlib_main_t *vm = vlib_get_main ();
149   transport_endpoint_cfg_t *lcl_ext;
150   udp_connection_t *listener;
151   udp_dst_port_info_t *pi;
152   void *iface_ip;
153
154   pi = udp_get_dst_port_info (um, clib_net_to_host_u16 (lcl->port),
155                               lcl->is_ip4);
156
157   if (pi && !pi->n_connections)
158     {
159       clib_warning ("port already used");
160       return -1;
161     }
162
163   pool_get (um->listener_pool, listener);
164   clib_memset (listener, 0, sizeof (udp_connection_t));
165
166   listener->c_lcl_port = lcl->port;
167   listener->c_c_index = listener - um->listener_pool;
168
169   /* If we are provided a sw_if_index, bind using one of its ips */
170   if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
171     {
172       if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
173                                                  lcl->is_ip4)))
174         ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
175     }
176   ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
177   listener->c_is_ip4 = lcl->is_ip4;
178   listener->c_proto = TRANSPORT_PROTO_UDP;
179   listener->c_s_index = session_index;
180   listener->c_fib_index = lcl->fib_index;
181   listener->flags |= UDP_CONN_F_OWNS_PORT;
182   lcl_ext = (transport_endpoint_cfg_t *) lcl;
183   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
184     listener->flags |= UDP_CONN_F_CONNECTED;
185   else
186     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
187   clib_spinlock_init (&listener->rx_lock);
188
189   udp_connection_register_port (vm, clib_net_to_host_u16 (lcl->port),
190                                 lcl->is_ip4);
191   return listener->c_c_index;
192 }
193
194 u32
195 udp_session_unbind (u32 listener_index)
196 {
197   udp_main_t *um = &udp_main;
198   udp_connection_t *listener;
199
200   listener = udp_listener_get (listener_index);
201   udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
202                                   listener->c_is_ip4);
203   pool_put (um->listener_pool, listener);
204   return 0;
205 }
206
207 transport_connection_t *
208 udp_session_get_listener (u32 listener_index)
209 {
210   udp_connection_t *us;
211
212   us = udp_listener_get (listener_index);
213   return &us->connection;
214 }
215
216 u32
217 udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
218 {
219   udp_connection_t *uc;
220   vlib_main_t *vm = vlib_get_main ();
221
222   uc = udp_get_connection_from_transport (tc);
223
224   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
225   if (tc->is_ip4)
226     vlib_buffer_push_ip4 (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
227                           IP_PROTOCOL_UDP, 1);
228   else
229     {
230       ip6_header_t *ih;
231       ih = vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
232                                  IP_PROTOCOL_UDP);
233       vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data;
234     }
235   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
236   vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
237   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
238
239   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
240     {
241       if (!transport_max_tx_dequeue (&uc->connection))
242         udp_connection_delete (uc);
243     }
244
245   return 0;
246 }
247
248 transport_connection_t *
249 udp_session_get (u32 connection_index, u32 thread_index)
250 {
251   udp_connection_t *uc;
252   uc = udp_connection_get (connection_index, thread_index);
253   if (uc)
254     return &uc->connection;
255   return 0;
256 }
257
258 void
259 udp_session_close (u32 connection_index, u32 thread_index)
260 {
261   udp_connection_t *uc;
262
263   uc = udp_connection_get (connection_index, thread_index);
264   if (!uc)
265     return;
266
267   if (!transport_max_tx_dequeue (&uc->connection))
268     udp_connection_delete (uc);
269   else
270     uc->flags |= UDP_CONN_F_CLOSING;
271 }
272
273 void
274 udp_session_cleanup (u32 connection_index, u32 thread_index)
275 {
276   udp_connection_t *uc;
277   uc = udp_connection_get (connection_index, thread_index);
278   if (uc)
279     udp_connection_free (uc);
280 }
281
282 u8 *
283 format_udp_connection_id (u8 * s, va_list * args)
284 {
285   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
286   if (!uc)
287     return s;
288   if (uc->c_is_ip4)
289     s = format (s, "[#%d][%s] %U:%d->%U:%d", uc->c_thread_index, "U",
290                 format_ip4_address, &uc->c_lcl_ip4,
291                 clib_net_to_host_u16 (uc->c_lcl_port), format_ip4_address,
292                 &uc->c_rmt_ip4, clib_net_to_host_u16 (uc->c_rmt_port));
293   else
294     s = format (s, "[#%d][%s] %U:%d->%U:%d", uc->c_thread_index, "U",
295                 format_ip6_address, &uc->c_lcl_ip6,
296                 clib_net_to_host_u16 (uc->c_lcl_port), format_ip6_address,
297                 &uc->c_rmt_ip6, clib_net_to_host_u16 (uc->c_rmt_port));
298   return s;
299 }
300
301 u8 *
302 format_udp_connection (u8 * s, va_list * args)
303 {
304   udp_connection_t *uc = va_arg (*args, udp_connection_t *);
305   u32 verbose = va_arg (*args, u32);
306   if (!uc)
307     return s;
308   s = format (s, "%-50U", format_udp_connection_id, uc);
309   if (verbose)
310     {
311       if (verbose == 1)
312         s = format (s, "%-15s", "-");
313       else
314         s = format (s, "\n");
315     }
316   return s;
317 }
318
319 u8 *
320 format_udp_session (u8 * s, va_list * args)
321 {
322   u32 uci = va_arg (*args, u32);
323   u32 thread_index = va_arg (*args, u32);
324   u32 verbose = va_arg (*args, u32);
325   udp_connection_t *uc;
326
327   uc = udp_connection_get (uci, thread_index);
328   return format (s, "%U", format_udp_connection, uc, verbose);
329 }
330
331 u8 *
332 format_udp_half_open_session (u8 * s, va_list * args)
333 {
334   u32 __clib_unused tci = va_arg (*args, u32);
335   u32 __clib_unused thread_index = va_arg (*args, u32);
336   clib_warning ("BUG");
337   return 0;
338 }
339
340 u8 *
341 format_udp_listener_session (u8 * s, va_list * args)
342 {
343   u32 tci = va_arg (*args, u32);
344   u32 __clib_unused thread_index = va_arg (*args, u32);
345   u32 verbose = va_arg (*args, u32);
346   udp_connection_t *uc = udp_listener_get (tci);
347   return format (s, "%U", format_udp_connection, uc, verbose);
348 }
349
350 static int
351 udp_session_send_params (transport_connection_t * tconn,
352                          transport_send_params_t * sp)
353 {
354   /* No constraint on TX window */
355   sp->snd_space = ~0;
356   /* TODO figure out MTU of output interface */
357   sp->snd_mss = 1460;
358   sp->tx_offset = 0;
359   sp->flags = 0;
360   return 0;
361 }
362
363 int
364 udp_open_connection (transport_endpoint_cfg_t * rmt)
365 {
366   vlib_main_t *vm = vlib_get_main ();
367   u32 thread_index = vm->thread_index;
368   udp_connection_t *uc;
369   ip46_address_t lcl_addr;
370   u16 lcl_port;
371
372   if (transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
373                                       &lcl_port))
374     return -1;
375
376   if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
377     {
378       /* If specific source port was requested abort */
379       if (rmt->peer.port)
380         return -1;
381
382       /* Try to find a port that's not used */
383       while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
384         {
385           lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
386                                                  &lcl_addr);
387           if (lcl_port < 1)
388             {
389               clib_warning ("Failed to allocate src port");
390               return -1;
391             }
392         }
393     }
394
395   udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
396
397   /* We don't poll main thread if we have workers */
398   if (vlib_num_workers ())
399     thread_index = 1;
400
401   uc = udp_connection_alloc (thread_index);
402   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
403   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
404   uc->c_rmt_port = rmt->port;
405   uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
406   uc->c_is_ip4 = rmt->is_ip4;
407   uc->c_proto = TRANSPORT_PROTO_UDP;
408   uc->c_fib_index = rmt->fib_index;
409   uc->flags |= UDP_CONN_F_OWNS_PORT;
410   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
411     uc->flags |= UDP_CONN_F_CONNECTED;
412   else
413     uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
414
415   return uc->c_c_index;
416 }
417
418 transport_connection_t *
419 udp_session_get_half_open (u32 conn_index)
420 {
421   udp_connection_t *uc;
422   u32 thread_index;
423
424   /* We don't poll main thread if we have workers */
425   thread_index = vlib_num_workers ()? 1 : 0;
426   uc = udp_connection_get (conn_index, thread_index);
427   if (!uc)
428     return 0;
429   return &uc->connection;
430 }
431
432 /* *INDENT-OFF* */
433 static const transport_proto_vft_t udp_proto = {
434   .start_listen = udp_session_bind,
435   .connect = udp_open_connection,
436   .stop_listen = udp_session_unbind,
437   .push_header = udp_push_header,
438   .get_connection = udp_session_get,
439   .get_listener = udp_session_get_listener,
440   .get_half_open = udp_session_get_half_open,
441   .close = udp_session_close,
442   .cleanup = udp_session_cleanup,
443   .send_params = udp_session_send_params,
444   .format_connection = format_udp_session,
445   .format_half_open = format_udp_half_open_session,
446   .format_listener = format_udp_listener_session,
447   .transport_options = {
448     .name = "udp",
449     .short_name = "U",
450     .tx_type = TRANSPORT_TX_DGRAM,
451     .service_type = TRANSPORT_SERVICE_CL,
452   },
453 };
454 /* *INDENT-ON* */
455
456
457 int
458 udpc_connection_open (transport_endpoint_cfg_t * rmt)
459 {
460   udp_connection_t *uc;
461   /* Reproduce the logic of udp_open_connection to find the correct thread */
462   u32 thread_index = vlib_num_workers ()? 1 : vlib_get_main ()->thread_index;
463   u32 uc_index;
464   uc_index = udp_open_connection (rmt);
465   if (uc_index == (u32) ~ 0)
466     return -1;
467   uc = udp_connection_get (uc_index, thread_index);
468   uc->flags |= UDP_CONN_F_CONNECTED;
469   return uc_index;
470 }
471
472 u32
473 udpc_connection_listen (u32 session_index, transport_endpoint_t * lcl)
474 {
475   udp_connection_t *listener;
476   u32 li_index;
477   li_index = udp_session_bind (session_index, lcl);
478   if (li_index == (u32) ~ 0)
479     return -1;
480   listener = udp_listener_get (li_index);
481   listener->flags |= UDP_CONN_F_CONNECTED;
482   /* Fake udp listener, i.e., make sure session layer adds a udp instead of
483    * udpc listener to the lookup table */
484   ((session_endpoint_cfg_t *) lcl)->transport_proto = TRANSPORT_PROTO_UDP;
485   return li_index;
486 }
487
488 /* *INDENT-OFF* */
489 static const transport_proto_vft_t udpc_proto = {
490   .start_listen = udpc_connection_listen,
491   .stop_listen = udp_session_unbind,
492   .connect = udpc_connection_open,
493   .push_header = udp_push_header,
494   .get_connection = udp_session_get,
495   .get_listener = udp_session_get_listener,
496   .get_half_open = udp_session_get_half_open,
497   .close = udp_session_close,
498   .cleanup = udp_session_cleanup,
499   .send_params = udp_session_send_params,
500   .format_connection = format_udp_session,
501   .format_half_open = format_udp_half_open_session,
502   .format_listener = format_udp_listener_session,
503   .transport_options = {
504     .name = "udpc",
505     .short_name = "U",
506     .tx_type = TRANSPORT_TX_DGRAM,
507     .service_type = TRANSPORT_SERVICE_VC,
508     .half_open_has_fifos = 1
509   },
510 };
511 /* *INDENT-ON* */
512
513 static clib_error_t *
514 udp_init (vlib_main_t * vm)
515 {
516   udp_main_t *um = vnet_get_udp_main ();
517   ip_main_t *im = &ip_main;
518   vlib_thread_main_t *tm = vlib_get_thread_main ();
519   u32 num_threads;
520   ip_protocol_info_t *pi;
521   int i;
522
523   /*
524    * Registrations
525    */
526
527   /* IP registration */
528   pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
529   if (pi == 0)
530     return clib_error_return (0, "UDP protocol info AWOL");
531   pi->format_header = format_udp_header;
532   pi->unformat_pg_edit = unformat_pg_udp_header;
533
534   /* Register as transport with URI */
535   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
536                                FIB_PROTOCOL_IP4, ip4_lookup_node.index);
537   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
538                                FIB_PROTOCOL_IP6, ip6_lookup_node.index);
539   transport_register_protocol (TRANSPORT_PROTO_UDPC, &udpc_proto,
540                                FIB_PROTOCOL_IP4, ip4_lookup_node.index);
541   transport_register_protocol (TRANSPORT_PROTO_UDPC, &udpc_proto,
542                                FIB_PROTOCOL_IP6, ip6_lookup_node.index);
543
544   /*
545    * Initialize data structures
546    */
547
548   num_threads = 1 /* main thread */  + tm->n_threads;
549   vec_validate (um->connections, num_threads - 1);
550   vec_validate (um->connection_peekers, num_threads - 1);
551   vec_validate (um->peekers_readers_locks, num_threads - 1);
552   vec_validate (um->peekers_write_locks, num_threads - 1);
553
554   if (num_threads > 1)
555     for (i = 0; i < num_threads; i++)
556       {
557         clib_spinlock_init (&um->peekers_readers_locks[i]);
558         clib_spinlock_init (&um->peekers_write_locks[i]);
559       }
560
561   um->local_to_input_edge[UDP_IP4] =
562     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
563   um->local_to_input_edge[UDP_IP6] =
564     vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
565   return 0;
566 }
567
568 /* *INDENT-OFF* */
569 VLIB_INIT_FUNCTION (udp_init) =
570 {
571   .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
572                            "ip6_lookup_init"),
573 };
574 /* *INDENT-ON* */
575
576
577 static clib_error_t *
578 show_udp_punt_fn (vlib_main_t * vm, unformat_input_t * input,
579                   vlib_cli_command_t * cmd_arg)
580 {
581   udp_main_t *um = vnet_get_udp_main ();
582
583   clib_error_t *error = NULL;
584
585   if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
586     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
587                               input);
588
589   udp_dst_port_info_t *port_info;
590   if (um->punt_unknown4)
591     {
592       vlib_cli_output (vm, "IPv4 UDP punt: enabled");
593     }
594   else
595     {
596       u8 *s = NULL;
597       vec_foreach (port_info, um->dst_port_infos[UDP_IP4])
598       {
599         if (udp_is_valid_dst_port (port_info->dst_port, 1))
600           {
601             s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
602           }
603       }
604       s = format (s, "%c", 0);
605       vlib_cli_output (vm, "IPV4 UDP ports punt : %s", s);
606     }
607
608   if (um->punt_unknown6)
609     {
610       vlib_cli_output (vm, "IPv6 UDP punt: enabled");
611     }
612   else
613     {
614       u8 *s = NULL;
615       vec_foreach (port_info, um->dst_port_infos[UDP_IP6])
616       {
617         if (udp_is_valid_dst_port (port_info->dst_port, 01))
618           {
619             s = format (s, (!s) ? "%d" : ", %d", port_info->dst_port);
620           }
621       }
622       s = format (s, "%c", 0);
623       vlib_cli_output (vm, "IPV6 UDP ports punt : %s", s);
624     }
625
626   return (error);
627 }
628 /* *INDENT-OFF* */
629 VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
630 {
631   .path = "show udp punt",
632   .short_help = "show udp punt [ipv4|ipv6]",
633   .function = show_udp_punt_fn,
634 };
635 /* *INDENT-ON* */
636
637 /*
638  * fd.io coding-style-patch-verification: ON
639  *
640  * Local Variables:
641  * eval: (c-set-style "gnu")
642  * End:
643  */