GBP Endpoint Learning
[vpp.git] / src / plugins / gbp / gbp_subnet.c
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 #include <plugins/gbp/gbp.h>
17 #include <plugins/gbp/gbp_fwd_dpo.h>
18 #include <plugins/gbp/gbp_policy_dpo.h>
19 #include <plugins/gbp/gbp_route_domain.h>
20
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/dpo/load_balance.h>
23
24 /**
25  * a key for the DB
26  */
27 typedef struct gbp_subnet_key_t_
28 {
29   fib_prefix_t gsk_pfx;
30   u32 gsk_fib_index;
31 } gbp_subnet_key_t;
32
33 /**
34  * Subnet
35  */
36 typedef struct gbp_subnet_t_
37 {
38   gbp_subnet_key_t *gs_key;
39   gbp_subnet_type_t gs_type;
40   index_t gs_rd;
41
42   union
43   {
44     struct
45     {
46       epg_id_t gs_epg;
47       u32 gs_sw_if_index;
48     } gs_stitched_external;
49   };
50 } gbp_subnet_t;
51
52 /**
53  * A DB of the subnets; key={pfx,fib-index}
54  */
55 uword *gbp_subnet_db;
56
57 /**
58  * pool of subnets
59  */
60 gbp_subnet_t *gbp_subnet_pool;
61
62 static index_t
63 gbp_subnet_db_find (u32 fib_index, const fib_prefix_t * pfx)
64 {
65   gbp_subnet_key_t key = {
66     .gsk_pfx = *pfx,
67     .gsk_fib_index = fib_index,
68   };
69   uword *p;
70
71   p = hash_get_mem (gbp_subnet_db, &key);
72
73   if (NULL != p)
74     return p[0];
75
76   return (INDEX_INVALID);
77 }
78
79 static void
80 gbp_subnet_db_add (u32 fib_index, const fib_prefix_t * pfx, gbp_subnet_t * gs)
81 {
82   gbp_subnet_key_t *key;
83
84   key = clib_mem_alloc (sizeof (*key));
85
86   clib_memcpy (&(key->gsk_pfx), pfx, sizeof (*pfx));
87   key->gsk_fib_index = fib_index;
88
89   hash_set_mem (gbp_subnet_db, key, (gs - gbp_subnet_pool));
90
91   gs->gs_key = key;
92 }
93
94 static void
95 gbp_subnet_db_del (gbp_subnet_t * gs)
96 {
97   hash_unset_mem (gbp_subnet_db, gs->gs_key);
98
99   clib_mem_free (gs->gs_key);
100   gs->gs_key = NULL;
101 }
102
103
104 static int
105 gbp_subnet_transport_add (const gbp_subnet_t * gs)
106 {
107   dpo_id_t gfd = DPO_INVALID;
108   gbp_route_domain_t *grd;
109   fib_protocol_t fproto;
110
111   fproto = gs->gs_key->gsk_pfx.fp_proto;
112   grd = gbp_route_domain_get (gs->gs_rd);
113
114   fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
115                                    &gs->gs_key->gsk_pfx,
116                                    FIB_SOURCE_PLUGIN_HI,
117                                    FIB_ENTRY_FLAG_NONE,
118                                    fib_proto_to_dpo (fproto),
119                                    &ADJ_BCAST_ADDR,
120                                    grd->grd_uu_sw_if_index[fproto],
121                                    ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
122
123   dpo_reset (&gfd);
124
125   return (0);
126 }
127
128 static int
129 gbp_subnet_internal_add (const gbp_subnet_t * gs)
130 {
131   dpo_id_t gfd = DPO_INVALID;
132
133   gbp_fwd_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
134                            &gfd);
135
136   fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
137                                       &gs->gs_key->gsk_pfx,
138                                       FIB_SOURCE_PLUGIN_HI,
139                                       FIB_ENTRY_FLAG_EXCLUSIVE, &gfd);
140
141   dpo_reset (&gfd);
142
143   return (0);
144 }
145
146 static int
147 gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, epg_id_t epg)
148 {
149   dpo_id_t gpd = DPO_INVALID;
150
151   gs->gs_stitched_external.gs_epg = epg;
152   gs->gs_stitched_external.gs_sw_if_index = sw_if_index;
153
154   gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
155                               gs->gs_stitched_external.gs_epg,
156                               gs->gs_stitched_external.gs_sw_if_index, &gpd);
157
158   fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
159                                       &gs->gs_key->gsk_pfx,
160                                       FIB_SOURCE_PLUGIN_HI,
161                                       (FIB_ENTRY_FLAG_EXCLUSIVE |
162                                        FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
163                                       &gpd);
164
165   dpo_reset (&gpd);
166
167   return (0);
168 }
169
170 int
171 gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
172 {
173   gbp_route_domain_t *grd;
174   index_t gsi, grdi;
175   gbp_subnet_t *gs;
176   u32 fib_index;
177
178   grdi = gbp_route_domain_find (rd_id);
179
180   if (~0 == grdi)
181     return (VNET_API_ERROR_NO_SUCH_FIB);
182
183   grd = gbp_route_domain_get (grdi);
184   fib_index = grd->grd_fib_index[pfx->fp_proto];
185
186   gsi = gbp_subnet_db_find (fib_index, pfx);
187
188   if (INDEX_INVALID == gsi)
189     return (VNET_API_ERROR_NO_SUCH_ENTRY);
190
191   gs = pool_elt_at_index (gbp_subnet_pool, gsi);
192
193   fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
194
195   gbp_subnet_db_del (gs);
196   gbp_route_domain_unlock (gs->gs_rd);
197
198   pool_put (gbp_subnet_pool, gs);
199
200   return (0);
201 }
202
203 int
204 gbp_subnet_add (u32 rd_id,
205                 const fib_prefix_t * pfx,
206                 gbp_subnet_type_t type, u32 sw_if_index, epg_id_t epg)
207 {
208   gbp_route_domain_t *grd;
209   index_t grdi, gsi;
210   gbp_subnet_t *gs;
211   u32 fib_index;
212   int rv;
213
214   grdi = gbp_route_domain_find_and_lock (rd_id);
215
216   if (~0 == grdi)
217     return (VNET_API_ERROR_NO_SUCH_FIB);
218
219   grd = gbp_route_domain_get (grdi);
220   fib_index = grd->grd_fib_index[pfx->fp_proto];
221
222   gsi = gbp_subnet_db_find (fib_index, pfx);
223
224   if (INDEX_INVALID != gsi)
225     return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS);
226
227   rv = -2;
228
229   pool_get (gbp_subnet_pool, gs);
230
231   gs->gs_type = type;
232   gs->gs_rd = grdi;
233   gbp_subnet_db_add (fib_index, pfx, gs);
234
235   switch (type)
236     {
237     case GBP_SUBNET_STITCHED_INTERNAL:
238       rv = gbp_subnet_internal_add (gs);
239       break;
240     case GBP_SUBNET_STITCHED_EXTERNAL:
241       rv = gbp_subnet_external_add (gs, sw_if_index, epg);
242       break;
243     case GBP_SUBNET_TRANSPORT:
244       rv = gbp_subnet_transport_add (gs);
245       break;
246     }
247
248   return (rv);
249 }
250
251 void
252 gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
253 {
254   gbp_route_domain_t *grd;
255   gbp_subnet_t *gs;
256   u32 sw_if_index;
257   epg_id_t epg;
258
259   epg = EPG_INVALID;
260   sw_if_index = ~0;
261
262   /* *INDENT-OFF* */
263   pool_foreach (gs, gbp_subnet_pool,
264   ({
265     grd = gbp_route_domain_get(gs->gs_rd);
266
267     switch (gs->gs_type)
268       {
269       case GBP_SUBNET_STITCHED_INTERNAL:
270       case GBP_SUBNET_TRANSPORT:
271         /* use defaults above */
272         break;
273       case GBP_SUBNET_STITCHED_EXTERNAL:
274         sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
275         epg = gs->gs_stitched_external.gs_epg;
276         break;
277       }
278
279     if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
280                          gs->gs_type, epg, sw_if_index, ctx))
281       break;
282   }));
283   /* *INDENT-ON* */
284 }
285
286 typedef enum gsb_subnet_show_flags_t_
287 {
288   GBP_SUBNET_SHOW_BRIEF,
289   GBP_SUBNET_SHOW_DETAILS,
290 } gsb_subnet_show_flags_t;
291
292 static u8 *
293 format_gbp_subnet_type (u8 * s, va_list * args)
294 {
295   gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
296
297   switch (type)
298     {
299     case GBP_SUBNET_STITCHED_INTERNAL:
300       return (format (s, "stitched-internal"));
301     case GBP_SUBNET_STITCHED_EXTERNAL:
302       return (format (s, "stitched-external"));
303     case GBP_SUBNET_TRANSPORT:
304       return (format (s, "transport"));
305     }
306
307   return (format (s, "unknown"));
308 }
309
310 u8 *
311 format_gbp_subnet (u8 * s, va_list * args)
312 {
313   index_t gsi = va_arg (*args, index_t);
314   gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
315   gbp_subnet_t *gs;
316   u32 table_id;
317
318   gs = pool_elt_at_index (gbp_subnet_pool, gsi);
319
320   table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
321                                      gs->gs_key->gsk_pfx.fp_proto);
322
323   s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
324               format_fib_prefix, &gs->gs_key->gsk_pfx,
325               format_gbp_subnet_type, gs->gs_type);
326
327   switch (gs->gs_type)
328     {
329     case GBP_SUBNET_STITCHED_INTERNAL:
330     case GBP_SUBNET_TRANSPORT:
331       break;
332     case GBP_SUBNET_STITCHED_EXTERNAL:
333       s = format (s, " {epg:%d %U}", gs->gs_stitched_external.gs_epg,
334                   format_vnet_sw_if_index_name,
335                   vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
336       break;
337     }
338
339   switch (flags)
340     {
341     case GBP_SUBNET_SHOW_DETAILS:
342       {
343         fib_node_index_t fei;
344
345         fei = fib_table_lookup_exact_match (gs->gs_key->gsk_fib_index,
346                                             &gs->gs_key->gsk_pfx);
347
348         s =
349           format (s, "\n  %U", format_fib_entry, fei,
350                   FIB_ENTRY_FORMAT_DETAIL);
351       }
352     case GBP_SUBNET_SHOW_BRIEF:
353       break;
354     }
355   return (s);
356 }
357
358 static clib_error_t *
359 gbp_subnet_show (vlib_main_t * vm,
360                  unformat_input_t * input, vlib_cli_command_t * cmd)
361 {
362   u32 gsi;
363
364   gsi = INDEX_INVALID;
365
366   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
367     {
368       if (unformat (input, "%d", &gsi))
369         ;
370       else
371         break;
372     }
373
374   if (INDEX_INVALID != gsi)
375     {
376       vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
377                        GBP_SUBNET_SHOW_DETAILS);
378     }
379   else
380     {
381       /* *INDENT-OFF* */
382       pool_foreach_index(gsi, gbp_subnet_pool,
383       ({
384         vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
385                          GBP_SUBNET_SHOW_BRIEF);
386       }));
387       /* *INDENT-ON* */
388     }
389
390   return (NULL);
391 }
392
393 /*?
394  * Show Group Based Policy Subnets
395  *
396  * @cliexpar
397  * @cliexstart{show gbp subnet}
398  * @cliexend
399  ?*/
400 /* *INDENT-OFF* */
401 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
402   .path = "show gbp subnet",
403   .short_help = "show gbp subnet\n",
404   .function = gbp_subnet_show,
405 };
406 /* *INDENT-ON* */
407
408 static clib_error_t *
409 gbp_subnet_init (vlib_main_t * vm)
410 {
411   gbp_subnet_db = hash_create_mem (0,
412                                    sizeof (gbp_subnet_key_t), sizeof (u32));
413
414   return (NULL);
415 }
416
417 VLIB_INIT_FUNCTION (gbp_subnet_init);
418
419 /*
420  * fd.io coding-style-patch-verification: ON
421  *
422  * Local Variables:
423  * eval: (c-set-style "gnu")
424  * End:
425  */