blob: 1878d2ed083e9a4daa5f76e8cfd45d79c4d8fdb7 [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
26udp_connection_register_port (vlib_main_t * vm, u16 lcl_port, u8 is_ip4)
27{
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;
97 u32 will_expand = 0;
98 pool_get_aligned_will_expand (um->connections[thread_index], will_expand,
99 CLIB_CACHE_LINE_BYTES);
100
101 if (PREDICT_FALSE (will_expand))
102 {
103 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks
104 [thread_index]);
105 pool_get_aligned (udp_main.connections[thread_index], uc,
106 CLIB_CACHE_LINE_BYTES);
107 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
108 [thread_index]);
109 }
110 else
111 {
112 pool_get_aligned (um->connections[thread_index], uc,
113 CLIB_CACHE_LINE_BYTES);
114 }
Dave Barachb7b92992018-10-17 10:38:51 -0400115 clib_memset (uc, 0, sizeof (*uc));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700116 uc->c_c_index = uc - um->connections[thread_index];
117 uc->c_thread_index = thread_index;
118 uc->c_proto = TRANSPORT_PROTO_UDP;
119 return uc;
120}
121
122void
123udp_connection_free (udp_connection_t * uc)
124{
Benoît Ganned4aeb842019-07-18 18:38:42 +0200125 u32 thread_index = uc->c_thread_index;
Florin Coras30fdf392020-12-02 21:14:56 -0800126 clib_spinlock_free (&uc->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700127 if (CLIB_DEBUG)
Dave Barachb7b92992018-10-17 10:38:51 -0400128 clib_memset (uc, 0xFA, sizeof (*uc));
Benoît Ganned4aeb842019-07-18 18:38:42 +0200129 pool_put (udp_main.connections[thread_index], uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700130}
Dave Barach68b0fb02017-02-28 15:15:56 -0500131
Florin Coras57a5a2d2020-04-01 23:16:11 +0000132static void
133udp_connection_cleanup (udp_connection_t * uc)
134{
135 transport_endpoint_cleanup (TRANSPORT_PROTO_UDP, &uc->c_lcl_ip,
136 uc->c_lcl_port);
137 udp_connection_unregister_port (clib_net_to_host_u16 (uc->c_lcl_port),
138 uc->c_is_ip4);
139 udp_connection_free (uc);
140}
141
Florin Corasc7542392019-07-22 08:08:43 -0700142void
143udp_connection_delete (udp_connection_t * uc)
144{
Florin Corasc7542392019-07-22 08:08:43 -0700145 session_transport_delete_notify (&uc->connection);
Florin Coras57a5a2d2020-04-01 23:16:11 +0000146 udp_connection_cleanup (uc);
Florin Corasc7542392019-07-22 08:08:43 -0700147}
148
Florin Coras57660d92020-04-04 22:45:34 +0000149static u8
150udp_connection_port_used_extern (u16 lcl_port, u8 is_ip4)
151{
152 udp_main_t *um = vnet_get_udp_main ();
153 udp_dst_port_info_t *pi;
154
155 pi = udp_get_dst_port_info (um, lcl_port, is_ip4);
156 return (pi && !pi->n_connections
157 && udp_is_valid_dst_port (lcl_port, is_ip4));
158}
159
Florin Corasc98ef752020-04-07 17:30:13 +0000160static u16
161udp_default_mtu (udp_main_t * um, u8 is_ip4)
162{
163 u16 ip_hlen = is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
164 return (um->default_mtu - sizeof (udp_header_t) - ip_hlen);
165}
166
167static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700168udp_session_bind (u32 session_index, transport_endpoint_t * lcl)
Dave Barach68b0fb02017-02-28 15:15:56 -0500169{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700170 udp_main_t *um = vnet_get_udp_main ();
171 vlib_main_t *vm = vlib_get_main ();
Florin Coras87b7e3d2020-03-27 15:06:07 +0000172 transport_endpoint_cfg_t *lcl_ext;
Dave Barach68b0fb02017-02-28 15:15:56 -0500173 udp_connection_t *listener;
Florin Coras57a5a2d2020-04-01 23:16:11 +0000174 u16 lcl_port_ho;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700175 void *iface_ip;
Dave Barach68b0fb02017-02-28 15:15:56 -0500176
Florin Coras57a5a2d2020-04-01 23:16:11 +0000177 lcl_port_ho = clib_net_to_host_u16 (lcl->port);
Florin Corasa0396202020-04-01 00:11:16 +0000178
Florin Coras57660d92020-04-04 22:45:34 +0000179 if (udp_connection_port_used_extern (lcl_port_ho, lcl->is_ip4))
Florin Corasa0396202020-04-01 00:11:16 +0000180 {
181 clib_warning ("port already used");
Florin Coras57660d92020-04-04 22:45:34 +0000182 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000183 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700184
185 pool_get (um->listener_pool, listener);
Dave Barachb7b92992018-10-17 10:38:51 -0400186 clib_memset (listener, 0, sizeof (udp_connection_t));
Florin Coras3cbc04b2017-10-02 00:18:51 -0700187
Florin Coras0e495682017-09-19 22:27:18 -0700188 listener->c_lcl_port = lcl->port;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700189 listener->c_c_index = listener - um->listener_pool;
190
191 /* If we are provided a sw_if_index, bind using one of its ips */
192 if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX)
193 {
194 if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index,
195 lcl->is_ip4)))
196 ip_set (&lcl->ip, iface_ip, lcl->is_ip4);
197 }
198 ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4);
199 listener->c_is_ip4 = lcl->is_ip4;
200 listener->c_proto = TRANSPORT_PROTO_UDP;
201 listener->c_s_index = session_index;
202 listener->c_fib_index = lcl->fib_index;
Florin Corasc98ef752020-04-07 17:30:13 +0000203 listener->mss = udp_default_mtu (um, listener->c_is_ip4);
Florin Coras9c4ec6f2020-04-01 02:50:13 +0000204 listener->flags |= UDP_CONN_F_OWNS_PORT | UDP_CONN_F_LISTEN;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000205 lcl_ext = (transport_endpoint_cfg_t *) lcl;
206 if (lcl_ext->transport_flags & TRANSPORT_CFG_F_CONNECTED)
207 listener->flags |= UDP_CONN_F_CONNECTED;
208 else
209 listener->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700210 clib_spinlock_init (&listener->rx_lock);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700211
Florin Coras57a5a2d2020-04-01 23:16:11 +0000212 udp_connection_register_port (vm, lcl_port_ho, lcl->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700213 return listener->c_c_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500214}
215
Florin Corasc98ef752020-04-07 17:30:13 +0000216static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700217udp_session_unbind (u32 listener_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500218{
Florin Corasa0396202020-04-01 00:11:16 +0000219 udp_main_t *um = &udp_main;
Dave Barach68b0fb02017-02-28 15:15:56 -0500220 udp_connection_t *listener;
Florin Corasa0396202020-04-01 00:11:16 +0000221
Dave Barach68b0fb02017-02-28 15:15:56 -0500222 listener = udp_listener_get (listener_index);
Florin Corasa0396202020-04-01 00:11:16 +0000223 udp_connection_unregister_port (clib_net_to_host_u16 (listener->c_lcl_port),
224 listener->c_is_ip4);
Florin Coras30fdf392020-12-02 21:14:56 -0800225 clib_spinlock_free (&listener->rx_lock);
Florin Corasa0396202020-04-01 00:11:16 +0000226 pool_put (um->listener_pool, listener);
Dave Barach68b0fb02017-02-28 15:15:56 -0500227 return 0;
228}
229
Florin Corasc98ef752020-04-07 17:30:13 +0000230static transport_connection_t *
Dave Barach68b0fb02017-02-28 15:15:56 -0500231udp_session_get_listener (u32 listener_index)
232{
233 udp_connection_t *us;
234
235 us = udp_listener_get (listener_index);
236 return &us->connection;
237}
238
Florin Corasc98ef752020-04-07 17:30:13 +0000239static u32
Florin Coras3cbc04b2017-10-02 00:18:51 -0700240udp_push_header (transport_connection_t * tc, vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500241{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700242 udp_connection_t *uc;
243 vlib_main_t *vm = vlib_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500244
Florin Corase759bb52020-04-08 01:55:39 +0000245 uc = udp_connection_from_transport (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500246
Florin Coras3cbc04b2017-10-02 00:18:51 -0700247 vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1);
248 if (tc->is_ip4)
Florin Corasab57edb2020-04-07 03:46:07 +0000249 vlib_buffer_push_ip4_custom (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4,
250 IP_PROTOCOL_UDP, 1 /* csum offload */ ,
251 0 /* is_df */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500252 else
Florin Corasab57edb2020-04-07 03:46:07 +0000253 vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6,
254 IP_PROTOCOL_UDP);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700255 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700256 vnet_buffer (b)->sw_if_index[VLIB_TX] = uc->c_fib_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700257 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
258
Florin Corasc7542392019-07-22 08:08:43 -0700259 if (PREDICT_FALSE (uc->flags & UDP_CONN_F_CLOSING))
260 {
261 if (!transport_max_tx_dequeue (&uc->connection))
262 udp_connection_delete (uc);
263 }
264
Florin Coras3cbc04b2017-10-02 00:18:51 -0700265 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500266}
267
Florin Corasc98ef752020-04-07 17:30:13 +0000268static transport_connection_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700269udp_session_get (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500270{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700271 udp_connection_t *uc;
272 uc = udp_connection_get (connection_index, thread_index);
273 if (uc)
274 return &uc->connection;
275 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500276}
277
Florin Corasc98ef752020-04-07 17:30:13 +0000278static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700279udp_session_close (u32 connection_index, u32 thread_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500280{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700281 udp_connection_t *uc;
Florin Corasc7542392019-07-22 08:08:43 -0700282
Florin Coras3cbc04b2017-10-02 00:18:51 -0700283 uc = udp_connection_get (connection_index, thread_index);
Florin Corasc7542392019-07-22 08:08:43 -0700284 if (!uc)
285 return;
286
287 if (!transport_max_tx_dequeue (&uc->connection))
288 udp_connection_delete (uc);
289 else
290 uc->flags |= UDP_CONN_F_CLOSING;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700291}
292
Florin Corasc98ef752020-04-07 17:30:13 +0000293static void
Florin Coras3cbc04b2017-10-02 00:18:51 -0700294udp_session_cleanup (u32 connection_index, u32 thread_index)
295{
296 udp_connection_t *uc;
297 uc = udp_connection_get (connection_index, thread_index);
Florin Corasd85666f2020-04-03 00:58:48 +0000298 if (!uc)
299 return;
300 if (uc->flags & UDP_CONN_F_MIGRATED)
301 udp_connection_free (uc);
302 else
Florin Coras57a5a2d2020-04-01 23:16:11 +0000303 udp_connection_cleanup (uc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500304}
305
Florin Coras70f879d2020-03-13 17:54:42 +0000306static int
307udp_session_send_params (transport_connection_t * tconn,
308 transport_send_params_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500309{
Florin Corasba78e232020-04-06 21:28:59 +0000310 udp_connection_t *uc;
311
Florin Corase759bb52020-04-08 01:55:39 +0000312 uc = udp_connection_from_transport (tconn);
Florin Corasba78e232020-04-06 21:28:59 +0000313
Dave Barach68b0fb02017-02-28 15:15:56 -0500314 /* No constraint on TX window */
Florin Coras70f879d2020-03-13 17:54:42 +0000315 sp->snd_space = ~0;
316 /* TODO figure out MTU of output interface */
Florin Corasba78e232020-04-06 21:28:59 +0000317 sp->snd_mss = uc->mss;
Florin Coras70f879d2020-03-13 17:54:42 +0000318 sp->tx_offset = 0;
319 sp->flags = 0;
320 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500321}
322
Florin Corasc98ef752020-04-07 17:30:13 +0000323static int
Florin Coras5665ced2018-10-25 18:03:45 -0700324udp_open_connection (transport_endpoint_cfg_t * rmt)
Dave Barach68b0fb02017-02-28 15:15:56 -0500325{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700326 vlib_main_t *vm = vlib_get_main ();
Damjan Marion067cd622018-07-11 12:47:43 +0200327 u32 thread_index = vm->thread_index;
Florin Corasba78e232020-04-06 21:28:59 +0000328 udp_main_t *um = &udp_main;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700329 ip46_address_t lcl_addr;
Florin Coras57660d92020-04-04 22:45:34 +0000330 udp_connection_t *uc;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700331 u16 lcl_port;
Florin Coras00e01d32019-10-21 16:07:46 -0700332 int rv;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700333
Florin Coras00e01d32019-10-21 16:07:46 -0700334 rv = transport_alloc_local_endpoint (TRANSPORT_PROTO_UDP, rmt, &lcl_addr,
335 &lcl_port);
336 if (rv)
Florin Coras57660d92020-04-04 22:45:34 +0000337 {
338 if (rv != SESSION_E_PORTINUSE)
339 return rv;
340
341 if (udp_connection_port_used_extern (lcl_port, rmt->is_ip4))
342 return SESSION_E_PORTINUSE;
343
344 /* If port in use, check if 5-tuple is also in use */
345 if (session_lookup_connection (rmt->fib_index, &lcl_addr, &rmt->ip,
346 lcl_port, rmt->port, TRANSPORT_PROTO_UDP,
347 rmt->is_ip4))
348 return SESSION_E_PORTINUSE;
349
350 /* 5-tuple is available so increase lcl endpoint refcount and proceed
351 * with connection allocation */
352 transport_share_local_endpoint (TRANSPORT_PROTO_UDP, &lcl_addr,
353 lcl_port);
354 goto conn_alloc;
355 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700356
Florin Corasa0396202020-04-01 00:11:16 +0000357 if (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700358 {
Florin Corasa0396202020-04-01 00:11:16 +0000359 /* If specific source port was requested abort */
360 if (rmt->peer.port)
Florin Coras00e01d32019-10-21 16:07:46 -0700361 return SESSION_E_PORTINUSE;
Florin Corasa0396202020-04-01 00:11:16 +0000362
363 /* Try to find a port that's not used */
364 while (udp_is_valid_dst_port (lcl_port, rmt->is_ip4))
Florin Coras3cbc04b2017-10-02 00:18:51 -0700365 {
Florin Corasa0396202020-04-01 00:11:16 +0000366 lcl_port = transport_alloc_local_port (TRANSPORT_PROTO_UDP,
367 &lcl_addr);
368 if (lcl_port < 1)
Florin Coras00e01d32019-10-21 16:07:46 -0700369 return SESSION_E_PORTINUSE;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700370 }
371 }
372
Florin Coras57660d92020-04-04 22:45:34 +0000373conn_alloc:
374
Florin Corasa0396202020-04-01 00:11:16 +0000375 udp_connection_register_port (vm, lcl_port, rmt->is_ip4);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700376
Florin Coras40903ac2018-06-10 14:41:23 -0700377 /* We don't poll main thread if we have workers */
378 if (vlib_num_workers ())
379 thread_index = 1;
380
Florin Coras3cbc04b2017-10-02 00:18:51 -0700381 uc = udp_connection_alloc (thread_index);
382 ip_copy (&uc->c_rmt_ip, &rmt->ip, rmt->is_ip4);
383 ip_copy (&uc->c_lcl_ip, &lcl_addr, rmt->is_ip4);
384 uc->c_rmt_port = rmt->port;
385 uc->c_lcl_port = clib_host_to_net_u16 (lcl_port);
386 uc->c_is_ip4 = rmt->is_ip4;
387 uc->c_proto = TRANSPORT_PROTO_UDP;
388 uc->c_fib_index = rmt->fib_index;
Florin Corasc98ef752020-04-07 17:30:13 +0000389 uc->mss = rmt->mss ? rmt->mss : udp_default_mtu (um, uc->c_is_ip4);
Florin Corasc7542392019-07-22 08:08:43 -0700390 uc->flags |= UDP_CONN_F_OWNS_PORT;
Florin Coras87b7e3d2020-03-27 15:06:07 +0000391 if (rmt->transport_flags & TRANSPORT_CFG_F_CONNECTED)
Florin Coras0ac57822021-03-03 08:06:12 -0800392 {
393 uc->flags |= UDP_CONN_F_CONNECTED;
394 }
Florin Coras87b7e3d2020-03-27 15:06:07 +0000395 else
Florin Coras0ac57822021-03-03 08:06:12 -0800396 {
397 clib_spinlock_init (&uc->rx_lock);
398 uc->c_flags |= TRANSPORT_CONNECTION_F_CLESS;
399 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700400
401 return uc->c_c_index;
402}
403
Florin Corasc98ef752020-04-07 17:30:13 +0000404static transport_connection_t *
Florin Coras40903ac2018-06-10 14:41:23 -0700405udp_session_get_half_open (u32 conn_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700406{
407 udp_connection_t *uc;
Florin Coras40903ac2018-06-10 14:41:23 -0700408 u32 thread_index;
409
410 /* We don't poll main thread if we have workers */
411 thread_index = vlib_num_workers ()? 1 : 0;
412 uc = udp_connection_get (conn_index, thread_index);
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200413 if (!uc)
414 return 0;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700415 return &uc->connection;
Dave Barach68b0fb02017-02-28 15:15:56 -0500416}
417
Florin Corasc98ef752020-04-07 17:30:13 +0000418static u8 *
419format_udp_session (u8 * s, va_list * args)
420{
421 u32 uci = va_arg (*args, u32);
422 u32 thread_index = va_arg (*args, u32);
423 u32 verbose = va_arg (*args, u32);
424 udp_connection_t *uc;
425
426 uc = udp_connection_get (uci, thread_index);
427 return format (s, "%U", format_udp_connection, uc, verbose);
428}
429
430static u8 *
431format_udp_half_open_session (u8 * s, va_list * args)
432{
433 u32 __clib_unused tci = va_arg (*args, u32);
434 u32 __clib_unused thread_index = va_arg (*args, u32);
435 clib_warning ("BUG");
436 return 0;
437}
438
439static u8 *
440format_udp_listener_session (u8 * s, va_list * args)
441{
442 u32 tci = va_arg (*args, u32);
443 u32 __clib_unused thread_index = va_arg (*args, u32);
444 u32 verbose = va_arg (*args, u32);
445 udp_connection_t *uc = udp_listener_get (tci);
446 return format (s, "%U", format_udp_connection, uc, verbose);
447}
448
Dave Barach68b0fb02017-02-28 15:15:56 -0500449/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200450static const transport_proto_vft_t udp_proto = {
Florin Coras1ee78302019-02-05 15:51:15 -0800451 .start_listen = udp_session_bind,
452 .connect = udp_open_connection,
453 .stop_listen = udp_session_unbind,
Dave Barach68b0fb02017-02-28 15:15:56 -0500454 .push_header = udp_push_header,
455 .get_connection = udp_session_get,
456 .get_listener = udp_session_get_listener,
Florin Coras40903ac2018-06-10 14:41:23 -0700457 .get_half_open = udp_session_get_half_open,
Dave Barach68b0fb02017-02-28 15:15:56 -0500458 .close = udp_session_close,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700459 .cleanup = udp_session_cleanup,
Florin Coras70f879d2020-03-13 17:54:42 +0000460 .send_params = udp_session_send_params,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700461 .format_connection = format_udp_session,
462 .format_half_open = format_udp_half_open_session,
Florin Coras371ca502018-02-21 12:07:41 -0800463 .format_listener = format_udp_listener_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200464 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000465 .name = "udp",
466 .short_name = "U",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200467 .tx_type = TRANSPORT_TX_DGRAM,
468 .service_type = TRANSPORT_SERVICE_CL,
469 },
Florin Coras7fb0fe12018-04-09 09:24:52 -0700470};
471/* *INDENT-ON* */
472
Dave Barach68b0fb02017-02-28 15:15:56 -0500473static clib_error_t *
474udp_init (vlib_main_t * vm)
475{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700476 udp_main_t *um = vnet_get_udp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -0500477 ip_main_t *im = &ip_main;
478 vlib_thread_main_t *tm = vlib_get_thread_main ();
479 u32 num_threads;
Dave Barach68b0fb02017-02-28 15:15:56 -0500480 ip_protocol_info_t *pi;
Florin Coras29ca16f2017-11-02 18:14:17 -0400481 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500482
Dave Barach68b0fb02017-02-28 15:15:56 -0500483 /*
484 * Registrations
485 */
486
487 /* IP registration */
488 pi = ip_get_protocol_info (im, IP_PROTOCOL_UDP);
489 if (pi == 0)
490 return clib_error_return (0, "UDP protocol info AWOL");
491 pi->format_header = format_udp_header;
492 pi->unformat_pg_edit = unformat_pg_udp_header;
493
Dave Barach68b0fb02017-02-28 15:15:56 -0500494 /* Register as transport with URI */
Florin Coras561af9b2017-12-09 10:19:43 -0800495 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
496 FIB_PROTOCOL_IP4, ip4_lookup_node.index);
497 transport_register_protocol (TRANSPORT_PROTO_UDP, &udp_proto,
498 FIB_PROTOCOL_IP6, ip6_lookup_node.index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500499
500 /*
501 * Initialize data structures
502 */
503
504 num_threads = 1 /* main thread */ + tm->n_threads;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700505 vec_validate (um->connections, num_threads - 1);
506 vec_validate (um->connection_peekers, num_threads - 1);
507 vec_validate (um->peekers_readers_locks, num_threads - 1);
508 vec_validate (um->peekers_write_locks, num_threads - 1);
Florin Coras29ca16f2017-11-02 18:14:17 -0400509
510 if (num_threads > 1)
511 for (i = 0; i < num_threads; i++)
512 {
513 clib_spinlock_init (&um->peekers_readers_locks[i]);
514 clib_spinlock_init (&um->peekers_write_locks[i]);
515 }
Florin Corasa0396202020-04-01 00:11:16 +0000516
517 um->local_to_input_edge[UDP_IP4] =
518 vlib_node_add_next (vm, udp4_local_node.index, udp4_input_node.index);
519 um->local_to_input_edge[UDP_IP6] =
520 vlib_node_add_next (vm, udp6_local_node.index, udp6_input_node.index);
Florin Corasba78e232020-04-06 21:28:59 +0000521
522 um->default_mtu = 1500;
Dave Barachf8d50682019-05-14 18:01:44 -0400523 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500524}
525
Dave Barachf8d50682019-05-14 18:01:44 -0400526/* *INDENT-OFF* */
527VLIB_INIT_FUNCTION (udp_init) =
528{
529 .runs_after = VLIB_INITS("ip_main_init", "ip4_lookup_init",
530 "ip6_lookup_init"),
531};
532/* *INDENT-ON* */
533
Dave Barach68b0fb02017-02-28 15:15:56 -0500534/*
535 * fd.io coding-style-patch-verification: ON
536 *
537 * Local Variables:
538 * eval: (c-set-style "gnu")
539 * End:
540 */