api: refactor format_vl_api_prefix_t return keys
[vpp.git] / src / plugins / gbp / gbp_contract.c
index 805e9b2..552201a 100644 (file)
@@ -40,6 +40,17 @@ gbp_next_hop_t *gbp_next_hop_pool;
 #define GBP_CONTRACT_DBG(...)                           \
     vlib_log_notice (gc_logger, __VA_ARGS__);
 
+/* Adjacency packet/byte counters indexed by adjacency index. */
+vlib_combined_counter_main_t gbp_contract_permit_counters = {
+  .name = "gbp-contracts-permit",
+  .stat_segment_name = "/net/gbp/contract/permit",
+};
+
+vlib_combined_counter_main_t gbp_contract_drop_counters = {
+  .name = "gbp-contracts-drop",
+  .stat_segment_name = "/net/gbp/contract/drop",
+};
+
 index_t
 gbp_rule_alloc (gbp_rule_action_t action,
                gbp_hash_mode_t hash_mode, index_t * nhs)
@@ -435,7 +446,9 @@ gbp_contract_mk_lbs (index_t * guis)
 int
 gbp_contract_update (sclass_t sclass,
                     sclass_t dclass,
-                    u32 acl_index, index_t * rules, u16 * allowed_ethertypes)
+                    u32 acl_index,
+                    index_t * rules,
+                    u16 * allowed_ethertypes, u32 * stats_index)
 {
   gbp_main_t *gm = &gbp_main;
   u32 *acl_vec = NULL;
@@ -471,6 +484,11 @@ gbp_contract_update (sclass_t sclass,
       gc->gc_key = key;
       gci = gc - gbp_contract_pool;
       hash_set (gbp_contract_db.gc_hash, key.as_u32, gci);
+
+      vlib_validate_combined_counter (&gbp_contract_drop_counters, gci);
+      vlib_zero_combined_counter (&gbp_contract_drop_counters, gci);
+      vlib_validate_combined_counter (&gbp_contract_permit_counters, gci);
+      vlib_zero_combined_counter (&gbp_contract_permit_counters, gci);
     }
 
   GBP_CONTRACT_DBG ("update: %U", format_gbp_contract, gci);
@@ -489,6 +507,8 @@ gbp_contract_update (sclass_t sclass,
   gm->acl_plugin.set_acl_vec_for_context (gc->gc_lc_index, acl_vec);
   vec_free (acl_vec);
 
+  *stats_index = gci;
+
   return (0);
 }
 
@@ -539,7 +559,7 @@ gbp_contract_cli (vlib_main_t * vm,
                  unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   sclass_t sclass = SCLASS_INVALID, dclass = SCLASS_INVALID;
-  u32 acl_index = ~0;
+  u32 acl_index = ~0, stats_index;
   u8 add = 1;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
@@ -565,7 +585,8 @@ gbp_contract_cli (vlib_main_t * vm,
 
   if (add)
     {
-      gbp_contract_update (sclass, dclass, acl_index, NULL, NULL);
+      gbp_contract_update (sclass, dclass, acl_index,
+                          NULL, NULL, &stats_index);
     }
   else
     {
@@ -606,14 +627,15 @@ u8 *
 format_gbp_contract (u8 * s, va_list * args)
 {
   index_t gci = va_arg (*args, index_t);
+  vlib_counter_t counts;
   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)
   {
@@ -624,8 +646,15 @@ format_gbp_contract (u8 * s, va_list * args)
   vec_foreach (et, gc->gc_allowed_ethertypes)
   {
     int host_et = clib_net_to_host_u16 (*et);
-    s = format (s, "0x%x, ", host_et);
+    if (0 != host_et)
+      s = format (s, "0x%x, ", host_et);
   }
+
+  vlib_get_combined_counter (&gbp_contract_drop_counters, gci, &counts);
+  s = format (s, "\n   drop:[%Ld:%Ld]", counts.packets, counts.bytes);
+  vlib_get_combined_counter (&gbp_contract_permit_counters, gci, &counts);
+  s = format (s, "\n   permit:[%Ld:%Ld]", counts.packets, counts.bytes);
+
   s = format (s, "]");
 
   return (s);
@@ -635,14 +664,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* */
 
@@ -659,7 +721,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* */