blob: 710d1ac9e52396033a3a583f94a3431d28b44c7f [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Corasc98ef752020-04-07 17:30:13 +00002 * Copyright (c) 2016-2020 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
Dave Barach68b0fb02017-02-28 15:15:56 -050016#include <vnet/udp/udp.h>
17#include <vnet/session/session.h>
18#include <vnet/dpo/load_balance.h>
Neale Rannse4031132020-10-26 13:00:06 +000019#include <vnet/ip/ip4_inlines.h>
20#include <vnet/ip/ip6_inlines.h>
Florin Corasa0396202020-04-01 00:11:16 +000021#include <vppinfra/sparse_vec.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
Filip Tehlar2c49ffe2019-03-06 07:16:08 -080023udp_main_t udp_main;
24
Florin Corasa0396202020-04-01 00:11:16 +000025static void
Florin Corasc8e41102022-03-18 10:27:29 -070026udp_connection_register_port (u16 lcl_port, u8 is_ip4)
Florin Corasa0396202020-04-01 00:11:16 +000027{
28 udp_main_t *um = &udp_main;
Florin Corasa0396202020-04-01 00:11:16 +000029 u16 *n;
30
Florin Corase1f20582022-11-11 11:37:36 -080031 /* 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 */
Florin Corasa0396202020-04-01 00:11:16 +000034
Florin Corasa0396202020-04-01 00:11:16 +000035 if (is_ip4)
Florin Corase1f20582022-11-11 11:37:36 -080036 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
Florin Corasa0396202020-04-01 00:11:16 +000037 else
Florin Corase1f20582022-11-11 11:37:36 -080038 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
Florin Corasa0396202020-04-01 00:11:16 +000039
Florin Corase1f20582022-11-11 11:37:36 -080040 n[0] = um->local_to_input_edge[is_ip4];
Florin Corasa0396202020-04-01 00:11:16 +000041}
42
43static void
44udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
45{
46 udp_main_t *um = &udp_main;
Florin Corase1f20582022-11-11 11:37:36 -080047 u16 *n;
Florin Corasa0396202020-04-01 00:11:16 +000048
Florin Corase1f20582022-11-11 11:37:36 -080049 if (is_ip4)
50 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
51 else
52 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
Florin Corasa0396202020-04-01 00:11:16 +000053
Florin Corase1f20582022-11-11 11:37:36 -080054 n[0] = UDP_NO_NODE_SET;
Florin Corasa0396202020-04-01 00:11:16 +000055}
56
Florin Coras3cbc04b2017-10-02 00:18:51 -070057udp_connection_t *
58udp_connection_alloc (u32 thread_index)
59{
Florin Coras4c89b182022-10-24 18:59:06 -070060 udp_worker_t *wrk = udp_worker_get (thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -070061 udp_connection_t *uc;
Florin Coras3cbc04b2017-10-02 00:18:51 -070062
Florin Coras4c89b182022-10-24 18:59:06 -070063 pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES);
Florin Corasa2b358b2022-03-18 12:50:03 -070064
Dave Barachb7b92992018-10-17 10:38:51 -040065 clib_memset (uc, 0, sizeof (*uc));
Florin Coras4c89b182022-10-24 18:59:06 -070066 uc->c_c_index = uc - wrk->connections;
Florin Coras3cbc04b2017-10-02 00:18:51 -070067 uc->c_thread_index = thread_index;
68 uc->c_proto = TRANSPORT_PROTO_UDP;
69 return uc;
70}
71
72void
73udp_connection_free (udp_connection_t * uc)
74{
Florin Coras4c89b182022-10-24 18:59:06 -070075 udp_worker_t *wrk = udp_worker_get (uc->c_thread_index);
76
Florin Coras30fdf392020-12-02 21:14:56 -080077 clib_spinlock_free (&uc->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -070078 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040079 clib_memset (uc, 0xFA, sizeof (*uc));
Florin Coras4c89b182022-10-24 18:59:06 -070080 pool_put (wrk->connections, uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -070081}
Dave Barach68b0fb02017-02-28 15:15:56 -050082
Florin Coras57a5a2d2020-04-01 23:16:11 +000083static void
84udp_connection_cleanup (udp_connection_t * uc)
85{
Florin Corase1f20582022-11-11 11:37:36 -080086 /* Unregister port from udp_local only if refcount went to zero */
87 if (!transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
88 uc->c_lcl_port))
89 udp_connection_unregister_port (uc->c_lcl_port, uc->c_is_ip4);
Florin Coras57a5a2d2020-04-01 23:16:11 +000090 udp_connection_free (uc);
91}
92
Florin Corasc7542392019-07-22 08:08:43 -070093void
94udp_connection_delete (udp_connection_t * uc)
95{
Florin Corasc7542392019-07-22 08:08:43 -070096 session_transport_delete_notify (&uc->connection);
Florin Coras57a5a2d2020-04-01 23:16:11 +000097 udp_connection_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -070098}
99
Florin Coras1c29dfb2022-10-24 18:46:20 -0700100static void
101udp_handle_cleanups (void *args)
102{
103 u32 thread_index = (u32) pointer_to_uword (args);
104 udp_connection_t *uc;
105 udp_worker_t *wrk;
106 u32 *uc_index;
107
108 wrk = udp_worker_get (thread_index);
109 vec_foreach (uc_index, wrk->pending_cleanups)
110 {
111 uc = udp_connection_get (*uc_index, thread_index);
112 udp_connection_delete (uc);
113 }
114 vec_reset_length (wrk->pending_cleanups);
115}
116
117static void
118udp_connection_program_cleanup (udp_connection_t *uc)
119{
120 uword thread_index = uc->c_thread_index;
121 udp_worker_t *wrk;
122
123 wrk = udp_worker_get (uc->c_thread_index);
124 vec_add1 (wrk->pending_cleanups, uc->c_c_index);
125
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 *));
130}
131
Florin Coras57660d92020-04-04 22:45:34 +0000132static u8
133udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
134{
135 udp_main_t *um = vnet_get_udp_main ();
136 udp_dst_port_info_t *pi;
137
138 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
Florin Corase1f20582022-11-11 11:37:36 -0800139 return (pi && udp_is_valid_dst_port (lcl_port, is_ip4));
Florin Coras57660d92020-04-04 22:45:34 +0000140}
141
Florin Corasc98ef752020-04-07 17:30:13 +0000142static u16
143udp_default_mtu (udp_main_t * um, u8 is_ip4)
144{
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);
147}
148
149static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800150udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500151{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700152 udp_main_t *um = vnet_get_udp_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000153 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500154 udp_connection_t *listener;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700155 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500156
Florin Corase1f20582022-11-11 11:37:36 -0800157 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl->port),
158 lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000159 {
160 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000161 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000162 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700163
164 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400165 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700166
Florin Coras0e495682017-09-19 22:27:18 -0700167 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700168 listener->c_c_index = listener - um->listener_pool;
169
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)
172 {
173 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
174 lcl->is_ip4)))
175 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
176 }
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;
Steven Luongbf12efc2022-07-25 09:29:23 -0700182 listener->mss =
183 lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000184 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000185 lcl_ext = (transport_endpoint_cfg_t *) lcl;
186 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
187 listener->flags |= UDP_CONN_F_CONNECTED;
188 else
189 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700190 clib_spinlock_init (&listener->rx_lock);
Florin Corasf8ee39f2022-10-18 18:37:56 -0700191 if (!um->csum_offload)
192 listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700193
Florin Corase1f20582022-11-11 11:37:36 -0800194 udp_connection_register_port (listener->c_lcl_port, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700195 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500196}
197
Florin Corasc98ef752020-04-07 17:30:13 +0000198static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700199udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500200{
Florin Corasa0396202020-04-01 00:11:16 +0000201 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500202 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000203
Dave Barach68b0fb02017-02-28 15:15:56 -0500204 listener = udp_listener_get (listener_index);
Florin Corase1f20582022-11-11 11:37:36 -0800205 udp_connection_unregister_port (listener->c_lcl_port, listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800206 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000207 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500208 return 0;
209}
210
Florin Corasc98ef752020-04-07 17:30:13 +0000211static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500212udp_session_get_listener (u32 listener_index)
213{
214 udp_connection_t *us;
215
216 us = udp_listener_get (listener_index);
217 return &us->connection;
218}
219
Florin Corasf66cc802021-04-08 19:10:07 -0700220always_inline u32
221udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500222{
Florin Corasf8ee39f2022-10-18 18:37:56 -0700223 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
224 udp_csum_offload (uc));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700225 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Florin Coras8c1be052022-10-17 11:52:34 -0700226 /* reuse tcp medatada for now */
227 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700228
Florin Corasf66cc802021-04-08 19:10:07 -0700229 return 0;
230}
231
232static u32
233udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
234{
235 vlib_main_t *vm = vlib_get_main ();
236 udp_connection_t *uc;
237
238 uc = udp_connection_from_transport (tc);
239
240 while (n_bufs >= 4)
241 {
242 vlib_prefetch_buffer_header (bs[2], STORE);
243 vlib_prefetch_buffer_header (bs[3], STORE);
244
245 udp_push_one_header (vm, uc, bs[0]);
246 udp_push_one_header (vm, uc, bs[1]);
247
248 n_bufs -= 2;
249 bs += 2;
250 }
251 while (n_bufs)
252 {
253 if (n_bufs > 1)
254 vlib_prefetch_buffer_header (bs[1], STORE);
255
256 udp_push_one_header (vm, uc, bs[0]);
257
258 n_bufs -= 1;
259 bs += 1;
260 }
261
Florin Corasc7542392019-07-22 08:08:43 -0700262 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
263 {
264 if (!transport_max_tx_dequeue (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700265 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700266 }
267
Florin Coras3cbc04b2017-10-02 00:18:51 -0700268 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500269}
270
Florin Corasc98ef752020-04-07 17:30:13 +0000271static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700272udp_session_get (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500273{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700274 udp_connection_t *uc;
275 uc = udp_connection_get (connection_index, thread_index);
276 if (uc)
277 return &uc->connection;
278 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500279}
280
Florin Corasc98ef752020-04-07 17:30:13 +0000281static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700282udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500283{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700284 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700285
Florin Coras3cbc04b2017-10-02 00:18:51 -0700286 uc = udp_connection_get (connection_index, thread_index);
Florin Coras02000442021-11-16 12:45:43 -0800287 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
Florin Corasc7542392019-07-22 08:08:43 -0700288 return;
289
290 if (!transport_max_tx_dequeue (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700291 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700292 else
293 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294}
295
Florin Corasc98ef752020-04-07 17:30:13 +0000296static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700297udp_session_cleanup (u32 connection_index, u32 thread_index)
298{
299 udp_connection_t *uc;
300 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000301 if (!uc)
302 return;
303 if (uc->flags & UDP_CONN_F_MIGRATED)
304 udp_connection_free (uc);
305 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000306 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500307}
308
Florin Coras70f879d2020-03-13 17:54:42 +0000309static int
310udp_session_send_params (transport_connection_t * tconn,
311 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500312{
Florin Corasba78e232020-04-06 21:28:59 +0000313 udp_connection_t *uc;
314
Florin Corase759bb52020-04-08 01:55:39 +0000315 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000316
Dave Barach68b0fb02017-02-28 15:15:56 -0500317 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000318 sp->snd_space = ~0;
319 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000320 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000321 sp->tx_offset = 0;
322 sp->flags = 0;
323 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500324}
325
Florin Corasc98ef752020-04-07 17:30:13 +0000326static int
Florin Coras5665ced2018-10-25 18:03:45 -0700327udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500328{
Florin Corasba78e232020-04-06 21:28:59 +0000329 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700330 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000331 udp_connection_t *uc;
Florin Corasc8e41102022-03-18 10:27:29 -0700332 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700333 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700334 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700335
Florin Coras00e01d32019-10-21 16:07:46 -0700336 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
337 &lcl_port);
338 if (rv)
Florin Coras57660d92020-04-04 22:45:34 +0000339 {
340 if (rv != SESSION_E_PORTINUSE)
341 return rv;
342
343 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
344 return SESSION_E_PORTINUSE;
345
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,
349 rmt->is_ip4))
350 return SESSION_E_PORTINUSE;
351
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,
355 lcl_port);
356 goto conn_alloc;
357 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700358
Florin Corasa0396202020-04-01 00:11:16 +0000359 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700360 {
Florin Corasa0396202020-04-01 00:11:16 +0000361 /* If specific source port was requested abort */
362 if (rmt->peer.port)
Florin Coras00e01d32019-10-21 16:07:46 -0700363 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000364
365 /* Try to find a port that's not used */
366 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700367 {
Florin Corasa0396202020-04-01 00:11:16 +0000368 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
369 &lcl_addr);
370 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700371 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700372 }
373 }
374
Florin Coras57660d92020-04-04 22:45:34 +0000375conn_alloc:
376
Florin Coras40903ac2018-06-10 14:41:23 -0700377 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700378 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700379
Florin Coras3cbc04b2017-10-02 00:18:51 -0700380 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;
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000388 uc->c_dscp = rmt->dscp;
Florin Corasc98ef752020-04-07 17:30:13 +0000389 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Steven Luongbf12efc2022-07-25 09:29:23 -0700390 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
391 uc->sw_if_index = rmt->peer.sw_if_index;
Florin Corasc7542392019-07-22 08:08:43 -0700392 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000393 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800394 {
395 uc->flags |= UDP_CONN_F_CONNECTED;
396 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000397 else
Florin Coras0ac57822021-03-03 08:06:12 -0800398 {
399 clib_spinlock_init (&uc->rx_lock);
400 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
401 }
Florin Corasf8ee39f2022-10-18 18:37:56 -0700402 if (!um->csum_offload)
403 uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras8c1be052022-10-17 11:52:34 -0700404 uc->next_node_index = rmt->next_node_index;
405 uc->next_node_opaque = rmt->next_node_opaque;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700406
Florin Corase1f20582022-11-11 11:37:36 -0800407 udp_connection_register_port (uc->c_lcl_port, rmt->is_ip4);
408
Florin Coras3cbc04b2017-10-02 00:18:51 -0700409 return uc->c_c_index;
410}
411
Florin Corasc98ef752020-04-07 17:30:13 +0000412static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700413udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700414{
415 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700416 u32 thread_index;
417
418 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700419 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700420 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200421 if (!uc)
422 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700423 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500424}
425
Florin Corasc98ef752020-04-07 17:30:13 +0000426static u8 *
427format_udp_session (u8 * s, va_list * args)
428{
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;
433
434 uc = udp_connection_get (uci, thread_index);
435 return format (s, "%U", format_udp_connection, uc, verbose);
436}
437
438static u8 *
439format_udp_half_open_session (u8 * s, va_list * args)
440{
441 u32 __clib_unused tci = va_arg (*args, u32);
442 u32 __clib_unused thread_index = va_arg (*args, u32);
443 clib_warning ("BUG");
444 return 0;
445}
446
447static u8 *
448format_udp_listener_session (u8 * s, va_list * args)
449{
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);
455}
456
Florin Coras797562c2022-11-18 18:29:23 -0800457static void
458udp_realloc_ports_sv (u16 **ports_nh_svp)
459{
460 u16 port, port_no, *ports_nh_sv, *mc;
461 u32 *ports = 0, *nh = 0, msum, i;
462 sparse_vec_header_t *h;
463 uword sv_index, *mb;
464
465 ports_nh_sv = *ports_nh_svp;
466
467 for (port = 1; port < 65535; port++)
468 {
469 port_no = clib_host_to_net_u16 (port);
470
471 sv_index = sparse_vec_index (ports_nh_sv, port_no);
472 if (sv_index != SPARSE_VEC_INVALID_INDEX)
473 {
474 vec_add1 (ports, port_no);
475 vec_add1 (nh, ports_nh_sv[sv_index]);
476 }
477 }
478
479 sparse_vec_free (ports_nh_sv);
480
481 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));
484
485 vec_resize (ports_nh_sv, 65535);
486
487 for (port = 1; port < 65535; port++)
Florin Corase1f20582022-11-11 11:37:36 -0800488 ports_nh_sv[port] = UDP_NO_NODE_SET;
Florin Coras797562c2022-11-18 18:29:23 -0800489
490 for (i = 0; i < vec_len (ports); i++)
491 ports_nh_sv[ports[i]] = nh[i];
492
493 h = sparse_vec_header (ports_nh_sv);
494 vec_foreach (mb, h->is_member_bitmap)
495 *mb = (uword) ~0;
496
497 msum = 0;
498 vec_foreach (mc, h->member_counts)
499 {
500 *mc = msum;
501 msum += msum == 0 ? 63 : 64;
502 }
503
504 vec_free (ports);
505 vec_free (nh);
506
507 *ports_nh_svp = ports_nh_sv;
508}
509
510static clib_error_t *
511udp_enable_disable (vlib_main_t *vm, u8 is_en)
512{
513 udp_main_t *um = &udp_main;
514
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
518 * order.
519 *
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.
523 *
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.
528 */
529
530 udp_realloc_ports_sv (&um->next_by_dst_port4);
531 udp_realloc_ports_sv (&um->next_by_dst_port6);
532
533 return 0;
534}
535
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200536static const transport_proto_vft_t udp_proto = {
Florin Coras797562c2022-11-18 18:29:23 -0800537 .enable = udp_enable_disable,
Florin Coras1ee78302019-02-05 15:51:15 -0800538 .start_listen = udp_session_bind,
539 .connect = udp_open_connection,
540 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500541 .push_header = udp_push_header,
542 .get_connection = udp_session_get,
543 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700544 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700546 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000547 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700548 .format_connection = format_udp_session,
549 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800550 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200551 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000552 .name = "udp",
553 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200554 .tx_type = TRANSPORT_TX_DGRAM,
555 .service_type = TRANSPORT_SERVICE_CL,
556 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700557};
Florin Coras7fb0fe12018-04-09 09:24:52 -0700558
Dave Barach68b0fb02017-02-28 15:15:56 -0500559static clib_error_t *
560udp_init (vlib_main_t * vm)
561{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700562 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500563 ip_main_t *im = &ip_main;
564 vlib_thread_main_t *tm = vlib_get_thread_main ();
565 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566 ip_protocol_info_t *pi;
567
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 /*
569 * Registrations
570 */
571
572 /* IP registration */
573 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
574 if (pi == 0)
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;
578
Florin Coras8c1be052022-10-17 11:52:34 -0700579 /* Register as transport with session layer */
Florin Coras561af9b2017-12-09 10:19:43 -0800580 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700581 FIB_PROTOCOL_IP4, udp4_output_node.index);
Florin Coras561af9b2017-12-09 10:19:43 -0800582 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700583 FIB_PROTOCOL_IP6, udp6_output_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500584
585 /*
586 * Initialize data structures
587 */
588
589 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras1c29dfb2022-10-24 18:46:20 -0700590 vec_validate (um->wrk, num_threads - 1);
Florin Corasa0396202020-04-01 00:11:16 +0000591
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);
Florin Corasba78e232020-04-06 21:28:59 +0000596
597 um->default_mtu = 1500;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700598 um->csum_offload = 1;
Dave Barachf8d50682019-05-14 18:01:44 -0400599 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500600}
601
Dave Barachf8d50682019-05-14 18:01:44 -0400602/* *INDENT-OFF* */
603VLIB_INIT_FUNCTION (udp_init) =
604{
605 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
606 "ip6_lookup_init"),
607};
608/* *INDENT-ON* */
609
Dave Barach68b0fb02017-02-28 15:15:56 -0500610/*
611 * fd.io coding-style-patch-verification: ON
612 *
613 * Local Variables:
614 * eval: (c-set-style "gnu")
615 * End:
616 */