Repair Doxygen build infrastructure
[vpp.git] / plugins / snat-plugin / snat / snat.h
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     {
37       ip4_address_t addr;
38       u16 port;
39       u16 protocol:3,
40         fib_index:13;
41     };
42     u64 as_u64;
43   };
44 } snat_session_key_t;
45
46 typedef struct {
47   union
48   {
49     struct
50     {
51       ip4_address_t addr;
52       u32 fib_index;
53     };
54     u64 as_u64;
55   };
56 } snat_user_key_t;
57
58 typedef struct {
59   union
60   {
61     struct
62     {
63       ip4_address_t addr;
64       u16 port;
65       u16 fib_index;
66     };
67     u64 as_u64;
68   };
69 } snat_static_mapping_key_t;
70
71
72 typedef enum {
73   SNAT_PROTOCOL_UDP = 0,
74   SNAT_PROTOCOL_TCP,
75   SNAT_PROTOCOL_ICMP,
76 } snat_protocol_t;
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 {
107   ip4_address_t addr;
108   u32 sessions_per_user_list_head_index;
109   u32 nsessions;
110   u32 nstaticsessions;
111 } snat_user_t;
112
113 typedef struct {
114   ip4_address_t addr;
115   u32 busy_ports;
116   uword * busy_port_bitmap;
117 } snat_address_t;
118
119 typedef struct {
120   ip4_address_t local_addr;
121   ip4_address_t external_addr;
122   u16 local_port;
123   u16 external_port;
124   u8 addr_only;
125   u32 vrf_id;
126   u32 fib_index;
127 } snat_static_mapping_t;
128
129 typedef struct {
130   u32 sw_if_index;
131   u8 is_inside;
132 } snat_interface_t;
133
134 typedef struct {
135   /* User pool */
136   snat_user_t * users;
137
138   /* Session pool */
139   snat_session_t * sessions;
140
141   /* Pool of doubly-linked list elements */
142   dlist_elt_t * list_pool;
143 } snat_main_per_thread_data_t;
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
159   u32 num_workers;
160   u32 first_worker_index;
161   u32 next_worker;
162   u32 * workers;
163
164   /* Per thread data */
165   snat_main_per_thread_data_t * 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 */
174   snat_static_mapping_t * static_mappings;
175
176   /* Interface pool */
177   snat_interface_t * interfaces;
178
179   /* Vector of outside addresses */
180   snat_address_t * addresses;
181
182   /* Randomize port allocation order */
183   u32 random_seed;
184
185   /* Worker handoff index */
186   u32 fq_in2out_index;
187   u32 fq_out2in_index;
188
189   /* Config parameters */
190   u8 static_mapping_only;
191   u8 static_mapping_connection_tracking;
192   u32 translation_buckets;
193   u32 translation_memory_size;
194   u32 user_buckets;
195   u32 user_memory_size;
196   u32 max_translations_per_user;
197   u32 outside_vrf_id;
198   u32 outside_fib_index;
199   u32 inside_vrf_id;
200   u32 inside_fib_index;
201
202   /* API message ID base */
203   u16 msg_id_base;
204
205   /* convenience */
206   vlib_main_t * vlib_main;
207   vnet_main_t * vnet_main;
208   ip4_main_t * ip4_main;
209   ip_lookup_main_t * ip4_lookup_main;
210   ethernet_main_t * ethernet_main;  
211   api_main_t * api_main;
212 } snat_main_t;
213
214 extern snat_main_t snat_main;
215 extern vlib_node_registration_t snat_in2out_node;
216 extern vlib_node_registration_t snat_out2in_node;
217 extern vlib_node_registration_t snat_in2out_fast_node;
218 extern vlib_node_registration_t snat_out2in_fast_node;
219 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
220 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
221
222 void snat_free_outside_address_and_port (snat_main_t * sm, 
223                                          snat_session_key_t * k, 
224                                          u32 address_index);
225
226 int snat_alloc_outside_address_and_port (snat_main_t * sm, 
227                                          snat_session_key_t * k,
228                                          u32 * address_indexp);
229
230 int snat_static_mapping_match (snat_main_t * sm,
231                                snat_session_key_t match,
232                                snat_session_key_t * mapping,
233                                u8 by_external);
234
235 format_function_t format_snat_user;
236
237 typedef struct {
238   u32 cached_sw_if_index;
239   u32 cached_ip4_address;
240 } snat_runtime_t;
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 {
255   u16 identifier;
256   u16 sequence;
257 } icmp_echo_header_t;
258
259 #endif /* __included_snat_h__ */