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