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