GBP Endpoint Learning
[vpp.git] / src / plugins / gbp / gbp_endpoint.h
1 /*
2  * Copyright (c) 2018 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 __GBP_ENDPOINT_H__
17 #define __GBP_ENDPOINT_H__
18
19 #include <plugins/gbp/gbp_types.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ethernet/mac_address.h>
22
23 #include <vppinfra/bihash_16_8.h>
24 #include <vppinfra/bihash_template.h>
25 #include <vppinfra/bihash_24_8.h>
26 #include <vppinfra/bihash_template.h>
27
28 /**
29  * Flags for each endpoint
30  */
31 typedef enum gbp_endpoint_attr_t_
32 {
33   GBP_ENDPOINT_ATTR_FIRST = 0,
34   GBP_ENDPOINT_ATTR_BOUNCE = GBP_ENDPOINT_ATTR_FIRST,
35   GBP_ENDPOINT_ATTR_REMOTE = 1,
36   GBP_ENDPOINT_ATTR_LEARNT = 2,
37   GBP_ENDPOINT_ATTR_LAST,
38 } gbp_endpoint_attr_t;
39
40 typedef enum gbp_endpoint_flags_t_
41 {
42   GBP_ENDPOINT_FLAG_NONE = 0,
43   GBP_ENDPOINT_FLAG_BOUNCE = (1 << GBP_ENDPOINT_ATTR_BOUNCE),
44   GBP_ENDPOINT_FLAG_REMOTE = (1 << GBP_ENDPOINT_ATTR_REMOTE),
45   GBP_ENDPOINT_FLAG_LEARNT = (1 << GBP_ENDPOINT_ATTR_LEARNT),
46 } gbp_endpoint_flags_t;
47
48 #define GBP_ENDPOINT_ATTR_NAMES {                 \
49     [GBP_ENDPOINT_ATTR_BOUNCE] = "bounce",        \
50     [GBP_ENDPOINT_ATTR_REMOTE] = "remote",        \
51     [GBP_ENDPOINT_ATTR_LEARNT] = "learnt",        \
52 }
53
54 extern u8 *format_gbp_endpoint_flags (u8 * s, va_list * args);
55
56 /**
57  * A Group Based Policy Endpoint.
58  * This is typically a VM or container. If the endpoint is local (i.e. on
59  * the same compute node as VPP) then there is one interface per-endpoint.
60  * If the EP is remote,e.g. reachable over a [vxlan] tunnel, then there
61  * will be multiple EPs reachable over the tunnel and they can be distinguished
62  * via either their MAC or IP Address[es].
63  */
64 typedef struct gbp_endpoint_t_
65 {
66   /**
67    * The interface on which the EP is connected
68    */
69   index_t ge_itf;
70   u32 ge_sw_if_index;
71
72   /**
73    * A vector of ip addresses that below to the endpoint
74    */
75   const ip46_address_t *ge_ips;
76
77   /**
78    * MAC address of the endpoint
79    */
80   mac_address_t ge_mac;
81
82   /**
83    * Index of the Endpoint's Group
84    */
85   index_t ge_epg;
86
87   /**
88    * Endpoint Group's ID
89    */
90   index_t ge_epg_id;
91
92   /**
93    * Endpoint flags
94    */
95   gbp_endpoint_flags_t ge_flags;
96
97   /**
98    * The L3 adj, if created
99    */
100   index_t *ge_adjs;
101
102   /**
103    * The last time a packet from seen from this end point
104    */
105   f64 ge_last_time;
106
107   /**
108    * Tunnel info for remote endpoints
109    */
110   struct
111   {
112     u32 ge_parent_sw_if_index;
113     ip46_address_t ge_src;
114     ip46_address_t ge_dst;
115   } tun;
116 } gbp_endpoint_t;
117
118 extern u8 *format_gbp_endpoint (u8 * s, va_list * args);
119
120 /**
121  * GBP Endpoint Databases
122  */
123 typedef struct gbp_ep_by_ip_itf_db_t_
124 {
125   index_t *ged_by_sw_if_index;
126   clib_bihash_24_8_t ged_by_ip_rd;
127   clib_bihash_16_8_t ged_by_mac_bd;
128 } gbp_ep_db_t;
129
130 extern int gbp_endpoint_update (u32 sw_if_index,
131                                 const ip46_address_t * ip,
132                                 const mac_address_t * mac,
133                                 epg_id_t epg_id,
134                                 gbp_endpoint_flags_t flags,
135                                 const ip46_address_t * tun_src,
136                                 const ip46_address_t * tun_dst, u32 * handle);
137 extern void gbp_endpoint_delete (index_t gbpei);
138
139 typedef walk_rc_t (*gbp_endpoint_cb_t) (index_t gbpei, void *ctx);
140 extern void gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx);
141 extern void gbp_endpoint_scan (vlib_main_t * vm);
142 extern f64 gbp_endpoint_scan_threshold (void);
143 extern int gbp_endpoint_is_remote (const gbp_endpoint_t * ge);
144
145 extern void gbp_endpoint_flush (u32 sw_if_index);
146
147 /**
148  * DP functions and databases
149  */
150 extern gbp_ep_db_t gbp_ep_db;
151 extern gbp_endpoint_t *gbp_endpoint_pool;
152
153 /**
154  * Get the endpoint from a port/interface
155  */
156 always_inline gbp_endpoint_t *
157 gbp_endpoint_get (index_t gbpei)
158 {
159   return (pool_elt_at_index (gbp_endpoint_pool, gbpei));
160 }
161
162 static_always_inline void
163 gbp_endpoint_mk_key_mac (const u8 * mac,
164                          u32 bd_index, clib_bihash_kv_16_8_t * key)
165 {
166   key->key[0] = ethernet_mac_address_u64 (mac);
167   key->key[1] = bd_index;
168 }
169
170 static_always_inline gbp_endpoint_t *
171 gbp_endpoint_find_mac (const u8 * mac, u32 bd_index)
172 {
173   clib_bihash_kv_16_8_t key, value;
174   int rv;
175
176   gbp_endpoint_mk_key_mac (mac, bd_index, &key);
177
178   rv = clib_bihash_search_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, &value);
179
180   if (0 != rv)
181     return NULL;
182
183   return (gbp_endpoint_get (value.value));
184 }
185
186 static_always_inline void
187 gbp_endpoint_mk_key_ip (const ip46_address_t * ip,
188                         u32 fib_index, clib_bihash_kv_24_8_t * key)
189 {
190   key->key[0] = ip->as_u64[0];
191   key->key[1] = ip->as_u64[1];
192   key->key[2] = fib_index;
193 }
194
195 static_always_inline void
196 gbp_endpoint_mk_key_ip4 (const ip4_address_t * ip,
197                          u32 fib_index, clib_bihash_kv_24_8_t * key)
198 {
199   const ip46_address_t a = {
200     .ip4 = *ip,
201   };
202   gbp_endpoint_mk_key_ip (&a, fib_index, key);
203 }
204
205 static_always_inline gbp_endpoint_t *
206 gbp_endpoint_find_ip4 (const ip4_address_t * ip, u32 fib_index)
207 {
208   clib_bihash_kv_24_8_t key, value;
209   int rv;
210
211   gbp_endpoint_mk_key_ip4 (ip, fib_index, &key);
212
213   rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
214
215   if (0 != rv)
216     return NULL;
217
218   return (gbp_endpoint_get (value.value));
219 }
220
221 static_always_inline void
222 gbp_endpoint_mk_key_ip6 (const ip6_address_t * ip,
223                          u32 fib_index, clib_bihash_kv_24_8_t * key)
224 {
225   key->key[0] = ip->as_u64[0];
226   key->key[1] = ip->as_u64[1];
227   key->key[2] = fib_index;
228 }
229
230 static_always_inline gbp_endpoint_t *
231 gbp_endpoint_find_ip6 (const ip6_address_t * ip, u32 fib_index)
232 {
233   clib_bihash_kv_24_8_t key, value;
234   int rv;
235
236   gbp_endpoint_mk_key_ip6 (ip, fib_index, &key);
237
238   rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
239
240   if (0 != rv)
241     return NULL;
242
243   return (gbp_endpoint_get (value.value));
244 }
245
246 static_always_inline gbp_endpoint_t *
247 gbp_endpoint_find_itf (u32 sw_if_index)
248 {
249   index_t gei;
250
251   gei = gbp_ep_db.ged_by_sw_if_index[sw_if_index];
252
253   if (INDEX_INVALID != gei)
254     return (gbp_endpoint_get (gei));
255
256   return (NULL);
257 }
258
259
260 #endif
261
262 /*
263  * fd.io coding-style-patch-verification: ON
264  *
265  * Local Variables:
266  * eval: (c-set-style "gnu")
267  * End:
268  */