blob: 1494b33a8c35efc69ed6f2b3e412f2d159060ff7 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Florin Corasc98ef752020-04-07 17:30:13 +00002 * Copyright (c) 2017-2020 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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 */
Dave Barach68b0fb02017-02-28 15:15:56 -050015#ifndef __included_udp_h__
16#define __included_udp_h__
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
18#include <vnet/vnet.h>
Florin Corasb040f982020-10-20 14:59:43 -070019#include <vnet/udp/udp_inlines.h>
20#include <vnet/udp/udp_local.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/udp/udp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vnet/ip/ip4_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#include <vnet/ip/format.h>
24
Dave Barach68b0fb02017-02-28 15:15:56 -050025#include <vnet/ip/ip.h>
26#include <vnet/session/transport.h>
27
Dave Barachd7cb1b52016-12-09 09:52:16 -050028typedef enum
29{
Ed Warnickecb9cada2015-12-08 15:45:58 -070030#define udp_error(n,s) UDP_ERROR_##n,
Dave Barach68b0fb02017-02-28 15:15:56 -050031#include <vnet/udp/udp_error.def>
Ed Warnickecb9cada2015-12-08 15:45:58 -070032#undef udp_error
33 UDP_N_ERROR,
34} udp_error_t;
35
Florin Coras9c4ec6f2020-04-01 02:50:13 +000036#define foreach_udp_connection_flag \
37 _(CONNECTED, "CONNECTED") /**< connected mode */ \
38 _(OWNS_PORT, "OWNS_PORT") /**< port belong to conn (UDPC) */ \
39 _(CLOSING, "CLOSING") /**< conn closed with data */ \
40 _(LISTEN, "LISTEN") /**< conn is listening */ \
Florin Corasd85666f2020-04-03 00:58:48 +000041 _(MIGRATED, "MIGRATED") /**< cloned to another thread */ \
Florin Coras9c4ec6f2020-04-01 02:50:13 +000042
43enum udp_conn_flags_bits
Florin Corasc7542392019-07-22 08:08:43 -070044{
Florin Coras9c4ec6f2020-04-01 02:50:13 +000045#define _(sym, str) UDP_CONN_F_BIT_##sym,
46 foreach_udp_connection_flag
47#undef _
48 UDP_CONN_N_FLAGS
49};
50
51typedef enum udp_conn_flags_
52{
53#define _(sym, str) UDP_CONN_F_##sym = 1 << UDP_CONN_F_BIT_##sym,
54 foreach_udp_connection_flag
55#undef _
Florin Corasc7542392019-07-22 08:08:43 -070056} udp_conn_flags_t;
57
Florin Coras3cbc04b2017-10-02 00:18:51 -070058typedef struct
59{
Dave Baracheb987d32018-05-03 08:26:39 -040060 /** Required for pool_get_aligned */
61 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Florin Coras7fb0fe12018-04-09 09:24:52 -070062 transport_connection_t connection; /**< must be first */
63 clib_spinlock_t rx_lock; /**< rx fifo lock */
Florin Corasc7542392019-07-22 08:08:43 -070064 u8 flags; /**< connection flags */
Florin Corasba78e232020-04-06 21:28:59 +000065 u16 mss; /**< connection mss */
Florin Coras3cbc04b2017-10-02 00:18:51 -070066} udp_connection_t;
67
Dave Barachd7cb1b52016-12-09 09:52:16 -050068typedef struct
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* Name (a c string). */
Dave Barachd7cb1b52016-12-09 09:52:16 -050071 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Florin Corasa0396202020-04-01 00:11:16 +000073 /* Port number in host byte order. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 udp_dst_port_t dst_port;
75
76 /* Node which handles this type. */
77 u32 node_index;
78
79 /* Next index for this type. */
80 u32 next_index;
Kingwel Xie4af47092018-10-26 23:42:15 -040081
Florin Corasa0396202020-04-01 00:11:16 +000082 /* UDP sessions refcount (not tunnels) */
83 u32 n_connections;
84
Kingwel Xie4af47092018-10-26 23:42:15 -040085 /* Parser for packet generator edits for this protocol */
86 unformat_function_t *unformat_pg_edit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087} udp_dst_port_info_t;
88
Dave Barachd7cb1b52016-12-09 09:52:16 -050089typedef enum
90{
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 UDP_IP6 = 0,
Dave Barachd7cb1b52016-12-09 09:52:16 -050092 UDP_IP4, /* the code is full of is_ip4... */
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 N_UDP_AF,
94} udp_af_t;
95
Dave Barachd7cb1b52016-12-09 09:52:16 -050096typedef struct
97{
98 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 /* Hash tables mapping name/protocol to protocol info index. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500101 uword *dst_port_info_by_name[N_UDP_AF];
102 uword *dst_port_info_by_dst_port[N_UDP_AF];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
Damjan Marion615fc612017-04-03 14:56:08 +0200104 /* Sparse vector mapping udp dst_port in network byte order
105 to next index. */
106 u16 *next_by_dst_port4;
107 u16 *next_by_dst_port6;
108 u8 punt_unknown4;
109 u8 punt_unknown6;
110
Florin Corasa0396202020-04-01 00:11:16 +0000111 /* Udp local to input arc index */
112 u32 local_to_input_edge[N_UDP_AF];
113
Florin Coras3cbc04b2017-10-02 00:18:51 -0700114 /*
115 * Per-worker thread udp connection pools used with session layer
116 */
117 udp_connection_t **connections;
118 u32 *connection_peekers;
119 clib_spinlock_t *peekers_readers_locks;
120 clib_spinlock_t *peekers_write_locks;
121 udp_connection_t *listener_pool;
122
Florin Corasba78e232020-04-06 21:28:59 +0000123 u16 default_mtu;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124} udp_main_t;
125
Florin Coras3cbc04b2017-10-02 00:18:51 -0700126extern udp_main_t udp_main;
127extern vlib_node_registration_t udp4_input_node;
128extern vlib_node_registration_t udp6_input_node;
Filip Tehlar2c49ffe2019-03-06 07:16:08 -0800129extern vlib_node_registration_t udp4_local_node;
130extern vlib_node_registration_t udp6_local_node;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700131
Florin Corasb040f982020-10-20 14:59:43 -0700132void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
133 char *dst_port_name, u8 is_ip4);
134
Florin Coras3cbc04b2017-10-02 00:18:51 -0700135always_inline udp_connection_t *
136udp_connection_get (u32 conn_index, u32 thread_index)
137{
138 if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
139 return 0;
140 return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
141}
142
143always_inline udp_connection_t *
144udp_listener_get (u32 conn_index)
145{
146 return pool_elt_at_index (udp_main.listener_pool, conn_index);
147}
148
149always_inline udp_main_t *
150vnet_get_udp_main ()
151{
152 return &udp_main;
153}
154
155always_inline udp_connection_t *
Florin Corase759bb52020-04-08 01:55:39 +0000156udp_connection_from_transport (transport_connection_t * tc)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700157{
158 return ((udp_connection_t *) tc);
159}
160
161always_inline u32
162udp_connection_index (udp_connection_t * uc)
163{
164 return (uc - udp_main.connections[uc->c_thread_index]);
165}
166
Florin Corase759bb52020-04-08 01:55:39 +0000167void udp_connection_free (udp_connection_t * uc);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700168udp_connection_t *udp_connection_alloc (u32 thread_index);
169
170/**
171 * Acquires a lock that blocks a connection pool from expanding.
172 */
173always_inline void
174udp_pool_add_peeker (u32 thread_index)
175{
176 if (thread_index != vlib_get_thread_index ())
177 return;
178 clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
179 udp_main.connection_peekers[thread_index] += 1;
180 if (udp_main.connection_peekers[thread_index] == 1)
181 clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]);
182 clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
183 [thread_index]);
184}
185
186always_inline void
187udp_pool_remove_peeker (u32 thread_index)
188{
189 if (thread_index != vlib_get_thread_index ())
190 return;
191 ASSERT (udp_main.connection_peekers[thread_index] > 0);
192 clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
193 udp_main.connection_peekers[thread_index] -= 1;
194 if (udp_main.connection_peekers[thread_index] == 0)
195 clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
196 [thread_index]);
197 clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
198 [thread_index]);
199}
200
201always_inline udp_connection_t *
Florin Coras7fb0fe12018-04-09 09:24:52 -0700202udp_connection_clone_safe (u32 connection_index, u32 thread_index)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700203{
204 udp_connection_t *old_c, *new_c;
205 u32 current_thread_index = vlib_get_thread_index ();
206 new_c = udp_connection_alloc (current_thread_index);
207
208 /* If during the memcpy pool is reallocated AND the memory allocator
209 * decides to give the old chunk of memory to somebody in a hurry to
210 * scribble something on it, we have a problem. So add this thread as
211 * a session pool peeker.
212 */
213 udp_pool_add_peeker (thread_index);
214 old_c = udp_main.connections[thread_index] + connection_index;
Dave Barach178cf492018-11-13 16:34:13 -0500215 clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
Florin Corasd85666f2020-04-03 00:58:48 +0000216 old_c->flags |= UDP_CONN_F_MIGRATED;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700217 udp_pool_remove_peeker (thread_index);
218 new_c->c_thread_index = current_thread_index;
219 new_c->c_c_index = udp_connection_index (new_c);
Nathan Skrzypczak161638f2019-05-13 16:25:50 +0200220 new_c->c_fib_index = old_c->c_fib_index;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700221 return new_c;
222}
223
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224always_inline udp_dst_port_info_t *
225udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
226{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500227 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
229}
230
231format_function_t format_udp_header;
232format_function_t format_udp_rx_trace;
Florin Corasc98ef752020-04-07 17:30:13 +0000233format_function_t format_udp_connection;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234unformat_function_t unformat_udp_header;
Elias Rudberg2dca1802020-05-27 01:03:46 +0200235unformat_function_t unformat_udp_port;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Florin Corasa0396202020-04-01 00:11:16 +0000237void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
238
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
Alexander Popovsky (apopovsk)740bcdb2016-11-15 15:36:23 -0800240
Dave Barachd7cb1b52016-12-09 09:52:16 -0500241/*
242 * fd.io coding-style-patch-verification: ON
243 *
244 * Local Variables:
245 * eval: (c-set-style "gnu")
246 * End:
247 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500248
249#endif /* __included_udp_h__ */