gbp: add anonymous l3-out external interfaces
[vpp.git] / src / plugins / gbp / gbp_policy_node.c
index 8fe1d7f..10f5956 100644 (file)
  */
 
 #include <plugins/gbp/gbp.h>
+#include <plugins/gbp/gbp_classify.h>
 #include <plugins/gbp/gbp_policy_dpo.h>
+#include <plugins/gbp/gbp_bridge_domain.h>
+#include <plugins/gbp/gbp_ext_itf.h>
 
 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
 #include <vnet/vxlan-gbp/vxlan_gbp.h>
@@ -49,8 +52,9 @@ typedef enum
 typedef struct gbp_policy_trace_t_
 {
   /* per-pkt trace data */
-  u32 sclass;
-  u32 dst_epg;
+  gbp_scope_t scope;
+  sclass_t sclass;
+  sclass_t dclass;
   u32 acl_index;
   u32 allowed;
   u32 flags;
@@ -107,22 +111,46 @@ gbp_policy_is_ethertype_allowed (const gbp_contract_t * gc0, u16 ethertype)
   return (0);
 }
 
+static_always_inline gbp_policy_next_t
+gbp_policy_l2_feature_next (gbp_policy_main_t * gpm, vlib_buffer_t * b,
+                           const gbp_policy_type_t type)
+{
+  u32 feat_bit;
+
+  switch (type)
+    {
+    case GBP_POLICY_PORT:
+      feat_bit = L2OUTPUT_FEAT_GBP_POLICY_PORT;
+      break;
+    case GBP_POLICY_MAC:
+      feat_bit = L2OUTPUT_FEAT_GBP_POLICY_MAC;
+      break;
+    case GBP_POLICY_LPM:
+      feat_bit = L2OUTPUT_FEAT_GBP_POLICY_LPM;
+      break;
+    default:
+      return GBP_POLICY_NEXT_DROP;
+    }
+
+  return vnet_l2_feature_next (b, gpm->l2_output_feat_next[type], feat_bit);
+}
+
 static uword
 gbp_policy_inline (vlib_main_t * vm,
                   vlib_node_runtime_t * node,
-                  vlib_frame_t * frame, u8 is_port_based)
+                  vlib_frame_t * frame, const gbp_policy_type_t type)
 {
   gbp_main_t *gm = &gbp_main;
   gbp_policy_main_t *gpm = &gbp_policy_main;
   u32 n_left_from, *from, *to_next;
   u32 next_index, thread_index;
-  u32 n_allow_intra, n_allow_a_bit;
+  u32 n_allow_intra, n_allow_a_bit, n_allow_sclass_1;
 
   next_index = 0;
   n_left_from = frame->n_vectors;
   from = vlib_frame_vector_args (frame);
   thread_index = vm->thread_index;
-  n_allow_intra = n_allow_a_bit = 0;
+  n_allow_intra = n_allow_a_bit = n_allow_sclass_1 = 0;
 
   while (n_left_from > 0)
     {
@@ -154,6 +182,9 @@ gbp_policy_inline (vlib_main_t * vm,
          h0 = vlib_buffer_get_current (b0);
          sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
 
+         /* zero out the key to ensure the pad space is clear */
+         key0.as_u64 = 0;
+
          /*
           * Reflection check; in and out on an ivxlan tunnel
           */
@@ -169,29 +200,48 @@ gbp_policy_inline (vlib_main_t * vm,
           */
          if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
            {
-             next0 = vnet_l2_feature_next (b0,
-                                           gpm->l2_output_feat_next
-                                           [is_port_based],
-                                           (is_port_based ?
-                                            L2OUTPUT_FEAT_GBP_POLICY_PORT :
-                                            L2OUTPUT_FEAT_GBP_POLICY_MAC));
+             next0 = gbp_policy_l2_feature_next (gpm, b0, type);
              n_allow_a_bit++;
-             key0.as_u32 = ~0;
+             key0.as_u64 = ~0;
              goto trace;
            }
 
          /*
           * determine the src and dst EPG
           */
-         if (is_port_based)
-           ge0 = gbp_endpoint_find_itf (sw_if_index0);
-         else
-           ge0 = gbp_endpoint_find_mac (h0->dst_address,
-                                        vnet_buffer (b0)->l2.bd_index);
 
-         if (NULL != ge0)
-           key0.gck_dst = ge0->ge_fwd.gef_sclass;
+         key0.gck_dst = SCLASS_INVALID;
+
+         if (GBP_POLICY_LPM == type)
+           {
+             const ip4_address_t *ip4 = 0;
+             const ip6_address_t *ip6 = 0;
+             const dpo_proto_t proto =
+               gbp_classify_get_ip_address (h0, &ip4, &ip6,
+                                            GBP_CLASSIFY_GET_IP_DST);
+             if (PREDICT_TRUE (DPO_PROTO_NONE != proto))
+               {
+                 const gbp_ext_itf_t *ext_itf =
+                   gbp_ext_itf_get (sw_if_index0);
+                 const gbp_policy_dpo_t *gpd =
+                   gbp_classify_get_gpd (ip4, ip6,
+                                         ext_itf->gx_fib_index[proto]);
+                 if (gpd)
+                   key0.gck_dst = gpd->gpd_sclass;
+               }
+           }
          else
+           {
+             if (GBP_POLICY_PORT == type)
+               ge0 = gbp_endpoint_find_itf (sw_if_index0);
+             else
+               ge0 = gbp_endpoint_find_mac (h0->dst_address,
+                                            vnet_buffer (b0)->l2.bd_index);
+             if (NULL != ge0)
+               key0.gck_dst = ge0->ge_fwd.gef_sclass;
+           }
+
+         if (SCLASS_INVALID == key0.gck_dst)
            {
              /* If you cannot determine the destination EP then drop */
              b0->error = node->errors[GBP_POLICY_ERROR_DROP_NO_DCLASS];
@@ -206,18 +256,24 @@ gbp_policy_inline (vlib_main_t * vm,
                  /*
                   * intra-epg allowed
                   */
-                 next0 =
-                   vnet_l2_feature_next (b0,
-                                         gpm->l2_output_feat_next
-                                         [is_port_based],
-                                         (is_port_based ?
-                                          L2OUTPUT_FEAT_GBP_POLICY_PORT :
-                                          L2OUTPUT_FEAT_GBP_POLICY_MAC));
+                 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
                  vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
                  n_allow_intra++;
                }
+             else if (PREDICT_FALSE (key0.gck_src == 1 || key0.gck_dst == 1))
+               {
+                 /*
+                  * sclass or dclass 1 allowed
+                  */
+                 next0 = gbp_policy_l2_feature_next (gpm, b0, type);
+                 vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
+                 n_allow_sclass_1++;
+               }
              else
                {
+                 key0.gck_scope =
+                   gbp_bridge_domain_get_scope (vnet_buffer (b0)->
+                                                l2.bd_index);
                  gci0 = gbp_contract_find (&key0);
 
                  if (INDEX_INVALID != gci0)
@@ -297,13 +353,9 @@ gbp_policy_inline (vlib_main_t * vm,
                              switch (gu->gu_action)
                                {
                                case GBP_RULE_PERMIT:
-                                 next0 = vnet_l2_feature_next
-                                   (b0,
-                                    gpm->l2_output_feat_next
-                                    [is_port_based],
-                                    (is_port_based ?
-                                     L2OUTPUT_FEAT_GBP_POLICY_PORT :
-                                     L2OUTPUT_FEAT_GBP_POLICY_MAC));
+                                 next0 =
+                                   gbp_policy_l2_feature_next (gpm, b0,
+                                                               type);
                                  break;
                                case GBP_RULE_DENY:
                                  next0 = GBP_POLICY_NEXT_DROP;
@@ -344,12 +396,7 @@ gbp_policy_inline (vlib_main_t * vm,
               * the src EPG is not set when the packet arrives on an EPG
               * uplink interface and we do not need to apply policy
               */
-             next0 =
-               vnet_l2_feature_next (b0,
-                                     gpm->l2_output_feat_next[is_port_based],
-                                     (is_port_based ?
-                                      L2OUTPUT_FEAT_GBP_POLICY_PORT :
-                                      L2OUTPUT_FEAT_GBP_POLICY_MAC));
+             next0 = gbp_policy_l2_feature_next (gpm, b0, type);
            }
 
        trace:
@@ -358,7 +405,8 @@ gbp_policy_inline (vlib_main_t * vm,
              gbp_policy_trace_t *t =
                vlib_add_trace (vm, node, b0, sizeof (*t));
              t->sclass = key0.gck_src;
-             t->dst_epg = key0.gck_dst;
+             t->dclass = key0.gck_dst;
+             t->scope = key0.gck_scope;
              t->acl_index = (gc0 ? gc0->gc_acl_index : ~0);
              t->allowed = (next0 != GBP_POLICY_NEXT_DROP);
              t->flags = vnet_buffer2 (b0)->gbp.flags;
@@ -377,6 +425,9 @@ gbp_policy_inline (vlib_main_t * vm,
                               GBP_POLICY_ERROR_ALLOW_INTRA, n_allow_intra);
   vlib_node_increment_counter (vm, node->node_index,
                               GBP_POLICY_ERROR_ALLOW_A_BIT, n_allow_a_bit);
+  vlib_node_increment_counter (vm, node->node_index,
+                              GBP_POLICY_ERROR_ALLOW_SCLASS_1,
+                              n_allow_sclass_1);
 
   return frame->n_vectors;
 }
@@ -385,14 +436,21 @@ VLIB_NODE_FN (gbp_policy_port_node) (vlib_main_t * vm,
                                     vlib_node_runtime_t * node,
                                     vlib_frame_t * frame)
 {
-  return (gbp_policy_inline (vm, node, frame, 1));
+  return (gbp_policy_inline (vm, node, frame, GBP_POLICY_PORT));
 }
 
 VLIB_NODE_FN (gbp_policy_mac_node) (vlib_main_t * vm,
                                    vlib_node_runtime_t * node,
                                    vlib_frame_t * frame)
 {
-  return (gbp_policy_inline (vm, node, frame, 0));
+  return (gbp_policy_inline (vm, node, frame, GBP_POLICY_MAC));
+}
+
+VLIB_NODE_FN (gbp_policy_lpm_node) (vlib_main_t * vm,
+                                   vlib_node_runtime_t * node,
+                                   vlib_frame_t * frame)
+{
+  return (gbp_policy_inline (vm, node, frame, GBP_POLICY_LPM));
 }
 
 /* packet trace format function */
@@ -404,8 +462,8 @@ format_gbp_policy_trace (u8 * s, va_list * args)
   gbp_policy_trace_t *t = va_arg (*args, gbp_policy_trace_t *);
 
   s =
-    format (s, "sclass:%d, dst:%d, acl:%d allowed:%d flags:%U",
-           t->sclass, t->dst_epg, t->acl_index, t->allowed,
+    format (s, "scope:%d sclass:%d, dclass:%d, acl:%d allowed:%d flags:%U",
+           t->scope, t->sclass, t->dclass, t->acl_index, t->allowed,
            format_vxlan_gbp_header_gpflags, t->flags);
 
   return s;
@@ -442,6 +500,21 @@ VLIB_REGISTER_NODE (gbp_policy_mac_node) = {
   },
 };
 
+VLIB_REGISTER_NODE (gbp_policy_lpm_node) = {
+  .name = "gbp-policy-lpm",
+  .vector_size = sizeof (u32),
+  .format_trace = format_gbp_policy_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(gbp_policy_error_strings),
+  .error_strings = gbp_policy_error_strings,
+
+  .n_next_nodes = GBP_POLICY_N_NEXT,
+  .next_nodes = {
+    [GBP_POLICY_NEXT_DROP] = "error-drop",
+  },
+};
+
 /* *INDENT-ON* */
 
 /*