blob: 5e4b29f25f6004422f3a8342d8c0561f38515a85 [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;
29 udp_dst_port_info_t *pi;
30 u16 *n;
31
32 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
33 if (!pi)
34 {
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;
38 }
39 else
40 {
41 pi->n_connections += 1;
42 /* Do not return. The fact that the pi is valid does not mean
43 * it's up to date */
44 }
45
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];
48
49 /* Setup udp protocol -> next index sparse vector mapping. */
50 if (is_ip4)
51 n = sparse_vec_validate (um->next_by_dst_port4,
52 clib_host_to_net_u16 (lcl_port));
53 else
54 n = sparse_vec_validate (um->next_by_dst_port6,
55 clib_host_to_net_u16 (lcl_port));
56
57 n[0] = pi->next_index;
58}
59
60static void
61udp_connection_unregister_port (u16 lcl_port, u8 is_ip4)
62{
63 udp_main_t *um = &udp_main;
64 udp_dst_port_info_t *pi;
65
66 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
67 if (!pi)
68 return;
69
70 if (!pi->n_connections)
71 {
72 clib_warning ("no connections using port %u", lcl_port);
73 return;
74 }
75
76 if (!clib_atomic_sub_fetch (&pi->n_connections, 1))
77 udp_unregister_dst_port (0, lcl_port, is_ip4);
78}
79
80void
81udp_connection_share_port (u16 lcl_port, u8 is_ip4)
82{
83 udp_main_t *um = &udp_main;
84 udp_dst_port_info_t *pi;
85
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);
90}
91
Florin Coras3cbc04b2017-10-02 00:18:51 -070092udp_connection_t *
93udp_connection_alloc (u32 thread_index)
94{
95 udp_main_t *um = &udp_main;
96 udp_connection_t *uc;
Florin Coras3cbc04b2017-10-02 00:18:51 -070097
Florin Corasa2b358b2022-03-18 12:50:03 -070098 pool_get_aligned_safe (um->connections[thread_index], uc,
99 CLIB_CACHE_LINE_BYTES);
100
Dave Barachb7b92992018-10-17 10:38:51 -0400101 clib_memset (uc, 0, sizeof (*uc));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700102 uc->c_c_index = uc - um->connections[thread_index];
103 uc->c_thread_index = thread_index;
104 uc->c_proto = TRANSPORT_PROTO_UDP;
105 return uc;
106}
107
108void
109udp_connection_free (udp_connection_t * uc)
110{
Benoît Ganned4aeb842019-07-18 18:38:42 +0200111 u32 thread_index = uc->c_thread_index;
Florin Coras30fdf392020-12-02 21:14:56 -0800112 clib_spinlock_free (&uc->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700113 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400114 clib_memset (uc, 0xFA, sizeof (*uc));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200115 pool_put (udp_main.connections[thread_index], uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700116}
Dave Barach68b0fb02017-02-28 15:15:56 -0500117
Florin Coras57a5a2d2020-04-01 23:16:11 +0000118static void
119udp_connection_cleanup (udp_connection_t * uc)
120{
121 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
122 uc->c_lcl_port);
123 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
124 uc->c_is_ip4);
125 udp_connection_free (uc);
126}
127
Florin Corasc7542392019-07-22 08:08:43 -0700128void
129udp_connection_delete (udp_connection_t * uc)
130{
Florin Corasc7542392019-07-22 08:08:43 -0700131 session_transport_delete_notify (&uc->connection);
Florin Coras57a5a2d2020-04-01 23:16:11 +0000132 udp_connection_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700133}
134
Florin Coras1c29dfb2022-10-24 18:46:20 -0700135static void
136udp_handle_cleanups (void *args)
137{
138 u32 thread_index = (u32) pointer_to_uword (args);
139 udp_connection_t *uc;
140 udp_worker_t *wrk;
141 u32 *uc_index;
142
143 wrk = udp_worker_get (thread_index);
144 vec_foreach (uc_index, wrk->pending_cleanups)
145 {
146 uc = udp_connection_get (*uc_index, thread_index);
147 udp_connection_delete (uc);
148 }
149 vec_reset_length (wrk->pending_cleanups);
150}
151
152static void
153udp_connection_program_cleanup (udp_connection_t *uc)
154{
155 uword thread_index = uc->c_thread_index;
156 udp_worker_t *wrk;
157
158 wrk = udp_worker_get (uc->c_thread_index);
159 vec_add1 (wrk->pending_cleanups, uc->c_c_index);
160
161 if (vec_len (wrk->pending_cleanups) == 1)
162 session_send_rpc_evt_to_thread_force (
163 thread_index, udp_handle_cleanups,
164 uword_to_pointer (thread_index, void *));
165}
166
Florin Coras57660d92020-04-04 22:45:34 +0000167static u8
168udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
169{
170 udp_main_t *um = vnet_get_udp_main ();
171 udp_dst_port_info_t *pi;
172
173 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
174 return (pi && !pi->n_connections
175 && udp_is_valid_dst_port (lcl_port, is_ip4));
176}
177
Florin Corasc98ef752020-04-07 17:30:13 +0000178static u16
179udp_default_mtu (udp_main_t * um, u8 is_ip4)
180{
181 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
182 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
183}
184
185static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800186udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500187{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700188 udp_main_t *um = vnet_get_udp_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000189 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500190 udp_connection_t *listener;
Florin Coras57a5a2d2020-04-01 23:16:11 +0000191 u16 lcl_port_ho;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700192 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500193
Florin Coras57a5a2d2020-04-01 23:16:11 +0000194 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
Florin Corasa0396202020-04-01 00:11:16 +0000195
Florin Coras57660d92020-04-04 22:45:34 +0000196 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000197 {
198 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000199 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000200 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201
202 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400203 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700204
Florin Coras0e495682017-09-19 22:27:18 -0700205 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700206 listener->c_c_index = listener - um->listener_pool;
207
208 /* If we are provided a sw_if_index, bind using one of its ips */
209 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
210 {
211 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
212 lcl->is_ip4)))
213 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
214 }
215 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
216 listener->c_is_ip4 = lcl->is_ip4;
217 listener->c_proto = TRANSPORT_PROTO_UDP;
218 listener->c_s_index = session_index;
219 listener->c_fib_index = lcl->fib_index;
Steven Luongbf12efc2022-07-25 09:29:23 -0700220 listener->mss =
221 lcl->mss ? lcl->mss : udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000222 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000223 lcl_ext = (transport_endpoint_cfg_t *) lcl;
224 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
225 listener->flags |= UDP_CONN_F_CONNECTED;
226 else
227 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700228 clib_spinlock_init (&listener->rx_lock);
Florin Corasf8ee39f2022-10-18 18:37:56 -0700229 if (!um->csum_offload)
230 listener->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700231
Florin Corasc8e41102022-03-18 10:27:29 -0700232 udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700233 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500234}
235
Florin Corasc98ef752020-04-07 17:30:13 +0000236static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700237udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500238{
Florin Corasa0396202020-04-01 00:11:16 +0000239 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500240 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000241
Dave Barach68b0fb02017-02-28 15:15:56 -0500242 listener = udp_listener_get (listener_index);
Florin Corasa0396202020-04-01 00:11:16 +0000243 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
244 listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800245 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000246 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500247 return 0;
248}
249
Florin Corasc98ef752020-04-07 17:30:13 +0000250static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500251udp_session_get_listener (u32 listener_index)
252{
253 udp_connection_t *us;
254
255 us = udp_listener_get (listener_index);
256 return &us->connection;
257}
258
Florin Corasf66cc802021-04-08 19:10:07 -0700259always_inline u32
260udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500261{
Florin Corasf8ee39f2022-10-18 18:37:56 -0700262 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port,
263 udp_csum_offload (uc));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700264 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Florin Coras8c1be052022-10-17 11:52:34 -0700265 /* reuse tcp medatada for now */
266 vnet_buffer (b)->tcp.connection_index = uc->c_c_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700267
Florin Corasf66cc802021-04-08 19:10:07 -0700268 return 0;
269}
270
271static u32
272udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
273{
274 vlib_main_t *vm = vlib_get_main ();
275 udp_connection_t *uc;
276
277 uc = udp_connection_from_transport (tc);
278
279 while (n_bufs >= 4)
280 {
281 vlib_prefetch_buffer_header (bs[2], STORE);
282 vlib_prefetch_buffer_header (bs[3], STORE);
283
284 udp_push_one_header (vm, uc, bs[0]);
285 udp_push_one_header (vm, uc, bs[1]);
286
287 n_bufs -= 2;
288 bs += 2;
289 }
290 while (n_bufs)
291 {
292 if (n_bufs > 1)
293 vlib_prefetch_buffer_header (bs[1], STORE);
294
295 udp_push_one_header (vm, uc, bs[0]);
296
297 n_bufs -= 1;
298 bs += 1;
299 }
300
Florin Corasc7542392019-07-22 08:08:43 -0700301 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
302 {
303 if (!transport_max_tx_dequeue (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700304 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700305 }
306
Florin Coras3cbc04b2017-10-02 00:18:51 -0700307 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500308}
309
Florin Corasc98ef752020-04-07 17:30:13 +0000310static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700311udp_session_get (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500312{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700313 udp_connection_t *uc;
314 uc = udp_connection_get (connection_index, thread_index);
315 if (uc)
316 return &uc->connection;
317 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500318}
319
Florin Corasc98ef752020-04-07 17:30:13 +0000320static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700321udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500322{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700323 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700324
Florin Coras3cbc04b2017-10-02 00:18:51 -0700325 uc = udp_connection_get (connection_index, thread_index);
Florin Coras02000442021-11-16 12:45:43 -0800326 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
Florin Corasc7542392019-07-22 08:08:43 -0700327 return;
328
329 if (!transport_max_tx_dequeue (&uc->connection))
Florin Coras1c29dfb2022-10-24 18:46:20 -0700330 udp_connection_program_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700331 else
332 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700333}
334
Florin Corasc98ef752020-04-07 17:30:13 +0000335static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700336udp_session_cleanup (u32 connection_index, u32 thread_index)
337{
338 udp_connection_t *uc;
339 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000340 if (!uc)
341 return;
342 if (uc->flags & UDP_CONN_F_MIGRATED)
343 udp_connection_free (uc);
344 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000345 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500346}
347
Florin Coras70f879d2020-03-13 17:54:42 +0000348static int
349udp_session_send_params (transport_connection_t * tconn,
350 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500351{
Florin Corasba78e232020-04-06 21:28:59 +0000352 udp_connection_t *uc;
353
Florin Corase759bb52020-04-08 01:55:39 +0000354 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000355
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000357 sp->snd_space = ~0;
358 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000359 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000360 sp->tx_offset = 0;
361 sp->flags = 0;
362 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500363}
364
Florin Corasc98ef752020-04-07 17:30:13 +0000365static int
Florin Coras5665ced2018-10-25 18:03:45 -0700366udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500367{
Florin Corasba78e232020-04-06 21:28:59 +0000368 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700369 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000370 udp_connection_t *uc;
Florin Corasc8e41102022-03-18 10:27:29 -0700371 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700372 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700373 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700374
Florin Coras00e01d32019-10-21 16:07:46 -0700375 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
376 &lcl_port);
377 if (rv)
Florin Coras57660d92020-04-04 22:45:34 +0000378 {
379 if (rv != SESSION_E_PORTINUSE)
380 return rv;
381
382 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
383 return SESSION_E_PORTINUSE;
384
385 /* If port in use, check if 5-tuple is also in use */
386 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
387 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
388 rmt->is_ip4))
389 return SESSION_E_PORTINUSE;
390
391 /* 5-tuple is available so increase lcl endpoint refcount and proceed
392 * with connection allocation */
393 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
394 lcl_port);
395 goto conn_alloc;
396 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700397
Florin Corasa0396202020-04-01 00:11:16 +0000398 if (udp_is_valid_dst_port (lcl_port, 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 Coras00e01d32019-10-21 16:07:46 -0700402 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000403
404 /* Try to find a port that's not used */
405 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700406 {
Florin Corasa0396202020-04-01 00:11:16 +0000407 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
408 &lcl_addr);
409 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700410 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700411 }
412 }
413
Florin Coras57660d92020-04-04 22:45:34 +0000414conn_alloc:
415
Florin Corasc8e41102022-03-18 10:27:29 -0700416 udp_connection_register_port (lcl_port, rmt->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700417
Florin Coras40903ac2018-06-10 14:41:23 -0700418 /* 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
Florin Coras3cbc04b2017-10-02 00:18:51 -0700421 uc = udp_connection_alloc (thread_index);
422 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
423 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
424 uc->c_rmt_port = rmt->port;
425 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
426 uc->c_is_ip4 = rmt->is_ip4;
427 uc->c_proto = TRANSPORT_PROTO_UDP;
428 uc->c_fib_index = rmt->fib_index;
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000429 uc->c_dscp = rmt->dscp;
Florin Corasc98ef752020-04-07 17:30:13 +0000430 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Steven Luongbf12efc2022-07-25 09:29:23 -0700431 if (rmt->peer.sw_if_index != ENDPOINT_INVALID_INDEX)
432 uc->sw_if_index = rmt->peer.sw_if_index;
Florin Corasc7542392019-07-22 08:08:43 -0700433 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000434 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800435 {
436 uc->flags |= UDP_CONN_F_CONNECTED;
437 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000438 else
Florin Coras0ac57822021-03-03 08:06:12 -0800439 {
440 clib_spinlock_init (&uc->rx_lock);
441 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
442 }
Florin Corasf8ee39f2022-10-18 18:37:56 -0700443 if (!um->csum_offload)
444 uc->cfg_flags |= UDP_CFG_F_NO_CSUM_OFFLOAD;
Florin Coras8c1be052022-10-17 11:52:34 -0700445 uc->next_node_index = rmt->next_node_index;
446 uc->next_node_opaque = rmt->next_node_opaque;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700447
448 return uc->c_c_index;
449}
450
Florin Corasc98ef752020-04-07 17:30:13 +0000451static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700452udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700453{
454 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700455 u32 thread_index;
456
457 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700458 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700459 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200460 if (!uc)
461 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700462 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500463}
464
Florin Corasc98ef752020-04-07 17:30:13 +0000465static u8 *
466format_udp_session (u8 * s, va_list * args)
467{
468 u32 uci = va_arg (*args, u32);
469 u32 thread_index = va_arg (*args, u32);
470 u32 verbose = va_arg (*args, u32);
471 udp_connection_t *uc;
472
473 uc = udp_connection_get (uci, thread_index);
474 return format (s, "%U", format_udp_connection, uc, verbose);
475}
476
477static u8 *
478format_udp_half_open_session (u8 * s, va_list * args)
479{
480 u32 __clib_unused tci = va_arg (*args, u32);
481 u32 __clib_unused thread_index = va_arg (*args, u32);
482 clib_warning ("BUG");
483 return 0;
484}
485
486static u8 *
487format_udp_listener_session (u8 * s, va_list * args)
488{
489 u32 tci = va_arg (*args, u32);
490 u32 __clib_unused thread_index = va_arg (*args, u32);
491 u32 verbose = va_arg (*args, u32);
492 udp_connection_t *uc = udp_listener_get (tci);
493 return format (s, "%U", format_udp_connection, uc, verbose);
494}
495
Dave Barach68b0fb02017-02-28 15:15:56 -0500496/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200497static const transport_proto_vft_t udp_proto = {
Florin Coras1ee78302019-02-05 15:51:15 -0800498 .start_listen = udp_session_bind,
499 .connect = udp_open_connection,
500 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 .push_header = udp_push_header,
502 .get_connection = udp_session_get,
503 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700504 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500505 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700506 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000507 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700508 .format_connection = format_udp_session,
509 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800510 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200511 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000512 .name = "udp",
513 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200514 .tx_type = TRANSPORT_TX_DGRAM,
515 .service_type = TRANSPORT_SERVICE_CL,
516 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700517};
518/* *INDENT-ON* */
519
Dave Barach68b0fb02017-02-28 15:15:56 -0500520static clib_error_t *
521udp_init (vlib_main_t * vm)
522{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700523 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500524 ip_main_t *im = &ip_main;
525 vlib_thread_main_t *tm = vlib_get_thread_main ();
526 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500527 ip_protocol_info_t *pi;
528
Dave Barach68b0fb02017-02-28 15:15:56 -0500529 /*
530 * Registrations
531 */
532
533 /* IP registration */
534 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
535 if (pi == 0)
536 return clib_error_return (0, "UDP protocol info AWOL");
537 pi->format_header = format_udp_header;
538 pi->unformat_pg_edit = unformat_pg_udp_header;
539
Florin Coras8c1be052022-10-17 11:52:34 -0700540 /* Register as transport with session layer */
Florin Coras561af9b2017-12-09 10:19:43 -0800541 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700542 FIB_PROTOCOL_IP4, udp4_output_node.index);
Florin Coras561af9b2017-12-09 10:19:43 -0800543 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
Florin Coras8c1be052022-10-17 11:52:34 -0700544 FIB_PROTOCOL_IP6, udp6_output_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500545
546 /*
547 * Initialize data structures
548 */
549
550 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700551 vec_validate (um->connections, num_threads - 1);
Florin Coras1c29dfb2022-10-24 18:46:20 -0700552 vec_validate (um->wrk, num_threads - 1);
Florin Corasa0396202020-04-01 00:11:16 +0000553
554 um->local_to_input_edge[UDP_IP4] =
555 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
556 um->local_to_input_edge[UDP_IP6] =
557 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
Florin Corasba78e232020-04-06 21:28:59 +0000558
559 um->default_mtu = 1500;
Florin Corasf8ee39f2022-10-18 18:37:56 -0700560 um->csum_offload = 1;
Dave Barachf8d50682019-05-14 18:01:44 -0400561 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500562}
563
Dave Barachf8d50682019-05-14 18:01:44 -0400564/* *INDENT-OFF* */
565VLIB_INIT_FUNCTION (udp_init) =
566{
567 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
568 "ip6_lookup_init"),
569};
570/* *INDENT-ON* */
571
Dave Barach68b0fb02017-02-28 15:15:56 -0500572/*
573 * fd.io coding-style-patch-verification: ON
574 *
575 * Local Variables:
576 * eval: (c-set-style "gnu")
577 * End:
578 */