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/adj/adj_delegate.h>
17 #include <linux-cp/lcp_adj.h>
19 #include <vppinfra/bihash_32_8.h>
20 #include <vppinfra/bihash_template.c>
22 static adj_delegate_type_t adj_type;
23 static lcp_adj_key_t *adj_keys;
26 * The table of adjacencies indexed by the rewrite string
28 BVT (clib_bihash) lcp_adj_tbl;
30 static_always_inline void
31 lcp_adj_mk_key_adj (const ip_adjacency_t *adj, lcp_adj_key_t *key)
33 lcp_adj_mk_key (adj->rewrite_header.data, adj->rewrite_header.data_bytes,
34 adj->rewrite_header.sw_if_index, key);
38 lcp_adj_delegate_format (const adj_delegate_t *aed, u8 *s)
40 return (format (s, "lcp"));
44 lcp_adj_delegate_adj_deleted (adj_delegate_t *aed)
49 adj = adj_get (aed->ad_adj_index);
51 lcp_adj_mk_key_adj (adj, &kv.k);
53 BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 0);
55 if (aed->ad_index != INDEX_INVALID)
56 pool_put_index (adj_keys, aed->ad_index);
59 /* when an adj is modified:
61 * An existing hash entry may need to be deleted. This may occur when:
62 * * The newly modified adj does not have IP_LOOKUP_NEXT_REWRITE as next idx
63 * * The rewrite (== major component of hash key) changed
65 * A new hash entry may need to be added. This may occur when:
66 * * The newly modified adj has IP_LOOKUP_NEXT_REWRITE as next idx
67 * * The rewrite changed or there was no existing hash entry
70 lcp_adj_delegate_adj_modified (adj_delegate_t *aed)
74 lcp_adj_key_t *adj_key = NULL;
75 u8 save_adj, key_changed;
79 adj = adj_get (aed->ad_adj_index);
80 save_adj = (IP_LOOKUP_NEXT_REWRITE == adj->lookup_next_index);
82 if (aed->ad_index != INDEX_INVALID)
83 adj_key = pool_elt_at_index (adj_keys, aed->ad_index);
85 /* return if there was no stored adj and we will not add one */
86 if (!adj_key && !save_adj)
89 /* build kv if a new entry should be stored */
92 lcp_adj_mk_key_adj (adj, &kv.k);
93 kv.v = aed->ad_adj_index;
95 key_changed = (clib_memcmp (adj_key, &kv.k, sizeof (*adj_key)) != 0);
98 /* delete old entry if needed */
99 if (adj_key && ((save_adj && key_changed) || (!save_adj)))
103 clib_memcpy_fast (&old_kv.k, adj_key, sizeof (*adj_key));
106 BV (clib_bihash_add_del) (&lcp_adj_tbl, &old_kv.kv, 0);
110 pool_put (adj_keys, adj_key);
111 aed->ad_index = INDEX_INVALID;
115 /* add new entry if needed */
118 BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
122 pool_get (adj_keys, adj_key);
123 aed->ad_index = adj_key - adj_keys;
125 clib_memcpy_fast (adj_key, &kv.k, sizeof (*adj_key));
130 lcp_adj_delegate_adj_created (adj_index_t ai)
134 index_t lai = INDEX_INVALID;
135 lcp_adj_key_t *adj_key;
139 if (IP_LOOKUP_NEXT_REWRITE == adj->lookup_next_index)
141 lcp_adj_mk_key_adj (adj, &kv.k);
142 pool_get (adj_keys, adj_key);
143 clib_memcpy_fast (adj_key, &kv.k, sizeof (*adj_key));
146 BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
147 lai = adj_key - adj_keys;
150 adj_delegate_add (adj, adj_type, lai);
154 format_lcp_adj_kvp (u8 *s, va_list *args)
156 BVT (clib_bihash_kv) *kv = va_arg (*args, BVT (clib_bihash_kv) *);
157 CLIB_UNUSED (int verbose) = va_arg (*args, int);
158 lcp_adj_kv_t *akv = (lcp_adj_kv_t *) kv;
160 s = format (s, " %U:%U\n %U", format_vnet_sw_if_index_name,
161 vnet_get_main (), akv->k.sw_if_index, format_hex_bytes,
162 akv->k.rewrite, 18, format_adj_nbr, akv->v, 4);
167 static clib_error_t *
168 lcp_adj_show_cmd (vlib_main_t *vm, unformat_input_t *input,
169 vlib_cli_command_t *cmd)
173 if (unformat (input, "verbose"))
176 vlib_cli_output (vm, "Linux-CP Adjs:\n%U", BV (format_bihash), &lcp_adj_tbl,
182 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
183 .path = "show lcp adj",
184 .function = lcp_adj_show_cmd,
185 .short_help = "show lcp adj",
189 const adj_delegate_vft_t lcp_adj_vft = {
190 .adv_format = lcp_adj_delegate_format,
191 .adv_adj_deleted = lcp_adj_delegate_adj_deleted,
192 .adv_adj_modified = lcp_adj_delegate_adj_modified,
193 .adv_adj_created = lcp_adj_delegate_adj_created,
196 static clib_error_t *
197 lcp_adj_init (vlib_main_t *vm)
199 adj_type = adj_delegate_register_new_type (&lcp_adj_vft);
201 BV (clib_bihash_init) (&lcp_adj_tbl, "linux-cp ADJ table", 1024, 1 << 24);
202 BV (clib_bihash_set_kvp_format_fn) (&lcp_adj_tbl, format_lcp_adj_kvp);
207 VLIB_INIT_FUNCTION (lcp_adj_init);
210 * fd.io coding-style-patch-verification: ON
213 * eval: (c-set-style "gnu")