X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fplugins%2Facl%2Facl.c;h=dc9caa6617795600d9b61b8dfb5995affc333daf;hb=33a58171e;hp=1e040b6a0363b532cec10a04db7494a3e45fcd9e;hpb=bb5d22dafb394c6a619894bb803f770266f223f0;p=vpp.git diff --git a/src/plugins/acl/acl.c b/src/plugins/acl/acl.c index 1e040b6a036..dc9caa66177 100644 --- a/src/plugins/acl/acl.c +++ b/src/plugins/acl/acl.c @@ -86,13 +86,14 @@ _(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_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES,acl_plugin_get_conn_table_max_entries) +_(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* */ @@ -260,13 +261,14 @@ 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]; + 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, @@ -304,13 +306,11 @@ static void acl_main_t *am = &acl_main; vl_api_acl_plugin_get_conn_table_max_entries_reply_t *rmp; int msg_size = sizeof (*rmp); - unix_shared_memory_queue_t *q; + vl_api_registration_t *rp; - q = vl_api_client_index_to_input_queue (mp->client_index); - if (q == 0) - { - return; - } + 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); @@ -320,7 +320,7 @@ static void rmp->context = mp->context; rmp->conn_table_max_entries = __bswap_64 (am->fa_conn_table_max_entries); - vl_msg_api_send_shmem (q, (u8 *) & rmp); + vl_api_send_msg (rp, (u8 *) rmp); } static void @@ -374,6 +374,82 @@ 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); + i32 rule_count = vec_len (am->acls[i].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_clear_combined_counters (&am->combined_acl_counters[i]); + } + + /* (re)validate for the actual ACL that is getting added/updated */ + 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[acl_index], + rule_count); + vlib_clear_combined_counters (&am->combined_acl_counters[acl_index]); + acl_plugin_counter_unlock (am); +} + +static int +acl_api_ip4_invalid_prefix (void *ip4_pref_raw, u8 ip4_prefix_len) +{ + ip4_address_t ip4_addr; + ip4_address_t ip4_mask; + ip4_address_t ip4_masked_addr; + + memcpy (&ip4_addr, ip4_pref_raw, sizeof (ip4_addr)); + ip4_preflen_to_mask (ip4_prefix_len, &ip4_mask); + ip4_masked_addr.as_u32 = ip4_addr.as_u32 & ip4_mask.as_u32; + int ret = (ip4_masked_addr.as_u32 != ip4_addr.as_u32); + if (ret) + { + clib_warning + ("inconsistent addr %U for prefix len %d; (%U when masked)", + format_ip4_address, ip4_pref_raw, ip4_prefix_len, format_ip4_address, + &ip4_masked_addr); + } + return ret; +} + +static int +acl_api_ip6_invalid_prefix (void *ip6_pref_raw, u8 ip6_prefix_len) +{ + ip6_address_t ip6_addr; + ip6_address_t ip6_mask; + ip6_address_t ip6_masked_addr; + + memcpy (&ip6_addr, ip6_pref_raw, sizeof (ip6_addr)); + ip6_preflen_to_mask (ip6_prefix_len, &ip6_mask); + ip6_masked_addr.as_u64[0] = ip6_addr.as_u64[0] & ip6_mask.as_u64[0]; + ip6_masked_addr.as_u64[1] = ip6_addr.as_u64[1] & ip6_mask.as_u64[1]; + int ret = ((ip6_masked_addr.as_u64[0] != ip6_addr.as_u64[0]) + || (ip6_masked_addr.as_u64[1] != ip6_addr.as_u64[1])); + if (ret) + { + clib_warning + ("inconsistent addr %U for prefix len %d; (%U when masked)", + format_ip6_address, ip6_pref_raw, ip6_prefix_len, format_ip6_address, + &ip6_masked_addr); + } + return ret; +} static int acl_add_list (u32 count, vl_api_acl_rule_t rules[], @@ -389,6 +465,43 @@ acl_add_list (u32 count, vl_api_acl_rule_t rules[], clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index, tag); + /* check if what they request is consistent */ + for (i = 0; i < count; i++) + { + if (rules[i].is_ipv6) + { + if (rules[i].src_ip_prefix_len > 128) + return VNET_API_ERROR_INVALID_VALUE; + if (rules[i].dst_ip_prefix_len > 128) + return VNET_API_ERROR_INVALID_VALUE; + if (acl_api_ip6_invalid_prefix + (&rules[i].src_ip_addr, rules[i].src_ip_prefix_len)) + return VNET_API_ERROR_INVALID_SRC_ADDRESS; + if (acl_api_ip6_invalid_prefix + (&rules[i].dst_ip_addr, rules[i].dst_ip_prefix_len)) + return VNET_API_ERROR_INVALID_DST_ADDRESS; + } + else + { + if (rules[i].src_ip_prefix_len > 32) + return VNET_API_ERROR_INVALID_VALUE; + if (rules[i].dst_ip_prefix_len > 32) + return VNET_API_ERROR_INVALID_VALUE; + if (acl_api_ip4_invalid_prefix + (&rules[i].src_ip_addr, rules[i].src_ip_prefix_len)) + return VNET_API_ERROR_INVALID_SRC_ADDRESS; + if (acl_api_ip4_invalid_prefix + (&rules[i].dst_ip_addr, rules[i].dst_ip_prefix_len)) + return VNET_API_ERROR_INVALID_DST_ADDRESS; + } + if (ntohs (rules[i].srcport_or_icmptype_first) > + ntohs (rules[i].srcport_or_icmptype_last)) + return VNET_API_ERROR_INVALID_VALUE_2; + if (ntohs (rules[i].dstport_or_icmpcode_first) > + ntohs (rules[i].dstport_or_icmpcode_last)) + return VNET_API_ERROR_INVALID_VALUE_2; + } + if (*acl_list_index != ~0) { /* They supplied some number, let's see if this ACL exists */ @@ -457,7 +570,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); @@ -467,6 +579,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); @@ -663,6 +780,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) @@ -791,10 +918,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); @@ -1899,6 +2022,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, 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) { @@ -2003,7 +2141,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); @@ -2012,14 +2151,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); @@ -3395,6 +3534,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) @@ -3660,7 +3801,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; }