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;
98 pool_get_aligned_safe (um->connections[thread_index], uc,
99 CLIB_CACHE_LINE_BYTES);
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;
109 udp_connection_free (udp_connection_t * uc)
111 u32 thread_index = uc->c_thread_index;
112 clib_spinlock_free (&uc->rx_lock);
114 clib_memset (uc, 0xFA, sizeof (*uc));
115 pool_put (udp_main.connections[thread_index], uc);
119 udp_connection_cleanup (udp_connection_t * uc)
121 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
123 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
125 udp_connection_free (uc);
129 udp_connection_delete (udp_connection_t * uc)
131 session_transport_delete_notify (&uc->connection);
132 udp_connection_cleanup (uc);
136 udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
138 udp_main_t *um = vnet_get_udp_main ();
139 udp_dst_port_info_t *pi;
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));
147 udp_default_mtu (udp_main_t * um, u8 is_ip4)
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);
154 udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
156 udp_main_t *um = vnet_get_udp_main ();
157 transport_endpoint_cfg_t *lcl_ext;
158 udp_connection_t *listener;
162 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
164 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
166 clib_warning ("port already used");
167 return SESSION_E_PORTINUSE;
170 pool_get (um->listener_pool, listener);
171 clib_memset (listener, 0, sizeof (udp_connection_t));
173 listener->c_lcl_port = lcl->port;
174 listener->c_c_index = listener - um->listener_pool;
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)
179 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
181 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
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;
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;
195 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
196 clib_spinlock_init (&listener->rx_lock);
198 udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
199 return listener->c_c_index;
203 udp_session_unbind (u32 listener_index)
205 udp_main_t *um = &udp_main;
206 udp_connection_t *listener;
208 listener = udp_listener_get (listener_index);
209 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
211 clib_spinlock_free (&listener->rx_lock);
212 pool_put (um->listener_pool, listener);
216 static transport_connection_t *
217 udp_session_get_listener (u32 listener_index)
219 udp_connection_t *us;
221 us = udp_listener_get (listener_index);
222 return &us->connection;
226 udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
228 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
229 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
230 /* reuse tcp medatada for now */
231 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
237 udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
239 vlib_main_t *vm = vlib_get_main ();
240 udp_connection_t *uc;
242 uc = udp_connection_from_transport (tc);
246 vlib_prefetch_buffer_header (bs[2], STORE);
247 vlib_prefetch_buffer_header (bs[3], STORE);
249 udp_push_one_header (vm, uc, bs[0]);
250 udp_push_one_header (vm, uc, bs[1]);
258 vlib_prefetch_buffer_header (bs[1], STORE);
260 udp_push_one_header (vm, uc, bs[0]);
266 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
268 if (!transport_max_tx_dequeue (&uc->connection))
269 udp_connection_delete (uc);
275 static transport_connection_t *
276 udp_session_get (u32 connection_index, u32 thread_index)
278 udp_connection_t *uc;
279 uc = udp_connection_get (connection_index, thread_index);
281 return &uc->connection;
286 udp_session_close (u32 connection_index, u32 thread_index)
288 udp_connection_t *uc;
290 uc = udp_connection_get (connection_index, thread_index);
291 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
294 if (!transport_max_tx_dequeue (&uc->connection))
295 udp_connection_delete (uc);
297 uc->flags |= UDP_CONN_F_CLOSING;
301 udp_session_cleanup (u32 connection_index, u32 thread_index)
303 udp_connection_t *uc;
304 uc = udp_connection_get (connection_index, thread_index);
307 if (uc->flags & UDP_CONN_F_MIGRATED)
308 udp_connection_free (uc);
310 udp_connection_cleanup (uc);
314 udp_session_send_params (transport_connection_t * tconn,
315 transport_send_params_t * sp)
317 udp_connection_t *uc;
319 uc = udp_connection_from_transport (tconn);
321 /* No constraint on TX window */
323 /* TODO figure out MTU of output interface */
324 sp->snd_mss = uc->mss;
331 udp_open_connection (transport_endpoint_cfg_t * rmt)
333 udp_main_t *um = &udp_main;
334 ip46_address_t lcl_addr;
335 udp_connection_t *uc;
340 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
344 if (rv != SESSION_E_PORTINUSE)
347 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
348 return SESSION_E_PORTINUSE;
350 /* If port in use, check if 5-tuple is also in use */
351 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
352 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
354 return SESSION_E_PORTINUSE;
356 /* 5-tuple is available so increase lcl endpoint refcount and proceed
357 * with connection allocation */
358 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
363 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
365 /* If specific source port was requested abort */
367 return SESSION_E_PORTINUSE;
369 /* Try to find a port that's not used */
370 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
372 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
375 return SESSION_E_PORTINUSE;
381 udp_connection_register_port (lcl_port, rmt->is_ip4);
383 /* We don't poll main thread if we have workers */
384 thread_index = transport_cl_thread ();
386 uc = udp_connection_alloc (thread_index);
387 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
388 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
389 uc->c_rmt_port = rmt->port;
390 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
391 uc->c_is_ip4 = rmt->is_ip4;
392 uc->c_proto = TRANSPORT_PROTO_UDP;
393 uc->c_fib_index = rmt->fib_index;
394 uc->c_dscp = rmt->dscp;
395 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
396 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
397 uc->sw_if_index = rmt->peer.sw_if_index;
398 uc->flags |= UDP_CONN_F_OWNS_PORT;
399 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
401 uc->flags |= UDP_CONN_F_CONNECTED;
405 clib_spinlock_init (&uc->rx_lock);
406 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
408 uc->next_node_index = rmt->next_node_index;
409 uc->next_node_opaque = rmt->next_node_opaque;
411 return uc->c_c_index;
414 static transport_connection_t *
415 udp_session_get_half_open (u32 conn_index)
417 udp_connection_t *uc;
420 /* We don't poll main thread if we have workers */
421 thread_index = transport_cl_thread ();
422 uc = udp_connection_get (conn_index, thread_index);
425 return &uc->connection;
429 format_udp_session (u8 * s, va_list * args)
431 u32 uci = va_arg (*args, u32);
432 u32 thread_index = va_arg (*args, u32);
433 u32 verbose = va_arg (*args, u32);
434 udp_connection_t *uc;
436 uc = udp_connection_get (uci, thread_index);
437 return format (s, "%U", format_udp_connection, uc, verbose);
441 format_udp_half_open_session (u8 * s, va_list * args)
443 u32 __clib_unused tci = va_arg (*args, u32);
444 u32 __clib_unused thread_index = va_arg (*args, u32);
445 clib_warning ("BUG");
450 format_udp_listener_session (u8 * s, va_list * args)
452 u32 tci = va_arg (*args, u32);
453 u32 __clib_unused thread_index = va_arg (*args, u32);
454 u32 verbose = va_arg (*args, u32);
455 udp_connection_t *uc = udp_listener_get (tci);
456 return format (s, "%U", format_udp_connection, uc, verbose);
460 static const transport_proto_vft_t udp_proto = {
461 .start_listen = udp_session_bind,
462 .connect = udp_open_connection,
463 .stop_listen = udp_session_unbind,
464 .push_header = udp_push_header,
465 .get_connection = udp_session_get,
466 .get_listener = udp_session_get_listener,
467 .get_half_open = udp_session_get_half_open,
468 .close = udp_session_close,
469 .cleanup = udp_session_cleanup,
470 .send_params = udp_session_send_params,
471 .format_connection = format_udp_session,
472 .format_half_open = format_udp_half_open_session,
473 .format_listener = format_udp_listener_session,
474 .transport_options = {
477 .tx_type = TRANSPORT_TX_DGRAM,
478 .service_type = TRANSPORT_SERVICE_CL,
483 static clib_error_t *
484 udp_init (vlib_main_t * vm)
486 udp_main_t *um = vnet_get_udp_main ();
487 ip_main_t *im = &ip_main;
488 vlib_thread_main_t *tm = vlib_get_thread_main ();
490 ip_protocol_info_t *pi;
496 /* IP registration */
497 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
499 return clib_error_return (0, "UDP protocol info AWOL");
500 pi->format_header = format_udp_header;
501 pi->unformat_pg_edit = unformat_pg_udp_header;
503 /* Register as transport with session layer */
504 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
505 FIB_PROTOCOL_IP4, udp4_output_node.index);
506 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
507 FIB_PROTOCOL_IP6, udp6_output_node.index);
510 * Initialize data structures
513 num_threads = 1 /* main thread */ + tm->n_threads;
514 vec_validate (um->connections, num_threads - 1);
516 um->local_to_input_edge[UDP_IP4] =
517 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
518 um->local_to_input_edge[UDP_IP6] =
519 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
521 um->default_mtu = 1500;
526 VLIB_INIT_FUNCTION (udp_init) =
528 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
534 * fd.io coding-style-patch-verification: ON
537 * eval: (c-set-style "gnu")