misc: minimize dependencies on udp.h
[vpp.git] / src / vnet / udp / udp.h
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/ip.h>
23 #include <vnet/ip/ip4.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/format.h>
26
27 #include <vnet/ip/ip.h>
28 #include <vnet/session/transport.h>
29
30 typedef enum
31 {
32 #define udp_error(n,s) UDP_ERROR_##n,
33 #include <vnet/udp/udp_error.def>
34 #undef udp_error
35   UDP_N_ERROR,
36 } udp_error_t;
37
38 #define foreach_udp_connection_flag                                     \
39   _(CONNECTED, "CONNECTED")     /**< connected mode */                  \
40   _(OWNS_PORT, "OWNS_PORT")     /**< port belong to conn (UDPC) */      \
41   _(CLOSING, "CLOSING")         /**< conn closed with data */           \
42   _(LISTEN, "LISTEN")           /**< conn is listening */               \
43   _(MIGRATED, "MIGRATED")       /**< cloned to another thread */        \
44
45 enum udp_conn_flags_bits
46 {
47 #define _(sym, str) UDP_CONN_F_BIT_##sym,
48   foreach_udp_connection_flag
49 #undef _
50   UDP_CONN_N_FLAGS
51 };
52
53 typedef enum udp_conn_flags_
54 {
55 #define _(sym, str) UDP_CONN_F_##sym = 1 << UDP_CONN_F_BIT_##sym,
56   foreach_udp_connection_flag
57 #undef _
58 } udp_conn_flags_t;
59
60 typedef struct
61 {
62   /** Required for pool_get_aligned */
63   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
64   transport_connection_t connection;    /**< must be first */
65   clib_spinlock_t rx_lock;              /**< rx fifo lock */
66   u8 flags;                             /**< connection flags */
67   u16 mss;                              /**< connection mss */
68 } udp_connection_t;
69
70 typedef struct
71 {
72   /* Name (a c string). */
73   char *name;
74
75   /* Port number in host byte order. */
76   udp_dst_port_t dst_port;
77
78   /* Node which handles this type. */
79   u32 node_index;
80
81   /* Next index for this type. */
82   u32 next_index;
83
84   /* UDP sessions refcount (not tunnels) */
85   u32 n_connections;
86
87   /* Parser for packet generator edits for this protocol */
88   unformat_function_t *unformat_pg_edit;
89 } udp_dst_port_info_t;
90
91 typedef enum
92 {
93   UDP_IP6 = 0,
94   UDP_IP4,                      /* the code is full of is_ip4... */
95   N_UDP_AF,
96 } udp_af_t;
97
98 typedef struct
99 {
100   udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
101
102   /* Hash tables mapping name/protocol to protocol info index. */
103   uword *dst_port_info_by_name[N_UDP_AF];
104   uword *dst_port_info_by_dst_port[N_UDP_AF];
105
106   /* Sparse vector mapping udp dst_port in network byte order
107      to next index. */
108   u16 *next_by_dst_port4;
109   u16 *next_by_dst_port6;
110   u8 punt_unknown4;
111   u8 punt_unknown6;
112
113   /* Udp local to input arc index */
114   u32 local_to_input_edge[N_UDP_AF];
115
116   /*
117    * Per-worker thread udp connection pools used with session layer
118    */
119   udp_connection_t **connections;
120   u32 *connection_peekers;
121   clib_spinlock_t *peekers_readers_locks;
122   clib_spinlock_t *peekers_write_locks;
123   udp_connection_t *listener_pool;
124
125   u16 default_mtu;
126 } udp_main_t;
127
128 extern udp_main_t udp_main;
129 extern vlib_node_registration_t udp4_input_node;
130 extern vlib_node_registration_t udp6_input_node;
131 extern vlib_node_registration_t udp4_local_node;
132 extern vlib_node_registration_t udp6_local_node;
133
134 void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
135                        char *dst_port_name, u8 is_ip4);
136
137 always_inline udp_connection_t *
138 udp_connection_get (u32 conn_index, u32 thread_index)
139 {
140   if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
141     return 0;
142   return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
143 }
144
145 always_inline udp_connection_t *
146 udp_listener_get (u32 conn_index)
147 {
148   return pool_elt_at_index (udp_main.listener_pool, conn_index);
149 }
150
151 always_inline udp_main_t *
152 vnet_get_udp_main ()
153 {
154   return &udp_main;
155 }
156
157 always_inline udp_connection_t *
158 udp_connection_from_transport (transport_connection_t * tc)
159 {
160   return ((udp_connection_t *) tc);
161 }
162
163 always_inline u32
164 udp_connection_index (udp_connection_t * uc)
165 {
166   return (uc - udp_main.connections[uc->c_thread_index]);
167 }
168
169 void udp_connection_free (udp_connection_t * uc);
170 udp_connection_t *udp_connection_alloc (u32 thread_index);
171
172 /**
173  * Acquires a lock that blocks a connection pool from expanding.
174  */
175 always_inline void
176 udp_pool_add_peeker (u32 thread_index)
177 {
178   if (thread_index != vlib_get_thread_index ())
179     return;
180   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
181   udp_main.connection_peekers[thread_index] += 1;
182   if (udp_main.connection_peekers[thread_index] == 1)
183     clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]);
184   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
185                                 [thread_index]);
186 }
187
188 always_inline void
189 udp_pool_remove_peeker (u32 thread_index)
190 {
191   if (thread_index != vlib_get_thread_index ())
192     return;
193   ASSERT (udp_main.connection_peekers[thread_index] > 0);
194   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
195   udp_main.connection_peekers[thread_index] -= 1;
196   if (udp_main.connection_peekers[thread_index] == 0)
197     clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
198                                   [thread_index]);
199   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
200                                 [thread_index]);
201 }
202
203 always_inline udp_connection_t *
204 udp_connection_clone_safe (u32 connection_index, u32 thread_index)
205 {
206   udp_connection_t *old_c, *new_c;
207   u32 current_thread_index = vlib_get_thread_index ();
208   new_c = udp_connection_alloc (current_thread_index);
209
210   /* If during the memcpy pool is reallocated AND the memory allocator
211    * decides to give the old chunk of memory to somebody in a hurry to
212    * scribble something on it, we have a problem. So add this thread as
213    * a session pool peeker.
214    */
215   udp_pool_add_peeker (thread_index);
216   old_c = udp_main.connections[thread_index] + connection_index;
217   clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
218   old_c->flags |= UDP_CONN_F_MIGRATED;
219   udp_pool_remove_peeker (thread_index);
220   new_c->c_thread_index = current_thread_index;
221   new_c->c_c_index = udp_connection_index (new_c);
222   new_c->c_fib_index = old_c->c_fib_index;
223   return new_c;
224 }
225
226 always_inline udp_dst_port_info_t *
227 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
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
233 format_function_t format_udp_header;
234 format_function_t format_udp_rx_trace;
235 format_function_t format_udp_connection;
236 unformat_function_t unformat_udp_header;
237 unformat_function_t unformat_udp_port;
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__ */