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