FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
udp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Cisco and/or its affiliates.
3  * 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 #ifndef __included_udp_h__
16 #define __included_udp_h__
17 
18 #include <vnet/vnet.h>
19 #include <vnet/udp/udp_inlines.h>
20 #include <vnet/udp/udp_local.h>
21 #include <vnet/udp/udp_packet.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/ip/format.h>
24 
25 #include <vnet/ip/ip.h>
26 #include <vnet/session/transport.h>
27 
28 typedef enum
29 {
30 #define udp_error(n,s) UDP_ERROR_##n,
31 #include <vnet/udp/udp_error.def>
32 #undef udp_error
34 } udp_error_t;
35 
36 #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 */ \
41  _(MIGRATED, "MIGRATED") /**< cloned to another thread */ \
42 
44 {
45 #define _(sym, str) UDP_CONN_F_BIT_##sym,
47 #undef _
49 };
50 
51 typedef enum udp_conn_flags_
52 {
53 #define _(sym, str) UDP_CONN_F_##sym = 1 << UDP_CONN_F_BIT_##sym,
55 #undef _
57 
58 typedef struct
59 {
60  /** Required for pool_get_aligned */
61  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
62  transport_connection_t connection; /**< must be first */
63  clib_spinlock_t rx_lock; /**< rx fifo lock */
64  u8 flags; /**< connection flags */
65  u16 mss; /**< connection mss */
67 
68 typedef struct
69 {
70  /* Name (a c string). */
71  char *name;
72 
73  /* Port number in host byte order. */
75 
76  /* Node which handles this type. */
78 
79  /* Next index for this type. */
81 
82  /* UDP sessions refcount (not tunnels) */
83  u32 n_connections;
84 
85  /* Parser for packet generator edits for this protocol */
88 
89 typedef enum
90 {
91  UDP_IP6 = 0,
92  UDP_IP4, /* the code is full of is_ip4... */
95 
96 typedef struct
97 {
98  udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
99 
100  /* Hash tables mapping name/protocol to protocol info index. */
101  uword *dst_port_info_by_name[N_UDP_AF];
102  uword *dst_port_info_by_dst_port[N_UDP_AF];
103 
104  /* 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 
111  /* Udp local to input arc index */
112  u32 local_to_input_edge[N_UDP_AF];
113 
114  /*
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 
123  u16 default_mtu;
125 
126 extern udp_main_t udp_main;
131 
133  char *dst_port_name, u8 is_ip4);
134 
137 {
139  return 0;
140  return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
141 }
142 
144 udp_listener_get (u32 conn_index)
145 {
146  return pool_elt_at_index (udp_main.listener_pool, conn_index);
147 }
148 
151 {
152  return &udp_main;
153 }
154 
157 {
158  return ((udp_connection_t *) tc);
159 }
160 
163 {
164  return (uc - udp_main.connections[uc->c_thread_index]);
165 }
166 
169 
170 /**
171  * Acquires a lock that blocks a connection pool from expanding.
172  */
173 always_inline void
175 {
177  return;
183  [thread_index]);
184 }
185 
186 always_inline void
188 {
190  return;
196  [thread_index]);
198  [thread_index]);
199 }
200 
202 udp_connection_clone_safe (u32 connection_index, u32 thread_index)
203 {
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  */
214  old_c = udp_main.connections[thread_index] + connection_index;
215  clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
216  old_c->flags |= UDP_CONN_F_MIGRATED;
218  new_c->c_thread_index = current_thread_index;
219  new_c->c_c_index = udp_connection_index (new_c);
220  new_c->c_fib_index = old_c->c_fib_index;
221  /* Assume cloned sessions don't need lock */
222  new_c->rx_lock = 0;
223  return new_c;
224 }
225 
228 {
229  uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
230  return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
231 }
232 
238 
239 void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
240 
241 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
242 
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */
250 
251 #endif /* __included_udp_h__ */
udp_dst_port_t
udp_dst_port_t
Definition: udp_local.h:58
udp_punt_unknown
void udp_punt_unknown(vlib_main_t *vm, u8 is_ip4, u8 is_add)
Definition: udp_local.c:545
udp_connection_alloc
udp_connection_t * udp_connection_alloc(u32 thread_index)
Definition: udp.c:93
udp_connection_t::flags
u8 flags
connection flags
Definition: udp.h:65
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
udp_listener_get
static udp_connection_t * udp_listener_get(u32 conn_index)
Definition: udp.h:145
udp_dst_port_info_t
Definition: udp.h:69
format_udp_rx_trace
format_function_t format_udp_rx_trace
Definition: udp.h:235
dst_port
vl_api_ip_port_and_mask_t dst_port
Definition: flow_types.api:92
udp6_local_node
vlib_node_registration_t udp6_local_node
(constructor) VLIB_REGISTER_NODE (udp6_local_node)
Definition: udp_local.c:425
udp_main
udp_main_t udp_main
Definition: udp.c:23
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
clib_spinlock_lock_if_init
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:106
udp_add_dst_port
void udp_add_dst_port(udp_main_t *um, udp_dst_port_t dst_port, char *dst_port_name, u8 is_ip4)
Definition: udp_local.c:448
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
CLIB_CACHE_LINE_ALIGN_MARK
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
name
string name[64]
Definition: fib.api:25
UDP_CONN_N_FLAGS
@ UDP_CONN_N_FLAGS
Definition: udp.h:49
foreach_udp_connection_flag
#define foreach_udp_connection_flag
cloned to another thread
Definition: udp.h:36
udp_connection_share_port
void udp_connection_share_port(u16 lcl_port, u8 is_ip4)
Definition: udp.c:81
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
udp4_input_node
vlib_node_registration_t udp4_input_node
(constructor) VLIB_REGISTER_NODE (udp4_input_node)
Definition: udp_input.c:346
udp_af_t
udp_af_t
Definition: udp.h:90
node_index
node node_index
Definition: interface_output.c:420
udp_conn_flags_t
enum udp_conn_flags_ udp_conn_flags_t
transport_connection_t
struct _transport_connection transport_connection_t
udp_connection_t
Definition: udp.h:59
udp4_local_node
vlib_node_registration_t udp4_local_node
(constructor) VLIB_REGISTER_NODE (udp4_local_node)
Definition: udp_local.c:403
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
udp_main_t::peekers_readers_locks
clib_spinlock_t * peekers_readers_locks
Definition: udp.h:120
unformat_udp_header
unformat_function_t unformat_udp_header
Definition: udp.h:237
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
UDP_IP4
@ UDP_IP4
Definition: udp.h:93
udp_main_t::dst_port_infos
udp_dst_port_info_t * dst_port_infos[N_UDP_AF]
Definition: udp.h:99
udp_connection_free
void udp_connection_free(udp_connection_t *uc)
Definition: udp.c:123
udp_connection_get
static udp_connection_t * udp_connection_get(u32 conn_index, u32 thread_index)
Definition: udp.h:137
udp_conn_flags_
udp_conn_flags_
Definition: udp.h:52
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
format.h
format_udp_header
format_function_t format_udp_header
Definition: udp.h:234
udp_pool_remove_peeker
static void udp_pool_remove_peeker(u32 thread_index)
Definition: udp.h:188
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
clib_spinlock_s
Definition: lock.h:51
udp_main_t::listener_pool
udp_connection_t * listener_pool
Definition: udp.h:122
udp_local.h
udp_packet.h
unformat_udp_port
unformat_function_t unformat_udp_port
Definition: udp.h:238
uword
u64 uword
Definition: types.h:112
hash_get
#define hash_get(h, key)
Definition: hash.h:249
UDP_N_ERROR
@ UDP_N_ERROR
Definition: udp.h:33
udp_inlines.h
UDP_IP6
@ UDP_IP6
Definition: udp.h:92
udp_error_t
udp_error_t
Definition: udp.h:28
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
unformat_pg_edit
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
udp_pool_add_peeker
static void udp_pool_add_peeker(u32 thread_index)
Acquires a lock that blocks a connection pool from expanding.
Definition: udp.h:175
ip4_packet.h
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
format_udp_connection
format_function_t format_udp_connection
Definition: udp.h:236
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
N_UDP_AF
@ N_UDP_AF
Definition: udp.h:94
udp_connection_index
static u32 udp_connection_index(udp_connection_t *uc)
Definition: udp.h:163
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
ip.h
u32
unsigned int u32
Definition: types.h:88
udp_error.def
udp6_input_node
vlib_node_registration_t udp6_input_node
(constructor) VLIB_REGISTER_NODE (udp6_input_node)
Definition: udp_input.c:372
unformat_function_t
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:225
udp_main_t::dst_port_info_by_dst_port
uword * dst_port_info_by_dst_port[N_UDP_AF]
Definition: udp.h:103
udp_get_dst_port_info
static udp_dst_port_info_t * udp_get_dst_port_info(udp_main_t *um, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp.h:228
vlib_main_t
Definition: main.h:102
udp_main_t
Definition: udp.h:97
u8
unsigned char u8
Definition: types.h:56
transport.h
udp_main_t::connection_peekers
u32 * connection_peekers
Definition: udp.h:119
udp_connection_t::rx_lock
clib_spinlock_t rx_lock
rx fifo lock
Definition: udp.h:64
udp_conn_flags_bits
udp_conn_flags_bits
Definition: udp.h:44
udp_connection_clone_safe
static udp_connection_t * udp_connection_clone_safe(u32 connection_index, u32 thread_index)
Definition: udp.h:203
vnet.h
clib_spinlock_unlock_if_init
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:129
udp_main_t::connections
udp_connection_t ** connections
Definition: udp.h:118
udp_main_t::peekers_write_locks
clib_spinlock_t * peekers_write_locks
Definition: udp.h:121
vnet_get_udp_main
static udp_main_t * vnet_get_udp_main()
Definition: udp.h:151
udp_connection_from_transport
static udp_connection_t * udp_connection_from_transport(transport_connection_t *tc)
Definition: udp.h:157
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105