gbp: Consider data-plane learnt source better than control-plane
[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,
36   GBP_ENDPOINT_ATTR_LEARNT,
37   GBP_ENDPOINT_ATTR_EXTERNAL,
38   GBP_ENDPOINT_ATTR_LAST,
39 } gbp_endpoint_attr_t;
40
41 typedef enum gbp_endpoint_flags_t_
42 {
43   GBP_ENDPOINT_FLAG_NONE = 0,
44   GBP_ENDPOINT_FLAG_BOUNCE = (1 << GBP_ENDPOINT_ATTR_BOUNCE),
45   GBP_ENDPOINT_FLAG_REMOTE = (1 << GBP_ENDPOINT_ATTR_REMOTE),
46   GBP_ENDPOINT_FLAG_LEARNT = (1 << GBP_ENDPOINT_ATTR_LEARNT),
47   GBP_ENDPOINT_FLAG_EXTERNAL = (1 << GBP_ENDPOINT_ATTR_EXTERNAL),
48 } gbp_endpoint_flags_t;
49
50 #define GBP_ENDPOINT_ATTR_NAMES {                 \
51     [GBP_ENDPOINT_ATTR_BOUNCE] = "bounce",        \
52     [GBP_ENDPOINT_ATTR_REMOTE] = "remote",        \
53     [GBP_ENDPOINT_ATTR_LEARNT] = "learnt",        \
54     [GBP_ENDPOINT_ATTR_EXTERNAL] = "external",    \
55 }
56
57 extern u8 *format_gbp_endpoint_flags (u8 * s, va_list * args);
58
59 /**
60  * Sources of Endpoints in priority order. The best (lowest value) source
61  * provides the forwarding information.
62  * Data-plane takes preference because the CP data is not always complete,
63  * it may not have the sclass.
64  */
65 #define foreach_gbp_endpoint_src    \
66   _(DP, "data-plane")               \
67   _(CP, "control-plane")            \
68   _(RR, "recursive-resolution")
69
70 typedef enum gbp_endpoint_src_t_
71 {
72 #define _(v,s) GBP_ENDPOINT_SRC_##v,
73   foreach_gbp_endpoint_src
74 #undef _
75 } gbp_endpoint_src_t;
76
77 #define GBP_ENDPOINT_SRC_MAX (GBP_ENDPOINT_SRC_RR+1)
78
79 extern u8 *format_gbp_endpoint_src (u8 * s, va_list * args);
80
81 /**
82  * This is the identity of an endpoint, as such it is information
83  * about an endpoint that is idempotent.
84  * The ID is used to add the EP into the various data-bases for retrieval.
85  */
86 typedef struct gbp_endpoint_key_t_
87 {
88   /**
89    * A vector of ip addresses that belong to the endpoint.
90    * Together with the route EPG's RD this forms the EP's L3 key
91    */
92   fib_prefix_t *gek_ips;
93
94   /**
95    * MAC address of the endpoint.
96    * Together with the route EPG's BD this forms the EP's L2 key
97    */
98   mac_address_t gek_mac;
99
100   /**
101    * Index of the Bridge-Domain
102    */
103   index_t gek_gbd;
104
105   /**
106    * Index of the Route-Domain
107    */
108   index_t gek_grd;
109 } gbp_endpoint_key_t;
110
111 /**
112  * Information about the location of the endpoint provided by a source
113  * of endpoints
114  */
115 typedef struct gbp_endpoint_loc_t_
116 {
117   /**
118    * The source providing this location information
119    */
120   gbp_endpoint_src_t gel_src;
121
122   /**
123    * The interface on which the EP is connected
124    */
125   u32 gel_sw_if_index;
126
127   /**
128    * Endpoint flags
129    */
130   gbp_endpoint_flags_t gel_flags;
131
132   /**
133    * Endpoint Group.
134    */
135   index_t gel_epg;
136
137   /**
138    * number of times this source has locked this
139    */
140   u32 gel_locks;
141
142   /**
143    * Tunnel info for remote endpoints
144    */
145   struct
146   {
147     u32 gel_parent_sw_if_index;
148     ip46_address_t gel_src;
149     ip46_address_t gel_dst;
150   } tun;
151 } gbp_endpoint_loc_t;
152
153 /**
154  * And endpoints current forwarding state
155  */
156 typedef struct gbp_endpoint_fwd_t_
157 {
158   /**
159    * The interface on which the EP is connected
160    */
161   index_t gef_itf;
162
163   /**
164    * The L3 adj, if created
165    */
166   index_t *gef_adjs;
167
168   /**
169    * Endpoint Group's sclass. cached for fast DP access.
170    */
171   sclass_t gef_sclass;
172
173   /**
174    * FIB index the EP is in
175    */
176   u32 gef_fib_index;
177
178   gbp_endpoint_flags_t gef_flags;
179 } gbp_endpoint_fwd_t;
180
181 /**
182  * A Group Based Policy Endpoint.
183  * This is typically a VM or container. If the endpoint is local (i.e. on
184  * the same compute node as VPP) then there is one interface per-endpoint.
185  * If the EP is remote,e.g. reachable over a [vxlan] tunnel, then there
186  * will be multiple EPs reachable over the tunnel and they can be distinguished
187  * via either their MAC or IP Address[es].
188  */
189 typedef struct gbp_endpoint_t_
190 {
191   /**
192    * A FIB node that allows the tracking of children.
193    */
194   fib_node_t ge_node;
195
196   /**
197    * The key/ID of this EP
198    */
199   gbp_endpoint_key_t ge_key;
200
201   /**
202    * Location information provided by the various sources.
203    * These are sorted based on source priority.
204    */
205   gbp_endpoint_loc_t *ge_locs;
206
207   gbp_endpoint_fwd_t ge_fwd;
208
209   /**
210    * The last time a packet from seen from this end point
211    */
212   f64 ge_last_time;
213 } gbp_endpoint_t;
214
215 extern u8 *format_gbp_endpoint (u8 * s, va_list * args);
216
217 /**
218  * GBP Endpoint Databases
219  */
220 typedef struct gbp_ep_by_ip_itf_db_t_
221 {
222   index_t *ged_by_sw_if_index;
223   clib_bihash_24_8_t ged_by_ip_rd;
224   clib_bihash_16_8_t ged_by_mac_bd;
225 } gbp_ep_db_t;
226
227 extern int gbp_endpoint_update_and_lock (gbp_endpoint_src_t src,
228                                          u32 sw_if_index,
229                                          const ip46_address_t * ip,
230                                          const mac_address_t * mac,
231                                          index_t gbd, index_t grd,
232                                          sclass_t sclass,
233                                          gbp_endpoint_flags_t flags,
234                                          const ip46_address_t * tun_src,
235                                          const ip46_address_t * tun_dst,
236                                          u32 * handle);
237 extern void gbp_endpoint_unlock (gbp_endpoint_src_t src, index_t gbpei);
238 extern u32 gbp_endpoint_child_add (index_t gei,
239                                    fib_node_type_t type,
240                                    fib_node_index_t index);
241 extern void gbp_endpoint_child_remove (index_t gei, u32 sibling);
242
243 typedef walk_rc_t (*gbp_endpoint_cb_t) (index_t gbpei, void *ctx);
244 extern void gbp_endpoint_walk (gbp_endpoint_cb_t cb, void *ctx);
245 extern void gbp_endpoint_scan (vlib_main_t * vm);
246 extern int gbp_endpoint_is_remote (const gbp_endpoint_t * ge);
247 extern int gbp_endpoint_is_local (const gbp_endpoint_t * ge);
248 extern int gbp_endpoint_is_external (const gbp_endpoint_t * ge);
249 extern int gbp_endpoint_is_learnt (const gbp_endpoint_t * ge);
250
251
252 extern void gbp_endpoint_flush (gbp_endpoint_src_t src, u32 sw_if_index);
253
254 /**
255  * DP functions and databases
256  */
257 extern gbp_ep_db_t gbp_ep_db;
258 extern gbp_endpoint_t *gbp_endpoint_pool;
259
260 /**
261  * Get the endpoint from a port/interface
262  */
263 always_inline gbp_endpoint_t *
264 gbp_endpoint_get (index_t gbpei)
265 {
266   return (pool_elt_at_index (gbp_endpoint_pool, gbpei));
267 }
268
269 static_always_inline void
270 gbp_endpoint_mk_key_mac (const u8 * mac,
271                          u32 bd_index, clib_bihash_kv_16_8_t * key)
272 {
273   key->key[0] = ethernet_mac_address_u64 (mac);
274   key->key[1] = bd_index;
275 }
276
277 static_always_inline gbp_endpoint_t *
278 gbp_endpoint_find_mac (const u8 * mac, u32 bd_index)
279 {
280   clib_bihash_kv_16_8_t key, value;
281   int rv;
282
283   gbp_endpoint_mk_key_mac (mac, bd_index, &key);
284
285   rv = clib_bihash_search_16_8 (&gbp_ep_db.ged_by_mac_bd, &key, &value);
286
287   if (0 != rv)
288     return NULL;
289
290   return (gbp_endpoint_get (value.value));
291 }
292
293 static_always_inline void
294 gbp_endpoint_mk_key_ip (const ip46_address_t * ip,
295                         u32 fib_index, clib_bihash_kv_24_8_t * key)
296 {
297   key->key[0] = ip->as_u64[0];
298   key->key[1] = ip->as_u64[1];
299   key->key[2] = fib_index;
300 }
301
302 static_always_inline void
303 gbp_endpoint_mk_key_ip4 (const ip4_address_t * ip,
304                          u32 fib_index, clib_bihash_kv_24_8_t * key)
305 {
306   const ip46_address_t a = {
307     .ip4 = *ip,
308   };
309   gbp_endpoint_mk_key_ip (&a, fib_index, key);
310 }
311
312 static_always_inline gbp_endpoint_t *
313 gbp_endpoint_find_ip4 (const ip4_address_t * ip, u32 fib_index)
314 {
315   clib_bihash_kv_24_8_t key, value;
316   int rv;
317
318   gbp_endpoint_mk_key_ip4 (ip, fib_index, &key);
319
320   rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
321
322   if (0 != rv)
323     return NULL;
324
325   return (gbp_endpoint_get (value.value));
326 }
327
328 static_always_inline void
329 gbp_endpoint_mk_key_ip6 (const ip6_address_t * ip,
330                          u32 fib_index, clib_bihash_kv_24_8_t * key)
331 {
332   key->key[0] = ip->as_u64[0];
333   key->key[1] = ip->as_u64[1];
334   key->key[2] = fib_index;
335 }
336
337 static_always_inline gbp_endpoint_t *
338 gbp_endpoint_find_ip6 (const ip6_address_t * ip, u32 fib_index)
339 {
340   clib_bihash_kv_24_8_t key, value;
341   int rv;
342
343   gbp_endpoint_mk_key_ip6 (ip, fib_index, &key);
344
345   rv = clib_bihash_search_24_8 (&gbp_ep_db.ged_by_ip_rd, &key, &value);
346
347   if (0 != rv)
348     return NULL;
349
350   return (gbp_endpoint_get (value.value));
351 }
352
353 static_always_inline gbp_endpoint_t *
354 gbp_endpoint_find_itf (u32 sw_if_index)
355 {
356   index_t gei;
357
358   gei = gbp_ep_db.ged_by_sw_if_index[sw_if_index];
359
360   if (INDEX_INVALID != gei)
361     return (gbp_endpoint_get (gei));
362
363   return (NULL);
364 }
365
366
367 #endif
368
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */