cnat: Destination based NAT
[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
65
66 typedef struct
67 {
68   u32 dst_address_length_refcounts[129];
69   u16 *prefix_lengths_in_search_order;
70   uword *non_empty_dst_address_length_bitmap;
71 } cnat_snat_pfx_table_meta_t;
72
73 typedef struct
74 {
75   /* Stores (ip family, prefix & mask) */
76   clib_bihash_24_8_t ip_hash;
77   /* family dependant cache */
78   cnat_snat_pfx_table_meta_t meta[2];
79   /* Precomputed ip masks (ip4 & ip6) */
80   ip6_address_t ip_masks[129];
81 } cnat_snat_pfx_table_t;
82
83 typedef struct cnat_main_
84 {
85   /* Memory size of the session bihash */
86   uword session_hash_memory;
87
88   /* Number of buckets of the  session bihash */
89   u32 session_hash_buckets;
90
91   /* Memory size of the translation bihash */
92   uword translation_hash_memory;
93
94   /* Number of buckets of the  translation bihash */
95   u32 translation_hash_buckets;
96
97   /* Memory size of the source NAT prefix bihash */
98   uword snat_hash_memory;
99
100   /* Number of buckets of the  source NAT prefix bihash */
101   u32 snat_hash_buckets;
102
103   /* Timeout after which to clear sessions (in seconds) */
104   u32 session_max_age;
105
106   /* Timeout after which to clear an established TCP
107    * session (in seconds) */
108   u32 tcp_max_age;
109
110   /* delay in seconds between two scans of session/clients tables */
111   f64 scanner_timeout;
112
113   /* Lock for the timestamp pool */
114   clib_rwlock_t ts_lock;
115
116   /* Source ports bitmap for snat */
117   clib_bitmap_t *src_ports;
118
119   /* Lock for src_ports access */
120   clib_spinlock_t src_ports_lock;
121
122   /* Ip4 Address to use for source NATing */
123   ip4_address_t snat_ip4;
124
125   /* Ip6 Address to use for source NATing */
126   ip6_address_t snat_ip6;
127
128   /* Longest prefix Match table for source NATing */
129   cnat_snat_pfx_table_t snat_pfx_table;
130 } cnat_main_t;
131
132 typedef struct cnat_timestamp_t_
133 {
134   /* Last time said session was seen */
135   f64 last_seen;
136   /* expire after N seconds */
137   u16 lifetime;
138   /* Users refcount, initially 3 (session, rsession, dpo) */
139   u16 refcnt;
140 } cnat_timestamp_t;
141
142 typedef struct cnat_node_ctx_t_
143 {
144   f64 now;
145   u64 seed;
146   u32 thread_index;
147   ip_address_family_t af;
148   u8 do_trace;
149 } cnat_node_ctx_t;
150
151 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
152 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
153                                      va_list * args);
154 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
155 extern cnat_timestamp_t *cnat_timestamps;
156 extern fib_source_t cnat_fib_source;
157 extern cnat_main_t cnat_main;
158 extern throttle_t cnat_throttle;
159
160 extern char *cnat_error_strings[];
161
162 typedef enum
163 {
164 #define cnat_error(n,s) CNAT_ERROR_##n,
165 #include <cnat/cnat_error.def>
166 #undef cnat_error
167   CNAT_N_ERROR,
168 } cnat_error_t;
169
170 /*
171   Dataplane functions
172 */
173
174 always_inline u32
175 cnat_timestamp_new (f64 t)
176 {
177   u32 index;
178   cnat_timestamp_t *ts;
179   clib_rwlock_writer_lock (&cnat_main.ts_lock);
180   pool_get (cnat_timestamps, ts);
181   ts->last_seen = t;
182   ts->lifetime = cnat_main.session_max_age;
183   ts->refcnt = CNAT_TIMESTAMP_INIT_REFCNT;
184   index = ts - cnat_timestamps;
185   clib_rwlock_writer_unlock (&cnat_main.ts_lock);
186   return index;
187 }
188
189 always_inline void
190 cnat_timestamp_inc_refcnt (u32 index)
191 {
192   clib_rwlock_reader_lock (&cnat_main.ts_lock);
193   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
194   ts->refcnt++;
195   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
196 }
197
198 always_inline void
199 cnat_timestamp_update (u32 index, f64 t)
200 {
201   return;
202   clib_rwlock_reader_lock (&cnat_main.ts_lock);
203   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
204   ts->last_seen = t;
205   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
206 }
207
208 always_inline void
209 cnat_timestamp_set_lifetime (u32 index, u16 lifetime)
210 {
211   clib_rwlock_reader_lock (&cnat_main.ts_lock);
212   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
213   ts->lifetime = lifetime;
214   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
215 }
216
217 always_inline f64
218 cnat_timestamp_exp (u32 index)
219 {
220   f64 t;
221   if (INDEX_INVALID == index)
222     return -1;
223   clib_rwlock_reader_lock (&cnat_main.ts_lock);
224   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
225   t = ts->last_seen + (f64) ts->lifetime;
226   clib_rwlock_reader_unlock (&cnat_main.ts_lock);
227   return t;
228 }
229
230 always_inline void
231 cnat_timestamp_free (u32 index)
232 {
233   if (INDEX_INVALID == index)
234     return;
235   clib_rwlock_writer_lock (&cnat_main.ts_lock);
236   cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
237   ts->refcnt--;
238   if (0 == ts->refcnt)
239     pool_put (cnat_timestamps, ts);
240   clib_rwlock_writer_unlock (&cnat_main.ts_lock);
241 }
242
243 always_inline void
244 cnat_free_port (u16 port)
245 {
246   cnat_main_t *cm = &cnat_main;
247   clib_spinlock_lock (&cm->src_ports_lock);
248   clib_bitmap_set_no_check (cm->src_ports, port, 0);
249   clib_spinlock_unlock (&cm->src_ports_lock);
250 }
251
252 always_inline int
253 cnat_allocate_port (cnat_main_t * cm, u16 * port)
254 {
255   *port = clib_net_to_host_u16 (*port);
256   if (*port == 0)
257     *port = MIN_SRC_PORT;
258   clib_spinlock_lock (&cm->src_ports_lock);
259   if (clib_bitmap_get_no_check (cm->src_ports, *port))
260     {
261       *port = clib_bitmap_next_clear (cm->src_ports, *port);
262       if (PREDICT_FALSE (*port >= UINT16_MAX))
263         *port = clib_bitmap_next_clear (cm->src_ports, MIN_SRC_PORT);
264       if (PREDICT_FALSE (*port >= UINT16_MAX))
265         return -1;
266     }
267   clib_bitmap_set_no_check (cm->src_ports, *port, 1);
268   *port = clib_host_to_net_u16 (*port);
269   clib_spinlock_unlock (&cm->src_ports_lock);
270   return 0;
271 }
272
273 /*
274  * fd.io coding-style-patch-verification: ON
275  *
276  * Local Variables:
277  * eval: (c-set-style "gnu")
278  * End:
279  */
280
281 #endif