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