linux-cp: Linux Interface Mirroring for Control Plane Integration
[vpp.git] / src / plugins / linux-cp / lcp_adj.h
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 #ifndef __LCP_ADJ_DELEGATE_H__
17 #define __LCP_ADJ_DELEGATE_H__
18
19 #include <vppinfra/bihash_32_8.h>
20
21 typedef struct lcp_adj_key_t_
22 {
23   u32 sw_if_index;
24   u8 rewrite[28];
25 } lcp_adj_key_t;
26
27 STATIC_ASSERT (sizeof (lcp_adj_key_t) == 32, "LCP ADJ Key size changed");
28
29 typedef struct lcp_adj_kv_t_
30 {
31   union
32   {
33     BVT (clib_bihash_kv) kv;
34     struct
35     {
36       lcp_adj_key_t k;
37       u64 v;
38     };
39   };
40 } lcp_adj_kv_t;
41
42 STATIC_ASSERT (sizeof (lcp_adj_kv_t) == sizeof (BVT (clib_bihash_kv)),
43                "LCP ADJ Key size changed");
44
45 /**
46  * The table of adjacencies indexed by the rewrite string
47  */
48 extern BVT (clib_bihash) lcp_adj_tbl;
49
50 static_always_inline void
51 lcp_adj_mk_key (const u8 *rewrite, u8 len, u32 sw_if_index, lcp_adj_key_t *key)
52 {
53   /*
54    * Construct the key from the provided rewrite, then pad with zeros
55    * to ensure the key does not have garbage bytes
56    */
57   ASSERT (len <= sizeof (key->rewrite));
58   clib_memcpy_fast (key->rewrite, rewrite, len);
59   clib_memset (key->rewrite + len, 0, sizeof (key->rewrite) - len);
60   key->sw_if_index = sw_if_index;
61 }
62
63 static_always_inline adj_index_t
64 lcp_adj_lkup (const u8 *rewrite, u8 len, u32 sw_if_index)
65 {
66   lcp_adj_kv_t kv;
67
68   lcp_adj_mk_key (rewrite, len, sw_if_index, &kv.k);
69
70   if (!BV (clib_bihash_search_inline) (&lcp_adj_tbl, &kv.kv))
71     return (kv.v);
72
73   return (ADJ_INDEX_INVALID);
74 }
75
76 /*
77  * fd.io coding-style-patch-verification: ON
78  *
79  * Local Variables:
80  * eval: (c-set-style "gnu")
81  * End:
82  */
83
84 #endif