blob: d2a2b6323f062810d5bb8727c87bfa52dc052c82 [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;
Damjan Marion66d4cb52022-03-17 18:59:46 +010097 u32 will_expand = pool_get_will_expand (um->connections[thread_index]);
Florin Coras3cbc04b2017-10-02 00:18:51 -070098
99 if (PREDICT_FALSE (will_expand))
100 {
101 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
102 [thread_index]);
103 pool_get_aligned (udp_main.connections[thread_index], uc,
104 CLIB_CACHE_LINE_BYTES);
105 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
106 [thread_index]);
107 }
108 else
109 {
110 pool_get_aligned (um->connections[thread_index], uc,
111 CLIB_CACHE_LINE_BYTES);
112 }
Dave Barachb7b92992018-10-17 10:38:51 -0400113 clib_memset (uc, 0, sizeof (*uc));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700114 uc->c_c_index = uc - um->connections[thread_index];
115 uc->c_thread_index = thread_index;
116 uc->c_proto = TRANSPORT_PROTO_UDP;
117 return uc;
118}
119
120void
121udp_connection_free (udp_connection_t * uc)
122{
Benoît Ganned4aeb842019-07-18 18:38:42 +0200123 u32 thread_index = uc->c_thread_index;
Florin Coras30fdf392020-12-02 21:14:56 -0800124 clib_spinlock_free (&uc->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700125 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400126 clib_memset (uc, 0xFA, sizeof (*uc));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200127 pool_put (udp_main.connections[thread_index], uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700128}
Dave Barach68b0fb02017-02-28 15:15:56 -0500129
Florin Coras57a5a2d2020-04-01 23:16:11 +0000130static void
131udp_connection_cleanup (udp_connection_t * uc)
132{
133 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
134 uc->c_lcl_port);
135 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
136 uc->c_is_ip4);
137 udp_connection_free (uc);
138}
139
Florin Corasc7542392019-07-22 08:08:43 -0700140void
141udp_connection_delete (udp_connection_t * uc)
142{
Florin Corasc7542392019-07-22 08:08:43 -0700143 session_transport_delete_notify (&uc->connection);
Florin Coras57a5a2d2020-04-01 23:16:11 +0000144 udp_connection_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700145}
146
Florin Coras57660d92020-04-04 22:45:34 +0000147static u8
148udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
149{
150 udp_main_t *um = vnet_get_udp_main ();
151 udp_dst_port_info_t *pi;
152
153 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
154 return (pi && !pi->n_connections
155 && udp_is_valid_dst_port (lcl_port, is_ip4));
156}
157
Florin Corasc98ef752020-04-07 17:30:13 +0000158static u16
159udp_default_mtu (udp_main_t * um, u8 is_ip4)
160{
161 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
162 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
163}
164
165static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800166udp_session_bind (u32 session_index, transport_endpoint_cfg_t *lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500167{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700168 udp_main_t *um = vnet_get_udp_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000169 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500170 udp_connection_t *listener;
Florin Coras57a5a2d2020-04-01 23:16:11 +0000171 u16 lcl_port_ho;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700172 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500173
Florin Coras57a5a2d2020-04-01 23:16:11 +0000174 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
Florin Corasa0396202020-04-01 00:11:16 +0000175
Florin Coras57660d92020-04-04 22:45:34 +0000176 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000177 {
178 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000179 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000180 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700181
182 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400183 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700184
Florin Coras0e495682017-09-19 22:27:18 -0700185 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700186 listener->c_c_index = listener - um->listener_pool;
187
188 /* If we are provided a sw_if_index, bind using one of its ips */
189 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
190 {
191 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
192 lcl->is_ip4)))
193 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
194 }
195 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
196 listener->c_is_ip4 = lcl->is_ip4;
197 listener->c_proto = TRANSPORT_PROTO_UDP;
198 listener->c_s_index = session_index;
199 listener->c_fib_index = lcl->fib_index;
Florin Corasc98ef752020-04-07 17:30:13 +0000200 listener->mss = udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000201 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000202 lcl_ext = (transport_endpoint_cfg_t *) lcl;
203 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
204 listener->flags |= UDP_CONN_F_CONNECTED;
205 else
206 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700207 clib_spinlock_init (&listener->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700208
Florin Corasc8e41102022-03-18 10:27:29 -0700209 udp_connection_register_port (lcl_port_ho, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700210 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500211}
212
Florin Corasc98ef752020-04-07 17:30:13 +0000213static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700214udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500215{
Florin Corasa0396202020-04-01 00:11:16 +0000216 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500217 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000218
Dave Barach68b0fb02017-02-28 15:15:56 -0500219 listener = udp_listener_get (listener_index);
Florin Corasa0396202020-04-01 00:11:16 +0000220 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
221 listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800222 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000223 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500224 return 0;
225}
226
Florin Corasc98ef752020-04-07 17:30:13 +0000227static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500228udp_session_get_listener (u32 listener_index)
229{
230 udp_connection_t *us;
231
232 us = udp_listener_get (listener_index);
233 return &us->connection;
234}
235
Florin Corasf66cc802021-04-08 19:10:07 -0700236always_inline u32
237udp_push_one_header (vlib_main_t *vm, udp_connection_t *uc, vlib_buffer_t *b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500238{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700239 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
Florin Corasf66cc802021-04-08 19:10:07 -0700240 if (uc->c_is_ip4)
Florin Corasab57edb2020-04-07 03:46:07 +0000241 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000242 IP_PROTOCOL_UDP, 1 /* csum offload */,
243 0 /* is_df */, uc->c_dscp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500244 else
Florin Corasab57edb2020-04-07 03:46:07 +0000245 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
246 IP_PROTOCOL_UDP);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700247 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700248 vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700249 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
250
Florin Corasf66cc802021-04-08 19:10:07 -0700251 return 0;
252}
253
254static u32
255udp_push_header (transport_connection_t *tc, vlib_buffer_t **bs, u32 n_bufs)
256{
257 vlib_main_t *vm = vlib_get_main ();
258 udp_connection_t *uc;
259
260 uc = udp_connection_from_transport (tc);
261
262 while (n_bufs >= 4)
263 {
264 vlib_prefetch_buffer_header (bs[2], STORE);
265 vlib_prefetch_buffer_header (bs[3], STORE);
266
267 udp_push_one_header (vm, uc, bs[0]);
268 udp_push_one_header (vm, uc, bs[1]);
269
270 n_bufs -= 2;
271 bs += 2;
272 }
273 while (n_bufs)
274 {
275 if (n_bufs > 1)
276 vlib_prefetch_buffer_header (bs[1], STORE);
277
278 udp_push_one_header (vm, uc, bs[0]);
279
280 n_bufs -= 1;
281 bs += 1;
282 }
283
Florin Corasc7542392019-07-22 08:08:43 -0700284 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
285 {
286 if (!transport_max_tx_dequeue (&uc->connection))
287 udp_connection_delete (uc);
288 }
289
Florin Coras3cbc04b2017-10-02 00:18:51 -0700290 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500291}
292
Florin Corasc98ef752020-04-07 17:30:13 +0000293static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294udp_session_get (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500295{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700296 udp_connection_t *uc;
297 uc = udp_connection_get (connection_index, thread_index);
298 if (uc)
299 return &uc->connection;
300 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500301}
302
Florin Corasc98ef752020-04-07 17:30:13 +0000303static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700304udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500305{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700306 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700307
Florin Coras3cbc04b2017-10-02 00:18:51 -0700308 uc = udp_connection_get (connection_index, thread_index);
Florin Coras02000442021-11-16 12:45:43 -0800309 if (!uc || (uc->flags & UDP_CONN_F_MIGRATED))
Florin Corasc7542392019-07-22 08:08:43 -0700310 return;
311
312 if (!transport_max_tx_dequeue (&uc->connection))
313 udp_connection_delete (uc);
314 else
315 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700316}
317
Florin Corasc98ef752020-04-07 17:30:13 +0000318static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700319udp_session_cleanup (u32 connection_index, u32 thread_index)
320{
321 udp_connection_t *uc;
322 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000323 if (!uc)
324 return;
325 if (uc->flags & UDP_CONN_F_MIGRATED)
326 udp_connection_free (uc);
327 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000328 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500329}
330
Florin Coras70f879d2020-03-13 17:54:42 +0000331static int
332udp_session_send_params (transport_connection_t * tconn,
333 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500334{
Florin Corasba78e232020-04-06 21:28:59 +0000335 udp_connection_t *uc;
336
Florin Corase759bb52020-04-08 01:55:39 +0000337 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000338
Dave Barach68b0fb02017-02-28 15:15:56 -0500339 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000340 sp->snd_space = ~0;
341 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000342 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000343 sp->tx_offset = 0;
344 sp->flags = 0;
345 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500346}
347
Florin Corasc98ef752020-04-07 17:30:13 +0000348static int
Florin Coras5665ced2018-10-25 18:03:45 -0700349udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500350{
Florin Corasba78e232020-04-06 21:28:59 +0000351 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700352 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000353 udp_connection_t *uc;
Florin Corasc8e41102022-03-18 10:27:29 -0700354 u32 thread_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700355 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700356 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700357
Florin Coras00e01d32019-10-21 16:07:46 -0700358 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
359 &lcl_port);
360 if (rv)
Florin Coras57660d92020-04-04 22:45:34 +0000361 {
362 if (rv != SESSION_E_PORTINUSE)
363 return rv;
364
365 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
366 return SESSION_E_PORTINUSE;
367
368 /* If port in use, check if 5-tuple is also in use */
369 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
370 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
371 rmt->is_ip4))
372 return SESSION_E_PORTINUSE;
373
374 /* 5-tuple is available so increase lcl endpoint refcount and proceed
375 * with connection allocation */
376 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
377 lcl_port);
378 goto conn_alloc;
379 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700380
Florin Corasa0396202020-04-01 00:11:16 +0000381 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700382 {
Florin Corasa0396202020-04-01 00:11:16 +0000383 /* If specific source port was requested abort */
384 if (rmt->peer.port)
Florin Coras00e01d32019-10-21 16:07:46 -0700385 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000386
387 /* Try to find a port that's not used */
388 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700389 {
Florin Corasa0396202020-04-01 00:11:16 +0000390 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
391 &lcl_addr);
392 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700393 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700394 }
395 }
396
Florin Coras57660d92020-04-04 22:45:34 +0000397conn_alloc:
398
Florin Corasc8e41102022-03-18 10:27:29 -0700399 udp_connection_register_port (lcl_port, rmt->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700400
Florin Coras40903ac2018-06-10 14:41:23 -0700401 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700402 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700403
Florin Coras3cbc04b2017-10-02 00:18:51 -0700404 uc = udp_connection_alloc (thread_index);
405 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
406 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
407 uc->c_rmt_port = rmt->port;
408 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
409 uc->c_is_ip4 = rmt->is_ip4;
410 uc->c_proto = TRANSPORT_PROTO_UDP;
411 uc->c_fib_index = rmt->fib_index;
Filip Tehlar3ef8bf32021-10-21 14:07:31 +0000412 uc->c_dscp = rmt->dscp;
Florin Corasc98ef752020-04-07 17:30:13 +0000413 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Florin Corasc7542392019-07-22 08:08:43 -0700414 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000415 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800416 {
417 uc->flags |= UDP_CONN_F_CONNECTED;
418 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000419 else
Florin Coras0ac57822021-03-03 08:06:12 -0800420 {
421 clib_spinlock_init (&uc->rx_lock);
422 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
423 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700424
425 return uc->c_c_index;
426}
427
Florin Corasc98ef752020-04-07 17:30:13 +0000428static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700429udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700430{
431 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700432 u32 thread_index;
433
434 /* We don't poll main thread if we have workers */
Florin Corasfb50bc32021-05-18 00:28:59 -0700435 thread_index = transport_cl_thread ();
Florin Coras40903ac2018-06-10 14:41:23 -0700436 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200437 if (!uc)
438 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700439 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500440}
441
Florin Corasc98ef752020-04-07 17:30:13 +0000442static u8 *
443format_udp_session (u8 * s, va_list * args)
444{
445 u32 uci = va_arg (*args, u32);
446 u32 thread_index = va_arg (*args, u32);
447 u32 verbose = va_arg (*args, u32);
448 udp_connection_t *uc;
449
450 uc = udp_connection_get (uci, thread_index);
451 return format (s, "%U", format_udp_connection, uc, verbose);
452}
453
454static u8 *
455format_udp_half_open_session (u8 * s, va_list * args)
456{
457 u32 __clib_unused tci = va_arg (*args, u32);
458 u32 __clib_unused thread_index = va_arg (*args, u32);
459 clib_warning ("BUG");
460 return 0;
461}
462
463static u8 *
464format_udp_listener_session (u8 * s, va_list * args)
465{
466 u32 tci = va_arg (*args, u32);
467 u32 __clib_unused thread_index = va_arg (*args, u32);
468 u32 verbose = va_arg (*args, u32);
469 udp_connection_t *uc = udp_listener_get (tci);
470 return format (s, "%U", format_udp_connection, uc, verbose);
471}
472
Dave Barach68b0fb02017-02-28 15:15:56 -0500473/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200474static const transport_proto_vft_t udp_proto = {
Florin Coras1ee78302019-02-05 15:51:15 -0800475 .start_listen = udp_session_bind,
476 .connect = udp_open_connection,
477 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500478 .push_header = udp_push_header,
479 .get_connection = udp_session_get,
480 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700481 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700483 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000484 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700485 .format_connection = format_udp_session,
486 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800487 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200488 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000489 .name = "udp",
490 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200491 .tx_type = TRANSPORT_TX_DGRAM,
492 .service_type = TRANSPORT_SERVICE_CL,
493 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700494};
495/* *INDENT-ON* */
496
Dave Barach68b0fb02017-02-28 15:15:56 -0500497static clib_error_t *
498udp_init (vlib_main_t * vm)
499{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700500 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 ip_main_t *im = &ip_main;
502 vlib_thread_main_t *tm = vlib_get_thread_main ();
503 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500504 ip_protocol_info_t *pi;
Florin Coras29ca16f2017-11-02 18:14:17 -0400505 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500506
Dave Barach68b0fb02017-02-28 15:15:56 -0500507 /*
508 * Registrations
509 */
510
511 /* IP registration */
512 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
513 if (pi == 0)
514 return clib_error_return (0, "UDP protocol info AWOL");
515 pi->format_header = format_udp_header;
516 pi->unformat_pg_edit = unformat_pg_udp_header;
517
Dave Barach68b0fb02017-02-28 15:15:56 -0500518 /* Register as transport with URI */
Florin Coras561af9b2017-12-09 10:19:43 -0800519 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
520 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
521 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
522 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500523
524 /*
525 * Initialize data structures
526 */
527
528 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700529 vec_validate (um->connections, num_threads - 1);
530 vec_validate (um->connection_peekers, num_threads - 1);
531 vec_validate (um->peekers_readers_locks, num_threads - 1);
532 vec_validate (um->peekers_write_locks, num_threads - 1);
Florin Coras29ca16f2017-11-02 18:14:17 -0400533
534 if (num_threads > 1)
535 for (i = 0; i < num_threads; i++)
536 {
537 clib_spinlock_init (&um->peekers_readers_locks[i]);
538 clib_spinlock_init (&um->peekers_write_locks[i]);
539 }
Florin Corasa0396202020-04-01 00:11:16 +0000540
541 um->local_to_input_edge[UDP_IP4] =
542 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
543 um->local_to_input_edge[UDP_IP6] =
544 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
Florin Corasba78e232020-04-06 21:28:59 +0000545
546 um->default_mtu = 1500;
Dave Barachf8d50682019-05-14 18:01:44 -0400547 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500548}
549
Dave Barachf8d50682019-05-14 18:01:44 -0400550/* *INDENT-OFF* */
551VLIB_INIT_FUNCTION (udp_init) =
552{
553 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
554 "ip6_lookup_init"),
555};
556/* *INDENT-ON* */
557
Dave Barach68b0fb02017-02-28 15:15:56 -0500558/*
559 * fd.io coding-style-patch-verification: ON
560 *
561 * Local Variables:
562 * eval: (c-set-style "gnu")
563 * End:
564 */