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/fib/ip4_fib.h>
20 #include <vppinfra/sparse_vec.h>
25 udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
27 udp_main_t *um = &udp_main;
28 udp_dst_port_info_t *pi;
31 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
34 udp_add_dst_port (um, lcl_port, 0, is_ip4);
35 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
36 pi->n_connections = 1;
40 pi->n_connections += 1;
41 /* Do not return. The fact that the pi is valid does not mean
45 pi->node_index = is_ip4 ? udp4_input_node.index : udp6_input_node.index;
46 pi->next_index = um->local_to_input_edge[is_ip4];
48 /* Setup udp protocol -> next index sparse vector mapping. */
50 n = sparse_vec_validate (um->next_by_dst_port4,
51 clib_host_to_net_u16 (lcl_port));
53 n = sparse_vec_validate (um->next_by_dst_port6,
54 clib_host_to_net_u16 (lcl_port));
56 n[0] = pi->next_index;
60 udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
62 udp_main_t *um = &udp_main;
63 udp_dst_port_info_t *pi;
65 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
69 if (!pi->n_connections)
71 clib_warning ("no connections using port %u", lcl_port);
75 if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
76 udp_unregister_dst_port (0, lcl_port, is_ip4);
80 udp_connection_share_port (u16 lcl_port, u8 is_ip4)
82 udp_main_t *um = &udp_main;
83 udp_dst_port_info_t *pi;
85 /* Done without a lock but the operation is atomic. Writers to pi hash
86 * table and vector should be guarded by a barrier sync */
87 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
88 clib_atomic_fetch_add_rel (&pi->n_connections, 1);
92 udp_connection_alloc (u32 thread_index)
94 udp_main_t *um = &udp_main;
97 pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
98 CLIB_CACHE_LINE_BYTES);
100 if (PREDICT_FALSE (will_expand))
102 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
104 pool_get_aligned (udp_main.connections[thread_index], uc,
105 CLIB_CACHE_LINE_BYTES);
106 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
111 pool_get_aligned (um->connections[thread_index], uc,
112 CLIB_CACHE_LINE_BYTES);
114 clib_memset (uc, 0, sizeof (*uc));
115 uc->c_c_index = uc - um->connections[thread_index];
116 uc->c_thread_index = thread_index;
117 uc->c_proto = TRANSPORT_PROTO_UDP;
118 clib_spinlock_init (&uc->rx_lock);
123 udp_connection_free (udp_connection_t * uc)
125 u32 thread_index = uc->c_thread_index;
127 clib_memset (uc, 0xFA, sizeof (*uc));
128 pool_put (udp_main.connections[thread_index], uc);
132 udp_connection_cleanup (udp_connection_t * uc)
134 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
136 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
138 udp_connection_free (uc);
142 udp_connection_delete (udp_connection_t * uc)
144 session_transport_delete_notify (&uc->connection);
145 udp_connection_cleanup (uc);
149 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
151 udp_main_t *um = vnet_get_udp_main ();
152 udp_dst_port_info_t *pi;
154 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
155 return (pi && !pi->n_connections
156 && udp_is_valid_dst_port (lcl_port, is_ip4));
160 udp_default_mtu (udp_main_t * um, u8 is_ip4)
162 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
163 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
167 udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
169 udp_main_t *um = vnet_get_udp_main ();
170 vlib_main_t *vm = vlib_get_main ();
171 transport_endpoint_cfg_t *lcl_ext;
172 udp_connection_t *listener;
176 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
178 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
180 clib_warning ("port already used");
181 return SESSION_E_PORTINUSE;
184 pool_get (um->listener_pool, listener);
185 clib_memset (listener, 0, sizeof (udp_connection_t));
187 listener->c_lcl_port = lcl->port;
188 listener->c_c_index = listener - um->listener_pool;
190 /* If we are provided a sw_if_index, bind using one of its ips */
191 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
193 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
195 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
197 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
198 listener->c_is_ip4 = lcl->is_ip4;
199 listener->c_proto = TRANSPORT_PROTO_UDP;
200 listener->c_s_index = session_index;
201 listener->c_fib_index = lcl->fib_index;
202 listener->mss = udp_default_mtu (um, listener->c_is_ip4);
203 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
204 lcl_ext = (transport_endpoint_cfg_t *) lcl;
205 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
206 listener->flags |= UDP_CONN_F_CONNECTED;
208 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
209 clib_spinlock_init (&listener->rx_lock);
211 udp_connection_register_port (vm, lcl_port_ho, lcl->is_ip4);
212 return listener->c_c_index;
216 udp_session_unbind (u32 listener_index)
218 udp_main_t *um = &udp_main;
219 udp_connection_t *listener;
221 listener = udp_listener_get (listener_index);
222 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
224 pool_put (um->listener_pool, listener);
228 static transport_connection_t *
229 udp_session_get_listener (u32 listener_index)
231 udp_connection_t *us;
233 us = udp_listener_get (listener_index);
234 return &us->connection;
238 udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
240 udp_connection_t *uc;
241 vlib_main_t *vm = vlib_get_main ();
243 uc = udp_connection_from_transport (tc);
245 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
247 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
248 IP_PROTOCOL_UDP, 1 /* csum offload */ ,
251 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
253 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
254 vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
255 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
257 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
259 if (!transport_max_tx_dequeue (&uc->connection))
260 udp_connection_delete (uc);
266 static transport_connection_t *
267 udp_session_get (u32 connection_index, u32 thread_index)
269 udp_connection_t *uc;
270 uc = udp_connection_get (connection_index, thread_index);
272 return &uc->connection;
277 udp_session_close (u32 connection_index, u32 thread_index)
279 udp_connection_t *uc;
281 uc = udp_connection_get (connection_index, thread_index);
285 if (!transport_max_tx_dequeue (&uc->connection))
286 udp_connection_delete (uc);
288 uc->flags |= UDP_CONN_F_CLOSING;
292 udp_session_cleanup (u32 connection_index, u32 thread_index)
294 udp_connection_t *uc;
295 uc = udp_connection_get (connection_index, thread_index);
298 if (uc->flags & UDP_CONN_F_MIGRATED)
299 udp_connection_free (uc);
301 udp_connection_cleanup (uc);
305 udp_session_send_params (transport_connection_t * tconn,
306 transport_send_params_t * sp)
308 udp_connection_t *uc;
310 uc = udp_connection_from_transport (tconn);
312 /* No constraint on TX window */
314 /* TODO figure out MTU of output interface */
315 sp->snd_mss = uc->mss;
322 udp_open_connection (transport_endpoint_cfg_t * rmt)
324 vlib_main_t *vm = vlib_get_main ();
325 u32 thread_index = vm->thread_index;
326 udp_main_t *um = &udp_main;
327 ip46_address_t lcl_addr;
328 udp_connection_t *uc;
332 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
336 if (rv != SESSION_E_PORTINUSE)
339 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
340 return SESSION_E_PORTINUSE;
342 /* If port in use, check if 5-tuple is also in use */
343 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
344 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
346 return SESSION_E_PORTINUSE;
348 /* 5-tuple is available so increase lcl endpoint refcount and proceed
349 * with connection allocation */
350 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
355 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
357 /* If specific source port was requested abort */
359 return SESSION_E_PORTINUSE;
361 /* Try to find a port that's not used */
362 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
364 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
367 return SESSION_E_PORTINUSE;
373 udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
375 /* We don't poll main thread if we have workers */
376 if (vlib_num_workers ())
379 uc = udp_connection_alloc (thread_index);
380 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
381 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
382 uc->c_rmt_port = rmt->port;
383 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
384 uc->c_is_ip4 = rmt->is_ip4;
385 uc->c_proto = TRANSPORT_PROTO_UDP;
386 uc->c_fib_index = rmt->fib_index;
387 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
388 uc->flags |= UDP_CONN_F_OWNS_PORT;
389 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
390 uc->flags |= UDP_CONN_F_CONNECTED;
392 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
394 return uc->c_c_index;
397 static transport_connection_t *
398 udp_session_get_half_open (u32 conn_index)
400 udp_connection_t *uc;
403 /* We don't poll main thread if we have workers */
404 thread_index = vlib_num_workers ()? 1 : 0;
405 uc = udp_connection_get (conn_index, thread_index);
408 return &uc->connection;
412 format_udp_session (u8 * s, va_list * args)
414 u32 uci = va_arg (*args, u32);
415 u32 thread_index = va_arg (*args, u32);
416 u32 verbose = va_arg (*args, u32);
417 udp_connection_t *uc;
419 uc = udp_connection_get (uci, thread_index);
420 return format (s, "%U", format_udp_connection, uc, verbose);
424 format_udp_half_open_session (u8 * s, va_list * args)
426 u32 __clib_unused tci = va_arg (*args, u32);
427 u32 __clib_unused thread_index = va_arg (*args, u32);
428 clib_warning ("BUG");
433 format_udp_listener_session (u8 * s, va_list * args)
435 u32 tci = va_arg (*args, u32);
436 u32 __clib_unused thread_index = va_arg (*args, u32);
437 u32 verbose = va_arg (*args, u32);
438 udp_connection_t *uc = udp_listener_get (tci);
439 return format (s, "%U", format_udp_connection, uc, verbose);
443 static const transport_proto_vft_t udp_proto = {
444 .start_listen = udp_session_bind,
445 .connect = udp_open_connection,
446 .stop_listen = udp_session_unbind,
447 .push_header = udp_push_header,
448 .get_connection = udp_session_get,
449 .get_listener = udp_session_get_listener,
450 .get_half_open = udp_session_get_half_open,
451 .close = udp_session_close,
452 .cleanup = udp_session_cleanup,
453 .send_params = udp_session_send_params,
454 .format_connection = format_udp_session,
455 .format_half_open = format_udp_half_open_session,
456 .format_listener = format_udp_listener_session,
457 .transport_options = {
460 .tx_type = TRANSPORT_TX_DGRAM,
461 .service_type = TRANSPORT_SERVICE_CL,
467 udpc_connection_open (transport_endpoint_cfg_t * rmt)
469 udp_connection_t *uc;
470 /* Reproduce the logic of udp_open_connection to find the correct thread */
471 u32 thread_index = vlib_num_workers ()? 1 : vlib_get_main ()->thread_index;
473 uc_index = udp_open_connection (rmt);
474 if (uc_index == (u32) ~ 0)
476 uc = udp_connection_get (uc_index, thread_index);
477 uc->flags |= UDP_CONN_F_CONNECTED;
482 udpc_connection_listen (u32 session_index, transport_endpoint_t * lcl)
484 udp_connection_t *listener;
486 li_index = udp_session_bind (session_index, lcl);
487 if (li_index == (u32) ~ 0)
489 listener = udp_listener_get (li_index);
490 listener->flags |= UDP_CONN_F_CONNECTED;
491 /* Fake udp listener, i.e., make sure session layer adds a udp instead of
492 * udpc listener to the lookup table */
493 ((session_endpoint_cfg_t *) lcl)->transport_proto = TRANSPORT_PROTO_UDP;
498 static const transport_proto_vft_t udpc_proto = {
499 .start_listen = udpc_connection_listen,
500 .stop_listen = udp_session_unbind,
501 .connect = udpc_connection_open,
502 .push_header = udp_push_header,
503 .get_connection = udp_session_get,
504 .get_listener = udp_session_get_listener,
505 .get_half_open = udp_session_get_half_open,
506 .close = udp_session_close,
507 .cleanup = udp_session_cleanup,
508 .send_params = udp_session_send_params,
509 .format_connection = format_udp_session,
510 .format_half_open = format_udp_half_open_session,
511 .format_listener = format_udp_listener_session,
512 .transport_options = {
515 .tx_type = TRANSPORT_TX_DGRAM,
516 .service_type = TRANSPORT_SERVICE_VC,
517 .half_open_has_fifos = 1
522 static clib_error_t *
523 udp_init (vlib_main_t * vm)
525 udp_main_t *um = vnet_get_udp_main ();
526 ip_main_t *im = &ip_main;
527 vlib_thread_main_t *tm = vlib_get_thread_main ();
529 ip_protocol_info_t *pi;
536 /* IP registration */
537 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
539 return clib_error_return (0, "UDP protocol info AWOL");
540 pi->format_header = format_udp_header;
541 pi->unformat_pg_edit = unformat_pg_udp_header;
543 /* Register as transport with URI */
544 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
545 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
546 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
547 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
548 transport_register_protocol (TRANSPORT_PROTO_UDPC, &udpc_proto,
549 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
550 transport_register_protocol (TRANSPORT_PROTO_UDPC, &udpc_proto,
551 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
554 * Initialize data structures
557 num_threads = 1 /* main thread */ + tm->n_threads;
558 vec_validate (um->connections, num_threads - 1);
559 vec_validate (um->connection_peekers, num_threads - 1);
560 vec_validate (um->peekers_readers_locks, num_threads - 1);
561 vec_validate (um->peekers_write_locks, num_threads - 1);
564 for (i = 0; i < num_threads; i++)
566 clib_spinlock_init (&um->peekers_readers_locks[i]);
567 clib_spinlock_init (&um->peekers_write_locks[i]);
570 um->local_to_input_edge[UDP_IP4] =
571 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
572 um->local_to_input_edge[UDP_IP6] =
573 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
575 um->default_mtu = 1500;
580 VLIB_INIT_FUNCTION (udp_init) =
582 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
588 * fd.io coding-style-patch-verification: ON
591 * eval: (c-set-style "gnu")