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