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