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