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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 udp_connection_register_port (u16 lcl_port, u8 is_ip4)
28 udp_main_t *um = &udp_main;
31 /* Setup udp protocol -> next index sparse vector mapping. Do not setup
32 * udp_dst_port_info_t as that is used to distinguish between external
33 * and transport consumed ports */
36 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
38 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
40 n[0] = um->local_to_input_edge[is_ip4];
44 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
46 udp_main_t *um = &udp_main;
50 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
52 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
54 n[0] = UDP_NO_NODE_SET;
58 udp_connection_alloc (u32 thread_index)
60 udp_worker_t *wrk = udp_worker_get (thread_index);
63 pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES);
65 clib_memset (uc, 0, sizeof (*uc));
66 uc->c_c_index = uc - wrk->connections;
67 uc->c_thread_index = thread_index;
68 uc->c_proto = TRANSPORT_PROTO_UDP;
73 udp_connection_free (udp_connection_t * uc)
75 udp_worker_t *wrk = udp_worker_get (uc->c_thread_index);
77 clib_spinlock_free (&uc->rx_lock);
79 clib_memset (uc, 0xFA, sizeof (*uc));
80 pool_put (wrk->connections, uc);
84 udp_connection_cleanup (udp_connection_t * uc)
86 /* Unregister port from udp_local only if refcount went to zero */
87 if (!transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
89 udp_connection_unregister_port (uc->c_lcl_port, uc->c_is_ip4);
90 udp_connection_free (uc);
94 udp_connection_delete (udp_connection_t * uc)
96 session_transport_delete_notify (&uc->connection);
97 udp_connection_cleanup (uc);
101 udp_handle_cleanups (void *args)
103 u32 thread_index = (u32) pointer_to_uword (args);
104 udp_connection_t *uc;
108 wrk = udp_worker_get (thread_index);
109 vec_foreach (uc_index, wrk->pending_cleanups)
111 uc = udp_connection_get (*uc_index, thread_index);
112 udp_connection_delete (uc);
114 vec_reset_length (wrk->pending_cleanups);
118 udp_connection_program_cleanup (udp_connection_t *uc)
120 uword thread_index = uc->c_thread_index;
123 wrk = udp_worker_get (uc->c_thread_index);
124 vec_add1 (wrk->pending_cleanups, uc->c_c_index);
126 if (vec_len (wrk->pending_cleanups) == 1)
127 session_send_rpc_evt_to_thread_force (
128 thread_index, udp_handle_cleanups,
129 uword_to_pointer (thread_index, void *));
133 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
135 udp_main_t *um = vnet_get_udp_main ();
136 udp_dst_port_info_t *pi;
138 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
139 return (pi && udp_is_valid_dst_port (lcl_port, is_ip4));
143 udp_default_mtu (udp_main_t * um, u8 is_ip4)
145 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
146 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
150 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
152 udp_main_t *um = vnet_get_udp_main ();
153 transport_endpoint_cfg_t *lcl_ext;
154 udp_connection_t *listener;
157 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl->port),
160 clib_warning ("port already used");
161 return SESSION_E_PORTINUSE;
164 pool_get (um->listener_pool, listener);
165 clib_memset (listener, 0, sizeof (udp_connection_t));
167 listener->c_lcl_port = lcl->port;
168 listener->c_c_index = listener - um->listener_pool;
170 /* If we are provided a sw_if_index, bind using one of its ips */
171 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
173 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
175 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
177 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
178 listener->c_is_ip4 = lcl->is_ip4;
179 listener->c_proto = TRANSPORT_PROTO_UDP;
180 listener->c_s_index = session_index;
181 listener->c_fib_index = lcl->fib_index;
183 lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
184 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
185 lcl_ext = (transport_endpoint_cfg_t *) lcl;
186 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
187 listener->flags |= UDP_CONN_F_CONNECTED;
189 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
190 clib_spinlock_init (&listener->rx_lock);
191 if (!um->csum_offload)
192 listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
194 udp_connection_register_port (listener->c_lcl_port, lcl->is_ip4);
195 return listener->c_c_index;
199 udp_session_unbind (u32 listener_index)
201 udp_main_t *um = &udp_main;
202 udp_connection_t *listener;
204 listener = udp_listener_get (listener_index);
205 udp_connection_unregister_port (listener->c_lcl_port, listener->c_is_ip4);
206 clib_spinlock_free (&listener->rx_lock);
207 pool_put (um->listener_pool, listener);
211 static transport_connection_t *
212 udp_session_get_listener (u32 listener_index)
214 udp_connection_t *us;
216 us = udp_listener_get (listener_index);
217 return &us->connection;
221 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
223 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
224 udp_csum_offload (uc));
225 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
226 /* reuse tcp medatada for now */
227 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
233 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
235 vlib_main_t *vm = vlib_get_main ();
236 udp_connection_t *uc;
238 uc = udp_connection_from_transport (tc);
242 vlib_prefetch_buffer_header (bs[2], STORE);
243 vlib_prefetch_buffer_header (bs[3], STORE);
245 udp_push_one_header (vm, uc, bs[0]);
246 udp_push_one_header (vm, uc, bs[1]);
254 vlib_prefetch_buffer_header (bs[1], STORE);
256 udp_push_one_header (vm, uc, bs[0]);
262 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
264 if (!transport_max_tx_dequeue (&uc->connection))
265 udp_connection_program_cleanup (uc);
271 static transport_connection_t *
272 udp_session_get (u32 connection_index, u32 thread_index)
274 udp_connection_t *uc;
275 uc = udp_connection_get (connection_index, thread_index);
277 return &uc->connection;
282 udp_session_close (u32 connection_index, u32 thread_index)
284 udp_connection_t *uc;
286 uc = udp_connection_get (connection_index, thread_index);
287 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
290 if (!transport_max_tx_dequeue (&uc->connection))
291 udp_connection_program_cleanup (uc);
293 uc->flags |= UDP_CONN_F_CLOSING;
297 udp_session_cleanup (u32 connection_index, u32 thread_index)
299 udp_connection_t *uc;
300 uc = udp_connection_get (connection_index, thread_index);
303 if (uc->flags & UDP_CONN_F_MIGRATED)
304 udp_connection_free (uc);
306 udp_connection_cleanup (uc);
310 udp_session_send_params (transport_connection_t * tconn,
311 transport_send_params_t * sp)
313 udp_connection_t *uc;
315 uc = udp_connection_from_transport (tconn);
317 /* No constraint on TX window */
319 /* TODO figure out MTU of output interface */
320 sp->snd_mss = uc->mss;
327 udp_open_connection (transport_endpoint_cfg_t * rmt)
329 udp_main_t *um = &udp_main;
330 ip46_address_t lcl_addr;
331 udp_connection_t *uc;
336 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
340 if (rv != SESSION_E_PORTINUSE)
343 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
344 return SESSION_E_PORTINUSE;
346 /* If port in use, check if 5-tuple is also in use */
347 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
348 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
350 return SESSION_E_PORTINUSE;
352 /* 5-tuple is available so increase lcl endpoint refcount and proceed
353 * with connection allocation */
354 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
359 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
361 /* If specific source port was requested abort */
363 return SESSION_E_PORTINUSE;
365 /* Try to find a port that's not used */
366 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
368 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
371 return SESSION_E_PORTINUSE;
377 /* We don't poll main thread if we have workers */
378 thread_index = transport_cl_thread ();
380 uc = udp_connection_alloc (thread_index);
381 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
382 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
383 uc->c_rmt_port = rmt->port;
384 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
385 uc->c_is_ip4 = rmt->is_ip4;
386 uc->c_proto = TRANSPORT_PROTO_UDP;
387 uc->c_fib_index = rmt->fib_index;
388 uc->c_dscp = rmt->dscp;
389 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
390 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
391 uc->sw_if_index = rmt->peer.sw_if_index;
392 uc->flags |= UDP_CONN_F_OWNS_PORT;
393 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
395 uc->flags |= UDP_CONN_F_CONNECTED;
399 clib_spinlock_init (&uc->rx_lock);
400 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
402 if (!um->csum_offload)
403 uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
404 uc->next_node_index = rmt->next_node_index;
405 uc->next_node_opaque = rmt->next_node_opaque;
407 udp_connection_register_port (uc->c_lcl_port, rmt->is_ip4);
409 return uc->c_c_index;
412 static transport_connection_t *
413 udp_session_get_half_open (u32 conn_index)
415 udp_connection_t *uc;
418 /* We don't poll main thread if we have workers */
419 thread_index = transport_cl_thread ();
420 uc = udp_connection_get (conn_index, thread_index);
423 return &uc->connection;
427 format_udp_session (u8 * s, va_list * args)
429 u32 uci = va_arg (*args, u32);
430 u32 thread_index = va_arg (*args, u32);
431 u32 verbose = va_arg (*args, u32);
432 udp_connection_t *uc;
434 uc = udp_connection_get (uci, thread_index);
435 return format (s, "%U", format_udp_connection, uc, verbose);
439 format_udp_half_open_session (u8 * s, va_list * args)
441 u32 __clib_unused tci = va_arg (*args, u32);
442 u32 __clib_unused thread_index = va_arg (*args, u32);
443 clib_warning ("BUG");
448 format_udp_listener_session (u8 * s, va_list * args)
450 u32 tci = va_arg (*args, u32);
451 u32 __clib_unused thread_index = va_arg (*args, u32);
452 u32 verbose = va_arg (*args, u32);
453 udp_connection_t *uc = udp_listener_get (tci);
454 return format (s, "%U", format_udp_connection, uc, verbose);
458 udp_realloc_ports_sv (u16 **ports_nh_svp)
460 u16 port, port_no, *ports_nh_sv, *mc;
461 u32 *ports = 0, *nh = 0, msum, i;
462 sparse_vec_header_t *h;
465 ports_nh_sv = *ports_nh_svp;
467 for (port = 1; port < 65535; port++)
469 port_no = clib_host_to_net_u16 (port);
471 sv_index = sparse_vec_index (ports_nh_sv, port_no);
472 if (sv_index != SPARSE_VEC_INVALID_INDEX)
474 vec_add1 (ports, port_no);
475 vec_add1 (nh, ports_nh_sv[sv_index]);
479 sparse_vec_free (ports_nh_sv);
482 sparse_vec_new (/* elt bytes */ sizeof (ports_nh_sv[0]),
483 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
485 vec_resize (ports_nh_sv, 65535);
487 for (port = 1; port < 65535; port++)
488 ports_nh_sv[port] = UDP_NO_NODE_SET;
490 for (i = 0; i < vec_len (ports); i++)
491 ports_nh_sv[ports[i]] = nh[i];
493 h = sparse_vec_header (ports_nh_sv);
494 vec_foreach (mb, h->is_member_bitmap)
498 vec_foreach (mc, h->member_counts)
501 msum += msum == 0 ? 63 : 64;
507 *ports_nh_svp = ports_nh_sv;
510 static clib_error_t *
511 udp_enable_disable (vlib_main_t *vm, u8 is_en)
513 udp_main_t *um = &udp_main;
515 /* Not ideal. The sparse vector used to map ports to next nodes assumes
516 * only a few ports are ever used. When udp transport is enabled this does
517 * not hold and, to make matters worse, ports are consumed in a random
520 * This can lead to a lot of slow updates to internal data structures
521 * which in turn can slow udp connection allocations until all ports are
522 * eventually consumed.
524 * Consequently, reallocate sparse vector, preallocate all ports and have
525 * them point to UDP_NO_NODE_SET. We could consider switching the sparse
526 * vector to a preallocated vector but that would increase memory
527 * consumption for vpp deployments that do not rely on host stack.
530 udp_realloc_ports_sv (&um->next_by_dst_port4);
531 udp_realloc_ports_sv (&um->next_by_dst_port6);
536 static const transport_proto_vft_t udp_proto = {
537 .enable = udp_enable_disable,
538 .start_listen = udp_session_bind,
539 .connect = udp_open_connection,
540 .stop_listen = udp_session_unbind,
541 .push_header = udp_push_header,
542 .get_connection = udp_session_get,
543 .get_listener = udp_session_get_listener,
544 .get_half_open = udp_session_get_half_open,
545 .close = udp_session_close,
546 .cleanup = udp_session_cleanup,
547 .send_params = udp_session_send_params,
548 .format_connection = format_udp_session,
549 .format_half_open = format_udp_half_open_session,
550 .format_listener = format_udp_listener_session,
551 .transport_options = {
554 .tx_type = TRANSPORT_TX_DGRAM,
555 .service_type = TRANSPORT_SERVICE_CL,
559 static clib_error_t *
560 udp_init (vlib_main_t * vm)
562 udp_main_t *um = vnet_get_udp_main ();
563 ip_main_t *im = &ip_main;
564 vlib_thread_main_t *tm = vlib_get_thread_main ();
566 ip_protocol_info_t *pi;
572 /* IP registration */
573 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
575 return clib_error_return (0, "UDP protocol info AWOL");
576 pi->format_header = format_udp_header;
577 pi->unformat_pg_edit = unformat_pg_udp_header;
579 /* Register as transport with session layer */
580 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
581 FIB_PROTOCOL_IP4, udp4_output_node.index);
582 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
583 FIB_PROTOCOL_IP6, udp6_output_node.index);
586 * Initialize data structures
589 num_threads = 1 /* main thread */ + tm->n_threads;
590 vec_validate (um->wrk, num_threads - 1);
592 um->local_to_input_edge[UDP_IP4] =
593 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
594 um->local_to_input_edge[UDP_IP6] =
595 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
597 um->default_mtu = 1500;
598 um->csum_offload = 1;
603 VLIB_INIT_FUNCTION (udp_init) =
605 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
611 * fd.io coding-style-patch-verification: ON
614 * eval: (c-set-style "gnu")