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];
42 __atomic_add_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1,
47 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
49 udp_main_t *um = &udp_main;
50 __atomic_add_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1,
55 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
57 udp_main_t *um = &udp_main;
60 /* Needed because listeners are not tracked as local endpoints */
61 if (__atomic_sub_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1,
66 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
68 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
70 n[0] = UDP_NO_NODE_SET;
74 udp_connection_alloc (u32 thread_index)
76 udp_worker_t *wrk = udp_worker_get (thread_index);
79 pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES);
81 clib_memset (uc, 0, sizeof (*uc));
82 uc->c_c_index = uc - wrk->connections;
83 uc->c_thread_index = thread_index;
84 uc->c_proto = TRANSPORT_PROTO_UDP;
89 udp_connection_free (udp_connection_t * uc)
91 udp_worker_t *wrk = udp_worker_get (uc->c_thread_index);
93 clib_spinlock_free (&uc->rx_lock);
95 clib_memset (uc, 0xFA, sizeof (*uc));
96 pool_put (wrk->connections, uc);
100 udp_connection_cleanup (udp_connection_t * uc)
102 transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
104 udp_connection_unregister_port (uc->c_lcl_port, uc->c_is_ip4);
105 udp_connection_free (uc);
109 udp_connection_delete (udp_connection_t * uc)
111 session_transport_delete_notify (&uc->connection);
112 udp_connection_cleanup (uc);
116 udp_handle_cleanups (void *args)
118 u32 thread_index = (u32) pointer_to_uword (args);
119 udp_connection_t *uc;
123 wrk = udp_worker_get (thread_index);
124 vec_foreach (uc_index, wrk->pending_cleanups)
126 uc = udp_connection_get (*uc_index, thread_index);
127 udp_connection_delete (uc);
129 vec_reset_length (wrk->pending_cleanups);
133 udp_connection_program_cleanup (udp_connection_t *uc)
135 uword thread_index = uc->c_thread_index;
138 wrk = udp_worker_get (uc->c_thread_index);
139 vec_add1 (wrk->pending_cleanups, uc->c_c_index);
141 if (vec_len (wrk->pending_cleanups) == 1)
142 session_send_rpc_evt_to_thread_force (
143 thread_index, udp_handle_cleanups,
144 uword_to_pointer (thread_index, void *));
148 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
150 udp_main_t *um = vnet_get_udp_main ();
151 udp_dst_port_info_t *pi;
153 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
154 return (pi && udp_is_valid_dst_port (lcl_port, is_ip4));
158 udp_default_mtu (udp_main_t * um, u8 is_ip4)
160 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
161 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
165 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
167 udp_main_t *um = vnet_get_udp_main ();
168 transport_endpoint_cfg_t *lcl_ext;
169 udp_connection_t *listener;
172 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl->port),
175 clib_warning ("port already used");
176 return SESSION_E_PORTINUSE;
179 pool_get (um->listener_pool, listener);
180 clib_memset (listener, 0, sizeof (udp_connection_t));
182 listener->c_lcl_port = lcl->port;
183 listener->c_c_index = listener - um->listener_pool;
185 /* If we are provided a sw_if_index, bind using one of its ips */
186 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
188 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
190 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
192 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
193 listener->c_is_ip4 = lcl->is_ip4;
194 listener->c_proto = TRANSPORT_PROTO_UDP;
195 listener->c_s_index = session_index;
196 listener->c_fib_index = lcl->fib_index;
198 lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
199 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
200 lcl_ext = (transport_endpoint_cfg_t *) lcl;
201 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
202 listener->flags |= UDP_CONN_F_CONNECTED;
204 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
205 clib_spinlock_init (&listener->rx_lock);
206 if (!um->csum_offload)
207 listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
209 udp_connection_register_port (listener->c_lcl_port, lcl->is_ip4);
210 return listener->c_c_index;
214 udp_session_unbind (u32 listener_index)
216 udp_main_t *um = &udp_main;
217 udp_connection_t *listener;
219 listener = udp_listener_get (listener_index);
220 udp_connection_unregister_port (listener->c_lcl_port, listener->c_is_ip4);
221 clib_spinlock_free (&listener->rx_lock);
222 pool_put (um->listener_pool, listener);
226 static transport_connection_t *
227 udp_session_get_listener (u32 listener_index)
229 udp_connection_t *us;
231 us = udp_listener_get (listener_index);
232 return &us->connection;
236 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b,
239 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
240 /* reuse tcp medatada for now */
241 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
245 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
246 udp_csum_offload (uc));
249 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
250 IP_PROTOCOL_UDP, udp_csum_offload (uc),
251 0 /* is_df */, uc->c_dscp);
253 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
256 vnet_buffer (b)->tcp.flags = 0;
260 u8 *data = vlib_buffer_get_current (b);
261 session_dgram_hdr_t hdr;
263 hdr = *(session_dgram_hdr_t *) (data - sizeof (hdr));
265 /* Local port assumed to be bound, not overwriting it */
266 vlib_buffer_push_udp (b, uc->c_lcl_port, hdr.rmt_port,
267 udp_csum_offload (uc));
270 vlib_buffer_push_ip4_custom (vm, b, &hdr.lcl_ip.ip4, &hdr.rmt_ip.ip4,
271 IP_PROTOCOL_UDP, udp_csum_offload (uc),
272 0 /* is_df */, uc->c_dscp);
274 vlib_buffer_push_ip6 (vm, b, &hdr.lcl_ip.ip6, &hdr.rmt_ip.ip6,
277 /* Not connected udp session. Mark buffer for custom handling in
279 vnet_buffer (b)->tcp.flags |= UDP_CONN_F_LISTEN;
286 udp_push_header_batch (udp_connection_t *uc, vlib_buffer_t **bs, u32 n_bufs,
289 vlib_main_t *vm = vlib_get_main ();
293 vlib_prefetch_buffer_header (bs[2], STORE);
294 vlib_prefetch_buffer_header (bs[3], STORE);
296 udp_push_one_header (vm, uc, bs[0], is_cless);
297 udp_push_one_header (vm, uc, bs[1], is_cless);
305 vlib_prefetch_buffer_header (bs[1], STORE);
307 udp_push_one_header (vm, uc, bs[0], is_cless);
315 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
317 udp_connection_t *uc;
319 uc = udp_connection_from_transport (tc);
320 if (uc->flags & UDP_CONN_F_CONNECTED)
321 udp_push_header_batch (uc, bs, n_bufs, 0 /* is_cless */);
323 udp_push_header_batch (uc, bs, n_bufs, 1 /* is_cless */);
325 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
327 if (!transport_tx_fifo_has_dgram (&uc->connection))
328 udp_connection_program_cleanup (uc);
334 static transport_connection_t *
335 udp_session_get (u32 connection_index, u32 thread_index)
337 udp_connection_t *uc;
338 uc = udp_connection_get (connection_index, thread_index);
340 return &uc->connection;
345 udp_session_close (u32 connection_index, u32 thread_index)
347 udp_connection_t *uc;
349 uc = udp_connection_get (connection_index, thread_index);
350 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
353 if (!transport_tx_fifo_has_dgram (&uc->connection))
354 udp_connection_program_cleanup (uc);
356 uc->flags |= UDP_CONN_F_CLOSING;
360 udp_session_cleanup (u32 connection_index, u32 thread_index)
362 udp_connection_t *uc;
363 uc = udp_connection_get (connection_index, thread_index);
366 if (uc->flags & UDP_CONN_F_MIGRATED)
367 udp_connection_free (uc);
369 udp_connection_cleanup (uc);
373 udp_session_send_params (transport_connection_t * tconn,
374 transport_send_params_t * sp)
376 udp_connection_t *uc;
378 uc = udp_connection_from_transport (tconn);
380 /* No constraint on TX window */
382 /* TODO figure out MTU of output interface */
383 sp->snd_mss = uc->mss;
390 udp_open_connection (transport_endpoint_cfg_t * rmt)
392 udp_main_t *um = &udp_main;
393 ip46_address_t lcl_addr;
394 udp_connection_t *uc;
399 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
404 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port),
407 /* If specific source port was requested abort */
410 transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
412 return SESSION_E_PORTINUSE;
415 /* Try to find a port that's not used */
416 while (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port),
419 transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
422 transport_alloc_local_port (TRANSPORT_PROTO_UDP, &lcl_addr, rmt);
423 if ((int) lcl_port < 1)
424 return SESSION_E_PORTINUSE;
428 /* We don't poll main thread if we have workers */
429 thread_index = transport_cl_thread ();
431 uc = udp_connection_alloc (thread_index);
432 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
433 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
434 uc->c_rmt_port = rmt->port;
435 uc->c_lcl_port = lcl_port;
436 uc->c_is_ip4 = rmt->is_ip4;
437 uc->c_proto = TRANSPORT_PROTO_UDP;
438 uc->c_fib_index = rmt->fib_index;
439 uc->c_dscp = rmt->dscp;
440 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
441 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
442 uc->sw_if_index = rmt->peer.sw_if_index;
443 uc->flags |= UDP_CONN_F_OWNS_PORT;
444 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
446 uc->flags |= UDP_CONN_F_CONNECTED;
450 clib_spinlock_init (&uc->rx_lock);
451 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
453 if (!um->csum_offload)
454 uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
455 uc->next_node_index = rmt->next_node_index;
456 uc->next_node_opaque = rmt->next_node_opaque;
458 udp_connection_register_port (uc->c_lcl_port, rmt->is_ip4);
460 return uc->c_c_index;
463 static transport_connection_t *
464 udp_session_get_half_open (u32 conn_index)
466 udp_connection_t *uc;
469 /* We don't poll main thread if we have workers */
470 thread_index = transport_cl_thread ();
471 uc = udp_connection_get (conn_index, thread_index);
474 return &uc->connection;
478 format_udp_session (u8 * s, va_list * args)
480 u32 uci = va_arg (*args, u32);
481 u32 thread_index = va_arg (*args, u32);
482 u32 verbose = va_arg (*args, u32);
483 udp_connection_t *uc;
485 uc = udp_connection_get (uci, thread_index);
486 return format (s, "%U", format_udp_connection, uc, verbose);
490 format_udp_half_open_session (u8 * s, va_list * args)
492 u32 __clib_unused tci = va_arg (*args, u32);
493 u32 __clib_unused thread_index = va_arg (*args, u32);
494 clib_warning ("BUG");
499 format_udp_listener_session (u8 * s, va_list * args)
501 u32 tci = va_arg (*args, u32);
502 u32 __clib_unused thread_index = va_arg (*args, u32);
503 u32 verbose = va_arg (*args, u32);
504 udp_connection_t *uc = udp_listener_get (tci);
505 return format (s, "%U", format_udp_connection, uc, verbose);
509 udp_realloc_ports_sv (u16 **ports_nh_svp)
511 u16 port, port_no, *ports_nh_sv, *mc;
512 u32 *ports = 0, *nh = 0, msum, i;
513 sparse_vec_header_t *h;
516 ports_nh_sv = *ports_nh_svp;
518 for (port = 1; port < 65535; port++)
520 port_no = clib_host_to_net_u16 (port);
522 sv_index = sparse_vec_index (ports_nh_sv, port_no);
523 if (sv_index != SPARSE_VEC_INVALID_INDEX)
525 vec_add1 (ports, port_no);
526 vec_add1 (nh, ports_nh_sv[sv_index]);
530 sparse_vec_free (ports_nh_sv);
533 sparse_vec_new (/* elt bytes */ sizeof (ports_nh_sv[0]),
534 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
536 vec_resize (ports_nh_sv, 65535);
538 for (port = 1; port < 65535; port++)
539 ports_nh_sv[port] = UDP_NO_NODE_SET;
541 for (i = 0; i < vec_len (ports); i++)
542 ports_nh_sv[ports[i]] = nh[i];
544 h = sparse_vec_header (ports_nh_sv);
545 vec_foreach (mb, h->is_member_bitmap)
549 vec_foreach (mc, h->member_counts)
552 msum += msum == 0 ? 63 : 64;
558 *ports_nh_svp = ports_nh_sv;
561 static clib_error_t *
562 udp_enable_disable (vlib_main_t *vm, u8 is_en)
564 udp_main_t *um = &udp_main;
566 /* Not ideal. The sparse vector used to map ports to next nodes assumes
567 * only a few ports are ever used. When udp transport is enabled this does
568 * not hold and, to make matters worse, ports are consumed in a random
571 * This can lead to a lot of slow updates to internal data structures
572 * which in turn can slow udp connection allocations until all ports are
573 * eventually consumed.
575 * Consequently, reallocate sparse vector, preallocate all ports and have
576 * them point to UDP_NO_NODE_SET. We could consider switching the sparse
577 * vector to a preallocated vector but that would increase memory
578 * consumption for vpp deployments that do not rely on host stack.
581 udp_realloc_ports_sv (&um->next_by_dst_port4);
582 udp_realloc_ports_sv (&um->next_by_dst_port6);
584 vec_validate (um->transport_ports_refcnt[0], 65535);
585 vec_validate (um->transport_ports_refcnt[1], 65535);
590 static const transport_proto_vft_t udp_proto = {
591 .enable = udp_enable_disable,
592 .start_listen = udp_session_bind,
593 .connect = udp_open_connection,
594 .stop_listen = udp_session_unbind,
595 .push_header = udp_push_header,
596 .get_connection = udp_session_get,
597 .get_listener = udp_session_get_listener,
598 .get_half_open = udp_session_get_half_open,
599 .close = udp_session_close,
600 .cleanup = udp_session_cleanup,
601 .send_params = udp_session_send_params,
602 .format_connection = format_udp_session,
603 .format_half_open = format_udp_half_open_session,
604 .format_listener = format_udp_listener_session,
605 .transport_options = {
608 .tx_type = TRANSPORT_TX_DGRAM,
609 .service_type = TRANSPORT_SERVICE_CL,
613 static clib_error_t *
614 udp_init (vlib_main_t * vm)
616 udp_main_t *um = vnet_get_udp_main ();
617 ip_main_t *im = &ip_main;
618 vlib_thread_main_t *tm = vlib_get_thread_main ();
620 ip_protocol_info_t *pi;
626 /* IP registration */
627 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
629 return clib_error_return (0, "UDP protocol info AWOL");
630 pi->format_header = format_udp_header;
631 pi->unformat_pg_edit = unformat_pg_udp_header;
633 /* Register as transport with session layer */
634 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
635 FIB_PROTOCOL_IP4, udp4_output_node.index);
636 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
637 FIB_PROTOCOL_IP6, udp6_output_node.index);
640 * Initialize data structures
643 num_threads = 1 /* main thread */ + tm->n_threads;
644 vec_validate (um->wrk, num_threads - 1);
646 um->local_to_input_edge[UDP_IP4] =
647 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
648 um->local_to_input_edge[UDP_IP6] =
649 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
651 um->default_mtu = 1500;
652 um->csum_offload = 1;
656 VLIB_INIT_FUNCTION (udp_init) =
658 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
663 * fd.io coding-style-patch-verification: ON
666 * eval: (c-set-style "gnu")