gbp: VRF scoped contracts
[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                               gbp_route_domain_get_scope (gs->gs_rd),
167                               gs->gs_stitched_external.gs_sclass,
168                               gs->gs_stitched_external.gs_sw_if_index, &gpd);
169
170   gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
171                                                    &gs->gs_key->gsk_pfx,
172                                                    FIB_SOURCE_PLUGIN_HI,
173                                                    (FIB_ENTRY_FLAG_EXCLUSIVE |
174                                                     FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
175                                                    &gpd);
176
177   dpo_reset (&gpd);
178
179   return (0);
180 }
181
182 static int
183 gbp_subnet_l3_out_add (gbp_subnet_t * gs, sclass_t sclass)
184 {
185   dpo_id_t gpd = DPO_INVALID;
186
187   gs->gs_l3_out.gs_sclass = sclass;
188
189   gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
190                               gbp_route_domain_get_scope (gs->gs_rd),
191                               gs->gs_l3_out.gs_sclass, ~0, &gpd);
192
193   gs->gs_fei = fib_table_entry_special_dpo_add (gs->gs_key->gsk_fib_index,
194                                                 &gs->gs_key->gsk_pfx,
195                                                 FIB_SOURCE_SPECIAL,
196                                                 FIB_ENTRY_FLAG_INTERPOSE,
197                                                 &gpd);
198
199   dpo_reset (&gpd);
200
201   return (0);
202 }
203
204 static void
205 gbp_subnet_del_i (index_t gsi)
206 {
207   gbp_subnet_t *gs;
208
209   gs = pool_elt_at_index (gbp_subnet_pool, gsi);
210
211   if (GBP_SUBNET_L3_OUT == gs->gs_type)
212     fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_SPECIAL);
213   else
214     fib_table_entry_delete_index (gs->gs_fei, FIB_SOURCE_PLUGIN_HI);
215
216   gbp_subnet_db_del (gs);
217   gbp_route_domain_unlock (gs->gs_rd);
218
219   pool_put (gbp_subnet_pool, gs);
220 }
221
222 int
223 gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
224 {
225   gbp_route_domain_t *grd;
226   index_t gsi, grdi;
227   u32 fib_index;
228
229   grdi = gbp_route_domain_find (rd_id);
230
231   if (~0 == grdi)
232     return (VNET_API_ERROR_NO_SUCH_FIB);
233
234   grd = gbp_route_domain_get (grdi);
235   fib_index = grd->grd_fib_index[pfx->fp_proto];
236
237   gsi = gbp_subnet_db_find (fib_index, pfx);
238
239   if (INDEX_INVALID == gsi)
240     return (VNET_API_ERROR_NO_SUCH_ENTRY);
241
242   gbp_subnet_del_i (gsi);
243
244   return (0);
245 }
246
247 int
248 gbp_subnet_add (u32 rd_id,
249                 const fib_prefix_t * pfx,
250                 gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
251 {
252   gbp_route_domain_t *grd;
253   index_t grdi, gsi;
254   gbp_subnet_t *gs;
255   u32 fib_index;
256   int rv;
257
258   grdi = gbp_route_domain_find_and_lock (rd_id);
259
260   if (~0 == grdi)
261     return (VNET_API_ERROR_NO_SUCH_FIB);
262
263   grd = gbp_route_domain_get (grdi);
264   fib_index = grd->grd_fib_index[pfx->fp_proto];
265
266   gsi = gbp_subnet_db_find (fib_index, pfx);
267
268   /*
269    * this is an update if the subnet already exists, so remove the old
270    */
271   if (INDEX_INVALID != gsi)
272     gbp_subnet_del_i (gsi);
273
274   rv = -2;
275
276   pool_get (gbp_subnet_pool, gs);
277
278   gs->gs_type = type;
279   gs->gs_rd = grdi;
280   gbp_subnet_db_add (fib_index, pfx, gs);
281
282   switch (type)
283     {
284     case GBP_SUBNET_STITCHED_INTERNAL:
285       rv = gbp_subnet_internal_add (gs);
286       break;
287     case GBP_SUBNET_STITCHED_EXTERNAL:
288       rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
289       break;
290     case GBP_SUBNET_TRANSPORT:
291       rv = gbp_subnet_transport_add (gs);
292       break;
293     case GBP_SUBNET_L3_OUT:
294       rv = gbp_subnet_l3_out_add (gs, sclass);
295       break;
296     }
297
298   return (rv);
299 }
300
301 static clib_error_t *
302 gbp_subnet_add_del_cli (vlib_main_t * vm,
303                         unformat_input_t * input, vlib_cli_command_t * cmd)
304 {
305   vnet_main_t *vnm = vnet_get_main ();
306   fib_prefix_t pfx = {.fp_addr = ip46_address_initializer };
307   int length;
308   u32 rd_id = ~0;
309   u32 sw_if_index = ~0;
310   gbp_subnet_type_t type = ~0;
311   u32 sclass = ~0;
312   int is_add = 1;
313   int rv;
314
315   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
316     {
317       if (unformat (input, "del"))
318         is_add = 0;
319       else if (unformat (input, "rd %d", &rd_id))
320         ;
321       else
322         if (unformat
323             (input, "prefix %U/%d", unformat_ip4_address, &pfx.fp_addr.ip4,
324              &length))
325         pfx.fp_proto = FIB_PROTOCOL_IP4;
326       else
327         if (unformat
328             (input, "prefix %U/%d", unformat_ip6_address, &pfx.fp_addr.ip6,
329              &length))
330         pfx.fp_proto = FIB_PROTOCOL_IP6;
331       else if (unformat (input, "type transport"))
332         type = GBP_SUBNET_TRANSPORT;
333       else if (unformat (input, "type internal"))
334         type = GBP_SUBNET_STITCHED_INTERNAL;
335       else if (unformat (input, "type external"))
336         type = GBP_SUBNET_STITCHED_EXTERNAL;
337       else if (unformat (input, "type l3out"))
338         type = GBP_SUBNET_L3_OUT;
339       else
340         if (unformat_user
341             (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
342         ;
343       else if (unformat (input, "sclass %u", &sclass))
344         ;
345       else
346         break;
347     }
348
349   pfx.fp_len = length;
350
351   if (is_add)
352     rv = gbp_subnet_add (rd_id, &pfx, type, sw_if_index, sclass);
353   else
354     rv = gbp_subnet_del (rd_id, &pfx);
355
356   switch (rv)
357     {
358     case 0:
359       return 0;
360     case VNET_API_ERROR_NO_SUCH_FIB:
361       return clib_error_return (0, "no such FIB");
362     }
363
364   return clib_error_return (0, "unknown error %d", rv);
365 }
366
367 /*?
368  * Add Group Based Policy Subnets
369  *
370  * @cliexpar
371  * @cliexstart{gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]}
372  * @cliexend
373  ?*/
374 /* *INDENT-OFF* */
375 VLIB_CLI_COMMAND (gbp_subnet_add_del, static) = {
376   .path = "gbp subnet",
377   .short_help = "gbp subnet [del] rd <ID> prefix <prefix> type <type> [<interface>] [sclass <sclass>]\n",
378   .function = gbp_subnet_add_del_cli,
379 };
380 /* *INDENT-ON* */
381
382
383
384 void
385 gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
386 {
387   gbp_route_domain_t *grd;
388   gbp_subnet_t *gs;
389   u32 sw_if_index;
390   sclass_t sclass;
391
392   sclass = SCLASS_INVALID;
393   sw_if_index = ~0;
394
395   /* *INDENT-OFF* */
396   pool_foreach (gs, gbp_subnet_pool,
397   ({
398     grd = gbp_route_domain_get(gs->gs_rd);
399
400     switch (gs->gs_type)
401       {
402       case GBP_SUBNET_STITCHED_INTERNAL:
403       case GBP_SUBNET_TRANSPORT:
404         /* use defaults above */
405         break;
406       case GBP_SUBNET_STITCHED_EXTERNAL:
407         sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
408         sclass = gs->gs_stitched_external.gs_sclass;
409         break;
410       case GBP_SUBNET_L3_OUT:
411         sclass = gs->gs_l3_out.gs_sclass;
412         break;
413       }
414
415     if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
416                          gs->gs_type, sw_if_index, sclass, ctx))
417       break;
418   }));
419   /* *INDENT-ON* */
420 }
421
422 typedef enum gsb_subnet_show_flags_t_
423 {
424   GBP_SUBNET_SHOW_BRIEF,
425   GBP_SUBNET_SHOW_DETAILS,
426 } gsb_subnet_show_flags_t;
427
428 static u8 *
429 format_gbp_subnet_type (u8 * s, va_list * args)
430 {
431   gbp_subnet_type_t type = va_arg (*args, gbp_subnet_type_t);
432
433   switch (type)
434     {
435     case GBP_SUBNET_STITCHED_INTERNAL:
436       return (format (s, "stitched-internal"));
437     case GBP_SUBNET_STITCHED_EXTERNAL:
438       return (format (s, "stitched-external"));
439     case GBP_SUBNET_TRANSPORT:
440       return (format (s, "transport"));
441     case GBP_SUBNET_L3_OUT:
442       return (format (s, "l3-out"));
443     }
444
445   return (format (s, "unknown"));
446 }
447
448 u8 *
449 format_gbp_subnet (u8 * s, va_list * args)
450 {
451   index_t gsi = va_arg (*args, index_t);
452   gsb_subnet_show_flags_t flags = va_arg (*args, gsb_subnet_show_flags_t);
453   gbp_subnet_t *gs;
454   u32 table_id;
455
456   gs = pool_elt_at_index (gbp_subnet_pool, gsi);
457
458   table_id = fib_table_get_table_id (gs->gs_key->gsk_fib_index,
459                                      gs->gs_key->gsk_pfx.fp_proto);
460
461   s = format (s, "[%d] tbl:%d %U %U", gsi, table_id,
462               format_fib_prefix, &gs->gs_key->gsk_pfx,
463               format_gbp_subnet_type, gs->gs_type);
464
465   switch (gs->gs_type)
466     {
467     case GBP_SUBNET_STITCHED_INTERNAL:
468     case GBP_SUBNET_TRANSPORT:
469       break;
470     case GBP_SUBNET_STITCHED_EXTERNAL:
471       s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
472                   format_vnet_sw_if_index_name,
473                   vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
474       break;
475     case GBP_SUBNET_L3_OUT:
476       s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
477       break;
478     }
479
480   switch (flags)
481     {
482     case GBP_SUBNET_SHOW_DETAILS:
483       {
484         s = format (s, "\n  %U", format_fib_entry, gs->gs_fei,
485                     FIB_ENTRY_FORMAT_DETAIL);
486       }
487     case GBP_SUBNET_SHOW_BRIEF:
488       break;
489     }
490   return (s);
491 }
492
493 static clib_error_t *
494 gbp_subnet_show (vlib_main_t * vm,
495                  unformat_input_t * input, vlib_cli_command_t * cmd)
496 {
497   u32 gsi;
498
499   gsi = INDEX_INVALID;
500
501   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
502     {
503       if (unformat (input, "%d", &gsi))
504         ;
505       else
506         break;
507     }
508
509   if (INDEX_INVALID != gsi)
510     {
511       vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
512                        GBP_SUBNET_SHOW_DETAILS);
513     }
514   else
515     {
516       /* *INDENT-OFF* */
517       pool_foreach_index(gsi, gbp_subnet_pool,
518       ({
519         vlib_cli_output (vm, "%U", format_gbp_subnet, gsi,
520                          GBP_SUBNET_SHOW_BRIEF);
521       }));
522       /* *INDENT-ON* */
523     }
524
525   return (NULL);
526 }
527
528 /*?
529  * Show Group Based Policy Subnets
530  *
531  * @cliexpar
532  * @cliexstart{show gbp subnet}
533  * @cliexend
534  ?*/
535 /* *INDENT-OFF* */
536 VLIB_CLI_COMMAND (gbp_subnet_show_node, static) = {
537   .path = "show gbp subnet",
538   .short_help = "show gbp subnet\n",
539   .function = gbp_subnet_show,
540 };
541 /* *INDENT-ON* */
542
543 static clib_error_t *
544 gbp_subnet_init (vlib_main_t * vm)
545 {
546   gbp_subnet_db = hash_create_mem (0,
547                                    sizeof (gbp_subnet_key_t), sizeof (u32));
548
549   return (NULL);
550 }
551
552 VLIB_INIT_FUNCTION (gbp_subnet_init);
553
554 /*
555  * fd.io coding-style-patch-verification: ON
556  *
557  * Local Variables:
558  * eval: (c-set-style "gnu")
559  * End:
560  */