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;
72 u32 dst_address_length_refcounts[129];
73 u16 *prefix_lengths_in_search_order;
74 uword *non_empty_dst_address_length_bitmap;
75 } cnat_snat_pfx_table_meta_t;
79 /* Stores (ip family, prefix & mask) */
80 clib_bihash_24_8_t ip_hash;
81 /* family dependant cache */
82 cnat_snat_pfx_table_meta_t meta[2];
83 /* Precomputed ip masks (ip4 & ip6) */
84 ip6_address_t ip_masks[129];
85 } cnat_snat_pfx_table_t;
87 typedef struct cnat_main_
89 /* Memory size of the session bihash */
90 uword session_hash_memory;
92 /* Number of buckets of the session bihash */
93 u32 session_hash_buckets;
95 /* Memory size of the translation bihash */
96 uword translation_hash_memory;
98 /* Number of buckets of the translation bihash */
99 u32 translation_hash_buckets;
101 /* Memory size of the source NAT prefix bihash */
102 uword snat_hash_memory;
104 /* Number of buckets of the source NAT prefix bihash */
105 u32 snat_hash_buckets;
107 /* Timeout after which to clear sessions (in seconds) */
110 /* Timeout after which to clear an established TCP
111 * session (in seconds) */
114 /* delay in seconds between two scans of session/clients tables */
117 /* Lock for the timestamp pool */
118 clib_rwlock_t ts_lock;
120 /* Ip4 Address to use for source NATing */
121 ip4_address_t snat_ip4;
123 /* Ip6 Address to use for source NATing */
124 ip6_address_t snat_ip6;
126 /* Longest prefix Match table for source NATing */
127 cnat_snat_pfx_table_t snat_pfx_table;
129 /* Index of the scanner process node */
130 uword scanner_node_index;
132 /* Did we do lazy init ? */
135 /* Enable or Disable the scanner on startup */
136 u8 default_scanner_state;
139 typedef struct cnat_timestamp_t_
141 /* Last time said session was seen */
143 /* expire after N seconds */
145 /* Users refcount, initially 3 (session, rsession, dpo) */
149 typedef struct cnat_node_ctx_
154 ip_address_family_t af;
158 cnat_main_t *cnat_get_main ();
159 extern u8 *format_cnat_endpoint (u8 * s, va_list * args);
160 extern uword unformat_cnat_ep_tuple (unformat_input_t * input,
162 extern uword unformat_cnat_ep (unformat_input_t * input, va_list * args);
163 extern cnat_timestamp_t *cnat_timestamps;
164 extern fib_source_t cnat_fib_source;
165 extern cnat_main_t cnat_main;
166 extern throttle_t cnat_throttle;
168 extern char *cnat_error_strings[];
172 #define cnat_error(n,s) CNAT_ERROR_##n,
173 #include <cnat/cnat_error.def>
178 typedef enum cnat_scanner_cmd_t_
182 } cnat_scanner_cmd_t;
185 * Lazy initialization when first adding a translation
188 extern void cnat_lazy_init ();
191 * Enable/Disable session cleanup
193 extern void cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type);
196 * fd.io coding-style-patch-verification: ON
199 * eval: (c-set-style "gnu")