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