b6b6e012c5373ad7cf423159ff2ea6ec78e090a3
[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 <vnet/fib/fib_node.h>
20 #include <vnet/fib/fib_source.h>
21 #include <vnet/ip/ip_types.h>
22 #include <vnet/ip/ip.h>
23
24 /* only in the default table for v4 and v6 */
25 #define CNAT_FIB_TABLE 0
26
27 /* default lifetime of NAT sessions (seconds) */
28 #define CNAT_DEFAULT_SESSION_MAX_AGE 30
29 /* lifetime of TCP conn NAT sessions after SYNACK (seconds) */
30 #define CNAT_DEFAULT_TCP_MAX_AGE 3600
31 /* lifetime of TCP conn NAT sessions after RST/FIN (seconds) */
32 #define CNAT_DEFAULT_TCP_RST_TIMEOUT 5
33 #define CNAT_DEFAULT_SCANNER_TIMEOUT (1.0)
34
35 #define CNAT_DEFAULT_SESSION_BUCKETS     1024
36 #define CNAT_DEFAULT_TRANSLATION_BUCKETS 1024
37 #define CNAT_DEFAULT_SNAT_BUCKETS        1024
38
39 #define CNAT_DEFAULT_SESSION_MEMORY      (1 << 20)
40 #define CNAT_DEFAULT_TRANSLATION_MEMORY  (256 << 10)
41 #define CNAT_DEFAULT_SNAT_MEMORY         (64 << 20)
42
43 /* This should be strictly lower than FIB_SOURCE_INTERFACE
44  * from fib_source.h */
45 #define CNAT_FIB_SOURCE_PRIORITY  0x02
46
47 /* Initial refcnt for timestamps (2 : session & rsession) */
48 #define CNAT_TIMESTAMP_INIT_REFCNT 2
49
50 #define MIN_SRC_PORT ((u16) 0xC000)
51
52 typedef struct cnat_endpoint_t_
53 {
54   ip_address_t ce_ip;
55   u16 ce_port;
56 } cnat_endpoint_t;
57
58 typedef struct cnat_endpoint_tuple_t_
59 {
60   cnat_endpoint_t dst_ep;
61   cnat_endpoint_t src_ep;
62 } cnat_endpoint_tuple_t;
63
64 typedef struct
65 {
66   u16 identifier;
67   u16 sequence;
68 } cnat_echo_header_t;
69
70 typedef struct
71 {
72   u32 dst_address_length_refcounts[129];
73   u16 *prefix_lengths_in_search_order;
74   uword *non_empty_dst_address_length_bitmap;
75 } cnat_snat_pfx_table_meta_t;
76
77 typedef struct
78 {
79   /* Stores (ip family, prefix & mask) */
80   clib_bihash_24_8_t ip_hash;
81   /* family dependant cache */
82   cnat_snat_pfx_table_meta_t meta[2];
83   /* Precomputed ip masks (ip4 & ip6) */
84   ip6_address_t ip_masks[129];
85 } cnat_snat_pfx_table_t;
86
87 typedef struct cnat_main_
88 {
89   /* Memory size of the session bihash */
90   uword session_hash_memory;
91
92   /* Number of buckets of the  session bihash */
93   u32 session_hash_buckets;
94
95   /* Memory size of the translation bihash */
96   uword translation_hash_memory;
97
98   /* Number of buckets of the  translation bihash */
99   u32 translation_hash_buckets;
100
101   /* Memory size of the source NAT prefix bihash */
102   uword snat_hash_memory;
103
104   /* Number of buckets of the  source NAT prefix bihash */
105   u32 snat_hash_buckets;
106
107   /* Timeout after which to clear sessions (in seconds) */
108   u32 session_max_age;
109
110   /* Timeout after which to clear an established TCP
111    * session (in seconds) */
112   u32 tcp_max_age;
113
114   /* delay in seconds between two scans of session/clients tables */
115   f64 scanner_timeout;
116
117   /* Lock for the timestamp pool */
118   clib_rwlock_t ts_lock;
119
120   /* Ip4 Address to use for source NATing */
121   ip4_address_t snat_ip4;
122
123   /* Ip6 Address to use for source NATing */
124   ip6_address_t snat_ip6;
125
126   /* Longest prefix Match table for source NATing */
127   cnat_snat_pfx_table_t snat_pfx_table;
128
129   /* Index of the scanner process node */
130   uword scanner_node_index;
131
132   /* Did we do lazy init ? */
133   u8 lazy_init_done;
134
135   /* Enable or Disable the scanner on startup */
136   u8 default_scanner_state;
137 } cnat_main_t;
138
139 typedef struct cnat_timestamp_t_
140 {
141   /* Last time said session was seen */
142   f64 last_seen;
143   /* expire after N seconds */
144   u16 lifetime;
145   /* Users refcount, initially 3 (session, rsession, dpo) */
146   u16 refcnt;
147 } cnat_timestamp_t;
148
149 typedef struct cnat_node_ctx_
150 {
151   f64 now;
152   u64 seed;
153   u32 thread_index;
154   ip_address_family_t af;
155   u8 do_trace;
156 } cnat_node_ctx_t;
157
158 cnat_main_t *cnat_get_main ();
159 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
160 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
161                                      va_list * args);
162 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
163 extern cnat_timestamp_t *cnat_timestamps;
164 extern fib_source_t cnat_fib_source;
165 extern cnat_main_t cnat_main;
166 extern throttle_t cnat_throttle;
167
168 extern char *cnat_error_strings[];
169
170 typedef enum
171 {
172 #define cnat_error(n,s) CNAT_ERROR_##n,
173 #include <cnat/cnat_error.def>
174 #undef cnat_error
175   CNAT_N_ERROR,
176 } cnat_error_t;
177
178 typedef enum cnat_scanner_cmd_t_
179 {
180   CNAT_SCANNER_OFF,
181   CNAT_SCANNER_ON,
182 } cnat_scanner_cmd_t;
183
184 /**
185  * Lazy initialization when first adding a translation
186  * or using snat
187  */
188 extern void cnat_lazy_init ();
189
190 /**
191  * Enable/Disable session cleanup
192  */
193 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */
202
203 #endif