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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/fib/fib_source.h>
17 #include <vnet/fib/fib_table.h>
18 #include <vnet/fib/fib_entry_track.h>
19 #include <vnet/dpo/load_balance.h>
20 #include <vnet/dpo/drop_dpo.h>
22 #include <cnat/cnat_translation.h>
23 #include <cnat/cnat_session.h>
24 #include <cnat/cnat_client.h>
26 cnat_translation_t *cnat_translation_pool;
27 clib_bihash_8_8_t cnat_translation_db;
28 addr_resolution_t *tr_resolutions;
30 typedef void (*cnat_if_addr_add_cb_t) (addr_resolution_t * ar,
31 ip_address_t * address, u8 is_del);
32 cnat_if_addr_add_cb_t *cnat_if_addr_add_cbs;
34 static fib_node_type_t cnat_translation_fib_node_type;
36 vlib_combined_counter_main_t cnat_translation_counters = {
37 .name = "cnat-translation",
38 .stat_segment_name = "/net/cnat-translation",
42 cnat_translation_watch_addr (index_t cti, u64 opaque, cnat_endpoint_t * ep,
43 cnat_addr_resol_type_t type)
45 addr_resolution_t *ar;
47 if (INDEX_INVALID == ep->ce_sw_if_index)
50 pool_get (tr_resolutions, ar);
51 ar->af = ep->ce_ip.version;
52 ar->sw_if_index = ep->ce_sw_if_index;
59 cnat_resolve_ep_tuple (cnat_endpoint_tuple_t * path)
61 cnat_resolve_ep (&path->src_ep);
62 cnat_resolve_ep (&path->dst_ep);
66 cnat_translation_unwatch_addr (u32 cti, cnat_addr_resol_type_t type)
68 /* Delete tr resolution entries matching translation index */
69 addr_resolution_t *ar;
70 index_t *indexes = 0, *ari;
71 pool_foreach (ar, tr_resolutions)
73 if ((cti == INDEX_INVALID || ar->cti == cti) &&
74 (ar->type == type || CNAT_RESOLV_ADDR_ANY == type))
75 vec_add1 (indexes, ar - tr_resolutions);
77 vec_foreach (ari, indexes) pool_put_index (tr_resolutions, *ari);
83 cnat_tracker_release (cnat_ep_trk_t * trk)
85 /* We only track fully resolved endpoints */
86 if (!(trk->ct_flags & CNAT_TRK_ACTIVE))
88 fib_entry_untrack (trk->ct_fei, trk->ct_sibling);
92 cnat_tracker_track (index_t cti, cnat_ep_trk_t * trk)
95 /* We only track fully resolved endpoints */
96 if (trk->ct_ep[VLIB_TX].ce_flags & CNAT_EP_FLAG_RESOLVED &&
97 trk->ct_ep[VLIB_RX].ce_flags & CNAT_EP_FLAG_RESOLVED)
98 trk->ct_flags |= CNAT_TRK_ACTIVE;
101 trk->ct_flags &= ~CNAT_TRK_ACTIVE;
105 ip_address_to_fib_prefix (&trk->ct_ep[VLIB_TX].ce_ip, &pfx);
106 trk->ct_fei = fib_entry_track (CNAT_FIB_TABLE,
108 cnat_translation_fib_node_type,
109 cti, &trk->ct_sibling);
111 fib_entry_contribute_forwarding (trk->ct_fei,
112 fib_forw_chain_type_from_fib_proto
113 (pfx.fp_proto), &trk->ct_dpo);
117 format_cnat_lb_type (u8 *s, va_list *args)
119 cnat_lb_type_t lb_type = va_arg (*args, int);
120 if (CNAT_LB_DEFAULT == lb_type)
121 s = format (s, "default");
122 else if (CNAT_LB_MAGLEV == lb_type)
123 s = format (s, "maglev");
125 s = format (s, "unknown");
130 unformat_cnat_lb_type (unformat_input_t *input, va_list *args)
132 cnat_lb_type_t *a = va_arg (*args, cnat_lb_type_t *);
133 if (unformat (input, "default"))
134 *a = CNAT_LB_DEFAULT;
135 else if (unformat (input, "maglev"))
143 * Add a translation to the bihash
145 * @param cci the ID of the parent client (invalid if vip not resolved)
146 * @param vip the translation endpoint
147 * @param proto the translation proto
148 * @param cti the translation index to be used as value
151 cnat_add_translation_to_db (index_t cci, cnat_endpoint_t * vip,
152 ip_protocol_t proto, index_t cti)
154 clib_bihash_kv_8_8_t bkey;
156 if (INDEX_INVALID == cci)
158 key = proto << 8 | 0x80 | vip->ce_ip.version;
159 key = key << 16 | vip->ce_port;
160 key = key << 32 | (u32) vip->ce_sw_if_index;
165 key = key << 16 | vip->ce_port;
166 key = key << 32 | (u32) cci;
172 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
176 * Remove a translation from the bihash
178 * @param cci the ID of the parent client
179 * @param vip the translation endpoint
180 * @param proto the translation proto
183 cnat_remove_translation_from_db (index_t cci, cnat_endpoint_t * vip,
186 clib_bihash_kv_8_8_t bkey;
188 if (INDEX_INVALID == cci)
190 key = proto << 8 | 0x80 | vip->ce_ip.version;
191 key = key << 16 | vip->ce_port;
192 key = key << 32 | (u32) vip->ce_sw_if_index;
197 key = key << 16 | vip->ce_port;
198 key = key << 32 | (u32) cci;
203 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
212 } cnat_maglev_entry_t;
215 cnat_maglev_entry_compare (void *_a, void *_b)
217 cnat_ep_trk_t *a = ((cnat_maglev_entry_t *) _a)->trk;
218 cnat_ep_trk_t *b = ((cnat_maglev_entry_t *) _b)->trk;
221 ip_address_cmp (&a->ct_ep[VLIB_TX].ce_ip, &b->ct_ep[VLIB_TX].ce_ip)))
223 if ((rv = a->ct_ep[VLIB_TX].ce_port - a->ct_ep[VLIB_TX].ce_port))
226 ip_address_cmp (&a->ct_ep[VLIB_RX].ce_ip, &b->ct_ep[VLIB_RX].ce_ip)))
228 if ((rv = a->ct_ep[VLIB_RX].ce_port - a->ct_ep[VLIB_RX].ce_port))
234 cnat_translation_init_maglev (cnat_translation_t *ct)
236 cnat_maglev_entry_t *backends = NULL, *bk;
237 cnat_main_t *cm = &cnat_main;
242 vec_foreach (trk, ct->ct_active_paths)
244 cnat_maglev_entry_t bk;
247 if (AF_IP4 == ip_addr_version (&trk->ct_ep[VLIB_TX].ce_ip))
250 a = ip_addr_v4 (&trk->ct_ep[VLIB_TX].ce_ip).data_u32;
251 b = trk->ct_ep[VLIB_TX].ce_port << 16 | trk->ct_ep[VLIB_RX].ce_port;
252 c = ip_addr_v4 (&trk->ct_ep[VLIB_RX].ce_ip).data_u32;
253 hash_v3_mix32 (a, b, c);
254 hash_v3_finalize32 (a, b, c);
261 a = ip_addr_v6 (&trk->ct_ep[VLIB_TX].ce_ip).as_u64[0] ^
262 ip_addr_v6 (&trk->ct_ep[VLIB_TX].ce_ip).as_u64[1];
263 b = trk->ct_ep[VLIB_TX].ce_port << 16 | trk->ct_ep[VLIB_RX].ce_port;
264 c = ip_addr_v6 (&trk->ct_ep[VLIB_RX].ce_ip).as_u64[0] ^
265 ip_addr_v6 (&trk->ct_ep[VLIB_RX].ce_ip).as_u64[1];
266 hash_mix64 (a, b, c);
271 bk.offset = h1 % cm->maglev_len;
272 bk.skip = h2 % (cm->maglev_len - 1) + 1;
275 vec_add1 (backends, bk);
281 vec_sort_with_function (backends, cnat_maglev_entry_compare);
283 /* Don't free if previous vector exists, just zero */
284 vec_validate (ct->lb_maglev, cm->maglev_len);
285 vec_set (ct->lb_maglev, -1);
289 vec_foreach (bk, backends)
292 u32 c = (bk->offset + next * bk->skip) % cm->maglev_len;
293 while (ct->lb_maglev[c] != (u32) -1)
296 c = (bk->offset + next * bk->skip) % cm->maglev_len;
298 ct->lb_maglev[c] = bk->index;
300 if (done == cm->maglev_len)
310 cnat_translation_stack (cnat_translation_t * ct)
312 fib_protocol_t fproto;
318 fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
319 dproto = fib_proto_to_dpo (fproto);
321 vec_reset_length (ct->ct_active_paths);
323 vec_foreach (trk, ct->ct_paths)
324 if (trk->ct_flags & CNAT_TRK_ACTIVE)
325 vec_add1 (ct->ct_active_paths, *trk);
327 lbi = load_balance_create (vec_len (ct->ct_active_paths),
328 fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
331 vec_foreach (trk, ct->ct_active_paths)
332 load_balance_set_bucket (lbi, ep_idx++, &trk->ct_dpo);
334 if (ep_idx > 0 && CNAT_LB_MAGLEV == ct->lb_type)
335 cnat_translation_init_maglev (ct);
337 dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
338 dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
339 ct->flags |= CNAT_TRANSLATION_STACKED;
343 cnat_translation_delete (u32 id)
345 cnat_translation_t *ct;
348 if (pool_is_free_index (cnat_translation_pool, id))
349 return (VNET_API_ERROR_NO_SUCH_ENTRY);
351 ct = pool_elt_at_index (cnat_translation_pool, id);
353 dpo_reset (&ct->ct_lb);
355 vec_foreach (trk, ct->ct_active_paths)
356 cnat_tracker_release (trk);
358 cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
359 cnat_client_translation_deleted (ct->ct_cci);
360 cnat_translation_unwatch_addr (id, CNAT_RESOLV_ADDR_ANY);
361 pool_put (cnat_translation_pool, ct);
367 cnat_translation_update (cnat_endpoint_t *vip, ip_protocol_t proto,
368 cnat_endpoint_tuple_t *paths, u8 flags,
369 cnat_lb_type_t lb_type)
371 cnat_endpoint_tuple_t *path;
372 const cnat_client_t *cc;
373 cnat_translation_t *ct;
378 if (cnat_resolve_ep (vip))
380 /* vip only contains a sw_if_index for now */
381 ct = cnat_find_translation (vip->ce_sw_if_index, vip->ce_port, proto);
386 /* do we know of this ep's vip */
387 cci = cnat_client_add (&vip->ce_ip, flags);
388 cc = cnat_client_get (cci);
390 ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
395 pool_get_zero (cnat_translation_pool, ct);
397 clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
398 ct->ct_proto = proto;
400 ct->index = ct - cnat_translation_pool;
401 ct->lb_type = lb_type;
403 cnat_add_translation_to_db (cci, vip, proto, ct->index);
404 cnat_client_translation_added (cci);
406 vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
407 vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
411 cnat_translation_unwatch_addr (ct->index, CNAT_RESOLV_ADDR_ANY);
412 cnat_translation_watch_addr (ct->index, 0, vip,
413 CNAT_RESOLV_ADDR_TRANSLATION);
415 vec_foreach (trk, ct->ct_paths)
417 cnat_tracker_release (trk);
420 vec_reset_length (ct->ct_paths);
421 ct->flags &= ~CNAT_TRANSLATION_STACKED;
424 vec_foreach (path, paths)
426 cnat_resolve_ep_tuple (path);
427 cnat_translation_watch_addr (ct->index,
428 path_idx << 32 | VLIB_RX, &path->src_ep,
429 CNAT_RESOLV_ADDR_BACKEND);
430 cnat_translation_watch_addr (ct->index,
431 path_idx << 32 | VLIB_TX, &path->dst_ep,
432 CNAT_RESOLV_ADDR_BACKEND);
435 vec_add2 (ct->ct_paths, trk, 1);
437 clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
438 sizeof (trk->ct_ep[VLIB_TX]));
439 clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
440 sizeof (trk->ct_ep[VLIB_RX]));
441 trk->ct_flags = path->ep_flags;
443 cnat_tracker_track (ct->index, trk);
446 cnat_translation_stack (ct);
452 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
456 pool_foreach_index (api, cnat_translation_pool)
464 format_cnat_ep_trk (u8 * s, va_list * args)
466 cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
467 u32 indent = va_arg (*args, u32);
469 s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
470 format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
471 s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
472 s = format (s, "\n%U%U",
473 format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
479 format_cnat_translation (u8 * s, va_list * args)
481 cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
482 cnat_main_t *cm = &cnat_main;
485 s = format (s, "[%d] ", ct->index);
486 s = format (s, "%U %U ", format_cnat_endpoint, &ct->ct_vip,
487 format_ip_protocol, ct->ct_proto);
488 s = format (s, "lb:%U ", format_cnat_lb_type, ct->lb_type);
490 vec_foreach (ck, ct->ct_paths)
491 s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
493 /* If printing a trace, the LB object might be deleted */
494 if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
496 s = format (s, "\n via:");
497 s = format (s, "\n%U%U",
498 format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
502 if (CNAT_LB_MAGLEV == ct->lb_type)
504 s = format (s, "\nmaglev backends map");
505 uword *bitmap = NULL;
506 clib_bitmap_alloc (bitmap, cm->maglev_len);
507 vec_foreach (ck, ct->ct_paths)
509 clib_bitmap_zero (bitmap);
510 for (u32 i = 0; i < vec_len (ct->lb_maglev); i++)
511 if (ct->lb_maglev[i] == bid)
512 clib_bitmap_set (bitmap, i, 1);
513 s = format (s, "\n backend#%d: %U", bid, format_bitmap_hex, bitmap);
517 clib_bitmap_free (bitmap);
523 static clib_error_t *
524 cnat_translation_show (vlib_main_t * vm,
525 unformat_input_t * input, vlib_cli_command_t * cmd)
528 cnat_translation_t *ct;
532 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
534 if (unformat (input, "%d", &cti))
537 return (clib_error_return (0, "unknown input '%U'",
538 format_unformat_error, input));
541 if (INDEX_INVALID == cti)
543 pool_foreach_index (cti, cnat_translation_pool)
545 ct = pool_elt_at_index (cnat_translation_pool, cti);
546 vlib_cli_output(vm, "%U", format_cnat_translation, ct);
551 vlib_cli_output (vm, "Invalid policy ID:%d", cti);
558 cnat_translation_purge (void)
560 /* purge all the translations */
561 index_t tri, *trp, *trs = NULL;
563 pool_foreach_index (tri, cnat_translation_pool)
568 vec_foreach (trp, trs) cnat_translation_delete (*trp);
570 ASSERT (0 == pool_elts (cnat_translation_pool));
577 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
578 .path = "show cnat translation",
579 .function = cnat_translation_show,
580 .short_help = "show cnat translation <VIP>",
585 cnat_translation_get_node (fib_node_index_t index)
587 cnat_translation_t *ct = cnat_translation_get (index);
588 return (&(ct->ct_node));
591 static cnat_translation_t *
592 cnat_translation_get_from_node (fib_node_t * node)
594 return ((cnat_translation_t *) (((char *) node) -
595 STRUCT_OFFSET_OF (cnat_translation_t,
600 cnat_translation_last_lock_gone (fib_node_t * node)
605 * A back walk has reached this ABF policy
607 static fib_node_back_walk_rc_t
608 cnat_translation_back_walk_notify (fib_node_t * node,
609 fib_node_back_walk_ctx_t * ctx)
612 * re-stack the fmask on the n-eos of the via
614 cnat_translation_t *ct = cnat_translation_get_from_node (node);
616 /* If we have more than FIB_PATH_LIST_POPULAR paths
617 * we might get called during path tracking
618 * (cnat_tracker_track) */
619 if (!(ct->flags & CNAT_TRANSLATION_STACKED))
620 return (FIB_NODE_BACK_WALK_CONTINUE);
622 cnat_translation_stack (ct);
624 return (FIB_NODE_BACK_WALK_CONTINUE);
628 * The translation's graph node virtual function table
630 static const fib_node_vft_t cnat_translation_vft = {
631 .fnv_get = cnat_translation_get_node,
632 .fnv_last_lock = cnat_translation_last_lock_gone,
633 .fnv_back_walk = cnat_translation_back_walk_notify,
636 static clib_error_t *
637 cnat_translation_cli_add_del (vlib_main_t * vm,
638 unformat_input_t * input,
639 vlib_cli_command_t * cmd)
641 u32 del_index = INDEX_INVALID;
642 ip_protocol_t proto = IP_PROTOCOL_TCP;
644 u8 flags = CNAT_FLAG_EXCLUSIVE;
645 cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
646 unformat_input_t _line_input, *line_input = &_line_input;
648 cnat_lb_type_t lb_type;
650 /* Get a line of input. */
651 if (!unformat_user (input, unformat_line_input, line_input))
654 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
656 if (unformat (line_input, "add"))
657 del_index = INDEX_INVALID;
658 else if (unformat (line_input, "del %d", &del_index))
661 if (unformat (line_input, "proto %U", unformat_ip_protocol, &proto))
663 else if (unformat (line_input, "vip %U", unformat_cnat_ep, &vip))
664 flags = CNAT_FLAG_EXCLUSIVE;
665 else if (unformat (line_input, "real %U", unformat_cnat_ep, &vip))
667 else if (unformat (line_input, "to %U", unformat_cnat_ep_tuple, &tmp))
669 vec_add2 (paths, path, 1);
670 clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
672 else if (unformat (line_input, "%U", unformat_cnat_lb_type, &lb_type))
676 e = clib_error_return (0, "unknown input '%U'",
677 format_unformat_error, line_input);
682 if (INDEX_INVALID == del_index)
683 cnat_translation_update (&vip, proto, paths, flags, lb_type);
685 cnat_translation_delete (del_index);
689 unformat_free (line_input);
693 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
695 .path = "cnat translation",
696 .short_help = "cnat translation [add|del] proto [TCP|UDP] [vip|real] [ip|sw_if_index [v6]] [port] [to [ip|sw_if_index [v6]] [port]->[ip|sw_if_index [v6]] [port]]",
697 .function = cnat_translation_cli_add_del,
701 cnat_if_addr_add_del_translation_cb (addr_resolution_t * ar,
702 ip_address_t * address, u8 is_del)
704 cnat_translation_t *ct;
705 ct = cnat_translation_get (ar->cti);
706 if (!is_del && ct->ct_vip.ce_flags & CNAT_EP_FLAG_RESOLVED)
709 cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
713 ct->ct_vip.ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
714 ct->ct_cci = INDEX_INVALID;
715 cnat_client_translation_deleted (ct->ct_cci);
716 /* Are there remaining addresses ? */
717 if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
723 ct->ct_cci = cnat_client_add (address, ct->flags);
724 cnat_client_translation_added (ct->ct_cci);
725 ip_address_copy (&ct->ct_vip.ce_ip, address);
726 ct->ct_vip.ce_flags |= CNAT_EP_FLAG_RESOLVED;
729 cnat_add_translation_to_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto,
734 cnat_if_addr_add_del_backend_cb (addr_resolution_t * ar,
735 ip_address_t * address, u8 is_del)
737 cnat_translation_t *ct;
741 u8 direction = ar->opaque & 0xf;
742 u32 path_idx = ar->opaque >> 32;
744 ct = cnat_translation_get (ar->cti);
746 trk = &ct->ct_paths[path_idx];
747 ep = &trk->ct_ep[direction];
749 if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
752 ASSERT (ep->ce_sw_if_index == ar->sw_if_index);
756 ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
757 /* Are there remaining addresses ? */
758 if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
764 ip_address_copy (&ep->ce_ip, address);
765 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
768 ct->flags &= ~CNAT_TRANSLATION_STACKED;
769 cnat_tracker_track (ar->cti, trk);
771 cnat_translation_stack (ct);
772 ct->flags |= CNAT_TRANSLATION_STACKED;
776 cnat_if_addr_add_del_snat_cb (addr_resolution_t * ar, ip_address_t * address,
780 ep = AF_IP4 == ar->af ? &cnat_main.snat_ip4 : &cnat_main.snat_ip6;
782 if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
787 ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
788 /* Are there remaining addresses ? */
789 if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
795 ip_address_copy (&ep->ce_ip, address);
796 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
802 cnat_if_addr_add_del_callback (u32 sw_if_index, ip_address_t * address,
805 addr_resolution_t *ar;
806 pool_foreach (ar, tr_resolutions)
808 if (ar->sw_if_index != sw_if_index)
810 if (ar->af != ip_addr_version (address))
812 cnat_if_addr_add_cbs[ar->type](ar, address, is_del);
817 cnat_ip6_if_addr_add_del_callback (struct ip6_main_t *im,
818 uword opaque, u32 sw_if_index,
819 ip6_address_t * address,
820 u32 address_length, u32 if_address_index,
824 ip_address_set (&addr, address, AF_IP6);
825 cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
829 cnat_ip4_if_addr_add_del_callback (struct ip4_main_t *im,
830 uword opaque, u32 sw_if_index,
831 ip4_address_t * address,
832 u32 address_length, u32 if_address_index,
836 ip_address_set (&addr, address, AF_IP4);
837 cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
840 static clib_error_t *
841 cnat_translation_init (vlib_main_t * vm)
843 ip4_main_t *i4m = &ip4_main;
844 ip6_main_t *i6m = &ip6_main;
845 cnat_main_t *cm = &cnat_main;
846 cnat_translation_fib_node_type =
847 fib_node_register_new_type (&cnat_translation_vft);
849 clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
850 cm->translation_hash_buckets,
851 cm->translation_hash_memory);
853 ip4_add_del_interface_address_callback_t cb4 = { 0 };
854 cb4.function = cnat_ip4_if_addr_add_del_callback;
855 vec_add1 (i4m->add_del_interface_address_callbacks, cb4);
857 ip6_add_del_interface_address_callback_t cb6 = { 0 };
858 cb6.function = cnat_ip6_if_addr_add_del_callback;
859 vec_add1 (i6m->add_del_interface_address_callbacks, cb6);
861 vec_validate (cnat_if_addr_add_cbs, CNAT_ADDR_N_RESOLUTIONS);
862 cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_BACKEND] =
863 cnat_if_addr_add_del_backend_cb;
864 cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_SNAT] = cnat_if_addr_add_del_snat_cb;
865 cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_TRANSLATION] =
866 cnat_if_addr_add_del_translation_cb;
870 VLIB_INIT_FUNCTION (cnat_translation_init);
873 * fd.io coding-style-patch-verification: ON
876 * eval: (c-set-style "gnu")