misc: Break the big IP header files to improve compile time
[vpp.git] / src / plugins / cnat / cnat_types.h
1 /*
2  * Copyright (c) 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
16 #ifndef __CNAT_TYPES_H__
17 #define __CNAT_TYPES_H__
18
19 #include <vppinfra/bihash_24_8.h>
20 #include <vnet/fib/fib_node.h>
21 #include <vnet/fib/fib_source.h>
22 #include <vnet/ip/ip_types.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/util/throttle.h>
25
26 /* only in the default table for v4 and v6 */
27 #define CNAT_FIB_TABLE 0
28
29 /* default lifetime of NAT sessions (seconds) */
30 #define CNAT_DEFAULT_SESSION_MAX_AGE 30
31 /* lifetime of TCP conn NAT sessions after SYNACK (seconds) */
32 #define CNAT_DEFAULT_TCP_MAX_AGE 3600
33 /* lifetime of TCP conn NAT sessions after RST/FIN (seconds) */
34 #define CNAT_DEFAULT_TCP_RST_TIMEOUT 5
35 #define CNAT_DEFAULT_SCANNER_TIMEOUT (1.0)
36
37 #define CNAT_DEFAULT_SESSION_BUCKETS     1024
38 #define CNAT_DEFAULT_TRANSLATION_BUCKETS 1024
39 #define CNAT_DEFAULT_SNAT_BUCKETS        1024
40
41 #define CNAT_DEFAULT_SESSION_MEMORY      (1 << 20)
42 #define CNAT_DEFAULT_TRANSLATION_MEMORY  (256 << 10)
43 #define CNAT_DEFAULT_SNAT_MEMORY         (64 << 20)
44
45 /* This should be strictly lower than FIB_SOURCE_INTERFACE
46  * from fib_source.h */
47 #define CNAT_FIB_SOURCE_PRIORITY  0x02
48
49 /* Initial refcnt for timestamps (2 : session & rsession) */
50 #define CNAT_TIMESTAMP_INIT_REFCNT 2
51
52 #define MIN_SRC_PORT ((u16) 0xC000)
53
54 typedef enum
55 {
56   /* Endpoint addr has been resolved */
57   CNAT_EP_FLAG_RESOLVED = 1,
58 } cnat_ep_flag_t;
59
60 typedef struct cnat_endpoint_t_
61 {
62   ip_address_t ce_ip;
63   u32 ce_sw_if_index;
64   u16 ce_port;
65   u8 ce_flags;
66 } cnat_endpoint_t;
67
68 typedef struct cnat_endpoint_tuple_t_
69 {
70   cnat_endpoint_t dst_ep;
71   cnat_endpoint_t src_ep;
72 } cnat_endpoint_tuple_t;
73
74 typedef struct
75 {
76   u16 identifier;
77   u16 sequence;
78 } cnat_echo_header_t;
79
80 typedef struct
81 {
82   u32 dst_address_length_refcounts[129];
83   u16 *prefix_lengths_in_search_order;
84   uword *non_empty_dst_address_length_bitmap;
85 } cnat_snat_pfx_table_meta_t;
86
87 typedef struct
88 {
89   /* Stores (ip family, prefix & mask) */
90   clib_bihash_24_8_t ip_hash;
91   /* family dependant cache */
92   cnat_snat_pfx_table_meta_t meta[2];
93   /* Precomputed ip masks (ip4 & ip6) */
94   ip6_address_t ip_masks[129];
95 } cnat_snat_pfx_table_t;
96
97 typedef struct cnat_main_
98 {
99   /* Memory size of the session bihash */
100   uword session_hash_memory;
101
102   /* Number of buckets of the  session bihash */
103   u32 session_hash_buckets;
104
105   /* Memory size of the translation bihash */
106   uword translation_hash_memory;
107
108   /* Number of buckets of the  translation bihash */
109   u32 translation_hash_buckets;
110
111   /* Memory size of the source NAT prefix bihash */
112   uword snat_hash_memory;
113
114   /* Number of buckets of the  source NAT prefix bihash */
115   u32 snat_hash_buckets;
116
117   /* Timeout after which to clear sessions (in seconds) */
118   u32 session_max_age;
119
120   /* Timeout after which to clear an established TCP
121    * session (in seconds) */
122   u32 tcp_max_age;
123
124   /* delay in seconds between two scans of session/clients tables */
125   f64 scanner_timeout;
126
127   /* Lock for the timestamp pool */
128   clib_rwlock_t ts_lock;
129
130   /* Ip4 Address to use for source NATing */
131   cnat_endpoint_t snat_ip4;
132
133   /* Ip6 Address to use for source NATing */
134   cnat_endpoint_t snat_ip6;
135
136   /* Longest prefix Match table for source NATing */
137   cnat_snat_pfx_table_t snat_pfx_table;
138
139   /* Index of the scanner process node */
140   uword scanner_node_index;
141
142   /* Did we do lazy init ? */
143   u8 lazy_init_done;
144
145   /* Enable or Disable the scanner on startup */
146   u8 default_scanner_state;
147 } cnat_main_t;
148
149 typedef struct cnat_timestamp_t_
150 {
151   /* Last time said session was seen */
152   f64 last_seen;
153   /* expire after N seconds */
154   u16 lifetime;
155   /* Users refcount, initially 3 (session, rsession, dpo) */
156   u16 refcnt;
157 } cnat_timestamp_t;
158
159 typedef struct cnat_node_ctx_
160 {
161   f64 now;
162   u64 seed;
163   u32 thread_index;
164   ip_address_family_t af;
165   u8 do_trace;
166 } cnat_node_ctx_t;
167
168 cnat_main_t *cnat_get_main ();
169 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
170 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
171                                      va_list * args);
172 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
173 extern cnat_timestamp_t *cnat_timestamps;
174 extern fib_source_t cnat_fib_source;
175 extern cnat_main_t cnat_main;
176 extern throttle_t cnat_throttle;
177
178 extern char *cnat_error_strings[];
179
180 typedef enum
181 {
182 #define cnat_error(n,s) CNAT_ERROR_##n,
183 #include <cnat/cnat_error.def>
184 #undef cnat_error
185   CNAT_N_ERROR,
186 } cnat_error_t;
187
188 typedef enum cnat_scanner_cmd_t_
189 {
190   CNAT_SCANNER_OFF,
191   CNAT_SCANNER_ON,
192 } cnat_scanner_cmd_t;
193
194 /**
195  * Lazy initialization when first adding a translation
196  * or using snat
197  */
198 extern void cnat_lazy_init ();
199
200 /**
201  * Enable/Disable session cleanup
202  */
203 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
204
205 /**
206  * Resolve endpoint address
207  */
208 extern u8 cnat_resolve_ep (cnat_endpoint_t * ep);
209 extern u8 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
210                              ip_address_t * addr);
211
212
213 /*
214  * fd.io coding-style-patch-verification: ON
215  *
216  * Local Variables:
217  * eval: (c-set-style "gnu")
218  * End:
219  */
220
221 #endif