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;
29 udp_dst_port_info_t *pi;
32 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
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;
41 pi->n_connections += 1;
42 /* Do not return. The fact that the pi is valid does not mean
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];
49 /* Setup udp protocol -> next index sparse vector mapping. */
51 n = sparse_vec_validate (um->next_by_dst_port4,
52 clib_host_to_net_u16 (lcl_port));
54 n = sparse_vec_validate (um->next_by_dst_port6,
55 clib_host_to_net_u16 (lcl_port));
57 n[0] = pi->next_index;
61 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
63 udp_main_t *um = &udp_main;
64 udp_dst_port_info_t *pi;
66 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
70 if (!pi->n_connections)
72 clib_warning ("no connections using port %u", lcl_port);
76 if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
77 udp_unregister_dst_port (0, lcl_port, is_ip4);
81 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
83 udp_main_t *um = &udp_main;
84 udp_dst_port_info_t *pi;
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);
93 udp_connection_alloc (u32 thread_index)
95 udp_main_t *um = &udp_main;
97 u32 will_expand = pool_get_will_expand (um->connections[thread_index]);
99 if (PREDICT_FALSE (will_expand))
101 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
103 pool_get_aligned (udp_main.connections[thread_index], uc,
104 CLIB_CACHE_LINE_BYTES);
105 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
110 pool_get_aligned (um->connections[thread_index], uc,
111 CLIB_CACHE_LINE_BYTES);
113 clib_memset (uc, 0, sizeof (*uc));
114 uc->c_c_index = uc - um->connections[thread_index];
115 uc->c_thread_index = thread_index;
116 uc->c_proto = TRANSPORT_PROTO_UDP;
121 udp_connection_free (udp_connection_t * uc)
123 u32 thread_index = uc->c_thread_index;
124 clib_spinlock_free (&uc->rx_lock);
126 clib_memset (uc, 0xFA, sizeof (*uc));
127 pool_put (udp_main.connections[thread_index], uc);
131 udp_connection_cleanup (udp_connection_t * uc)
133 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
135 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
137 udp_connection_free (uc);
141 udp_connection_delete (udp_connection_t * uc)
143 session_transport_delete_notify (&uc->connection);
144 udp_connection_cleanup (uc);
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 && !pi->n_connections
155 && udp_is_valid_dst_port (lcl_port, is_ip4));
159 udp_default_mtu (udp_main_t * um, u8 is_ip4)
161 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
162 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
166 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
168 udp_main_t *um = vnet_get_udp_main ();
169 transport_endpoint_cfg_t *lcl_ext;
170 udp_connection_t *listener;
174 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
176 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
178 clib_warning ("port already used");
179 return SESSION_E_PORTINUSE;
182 pool_get (um->listener_pool, listener);
183 clib_memset (listener, 0, sizeof (udp_connection_t));
185 listener->c_lcl_port = lcl->port;
186 listener->c_c_index = listener - um->listener_pool;
188 /* If we are provided a sw_if_index, bind using one of its ips */
189 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
191 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
193 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
195 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
196 listener->c_is_ip4 = lcl->is_ip4;
197 listener->c_proto = TRANSPORT_PROTO_UDP;
198 listener->c_s_index = session_index;
199 listener->c_fib_index = lcl->fib_index;
200 listener->mss = udp_default_mtu (um, listener->c_is_ip4);
201 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
202 lcl_ext = (transport_endpoint_cfg_t *) lcl;
203 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
204 listener->flags |= UDP_CONN_F_CONNECTED;
206 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
207 clib_spinlock_init (&listener->rx_lock);
209 udp_connection_register_port (lcl_port_ho, 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 (clib_net_to_host_u16 (listener->c_lcl_port),
222 clib_spinlock_free (&listener->rx_lock);
223 pool_put (um->listener_pool, listener);
227 static transport_connection_t *
228 udp_session_get_listener (u32 listener_index)
230 udp_connection_t *us;
232 us = udp_listener_get (listener_index);
233 return &us->connection;
237 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
239 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
241 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
242 IP_PROTOCOL_UDP, 1 /* csum offload */,
243 0 /* is_df */, uc->c_dscp);
245 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
247 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
248 vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
249 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
255 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
257 vlib_main_t *vm = vlib_get_main ();
258 udp_connection_t *uc;
260 uc = udp_connection_from_transport (tc);
264 vlib_prefetch_buffer_header (bs[2], STORE);
265 vlib_prefetch_buffer_header (bs[3], STORE);
267 udp_push_one_header (vm, uc, bs[0]);
268 udp_push_one_header (vm, uc, bs[1]);
276 vlib_prefetch_buffer_header (bs[1], STORE);
278 udp_push_one_header (vm, uc, bs[0]);
284 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
286 if (!transport_max_tx_dequeue (&uc->connection))
287 udp_connection_delete (uc);
293 static transport_connection_t *
294 udp_session_get (u32 connection_index, u32 thread_index)
296 udp_connection_t *uc;
297 uc = udp_connection_get (connection_index, thread_index);
299 return &uc->connection;
304 udp_session_close (u32 connection_index, u32 thread_index)
306 udp_connection_t *uc;
308 uc = udp_connection_get (connection_index, thread_index);
309 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
312 if (!transport_max_tx_dequeue (&uc->connection))
313 udp_connection_delete (uc);
315 uc->flags |= UDP_CONN_F_CLOSING;
319 udp_session_cleanup (u32 connection_index, u32 thread_index)
321 udp_connection_t *uc;
322 uc = udp_connection_get (connection_index, thread_index);
325 if (uc->flags & UDP_CONN_F_MIGRATED)
326 udp_connection_free (uc);
328 udp_connection_cleanup (uc);
332 udp_session_send_params (transport_connection_t * tconn,
333 transport_send_params_t * sp)
335 udp_connection_t *uc;
337 uc = udp_connection_from_transport (tconn);
339 /* No constraint on TX window */
341 /* TODO figure out MTU of output interface */
342 sp->snd_mss = uc->mss;
349 udp_open_connection (transport_endpoint_cfg_t * rmt)
351 udp_main_t *um = &udp_main;
352 ip46_address_t lcl_addr;
353 udp_connection_t *uc;
358 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
362 if (rv != SESSION_E_PORTINUSE)
365 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
366 return SESSION_E_PORTINUSE;
368 /* If port in use, check if 5-tuple is also in use */
369 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
370 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
372 return SESSION_E_PORTINUSE;
374 /* 5-tuple is available so increase lcl endpoint refcount and proceed
375 * with connection allocation */
376 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
381 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
383 /* If specific source port was requested abort */
385 return SESSION_E_PORTINUSE;
387 /* Try to find a port that's not used */
388 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
390 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
393 return SESSION_E_PORTINUSE;
399 udp_connection_register_port (lcl_port, rmt->is_ip4);
401 /* We don't poll main thread if we have workers */
402 thread_index = transport_cl_thread ();
404 uc = udp_connection_alloc (thread_index);
405 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
406 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
407 uc->c_rmt_port = rmt->port;
408 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
409 uc->c_is_ip4 = rmt->is_ip4;
410 uc->c_proto = TRANSPORT_PROTO_UDP;
411 uc->c_fib_index = rmt->fib_index;
412 uc->c_dscp = rmt->dscp;
413 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
414 uc->flags |= UDP_CONN_F_OWNS_PORT;
415 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
417 uc->flags |= UDP_CONN_F_CONNECTED;
421 clib_spinlock_init (&uc->rx_lock);
422 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
425 return uc->c_c_index;
428 static transport_connection_t *
429 udp_session_get_half_open (u32 conn_index)
431 udp_connection_t *uc;
434 /* We don't poll main thread if we have workers */
435 thread_index = transport_cl_thread ();
436 uc = udp_connection_get (conn_index, thread_index);
439 return &uc->connection;
443 format_udp_session (u8 * s, va_list * args)
445 u32 uci = va_arg (*args, u32);
446 u32 thread_index = va_arg (*args, u32);
447 u32 verbose = va_arg (*args, u32);
448 udp_connection_t *uc;
450 uc = udp_connection_get (uci, thread_index);
451 return format (s, "%U", format_udp_connection, uc, verbose);
455 format_udp_half_open_session (u8 * s, va_list * args)
457 u32 __clib_unused tci = va_arg (*args, u32);
458 u32 __clib_unused thread_index = va_arg (*args, u32);
459 clib_warning ("BUG");
464 format_udp_listener_session (u8 * s, va_list * args)
466 u32 tci = va_arg (*args, u32);
467 u32 __clib_unused thread_index = va_arg (*args, u32);
468 u32 verbose = va_arg (*args, u32);
469 udp_connection_t *uc = udp_listener_get (tci);
470 return format (s, "%U", format_udp_connection, uc, verbose);
474 static const transport_proto_vft_t udp_proto = {
475 .start_listen = udp_session_bind,
476 .connect = udp_open_connection,
477 .stop_listen = udp_session_unbind,
478 .push_header = udp_push_header,
479 .get_connection = udp_session_get,
480 .get_listener = udp_session_get_listener,
481 .get_half_open = udp_session_get_half_open,
482 .close = udp_session_close,
483 .cleanup = udp_session_cleanup,
484 .send_params = udp_session_send_params,
485 .format_connection = format_udp_session,
486 .format_half_open = format_udp_half_open_session,
487 .format_listener = format_udp_listener_session,
488 .transport_options = {
491 .tx_type = TRANSPORT_TX_DGRAM,
492 .service_type = TRANSPORT_SERVICE_CL,
497 static clib_error_t *
498 udp_init (vlib_main_t * vm)
500 udp_main_t *um = vnet_get_udp_main ();
501 ip_main_t *im = &ip_main;
502 vlib_thread_main_t *tm = vlib_get_thread_main ();
504 ip_protocol_info_t *pi;
511 /* IP registration */
512 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
514 return clib_error_return (0, "UDP protocol info AWOL");
515 pi->format_header = format_udp_header;
516 pi->unformat_pg_edit = unformat_pg_udp_header;
518 /* Register as transport with URI */
519 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
520 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
521 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
522 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
525 * Initialize data structures
528 num_threads = 1 /* main thread */ + tm->n_threads;
529 vec_validate (um->connections, num_threads - 1);
530 vec_validate (um->connection_peekers, num_threads - 1);
531 vec_validate (um->peekers_readers_locks, num_threads - 1);
532 vec_validate (um->peekers_write_locks, num_threads - 1);
535 for (i = 0; i < num_threads; i++)
537 clib_spinlock_init (&um->peekers_readers_locks[i]);
538 clib_spinlock_init (&um->peekers_write_locks[i]);
541 um->local_to_input_edge[UDP_IP4] =
542 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
543 um->local_to_input_edge[UDP_IP6] =
544 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
546 um->default_mtu = 1500;
551 VLIB_INIT_FUNCTION (udp_init) =
553 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
559 * fd.io coding-style-patch-verification: ON
562 * eval: (c-set-style "gnu")