FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
cnat_types.h
Go to the documentation of this file.
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 {
57 
58 typedef struct cnat_endpoint_tuple_t_
59 {
63 
64 
65 
66 typedef struct
67 {
68  u32 dst_address_length_refcounts[129];
72 
73 typedef struct
74 {
75  /* Stores (ip family, prefix & mask) */
76  clib_bihash_24_8_t ip_hash;
77  /* family dependant cache */
79  /* Precomputed ip masks (ip4 & ip6) */
80  ip6_address_t ip_masks[129];
82 
83 typedef struct cnat_main_
84 {
85  /* Memory size of the session bihash */
87 
88  /* Number of buckets of the session bihash */
90 
91  /* Memory size of the translation bihash */
93 
94  /* Number of buckets of the translation bihash */
96 
97  /* Memory size of the source NAT prefix bihash */
99 
100  /* Number of buckets of the source NAT prefix bihash */
102 
103  /* Timeout after which to clear sessions (in seconds) */
105 
106  /* Timeout after which to clear an established TCP
107  * session (in seconds) */
109 
110  /* delay in seconds between two scans of session/clients tables */
112 
113  /* Lock for the timestamp pool */
115 
116  /* Source ports bitmap for snat */
118 
119  /* Lock for src_ports access */
121 
122  /* Ip4 Address to use for source NATing */
124 
125  /* Ip6 Address to use for source NATing */
126  ip6_address_t snat_ip6;
127 
128  /* Longest prefix Match table for source NATing */
130 } cnat_main_t;
131 
132 typedef struct cnat_timestamp_t_
133 {
134  /* Last time said session was seen */
136  /* expire after N seconds */
138  /* Users refcount, initially 3 (session, rsession, dpo) */
141 
142 typedef struct cnat_node_ctx_t_
143 {
150 
151 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
153  va_list * args);
154 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
157 extern cnat_main_t cnat_main;
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
168 } cnat_error_t;
169 
170 /*
171  Dataplane functions
172 */
173 
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;
184  index = ts - cnat_timestamps;
185  clib_rwlock_writer_unlock (&cnat_main.ts_lock);
186  return index;
187 }
188 
189 always_inline void
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
200 {
201  clib_rwlock_reader_lock (&cnat_main.ts_lock);
202  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
203  ts->last_seen = t;
204  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
205 }
206 
207 always_inline void
209 {
210  clib_rwlock_reader_lock (&cnat_main.ts_lock);
211  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
212  ts->lifetime = lifetime;
213  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
214 }
215 
218 {
219  f64 t;
220  if (INDEX_INVALID == index)
221  return -1;
222  clib_rwlock_reader_lock (&cnat_main.ts_lock);
223  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
224  t = ts->last_seen + (f64) ts->lifetime;
225  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
226  return t;
227 }
228 
229 always_inline void
231 {
232  if (INDEX_INVALID == index)
233  return;
234  clib_rwlock_writer_lock (&cnat_main.ts_lock);
235  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
236  ts->refcnt--;
237  if (0 == ts->refcnt)
238  pool_put (cnat_timestamps, ts);
239  clib_rwlock_writer_unlock (&cnat_main.ts_lock);
240 }
241 
242 always_inline void
244 {
247  clib_bitmap_set_no_check (cm->src_ports, port, 0);
249 }
250 
251 always_inline int
253 {
254  *port = clib_net_to_host_u16 (*port);
255  if (*port == 0)
256  *port = MIN_SRC_PORT;
258  if (clib_bitmap_get_no_check (cm->src_ports, *port))
259  {
260  *port = clib_bitmap_next_clear (cm->src_ports, *port);
261  if (PREDICT_FALSE (*port >= UINT16_MAX))
262  *port = clib_bitmap_next_clear (cm->src_ports, MIN_SRC_PORT);
263  if (PREDICT_FALSE (*port >= UINT16_MAX))
264  return -1;
265  }
266  clib_bitmap_set_no_check (cm->src_ports, *port, 1);
267  *port = clib_host_to_net_u16 (*port);
269  return 0;
270 }
271 
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */
279 
280 #endif
static void clib_rwlock_reader_lock(clib_rwlock_t *p)
Definition: lock.h:167
#define CNAT_TIMESTAMP_INIT_REFCNT
Definition: cnat_types.h:48
enum fib_source_t_ fib_source_t
The different sources that can create a route.
static_always_inline void clib_spinlock_unlock(clib_spinlock_t *p)
Definition: lock.h:119
static_always_inline void clib_spinlock_lock(clib_spinlock_t *p)
Definition: lock.h:80
static void cnat_timestamp_inc_refcnt(u32 index)
Definition: cnat_types.h:190
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:190
unsigned long u64
Definition: types.h:89
throttle_t cnat_throttle
Definition: cnat_types.c:21
u32 session_hash_buckets
Definition: cnat_types.h:89
cnat_endpoint_t src_ep
Definition: cnat_types.h:61
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
static uword clib_bitmap_get_no_check(uword *ai, uword i)
Gets the ith bit value from a bitmap Does not sanity-check the bit position.
Definition: bitmap.h:212
cnat_timestamp_t * cnat_timestamps
Definition: cnat_types.c:20
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
static u32 cnat_timestamp_new(f64 t)
Definition: cnat_types.h:175
static uword clib_bitmap_set_no_check(uword *a, uword i, uword new_value)
Sets the ith bit of a bitmap to new_value.
Definition: bitmap.h:141
u64 lifetime
Definition: ikev2_types.api:80
cnat_endpoint_t dst_ep
Definition: cnat_types.h:60
clib_bihash_24_8_t ip_hash
Definition: cnat_types.h:76
unsigned int u32
Definition: types.h:88
struct cnat_node_ctx_t_ cnat_node_ctx_t
static void cnat_free_port(u16 port)
Definition: cnat_types.h:243
static f64 cnat_timestamp_exp(u32 index)
Definition: cnat_types.h:217
struct cnat_main_ cnat_main_t
uword unformat_cnat_ep_tuple(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:49
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
uword unformat_cnat_ep(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:30
static void clib_rwlock_reader_unlock(clib_rwlock_t *p)
Definition: lock.h:182
static int cnat_allocate_port(cnat_main_t *cm, u16 *port)
Definition: cnat_types.h:252
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
clib_spinlock_t src_ports_lock
Definition: cnat_types.h:120
static void cnat_timestamp_free(u32 index)
Definition: cnat_types.h:230
#define PREDICT_FALSE(x)
Definition: clib.h:120
#define always_inline
Definition: ipsec.h:28
A throttle Used in the data plane to decide if a given hash should be throttled, i.e.
Definition: throttle.h:28
f64 scanner_timeout
Definition: cnat_types.h:111
#define MIN_SRC_PORT
Definition: cnat_types.h:50
char * cnat_error_strings[]
Definition: cnat_types.c:23
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:204
clib_bitmap_t * src_ports
Definition: cnat_types.h:117
static void cnat_timestamp_update(u32 index, f64 t)
Definition: cnat_types.h:199
cnat_main_t cnat_main
Definition: cnat_types.c:18
uword session_hash_memory
Definition: cnat_types.h:86
clib_rwlock_t ts_lock
Definition: cnat_types.h:114
cnat_snat_pfx_table_t snat_pfx_table
Definition: cnat_types.h:129
ip4_address_t snat_ip4
Definition: cnat_types.h:123
u32 session_max_age
Definition: cnat_types.h:104
enum ip_address_family_t_ ip_address_family_t
u32 translation_hash_buckets
Definition: cnat_types.h:95
uword * non_empty_dst_address_length_bitmap
Definition: cnat_types.h:70
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
u64 uword
Definition: types.h:112
u32 index
Definition: flow_types.api:221
fib_source_t cnat_fib_source
Definition: cnat_types.c:19
uword translation_hash_memory
Definition: cnat_types.h:92
u16 port
Definition: lb_types.api:72
u32 tcp_max_age
Definition: cnat_types.h:108
ip_address_family_t af
Definition: cnat_types.h:147
uword clib_bitmap_t
Definition: bitmap.h:50
u8 * format_cnat_endpoint(u8 *s, va_list *args)
Definition: cnat_types.c:65
ip6_address_t snat_ip6
Definition: cnat_types.h:126
ip_address_t ce_ip
Definition: cnat_types.h:54
cnat_error_t
Definition: cnat_types.h:162
struct cnat_timestamp_t_ cnat_timestamp_t
static void cnat_timestamp_set_lifetime(u32 index, u16 lifetime)
Definition: cnat_types.h:208
struct cnat_endpoint_tuple_t_ cnat_endpoint_tuple_t
struct cnat_endpoint_t_ cnat_endpoint_t
u32 snat_hash_buckets
Definition: cnat_types.h:101
uword snat_hash_memory
Definition: cnat_types.h:98