GBP: contracts API fixed length of allowed ethertypes
[vpp.git] / src / plugins / gbp / gbp_contract.c
index ec54569..e4ac611 100644 (file)
@@ -48,6 +48,7 @@ gbp_rule_alloc (gbp_rule_action_t action,
 
   pool_get_zero (gbp_rule_pool, gu);
 
+  gu->gu_hash_mode = hash_mode;
   gu->gu_nhs = nhs;
   gu->gu_action = action;
 
@@ -158,9 +159,9 @@ format_gbp_rule_action (u8 * s, va_list * args)
 static u8 *
 format_gbp_hash_mode (u8 * s, va_list * args)
 {
-  gbp_hash_mode_t action = va_arg (*args, gbp_hash_mode_t);
+  gbp_hash_mode_t hash_mode = va_arg (*args, gbp_hash_mode_t);
 
-  switch (action)
+  switch (hash_mode)
     {
 #define _(v,a) case GBP_HASH_MODE_##v: return (format (s, "%s", a));
       foreach_gbp_hash_mode
@@ -261,6 +262,23 @@ gbp_contract_mk_adj (gbp_next_hop_t * gnh, fib_protocol_t fproto)
   adj_unlock (old_ai);
 }
 
+static flow_hash_config_t
+gbp_contract_mk_lb_hp (gbp_hash_mode_t gu_hash_mode)
+{
+  switch (gu_hash_mode)
+    {
+    case GBP_HASH_MODE_SRC_IP:
+      return IP_FLOW_HASH_SRC_ADDR;
+    case GBP_HASH_MODE_DST_IP:
+      return IP_FLOW_HASH_DST_ADDR;
+    case GBP_HASH_MODE_SYMMETRIC:
+      return (IP_FLOW_HASH_SRC_ADDR | IP_FLOW_HASH_DST_ADDR |
+             IP_FLOW_HASH_PROTO | IP_FLOW_HASH_SYMMETRIC);
+    }
+
+  return 0;
+}
+
 static void
 gbp_contract_mk_lb (index_t gui, fib_protocol_t fproto)
 {
@@ -307,16 +325,17 @@ gbp_contract_mk_lb (index_t gui, fib_protocol_t fproto)
               dproto, gnh->gnh_ai[fproto]);
     }
 
-    // FIXME get algo and sticky bit from contract LB algo
     if (!dpo_id_is_valid (&gu->gu_dpo[pnode][fproto]))
       {
        dpo_id_t dpo = DPO_INVALID;
 
        dpo_set (&dpo, DPO_LOAD_BALANCE, dproto,
                 load_balance_create (vec_len (paths),
-                                     dproto, IP_FLOW_HASH_DEFAULT));
-       dpo_stack_from_node (policy_nodes[pnode],
-                            &gu->gu_dpo[pnode][fproto], &dpo);
+                                     dproto,
+                                     gbp_contract_mk_lb_hp
+                                     (gu->gu_hash_mode)));
+       dpo_stack_from_node (policy_nodes[pnode], &gu->gu_dpo[pnode][fproto],
+                            &dpo);
        dpo_reset (&dpo);
       }
 
@@ -358,7 +377,7 @@ gbp_contract_next_hop_resolve (index_t gui, index_t gnhi)
                                     gbd->gb_uu_fwd_sw_if_index,
                                     ips,
                                     &gnh->gnh_mac,
-                                    gnh->gnh_bd, gnh->gnh_rd, EPG_INVALID,
+                                    gnh->gnh_bd, gnh->gnh_rd, SCLASS_INVALID,
                                     GBP_ENDPOINT_FLAG_NONE, NULL, NULL,
                                     &gnh->gnh_ge);
 
@@ -414,8 +433,9 @@ gbp_contract_mk_lbs (index_t * guis)
 }
 
 int
-gbp_contract_update (epg_id_t src_epg,
-                    epg_id_t dst_epg, u32 acl_index, index_t * rules)
+gbp_contract_update (sclass_t sclass,
+                    sclass_t dclass,
+                    u32 acl_index, index_t * rules, u16 * allowed_ethertypes)
 {
   gbp_main_t *gm = &gbp_main;
   u32 *acl_vec = NULL;
@@ -424,8 +444,8 @@ gbp_contract_update (epg_id_t src_epg,
   uword *p;
 
   gbp_contract_key_t key = {
-    .gck_src = src_epg,
-    .gck_dst = dst_epg,
+    .gck_src = sclass,
+    .gck_dst = dclass,
   };
 
   if (~0 == gm->gbp_acl_user_id)
@@ -443,6 +463,7 @@ gbp_contract_update (epg_id_t src_epg,
       gbp_contract_rules_free (gc->gc_rules);
       gbp_main.acl_plugin.put_lookup_context_index (gc->gc_lc_index);
       gc->gc_rules = NULL;
+      vec_free (gc->gc_allowed_ethertypes);
     }
   else
     {
@@ -455,13 +476,14 @@ gbp_contract_update (epg_id_t src_epg,
   GBP_CONTRACT_DBG ("update: %U", format_gbp_contract, gci);
 
   gc->gc_rules = rules;
+  gc->gc_allowed_ethertypes = allowed_ethertypes;
   gbp_contract_resolve (gc->gc_rules);
   gbp_contract_mk_lbs (gc->gc_rules);
 
   gc->gc_acl_index = acl_index;
   gc->gc_lc_index =
     gm->acl_plugin.get_lookup_context_index (gm->gbp_acl_user_id,
-                                            src_epg, dst_epg);
+                                            sclass, dclass);
 
   vec_add1 (acl_vec, gc->gc_acl_index);
   gm->acl_plugin.set_acl_vec_for_context (gc->gc_lc_index, acl_vec);
@@ -471,11 +493,11 @@ gbp_contract_update (epg_id_t src_epg,
 }
 
 int
-gbp_contract_delete (epg_id_t src_epg, epg_id_t dst_epg)
+gbp_contract_delete (sclass_t sclass, sclass_t dclass)
 {
   gbp_contract_key_t key = {
-    .gck_src = src_epg,
-    .gck_dst = dst_epg,
+    .gck_src = sclass,
+    .gck_dst = dclass,
   };
   gbp_contract_t *gc;
   uword *p;
@@ -487,6 +509,7 @@ gbp_contract_delete (epg_id_t src_epg, epg_id_t dst_epg)
 
       gbp_contract_rules_free (gc->gc_rules);
       gbp_main.acl_plugin.put_lookup_context_index (gc->gc_lc_index);
+      vec_free (gc->gc_allowed_ethertypes);
 
       hash_unset (gbp_contract_db.gc_hash, key.as_u32);
       pool_put (gbp_contract_pool, gc);
@@ -515,7 +538,7 @@ static clib_error_t *
 gbp_contract_cli (vlib_main_t * vm,
                  unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  epg_id_t src_epg_id = EPG_INVALID, dst_epg_id = EPG_INVALID;
+  sclass_t sclass = SCLASS_INVALID, dclass = SCLASS_INVALID;
   u32 acl_index = ~0;
   u8 add = 1;
 
@@ -525,9 +548,9 @@ gbp_contract_cli (vlib_main_t * vm,
        add = 1;
       else if (unformat (input, "del"))
        add = 0;
-      else if (unformat (input, "src-epg %d", &src_epg_id))
+      else if (unformat (input, "src-epg %d", &sclass))
        ;
-      else if (unformat (input, "dst-epg %d", &dst_epg_id))
+      else if (unformat (input, "dst-epg %d", &dclass))
        ;
       else if (unformat (input, "acl-index %d", &acl_index))
        ;
@@ -535,18 +558,18 @@ gbp_contract_cli (vlib_main_t * vm,
        break;
     }
 
-  if (EPG_INVALID == src_epg_id)
+  if (SCLASS_INVALID == sclass)
     return clib_error_return (0, "Source EPG-ID must be specified");
-  if (EPG_INVALID == dst_epg_id)
+  if (SCLASS_INVALID == dclass)
     return clib_error_return (0, "Destination EPG-ID must be specified");
 
   if (add)
     {
-      gbp_contract_update (src_epg_id, dst_epg_id, acl_index, NULL);
+      gbp_contract_update (sclass, dclass, acl_index, NULL, NULL);
     }
   else
     {
-      gbp_contract_delete (src_epg_id, dst_epg_id);
+      gbp_contract_delete (sclass, dclass);
     }
 
   return (NULL);
@@ -585,17 +608,27 @@ format_gbp_contract (u8 * s, va_list * args)
   index_t gci = va_arg (*args, index_t);
   gbp_contract_t *gc;
   index_t *gui;
+  u16 *et;
 
   gc = gbp_contract_get (gci);
 
-  s = format (s, "%U: acl-index:%d",
-             format_gbp_contract_key, &gc->gc_key, gc->gc_acl_index);
+  s = format (s, "[%d] %U: acl-index:%d",
+             gci, format_gbp_contract_key, &gc->gc_key, gc->gc_acl_index);
 
   vec_foreach (gui, gc->gc_rules)
   {
     s = format (s, "\n    %d: %U", *gui, format_gbp_rule, *gui);
   }
 
+  s = format (s, "\n    allowed-ethertypes:[");
+  vec_foreach (et, gc->gc_allowed_ethertypes)
+  {
+    int host_et = clib_net_to_host_u16 (*et);
+    if (0 != host_et)
+      s = format (s, "0x%x, ", host_et);
+  }
+  s = format (s, "]");
+
   return (s);
 }
 
@@ -603,14 +636,47 @@ static clib_error_t *
 gbp_contract_show (vlib_main_t * vm,
                   unformat_input_t * input, vlib_cli_command_t * cmd)
 {
+  gbp_contract_t *gc;
+  u32 src, dst;
   index_t gci;
 
+  src = dst = SCLASS_INVALID;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "src %d", &src))
+       ;
+      else if (unformat (input, "dst %d", &dst))
+       ;
+      else
+       break;
+    }
+
   vlib_cli_output (vm, "Contracts:");
 
   /* *INDENT-OFF* */
-  pool_foreach_index (gci, gbp_contract_pool,
+  pool_foreach (gc, gbp_contract_pool,
   ({
-    vlib_cli_output (vm, "  [%d] %U", gci, format_gbp_contract, gci);
+    gci = gc - gbp_contract_pool;
+
+    if (SCLASS_INVALID != src && SCLASS_INVALID != dst)
+      {
+        if (gc->gc_key.gck_src == src &&
+            gc->gc_key.gck_dst == dst)
+          vlib_cli_output (vm, "  %U", format_gbp_contract, gci);
+      }
+    else if (SCLASS_INVALID != src)
+      {
+        if (gc->gc_key.gck_src == src)
+          vlib_cli_output (vm, "  %U", format_gbp_contract, gci);
+      }
+    else if (SCLASS_INVALID != dst)
+      {
+        if (gc->gc_key.gck_dst == dst)
+          vlib_cli_output (vm, "  %U", format_gbp_contract, gci);
+      }
+    else
+      vlib_cli_output (vm, "  %U", format_gbp_contract, gci);
   }));
   /* *INDENT-ON* */
 
@@ -627,7 +693,7 @@ gbp_contract_show (vlib_main_t * vm,
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
   .path = "show gbp contract",
-  .short_help = "show gbp contract\n",
+  .short_help = "show gbp contract [src <SRC>] [dst <DST>]\n",
   .function = gbp_contract_show,
 };
 /* *INDENT-ON* */