hs-test: more debug output in http3 test
[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_CLIENT_BUCKETS      1024
40 #define CNAT_DEFAULT_SNAT_BUCKETS        1024
41 #define CNAT_DEFAULT_SNAT_IF_MAP_LEN     4096
42
43 #define CNAT_DEFAULT_SESSION_MEMORY      (1 << 20)
44 #define CNAT_DEFAULT_TRANSLATION_MEMORY  (256 << 10)
45 #define CNAT_DEFAULT_CLIENT_MEMORY       (256 << 10)
46 #define CNAT_DEFAULT_SNAT_MEMORY         (64 << 10)
47
48 /* Should be prime >~ 100 * numBackends */
49 #define CNAT_DEFAULT_MAGLEV_LEN 1009
50
51 /* This should be strictly lower than FIB_SOURCE_INTERFACE
52  * from fib_source.h */
53 #define CNAT_FIB_SOURCE_PRIORITY  0x02
54
55 /* Initial number of timestamps for a session
56  * this will be incremented when adding the reverse
57  * session in cnat_rsession_create */
58 #define CNAT_TIMESTAMP_INIT_REFCNT 1
59
60 #define MIN_SRC_PORT ((u16) 0xC000)
61
62 typedef struct
63 {
64   /* Source and destination port. */
65   u16 src_port, dst_port;
66
67   /* Random value to distinguish connections. */
68   u32 verification_tag;
69
70   u32 checksum;
71 } sctp_header_t;
72
73 typedef enum cnat_trk_flag_t_
74 {
75   /* Endpoint is active (static or dhcp resolved) */
76   CNAT_TRK_ACTIVE = (1 << 0),
77   /* Don't translate this endpoint, but still
78    * forward. Used by maglev for DSR */
79   CNAT_TRK_FLAG_NO_NAT = (1 << 1),
80   /* */
81   CNAT_TRK_FLAG_TEST_DISABLED = (1 << 7),
82 } cnat_trk_flag_t;
83
84 typedef enum
85 {
86   /* Endpoint addr has been resolved */
87   CNAT_EP_FLAG_RESOLVED = (1 << 0),
88 } cnat_ep_flag_t;
89
90 typedef struct cnat_endpoint_t_
91 {
92   ip_address_t ce_ip;
93   u32 ce_sw_if_index;
94   u16 ce_port;
95   u8 ce_flags;
96 } cnat_endpoint_t;
97
98 typedef struct cnat_endpoint_tuple_t_
99 {
100   cnat_endpoint_t dst_ep;
101   cnat_endpoint_t src_ep;
102   u8 ep_flags; /* cnat_trk_flag_t */
103 } cnat_endpoint_tuple_t;
104
105 typedef struct
106 {
107   u16 identifier;
108   u16 sequence;
109 } cnat_echo_header_t;
110
111 typedef struct cnat_main_
112 {
113   /* Memory size of the session bihash */
114   uword session_hash_memory;
115
116   /* Number of buckets of the  session bihash */
117   u32 session_hash_buckets;
118
119   /* Memory size of the translation bihash */
120   uword translation_hash_memory;
121
122   /* Number of buckets of the  translation bihash */
123   u32 translation_hash_buckets;
124
125   /* Memory size of the client bihash */
126   uword client_hash_memory;
127
128   /* Number of buckets of the  client bihash */
129   u32 client_hash_buckets;
130
131   /* Memory size of the source NAT prefix bihash */
132   uword snat_hash_memory;
133
134   /* Number of buckets of the  source NAT prefix bihash */
135   u32 snat_hash_buckets;
136
137   /* Bit map for include / exclude sw_if_index
138    * so max number of expected interfaces */
139   u32 snat_if_map_length;
140
141   /* Timeout after which to clear sessions (in seconds) */
142   u32 session_max_age;
143
144   /* Timeout after which to clear an established TCP
145    * session (in seconds) */
146   u32 tcp_max_age;
147
148   /* delay in seconds between two scans of session/clients tables */
149   f64 scanner_timeout;
150
151   /* Index of the scanner process node */
152   uword scanner_node_index;
153
154   /* Did we do lazy init ? */
155   u8 lazy_init_done;
156
157   /* Enable or Disable the scanner on startup */
158   u8 default_scanner_state;
159
160   /* Number of buckets for maglev, should be a
161    * prime >= 100 * max num bakends */
162   u32 maglev_len;
163 } cnat_main_t;
164
165 typedef struct cnat_timestamp_t_
166 {
167   /* Last time said session was seen */
168   f64 last_seen;
169   /* expire after N seconds */
170   u16 lifetime;
171   /* Users refcount, initially 3 (session, rsession, dpo) */
172   u16 refcnt;
173 } cnat_timestamp_t;
174
175 /* Create the first pool with 1 << CNAT_TS_BASE_SIZE elts */
176 #define CNAT_TS_BASE_SIZE (8)
177 /* reserve the top CNAT_TS_MPOOL_BITS bits for finding the pool */
178 #define CNAT_TS_MPOOL_BITS (6)
179
180 typedef struct cnat_timestamp_mpool_t_
181 {
182   /* Increasing fixed size pools of timestamps */
183   cnat_timestamp_t *ts_pools[1 << CNAT_TS_MPOOL_BITS];
184   /* Bitmap of pools with free space */
185   uword *ts_free;
186   /* Index of next pool to init */
187   u8 next_empty_pool_idx;
188   /* ts creation lock */
189   clib_spinlock_t ts_lock;
190 } cnat_timestamp_mpool_t;
191
192 typedef struct cnat_node_ctx_
193 {
194   f64 now;
195   u32 thread_index;
196   ip_address_family_t af;
197   u8 do_trace;
198 } cnat_node_ctx_t;
199
200 cnat_main_t *cnat_get_main ();
201 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
202 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
203                                      va_list * args);
204 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
205 extern cnat_timestamp_mpool_t cnat_timestamps;
206 extern cnat_main_t cnat_main;
207
208 extern char *cnat_error_strings[];
209
210 typedef enum
211 {
212 #define cnat_error(n,s) CNAT_ERROR_##n,
213 #include <cnat/cnat_error.def>
214 #undef cnat_error
215   CNAT_N_ERROR,
216 } cnat_error_t;
217
218 typedef enum cnat_scanner_cmd_t_
219 {
220   CNAT_SCANNER_OFF,
221   CNAT_SCANNER_ON,
222 } cnat_scanner_cmd_t;
223
224 /**
225  * Lazy initialization when first adding a translation
226  * or using snat
227  */
228 extern void cnat_lazy_init ();
229
230 /**
231  * Enable/Disable session cleanup
232  */
233 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
234
235 /**
236  * Resolve endpoint address
237  */
238 extern u8 cnat_resolve_ep (cnat_endpoint_t * ep);
239 extern u8 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
240                              ip_address_t * addr);
241
242
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */
250
251 #endif