24 u32 bib_buckets = 1024;
25 u32 bib_memory_size = 128 << 20;
26 u32 st_buckets = 2048;
27 u32 st_memory_size = 256 << 20;
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,
44 nat64_db_bib_entry_t *
47 u16 out_port,
u32 fib_index,
u8 proto,
50 nat64_db_bib_entry_t *bibe;
58 #define _(N, i, n, s) \ 59 case SNAT_PROTOCOL_##N: \ 60 pool_get (db->bib._##n##_bib, bibe); \ 61 kv.value = bibe - db->bib._##n##_bib; \ 68 kv.
value = bibe - db->
bib._unk_proto_bib;
71 memset (bibe, 0,
sizeof (*bibe));
72 bibe->in_addr.as_u64[0] = in_addr->
as_u64[0];
73 bibe->in_addr.as_u64[1] = in_addr->
as_u64[1];
74 bibe->in_port = in_port;
75 bibe->out_addr.as_u32 = out_addr->
as_u32;
76 bibe->out_port = out_port;
77 bibe->fib_index = fib_index;
79 bibe->is_static = is_static;
82 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
83 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
85 bibe_key.
port = bibe->in_port;
86 bibe_key.
proto = bibe->proto;
91 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 1);
93 memset (&bibe_key.
addr, 0, sizeof (bibe_key.
addr));
94 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
96 bibe_key.
port = bibe->out_port;
100 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 1);
110 nat64_db_bib_entry_t *bib;
111 u32 *ste_to_be_free = 0, *ste_index, bibe_index;
112 nat64_db_st_entry_t *st, *ste;
117 #define _(N, i, n, s) \ 118 case SNAT_PROTOCOL_##N: \ 119 bib = db->bib._##n##_bib; \ 120 st = db->st._##n##_st; \ 126 bib = db->
bib._unk_proto_bib;
127 st = db->
st._unk_proto_st;
131 bibe_index = bibe - bib;
138 if (ste->bibe_index == bibe_index)
139 vec_add1 (ste_to_be_free, ste - st);}
147 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
148 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
150 bibe_key.
port = bibe->in_port;
151 bibe_key.
proto = bibe->proto;
156 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 0);
158 memset (&bibe_key.
addr, 0, sizeof (bibe_key.
addr));
159 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
161 bibe_key.
port = bibe->out_port;
165 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 0);
172 nat64_db_bib_entry_t *
174 u8 proto,
u32 fib_index,
u8 is_ip6)
176 nat64_db_bib_entry_t *bibe = 0;
179 nat64_db_bib_entry_t *bib;
184 #define _(N, i, n, s) \ 185 case SNAT_PROTOCOL_##N: \ 186 bib = db->bib._##n##_bib; \ 192 bib = db->
bib._unk_proto_bib;
196 bibe_key.
addr.as_u64[0] = addr->as_u64[0];
197 bibe_key.
addr.as_u64[1] = addr->as_u64[1];
199 bibe_key.
port = port;
200 bibe_key.
proto = proto;
207 if (!clib_bihash_search_24_8
218 nat64_db_bib_entry_t *bib, *bibe;
223 #define _(N, i, n, s) \ 224 bib = db->bib._##n##_bib; \ 225 pool_foreach (bibe, bib, ({ \ 226 if (fn (bibe, ctx)) \ 231 bib = db->
bib._unk_proto_bib;
243 #define _(N, i, n, s) \ 244 case SNAT_PROTOCOL_##N: \ 245 bib = db->bib._##n##_bib; \ 251 bib = db->
bib._unk_proto_bib;
265 nat64_db_bib_entry_t *
268 nat64_db_bib_entry_t *bib;
273 #define _(N, i, n, s) \ 274 case SNAT_PROTOCOL_##N: \ 275 bib = db->bib._##n##_bib; \ 281 bib = db->
bib._unk_proto_bib;
292 nat64_db_st_entry_t *st, *ste;
297 #define _(N, i, n, s) \ 298 st = db->st._##n##_st; \ 299 pool_foreach (ste, st, ({ \ 305 st = db->
st._unk_proto_st;
317 #define _(N, i, n, s) \ 318 case SNAT_PROTOCOL_##N: \ 319 st = db->st._##n##_st; \ 325 st = db->
st._unk_proto_st;
339 nat64_db_st_entry_t *
344 nat64_db_st_entry_t *ste;
345 nat64_db_bib_entry_t *bib;
353 #define _(N, i, n, s) \ 354 case SNAT_PROTOCOL_##N: \ 355 pool_get (db->st._##n##_st, ste); \ 356 kv.value = ste - db->st._##n##_st; \ 357 bib = db->bib._##n##_bib; \ 364 kv.
value = ste - db->
st._unk_proto_st;
365 bib = db->
bib._unk_proto_bib;
368 memset (ste, 0,
sizeof (*ste));
369 ste->in_r_addr.as_u64[0] = in_r_addr->
as_u64[0];
370 ste->in_r_addr.as_u64[1] = in_r_addr->
as_u64[1];
371 ste->out_r_addr.as_u32 = out_r_addr->
as_u32;
372 ste->r_port = r_port;
373 ste->bibe_index = bibe - bib;
374 ste->proto = bibe->proto;
380 memset (&ste_key, 0,
sizeof (ste_key));
381 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
382 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
383 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
384 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
386 ste_key.
l_port = bibe->in_port;
387 ste_key.
r_port = ste->r_port;
388 ste_key.
proto = ste->proto;
395 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 1);
397 memset (&ste_key, 0,
sizeof (ste_key));
398 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
399 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
400 ste_key.
l_port = bibe->out_port;
401 ste_key.
r_port = ste->r_port;
402 ste_key.
proto = ste->proto;
409 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 1);
417 nat64_db_st_entry_t *st;
418 nat64_db_bib_entry_t *bib, *bibe;
425 #define _(N, i, n, s) \ 426 case SNAT_PROTOCOL_##N: \ 427 st = db->st._##n##_st; \ 428 bib = db->bib._##n##_bib; \ 434 st = db->
st._unk_proto_st;
435 bib = db->
bib._unk_proto_bib;
442 memset (&ste_key, 0,
sizeof (ste_key));
443 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
444 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
445 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
446 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
448 ste_key.
l_port = bibe->in_port;
449 ste_key.
r_port = ste->r_port;
450 ste_key.
proto = ste->proto;
457 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 0);
459 memset (&ste_key, 0,
sizeof (ste_key));
460 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
461 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
462 ste_key.
l_port = bibe->out_port;
463 ste_key.
r_port = ste->r_port;
464 ste_key.
proto = ste->proto;
471 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 0);
480 if (!bibe->is_static && !bibe->ses_num)
484 nat64_db_st_entry_t *
486 ip46_address_t * r_addr,
u16 l_port,
u16 r_port,
487 u8 proto,
u32 fib_index,
u8 is_ip6)
489 nat64_db_st_entry_t *ste = 0;
490 nat64_db_st_entry_t *st;
497 #define _(N, i, n, s) \ 498 case SNAT_PROTOCOL_##N: \ 499 st = db->st._##n##_st; \ 505 st = db->
st._unk_proto_st;
509 memset (&ste_key, 0,
sizeof (ste_key));
510 ste_key.
l_addr.as_u64[0] = l_addr->as_u64[0];
511 ste_key.
l_addr.as_u64[1] = l_addr->as_u64[1];
512 ste_key.
r_addr.as_u64[0] = r_addr->as_u64[0];
513 ste_key.
r_addr.as_u64[1] = r_addr->as_u64[1];
517 ste_key.
proto = proto;
525 if (!clib_bihash_search_48_8
535 u32 *ste_to_be_free = 0, *ste_index;
536 nat64_db_st_entry_t *st, *ste;
539 #define _(N, i, n, s) \ 540 st = db->st._##n##_st; \ 541 pool_foreach (ste, st, ({\ 542 if (i == SNAT_PROTOCOL_TCP && !ste->tcp_state) \ 544 if (ste->expire < now) \ 545 vec_add1 (ste_to_be_free, ste - st); \ 547 vec_foreach (ste_index, ste_to_be_free) \ 548 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 549 vec_free (ste_to_be_free); \ 553 st = db->
st._unk_proto_st;
555 if (ste->expire < now)
556 vec_add1 (ste_to_be_free, ste - st);
567 u32 *ste_to_be_free = 0, *ste_index;
568 nat64_db_st_entry_t *st, *ste;
569 nat64_db_bib_entry_t *bibe;
572 #define _(N, i, n, s) \ 573 st = db->st._##n##_st; \ 574 pool_foreach (ste, st, ({ \ 575 bibe = pool_elt_at_index (db->bib._##n##_bib, ste->bibe_index); \ 576 if (bibe->out_addr.as_u32 == out_addr->as_u32) \ 577 vec_add1 (ste_to_be_free, ste - st); \ 579 vec_foreach (ste_index, ste_to_be_free) \ 580 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 581 vec_free (ste_to_be_free); \ 585 st = db->
st._unk_proto_st;
588 if (bibe->out_addr.as_u32 == out_addr->
as_u32)
589 vec_add1 (ste_to_be_free, ste - st);
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.
#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.
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
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.
#define vec_free(V)
Free vector's memory (no header).
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.
static u32 ip_proto_to_snat_proto(u8 ip_proto)
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
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.
int nat64_db_init(nat64_db_t *db)
Initialize NAT64 DB.
void nat64_db_bib_walk(nat64_db_t *db, u8 proto, nat64_db_bib_walk_fn_t fn, void *ctx)
Walk NAT64 BIB.
clib_bihash_48_8_t out2in