26 u32 st_buckets,
u32 st_memory_size,
29 clib_bihash_init_24_8 (&db->
bib.
in2out,
"bib-in2out", bib_buckets,
32 clib_bihash_init_24_8 (&db->
bib.
out2in,
"bib-out2in", bib_buckets,
35 clib_bihash_init_48_8 (&db->
st.
in2out,
"st-in2out", st_buckets,
38 clib_bihash_init_48_8 (&db->
st.
out2in,
"st-out2in", st_buckets,
51 nat64_db_bib_entry_t *
54 u16 out_port,
u32 fib_index,
u8 proto,
57 nat64_db_bib_entry_t *bibe;
73 #define _(N, i, n, s) \ 74 case SNAT_PROTOCOL_##N: \ 75 pool_get (db->bib._##n##_bib, bibe); \ 76 kv.value = bibe - db->bib._##n##_bib; \ 83 kv.
value = bibe - db->
bib._unk_proto_bib;
89 memset (bibe, 0,
sizeof (*bibe));
90 bibe->in_addr.as_u64[0] = in_addr->
as_u64[0];
91 bibe->in_addr.as_u64[1] = in_addr->
as_u64[1];
92 bibe->in_port = in_port;
93 bibe->out_addr.as_u32 = out_addr->
as_u32;
94 bibe->out_port = out_port;
95 bibe->fib_index = fib_index;
97 bibe->is_static = is_static;
100 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
101 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
103 bibe_key.
port = bibe->in_port;
104 bibe_key.
proto = bibe->proto;
109 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 1);
112 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
114 bibe_key.
port = bibe->out_port;
118 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 1);
131 nat64_db_bib_entry_t *bib;
132 u32 *ste_to_be_free = 0, *ste_index, bibe_index;
133 nat64_db_st_entry_t *st, *ste;
139 #define _(N, i, n, s) \ 140 case SNAT_PROTOCOL_##N: \ 141 bib = db->bib._##n##_bib; \ 142 st = db->st._##n##_st; \ 148 bib = db->
bib._unk_proto_bib;
149 st = db->
st._unk_proto_st;
155 bibe_index = bibe - bib;
162 if (ste->bibe_index == bibe_index)
163 vec_add1 (ste_to_be_free, ste - st);}
171 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
172 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
174 bibe_key.
port = bibe->in_port;
175 bibe_key.
proto = bibe->proto;
180 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 0);
183 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
185 bibe_key.
port = bibe->out_port;
189 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 0);
196 bibe->in_port, bibe->out_port,
204 nat64_db_bib_entry_t *
206 u8 proto,
u32 fib_index,
u8 is_ip6)
208 nat64_db_bib_entry_t *bibe = 0;
211 nat64_db_bib_entry_t *bib;
216 #define _(N, i, n, s) \ 217 case SNAT_PROTOCOL_##N: \ 218 bib = db->bib._##n##_bib; \ 224 bib = db->
bib._unk_proto_bib;
228 bibe_key.
addr.as_u64[0] = addr->as_u64[0];
229 bibe_key.
addr.as_u64[1] = addr->as_u64[1];
231 bibe_key.
port = port;
232 bibe_key.
proto = proto;
239 if (!clib_bihash_search_24_8
250 nat64_db_bib_entry_t *bib, *bibe;
255 #define _(N, i, n, s) \ 256 bib = db->bib._##n##_bib; \ 257 pool_foreach (bibe, bib, ({ \ 258 if (fn (bibe, ctx)) \ 263 bib = db->
bib._unk_proto_bib;
275 #define _(N, i, n, s) \ 276 case SNAT_PROTOCOL_##N: \ 277 bib = db->bib._##n##_bib; \ 283 bib = db->
bib._unk_proto_bib;
297 nat64_db_bib_entry_t *
300 nat64_db_bib_entry_t *bib;
305 #define _(N, i, n, s) \ 306 case SNAT_PROTOCOL_##N: \ 307 bib = db->bib._##n##_bib; \ 313 bib = db->
bib._unk_proto_bib;
324 nat64_db_st_entry_t *st, *ste;
329 #define _(N, i, n, s) \ 330 st = db->st._##n##_st; \ 331 pool_foreach (ste, st, ({ \ 337 st = db->
st._unk_proto_st;
349 #define _(N, i, n, s) \ 350 case SNAT_PROTOCOL_##N: \ 351 st = db->st._##n##_st; \ 357 st = db->
st._unk_proto_st;
371 nat64_db_st_entry_t *
376 nat64_db_st_entry_t *ste;
377 nat64_db_bib_entry_t *bib;
392 #define _(N, i, n, s) \ 393 case SNAT_PROTOCOL_##N: \ 394 pool_get (db->st._##n##_st, ste); \ 395 kv.value = ste - db->st._##n##_st; \ 396 bib = db->bib._##n##_bib; \ 403 kv.
value = ste - db->
st._unk_proto_st;
404 bib = db->
bib._unk_proto_bib;
410 memset (ste, 0,
sizeof (*ste));
411 ste->in_r_addr.as_u64[0] = in_r_addr->
as_u64[0];
412 ste->in_r_addr.as_u64[1] = in_r_addr->
as_u64[1];
413 ste->out_r_addr.as_u32 = out_r_addr->
as_u32;
414 ste->r_port = r_port;
415 ste->bibe_index = bibe - bib;
416 ste->proto = bibe->proto;
422 memset (&ste_key, 0,
sizeof (ste_key));
423 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
424 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
425 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
426 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
428 ste_key.
l_port = bibe->in_port;
429 ste_key.
r_port = ste->r_port;
430 ste_key.
proto = ste->proto;
437 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 1);
439 memset (&ste_key, 0,
sizeof (ste_key));
440 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
441 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
442 ste_key.
l_port = bibe->out_port;
443 ste_key.
r_port = ste->r_port;
444 ste_key.
proto = ste->proto;
451 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 1);
455 bibe->proto, bibe->in_port, bibe->out_port,
456 &ste->in_r_addr, &ste->out_r_addr,
466 nat64_db_st_entry_t *st;
467 nat64_db_bib_entry_t *bib, *bibe;
475 #define _(N, i, n, s) \ 476 case SNAT_PROTOCOL_##N: \ 477 st = db->st._##n##_st; \ 478 bib = db->bib._##n##_bib; \ 484 st = db->
st._unk_proto_st;
485 bib = db->
bib._unk_proto_bib;
494 memset (&ste_key, 0,
sizeof (ste_key));
495 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
496 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
497 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
498 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
500 ste_key.
l_port = bibe->in_port;
501 ste_key.
r_port = ste->r_port;
502 ste_key.
proto = ste->proto;
509 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 0);
511 memset (&ste_key, 0,
sizeof (ste_key));
512 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
513 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
514 ste_key.
l_port = bibe->out_port;
515 ste_key.
r_port = ste->r_port;
516 ste_key.
proto = ste->proto;
523 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 0);
527 bibe->proto, bibe->in_port, bibe->out_port,
528 &ste->in_r_addr, &ste->out_r_addr,
539 if (!bibe->is_static && !bibe->ses_num)
543 nat64_db_st_entry_t *
545 ip46_address_t * r_addr,
u16 l_port,
u16 r_port,
546 u8 proto,
u32 fib_index,
u8 is_ip6)
548 nat64_db_st_entry_t *ste = 0;
549 nat64_db_st_entry_t *st;
556 #define _(N, i, n, s) \ 557 case SNAT_PROTOCOL_##N: \ 558 st = db->st._##n##_st; \ 564 st = db->
st._unk_proto_st;
568 memset (&ste_key, 0,
sizeof (ste_key));
569 ste_key.
l_addr.as_u64[0] = l_addr->as_u64[0];
570 ste_key.
l_addr.as_u64[1] = l_addr->as_u64[1];
571 ste_key.
r_addr.as_u64[0] = r_addr->as_u64[0];
572 ste_key.
r_addr.as_u64[1] = r_addr->as_u64[1];
576 ste_key.
proto = proto;
584 if (!clib_bihash_search_48_8
594 nat64_db_st_entry_t *st;
599 #define _(N, i, n, s) \ 600 case SNAT_PROTOCOL_##N: \ 601 st = db->st._##n##_st; \ 607 st = db->
st._unk_proto_st;
614 nat64_db_st_entry_t *
617 nat64_db_st_entry_t *st;
622 #define _(N, i, n, s) \ 623 case SNAT_PROTOCOL_##N: \ 624 st = db->st._##n##_st; \ 630 st = db->
st._unk_proto_st;
640 u32 *ste_to_be_free = 0, *ste_index;
641 nat64_db_st_entry_t *st, *ste;
644 #define _(N, i, n, s) \ 645 st = db->st._##n##_st; \ 646 pool_foreach (ste, st, ({\ 647 if (i == SNAT_PROTOCOL_TCP && !ste->tcp_state) \ 649 if (ste->expire < now) \ 650 vec_add1 (ste_to_be_free, ste - st); \ 652 vec_foreach (ste_index, ste_to_be_free) \ 653 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 654 vec_free (ste_to_be_free); \ 658 st = db->
st._unk_proto_st;
660 if (ste->expire < now)
661 vec_add1 (ste_to_be_free, ste - st);
672 u32 *ste_to_be_free = 0, *ste_index;
673 nat64_db_st_entry_t *st, *ste;
674 nat64_db_bib_entry_t *bibe;
678 #define _(N, i, n, s) \ 679 st = db->st._##n##_st; \ 680 pool_foreach (ste, st, ({ \ 681 bibe = pool_elt_at_index (db->bib._##n##_bib, ste->bibe_index); \ 682 if (bibe->out_addr.as_u32 == out_addr->as_u32) \ 683 vec_add1 (ste_to_be_free, ste - st); \ 685 vec_foreach (ste_index, ste_to_be_free) \ 686 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 687 vec_free (ste_to_be_free); \ 691 st = db->
st._unk_proto_st;
694 if (bibe->out_addr.as_u32 == out_addr->
as_u32)
695 vec_add1 (ste_to_be_free, ste - st);
void nat_ipfix_logging_max_sessions(u32 limit)
Generate maximum session entries exceeded event.
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.
nat64_db_free_addr_port_function_t free_addr_port_cb
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.
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.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
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.
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.
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
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.
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
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.
clib_bihash_48_8_t in2out
clib_bihash_24_8_t in2out
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
#define pool_put(P, E)
Free an object E in pool P.
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
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.
#define vec_free(V)
Free vector's memory (no header).
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. ...
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.
void nat64_db_st_walk(nat64_db_t *db, u8 proto, nat64_db_st_walk_fn_t fn, void *ctx)
Walk NAT64 session table.
u32 nat64_db_st_entry_get_index(nat64_db_t *db, nat64_db_st_entry_t *ste)
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.
void nat64_db_free_out_addr(nat64_db_t *db, ip4_address_t *out_addr)
Free sessions using specific outside address.
clib_bihash_24_8_t out2in
static u32 ip_proto_to_snat_proto(u8 ip_proto)
The NAT inline functions.
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
void nat64_db_st_entry_free(nat64_db_t *db, nat64_db_st_entry_t *ste)
Free NAT64 session table entry.
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.
#define vec_foreach(var, vec)
Vector iterator.
void nat64_db_bib_walk(nat64_db_t *db, u8 proto, nat64_db_bib_walk_fn_t fn, void *ctx)
Walk NAT64 BIB.
A protocol Independent FIB table.
clib_bihash_48_8_t out2in
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.