blob: 43391f9f4e5449c4eaa9c563523cd2904d8b3ace [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 Coras57660d92020-04-04 22:45:34 +0000135static u8
136udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
137{
138 udp_main_t *um = vnet_get_udp_main ();
139 udp_dst_port_info_t *pi;
140
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));
144}
145
Florin Corasc98ef752020-04-07 17:30:13 +0000146static u16
147udp_default_mtu (udp_main_t * um, u8 is_ip4)
148{
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);
151}
152
153static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800154udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500155{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700156 udp_main_t *um = vnet_get_udp_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000157 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500158 udp_connection_t *listener;
Florin Coras57a5a2d2020-04-01 23:16:11 +0000159 u16 lcl_port_ho;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700160 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500161
Florin Coras57a5a2d2020-04-01 23:16:11 +0000162 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
Florin Corasa0396202020-04-01 00:11:16 +0000163
Florin Coras57660d92020-04-04 22:45:34 +0000164 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000165 {
166 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000167 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000168 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700169
170 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400171 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700172
Florin Coras0e495682017-09-19 22:27:18 -0700173 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700174 listener->c_c_index = listener - um->listener_pool;
175
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)
178 {
179 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
180 lcl->is_ip4)))
181 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
182 }
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;
Florin Corasc98ef752020-04-07 17:30:13 +0000188 listener->mss = udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000189 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000190 lcl_ext = (transport_endpoint_cfg_t *) lcl;
191 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
192 listener->flags |= UDP_CONN_F_CONNECTED;
193 else
194 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700195 clib_spinlock_init (&listener->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700196
Florin Corasc8e41102022-03-18 10:27:29 -0700197 udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700198 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500199}
200
Florin Corasc98ef752020-04-07 17:30:13 +0000201static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700202udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500203{
Florin Corasa0396202020-04-01 00:11:16 +0000204 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500205 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000206
Dave Barach68b0fb02017-02-28 15:15:56 -0500207 listener = udp_listener_get (listener_index);
Florin Corasa0396202020-04-01 00:11:16 +0000208 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
209 listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800210 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000211 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500212 return 0;
213}
214
Florin Corasc98ef752020-04-07 17:30:13 +0000215static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500216udp_session_get_listener (u32 listener_index)
217{
218 udp_connection_t *us;
219
220 us = udp_listener_get (listener_index);
221 return &us->connection;
222}
223
Florin Corasf66cc802021-04-08 19:10:07 -0700224always_inline u32
225udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500226{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700227 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
Florin Corasf66cc802021-04-08 19:10:07 -0700228 if (uc->c_is_ip4)
Florin Corasab57edb2020-04-07 03:46:07 +0000229 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000230 IP_PROTOCOL_UDP, 1 /* csum offload */,
231 0 /* is_df */, uc->c_dscp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500232 else
Florin Corasab57edb2020-04-07 03:46:07 +0000233 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
234 IP_PROTOCOL_UDP);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700235 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700236 vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700237 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
238
Florin Corasf66cc802021-04-08 19:10:07 -0700239 return 0;
240}
241
242static u32
243udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
244{
245 vlib_main_t *vm = vlib_get_main ();
246 udp_connection_t *uc;
247
248 uc = udp_connection_from_transport (tc);
249
250 while (n_bufs >= 4)
251 {
252 vlib_prefetch_buffer_header (bs[2], STORE);
253 vlib_prefetch_buffer_header (bs[3], STORE);
254
255 udp_push_one_header (vm, uc, bs[0]);
256 udp_push_one_header (vm, uc, bs[1]);
257
258 n_bufs -= 2;
259 bs += 2;
260 }
261 while (n_bufs)
262 {
263 if (n_bufs > 1)
264 vlib_prefetch_buffer_header (bs[1], STORE);
265
266 udp_push_one_header (vm, uc, bs[0]);
267
268 n_bufs -= 1;
269 bs += 1;
270 }
271
Florin Corasc7542392019-07-22 08:08:43 -0700272 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
273 {
274 if (!transport_max_tx_dequeue (&uc->connection))
275 udp_connection_delete (uc);
276 }
277
Florin Coras3cbc04b2017-10-02 00:18:51 -0700278 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500279}
280
Florin Corasc98ef752020-04-07 17:30:13 +0000281static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700282udp_session_get (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;
285 uc = udp_connection_get (connection_index, thread_index);
286 if (uc)
287 return &uc->connection;
288 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500289}
290
Florin Corasc98ef752020-04-07 17:30:13 +0000291static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700292udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500293{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700295
Florin Coras3cbc04b2017-10-02 00:18:51 -0700296 uc = udp_connection_get (connection_index, thread_index);
Florin Coras02000442021-11-16 12:45:43 -0800297 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
Florin Corasc7542392019-07-22 08:08:43 -0700298 return;
299
300 if (!transport_max_tx_dequeue (&uc->connection))
301 udp_connection_delete (uc);
302 else
303 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700304}
305
Florin Corasc98ef752020-04-07 17:30:13 +0000306static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700307udp_session_cleanup (u32 connection_index, u32 thread_index)
308{
309 udp_connection_t *uc;
310 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000311 if (!uc)
312 return;
313 if (uc->flags & UDP_CONN_F_MIGRATED)
314 udp_connection_free (uc);
315 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000316 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500317}
318
Florin Coras70f879d2020-03-13 17:54:42 +0000319static int
320udp_session_send_params (transport_connection_t * tconn,
321 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500322{
Florin Corasba78e232020-04-06 21:28:59 +0000323 udp_connection_t *uc;
324
Florin Corase759bb52020-04-08 01:55:39 +0000325 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000326
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000328 sp->snd_space = ~0;
329 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000330 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000331 sp->tx_offset = 0;
332 sp->flags = 0;
333 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500334}
335
Florin Corasc98ef752020-04-07 17:30:13 +0000336static int
Florin Coras5665ced2018-10-25 18:03:45 -0700337udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500338{
Florin Corasba78e232020-04-06 21:28:59 +0000339 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700340 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000341 udp_connection_t *uc;
Florin Corasc8e41102022-03-18 10:27:29 -0700342 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700343 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700344 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700345
Florin Coras00e01d32019-10-21 16:07:46 -0700346 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
347 &lcl_port);
348 if (rv)
Florin Coras57660d92020-04-04 22:45:34 +0000349 {
350 if (rv != SESSION_E_PORTINUSE)
351 return rv;
352
353 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
354 return SESSION_E_PORTINUSE;
355
356 /* If port in use, check if 5-tuple is also in use */
357 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
358 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
359 rmt->is_ip4))
360 return SESSION_E_PORTINUSE;
361
362 /* 5-tuple is available so increase lcl endpoint refcount and proceed
363 * with connection allocation */
364 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
365 lcl_port);
366 goto conn_alloc;
367 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700368
Florin Corasa0396202020-04-01 00:11:16 +0000369 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700370 {
Florin Corasa0396202020-04-01 00:11:16 +0000371 /* If specific source port was requested abort */
372 if (rmt->peer.port)
Florin Coras00e01d32019-10-21 16:07:46 -0700373 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000374
375 /* Try to find a port that's not used */
376 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700377 {
Florin Corasa0396202020-04-01 00:11:16 +0000378 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
379 &lcl_addr);
380 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700381 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700382 }
383 }
384
Florin Coras57660d92020-04-04 22:45:34 +0000385conn_alloc:
386
Florin Corasc8e41102022-03-18 10:27:29 -0700387 udp_connection_register_port (lcl_port, rmt->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700388
Florin Coras40903ac2018-06-10 14:41:23 -0700389 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700390 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700391
Florin Coras3cbc04b2017-10-02 00:18:51 -0700392 uc = udp_connection_alloc (thread_index);
393 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
394 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
395 uc->c_rmt_port = rmt->port;
396 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
397 uc->c_is_ip4 = rmt->is_ip4;
398 uc->c_proto = TRANSPORT_PROTO_UDP;
399 uc->c_fib_index = rmt->fib_index;
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000400 uc->c_dscp = rmt->dscp;
Florin Corasc98ef752020-04-07 17:30:13 +0000401 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Florin Corasc7542392019-07-22 08:08:43 -0700402 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000403 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800404 {
405 uc->flags |= UDP_CONN_F_CONNECTED;
406 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000407 else
Florin Coras0ac57822021-03-03 08:06:12 -0800408 {
409 clib_spinlock_init (&uc->rx_lock);
410 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
411 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700412
413 return uc->c_c_index;
414}
415
Florin Corasc98ef752020-04-07 17:30:13 +0000416static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700417udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700418{
419 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700420 u32 thread_index;
421
422 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700423 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700424 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200425 if (!uc)
426 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700427 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500428}
429
Florin Corasc98ef752020-04-07 17:30:13 +0000430static u8 *
431format_udp_session (u8 * s, va_list * args)
432{
433 u32 uci = va_arg (*args, u32);
434 u32 thread_index = va_arg (*args, u32);
435 u32 verbose = va_arg (*args, u32);
436 udp_connection_t *uc;
437
438 uc = udp_connection_get (uci, thread_index);
439 return format (s, "%U", format_udp_connection, uc, verbose);
440}
441
442static u8 *
443format_udp_half_open_session (u8 * s, va_list * args)
444{
445 u32 __clib_unused tci = va_arg (*args, u32);
446 u32 __clib_unused thread_index = va_arg (*args, u32);
447 clib_warning ("BUG");
448 return 0;
449}
450
451static u8 *
452format_udp_listener_session (u8 * s, va_list * args)
453{
454 u32 tci = va_arg (*args, u32);
455 u32 __clib_unused thread_index = va_arg (*args, u32);
456 u32 verbose = va_arg (*args, u32);
457 udp_connection_t *uc = udp_listener_get (tci);
458 return format (s, "%U", format_udp_connection, uc, verbose);
459}
460
Dave Barach68b0fb02017-02-28 15:15:56 -0500461/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200462static const transport_proto_vft_t udp_proto = {
Florin Coras1ee78302019-02-05 15:51:15 -0800463 .start_listen = udp_session_bind,
464 .connect = udp_open_connection,
465 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500466 .push_header = udp_push_header,
467 .get_connection = udp_session_get,
468 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700469 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500470 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700471 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000472 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700473 .format_connection = format_udp_session,
474 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800475 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200476 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000477 .name = "udp",
478 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200479 .tx_type = TRANSPORT_TX_DGRAM,
480 .service_type = TRANSPORT_SERVICE_CL,
481 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700482};
483/* *INDENT-ON* */
484
Dave Barach68b0fb02017-02-28 15:15:56 -0500485static clib_error_t *
486udp_init (vlib_main_t * vm)
487{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700488 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500489 ip_main_t *im = &ip_main;
490 vlib_thread_main_t *tm = vlib_get_thread_main ();
491 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500492 ip_protocol_info_t *pi;
493
Dave Barach68b0fb02017-02-28 15:15:56 -0500494 /*
495 * Registrations
496 */
497
498 /* IP registration */
499 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
500 if (pi == 0)
501 return clib_error_return (0, "UDP protocol info AWOL");
502 pi->format_header = format_udp_header;
503 pi->unformat_pg_edit = unformat_pg_udp_header;
504
Dave Barach68b0fb02017-02-28 15:15:56 -0500505 /* Register as transport with URI */
Florin Coras561af9b2017-12-09 10:19:43 -0800506 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
507 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
508 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
509 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500510
511 /*
512 * Initialize data structures
513 */
514
515 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700516 vec_validate (um->connections, num_threads - 1);
Florin Corasa0396202020-04-01 00:11:16 +0000517
518 um->local_to_input_edge[UDP_IP4] =
519 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
520 um->local_to_input_edge[UDP_IP6] =
521 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
Florin Corasba78e232020-04-06 21:28:59 +0000522
523 um->default_mtu = 1500;
Dave Barachf8d50682019-05-14 18:01:44 -0400524 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500525}
526
Dave Barachf8d50682019-05-14 18:01:44 -0400527/* *INDENT-OFF* */
528VLIB_INIT_FUNCTION (udp_init) =
529{
530 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
531 "ip6_lookup_init"),
532};
533/* *INDENT-ON* */
534
Dave Barach68b0fb02017-02-28 15:15:56 -0500535/*
536 * fd.io coding-style-patch-verification: ON
537 *
538 * Local Variables:
539 * eval: (c-set-style "gnu")
540 * End:
541 */