fib: fib api updates
[vpp.git] / src / vnet / vxlan-gbp / vxlan_gbp.c
index 253261d..7b09b57 100644 (file)
  *
  * VXLAN GBP provides the features of vxlan and carry group policy id.
  */
-
+static vlib_punt_hdl_t punt_hdl;
 
 vxlan_gbp_main_t vxlan_gbp_main;
 
-static u8 *
-format_decap_next (u8 * s, va_list * args)
+u8 *
+format_vxlan_gbp_tunnel_mode (u8 * s, va_list * args)
 {
-  u32 next_index = va_arg (*args, u32);
+  vxlan_gbp_tunnel_mode_t mode = va_arg (*args, vxlan_gbp_tunnel_mode_t);
 
-  if (next_index == VXLAN_GBP_INPUT_NEXT_DROP)
-    return format (s, "drop");
-  else
-    return format (s, "index %d", next_index);
-  return s;
+  switch (mode)
+    {
+    case VXLAN_GBP_TUNNEL_MODE_L2:
+      s = format (s, "L2");
+      break;
+    case VXLAN_GBP_TUNNEL_MODE_L3:
+      s = format (s, "L3");
+      break;
+    }
+  return (s);
 }
 
 u8 *
@@ -51,17 +56,15 @@ format_vxlan_gbp_tunnel (u8 * s, va_list * args)
 
   s = format (s,
              "[%d] instance %d src %U dst %U vni %d fib-idx %d"
-             " sw-if-idx %d ",
+             " sw-if-idx %d mode %U ",
              t->dev_instance, t->user_instance,
              format_ip46_address, &t->src, IP46_TYPE_ANY,
              format_ip46_address, &t->dst, IP46_TYPE_ANY,
-             t->vni, t->encap_fib_index, t->sw_if_index);
+             t->vni, t->encap_fib_index, t->sw_if_index,
+             format_vxlan_gbp_tunnel_mode, t->mode);
 
   s = format (s, "encap-dpo-idx %d ", t->next_dpo.dpoi_index);
 
-  if (PREDICT_FALSE (t->decap_next_index != VXLAN_GBP_INPUT_NEXT_L2_INPUT))
-    s = format (s, "decap-next-%U ", format_decap_next, t->decap_next_index);
-
   if (PREDICT_FALSE (ip46_address_is_multicast (&t->dst)))
     s = format (s, "mcast-sw-if-idx %d ", t->mcast_sw_if_index);
 
@@ -210,9 +213,9 @@ const static fib_node_vft_t vxlan_gbp_vft = {
 
 #define foreach_copy_field                      \
 _(vni)                                          \
+_(mode)                                         \
 _(mcast_sw_if_index)                            \
 _(encap_fib_index)                              \
-_(decap_next_index)                             \
 _(src)                                          \
 _(dst)
 
@@ -230,7 +233,7 @@ vxlan_gbp_rewrite (vxlan_gbp_tunnel_t * t, bool is_ip6)
   vxlan_gbp_header_t *vxlan_gbp;
   /* Fixed portion of the (outer) ip header */
 
-  memset (&h, 0, sizeof (h));
+  clib_memset (&h, 0, sizeof (h));
   if (!is_ip6)
     {
       ip4_header_t *ip = &h.h4.ip4;
@@ -267,18 +270,6 @@ vxlan_gbp_rewrite (vxlan_gbp_tunnel_t * t, bool is_ip6)
   vnet_rewrite_set_data (*t, &h, len);
 }
 
-static bool
-vxlan_gbp_decap_next_is_valid (vxlan_gbp_main_t * vxm, u32 is_ip6,
-                              u32 decap_next_index)
-{
-  vlib_main_t *vm = vxm->vlib_main;
-  u32 input_idx = (!is_ip6) ?
-    vxlan4_gbp_input_node.index : vxlan6_gbp_input_node.index;
-  vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
-
-  return decap_next_index < r->n_next_nodes;
-}
-
 static uword
 vtep_addr_ref (ip46_address_t * ip)
 {
@@ -352,6 +343,42 @@ mcast_shared_remove (ip46_address_t * dst)
   hash_unset_mem_free (&vxlan_gbp_main.mcast_shared, dst);
 }
 
+inline void
+vxlan_gbp_register_udp_ports (void)
+{
+  vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
+
+  if (vxm->udp_ports_registered == 0)
+    {
+      udp_register_dst_port (vxm->vlib_main, UDP_DST_PORT_vxlan_gbp,
+                            vxlan4_gbp_input_node.index, /* is_ip4 */ 1);
+      udp_register_dst_port (vxm->vlib_main, UDP_DST_PORT_vxlan6_gbp,
+                            vxlan6_gbp_input_node.index, /* is_ip4 */ 0);
+    }
+  /*
+   * Counts the number of vxlan_gbp tunnels
+   */
+  vxm->udp_ports_registered += 1;
+}
+
+inline void
+vxlan_gbp_unregister_udp_ports (void)
+{
+  vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
+
+  ASSERT (vxm->udp_ports_registered != 0);
+
+  if (vxm->udp_ports_registered == 1)
+    {
+      udp_unregister_dst_port (vxm->vlib_main, UDP_DST_PORT_vxlan_gbp,
+                              /* is_ip4 */ 1);
+      udp_unregister_dst_port (vxm->vlib_main, UDP_DST_PORT_vxlan6_gbp,
+                              /* is_ip4 */ 0);
+    }
+
+  vxm->udp_ports_registered -= 1;
+}
+
 int vnet_vxlan_gbp_tunnel_add_del
   (vnet_vxlan_gbp_tunnel_add_del_args_t * a, u32 * sw_if_indexp)
 {
@@ -367,7 +394,9 @@ int vnet_vxlan_gbp_tunnel_add_del
   int not_found;
   if (!is_ip6)
     {
-      key4.key[0] = a->dst.ip4.as_u32;
+      key4.key[0] = ip46_address_is_multicast (&a->dst) ?
+       a->dst.ip4.as_u32 :
+       a->dst.ip4.as_u32 | (((u64) a->src.ip4.as_u32) << 32);
       key4.key[1] = (((u64) a->encap_fib_index) << 32)
        | clib_host_to_net_u32 (a->vni << 8);
       not_found =
@@ -398,16 +427,13 @@ int vnet_vxlan_gbp_tunnel_add_del
 
       /* adding a tunnel: tunnel must not already exist */
       if (p)
-       return VNET_API_ERROR_TUNNEL_EXIST;
-
-      /* if not set explicitly, default to l2 */
-      if (a->decap_next_index == ~0)
-       a->decap_next_index = VXLAN_GBP_INPUT_NEXT_L2_INPUT;
-      if (!vxlan_gbp_decap_next_is_valid (vxm, is_ip6, a->decap_next_index))
-       return VNET_API_ERROR_INVALID_DECAP_NEXT;
-
+       {
+         t = pool_elt_at_index (vxm->tunnels, *p);
+         *sw_if_indexp = t->sw_if_index;
+         return VNET_API_ERROR_TUNNEL_EXIST;
+       }
       pool_get_aligned (vxm->tunnels, t, CLIB_CACHE_LINE_BYTES);
-      memset (t, 0, sizeof (*t));
+      clib_memset (t, 0, sizeof (*t));
       dev_instance = t - vxm->tunnels;
 
       /* copy from arg structure */
@@ -455,6 +481,8 @@ int vnet_vxlan_gbp_tunnel_add_del
          return VNET_API_ERROR_INVALID_REGISTRATION;
        }
 
+      vxlan_gbp_register_udp_ports ();
+
       t->hw_if_index = vnet_register_interface
        (vnm, vxlan_gbp_device_class.index, dev_instance,
         vxlan_gbp_hw_class.index, dev_instance);
@@ -467,6 +495,12 @@ int vnet_vxlan_gbp_tunnel_add_del
 
       t->sw_if_index = sw_if_index = hi->sw_if_index;
 
+      if (VXLAN_GBP_TUNNEL_MODE_L3 == t->mode)
+       {
+         ip4_sw_interface_enable_disable (t->sw_if_index, 1);
+         ip6_sw_interface_enable_disable (t->sw_if_index, 1);
+       }
+
       vec_validate_init_empty (vxm->tunnel_index_by_sw_if_index, sw_if_index,
                               ~0);
       vxm->tunnel_index_by_sw_if_index[sw_if_index] = dev_instance;
@@ -506,8 +540,8 @@ int vnet_vxlan_gbp_tunnel_add_del
       else
        {
          /* Multicast tunnel -
-          * as the same mcast group can be used for mutiple mcast tunnels
-          * with different VNIs, create the output fib adjecency only if
+          * as the same mcast group can be used for multiple mcast tunnels
+          * with different VNIs, create the output fib adjacency only if
           * it does not already exist
           */
          fib_protocol_t fp = fib_ip_proto (is_ip6);
@@ -523,6 +557,7 @@ int vnet_vxlan_gbp_tunnel_add_del
                .frp_fib_index = ~0,
                .frp_weight = 0,
                .frp_flags = FIB_ROUTE_PATH_LOCAL,
+               .frp_mitf_flags = MFIB_ITF_FLAG_FORWARD,
              };
              const mfib_prefix_t mpfx = {
                .fp_proto = fp,
@@ -537,16 +572,15 @@ int vnet_vxlan_gbp_tunnel_add_del
               */
              mfib_table_entry_path_update (t->encap_fib_index,
                                            &mpfx,
-                                           MFIB_SOURCE_VXLAN_GBP,
-                                           &path, MFIB_ITF_FLAG_FORWARD);
+                                           MFIB_SOURCE_VXLAN_GBP, &path);
 
              path.frp_sw_if_index = a->mcast_sw_if_index;
              path.frp_flags = FIB_ROUTE_PATH_FLAG_NONE;
+             path.frp_mitf_flags = MFIB_ITF_FLAG_ACCEPT;
              mfei = mfib_table_entry_path_update (t->encap_fib_index,
                                                   &mpfx,
                                                   MFIB_SOURCE_VXLAN_GBP,
-                                                  &path,
-                                                  MFIB_ITF_FLAG_ACCEPT);
+                                                  &path);
 
              /*
               * Create the mcast adjacency to send traffic to the group
@@ -588,6 +622,12 @@ int vnet_vxlan_gbp_tunnel_add_del
       sw_if_index = t->sw_if_index;
       vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
 
+      if (VXLAN_GBP_TUNNEL_MODE_L3 == t->mode)
+       {
+         ip4_sw_interface_enable_disable (t->sw_if_index, 0);
+         ip6_sw_interface_enable_disable (t->sw_if_index, 0);
+       }
+
       vxm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
 
       if (!is_ip6)
@@ -608,6 +648,7 @@ int vnet_vxlan_gbp_tunnel_add_del
          mcast_shared_remove (&t->dst);
        }
 
+      vxlan_gbp_unregister_udp_ports ();
       vnet_delete_hw_interface (vnm, t->hw_if_index);
       hash_unset (vxm->instance_used, t->user_instance);
 
@@ -621,6 +662,36 @@ int vnet_vxlan_gbp_tunnel_add_del
   return 0;
 }
 
+int
+vnet_vxlan_gbp_tunnel_del (u32 sw_if_index)
+{
+  vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
+  vxlan_gbp_tunnel_t *t = 0;
+  u32 ti;
+
+  if (sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index))
+    return VNET_API_ERROR_NO_SUCH_ENTRY;
+
+  ti = vxm->tunnel_index_by_sw_if_index[sw_if_index];
+  if (~0 != ti)
+    {
+      t = pool_elt_at_index (vxm->tunnels, ti);
+
+      vnet_vxlan_gbp_tunnel_add_del_args_t args = {
+       .is_add = 0,
+       .is_ip6 = !ip46_address_is_ip4 (&t->src),
+       .vni = t->vni,
+       .src = t->src,
+       .dst = t->dst,
+       .instance = ~0,
+      };
+
+      return (vnet_vxlan_gbp_tunnel_add_del (&args, NULL));
+    }
+
+  return VNET_API_ERROR_NO_SUCH_ENTRY;
+}
+
 static uword
 get_decap_next_for_node (u32 node_index, u32 ipv4_set)
 {
@@ -661,6 +732,7 @@ vxlan_gbp_tunnel_add_del_command_fn (vlib_main_t * vm,
   unformat_input_t _line_input, *line_input = &_line_input;
   ip46_address_t src = ip46_address_initializer, dst =
     ip46_address_initializer;
+  vxlan_gbp_tunnel_mode_t mode = VXLAN_GBP_TUNNEL_MODE_L2;
   u8 is_add = 1;
   u8 src_set = 0;
   u8 dst_set = 0;
@@ -820,7 +892,7 @@ vxlan_gbp_tunnel_add_del_command_fn (vlib_main_t * vm,
  * Example of how to create a VXLAN Tunnel with a known name, vxlan_gbp_tunnel42:
  * @cliexcmd{create vxlan_gbp tunnel src 10.0.3.1 dst 10.0.3.3 instance 42}
  * Example of how to create a multicast VXLAN Tunnel with a known name, vxlan_gbp_tunnel23:
- * @cliexcmd{create vxlan_gbp tunnel src 10.0.3.1 group 239.1.1.1 GigabitEtherner0/8/0 instance 23}
+ * @cliexcmd{create vxlan_gbp tunnel src 10.0.3.1 group 239.1.1.1 GigabitEthernet0/8/0 instance 23}
  * Example of how to delete a VXLAN Tunnel:
  * @cliexcmd{create vxlan_gbp tunnel src 10.0.3.1 dst 10.0.3.3 vni 13 del}
  ?*/
@@ -986,7 +1058,7 @@ set_ip4_vxlan_gbp_bypass (vlib_main_t * vm,
  *                                   ip4-lookup [2]
  * @cliexend
  *
- * Example of how to display the feature enabed on an interface:
+ * Example of how to display the feature enabled on an interface:
  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
  * IP feature paths configured on GigabitEthernet2/0/0...
  * ...
@@ -1043,7 +1115,7 @@ set_ip6_vxlan_gbp_bypass (vlib_main_t * vm,
  *                                   ip6-lookup [2]
  * @cliexend
  *
- * Example of how to display the feature enabed on an interface:
+ * Example of how to display the feature enabled on an interface:
  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
  * IP feature paths configured on GigabitEthernet2/0/0...
  * ...
@@ -1088,17 +1160,26 @@ vxlan_gbp_init (vlib_main_t * vm)
                                       sizeof (ip46_address_t),
                                       sizeof (mcast_shared_t));
 
-  udp_register_dst_port (vm, UDP_DST_PORT_vxlan_gbp,
-                        vxlan4_gbp_input_node.index, /* is_ip4 */ 1);
-  udp_register_dst_port (vm, UDP_DST_PORT_vxlan6_gbp,
-                        vxlan6_gbp_input_node.index, /* is_ip4 */ 0);
-
   fib_node_register_type (FIB_NODE_TYPE_VXLAN_GBP_TUNNEL, &vxlan_gbp_vft);
 
-  return 0;
+  punt_hdl = vlib_punt_client_register ("vxlan-gbp");
+
+  vlib_punt_reason_alloc (punt_hdl,
+                         "VXLAN-GBP-no-such-v4-tunnel",
+                         &vxm->punt_no_such_tunnel[FIB_PROTOCOL_IP4]);
+  vlib_punt_reason_alloc (punt_hdl,
+                         "VXLAN-GBP-no-such-v6-tunnel",
+                         &vxm->punt_no_such_tunnel[FIB_PROTOCOL_IP6]);
+
+  return (0);
 }
 
-VLIB_INIT_FUNCTION (vxlan_gbp_init);
+/* *INDENT-OFF* */
+VLIB_INIT_FUNCTION (vxlan_gbp_init) =
+{
+  .runs_after = VLIB_INITS("punt_init"),
+};
+/* *INDENT-ON* */
 
 /*
  * fd.io coding-style-patch-verification: ON