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 <vlibapi/api.h>
28 #include <vlibmemory/api.h>
30 /* define message IDs */
31 #include <acl/acl_msg_enum.h>
33 /* define message structures */
35 #include <acl/acl_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <acl/acl_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <acl/acl_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.h>
55 #include "public_inlines.h"
59 #define REPLY_MSG_ID_BASE am->msg_id_base
60 #include <vlibapi/api_helper_macros.h>
63 * The code for the bihash, used by the session management.
65 #include <vppinfra/bihash_40_8.h>
66 #include <vppinfra/bihash_template.h>
67 #include <vppinfra/bihash_template.c>
69 /* List of message types that this plugin understands */
71 #define foreach_acl_plugin_api_msg \
72 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
73 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
74 _(ACL_ADD_REPLACE, acl_add_replace) \
76 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
77 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
78 _(ACL_DUMP, acl_dump) \
79 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
80 _(MACIP_ACL_ADD, macip_acl_add) \
81 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
82 _(MACIP_ACL_DEL, macip_acl_del) \
83 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
84 _(MACIP_ACL_DUMP, macip_acl_dump) \
85 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
86 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
87 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
88 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump) \
89 _(ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES,acl_plugin_get_conn_table_max_entries)
93 VLIB_PLUGIN_REGISTER () = {
94 .version = VPP_BUILD_VER,
95 .description = "Access Control Lists",
99 /* methods exported from ACL-as-a-service */
100 static acl_plugin_methods_t acl_plugin;
104 format_vec16 (u8 * s, va_list * va)
106 u16 *v = va_arg (*va, u16 *);
107 char *fmt = va_arg (*va, char *);
109 for (i = 0; i < vec_len (v); i++)
112 s = format (s, ", ");
113 s = format (s, fmt, v[i]);
119 acl_set_heap (acl_main_t * am)
121 if (0 == am->acl_mheap)
123 if (0 == am->acl_mheap_size)
125 vlib_thread_main_t *tm = vlib_get_thread_main ();
126 u64 per_worker_slack = 1000000LL;
127 u64 per_worker_size =
129 ((u64) am->fa_conn_table_max_entries) * sizeof (fa_session_t);
130 u64 per_worker_size_with_slack = per_worker_slack + per_worker_size;
131 u64 main_slack = 2000000LL;
132 u64 bihash_size = (u64) am->fa_conn_table_hash_memory_size;
135 per_worker_size_with_slack * tm->n_vlib_mains + bihash_size +
138 u64 max_possible = ((uword) ~ 0);
139 if (am->acl_mheap_size > max_possible)
141 clib_warning ("ACL heap size requested: %lld, max possible %lld",
142 am->acl_mheap_size, max_possible);
145 am->acl_mheap = mheap_alloc_with_lock (0 /* use VM */ ,
148 if (0 == am->acl_mheap)
151 ("ACL plugin failed to allocate main heap of %U bytes, abort",
152 format_memory_size, am->acl_mheap_size);
155 void *oldheap = clib_mem_set_heap (am->acl_mheap);
160 acl_plugin_set_heap ()
162 acl_main_t *am = &acl_main;
163 return acl_set_heap (am);
167 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
169 clib_mem_set_heap (acl_set_heap (am));
170 #if USE_DLMALLOC == 0
171 mheap_t *h = mheap_header (am->acl_mheap);
174 h->flags |= MHEAP_FLAG_VALIDATE;
175 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
180 h->flags &= ~MHEAP_FLAG_VALIDATE;
181 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
187 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
189 clib_mem_set_heap (acl_set_heap (am));
190 #if USE_DLMALLOC == 0
191 mheap_t *h = mheap_header (am->acl_mheap);
194 h->flags |= MHEAP_FLAG_TRACE;
198 h->flags &= ~MHEAP_FLAG_TRACE;
204 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
206 acl_main_t *am = &acl_main;
207 vl_api_acl_plugin_get_version_reply_t *rmp;
208 int msg_size = sizeof (*rmp);
209 vl_api_registration_t *reg;
211 reg = vl_api_client_index_to_registration (mp->client_index);
215 rmp = vl_msg_api_alloc (msg_size);
216 clib_memset (rmp, 0, msg_size);
218 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
219 rmp->context = mp->context;
220 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
221 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
223 vl_api_send_msg (reg, (u8 *) rmp);
227 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
230 vl_api_acl_plugin_control_ping_reply_t *rmp;
231 acl_main_t *am = &acl_main;
235 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
237 rmp->vpe_pid = ntohl (getpid ());
243 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
245 clib_warning ("%v", out0);
246 vec_reset_length (out0);
250 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
252 vlib_cli_output (vm, "%v", out0);
253 vec_reset_length (out0);
256 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
259 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
260 acl_main_t * am, int acl_index)
263 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
264 am->acls[acl_index].count, am->acls[acl_index].tag);
267 for (j = 0; j < am->acls[acl_index].count; j++)
269 r = &am->acls[acl_index].rules[j];
270 out0 = format (out0, " %9d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
271 out0 = format_acl_action (out0, r->is_permit);
272 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
273 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
276 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
277 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
278 out0 = format (out0, " proto %d", r->proto);
279 out0 = format (out0, " sport %d", r->src_port_or_type_first);
280 if (r->src_port_or_type_first != r->src_port_or_type_last)
282 out0 = format (out0, "-%d", r->src_port_or_type_last);
284 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
285 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
287 out0 = format (out0, "-%d", r->dst_port_or_code_last);
289 if (r->tcp_flags_mask || r->tcp_flags_value)
292 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
295 out0 = format (out0, "\n");
301 vl_api_acl_plugin_get_conn_table_max_entries_t_handler
302 (vl_api_acl_plugin_get_conn_table_max_entries_t * mp)
304 acl_main_t *am = &acl_main;
305 vl_api_acl_plugin_get_conn_table_max_entries_reply_t *rmp;
306 int msg_size = sizeof (*rmp);
307 unix_shared_memory_queue_t *q;
309 q = vl_api_client_index_to_input_queue (mp->client_index);
315 rmp = vl_msg_api_alloc (msg_size);
316 memset (rmp, 0, msg_size);
318 ntohs (VL_API_ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES_REPLY +
320 rmp->context = mp->context;
321 rmp->conn_table_max_entries = __bswap_64 (am->fa_conn_table_max_entries);
323 vl_msg_api_send_shmem (q, (u8 *) & rmp);
327 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
329 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
333 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
335 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
339 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
342 u32 **ppolicy_epoch_by_swi =
343 is_input ? &am->input_policy_epoch_by_sw_if_index :
344 &am->output_policy_epoch_by_sw_if_index;
345 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
347 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
349 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
350 (is_input * FA_POLICY_EPOCH_IS_INPUT);
354 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
356 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
357 : &am->output_sw_if_index_vec_by_acl;
358 if (acl_num < vec_len (*p_swi_vec_by_acl))
361 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
363 increment_policy_epoch (am, *p_swi, is_input);
370 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
372 try_increment_acl_policy_epoch (am, acl_num, 0);
373 try_increment_acl_policy_epoch (am, acl_num, 1);
379 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
380 u32 * acl_list_index, u8 * tag)
382 acl_main_t *am = &acl_main;
385 acl_rule_t *acl_new_rules = 0;
388 if (am->trace_acl > 255)
389 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
392 if (*acl_list_index != ~0)
394 /* They supplied some number, let's see if this ACL exists */
395 if (pool_is_free_index (am->acls, *acl_list_index))
397 /* tried to replace a non-existent ACL, no point doing anything */
399 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
400 *acl_list_index, tag);
401 return VNET_API_ERROR_NO_SUCH_ENTRY;
407 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
408 *acl_list_index, tag);
411 void *oldheap = acl_set_heap (am);
413 /* Create and populate the rules */
415 vec_validate (acl_new_rules, count - 1);
417 for (i = 0; i < count; i++)
419 r = vec_elt_at_index (acl_new_rules, i);
420 clib_memset (r, 0, sizeof (*r));
421 r->is_permit = rules[i].is_permit;
422 r->is_ipv6 = rules[i].is_ipv6;
425 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
426 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
430 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
431 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
433 r->src_prefixlen = rules[i].src_ip_prefix_len;
434 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
435 r->proto = rules[i].proto;
436 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
437 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
438 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
439 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
440 r->tcp_flags_value = rules[i].tcp_flags_value;
441 r->tcp_flags_mask = rules[i].tcp_flags_mask;
444 if (~0 == *acl_list_index)
447 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
448 clib_memset (a, 0, sizeof (*a));
449 /* Will return the newly allocated ACL index */
450 *acl_list_index = a - am->acls;
454 a = am->acls + *acl_list_index;
455 /* Get rid of the old rules */
459 a->rules = acl_new_rules;
461 memcpy (a->tag, tag, sizeof (a->tag));
462 if (am->trace_acl > 255)
463 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
464 if (am->reclassify_sessions)
466 /* a change in an ACLs if they are applied may mean a new policy epoch */
467 policy_notify_acl_change (am, *acl_list_index);
470 /* notify the lookup contexts about the ACL changes */
471 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
472 clib_mem_set_heap (oldheap);
477 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
479 if (acl_index < vec_len (foo_index_vec_by_acl))
481 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
483 /* ACL is applied somewhere. */
491 acl_del_list (u32 acl_list_index)
493 acl_main_t *am = &acl_main;
495 if (pool_is_free_index (am->acls, acl_list_index))
497 return VNET_API_ERROR_NO_SUCH_ENTRY;
499 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
500 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
501 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
502 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
503 /* lookup contexts cover other cases, not just inbound/outbound, so check that */
504 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
505 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
507 void *oldheap = acl_set_heap (am);
509 /* now we can delete the ACL itself */
510 a = pool_elt_at_index (am->acls, acl_list_index);
513 pool_put (am->acls, a);
514 /* acl_list_index is now free, notify the lookup contexts */
515 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
516 clib_mem_set_heap (oldheap);
521 count_skip (u8 * p, u32 size)
523 u64 *p64 = (u64 *) p;
524 /* Be tolerant to null pointer */
528 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
532 return (p64 - (u64 *) p) / 2;
536 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
537 u32 mask_len, u32 next_table_index,
538 u32 miss_next_index, u32 * table_index,
542 u32 memory_size = 2 << 22;
543 u32 skip = count_skip (mask, mask_len);
544 u32 match = (mask_len / 16) - skip;
545 u8 *skip_mask_ptr = mask + 16 * skip;
546 u32 current_data_flag = 0;
547 int current_data_offset = 0;
552 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
553 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
554 memory_size, skip, match,
555 next_table_index, miss_next_index,
556 table_index, current_data_flag,
557 current_data_offset, is_add,
558 1 /* delete_chain */ );
559 clib_mem_set_heap (oldheap);
564 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
567 ? am->input_etype_whitelist_by_sw_if_index
568 : am->output_etype_whitelist_by_sw_if_index;
569 u16 *whitelist = (vec_len (v) > sw_if_index) ? vec_elt (v, sw_if_index) : 0;
570 return vec_len (whitelist) > 0;
574 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
576 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
577 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
578 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
580 clib_mem_set_heap (oldheap);
585 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
591 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
593 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
595 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
599 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
601 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
602 rv = vnet_l2_feature_enable_disable ("l2-input-ip4", "acl-plugin-in-ip4-l2",
603 sw_if_index, enable_disable, 0, 0);
605 clib_error ("Could not enable on input");
606 rv = vnet_l2_feature_enable_disable ("l2-input-ip6", "acl-plugin-in-ip6-l2",
607 sw_if_index, enable_disable, 0, 0);
609 clib_error ("Could not enable on input");
611 if (intf_has_etype_whitelist (am, sw_if_index, 1))
612 vnet_l2_feature_enable_disable ("l2-input-nonip",
613 "acl-plugin-in-nonip-l2", sw_if_index,
614 enable_disable, 0, 0);
616 clib_mem_set_heap (oldheap);
618 am->in_acl_on_sw_if_index =
619 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
625 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
631 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
633 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
635 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
639 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
641 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
643 vnet_l2_feature_enable_disable ("l2-output-ip4", "acl-plugin-out-ip4-l2",
644 sw_if_index, enable_disable, 0, 0);
646 clib_error ("Could not enable on output");
648 vnet_l2_feature_enable_disable ("l2-output-ip6", "acl-plugin-out-ip6-l2",
649 sw_if_index, enable_disable, 0, 0);
651 clib_error ("Could not enable on output");
652 if (intf_has_etype_whitelist (am, sw_if_index, 0))
653 vnet_l2_feature_enable_disable ("l2-output-nonip",
654 "acl-plugin-out-nonip-l2", sw_if_index,
655 enable_disable, 0, 0);
658 clib_mem_set_heap (oldheap);
660 am->out_acl_on_sw_if_index =
661 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
667 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
668 int is_input, int enable_disable)
671 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
673 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
677 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
679 return (pool_is_free_index (am->acls, acl_list_index));
683 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
684 u8 is_input, u32 * vec_acl_list_index,
685 int *may_clear_sessions)
688 uword *seen_acl_bitmap = 0;
689 uword *old_seen_acl_bitmap = 0;
690 uword *change_acl_bitmap = 0;
695 if (am->trace_acl > 255)
697 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
698 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
700 vec_foreach (pacln, vec_acl_list_index)
702 if (acl_is_not_defined (am, *pacln))
704 /* ACL is not defined. Can not apply */
705 clib_warning ("ERROR: ACL %d not defined", *pacln);
706 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
709 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
711 /* ACL being applied twice within the list. error. */
712 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
713 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
716 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
720 u32 **pinout_lc_index_by_sw_if_index =
722 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
724 u32 ***pinout_acl_vec_by_sw_if_index =
726 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
728 u32 ***pinout_sw_if_index_vec_by_acl =
730 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
732 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
734 clib_bitmap_validate (old_seen_acl_bitmap, 1);
736 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
738 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
741 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
743 if (am->trace_acl > 255)
744 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
745 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
746 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
749 clib_bitmap_foreach(acln, change_acl_bitmap, ({
750 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
751 /* ACL is being removed. */
752 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
753 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
754 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
757 /* ACL is being added. */
758 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
759 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
764 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
765 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
766 vec_dup (vec_acl_list_index);
768 if (am->reclassify_sessions)
770 /* re-applying ACLs means a new policy epoch */
771 increment_policy_epoch (am, sw_if_index, is_input);
775 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
776 if (may_clear_sessions && *may_clear_sessions
777 && !clib_bitmap_is_zero (change_acl_bitmap))
779 acl_clear_sessions (am, sw_if_index);
780 *may_clear_sessions = 0;
785 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
787 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
789 if (vec_len (vec_acl_list_index) > 0)
791 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
794 if (~0 == am->interface_acl_user_id)
795 am->interface_acl_user_id =
796 acl_plugin.register_user_module ("interface ACL", "sw_if_index",
799 acl_plugin.get_lookup_context_index (am->interface_acl_user_id,
800 sw_if_index, is_input);
801 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
803 acl_plugin.set_acl_vec_for_context (lc_index, vec_acl_list_index);
807 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
809 acl_plugin.put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
810 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
814 /* ensure ACL processing is enabled/disabled as needed */
815 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
816 vec_len (vec_acl_list_index) > 0);
819 clib_bitmap_free (change_acl_bitmap);
820 clib_bitmap_free (seen_acl_bitmap);
821 clib_bitmap_free (old_seen_acl_bitmap);
826 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
827 int *may_clear_sessions)
829 acl_main_t *am = &acl_main;
830 void *oldheap = acl_set_heap (am);
831 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
833 clib_mem_set_heap (oldheap);
837 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
841 acl_main_t *am = &acl_main;
843 int may_clear_sessions = 1;
845 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
846 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
848 u32 ***pinout_acl_vec_by_sw_if_index =
850 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
852 void *oldheap = acl_set_heap (am);
856 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
857 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
862 rv = error_already_applied;
866 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
867 vec_add1 (acl_vec, acl_list_index);
871 if (sw_if_index >= vec_len (*pinout_acl_vec_by_sw_if_index))
873 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
877 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
882 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
886 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
887 vec_del1 (acl_vec, index);
890 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
891 &may_clear_sessions);
894 clib_mem_set_heap (oldheap);
899 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
902 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
903 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
905 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
906 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
908 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
909 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
912 * if there are already inbound/outbound ACLs applied, toggle the
913 * enable/disable - this will recreate the necessary tables.
916 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
918 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
920 acl_interface_in_enable_disable (am, sw_if_index, 0);
921 acl_interface_in_enable_disable (am, sw_if_index, 1);
924 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
926 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
928 acl_interface_out_enable_disable (am, sw_if_index, 0);
929 acl_interface_out_enable_disable (am, sw_if_index, 1);
945 u32 dot1q_table_index;
946 u32 dot1ad_table_index;
947 u32 arp_dot1q_table_index;
948 u32 arp_dot1ad_table_index;
951 u32 out_arp_table_index;
952 u32 out_dot1q_table_index;
953 u32 out_dot1ad_table_index;
954 u32 out_arp_dot1q_table_index;
955 u32 out_arp_dot1ad_table_index;
956 } macip_match_type_t;
959 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
965 for (i = 0; i < vec_len (mv); i++)
967 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
968 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
978 /* Get metric used to sort match types.
979 The more specific and the more often seen - the bigger the metric */
981 match_type_metric (macip_match_type_t * m)
983 unsigned int mac_bits_set = 0;
984 unsigned int mac_byte;
986 for (i = 0; i < 6; i++)
988 mac_byte = m->mac_mask[i];
989 for (; mac_byte; mac_byte >>= 1)
990 mac_bits_set += mac_byte & 1;
993 * Attempt to place the more specific and the more used rules on top.
994 * There are obvious caveat corner cases to this, but they do not
995 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
996 * going with a wildcard IPv4 with a specific MAC).
998 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1002 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1004 /* Ascending sort based on the metric values */
1005 return match_type_metric (m1) - match_type_metric (m2);
1008 /* Get the offset of L3 source within ethernet packet */
1010 get_l3_src_offset (int is6)
1013 return (sizeof (ethernet_header_t) +
1014 offsetof (ip6_header_t, src_address));
1016 return (sizeof (ethernet_header_t) +
1017 offsetof (ip4_header_t, src_address));
1021 get_l3_dst_offset (int is6)
1024 return (sizeof (ethernet_header_t) +
1025 offsetof (ip6_header_t, dst_address));
1027 return (sizeof (ethernet_header_t) +
1028 offsetof (ip4_header_t, dst_address));
1032 * return if the is_permit value also requires to create the egress tables
1033 * For backwards compatibility, we keep the is_permit = 1 to only
1034 * create the ingress tables, and the new value of 3 will also
1035 * create the egress tables based on destination.
1038 macip_permit_also_egress (u8 is_permit)
1040 return (is_permit == 3);
1044 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1046 macip_match_type_t *mvec = NULL;
1047 macip_match_type_t *mt;
1048 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1050 u32 match_type_index;
1054 vnet_classify_main_t *cm = &vnet_classify_main;
1056 /* Count the number of different types of rules */
1057 for (i = 0; i < a->count; i++)
1061 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1062 a->rules[i].src_prefixlen,
1063 a->rules[i].is_ipv6)))
1065 match_type_index = vec_len (mvec);
1066 vec_validate (mvec, match_type_index);
1067 memcpy (mvec[match_type_index].mac_mask,
1068 a->rules[i].src_mac_mask, 6);
1069 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1070 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1071 mvec[match_type_index].has_egress = 0;
1072 mvec[match_type_index].table_index = ~0;
1073 mvec[match_type_index].arp_table_index = ~0;
1074 mvec[match_type_index].dot1q_table_index = ~0;
1075 mvec[match_type_index].dot1ad_table_index = ~0;
1076 mvec[match_type_index].arp_dot1q_table_index = ~0;
1077 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1078 mvec[match_type_index].out_table_index = ~0;
1079 mvec[match_type_index].out_arp_table_index = ~0;
1080 mvec[match_type_index].out_dot1q_table_index = ~0;
1081 mvec[match_type_index].out_dot1ad_table_index = ~0;
1082 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1083 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1085 mvec[match_type_index].count++;
1086 mvec[match_type_index].has_egress |=
1087 macip_permit_also_egress (a->rules[i].is_permit);
1089 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1090 vec_sort_with_function (mvec, match_type_compare);
1091 /* Create the classifier tables */
1093 out_last_table = ~0;
1094 /* First add ARP tables */
1095 vec_foreach (mt, mvec)
1098 int is6 = mt->is_ipv6;
1100 u32 *last_tag_table;
1101 u32 *out_last_tag_table;
1108 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
1109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1110 | Destination Address |
1111 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1116 | EtherType | Hardware Type |
1117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1118 | Protocol Type | Hw addr len | Proto addr len|
1119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1122 | Sender Hardware Address |
1123 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1124 | Sender Protocol Address |
1125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1126 | Target Hardware Address |
1127 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1128 | | TargetProtocolAddress |
1129 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1131 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1133 for (tags = 2; tags >= 0; tags--)
1135 clib_memset (mask, 0, sizeof (mask));
1136 /* source MAC address */
1137 memcpy (&mask[6], mt->mac_mask, 6);
1143 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1145 last_tag_table = &mt->arp_table_index;
1148 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1149 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1151 last_tag_table = &mt->arp_dot1q_table_index;
1154 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1155 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1156 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1158 last_tag_table = &mt->arp_dot1ad_table_index;
1162 /* sender hardware address within ARP */
1163 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1164 /* sender protocol address within ARP */
1165 for (i = 0; i < (mt->prefix_len / 8); i++)
1166 mask[l3_offset + 14 + i] = 0xff;
1167 if (mt->prefix_len % 8)
1168 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1169 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1171 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1173 1)) / sizeof (u32x4)) * sizeof (u32x4);
1174 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1175 (~0 == last_table) ? 0 : ~0,
1177 last_table = *last_tag_table;
1180 /* egress ARP table */
1181 clib_memset (mask, 0, sizeof (mask));
1187 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1189 out_last_tag_table = &mt->out_arp_table_index;
1192 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1193 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1195 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1198 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1199 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1200 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1202 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1206 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1207 /* mask captures just the destination and the ethertype */
1208 mask_len = ((l3_offset +
1210 1)) / sizeof (u32x4)) * sizeof (u32x4);
1211 acl_classify_add_del_table_small (cm, mask, mask_len,
1214 out_last_table) ? 0 : ~0,
1215 out_last_tag_table, 1);
1216 out_last_table = *out_last_tag_table;
1221 /* Now add IP[46] tables */
1222 vec_foreach (mt, mvec)
1225 int is6 = mt->is_ipv6;
1229 u32 *last_tag_table;
1230 u32 *out_last_tag_table;
1233 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1235 for (tags = 2; tags >= 0; tags--)
1237 clib_memset (mask, 0, sizeof (mask));
1238 memcpy (&mask[6], mt->mac_mask, 6);
1239 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1244 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1245 last_tag_table = &mt->table_index;
1248 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1249 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1250 last_tag_table = &mt->dot1q_table_index;
1253 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1254 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1255 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1256 last_tag_table = &mt->dot1ad_table_index;
1259 for (i = 0; i < (mt->prefix_len / 8); i++)
1261 mask[l3_src_offs + i] = 0xff;
1263 if (mt->prefix_len % 8)
1265 mask[l3_src_offs + (mt->prefix_len / 8)] =
1266 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1269 * Round-up the number of bytes needed to store the prefix,
1270 * and round up the number of vectors too
1272 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1273 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1274 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1275 (~0 == last_table) ? 0 : ~0,
1277 last_table = *last_tag_table;
1281 for (tags = 2; tags >= 0; tags--)
1283 clib_memset (mask, 0, sizeof (mask));
1284 /* MAC destination */
1285 memcpy (&mask[0], mt->mac_mask, 6);
1286 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1291 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1292 out_last_tag_table = &mt->out_table_index;
1295 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1296 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1297 out_last_tag_table = &mt->out_dot1q_table_index;
1300 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1301 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1302 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1303 out_last_tag_table = &mt->out_dot1ad_table_index;
1306 for (i = 0; i < (mt->prefix_len / 8); i++)
1308 mask[l3_dst_offs + i] = 0xff;
1310 if (mt->prefix_len % 8)
1312 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1313 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1316 * Round-up the number of bytes needed to store the prefix,
1317 * and round up the number of vectors too
1319 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1321 1)) / sizeof (u32x4)) * sizeof (u32x4);
1322 acl_classify_add_del_table_small (cm, mask, mask_len,
1324 (~0 == out_last_table) ? 0 : ~0,
1325 out_last_tag_table, 1);
1326 out_last_table = *out_last_tag_table;
1330 a->ip4_table_index = last_table;
1331 a->ip6_table_index = last_table;
1332 a->l2_table_index = last_table;
1334 a->out_ip4_table_index = out_last_table;
1335 a->out_ip6_table_index = out_last_table;
1336 a->out_l2_table_index = out_last_table;
1338 /* Populate the classifier tables with rules from the MACIP ACL */
1339 for (i = 0; i < a->count; i++)
1343 int is6 = a->rules[i].is_ipv6;
1350 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1351 a->rules[i].src_prefixlen,
1352 a->rules[i].is_ipv6);
1353 ASSERT (match_type_index != ~0);
1355 for (tags = 2; tags >= 0; tags--)
1357 clib_memset (mask, 0, sizeof (mask));
1358 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1359 memcpy (&mask[6], a->rules[i].src_mac, 6);
1364 tag_table = mvec[match_type_index].table_index;
1368 tag_table = mvec[match_type_index].dot1q_table_index;
1374 tag_table = mvec[match_type_index].dot1ad_table_index;
1384 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1386 mask[eth + 1] = 0xdd;
1390 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1392 mask[eth + 1] = 0x00;
1395 /* add session to table mvec[match_type_index].table_index; */
1396 vnet_classify_add_del_session (cm, tag_table,
1397 mask, a->rules[i].is_permit ? ~0 : 0,
1398 i, 0, action, metadata, 1);
1399 clib_memset (&mask[12], 0, sizeof (mask) - 12);
1402 /* add ARP table entry too */
1403 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1405 clib_memset (mask, 0, sizeof (mask));
1406 memcpy (&mask[6], a->rules[i].src_mac, 6);
1408 for (tags = 2; tags >= 0; tags--)
1414 tag_table = mvec[match_type_index].arp_table_index;
1420 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1428 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1439 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1440 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1442 vnet_classify_add_del_session (cm, tag_table, mask,
1443 a->rules[i].is_permit ? ~0 : 0,
1444 i, 0, action, metadata, 1);
1447 if (macip_permit_also_egress (a->rules[i].is_permit))
1449 /* Add the egress entry with destination set */
1450 for (tags = 2; tags >= 0; tags--)
1452 clib_memset (mask, 0, sizeof (mask));
1453 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1454 /* src mac in the other direction becomes dst */
1455 memcpy (&mask[0], a->rules[i].src_mac, 6);
1460 tag_table = mvec[match_type_index].out_table_index;
1464 tag_table = mvec[match_type_index].out_dot1q_table_index;
1470 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1480 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1483 mask[eth + 1] = 0xdd;
1487 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1490 mask[eth + 1] = 0x00;
1493 /* add session to table mvec[match_type_index].table_index; */
1494 vnet_classify_add_del_session (cm, tag_table,
1496 a->rules[i].is_permit ? ~0 : 0,
1497 i, 0, action, metadata, 1);
1498 // clib_memset (&mask[12], 0, sizeof (mask) - 12);
1501 /* add ARP table entry too */
1502 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1504 for (tags = 2; tags >= 0; tags--)
1506 clib_memset (mask, 0, sizeof (mask));
1511 tag_table = mvec[match_type_index].out_arp_table_index;
1517 mvec[match_type_index].out_arp_dot1q_table_index;
1525 mvec[match_type_index].out_arp_dot1ad_table_index;
1535 vnet_classify_add_del_session (cm, tag_table,
1538 rules[i].is_permit ? ~0 : 0,
1539 i, 0, action, metadata, 1);
1548 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1550 vnet_classify_main_t *cm = &vnet_classify_main;
1551 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1553 if (a->ip4_table_index != ~0)
1555 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1556 &a->ip4_table_index, 0);
1557 a->ip4_table_index = ~0;
1559 if (a->ip6_table_index != ~0)
1561 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1562 &a->ip6_table_index, 0);
1563 a->ip6_table_index = ~0;
1565 if (a->l2_table_index != ~0)
1567 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1569 a->l2_table_index = ~0;
1571 if (a->out_ip4_table_index != ~0)
1573 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1574 &a->out_ip4_table_index, 0);
1575 a->out_ip4_table_index = ~0;
1577 if (a->out_ip6_table_index != ~0)
1579 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1580 &a->out_ip6_table_index, 0);
1581 a->out_ip6_table_index = ~0;
1583 if (a->out_l2_table_index != ~0)
1585 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1586 &a->out_l2_table_index, 0);
1587 a->out_l2_table_index = ~0;
1592 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1598 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1600 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1601 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1603 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1604 a->ip6_table_index, a->l2_table_index,
1606 /* return the first unhappy outcome but make try to plough through. */
1609 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
1610 a->out_ip6_table_index,
1611 a->out_l2_table_index, is_apply);
1612 /* return the first unhappy outcome but make try to plough through. */
1619 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1620 u32 * acl_list_index, u8 * tag)
1622 acl_main_t *am = &acl_main;
1623 macip_acl_list_t *a;
1624 macip_acl_rule_t *r;
1625 macip_acl_rule_t *acl_new_rules = 0;
1629 if (*acl_list_index != ~0)
1631 /* They supplied some number, let's see if this MACIP ACL exists */
1632 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1634 /* tried to replace a non-existent ACL, no point doing anything */
1636 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1637 *acl_list_index, tag);
1638 return VNET_API_ERROR_NO_SUCH_ENTRY;
1645 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1648 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1649 if (~0 != *acl_list_index)
1650 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1651 void *oldheap = acl_set_heap (am);
1652 /* Create and populate the rules */
1654 vec_validate (acl_new_rules, count - 1);
1656 for (i = 0; i < count; i++)
1658 r = &acl_new_rules[i];
1659 r->is_permit = rules[i].is_permit;
1660 r->is_ipv6 = rules[i].is_ipv6;
1661 memcpy (&r->src_mac, rules[i].src_mac, 6);
1662 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1663 if (rules[i].is_ipv6)
1664 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1666 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1667 r->src_prefixlen = rules[i].src_ip_prefix_len;
1670 if (~0 == *acl_list_index)
1673 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1674 clib_memset (a, 0, sizeof (*a));
1675 /* Will return the newly allocated ACL index */
1676 *acl_list_index = a - am->macip_acls;
1680 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1683 vec_free (a->rules);
1685 macip_destroy_classify_tables (am, *acl_list_index);
1688 a->rules = acl_new_rules;
1690 memcpy (a->tag, tag, sizeof (a->tag));
1692 /* Create and populate the classifier tables */
1693 macip_create_classify_tables (am, *acl_list_index);
1694 clib_mem_set_heap (oldheap);
1695 /* If the ACL was already applied somewhere, reapply the newly created tables */
1697 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1701 /* No check that sw_if_index denotes a valid interface - the callers
1702 * were supposed to validate.
1704 * That said, if sw_if_index corresponds to an interface that exists at all,
1705 * this function must return errors accordingly if the ACL is not applied.
1709 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1712 u32 macip_acl_index;
1713 macip_acl_list_t *a;
1715 /* The vector is too short - MACIP ACL is not applied */
1716 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
1717 return VNET_API_ERROR_NO_SUCH_ENTRY;
1719 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1720 /* No point in deleting MACIP ACL which is not applied */
1721 if (~0 == macip_acl_index)
1722 return VNET_API_ERROR_NO_SUCH_ENTRY;
1724 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1725 /* remove the classifier tables off the interface L2 ACL */
1727 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1728 a->ip6_table_index, a->l2_table_index, 0);
1730 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1731 a->out_ip4_table_index, a->out_ip6_table_index,
1732 a->out_l2_table_index, 0);
1733 /* Unset the MACIP ACL index */
1734 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1735 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
1736 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
1739 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
1743 /* No check for validity of sw_if_index - the callers were supposed to validate */
1746 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1747 u32 macip_acl_index)
1749 macip_acl_list_t *a;
1751 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1753 return VNET_API_ERROR_NO_SUCH_ENTRY;
1755 void *oldheap = acl_set_heap (am);
1756 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1757 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1758 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
1759 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
1760 clib_mem_set_heap (oldheap);
1761 /* If there already a MACIP ACL applied, unapply it */
1762 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1763 macip_acl_interface_del_acl (am, sw_if_index);
1764 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1766 /* Apply the classifier tables for L2 ACLs */
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, 1);
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, 1);
1778 macip_acl_del_list (u32 acl_list_index)
1780 acl_main_t *am = &acl_main;
1781 macip_acl_list_t *a;
1783 if (pool_is_free_index (am->macip_acls, acl_list_index))
1785 return VNET_API_ERROR_NO_SUCH_ENTRY;
1788 /* delete any references to the ACL */
1789 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1791 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1793 macip_acl_interface_del_acl (am, i);
1797 void *oldheap = acl_set_heap (am);
1798 /* Now that classifier tables are detached, clean them up */
1799 macip_destroy_classify_tables (am, acl_list_index);
1801 /* now we can delete the ACL itself */
1802 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1805 vec_free (a->rules);
1807 pool_put (am->macip_acls, a);
1808 clib_mem_set_heap (oldheap);
1814 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1817 acl_main_t *am = &acl_main;
1821 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1825 rv = macip_acl_interface_del_acl (am, sw_if_index);
1831 * If the client does not allocate enough memory for a variable-length
1832 * message, and then proceed to use it as if the full memory allocated,
1833 * absent the check we happily consume that on the VPP side, and go
1834 * along as if nothing happened. However, the resulting
1835 * effects range from just garbage in the API decode
1836 * (because the decoder snoops too far), to potential memory
1839 * This verifies that the actual length of the message is
1840 * at least expected_len, and complains loudly if it is not.
1842 * A failing check here is 100% a software bug on the API user side,
1843 * so we might as well yell.
1847 verify_message_len (void *mp, u32 expected_len, char *where)
1849 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1850 if (supplied_len < expected_len)
1852 clib_warning ("%s: Supplied message length %d is less than expected %d",
1853 where, supplied_len, expected_len);
1862 /* API message handler */
1864 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1866 vl_api_acl_add_replace_reply_t *rmp;
1867 acl_main_t *am = &acl_main;
1869 u32 acl_list_index = ntohl (mp->acl_index);
1870 u32 acl_count = ntohl (mp->count);
1871 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1873 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1875 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1879 rv = VNET_API_ERROR_INVALID_VALUE;
1883 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1885 rmp->acl_index = htonl(acl_list_index);
1891 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1893 acl_main_t *am = &acl_main;
1894 vl_api_acl_del_reply_t *rmp;
1897 rv = acl_del_list (ntohl (mp->acl_index));
1899 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1903 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1905 acl_main_t *am = &acl_main;
1906 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1907 u32 sw_if_index = ntohl (mp->sw_if_index);
1908 vl_api_acl_interface_add_del_reply_t *rmp;
1911 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1912 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1915 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1916 mp->is_input, ntohl (mp->acl_index));
1918 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1922 vl_api_acl_interface_set_acl_list_t_handler
1923 (vl_api_acl_interface_set_acl_list_t * mp)
1925 acl_main_t *am = &acl_main;
1926 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1929 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1930 u32 sw_if_index = ntohl (mp->sw_if_index);
1932 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1933 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1936 int may_clear_sessions = 1;
1937 for (i = 0; i < mp->count; i++)
1939 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1941 /* ACL does not exist, so we can not apply it */
1942 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1947 void *oldheap = acl_set_heap (am);
1949 u32 *in_acl_vec = 0;
1950 u32 *out_acl_vec = 0;
1951 for (i = 0; i < mp->count; i++)
1952 if (i < mp->n_input)
1953 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
1955 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
1958 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
1959 &may_clear_sessions);
1961 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
1963 &may_clear_sessions);
1964 vec_free (in_acl_vec);
1965 vec_free (out_acl_vec);
1966 clib_mem_set_heap (oldheap);
1970 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1974 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1976 api_rule->is_permit = r->is_permit;
1977 api_rule->is_ipv6 = r->is_ipv6;
1980 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1981 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1985 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1986 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1988 api_rule->src_ip_prefix_len = r->src_prefixlen;
1989 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1990 api_rule->proto = r->proto;
1991 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1992 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1993 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1994 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1995 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1996 api_rule->tcp_flags_value = r->tcp_flags_value;
2000 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2001 acl_list_t * acl, u32 context)
2003 vl_api_acl_details_t *mp;
2004 vl_api_acl_rule_t *rules;
2006 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2007 void *oldheap = acl_set_heap (am);
2009 mp = vl_msg_api_alloc (msg_size);
2010 clib_memset (mp, 0, msg_size);
2011 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2013 /* fill in the message */
2014 mp->context = context;
2015 mp->count = htonl (acl->count);
2016 mp->acl_index = htonl (acl - am->acls);
2017 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2018 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2020 for (i = 0; i < acl->count; i++)
2022 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2025 clib_mem_set_heap (oldheap);
2026 vl_api_send_msg (reg, (u8 *) mp);
2031 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2033 acl_main_t *am = &acl_main;
2037 vl_api_registration_t *reg;
2039 reg = vl_api_client_index_to_registration (mp->client_index);
2043 if (mp->acl_index == ~0)
2046 /* Just dump all ACLs */
2047 pool_foreach (acl, am->acls,
2049 send_acl_details(am, reg, acl, mp->context);
2055 acl_index = ntohl (mp->acl_index);
2056 if (!pool_is_free_index (am->acls, acl_index))
2058 acl = pool_elt_at_index (am->acls, acl_index);
2059 send_acl_details (am, reg, acl, mp->context);
2065 /* FIXME API: should we signal an error here at all ? */
2071 send_acl_interface_list_details (acl_main_t * am,
2072 vl_api_registration_t * reg,
2073 u32 sw_if_index, u32 context)
2075 vl_api_acl_interface_list_details_t *mp;
2081 void *oldheap = acl_set_heap (am);
2083 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2084 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2086 clib_mem_set_heap (oldheap);
2088 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2089 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2090 count = n_input + n_output;
2092 msg_size = sizeof (*mp);
2093 msg_size += sizeof (mp->acls[0]) * count;
2095 mp = vl_msg_api_alloc (msg_size);
2096 clib_memset (mp, 0, msg_size);
2098 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2100 /* fill in the message */
2101 mp->context = context;
2102 mp->sw_if_index = htonl (sw_if_index);
2104 mp->n_input = n_input;
2105 for (i = 0; i < n_input; i++)
2107 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2109 for (i = 0; i < n_output; i++)
2111 mp->acls[n_input + i] =
2112 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2114 vl_api_send_msg (reg, (u8 *) mp);
2118 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2121 acl_main_t *am = &acl_main;
2122 vnet_sw_interface_t *swif;
2123 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2126 vl_api_registration_t *reg;
2128 reg = vl_api_client_index_to_registration (mp->client_index);
2132 if (mp->sw_if_index == ~0)
2135 pool_foreach (swif, im->sw_interfaces,
2137 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2143 sw_if_index = ntohl (mp->sw_if_index);
2144 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2145 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2149 /* MACIP ACL API handlers */
2152 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2154 vl_api_macip_acl_add_reply_t *rmp;
2155 acl_main_t *am = &acl_main;
2157 u32 acl_list_index = ~0;
2158 u32 acl_count = ntohl (mp->count);
2159 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2161 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2163 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2167 rv = VNET_API_ERROR_INVALID_VALUE;
2171 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2173 rmp->acl_index = htonl(acl_list_index);
2179 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2181 vl_api_macip_acl_add_replace_reply_t *rmp;
2182 acl_main_t *am = &acl_main;
2184 u32 acl_list_index = ntohl (mp->acl_index);
2185 u32 acl_count = ntohl (mp->count);
2186 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2188 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2190 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2194 rv = VNET_API_ERROR_INVALID_VALUE;
2198 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2200 rmp->acl_index = htonl(acl_list_index);
2206 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2208 acl_main_t *am = &acl_main;
2209 vl_api_macip_acl_del_reply_t *rmp;
2212 rv = macip_acl_del_list (ntohl (mp->acl_index));
2214 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2218 vl_api_macip_acl_interface_add_del_t_handler
2219 (vl_api_macip_acl_interface_add_del_t * mp)
2221 acl_main_t *am = &acl_main;
2222 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2224 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2225 u32 sw_if_index = ntohl (mp->sw_if_index);
2227 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2228 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2231 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2232 ntohl (mp->acl_index));
2234 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2238 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2239 macip_acl_list_t * acl, u32 context)
2241 vl_api_macip_acl_details_t *mp;
2242 vl_api_macip_acl_rule_t *rules;
2243 macip_acl_rule_t *r;
2245 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2247 mp = vl_msg_api_alloc (msg_size);
2248 clib_memset (mp, 0, msg_size);
2249 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2251 /* fill in the message */
2252 mp->context = context;
2255 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2256 mp->count = htonl (acl->count);
2257 mp->acl_index = htonl (acl - am->macip_acls);
2259 for (i = 0; i < acl->count; i++)
2262 rules[i].is_permit = r->is_permit;
2263 rules[i].is_ipv6 = r->is_ipv6;
2264 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2265 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2266 sizeof (r->src_mac_mask));
2268 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2269 sizeof (r->src_ip_addr.ip6));
2271 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2272 sizeof (r->src_ip_addr.ip4));
2273 rules[i].src_ip_prefix_len = r->src_prefixlen;
2278 /* No martini, no party - no ACL applied to this interface. */
2283 vl_api_send_msg (reg, (u8 *) mp);
2288 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2290 acl_main_t *am = &acl_main;
2291 macip_acl_list_t *acl;
2293 vl_api_registration_t *reg;
2295 reg = vl_api_client_index_to_registration (mp->client_index);
2299 if (mp->acl_index == ~0)
2301 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2302 pool_foreach (acl, am->macip_acls, (
2304 send_macip_acl_details (am, reg,
2313 u32 acl_index = ntohl (mp->acl_index);
2314 if (!pool_is_free_index (am->macip_acls, acl_index))
2316 acl = pool_elt_at_index (am->macip_acls, acl_index);
2317 send_macip_acl_details (am, reg, acl, mp->context);
2323 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2326 acl_main_t *am = &acl_main;
2327 vl_api_macip_acl_interface_get_reply_t *rmp;
2328 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2329 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2330 vl_api_registration_t *reg;
2333 reg = vl_api_client_index_to_registration (mp->client_index);
2337 rmp = vl_msg_api_alloc (msg_size);
2338 clib_memset (rmp, 0, msg_size);
2340 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2341 rmp->context = mp->context;
2342 rmp->count = htonl (count);
2343 for (i = 0; i < count; i++)
2345 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2348 vl_api_send_msg (reg, (u8 *) rmp);
2352 send_macip_acl_interface_list_details (acl_main_t * am,
2353 vl_api_registration_t * reg,
2355 u32 acl_index, u32 context)
2357 vl_api_macip_acl_interface_list_details_t *rmp;
2358 /* at this time there is only ever 1 mac ip acl per interface */
2359 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2361 rmp = vl_msg_api_alloc (msg_size);
2362 clib_memset (rmp, 0, msg_size);
2364 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2366 /* fill in the message */
2367 rmp->context = context;
2369 rmp->sw_if_index = htonl (sw_if_index);
2370 rmp->acls[0] = htonl (acl_index);
2372 vl_api_send_msg (reg, (u8 *) rmp);
2376 vl_api_macip_acl_interface_list_dump_t_handler
2377 (vl_api_macip_acl_interface_list_dump_t * mp)
2379 vl_api_registration_t *reg;
2380 acl_main_t *am = &acl_main;
2381 u32 sw_if_index = ntohl (mp->sw_if_index);
2383 reg = vl_api_client_index_to_registration (mp->client_index);
2387 if (sw_if_index == ~0)
2389 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2391 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2393 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2394 am->macip_acl_by_sw_if_index
2402 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2404 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2405 am->macip_acl_by_sw_if_index
2406 [sw_if_index], mp->context);
2412 vl_api_acl_interface_set_etype_whitelist_t_handler
2413 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2415 acl_main_t *am = &acl_main;
2416 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2419 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2420 u32 sw_if_index = ntohl (mp->sw_if_index);
2421 u16 *vec_in = 0, *vec_out = 0;
2422 void *oldheap = acl_set_heap (am);
2424 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2425 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2428 for (i = 0; i < mp->count; i++)
2430 if (i < mp->n_input)
2431 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2433 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2435 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2438 clib_mem_set_heap (oldheap);
2439 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2443 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2444 vl_api_registration_t * reg,
2445 u32 sw_if_index, u32 context)
2447 vl_api_acl_interface_etype_whitelist_details_t *mp;
2454 u16 *whitelist_in = 0;
2455 u16 *whitelist_out = 0;
2457 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2459 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2461 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2463 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2465 if ((0 == whitelist_in) && (0 == whitelist_out))
2466 return; /* nothing to do */
2468 void *oldheap = acl_set_heap (am);
2470 n_input = vec_len (whitelist_in);
2471 n_output = vec_len (whitelist_out);
2472 count = n_input + n_output;
2474 msg_size = sizeof (*mp);
2475 msg_size += sizeof (mp->whitelist[0]) * count;
2477 mp = vl_msg_api_alloc (msg_size);
2478 clib_memset (mp, 0, msg_size);
2480 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2482 /* fill in the message */
2483 mp->context = context;
2484 mp->sw_if_index = htonl (sw_if_index);
2486 mp->n_input = n_input;
2487 for (i = 0; i < n_input; i++)
2489 mp->whitelist[i] = htons (whitelist_in[i]);
2491 for (i = 0; i < n_output; i++)
2493 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2495 clib_mem_set_heap (oldheap);
2496 vl_api_send_msg (reg, (u8 *) mp);
2501 vl_api_acl_interface_etype_whitelist_dump_t_handler
2502 (vl_api_acl_interface_list_dump_t * mp)
2504 acl_main_t *am = &acl_main;
2505 vnet_sw_interface_t *swif;
2506 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2509 vl_api_registration_t *reg;
2511 reg = vl_api_client_index_to_registration (mp->client_index);
2515 if (mp->sw_if_index == ~0)
2518 pool_foreach (swif, im->sw_interfaces,
2520 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2526 sw_if_index = ntohl (mp->sw_if_index);
2527 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2528 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2535 /* Set up the API message handling tables */
2536 static clib_error_t *
2537 acl_plugin_api_hookup (vlib_main_t * vm)
2539 acl_main_t *am = &acl_main;
2541 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2543 vl_api_##n##_t_handler, \
2545 vl_api_##n##_t_endian, \
2546 vl_api_##n##_t_print, \
2547 sizeof(vl_api_##n##_t), 1);
2548 foreach_acl_plugin_api_msg;
2554 #define vl_msg_name_crc_list
2555 #include <acl/acl_all_api_h.h>
2556 #undef vl_msg_name_crc_list
2559 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2561 #define _(id,n,crc) \
2562 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2563 foreach_vl_msg_name_crc_acl;
2568 acl_set_timeout_sec (int timeout_type, u32 value)
2570 acl_main_t *am = &acl_main;
2571 clib_time_t *ct = &am->vlib_main->clib_time;
2573 if (timeout_type < ACL_N_TIMEOUTS)
2575 am->session_timeout_sec[timeout_type] = value;
2579 clib_warning ("Unknown timeout type %d", timeout_type);
2582 am->session_timeout[timeout_type] =
2583 (u64) (((f64) value) / ct->seconds_per_clock);
2587 acl_set_session_max_entries (u32 value)
2589 acl_main_t *am = &acl_main;
2590 am->fa_conn_table_max_entries = value;
2594 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2596 acl_main_t *am = &acl_main;
2598 if ((eh < 256) && (value < 2))
2600 am->fa_ipv6_known_eh_bitmap =
2601 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2609 static clib_error_t *
2610 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2612 acl_main_t *am = &acl_main;
2613 if (0 == am->acl_mheap)
2615 /* ACL heap is not initialized, so definitely nothing to do. */
2620 int may_clear_sessions = 1;
2621 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2622 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2624 /* also unapply any ACLs in case the users did not do so. */
2625 macip_acl_interface_del_acl (am, sw_if_index);
2626 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
2627 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
2632 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2636 static clib_error_t *
2637 acl_set_aclplugin_fn (vlib_main_t * vm,
2638 unformat_input_t * input, vlib_cli_command_t * cmd)
2640 clib_error_t *error = 0;
2644 uword memory_size = 0;
2645 acl_main_t *am = &acl_main;
2647 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2649 if (!acl_set_skip_ipv6_eh (eh_val, val))
2651 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2655 if (unformat (input, "use-hash-acl-matching %u", &val))
2657 am->use_hash_acl_matching = (val != 0);
2660 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2662 am->l4_match_nonfirst_fragment = (val != 0);
2665 if (unformat (input, "reclassify-sessions %u", &val))
2667 am->reclassify_sessions = (val != 0);
2670 if (unformat (input, "event-trace"))
2672 if (!unformat (input, "%u", &val))
2674 error = clib_error_return (0,
2675 "expecting trace level, got `%U`",
2676 format_unformat_error, input);
2681 am->trace_acl = val;
2685 if (unformat (input, "heap"))
2687 if (unformat (input, "main"))
2689 if (unformat (input, "validate %u", &val))
2690 acl_plugin_acl_set_validate_heap (am, val);
2691 else if (unformat (input, "trace %u", &val))
2692 acl_plugin_acl_set_trace_heap (am, val);
2695 else if (unformat (input, "hash"))
2697 if (unformat (input, "validate %u", &val))
2698 acl_plugin_hash_acl_set_validate_heap (val);
2699 else if (unformat (input, "trace %u", &val))
2700 acl_plugin_hash_acl_set_trace_heap (val);
2705 if (unformat (input, "session"))
2707 if (unformat (input, "table"))
2709 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2710 if (unformat (input, "max-entries"))
2712 if (!unformat (input, "%u", &val))
2714 error = clib_error_return (0,
2715 "expecting maximum number of entries, got `%U`",
2716 format_unformat_error, input);
2721 acl_set_session_max_entries (val);
2725 if (unformat (input, "hash-table-buckets"))
2727 if (!unformat (input, "%u", &val))
2729 error = clib_error_return (0,
2730 "expecting maximum number of hash table buckets, got `%U`",
2731 format_unformat_error, input);
2736 am->fa_conn_table_hash_num_buckets = val;
2740 if (unformat (input, "hash-table-memory"))
2742 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2744 error = clib_error_return (0,
2745 "expecting maximum amount of hash table memory, got `%U`",
2746 format_unformat_error, input);
2751 am->fa_conn_table_hash_memory_size = memory_size;
2755 if (unformat (input, "event-trace"))
2757 if (!unformat (input, "%u", &val))
2759 error = clib_error_return (0,
2760 "expecting trace level, got `%U`",
2761 format_unformat_error, input);
2766 am->trace_sessions = val;
2772 if (unformat (input, "timeout"))
2774 if (unformat (input, "udp"))
2776 if (unformat (input, "idle"))
2778 if (!unformat (input, "%u", &timeout))
2780 error = clib_error_return (0,
2781 "expecting timeout value in seconds, got `%U`",
2782 format_unformat_error,
2788 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2793 if (unformat (input, "tcp"))
2795 if (unformat (input, "idle"))
2797 if (!unformat (input, "%u", &timeout))
2799 error = clib_error_return (0,
2800 "expecting timeout value in seconds, got `%U`",
2801 format_unformat_error,
2807 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2811 if (unformat (input, "transient"))
2813 if (!unformat (input, "%u", &timeout))
2815 error = clib_error_return (0,
2816 "expecting timeout value in seconds, got `%U`",
2817 format_unformat_error,
2823 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2837 my_format_mac_address (u8 * s, va_list * args)
2839 u8 *a = va_arg (*args, u8 *);
2840 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2841 a[0], a[1], a[2], a[3], a[4], a[5]);
2845 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2847 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2849 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2850 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2851 format_ip46_address, &a->src_ip_addr,
2852 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2854 my_format_mac_address, a->src_mac,
2855 my_format_mac_address, a->src_mac_mask);
2860 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2862 vlib_main_t *vm = am->vlib_main;
2865 /* Don't try to print someone else's memory */
2866 if (macip_acl_index >= vec_len (am->macip_acls))
2869 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2870 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2872 vlib_cli_output (vm,
2873 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2874 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2876 vlib_cli_output (vm,
2877 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2878 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2879 vlib_cli_output (vm,
2880 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
2881 a->out_ip4_table_index, a->out_ip6_table_index,
2882 a->out_l2_table_index);
2883 for (i = 0; i < vec_len (a->rules); i++)
2884 vlib_cli_output (vm, " rule %d: %U\n", i,
2885 my_macip_acl_rule_t_pretty_format,
2886 vec_elt_at_index (a->rules, i));
2890 static clib_error_t *
2891 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2893 input, vlib_cli_command_t * cmd)
2895 clib_error_t *error = 0;
2896 acl_main_t *am = &acl_main;
2900 (void) unformat (input, "index %u", &acl_index);
2902 for (i = 0; i < vec_len (am->macip_acls); i++)
2904 /* Don't attempt to show the ACLs that do not exist */
2905 if (pool_is_free_index (am->macip_acls, i))
2908 if ((acl_index != ~0) && (acl_index != i))
2913 macip_acl_print (am, i);
2914 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
2916 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
2918 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
2926 static clib_error_t *
2927 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2929 input, vlib_cli_command_t * cmd)
2931 clib_error_t *error = 0;
2932 acl_main_t *am = &acl_main;
2934 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2936 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
2937 vec_elt (am->macip_acl_by_sw_if_index, i));
2943 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2946 vlib_main_t *vm = am->vlib_main;
2948 for (i = 0; i < vec_len (am->acls); i++)
2950 if (acl_is_not_defined (am, i))
2952 /* don't attempt to show the ACLs that do not exist */
2955 if ((acl_index != ~0) && (acl_index != i))
2959 acl_print_acl (vm, am, i);
2961 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2963 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
2964 format_vec32, am->input_sw_if_index_vec_by_acl[i],
2967 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2969 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
2970 format_vec32, am->output_sw_if_index_vec_by_acl[i],
2973 if (i < vec_len (am->lc_index_vec_by_acl))
2975 vlib_cli_output (vm, " used in lookup context index: %U\n",
2976 format_vec32, am->lc_index_vec_by_acl[i], "%d");
2981 static clib_error_t *
2982 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2983 unformat_input_t * input, vlib_cli_command_t * cmd)
2985 clib_error_t *error = 0;
2986 acl_main_t *am = &acl_main;
2989 (void) unformat (input, "index %u", &acl_index);
2991 acl_plugin_show_acl (am, acl_index);
2995 static clib_error_t *
2996 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
2997 unformat_input_t * input,
2998 vlib_cli_command_t * cmd)
3000 clib_error_t *error = 0;
3003 (void) unformat (input, "index %u", &lc_index);
3005 acl_plugin_show_lookup_context (lc_index);
3009 static clib_error_t *
3010 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3011 unformat_input_t * input,
3012 vlib_cli_command_t * cmd)
3014 clib_error_t *error = 0;
3017 (void) unformat (input, "index %u", &lc_index);
3019 acl_plugin_show_lookup_user (lc_index);
3025 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3028 vlib_main_t *vm = am->vlib_main;
3031 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3032 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3034 /* if we need a particular interface, skip all the others */
3035 if ((sw_if_index != ~0) && (sw_if_index != swi))
3038 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3039 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3040 vlib_cli_output (vm, " input policy epoch: %x\n",
3041 vec_elt (am->input_policy_epoch_by_sw_if_index,
3043 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3044 vlib_cli_output (vm, " output policy epoch: %x\n",
3045 vec_elt (am->output_policy_epoch_by_sw_if_index,
3049 if (intf_has_etype_whitelist (am, swi, 1))
3051 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3052 am->input_etype_whitelist_by_sw_if_index[swi],
3055 if (intf_has_etype_whitelist (am, swi, 0))
3057 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3058 am->output_etype_whitelist_by_sw_if_index[swi],
3062 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3063 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3065 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3066 am->input_acl_vec_by_sw_if_index[swi], "%d");
3069 vlib_cli_output (vm, "\n");
3070 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3072 acl_print_acl (vm, am, *pj);
3074 vlib_cli_output (vm, "\n");
3078 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3079 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3081 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3082 am->output_acl_vec_by_sw_if_index[swi], "%d");
3085 vlib_cli_output (vm, "\n");
3086 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3088 acl_print_acl (vm, am, *pj);
3090 vlib_cli_output (vm, "\n");
3093 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3095 vlib_cli_output (vm, " input lookup context index: %d",
3096 am->input_lc_index_by_sw_if_index[swi]);
3098 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3100 vlib_cli_output (vm, " output lookup context index: %d",
3101 am->output_lc_index_by_sw_if_index[swi]);
3108 static clib_error_t *
3109 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3110 unformat_input_t * input,
3111 vlib_cli_command_t * cmd)
3113 clib_error_t *error = 0;
3114 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3117 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3118 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3119 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3120 format_acl_plugin_5tuple, five_tuple);
3122 error = clib_error_return (0, "expecting 6 hex integers");
3127 static clib_error_t *
3128 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3130 input, vlib_cli_command_t * cmd)
3132 clib_error_t *error = 0;
3133 acl_main_t *am = &acl_main;
3135 u32 sw_if_index = ~0;
3136 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3137 int show_acl = unformat (input, "acl");
3138 int detail = unformat (input, "detail");
3140 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3144 static clib_error_t *
3145 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3146 unformat_input_t * input,
3147 vlib_cli_command_t * cmd)
3149 clib_error_t *error = 0;
3150 acl_main_t *am = &acl_main;
3152 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3155 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3159 vlib_cli_output (vm, " Not initialized\n");
3161 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3162 if (am->hash_lookup_mheap)
3164 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3168 vlib_cli_output (vm, " Not initialized\n");
3174 acl_plugin_show_sessions (acl_main_t * am,
3175 u32 show_session_thread_id,
3176 u32 show_session_session_index)
3178 vlib_main_t *vm = am->vlib_main;
3180 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3181 vnet_sw_interface_t *swif;
3182 u64 now = clib_cpu_time_now ();
3183 u64 clocks_per_second = am->vlib_main->clib_time.clocks_per_second;
3186 u64 n_adds = am->fa_session_total_adds;
3187 u64 n_dels = am->fa_session_total_dels;
3188 u64 n_deact = am->fa_session_total_deactivations;
3189 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3190 n_dels, n_adds - n_dels);
3191 vlib_cli_output (vm, "Sessions active: add %lu - deact %lu = %lu", n_adds,
3192 n_deact, n_adds - n_deact);
3193 vlib_cli_output (vm, "Sessions being purged: deact %lu - del %lu = %lu",
3194 n_deact, n_dels, n_deact - n_dels);
3196 vlib_cli_output (vm, "now: %lu clocks per second: %lu", now,
3198 vlib_cli_output (vm, "\n\nPer-thread data:");
3199 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3201 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3202 vlib_cli_output (vm, "Thread #%d:", wk);
3203 if (show_session_thread_id == wk
3204 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3206 vlib_cli_output (vm, " session index %u:",
3207 show_session_session_index);
3208 fa_session_t *sess =
3209 pw->fa_sessions_pool + show_session_session_index;
3210 u64 *m = (u64 *) & sess->info;
3211 vlib_cli_output (vm,
3212 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3213 m[0], m[1], m[2], m[3], m[4], m[5]);
3214 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3215 vlib_cli_output (vm, " tcp_flags_seen: %x",
3216 sess->tcp_flags_seen.as_u16);
3217 vlib_cli_output (vm, " last active time: %lu",
3218 sess->last_active_time);
3219 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3220 vlib_cli_output (vm, " link enqueue time: %lu",
3221 sess->link_enqueue_time);
3222 vlib_cli_output (vm, " link next index: %u",
3223 sess->link_next_idx);
3224 vlib_cli_output (vm, " link prev index: %u",
3225 sess->link_prev_idx);
3226 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3228 vlib_cli_output (vm, " connection add/del stats:", wk);
3229 pool_foreach (swif, im->sw_interfaces, (
3236 (pw->fa_session_adds_by_sw_if_index)
3238 pw->fa_session_adds_by_sw_if_index
3243 (pw->fa_session_dels_by_sw_if_index)
3245 pw->fa_session_dels_by_sw_if_index
3247 u64 n_epoch_changes =
3250 (pw->fa_session_epoch_change_by_sw_if_index)
3252 pw->fa_session_epoch_change_by_sw_if_index
3254 vlib_cli_output (vm,
3255 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3265 vlib_cli_output (vm, " connection timeout type lists:", wk);
3267 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3269 u32 head_session_index = pw->fa_conn_list_head[tt];
3270 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3271 head_session_index);
3272 if (~0 != head_session_index)
3274 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3275 vlib_cli_output (vm, " last active time: %lu",
3276 sess->last_active_time);
3277 vlib_cli_output (vm, " link enqueue time: %lu",
3278 sess->link_enqueue_time);
3282 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3283 vlib_cli_output (vm, " Requeue until time: %lu",
3284 pw->requeue_until_time);
3285 vlib_cli_output (vm, " Current time wait interval: %lu",
3286 pw->current_time_wait_interval);
3287 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3288 pw->cnt_deleted_sessions);
3289 vlib_cli_output (vm, " Delete already deleted: %lu",
3290 pw->cnt_already_deleted_sessions);
3291 vlib_cli_output (vm, " Session timers restarted: %lu",
3292 pw->cnt_session_timer_restarted);
3293 vlib_cli_output (vm, " Swipe until this time: %lu",
3294 pw->swipe_end_time);
3295 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3296 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3297 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3299 pw->pending_clear_sw_if_index_bitmap);
3300 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3301 vlib_cli_output (vm, " interrupt is pending: %d",
3302 pw->interrupt_is_pending);
3303 vlib_cli_output (vm, " interrupt is needed: %d",
3304 pw->interrupt_is_needed);
3305 vlib_cli_output (vm, " interrupt is unwanted: %d",
3306 pw->interrupt_is_unwanted);
3307 vlib_cli_output (vm, " interrupt generation: %d",
3308 pw->interrupt_generation);
3309 vlib_cli_output (vm, " received session change requests: %d",
3310 pw->rcvd_session_change_requests);
3311 vlib_cli_output (vm, " sent session change requests: %d",
3312 pw->sent_session_change_requests);
3314 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3315 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3316 foreach_fa_cleaner_counter;
3318 vlib_cli_output (vm, "Interrupt generation: %d",
3319 am->fa_interrupt_generation);
3320 vlib_cli_output (vm,
3321 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3322 am->fa_min_deleted_sessions_per_interval,
3323 am->fa_max_deleted_sessions_per_interval,
3324 am->fa_cleaner_wait_time_increment * 1000.0,
3325 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3326 1000.0 / (f64) vm->clib_time.clocks_per_second);
3327 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3330 static clib_error_t *
3331 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3332 unformat_input_t * input,
3333 vlib_cli_command_t * cmd)
3335 clib_error_t *error = 0;
3336 acl_main_t *am = &acl_main;
3338 u32 show_bihash_verbose = 0;
3339 u32 show_session_thread_id = ~0;
3340 u32 show_session_session_index = ~0;
3341 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3342 &show_session_session_index);
3343 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3345 acl_plugin_show_sessions (am, show_session_thread_id,
3346 show_session_session_index);
3347 show_fa_sessions_hash (vm, show_bihash_verbose);
3351 static clib_error_t *
3352 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3353 unformat_input_t * input,
3354 vlib_cli_command_t * cmd)
3356 clib_error_t *error = 0;
3360 int show_acl_hash_info = 0;
3361 int show_applied_info = 0;
3362 int show_mask_type = 0;
3363 int show_bihash = 0;
3364 u32 show_bihash_verbose = 0;
3366 if (unformat (input, "acl"))
3368 show_acl_hash_info = 1;
3369 /* mask-type is handy to see as well right there */
3371 unformat (input, "index %u", &acl_index);
3373 else if (unformat (input, "applied"))
3375 show_applied_info = 1;
3376 unformat (input, "lc_index %u", &lc_index);
3378 else if (unformat (input, "mask"))
3382 else if (unformat (input, "hash"))
3385 unformat (input, "verbose %u", &show_bihash_verbose);
3389 (show_mask_type || show_acl_hash_info || show_applied_info
3392 /* if no qualifiers specified, show all */
3394 show_acl_hash_info = 1;
3395 show_applied_info = 1;
3399 acl_plugin_show_tables_mask_type ();
3400 if (show_acl_hash_info)
3401 acl_plugin_show_tables_acl_hash_info (acl_index);
3402 if (show_applied_info)
3403 acl_plugin_show_tables_applied_info (lc_index);
3405 acl_plugin_show_tables_bihash (show_bihash_verbose);
3410 static clib_error_t *
3411 acl_clear_aclplugin_fn (vlib_main_t * vm,
3412 unformat_input_t * input, vlib_cli_command_t * cmd)
3414 clib_error_t *error = 0;
3415 acl_main_t *am = &acl_main;
3416 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3417 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3422 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3423 .path = "set acl-plugin",
3424 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3425 .function = acl_set_aclplugin_fn,
3428 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3429 .path = "show acl-plugin acl",
3430 .short_help = "show acl-plugin acl [index N]",
3431 .function = acl_show_aclplugin_acl_fn,
3434 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3435 .path = "show acl-plugin lookup context",
3436 .short_help = "show acl-plugin lookup context [index N]",
3437 .function = acl_show_aclplugin_lookup_context_fn,
3440 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3441 .path = "show acl-plugin lookup user",
3442 .short_help = "show acl-plugin lookup user [index N]",
3443 .function = acl_show_aclplugin_lookup_user_fn,
3446 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3447 .path = "show acl-plugin decode 5tuple",
3448 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3449 .function = acl_show_aclplugin_decode_5tuple_fn,
3452 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3453 .path = "show acl-plugin interface",
3454 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3455 .function = acl_show_aclplugin_interface_fn,
3458 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3459 .path = "show acl-plugin memory",
3460 .short_help = "show acl-plugin memory",
3461 .function = acl_show_aclplugin_memory_fn,
3464 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3465 .path = "show acl-plugin sessions",
3466 .short_help = "show acl-plugin sessions",
3467 .function = acl_show_aclplugin_sessions_fn,
3470 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3471 .path = "show acl-plugin tables",
3472 .short_help = "show acl-plugin tables [ acl [index N] | applied [ lc_index N ] | mask | hash [verbose N] ]",
3473 .function = acl_show_aclplugin_tables_fn,
3476 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3477 .path = "show acl-plugin macip acl",
3478 .short_help = "show acl-plugin macip acl [index N]",
3479 .function = acl_show_aclplugin_macip_acl_fn,
3482 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3483 .path = "show acl-plugin macip interface",
3484 .short_help = "show acl-plugin macip interface",
3485 .function = acl_show_aclplugin_macip_interface_fn,
3488 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3489 .path = "clear acl-plugin sessions",
3490 .short_help = "clear acl-plugin sessions",
3491 .function = acl_clear_aclplugin_fn,
3495 static clib_error_t *
3496 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3498 acl_main_t *am = &acl_main;
3499 u32 conn_table_hash_buckets;
3500 uword conn_table_hash_memory_size;
3501 u32 conn_table_max_entries;
3502 uword main_heap_size;
3503 uword hash_heap_size;
3504 u32 hash_lookup_hash_buckets;
3505 uword hash_lookup_hash_memory;
3506 u32 reclassify_sessions;
3507 u32 use_tuple_merge;
3508 u32 tuple_merge_split_threshold;
3510 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3513 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3514 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3517 (input, "connection hash memory %U", unformat_memory_size,
3518 &conn_table_hash_memory_size))
3519 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3520 else if (unformat (input, "connection count max %d",
3521 &conn_table_max_entries))
3522 am->fa_conn_table_max_entries = conn_table_max_entries;
3525 (input, "main heap size %U", unformat_memory_size,
3527 am->acl_mheap_size = main_heap_size;
3530 (input, "hash lookup heap size %U", unformat_memory_size,
3532 am->hash_lookup_mheap_size = hash_heap_size;
3533 else if (unformat (input, "hash lookup hash buckets %d",
3534 &hash_lookup_hash_buckets))
3535 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3538 (input, "hash lookup hash memory %U", unformat_memory_size,
3539 &hash_lookup_hash_memory))
3540 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3541 else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
3542 am->use_tuple_merge = use_tuple_merge;
3545 (input, "tuple merge split threshold %d",
3546 &tuple_merge_split_threshold))
3547 am->tuple_merge_split_threshold = tuple_merge_split_threshold;
3549 else if (unformat (input, "reclassify sessions %d",
3550 &reclassify_sessions))
3551 am->reclassify_sessions = reclassify_sessions;
3554 return clib_error_return (0, "unknown input '%U'",
3555 format_unformat_error, input);
3560 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3562 static clib_error_t *
3563 acl_init (vlib_main_t * vm)
3565 acl_main_t *am = &acl_main;
3566 clib_error_t *error = 0;
3567 clib_memset (am, 0, sizeof (*am));
3569 am->vnet_main = vnet_get_main ();
3570 am->log_default = vlib_log_register_class ("acl_plugin", 0);
3572 u8 *name = format (0, "acl_%08x%c", api_version, 0);
3574 /* Ask for a correctly-sized block of API message decode slots */
3575 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3576 VL_MSG_FIRST_AVAILABLE);
3578 error = acl_plugin_api_hookup (vm);
3580 /* Add our API messages to the global name_crc hash table */
3581 setup_message_id_table (am, &api_main);
3588 error = acl_plugin_exports_init (&acl_plugin);
3593 am->acl_mheap_size = 0; /* auto size when initializing */
3594 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3596 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3597 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3599 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3600 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3601 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3602 TCP_SESSION_IDLE_TIMEOUT_SEC;
3603 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3604 UDP_SESSION_IDLE_TIMEOUT_SEC;
3606 am->fa_conn_table_hash_num_buckets =
3607 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3608 am->fa_conn_table_hash_memory_size =
3609 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3610 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3611 am->reclassify_sessions = 0;
3612 vlib_thread_main_t *tm = vlib_get_thread_main ();
3614 am->fa_min_deleted_sessions_per_interval =
3615 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3616 am->fa_max_deleted_sessions_per_interval =
3617 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3618 am->fa_cleaner_wait_time_increment =
3619 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3621 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3624 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3626 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3627 if (tm->n_vlib_mains > 1)
3629 clib_spinlock_init (&pw->pending_session_change_request_lock);
3631 vec_validate (pw->expired,
3633 am->fa_max_deleted_sessions_per_interval);
3634 _vec_len (pw->expired) = 0;
3635 vec_validate_init_empty (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1,
3636 FA_SESSION_BOGUS_INDEX);
3637 vec_validate_init_empty (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1,
3638 FA_SESSION_BOGUS_INDEX);
3639 vec_validate_init_empty (pw->fa_conn_list_head_expiry_time,
3640 ACL_N_TIMEOUTS - 1, ~0ULL);
3644 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3645 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3646 am->fa_cleaner_cnt_unknown_event = 0;
3647 am->fa_cleaner_cnt_timer_restarted = 0;
3648 am->fa_cleaner_cnt_wait_with_timeout = 0;
3651 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3654 am->l4_match_nonfirst_fragment = 1;
3656 /* use the new fancy hash-based matching */
3657 am->use_hash_acl_matching = 1;
3658 /* use tuplemerge by default */
3659 am->use_tuple_merge = 1;
3660 /* Set the default threshold */
3661 am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
3663 am->interface_acl_user_id = ~0; /* defer till the first use */
3668 VLIB_INIT_FUNCTION (acl_init);
3672 * fd.io coding-style-patch-verification: ON
3675 * eval: (c-set-style "gnu")