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