blob: a8e013001e859e6a27ffbe6cfbd6640e4acb4bba [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 Coras9bc72ac2023-01-09 12:46:07 -080041
42 __atomic_add_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1,
43 __ATOMIC_RELAXED);
Florin Corasa0396202020-04-01 00:11:16 +000044}
45
46static void
47udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
48{
49 udp_main_t *um = &udp_main;
Florin Corase1f20582022-11-11 11:37:36 -080050 u16 *n;
Florin Corasa0396202020-04-01 00:11:16 +000051
Florin Coras9bc72ac2023-01-09 12:46:07 -080052 /* Needed because listeners are not tracked as local endpoints */
53 if (__atomic_sub_fetch (&um->transport_ports_refcnt[is_ip4][lcl_port], 1,
54 __ATOMIC_RELAXED))
55 return;
56
Florin Corase1f20582022-11-11 11:37:36 -080057 if (is_ip4)
58 n = sparse_vec_validate (um->next_by_dst_port4, lcl_port);
59 else
60 n = sparse_vec_validate (um->next_by_dst_port6, lcl_port);
Florin Corasa0396202020-04-01 00:11:16 +000061
Florin Corase1f20582022-11-11 11:37:36 -080062 n[0] = UDP_NO_NODE_SET;
Florin Corasa0396202020-04-01 00:11:16 +000063}
64
Florin Coras3cbc04b2017-10-02 00:18:51 -070065udp_connection_t *
66udp_connection_alloc (u32 thread_index)
67{
Florin Coras4c89b182022-10-24 18:59:06 -070068 udp_worker_t *wrk = udp_worker_get (thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -070069 udp_connection_t *uc;
Florin Coras3cbc04b2017-10-02 00:18:51 -070070
Florin Coras4c89b182022-10-24 18:59:06 -070071 pool_get_aligned_safe (wrk->connections, uc, CLIB_CACHE_LINE_BYTES);
Florin Corasa2b358b2022-03-18 12:50:03 -070072
Dave Barachb7b92992018-10-17 10:38:51 -040073 clib_memset (uc, 0, sizeof (*uc));
Florin Coras4c89b182022-10-24 18:59:06 -070074 uc->c_c_index = uc - wrk->connections;
Florin Coras3cbc04b2017-10-02 00:18:51 -070075 uc->c_thread_index = thread_index;
76 uc->c_proto = TRANSPORT_PROTO_UDP;
77 return uc;
78}
79
80void
81udp_connection_free (udp_connection_t * uc)
82{
Florin Coras4c89b182022-10-24 18:59:06 -070083 udp_worker_t *wrk = udp_worker_get (uc->c_thread_index);
84
Florin Coras30fdf392020-12-02 21:14:56 -080085 clib_spinlock_free (&uc->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -070086 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -040087 clib_memset (uc, 0xFA, sizeof (*uc));
Florin Coras4c89b182022-10-24 18:59:06 -070088 pool_put (wrk->connections, uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -070089}
Dave Barach68b0fb02017-02-28 15:15:56 -050090
Florin Coras57a5a2d2020-04-01 23:16:11 +000091static void
92udp_connection_cleanup (udp_connection_t * uc)
93{
Florin Corase1f20582022-11-11 11:37:36 -080094 /* Unregister port from udp_local only if refcount went to zero */
95 if (!transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
96 uc->c_lcl_port))
97 udp_connection_unregister_port (uc->c_lcl_port, uc->c_is_ip4);
Florin Coras57a5a2d2020-04-01 23:16:11 +000098 udp_connection_free (uc);
99}
100
Florin Corasc7542392019-07-22 08:08:43 -0700101void
102udp_connection_delete (udp_connection_t * uc)
103{
Florin Corasc7542392019-07-22 08:08:43 -0700104 session_transport_delete_notify (&uc->connection);
Florin Coras57a5a2d2020-04-01 23:16:11 +0000105 udp_connection_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700106}
107
Florin Coras1c29dfb2022-10-24 18:46:20 -0700108static void
109udp_handle_cleanups (void *args)
110{
111 u32 thread_index = (u32) pointer_to_uword (args);
112 udp_connection_t *uc;
113 udp_worker_t *wrk;
114 u32 *uc_index;
115
116 wrk = udp_worker_get (thread_index);
117 vec_foreach (uc_index, wrk->pending_cleanups)
118 {
119 uc = udp_connection_get (*uc_index, thread_index);
120 udp_connection_delete (uc);
121 }
122 vec_reset_length (wrk->pending_cleanups);
123}
124
125static void
126udp_connection_program_cleanup (udp_connection_t *uc)
127{
128 uword thread_index = uc->c_thread_index;
129 udp_worker_t *wrk;
130
131 wrk = udp_worker_get (uc->c_thread_index);
132 vec_add1 (wrk->pending_cleanups, uc->c_c_index);
133
134 if (vec_len (wrk->pending_cleanups) == 1)
135 session_send_rpc_evt_to_thread_force (
136 thread_index, udp_handle_cleanups,
137 uword_to_pointer (thread_index, void *));
138}
139
Florin Coras57660d92020-04-04 22:45:34 +0000140static u8
141udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
142{
143 udp_main_t *um = vnet_get_udp_main ();
144 udp_dst_port_info_t *pi;
145
146 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
Florin Corase1f20582022-11-11 11:37:36 -0800147 return (pi && udp_is_valid_dst_port (lcl_port, is_ip4));
Florin Coras57660d92020-04-04 22:45:34 +0000148}
149
Florin Corasc98ef752020-04-07 17:30:13 +0000150static u16
151udp_default_mtu (udp_main_t * um, u8 is_ip4)
152{
153 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
154 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
155}
156
157static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800158udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500159{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700160 udp_main_t *um = vnet_get_udp_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000161 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162 udp_connection_t *listener;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700163 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500164
Florin Corase1f20582022-11-11 11:37:36 -0800165 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl->port),
166 lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000167 {
168 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000169 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000170 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700171
172 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400173 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700174
Florin Coras0e495682017-09-19 22:27:18 -0700175 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700176 listener->c_c_index = listener - um->listener_pool;
177
178 /* If we are provided a sw_if_index, bind using one of its ips */
179 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
180 {
181 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
182 lcl->is_ip4)))
183 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
184 }
185 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
186 listener->c_is_ip4 = lcl->is_ip4;
187 listener->c_proto = TRANSPORT_PROTO_UDP;
188 listener->c_s_index = session_index;
189 listener->c_fib_index = lcl->fib_index;
Steven Luongbf12efc2022-07-25 09:29:23 -0700190 listener->mss =
191 lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000192 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000193 lcl_ext = (transport_endpoint_cfg_t *) lcl;
194 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
195 listener->flags |= UDP_CONN_F_CONNECTED;
196 else
197 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700198 clib_spinlock_init (&listener->rx_lock);
Florin Corasf8ee39f2022-10-18 18:37:56 -0700199 if (!um->csum_offload)
200 listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201
Florin Corase1f20582022-11-11 11:37:36 -0800202 udp_connection_register_port (listener->c_lcl_port, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500204}
205
Florin Corasc98ef752020-04-07 17:30:13 +0000206static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700207udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500208{
Florin Corasa0396202020-04-01 00:11:16 +0000209 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500210 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000211
Dave Barach68b0fb02017-02-28 15:15:56 -0500212 listener = udp_listener_get (listener_index);
Florin Corase1f20582022-11-11 11:37:36 -0800213 udp_connection_unregister_port (listener->c_lcl_port, listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800214 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000215 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500216 return 0;
217}
218
Florin Corasc98ef752020-04-07 17:30:13 +0000219static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500220udp_session_get_listener (u32 listener_index)
221{
222 udp_connection_t *us;
223
224 us = udp_listener_get (listener_index);
225 return &us->connection;
226}
227
Florin Corasf66cc802021-04-08 19:10:07 -0700228always_inline u32
Florin Corasd96859f2023-06-24 14:43:42 -0700229udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b,
230 u8 is_cless)
Dave Barach68b0fb02017-02-28 15:15:56 -0500231{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700232 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Florin Coras8c1be052022-10-17 11:52:34 -0700233 /* reuse tcp medatada for now */
234 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700235
Florin Corasd96859f2023-06-24 14:43:42 -0700236 if (!is_cless)
237 {
238 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
239 udp_csum_offload (uc));
240
241 if (uc->c_is_ip4)
242 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
243 IP_PROTOCOL_UDP, udp_csum_offload (uc),
244 0 /* is_df */, uc->c_dscp);
245 else
246 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
247 IP_PROTOCOL_UDP);
248
249 vnet_buffer (b)->tcp.flags = 0;
250 }
Florin Coras15952b22022-12-19 10:55:18 -0800251 else
Florin Corasd96859f2023-06-24 14:43:42 -0700252 {
253 u8 *data = vlib_buffer_get_current (b);
254 session_dgram_hdr_t hdr;
255
256 hdr = *(session_dgram_hdr_t *) (data - sizeof (hdr));
257
258 /* Local port assumed to be bound, not overwriting it */
259 vlib_buffer_push_udp (b, uc->c_lcl_port, hdr.rmt_port,
260 udp_csum_offload (uc));
261
262 if (uc->c_is_ip4)
263 vlib_buffer_push_ip4_custom (vm, b, &hdr.lcl_ip.ip4, &hdr.rmt_ip.ip4,
264 IP_PROTOCOL_UDP, udp_csum_offload (uc),
265 0 /* is_df */, uc->c_dscp);
266 else
267 vlib_buffer_push_ip6 (vm, b, &hdr.lcl_ip.ip6, &hdr.rmt_ip.ip6,
268 IP_PROTOCOL_UDP);
269
270 /* Not connected udp session. Mark buffer for custom handling in
271 * udp_output */
272 vnet_buffer (b)->tcp.flags |= UDP_CONN_F_LISTEN;
273 }
Florin Coras15952b22022-12-19 10:55:18 -0800274
Florin Corasf66cc802021-04-08 19:10:07 -0700275 return 0;
276}
277
Florin Corasd96859f2023-06-24 14:43:42 -0700278always_inline void
279udp_push_header_batch (udp_connection_t *uc, vlib_buffer_t **bs, u32 n_bufs,
280 u8 is_cless)
Florin Corasf66cc802021-04-08 19:10:07 -0700281{
282 vlib_main_t *vm = vlib_get_main ();
Florin Corasf66cc802021-04-08 19:10:07 -0700283
284 while (n_bufs >= 4)
285 {
286 vlib_prefetch_buffer_header (bs[2], STORE);
287 vlib_prefetch_buffer_header (bs[3], STORE);
288
Florin Corasd96859f2023-06-24 14:43:42 -0700289 udp_push_one_header (vm, uc, bs[0], is_cless);
290 udp_push_one_header (vm, uc, bs[1], is_cless);
Florin Corasf66cc802021-04-08 19:10:07 -0700291
292 n_bufs -= 2;
293 bs += 2;
294 }
295 while (n_bufs)
296 {
297 if (n_bufs > 1)
298 vlib_prefetch_buffer_header (bs[1], STORE);
299
Florin Corasd96859f2023-06-24 14:43:42 -0700300 udp_push_one_header (vm, uc, bs[0], is_cless);
Florin Corasf66cc802021-04-08 19:10:07 -0700301
302 n_bufs -= 1;
303 bs += 1;
304 }
Florin Corasd96859f2023-06-24 14:43:42 -0700305}
306
307static u32
308udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
309{
310 udp_connection_t *uc;
311
312 uc = udp_connection_from_transport (tc);
313 if (uc->flags & UDP_CONN_F_CONNECTED)
314 udp_push_header_batch (uc, bs, n_bufs, 0 /* is_cless */);
315 else
316 udp_push_header_batch (uc, bs, n_bufs, 1 /* is_cless */);
Florin Corasf66cc802021-04-08 19:10:07 -0700317
Florin Corasc7542392019-07-22 08:08:43 -0700318 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
319 {
Florin Corasc8767c42023-06-27 19:45:59 -0700320 if (!transport_tx_fifo_has_dgram (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700321 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700322 }
323
Florin Coras3cbc04b2017-10-02 00:18:51 -0700324 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500325}
326
Florin Corasc98ef752020-04-07 17:30:13 +0000327static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700328udp_session_get (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500329{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700330 udp_connection_t *uc;
331 uc = udp_connection_get (connection_index, thread_index);
332 if (uc)
333 return &uc->connection;
334 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500335}
336
Florin Corasc98ef752020-04-07 17:30:13 +0000337static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700338udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500339{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700340 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700341
Florin Coras3cbc04b2017-10-02 00:18:51 -0700342 uc = udp_connection_get (connection_index, thread_index);
Florin Coras02000442021-11-16 12:45:43 -0800343 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
Florin Corasc7542392019-07-22 08:08:43 -0700344 return;
345
Florin Corasc8767c42023-06-27 19:45:59 -0700346 if (!transport_tx_fifo_has_dgram (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700347 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700348 else
349 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700350}
351
Florin Corasc98ef752020-04-07 17:30:13 +0000352static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700353udp_session_cleanup (u32 connection_index, u32 thread_index)
354{
355 udp_connection_t *uc;
356 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000357 if (!uc)
358 return;
359 if (uc->flags & UDP_CONN_F_MIGRATED)
360 udp_connection_free (uc);
361 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000362 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500363}
364
Florin Coras70f879d2020-03-13 17:54:42 +0000365static int
366udp_session_send_params (transport_connection_t * tconn,
367 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500368{
Florin Corasba78e232020-04-06 21:28:59 +0000369 udp_connection_t *uc;
370
Florin Corase759bb52020-04-08 01:55:39 +0000371 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000372
Dave Barach68b0fb02017-02-28 15:15:56 -0500373 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000374 sp->snd_space = ~0;
375 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000376 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000377 sp->tx_offset = 0;
378 sp->flags = 0;
379 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500380}
381
Florin Corasc98ef752020-04-07 17:30:13 +0000382static int
Florin Coras5665ced2018-10-25 18:03:45 -0700383udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500384{
Florin Corasba78e232020-04-06 21:28:59 +0000385 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700386 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000387 udp_connection_t *uc;
Florin Corasc8e41102022-03-18 10:27:29 -0700388 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700389 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700390 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700391
Florin Coras00e01d32019-10-21 16:07:46 -0700392 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
393 &lcl_port);
394 if (rv)
Florin Coras3d6156f2023-01-30 11:18:36 -0800395 return rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700396
Florin Corasd921b892023-05-25 12:04:53 -0700397 if (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port),
398 rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700399 {
Florin Corasa0396202020-04-01 00:11:16 +0000400 /* If specific source port was requested abort */
401 if (rmt->peer.port)
Florin Coras3d6156f2023-01-30 11:18:36 -0800402 {
403 transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
404 lcl_port);
405 return SESSION_E_PORTINUSE;
406 }
Florin Corasa0396202020-04-01 00:11:16 +0000407
408 /* Try to find a port that's not used */
Florin Corasd921b892023-05-25 12:04:53 -0700409 while (udp_connection_port_used_extern (clib_net_to_host_u16 (lcl_port),
410 rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700411 {
Florin Coras3d6156f2023-01-30 11:18:36 -0800412 transport_release_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
413 lcl_port);
414 lcl_port =
415 transport_alloc_local_port (TRANSPORT_PROTO_UDP, &lcl_addr, rmt);
Florin Corasa0396202020-04-01 00:11:16 +0000416 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700417 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700418 }
419 }
420
Florin Coras40903ac2018-06-10 14:41:23 -0700421 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700422 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700423
Florin Coras3cbc04b2017-10-02 00:18:51 -0700424 uc = udp_connection_alloc (thread_index);
425 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
426 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
427 uc->c_rmt_port = rmt->port;
428 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
429 uc->c_is_ip4 = rmt->is_ip4;
430 uc->c_proto = TRANSPORT_PROTO_UDP;
431 uc->c_fib_index = rmt->fib_index;
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000432 uc->c_dscp = rmt->dscp;
Florin Corasc98ef752020-04-07 17:30:13 +0000433 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Steven Luongbf12efc2022-07-25 09:29:23 -0700434 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
435 uc->sw_if_index = rmt->peer.sw_if_index;
Florin Corasc7542392019-07-22 08:08:43 -0700436 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000437 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800438 {
439 uc->flags |= UDP_CONN_F_CONNECTED;
440 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000441 else
Florin Coras0ac57822021-03-03 08:06:12 -0800442 {
443 clib_spinlock_init (&uc->rx_lock);
444 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
445 }
Florin Corasf8ee39f2022-10-18 18:37:56 -0700446 if (!um->csum_offload)
447 uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras8c1be052022-10-17 11:52:34 -0700448 uc->next_node_index = rmt->next_node_index;
449 uc->next_node_opaque = rmt->next_node_opaque;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700450
Florin Corase1f20582022-11-11 11:37:36 -0800451 udp_connection_register_port (uc->c_lcl_port, rmt->is_ip4);
452
Florin Coras3cbc04b2017-10-02 00:18:51 -0700453 return uc->c_c_index;
454}
455
Florin Corasc98ef752020-04-07 17:30:13 +0000456static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700457udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700458{
459 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700460 u32 thread_index;
461
462 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700463 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700464 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200465 if (!uc)
466 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700467 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500468}
469
Florin Corasc98ef752020-04-07 17:30:13 +0000470static u8 *
471format_udp_session (u8 * s, va_list * args)
472{
473 u32 uci = va_arg (*args, u32);
474 u32 thread_index = va_arg (*args, u32);
475 u32 verbose = va_arg (*args, u32);
476 udp_connection_t *uc;
477
478 uc = udp_connection_get (uci, thread_index);
479 return format (s, "%U", format_udp_connection, uc, verbose);
480}
481
482static u8 *
483format_udp_half_open_session (u8 * s, va_list * args)
484{
485 u32 __clib_unused tci = va_arg (*args, u32);
486 u32 __clib_unused thread_index = va_arg (*args, u32);
487 clib_warning ("BUG");
488 return 0;
489}
490
491static u8 *
492format_udp_listener_session (u8 * s, va_list * args)
493{
494 u32 tci = va_arg (*args, u32);
495 u32 __clib_unused thread_index = va_arg (*args, u32);
496 u32 verbose = va_arg (*args, u32);
497 udp_connection_t *uc = udp_listener_get (tci);
498 return format (s, "%U", format_udp_connection, uc, verbose);
499}
500
Florin Coras797562c2022-11-18 18:29:23 -0800501static void
502udp_realloc_ports_sv (u16 **ports_nh_svp)
503{
504 u16 port, port_no, *ports_nh_sv, *mc;
505 u32 *ports = 0, *nh = 0, msum, i;
506 sparse_vec_header_t *h;
507 uword sv_index, *mb;
508
509 ports_nh_sv = *ports_nh_svp;
510
511 for (port = 1; port < 65535; port++)
512 {
513 port_no = clib_host_to_net_u16 (port);
514
515 sv_index = sparse_vec_index (ports_nh_sv, port_no);
516 if (sv_index != SPARSE_VEC_INVALID_INDEX)
517 {
518 vec_add1 (ports, port_no);
519 vec_add1 (nh, ports_nh_sv[sv_index]);
520 }
521 }
522
523 sparse_vec_free (ports_nh_sv);
524
525 ports_nh_sv =
526 sparse_vec_new (/* elt bytes */ sizeof (ports_nh_sv[0]),
527 /* bits in index */ BITS (((udp_header_t *) 0)->dst_port));
528
529 vec_resize (ports_nh_sv, 65535);
530
531 for (port = 1; port < 65535; port++)
Florin Corase1f20582022-11-11 11:37:36 -0800532 ports_nh_sv[port] = UDP_NO_NODE_SET;
Florin Coras797562c2022-11-18 18:29:23 -0800533
534 for (i = 0; i < vec_len (ports); i++)
535 ports_nh_sv[ports[i]] = nh[i];
536
537 h = sparse_vec_header (ports_nh_sv);
538 vec_foreach (mb, h->is_member_bitmap)
539 *mb = (uword) ~0;
540
541 msum = 0;
542 vec_foreach (mc, h->member_counts)
543 {
544 *mc = msum;
545 msum += msum == 0 ? 63 : 64;
546 }
547
548 vec_free (ports);
549 vec_free (nh);
550
551 *ports_nh_svp = ports_nh_sv;
552}
553
554static clib_error_t *
555udp_enable_disable (vlib_main_t *vm, u8 is_en)
556{
557 udp_main_t *um = &udp_main;
558
559 /* Not ideal. The sparse vector used to map ports to next nodes assumes
560 * only a few ports are ever used. When udp transport is enabled this does
561 * not hold and, to make matters worse, ports are consumed in a random
562 * order.
563 *
564 * This can lead to a lot of slow updates to internal data structures
565 * which in turn can slow udp connection allocations until all ports are
566 * eventually consumed.
567 *
568 * Consequently, reallocate sparse vector, preallocate all ports and have
569 * them point to UDP_NO_NODE_SET. We could consider switching the sparse
570 * vector to a preallocated vector but that would increase memory
571 * consumption for vpp deployments that do not rely on host stack.
572 */
573
574 udp_realloc_ports_sv (&um->next_by_dst_port4);
575 udp_realloc_ports_sv (&um->next_by_dst_port6);
576
Florin Coras9bc72ac2023-01-09 12:46:07 -0800577 vec_validate (um->transport_ports_refcnt[0], 65535);
578 vec_validate (um->transport_ports_refcnt[1], 65535);
579
Florin Coras797562c2022-11-18 18:29:23 -0800580 return 0;
581}
582
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200583static const transport_proto_vft_t udp_proto = {
Florin Coras797562c2022-11-18 18:29:23 -0800584 .enable = udp_enable_disable,
Florin Coras1ee78302019-02-05 15:51:15 -0800585 .start_listen = udp_session_bind,
586 .connect = udp_open_connection,
587 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500588 .push_header = udp_push_header,
589 .get_connection = udp_session_get,
590 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700591 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500592 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700593 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000594 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700595 .format_connection = format_udp_session,
596 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800597 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200598 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000599 .name = "udp",
600 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200601 .tx_type = TRANSPORT_TX_DGRAM,
602 .service_type = TRANSPORT_SERVICE_CL,
603 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700604};
Florin Coras7fb0fe12018-04-09 09:24:52 -0700605
Dave Barach68b0fb02017-02-28 15:15:56 -0500606static clib_error_t *
607udp_init (vlib_main_t * vm)
608{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700609 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500610 ip_main_t *im = &ip_main;
611 vlib_thread_main_t *tm = vlib_get_thread_main ();
612 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500613 ip_protocol_info_t *pi;
614
Dave Barach68b0fb02017-02-28 15:15:56 -0500615 /*
616 * Registrations
617 */
618
619 /* IP registration */
620 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
621 if (pi == 0)
622 return clib_error_return (0, "UDP protocol info AWOL");
623 pi->format_header = format_udp_header;
624 pi->unformat_pg_edit = unformat_pg_udp_header;
625
Florin Coras8c1be052022-10-17 11:52:34 -0700626 /* Register as transport with session layer */
Florin Coras561af9b2017-12-09 10:19:43 -0800627 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700628 FIB_PROTOCOL_IP4, udp4_output_node.index);
Florin Coras561af9b2017-12-09 10:19:43 -0800629 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700630 FIB_PROTOCOL_IP6, udp6_output_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500631
632 /*
633 * Initialize data structures
634 */
635
636 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras1c29dfb2022-10-24 18:46:20 -0700637 vec_validate (um->wrk, num_threads - 1);
Florin Corasa0396202020-04-01 00:11:16 +0000638
639 um->local_to_input_edge[UDP_IP4] =
640 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
641 um->local_to_input_edge[UDP_IP6] =
642 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
Florin Corasba78e232020-04-06 21:28:59 +0000643
644 um->default_mtu = 1500;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700645 um->csum_offload = 1;
Dave Barachf8d50682019-05-14 18:01:44 -0400646 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500647}
648
Dave Barachf8d50682019-05-14 18:01:44 -0400649/* *INDENT-OFF* */
650VLIB_INIT_FUNCTION (udp_init) =
651{
652 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
653 "ip6_lookup_init"),
654};
655/* *INDENT-ON* */
656
Dave Barach68b0fb02017-02-28 15:15:56 -0500657/*
658 * fd.io coding-style-patch-verification: ON
659 *
660 * Local Variables:
661 * eval: (c-set-style "gnu")
662 * End:
663 */