gbp: disable L2 BD learning per-interface
[vpp.git] / src / plugins / gbp / gbp_bridge_domain.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_bridge_domain.h>
17 #include <plugins/gbp/gbp_route_domain.h>
18 #include <plugins/gbp/gbp_endpoint.h>
19 #include <plugins/gbp/gbp_learn.h>
20
21 #include <vnet/dpo/dvr_dpo.h>
22 #include <vnet/fib/fib_table.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_bvi.h>
26 #include <vnet/l2/l2_fib.h>
27
28 /**
29  * Pool of GBP bridge_domains
30  */
31 gbp_bridge_domain_t *gbp_bridge_domain_pool;
32
33 /**
34  * DB of bridge_domains
35  */
36 gbp_bridge_domain_db_t gbp_bridge_domain_db;
37
38 /**
39  * Map of BD index to contract scope
40  */
41 gbp_scope_t *gbp_scope_by_bd_index;
42
43 /**
44  * logger
45  */
46 vlib_log_class_t gb_logger;
47
48 #define GBP_BD_DBG(...)                           \
49     vlib_log_debug (gb_logger, __VA_ARGS__);
50
51 index_t
52 gbp_bridge_domain_index (const gbp_bridge_domain_t * gbd)
53 {
54   return (gbd - gbp_bridge_domain_pool);
55 }
56
57 static void
58 gbp_bridge_domain_lock (index_t i)
59 {
60   gbp_bridge_domain_t *gb;
61
62   gb = gbp_bridge_domain_get (i);
63   gb->gb_locks++;
64 }
65
66 u32
67 gbp_bridge_domain_get_bd_id (index_t gbdi)
68 {
69   gbp_bridge_domain_t *gb;
70
71   gb = gbp_bridge_domain_get (gbdi);
72
73   return (gb->gb_bd_id);
74 }
75
76 static index_t
77 gbp_bridge_domain_find (u32 bd_id)
78 {
79   uword *p;
80
81   p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
82
83   if (NULL != p)
84     return p[0];
85
86   return (INDEX_INVALID);
87 }
88
89 index_t
90 gbp_bridge_domain_find_and_lock (u32 bd_id)
91 {
92   uword *p;
93
94   p = hash_get (gbp_bridge_domain_db.gbd_by_bd_id, bd_id);
95
96   if (NULL != p)
97     {
98       gbp_bridge_domain_lock (p[0]);
99       return p[0];
100     }
101   return (INDEX_INVALID);
102 }
103
104 static void
105 gbp_bridge_domain_db_add (gbp_bridge_domain_t * gb)
106 {
107   index_t gbi = gb - gbp_bridge_domain_pool;
108
109   hash_set (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id, gbi);
110   vec_validate_init_empty (gbp_bridge_domain_db.gbd_by_bd_index,
111                            gb->gb_bd_index, INDEX_INVALID);
112   gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = gbi;
113 }
114
115 static void
116 gbp_bridge_domain_db_remove (gbp_bridge_domain_t * gb)
117 {
118   hash_unset (gbp_bridge_domain_db.gbd_by_bd_id, gb->gb_bd_id);
119   gbp_bridge_domain_db.gbd_by_bd_index[gb->gb_bd_index] = INDEX_INVALID;
120 }
121
122 u8 *
123 format_gbp_bridge_domain_flags (u8 * s, va_list * args)
124 {
125   gbp_bridge_domain_flags_t gf = va_arg (*args, gbp_bridge_domain_flags_t);
126
127   if (gf)
128     {
129       if (gf & GBP_BD_FLAG_DO_NOT_LEARN)
130         s = format (s, "do-not-learn ");
131       if (gf & GBP_BD_FLAG_UU_FWD_DROP)
132         s = format (s, "uu-fwd-drop ");
133       if (gf & GBP_BD_FLAG_MCAST_DROP)
134         s = format (s, "mcast-drop ");
135       if (gf & GBP_BD_FLAG_UCAST_ARP)
136         s = format (s, "ucast-arp ");
137     }
138   else
139     {
140       s = format (s, "none");
141     }
142   return (s);
143 }
144
145 static u8 *
146 format_gbp_bridge_domain_ptr (u8 * s, va_list * args)
147 {
148   gbp_bridge_domain_t *gb = va_arg (*args, gbp_bridge_domain_t *);
149   vnet_main_t *vnm = vnet_get_main ();
150
151   if (NULL != gb)
152     s = format (s, "[%d] bd:[%d,%d], bvi:%U uu-flood:%U flags:%U locks:%d",
153                 gb - gbp_bridge_domain_pool,
154                 gb->gb_bd_id,
155                 gb->gb_bd_index,
156                 format_vnet_sw_if_index_name, vnm, gb->gb_bvi_sw_if_index,
157                 format_vnet_sw_if_index_name, vnm, gb->gb_uu_fwd_sw_if_index,
158                 format_gbp_bridge_domain_flags, gb->gb_flags, gb->gb_locks);
159   else
160     s = format (s, "NULL");
161
162   return (s);
163 }
164
165 u8 *
166 format_gbp_bridge_domain (u8 * s, va_list * args)
167 {
168   index_t gbi = va_arg (*args, index_t);
169
170   s =
171     format (s, "%U", format_gbp_bridge_domain_ptr,
172             gbp_bridge_domain_get (gbi));
173
174   return (s);
175 }
176
177 void
178 gbp_bridge_domain_itf_add (u32 sw_if_index, u32 bd_index,
179                            l2_bd_port_type_t type)
180 {
181   set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L2_BRIDGE,
182                    sw_if_index, bd_index, type, 0, 0);
183   /*
184    * adding an interface to the bridge enable learning on the
185    * interface. Disable learning on the interface by default for gbp
186    * interfaces
187    */
188   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_LEARN, 0);
189 }
190
191 void
192 gbp_bridge_domain_itf_del (u32 sw_if_index, u32 bd_index,
193                            l2_bd_port_type_t type)
194 {
195   set_int_l2_mode (vlib_get_main (), vnet_get_main (), MODE_L3, sw_if_index,
196                    bd_index, type, 0, 0);
197 }
198
199 int
200 gbp_bridge_domain_add_and_lock (u32 bd_id,
201                                 u32 rd_id,
202                                 gbp_bridge_domain_flags_t flags,
203                                 u32 bvi_sw_if_index,
204                                 u32 uu_fwd_sw_if_index,
205                                 u32 bm_flood_sw_if_index)
206 {
207   gbp_bridge_domain_t *gb;
208   index_t gbi;
209
210   gbi = gbp_bridge_domain_find (bd_id);
211
212   if (INDEX_INVALID == gbi)
213     {
214       gbp_route_domain_t *gr;
215       u32 bd_index;
216
217       bd_index = bd_find_index (&bd_main, bd_id);
218
219       if (~0 == bd_index)
220         return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
221
222       bd_flags_t bd_flags = L2_NONE;
223       if (flags & GBP_BD_FLAG_UU_FWD_DROP)
224         bd_flags |= L2_UU_FLOOD;
225       if (flags & GBP_BD_FLAG_MCAST_DROP)
226         bd_flags |= L2_FLOOD;
227
228       pool_get (gbp_bridge_domain_pool, gb);
229       memset (gb, 0, sizeof (*gb));
230
231       gb->gb_bd_id = bd_id;
232       gb->gb_bd_index = bd_index;
233       gb->gb_uu_fwd_sw_if_index = uu_fwd_sw_if_index;
234       gb->gb_bvi_sw_if_index = bvi_sw_if_index;
235       gb->gb_bm_flood_sw_if_index = bm_flood_sw_if_index;
236       gb->gb_locks = 1;
237       gb->gb_flags = flags;
238       gb->gb_rdi = gbp_route_domain_find_and_lock (rd_id);
239
240       /*
241        * set the scope from the BD's RD's scope
242        */
243       gr = gbp_route_domain_get (gb->gb_rdi);
244       vec_validate (gbp_scope_by_bd_index, gb->gb_bd_index);
245       gbp_scope_by_bd_index[gb->gb_bd_index] = gr->grd_scope;
246
247       /*
248        * Set the BVI and uu-flood interfaces into the BD
249        */
250       gbp_bridge_domain_itf_add (gb->gb_bvi_sw_if_index, bd_index,
251                                  L2_BD_PORT_TYPE_BVI);
252
253       if ((!(flags & GBP_BD_FLAG_UU_FWD_DROP)
254            || (flags & GBP_BD_FLAG_UCAST_ARP))
255           && ~0 != gb->gb_uu_fwd_sw_if_index)
256         gbp_bridge_domain_itf_add (gb->gb_uu_fwd_sw_if_index, bd_index,
257                                    L2_BD_PORT_TYPE_UU_FWD);
258       if (!(flags & GBP_BD_FLAG_MCAST_DROP)
259           && ~0 != gb->gb_bm_flood_sw_if_index)
260         {
261           gbp_bridge_domain_itf_add (gb->gb_bm_flood_sw_if_index, bd_index,
262                                      L2_BD_PORT_TYPE_NORMAL);
263           gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
264         }
265
266       /*
267        * unset any flag(s) set above
268        */
269       bd_set_flags (vlib_get_main (), bd_index, bd_flags, 0);
270
271       if (flags & GBP_BD_FLAG_UCAST_ARP)
272         {
273           bd_flags = L2_ARP_UFWD;
274           bd_set_flags (vlib_get_main (), bd_index, bd_flags, 1);
275         }
276
277       /*
278        * Add the BVI's MAC to the L2FIB
279        */
280       l2fib_add_entry (vnet_sw_interface_get_hw_address
281                        (vnet_get_main (), gb->gb_bvi_sw_if_index),
282                        gb->gb_bd_index, gb->gb_bvi_sw_if_index,
283                        (L2FIB_ENTRY_RESULT_FLAG_STATIC |
284                         L2FIB_ENTRY_RESULT_FLAG_BVI));
285
286       gbp_bridge_domain_db_add (gb);
287     }
288   else
289     {
290       gb = gbp_bridge_domain_get (gbi);
291       gb->gb_locks++;
292     }
293
294   GBP_BD_DBG ("add: %U", format_gbp_bridge_domain_ptr, gb);
295
296   return (0);
297 }
298
299 void
300 gbp_bridge_domain_unlock (index_t index)
301 {
302   gbp_bridge_domain_t *gb;
303
304   gb = gbp_bridge_domain_get (index);
305
306   gb->gb_locks--;
307
308   if (0 == gb->gb_locks)
309     {
310       GBP_BD_DBG ("destroy: %U", format_gbp_bridge_domain_ptr, gb);
311
312       l2fib_del_entry (vnet_sw_interface_get_hw_address
313                        (vnet_get_main (), gb->gb_bvi_sw_if_index),
314                        gb->gb_bd_index, gb->gb_bvi_sw_if_index);
315
316       gbp_bridge_domain_itf_del (gb->gb_bvi_sw_if_index, gb->gb_bd_index,
317                                  L2_BD_PORT_TYPE_BVI);
318       if (~0 != gb->gb_uu_fwd_sw_if_index)
319         gbp_bridge_domain_itf_del (gb->gb_uu_fwd_sw_if_index, gb->gb_bd_index,
320                                    L2_BD_PORT_TYPE_UU_FWD);
321       if (~0 != gb->gb_bm_flood_sw_if_index)
322         {
323           gbp_bridge_domain_itf_del (gb->gb_bm_flood_sw_if_index,
324                                      gb->gb_bd_index, L2_BD_PORT_TYPE_NORMAL);
325           gbp_learn_enable (gb->gb_bm_flood_sw_if_index, GBP_LEARN_MODE_L2);
326         }
327
328       gbp_bridge_domain_db_remove (gb);
329       gbp_route_domain_unlock (gb->gb_rdi);
330
331       pool_put (gbp_bridge_domain_pool, gb);
332     }
333 }
334
335 int
336 gbp_bridge_domain_delete (u32 bd_id)
337 {
338   index_t gbi;
339
340   GBP_BD_DBG ("del: %d", bd_id);
341   gbi = gbp_bridge_domain_find (bd_id);
342
343   if (INDEX_INVALID != gbi)
344     {
345       GBP_BD_DBG ("del: %U", format_gbp_bridge_domain, gbi);
346       gbp_bridge_domain_unlock (gbi);
347
348       return (0);
349     }
350
351   return (VNET_API_ERROR_NO_SUCH_ENTRY);
352 }
353
354 void
355 gbp_bridge_domain_walk (gbp_bridge_domain_cb_t cb, void *ctx)
356 {
357   gbp_bridge_domain_t *gbpe;
358
359   /* *INDENT-OFF* */
360   pool_foreach(gbpe, gbp_bridge_domain_pool,
361   {
362     if (!cb(gbpe, ctx))
363       break;
364   });
365   /* *INDENT-ON* */
366 }
367
368 static clib_error_t *
369 gbp_bridge_domain_cli (vlib_main_t * vm,
370                        unformat_input_t * input, vlib_cli_command_t * cmd)
371 {
372   vnet_main_t *vnm = vnet_get_main ();
373   gbp_bridge_domain_flags_t flags;
374   u32 bm_flood_sw_if_index = ~0;
375   u32 uu_fwd_sw_if_index = ~0;
376   u32 bd_id = ~0, rd_id = ~0;
377   u32 bvi_sw_if_index = ~0;
378   u8 add = 1;
379
380   flags = GBP_BD_FLAG_NONE;
381
382   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
383     {
384       if (unformat (input, "bvi %U", unformat_vnet_sw_interface,
385                     vnm, &bvi_sw_if_index))
386         ;
387       else if (unformat (input, "uu-fwd %U", unformat_vnet_sw_interface,
388                          vnm, &uu_fwd_sw_if_index))
389         ;
390       else if (unformat (input, "bm-flood %U", unformat_vnet_sw_interface,
391                          vnm, &bm_flood_sw_if_index))
392         ;
393       else if (unformat (input, "add"))
394         add = 1;
395       else if (unformat (input, "del"))
396         add = 0;
397       else if (unformat (input, "flags %d", &flags))
398         ;
399       else if (unformat (input, "bd %d", &bd_id))
400         ;
401       else if (unformat (input, "rd %d", &rd_id))
402         ;
403       else
404         break;
405     }
406
407   if (~0 == bd_id)
408     return clib_error_return (0, "BD-ID must be specified");
409   if (~0 == rd_id)
410     return clib_error_return (0, "RD-ID must be specified");
411
412   if (add)
413     {
414       if (~0 == bvi_sw_if_index)
415         return clib_error_return (0, "interface must be specified");
416
417       gbp_bridge_domain_add_and_lock (bd_id, rd_id,
418                                       flags,
419                                       bvi_sw_if_index,
420                                       uu_fwd_sw_if_index,
421                                       bm_flood_sw_if_index);
422     }
423   else
424     gbp_bridge_domain_delete (bd_id);
425
426   return (NULL);
427 }
428
429 /*?
430  * Configure a GBP bridge-domain
431  *
432  * @cliexpar
433  * @cliexstart{gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]}
434  * @cliexend
435  ?*/
436 /* *INDENT-OFF* */
437 VLIB_CLI_COMMAND (gbp_bridge_domain_cli_node, static) = {
438   .path = "gbp bridge-domain",
439   .short_help = "gbp bridge-domain [del] bd <ID> bvi <interface> [uu-fwd <interface>] [bm-flood <interface>] [flags <flags>]",
440   .function = gbp_bridge_domain_cli,
441 };
442
443 static int
444 gbp_bridge_domain_show_one (gbp_bridge_domain_t *gb, void *ctx)
445 {
446   vlib_main_t *vm;
447
448   vm = ctx;
449   vlib_cli_output (vm, "  %U", format_gbp_bridge_domain_ptr, gb);
450
451   return (1);
452 }
453
454 static clib_error_t *
455 gbp_bridge_domain_show (vlib_main_t * vm,
456                    unformat_input_t * input, vlib_cli_command_t * cmd)
457 {
458   vlib_cli_output (vm, "Bridge-Domains:");
459   gbp_bridge_domain_walk (gbp_bridge_domain_show_one, vm);
460
461   return (NULL);
462 }
463
464
465 /*?
466  * Show Group Based Policy Bridge_Domains and derived information
467  *
468  * @cliexpar
469  * @cliexstart{show gbp bridge_domain}
470  * @cliexend
471  ?*/
472 /* *INDENT-OFF* */
473 VLIB_CLI_COMMAND (gbp_bridge_domain_show_node, static) = {
474   .path = "show gbp bridge-domain",
475   .short_help = "show gbp bridge-domain\n",
476   .function = gbp_bridge_domain_show,
477 };
478 /* *INDENT-ON* */
479
480 static clib_error_t *
481 gbp_bridge_domain_init (vlib_main_t * vm)
482 {
483   gb_logger = vlib_log_register_class ("gbp", "bd");
484
485   return (NULL);
486 }
487
488 VLIB_INIT_FUNCTION (gbp_bridge_domain_init);
489
490 /*
491  * fd.io coding-style-patch-verification: ON
492  *
493  * Local Variables:
494  * eval: (c-set-style "gnu")
495  * End:
496  */