7851da8e91bc5fc57b7f16cf7f5bc85e5b2f6207
[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_main_t *um = &udp_main;
96   udp_connection_t *uc;
97
98   pool_get_aligned_safe (um->connections[thread_index], uc,
99                          CLIB_CACHE_LINE_BYTES);
100
101   clib_memset (uc, 0, sizeof (*uc));
102   uc->c_c_index = uc - um->connections[thread_index];
103   uc->c_thread_index = thread_index;
104   uc->c_proto = TRANSPORT_PROTO_UDP;
105   return uc;
106 }
107
108 void
109 udp_connection_free (udp_connection_t * uc)
110 {
111   u32 thread_index = uc->c_thread_index;
112   clib_spinlock_free (&uc->rx_lock);
113   if (CLIB_DEBUG)
114     clib_memset (uc, 0xFA, sizeof (*uc));
115   pool_put (udp_main.connections[thread_index], uc);
116 }
117
118 static void
119 udp_connection_cleanup (udp_connection_t * uc)
120 {
121   transport_endpoint_cleanup (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 u8
136 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
137 {
138   udp_main_t *um = vnet_get_udp_main ();
139   udp_dst_port_info_t *pi;
140
141   pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
142   return (pi && !pi->n_connections
143           && udp_is_valid_dst_port (lcl_port, is_ip4));
144 }
145
146 static u16
147 udp_default_mtu (udp_main_t * um, u8 is_ip4)
148 {
149   u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
150   return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
151 }
152
153 static u32
154 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
155 {
156   udp_main_t *um = vnet_get_udp_main ();
157   transport_endpoint_cfg_t *lcl_ext;
158   udp_connection_t *listener;
159   u16 lcl_port_ho;
160   void *iface_ip;
161
162   lcl_port_ho = clib_net_to_host_u16 (lcl->port);
163
164   if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
165     {
166       clib_warning ("port already used");
167       return SESSION_E_PORTINUSE;
168     }
169
170   pool_get (um->listener_pool, listener);
171   clib_memset (listener, 0, sizeof (udp_connection_t));
172
173   listener->c_lcl_port = lcl->port;
174   listener->c_c_index = listener - um->listener_pool;
175
176   /* If we are provided a sw_if_index, bind using one of its ips */
177   if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
178     {
179       if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
180                                                  lcl->is_ip4)))
181         ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
182     }
183   ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
184   listener->c_is_ip4 = lcl->is_ip4;
185   listener->c_proto = TRANSPORT_PROTO_UDP;
186   listener->c_s_index = session_index;
187   listener->c_fib_index = lcl->fib_index;
188   listener->mss =
189     lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
190   listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
191   lcl_ext = (transport_endpoint_cfg_t *) lcl;
192   if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
193     listener->flags |= UDP_CONN_F_CONNECTED;
194   else
195     listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
196   clib_spinlock_init (&listener->rx_lock);
197   if (!um->csum_offload)
198     listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
199
200   udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
201   return listener->c_c_index;
202 }
203
204 static u32
205 udp_session_unbind (u32 listener_index)
206 {
207   udp_main_t *um = &udp_main;
208   udp_connection_t *listener;
209
210   listener = udp_listener_get (listener_index);
211   udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
212                                   listener->c_is_ip4);
213   clib_spinlock_free (&listener->rx_lock);
214   pool_put (um->listener_pool, listener);
215   return 0;
216 }
217
218 static transport_connection_t *
219 udp_session_get_listener (u32 listener_index)
220 {
221   udp_connection_t *us;
222
223   us = udp_listener_get (listener_index);
224   return &us->connection;
225 }
226
227 always_inline u32
228 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
229 {
230   vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
231                         udp_csum_offload (uc));
232   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
233   /* reuse tcp medatada for now */
234   vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
235
236   return 0;
237 }
238
239 static u32
240 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
241 {
242   vlib_main_t *vm = vlib_get_main ();
243   udp_connection_t *uc;
244
245   uc = udp_connection_from_transport (tc);
246
247   while (n_bufs >= 4)
248     {
249       vlib_prefetch_buffer_header (bs[2], STORE);
250       vlib_prefetch_buffer_header (bs[3], STORE);
251
252       udp_push_one_header (vm, uc, bs[0]);
253       udp_push_one_header (vm, uc, bs[1]);
254
255       n_bufs -= 2;
256       bs += 2;
257     }
258   while (n_bufs)
259     {
260       if (n_bufs > 1)
261         vlib_prefetch_buffer_header (bs[1], STORE);
262
263       udp_push_one_header (vm, uc, bs[0]);
264
265       n_bufs -= 1;
266       bs += 1;
267     }
268
269   if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
270     {
271       if (!transport_max_tx_dequeue (&uc->connection))
272         udp_connection_delete (uc);
273     }
274
275   return 0;
276 }
277
278 static transport_connection_t *
279 udp_session_get (u32 connection_index, u32 thread_index)
280 {
281   udp_connection_t *uc;
282   uc = udp_connection_get (connection_index, thread_index);
283   if (uc)
284     return &uc->connection;
285   return 0;
286 }
287
288 static void
289 udp_session_close (u32 connection_index, u32 thread_index)
290 {
291   udp_connection_t *uc;
292
293   uc = udp_connection_get (connection_index, thread_index);
294   if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
295     return;
296
297   if (!transport_max_tx_dequeue (&uc->connection))
298     udp_connection_delete (uc);
299   else
300     uc->flags |= UDP_CONN_F_CLOSING;
301 }
302
303 static void
304 udp_session_cleanup (u32 connection_index, u32 thread_index)
305 {
306   udp_connection_t *uc;
307   uc = udp_connection_get (connection_index, thread_index);
308   if (!uc)
309     return;
310   if (uc->flags & UDP_CONN_F_MIGRATED)
311     udp_connection_free (uc);
312   else
313     udp_connection_cleanup (uc);
314 }
315
316 static int
317 udp_session_send_params (transport_connection_t * tconn,
318                          transport_send_params_t * sp)
319 {
320   udp_connection_t *uc;
321
322   uc = udp_connection_from_transport (tconn);
323
324   /* No constraint on TX window */
325   sp->snd_space = ~0;
326   /* TODO figure out MTU of output interface */
327   sp->snd_mss = uc->mss;
328   sp->tx_offset = 0;
329   sp->flags = 0;
330   return 0;
331 }
332
333 static int
334 udp_open_connection (transport_endpoint_cfg_t * rmt)
335 {
336   udp_main_t *um = &udp_main;
337   ip46_address_t lcl_addr;
338   udp_connection_t *uc;
339   u32 thread_index;
340   u16 lcl_port;
341   int rv;
342
343   rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
344                                        &lcl_port);
345   if (rv)
346     {
347       if (rv != SESSION_E_PORTINUSE)
348         return rv;
349
350       if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
351         return SESSION_E_PORTINUSE;
352
353       /* If port in use, check if 5-tuple is also in use */
354       if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
355                                      lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
356                                      rmt->is_ip4))
357         return SESSION_E_PORTINUSE;
358
359       /* 5-tuple is available so increase lcl endpoint refcount and proceed
360        * with connection allocation */
361       transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
362                                       lcl_port);
363       goto conn_alloc;
364     }
365
366   if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
367     {
368       /* If specific source port was requested abort */
369       if (rmt->peer.port)
370         return SESSION_E_PORTINUSE;
371
372       /* Try to find a port that's not used */
373       while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
374         {
375           lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
376                                                  &lcl_addr);
377           if (lcl_port < 1)
378             return SESSION_E_PORTINUSE;
379         }
380     }
381
382 conn_alloc:
383
384   udp_connection_register_port (lcl_port, rmt->is_ip4);
385
386   /* We don't poll main thread if we have workers */
387   thread_index = transport_cl_thread ();
388
389   uc = udp_connection_alloc (thread_index);
390   ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
391   ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
392   uc->c_rmt_port = rmt->port;
393   uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
394   uc->c_is_ip4 = rmt->is_ip4;
395   uc->c_proto = TRANSPORT_PROTO_UDP;
396   uc->c_fib_index = rmt->fib_index;
397   uc->c_dscp = rmt->dscp;
398   uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
399   if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
400     uc->sw_if_index = rmt->peer.sw_if_index;
401   uc->flags |= UDP_CONN_F_OWNS_PORT;
402   if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
403     {
404       uc->flags |= UDP_CONN_F_CONNECTED;
405     }
406   else
407     {
408       clib_spinlock_init (&uc->rx_lock);
409       uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
410     }
411   if (!um->csum_offload)
412     uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
413   uc->next_node_index = rmt->next_node_index;
414   uc->next_node_opaque = rmt->next_node_opaque;
415
416   return uc->c_c_index;
417 }
418
419 static transport_connection_t *
420 udp_session_get_half_open (u32 conn_index)
421 {
422   udp_connection_t *uc;
423   u32 thread_index;
424
425   /* We don't poll main thread if we have workers */
426   thread_index = transport_cl_thread ();
427   uc = udp_connection_get (conn_index, thread_index);
428   if (!uc)
429     return 0;
430   return &uc->connection;
431 }
432
433 static u8 *
434 format_udp_session (u8 * s, va_list * args)
435 {
436   u32 uci = va_arg (*args, u32);
437   u32 thread_index = va_arg (*args, u32);
438   u32 verbose = va_arg (*args, u32);
439   udp_connection_t *uc;
440
441   uc = udp_connection_get (uci, thread_index);
442   return format (s, "%U", format_udp_connection, uc, verbose);
443 }
444
445 static u8 *
446 format_udp_half_open_session (u8 * s, va_list * args)
447 {
448   u32 __clib_unused tci = va_arg (*args, u32);
449   u32 __clib_unused thread_index = va_arg (*args, u32);
450   clib_warning ("BUG");
451   return 0;
452 }
453
454 static u8 *
455 format_udp_listener_session (u8 * s, va_list * args)
456 {
457   u32 tci = va_arg (*args, u32);
458   u32 __clib_unused thread_index = va_arg (*args, u32);
459   u32 verbose = va_arg (*args, u32);
460   udp_connection_t *uc = udp_listener_get (tci);
461   return format (s, "%U", format_udp_connection, uc, verbose);
462 }
463
464 /* *INDENT-OFF* */
465 static const transport_proto_vft_t udp_proto = {
466   .start_listen = udp_session_bind,
467   .connect = udp_open_connection,
468   .stop_listen = udp_session_unbind,
469   .push_header = udp_push_header,
470   .get_connection = udp_session_get,
471   .get_listener = udp_session_get_listener,
472   .get_half_open = udp_session_get_half_open,
473   .close = udp_session_close,
474   .cleanup = udp_session_cleanup,
475   .send_params = udp_session_send_params,
476   .format_connection = format_udp_session,
477   .format_half_open = format_udp_half_open_session,
478   .format_listener = format_udp_listener_session,
479   .transport_options = {
480     .name = "udp",
481     .short_name = "U",
482     .tx_type = TRANSPORT_TX_DGRAM,
483     .service_type = TRANSPORT_SERVICE_CL,
484   },
485 };
486 /* *INDENT-ON* */
487
488 static clib_error_t *
489 udp_init (vlib_main_t * vm)
490 {
491   udp_main_t *um = vnet_get_udp_main ();
492   ip_main_t *im = &ip_main;
493   vlib_thread_main_t *tm = vlib_get_thread_main ();
494   u32 num_threads;
495   ip_protocol_info_t *pi;
496
497   /*
498    * Registrations
499    */
500
501   /* IP registration */
502   pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
503   if (pi == 0)
504     return clib_error_return (0, "UDP protocol info AWOL");
505   pi->format_header = format_udp_header;
506   pi->unformat_pg_edit = unformat_pg_udp_header;
507
508   /* Register as transport with session layer */
509   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
510                                FIB_PROTOCOL_IP4, udp4_output_node.index);
511   transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
512                                FIB_PROTOCOL_IP6, udp6_output_node.index);
513
514   /*
515    * Initialize data structures
516    */
517
518   num_threads = 1 /* main thread */  + tm->n_threads;
519   vec_validate (um->connections, num_threads - 1);
520
521   um->local_to_input_edge[UDP_IP4] =
522     vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
523   um->local_to_input_edge[UDP_IP6] =
524     vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
525
526   um->default_mtu = 1500;
527   um->csum_offload = 1;
528   return 0;
529 }
530
531 /* *INDENT-OFF* */
532 VLIB_INIT_FUNCTION (udp_init) =
533 {
534   .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
535                            "ip6_lookup_init"),
536 };
537 /* *INDENT-ON* */
538
539 /*
540  * fd.io coding-style-patch-verification: ON
541  *
542  * Local Variables:
543  * eval: (c-set-style "gnu")
544  * End:
545  */