nat: use correct data types for memory sizes
[vpp.git] / src / plugins / nat / nat64.h
1 /*
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:
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 /**
16  * @file
17  * @brief NAT64 global declarations
18  */
19 #ifndef __included_nat64_h__
20 #define __included_nat64_h__
21
22 #include <nat/nat.h>
23 #include <nat/nat64_db.h>
24
25 #define foreach_nat64_tcp_ses_state            \
26   _(0, CLOSED, "closed")                       \
27   _(1, V4_INIT, "v4-init")                     \
28   _(2, V6_INIT, "v6-init")                     \
29   _(3, ESTABLISHED, "established")             \
30   _(4, V4_FIN_RCV, "v4-fin-rcv")               \
31   _(5, V6_FIN_RCV, "v6-fin-rcv")               \
32   _(6, V6_FIN_V4_FIN_RCV, "v6-fin-v4-fin-rcv") \
33   _(7, TRANS, "trans")
34
35 typedef enum
36 {
37 #define _(v, N, s) NAT64_TCP_STATE_##N = v,
38   foreach_nat64_tcp_ses_state
39 #undef _
40 } nat64_tcp_ses_state_t;
41
42 typedef enum
43 {
44   NAT64_CLEANER_RESCHEDULE = 1,
45 } nat64_cleaner_process_event_e;
46
47 typedef struct
48 {
49   ip6_address_t prefix;
50   u8 plen;
51   u32 vrf_id;
52   u32 fib_index;
53 } nat64_prefix_t;
54
55 typedef struct
56 {
57   ip6_address_t in_addr;
58   u16 in_port;
59   ip4_address_t out_addr;
60   u16 out_port;
61   u32 fib_index;
62   u32 thread_index;
63   u8 proto;
64   u8 is_add;
65   u8 done;
66 } nat64_static_bib_to_update_t;
67
68 typedef struct
69 {
70   /** Interface pool */
71   snat_interface_t *interfaces;
72
73   /** Address pool vector */
74   snat_address_t *addr_pool;
75
76   /** sw_if_indices whose interface addresses should be auto-added */
77   u32 *auto_add_sw_if_indices;
78
79   /** Pref64 vector */
80   nat64_prefix_t *pref64;
81
82   /** BIB and session DB per thread */
83   nat64_db_t *db;
84
85   /** Worker handoff */
86   u32 fq_in2out_index;
87   u32 fq_out2in_index;
88
89   /** Pool of static BIB entries to be added/deleted in worker threads */
90   nat64_static_bib_to_update_t *static_bibs;
91
92   /** config parameters */
93   u32 bib_buckets;
94   uword bib_memory_size;
95   u32 st_buckets;
96   uword st_memory_size;
97
98   /** values of various timeouts */
99   u32 udp_timeout;
100   u32 icmp_timeout;
101   u32 tcp_trans_timeout;
102   u32 tcp_est_timeout;
103
104   /* Total count of interfaces enabled */
105   u32 total_enabled_count;
106   /* The process node which orcherstrates the cleanup */
107   u32 nat64_expire_walk_node_index;
108
109   /* counters/gauges */
110   vlib_simple_counter_main_t total_bibs;
111   vlib_simple_counter_main_t total_sessions;
112
113   /** node index **/
114   u32 error_node_index;
115
116   u32 in2out_node_index;
117   u32 in2out_slowpath_node_index;
118
119   u32 out2in_node_index;
120
121   ip4_main_t *ip4_main;
122   snat_main_t *sm;
123 } nat64_main_t;
124
125 extern nat64_main_t nat64_main;
126 extern vlib_node_registration_t nat64_in2out_node;
127 extern vlib_node_registration_t nat64_out2in_node;
128
129 /**
130  * @brief Add/delete address to NAT64 pool.
131  *
132  * @param thread_index Thread index used by ipfix nat logging (not address per thread).
133  * @param addr   IPv4 address.
134  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
135  * @param is_add 1 if add, 0 if delete.
136  *
137  * @returns 0 on success, non-zero value otherwise.
138  */
139 int nat64_add_del_pool_addr (u32 thread_index,
140                              ip4_address_t * addr, u32 vrf_id, u8 is_add);
141
142 /**
143  * @brief Call back function when walking addresses in NAT64 pool, non-zero
144  * return value stop walk.
145  */
146 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
147
148 /**
149  * @brief Walk NAT64 pool.
150  *
151  * @param fn The function to invoke on each entry visited.
152  * @param ctx A context passed in the visit function.
153  */
154 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
155
156 /**
157  * @brief NAT64 pool address from specific (DHCP addressed) interface.
158  *
159  * @param sw_if_index Index of the interface.
160  * @param is_add      1 if add, 0 if delete.
161  *
162  * @returns 0 on success, non-zero value otherwise.
163  */
164 int nat64_add_interface_address (u32 sw_if_index, int is_add);
165
166 /**
167  * @brief Enable/disable NAT64 feature on the interface.
168  *
169  * @param sw_if_index Index of the interface.
170  * @param is_inside   1 if inside, 0 if outside.
171  * @param is_add      1 if add, 0 if delete.
172  *
173  * @returns 0 on success, non-zero value otherwise.
174  */
175 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
176
177 /**
178  * @brief Call back function when walking interfaces with NAT64 feature,
179  * non-zero return value stop walk.
180  */
181 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
182
183 /**
184  * @brief Walk NAT64 interfaces.
185  *
186  * @param fn The function to invoke on each entry visited.
187  * @param ctx A context passed in the visit function.
188  */
189 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
190
191 /**
192  * @brief Initialize NAT64.
193  *
194  * @param vm vlib main.
195  *
196  * @return error code.
197  */
198 clib_error_t *nat64_init (vlib_main_t * vm);
199
200 /**
201  * @brief Add/delete static NAT64 BIB entry.
202  *
203  * @param in_addr  Inside IPv6 address.
204  * @param out_addr Outside IPv4 address.
205  * @param in_port  Inside port number.
206  * @param out_port Outside port number.
207  * @param proto    L4 protocol.
208  * @param vrf_id   VRF id of tenant.
209  * @param is_add   1 if add, 0 if delete.
210  *
211  * @returns 0 on success, non-zero value otherwise.
212  */
213 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
214                                     ip4_address_t * out_addr, u16 in_port,
215                                     u16 out_port, u8 proto, u32 vrf_id,
216                                     u8 is_add);
217
218 /**
219  * @brief Alloce IPv4 address and port pair from NAT64 pool.
220  *
221  * @param fib_index    FIB index of tenant.
222  * @param proto        L4 protocol.
223  * @param addr         Allocated IPv4 address.
224  * @param port         Allocated port number.
225  * @param thread_index Thread index.
226  *
227  * @returns 0 on success, non-zero value otherwise.
228  */
229 int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
230                                    ip4_address_t * addr, u16 * port,
231                                    u32 thread_index);
232
233 /**
234  * @brief Set UDP session timeout.
235  *
236  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
237  *
238  * @returns 0 on success, non-zero value otherwise.
239  */
240 int nat64_set_udp_timeout (u32 timeout);
241
242 /**
243  * @brief Get UDP session timeout.
244  *
245  * @returns UDP session timeout in seconds.
246  */
247 u32 nat64_get_udp_timeout (void);
248
249 /**
250  * @brief Set ICMP session timeout.
251  *
252  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
253  *
254  * @returns 0 on success, non-zero value otherwise.
255  */
256 int nat64_set_icmp_timeout (u32 timeout);
257
258 /**
259  * @brief Get ICMP session timeout.
260  *
261  * @returns ICMP session timeout in seconds.
262  */
263 u32 nat64_get_icmp_timeout (void);
264
265 /**
266  * @brief Set TCP session timeouts.
267  *
268  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
269  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
270  *
271  * @returns 0 on success, non-zero value otherwise.
272  */
273 int nat64_set_tcp_timeouts (u32 trans, u32 est);
274
275 /**
276  * @brief Get TCP transitory timeout.
277  *
278  * @returns TCP transitory timeout in seconds.
279  */
280 u32 nat64_get_tcp_trans_timeout (void);
281
282 /**
283  * @brief Get TCP established timeout.
284  *
285  * @returns TCP established timeout in seconds.
286  */
287 u32 nat64_get_tcp_est_timeout (void);
288
289 /**
290  * @brief Reset NAT64 session timeout.
291  *
292  * @param ste Session table entry.
293  * @param vm VLIB main.
294  **/
295 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
296                                   vlib_main_t * vm);
297
298 /**
299  * @brief Set NAT64 TCP session state.
300  *
301  * @param ste Session table entry.
302  * @param tcp TCP header.
303  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
304  */
305 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
306                                   tcp_header_t * tcp, u8 is_ip6);
307
308 /**
309  * @brief Add/delete NAT64 prefix.
310  *
311  * @param prefix NAT64 prefix.
312  * @param plen Prefix length.
313  * @param vrf_id VRF id of tenant.
314  * @param is_add 1 if add, 0 if delete.
315  *
316  * @returns 0 on success, non-zero value otherwise.
317  */
318 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
319                           u8 is_add);
320
321 /**
322  * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
323  * return value stop walk.
324  */
325 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
326
327 /**
328  * @brief Walk NAT64 prefixes.
329  *
330  * @param fn The function to invoke on each entry visited.
331  * @param ctx A context passed in the visit function.
332  */
333 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
334
335 /**
336  * Compose IPv4-embedded IPv6 addresses.
337  * @param ip6 IPv4-embedded IPv6 addresses.
338  * @param ip4 IPv4 address.
339  * @param fib_index Tenant FIB index.
340  */
341 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
342                         u32 fib_index);
343
344 /**
345  * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
346  *
347  * @param ip6 IPv4-embedded IPv6 addresses.
348  * @param ip4 IPv4 address.
349  * @param fib_index Tenant FIB index.
350  */
351 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
352                         u32 fib_index);
353
354 /**
355  * @brief Set NAT64 hash tables configuration.
356  *
357  * @param bib_buckets Number of BIB hash buckets.
358  * @param bib_memory_size Memory size of BIB hash.
359  * @param st_buckets Number of session table hash buckets.
360  * @param st_memory_size Memory size of session table hash.
361  */
362 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
363                      uword st_memory_size);
364
365 /**
366  * @brief Get worker thread index for NAT64 in2out.
367  *
368  * @param addr IPv6 src address.
369  *
370  * @returns worker thread index.
371  */
372 u32 nat64_get_worker_in2out (ip6_address_t * addr);
373
374 /**
375  * @brief Get worker thread index for NAT64 out2in.
376  *
377  * @param ip IPv4 header.
378  *
379  * @returns worker thread index.
380  */
381 u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip);
382
383 #endif /* __included_nat64_h__ */
384
385 /*
386  * fd.io coding-style-patch-verification: ON
387  *
388  * Local Variables:
389  * eval: (c-set-style "gnu")
390  * End:
391  */