9dba2ca94da7fc0112af5461398c6bdabaea3a3c
[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 #define foreach_nat_error \
26   _(VALUE_EXIST, -1, "Value already exists") \
27   _(NO_SUCH_ENTRY, -2, "No such entry") \
28   _(UNKNOWN_PROTOCOL, -3, "Unknown protocol") \
29   _(OUT_OF_TRANSLATIONS, -4, "Out of translations")
30
31 #define foreach_nat_protocol \
32   _(UDP, 0, udp, "udp")       \
33   _(TCP, 1, tcp, "tcp")       \
34   _(ICMP, 2, icmp, "icmp")
35
36 typedef enum
37 {
38 #define _(N, i, s) NAT_ERROR_##N = i,
39   foreach_nat_error
40 #undef _
41 } nat_error_t;
42
43 typedef enum
44 {
45 #define _(N, i, n, s) NAT_PROTOCOL_##N = i,
46   foreach_nat_protocol
47 #undef _
48 } nat_protocol_t;
49
50 typedef struct nat_ip4_pool_addr_s nat_ip4_pool_addr_t;
51 typedef struct nat_ip4_addr_port_s nat_ip4_addr_port_t;
52 typedef struct nat_ip4_pool_s nat_ip4_pool_t;
53
54 typedef void (nat_add_del_ip4_pool_addr_cb_t) (ip4_address_t addr,
55                                                u8 is_add, void *opaque);
56
57 typedef int (nat_alloc_ip4_addr_and_port_cb_t) (nat_ip4_pool_t * pool,
58                                                 u32 fib_index,
59                                                 u32 thread_index,
60                                                 u32 nat_thread_index,
61                                                 u16 port_per_thread,
62                                                 u16 protocol,
63                                                 nat_ip4_addr_port_t * out);
64
65 struct nat_ip4_pool_addr_s
66 {
67   ip4_address_t addr;
68   u32 fib_index;
69 /* *INDENT-OFF* */
70 #define _(N, i, n, s) \
71   u16 busy_##n##_ports; \
72   u16 * busy_##n##_ports_per_thread; \
73   uword * busy_##n##_port_bitmap;
74   foreach_nat_protocol
75 #undef _
76 /* *INDENT-ON* */
77 };
78
79 struct nat_ip4_addr_port_s
80 {
81   ip4_address_t addr;
82   u16 port;
83 };
84
85 struct nat_ip4_pool_s
86 {
87   nat_add_del_ip4_pool_addr_cb_t *add_del_pool_addr_cb;
88   nat_alloc_ip4_addr_and_port_cb_t *alloc_addr_and_port_cb;
89   nat_ip4_pool_addr_t *pool_addr;
90   u32 random_seed;
91 };
92
93 int
94 nat_add_del_ip4_pool_addr (nat_ip4_pool_t * pool,
95                            ip4_address_t addr, u8 is_add);
96
97 int
98 nat_add_del_ip4_pool_addrs (nat_ip4_pool_t * pool,
99                             ip4_address_t addr,
100                             u32 count, u8 is_add, void *opaque);
101
102 int
103 nat_alloc_ip4_addr_and_port_cb_default (nat_ip4_pool_t * pool,
104                                         u32 fib_index,
105                                         u32 thread_index,
106                                         u32 nat_thread_index,
107                                         u16 port_per_thread,
108                                         u16 protocol,
109                                         nat_ip4_addr_port_t * out);
110
111 int
112 nat_alloc_ip4_addr_and_port (nat_ip4_pool_t * pool,
113                              u32 fib_index,
114                              u32 thread_index,
115                              u32 nat_thread_index,
116                              u16 port_per_thread,
117                              u16 protocol, nat_ip4_addr_port_t * out);
118
119 int
120 nat_free_ip4_addr_and_port (nat_ip4_pool_t * pool,
121                             u32 thread_index,
122                             u16 protocol, nat_ip4_addr_port_t * in);
123
124 #endif /* included_nat_lib_alloc_h__ */
125
126 /*
127  * fd.io coding-style-patch-verification: ON
128  *
129  * Local Variables:
130  * eval: (c-set-style "gnu")
131  * End:
132  */