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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __CNAT_TYPES_H__
17 #define __CNAT_TYPES_H__
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>
24 /* only in the default table for v4 and v6 */
25 #define CNAT_FIB_TABLE 0
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)
35 #define CNAT_DEFAULT_SESSION_BUCKETS 1024
36 #define CNAT_DEFAULT_TRANSLATION_BUCKETS 1024
37 #define CNAT_DEFAULT_SNAT_BUCKETS 1024
39 #define CNAT_DEFAULT_SESSION_MEMORY (1 << 20)
40 #define CNAT_DEFAULT_TRANSLATION_MEMORY (256 << 10)
41 #define CNAT_DEFAULT_SNAT_MEMORY (64 << 20)
43 /* This should be strictly lower than FIB_SOURCE_INTERFACE
44 * from fib_source.h */
45 #define CNAT_FIB_SOURCE_PRIORITY 0x02
47 /* Initial refcnt for timestamps (2 : session & rsession) */
48 #define CNAT_TIMESTAMP_INIT_REFCNT 2
50 #define MIN_SRC_PORT ((u16) 0xC000)
52 typedef struct cnat_endpoint_t_
58 typedef struct cnat_endpoint_tuple_t_
60 cnat_endpoint_t dst_ep;
61 cnat_endpoint_t src_ep;
62 } cnat_endpoint_tuple_t;
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;
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;
83 typedef struct cnat_main_
85 /* Memory size of the session bihash */
86 uword session_hash_memory;
88 /* Number of buckets of the session bihash */
89 u32 session_hash_buckets;
91 /* Memory size of the translation bihash */
92 uword translation_hash_memory;
94 /* Number of buckets of the translation bihash */
95 u32 translation_hash_buckets;
97 /* Memory size of the source NAT prefix bihash */
98 uword snat_hash_memory;
100 /* Number of buckets of the source NAT prefix bihash */
101 u32 snat_hash_buckets;
103 /* Timeout after which to clear sessions (in seconds) */
106 /* Timeout after which to clear an established TCP
107 * session (in seconds) */
110 /* delay in seconds between two scans of session/clients tables */
113 /* Lock for the timestamp pool */
114 clib_rwlock_t ts_lock;
116 /* Source ports bitmap for snat */
117 clib_bitmap_t *src_ports;
119 /* Lock for src_ports access */
120 clib_spinlock_t src_ports_lock;
122 /* Ip4 Address to use for source NATing */
123 ip4_address_t snat_ip4;
125 /* Ip6 Address to use for source NATing */
126 ip6_address_t snat_ip6;
128 /* Longest prefix Match table for source NATing */
129 cnat_snat_pfx_table_t snat_pfx_table;
132 typedef struct cnat_timestamp_t_
134 /* Last time said session was seen */
136 /* expire after N seconds */
138 /* Users refcount, initially 3 (session, rsession, dpo) */
142 typedef struct cnat_node_ctx_t_
147 ip_address_family_t af;
151 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
152 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
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;
160 extern char *cnat_error_strings[];
164 #define cnat_error(n,s) CNAT_ERROR_##n,
165 #include <cnat/cnat_error.def>
175 cnat_timestamp_new (f64 t)
178 cnat_timestamp_t *ts;
179 clib_rwlock_writer_lock (&cnat_main.ts_lock);
180 pool_get (cnat_timestamps, ts);
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);
190 cnat_timestamp_inc_refcnt (u32 index)
192 clib_rwlock_reader_lock (&cnat_main.ts_lock);
193 cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
195 clib_rwlock_reader_unlock (&cnat_main.ts_lock);
199 cnat_timestamp_update (u32 index, f64 t)
202 clib_rwlock_reader_lock (&cnat_main.ts_lock);
203 cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
205 clib_rwlock_reader_unlock (&cnat_main.ts_lock);
209 cnat_timestamp_set_lifetime (u32 index, u16 lifetime)
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);
218 cnat_timestamp_exp (u32 index)
221 if (INDEX_INVALID == index)
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);
231 cnat_timestamp_free (u32 index)
233 if (INDEX_INVALID == index)
235 clib_rwlock_writer_lock (&cnat_main.ts_lock);
236 cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
239 pool_put (cnat_timestamps, ts);
240 clib_rwlock_writer_unlock (&cnat_main.ts_lock);
244 cnat_free_port (u16 port)
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);
253 cnat_allocate_port (cnat_main_t * cm, u16 * port)
255 *port = clib_net_to_host_u16 (*port);
257 *port = MIN_SRC_PORT;
258 clib_spinlock_lock (&cm->src_ports_lock);
259 if (clib_bitmap_get_no_check (cm->src_ports, *port))
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))
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);
274 * fd.io coding-style-patch-verification: ON
277 * eval: (c-set-style "gnu")