dc3f8f5bfa177f952af64504c88a7b5461adf0fc
[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/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
33   UDP_N_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
43 enum udp_conn_flags_bits
44 {
45 #define _(sym, str) UDP_CONN_F_BIT_##sym,
46   foreach_udp_connection_flag
47 #undef _
48   UDP_CONN_N_FLAGS
49 };
50
51 typedef 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 _
56 } udp_conn_flags_t;
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 */
66 } udp_connection_t;
67
68 typedef struct
69 {
70   /* Name (a c string). */
71   char *name;
72
73   /* Port number in host byte order. */
74   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;
81
82   /* UDP sessions refcount (not tunnels) */
83   u32 n_connections;
84
85   /* Parser for packet generator edits for this protocol */
86   unformat_function_t *unformat_pg_edit;
87 } udp_dst_port_info_t;
88
89 typedef enum
90 {
91   UDP_IP6 = 0,
92   UDP_IP4,                      /* the code is full of is_ip4... */
93   N_UDP_AF,
94 } udp_af_t;
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;
124   u16 msg_id_base;
125 } udp_main_t;
126
127 extern udp_main_t udp_main;
128 extern vlib_node_registration_t udp4_input_node;
129 extern vlib_node_registration_t udp6_input_node;
130 extern vlib_node_registration_t udp4_local_node;
131 extern vlib_node_registration_t udp6_local_node;
132
133 void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
134                        char *dst_port_name, u8 is_ip4);
135
136 always_inline udp_connection_t *
137 udp_connection_get (u32 conn_index, u32 thread_index)
138 {
139   if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
140     return 0;
141   return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
142 }
143
144 always_inline udp_connection_t *
145 udp_listener_get (u32 conn_index)
146 {
147   return pool_elt_at_index (udp_main.listener_pool, conn_index);
148 }
149
150 always_inline udp_main_t *
151 vnet_get_udp_main ()
152 {
153   return &udp_main;
154 }
155
156 always_inline udp_connection_t *
157 udp_connection_from_transport (transport_connection_t * tc)
158 {
159   return ((udp_connection_t *) tc);
160 }
161
162 always_inline u32
163 udp_connection_index (udp_connection_t * uc)
164 {
165   return (uc - udp_main.connections[uc->c_thread_index]);
166 }
167
168 void udp_connection_free (udp_connection_t * uc);
169 udp_connection_t *udp_connection_alloc (u32 thread_index);
170
171 /**
172  * Acquires a lock that blocks a connection pool from expanding.
173  */
174 always_inline void
175 udp_pool_add_peeker (u32 thread_index)
176 {
177   if (thread_index != vlib_get_thread_index ())
178     return;
179   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
180   udp_main.connection_peekers[thread_index] += 1;
181   if (udp_main.connection_peekers[thread_index] == 1)
182     clib_spinlock_lock_if_init (&udp_main.peekers_write_locks[thread_index]);
183   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
184                                 [thread_index]);
185 }
186
187 always_inline void
188 udp_pool_remove_peeker (u32 thread_index)
189 {
190   if (thread_index != vlib_get_thread_index ())
191     return;
192   ASSERT (udp_main.connection_peekers[thread_index] > 0);
193   clib_spinlock_lock_if_init (&udp_main.peekers_readers_locks[thread_index]);
194   udp_main.connection_peekers[thread_index] -= 1;
195   if (udp_main.connection_peekers[thread_index] == 0)
196     clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks
197                                   [thread_index]);
198   clib_spinlock_unlock_if_init (&udp_main.peekers_readers_locks
199                                 [thread_index]);
200 }
201
202 always_inline udp_connection_t *
203 udp_connection_clone_safe (u32 connection_index, u32 thread_index)
204 {
205   udp_connection_t *old_c, *new_c;
206   u32 current_thread_index = vlib_get_thread_index ();
207   new_c = udp_connection_alloc (current_thread_index);
208
209   /* If during the memcpy pool is reallocated AND the memory allocator
210    * decides to give the old chunk of memory to somebody in a hurry to
211    * scribble something on it, we have a problem. So add this thread as
212    * a session pool peeker.
213    */
214   udp_pool_add_peeker (thread_index);
215   old_c = udp_main.connections[thread_index] + connection_index;
216   clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
217   old_c->flags |= UDP_CONN_F_MIGRATED;
218   udp_pool_remove_peeker (thread_index);
219   new_c->c_thread_index = current_thread_index;
220   new_c->c_c_index = udp_connection_index (new_c);
221   new_c->c_fib_index = old_c->c_fib_index;
222   /* Assume cloned sessions don't need lock */
223   new_c->rx_lock = 0;
224   return new_c;
225 }
226
227 always_inline udp_dst_port_info_t *
228 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
229 {
230   uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
231   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
232 }
233
234 format_function_t format_udp_header;
235 format_function_t format_udp_rx_trace;
236 format_function_t format_udp_connection;
237 unformat_function_t unformat_udp_header;
238 unformat_function_t unformat_udp_port;
239
240 void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
241
242 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
243
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "gnu")
249  * End:
250  */
251
252 #endif /* __included_udp_h__ */