GBP: Sclass to src-epg conversions
[vpp.git] / src / 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_itf.h>
18 #include <plugins/gbp/gbp_learn.h>
19 #include <plugins/gbp/gbp_bridge_domain.h>
20 #include <plugins/gbp/gbp_route_domain.h>
21 #include <plugins/gbp/gbp_sclass.h>
22
23 #include <vnet/vxlan-gbp/vxlan_gbp.h>
24 #include <vlibmemory/api.h>
25 #include <vnet/fib/fib_table.h>
26
27 /**
28  * A reference to a VXLAN-GBP tunnel created as a child/dependent tunnel
29  * of the tempplate GBP-VXLAN tunnel
30  */
31 typedef struct vxlan_tunnel_ref_t_
32 {
33   u32 vxr_sw_if_index;
34   index_t vxr_itf;
35   u32 vxr_locks;
36   index_t vxr_parent;
37   gbp_vxlan_tunnel_layer_t vxr_layer;
38 } vxlan_tunnel_ref_t;
39
40 /**
41  * DB of added tunnels
42  */
43 uword *gv_db;
44
45 /**
46  * Logger
47  */
48 vlib_log_class_t gt_logger;
49
50 /**
51  * Pool of template tunnels
52  */
53 gbp_vxlan_tunnel_t *gbp_vxlan_tunnel_pool;
54
55 /**
56  * Pool of child tunnels
57  */
58 vxlan_tunnel_ref_t *vxlan_tunnel_ref_pool;
59
60 /**
61  * DB of template interfaces by SW interface index
62  */
63 index_t *gbp_vxlan_tunnel_db;
64
65 /**
66  * DB of child interfaces by SW interface index
67  */
68 index_t *vxlan_tunnel_ref_db;
69
70
71 static char *gbp_vxlan_tunnel_layer_strings[] = {
72 #define _(n,s) [GBP_VXLAN_TUN_##n] = s,
73   forecah_gbp_vxlan_tunnel_layer
74 #undef _
75 };
76
77 #define GBP_VXLAN_TUN_DBG(...)                          \
78     vlib_log_debug (gt_logger, __VA_ARGS__);
79
80
81
82 always_inline gbp_vxlan_tunnel_t *
83 gbp_vxlan_tunnel_get (index_t gti)
84 {
85   return (pool_elt_at_index (gbp_vxlan_tunnel_pool, gti));
86 }
87
88 static vxlan_tunnel_ref_t *
89 vxlan_tunnel_ref_get (index_t vxri)
90 {
91   return (pool_elt_at_index (vxlan_tunnel_ref_pool, vxri));
92 }
93
94 static u8 *
95 format_vxlan_tunnel_ref (u8 * s, va_list * args)
96 {
97   index_t vxri = va_arg (*args, u32);
98   vxlan_tunnel_ref_t *vxr;
99
100   vxr = vxlan_tunnel_ref_get (vxri);
101
102   s = format (s, "[%U locks:%d]", format_vnet_sw_if_index_name,
103               vnet_get_main (), vxr->vxr_sw_if_index, vxr->vxr_locks);
104
105   return (s);
106 }
107
108 static u32
109 gdb_vxlan_dep_add (gbp_vxlan_tunnel_t * gt,
110                    const ip46_address_t * src, const ip46_address_t * dst)
111 {
112   vnet_vxlan_gbp_tunnel_add_del_args_t args = {
113     .is_add = 1,
114     .is_ip6 = !ip46_address_is_ip4 (src),
115     .vni = gt->gt_vni,
116     .src = *src,
117     .dst = *dst,
118     .instance = ~0,
119     .mode = (GBP_VXLAN_TUN_L2 == gt->gt_layer ?
120              VXLAN_GBP_TUNNEL_MODE_L2 : VXLAN_GBP_TUNNEL_MODE_L3),
121   };
122   vxlan_tunnel_ref_t *vxr;
123   u32 sw_if_index;
124   index_t vxri;
125   int rv;
126
127   sw_if_index = ~0;
128   rv = vnet_vxlan_gbp_tunnel_add_del (&args, &sw_if_index);
129
130   if (VNET_API_ERROR_TUNNEL_EXIST == rv)
131     {
132       vxri = vxlan_tunnel_ref_db[sw_if_index];
133
134       vxr = vxlan_tunnel_ref_get (vxri);
135       vxr->vxr_locks++;
136     }
137   else if (0 == rv)
138     {
139       ASSERT (~0 != sw_if_index);
140       GBP_VXLAN_TUN_DBG ("add-dep:%U %U %U %d", format_vnet_sw_if_index_name,
141                          vnet_get_main (), sw_if_index,
142                          format_ip46_address, src, IP46_TYPE_ANY,
143                          format_ip46_address, dst, IP46_TYPE_ANY, gt->gt_vni);
144
145       pool_get_zero (vxlan_tunnel_ref_pool, vxr);
146
147       vxri = (vxr - vxlan_tunnel_ref_pool);
148       vxr->vxr_parent = gt - gbp_vxlan_tunnel_pool;
149       vxr->vxr_sw_if_index = sw_if_index;
150       vxr->vxr_locks = 1;
151       vxr->vxr_layer = gt->gt_layer;
152
153       /*
154        * store the child both on the parent's list and the global DB
155        */
156       vec_add1 (gt->gt_tuns, vxri);
157
158       vec_validate_init_empty (vxlan_tunnel_ref_db,
159                                vxr->vxr_sw_if_index, INDEX_INVALID);
160       vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = vxri;
161
162       if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
163         {
164           l2output_feat_masks_t ofeat;
165           l2input_feat_masks_t ifeat;
166           gbp_bridge_domain_t *gbd;
167
168           gbd = gbp_bridge_domain_get (gt->gt_gbd);
169           vxr->vxr_itf = gbp_itf_add_and_lock (vxr->vxr_sw_if_index,
170                                                gt->gt_bd_index);
171
172           ofeat = (L2OUTPUT_FEAT_GBP_POLICY_MAC |
173                    L2OUTPUT_FEAT_GBP_ID_2_SCLASS);
174           ifeat = L2INPUT_FEAT_GBP_SCLASS_2_ID;
175
176           if (!(gbd->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
177             ifeat |= L2INPUT_FEAT_GBP_LEARN;
178
179           gbp_itf_set_l2_output_feature (vxr->vxr_itf,
180                                          vxr->vxr_sw_if_index, ofeat);
181           gbp_itf_set_l2_input_feature (vxr->vxr_itf,
182                                         vxr->vxr_sw_if_index, ifeat);
183         }
184       else
185         {
186           const gbp_route_domain_t *grd;
187           fib_protocol_t fproto;
188
189           grd = gbp_route_domain_get (gt->gt_grd);
190
191           FOR_EACH_FIB_IP_PROTOCOL (fproto)
192             ip_table_bind (fproto, vxr->vxr_sw_if_index,
193                            grd->grd_table_id[fproto], 1);
194
195           gbp_learn_enable (vxr->vxr_sw_if_index, GBP_LEARN_MODE_L3);
196           gbp_sclass_enable_ip (vxr->vxr_sw_if_index);
197         }
198     }
199
200   return (sw_if_index);
201 }
202
203 u32
204 vxlan_gbp_tunnel_get_parent (u32 sw_if_index)
205 {
206   ASSERT ((sw_if_index < vec_len (vxlan_tunnel_ref_db)) &&
207           (INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index]));
208
209   gbp_vxlan_tunnel_t *gt;
210   vxlan_tunnel_ref_t *vxr;
211
212   vxr = vxlan_tunnel_ref_get (vxlan_tunnel_ref_db[sw_if_index]);
213   gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
214
215   return (gt->gt_sw_if_index);
216 }
217
218 gbp_vxlan_tunnel_type_t
219 gbp_vxlan_tunnel_get_type (u32 sw_if_index)
220 {
221   if (sw_if_index < vec_len (vxlan_tunnel_ref_db) &&
222       INDEX_INVALID != vxlan_tunnel_ref_db[sw_if_index])
223     {
224       return (VXLAN_GBP_TUNNEL);
225     }
226   else if (sw_if_index < vec_len (gbp_vxlan_tunnel_db) &&
227            INDEX_INVALID != gbp_vxlan_tunnel_db[sw_if_index])
228     {
229       return (GBP_VXLAN_TEMPLATE_TUNNEL);
230     }
231
232   ASSERT (0);
233   return (GBP_VXLAN_TEMPLATE_TUNNEL);
234 }
235
236 u32
237 gbp_vxlan_tunnel_clone_and_lock (u32 sw_if_index,
238                                  const ip46_address_t * src,
239                                  const ip46_address_t * dst)
240 {
241   gbp_vxlan_tunnel_t *gt;
242   index_t gti;
243
244   gti = gbp_vxlan_tunnel_db[sw_if_index];
245
246   if (INDEX_INVALID == gti)
247     return (~0);
248
249   gt = pool_elt_at_index (gbp_vxlan_tunnel_pool, gti);
250
251   return (gdb_vxlan_dep_add (gt, src, dst));
252 }
253
254 static void
255 gdb_vxlan_dep_del (index_t vxri)
256 {
257   vxlan_tunnel_ref_t *vxr;
258   gbp_vxlan_tunnel_t *gt;
259   u32 pos;
260
261   vxr = vxlan_tunnel_ref_get (vxri);
262   gt = gbp_vxlan_tunnel_get (vxr->vxr_parent);
263
264   GBP_VXLAN_TUN_DBG ("del-dep:%U", format_vxlan_tunnel_ref, vxri);
265
266   vxlan_tunnel_ref_db[vxr->vxr_sw_if_index] = INDEX_INVALID;
267   pos = vec_search (gt->gt_tuns, vxri);
268
269   ASSERT (~0 != pos);
270   vec_del1 (gt->gt_tuns, pos);
271
272   if (GBP_VXLAN_TUN_L2 == vxr->vxr_layer)
273     {
274       gbp_itf_set_l2_output_feature (vxr->vxr_itf, vxr->vxr_sw_if_index,
275                                      L2OUTPUT_FEAT_NONE);
276       gbp_itf_set_l2_input_feature (vxr->vxr_itf, vxr->vxr_sw_if_index,
277                                     L2INPUT_FEAT_NONE);
278       gbp_itf_unlock (vxr->vxr_itf);
279     }
280   else
281     {
282       fib_protocol_t fproto;
283
284       FOR_EACH_FIB_IP_PROTOCOL (fproto)
285         ip_table_bind (fproto, vxr->vxr_sw_if_index, 0, 0);
286       gbp_sclass_disable_ip (vxr->vxr_sw_if_index);
287       gbp_learn_disable (vxr->vxr_sw_if_index, GBP_LEARN_MODE_L3);
288     }
289
290   vnet_vxlan_gbp_tunnel_del (vxr->vxr_sw_if_index);
291
292   pool_put (vxlan_tunnel_ref_pool, vxr);
293 }
294
295 void
296 vxlan_gbp_tunnel_unlock (u32 sw_if_index)
297 {
298   vxlan_tunnel_ref_t *vxr;
299   index_t vxri;
300
301   vxri = vxlan_tunnel_ref_db[sw_if_index];
302
303   ASSERT (vxri != INDEX_INVALID);
304
305   vxr = vxlan_tunnel_ref_get (vxri);
306   vxr->vxr_locks--;
307
308   if (0 == vxr->vxr_locks)
309     {
310       gdb_vxlan_dep_del (vxri);
311     }
312 }
313
314 void
315 vxlan_gbp_tunnel_lock (u32 sw_if_index)
316 {
317   vxlan_tunnel_ref_t *vxr;
318   index_t vxri;
319
320   vxri = vxlan_tunnel_ref_db[sw_if_index];
321
322   ASSERT (vxri != INDEX_INVALID);
323
324   vxr = vxlan_tunnel_ref_get (vxri);
325   vxr->vxr_locks++;
326 }
327
328 #define foreach_gbp_vxlan_input_next         \
329   _(DROP, "error-drop")                      \
330   _(L2_INPUT, "l2-input")                    \
331   _(IP4_INPUT, "ip4-input")                  \
332   _(IP6_INPUT, "ip6-input")
333
334 typedef enum
335 {
336 #define _(s,n) GBP_VXLAN_INPUT_NEXT_##s,
337   foreach_gbp_vxlan_input_next
338 #undef _
339     GBP_VXLAN_INPUT_N_NEXT,
340 } gbp_vxlan_input_next_t;
341
342 #define foreach_gbp_vxlan_error              \
343   _(DECAPPED, "decapped")                    \
344   _(LEARNED, "learned")
345
346 typedef enum
347 {
348 #define _(s,n) GBP_VXLAN_ERROR_##s,
349   foreach_gbp_vxlan_error
350 #undef _
351     GBP_VXLAN_N_ERROR,
352 } gbp_vxlan_input_error_t;
353
354 static char *gbp_vxlan_error_strings[] = {
355 #define _(n,s) s,
356   foreach_gbp_vxlan_error
357 #undef _
358 };
359
360 typedef struct gbp_vxlan_trace_t_
361 {
362   u8 dropped;
363   u32 vni;
364   u32 sw_if_index;
365   u16 sclass;
366   u8 flags;
367 } gbp_vxlan_trace_t;
368
369
370 static uword
371 gbp_vxlan_decap (vlib_main_t * vm,
372                  vlib_node_runtime_t * node,
373                  vlib_frame_t * from_frame, u8 is_ip4)
374 {
375   u32 n_left_to_next, n_left_from, next_index, *to_next, *from;
376
377   next_index = 0;
378   from = vlib_frame_vector_args (from_frame);
379   n_left_from = from_frame->n_vectors;
380
381   while (n_left_from > 0)
382     {
383
384       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
385
386       while (n_left_from > 0 && n_left_to_next > 0)
387         {
388           vxlan_gbp_header_t *vxlan_gbp0;
389           gbp_vxlan_input_next_t next0;
390           gbp_vxlan_tunnel_t *gt0;
391           vlib_buffer_t *b0;
392           u32 bi0, vni0;
393           uword *p;
394
395           bi0 = to_next[0] = from[0];
396           from += 1;
397           to_next += 1;
398           n_left_from -= 1;
399           n_left_to_next -= 1;
400           next0 = GBP_VXLAN_INPUT_NEXT_DROP;
401
402           b0 = vlib_get_buffer (vm, bi0);
403           vxlan_gbp0 =
404             vlib_buffer_get_current (b0) - sizeof (vxlan_gbp_header_t);
405
406           vni0 = vxlan_gbp_get_vni (vxlan_gbp0);
407           p = hash_get (gv_db, vni0);
408
409           if (PREDICT_FALSE (NULL == p))
410             {
411               gt0 = NULL;
412               next0 = GBP_VXLAN_INPUT_NEXT_DROP;
413             }
414           else
415             {
416               gt0 = gbp_vxlan_tunnel_get (p[0]);
417
418               vnet_buffer (b0)->sw_if_index[VLIB_RX] = gt0->gt_sw_if_index;
419
420               if (GBP_VXLAN_TUN_L2 == gt0->gt_layer)
421                 /*
422                  * An L2 layer tunnel goes into the BD
423                  */
424                 next0 = GBP_VXLAN_INPUT_NEXT_L2_INPUT;
425               else
426                 {
427                   /*
428                    * An L3 layer tunnel needs to strip the L2 header
429                    * an inject into the RD
430                    */
431                   ethernet_header_t *e0;
432                   u16 type0;
433
434                   e0 = vlib_buffer_get_current (b0);
435                   type0 = clib_net_to_host_u16 (e0->type);
436                   switch (type0)
437                     {
438                     case ETHERNET_TYPE_IP4:
439                       next0 = GBP_VXLAN_INPUT_NEXT_IP4_INPUT;
440                       break;
441                     case ETHERNET_TYPE_IP6:
442                       next0 = GBP_VXLAN_INPUT_NEXT_IP6_INPUT;
443                       break;
444                     default:
445                       goto trace;
446                     }
447                   vlib_buffer_advance (b0, sizeof (*e0));
448                 }
449             }
450
451         trace:
452           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
453             {
454               gbp_vxlan_trace_t *tr;
455
456               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
457               tr->dropped = (next0 == GBP_VXLAN_INPUT_NEXT_DROP);
458               tr->vni = vni0;
459               tr->sw_if_index = (gt0 ? gt0->gt_sw_if_index : ~0);
460               tr->flags = vxlan_gbp_get_gpflags (vxlan_gbp0);
461               tr->sclass = vxlan_gbp_get_sclass (vxlan_gbp0);
462             }
463
464           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
465                                            to_next, n_left_to_next,
466                                            bi0, next0);
467         }
468
469       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
470     }
471
472   return from_frame->n_vectors;
473 }
474
475 static u8 *
476 format_gbp_vxlan_rx_trace (u8 * s, va_list * args)
477 {
478   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
479   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
480   gbp_vxlan_trace_t *t = va_arg (*args, gbp_vxlan_trace_t *);
481
482   s = format (s, "vni:%d dropped:%d rx:%d sclass:%d flags:%U",
483               t->vni, t->dropped, t->sw_if_index,
484               t->sclass, format_vxlan_gbp_header_gpflags, t->flags);
485
486   return (s);
487 }
488
489 static uword
490 gbp_vxlan4_decap (vlib_main_t * vm,
491                   vlib_node_runtime_t * node, vlib_frame_t * from_frame)
492 {
493   return gbp_vxlan_decap (vm, node, from_frame, 1);
494 }
495
496 /* *INDENT-OFF* */
497 VLIB_REGISTER_NODE (gbp_vxlan4_input_node) =
498 {
499   .function = gbp_vxlan4_decap,
500   .name = "gbp-vxlan4",
501   .vector_size = sizeof (u32),
502   .n_errors = GBP_VXLAN_N_ERROR,
503   .error_strings = gbp_vxlan_error_strings,
504   .n_next_nodes = GBP_VXLAN_INPUT_N_NEXT,
505   .format_trace = format_gbp_vxlan_rx_trace,
506   .next_nodes = {
507 #define _(s,n) [GBP_VXLAN_INPUT_NEXT_##s] = n,
508     foreach_gbp_vxlan_input_next
509 #undef _
510   },
511 };
512 VLIB_NODE_FUNCTION_MULTIARCH (gbp_vxlan4_input_node, gbp_vxlan4_decap)
513
514 /* *INDENT-ON* */
515
516 void
517 gbp_vxlan_walk (gbp_vxlan_cb_t cb, void *ctx)
518 {
519   gbp_vxlan_tunnel_t *gt;
520
521   /* *INDENT-OFF* */
522   pool_foreach (gt, gbp_vxlan_tunnel_pool,
523     ({
524       if (WALK_CONTINUE != cb(gt, ctx))
525         break;
526     }));
527   /* *INDENT-ON* */
528 }
529
530 static walk_rc_t
531 gbp_vxlan_tunnel_show_one (gbp_vxlan_tunnel_t * gt, void *ctx)
532 {
533   vlib_cli_output (ctx, "%U", format_gbp_vxlan_tunnel,
534                    gt - gbp_vxlan_tunnel_pool);
535
536   return (WALK_CONTINUE);
537 }
538
539 static u8 *
540 format_gbp_vxlan_tunnel_name (u8 * s, va_list * args)
541 {
542   u32 dev_instance = va_arg (*args, u32);
543
544   return format (s, "gbp-vxlan-%d", dev_instance);
545 }
546
547 u8 *
548 format_gbp_vxlan_tunnel_layer (u8 * s, va_list * args)
549 {
550   gbp_vxlan_tunnel_layer_t gl = va_arg (*args, gbp_vxlan_tunnel_layer_t);
551   s = format (s, "%s", gbp_vxlan_tunnel_layer_strings[gl]);
552
553   return (s);
554 }
555
556 u8 *
557 format_gbp_vxlan_tunnel (u8 * s, va_list * args)
558 {
559   u32 dev_instance = va_arg (*args, u32);
560   CLIB_UNUSED (int verbose) = va_arg (*args, int);
561   gbp_vxlan_tunnel_t *gt = gbp_vxlan_tunnel_get (dev_instance);
562   index_t *vxri;
563
564   s = format (s, "GBP VXLAN tunnel: hw:%d sw:%d vni:%d %U",
565               gt->gt_hw_if_index, gt->gt_sw_if_index, gt->gt_vni,
566               format_gbp_vxlan_tunnel_layer, gt->gt_layer);
567   if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
568     s = format (s, " BD:%d bd-index:%d", gt->gt_bd_rd_id, gt->gt_bd_index);
569   else
570     s = format (s, " RD:%d fib-index:[%d,%d]",
571                 gt->gt_bd_rd_id,
572                 gt->gt_fib_index[FIB_PROTOCOL_IP4],
573                 gt->gt_fib_index[FIB_PROTOCOL_IP6]);
574
575   s = format (s, " children:[");
576   vec_foreach (vxri, gt->gt_tuns)
577   {
578     s = format (s, "%U, ", format_vxlan_tunnel_ref, *vxri);
579   }
580   s = format (s, "]");
581
582   return s;
583 }
584
585 typedef struct gbp_vxlan_tx_trace_t_
586 {
587   u32 vni;
588 } gbp_vxlan_tx_trace_t;
589
590 u8 *
591 format_gbp_vxlan_tx_trace (u8 * s, va_list * args)
592 {
593   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
594   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
595   gbp_vxlan_tx_trace_t *t = va_arg (*args, gbp_vxlan_tx_trace_t *);
596
597   s = format (s, "GBP-VXLAN: vni:%d", t->vni);
598
599   return (s);
600 }
601
602 clib_error_t *
603 gbp_vxlan_interface_admin_up_down (vnet_main_t * vnm,
604                                    u32 hw_if_index, u32 flags)
605 {
606   vnet_hw_interface_t *hi;
607   u32 ti;
608
609   hi = vnet_get_hw_interface (vnm, hw_if_index);
610
611   if (NULL == gbp_vxlan_tunnel_db ||
612       hi->sw_if_index >= vec_len (gbp_vxlan_tunnel_db))
613     return (NULL);
614
615   ti = gbp_vxlan_tunnel_db[hi->sw_if_index];
616
617   if (~0 == ti)
618     /* not one of ours */
619     return (NULL);
620
621   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
622     vnet_hw_interface_set_flags (vnm, hw_if_index,
623                                  VNET_HW_INTERFACE_FLAG_LINK_UP);
624   else
625     vnet_hw_interface_set_flags (vnm, hw_if_index, 0);
626
627   return (NULL);
628 }
629
630 static uword
631 gbp_vxlan_interface_tx (vlib_main_t * vm,
632                         vlib_node_runtime_t * node, vlib_frame_t * frame)
633 {
634   clib_warning ("you shouldn't be here, leaking buffers...");
635   return frame->n_vectors;
636 }
637
638 /* *INDENT-OFF* */
639 VNET_DEVICE_CLASS (gbp_vxlan_device_class) = {
640   .name = "GBP VXLAN tunnel-template",
641   .format_device_name = format_gbp_vxlan_tunnel_name,
642   .format_device = format_gbp_vxlan_tunnel,
643   .format_tx_trace = format_gbp_vxlan_tx_trace,
644   .admin_up_down_function = gbp_vxlan_interface_admin_up_down,
645   .tx_function = gbp_vxlan_interface_tx,
646 };
647
648 VNET_HW_INTERFACE_CLASS (gbp_vxlan_hw_interface_class) = {
649   .name = "GBP-VXLAN",
650   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
651 };
652 /* *INDENT-ON* */
653
654 int
655 gbp_vxlan_tunnel_add (u32 vni, gbp_vxlan_tunnel_layer_t layer,
656                       u32 bd_rd_id, u32 * sw_if_indexp)
657 {
658   gbp_vxlan_tunnel_t *gt;
659   index_t gti;
660   uword *p;
661   int rv;
662
663   rv = 0;
664   p = hash_get (gv_db, vni);
665
666   GBP_VXLAN_TUN_DBG ("add: %d %d %d", vni, layer, bd_rd_id);
667
668   if (NULL == p)
669     {
670       vnet_sw_interface_t *si;
671       vnet_hw_interface_t *hi;
672       index_t gbi, grdi;
673       vnet_main_t *vnm;
674
675       gbi = grdi = INDEX_INVALID;
676
677       if (layer == GBP_VXLAN_TUN_L2)
678         {
679           gbi = gbp_bridge_domain_find_and_lock (bd_rd_id);
680
681           if (INDEX_INVALID == gbi)
682             {
683               return (VNET_API_ERROR_BD_NOT_MODIFIABLE);
684             }
685         }
686       else
687         {
688           grdi = gbp_route_domain_find_and_lock (bd_rd_id);
689
690           if (INDEX_INVALID == grdi)
691             {
692               return (VNET_API_ERROR_NO_SUCH_FIB);
693             }
694         }
695
696       vnm = vnet_get_main ();
697       pool_get (gbp_vxlan_tunnel_pool, gt);
698       gti = gt - gbp_vxlan_tunnel_pool;
699
700       gt->gt_vni = vni;
701       gt->gt_layer = layer;
702       gt->gt_bd_rd_id = bd_rd_id;
703       gt->gt_hw_if_index = vnet_register_interface (vnm,
704                                                     gbp_vxlan_device_class.index,
705                                                     gti,
706                                                     gbp_vxlan_hw_interface_class.index,
707                                                     gti);
708
709       hi = vnet_get_hw_interface (vnm, gt->gt_hw_if_index);
710
711       gt->gt_sw_if_index = hi->sw_if_index;
712
713       /* don't flood packets in a BD to these interfaces */
714       si = vnet_get_sw_interface (vnm, gt->gt_sw_if_index);
715       si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
716
717       if (layer == GBP_VXLAN_TUN_L2)
718         {
719           gbp_bridge_domain_t *gb;
720
721           gb = gbp_bridge_domain_get (gbi);
722
723           gt->gt_gbd = gbi;
724           gt->gt_bd_index = gb->gb_bd_id;
725           gb->gb_vni_sw_if_index = gt->gt_sw_if_index;
726           /* set it up as a GBP interface */
727           gt->gt_itf = gbp_itf_add_and_lock (gt->gt_sw_if_index,
728                                              gt->gt_bd_index);
729           gbp_learn_enable (gt->gt_sw_if_index, GBP_LEARN_MODE_L2);
730           gbp_sclass_enable_l2 (gt->gt_sw_if_index);
731         }
732       else
733         {
734           gbp_route_domain_t *grd;
735           fib_protocol_t fproto;
736
737           grd = gbp_route_domain_get (grdi);
738
739           gt->gt_grd = grdi;
740           grd->grd_vni_sw_if_index = gt->gt_sw_if_index;
741
742           gbp_learn_enable (gt->gt_sw_if_index, GBP_LEARN_MODE_L3);
743           gbp_sclass_enable_ip (gt->gt_sw_if_index);
744
745           ip4_sw_interface_enable_disable (gt->gt_sw_if_index, 1);
746           ip6_sw_interface_enable_disable (gt->gt_sw_if_index, 1);
747
748           FOR_EACH_FIB_IP_PROTOCOL (fproto)
749           {
750             gt->gt_fib_index[fproto] = grd->grd_fib_index[fproto];
751
752             ip_table_bind (fproto, gt->gt_sw_if_index,
753                            grd->grd_table_id[fproto], 1);
754           }
755         }
756
757       /*
758        * save the tunnel by VNI and by sw_if_index
759        */
760       hash_set (gv_db, vni, gti);
761
762       vec_validate (gbp_vxlan_tunnel_db, gt->gt_sw_if_index);
763       gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = gti;
764
765       if (sw_if_indexp)
766         *sw_if_indexp = gt->gt_sw_if_index;
767
768       vxlan_gbp_register_udp_ports ();
769     }
770   else
771     {
772       gti = p[0];
773       rv = VNET_API_ERROR_IF_ALREADY_EXISTS;
774     }
775
776   GBP_VXLAN_TUN_DBG ("add: %U", format_gbp_vxlan_tunnel, gti);
777
778   return (rv);
779 }
780
781 int
782 gbp_vxlan_tunnel_del (u32 vni)
783 {
784   gbp_vxlan_tunnel_t *gt;
785   uword *p;
786
787   p = hash_get (gv_db, vni);
788
789   if (NULL != p)
790     {
791       vnet_main_t *vnm;
792
793       vnm = vnet_get_main ();
794       gt = gbp_vxlan_tunnel_get (p[0]);
795
796       vxlan_gbp_unregister_udp_ports ();
797
798       GBP_VXLAN_TUN_DBG ("del: %U", format_gbp_vxlan_tunnel,
799                          gt - gbp_vxlan_tunnel_pool);
800
801       gbp_endpoint_flush (GBP_ENDPOINT_SRC_DP, gt->gt_sw_if_index);
802       ASSERT (0 == vec_len (gt->gt_tuns));
803       vec_free (gt->gt_tuns);
804
805       if (GBP_VXLAN_TUN_L2 == gt->gt_layer)
806         {
807           gbp_learn_disable (gt->gt_sw_if_index, GBP_LEARN_MODE_L2);
808           gbp_sclass_disable_l2 (gt->gt_sw_if_index);
809           gbp_itf_unlock (gt->gt_itf);
810           gbp_bridge_domain_unlock (gt->gt_gbd);
811         }
812       else
813         {
814           fib_protocol_t fproto;
815
816           FOR_EACH_FIB_IP_PROTOCOL (fproto)
817             ip_table_bind (fproto, gt->gt_sw_if_index, 0, 0);
818
819           ip4_sw_interface_enable_disable (gt->gt_sw_if_index, 0);
820           ip6_sw_interface_enable_disable (gt->gt_sw_if_index, 0);
821
822           gbp_learn_disable (gt->gt_sw_if_index, GBP_LEARN_MODE_L3);
823           gbp_sclass_disable_ip (gt->gt_sw_if_index);
824           gbp_route_domain_unlock (gt->gt_grd);
825         }
826
827       vnet_sw_interface_set_flags (vnm, gt->gt_sw_if_index, 0);
828       vnet_delete_hw_interface (vnm, gt->gt_hw_if_index);
829
830       hash_unset (gv_db, vni);
831       gbp_vxlan_tunnel_db[gt->gt_sw_if_index] = INDEX_INVALID;
832
833       pool_put (gbp_vxlan_tunnel_pool, gt);
834     }
835   else
836     return VNET_API_ERROR_NO_SUCH_ENTRY;
837
838   return (0);
839 }
840
841 static clib_error_t *
842 gbp_vxlan_show (vlib_main_t * vm,
843                 unformat_input_t * input, vlib_cli_command_t * cmd)
844 {
845   gbp_vxlan_walk (gbp_vxlan_tunnel_show_one, vm);
846
847   return (NULL);
848 }
849
850 /*?
851  * Show Group Based Policy VXLAN tunnels
852  *
853  * @cliexpar
854  * @cliexstart{show gbp vxlan}
855  * @cliexend
856  ?*/
857 /* *INDENT-OFF* */
858 VLIB_CLI_COMMAND (gbp_vxlan_show_node, static) = {
859   .path = "show gbp vxlan",
860   .short_help = "show gbp vxlan\n",
861   .function = gbp_vxlan_show,
862 };
863 /* *INDENT-ON* */
864
865 static clib_error_t *
866 gbp_vxlan_init (vlib_main_t * vm)
867 {
868   u32 slot4;
869
870   /*
871    * insert ourselves into the VXLAN-GBP arc to collect the no-tunnel
872    * packets.
873    */
874   slot4 = vlib_node_add_next_with_slot (vm,
875                                         vxlan4_gbp_input_node.index,
876                                         gbp_vxlan4_input_node.index,
877                                         VXLAN_GBP_INPUT_NEXT_NO_TUNNEL);
878   ASSERT (slot4 == VXLAN_GBP_INPUT_NEXT_NO_TUNNEL);
879
880   /* slot6 = vlib_node_add_next_with_slot (vm, */
881   /*                                    vxlan6_gbp_input_node.index, */
882   /*                                    gbp_vxlan6_input_node.index, */
883   /*                                    VXLAN_GBP_INPUT_NEXT_NO_TUNNEL); */
884   /* ASSERT (slot6 == VXLAN_GBP_INPUT_NEXT_NO_TUNNEL); */
885
886   gt_logger = vlib_log_register_class ("gbp", "tun");
887
888   return (NULL);
889 }
890
891 VLIB_INIT_FUNCTION (gbp_vxlan_init);
892
893 /*
894  * fd.io coding-style-patch-verification: ON
895  *
896  * Local Variables:
897  * eval: (c-set-style "gnu")
898  * End:
899  */