23 u32 st_buckets,
u32 st_memory_size,
26 clib_bihash_init_24_8 (&db->
bib.
in2out,
"bib-in2out", bib_buckets,
29 clib_bihash_init_24_8 (&db->
bib.
out2in,
"bib-out2in", bib_buckets,
32 clib_bihash_init_48_8 (&db->
st.
in2out,
"st-in2out", st_buckets,
35 clib_bihash_init_48_8 (&db->
st.
out2in,
"st-out2in", st_buckets,
42 nat64_db_bib_entry_t *
45 u16 out_port,
u32 fib_index,
u8 proto,
48 nat64_db_bib_entry_t *bibe;
56 #define _(N, i, n, s) \ 57 case SNAT_PROTOCOL_##N: \ 58 pool_get (db->bib._##n##_bib, bibe); \ 59 kv.value = bibe - db->bib._##n##_bib; \ 66 kv.
value = bibe - db->
bib._unk_proto_bib;
69 memset (bibe, 0,
sizeof (*bibe));
70 bibe->in_addr.as_u64[0] = in_addr->
as_u64[0];
71 bibe->in_addr.as_u64[1] = in_addr->
as_u64[1];
72 bibe->in_port = in_port;
73 bibe->out_addr.as_u32 = out_addr->
as_u32;
74 bibe->out_port = out_port;
75 bibe->fib_index = fib_index;
77 bibe->is_static = is_static;
80 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
81 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
83 bibe_key.
port = bibe->in_port;
84 bibe_key.
proto = bibe->proto;
89 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 1);
91 memset (&bibe_key.
addr, 0, sizeof (bibe_key.
addr));
92 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
94 bibe_key.
port = bibe->out_port;
98 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 1);
108 nat64_db_bib_entry_t *bib;
109 u32 *ste_to_be_free = 0, *ste_index, bibe_index;
110 nat64_db_st_entry_t *st, *ste;
115 #define _(N, i, n, s) \ 116 case SNAT_PROTOCOL_##N: \ 117 bib = db->bib._##n##_bib; \ 118 st = db->st._##n##_st; \ 124 bib = db->
bib._unk_proto_bib;
125 st = db->
st._unk_proto_st;
129 bibe_index = bibe - bib;
136 if (ste->bibe_index == bibe_index)
137 vec_add1 (ste_to_be_free, ste - st);}
145 bibe_key.
addr.as_u64[0] = bibe->in_addr.as_u64[0];
146 bibe_key.
addr.as_u64[1] = bibe->in_addr.as_u64[1];
148 bibe_key.
port = bibe->in_port;
149 bibe_key.
proto = bibe->proto;
154 clib_bihash_add_del_24_8 (&db->
bib.
in2out, &kv, 0);
156 memset (&bibe_key.
addr, 0, sizeof (bibe_key.
addr));
157 bibe_key.
addr.ip4.as_u32 = bibe->out_addr.as_u32;
159 bibe_key.
port = bibe->out_port;
163 clib_bihash_add_del_24_8 (&db->
bib.
out2in, &kv, 0);
171 nat64_db_bib_entry_t *
173 u8 proto,
u32 fib_index,
u8 is_ip6)
175 nat64_db_bib_entry_t *bibe = 0;
178 nat64_db_bib_entry_t *bib;
183 #define _(N, i, n, s) \ 184 case SNAT_PROTOCOL_##N: \ 185 bib = db->bib._##n##_bib; \ 191 bib = db->
bib._unk_proto_bib;
195 bibe_key.
addr.as_u64[0] = addr->as_u64[0];
196 bibe_key.
addr.as_u64[1] = addr->as_u64[1];
198 bibe_key.
port = port;
199 bibe_key.
proto = proto;
206 if (!clib_bihash_search_24_8
217 nat64_db_bib_entry_t *bib, *bibe;
222 #define _(N, i, n, s) \ 223 bib = db->bib._##n##_bib; \ 224 pool_foreach (bibe, bib, ({ \ 225 if (fn (bibe, ctx)) \ 230 bib = db->
bib._unk_proto_bib;
242 #define _(N, i, n, s) \ 243 case SNAT_PROTOCOL_##N: \ 244 bib = db->bib._##n##_bib; \ 250 bib = db->
bib._unk_proto_bib;
264 nat64_db_bib_entry_t *
267 nat64_db_bib_entry_t *bib;
272 #define _(N, i, n, s) \ 273 case SNAT_PROTOCOL_##N: \ 274 bib = db->bib._##n##_bib; \ 280 bib = db->
bib._unk_proto_bib;
291 nat64_db_st_entry_t *st, *ste;
296 #define _(N, i, n, s) \ 297 st = db->st._##n##_st; \ 298 pool_foreach (ste, st, ({ \ 304 st = db->
st._unk_proto_st;
316 #define _(N, i, n, s) \ 317 case SNAT_PROTOCOL_##N: \ 318 st = db->st._##n##_st; \ 324 st = db->
st._unk_proto_st;
338 nat64_db_st_entry_t *
343 nat64_db_st_entry_t *ste;
344 nat64_db_bib_entry_t *bib;
352 #define _(N, i, n, s) \ 353 case SNAT_PROTOCOL_##N: \ 354 pool_get (db->st._##n##_st, ste); \ 355 kv.value = ste - db->st._##n##_st; \ 356 bib = db->bib._##n##_bib; \ 363 kv.
value = ste - db->
st._unk_proto_st;
364 bib = db->
bib._unk_proto_bib;
367 memset (ste, 0,
sizeof (*ste));
368 ste->in_r_addr.as_u64[0] = in_r_addr->
as_u64[0];
369 ste->in_r_addr.as_u64[1] = in_r_addr->
as_u64[1];
370 ste->out_r_addr.as_u32 = out_r_addr->
as_u32;
371 ste->r_port = r_port;
372 ste->bibe_index = bibe - bib;
373 ste->proto = bibe->proto;
379 memset (&ste_key, 0,
sizeof (ste_key));
380 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
381 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
382 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
383 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
385 ste_key.
l_port = bibe->in_port;
386 ste_key.
r_port = ste->r_port;
387 ste_key.
proto = ste->proto;
394 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 1);
396 memset (&ste_key, 0,
sizeof (ste_key));
397 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
398 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
399 ste_key.
l_port = bibe->out_port;
400 ste_key.
r_port = ste->r_port;
401 ste_key.
proto = ste->proto;
408 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 1);
416 nat64_db_st_entry_t *st;
417 nat64_db_bib_entry_t *bib, *bibe;
424 #define _(N, i, n, s) \ 425 case SNAT_PROTOCOL_##N: \ 426 st = db->st._##n##_st; \ 427 bib = db->bib._##n##_bib; \ 433 st = db->
st._unk_proto_st;
434 bib = db->
bib._unk_proto_bib;
441 memset (&ste_key, 0,
sizeof (ste_key));
442 ste_key.
l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
443 ste_key.
l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
444 ste_key.
r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
445 ste_key.
r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
447 ste_key.
l_port = bibe->in_port;
448 ste_key.
r_port = ste->r_port;
449 ste_key.
proto = ste->proto;
456 clib_bihash_add_del_48_8 (&db->
st.
in2out, &kv, 0);
458 memset (&ste_key, 0,
sizeof (ste_key));
459 ste_key.
l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
460 ste_key.
r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
461 ste_key.
l_port = bibe->out_port;
462 ste_key.
r_port = ste->r_port;
463 ste_key.
proto = ste->proto;
470 clib_bihash_add_del_48_8 (&db->
st.
out2in, &kv, 0);
479 if (!bibe->is_static && !bibe->ses_num)
483 nat64_db_st_entry_t *
485 ip46_address_t * r_addr,
u16 l_port,
u16 r_port,
486 u8 proto,
u32 fib_index,
u8 is_ip6)
488 nat64_db_st_entry_t *ste = 0;
489 nat64_db_st_entry_t *st;
496 #define _(N, i, n, s) \ 497 case SNAT_PROTOCOL_##N: \ 498 st = db->st._##n##_st; \ 504 st = db->
st._unk_proto_st;
508 memset (&ste_key, 0,
sizeof (ste_key));
509 ste_key.
l_addr.as_u64[0] = l_addr->as_u64[0];
510 ste_key.
l_addr.as_u64[1] = l_addr->as_u64[1];
511 ste_key.
r_addr.as_u64[0] = r_addr->as_u64[0];
512 ste_key.
r_addr.as_u64[1] = r_addr->as_u64[1];
516 ste_key.
proto = proto;
524 if (!clib_bihash_search_48_8
534 nat64_db_st_entry_t *st;
539 #define _(N, i, n, s) \ 540 case SNAT_PROTOCOL_##N: \ 541 st = db->st._##n##_st; \ 547 st = db->
st._unk_proto_st;
554 nat64_db_st_entry_t *
557 nat64_db_st_entry_t *st;
562 #define _(N, i, n, s) \ 563 case SNAT_PROTOCOL_##N: \ 564 st = db->st._##n##_st; \ 570 st = db->
st._unk_proto_st;
580 u32 *ste_to_be_free = 0, *ste_index;
581 nat64_db_st_entry_t *st, *ste;
584 #define _(N, i, n, s) \ 585 st = db->st._##n##_st; \ 586 pool_foreach (ste, st, ({\ 587 if (i == SNAT_PROTOCOL_TCP && !ste->tcp_state) \ 589 if (ste->expire < now) \ 590 vec_add1 (ste_to_be_free, ste - st); \ 592 vec_foreach (ste_index, ste_to_be_free) \ 593 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 594 vec_free (ste_to_be_free); \ 598 st = db->
st._unk_proto_st;
600 if (ste->expire < now)
601 vec_add1 (ste_to_be_free, ste - st);
612 u32 *ste_to_be_free = 0, *ste_index;
613 nat64_db_st_entry_t *st, *ste;
614 nat64_db_bib_entry_t *bibe;
617 #define _(N, i, n, s) \ 618 st = db->st._##n##_st; \ 619 pool_foreach (ste, st, ({ \ 620 bibe = pool_elt_at_index (db->bib._##n##_bib, ste->bibe_index); \ 621 if (bibe->out_addr.as_u32 == out_addr->as_u32) \ 622 vec_add1 (ste_to_be_free, ste - st); \ 624 vec_foreach (ste_index, ste_to_be_free) \ 625 nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \ 626 vec_free (ste_to_be_free); \ 630 st = db->
st._unk_proto_st;
633 if (bibe->out_addr.as_u32 == out_addr->
as_u32)
634 vec_add1 (ste_to_be_free, ste - st);
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.
#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).
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)
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.
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