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