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_maglev.h>
24 #include <cnat/cnat_session.h>
25 #include <cnat/cnat_client.h>
27 cnat_translation_t *cnat_translation_pool;
28 clib_bihash_8_8_t cnat_translation_db;
29 addr_resolution_t *tr_resolutions;
30 cnat_if_addr_add_cb_t *cnat_if_addr_add_cbs;
32 static fib_node_type_t cnat_translation_fib_node_type;
34 vlib_combined_counter_main_t cnat_translation_counters = {
35 .name = "cnat-translation",
36 .stat_segment_name = "/net/cnat-translation",
40 cnat_translation_watch_addr (index_t cti, u64 opaque, cnat_endpoint_t * ep,
41 cnat_addr_resol_type_t type)
43 addr_resolution_t *ar;
45 if (INDEX_INVALID == ep->ce_sw_if_index)
48 pool_get (tr_resolutions, ar);
49 ar->af = ep->ce_ip.version;
50 ar->sw_if_index = ep->ce_sw_if_index;
57 cnat_resolve_ep_tuple (cnat_endpoint_tuple_t * path)
59 cnat_resolve_ep (&path->src_ep);
60 cnat_resolve_ep (&path->dst_ep);
64 cnat_translation_unwatch_addr (u32 cti, cnat_addr_resol_type_t type)
66 /* Delete tr resolution entries matching translation index */
67 addr_resolution_t *ar;
68 index_t *indexes = 0, *ari;
69 pool_foreach (ar, tr_resolutions)
71 if ((cti == INDEX_INVALID || ar->cti == cti) &&
72 (ar->type == type || CNAT_RESOLV_ADDR_ANY == type))
73 vec_add1 (indexes, ar - tr_resolutions);
75 vec_foreach (ari, indexes) pool_put_index (tr_resolutions, *ari);
81 cnat_tracker_release (cnat_ep_trk_t * trk)
83 /* We only track fully resolved endpoints */
84 if (!(trk->ct_flags & CNAT_TRK_ACTIVE))
86 fib_entry_untrack (trk->ct_fei, trk->ct_sibling);
90 cnat_tracker_track (index_t cti, cnat_ep_trk_t * trk)
93 /* We only track fully resolved endpoints */
94 if (trk->ct_ep[VLIB_TX].ce_flags & CNAT_EP_FLAG_RESOLVED &&
95 trk->ct_ep[VLIB_RX].ce_flags & CNAT_EP_FLAG_RESOLVED)
96 trk->ct_flags |= CNAT_TRK_ACTIVE;
99 trk->ct_flags &= ~CNAT_TRK_ACTIVE;
103 ip_address_to_fib_prefix (&trk->ct_ep[VLIB_TX].ce_ip, &pfx);
104 trk->ct_fei = fib_entry_track (CNAT_FIB_TABLE,
106 cnat_translation_fib_node_type,
107 cti, &trk->ct_sibling);
109 fib_entry_contribute_forwarding (trk->ct_fei,
110 fib_forw_chain_type_from_fib_proto
111 (pfx.fp_proto), &trk->ct_dpo);
115 format_cnat_lb_type (u8 *s, va_list *args)
117 cnat_lb_type_t lb_type = va_arg (*args, int);
118 if (CNAT_LB_DEFAULT == lb_type)
119 s = format (s, "default");
120 else if (CNAT_LB_MAGLEV == lb_type)
121 s = format (s, "maglev");
123 s = format (s, "unknown");
128 unformat_cnat_lb_type (unformat_input_t *input, va_list *args)
130 cnat_lb_type_t *a = va_arg (*args, cnat_lb_type_t *);
131 if (unformat (input, "default"))
132 *a = CNAT_LB_DEFAULT;
133 else if (unformat (input, "maglev"))
141 * Add a translation to the bihash
143 * @param cci the ID of the parent client (invalid if vip not resolved)
144 * @param vip the translation endpoint
145 * @param proto the translation proto
146 * @param cti the translation index to be used as value
149 cnat_add_translation_to_db (index_t cci, cnat_endpoint_t * vip,
150 ip_protocol_t proto, index_t cti)
152 clib_bihash_kv_8_8_t bkey;
154 if (INDEX_INVALID == cci)
156 key = proto << 8 | 0x80 | vip->ce_ip.version;
157 key = key << 16 | vip->ce_port;
158 key = key << 32 | (u32) vip->ce_sw_if_index;
163 key = key << 16 | vip->ce_port;
164 key = key << 32 | (u32) cci;
170 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
174 * Remove a translation from the bihash
176 * @param cci the ID of the parent client
177 * @param vip the translation endpoint
178 * @param proto the translation proto
181 cnat_remove_translation_from_db (index_t cci, cnat_endpoint_t * vip,
184 clib_bihash_kv_8_8_t bkey;
186 if (INDEX_INVALID == cci)
188 key = proto << 8 | 0x80 | vip->ce_ip.version;
189 key = key << 16 | vip->ce_port;
190 key = key << 32 | (u32) vip->ce_sw_if_index;
195 key = key << 16 | vip->ce_port;
196 key = key << 32 | (u32) cci;
201 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
207 cnat_translation_stack (cnat_translation_t * ct)
209 fib_protocol_t fproto;
215 fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
216 dproto = fib_proto_to_dpo (fproto);
218 vec_reset_length (ct->ct_active_paths);
220 vec_foreach (trk, ct->ct_paths)
221 if (trk->ct_flags & CNAT_TRK_ACTIVE)
222 vec_add1 (ct->ct_active_paths, *trk);
224 lbi = load_balance_create (vec_len (ct->ct_active_paths),
225 fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
228 vec_foreach (trk, ct->ct_active_paths)
229 load_balance_set_bucket (lbi, ep_idx++, &trk->ct_dpo);
231 if (ep_idx > 0 && CNAT_LB_MAGLEV == ct->lb_type)
232 cnat_translation_init_maglev (ct);
234 dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
235 dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
236 ct->flags |= CNAT_TR_FLAG_STACKED;
240 cnat_translation_delete (u32 id)
242 cnat_translation_t *ct;
245 if (pool_is_free_index (cnat_translation_pool, id))
246 return (VNET_API_ERROR_NO_SUCH_ENTRY);
248 ct = pool_elt_at_index (cnat_translation_pool, id);
250 dpo_reset (&ct->ct_lb);
252 vec_foreach (trk, ct->ct_active_paths)
253 cnat_tracker_release (trk);
255 cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
256 cnat_client_translation_deleted (ct->ct_cci);
257 cnat_translation_unwatch_addr (id, CNAT_RESOLV_ADDR_ANY);
258 pool_put (cnat_translation_pool, ct);
264 cnat_translation_update (cnat_endpoint_t *vip, ip_protocol_t proto,
265 cnat_endpoint_tuple_t *paths, u8 flags,
266 cnat_lb_type_t lb_type)
268 cnat_endpoint_tuple_t *path;
269 const cnat_client_t *cc;
270 cnat_translation_t *ct;
275 if (cnat_resolve_ep (vip))
277 /* vip only contains a sw_if_index for now */
278 ct = cnat_find_translation (vip->ce_sw_if_index, vip->ce_port, proto);
283 /* do we know of this ep's vip */
284 cci = cnat_client_add (&vip->ce_ip, flags);
285 cc = cnat_client_get (cci);
287 ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
292 pool_get_zero (cnat_translation_pool, ct);
294 clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
295 ct->ct_proto = proto;
297 ct->index = ct - cnat_translation_pool;
298 ct->lb_type = lb_type;
300 cnat_add_translation_to_db (cci, vip, proto, ct->index);
301 cnat_client_translation_added (cci);
303 vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
304 vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
308 cnat_translation_unwatch_addr (ct->index, CNAT_RESOLV_ADDR_ANY);
309 cnat_translation_watch_addr (ct->index, 0, vip,
310 CNAT_RESOLV_ADDR_TRANSLATION);
312 vec_foreach (trk, ct->ct_paths)
314 cnat_tracker_release (trk);
317 vec_reset_length (ct->ct_paths);
318 ct->flags &= ~CNAT_TR_FLAG_STACKED;
321 vec_foreach (path, paths)
323 cnat_resolve_ep_tuple (path);
324 cnat_translation_watch_addr (ct->index,
325 path_idx << 32 | VLIB_RX, &path->src_ep,
326 CNAT_RESOLV_ADDR_BACKEND);
327 cnat_translation_watch_addr (ct->index,
328 path_idx << 32 | VLIB_TX, &path->dst_ep,
329 CNAT_RESOLV_ADDR_BACKEND);
332 vec_add2 (ct->ct_paths, trk, 1);
334 clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
335 sizeof (trk->ct_ep[VLIB_TX]));
336 clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
337 sizeof (trk->ct_ep[VLIB_RX]));
338 trk->ct_flags = path->ep_flags;
340 cnat_tracker_track (ct->index, trk);
343 cnat_translation_stack (ct);
349 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
353 pool_foreach_index (api, cnat_translation_pool)
361 format_cnat_ep_trk (u8 * s, va_list * args)
363 cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
364 u32 indent = va_arg (*args, u32);
366 s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
367 format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
368 s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
369 s = format (s, "\n%U%U",
370 format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
376 format_cnat_translation (u8 * s, va_list * args)
378 cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
379 cnat_main_t *cm = &cnat_main;
382 s = format (s, "[%d] ", ct->index);
383 s = format (s, "%U %U ", format_cnat_endpoint, &ct->ct_vip,
384 format_ip_protocol, ct->ct_proto);
385 s = format (s, "lb:%U ", format_cnat_lb_type, ct->lb_type);
387 vec_foreach (ck, ct->ct_paths)
388 s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
390 /* If printing a trace, the LB object might be deleted */
391 if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
393 s = format (s, "\n via:");
394 s = format (s, "\n%U%U",
395 format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
399 if (CNAT_LB_MAGLEV == ct->lb_type)
401 s = format (s, "\nmaglev backends map");
402 uword *bitmap = NULL;
403 clib_bitmap_alloc (bitmap, cm->maglev_len);
404 vec_foreach (ck, ct->ct_paths)
406 clib_bitmap_zero (bitmap);
407 for (u32 i = 0; i < vec_len (ct->lb_maglev); i++)
408 if (ct->lb_maglev[i] == bid)
409 clib_bitmap_set (bitmap, i, 1);
410 s = format (s, "\n backend#%d: %U", bid, format_bitmap_hex, bitmap);
414 clib_bitmap_free (bitmap);
420 static clib_error_t *
421 cnat_translation_show (vlib_main_t * vm,
422 unformat_input_t * input, vlib_cli_command_t * cmd)
425 cnat_translation_t *ct;
429 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
431 if (unformat (input, "%d", &cti))
434 return (clib_error_return (0, "unknown input '%U'",
435 format_unformat_error, input));
438 if (INDEX_INVALID == cti)
440 pool_foreach_index (cti, cnat_translation_pool)
442 ct = pool_elt_at_index (cnat_translation_pool, cti);
443 vlib_cli_output(vm, "%U", format_cnat_translation, ct);
448 vlib_cli_output (vm, "Invalid policy ID:%d", cti);
455 cnat_translation_purge (void)
457 /* purge all the translations */
458 index_t tri, *trp, *trs = NULL;
460 pool_foreach_index (tri, cnat_translation_pool)
465 vec_foreach (trp, trs) cnat_translation_delete (*trp);
467 ASSERT (0 == pool_elts (cnat_translation_pool));
474 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
475 .path = "show cnat translation",
476 .function = cnat_translation_show,
477 .short_help = "show cnat translation <VIP>",
482 cnat_translation_get_node (fib_node_index_t index)
484 cnat_translation_t *ct = cnat_translation_get (index);
485 return (&(ct->ct_node));
488 static cnat_translation_t *
489 cnat_translation_get_from_node (fib_node_t * node)
491 return ((cnat_translation_t *) (((char *) node) -
492 STRUCT_OFFSET_OF (cnat_translation_t,
497 cnat_translation_last_lock_gone (fib_node_t * node)
502 * A back walk has reached this ABF policy
504 static fib_node_back_walk_rc_t
505 cnat_translation_back_walk_notify (fib_node_t * node,
506 fib_node_back_walk_ctx_t * ctx)
509 * re-stack the fmask on the n-eos of the via
511 cnat_translation_t *ct = cnat_translation_get_from_node (node);
513 /* If we have more than FIB_PATH_LIST_POPULAR paths
514 * we might get called during path tracking
515 * (cnat_tracker_track) */
516 if (!(ct->flags & CNAT_TR_FLAG_STACKED))
517 return (FIB_NODE_BACK_WALK_CONTINUE);
519 cnat_translation_stack (ct);
521 return (FIB_NODE_BACK_WALK_CONTINUE);
525 * The translation's graph node virtual function table
527 static const fib_node_vft_t cnat_translation_vft = {
528 .fnv_get = cnat_translation_get_node,
529 .fnv_last_lock = cnat_translation_last_lock_gone,
530 .fnv_back_walk = cnat_translation_back_walk_notify,
533 static clib_error_t *
534 cnat_translation_cli_add_del (vlib_main_t * vm,
535 unformat_input_t * input,
536 vlib_cli_command_t * cmd)
538 u32 del_index = INDEX_INVALID;
539 ip_protocol_t proto = IP_PROTOCOL_TCP;
541 u8 flags = CNAT_FLAG_EXCLUSIVE;
542 cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
543 unformat_input_t _line_input, *line_input = &_line_input;
545 cnat_lb_type_t lb_type;
547 /* Get a line of input. */
548 if (!unformat_user (input, unformat_line_input, line_input))
551 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
553 if (unformat (line_input, "add"))
554 del_index = INDEX_INVALID;
555 else if (unformat (line_input, "del %d", &del_index))
558 if (unformat (line_input, "proto %U", unformat_ip_protocol, &proto))
560 else if (unformat (line_input, "vip %U", unformat_cnat_ep, &vip))
561 flags = CNAT_FLAG_EXCLUSIVE;
562 else if (unformat (line_input, "real %U", unformat_cnat_ep, &vip))
564 else if (unformat (line_input, "to %U", unformat_cnat_ep_tuple, &tmp))
566 vec_add2 (paths, path, 1);
567 clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
569 else if (unformat (line_input, "%U", unformat_cnat_lb_type, &lb_type))
573 e = clib_error_return (0, "unknown input '%U'",
574 format_unformat_error, line_input);
579 if (INDEX_INVALID == del_index)
580 cnat_translation_update (&vip, proto, paths, flags, lb_type);
582 cnat_translation_delete (del_index);
586 unformat_free (line_input);
590 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
592 .path = "cnat translation",
593 .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]]",
594 .function = cnat_translation_cli_add_del,
598 cnat_if_addr_add_del_translation_cb (addr_resolution_t * ar,
599 ip_address_t * address, u8 is_del)
601 cnat_translation_t *ct;
602 ct = cnat_translation_get (ar->cti);
603 if (!is_del && ct->ct_vip.ce_flags & CNAT_EP_FLAG_RESOLVED)
606 cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
610 ct->ct_vip.ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
611 ct->ct_cci = INDEX_INVALID;
612 cnat_client_translation_deleted (ct->ct_cci);
613 /* Are there remaining addresses ? */
614 if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
620 ct->ct_cci = cnat_client_add (address, ct->flags);
621 cnat_client_translation_added (ct->ct_cci);
622 ip_address_copy (&ct->ct_vip.ce_ip, address);
623 ct->ct_vip.ce_flags |= CNAT_EP_FLAG_RESOLVED;
626 cnat_add_translation_to_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto,
631 cnat_if_addr_add_del_backend_cb (addr_resolution_t * ar,
632 ip_address_t * address, u8 is_del)
634 cnat_translation_t *ct;
638 u8 direction = ar->opaque & 0xf;
639 u32 path_idx = ar->opaque >> 32;
641 ct = cnat_translation_get (ar->cti);
643 trk = &ct->ct_paths[path_idx];
644 ep = &trk->ct_ep[direction];
646 if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
649 ASSERT (ep->ce_sw_if_index == ar->sw_if_index);
653 ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
654 /* Are there remaining addresses ? */
655 if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
661 ip_address_copy (&ep->ce_ip, address);
662 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
665 ct->flags &= ~CNAT_TR_FLAG_STACKED;
666 cnat_tracker_track (ar->cti, trk);
668 cnat_translation_stack (ct);
669 ct->flags |= CNAT_TR_FLAG_STACKED;
673 cnat_if_addr_add_del_callback (u32 sw_if_index, ip_address_t * address,
676 addr_resolution_t *ar;
677 pool_foreach (ar, tr_resolutions)
679 if (ar->sw_if_index != sw_if_index)
681 if (ar->af != ip_addr_version (address))
683 cnat_if_addr_add_cbs[ar->type](ar, address, is_del);
688 cnat_ip6_if_addr_add_del_callback (struct ip6_main_t *im,
689 uword opaque, u32 sw_if_index,
690 ip6_address_t * address,
691 u32 address_length, u32 if_address_index,
695 ip_address_set (&addr, address, AF_IP6);
696 cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
700 cnat_ip4_if_addr_add_del_callback (struct ip4_main_t *im,
701 uword opaque, u32 sw_if_index,
702 ip4_address_t * address,
703 u32 address_length, u32 if_address_index,
707 ip_address_set (&addr, address, AF_IP4);
708 cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
712 cnat_translation_register_addr_add_cb (cnat_addr_resol_type_t typ,
713 cnat_if_addr_add_cb_t fn)
715 vec_validate (cnat_if_addr_add_cbs, CNAT_ADDR_N_RESOLUTIONS);
716 cnat_if_addr_add_cbs[typ] = fn;
719 static clib_error_t *
720 cnat_translation_init (vlib_main_t * vm)
722 ip4_main_t *i4m = &ip4_main;
723 ip6_main_t *i6m = &ip6_main;
724 cnat_main_t *cm = &cnat_main;
725 cnat_translation_fib_node_type =
726 fib_node_register_new_type ("cnat-translation", &cnat_translation_vft);
728 clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
729 cm->translation_hash_buckets,
730 cm->translation_hash_memory);
732 ip4_add_del_interface_address_callback_t cb4 = { 0 };
733 cb4.function = cnat_ip4_if_addr_add_del_callback;
734 vec_add1 (i4m->add_del_interface_address_callbacks, cb4);
736 ip6_add_del_interface_address_callback_t cb6 = { 0 };
737 cb6.function = cnat_ip6_if_addr_add_del_callback;
738 vec_add1 (i6m->add_del_interface_address_callbacks, cb6);
740 cnat_translation_register_addr_add_cb (CNAT_RESOLV_ADDR_BACKEND,
741 cnat_if_addr_add_del_backend_cb);
742 cnat_translation_register_addr_add_cb (CNAT_RESOLV_ADDR_TRANSLATION,
743 cnat_if_addr_add_del_translation_cb);
748 VLIB_INIT_FUNCTION (cnat_translation_init);
751 * fd.io coding-style-patch-verification: ON
754 * eval: (c-set-style "gnu")