2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/l2/l2_in_out_feat_arc.h>
24 #include <vnet/classify/in_out_acl.h>
25 #include <vpp/app/version.h>
27 #include <vnet/ethernet/ethernet_types_api.h>
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
32 /* define message IDs */
33 #include <acl/acl.api_enum.h>
34 #include <acl/acl.api_types.h>
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #include "manual_fns.h"
40 #include "public_inlines.h"
44 #define REPLY_MSG_ID_BASE am->msg_id_base
45 #include <vlibapi/api_helper_macros.h>
48 * The code for the bihash, used by the session management.
50 #include <vppinfra/bihash_40_8.h>
51 #include <vppinfra/bihash_template.h>
52 #include <vppinfra/bihash_template.c>
55 VLIB_PLUGIN_REGISTER () = {
56 .version = VPP_BUILD_VER,
57 .description = "Access Control Lists (ACL)",
61 /* methods exported from ACL-as-a-service */
62 static acl_plugin_methods_t acl_plugin;
66 format_vec16 (u8 * s, va_list * va)
68 u16 *v = va_arg (*va, u16 *);
69 char *fmt = va_arg (*va, char *);
71 for (i = 0; i < vec_len (v); i++)
75 s = format (s, fmt, v[i]);
81 acl_set_heap (acl_main_t * am)
83 if (0 == am->acl_mheap)
85 if (0 == am->acl_mheap_size)
87 vlib_thread_main_t *tm = vlib_get_thread_main ();
88 u64 per_worker_slack = 1000000LL;
91 ((u64) am->fa_conn_table_max_entries) * sizeof (fa_session_t);
92 u64 per_worker_size_with_slack = per_worker_slack + per_worker_size;
93 u64 main_slack = 2000000LL;
94 u64 bihash_size = (u64) am->fa_conn_table_hash_memory_size;
97 per_worker_size_with_slack * tm->n_vlib_mains + bihash_size +
100 u64 max_possible = ((uword) ~ 0);
101 if (am->acl_mheap_size > max_possible)
103 clib_warning ("ACL heap size requested: %lld, max possible %lld",
104 am->acl_mheap_size, max_possible);
107 am->acl_mheap = mheap_alloc_with_lock (0 /* use VM */ ,
110 if (0 == am->acl_mheap)
113 ("ACL plugin failed to allocate main heap of %U bytes, abort",
114 format_memory_size, am->acl_mheap_size);
117 void *oldheap = clib_mem_set_heap (am->acl_mheap);
122 acl_plugin_set_heap ()
124 acl_main_t *am = &acl_main;
125 return acl_set_heap (am);
129 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
131 clib_mem_set_heap (acl_set_heap (am));
135 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
137 clib_mem_set_heap (acl_set_heap (am));
141 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
143 acl_main_t *am = &acl_main;
144 vl_api_acl_plugin_get_version_reply_t *rmp;
145 int msg_size = sizeof (*rmp);
146 vl_api_registration_t *reg;
148 reg = vl_api_client_index_to_registration (mp->client_index);
152 rmp = vl_msg_api_alloc (msg_size);
153 clib_memset (rmp, 0, msg_size);
155 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
156 rmp->context = mp->context;
157 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
158 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
160 vl_api_send_msg (reg, (u8 *) rmp);
164 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
167 vl_api_acl_plugin_control_ping_reply_t *rmp;
168 acl_main_t *am = &acl_main;
172 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
174 rmp->vpe_pid = ntohl (getpid ());
180 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
182 clib_warning ("%v", out0);
183 vec_reset_length (out0);
187 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
189 vlib_cli_output (vm, "%v", out0);
190 vec_reset_length (out0);
193 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
196 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
197 acl_main_t * am, int acl_index)
200 acl_rule_t *acl_rules = am->acls[acl_index].rules;
201 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
202 vec_len (acl_rules), am->acls[acl_index].tag);
205 for (j = 0; j < vec_len (acl_rules); j++)
208 out0 = format (out0, " %9d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
209 out0 = format_acl_action (out0, r->is_permit);
210 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
211 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
214 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
215 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
216 out0 = format (out0, " proto %d", r->proto);
217 out0 = format (out0, " sport %d", r->src_port_or_type_first);
218 if (r->src_port_or_type_first != r->src_port_or_type_last)
220 out0 = format (out0, "-%d", r->src_port_or_type_last);
222 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
223 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
225 out0 = format (out0, "-%d", r->dst_port_or_code_last);
227 if (r->tcp_flags_mask || r->tcp_flags_value)
230 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
233 out0 = format (out0, "\n");
239 vl_api_acl_plugin_get_conn_table_max_entries_t_handler
240 (vl_api_acl_plugin_get_conn_table_max_entries_t * mp)
242 acl_main_t *am = &acl_main;
243 vl_api_acl_plugin_get_conn_table_max_entries_reply_t *rmp;
244 int msg_size = sizeof (*rmp);
245 vl_api_registration_t *rp;
247 rp = vl_api_client_index_to_registration (mp->client_index);
251 rmp = vl_msg_api_alloc (msg_size);
252 memset (rmp, 0, msg_size);
254 ntohs (VL_API_ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES_REPLY +
256 rmp->context = mp->context;
257 rmp->conn_table_max_entries = __bswap_64 (am->fa_conn_table_max_entries);
259 vl_api_send_msg (rp, (u8 *) rmp);
263 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
265 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
269 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
271 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
275 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
278 u32 **ppolicy_epoch_by_swi =
279 is_input ? &am->input_policy_epoch_by_sw_if_index :
280 &am->output_policy_epoch_by_sw_if_index;
281 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
283 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
285 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
286 (is_input * FA_POLICY_EPOCH_IS_INPUT);
290 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
292 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
293 : &am->output_sw_if_index_vec_by_acl;
294 if (acl_num < vec_len (*p_swi_vec_by_acl))
297 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
299 increment_policy_epoch (am, *p_swi, is_input);
306 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
308 try_increment_acl_policy_epoch (am, acl_num, 0);
309 try_increment_acl_policy_epoch (am, acl_num, 1);
314 validate_and_reset_acl_counters (acl_main_t * am, u32 acl_index)
317 /* counters are set as vectors [acl#] pointing to vectors of [acl rule] */
318 acl_plugin_counter_lock (am);
320 int old_len = vec_len (am->combined_acl_counters);
322 vec_validate (am->combined_acl_counters, acl_index);
324 for (i = old_len; i < vec_len (am->combined_acl_counters); i++)
326 am->combined_acl_counters[i].name = 0;
327 /* filled in once only */
328 am->combined_acl_counters[i].stat_segment_name = (void *)
329 format (0, "/acl/%d/matches%c", i, 0);
330 i32 rule_count = vec_len (am->acls[i].rules);
331 /* Validate one extra so we always have at least one counter for an ACL */
332 vlib_validate_combined_counter (&am->combined_acl_counters[i],
334 vlib_clear_combined_counters (&am->combined_acl_counters[i]);
337 /* (re)validate for the actual ACL that is getting added/updated */
338 i32 rule_count = vec_len (am->acls[acl_index].rules);
339 /* Validate one extra so we always have at least one counter for an ACL */
340 vlib_validate_combined_counter (&am->combined_acl_counters[acl_index],
342 vlib_clear_combined_counters (&am->combined_acl_counters[acl_index]);
343 acl_plugin_counter_unlock (am);
347 acl_api_ip4_invalid_prefix (const vl_api_prefix_t * prefix)
349 ip4_address_t ip4_addr;
350 ip4_address_t ip4_mask;
351 ip4_address_t ip4_masked_addr;
353 if (prefix->len > 32)
356 ip4_address_decode (prefix->address.un.ip4, &ip4_addr);
357 ip4_preflen_to_mask (prefix->len, &ip4_mask);
358 ip4_masked_addr.as_u32 = ip4_addr.as_u32 & ip4_mask.as_u32;
359 int ret = (ip4_masked_addr.as_u32 != ip4_addr.as_u32);
363 ("inconsistent addr %U for prefix len %d; (%U when masked)",
364 format_ip4_address, prefix->address.un.ip4, prefix->len,
365 format_ip4_address, &ip4_masked_addr);
371 acl_api_ip6_invalid_prefix (const vl_api_prefix_t * prefix)
373 ip6_address_t ip6_addr;
374 ip6_address_t ip6_mask;
375 ip6_address_t ip6_masked_addr;
377 if (prefix->len > 128)
380 ip6_address_decode (prefix->address.un.ip6, &ip6_addr);
381 ip6_preflen_to_mask (prefix->len, &ip6_mask);
382 ip6_masked_addr.as_u64[0] = ip6_addr.as_u64[0] & ip6_mask.as_u64[0];
383 ip6_masked_addr.as_u64[1] = ip6_addr.as_u64[1] & ip6_mask.as_u64[1];
384 int ret = ((ip6_masked_addr.as_u64[0] != ip6_addr.as_u64[0])
385 || (ip6_masked_addr.as_u64[1] != ip6_addr.as_u64[1]));
389 ("inconsistent addr %U for prefix len %d; (%U when masked)",
390 format_ip6_address, prefix->address.un.ip6, prefix->len,
391 format_ip6_address, &ip6_masked_addr);
397 acl_api_invalid_prefix (const vl_api_prefix_t * prefix)
399 if (prefix->address.af == ADDRESS_IP6)
400 return acl_api_ip6_invalid_prefix (prefix);
401 return acl_api_ip4_invalid_prefix (prefix);
405 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
406 u32 * acl_list_index, u8 * tag)
408 acl_main_t *am = &acl_main;
411 acl_rule_t *acl_new_rules = 0;
414 if (am->trace_acl > 255)
415 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
418 /* check if what they request is consistent */
419 for (i = 0; i < count; i++)
421 if (acl_api_invalid_prefix (&rules[i].src_prefix))
422 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
423 if (acl_api_invalid_prefix (&rules[i].dst_prefix))
424 return VNET_API_ERROR_INVALID_DST_ADDRESS;
425 if (ntohs (rules[i].srcport_or_icmptype_first) >
426 ntohs (rules[i].srcport_or_icmptype_last))
427 return VNET_API_ERROR_INVALID_VALUE_2;
428 if (ntohs (rules[i].dstport_or_icmpcode_first) >
429 ntohs (rules[i].dstport_or_icmpcode_last))
430 return VNET_API_ERROR_INVALID_VALUE_2;
433 if (*acl_list_index != ~0)
435 /* They supplied some number, let's see if this ACL exists */
436 if (pool_is_free_index (am->acls, *acl_list_index))
438 /* tried to replace a non-existent ACL, no point doing anything */
440 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
441 *acl_list_index, tag);
442 return VNET_API_ERROR_NO_SUCH_ENTRY;
448 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
449 *acl_list_index, tag);
452 void *oldheap = acl_set_heap (am);
454 /* Create and populate the rules */
456 vec_validate (acl_new_rules, count - 1);
458 for (i = 0; i < count; i++)
460 r = vec_elt_at_index (acl_new_rules, i);
461 clib_memset (r, 0, sizeof (*r));
462 r->is_permit = rules[i].is_permit;
463 r->is_ipv6 = rules[i].src_prefix.address.af;
464 ip_address_decode (&rules[i].src_prefix.address, &r->src);
465 ip_address_decode (&rules[i].dst_prefix.address, &r->dst);
466 r->src_prefixlen = rules[i].src_prefix.len;
467 r->dst_prefixlen = rules[i].dst_prefix.len;
468 r->proto = rules[i].proto;
469 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
470 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
471 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
472 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
473 r->tcp_flags_value = rules[i].tcp_flags_value;
474 r->tcp_flags_mask = rules[i].tcp_flags_mask;
477 if (~0 == *acl_list_index)
480 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
481 clib_memset (a, 0, sizeof (*a));
482 /* Will return the newly allocated ACL index */
483 *acl_list_index = a - am->acls;
487 a = am->acls + *acl_list_index;
488 /* Get rid of the old rules */
492 a->rules = acl_new_rules;
493 memcpy (a->tag, tag, sizeof (a->tag));
494 if (am->trace_acl > 255)
495 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
496 if (am->reclassify_sessions)
498 /* a change in an ACLs if they are applied may mean a new policy epoch */
499 policy_notify_acl_change (am, *acl_list_index);
502 /* stats segment expects global heap, so restore it temporarily */
503 clib_mem_set_heap (oldheap);
504 validate_and_reset_acl_counters (am, *acl_list_index);
505 oldheap = acl_set_heap (am);
507 /* notify the lookup contexts about the ACL changes */
508 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
509 clib_mem_set_heap (oldheap);
514 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
516 if (acl_index < vec_len (foo_index_vec_by_acl))
518 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
520 /* ACL is applied somewhere. */
528 acl_del_list (u32 acl_list_index)
530 acl_main_t *am = &acl_main;
532 if (pool_is_free_index (am->acls, acl_list_index))
534 return VNET_API_ERROR_NO_SUCH_ENTRY;
536 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
537 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
538 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
539 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
540 /* lookup contexts cover other cases, not just inbound/outbound, so check that */
541 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
542 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
544 void *oldheap = acl_set_heap (am);
546 /* now we can delete the ACL itself */
547 a = pool_elt_at_index (am->acls, acl_list_index);
550 pool_put (am->acls, a);
551 /* acl_list_index is now free, notify the lookup contexts */
552 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
553 clib_mem_set_heap (oldheap);
558 count_skip (u8 * p, u32 size)
560 u64 *p64 = (u64 *) p;
561 /* Be tolerant to null pointer */
565 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
569 return (p64 - (u64 *) p) / 2;
573 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
574 u32 mask_len, u32 next_table_index,
575 u32 miss_next_index, u32 * table_index,
579 u32 memory_size = 2 << 22;
580 u32 skip = count_skip (mask, mask_len);
581 u32 match = (mask_len / 16) - skip;
582 u8 *skip_mask_ptr = mask + 16 * skip;
583 u32 current_data_flag = 0;
584 int current_data_offset = 0;
589 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
590 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
591 memory_size, skip, match,
592 next_table_index, miss_next_index,
593 table_index, current_data_flag,
594 current_data_offset, is_add,
595 1 /* delete_chain */ );
596 clib_mem_set_heap (oldheap);
601 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
604 ? am->input_etype_whitelist_by_sw_if_index
605 : am->output_etype_whitelist_by_sw_if_index;
606 u16 *whitelist = (vec_len (v) > sw_if_index) ? vec_elt (v, sw_if_index) : 0;
607 return vec_len (whitelist) > 0;
611 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
613 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
614 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
615 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
617 clib_mem_set_heap (oldheap);
622 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
628 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
630 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
632 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
636 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
638 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
639 rv = vnet_l2_feature_enable_disable ("l2-input-ip4", "acl-plugin-in-ip4-l2",
640 sw_if_index, enable_disable, 0, 0);
642 clib_error ("Could not enable on input");
643 rv = vnet_l2_feature_enable_disable ("l2-input-ip6", "acl-plugin-in-ip6-l2",
644 sw_if_index, enable_disable, 0, 0);
646 clib_error ("Could not enable on input");
648 if (intf_has_etype_whitelist (am, sw_if_index, 1))
649 vnet_l2_feature_enable_disable ("l2-input-nonip",
650 "acl-plugin-in-nonip-l2", sw_if_index,
651 enable_disable, 0, 0);
653 clib_mem_set_heap (oldheap);
655 am->in_acl_on_sw_if_index =
656 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
662 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
668 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
670 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
672 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
676 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
678 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
680 vnet_l2_feature_enable_disable ("l2-output-ip4", "acl-plugin-out-ip4-l2",
681 sw_if_index, enable_disable, 0, 0);
683 clib_error ("Could not enable on output");
685 vnet_l2_feature_enable_disable ("l2-output-ip6", "acl-plugin-out-ip6-l2",
686 sw_if_index, enable_disable, 0, 0);
688 clib_error ("Could not enable on output");
689 if (intf_has_etype_whitelist (am, sw_if_index, 0))
690 vnet_l2_feature_enable_disable ("l2-output-nonip",
691 "acl-plugin-out-nonip-l2", sw_if_index,
692 enable_disable, 0, 0);
695 clib_mem_set_heap (oldheap);
697 am->out_acl_on_sw_if_index =
698 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
704 acl_stats_intf_counters_enable_disable (acl_main_t * am, int enable_disable)
708 am->interface_acl_counters_enabled = enable_disable;
714 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
715 int is_input, int enable_disable)
718 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
720 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
724 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
726 return (pool_is_free_index (am->acls, acl_list_index));
730 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
731 u8 is_input, u32 * vec_acl_list_index,
732 int *may_clear_sessions)
735 uword *seen_acl_bitmap = 0;
736 uword *old_seen_acl_bitmap = 0;
737 uword *change_acl_bitmap = 0;
742 if (am->trace_acl > 255)
744 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
745 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
747 vec_foreach (pacln, vec_acl_list_index)
749 if (acl_is_not_defined (am, *pacln))
751 /* ACL is not defined. Can not apply */
752 clib_warning ("ERROR: ACL %d not defined", *pacln);
753 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
756 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
758 /* ACL being applied twice within the list. error. */
759 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
760 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
763 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
767 u32 **pinout_lc_index_by_sw_if_index =
769 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
771 u32 ***pinout_acl_vec_by_sw_if_index =
773 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
775 u32 ***pinout_sw_if_index_vec_by_acl =
777 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
779 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
781 clib_bitmap_validate (old_seen_acl_bitmap, 1);
783 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
785 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
788 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
790 if (am->trace_acl > 255)
791 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
792 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
793 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
796 clib_bitmap_foreach(acln, change_acl_bitmap, ({
797 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
798 /* ACL is being removed. */
799 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
800 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
801 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
804 /* ACL is being added. */
805 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
806 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
811 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
812 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
813 vec_dup (vec_acl_list_index);
815 if (am->reclassify_sessions)
817 /* re-applying ACLs means a new policy epoch */
818 increment_policy_epoch (am, sw_if_index, is_input);
822 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
823 if (may_clear_sessions && *may_clear_sessions
824 && !clib_bitmap_is_zero (change_acl_bitmap))
826 acl_clear_sessions (am, sw_if_index);
827 *may_clear_sessions = 0;
832 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
834 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
836 if (vec_len (vec_acl_list_index) > 0)
838 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
842 acl_plugin.get_lookup_context_index (am->interface_acl_user_id,
843 sw_if_index, is_input);
844 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
846 acl_plugin.set_acl_vec_for_context (lc_index, vec_acl_list_index);
850 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
852 acl_plugin.put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
853 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
857 /* ensure ACL processing is enabled/disabled as needed */
858 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
859 vec_len (vec_acl_list_index) > 0);
862 clib_bitmap_free (change_acl_bitmap);
863 clib_bitmap_free (seen_acl_bitmap);
864 clib_bitmap_free (old_seen_acl_bitmap);
869 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
870 int *may_clear_sessions)
872 acl_main_t *am = &acl_main;
873 void *oldheap = acl_set_heap (am);
874 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
876 clib_mem_set_heap (oldheap);
880 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
884 acl_main_t *am = &acl_main;
886 int may_clear_sessions = 1;
888 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
889 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
891 u32 ***pinout_acl_vec_by_sw_if_index =
893 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
895 void *oldheap = acl_set_heap (am);
899 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
900 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
905 rv = error_already_applied;
909 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
910 vec_add1 (acl_vec, acl_list_index);
914 if (sw_if_index >= vec_len (*pinout_acl_vec_by_sw_if_index))
916 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
920 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
925 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
929 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
930 vec_del1 (acl_vec, index);
933 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
934 &may_clear_sessions);
937 clib_mem_set_heap (oldheap);
942 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
945 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
946 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
948 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
949 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
951 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
952 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
955 * if there are already inbound/outbound ACLs applied, toggle the
956 * enable/disable - this will recreate the necessary tables.
959 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
961 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
963 acl_interface_in_enable_disable (am, sw_if_index, 0);
964 acl_interface_in_enable_disable (am, sw_if_index, 1);
967 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
969 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
971 acl_interface_out_enable_disable (am, sw_if_index, 0);
972 acl_interface_out_enable_disable (am, sw_if_index, 1);
988 u32 dot1q_table_index;
989 u32 dot1ad_table_index;
990 u32 arp_dot1q_table_index;
991 u32 arp_dot1ad_table_index;
994 u32 out_arp_table_index;
995 u32 out_dot1q_table_index;
996 u32 out_dot1ad_table_index;
997 u32 out_arp_dot1q_table_index;
998 u32 out_arp_dot1ad_table_index;
999 } macip_match_type_t;
1002 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1008 for (i = 0; i < vec_len (mv); i++)
1010 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1011 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1021 /* Get metric used to sort match types.
1022 The more specific and the more often seen - the bigger the metric */
1024 match_type_metric (macip_match_type_t * m)
1026 unsigned int mac_bits_set = 0;
1027 unsigned int mac_byte;
1029 for (i = 0; i < 6; i++)
1031 mac_byte = m->mac_mask[i];
1032 for (; mac_byte; mac_byte >>= 1)
1033 mac_bits_set += mac_byte & 1;
1036 * Attempt to place the more specific and the more used rules on top.
1037 * There are obvious caveat corner cases to this, but they do not
1038 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1039 * going with a wildcard IPv4 with a specific MAC).
1041 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1045 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1047 /* Ascending sort based on the metric values */
1048 return match_type_metric (m1) - match_type_metric (m2);
1051 /* Get the offset of L3 source within ethernet packet */
1053 get_l3_src_offset (int is6)
1056 return (sizeof (ethernet_header_t) +
1057 offsetof (ip6_header_t, src_address));
1059 return (sizeof (ethernet_header_t) +
1060 offsetof (ip4_header_t, src_address));
1064 get_l3_dst_offset (int is6)
1067 return (sizeof (ethernet_header_t) +
1068 offsetof (ip6_header_t, dst_address));
1070 return (sizeof (ethernet_header_t) +
1071 offsetof (ip4_header_t, dst_address));
1075 * return if the is_permit value also requires to create the egress tables
1076 * For backwards compatibility, we keep the is_permit = 1 to only
1077 * create the ingress tables, and the new value of 3 will also
1078 * create the egress tables based on destination.
1081 macip_permit_also_egress (u8 is_permit)
1083 return (is_permit == 3);
1087 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1089 macip_match_type_t *mvec = NULL;
1090 macip_match_type_t *mt;
1091 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1093 u32 match_type_index;
1097 vnet_classify_main_t *cm = &vnet_classify_main;
1099 /* Count the number of different types of rules */
1100 for (i = 0; i < a->count; i++)
1104 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1105 a->rules[i].src_prefixlen,
1106 a->rules[i].is_ipv6)))
1108 match_type_index = vec_len (mvec);
1109 vec_validate (mvec, match_type_index);
1110 memcpy (mvec[match_type_index].mac_mask,
1111 a->rules[i].src_mac_mask, 6);
1112 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1113 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1114 mvec[match_type_index].has_egress = 0;
1115 mvec[match_type_index].table_index = ~0;
1116 mvec[match_type_index].arp_table_index = ~0;
1117 mvec[match_type_index].dot1q_table_index = ~0;
1118 mvec[match_type_index].dot1ad_table_index = ~0;
1119 mvec[match_type_index].arp_dot1q_table_index = ~0;
1120 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1121 mvec[match_type_index].out_table_index = ~0;
1122 mvec[match_type_index].out_arp_table_index = ~0;
1123 mvec[match_type_index].out_dot1q_table_index = ~0;
1124 mvec[match_type_index].out_dot1ad_table_index = ~0;
1125 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1126 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1128 mvec[match_type_index].count++;
1129 mvec[match_type_index].has_egress |=
1130 macip_permit_also_egress (a->rules[i].is_permit);
1132 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1133 vec_sort_with_function (mvec, match_type_compare);
1134 /* Create the classifier tables */
1136 out_last_table = ~0;
1137 /* First add ARP tables */
1138 vec_foreach (mt, mvec)
1141 int is6 = mt->is_ipv6;
1143 u32 *last_tag_table;
1144 u32 *out_last_tag_table;
1151 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1152 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1153 | Destination Address |
1154 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1156 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1158 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1159 | EtherType | Hardware Type |
1160 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1161 | Protocol Type | Hw addr len | Proto addr len|
1162 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1164 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1165 | Sender Hardware Address |
1166 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1167 | Sender Protocol Address |
1168 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1169 | Target Hardware Address |
1170 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1171 | | TargetProtocolAddress |
1172 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1174 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1176 for (tags = 2; tags >= 0; tags--)
1178 clib_memset (mask, 0, sizeof (mask));
1179 /* source MAC address */
1180 memcpy (&mask[6], mt->mac_mask, 6);
1186 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1188 last_tag_table = &mt->arp_table_index;
1191 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1192 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1194 last_tag_table = &mt->arp_dot1q_table_index;
1197 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1198 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1199 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1201 last_tag_table = &mt->arp_dot1ad_table_index;
1205 /* sender hardware address within ARP */
1206 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1207 /* sender protocol address within ARP */
1208 for (i = 0; i < (mt->prefix_len / 8); i++)
1209 mask[l3_offset + 14 + i] = 0xff;
1210 if (mt->prefix_len % 8)
1211 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1212 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1214 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1216 1)) / sizeof (u32x4)) * sizeof (u32x4);
1217 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1218 (~0 == last_table) ? 0 : ~0,
1220 last_table = *last_tag_table;
1223 /* egress ARP table */
1224 clib_memset (mask, 0, sizeof (mask));
1230 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1232 out_last_tag_table = &mt->out_arp_table_index;
1235 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1236 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1238 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1241 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1242 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1243 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1245 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1249 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1250 /* mask captures just the destination and the ethertype */
1251 mask_len = ((l3_offset +
1253 1)) / sizeof (u32x4)) * sizeof (u32x4);
1254 acl_classify_add_del_table_small (cm, mask, mask_len,
1257 out_last_table) ? 0 : ~0,
1258 out_last_tag_table, 1);
1259 out_last_table = *out_last_tag_table;
1264 /* Now add IP[46] tables */
1265 vec_foreach (mt, mvec)
1268 int is6 = mt->is_ipv6;
1272 u32 *last_tag_table;
1273 u32 *out_last_tag_table;
1276 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1278 for (tags = 2; tags >= 0; tags--)
1280 clib_memset (mask, 0, sizeof (mask));
1281 memcpy (&mask[6], mt->mac_mask, 6);
1282 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1287 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1288 last_tag_table = &mt->table_index;
1291 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1292 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1293 last_tag_table = &mt->dot1q_table_index;
1296 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1297 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1298 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1299 last_tag_table = &mt->dot1ad_table_index;
1302 for (i = 0; i < (mt->prefix_len / 8); i++)
1304 mask[l3_src_offs + i] = 0xff;
1306 if (mt->prefix_len % 8)
1308 mask[l3_src_offs + (mt->prefix_len / 8)] =
1309 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1312 * Round-up the number of bytes needed to store the prefix,
1313 * and round up the number of vectors too
1315 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1316 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1317 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1318 (~0 == last_table) ? 0 : ~0,
1320 last_table = *last_tag_table;
1324 for (tags = 2; tags >= 0; tags--)
1326 clib_memset (mask, 0, sizeof (mask));
1327 /* MAC destination */
1328 memcpy (&mask[0], mt->mac_mask, 6);
1329 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1334 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1335 out_last_tag_table = &mt->out_table_index;
1338 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1339 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1340 out_last_tag_table = &mt->out_dot1q_table_index;
1343 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1344 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1345 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1346 out_last_tag_table = &mt->out_dot1ad_table_index;
1349 for (i = 0; i < (mt->prefix_len / 8); i++)
1351 mask[l3_dst_offs + i] = 0xff;
1353 if (mt->prefix_len % 8)
1355 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1356 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1359 * Round-up the number of bytes needed to store the prefix,
1360 * and round up the number of vectors too
1362 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1364 1)) / sizeof (u32x4)) * sizeof (u32x4);
1365 acl_classify_add_del_table_small (cm, mask, mask_len,
1367 (~0 == out_last_table) ? 0 : ~0,
1368 out_last_tag_table, 1);
1369 out_last_table = *out_last_tag_table;
1373 a->ip4_table_index = last_table;
1374 a->ip6_table_index = last_table;
1375 a->l2_table_index = last_table;
1377 a->out_ip4_table_index = out_last_table;
1378 a->out_ip6_table_index = out_last_table;
1379 a->out_l2_table_index = out_last_table;
1381 /* Populate the classifier tables with rules from the MACIP ACL */
1382 for (i = 0; i < a->count; i++)
1386 int is6 = a->rules[i].is_ipv6;
1393 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1394 a->rules[i].src_prefixlen,
1395 a->rules[i].is_ipv6);
1396 ASSERT (match_type_index != ~0);
1398 for (tags = 2; tags >= 0; tags--)
1400 clib_memset (mask, 0, sizeof (mask));
1401 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1402 memcpy (&mask[6], a->rules[i].src_mac, 6);
1407 tag_table = mvec[match_type_index].table_index;
1411 tag_table = mvec[match_type_index].dot1q_table_index;
1417 tag_table = mvec[match_type_index].dot1ad_table_index;
1427 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1429 mask[eth + 1] = 0xdd;
1433 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1435 mask[eth + 1] = 0x00;
1438 /* add session to table mvec[match_type_index].table_index; */
1439 vnet_classify_add_del_session (cm, tag_table,
1440 mask, a->rules[i].is_permit ? ~0 : 0,
1441 i, 0, action, metadata, 1);
1442 clib_memset (&mask[12], 0, sizeof (mask) - 12);
1445 /* add ARP table entry too */
1446 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1448 clib_memset (mask, 0, sizeof (mask));
1449 memcpy (&mask[6], a->rules[i].src_mac, 6);
1451 for (tags = 2; tags >= 0; tags--)
1457 tag_table = mvec[match_type_index].arp_table_index;
1463 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1471 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1482 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1483 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1485 vnet_classify_add_del_session (cm, tag_table, mask,
1486 a->rules[i].is_permit ? ~0 : 0,
1487 i, 0, action, metadata, 1);
1490 if (macip_permit_also_egress (a->rules[i].is_permit))
1492 /* Add the egress entry with destination set */
1493 for (tags = 2; tags >= 0; tags--)
1495 clib_memset (mask, 0, sizeof (mask));
1496 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1497 /* src mac in the other direction becomes dst */
1498 memcpy (&mask[0], a->rules[i].src_mac, 6);
1503 tag_table = mvec[match_type_index].out_table_index;
1507 tag_table = mvec[match_type_index].out_dot1q_table_index;
1513 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1523 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1526 mask[eth + 1] = 0xdd;
1530 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1533 mask[eth + 1] = 0x00;
1536 /* add session to table mvec[match_type_index].table_index; */
1537 vnet_classify_add_del_session (cm, tag_table,
1539 a->rules[i].is_permit ? ~0 : 0,
1540 i, 0, action, metadata, 1);
1541 // clib_memset (&mask[12], 0, sizeof (mask) - 12);
1544 /* add ARP table entry too */
1545 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1547 for (tags = 2; tags >= 0; tags--)
1549 clib_memset (mask, 0, sizeof (mask));
1554 tag_table = mvec[match_type_index].out_arp_table_index;
1560 mvec[match_type_index].out_arp_dot1q_table_index;
1568 mvec[match_type_index].out_arp_dot1ad_table_index;
1578 vnet_classify_add_del_session (cm, tag_table,
1581 rules[i].is_permit ? ~0 : 0,
1582 i, 0, action, metadata, 1);
1591 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1593 vnet_classify_main_t *cm = &vnet_classify_main;
1594 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1596 if (a->ip4_table_index != ~0)
1598 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1599 &a->ip4_table_index, 0);
1600 a->ip4_table_index = ~0;
1602 if (a->ip6_table_index != ~0)
1604 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1605 &a->ip6_table_index, 0);
1606 a->ip6_table_index = ~0;
1608 if (a->l2_table_index != ~0)
1610 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1612 a->l2_table_index = ~0;
1614 if (a->out_ip4_table_index != ~0)
1616 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1617 &a->out_ip4_table_index, 0);
1618 a->out_ip4_table_index = ~0;
1620 if (a->out_ip6_table_index != ~0)
1622 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1623 &a->out_ip6_table_index, 0);
1624 a->out_ip6_table_index = ~0;
1626 if (a->out_l2_table_index != ~0)
1628 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1629 &a->out_l2_table_index, 0);
1630 a->out_l2_table_index = ~0;
1635 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1641 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1643 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1644 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1646 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1647 a->ip6_table_index, a->l2_table_index,
1649 /* return the first unhappy outcome but make try to plough through. */
1652 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
1653 a->out_ip6_table_index,
1654 a->out_l2_table_index, is_apply);
1655 /* return the first unhappy outcome but make try to plough through. */
1662 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1663 u32 * acl_list_index, u8 * tag)
1665 acl_main_t *am = &acl_main;
1666 macip_acl_list_t *a;
1667 macip_acl_rule_t *r;
1668 macip_acl_rule_t *acl_new_rules = 0;
1672 if (*acl_list_index != ~0)
1674 /* They supplied some number, let's see if this MACIP ACL exists */
1675 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1677 /* tried to replace a non-existent ACL, no point doing anything */
1679 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1680 *acl_list_index, tag);
1681 return VNET_API_ERROR_NO_SUCH_ENTRY;
1688 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1691 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1692 if (~0 != *acl_list_index)
1693 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1694 void *oldheap = acl_set_heap (am);
1695 /* Create and populate the rules */
1697 vec_validate (acl_new_rules, count - 1);
1699 for (i = 0; i < count; i++)
1701 r = &acl_new_rules[i];
1702 r->is_permit = rules[i].is_permit;
1703 r->is_ipv6 = rules[i].src_prefix.address.af;
1704 mac_address_decode (rules[i].src_mac, (mac_address_t *) & r->src_mac);
1705 mac_address_decode (rules[i].src_mac_mask,
1706 (mac_address_t *) & r->src_mac_mask);
1707 ip_address_decode (&rules[i].src_prefix.address, &r->src_ip_addr);
1708 r->src_prefixlen = rules[i].src_prefix.len;
1711 if (~0 == *acl_list_index)
1714 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1715 clib_memset (a, 0, sizeof (*a));
1716 /* Will return the newly allocated ACL index */
1717 *acl_list_index = a - am->macip_acls;
1721 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1724 vec_free (a->rules);
1726 macip_destroy_classify_tables (am, *acl_list_index);
1729 a->rules = acl_new_rules;
1731 memcpy (a->tag, tag, sizeof (a->tag));
1733 /* Create and populate the classifier tables */
1734 macip_create_classify_tables (am, *acl_list_index);
1735 clib_mem_set_heap (oldheap);
1736 /* If the ACL was already applied somewhere, reapply the newly created tables */
1738 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1742 /* No check that sw_if_index denotes a valid interface - the callers
1743 * were supposed to validate.
1745 * That said, if sw_if_index corresponds to an interface that exists at all,
1746 * this function must return errors accordingly if the ACL is not applied.
1750 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1753 u32 macip_acl_index;
1754 macip_acl_list_t *a;
1756 /* The vector is too short - MACIP ACL is not applied */
1757 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
1758 return VNET_API_ERROR_NO_SUCH_ENTRY;
1760 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1761 /* No point in deleting MACIP ACL which is not applied */
1762 if (~0 == macip_acl_index)
1763 return VNET_API_ERROR_NO_SUCH_ENTRY;
1765 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1766 /* remove the classifier tables off the interface L2 ACL */
1768 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1769 a->ip6_table_index, a->l2_table_index, 0);
1771 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1772 a->out_ip4_table_index, a->out_ip6_table_index,
1773 a->out_l2_table_index, 0);
1774 /* Unset the MACIP ACL index */
1775 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1776 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
1777 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
1780 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
1784 /* No check for validity of sw_if_index - the callers were supposed to validate */
1787 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1788 u32 macip_acl_index)
1790 macip_acl_list_t *a;
1792 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1794 return VNET_API_ERROR_NO_SUCH_ENTRY;
1796 void *oldheap = acl_set_heap (am);
1797 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1798 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1799 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
1800 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
1801 clib_mem_set_heap (oldheap);
1802 /* If there already a MACIP ACL applied, unapply it */
1803 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1804 macip_acl_interface_del_acl (am, sw_if_index);
1805 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1807 /* Apply the classifier tables for L2 ACLs */
1809 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1810 a->ip6_table_index, a->l2_table_index, 1);
1812 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1813 a->out_ip4_table_index, a->out_ip6_table_index,
1814 a->out_l2_table_index, 1);
1819 macip_acl_del_list (u32 acl_list_index)
1821 acl_main_t *am = &acl_main;
1822 macip_acl_list_t *a;
1824 if (pool_is_free_index (am->macip_acls, acl_list_index))
1826 return VNET_API_ERROR_NO_SUCH_ENTRY;
1829 /* delete any references to the ACL */
1830 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1832 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1834 macip_acl_interface_del_acl (am, i);
1838 void *oldheap = acl_set_heap (am);
1839 /* Now that classifier tables are detached, clean them up */
1840 macip_destroy_classify_tables (am, acl_list_index);
1842 /* now we can delete the ACL itself */
1843 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1846 vec_free (a->rules);
1848 pool_put (am->macip_acls, a);
1849 clib_mem_set_heap (oldheap);
1855 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1858 acl_main_t *am = &acl_main;
1862 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1866 rv = macip_acl_interface_del_acl (am, sw_if_index);
1872 * If the client does not allocate enough memory for a variable-length
1873 * message, and then proceed to use it as if the full memory allocated,
1874 * absent the check we happily consume that on the VPP side, and go
1875 * along as if nothing happened. However, the resulting
1876 * effects range from just garbage in the API decode
1877 * (because the decoder snoops too far), to potential memory
1880 * This verifies that the actual length of the message is
1881 * at least expected_len, and complains loudly if it is not.
1883 * A failing check here is 100% a software bug on the API user side,
1884 * so we might as well yell.
1888 verify_message_len (void *mp, u32 expected_len, char *where)
1890 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1891 if (supplied_len < expected_len)
1893 clib_warning ("%s: Supplied message length %d is less than expected %d",
1894 where, supplied_len, expected_len);
1903 /* API message handler */
1905 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1907 vl_api_acl_add_replace_reply_t *rmp;
1908 acl_main_t *am = &acl_main;
1910 u32 acl_list_index = ntohl (mp->acl_index);
1911 u32 acl_count = ntohl (mp->count);
1912 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1914 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1916 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1920 rv = VNET_API_ERROR_INVALID_VALUE;
1924 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1926 rmp->acl_index = htonl(acl_list_index);
1932 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1934 acl_main_t *am = &acl_main;
1935 vl_api_acl_del_reply_t *rmp;
1938 rv = acl_del_list (ntohl (mp->acl_index));
1940 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1945 vl_api_acl_stats_intf_counters_enable_t_handler
1946 (vl_api_acl_stats_intf_counters_enable_t * mp)
1948 acl_main_t *am = &acl_main;
1949 vl_api_acl_stats_intf_counters_enable_reply_t *rmp;
1952 rv = acl_stats_intf_counters_enable_disable (am, mp->enable);
1954 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1959 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1961 acl_main_t *am = &acl_main;
1962 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1963 u32 sw_if_index = ntohl (mp->sw_if_index);
1964 vl_api_acl_interface_add_del_reply_t *rmp;
1967 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1968 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1971 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1972 mp->is_input, ntohl (mp->acl_index));
1974 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1978 vl_api_acl_interface_set_acl_list_t_handler
1979 (vl_api_acl_interface_set_acl_list_t * mp)
1981 acl_main_t *am = &acl_main;
1982 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1985 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1986 u32 sw_if_index = ntohl (mp->sw_if_index);
1988 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1989 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1992 int may_clear_sessions = 1;
1993 for (i = 0; i < mp->count; i++)
1995 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1997 /* ACL does not exist, so we can not apply it */
1998 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2003 void *oldheap = acl_set_heap (am);
2005 u32 *in_acl_vec = 0;
2006 u32 *out_acl_vec = 0;
2007 for (i = 0; i < mp->count; i++)
2008 if (i < mp->n_input)
2009 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2011 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2014 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2015 &may_clear_sessions);
2017 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2019 &may_clear_sessions);
2020 vec_free (in_acl_vec);
2021 vec_free (out_acl_vec);
2022 clib_mem_set_heap (oldheap);
2026 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2030 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2032 api_rule->is_permit = r->is_permit;
2033 ip_address_encode (&r->src, r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2034 &api_rule->src_prefix.address);
2035 ip_address_encode (&r->dst, r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2036 &api_rule->dst_prefix.address);
2037 api_rule->src_prefix.len = r->src_prefixlen;
2038 api_rule->dst_prefix.len = r->dst_prefixlen;
2039 api_rule->proto = r->proto;
2040 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2041 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2042 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2043 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2044 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2045 api_rule->tcp_flags_value = r->tcp_flags_value;
2049 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2050 acl_list_t * acl, u32 context)
2052 vl_api_acl_details_t *mp;
2053 vl_api_acl_rule_t *rules;
2055 acl_rule_t *acl_rules = acl->rules;
2056 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * vec_len (acl_rules);
2057 void *oldheap = acl_set_heap (am);
2059 mp = vl_msg_api_alloc (msg_size);
2060 clib_memset (mp, 0, msg_size);
2061 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2063 /* fill in the message */
2064 mp->context = context;
2065 mp->count = htonl (vec_len (acl_rules));
2066 mp->acl_index = htonl (acl - am->acls);
2067 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2068 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2070 for (i = 0; i < vec_len (acl_rules); i++)
2072 copy_acl_rule_to_api_rule (&rules[i], &acl_rules[i]);
2075 clib_mem_set_heap (oldheap);
2076 vl_api_send_msg (reg, (u8 *) mp);
2081 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2083 acl_main_t *am = &acl_main;
2087 vl_api_registration_t *reg;
2089 reg = vl_api_client_index_to_registration (mp->client_index);
2093 if (mp->acl_index == ~0)
2096 /* Just dump all ACLs */
2097 pool_foreach (acl, am->acls,
2099 send_acl_details(am, reg, acl, mp->context);
2105 acl_index = ntohl (mp->acl_index);
2106 if (!pool_is_free_index (am->acls, acl_index))
2108 acl = pool_elt_at_index (am->acls, acl_index);
2109 send_acl_details (am, reg, acl, mp->context);
2115 /* FIXME API: should we signal an error here at all ? */
2121 send_acl_interface_list_details (acl_main_t * am,
2122 vl_api_registration_t * reg,
2123 u32 sw_if_index, u32 context)
2125 vl_api_acl_interface_list_details_t *mp;
2131 void *oldheap = acl_set_heap (am);
2133 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2134 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2136 clib_mem_set_heap (oldheap);
2138 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2139 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2140 count = n_input + n_output;
2142 msg_size = sizeof (*mp);
2143 msg_size += sizeof (mp->acls[0]) * count;
2145 mp = vl_msg_api_alloc (msg_size);
2146 clib_memset (mp, 0, msg_size);
2148 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2150 /* fill in the message */
2151 mp->context = context;
2152 mp->sw_if_index = htonl (sw_if_index);
2154 mp->n_input = n_input;
2155 for (i = 0; i < n_input; i++)
2157 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2159 for (i = 0; i < n_output; i++)
2161 mp->acls[n_input + i] =
2162 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2164 vl_api_send_msg (reg, (u8 *) mp);
2168 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2171 acl_main_t *am = &acl_main;
2172 vnet_sw_interface_t *swif;
2173 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2176 vl_api_registration_t *reg;
2178 reg = vl_api_client_index_to_registration (mp->client_index);
2182 if (mp->sw_if_index == ~0)
2185 pool_foreach (swif, im->sw_interfaces,
2187 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2193 sw_if_index = ntohl (mp->sw_if_index);
2194 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2195 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2199 /* MACIP ACL API handlers */
2202 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2204 vl_api_macip_acl_add_reply_t *rmp;
2205 acl_main_t *am = &acl_main;
2207 u32 acl_list_index = ~0;
2208 u32 acl_count = ntohl (mp->count);
2209 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2211 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2213 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2217 rv = VNET_API_ERROR_INVALID_VALUE;
2221 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2223 rmp->acl_index = htonl(acl_list_index);
2229 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2231 vl_api_macip_acl_add_replace_reply_t *rmp;
2232 acl_main_t *am = &acl_main;
2234 u32 acl_list_index = ntohl (mp->acl_index);
2235 u32 acl_count = ntohl (mp->count);
2236 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2238 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2240 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2244 rv = VNET_API_ERROR_INVALID_VALUE;
2248 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2250 rmp->acl_index = htonl(acl_list_index);
2256 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2258 acl_main_t *am = &acl_main;
2259 vl_api_macip_acl_del_reply_t *rmp;
2262 rv = macip_acl_del_list (ntohl (mp->acl_index));
2264 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2268 vl_api_macip_acl_interface_add_del_t_handler
2269 (vl_api_macip_acl_interface_add_del_t * mp)
2271 acl_main_t *am = &acl_main;
2272 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2274 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2275 u32 sw_if_index = ntohl (mp->sw_if_index);
2277 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2278 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2281 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2282 ntohl (mp->acl_index));
2284 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2288 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2289 macip_acl_list_t * acl, u32 context)
2291 vl_api_macip_acl_details_t *mp;
2292 vl_api_macip_acl_rule_t *rules;
2293 macip_acl_rule_t *r;
2295 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2297 mp = vl_msg_api_alloc (msg_size);
2298 clib_memset (mp, 0, msg_size);
2299 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2301 /* fill in the message */
2302 mp->context = context;
2305 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2306 mp->count = htonl (acl->count);
2307 mp->acl_index = htonl (acl - am->macip_acls);
2309 for (i = 0; i < acl->count; i++)
2312 rules[i].is_permit = r->is_permit;
2313 mac_address_encode ((mac_address_t *) & r->src_mac,
2315 mac_address_encode ((mac_address_t *) & r->src_mac_mask,
2316 rules[i].src_mac_mask);
2317 ip_address_encode (&r->src_ip_addr,
2318 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2319 &rules[i].src_prefix.address);
2320 rules[i].src_prefix.len = r->src_prefixlen;
2325 /* No martini, no party - no ACL applied to this interface. */
2330 vl_api_send_msg (reg, (u8 *) mp);
2335 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2337 acl_main_t *am = &acl_main;
2338 macip_acl_list_t *acl;
2340 vl_api_registration_t *reg;
2342 reg = vl_api_client_index_to_registration (mp->client_index);
2346 if (mp->acl_index == ~0)
2348 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2350 pool_foreach (acl, am->macip_acls,
2352 send_macip_acl_details (am, reg, acl, mp->context);
2358 u32 acl_index = ntohl (mp->acl_index);
2359 if (!pool_is_free_index (am->macip_acls, acl_index))
2361 acl = pool_elt_at_index (am->macip_acls, acl_index);
2362 send_macip_acl_details (am, reg, acl, mp->context);
2368 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2371 acl_main_t *am = &acl_main;
2372 vl_api_macip_acl_interface_get_reply_t *rmp;
2373 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2374 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2375 vl_api_registration_t *reg;
2378 reg = vl_api_client_index_to_registration (mp->client_index);
2382 rmp = vl_msg_api_alloc (msg_size);
2383 clib_memset (rmp, 0, msg_size);
2385 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2386 rmp->context = mp->context;
2387 rmp->count = htonl (count);
2388 for (i = 0; i < count; i++)
2390 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2393 vl_api_send_msg (reg, (u8 *) rmp);
2397 send_macip_acl_interface_list_details (acl_main_t * am,
2398 vl_api_registration_t * reg,
2400 u32 acl_index, u32 context)
2402 vl_api_macip_acl_interface_list_details_t *rmp;
2403 /* at this time there is only ever 1 mac ip acl per interface */
2404 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2406 rmp = vl_msg_api_alloc (msg_size);
2407 clib_memset (rmp, 0, msg_size);
2409 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2411 /* fill in the message */
2412 rmp->context = context;
2414 rmp->sw_if_index = htonl (sw_if_index);
2415 rmp->acls[0] = htonl (acl_index);
2417 vl_api_send_msg (reg, (u8 *) rmp);
2421 vl_api_macip_acl_interface_list_dump_t_handler
2422 (vl_api_macip_acl_interface_list_dump_t * mp)
2424 vl_api_registration_t *reg;
2425 acl_main_t *am = &acl_main;
2426 u32 sw_if_index = ntohl (mp->sw_if_index);
2428 reg = vl_api_client_index_to_registration (mp->client_index);
2432 if (sw_if_index == ~0)
2434 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2436 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2438 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2439 am->macip_acl_by_sw_if_index
2447 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2449 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2450 am->macip_acl_by_sw_if_index
2451 [sw_if_index], mp->context);
2457 vl_api_acl_interface_set_etype_whitelist_t_handler
2458 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2460 acl_main_t *am = &acl_main;
2461 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2464 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2465 u32 sw_if_index = ntohl (mp->sw_if_index);
2466 u16 *vec_in = 0, *vec_out = 0;
2467 void *oldheap = acl_set_heap (am);
2469 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2470 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2473 for (i = 0; i < mp->count; i++)
2475 if (i < mp->n_input)
2476 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2478 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2480 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2483 clib_mem_set_heap (oldheap);
2484 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2488 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2489 vl_api_registration_t * reg,
2490 u32 sw_if_index, u32 context)
2492 vl_api_acl_interface_etype_whitelist_details_t *mp;
2499 u16 *whitelist_in = 0;
2500 u16 *whitelist_out = 0;
2502 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2504 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2506 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2508 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2510 if ((0 == whitelist_in) && (0 == whitelist_out))
2511 return; /* nothing to do */
2513 void *oldheap = acl_set_heap (am);
2515 n_input = vec_len (whitelist_in);
2516 n_output = vec_len (whitelist_out);
2517 count = n_input + n_output;
2519 msg_size = sizeof (*mp);
2520 msg_size += sizeof (mp->whitelist[0]) * count;
2522 mp = vl_msg_api_alloc (msg_size);
2523 clib_memset (mp, 0, msg_size);
2525 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2527 /* fill in the message */
2528 mp->context = context;
2529 mp->sw_if_index = htonl (sw_if_index);
2531 mp->n_input = n_input;
2532 for (i = 0; i < n_input; i++)
2534 mp->whitelist[i] = htons (whitelist_in[i]);
2536 for (i = 0; i < n_output; i++)
2538 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2540 clib_mem_set_heap (oldheap);
2541 vl_api_send_msg (reg, (u8 *) mp);
2546 vl_api_acl_interface_etype_whitelist_dump_t_handler
2547 (vl_api_acl_interface_list_dump_t * mp)
2549 acl_main_t *am = &acl_main;
2550 vnet_sw_interface_t *swif;
2551 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2554 vl_api_registration_t *reg;
2556 reg = vl_api_client_index_to_registration (mp->client_index);
2560 if (mp->sw_if_index == ~0)
2563 pool_foreach (swif, im->sw_interfaces,
2565 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2571 sw_if_index = ntohl (mp->sw_if_index);
2572 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2573 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2579 acl_set_timeout_sec (int timeout_type, u32 value)
2581 acl_main_t *am = &acl_main;
2582 clib_time_t *ct = &am->vlib_main->clib_time;
2584 if (timeout_type < ACL_N_TIMEOUTS)
2586 am->session_timeout_sec[timeout_type] = value;
2590 clib_warning ("Unknown timeout type %d", timeout_type);
2593 am->session_timeout[timeout_type] =
2594 (u64) (((f64) value) / ct->seconds_per_clock);
2598 acl_set_session_max_entries (u32 value)
2600 acl_main_t *am = &acl_main;
2601 am->fa_conn_table_max_entries = value;
2605 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2607 acl_main_t *am = &acl_main;
2609 if ((eh < 256) && (value < 2))
2611 am->fa_ipv6_known_eh_bitmap =
2612 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2620 static clib_error_t *
2621 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2623 acl_main_t *am = &acl_main;
2624 if (0 == am->acl_mheap)
2626 /* ACL heap is not initialized, so definitely nothing to do. */
2631 int may_clear_sessions = 1;
2632 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2633 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2635 /* also unapply any ACLs in case the users did not do so. */
2636 macip_acl_interface_del_acl (am, sw_if_index);
2637 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
2638 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
2643 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2647 static clib_error_t *
2648 acl_set_aclplugin_fn (vlib_main_t * vm,
2649 unformat_input_t * input, vlib_cli_command_t * cmd)
2651 clib_error_t *error = 0;
2655 uword memory_size = 0;
2656 acl_main_t *am = &acl_main;
2658 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2660 if (!acl_set_skip_ipv6_eh (eh_val, val))
2662 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2666 if (unformat (input, "use-hash-acl-matching %u", &val))
2668 am->use_hash_acl_matching = (val != 0);
2671 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2673 am->l4_match_nonfirst_fragment = (val != 0);
2676 if (unformat (input, "reclassify-sessions %u", &val))
2678 am->reclassify_sessions = (val != 0);
2681 if (unformat (input, "event-trace"))
2683 if (!unformat (input, "%u", &val))
2685 error = clib_error_return (0,
2686 "expecting trace level, got `%U`",
2687 format_unformat_error, input);
2692 am->trace_acl = val;
2696 if (unformat (input, "heap"))
2698 if (unformat (input, "main"))
2700 if (unformat (input, "validate %u", &val))
2701 acl_plugin_acl_set_validate_heap (am, val);
2702 else if (unformat (input, "trace %u", &val))
2703 acl_plugin_acl_set_trace_heap (am, val);
2706 else if (unformat (input, "hash"))
2708 if (unformat (input, "validate %u", &val))
2709 acl_plugin_hash_acl_set_validate_heap (val);
2710 else if (unformat (input, "trace %u", &val))
2711 acl_plugin_hash_acl_set_trace_heap (val);
2716 if (unformat (input, "session"))
2718 if (unformat (input, "table"))
2720 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2721 if (unformat (input, "max-entries"))
2723 if (!unformat (input, "%u", &val))
2725 error = clib_error_return (0,
2726 "expecting maximum number of entries, got `%U`",
2727 format_unformat_error, input);
2732 acl_set_session_max_entries (val);
2736 if (unformat (input, "hash-table-buckets"))
2738 if (!unformat (input, "%u", &val))
2740 error = clib_error_return (0,
2741 "expecting maximum number of hash table buckets, got `%U`",
2742 format_unformat_error, input);
2747 am->fa_conn_table_hash_num_buckets = val;
2751 if (unformat (input, "hash-table-memory"))
2753 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2755 error = clib_error_return (0,
2756 "expecting maximum amount of hash table memory, got `%U`",
2757 format_unformat_error, input);
2762 am->fa_conn_table_hash_memory_size = memory_size;
2766 if (unformat (input, "event-trace"))
2768 if (!unformat (input, "%u", &val))
2770 error = clib_error_return (0,
2771 "expecting trace level, got `%U`",
2772 format_unformat_error, input);
2777 am->trace_sessions = val;
2783 if (unformat (input, "timeout"))
2785 if (unformat (input, "udp"))
2787 if (unformat (input, "idle"))
2789 if (!unformat (input, "%u", &timeout))
2791 error = clib_error_return (0,
2792 "expecting timeout value in seconds, got `%U`",
2793 format_unformat_error,
2799 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2804 if (unformat (input, "tcp"))
2806 if (unformat (input, "idle"))
2808 if (!unformat (input, "%u", &timeout))
2810 error = clib_error_return (0,
2811 "expecting timeout value in seconds, got `%U`",
2812 format_unformat_error,
2818 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2822 if (unformat (input, "transient"))
2824 if (!unformat (input, "%u", &timeout))
2826 error = clib_error_return (0,
2827 "expecting timeout value in seconds, got `%U`",
2828 format_unformat_error,
2834 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2848 my_format_mac_address (u8 * s, va_list * args)
2850 u8 *a = va_arg (*args, u8 *);
2851 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2852 a[0], a[1], a[2], a[3], a[4], a[5]);
2856 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2858 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2860 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2861 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2862 format_ip46_address, &a->src_ip_addr,
2863 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2865 my_format_mac_address, a->src_mac,
2866 my_format_mac_address, a->src_mac_mask);
2871 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2873 vlib_main_t *vm = am->vlib_main;
2876 /* Don't try to print someone else's memory */
2877 if (macip_acl_index >= vec_len (am->macip_acls))
2880 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2881 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2883 vlib_cli_output (vm,
2884 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2885 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2887 vlib_cli_output (vm,
2888 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2889 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2890 vlib_cli_output (vm,
2891 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
2892 a->out_ip4_table_index, a->out_ip6_table_index,
2893 a->out_l2_table_index);
2894 for (i = 0; i < vec_len (a->rules); i++)
2895 vlib_cli_output (vm, " rule %d: %U\n", i,
2896 my_macip_acl_rule_t_pretty_format,
2897 vec_elt_at_index (a->rules, i));
2901 static clib_error_t *
2902 acl_set_aclplugin_interface_fn (vlib_main_t * vm,
2903 unformat_input_t * input,
2904 vlib_cli_command_t * cmd)
2906 unformat_input_t _line_input, *line_input = &_line_input;
2907 u32 sw_if_index, is_add, is_input, acl_index;
2909 is_add = is_input = 1;
2910 acl_index = sw_if_index = ~0;
2912 if (!unformat_user (input, unformat_line_input, line_input))
2915 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2917 if (unformat (line_input, "%U",
2918 unformat_vnet_sw_interface, vnet_get_main (),
2921 else if (unformat (line_input, "add"))
2923 else if (unformat (line_input, "del"))
2925 else if (unformat (line_input, "acl %d", &acl_index))
2927 else if (unformat (line_input, "input"))
2929 else if (unformat (line_input, "output"))
2935 if (~0 == sw_if_index)
2936 return (clib_error_return (0, "invalid interface"));
2937 if (~0 == acl_index)
2938 return (clib_error_return (0, "invalid acl"));
2940 acl_interface_add_del_inout_acl (sw_if_index, is_add, is_input, acl_index);
2942 unformat_free (line_input);
2946 #define vec_validate_acl_rules(v, idx) \
2948 if (vec_len(v) < idx+1) { \
2949 vec_validate(v, idx); \
2950 v[idx].is_permit = 0x1; \
2951 v[idx].srcport_or_icmptype_last = 0xffff; \
2952 v[idx].dstport_or_icmpcode_last = 0xffff; \
2956 static clib_error_t *
2957 acl_set_aclplugin_acl_fn (vlib_main_t * vm,
2958 unformat_input_t * input, vlib_cli_command_t * cmd)
2960 unformat_input_t _line_input, *line_input = &_line_input;
2961 vl_api_acl_rule_t *rules = 0;
2964 int n_rules_override = -1;
2969 u32 tcpflags, tcpmask;
2970 u32 src_prefix_length = 0, dst_prefix_length = 0;
2971 ip4_address_t src_v4address, dst_v4address;
2972 ip6_address_t src_v6address, dst_v6address;
2973 u8 *tag = (u8 *) "cli";
2975 if (!unformat_user (input, unformat_line_input, line_input))
2978 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2980 if (unformat (line_input, "ipv6"))
2982 vec_validate_acl_rules (rules, rule_idx);
2983 rules[rule_idx].is_ipv6 = 1;
2985 else if (unformat (line_input, "ipv4"))
2987 vec_validate_acl_rules (rules, rule_idx);
2988 rules[rule_idx].is_ipv6 = 0;
2990 else if (unformat (line_input, "permit+reflect"))
2992 vec_validate_acl_rules (rules, rule_idx);
2993 rules[rule_idx].is_permit = 2;
2995 else if (unformat (line_input, "permit"))
2997 vec_validate_acl_rules (rules, rule_idx);
2998 rules[rule_idx].is_permit = 1;
3000 else if (unformat (line_input, "deny"))
3002 vec_validate_acl_rules (rules, rule_idx);
3003 rules[rule_idx].is_permit = 0;
3005 else if (unformat (line_input, "count %d", &n_rules_override))
3007 /* we will use this later */
3009 else if (unformat (line_input, "action %d", &action))
3011 vec_validate_acl_rules (rules, rule_idx);
3012 rules[rule_idx].is_permit = action;
3014 else if (unformat (line_input, "src %U/%d",
3015 unformat_ip4_address, &src_v4address,
3016 &src_prefix_length))
3018 vec_validate_acl_rules (rules, rule_idx);
3019 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
3020 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
3021 rules[rule_idx].is_ipv6 = 0;
3023 else if (unformat (line_input, "src %U/%d",
3024 unformat_ip6_address, &src_v6address,
3025 &src_prefix_length))
3027 vec_validate_acl_rules (rules, rule_idx);
3028 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
3029 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
3030 rules[rule_idx].is_ipv6 = 1;
3032 else if (unformat (line_input, "dst %U/%d",
3033 unformat_ip4_address, &dst_v4address,
3034 &dst_prefix_length))
3036 vec_validate_acl_rules (rules, rule_idx);
3037 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
3038 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
3039 rules[rule_idx].is_ipv6 = 0;
3041 else if (unformat (line_input, "dst %U/%d",
3042 unformat_ip6_address, &dst_v6address,
3043 &dst_prefix_length))
3045 vec_validate_acl_rules (rules, rule_idx);
3046 memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
3047 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
3048 rules[rule_idx].is_ipv6 = 1;
3050 else if (unformat (line_input, "sport %d-%d", &port1, &port2))
3052 vec_validate_acl_rules (rules, rule_idx);
3053 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
3054 rules[rule_idx].srcport_or_icmptype_last = htons (port2);
3056 else if (unformat (line_input, "sport %d", &port1))
3058 vec_validate_acl_rules (rules, rule_idx);
3059 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
3060 rules[rule_idx].srcport_or_icmptype_last = htons (port1);
3062 else if (unformat (line_input, "dport %d-%d", &port1, &port2))
3064 vec_validate_acl_rules (rules, rule_idx);
3065 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
3066 rules[rule_idx].dstport_or_icmpcode_last = htons (port2);
3068 else if (unformat (line_input, "dport %d", &port1))
3070 vec_validate_acl_rules (rules, rule_idx);
3071 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
3072 rules[rule_idx].dstport_or_icmpcode_last = htons (port1);
3074 else if (unformat (line_input, "tcpflags %d %d", &tcpflags, &tcpmask))
3076 vec_validate_acl_rules (rules, rule_idx);
3077 rules[rule_idx].tcp_flags_value = tcpflags;
3078 rules[rule_idx].tcp_flags_mask = tcpmask;
3081 if (unformat (line_input, "tcpflags %d mask %d", &tcpflags, &tcpmask))
3083 vec_validate_acl_rules (rules, rule_idx);
3084 rules[rule_idx].tcp_flags_value = tcpflags;
3085 rules[rule_idx].tcp_flags_mask = tcpmask;
3087 else if (unformat (line_input, "proto %d", &proto))
3089 vec_validate_acl_rules (rules, rule_idx);
3090 rules[rule_idx].proto = proto;
3092 else if (unformat (line_input, "tag %s", &tag))
3095 else if (unformat (line_input, ","))
3098 vec_validate_acl_rules (rules, rule_idx);
3106 rv = acl_add_list (vec_len (rules), rules, &acl_index, tag);
3111 return (clib_error_return (0, "failed"));
3113 vlib_cli_output (vm, "ACL index:%d", acl_index);
3118 static clib_error_t *
3119 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3121 input, vlib_cli_command_t * cmd)
3123 clib_error_t *error = 0;
3124 acl_main_t *am = &acl_main;
3128 (void) unformat (input, "index %u", &acl_index);
3130 for (i = 0; i < vec_len (am->macip_acls); i++)
3132 /* Don't attempt to show the ACLs that do not exist */
3133 if (pool_is_free_index (am->macip_acls, i))
3136 if ((acl_index != ~0) && (acl_index != i))
3141 macip_acl_print (am, i);
3142 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3144 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3146 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3154 static clib_error_t *
3155 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3157 input, vlib_cli_command_t * cmd)
3159 clib_error_t *error = 0;
3160 acl_main_t *am = &acl_main;
3162 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3164 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3165 vec_elt (am->macip_acl_by_sw_if_index, i));
3171 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3174 vlib_main_t *vm = am->vlib_main;
3176 for (i = 0; i < vec_len (am->acls); i++)
3178 if (acl_is_not_defined (am, i))
3180 /* don't attempt to show the ACLs that do not exist */
3183 if ((acl_index != ~0) && (acl_index != i))
3187 acl_print_acl (vm, am, i);
3189 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3191 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3192 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3195 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3197 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3198 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3201 if (i < vec_len (am->lc_index_vec_by_acl))
3203 vlib_cli_output (vm, " used in lookup context index: %U\n",
3204 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3209 static clib_error_t *
3210 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3211 unformat_input_t * input, vlib_cli_command_t * cmd)
3213 clib_error_t *error = 0;
3214 acl_main_t *am = &acl_main;
3217 (void) unformat (input, "index %u", &acl_index);
3219 acl_plugin_show_acl (am, acl_index);
3223 static clib_error_t *
3224 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3225 unformat_input_t * input,
3226 vlib_cli_command_t * cmd)
3228 clib_error_t *error = 0;
3231 (void) unformat (input, "index %u", &lc_index);
3233 acl_plugin_show_lookup_context (lc_index);
3237 static clib_error_t *
3238 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3239 unformat_input_t * input,
3240 vlib_cli_command_t * cmd)
3242 clib_error_t *error = 0;
3245 (void) unformat (input, "index %u", &lc_index);
3247 acl_plugin_show_lookup_user (lc_index);
3253 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3256 vlib_main_t *vm = am->vlib_main;
3259 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3260 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3262 /* if we need a particular interface, skip all the others */
3263 if ((sw_if_index != ~0) && (sw_if_index != swi))
3266 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3267 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3268 vlib_cli_output (vm, " input policy epoch: %x\n",
3269 vec_elt (am->input_policy_epoch_by_sw_if_index,
3271 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3272 vlib_cli_output (vm, " output policy epoch: %x\n",
3273 vec_elt (am->output_policy_epoch_by_sw_if_index,
3277 if (intf_has_etype_whitelist (am, swi, 1))
3279 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3280 am->input_etype_whitelist_by_sw_if_index[swi],
3283 if (intf_has_etype_whitelist (am, swi, 0))
3285 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3286 am->output_etype_whitelist_by_sw_if_index[swi],
3290 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3291 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3293 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3294 am->input_acl_vec_by_sw_if_index[swi], "%d");
3297 vlib_cli_output (vm, "\n");
3298 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3300 acl_print_acl (vm, am, *pj);
3302 vlib_cli_output (vm, "\n");
3306 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3307 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3309 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3310 am->output_acl_vec_by_sw_if_index[swi], "%d");
3313 vlib_cli_output (vm, "\n");
3314 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3316 acl_print_acl (vm, am, *pj);
3318 vlib_cli_output (vm, "\n");
3321 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3323 vlib_cli_output (vm, " input lookup context index: %d",
3324 am->input_lc_index_by_sw_if_index[swi]);
3326 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3328 vlib_cli_output (vm, " output lookup context index: %d",
3329 am->output_lc_index_by_sw_if_index[swi]);
3336 static clib_error_t *
3337 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3338 unformat_input_t * input,
3339 vlib_cli_command_t * cmd)
3341 clib_error_t *error = 0;
3342 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3345 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3346 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3347 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3348 format_acl_plugin_5tuple, five_tuple);
3350 error = clib_error_return (0, "expecting 6 hex integers");
3355 static clib_error_t *
3356 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3358 input, vlib_cli_command_t * cmd)
3360 clib_error_t *error = 0;
3361 acl_main_t *am = &acl_main;
3363 u32 sw_if_index = ~0;
3364 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3365 int show_acl = unformat (input, "acl");
3366 int detail = unformat (input, "detail");
3368 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3372 static clib_error_t *
3373 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3374 unformat_input_t * input,
3375 vlib_cli_command_t * cmd)
3377 clib_error_t *error = 0;
3378 acl_main_t *am = &acl_main;
3380 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3383 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3387 vlib_cli_output (vm, " Not initialized\n");
3389 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3390 if (am->hash_lookup_mheap)
3392 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3396 vlib_cli_output (vm, " Not initialized\n");
3402 acl_plugin_show_sessions (acl_main_t * am,
3403 u32 show_session_thread_id,
3404 u32 show_session_session_index)
3406 vlib_main_t *vm = am->vlib_main;
3408 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3409 vnet_sw_interface_t *swif;
3410 u64 now = clib_cpu_time_now ();
3411 u64 clocks_per_second = am->vlib_main->clib_time.clocks_per_second;
3414 u64 n_adds = am->fa_session_total_adds;
3415 u64 n_dels = am->fa_session_total_dels;
3416 u64 n_deact = am->fa_session_total_deactivations;
3417 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3418 n_dels, n_adds - n_dels);
3419 vlib_cli_output (vm, "Sessions active: add %lu - deact %lu = %lu", n_adds,
3420 n_deact, n_adds - n_deact);
3421 vlib_cli_output (vm, "Sessions being purged: deact %lu - del %lu = %lu",
3422 n_deact, n_dels, n_deact - n_dels);
3424 vlib_cli_output (vm, "now: %lu clocks per second: %lu", now,
3426 vlib_cli_output (vm, "\n\nPer-thread data:");
3427 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3429 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3430 vlib_cli_output (vm, "Thread #%d:", wk);
3431 if (show_session_thread_id == wk
3432 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3434 vlib_cli_output (vm, " session index %u:",
3435 show_session_session_index);
3436 fa_session_t *sess =
3437 pw->fa_sessions_pool + show_session_session_index;
3438 u64 *m = (u64 *) & sess->info;
3439 vlib_cli_output (vm,
3440 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3441 m[0], m[1], m[2], m[3], m[4], m[5]);
3442 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3443 vlib_cli_output (vm, " tcp_flags_seen: %x",
3444 sess->tcp_flags_seen.as_u16);
3445 vlib_cli_output (vm, " last active time: %lu",
3446 sess->last_active_time);
3447 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3448 vlib_cli_output (vm, " link enqueue time: %lu",
3449 sess->link_enqueue_time);
3450 vlib_cli_output (vm, " link next index: %u",
3451 sess->link_next_idx);
3452 vlib_cli_output (vm, " link prev index: %u",
3453 sess->link_prev_idx);
3454 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3456 vlib_cli_output (vm, " connection add/del stats:", wk);
3458 pool_foreach (swif, im->sw_interfaces,
3460 u32 sw_if_index = swif->sw_if_index;
3462 (sw_if_index < vec_len (pw->fa_session_adds_by_sw_if_index) ?
3463 pw->fa_session_adds_by_sw_if_index[sw_if_index] :
3466 (sw_if_index < vec_len (pw->fa_session_dels_by_sw_if_index) ?
3467 pw->fa_session_dels_by_sw_if_index[sw_if_index] :
3469 u64 n_epoch_changes =
3470 (sw_if_index < vec_len (pw->fa_session_epoch_change_by_sw_if_index) ?
3471 pw->fa_session_epoch_change_by_sw_if_index[sw_if_index] :
3473 vlib_cli_output (vm,
3474 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3484 vlib_cli_output (vm, " connection timeout type lists:", wk);
3486 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3488 u32 head_session_index = pw->fa_conn_list_head[tt];
3489 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3490 head_session_index);
3491 if (~0 != head_session_index)
3493 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3494 vlib_cli_output (vm, " last active time: %lu",
3495 sess->last_active_time);
3496 vlib_cli_output (vm, " link enqueue time: %lu",
3497 sess->link_enqueue_time);
3501 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3502 vlib_cli_output (vm, " Requeue until time: %lu",
3503 pw->requeue_until_time);
3504 vlib_cli_output (vm, " Current time wait interval: %lu",
3505 pw->current_time_wait_interval);
3506 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3507 pw->cnt_deleted_sessions);
3508 vlib_cli_output (vm, " Delete already deleted: %lu",
3509 pw->cnt_already_deleted_sessions);
3510 vlib_cli_output (vm, " Session timers restarted: %lu",
3511 pw->cnt_session_timer_restarted);
3512 vlib_cli_output (vm, " Swipe until this time: %lu",
3513 pw->swipe_end_time);
3514 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3515 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3516 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3518 pw->pending_clear_sw_if_index_bitmap);
3519 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3520 vlib_cli_output (vm, " interrupt is pending: %d",
3521 pw->interrupt_is_pending);
3522 vlib_cli_output (vm, " interrupt is needed: %d",
3523 pw->interrupt_is_needed);
3524 vlib_cli_output (vm, " interrupt is unwanted: %d",
3525 pw->interrupt_is_unwanted);
3526 vlib_cli_output (vm, " interrupt generation: %d",
3527 pw->interrupt_generation);
3528 vlib_cli_output (vm, " received session change requests: %d",
3529 pw->rcvd_session_change_requests);
3530 vlib_cli_output (vm, " sent session change requests: %d",
3531 pw->sent_session_change_requests);
3533 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3534 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3535 foreach_fa_cleaner_counter;
3537 vlib_cli_output (vm, "Interrupt generation: %d",
3538 am->fa_interrupt_generation);
3539 vlib_cli_output (vm,
3540 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3541 am->fa_min_deleted_sessions_per_interval,
3542 am->fa_max_deleted_sessions_per_interval,
3543 am->fa_cleaner_wait_time_increment * 1000.0,
3544 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3545 1000.0 / (f64) vm->clib_time.clocks_per_second);
3546 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3549 static clib_error_t *
3550 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3551 unformat_input_t * input,
3552 vlib_cli_command_t * cmd)
3554 clib_error_t *error = 0;
3555 acl_main_t *am = &acl_main;
3557 u32 show_bihash_verbose = 0;
3558 u32 show_session_thread_id = ~0;
3559 u32 show_session_session_index = ~0;
3560 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3561 &show_session_session_index);
3562 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3564 acl_plugin_show_sessions (am, show_session_thread_id,
3565 show_session_session_index);
3566 show_fa_sessions_hash (vm, show_bihash_verbose);
3570 static clib_error_t *
3571 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3572 unformat_input_t * input,
3573 vlib_cli_command_t * cmd)
3575 clib_error_t *error = 0;
3579 int show_acl_hash_info = 0;
3580 int show_applied_info = 0;
3581 int show_mask_type = 0;
3582 int show_bihash = 0;
3583 u32 show_bihash_verbose = 0;
3585 if (unformat (input, "acl"))
3587 show_acl_hash_info = 1;
3588 /* mask-type is handy to see as well right there */
3590 unformat (input, "index %u", &acl_index);
3592 else if (unformat (input, "applied"))
3594 show_applied_info = 1;
3595 unformat (input, "lc_index %u", &lc_index);
3597 else if (unformat (input, "mask"))
3601 else if (unformat (input, "hash"))
3604 unformat (input, "verbose %u", &show_bihash_verbose);
3608 (show_mask_type || show_acl_hash_info || show_applied_info
3611 /* if no qualifiers specified, show all */
3613 show_acl_hash_info = 1;
3614 show_applied_info = 1;
3617 vlib_cli_output (vm, "Stats counters enabled for interface ACLs: %d",
3618 acl_main.interface_acl_counters_enabled);
3620 acl_plugin_show_tables_mask_type ();
3621 if (show_acl_hash_info)
3622 acl_plugin_show_tables_acl_hash_info (acl_index);
3623 if (show_applied_info)
3624 acl_plugin_show_tables_applied_info (lc_index);
3626 acl_plugin_show_tables_bihash (show_bihash_verbose);
3631 static clib_error_t *
3632 acl_clear_aclplugin_fn (vlib_main_t * vm,
3633 unformat_input_t * input, vlib_cli_command_t * cmd)
3635 clib_error_t *error = 0;
3636 acl_main_t *am = &acl_main;
3637 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3638 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3643 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3644 .path = "set acl-plugin",
3645 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3646 .function = acl_set_aclplugin_fn,
3649 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3650 .path = "show acl-plugin acl",
3651 .short_help = "show acl-plugin acl [index N]",
3652 .function = acl_show_aclplugin_acl_fn,
3655 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3656 .path = "show acl-plugin lookup context",
3657 .short_help = "show acl-plugin lookup context [index N]",
3658 .function = acl_show_aclplugin_lookup_context_fn,
3661 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3662 .path = "show acl-plugin lookup user",
3663 .short_help = "show acl-plugin lookup user [index N]",
3664 .function = acl_show_aclplugin_lookup_user_fn,
3667 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3668 .path = "show acl-plugin decode 5tuple",
3669 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3670 .function = acl_show_aclplugin_decode_5tuple_fn,
3673 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3674 .path = "show acl-plugin interface",
3675 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3676 .function = acl_show_aclplugin_interface_fn,
3679 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3680 .path = "show acl-plugin memory",
3681 .short_help = "show acl-plugin memory",
3682 .function = acl_show_aclplugin_memory_fn,
3685 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3686 .path = "show acl-plugin sessions",
3687 .short_help = "show acl-plugin sessions",
3688 .function = acl_show_aclplugin_sessions_fn,
3691 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3692 .path = "show acl-plugin tables",
3693 .short_help = "show acl-plugin tables [ acl [index N] | applied [ lc_index N ] | mask | hash [verbose N] ]",
3694 .function = acl_show_aclplugin_tables_fn,
3697 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3698 .path = "show acl-plugin macip acl",
3699 .short_help = "show acl-plugin macip acl [index N]",
3700 .function = acl_show_aclplugin_macip_acl_fn,
3703 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3704 .path = "show acl-plugin macip interface",
3705 .short_help = "show acl-plugin macip interface",
3706 .function = acl_show_aclplugin_macip_interface_fn,
3709 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3710 .path = "clear acl-plugin sessions",
3711 .short_help = "clear acl-plugin sessions",
3712 .function = acl_clear_aclplugin_fn,
3716 * [un]Apply an ACL to an interface.
3717 * The ACL is applied in a given direction, either input or output.
3718 * The ACL being applied must already exist.
3721 * <b><em> set acl-plugin interface <input|output> acl <index> [del] </b></em>
3724 VLIB_CLI_COMMAND (aclplugin_set_interface_command, static) = {
3725 .path = "set acl-plugin interface",
3726 .short_help = "set acl-plugin interface <interface> <input|output> <acl INDEX> [del] ",
3727 .function = acl_set_aclplugin_interface_fn,
3731 * Create an Access Control List (ACL)
3732 * an ACL is composed of more than one Access control element (ACE). Multiple
3733 * ACEs can be specified with this command using a comma separated list.
3735 * Each ACE describes a tuple of src+dst IP prefix, ip protocol, src+dst port ranges.
3736 * (the ACL plugin also support ICMP types/codes instead of UDP/TCP ports, but
3737 * this CLI does not).
3739 * An ACL can optionally be assigned a 'tag' - which is an identifier understood
3740 * by the client. VPP does not examine it in any way.
3743 * <b><em> set acl-plugin acl <permit|deny> src <PREFIX> dst <PREFIX> proto <TCP|UDP> sport <X-Y> dport <X-Y> [tag FOO] </b></em>
3746 VLIB_CLI_COMMAND (aclplugin_set_acl_command, static) = {
3747 .path = "set acl-plugin acl",
3748 .short_help = "set acl-plugin acl <permit|deny> src <PREFIX> dst <PREFIX> proto X sport X-Y dport X-Y [tag FOO] {use comma separated list for multiple rules}",
3749 .function = acl_set_aclplugin_acl_fn,
3753 static clib_error_t *
3754 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3756 acl_main_t *am = &acl_main;
3757 u32 conn_table_hash_buckets;
3758 uword conn_table_hash_memory_size;
3759 u32 conn_table_max_entries;
3760 uword main_heap_size;
3761 uword hash_heap_size;
3762 u32 hash_lookup_hash_buckets;
3763 uword hash_lookup_hash_memory;
3764 u32 reclassify_sessions;
3765 u32 use_tuple_merge;
3766 u32 tuple_merge_split_threshold;
3768 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3771 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3772 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3775 (input, "connection hash memory %U", unformat_memory_size,
3776 &conn_table_hash_memory_size))
3777 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3778 else if (unformat (input, "connection count max %d",
3779 &conn_table_max_entries))
3780 am->fa_conn_table_max_entries = conn_table_max_entries;
3783 (input, "main heap size %U", unformat_memory_size,
3785 am->acl_mheap_size = main_heap_size;
3788 (input, "hash lookup heap size %U", unformat_memory_size,
3790 am->hash_lookup_mheap_size = hash_heap_size;
3791 else if (unformat (input, "hash lookup hash buckets %d",
3792 &hash_lookup_hash_buckets))
3793 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3796 (input, "hash lookup hash memory %U", unformat_memory_size,
3797 &hash_lookup_hash_memory))
3798 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3799 else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
3800 am->use_tuple_merge = use_tuple_merge;
3803 (input, "tuple merge split threshold %d",
3804 &tuple_merge_split_threshold))
3805 am->tuple_merge_split_threshold = tuple_merge_split_threshold;
3807 else if (unformat (input, "reclassify sessions %d",
3808 &reclassify_sessions))
3809 am->reclassify_sessions = reclassify_sessions;
3812 return clib_error_return (0, "unknown input '%U'",
3813 format_unformat_error, input);
3818 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3820 /* Set up the API message handling tables */
3821 #include <vnet/format_fns.h>
3822 #include <acl/acl.api.c>
3824 static clib_error_t *
3825 acl_init (vlib_main_t * vm)
3827 acl_main_t *am = &acl_main;
3828 clib_error_t *error = 0;
3829 clib_memset (am, 0, sizeof (*am));
3831 am->vnet_main = vnet_get_main ();
3832 am->log_default = vlib_log_register_class ("acl_plugin", 0);
3834 /* Ask for a correctly-sized block of API message decode slots */
3835 am->msg_id_base = setup_message_id_table ();
3837 error = acl_plugin_exports_init (&acl_plugin);
3842 am->acl_mheap_size = 0; /* auto size when initializing */
3843 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3845 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3846 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3848 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3849 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3850 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3851 TCP_SESSION_IDLE_TIMEOUT_SEC;
3852 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3853 UDP_SESSION_IDLE_TIMEOUT_SEC;
3855 am->fa_conn_table_hash_num_buckets =
3856 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3857 am->fa_conn_table_hash_memory_size =
3858 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3859 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3860 am->reclassify_sessions = 0;
3861 vlib_thread_main_t *tm = vlib_get_thread_main ();
3863 am->fa_min_deleted_sessions_per_interval =
3864 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3865 am->fa_max_deleted_sessions_per_interval =
3866 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3867 am->fa_cleaner_wait_time_increment =
3868 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3870 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3873 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3875 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3876 if (tm->n_vlib_mains > 1)
3878 clib_spinlock_init (&pw->pending_session_change_request_lock);
3880 vec_validate (pw->expired,
3882 am->fa_max_deleted_sessions_per_interval);
3883 _vec_len (pw->expired) = 0;
3884 vec_validate_init_empty (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1,
3885 FA_SESSION_BOGUS_INDEX);
3886 vec_validate_init_empty (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1,
3887 FA_SESSION_BOGUS_INDEX);
3888 vec_validate_init_empty (pw->fa_conn_list_head_expiry_time,
3889 ACL_N_TIMEOUTS - 1, ~0ULL);
3893 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3894 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3895 am->fa_cleaner_cnt_unknown_event = 0;
3896 am->fa_cleaner_cnt_timer_restarted = 0;
3897 am->fa_cleaner_cnt_wait_with_timeout = 0;
3900 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3903 am->l4_match_nonfirst_fragment = 1;
3905 /* use the new fancy hash-based matching */
3906 am->use_hash_acl_matching = 1;
3907 /* use tuplemerge by default */
3908 am->use_tuple_merge = 1;
3909 /* Set the default threshold */
3910 am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
3912 am->interface_acl_user_id =
3913 acl_plugin.register_user_module ("interface ACL", "sw_if_index",
3916 am->acl_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
3917 CLIB_CACHE_LINE_BYTES);
3918 am->acl_counter_lock[0] = 0; /* should be no need */
3923 VLIB_INIT_FUNCTION (acl_init);
3927 * fd.io coding-style-patch-verification: ON
3930 * eval: (c-set-style "gnu")