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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT port/address allocation lib
20 #ifndef included_nat_lib_alloc_h__
21 #define included_nat_lib_alloc_h__
23 #include <vnet/ip/ip.h>
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")
31 #define foreach_nat_protocol \
32 _(UDP, 0, udp, "udp") \
33 _(TCP, 1, tcp, "tcp") \
34 _(ICMP, 2, icmp, "icmp")
38 #define _(N, i, s) NAT_ERROR_##N = i,
45 #define _(N, i, n, s) NAT_PROTOCOL_##N = i,
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;
54 typedef void (nat_add_del_ip4_pool_addr_cb_t) (ip4_address_t addr,
55 u8 is_add, void *opaque);
57 typedef int (nat_alloc_ip4_addr_and_port_cb_t) (nat_ip4_pool_t * pool,
63 nat_ip4_addr_port_t * out);
65 struct nat_ip4_pool_addr_s
70 #define _(N, i, n, s) \
71 u16 busy_##n##_ports; \
72 u16 * busy_##n##_ports_per_thread; \
73 uword * busy_##n##_port_bitmap;
79 struct nat_ip4_addr_port_s
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;
94 nat_add_del_ip4_pool_addr (nat_ip4_pool_t * pool,
95 ip4_address_t addr, u8 is_add);
98 nat_add_del_ip4_pool_addrs (nat_ip4_pool_t * pool,
100 u32 count, u8 is_add, void *opaque);
103 nat_alloc_ip4_addr_and_port_cb_default (nat_ip4_pool_t * pool,
106 u32 nat_thread_index,
109 nat_ip4_addr_port_t * out);
112 nat_alloc_ip4_addr_and_port (nat_ip4_pool_t * pool,
115 u32 nat_thread_index,
117 u16 protocol, nat_ip4_addr_port_t * out);
120 nat_free_ip4_addr_and_port (nat_ip4_pool_t * pool,
122 u16 protocol, nat_ip4_addr_port_t * in);
124 #endif /* included_nat_lib_alloc_h__ */
127 * fd.io coding-style-patch-verification: ON
130 * eval: (c-set-style "gnu")