FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
snat.h
Go to the documentation of this file.
1 
2 /*
3  * snat.h - simple nat definitions
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_snat_h__
19 #define __included_snat_h__
20 
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/dlist.h>
28 #include <vppinfra/error.h>
29 #include <vlibapi/api.h>
30 
31 /* Key */
32 typedef struct {
33  union
34  {
35  struct
36  {
39  u16 protocol:3,
40  fib_index:13;
41  };
43  };
45 
46 typedef struct {
47  union
48  {
49  struct
50  {
53  };
55  };
57 
58 typedef struct {
59  union
60  {
61  struct
62  {
66  };
68  };
70 
71 
72 typedef enum {
77 
78 
79 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
80 
81 typedef CLIB_PACKED(struct {
82  snat_session_key_t out2in; /* 0-15 */
83 
84  snat_session_key_t in2out; /* 16-31 */
85 
86  u32 flags; /* 32-35 */
87 
88  /* per-user translations */
89  u32 per_user_index; /* 36-39 */
90 
91  u32 per_user_list_head_index; /* 40-43 */
92 
93  /* Last heard timer */
94  f64 last_heard; /* 44-51 */
95 
96  u64 total_bytes; /* 52-59 */
97 
98  u32 total_pkts; /* 60-63 */
99 
100  /* Outside address */
101  u32 outside_address_index; /* 64-67 */
102 
103 }) snat_session_t;
104 
105 
106 typedef struct {
111 } snat_user_t;
112 
113 typedef struct {
118 
119 typedef struct {
128 
129 typedef struct {
133 
134 typedef struct {
135  /* User pool */
137 
138  /* Session pool */
139  snat_session_t * sessions;
140 
141  /* Pool of doubly-linked list elements */
144 
145 typedef struct {
146  /* Main lookup tables */
147  clib_bihash_8_8_t out2in;
148  clib_bihash_8_8_t in2out;
149 
150  /* Find-a-user => src address lookup */
151  clib_bihash_8_8_t user_hash;
152 
153  /* Non-translated packets worker lookup => src address + VRF */
154  clib_bihash_8_8_t worker_by_in;
155 
156  /* Translated packets worker lookup => IP address + port number */
157  clib_bihash_8_8_t worker_by_out;
158 
163 
164  /* Per thread data */
166 
167  /* Find a static mapping by local */
168  clib_bihash_8_8_t static_mapping_by_local;
169 
170  /* Find a static mapping by external */
171  clib_bihash_8_8_t static_mapping_by_external;
172 
173  /* Static mapping pool */
175 
176  /* Interface pool */
178 
179  /* Vector of outside addresses */
181 
182  /* Randomize port allocation order */
184 
185  /* Worker handoff index */
188 
189  /* Config parameters */
201 
202  /* API message ID base */
203  u16 msg_id_base;
204 
205  /* convenience */
212 } snat_main_t;
213 
214 extern snat_main_t snat_main;
221 
223  snat_session_key_t * k,
224  u32 address_index);
225 
227  snat_session_key_t * k,
228  u32 * address_indexp);
229 
231  snat_session_key_t match,
232  snat_session_key_t * mapping,
233  u8 by_external);
234 
236 
237 typedef struct {
241 
242 /** \brief Check if SNAT session is created from static mapping.
243  @param s SNAT session
244  @return 1 if SNAT session is created from static mapping otherwise 0
245 */
246 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
247 
248 /*
249  * Why is this here? Because we don't need to touch this layer to
250  * simply reply to an icmp. We need to change id to a unique
251  * value to NAT an echo request/reply.
252  */
253 
254 typedef struct {
258 
259 #endif /* __included_snat_h__ */
ip4_address_t external_addr
Definition: snat.h:121
u32 translation_memory_size
Definition: snat.h:193
clib_bihash_8_8_t static_mapping_by_external
Definition: snat.h:171
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external)
Match SNAT static mapping.
Definition: snat.c:1242
typedef CLIB_PACKED(struct{snat_session_key_t out2in;snat_session_key_t in2out;u32 flags;u32 per_user_index;u32 per_user_list_head_index;f64 last_heard;u64 total_bytes;u32 total_pkts;u32 outside_address_index;}) snat_session_t
u32 sessions_per_user_list_head_index
Definition: snat.h:108
u32 max_translations_per_user
Definition: snat.h:196
vlib_main_t * vlib_main
Definition: snat.h:206
u32 busy_ports
Definition: snat.h:115
int snat_alloc_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 *address_indexp)
Definition: snat.c:1292
u32 nsessions
Definition: snat.h:109
clib_bihash_8_8_t out2in
Definition: snat.h:147
u8 static_mapping_connection_tracking
Definition: snat.h:191
ip_lookup_main_t * ip4_lookup_main
Definition: snat.h:209
vlib_node_registration_t snat_out2in_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node)
Definition: out2in.c:80
u32 nstaticsessions
Definition: snat.h:110
struct _vlib_node_registration vlib_node_registration_t
u32 fq_out2in_index
Definition: snat.h:187
dlist_elt_t * list_pool
Definition: snat.h:142
u32 * workers
Definition: snat.h:162
snat_interface_t * interfaces
Definition: snat.h:177
u32 inside_vrf_id
Definition: snat.h:199
u32 cached_sw_if_index
Definition: snat.h:238
u32 random_seed
Definition: snat.h:183
snat_main_per_thread_data_t * per_thread_data
Definition: snat.h:165
ip4_address_t addr
Definition: snat.h:107
ip4_main_t * ip4_main
Definition: snat.h:208
snat_static_mapping_t * static_mappings
Definition: snat.h:174
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
Definition: in2out.c:84
unsigned long u64
Definition: types.h:89
ip4_address_t local_addr
Definition: snat.h:120
format_function_t format_snat_user
Definition: snat.h:235
clib_bihash_8_8_t worker_by_in
Definition: snat.h:154
u32 translation_buckets
Definition: snat.h:192
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
Definition: in2out.c:87
void snat_free_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 address_index)
Definition: snat.c:1213
snat_user_t * users
Definition: snat.h:136
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
Definition: out2in.c:78
api_main_t * api_main
Definition: snat.h:211
u8 static_mapping_only
Definition: snat.h:190
clib_bihash_8_8_t user_hash
Definition: snat.h:151
snat_address_t * addresses
Definition: snat.h:180
u32 inside_fib_index
Definition: snat.h:200
ip4_address_t addr
Definition: snat.h:37
u32 sw_if_index
Definition: snat.h:130
clib_bihash_8_8_t static_mapping_by_local
Definition: snat.h:168
snat_protocol_t
Definition: snat.h:72
unsigned int u32
Definition: types.h:88
u32 num_workers
Definition: snat.h:159
IPv4 main type.
Definition: ip4.h:95
vlib_node_registration_t snat_out2in_fast_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_fast_node)
Definition: out2in.c:79
u64 as_u64
Definition: snat.h:54
vlib_node_registration_t snat_in2out_fast_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_fast_node)
Definition: in2out.c:86
snat_main_t snat_main
Definition: jvpp_snat.h:42
clib_bihash_8_8_t in2out
Definition: snat.h:148
ip4_address_t addr
Definition: snat.h:51
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
u32 next_worker
Definition: snat.h:161
ip4_address_t addr
Definition: snat.h:114
u64 uword
Definition: types.h:112
u32 fq_in2out_index
Definition: snat.h:186
u32 user_buckets
Definition: snat.h:194
unsigned short u16
Definition: types.h:57
vnet_main_t * vnet_main
Definition: snat.h:207
uword * busy_port_bitmap
Definition: snat.h:116
u32 outside_fib_index
Definition: snat.h:198
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
u32 first_worker_index
Definition: snat.h:160
u32 fib_index
Definition: snat.h:52
u32 user_memory_size
Definition: snat.h:195
ip4_address_t addr
Definition: snat.h:63
ethernet_main_t * ethernet_main
Definition: snat.h:210
u32 flags
Definition: vhost-user.h:75
snat_session_t * sessions
Definition: snat.h:139
u32 cached_ip4_address
Definition: snat.h:239
u32 outside_vrf_id
Definition: snat.h:197
clib_bihash_8_8_t worker_by_out
Definition: snat.h:157