misc: deprecate gbp and its dependents
[vpp.git] / extras / deprecated / plugins / gbp / gbp_vxlan.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_vxlan.h>
17 #include <plugins/gbp/gbp_learn.h>
18 #include <plugins/gbp/gbp_bridge_domain.h>
19 #include <plugins/gbp/gbp_route_domain.h>
20
21 #include <vnet/vxlan-gbp/vxlan_gbp.h>
22 #include <vlibmemory/api.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vlib/punt.h>
25
26 /**
27  * A reference to a VXLAN-GBP tunnel created as a child/dependent tunnel
28  * of the template GBP-VXLAN tunnel
29  */
30 typedef struct vxlan_tunnel_ref_t_
31 {
32   gbp_itf_hdl_t vxr_itf;
33   u32 vxr_sw_if_index;
34   index_t vxr_parent;
35   gbp_vxlan_tunnel_layer_t vxr_layer;
36 } vxlan_tunnel_ref_t;
37
38 /**
39  * DB of added tunnels
40  */
41 uword *gv_db;
42
43 /**
44  * Logger
45  */
46 static vlib_log_class_t gt_logger;
47
48 /**
49  * Pool of template tunnels
50  */
51 static gbp_vxlan_tunnel_t *gbp_vxlan_tunnel_pool;
52
53 /**
54  * Pool of child tunnels
55  */
56 static vxlan_tunnel_ref_t *vxlan_tunnel_ref_pool;
57
58 /**
59  * DB of template interfaces by SW interface index
60  */
61 static index_t *gbp_vxlan_tunnel_db;
62
63 /**
64  * DB of child interfaces by SW interface index
65  */
66 static index_t *vxlan_tunnel_ref_db;
67
68 /**
69  * handle registered with the ;unt infra
70  */
71 static vlib_punt_hdl_t punt_hdl;
72
73 static char *gbp_vxlan_tunnel_layer_strings[] = {
74 #define _(n,s) [GBP_VXLAN_TUN_##n] = s,
75   foreach_gbp_vxlan_tunnel_layer
76 #undef _
77 };
78
79 #define GBP_VXLAN_TUN_DBG(...)                          \
80     vlib_log_debug (gt_logger, __VA_ARGS__);
81
82
83 gbp_vxlan_tunnel_t *
84 gbp_vxlan_tunnel_get (index_t gti)
85 {
86   return (pool_elt_at_index (gbp_vxlan_tunnel_pool, gti));
87 }
88
89 static vxlan_tunnel_ref_t *
90 vxlan_tunnel_ref_get (index_t vxri)
91 {
92   return (pool_elt_at_index (vxlan_tunnel_ref_pool, vxri));
93 }
94
95 static u8 *
96 format_vxlan_tunnel_ref (u8 * s, va_list * args)
97 {
98   index_t vxri = va_arg (*args, u32);
99   vxlan_tunnel_ref_t *vxr;
100
101   vxr = vxlan_tunnel_ref_get (vxri);
102
103   s = format (s, "[%U]", format_gbp_itf_hdl, vxr->vxr_itf);
104
105   return (s);
106 }
107
108 static void
109 gdb_vxlan_dep_del (u32 sw_if_index)
110 {
111   vxlan_tunnel_ref_t *vxr;
112   gbp_vxlan_tunnel_t *gt;
113   index_t vxri;
114   u32 pos;
115
116   vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
117   vxri = vxr - vxlan_tunnel_ref_pool;
118   gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
119
120   GBP_VXLAN_TUN_DBG ("del-dep:%U", format_vxlan_tunnel_ref, vxri);
121
122   vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = INDEX_INVALID;
123   pos = vec_search (gt->gt_tuns, vxri);
124
125   ASSERT (~0 != pos);
126   vec_del1 (gt->gt_tuns, pos);
127
128   vnet_vxlan_gbp_tunnel_del (vxr->vxr_sw_if_index);
129
130   pool_put (vxlan_tunnel_ref_pool, vxr);
131 }
132
133 static gbp_itf_hdl_t
134 gdb_vxlan_dep_add (gbp_vxlan_tunnel_t * gt,
135                    const ip46_address_t * src, const ip46_address_t * dst)
136 {
137   vnet_vxlan_gbp_tunnel_add_del_args_t args = {
138     .is_add = 1,
139     .is_ip6 = !ip46_address_is_ip4 (src),
140     .vni = gt->gt_vni,
141     .src = *src,
142     .dst = *dst,
143     .instance = ~0,
144     .mode = (GBP_VXLAN_TUN_L2 == gt->gt_layer ?
145              VXLAN_GBP_TUNNEL_MODE_L2 : VXLAN_GBP_TUNNEL_MODE_L3),
146   };
147   vxlan_tunnel_ref_t *vxr;
148   u32 sw_if_index;
149   index_t vxri;
150   int rv;
151
152   sw_if_index = ~0;
153   rv = vnet_vxlan_gbp_tunnel_add_del (&args, &sw_if_index);
154
155   if (VNET_API_ERROR_TUNNEL_EXIST == rv)
156     {
157       vxri = vxlan_tunnel_ref_db[sw_if_index];
158
159       vxr = vxlan_tunnel_ref_get (vxri);
160       gbp_itf_lock (vxr->vxr_itf);
161     }
162   else if (0 == rv)
163     {
164       ASSERT (~0 != sw_if_index);
165       GBP_VXLAN_TUN_DBG ("add-dep:%U %U %U %d", format_vnet_sw_if_index_name,
166                          vnet_get_main (), sw_if_index,
167                          format_ip46_address, src, IP46_TYPE_ANY,
168                          format_ip46_address, dst, IP46_TYPE_ANY, gt->gt_vni);
169
170       pool_get_zero (vxlan_tunnel_ref_pool, vxr);
171
172       vxri = (vxr - vxlan_tunnel_ref_pool);
173       vxr->vxr_parent = gt - gbp_vxlan_tunnel_pool;
174       vxr->vxr_sw_if_index = sw_if_index;
175       vxr->vxr_layer = gt->gt_layer;
176
177       /*
178        * store the child both on the parent's list and the global DB
179        */
180       vec_add1 (gt->gt_tuns, vxri);
181
182       vec_validate_init_empty (vxlan_tunnel_ref_db,
183                                vxr->vxr_sw_if_index, INDEX_INVALID);
184       vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = vxri;
185
186       if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
187         {
188           l2output_feat_masks_t ofeat;
189           l2input_feat_masks_t ifeat;
190           gbp_bridge_domain_t *gbd;
191
192           gbd = gbp_bridge_domain_get (gt->gt_gbd);
193           vxr->vxr_itf = gbp_itf_l2_add_and_lock_w_free
194             (vxr->vxr_sw_if_index, gt->gt_gbd, gdb_vxlan_dep_del);
195
196           ofeat = L2OUTPUT_FEAT_GBP_POLICY_MAC;
197           ifeat = L2INPUT_FEAT_NONE;
198
199           if (!(gbd->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
200             ifeat |= L2INPUT_FEAT_GBP_LEARN;
201
202           gbp_itf_l2_set_output_feature (vxr->vxr_itf, ofeat);
203           gbp_itf_l2_set_input_feature (vxr->vxr_itf, ifeat);
204         }
205       else
206         {
207           vxr->vxr_itf = gbp_itf_l3_add_and_lock_w_free
208             (vxr->vxr_sw_if_index, gt->gt_grd, gdb_vxlan_dep_del);
209
210           gbp_itf_l3_set_input_feature (vxr->vxr_itf, GBP_ITF_L3_FEAT_LEARN);
211         }
212     }
213   else
214     {
215       return (GBP_ITF_HDL_INVALID);
216     }
217
218   return (vxr->vxr_itf);
219 }
220
221 u32
222 vxlan_gbp_tunnel_get_parent (u32 sw_if_index)
223 {
224   ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
225           (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
226
227   gbp_vxlan_tunnel_t *gt;
228   vxlan_tunnel_ref_t *vxr;
229
230   vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
231   gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
232
233   return (gt->gt_sw_if_index);
234 }
235
236 gbp_itf_hdl_t
237 vxlan_gbp_tunnel_lock_itf (u32 sw_if_index)
238 {
239   ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
240           (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
241
242   vxlan_tunnel_ref_t *vxr;
243
244   vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
245
246   gbp_itf_lock (vxr->vxr_itf);
247
248   return (vxr->vxr_itf);
249 }
250
251
252 gbp_vxlan_tunnel_type_t
253 gbp_vxlan_tunnel_get_type (u32 sw_if_index)
254 {
255   if (sw_if_index < vec_len (vxlan_tunnel_ref_db) &&
256       INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index])
257     {
258       return (VXLAN_GBP_TUNNEL);
259     }
260   else if (sw_if_index < vec_len (gbp_vxlan_tunnel_db) &&
261            INDEX_INVALID != gbp_vxlan_tunnel_db[sw_if_index])
262     {
263       return (GBP_VXLAN_TEMPLATE_TUNNEL);
264     }
265
266   ASSERT (0);
267   return (GBP_VXLAN_TEMPLATE_TUNNEL);
268 }
269
270 gbp_itf_hdl_t
271 gbp_vxlan_tunnel_clone_and_lock (u32 sw_if_index,
272                                  const ip46_address_t * src,
273                                  const ip46_address_t * dst)
274 {
275   gbp_vxlan_tunnel_t *gt;
276   index_t gti;
277
278   gti = gbp_vxlan_tunnel_db[sw_if_index];
279
280   if (INDEX_INVALID == gti)
281     return (GBP_ITF_HDL_INVALID);
282
283   gt = pool_elt_at_index (gbp_vxlan_tunnel_pool, gti);
284
285   return (gdb_vxlan_dep_add (gt, src, dst));
286 }
287
288 void
289 vxlan_gbp_tunnel_unlock (u32 sw_if_index)
290 {
291   /* vxlan_tunnel_ref_t *vxr; */
292   /* index_t vxri; */
293
294   /* vxri = vxlan_tunnel_ref_db[sw_if_index]; */
295
296   /* ASSERT (vxri != INDEX_INVALID); */
297
298   /* vxr = vxlan_tunnel_ref_get (vxri); */
299
300   /* gdb_vxlan_dep_del (vxri); */
301 }
302
303 void
304 gbp_vxlan_walk (gbp_vxlan_cb_t cb, void *ctx)
305 {
306   gbp_vxlan_tunnel_t *gt;
307
308   /* *INDENT-OFF* */
309   pool_foreach (gt, gbp_vxlan_tunnel_pool)
310      {
311       if (WALK_CONTINUE != cb(gt, ctx))
312         break;
313     }
314   /* *INDENT-ON* */
315 }
316
317 static walk_rc_t
318 gbp_vxlan_tunnel_show_one (gbp_vxlan_tunnel_t * gt, void *ctx)
319 {
320   vlib_cli_output (ctx, "%U", format_gbp_vxlan_tunnel,
321                    gt - gbp_vxlan_tunnel_pool);
322
323   return (WALK_CONTINUE);
324 }
325
326 static u8 *
327 format_gbp_vxlan_tunnel_name (u8 * s, va_list * args)
328 {
329   u32 dev_instance = va_arg (*args, u32);
330
331   return format (s, "gbp-vxlan-%d", dev_instance);
332 }
333
334 u8 *
335 format_gbp_vxlan_tunnel_layer (u8 * s, va_list * args)
336 {
337   gbp_vxlan_tunnel_layer_t gl = va_arg (*args, gbp_vxlan_tunnel_layer_t);
338   s = format (s, "%s", gbp_vxlan_tunnel_layer_strings[gl]);
339
340   return (s);
341 }
342
343 u8 *
344 format_gbp_vxlan_tunnel (u8 * s, va_list * args)
345 {
346   u32 dev_instance = va_arg (*args, u32);
347   CLIB_UNUSED (int verbose) = va_arg (*args, int);
348   gbp_vxlan_tunnel_t *gt = gbp_vxlan_tunnel_get (dev_instance);
349   index_t *vxri;
350
351   s = format (s, " [%d] gbp-vxlan-tunnel: hw:%d sw:%d vni:%d %U",
352               dev_instance, gt->gt_hw_if_index,
353               gt->gt_sw_if_index, gt->gt_vni,
354               format_gbp_vxlan_tunnel_layer, gt->gt_layer);
355   if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
356     s = format (s, " BD:%d gbd-index:%d", gt->gt_bd_rd_id, gt->gt_gbd);
357   else
358     s = format (s, " RD:%d grd-index:%d", gt->gt_bd_rd_id, gt->gt_grd);
359
360   s = format (s, "   dependents:");
361   vec_foreach (vxri, gt->gt_tuns)
362   {
363     s = format (s, "\n    %U, ", format_vxlan_tunnel_ref, *vxri);
364   }
365
366   return s;
367 }
368
369 typedef struct gbp_vxlan_tx_trace_t_
370 {
371   u32 vni;
372 } gbp_vxlan_tx_trace_t;
373
374 u8 *
375 format_gbp_vxlan_tx_trace (u8 * s, va_list * args)
376 {
377   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
378   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
379   gbp_vxlan_tx_trace_t *t = va_arg (*args, gbp_vxlan_tx_trace_t *);
380
381   s = format (s, "GBP-VXLAN: vni:%d", t->vni);
382
383   return (s);
384 }
385
386 clib_error_t *
387 gbp_vxlan_interface_admin_up_down (vnet_main_t * vnm,
388                                    u32 hw_if_index, u32 flags)
389 {
390   vnet_hw_interface_t *hi;
391   u32 ti;
392
393   hi = vnet_get_hw_interface (vnm, hw_if_index);
394
395   if (NULL == gbp_vxlan_tunnel_db ||
396       hi->sw_if_index >= vec_len (gbp_vxlan_tunnel_db))
397     return (NULL);
398
399   ti = gbp_vxlan_tunnel_db[hi->sw_if_index];
400
401   if (~0 == ti)
402     /* not one of ours */
403     return (NULL);
404
405   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
406     vnet_hw_interface_set_flags (vnm, hw_if_index,
407                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
408   else
409     vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
410
411   return (NULL);
412 }
413
414 static uword
415 gbp_vxlan_interface_tx (vlib_main_t * vm,
416                         vlib_node_runtime_t * node, vlib_frame_t * frame)
417 {
418   clib_warning ("you shouldn't be here, leaking buffers...");
419   return frame->n_vectors;
420 }
421
422 /* *INDENT-OFF* */
423 VNET_DEVICE_CLASS (gbp_vxlan_device_class) = {
424   .name = "GBP VXLAN tunnel-template",
425   .format_device_name = format_gbp_vxlan_tunnel_name,
426   .format_device = format_gbp_vxlan_tunnel,
427   .format_tx_trace = format_gbp_vxlan_tx_trace,
428   .admin_up_down_function = gbp_vxlan_interface_admin_up_down,
429   .tx_function = gbp_vxlan_interface_tx,
430 };
431
432 VNET_HW_INTERFACE_CLASS (gbp_vxlan_hw_interface_class) = {
433   .name = "GBP-VXLAN",
434   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
435 };
436 /* *INDENT-ON* */
437
438 int
439 gbp_vxlan_tunnel_add (u32 vni, gbp_vxlan_tunnel_layer_t layer,
440                       u32 bd_rd_id,
441                       const ip4_address_t * src, u32 * sw_if_indexp)
442 {
443   gbp_vxlan_tunnel_t *gt;
444   index_t gti;
445   uword *p;
446   int rv;
447
448   rv = 0;
449   p = hash_get (gv_db, vni);
450
451   GBP_VXLAN_TUN_DBG ("add: %d %d %d", vni, layer, bd_rd_id);
452
453   if (NULL == p)
454     {
455       vnet_sw_interface_t *si;
456       vnet_hw_interface_t *hi;
457       index_t gbi, grdi;
458       vnet_main_t *vnm;
459
460       gbi = grdi = INDEX_INVALID;
461
462       if (layer == GBP_VXLAN_TUN_L2)
463         {
464           gbi = gbp_bridge_domain_find_and_lock (bd_rd_id);
465
466           if (INDEX_INVALID == gbi)
467             {
468               return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
469             }
470         }
471       else
472         {
473           grdi = gbp_route_domain_find_and_lock (bd_rd_id);
474
475           if (INDEX_INVALID == grdi)
476             {
477               return (VNET_API_ERROR_NO_SUCH_FIB);
478             }
479         }
480
481       vnm = vnet_get_main ();
482       pool_get (gbp_vxlan_tunnel_pool, gt);
483       gti = gt - gbp_vxlan_tunnel_pool;
484
485       gt->gt_vni = vni;
486       gt->gt_layer = layer;
487       gt->gt_bd_rd_id = bd_rd_id;
488       gt->gt_src.ip4.as_u32 = src->as_u32;
489       gt->gt_hw_if_index = vnet_register_interface (vnm,
490                                                     gbp_vxlan_device_class.index,
491                                                     gti,
492                                                     gbp_vxlan_hw_interface_class.index,
493                                                     gti);
494
495       hi = vnet_get_hw_interface (vnm, gt->gt_hw_if_index);
496
497       gt->gt_sw_if_index = hi->sw_if_index;
498
499       /* don't flood packets in a BD to these interfaces */
500       si = vnet_get_sw_interface (vnm, gt->gt_sw_if_index);
501       si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
502
503       if (layer == GBP_VXLAN_TUN_L2)
504         {
505           gbp_bridge_domain_t *gb;
506
507           gb = gbp_bridge_domain_get (gbi);
508
509           gt->gt_gbd = gbi;
510           gb->gb_vni = gti;
511           /* set it up as a GBP interface */
512           gt->gt_itf = gbp_itf_l2_add_and_lock (gt->gt_sw_if_index,
513                                                 gt->gt_gbd);
514           gbp_itf_l2_set_input_feature (gt->gt_itf, L2INPUT_FEAT_GBP_LEARN);
515         }
516       else
517         {
518           gt->gt_grd = grdi;
519           gt->gt_itf = gbp_itf_l3_add_and_lock (gt->gt_sw_if_index,
520                                                 gt->gt_grd);
521           gbp_itf_l3_set_input_feature (gt->gt_itf, GBP_ITF_L3_FEAT_LEARN);
522         }
523
524       /*
525        * save the tunnel by VNI and by sw_if_index
526        */
527       hash_set (gv_db, vni, gti);
528
529       vec_validate_init_empty (gbp_vxlan_tunnel_db,
530                                gt->gt_sw_if_index, INDEX_INVALID);
531       gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = gti;
532
533       if (sw_if_indexp)
534         *sw_if_indexp = gt->gt_sw_if_index;
535
536       vxlan_gbp_register_udp_ports ();
537     }
538   else
539     {
540       gti = p[0];
541       rv = VNET_API_ERROR_IF_ALREADY_EXISTS;
542     }
543
544   GBP_VXLAN_TUN_DBG ("add: %U", format_gbp_vxlan_tunnel, gti);
545
546   return (rv);
547 }
548
549 int
550 gbp_vxlan_tunnel_del (u32 vni)
551 {
552   gbp_vxlan_tunnel_t *gt;
553   uword *p;
554
555   p = hash_get (gv_db, vni);
556
557   if (NULL != p)
558     {
559       vnet_main_t *vnm;
560
561       vnm = vnet_get_main ();
562       gt = gbp_vxlan_tunnel_get (p[0]);
563
564       vxlan_gbp_unregister_udp_ports ();
565
566       GBP_VXLAN_TUN_DBG ("del: %U", format_gbp_vxlan_tunnel,
567                          gt - gbp_vxlan_tunnel_pool);
568
569       gbp_endpoint_flush (GBP_ENDPOINT_SRC_DP, gt->gt_sw_if_index);
570       ASSERT (0 == vec_len (gt->gt_tuns));
571       vec_free (gt->gt_tuns);
572
573       gbp_itf_unlock (&gt->gt_itf);
574
575       if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
576         {
577           gbp_bridge_domain_unlock (gt->gt_gbd);
578         }
579       else
580         {
581           gbp_route_domain_unlock (gt->gt_grd);
582         }
583
584       vnet_sw_interface_set_flags (vnm, gt->gt_sw_if_index, 0);
585       vnet_delete_hw_interface (vnm, gt->gt_hw_if_index);
586
587       hash_unset (gv_db, vni);
588       gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = INDEX_INVALID;
589
590       pool_put (gbp_vxlan_tunnel_pool, gt);
591     }
592   else
593     return VNET_API_ERROR_NO_SUCH_ENTRY;
594
595   return (0);
596 }
597
598 static clib_error_t *
599 gbp_vxlan_show (vlib_main_t * vm,
600                 unformat_input_t * input, vlib_cli_command_t * cmd)
601 {
602
603   vlib_cli_output (vm, "GBP-VXLAN Interfaces:");
604
605   gbp_vxlan_walk (gbp_vxlan_tunnel_show_one, vm);
606
607   return (NULL);
608 }
609
610 /*?
611  * Show Group Based Policy VXLAN tunnels
612  *
613  * @cliexpar
614  * @cliexstart{show gbp vxlan}
615  * @cliexend
616  ?*/
617 /* *INDENT-OFF* */
618 VLIB_CLI_COMMAND (gbp_vxlan_show_node, static) = {
619   .path = "show gbp vxlan",
620   .short_help = "show gbp vxlan\n",
621   .function = gbp_vxlan_show,
622 };
623 /* *INDENT-ON* */
624
625 static clib_error_t *
626 gbp_vxlan_init (vlib_main_t * vm)
627 {
628   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
629
630   gt_logger = vlib_log_register_class ("gbp", "tun");
631
632   punt_hdl = vlib_punt_client_register ("gbp-vxlan");
633
634   vlib_punt_register (punt_hdl,
635                       vxm->punt_no_such_tunnel[FIB_PROTOCOL_IP4],
636                       "gbp-vxlan4");
637
638   return (0);
639 }
640
641 /* *INDENT-OFF* */
642 VLIB_INIT_FUNCTION (gbp_vxlan_init) =
643 {
644   .runs_after = VLIB_INITS("punt_init", "vxlan_gbp_init"),
645 };
646 /* *INDENT-ON* */
647
648 /*
649  * fd.io coding-style-patch-verification: ON
650  *
651  * Local Variables:
652  * eval: (c-set-style "gnu")
653  * End:
654  */