FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
nat64_db.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 NAT64 DB
18  */
19 #include <nat/nat64_db.h>
20 #include <nat/nat_ipfix_logging.h>
21 #include <vnet/fib/fib_table.h>
22 
23 int
24 nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
25  u32 st_buckets, u32 st_memory_size,
26  nat64_db_free_addr_port_function_t free_addr_port_cb)
27 {
28  clib_bihash_init_24_8 (&db->bib.in2out, "bib-in2out", bib_buckets,
29  bib_memory_size);
30 
31  clib_bihash_init_24_8 (&db->bib.out2in, "bib-out2in", bib_buckets,
32  bib_memory_size);
33 
34  clib_bihash_init_48_8 (&db->st.in2out, "st-in2out", st_buckets,
35  st_memory_size);
36 
37  clib_bihash_init_48_8 (&db->st.out2in, "st-out2in", st_buckets,
38  st_memory_size);
39 
40  db->free_addr_port_cb = free_addr_port_cb;
41  db->bib.limit = 10 * bib_buckets;
42  db->bib.bib_entries_num = 0;
43  db->st.limit = 10 * st_buckets;
44  db->st.st_entries_num = 0;
45  db->addr_free = 0;
46 
47  return 0;
48 }
49 
50 nat64_db_bib_entry_t *
52  ip4_address_t * out_addr, u16 in_port,
53  u16 out_port, u32 fib_index, u8 proto,
54  u8 is_static)
55 {
56  nat64_db_bib_entry_t *bibe;
57  nat64_db_bib_entry_key_t bibe_key;
59  fib_table_t *fib;
60 
61  if (db->bib.bib_entries_num >= db->bib.limit)
62  {
63  db->free_addr_port_cb (db, out_addr, out_port, proto);
65  return 0;
66  }
67 
68  /* create pool entry */
69  switch (ip_proto_to_snat_proto (proto))
70  {
71 /* *INDENT-OFF* */
72 #define _(N, i, n, s) \
73  case SNAT_PROTOCOL_##N: \
74  pool_get (db->bib._##n##_bib, bibe); \
75  kv.value = bibe - db->bib._##n##_bib; \
76  break;
78 #undef _
79 /* *INDENT-ON* */
80  default:
81  pool_get (db->bib._unk_proto_bib, bibe);
82  kv.value = bibe - db->bib._unk_proto_bib;
83  break;
84  }
85 
86  db->bib.bib_entries_num++;
87 
88  memset (bibe, 0, sizeof (*bibe));
89  bibe->in_addr.as_u64[0] = in_addr->as_u64[0];
90  bibe->in_addr.as_u64[1] = in_addr->as_u64[1];
91  bibe->in_port = in_port;
92  bibe->out_addr.as_u32 = out_addr->as_u32;
93  bibe->out_port = out_port;
94  bibe->fib_index = fib_index;
95  bibe->proto = proto;
96  bibe->is_static = is_static;
97 
98  /* create hash lookup */
99  bibe_key.addr.as_u64[0] = bibe->in_addr.as_u64[0];
100  bibe_key.addr.as_u64[1] = bibe->in_addr.as_u64[1];
101  bibe_key.fib_index = bibe->fib_index;
102  bibe_key.port = bibe->in_port;
103  bibe_key.proto = bibe->proto;
104  bibe_key.rsvd = 0;
105  kv.key[0] = bibe_key.as_u64[0];
106  kv.key[1] = bibe_key.as_u64[1];
107  kv.key[2] = bibe_key.as_u64[2];
108  clib_bihash_add_del_24_8 (&db->bib.in2out, &kv, 1);
109 
110  memset (&bibe_key.addr, 0, sizeof (bibe_key.addr));
111  bibe_key.addr.ip4.as_u32 = bibe->out_addr.as_u32;
112  bibe_key.fib_index = 0;
113  bibe_key.port = bibe->out_port;
114  kv.key[0] = bibe_key.as_u64[0];
115  kv.key[1] = bibe_key.as_u64[1];
116  kv.key[2] = bibe_key.as_u64[2];
117  clib_bihash_add_del_24_8 (&db->bib.out2in, &kv, 1);
118 
119  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
120  nat_ipfix_logging_nat64_bib (in_addr, out_addr, proto, in_port, out_port,
121  fib->ft_table_id, 1);
122  return bibe;
123 }
124 
125 void
126 nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe)
127 {
128  nat64_db_bib_entry_key_t bibe_key;
130  nat64_db_bib_entry_t *bib;
131  u32 *ste_to_be_free = 0, *ste_index, bibe_index;
132  nat64_db_st_entry_t *st, *ste;
133  fib_table_t *fib;
134 
135  switch (ip_proto_to_snat_proto (bibe->proto))
136  {
137 /* *INDENT-OFF* */
138 #define _(N, i, n, s) \
139  case SNAT_PROTOCOL_##N: \
140  bib = db->bib._##n##_bib; \
141  st = db->st._##n##_st; \
142  break;
144 #undef _
145 /* *INDENT-ON* */
146  default:
147  bib = db->bib._unk_proto_bib;
148  st = db->st._unk_proto_st;
149  break;
150  }
151 
152  db->bib.bib_entries_num--;
153 
154  bibe_index = bibe - bib;
155 
156  /* delete ST entries for static BIB entry */
157  if (bibe->is_static)
158  {
159  pool_foreach (ste, st, (
160  {
161  if (ste->bibe_index == bibe_index)
162  vec_add1 (ste_to_be_free, ste - st);}
163  ));
164  vec_foreach (ste_index, ste_to_be_free)
165  nat64_db_st_entry_free (db, pool_elt_at_index (st, ste_index[0]));
166  vec_free (ste_to_be_free);
167  }
168 
169  /* delete hash lookup */
170  bibe_key.addr.as_u64[0] = bibe->in_addr.as_u64[0];
171  bibe_key.addr.as_u64[1] = bibe->in_addr.as_u64[1];
172  bibe_key.fib_index = bibe->fib_index;
173  bibe_key.port = bibe->in_port;
174  bibe_key.proto = bibe->proto;
175  bibe_key.rsvd = 0;
176  kv.key[0] = bibe_key.as_u64[0];
177  kv.key[1] = bibe_key.as_u64[1];
178  kv.key[2] = bibe_key.as_u64[2];
179  clib_bihash_add_del_24_8 (&db->bib.in2out, &kv, 0);
180 
181  memset (&bibe_key.addr, 0, sizeof (bibe_key.addr));
182  bibe_key.addr.ip4.as_u32 = bibe->out_addr.as_u32;
183  bibe_key.fib_index = 0;
184  bibe_key.port = bibe->out_port;
185  kv.key[0] = bibe_key.as_u64[0];
186  kv.key[1] = bibe_key.as_u64[1];
187  kv.key[2] = bibe_key.as_u64[2];
188  clib_bihash_add_del_24_8 (&db->bib.out2in, &kv, 0);
189 
190  if (!db->addr_free)
191  db->free_addr_port_cb (db, &bibe->out_addr, bibe->out_port, bibe->proto);
192 
193  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
194  nat_ipfix_logging_nat64_bib (&bibe->in_addr, &bibe->out_addr, bibe->proto,
195  bibe->in_port, bibe->out_port,
196  fib->ft_table_id, 0);
197 
198  /* delete from pool */
199  pool_put (bib, bibe);
200 
201 }
202 
203 nat64_db_bib_entry_t *
204 nat64_db_bib_entry_find (nat64_db_t * db, ip46_address_t * addr, u16 port,
205  u8 proto, u32 fib_index, u8 is_ip6)
206 {
207  nat64_db_bib_entry_t *bibe = 0;
208  nat64_db_bib_entry_key_t bibe_key;
209  clib_bihash_kv_24_8_t kv, value;
210  nat64_db_bib_entry_t *bib;
211 
212  switch (ip_proto_to_snat_proto (proto))
213  {
214 /* *INDENT-OFF* */
215 #define _(N, i, n, s) \
216  case SNAT_PROTOCOL_##N: \
217  bib = db->bib._##n##_bib; \
218  break;
220 #undef _
221 /* *INDENT-ON* */
222  default:
223  bib = db->bib._unk_proto_bib;
224  break;
225  }
226 
227  bibe_key.addr.as_u64[0] = addr->as_u64[0];
228  bibe_key.addr.as_u64[1] = addr->as_u64[1];
229  bibe_key.fib_index = fib_index;
230  bibe_key.port = port;
231  bibe_key.proto = proto;
232  bibe_key.rsvd = 0;
233 
234  kv.key[0] = bibe_key.as_u64[0];
235  kv.key[1] = bibe_key.as_u64[1];
236  kv.key[2] = bibe_key.as_u64[2];
237 
238  if (!clib_bihash_search_24_8
239  (is_ip6 ? &db->bib.in2out : &db->bib.out2in, &kv, &value))
240  bibe = pool_elt_at_index (bib, value.value);
241 
242  return bibe;
243 }
244 
245 void
247  nat64_db_bib_walk_fn_t fn, void *ctx)
248 {
249  nat64_db_bib_entry_t *bib, *bibe;
250 
251  if (proto == 255)
252  {
253  /* *INDENT-OFF* */
254  #define _(N, i, n, s) \
255  bib = db->bib._##n##_bib; \
256  pool_foreach (bibe, bib, ({ \
257  if (fn (bibe, ctx)) \
258  return; \
259  }));
261  #undef _
262  bib = db->bib._unk_proto_bib;
263  pool_foreach (bibe, bib, ({
264  if (fn (bibe, ctx))
265  return;
266  }));
267  /* *INDENT-ON* */
268  }
269  else
270  {
271  switch (ip_proto_to_snat_proto (proto))
272  {
273  /* *INDENT-OFF* */
274  #define _(N, i, n, s) \
275  case SNAT_PROTOCOL_##N: \
276  bib = db->bib._##n##_bib; \
277  break;
279  #undef _
280  /* *INDENT-ON* */
281  default:
282  bib = db->bib._unk_proto_bib;
283  break;
284  }
285 
286  /* *INDENT-OFF* */
287  pool_foreach (bibe, bib,
288  ({
289  if (fn (bibe, ctx))
290  return;
291  }));
292  /* *INDENT-ON* */
293  }
294 }
295 
296 nat64_db_bib_entry_t *
297 nat64_db_bib_entry_by_index (nat64_db_t * db, u8 proto, u32 bibe_index)
298 {
299  nat64_db_bib_entry_t *bib;
300 
301  switch (ip_proto_to_snat_proto (proto))
302  {
303 /* *INDENT-OFF* */
304 #define _(N, i, n, s) \
305  case SNAT_PROTOCOL_##N: \
306  bib = db->bib._##n##_bib; \
307  break;
309 #undef _
310 /* *INDENT-ON* */
311  default:
312  bib = db->bib._unk_proto_bib;
313  break;
314  }
315 
316  return pool_elt_at_index (bib, bibe_index);
317 }
318 
319 void
321  nat64_db_st_walk_fn_t fn, void *ctx)
322 {
323  nat64_db_st_entry_t *st, *ste;
324 
325  if (proto == 255)
326  {
327  /* *INDENT-OFF* */
328  #define _(N, i, n, s) \
329  st = db->st._##n##_st; \
330  pool_foreach (ste, st, ({ \
331  if (fn (ste, ctx)) \
332  return; \
333  }));
335  #undef _
336  st = db->st._unk_proto_st;
337  pool_foreach (ste, st, ({
338  if (fn (ste, ctx))
339  return;
340  }));
341  /* *INDENT-ON* */
342  }
343  else
344  {
345  switch (ip_proto_to_snat_proto (proto))
346  {
347  /* *INDENT-OFF* */
348  #define _(N, i, n, s) \
349  case SNAT_PROTOCOL_##N: \
350  st = db->st._##n##_st; \
351  break;
353  #undef _
354  /* *INDENT-ON* */
355  default:
356  st = db->st._unk_proto_st;
357  break;
358  }
359 
360  /* *INDENT-OFF* */
361  pool_foreach (ste, st,
362  ({
363  if (fn (ste, ctx))
364  return;
365  }));
366  /* *INDENT-ON* */
367  }
368 }
369 
370 nat64_db_st_entry_t *
371 nat64_db_st_entry_create (nat64_db_t * db, nat64_db_bib_entry_t * bibe,
372  ip6_address_t * in_r_addr,
373  ip4_address_t * out_r_addr, u16 r_port)
374 {
375  nat64_db_st_entry_t *ste;
376  nat64_db_bib_entry_t *bib;
377  nat64_db_st_entry_key_t ste_key;
379  fib_table_t *fib;
380 
381  if (db->st.st_entries_num >= db->st.limit)
382  {
384  return 0;
385  }
386 
387  /* create pool entry */
388  switch (ip_proto_to_snat_proto (bibe->proto))
389  {
390 /* *INDENT-OFF* */
391 #define _(N, i, n, s) \
392  case SNAT_PROTOCOL_##N: \
393  pool_get (db->st._##n##_st, ste); \
394  kv.value = ste - db->st._##n##_st; \
395  bib = db->bib._##n##_bib; \
396  break;
398 #undef _
399 /* *INDENT-ON* */
400  default:
401  pool_get (db->st._unk_proto_st, ste);
402  kv.value = ste - db->st._unk_proto_st;
403  bib = db->bib._unk_proto_bib;
404  break;
405  }
406 
407  db->st.st_entries_num++;
408 
409  memset (ste, 0, sizeof (*ste));
410  ste->in_r_addr.as_u64[0] = in_r_addr->as_u64[0];
411  ste->in_r_addr.as_u64[1] = in_r_addr->as_u64[1];
412  ste->out_r_addr.as_u32 = out_r_addr->as_u32;
413  ste->r_port = r_port;
414  ste->bibe_index = bibe - bib;
415  ste->proto = bibe->proto;
416 
417  /* increment session number for BIB entry */
418  bibe->ses_num++;
419 
420  /* create hash lookup */
421  memset (&ste_key, 0, sizeof (ste_key));
422  ste_key.l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
423  ste_key.l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
424  ste_key.r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
425  ste_key.r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
426  ste_key.fib_index = bibe->fib_index;
427  ste_key.l_port = bibe->in_port;
428  ste_key.r_port = ste->r_port;
429  ste_key.proto = ste->proto;
430  kv.key[0] = ste_key.as_u64[0];
431  kv.key[1] = ste_key.as_u64[1];
432  kv.key[2] = ste_key.as_u64[2];
433  kv.key[3] = ste_key.as_u64[3];
434  kv.key[4] = ste_key.as_u64[4];
435  kv.key[5] = ste_key.as_u64[5];
436  clib_bihash_add_del_48_8 (&db->st.in2out, &kv, 1);
437 
438  memset (&ste_key, 0, sizeof (ste_key));
439  ste_key.l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
440  ste_key.r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
441  ste_key.l_port = bibe->out_port;
442  ste_key.r_port = ste->r_port;
443  ste_key.proto = ste->proto;
444  kv.key[0] = ste_key.as_u64[0];
445  kv.key[1] = ste_key.as_u64[1];
446  kv.key[2] = ste_key.as_u64[2];
447  kv.key[3] = ste_key.as_u64[3];
448  kv.key[4] = ste_key.as_u64[4];
449  kv.key[5] = ste_key.as_u64[5];
450  clib_bihash_add_del_48_8 (&db->st.out2in, &kv, 1);
451 
452  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
453  nat_ipfix_logging_nat64_session (&bibe->in_addr, &bibe->out_addr,
454  bibe->proto, bibe->in_port, bibe->out_port,
455  &ste->in_r_addr, &ste->out_r_addr,
456  ste->r_port, ste->r_port, fib->ft_table_id,
457  1);
458 
459  return ste;
460 }
461 
462 void
463 nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste)
464 {
465  nat64_db_st_entry_t *st;
466  nat64_db_bib_entry_t *bib, *bibe;
467  nat64_db_st_entry_key_t ste_key;
469  fib_table_t *fib;
470 
471  switch (ip_proto_to_snat_proto (ste->proto))
472  {
473 /* *INDENT-OFF* */
474 #define _(N, i, n, s) \
475  case SNAT_PROTOCOL_##N: \
476  st = db->st._##n##_st; \
477  bib = db->bib._##n##_bib; \
478  break;
480 #undef _
481 /* *INDENT-ON* */
482  default:
483  st = db->st._unk_proto_st;
484  bib = db->bib._unk_proto_bib;
485  break;
486  }
487 
488  bibe = pool_elt_at_index (bib, ste->bibe_index);
489 
490  db->st.st_entries_num--;
491 
492  /* delete hash lookup */
493  memset (&ste_key, 0, sizeof (ste_key));
494  ste_key.l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
495  ste_key.l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
496  ste_key.r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
497  ste_key.r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
498  ste_key.fib_index = bibe->fib_index;
499  ste_key.l_port = bibe->in_port;
500  ste_key.r_port = ste->r_port;
501  ste_key.proto = ste->proto;
502  kv.key[0] = ste_key.as_u64[0];
503  kv.key[1] = ste_key.as_u64[1];
504  kv.key[2] = ste_key.as_u64[2];
505  kv.key[3] = ste_key.as_u64[3];
506  kv.key[4] = ste_key.as_u64[4];
507  kv.key[5] = ste_key.as_u64[5];
508  clib_bihash_add_del_48_8 (&db->st.in2out, &kv, 0);
509 
510  memset (&ste_key, 0, sizeof (ste_key));
511  ste_key.l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
512  ste_key.r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
513  ste_key.l_port = bibe->out_port;
514  ste_key.r_port = ste->r_port;
515  ste_key.proto = ste->proto;
516  kv.key[0] = ste_key.as_u64[0];
517  kv.key[1] = ste_key.as_u64[1];
518  kv.key[2] = ste_key.as_u64[2];
519  kv.key[3] = ste_key.as_u64[3];
520  kv.key[4] = ste_key.as_u64[4];
521  kv.key[5] = ste_key.as_u64[5];
522  clib_bihash_add_del_48_8 (&db->st.out2in, &kv, 0);
523 
524  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
525  nat_ipfix_logging_nat64_session (&bibe->in_addr, &bibe->out_addr,
526  bibe->proto, bibe->in_port, bibe->out_port,
527  &ste->in_r_addr, &ste->out_r_addr,
528  ste->r_port, ste->r_port, fib->ft_table_id,
529  0);
530 
531  /* delete from pool */
532  pool_put (st, ste);
533 
534  /* decrement session number for BIB entry */
535  bibe->ses_num--;
536 
537  /* delete BIB entry if last session and dynamic */
538  if (!bibe->is_static && !bibe->ses_num)
539  nat64_db_bib_entry_free (db, bibe);
540 }
541 
542 nat64_db_st_entry_t *
543 nat64_db_st_entry_find (nat64_db_t * db, ip46_address_t * l_addr,
544  ip46_address_t * r_addr, u16 l_port, u16 r_port,
545  u8 proto, u32 fib_index, u8 is_ip6)
546 {
547  nat64_db_st_entry_t *ste = 0;
548  nat64_db_st_entry_t *st;
549  nat64_db_st_entry_key_t ste_key;
550  clib_bihash_kv_48_8_t kv, value;
551 
552  switch (ip_proto_to_snat_proto (proto))
553  {
554 /* *INDENT-OFF* */
555 #define _(N, i, n, s) \
556  case SNAT_PROTOCOL_##N: \
557  st = db->st._##n##_st; \
558  break;
560 #undef _
561 /* *INDENT-ON* */
562  default:
563  st = db->st._unk_proto_st;
564  break;
565  }
566 
567  memset (&ste_key, 0, sizeof (ste_key));
568  ste_key.l_addr.as_u64[0] = l_addr->as_u64[0];
569  ste_key.l_addr.as_u64[1] = l_addr->as_u64[1];
570  ste_key.r_addr.as_u64[0] = r_addr->as_u64[0];
571  ste_key.r_addr.as_u64[1] = r_addr->as_u64[1];
572  ste_key.fib_index = fib_index;
573  ste_key.l_port = l_port;
574  ste_key.r_port = r_port;
575  ste_key.proto = proto;
576  kv.key[0] = ste_key.as_u64[0];
577  kv.key[1] = ste_key.as_u64[1];
578  kv.key[2] = ste_key.as_u64[2];
579  kv.key[3] = ste_key.as_u64[3];
580  kv.key[4] = ste_key.as_u64[4];
581  kv.key[5] = ste_key.as_u64[5];
582 
583  if (!clib_bihash_search_48_8
584  (is_ip6 ? &db->st.in2out : &db->st.out2in, &kv, &value))
585  ste = pool_elt_at_index (st, value.value);
586 
587  return ste;
588 }
589 
590 u32
591 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste)
592 {
593  nat64_db_st_entry_t *st;
594 
595  switch (ip_proto_to_snat_proto (ste->proto))
596  {
597 /* *INDENT-OFF* */
598 #define _(N, i, n, s) \
599  case SNAT_PROTOCOL_##N: \
600  st = db->st._##n##_st; \
601  break;
603 #undef _
604 /* *INDENT-ON* */
605  default:
606  st = db->st._unk_proto_st;
607  return (u32) ~ 0;
608  }
609 
610  return ste - st;
611 }
612 
613 nat64_db_st_entry_t *
614 nat64_db_st_entry_by_index (nat64_db_t * db, u8 proto, u32 ste_index)
615 {
616  nat64_db_st_entry_t *st;
617 
618  switch (ip_proto_to_snat_proto (proto))
619  {
620 /* *INDENT-OFF* */
621 #define _(N, i, n, s) \
622  case SNAT_PROTOCOL_##N: \
623  st = db->st._##n##_st; \
624  break;
626 #undef _
627 /* *INDENT-ON* */
628  default:
629  st = db->st._unk_proto_st;
630  break;
631  }
632 
633  return pool_elt_at_index (st, ste_index);
634 }
635 
636 void
638 {
639  u32 *ste_to_be_free = 0, *ste_index;
640  nat64_db_st_entry_t *st, *ste;
641 
642 /* *INDENT-OFF* */
643 #define _(N, i, n, s) \
644  st = db->st._##n##_st; \
645  pool_foreach (ste, st, ({\
646  if (i == SNAT_PROTOCOL_TCP && !ste->tcp_state) \
647  continue; \
648  if (ste->expire < now) \
649  vec_add1 (ste_to_be_free, ste - st); \
650  })); \
651  vec_foreach (ste_index, ste_to_be_free) \
652  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \
653  vec_free (ste_to_be_free); \
654  ste_to_be_free = 0;
656 #undef _
657  st = db->st._unk_proto_st;
658  pool_foreach (ste, st, ({
659  if (ste->expire < now)
660  vec_add1 (ste_to_be_free, ste - st);
661  }));
662  vec_foreach (ste_index, ste_to_be_free)
663  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0]));
664  vec_free (ste_to_be_free);
665 /* *INDENT-ON* */
666 }
667 
668 void
670 {
671  u32 *ste_to_be_free = 0, *ste_index;
672  nat64_db_st_entry_t *st, *ste;
673  nat64_db_bib_entry_t *bibe;
674 
675  db->addr_free = 1;
676 /* *INDENT-OFF* */
677 #define _(N, i, n, s) \
678  st = db->st._##n##_st; \
679  pool_foreach (ste, st, ({ \
680  bibe = pool_elt_at_index (db->bib._##n##_bib, ste->bibe_index); \
681  if (bibe->out_addr.as_u32 == out_addr->as_u32) \
682  vec_add1 (ste_to_be_free, ste - st); \
683  })); \
684  vec_foreach (ste_index, ste_to_be_free) \
685  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \
686  vec_free (ste_to_be_free); \
687  ste_to_be_free = 0;
689 #undef _
690  st = db->st._unk_proto_st;
691  pool_foreach (ste, st, ({
692  bibe = pool_elt_at_index (db->bib._unk_proto_bib, ste->bibe_index);
693  if (bibe->out_addr.as_u32 == out_addr->as_u32)
694  vec_add1 (ste_to_be_free, ste - st);
695  }));
696  vec_foreach (ste_index, ste_to_be_free)
697  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0]));
698  vec_free (ste_to_be_free);
699  db->addr_free = 0;
700 /* *INDENT-ON* */
701 }
702 
703 /*
704  * fd.io coding-style-patch-verification: ON
705  *
706  * Local Variables:
707  * eval: (c-set-style "gnu")
708  * End:
709  */
void nat_ipfix_logging_max_sessions(u32 limit)
Generate maximum session entries exceeded event.
Definition: nat64_db.h:76
int nat64_db_init(nat64_db_t *db, u32 bib_buckets, u32 bib_memory_size, u32 st_buckets, u32 st_memory_size, nat64_db_free_addr_port_function_t free_addr_port_cb)
Initialize NAT64 DB.
Definition: nat64_db.c:24
nat64_db_free_addr_port_function_t free_addr_port_cb
Definition: nat64_db.h:140
u16 l_port
Definition: nat64_db.h:85
u64 as_u64[2]
Definition: ip6_packet.h:51
nat64_db_st_entry_t * nat64_db_st_entry_create(nat64_db_t *db, nat64_db_bib_entry_t *bibe, ip6_address_t *in_r_addr, ip4_address_t *out_r_addr, u16 r_port)
Create new NAT64 session table entry.
Definition: nat64_db.c:371
nat64_db_bib_entry_t * nat64_db_bib_entry_create(nat64_db_t *db, ip6_address_t *in_addr, ip4_address_t *out_addr, u16 in_port, u16 out_port, u32 fib_index, u8 proto, u8 is_static)
Create new NAT64 BIB entry.
Definition: nat64_db.c:51
Definition: nat64_db.h:27
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:520
nat64_db_bib_entry_t * nat64_db_bib_entry_find(nat64_db_t *db, ip46_address_t *addr, u16 port, u8 proto, u32 fib_index, u8 is_ip6)
Find NAT64 BIB entry.
Definition: nat64_db.c:204
u32 fib_index
Definition: nat64_db.h:84
nat64_db_st_entry_t * nat64_db_st_entry_by_index(nat64_db_t *db, u8 proto, u32 ste_index)
Get ST entry by index and protocol.
Definition: nat64_db.c:614
nat64_db_bib_t bib
Definition: nat64_db.h:138
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
int(* nat64_db_bib_walk_fn_t)(nat64_db_bib_entry_t *bibe, void *ctx)
Call back function when walking NAT64 BIB, non-zero return value stop walk.
Definition: nat64_db.h:193
u32 st_entries_num
Definition: nat64_db.h:123
ip46_address_t l_addr
Definition: nat64_db.h:82
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
void nat_ipfix_logging_nat64_bib(ip6_address_t *src_ip, ip4_address_t *nat_src_ip, u8 proto, u16 src_port, u16 nat_src_port, u32 vrf_id, u8 is_create)
Generate NAT64 BIB create and delete events.
u16 r_port
Definition: nat64_db.h:86
clib_bihash_48_8_t in2out
Definition: nat64_db.h:119
u16 port
Definition: nat64_db.h:35
u64 as_u64[6]
Definition: nat64_db.h:90
clib_bihash_24_8_t in2out
Definition: nat64_db.h:69
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:637
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:126
nat64_db_st_t st
Definition: nat64_db.h:139
u8 addr_free
Definition: nat64_db.h:141
void nat_ipfix_logging_max_bibs(u32 limit)
Generate maximum BIB entries exceeded event.
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:89
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
ip46_address_t addr
Definition: nat64_db.h:33
void(* nat64_db_free_addr_port_function_t)(struct nat64_db_s *db, ip4_address_t *addr, u16 port, u8 proto)
Call back function to free NAT64 pool address and port when BIB entry is deleted. ...
Definition: nat64_db.h:132
u8 proto
Definition: nat64_db.h:87
nat64_db_st_entry_t * nat64_db_st_entry_find(nat64_db_t *db, ip46_address_t *l_addr, ip46_address_t *r_addr, u16 l_port, u16 r_port, u8 proto, u32 fib_index, u8 is_ip6)
Find NAT64 session table entry.
Definition: nat64_db.c:543
unsigned int u32
Definition: types.h:88
void nat64_db_st_walk(nat64_db_t *db, u8 proto, nat64_db_st_walk_fn_t fn, void *ctx)
Walk NAT64 session table.
Definition: nat64_db.c:320
long ctx[MAX_CONNS]
Definition: main.c:126
u32 nat64_db_st_entry_get_index(nat64_db_t *db, nat64_db_st_entry_t *ste)
Definition: nat64_db.c:591
static u32 ip_proto_to_snat_proto(u8 ip_proto)
Definition: nat.h:486
u32 fib_index
Definition: nat64_db.h:34
nat64_db_bib_entry_t * nat64_db_bib_entry_by_index(nat64_db_t *db, u8 proto, u32 bibe_index)
Get BIB entry by index and protocol.
Definition: nat64_db.c:297
u32 bib_entries_num
Definition: nat64_db.h:73
void nat64_db_free_out_addr(nat64_db_t *db, ip4_address_t *out_addr)
Free sessions using specific outside address.
Definition: nat64_db.c:669
u8 proto
Definition: nat64_db.h:36
unsigned short u16
Definition: types.h:57
clib_bihash_24_8_t out2in
Definition: nat64_db.h:70
unsigned char u8
Definition: types.h:56
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:27
void nat64_db_st_entry_free(nat64_db_t *db, nat64_db_st_entry_t *ste)
Free NAT64 session table entry.
Definition: nat64_db.c:463
int(* nat64_db_st_walk_fn_t)(nat64_db_st_entry_t *ste, void *ctx)
Call back function when walking NAT64 session table, non-zero return value stop walk.
Definition: nat64_db.h:290
NAT64 DB.
#define vec_foreach(var, vec)
Vector iterator.
vhost_vring_addr_t addr
Definition: vhost-user.h:83
u8 rsvd
Definition: nat64_db.h:37
void nat64_db_bib_walk(nat64_db_t *db, u8 proto, nat64_db_bib_walk_fn_t fn, void *ctx)
Walk NAT64 BIB.
Definition: nat64_db.c:246
u64 as_u64[3]
Definition: nat64_db.h:39
ip46_address_t r_addr
Definition: nat64_db.h:83
A protocol Independent FIB table.
Definition: fib_table.h:69
clib_bihash_48_8_t out2in
Definition: nat64_db.h:120
void nat_ipfix_logging_nat64_session(ip6_address_t *src_ip, ip4_address_t *nat_src_ip, u8 proto, u16 src_port, u16 nat_src_port, ip6_address_t *dst_ip, ip4_address_t *nat_dst_ip, u16 dst_port, u16 nat_dst_port, u32 vrf_id, u8 is_create)
Generate NAT64 session create and delete events.