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