linux-cp: Linux Interface Mirroring for Control Plane Integration
[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
24 /**
25  * The table of adjacencies indexed by the rewrite string
26  */
27 BVT (clib_bihash) lcp_adj_tbl;
28
29 static_always_inline void
30 lcp_adj_mk_key_adj (const ip_adjacency_t *adj, lcp_adj_key_t *key)
31 {
32   lcp_adj_mk_key (adj->rewrite_header.data, adj->rewrite_header.data_bytes,
33                   adj->rewrite_header.sw_if_index, key);
34 }
35
36 static u8 *
37 lcp_adj_delegate_format (const adj_delegate_t *aed, u8 *s)
38 {
39   return (format (s, "lcp"));
40 }
41
42 static void
43 lcp_adj_delegate_adj_deleted (adj_delegate_t *aed)
44 {
45   ip_adjacency_t *adj;
46   lcp_adj_kv_t kv;
47
48   adj = adj_get (aed->ad_adj_index);
49
50   lcp_adj_mk_key_adj (adj, &kv.k);
51
52   BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 0);
53 }
54
55 static void
56 lcp_adj_delegate_adj_modified (adj_delegate_t *aed)
57 {
58   ip_adjacency_t *adj;
59   lcp_adj_kv_t kv;
60
61   adj = adj_get (aed->ad_adj_index);
62
63   if (IP_LOOKUP_NEXT_REWRITE != adj->lookup_next_index)
64     return;
65
66   lcp_adj_mk_key_adj (adj, &kv.k);
67   kv.v = aed->ad_adj_index;
68
69   BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
70 }
71
72 static void
73 lcp_adj_delegate_adj_created (adj_index_t ai)
74 {
75   ip_adjacency_t *adj;
76   lcp_adj_kv_t kv;
77
78   adj = adj_get (ai);
79
80   if (IP_LOOKUP_NEXT_REWRITE != adj->lookup_next_index)
81     return;
82
83   lcp_adj_mk_key_adj (adj, &kv.k);
84   kv.v = ai;
85
86   BV (clib_bihash_add_del) (&lcp_adj_tbl, &kv.kv, 1);
87 }
88
89 u8 *
90 format_lcp_adj_kvp (u8 *s, va_list *args)
91 {
92   BVT (clib_bihash_kv) *kv = va_arg (*args, BVT (clib_bihash_kv) *);
93   CLIB_UNUSED (int verbose) = va_arg (*args, int);
94   lcp_adj_kv_t *akv = (lcp_adj_kv_t *) kv;
95
96   s = format (s, "  %U:%U\n    %U", format_vnet_sw_if_index_name,
97               vnet_get_main (), akv->k.sw_if_index, format_hex_bytes,
98               akv->k.rewrite, 18, format_adj_nbr, akv->v, 4);
99
100   return (s);
101 }
102
103 static clib_error_t *
104 lcp_adj_show_cmd (vlib_main_t *vm, unformat_input_t *input,
105                   vlib_cli_command_t *cmd)
106 {
107   u8 verbose = 0;
108
109   if (unformat (input, "verbose"))
110     verbose = 1;
111
112   vlib_cli_output (vm, "Linux-CP Adjs:\n%U", BV (format_bihash), &lcp_adj_tbl,
113                    verbose);
114
115   return 0;
116 }
117
118 VLIB_CLI_COMMAND (lcp_itf_pair_show_cmd_node, static) = {
119   .path = "show lcp adj",
120   .function = lcp_adj_show_cmd,
121   .short_help = "show lcp adj",
122   .is_mp_safe = 1,
123 };
124
125 const adj_delegate_vft_t lcp_adj_vft = {
126   .adv_format = lcp_adj_delegate_format,
127   .adv_adj_deleted = lcp_adj_delegate_adj_deleted,
128   .adv_adj_modified = lcp_adj_delegate_adj_modified,
129   .adv_adj_created = lcp_adj_delegate_adj_created,
130 };
131
132 static clib_error_t *
133 lcp_adj_init (vlib_main_t *vm)
134 {
135   adj_type = adj_delegate_register_new_type (&lcp_adj_vft);
136
137   BV (clib_bihash_init) (&lcp_adj_tbl, "linux-cp ADJ table", 1024, 1 << 24);
138   BV (clib_bihash_set_kvp_format_fn) (&lcp_adj_tbl, format_lcp_adj_kvp);
139
140   return (NULL);
141 }
142
143 VLIB_INIT_FUNCTION (lcp_adj_init);
144
145 /*
146  * fd.io coding-style-patch-verification: ON
147  *
148  * Local Variables:
149  * eval: (c-set-style "gnu")
150  * End:
151  */