nat: static mappings in flow hash
[vpp.git] / src / plugins / nat / nat64 / nat64_db.h
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 #ifndef __included_nat64_db_h__
16 #define __included_nat64_db_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/fib/fib_source.h>
21 #include <nat/lib/nat_proto.h>
22
23 #include <vppinfra/bihash_24_8.h>
24 #include <vppinfra/bihash_48_8.h>
25
26 typedef struct
27 {
28   u32 bib_buckets;
29   u32 bib_memory_size;
30   u32 st_buckets;
31   u32 st_memory_size;
32 } nat64_config_t;
33
34 typedef struct
35 {
36   union
37   {
38     struct
39     {
40       ip46_address_t addr;
41       u32 fib_index;
42       u16 port;
43       u8 proto;
44       u8 rsvd;
45     };
46     u64 as_u64[3];
47   };
48 } nat64_db_bib_entry_key_t;
49
50 /* *INDENT-OFF* */
51 typedef CLIB_PACKED(struct
52 {
53   ip6_address_t in_addr;
54   u16 in_port;
55   ip4_address_t out_addr;
56   u16 out_port;
57   u32 fib_index;
58   u32 ses_num;
59   u8 proto;
60   u8 is_static;
61 }) nat64_db_bib_entry_t;
62 /* *INDENT-ON* */
63
64 typedef struct
65 {
66   /* BIBs */
67 /* *INDENT-OFF* */
68 #define _(N, i, n, s) \
69   nat64_db_bib_entry_t *_##n##_bib;
70   foreach_nat_protocol
71 #undef _
72 /* *INDENT-ON* */
73   nat64_db_bib_entry_t *_unk_proto_bib;
74
75   /* BIB lookup */
76   clib_bihash_24_8_t in2out;
77   clib_bihash_24_8_t out2in;
78
79   u32 limit;
80   u32 bib_entries_num;
81 } nat64_db_bib_t;
82
83 typedef struct
84 {
85   union
86   {
87     struct
88     {
89       ip46_address_t l_addr;
90       ip46_address_t r_addr;
91       u32 fib_index;
92       u16 l_port;
93       u16 r_port;
94       u8 proto;
95       u8 rsvd[7];
96     };
97     u64 as_u64[6];
98   };
99 } nat64_db_st_entry_key_t;
100
101 /* *INDENT-OFF* */
102 typedef CLIB_PACKED(struct
103 {
104   ip6_address_t in_r_addr;
105   ip4_address_t out_r_addr;
106   u16 r_port;
107   u32 bibe_index;
108   u32 expire;
109   u8 proto;
110   u8 tcp_state;
111 }) nat64_db_st_entry_t;
112 /* *INDENT-ON* */
113
114 typedef struct
115 {
116   /* session tables */
117 /* *INDENT-OFF* */
118 #define _(N, i, n, s) \
119   nat64_db_st_entry_t *_##n##_st;
120   foreach_nat_protocol
121 #undef _
122 /* *INDENT-ON* */
123   nat64_db_st_entry_t *_unk_proto_st;
124
125   /* session lookup */
126   clib_bihash_48_8_t in2out;
127   clib_bihash_48_8_t out2in;
128
129   u32 limit;
130   u32 st_entries_num;
131 } nat64_db_st_t;
132
133 struct nat64_db_s;
134
135 /**
136  * @brief Call back function to free NAT64 pool address and port when BIB
137  * entry is deleted.
138  */
139 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
140                                                     ip4_address_t * addr,
141                                                     u16 port, u8 proto);
142
143 typedef struct nat64_db_s
144 {
145   nat64_db_bib_t bib;
146   nat64_db_st_t st;
147   nat64_db_free_addr_port_function_t free_addr_port_cb;
148   u8 addr_free;
149 } nat64_db_t;
150
151 /**
152  * @brief Initialize NAT64 DB.
153  *
154  * @param db NAT64 DB.
155  * @param c.bib_buckets Number of BIB hash buckets.
156  * @param c.bib_memory_size Memory size of BIB hash.
157  * @param c.st_buckets Number of session table hash buckets.
158  * @param c.st_memory_size Memory size of session table hash.
159  * @param free_addr_port_cb Call back function to free address and port.
160  *
161  * @returns 0 on success, non-zero value otherwise.
162  */
163 int nat64_db_init (nat64_db_t * db, nat64_config_t c,
164                    nat64_db_free_addr_port_function_t free_addr_port_cb);
165
166 /**
167  * @brief Free NAT64 DB.
168  *
169  * @param db NAT64 DB.
170  *
171  * @returns 0 on success, non-zero value otherwise.
172  */
173 int nat64_db_free (nat64_db_t * db);
174
175
176 /**
177  * @brief Create new NAT64 BIB entry.
178  *
179  * @param thread_index thread index.
180  * @param db NAT64 DB.
181  * @param in_addr Inside IPv6 address.
182  * @param out_addr Outside IPv4 address.
183  * @param in_port Inside port number.
184  * @param out_port Outside port number.
185  * @param fib_index FIB index.
186  * @param proto L4 protocol.
187  * @param is_static 1 if static, 0 if dynamic.
188  *
189  * @returns BIB entry on success, 0 otherwise.
190  */
191 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
192                                                  nat64_db_t * db,
193                                                  ip6_address_t * in_addr,
194                                                  ip4_address_t * out_addr,
195                                                  u16 in_port, u16 out_port,
196                                                  u32 fib_index, u8 proto,
197                                                  u8 is_static);
198
199 /**
200  * @brief Free NAT64 BIB entry.
201  *
202  * @param thread_index thread index.
203  * @param db NAT64 DB.
204  * @param bibe BIB entry.
205  */
206 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
207                               nat64_db_bib_entry_t * bibe);
208
209 /**
210  * @brief Call back function when walking NAT64 BIB, non-zero
211  * return value stop walk.
212  */
213 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
214                                        void *ctx);
215 /**
216  * @brief Walk NAT64 BIB.
217  *
218  * @param db NAT64 DB.
219  * @param proto BIB L4 protocol:
220  *  - 255 all BIBs
221  *  - 6 TCP BIB
222  *  - 17 UDP BIB
223  *  - 1/58 ICMP BIB
224  *
225  * u - otherwise "unknown" protocol BIB
226  * @param fn The function to invoke on each entry visited.
227  * @param ctx A context passed in the visit function.
228  */
229 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
230                         nat64_db_bib_walk_fn_t fn, void *ctx);
231
232 /**
233  * @brief Find NAT64 BIB entry.
234  *
235  * @param db NAT64 DB.
236  * @param addr IP address.
237  * @param port Port number.
238  * @param proto L4 protocol.
239  * @param fib_index FIB index.
240  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
241  *
242  * @return BIB entry if found.
243  */
244 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
245                                                ip46_address_t * addr,
246                                                u16 port,
247                                                u8 proto,
248                                                u32 fib_index, u8 is_ip6);
249
250 /**
251  * @brief Get BIB entry by index and protocol.
252  *
253  * @param db NAT64 DB.
254  * @param proto L4 protocol.
255  * @param bibe_index BIB entry index.
256  *
257  * @return BIB entry if found.
258  */
259 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
260                                                    u8 proto, u32 bibe_index);
261 /**
262  * @brief Create new NAT64 session table entry.
263  *
264  * @param thread_index thread index.
265  * @param db NAT64 DB.
266  * @param bibe Corresponding BIB entry.
267  * @param in_r_addr Inside IPv6 address of the remote host.
268  * @param out_r_addr Outside IPv4 address of the remote host.
269  * @param r_port Remote host port number.
270  *
271  * @returns BIB entry on success, 0 otherwise.
272  */
273 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
274                                                nat64_db_t * db,
275                                                nat64_db_bib_entry_t * bibe,
276                                                ip6_address_t * in_r_addr,
277                                                ip4_address_t * out_r_addr,
278                                                u16 r_port);
279
280 /**
281  * @brief Free NAT64 session table entry.
282  *
283  * @param thread_index thread index.
284  * @param db NAT64 DB.
285  * @param ste Session table entry.
286  */
287 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
288                              nat64_db_st_entry_t * ste);
289
290 /**
291  * @brief Find NAT64 session table entry.
292  *
293  * @param db NAT64 DB.
294  * @param l_addr Local host address.
295  * @param r_addr Remote host address.
296  * @param l_port Local host port number.
297  * @param r_port Remote host port number.
298  * @param proto L4 protocol.
299  * @param fib_index FIB index.
300  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
301  *
302  * @return BIB entry if found.
303  */
304 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
305                                              ip46_address_t * l_addr,
306                                              ip46_address_t * r_addr,
307                                              u16 l_port, u16 r_port,
308                                              u8 proto,
309                                              u32 fib_index, u8 is_ip6);
310
311 /**
312  * @brief Call back function when walking NAT64 session table, non-zero
313  * return value stop walk.
314  */
315 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
316
317 /**
318  * @brief Walk NAT64 session table.
319  *
320  * @param db NAT64 DB.
321  * @param proto L4 protocol:
322  *  - 255 all session tables
323  *  - 6 TCP session table
324  *  - 17 UDP session table
325  *  - 1/58 ICMP session table
326  *  - otherwise "unknown" protocol session table
327  * @param fn The function to invoke on each entry visited.
328  * @param ctx A context passed in the visit function.
329  */
330 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
331                        nat64_db_st_walk_fn_t fn, void *ctx);
332
333 /**
334  * @brief Free expired session entries in session tables.
335  *
336  * @param thread_index thread index.
337  * @param db NAT64 DB.
338  * @param now Current time.
339  */
340 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
341
342 /**
343  * @brief Free sessions using specific outside address.
344  *
345  * @param thread_index thread index.
346  * @param db NAT64 DB.
347  * @param out_addr Outside address to match.
348  */
349 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
350                              ip4_address_t * out_addr);
351
352 /*
353  * @brief Get ST entry index.
354  *
355  * @param db NAT64 DB.
356  * @param ste ST entry.
357  *
358  * @return ST entry index on success, ~0 otherwise.
359  */
360 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
361
362 /**
363  * @brief Get ST entry by index and protocol.
364  *
365  * @param db NAT64 DB.
366  * @param proto L4 protocol.
367  * @param bibe_index ST entry index.
368  *
369  * @return BIB entry if found.
370  */
371 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
372                                                  u8 proto, u32 ste_index);
373 #endif /* __included_nat64_db_h__ */
374
375 /*
376  * fd.io coding-style-patch-verification: ON
377  *
378  * Local Variables:
379  * eval: (c-set-style "gnu")
380  * End:
381  */