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;
29 static fib_node_type_t cnat_translation_fib_node_type;
31 vlib_combined_counter_main_t cnat_translation_counters = {
32 .name = "cnat-translation",
33 .stat_segment_name = "/net/cnat-translation",
37 cnat_tracker_release (cnat_ep_trk_t * trk)
39 fib_entry_untrack (trk->ct_fei, trk->ct_sibling);
43 cnat_tracker_track (index_t cti,
44 const cnat_endpoint_tuple_t * path, cnat_ep_trk_t * trk)
48 ip_address_to_fib_prefix (&path->dst_ep.ce_ip, &pfx);
50 clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
51 sizeof (trk->ct_ep[VLIB_TX]));
52 clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
53 sizeof (trk->ct_ep[VLIB_RX]));
55 trk->ct_fei = fib_entry_track (CNAT_FIB_TABLE,
57 cnat_translation_fib_node_type,
58 cti, &trk->ct_sibling);
60 fib_entry_contribute_forwarding (trk->ct_fei,
61 fib_forw_chain_type_from_fib_proto
62 (pfx.fp_proto), &trk->ct_dpo);
66 cnat_add_translation_to_db (index_t cci, u16 port, ip_protocol_t proto,
69 clib_bihash_kv_8_8_t bkey;
72 key = (proto << 16) | port;
73 key = key << 32 | (u32) cci;
78 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
82 cnat_remove_translation_from_db (index_t cci, u16 port, ip_protocol_t proto)
84 clib_bihash_kv_8_8_t bkey;
87 key = (proto << 16) | port;
88 key = key << 32 | (u32) cci;
92 clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
96 cnat_translation_stack (cnat_translation_t * ct)
98 fib_protocol_t fproto;
103 fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
104 dproto = fib_proto_to_dpo (fproto);
106 lbi = load_balance_create (vec_len (ct->ct_paths),
107 fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
109 vec_foreach (trk, ct->ct_paths)
110 load_balance_set_bucket (lbi, trk - ct->ct_paths, &trk->ct_dpo);
112 dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
113 dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
117 cnat_translation_delete (u32 id)
119 cnat_translation_t *ct;
122 if (pool_is_free_index (cnat_translation_pool, id))
123 return (VNET_API_ERROR_NO_SUCH_ENTRY);
125 ct = pool_elt_at_index (cnat_translation_pool, id);
127 dpo_reset (&ct->ct_lb);
129 vec_foreach (trk, ct->ct_paths) cnat_tracker_release (trk);
131 cnat_remove_translation_from_db (ct->ct_cci, ct->ct_vip.ce_port,
133 cnat_client_translation_deleted (ct->ct_cci);
134 pool_put (cnat_translation_pool, ct);
140 cnat_translation_update (const cnat_endpoint_t * vip,
142 const cnat_endpoint_tuple_t * paths, u8 flags)
144 const cnat_endpoint_tuple_t *path;
145 const cnat_client_t *cc;
146 cnat_translation_t *ct;
152 /* do we know of this ep's vip */
153 cci = cnat_client_add (&vip->ce_ip, flags);
154 cc = cnat_client_get (cci);
156 ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
160 pool_get_zero (cnat_translation_pool, ct);
162 clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
163 ct->ct_proto = proto;
165 ct->index = ct - cnat_translation_pool;
167 cnat_add_translation_to_db (cci, ct->ct_vip.ce_port, ct->ct_proto,
169 cnat_client_translation_added (cci);
171 vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
172 vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
176 vec_foreach (trk, ct->ct_paths)
178 cnat_tracker_release (trk);
181 vec_reset_length (ct->ct_paths);
183 vec_foreach (path, paths)
185 vec_add2 (ct->ct_paths, trk, 1);
187 cnat_tracker_track (ct->index, path, trk);
190 cnat_translation_stack (ct);
196 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
201 pool_foreach_index(api, cnat_translation_pool,
210 format_cnat_ep_trk (u8 * s, va_list * args)
212 cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
213 u32 indent = va_arg (*args, u32);
215 s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
216 format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
217 s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
218 s = format (s, "\n%U%U",
219 format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
225 format_cnat_translation (u8 * s, va_list * args)
227 cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
230 s = format (s, "[%d] ", ct->index);
231 s = format (s, "%U %U", format_cnat_endpoint, &ct->ct_vip,
232 format_ip_protocol, ct->ct_proto);
234 vec_foreach (ck, ct->ct_paths)
235 s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
237 /* If printing a trace, the LB object might be deleted */
238 if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
240 s = format (s, "\n via:");
241 s = format (s, "\n%U%U",
242 format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
248 static clib_error_t *
249 cnat_translation_show (vlib_main_t * vm,
250 unformat_input_t * input, vlib_cli_command_t * cmd)
253 cnat_translation_t *ct;
257 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
259 if (unformat (input, "%d", &cti))
262 return (clib_error_return (0, "unknown input '%U'",
263 format_unformat_error, input));
266 if (INDEX_INVALID == cti)
269 pool_foreach_index(cti, cnat_translation_pool,
271 ct = pool_elt_at_index (cnat_translation_pool, cti);
272 vlib_cli_output(vm, "%U", format_cnat_translation, ct);
278 vlib_cli_output (vm, "Invalid policy ID:%d", cti);
285 cnat_translation_purge (void)
287 /* purge all the translations */
288 index_t tri, *trp, *trs = NULL;
291 pool_foreach_index(tri, cnat_translation_pool,
297 vec_foreach (trp, trs) cnat_translation_delete (*trp);
299 ASSERT (0 == pool_elts (cnat_translation_pool));
307 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
308 .path = "show cnat translation",
309 .function = cnat_translation_show,
310 .short_help = "show cnat translation <VIP>",
316 cnat_translation_get_node (fib_node_index_t index)
318 cnat_translation_t *ct = cnat_translation_get (index);
319 return (&(ct->ct_node));
322 static cnat_translation_t *
323 cnat_translation_get_from_node (fib_node_t * node)
325 return ((cnat_translation_t *) (((char *) node) -
326 STRUCT_OFFSET_OF (cnat_translation_t,
331 cnat_translation_last_lock_gone (fib_node_t * node)
336 * A back walk has reached this ABF policy
338 static fib_node_back_walk_rc_t
339 cnat_translation_back_walk_notify (fib_node_t * node,
340 fib_node_back_walk_ctx_t * ctx)
343 * re-stack the fmask on the n-eos of the via
345 cnat_translation_t *ct = cnat_translation_get_from_node (node);
347 cnat_translation_stack (ct);
349 return (FIB_NODE_BACK_WALK_CONTINUE);
353 * The translation's graph node virtual function table
355 static const fib_node_vft_t cnat_translation_vft = {
356 .fnv_get = cnat_translation_get_node,
357 .fnv_last_lock = cnat_translation_last_lock_gone,
358 .fnv_back_walk = cnat_translation_back_walk_notify,
361 static clib_error_t *
362 cnat_translation_cli_add_del (vlib_main_t * vm,
363 unformat_input_t * input,
364 vlib_cli_command_t * cmd)
366 u32 del_index = INDEX_INVALID;
367 ip_protocol_t proto = IP_PROTOCOL_TCP;
369 u8 flags = CNAT_FLAG_EXCLUSIVE;
370 cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
372 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
374 if (unformat (input, "add"))
375 del_index = INDEX_INVALID;
376 else if (unformat (input, "del %d", &del_index))
378 else if (unformat (input, "proto %U", unformat_ip_protocol, &proto))
380 else if (unformat (input, "vip %U", unformat_cnat_ep, &vip))
381 flags = CNAT_FLAG_EXCLUSIVE;
382 else if (unformat (input, "real %U", unformat_cnat_ep, &vip))
384 else if (unformat (input, "to %U", unformat_cnat_ep_tuple, &tmp))
386 pool_get (paths, path);
387 clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
390 return (clib_error_return (0, "unknown input '%U'",
391 format_unformat_error, input));
394 if (INDEX_INVALID == del_index)
395 cnat_translation_update (&vip, proto, paths, flags);
397 cnat_translation_delete (del_index);
404 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
406 .path = "cnat translation",
407 .short_help = "cnat translation [add|del] proto [TCP|UDP] [vip|real] [ip] [port] [to [ip] [port]->[ip] [port]]",
408 .function = cnat_translation_cli_add_del,
412 static clib_error_t *
413 cnat_translation_init (vlib_main_t * vm)
415 cnat_main_t *cm = &cnat_main;
416 cnat_translation_fib_node_type =
417 fib_node_register_new_type (&cnat_translation_vft);
419 clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
420 cm->translation_hash_buckets,
421 cm->translation_hash_memory);
426 VLIB_INIT_FUNCTION (cnat_translation_init);
429 * fd.io coding-style-patch-verification: ON
432 * eval: (c-set-style "gnu")