FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
alloc.c
Go to the documentation of this file.
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 #include <nat/lib/lib.h>
21 #include <nat/lib/alloc.h>
22 
25 {
26  u32 v;
27  v = clib_net_to_host_u32 (addr->as_u32) + 1;
28  addr->as_u32 = clib_host_to_net_u32 (v);
29 }
30 
31 int
33  u8 is_add)
34 {
35  int i;
38 
39  // lookup for the address
40  for (i = 0; i < vec_len (pool->pool_addr); i++)
41  {
42  if (pool->pool_addr[i].addr.as_u32 == addr.as_u32)
43  {
44  a = pool->pool_addr + 1;
45  break;
46  }
47  }
48  if (is_add)
49  {
50  if (a)
51  return NAT_ERROR_VALUE_EXIST;
52  vec_add2 (pool->pool_addr, a, 1);
53  a->addr = addr;
54 #define _(N, i, n, s) \
55  clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
56  a->busy_##n##_ports = 0; \
57  vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
59 #undef _
60  }
61  else
62  {
63  if (!a)
64  return NAT_ERROR_NO_SUCH_ENTRY;
65 #define _(N, id, n, s) \
66  clib_bitmap_free (a->busy_##n##_port_bitmap); \
67  vec_free (a->busy_##n##_ports_per_thread);
69 #undef _
70  vec_del1 (pool->pool_addr, i);
71  }
72  return 0;
73 }
74 
75 int
77  ip4_address_t addr, u32 count, u8 is_add,
78  void *opaque)
79 {
80  int i, rv;
81 
82  for (i = 0; i < count; i++)
83  {
84  // TODO:
85  // a) consider if we could benefit from pre and post cb
86  // b) consider if we could benefit from add/del cb separation
87 
88  // pre call:
89  // pool->add_del_pool_addr_pre_cb (&addr, is_add, opaque);
90 
91  if ((rv = nat_add_del_ip4_pool_addr (pool, addr, is_add)) != 0)
92  return rv;
93 
94  // post call:
95  // pool->add_del_pool_addr_post_cb (&addr, is_add, opaque);
96 
97  pool->add_del_pool_addr_cb (addr, is_add, opaque);
99  }
100 
101  return 0;
102 }
103 
105 nat_random_port (u32 * random_seed, u16 min, u16 max)
106 {
107  return min + random_u32 (random_seed) /
108  (random_u32_max () / (max - min + 1) + 1);
109 }
110 
111 int
113  u32 fib_index,
115  u32 nat_thread_index,
116  u16 port_per_thread,
117  u16 protocol,
118  nat_ip4_addr_port_t * out)
119 {
120  nat_ip4_pool_addr_t *a, *ga = 0;
121  u32 i;
122  u32 portnum;
123 
124  for (i = 0; i < vec_len (pool->pool_addr); i++)
125  {
126  a = pool->pool_addr + i;
127  switch (protocol)
128  {
129 #define _(N, j, n, s) \
130  case NAT_PROTOCOL_##N: \
131  if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
132  { \
133  if (a->fib_index == fib_index) \
134  { \
135  while (1) \
136  { \
137  portnum = (port_per_thread * \
138  nat_thread_index) + \
139  nat_random_port(&pool->random_seed, 1, port_per_thread) + 1024; \
140  if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
141  continue; \
142  clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
143  a->busy_##n##_ports_per_thread[thread_index]++; \
144  a->busy_##n##_ports++; \
145  out->addr = a->addr; \
146  out->port = clib_host_to_net_u16(portnum); \
147  return 0; \
148  } \
149  } \
150  else if (a->fib_index == ~0) \
151  { \
152  ga = a; \
153  } \
154  } \
155  break;
157 #undef _
158  default:
159  return NAT_ERROR_UNKNOWN_PROTOCOL;
160  }
161 
162  }
163  if (ga)
164  {
165  a = ga;
166  switch (protocol)
167  {
168 #define _(N, j, n, s) \
169  case NAT_PROTOCOL_##N: \
170  while (1) \
171  { \
172  portnum = (port_per_thread * \
173  nat_thread_index) + \
174  nat_random_port(&pool->random_seed, 1, port_per_thread) + 1024; \
175  if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
176  continue; \
177  clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
178  a->busy_##n##_ports_per_thread[thread_index]++; \
179  a->busy_##n##_ports++; \
180  out->addr = a->addr; \
181  out->port = clib_host_to_net_u16(portnum); \
182  return 0; \
183  }
184  break;
186 #undef _
187  default:
188  return NAT_ERROR_UNKNOWN_PROTOCOL;
189  }
190  }
191  return NAT_ERROR_OUT_OF_TRANSLATIONS;
192 }
193 
194 int
196  u32 fib_index,
198  u32 nat_thread_index,
199  u16 port_per_thread,
201 {
202  return pool->alloc_addr_and_port_cb (pool,
203  fib_index,
204  thread_index,
205  nat_thread_index,
206  port_per_thread, protocol, out);
207 }
208 
209 // TODO: consider using standard u16 port and ip4_address_t as input ?
210 int
213  u16 protocol, nat_ip4_addr_port_t * addr_port)
214 {
215  nat_ip4_pool_addr_t *a = 0;
216  u32 i;
217  u16 port = clib_net_to_host_u16 (addr_port->port);
218 
219  for (i = 0; i < vec_len (pool->pool_addr); i++)
220  {
221  if (pool->pool_addr[i].addr.as_u32 == addr_port->addr.as_u32)
222  {
223  a = pool->pool_addr + i;
224  break;
225  }
226  }
227 
228  if (!a)
229  {
230  return NAT_ERROR_NO_SUCH_ENTRY;
231  }
232 
233  switch (protocol)
234  {
235 #define _(N, i, n, s) \
236  case NAT_PROTOCOL_##N: \
237  ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
238  port) == 1); \
239  clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
240  port, 0); \
241  a->busy_##n##_ports--; \
242  a->busy_##n##_ports_per_thread[thread_index]--; \
243  break;
245 #undef _
246  default:
247  return NAT_ERROR_UNKNOWN_PROTOCOL;
248  }
249  return 0;
250 }
251 
252 /*
253  * fd.io coding-style-patch-verification: ON
254  *
255  * Local Variables:
256  * eval: (c-set-style "gnu")
257  * End:
258  */
lib.h
NAT port/address allocation lib.
nat_ip4_pool_addr_s
Definition: alloc.h:40
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
nat_ip4_addr_increment
static_always_inline void nat_ip4_addr_increment(ip4_address_t *addr)
Definition: alloc.c:24
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
nat_ip4_addr_port_s::port
u16 port
Definition: alloc.h:57
u16
unsigned short u16
Definition: types.h:57
nat_free_ip4_addr_and_port
int nat_free_ip4_addr_and_port(nat_ip4_pool_t *pool, u32 thread_index, u16 protocol, nat_ip4_addr_port_t *addr_port)
Definition: alloc.c:211
port
u16 port
Definition: lb_types.api:73
nat_ip4_pool_addr_s::addr
ip4_address_t addr
Definition: alloc.h:42
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
nat_alloc_ip4_addr_and_port
int nat_alloc_ip4_addr_and_port(nat_ip4_pool_t *pool, u32 fib_index, u32 thread_index, u32 nat_thread_index, u16 port_per_thread, u16 protocol, nat_ip4_addr_port_t *out)
Definition: alloc.c:195
nat_ip4_pool_s::alloc_addr_and_port_cb
nat_alloc_ip4_addr_and_port_cb_t * alloc_addr_and_port_cb
Definition: alloc.h:63
random_u32_max
static u32 random_u32_max(void)
Maximum value returned by random_u32()
Definition: random.h:80
random_u32
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
nat_ip4_pool_s
Definition: alloc.h:60
foreach_nat_protocol
@ foreach_nat_protocol
Definition: lib.h:66
nat_add_del_ip4_pool_addr
int nat_add_del_ip4_pool_addr(nat_ip4_pool_t *pool, ip4_address_t addr, u8 is_add)
Definition: alloc.c:32
count
u8 count
Definition: dhcp.api:208
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
nat_ip4_addr_port_s::addr
ip4_address_t addr
Definition: alloc.h:56
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
static_always_inline
#define static_always_inline
Definition: clib.h:112
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
nat_ip4_pool_s::add_del_pool_addr_cb
nat_add_del_ip4_pool_addr_cb_t * add_del_pool_addr_cb
Definition: alloc.h:62
ip4_address_t
Definition: ip4_packet.h:50
nat_ip4_addr_port_s
Definition: alloc.h:54
nat_random_port
static_always_inline u16 nat_random_port(u32 *random_seed, u16 min, u16 max)
Definition: alloc.c:105
alloc.h
NAT port/address allocation lib.
nat_ip4_pool_s::pool_addr
nat_ip4_pool_addr_t * pool_addr
Definition: alloc.h:64
u32
unsigned int u32
Definition: types.h:88
vlib_thread_main_t
Definition: threads.h:264
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
nat_alloc_ip4_addr_and_port_cb_default
int nat_alloc_ip4_addr_and_port_cb_default(nat_ip4_pool_t *pool, u32 fib_index, u32 thread_index, u32 nat_thread_index, u16 port_per_thread, u16 protocol, nat_ip4_addr_port_t *out)
Definition: alloc.c:112
nat_add_del_ip4_pool_addrs
int nat_add_del_ip4_pool_addrs(nat_ip4_pool_t *pool, ip4_address_t addr, u32 count, u8 is_add, void *opaque)
Definition: alloc.c:76
u8
unsigned char u8
Definition: types.h:56
a
a
Definition: bitmap.h:544
rv
int __clib_unused rv
Definition: application.c:491
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
vec_del1
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:896