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