cnat: Prepare extended snat policies
[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 #define CNAT_DEFAULT_SNAT_IF_MAP_LEN     4096
41
42 #define CNAT_DEFAULT_SESSION_MEMORY      (1 << 20)
43 #define CNAT_DEFAULT_TRANSLATION_MEMORY  (256 << 10)
44 #define CNAT_DEFAULT_SNAT_MEMORY         (64 << 20)
45
46 /* Should be prime >~ 100 * numBackends */
47 #define CNAT_DEFAULT_MAGLEV_LEN 1009
48
49 /* This should be strictly lower than FIB_SOURCE_INTERFACE
50  * from fib_source.h */
51 #define CNAT_FIB_SOURCE_PRIORITY  0x02
52
53 /* Initial refcnt for timestamps (2 : session & rsession) */
54 #define CNAT_TIMESTAMP_INIT_REFCNT 2
55
56 #define MIN_SRC_PORT ((u16) 0xC000)
57
58 typedef enum
59 {
60   /* Endpoint addr has been resolved */
61   CNAT_EP_FLAG_RESOLVED = (1 << 0),
62 } cnat_ep_flag_t;
63
64 typedef struct cnat_endpoint_t_
65 {
66   ip_address_t ce_ip;
67   u32 ce_sw_if_index;
68   u16 ce_port;
69   u8 ce_flags;
70 } cnat_endpoint_t;
71
72 typedef struct cnat_endpoint_tuple_t_
73 {
74   cnat_endpoint_t dst_ep;
75   cnat_endpoint_t src_ep;
76   u8 ep_flags; /* cnat_trk_flag_t */
77 } cnat_endpoint_tuple_t;
78
79 typedef struct
80 {
81   u16 identifier;
82   u16 sequence;
83 } cnat_echo_header_t;
84
85 typedef struct cnat_main_
86 {
87   /* Memory size of the session bihash */
88   uword session_hash_memory;
89
90   /* Number of buckets of the  session bihash */
91   u32 session_hash_buckets;
92
93   /* Memory size of the translation bihash */
94   uword translation_hash_memory;
95
96   /* Number of buckets of the  translation bihash */
97   u32 translation_hash_buckets;
98
99   /* Memory size of the source NAT prefix bihash */
100   uword snat_hash_memory;
101
102   /* Number of buckets of the  source NAT prefix bihash */
103   u32 snat_hash_buckets;
104
105   /* Bit map for include / exclude sw_if_index
106    * so max number of expected interfaces */
107   u32 snat_if_map_length;
108
109   /* Timeout after which to clear sessions (in seconds) */
110   u32 session_max_age;
111
112   /* Timeout after which to clear an established TCP
113    * session (in seconds) */
114   u32 tcp_max_age;
115
116   /* delay in seconds between two scans of session/clients tables */
117   f64 scanner_timeout;
118
119   /* Lock for the timestamp pool */
120   clib_rwlock_t ts_lock;
121
122   /* Index of the scanner process node */
123   uword scanner_node_index;
124
125   /* Did we do lazy init ? */
126   u8 lazy_init_done;
127
128   /* Enable or Disable the scanner on startup */
129   u8 default_scanner_state;
130
131   /* Number of buckets for maglev, should be a
132    * prime >= 100 * max num bakends */
133   u32 maglev_len;
134 } cnat_main_t;
135
136 typedef struct cnat_timestamp_t_
137 {
138   /* Last time said session was seen */
139   f64 last_seen;
140   /* expire after N seconds */
141   u16 lifetime;
142   /* Users refcount, initially 3 (session, rsession, dpo) */
143   u16 refcnt;
144 } cnat_timestamp_t;
145
146 typedef struct cnat_node_ctx_
147 {
148   f64 now;
149   u32 thread_index;
150   ip_address_family_t af;
151   u8 do_trace;
152 } cnat_node_ctx_t;
153
154 cnat_main_t *cnat_get_main ();
155 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
156 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
157                                      va_list * args);
158 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
159 extern cnat_timestamp_t *cnat_timestamps;
160 extern fib_source_t cnat_fib_source;
161 extern cnat_main_t cnat_main;
162
163 extern char *cnat_error_strings[];
164
165 typedef enum
166 {
167 #define cnat_error(n,s) CNAT_ERROR_##n,
168 #include <cnat/cnat_error.def>
169 #undef cnat_error
170   CNAT_N_ERROR,
171 } cnat_error_t;
172
173 typedef enum cnat_scanner_cmd_t_
174 {
175   CNAT_SCANNER_OFF,
176   CNAT_SCANNER_ON,
177 } cnat_scanner_cmd_t;
178
179 /**
180  * Lazy initialization when first adding a translation
181  * or using snat
182  */
183 extern void cnat_lazy_init ();
184
185 /**
186  * Enable/Disable session cleanup
187  */
188 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
189
190 /**
191  * Resolve endpoint address
192  */
193 extern u8 cnat_resolve_ep (cnat_endpoint_t * ep);
194 extern u8 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
195                              ip_address_t * addr);
196
197
198 /*
199  * fd.io coding-style-patch-verification: ON
200  *
201  * Local Variables:
202  * eval: (c-set-style "gnu")
203  * End:
204  */
205
206 #endif