2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT plugin virtual fragmentation reassembly
19 #ifndef __included_nat_reass_h__
20 #define __included_nat_reass_h__
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vppinfra/bihash_16_8.h>
25 #include <vppinfra/bihash_48_8.h>
26 #include <vppinfra/dlist.h>
28 #define NAT_REASS_TIMEOUT_DEFAULT 2
29 #define NAT_MAX_REASS_DEAFULT 1024
30 #define NAT_MAX_FRAG_DEFAULT 5
31 #define NAT_REASS_HT_LOAD_FACTOR (0.75)
41 /* align by making this 4 octets even though its a 2 octets field */
43 /* align by making this 4 octets even though its a 1 octet field */
48 } nat_reass_ip4_key_t;
51 typedef CLIB_PACKED(struct
53 nat_reass_ip4_key_t key;
57 u32 frags_per_reass_list_head_index;
71 /* align by making this 4 octets even though its a 1 octet field */
77 } nat_reass_ip6_key_t;
80 typedef CLIB_PACKED(struct
82 nat_reass_ip6_key_t key;
86 u32 frags_per_reass_list_head_index;
106 nat_reass_ip4_t *ip4_reass_pool;
107 clib_bihash_16_8_t ip4_reass_hash;
108 dlist_elt_t *ip4_reass_lru_list_pool;
109 dlist_elt_t *ip4_frags_list_pool;
110 u32 ip4_reass_head_index;
112 clib_spinlock_t ip4_reass_lock;
115 nat_reass_ip6_t *ip6_reass_pool;
116 clib_bihash_48_8_t ip6_reass_hash;
117 dlist_elt_t *ip6_reass_lru_list_pool;
118 dlist_elt_t *ip6_frags_list_pool;
119 u32 ip6_reass_head_index;
121 clib_spinlock_t ip6_reass_lock;
124 vlib_main_t *vlib_main;
125 vnet_main_t *vnet_main;
129 * @brief Set NAT virtual fragmentation reassembly configuration.
131 * @param timeout Reassembly timeout.
132 * @param max_reass Maximum number of concurrent reassemblies.
133 * @param max_frag Maximum number of fragmets per reassembly
134 * @param drop_frag If zero translate fragments, otherwise drop fragments.
135 * @param is_ip6 1 if IPv6, 0 if IPv4.
137 * @returns 0 on success, non-zero value otherwise.
139 int nat_reass_set (u32 timeout, u16 max_reass, u8 max_frag, u8 drop_frag,
143 * @brief Get reassembly timeout.
145 * @param is_ip6 1 if IPv6, 0 if IPv4.
147 * @returns reassembly timeout.
149 u32 nat_reass_get_timeout (u8 is_ip6);
152 * @brief Get maximum number of concurrent reassemblies.
154 * @param is_ip6 1 if IPv6, 0 if IPv4.
156 * @returns maximum number of concurrent reassemblies.
158 u16 nat_reass_get_max_reass (u8 is_ip6);
161 * @brief Get maximum number of fragmets per reassembly.
163 * @param is_ip6 1 if IPv6, 0 if IPv4.
165 * @returns maximum number of fragmets per reassembly.
167 u8 nat_reass_get_max_frag (u8 is_ip6);
170 * @brief Get status of virtual fragmentation reassembly.
172 * @param is_ip6 1 if IPv6, 0 if IPv4.
174 * @returns zero if translate fragments, non-zero value if drop fragments.
176 u8 nat_reass_is_drop_frag (u8 is_ip6);
179 * @brief Initialize NAT virtual fragmentation reassembly.
181 * @param vm vlib main.
183 * @return error code.
185 clib_error_t *nat_reass_init (vlib_main_t * vm);
188 * @brief Find or create reassembly.
190 * @param src Source IPv4 address.
191 * @param dst Destination IPv4 address.
192 * @param frag_id Fragment ID.
193 * @param proto L4 protocol.
194 * @param reset_timeout If non-zero value reset timeout.
195 * @param bi_to_drop Fragments to drop.
197 * @returns Reassembly data or 0 on failure.
199 nat_reass_ip4_t *nat_ip4_reass_find_or_create (ip4_address_t src,
201 u16 frag_id, u8 proto,
205 * @brief Cache fragment.
207 * @param reass Reassembly data.
208 * @param bi Buffer index.
210 * @returns 0 on success, non-zero value otherwise.
212 int nat_ip4_reass_add_fragment (nat_reass_ip4_t * reass, u32 bi);
215 * @brief Get cached fragments.
217 * @param reass Reassembly data.
218 * @param bi Vector of buffer indexes.
220 void nat_ip4_reass_get_frags (nat_reass_ip4_t * reass, u32 ** bi);
223 * @breif Call back function when walking IPv4 reassemblies, non-zero return
226 typedef int (*nat_ip4_reass_walk_fn_t) (nat_reass_ip4_t * reass, void *ctx);
229 * @brief Walk IPv4 reassemblies.
231 * @param fn The function to invoke on each entry visited.
232 * @param ctx A context passed in the visit function.
234 void nat_ip4_reass_walk (nat_ip4_reass_walk_fn_t fn, void *ctx);
237 * @brief Find or create reassembly.
239 * @param src Source IPv6 address.
240 * @param dst Destination IPv6 address.
241 * @param frag_id Fragment ID.
242 * @param proto L4 protocol.
243 * @param reset_timeout If non-zero value reset timeout.
244 * @param bi_to_drop Fragments to drop.
246 * @returns Reassembly data or 0 on failure.
248 nat_reass_ip6_t *nat_ip6_reass_find_or_create (ip6_address_t src,
250 u32 frag_id, u8 proto,
254 * @brief Cache fragment.
256 * @param reass Reassembly data.
257 * @param bi Buffer index.
259 * @returns 0 on success, non-zero value otherwise.
261 int nat_ip6_reass_add_fragment (nat_reass_ip6_t * reass, u32 bi);
264 * @brief Get cached fragments.
266 * @param reass Reassembly data.
267 * @param bi Vector of buffer indexes.
269 void nat_ip6_reass_get_frags (nat_reass_ip6_t * reass, u32 ** bi);
272 * @breif Call back function when walking IPv6 reassemblies, non-zero return
275 typedef int (*nat_ip6_reass_walk_fn_t) (nat_reass_ip6_t * reass, void *ctx);
278 * @brief Walk IPv6 reassemblies.
280 * @param fn The function to invoke on each entry visited.
281 * @param ctx A context passed in the visit function.
283 void nat_ip6_reass_walk (nat_ip6_reass_walk_fn_t fn, void *ctx);
285 #endif /* __included_nat_reass_h__ */
288 * fd.io coding-style-patch-verification: ON
291 * eval: (c-set-style "gnu")