nat: static mappings in flow hash
[vpp.git] / src / plugins / nat / lib / alloc.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 /**
16  * @file
17  * @brief NAT port/address allocation lib
18  */
19
20 #ifndef included_nat_lib_alloc_h__
21 #define included_nat_lib_alloc_h__
22
23 #include <vnet/ip/ip.h>
24 #include <nat/lib/nat_proto.h>
25
26 typedef struct nat_ip4_pool_addr_s nat_ip4_pool_addr_t;
27 typedef struct nat_ip4_addr_port_s nat_ip4_addr_port_t;
28 typedef struct nat_ip4_pool_s nat_ip4_pool_t;
29
30 typedef void (nat_add_del_ip4_pool_addr_cb_t) (ip4_address_t addr,
31                                                u8 is_add, void *opaque);
32
33 typedef int (nat_alloc_ip4_addr_and_port_cb_t) (nat_ip4_pool_t * pool,
34                                                 u32 fib_index,
35                                                 u32 thread_index,
36                                                 u32 nat_thread_index,
37                                                 u16 port_per_thread,
38                                                 u16 protocol,
39                                                 nat_ip4_addr_port_t * out);
40
41 struct nat_ip4_pool_addr_s
42 {
43   ip4_address_t addr;
44   u32 fib_index;
45 /* *INDENT-OFF* */
46 #define _(N, i, n, s) \
47   u16 busy_##n##_ports; \
48   u16 * busy_##n##_ports_per_thread; \
49   uword * busy_##n##_port_bitmap;
50   foreach_nat_protocol
51 #undef _
52 /* *INDENT-ON* */
53 };
54
55 struct nat_ip4_addr_port_s
56 {
57   ip4_address_t addr;
58   u16 port;
59 };
60
61 struct nat_ip4_pool_s
62 {
63   nat_add_del_ip4_pool_addr_cb_t *add_del_pool_addr_cb;
64   nat_alloc_ip4_addr_and_port_cb_t *alloc_addr_and_port_cb;
65   nat_ip4_pool_addr_t *pool_addr;
66   u32 random_seed;
67 };
68
69 int
70 nat_add_del_ip4_pool_addr (nat_ip4_pool_t * pool,
71                            ip4_address_t addr, u8 is_add);
72
73 int
74 nat_add_del_ip4_pool_addrs (nat_ip4_pool_t * pool,
75                             ip4_address_t addr,
76                             u32 count, u8 is_add, void *opaque);
77
78 int
79 nat_alloc_ip4_addr_and_port_cb_default (nat_ip4_pool_t * pool,
80                                         u32 fib_index,
81                                         u32 thread_index,
82                                         u32 nat_thread_index,
83                                         u16 port_per_thread,
84                                         u16 protocol,
85                                         nat_ip4_addr_port_t * out);
86
87 int
88 nat_alloc_ip4_addr_and_port (nat_ip4_pool_t * pool,
89                              u32 fib_index,
90                              u32 thread_index,
91                              u32 nat_thread_index,
92                              u16 port_per_thread,
93                              u16 protocol, nat_ip4_addr_port_t * out);
94
95 int
96 nat_free_ip4_addr_and_port (nat_ip4_pool_t * pool,
97                             u32 thread_index,
98                             u16 protocol, nat_ip4_addr_port_t * in);
99
100 #endif /* included_nat_lib_alloc_h__ */
101
102 /*
103  * fd.io coding-style-patch-verification: ON
104  *
105  * Local Variables:
106  * eval: (c-set-style "gnu")
107  * End:
108  */