acl: implement counters
[vpp.git] / src / plugins / acl / acl.c
index dbc658b..24dd53b 100644 (file)
@@ -85,13 +85,15 @@ _(MACIP_ACL_DUMP, macip_acl_dump) \
 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
-_(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
+_(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump) \
+_(ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES,acl_plugin_get_conn_table_max_entries) \
+_(ACL_STATS_INTF_COUNTERS_ENABLE, acl_stats_intf_counters_enable)
 
 
 /* *INDENT-OFF* */
 VLIB_PLUGIN_REGISTER () = {
     .version = VPP_BUILD_VER,
-    .description = "Access Control Lists",
+    .description = "Access Control Lists (ACL)",
 };
 /* *INDENT-ON* */
 
@@ -259,14 +261,15 @@ acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
                 acl_main_t * am, int acl_index)
 {
   acl_rule_t *r;
+  acl_rule_t *acl_rules = am->acls[acl_index].rules;
   u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
-                    am->acls[acl_index].count, am->acls[acl_index].tag);
+                    vec_len (acl_rules), am->acls[acl_index].tag);
   int j;
   vpr (vm, out0);
-  for (j = 0; j < am->acls[acl_index].count; j++)
+  for (j = 0; j < vec_len (acl_rules); j++)
     {
-      r = &am->acls[acl_index].rules[j];
-      out0 = format (out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
+      r = &acl_rules[j];
+      out0 = format (out0, "  %9d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
       out0 = format_acl_action (out0, r->is_permit);
       out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
                     r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
@@ -296,6 +299,30 @@ acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
     }
 }
 
+static void
+  vl_api_acl_plugin_get_conn_table_max_entries_t_handler
+  (vl_api_acl_plugin_get_conn_table_max_entries_t * mp)
+{
+  acl_main_t *am = &acl_main;
+  vl_api_acl_plugin_get_conn_table_max_entries_reply_t *rmp;
+  int msg_size = sizeof (*rmp);
+  vl_api_registration_t *rp;
+
+  rp = vl_api_client_index_to_registration (mp->client_index);
+  if (rp == 0)
+    return;
+
+  rmp = vl_msg_api_alloc (msg_size);
+  memset (rmp, 0, msg_size);
+  rmp->_vl_msg_id =
+    ntohs (VL_API_ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES_REPLY +
+          am->msg_id_base);
+  rmp->context = mp->context;
+  rmp->conn_table_max_entries = __bswap_64 (am->fa_conn_table_max_entries);
+
+  vl_api_send_msg (rp, (u8 *) rmp);
+}
+
 static void
 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
 {
@@ -347,6 +374,33 @@ policy_notify_acl_change (acl_main_t * am, u32 acl_num)
 }
 
 
+static void
+validate_and_reset_acl_counters (acl_main_t * am, u32 acl_index)
+{
+  int i;
+  /* counters are set as vectors [acl#] pointing to vectors of [acl rule] */
+  acl_plugin_counter_lock (am);
+
+  int old_len = vec_len (am->combined_acl_counters);
+
+  vec_validate (am->combined_acl_counters, acl_index);
+
+  for (i = old_len; i < vec_len (am->combined_acl_counters); i++)
+    {
+      am->combined_acl_counters[i].name = 0;
+      /* filled in once only */
+      am->combined_acl_counters[i].stat_segment_name = (void *)
+       format (0, "/acl/%d/matches%c", i, 0);
+      clib_warning ("add stats segment: %s",
+                   am->combined_acl_counters[i].stat_segment_name);
+      i32 rule_count = vec_len (am->acls[acl_index].rules);
+      /* Validate one extra so we always have at least one counter for an ACL */
+      vlib_validate_combined_counter (&am->combined_acl_counters[i],
+                                     rule_count);
+      vlib_zero_combined_counter (&am->combined_acl_counters[i], rule_count);
+    }
+  acl_plugin_counter_unlock (am);
+}
 
 static int
 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
@@ -430,7 +484,6 @@ acl_add_list (u32 count, vl_api_acl_rule_t rules[],
        vec_free (a->rules);
     }
   a->rules = acl_new_rules;
-  a->count = count;
   memcpy (a->tag, tag, sizeof (a->tag));
   if (am->trace_acl > 255)
     warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
@@ -440,6 +493,11 @@ acl_add_list (u32 count, vl_api_acl_rule_t rules[],
       policy_notify_acl_change (am, *acl_list_index);
     }
 
+  /* stats segment expects global heap, so restore it temporarily */
+  clib_mem_set_heap (oldheap);
+  validate_and_reset_acl_counters (am, *acl_list_index);
+  oldheap = acl_set_heap (am);
+
   /* notify the lookup contexts about the ACL changes */
   acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
   clib_mem_set_heap (oldheap);
@@ -636,6 +694,16 @@ acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
   return rv;
 }
 
+static int
+acl_stats_intf_counters_enable_disable (acl_main_t * am, int enable_disable)
+{
+  int rv = 0;
+
+  am->interface_acl_counters_enabled = enable_disable;
+
+  return rv;
+}
+
 static int
 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
                                    int is_input, int enable_disable)
@@ -764,10 +832,6 @@ acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
       u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
       if (~0 == lc_index)
        {
-         if (~0 == am->interface_acl_user_id)
-           am->interface_acl_user_id =
-             acl_plugin.register_user_module ("interface ACL", "sw_if_index",
-                                              "is_input");
          lc_index =
            acl_plugin.get_lookup_context_index (am->interface_acl_user_id,
                                                 sw_if_index, is_input);
@@ -1872,6 +1936,21 @@ vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
 }
 
+
+static void
+  vl_api_acl_stats_intf_counters_enable_t_handler
+  (vl_api_acl_stats_intf_counters_enable_t * mp)
+{
+  acl_main_t *am = &acl_main;
+  vl_api_acl_stats_intf_counters_enable_reply_t *rmp;
+  int rv;
+
+  rv = acl_stats_intf_counters_enable_disable (am, ntohl (mp->enable));
+
+  REPLY_MACRO (VL_API_ACL_DEL_REPLY);
+}
+
+
 static void
 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
 {
@@ -1976,7 +2055,8 @@ send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
   vl_api_acl_details_t *mp;
   vl_api_acl_rule_t *rules;
   int i;
-  int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
+  acl_rule_t *acl_rules = acl->rules;
+  int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * vec_len (acl_rules);
   void *oldheap = acl_set_heap (am);
 
   mp = vl_msg_api_alloc (msg_size);
@@ -1985,14 +2065,14 @@ send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
 
   /* fill in the message */
   mp->context = context;
-  mp->count = htonl (acl->count);
+  mp->count = htonl (vec_len (acl_rules));
   mp->acl_index = htonl (acl - am->acls);
   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
   rules = mp->r;
-  for (i = 0; i < acl->count; i++)
+  for (i = 0; i < vec_len (acl_rules); i++)
     {
-      copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
+      copy_acl_rule_to_api_rule (&rules[i], &acl_rules[i]);
     }
 
   clib_mem_set_heap (oldheap);
@@ -3368,6 +3448,8 @@ acl_show_aclplugin_tables_fn (vlib_main_t * vm,
       show_applied_info = 1;
       show_bihash = 1;
     }
+  vlib_cli_output (vm, "Stats counters enabled for interface ACLs: %d",
+                  acl_main.interface_acl_counters_enabled);
   if (show_mask_type)
     acl_plugin_show_tables_mask_type ();
   if (show_acl_hash_info)
@@ -3470,12 +3552,12 @@ acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
 {
   acl_main_t *am = &acl_main;
   u32 conn_table_hash_buckets;
-  u32 conn_table_hash_memory_size;
+  uword conn_table_hash_memory_size;
   u32 conn_table_max_entries;
   uword main_heap_size;
   uword hash_heap_size;
   u32 hash_lookup_hash_buckets;
-  u32 hash_lookup_hash_memory;
+  uword hash_lookup_hash_memory;
   u32 reclassify_sessions;
   u32 use_tuple_merge;
   u32 tuple_merge_split_threshold;
@@ -3485,8 +3567,10 @@ acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
       if (unformat
          (input, "connection hash buckets %d", &conn_table_hash_buckets))
        am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
-      else if (unformat (input, "connection hash memory %d",
-                        &conn_table_hash_memory_size))
+      else
+       if (unformat
+           (input, "connection hash memory %U", unformat_memory_size,
+            &conn_table_hash_memory_size))
        am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
       else if (unformat (input, "connection count max %d",
                         &conn_table_max_entries))
@@ -3504,8 +3588,10 @@ acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
       else if (unformat (input, "hash lookup hash buckets %d",
                         &hash_lookup_hash_buckets))
        am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
-      else if (unformat (input, "hash lookup hash memory %d",
-                        &hash_lookup_hash_memory))
+      else
+       if (unformat
+           (input, "hash lookup hash memory %U", unformat_memory_size,
+            &hash_lookup_hash_memory))
        am->hash_lookup_hash_memory = hash_lookup_hash_memory;
       else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
        am->use_tuple_merge = use_tuple_merge;
@@ -3629,7 +3715,13 @@ acl_init (vlib_main_t * vm)
   /* Set the default threshold */
   am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
 
-  am->interface_acl_user_id = ~0;      /* defer till the first use */
+  am->interface_acl_user_id =
+    acl_plugin.register_user_module ("interface ACL", "sw_if_index",
+                                    "is_input");
+
+  am->acl_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
+                                                CLIB_CACHE_LINE_BYTES);
+  am->acl_counter_lock[0] = 0; /* should be no need */
 
   return error;
 }