cbeb91a75a11541060f3dd946323b4284988c237
[vpp.git] / src / plugins / linux-cp / lcp_adj.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/adj/adj_delegate.h>
17 #include <linux-cp/lcp_adj.h>
18
19 #include <vppinfra/bihash_32_8.h>
20 #include <vppinfra/bihash_template.c>
21
22 static adj_delegate_type_t adj_type;
23 static lcp_adj_key_t *adj_keys;
24
25 /**
26  * The table of adjacencies indexed by the rewrite string
27  */
28 BVT (clib_bihash) lcp_adj_tbl;
29
30 static_always_inline void
31 lcp_adj_mk_key_adj (const ip_adjacency_t *adj, lcp_adj_key_t *key)
32 {
33   lcp_adj_mk_key (adj->rewrite_header.data, adj->rewrite_header.data_bytes,
34                   adj->rewrite_header.sw_if_index, key);
35 }
36
37 static u8 *
38 lcp_adj_delegate_format (const adj_delegate_t *aed, u8 *s)
39 {
40   return (format (s, "lcp"));
41 }
42
43 static void
44 lcp_adj_delegate_adj_deleted (adj_delegate_t *aed)
45 {
46   ip_adjacency_t *adj;
47   lcp_adj_kv_t kv;
48
49   adj = adj_get (aed->ad_adj_index);
50
51   lcp_adj_mk_key_adj (adj, &kv.k);
52
53   BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 0);
54
55   if (aed->ad_index != INDEX_INVALID)
56     pool_put_index (adj_keys, aed->ad_index);
57 }
58
59 /* when an adj is modified:
60  *
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
64  *
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
68  */
69 static void
70 lcp_adj_delegate_adj_modified (adj_delegate_t *aed)
71 {
72   ip_adjacency_t *adj;
73   lcp_adj_kv_t kv;
74   lcp_adj_key_t *adj_key = NULL;
75   u8 save_adj, key_changed;
76
77   key_changed = 0;
78
79   adj = adj_get (aed->ad_adj_index);
80   save_adj = (IP_LOOKUP_NEXT_REWRITE == adj->lookup_next_index);
81
82   if (aed->ad_index != INDEX_INVALID)
83     adj_key = pool_elt_at_index (adj_keys, aed->ad_index);
84
85   /* return if there was no stored adj and we will not add one */
86   if (!adj_key && !save_adj)
87     return;
88
89   /* build kv if a new entry should be stored */
90   if (save_adj)
91     {
92       lcp_adj_mk_key_adj (adj, &kv.k);
93       kv.v = aed->ad_adj_index;
94       if (adj_key)
95         key_changed = (clib_memcmp (adj_key, &kv.k, sizeof (*adj_key)) != 0);
96     }
97
98   /* delete old entry if needed */
99   if (adj_key && ((save_adj && key_changed) || (!save_adj)))
100     {
101       lcp_adj_kv_t old_kv;
102
103       clib_memcpy_fast (&old_kv.k, adj_key, sizeof (*adj_key));
104       old_kv.v = 0;
105
106       BV (clib_bihash_add_del) (&lcp_adj_tbl, &old_kv.kv, 0);
107
108       if (!save_adj)
109         {
110           pool_put (adj_keys, adj_key);
111           aed->ad_index = INDEX_INVALID;
112         }
113     }
114
115   /* add new entry if needed */
116   if (save_adj)
117     {
118       BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
119
120       if (!adj_key)
121         {
122           pool_get (adj_keys, adj_key);
123           aed->ad_index = adj_key - adj_keys;
124         }
125       clib_memcpy_fast (adj_key, &kv.k, sizeof (*adj_key));
126     }
127 }
128
129 static void
130 lcp_adj_delegate_adj_created (adj_index_t ai)
131 {
132   ip_adjacency_t *adj;
133   lcp_adj_kv_t kv;
134   index_t lai = INDEX_INVALID;
135   lcp_adj_key_t *adj_key;
136
137   adj = adj_get (ai);
138
139   if (IP_LOOKUP_NEXT_REWRITE == adj->lookup_next_index)
140     {
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));
144       kv.v = ai;
145
146       BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
147       lai = adj_key - adj_keys;
148     }
149
150   adj_delegate_add (adj, adj_type, lai);
151 }
152
153 u8 *
154 format_lcp_adj_kvp (u8 *s, va_list *args)
155 {
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;
159
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);
163
164   return (s);
165 }
166
167 static clib_error_t *
168 lcp_adj_show_cmd (vlib_main_t *vm, unformat_input_t *input,
169                   vlib_cli_command_t *cmd)
170 {
171   u8 verbose = 0;
172
173   if (unformat (input, "verbose"))
174     verbose = 1;
175
176   vlib_cli_output (vm, "Linux-CP Adjs:\n%U", BV (format_bihash), &lcp_adj_tbl,
177                    verbose);
178
179   return 0;
180 }
181
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",
186   .is_mp_safe = 1,
187 };
188
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,
194 };
195
196 static clib_error_t *
197 lcp_adj_init (vlib_main_t *vm)
198 {
199   adj_type = adj_delegate_register_new_type (&lcp_adj_vft);
200
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);
203
204   return (NULL);
205 }
206
207 VLIB_INIT_FUNCTION (lcp_adj_init);
208
209 /*
210  * fd.io coding-style-patch-verification: ON
211  *
212  * Local Variables:
213  * eval: (c-set-style "gnu")
214  * End:
215  */