udp: support for disabling tx csum
[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(f, n, s, d) UDP_ERROR_##f,
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 #define foreach_udp_cfg_flag _ (NO_CSUM_OFFLOAD, "no-csum-offload")
59
60 typedef enum udp_cfg_flag_bits_
61 {
62 #define _(sym, str) UDP_CFG_F_##sym##_BIT,
63   foreach_udp_cfg_flag
64 #undef _
65     UDP_CFG_N_FLAG_BITS
66 } udp_cfg_flag_bits_e;
67
68 typedef enum udp_cfg_flag_
69 {
70 #define _(sym, str) UDP_CFG_F_##sym = 1 << UDP_CFG_F_##sym##_BIT,
71   foreach_udp_cfg_flag
72 #undef _
73     UDP_CFG_N_FLAGS
74 } __clib_packed udp_cfg_flags_t;
75
76 typedef struct
77 {
78   /** Required for pool_get_aligned */
79   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
80   transport_connection_t connection;    /**< must be first */
81   clib_spinlock_t rx_lock;              /**< rx fifo lock */
82   u8 flags;                             /**< connection flags */
83   udp_cfg_flags_t cfg_flags;            /**< configuration flags */
84   u16 mss;                              /**< connection mss */
85   u32 sw_if_index;                      /**< connection sw_if_index */
86   u32 next_node_index;  /**< Can be used to control next node in output */
87   u32 next_node_opaque; /**< Opaque to pass to next node */
88 } udp_connection_t;
89
90 #define udp_csum_offload(uc) (!((uc)->cfg_flags & UDP_CFG_F_NO_CSUM_OFFLOAD))
91
92 typedef struct
93 {
94   /* Name (a c string). */
95   char *name;
96
97   /* Port number in host byte order. */
98   udp_dst_port_t dst_port;
99
100   /* Node which handles this type. */
101   u32 node_index;
102
103   /* Next index for this type. */
104   u32 next_index;
105
106   /* UDP sessions refcount (not tunnels) */
107   u32 n_connections;
108
109   /* Parser for packet generator edits for this protocol */
110   unformat_function_t *unformat_pg_edit;
111 } udp_dst_port_info_t;
112
113 typedef enum
114 {
115   UDP_IP6 = 0,
116   UDP_IP4,                      /* the code is full of is_ip4... */
117   N_UDP_AF,
118 } udp_af_t;
119
120 typedef struct
121 {
122   udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
123
124   /* Hash tables mapping name/protocol to protocol info index. */
125   uword *dst_port_info_by_name[N_UDP_AF];
126   uword *dst_port_info_by_dst_port[N_UDP_AF];
127
128   /* Sparse vector mapping udp dst_port in network byte order
129      to next index. */
130   u16 *next_by_dst_port4;
131   u16 *next_by_dst_port6;
132   u8 punt_unknown4;
133   u8 punt_unknown6;
134
135   /* Udp local to input arc index */
136   u32 local_to_input_edge[N_UDP_AF];
137
138   /*
139    * Per-worker thread udp connection pools used with session layer
140    */
141   udp_connection_t **connections;
142   udp_connection_t *listener_pool;
143
144   u16 default_mtu;
145   u16 msg_id_base;
146   u8 csum_offload;
147
148   u8 icmp_send_unreachable_disabled;
149 } udp_main_t;
150
151 extern udp_main_t udp_main;
152 extern vlib_node_registration_t udp4_input_node;
153 extern vlib_node_registration_t udp6_input_node;
154 extern vlib_node_registration_t udp4_local_node;
155 extern vlib_node_registration_t udp6_local_node;
156 extern vlib_node_registration_t udp4_output_node;
157 extern vlib_node_registration_t udp6_output_node;
158
159 void udp_add_dst_port (udp_main_t * um, udp_dst_port_t dst_port,
160                        char *dst_port_name, u8 is_ip4);
161
162 always_inline udp_connection_t *
163 udp_connection_get (u32 conn_index, u32 thread_index)
164 {
165   if (pool_is_free_index (udp_main.connections[thread_index], conn_index))
166     return 0;
167   return pool_elt_at_index (udp_main.connections[thread_index], conn_index);
168 }
169
170 always_inline udp_connection_t *
171 udp_listener_get (u32 conn_index)
172 {
173   return pool_elt_at_index (udp_main.listener_pool, conn_index);
174 }
175
176 always_inline udp_main_t *
177 vnet_get_udp_main ()
178 {
179   return &udp_main;
180 }
181
182 always_inline udp_connection_t *
183 udp_connection_from_transport (transport_connection_t * tc)
184 {
185   return ((udp_connection_t *) tc);
186 }
187
188 void udp_connection_free (udp_connection_t * uc);
189 udp_connection_t *udp_connection_alloc (u32 thread_index);
190
191 always_inline udp_connection_t *
192 udp_connection_clone_safe (u32 connection_index, u32 thread_index)
193 {
194   u32 current_thread_index = vlib_get_thread_index (), new_index;
195   udp_connection_t *old_c, *new_c;
196
197   new_c = udp_connection_alloc (current_thread_index);
198   new_index = new_c->c_c_index;
199   /* Connection pool always realloced with barrier */
200   old_c = udp_main.connections[thread_index] + connection_index;
201   clib_memcpy_fast (new_c, old_c, sizeof (*new_c));
202   old_c->flags |= UDP_CONN_F_MIGRATED;
203   new_c->c_thread_index = current_thread_index;
204   new_c->c_c_index = new_index;
205   new_c->c_fib_index = old_c->c_fib_index;
206   /* Assume cloned sessions don't need lock */
207   new_c->rx_lock = 0;
208   return new_c;
209 }
210
211 always_inline udp_dst_port_info_t *
212 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
213 {
214   uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
215   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
216 }
217
218 format_function_t format_udp_header;
219 format_function_t format_udp_rx_trace;
220 format_function_t format_udp_connection;
221 unformat_function_t unformat_udp_header;
222 unformat_function_t unformat_udp_port;
223
224 void udp_connection_share_port (u16 lcl_port, u8 is_ip4);
225
226 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
227
228 /*
229  * fd.io coding-style-patch-verification: ON
230  *
231  * Local Variables:
232  * eval: (c-set-style "gnu")
233  * End:
234  */
235
236 #endif /* __included_udp_h__ */