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