GBP: use sclass in the DP for policy
[vpp.git] / src / plugins / gbp / gbp_subnet.c
index d9d4299..5e56c82 100644 (file)
@@ -43,10 +43,16 @@ typedef struct gbp_subnet_t_
   {
     struct
     {
-      epg_id_t gs_epg;
+      sclass_t gs_sclass;
       u32 gs_sw_if_index;
     } gs_stitched_external;
+    struct
+    {
+      sclass_t gs_sclass;
+    } gs_l3_out;
   };
+
+  fib_node_index_t gs_fei;
 } gbp_subnet_t;
 
 /**
@@ -102,7 +108,7 @@ gbp_subnet_db_del (gbp_subnet_t * gs)
 
 
 static int
-gbp_subnet_transport_add (const gbp_subnet_t * gs)
+gbp_subnet_transport_add (gbp_subnet_t * gs)
 {
   dpo_id_t gfd = DPO_INVALID;
   gbp_route_domain_t *grd;
@@ -111,14 +117,18 @@ gbp_subnet_transport_add (const gbp_subnet_t * gs)
   fproto = gs->gs_key->gsk_pfx.fp_proto;
   grd = gbp_route_domain_get (gs->gs_rd);
 
-  fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
-                                  &gs->gs_key->gsk_pfx,
-                                  FIB_SOURCE_PLUGIN_HI,
-                                  FIB_ENTRY_FLAG_NONE,
-                                  fib_proto_to_dpo (fproto),
-                                  &ADJ_BCAST_ADDR,
-                                  grd->grd_uu_sw_if_index[fproto],
-                                  ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
+  if (~0 == grd->grd_uu_sw_if_index[fproto])
+    return (VNET_API_ERROR_INVALID_SW_IF_INDEX);
+
+  gs->gs_fei = fib_table_entry_update_one_path (gs->gs_key->gsk_fib_index,
+                                               &gs->gs_key->gsk_pfx,
+                                               FIB_SOURCE_PLUGIN_HI,
+                                               FIB_ENTRY_FLAG_NONE,
+                                               fib_proto_to_dpo (fproto),
+                                               &ADJ_BCAST_ADDR,
+                                               grd->grd_uu_sw_if_index
+                                               [fproto], ~0, 1, NULL,
+                                               FIB_ROUTE_PATH_FLAG_NONE);
 
   dpo_reset (&gfd);
 
@@ -126,17 +136,18 @@ gbp_subnet_transport_add (const gbp_subnet_t * gs)
 }
 
 static int
-gbp_subnet_internal_add (const gbp_subnet_t * gs)
+gbp_subnet_internal_add (gbp_subnet_t * gs)
 {
   dpo_id_t gfd = DPO_INVALID;
 
   gbp_fwd_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
                           &gfd);
 
-  fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
-                                     &gs->gs_key->gsk_pfx,
-                                     FIB_SOURCE_PLUGIN_HI,
-                                     FIB_ENTRY_FLAG_EXCLUSIVE, &gfd);
+  gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
+                                                  &gs->gs_key->gsk_pfx,
+                                                  FIB_SOURCE_PLUGIN_HI,
+                                                  FIB_ENTRY_FLAG_EXCLUSIVE,
+                                                  &gfd);
 
   dpo_reset (&gfd);
 
@@ -144,23 +155,44 @@ gbp_subnet_internal_add (const gbp_subnet_t * gs)
 }
 
 static int
-gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, epg_id_t epg)
+gbp_subnet_external_add (gbp_subnet_t * gs, u32 sw_if_index, sclass_t sclass)
 {
   dpo_id_t gpd = DPO_INVALID;
 
-  gs->gs_stitched_external.gs_epg = epg;
+  gs->gs_stitched_external.gs_sclass = sclass;
   gs->gs_stitched_external.gs_sw_if_index = sw_if_index;
 
   gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
-                             gs->gs_stitched_external.gs_epg,
+                             gs->gs_stitched_external.gs_sclass,
                              gs->gs_stitched_external.gs_sw_if_index, &gpd);
 
-  fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
-                                     &gs->gs_key->gsk_pfx,
-                                     FIB_SOURCE_PLUGIN_HI,
-                                     (FIB_ENTRY_FLAG_EXCLUSIVE |
-                                      FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
-                                     &gpd);
+  gs->gs_fei = fib_table_entry_special_dpo_update (gs->gs_key->gsk_fib_index,
+                                                  &gs->gs_key->gsk_pfx,
+                                                  FIB_SOURCE_PLUGIN_HI,
+                                                  (FIB_ENTRY_FLAG_EXCLUSIVE |
+                                                   FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT),
+                                                  &gpd);
+
+  dpo_reset (&gpd);
+
+  return (0);
+}
+
+static int
+gbp_subnet_l3_out_add (gbp_subnet_t * gs, u32 sw_if_index, sclass_t sclass)
+{
+  dpo_id_t gpd = DPO_INVALID;
+
+  gs->gs_l3_out.gs_sclass = sclass;
+
+  gbp_policy_dpo_add_or_lock (fib_proto_to_dpo (gs->gs_key->gsk_pfx.fp_proto),
+                             gs->gs_l3_out.gs_sclass, ~0, &gpd);
+
+  gs->gs_fei = fib_table_entry_special_dpo_add (gs->gs_key->gsk_fib_index,
+                                               &gs->gs_key->gsk_pfx,
+                                               FIB_SOURCE_SPECIAL,
+                                               FIB_ENTRY_FLAG_INTERPOSE,
+                                               &gpd);
 
   dpo_reset (&gpd);
 
@@ -190,7 +222,10 @@ gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
 
   gs = pool_elt_at_index (gbp_subnet_pool, gsi);
 
-  fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
+  if (GBP_SUBNET_L3_OUT == gs->gs_type)
+    fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_SPECIAL);
+  else
+    fib_table_entry_delete (fib_index, pfx, FIB_SOURCE_PLUGIN_HI);
 
   gbp_subnet_db_del (gs);
   gbp_route_domain_unlock (gs->gs_rd);
@@ -203,7 +238,7 @@ gbp_subnet_del (u32 rd_id, const fib_prefix_t * pfx)
 int
 gbp_subnet_add (u32 rd_id,
                const fib_prefix_t * pfx,
-               gbp_subnet_type_t type, u32 sw_if_index, epg_id_t epg)
+               gbp_subnet_type_t type, u32 sw_if_index, sclass_t sclass)
 {
   gbp_route_domain_t *grd;
   index_t grdi, gsi;
@@ -238,11 +273,14 @@ gbp_subnet_add (u32 rd_id,
       rv = gbp_subnet_internal_add (gs);
       break;
     case GBP_SUBNET_STITCHED_EXTERNAL:
-      rv = gbp_subnet_external_add (gs, sw_if_index, epg);
+      rv = gbp_subnet_external_add (gs, sw_if_index, sclass);
       break;
     case GBP_SUBNET_TRANSPORT:
       rv = gbp_subnet_transport_add (gs);
       break;
+    case GBP_SUBNET_L3_OUT:
+      rv = gbp_subnet_l3_out_add (gs, sw_if_index, sclass);
+      break;
     }
 
   return (rv);
@@ -254,9 +292,9 @@ gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
   gbp_route_domain_t *grd;
   gbp_subnet_t *gs;
   u32 sw_if_index;
-  epg_id_t epg;
+  sclass_t sclass;
 
-  epg = EPG_INVALID;
+  sclass = SCLASS_INVALID;
   sw_if_index = ~0;
 
   /* *INDENT-OFF* */
@@ -272,12 +310,15 @@ gbp_subnet_walk (gbp_subnet_cb_t cb, void *ctx)
         break;
       case GBP_SUBNET_STITCHED_EXTERNAL:
         sw_if_index = gs->gs_stitched_external.gs_sw_if_index;
-        epg = gs->gs_stitched_external.gs_epg;
+        sclass = gs->gs_stitched_external.gs_sclass;
+        break;
+      case GBP_SUBNET_L3_OUT:
+        sclass = gs->gs_l3_out.gs_sclass;
         break;
       }
 
     if (WALK_STOP == cb (grd->grd_id, &gs->gs_key->gsk_pfx,
-                         gs->gs_type, epg, sw_if_index, ctx))
+                         gs->gs_type, sw_if_index, sclass, ctx))
       break;
   }));
   /* *INDENT-ON* */
@@ -302,6 +343,8 @@ format_gbp_subnet_type (u8 * s, va_list * args)
       return (format (s, "stitched-external"));
     case GBP_SUBNET_TRANSPORT:
       return (format (s, "transport"));
+    case GBP_SUBNET_L3_OUT:
+      return (format (s, "l3-out"));
     }
 
   return (format (s, "unknown"));
@@ -330,24 +373,21 @@ format_gbp_subnet (u8 * s, va_list * args)
     case GBP_SUBNET_TRANSPORT:
       break;
     case GBP_SUBNET_STITCHED_EXTERNAL:
-      s = format (s, " {epg:%d %U}", gs->gs_stitched_external.gs_epg,
+      s = format (s, " {sclass:%d %U}", gs->gs_stitched_external.gs_sclass,
                  format_vnet_sw_if_index_name,
                  vnet_get_main (), gs->gs_stitched_external.gs_sw_if_index);
       break;
+    case GBP_SUBNET_L3_OUT:
+      s = format (s, " {sclass:%d}", gs->gs_l3_out.gs_sclass);
+      break;
     }
 
   switch (flags)
     {
     case GBP_SUBNET_SHOW_DETAILS:
       {
-       fib_node_index_t fei;
-
-       fei = fib_table_lookup_exact_match (gs->gs_key->gsk_fib_index,
-                                           &gs->gs_key->gsk_pfx);
-
-       s =
-         format (s, "\n  %U", format_fib_entry, fei,
-                 FIB_ENTRY_FORMAT_DETAIL);
+       s = format (s, "\n  %U", format_fib_entry, gs->gs_fei,
+                   FIB_ENTRY_FORMAT_DETAIL);
       }
     case GBP_SUBNET_SHOW_BRIEF:
       break;