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/classify/in_out_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
32 /* define message structures */
34 #include <acl/acl_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <acl/acl_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <acl/acl_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
54 #include "public_inlines.h"
57 acl_main_t *p_acl_main = &acl_main;
59 #define REPLY_MSG_ID_BASE am->msg_id_base
60 #include <vlibapi/api_helper_macros.h>
62 /* List of message types that this plugin understands */
64 #define foreach_acl_plugin_api_msg \
65 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
66 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
67 _(ACL_ADD_REPLACE, acl_add_replace) \
69 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
70 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
71 _(ACL_DUMP, acl_dump) \
72 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
73 _(MACIP_ACL_ADD, macip_acl_add) \
74 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
75 _(MACIP_ACL_DEL, macip_acl_del) \
76 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
77 _(MACIP_ACL_DUMP, macip_acl_dump) \
78 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
79 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
80 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
81 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
85 VLIB_PLUGIN_REGISTER () = {
86 .version = VPP_BUILD_VER,
87 .description = "Access Control Lists",
93 format_vec16 (u8 * s, va_list * va)
95 u16 *v = va_arg (*va, u16 *);
96 char *fmt = va_arg (*va, char *);
98 for (i = 0; i < vec_len (v); i++)
101 s = format (s, ", ");
102 s = format (s, fmt, v[i]);
110 acl_plugin_acl_exists (u32 acl_index)
112 acl_main_t *am = &acl_main;
114 if (pool_is_free_index (am->acls, acl_index))
121 acl_set_heap (acl_main_t * am)
123 if (0 == am->acl_mheap)
125 if (0 == am->acl_mheap_size)
127 vlib_thread_main_t *tm = vlib_get_thread_main ();
128 u64 per_worker_slack = 1000000LL;
129 u64 per_worker_size =
131 ((u64) am->fa_conn_table_max_entries) * sizeof (fa_session_t);
132 u64 per_worker_size_with_slack = per_worker_slack + per_worker_size;
133 u64 main_slack = 2000000LL;
134 u64 bihash_size = (u64) am->fa_conn_table_hash_memory_size;
137 per_worker_size_with_slack * tm->n_vlib_mains + bihash_size +
140 u64 max_possible = ((uword) ~ 0);
141 if (am->acl_mheap_size > max_possible)
143 clib_warning ("ACL heap size requested: %lld, max possible %lld",
144 am->acl_mheap_size, max_possible);
146 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
147 if (0 == am->acl_mheap)
150 ("ACL plugin failed to allocate main heap of %U bytes, abort",
151 format_memory_size, am->acl_mheap_size);
153 mheap_t *h = mheap_header (am->acl_mheap);
154 h->flags |= MHEAP_FLAG_THREAD_SAFE;
156 void *oldheap = clib_mem_set_heap (am->acl_mheap);
161 acl_plugin_set_heap ()
163 acl_main_t *am = &acl_main;
164 return acl_set_heap (am);
168 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
170 clib_mem_set_heap (acl_set_heap (am));
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;
186 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
188 clib_mem_set_heap (acl_set_heap (am));
189 mheap_t *h = mheap_header (am->acl_mheap);
192 h->flags |= MHEAP_FLAG_TRACE;
196 h->flags &= ~MHEAP_FLAG_TRACE;
201 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
203 acl_main_t *am = &acl_main;
204 vl_api_acl_plugin_get_version_reply_t *rmp;
205 int msg_size = sizeof (*rmp);
206 vl_api_registration_t *reg;
208 reg = vl_api_client_index_to_registration (mp->client_index);
212 rmp = vl_msg_api_alloc (msg_size);
213 memset (rmp, 0, msg_size);
215 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
216 rmp->context = mp->context;
217 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
218 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
220 vl_api_send_msg (reg, (u8 *) rmp);
224 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
227 vl_api_acl_plugin_control_ping_reply_t *rmp;
228 acl_main_t *am = &acl_main;
232 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
234 rmp->vpe_pid = ntohl (getpid ());
240 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
242 clib_warning ("%v", out0);
243 vec_reset_length (out0);
247 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
249 vlib_cli_output (vm, "%v", out0);
250 vec_reset_length (out0);
253 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
256 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
257 acl_main_t * am, int acl_index)
260 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
261 am->acls[acl_index].count, am->acls[acl_index].tag);
264 for (j = 0; j < am->acls[acl_index].count; j++)
266 r = &am->acls[acl_index].rules[j];
267 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
268 out0 = format_acl_action (out0, r->is_permit);
269 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
270 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
273 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
274 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
275 out0 = format (out0, " proto %d", r->proto);
276 out0 = format (out0, " sport %d", r->src_port_or_type_first);
277 if (r->src_port_or_type_first != r->src_port_or_type_last)
279 out0 = format (out0, "-%d", r->src_port_or_type_last);
281 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
282 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
284 out0 = format (out0, "-%d", r->dst_port_or_code_last);
286 if (r->tcp_flags_mask || r->tcp_flags_value)
289 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
292 out0 = format (out0, "\n");
298 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
300 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
304 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
306 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
310 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
313 u32 **ppolicy_epoch_by_swi =
314 is_input ? &am->input_policy_epoch_by_sw_if_index :
315 &am->output_policy_epoch_by_sw_if_index;
316 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
318 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
320 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
321 (is_input * FA_POLICY_EPOCH_IS_INPUT);
325 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
327 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
328 : &am->output_sw_if_index_vec_by_acl;
329 if (acl_num < vec_len (*p_swi_vec_by_acl))
332 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
334 increment_policy_epoch (am, *p_swi, is_input);
341 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
343 try_increment_acl_policy_epoch (am, acl_num, 0);
344 try_increment_acl_policy_epoch (am, acl_num, 1);
350 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
351 u32 * acl_list_index, u8 * tag)
353 acl_main_t *am = &acl_main;
356 acl_rule_t *acl_new_rules = 0;
359 if (am->trace_acl > 255)
360 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
363 if (*acl_list_index != ~0)
365 /* They supplied some number, let's see if this ACL exists */
366 if (pool_is_free_index (am->acls, *acl_list_index))
368 /* tried to replace a non-existent ACL, no point doing anything */
370 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
371 *acl_list_index, tag);
372 return VNET_API_ERROR_NO_SUCH_ENTRY;
378 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
379 *acl_list_index, tag);
382 void *oldheap = acl_set_heap (am);
384 /* Create and populate the rules */
386 vec_validate (acl_new_rules, count - 1);
388 for (i = 0; i < count; i++)
390 r = vec_elt_at_index (acl_new_rules, i);
391 memset (r, 0, sizeof (*r));
392 r->is_permit = rules[i].is_permit;
393 r->is_ipv6 = rules[i].is_ipv6;
396 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
397 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
401 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
402 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
404 r->src_prefixlen = rules[i].src_ip_prefix_len;
405 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
406 r->proto = rules[i].proto;
407 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
408 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
409 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
410 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
411 r->tcp_flags_value = rules[i].tcp_flags_value;
412 r->tcp_flags_mask = rules[i].tcp_flags_mask;
415 if (~0 == *acl_list_index)
418 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
419 memset (a, 0, sizeof (*a));
420 /* Will return the newly allocated ACL index */
421 *acl_list_index = a - am->acls;
425 a = am->acls + *acl_list_index;
426 /* Get rid of the old rules */
430 a->rules = acl_new_rules;
432 memcpy (a->tag, tag, sizeof (a->tag));
433 if (am->trace_acl > 255)
434 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
435 if (am->reclassify_sessions)
437 /* a change in an ACLs if they are applied may mean a new policy epoch */
438 policy_notify_acl_change (am, *acl_list_index);
441 /* notify the lookup contexts about the ACL changes */
442 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
443 clib_mem_set_heap (oldheap);
448 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
450 if (acl_index < vec_len (foo_index_vec_by_acl))
452 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
454 /* ACL is applied somewhere. */
462 acl_del_list (u32 acl_list_index)
464 acl_main_t *am = &acl_main;
466 if (pool_is_free_index (am->acls, acl_list_index))
468 return VNET_API_ERROR_NO_SUCH_ENTRY;
470 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
471 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
472 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
473 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
474 /* lookup contexts cover other cases, not just inbound/oubound, so check that */
475 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
476 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
478 void *oldheap = acl_set_heap (am);
480 /* now we can delete the ACL itself */
481 a = pool_elt_at_index (am->acls, acl_list_index);
484 pool_put (am->acls, a);
485 /* acl_list_index is now free, notify the lookup contexts */
486 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
487 clib_mem_set_heap (oldheap);
491 /* Some aids in ASCII graphing the content */
498 u8 ip4_5tuple_mask[] =
499 _(" dmac smac etype ")
500 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
507 _(" ttl pr checksum ")
516 _("L4 T/U sport dport ")
526 u8 ip6_5tuple_mask[] =
527 _(" dmac smac etype ")
528 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
530 _(0x0000) __ __ __ __
532 _(0x0004) __ __ XX __
534 _(0x0008) XX XX XX XX
535 _(0x000C) XX XX XX XX
536 _(0x0010) XX XX XX XX
537 _(0x0014) XX XX XX XX
539 _(0x0018) XX XX XX XX
540 _(0x001C) XX XX XX XX
541 _(0x0020) XX XX XX XX
542 _(0x0024) XX XX XX XX
543 _("L4T/U sport dport ")
544 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
546 u8 dot1q_5tuple_mask[] =
547 _(" dmac smac dot1q etype ")
548 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
549 _(padpad) __ __ __ __
550 _(padpad) __ __ __ __
551 _(padpad) __ __ __ __
554 u8 dot1ad_5tuple_mask[] =
555 _(" dmac smac dot1ad dot1q etype ")
556 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
557 _(padpad) __ __ __ __
558 _(padpad) __ __ __ __
561 u8 ethertype_mask[] =
562 _(" dmac smac etype ")
563 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
572 count_skip (u8 * p, u32 size)
574 u64 *p64 = (u64 *) p;
575 /* Be tolerant to null pointer */
579 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
583 return (p64 - (u64 *) p) / 2;
587 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
588 u32 mask_len, u32 next_table_index,
589 u32 miss_next_index, u32 * table_index,
593 u32 memory_size = 2 << 13;
594 u32 skip = count_skip (mask, mask_len);
595 u32 match = (mask_len / 16) - skip;
596 u8 *skip_mask_ptr = mask + 16 * skip;
597 u32 current_data_flag = 0;
598 int current_data_offset = 0;
602 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
603 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
604 memory_size, skip, match,
605 next_table_index, miss_next_index,
606 table_index, current_data_flag,
607 current_data_offset, is_add,
608 1 /* delete_chain */ );
609 clib_mem_set_heap (oldheap);
614 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
615 u32 mask_len, u32 next_table_index,
616 u32 miss_next_index, u32 * table_index,
620 u32 memory_size = 2 << 22;
621 u32 skip = count_skip (mask, mask_len);
622 u32 match = (mask_len / 16) - skip;
623 u8 *skip_mask_ptr = mask + 16 * skip;
624 u32 current_data_flag = 0;
625 int current_data_offset = 0;
630 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
631 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
632 memory_size, skip, match,
633 next_table_index, miss_next_index,
634 table_index, current_data_flag,
635 current_data_offset, is_add,
636 1 /* delete_chain */ );
637 clib_mem_set_heap (oldheap);
642 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
644 vnet_classify_main_t *cm = &vnet_classify_main;
645 u32 ip4_table_index = ~0;
646 u32 ip6_table_index = ~0;
647 u32 dot1q_table_index = ~0;
648 u32 dot1ad_table_index = ~0;
649 u32 etype_table_index = ~0;
650 void *oldheap = acl_set_heap (am);
652 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
654 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
656 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
658 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
660 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
663 /* switch to global heap while calling vnet_* functions */
664 clib_mem_set_heap (cm->vlib_main->heap_base);
665 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
667 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
670 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
671 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
672 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
673 sizeof (ip4_5tuple_mask) - 1, ~0,
674 am->l2_input_classify_next_acl_ip4,
675 &ip4_table_index, 0);
677 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
680 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
681 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
682 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
683 sizeof (ip6_5tuple_mask) - 1, ~0,
684 am->l2_input_classify_next_acl_ip6,
685 &ip6_table_index, 0);
687 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
690 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
691 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
692 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
693 sizeof (ip6_5tuple_mask) - 1, ~0,
694 ~0, &dot1q_table_index, 0);
696 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
699 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
700 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
701 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
702 sizeof (dot1ad_5tuple_mask) - 1, ~0,
703 ~0, &dot1ad_table_index, 0);
705 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
708 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
709 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
710 acl_classify_add_del_table_tiny (cm, ethertype_mask,
711 sizeof (ethertype_mask) - 1, ~0,
712 ~0, &etype_table_index, 0);
714 clib_mem_set_heap (oldheap);
719 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
721 vnet_classify_main_t *cm = &vnet_classify_main;
722 u32 ip4_table_index = ~0;
723 u32 ip6_table_index = ~0;
724 u32 dot1q_table_index = ~0;
725 u32 dot1ad_table_index = ~0;
726 u32 etype_table_index = ~0;
727 void *oldheap = acl_set_heap (am);
729 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
731 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
733 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
735 vec_validate_init_empty
736 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
737 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
740 /* switch to global heap while calling vnet_* functions */
741 clib_mem_set_heap (cm->vlib_main->heap_base);
743 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
745 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
748 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
749 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
750 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
751 sizeof (ip4_5tuple_mask) - 1, ~0,
752 am->l2_output_classify_next_acl_ip4,
753 &ip4_table_index, 0);
755 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
758 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
759 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
760 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
761 sizeof (ip6_5tuple_mask) - 1, ~0,
762 am->l2_output_classify_next_acl_ip6,
763 &ip6_table_index, 0);
765 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
768 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
769 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
770 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
771 sizeof (ip6_5tuple_mask) - 1, ~0,
772 ~0, &dot1q_table_index, 0);
774 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
777 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
778 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
779 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
780 sizeof (dot1ad_5tuple_mask) - 1, ~0,
781 ~0, &dot1ad_table_index, 0);
783 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
786 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
787 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
788 acl_classify_add_del_table_tiny (cm, ethertype_mask,
789 sizeof (ethertype_mask) - 1, ~0,
790 ~0, &etype_table_index, 0);
792 clib_mem_set_heap (oldheap);
797 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
798 u8 is_dot1ad, u8 is_ip6)
800 vnet_classify_main_t *cm = &vnet_classify_main;
810 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
816 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
818 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
819 idx = (is_dot1ad) ? 20 : 16;
822 /* 802.1ad ethertype */
825 /* 802.1q ethertype */
831 /* 802.1q ethertype */
836 /* add sessions to vlan tables per ethernet_type */
840 match[idx + 1] = 0xdd;
846 match[idx + 1] = 0x00;
849 vnet_classify_add_del_session (cm, table_index, match, next_acl,
850 session_idx, 0, 0, 0, 1);
851 /* reset the mask back to being a mask */
853 match[idx + 1] = 0xff;
864 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
867 ? am->input_etype_whitelist_by_sw_if_index
868 : am->output_etype_whitelist_by_sw_if_index;
869 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
873 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
874 u32 etype_table_index)
876 vnet_classify_main_t *cm = &vnet_classify_main;
878 ? am->input_etype_whitelist_by_sw_if_index
879 : am->output_etype_whitelist_by_sw_if_index;
880 u8 *match = ethertype_mask;
884 u16 *whitelist = vec_elt (v, sw_if_index);
885 u32 next = ~0; /* permit */
886 for (i = 0; i < vec_len (whitelist); i++)
889 match[12] = (whitelist[i] >> 8) & 0xff;
890 match[13] = whitelist[i] & 0xff;
892 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
893 whitelist[i], 0, 0, 0, 1);
896 /* restore the mask */
903 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
905 vnet_classify_main_t *cm = &vnet_classify_main;
906 u32 ip4_table_index = ~0;
907 u32 ip6_table_index = ~0;
908 u32 dot1q_table_index = ~0;
909 u32 dot1ad_table_index = ~0;
910 u32 etype_table_index = ~0;
913 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
915 /* in case there were previous tables attached */
916 acl_unhook_l2_input_classify (am, sw_if_index);
918 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
919 sizeof (ip4_5tuple_mask) - 1, ~0,
920 am->l2_input_classify_next_acl_ip4,
921 &ip4_table_index, 1);
926 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
927 sizeof (ip6_5tuple_mask) - 1, ~0,
928 am->l2_input_classify_next_acl_ip6,
929 &ip6_table_index, 1);
932 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
933 sizeof (ip4_5tuple_mask) - 1, ~0,
934 am->l2_input_classify_next_acl_ip4,
935 &ip4_table_index, 0);
939 if (intf_has_etype_whitelist (am, sw_if_index, 1))
941 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
942 &etype_table_index, 1);
943 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
947 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
948 sizeof (dot1ad_5tuple_mask) - 1,
949 etype_table_index, ~0,
950 &dot1ad_table_index, 1);
952 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
953 sizeof (dot1q_5tuple_mask) - 1,
954 dot1ad_table_index, ~0,
955 &dot1q_table_index, 1);
958 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
959 sizeof (dot1ad_5tuple_mask) - 1, ~0,
960 ~0, &dot1ad_table_index, 0);
961 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
962 sizeof (ip6_5tuple_mask) - 1, ~0,
963 am->l2_input_classify_next_acl_ip6,
964 &ip6_table_index, 0);
965 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
966 sizeof (ip4_5tuple_mask) - 1, ~0,
967 am->l2_input_classify_next_acl_ip4,
968 &ip4_table_index, 0);
973 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
974 ip6_table_index, dot1q_table_index);
978 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
979 sizeof (ip4_5tuple_mask) - 1, ~0,
980 am->l2_input_classify_next_acl_ip4,
981 &ip4_table_index, 0);
982 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
983 sizeof (ip6_5tuple_mask) - 1, ~0,
984 am->l2_input_classify_next_acl_ip6,
985 &ip6_table_index, 0);
986 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
987 sizeof (dot1q_5tuple_mask) - 1, ~0,
988 ~0, &dot1q_table_index, 0);
989 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
990 sizeof (dot1ad_5tuple_mask) - 1, ~0,
991 ~0, &dot1ad_table_index, 0);
995 /* add sessions to vlan tables per ethernet_type */
996 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
997 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
998 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
999 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
1001 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
1003 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
1005 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
1007 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1009 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1011 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
1014 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
1016 clib_mem_set_heap (prevheap);
1021 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
1023 vnet_classify_main_t *cm = &vnet_classify_main;
1024 u32 ip4_table_index = ~0;
1025 u32 ip6_table_index = ~0;
1026 u32 dot1q_table_index = ~0;
1027 u32 dot1ad_table_index = ~0;
1028 u32 etype_table_index = ~0;
1031 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
1033 /* in case there were previous tables attached */
1034 acl_unhook_l2_output_classify (am, sw_if_index);
1036 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1037 sizeof (ip4_5tuple_mask) - 1, ~0,
1038 am->l2_output_classify_next_acl_ip4,
1039 &ip4_table_index, 1);
1043 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1044 sizeof (ip6_5tuple_mask) - 1, ~0,
1045 am->l2_output_classify_next_acl_ip6,
1046 &ip6_table_index, 1);
1049 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1050 sizeof (ip4_5tuple_mask) - 1, ~0,
1051 am->l2_output_classify_next_acl_ip4,
1052 &ip4_table_index, 0);
1056 if (intf_has_etype_whitelist (am, sw_if_index, 0))
1058 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
1059 &etype_table_index, 1);
1060 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
1065 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1066 sizeof (dot1ad_5tuple_mask) - 1,
1067 etype_table_index, ~0,
1068 &dot1ad_table_index, 1);
1070 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1071 sizeof (dot1q_5tuple_mask) - 1,
1072 dot1ad_table_index, ~0,
1073 &dot1q_table_index, 1);
1076 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1077 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1078 ~0, &dot1ad_table_index, 0);
1079 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1080 sizeof (ip6_5tuple_mask) - 1, ~0,
1081 am->l2_output_classify_next_acl_ip6,
1082 &ip6_table_index, 0);
1083 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1084 sizeof (ip4_5tuple_mask) - 1, ~0,
1085 am->l2_output_classify_next_acl_ip4,
1086 &ip4_table_index, 0);
1091 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1092 ip6_table_index, dot1q_table_index);
1095 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
1096 sw_if_index, ip4_table_index, ip6_table_index);
1100 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1101 sizeof (ip6_5tuple_mask) - 1, ~0,
1102 am->l2_output_classify_next_acl_ip6,
1103 &ip6_table_index, 0);
1104 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1105 sizeof (ip4_5tuple_mask) - 1, ~0,
1106 am->l2_output_classify_next_acl_ip4,
1107 &ip4_table_index, 0);
1108 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1109 sizeof (dot1q_5tuple_mask) - 1, ~0,
1110 ~0, &dot1q_table_index, 0);
1111 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1112 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1113 ~0, &dot1ad_table_index, 0);
1117 /* add sessions to vlan tables per ethernet_type */
1118 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
1119 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
1120 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
1121 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
1123 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
1125 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
1127 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
1129 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
1131 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
1134 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
1136 clib_mem_set_heap (prevheap);
1141 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
1143 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1144 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1145 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1147 clib_mem_set_heap (oldheap);
1152 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1157 /* Utterly wrong? */
1158 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1160 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1162 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
1166 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1170 rv = acl_hook_l2_input_classify (am, sw_if_index);
1174 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1177 am->in_acl_on_sw_if_index =
1178 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
1184 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1189 /* Utterly wrong? */
1190 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1192 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1194 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
1198 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1202 rv = acl_hook_l2_output_classify (am, sw_if_index);
1206 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1209 am->out_acl_on_sw_if_index =
1210 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
1216 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
1217 int is_input, int enable_disable)
1220 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
1222 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
1226 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1228 return (pool_is_free_index (am->acls, acl_list_index));
1232 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
1233 u8 is_input, u32 * vec_acl_list_index,
1234 int *may_clear_sessions)
1237 uword *seen_acl_bitmap = 0;
1238 uword *old_seen_acl_bitmap = 0;
1239 uword *change_acl_bitmap = 0;
1244 if (am->trace_acl > 255)
1246 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
1247 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
1249 vec_foreach (pacln, vec_acl_list_index)
1251 if (acl_is_not_defined (am, *pacln))
1253 /* ACL is not defined. Can not apply */
1254 clib_warning ("ERROR: ACL %d not defined", *pacln);
1255 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1258 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
1260 /* ACL being applied twice within the list. error. */
1261 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
1262 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1265 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
1269 u32 **pinout_lc_index_by_sw_if_index =
1271 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
1273 u32 ***pinout_acl_vec_by_sw_if_index =
1275 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1277 u32 ***pinout_sw_if_index_vec_by_acl =
1279 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
1281 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1283 clib_bitmap_validate (old_seen_acl_bitmap, 1);
1285 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
1287 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
1290 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
1292 if (am->trace_acl > 255)
1293 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
1294 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
1295 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
1298 clib_bitmap_foreach(acln, change_acl_bitmap, ({
1299 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
1300 /* ACL is being removed. */
1301 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
1302 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1303 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
1306 /* ACL is being added. */
1307 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
1308 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1313 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1314 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
1315 vec_dup (vec_acl_list_index);
1317 if (am->reclassify_sessions)
1319 /* re-applying ACLs means a new policy epoch */
1320 increment_policy_epoch (am, sw_if_index, is_input);
1324 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
1325 if (may_clear_sessions && *may_clear_sessions
1326 && !clib_bitmap_is_zero (change_acl_bitmap))
1328 acl_clear_sessions (am, sw_if_index);
1329 *may_clear_sessions = 0;
1334 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
1336 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
1338 if (vec_len (vec_acl_list_index) > 0)
1340 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
1343 if (~0 == am->interface_acl_user_id)
1344 am->interface_acl_user_id =
1345 acl_plugin_register_user_module ("interface ACL", "sw_if_index",
1348 acl_plugin_get_lookup_context_index (am->interface_acl_user_id,
1349 sw_if_index, is_input);
1350 ASSERT (lc_index >= 0);
1351 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
1353 acl_plugin_set_acl_vec_for_context (lc_index, vec_acl_list_index);
1357 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
1359 acl_plugin_put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
1360 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
1364 /* ensure ACL processing is enabled/disabled as needed */
1365 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
1366 vec_len (vec_acl_list_index) > 0);
1369 clib_bitmap_free (change_acl_bitmap);
1370 clib_bitmap_free (seen_acl_bitmap);
1371 clib_bitmap_free (old_seen_acl_bitmap);
1376 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
1377 int *may_clear_sessions)
1379 acl_main_t *am = &acl_main;
1380 void *oldheap = acl_set_heap (am);
1381 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
1382 may_clear_sessions);
1383 clib_mem_set_heap (oldheap);
1387 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1391 acl_main_t *am = &acl_main;
1393 int may_clear_sessions = 1;
1395 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
1396 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1398 u32 ***pinout_acl_vec_by_sw_if_index =
1400 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1402 void *oldheap = acl_set_heap (am);
1406 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1407 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1412 rv = error_already_applied;
1416 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1417 vec_add1 (acl_vec, acl_list_index);
1421 if (sw_if_index > vec_len (*pinout_acl_vec_by_sw_if_index))
1423 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1427 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1432 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1436 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1437 vec_del1 (acl_vec, index);
1440 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
1441 &may_clear_sessions);
1444 clib_mem_set_heap (oldheap);
1449 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1452 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1453 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1455 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1456 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1458 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1459 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1462 * if there are already inbound/outbound ACLs applied, toggle the
1463 * enable/disable - this will recreate the necessary tables.
1466 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1468 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1470 acl_interface_in_enable_disable (am, sw_if_index, 0);
1471 acl_interface_in_enable_disable (am, sw_if_index, 1);
1474 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1476 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1478 acl_interface_out_enable_disable (am, sw_if_index, 0);
1479 acl_interface_out_enable_disable (am, sw_if_index, 1);
1494 u32 arp_table_index;
1495 u32 dot1q_table_index;
1496 u32 dot1ad_table_index;
1497 u32 arp_dot1q_table_index;
1498 u32 arp_dot1ad_table_index;
1500 u32 out_table_index;
1501 u32 out_arp_table_index;
1502 u32 out_dot1q_table_index;
1503 u32 out_dot1ad_table_index;
1504 u32 out_arp_dot1q_table_index;
1505 u32 out_arp_dot1ad_table_index;
1506 } macip_match_type_t;
1509 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1515 for (i = 0; i < vec_len (mv); i++)
1517 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1518 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1528 /* Get metric used to sort match types.
1529 The more specific and the more often seen - the bigger the metric */
1531 match_type_metric (macip_match_type_t * m)
1533 unsigned int mac_bits_set = 0;
1534 unsigned int mac_byte;
1536 for (i = 0; i < 6; i++)
1538 mac_byte = m->mac_mask[i];
1539 for (; mac_byte; mac_byte >>= 1)
1540 mac_bits_set += mac_byte & 1;
1543 * Attempt to place the more specific and the more used rules on top.
1544 * There are obvious caveat corner cases to this, but they do not
1545 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1546 * going with a wildcard IPv4 with a specific MAC).
1548 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1552 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1554 /* Ascending sort based on the metric values */
1555 return match_type_metric (m1) - match_type_metric (m2);
1558 /* Get the offset of L3 source within ethernet packet */
1560 get_l3_src_offset (int is6)
1563 return (sizeof (ethernet_header_t) +
1564 offsetof (ip6_header_t, src_address));
1566 return (sizeof (ethernet_header_t) +
1567 offsetof (ip4_header_t, src_address));
1571 get_l3_dst_offset (int is6)
1574 return (sizeof (ethernet_header_t) +
1575 offsetof (ip6_header_t, dst_address));
1577 return (sizeof (ethernet_header_t) +
1578 offsetof (ip4_header_t, dst_address));
1582 * return if the is_permit value also requires to create the egress tables
1583 * For backwards compatibility, we keep the is_permit = 1 to only
1584 * create the ingress tables, and the new value of 3 will also
1585 * create the egress tables based on destination.
1588 macip_permit_also_egress (u8 is_permit)
1590 return (is_permit == 3);
1594 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1596 macip_match_type_t *mvec = NULL;
1597 macip_match_type_t *mt;
1598 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1600 u32 match_type_index;
1604 vnet_classify_main_t *cm = &vnet_classify_main;
1606 /* Count the number of different types of rules */
1607 for (i = 0; i < a->count; i++)
1611 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1612 a->rules[i].src_prefixlen,
1613 a->rules[i].is_ipv6)))
1615 match_type_index = vec_len (mvec);
1616 vec_validate (mvec, match_type_index);
1617 memcpy (mvec[match_type_index].mac_mask,
1618 a->rules[i].src_mac_mask, 6);
1619 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1620 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1621 mvec[match_type_index].has_egress = 0;
1622 mvec[match_type_index].table_index = ~0;
1623 mvec[match_type_index].arp_table_index = ~0;
1624 mvec[match_type_index].dot1q_table_index = ~0;
1625 mvec[match_type_index].dot1ad_table_index = ~0;
1626 mvec[match_type_index].arp_dot1q_table_index = ~0;
1627 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1628 mvec[match_type_index].out_table_index = ~0;
1629 mvec[match_type_index].out_arp_table_index = ~0;
1630 mvec[match_type_index].out_dot1q_table_index = ~0;
1631 mvec[match_type_index].out_dot1ad_table_index = ~0;
1632 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1633 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1635 mvec[match_type_index].count++;
1636 mvec[match_type_index].has_egress |=
1637 macip_permit_also_egress (a->rules[i].is_permit);
1639 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1640 vec_sort_with_function (mvec, match_type_compare);
1641 /* Create the classifier tables */
1643 out_last_table = ~0;
1644 /* First add ARP tables */
1645 vec_foreach (mt, mvec)
1648 int is6 = mt->is_ipv6;
1650 u32 *last_tag_table;
1651 u32 *out_last_tag_table;
1658 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
1659 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1660 | Destination Address |
1661 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1663 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1665 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1666 | EtherType | Hardware Type |
1667 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1668 | Protocol Type | Hw addr len | Proto addr len|
1669 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1671 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1672 | Sender Hardware Address |
1673 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1674 | Sender Protocol Address |
1675 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1676 | Target Hardware Address |
1677 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1678 | | TargetProtocolAddress |
1679 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1681 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1683 for (tags = 2; tags >= 0; tags--)
1685 memset (mask, 0, sizeof (mask));
1686 /* source MAC address */
1687 memcpy (&mask[6], mt->mac_mask, 6);
1693 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1695 last_tag_table = &mt->arp_table_index;
1698 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1699 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1701 last_tag_table = &mt->arp_dot1q_table_index;
1704 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1705 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1706 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1708 last_tag_table = &mt->arp_dot1ad_table_index;
1712 /* sender hardware address within ARP */
1713 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1714 /* sender protocol address within ARP */
1715 for (i = 0; i < (mt->prefix_len / 8); i++)
1716 mask[l3_offset + 14 + i] = 0xff;
1717 if (mt->prefix_len % 8)
1718 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1719 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1721 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1723 1)) / sizeof (u32x4)) * sizeof (u32x4);
1724 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1725 (~0 == last_table) ? 0 : ~0,
1727 last_table = *last_tag_table;
1730 /* egress ARP table */
1731 memset (mask, 0, sizeof (mask));
1737 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1739 out_last_tag_table = &mt->out_arp_table_index;
1742 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1743 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1745 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1748 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1749 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1750 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1752 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1756 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1757 /* mask captures just the destination and the ethertype */
1758 mask_len = ((l3_offset +
1760 1)) / sizeof (u32x4)) * sizeof (u32x4);
1761 acl_classify_add_del_table_small (cm, mask, mask_len,
1764 out_last_table) ? 0 : ~0,
1765 out_last_tag_table, 1);
1766 out_last_table = *out_last_tag_table;
1771 /* Now add IP[46] tables */
1772 vec_foreach (mt, mvec)
1775 int is6 = mt->is_ipv6;
1779 u32 *last_tag_table;
1780 u32 *out_last_tag_table;
1783 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1785 for (tags = 2; tags >= 0; tags--)
1787 memset (mask, 0, sizeof (mask));
1788 memcpy (&mask[6], mt->mac_mask, 6);
1789 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1794 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1795 last_tag_table = &mt->table_index;
1798 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1799 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1800 last_tag_table = &mt->dot1q_table_index;
1803 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1804 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1805 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1806 last_tag_table = &mt->dot1ad_table_index;
1809 for (i = 0; i < (mt->prefix_len / 8); i++)
1811 mask[l3_src_offs + i] = 0xff;
1813 if (mt->prefix_len % 8)
1815 mask[l3_src_offs + (mt->prefix_len / 8)] =
1816 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1819 * Round-up the number of bytes needed to store the prefix,
1820 * and round up the number of vectors too
1822 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1823 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1824 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1825 (~0 == last_table) ? 0 : ~0,
1827 last_table = *last_tag_table;
1831 for (tags = 2; tags >= 0; tags--)
1833 memset (mask, 0, sizeof (mask));
1834 /* MAC destination */
1835 memcpy (&mask[0], mt->mac_mask, 6);
1836 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1841 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1842 out_last_tag_table = &mt->out_table_index;
1845 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1846 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1847 out_last_tag_table = &mt->out_dot1q_table_index;
1850 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1851 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1852 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1853 out_last_tag_table = &mt->out_dot1ad_table_index;
1856 for (i = 0; i < (mt->prefix_len / 8); i++)
1858 mask[l3_dst_offs + i] = 0xff;
1860 if (mt->prefix_len % 8)
1862 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1863 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1866 * Round-up the number of bytes needed to store the prefix,
1867 * and round up the number of vectors too
1869 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1871 1)) / sizeof (u32x4)) * sizeof (u32x4);
1872 acl_classify_add_del_table_small (cm, mask, mask_len,
1874 (~0 == out_last_table) ? 0 : ~0,
1875 out_last_tag_table, 1);
1876 out_last_table = *out_last_tag_table;
1880 a->ip4_table_index = last_table;
1881 a->ip6_table_index = last_table;
1882 a->l2_table_index = last_table;
1884 a->out_ip4_table_index = out_last_table;
1885 a->out_ip6_table_index = out_last_table;
1886 a->out_l2_table_index = out_last_table;
1888 /* Populate the classifier tables with rules from the MACIP ACL */
1889 for (i = 0; i < a->count; i++)
1893 int is6 = a->rules[i].is_ipv6;
1900 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1901 a->rules[i].src_prefixlen,
1902 a->rules[i].is_ipv6);
1903 ASSERT (match_type_index != ~0);
1905 for (tags = 2; tags >= 0; tags--)
1907 memset (mask, 0, sizeof (mask));
1908 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1909 memcpy (&mask[6], a->rules[i].src_mac, 6);
1914 tag_table = mvec[match_type_index].table_index;
1918 tag_table = mvec[match_type_index].dot1q_table_index;
1924 tag_table = mvec[match_type_index].dot1ad_table_index;
1934 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1936 mask[eth + 1] = 0xdd;
1940 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1942 mask[eth + 1] = 0x00;
1945 /* add session to table mvec[match_type_index].table_index; */
1946 vnet_classify_add_del_session (cm, tag_table,
1947 mask, a->rules[i].is_permit ? ~0 : 0,
1948 i, 0, action, metadata, 1);
1949 memset (&mask[12], 0, sizeof (mask) - 12);
1952 /* add ARP table entry too */
1953 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1955 memset (mask, 0, sizeof (mask));
1956 memcpy (&mask[6], a->rules[i].src_mac, 6);
1958 for (tags = 2; tags >= 0; tags--)
1964 tag_table = mvec[match_type_index].arp_table_index;
1970 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1978 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1989 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1990 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1992 vnet_classify_add_del_session (cm, tag_table, mask,
1993 a->rules[i].is_permit ? ~0 : 0,
1994 i, 0, action, metadata, 1);
1997 if (macip_permit_also_egress (a->rules[i].is_permit))
1999 /* Add the egress entry with destination set */
2000 for (tags = 2; tags >= 0; tags--)
2002 memset (mask, 0, sizeof (mask));
2003 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
2004 /* src mac in the other direction becomes dst */
2005 memcpy (&mask[0], a->rules[i].src_mac, 6);
2010 tag_table = mvec[match_type_index].out_table_index;
2014 tag_table = mvec[match_type_index].out_dot1q_table_index;
2020 tag_table = mvec[match_type_index].out_dot1ad_table_index;
2030 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
2033 mask[eth + 1] = 0xdd;
2037 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
2040 mask[eth + 1] = 0x00;
2043 /* add session to table mvec[match_type_index].table_index; */
2044 vnet_classify_add_del_session (cm, tag_table,
2046 a->rules[i].is_permit ? ~0 : 0,
2047 i, 0, action, metadata, 1);
2048 // memset (&mask[12], 0, sizeof (mask) - 12);
2051 /* add ARP table entry too */
2052 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
2054 for (tags = 2; tags >= 0; tags--)
2056 memset (mask, 0, sizeof (mask));
2061 tag_table = mvec[match_type_index].out_arp_table_index;
2067 mvec[match_type_index].out_arp_dot1q_table_index;
2075 mvec[match_type_index].out_arp_dot1ad_table_index;
2085 vnet_classify_add_del_session (cm, tag_table,
2088 rules[i].is_permit ? ~0 : 0,
2089 i, 0, action, metadata, 1);
2098 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
2100 vnet_classify_main_t *cm = &vnet_classify_main;
2101 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2103 if (a->ip4_table_index != ~0)
2105 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2106 &a->ip4_table_index, 0);
2107 a->ip4_table_index = ~0;
2109 if (a->ip6_table_index != ~0)
2111 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2112 &a->ip6_table_index, 0);
2113 a->ip6_table_index = ~0;
2115 if (a->l2_table_index != ~0)
2117 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
2119 a->l2_table_index = ~0;
2121 if (a->out_ip4_table_index != ~0)
2123 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2124 &a->out_ip4_table_index, 0);
2125 a->out_ip4_table_index = ~0;
2127 if (a->out_ip6_table_index != ~0)
2129 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2130 &a->out_ip6_table_index, 0);
2131 a->out_ip6_table_index = ~0;
2133 if (a->out_l2_table_index != ~0)
2135 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2136 &a->out_l2_table_index, 0);
2137 a->out_l2_table_index = ~0;
2142 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2148 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2150 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2151 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2153 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2154 a->ip6_table_index, a->l2_table_index,
2156 /* return the first unhappy outcome but make try to plough through. */
2159 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2160 a->out_ip6_table_index,
2161 a->out_l2_table_index, is_apply);
2162 /* return the first unhappy outcome but make try to plough through. */
2169 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2170 u32 * acl_list_index, u8 * tag)
2172 acl_main_t *am = &acl_main;
2173 macip_acl_list_t *a;
2174 macip_acl_rule_t *r;
2175 macip_acl_rule_t *acl_new_rules = 0;
2179 if (*acl_list_index != ~0)
2181 /* They supplied some number, let's see if this MACIP ACL exists */
2182 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2184 /* tried to replace a non-existent ACL, no point doing anything */
2186 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2187 *acl_list_index, tag);
2188 return VNET_API_ERROR_NO_SUCH_ENTRY;
2195 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2198 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2199 if (~0 != *acl_list_index)
2200 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2201 void *oldheap = acl_set_heap (am);
2202 /* Create and populate the rules */
2204 vec_validate (acl_new_rules, count - 1);
2206 for (i = 0; i < count; i++)
2208 r = &acl_new_rules[i];
2209 r->is_permit = rules[i].is_permit;
2210 r->is_ipv6 = rules[i].is_ipv6;
2211 memcpy (&r->src_mac, rules[i].src_mac, 6);
2212 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2213 if (rules[i].is_ipv6)
2214 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2216 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2217 r->src_prefixlen = rules[i].src_ip_prefix_len;
2220 if (~0 == *acl_list_index)
2223 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2224 memset (a, 0, sizeof (*a));
2225 /* Will return the newly allocated ACL index */
2226 *acl_list_index = a - am->macip_acls;
2230 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2233 vec_free (a->rules);
2235 macip_destroy_classify_tables (am, *acl_list_index);
2238 a->rules = acl_new_rules;
2240 memcpy (a->tag, tag, sizeof (a->tag));
2242 /* Create and populate the classifer tables */
2243 macip_create_classify_tables (am, *acl_list_index);
2244 clib_mem_set_heap (oldheap);
2245 /* If the ACL was already applied somewhere, reapply the newly created tables */
2247 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2251 /* No check that sw_if_index denotes a valid interface - the callers
2252 * were supposed to validate.
2254 * That said, if sw_if_index corresponds to an interface that exists at all,
2255 * this function must return errors accordingly if the ACL is not applied.
2259 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2262 u32 macip_acl_index;
2263 macip_acl_list_t *a;
2265 /* The vector is too short - MACIP ACL is not applied */
2266 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
2267 return VNET_API_ERROR_NO_SUCH_ENTRY;
2269 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2270 /* No point in deleting MACIP ACL which is not applied */
2271 if (~0 == macip_acl_index)
2272 return VNET_API_ERROR_NO_SUCH_ENTRY;
2274 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2275 /* remove the classifier tables off the interface L2 ACL */
2277 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2278 a->ip6_table_index, a->l2_table_index, 0);
2280 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2281 a->out_ip4_table_index, a->out_ip6_table_index,
2282 a->out_l2_table_index, 0);
2283 /* Unset the MACIP ACL index */
2284 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2285 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
2286 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
2289 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
2293 /* No check for validity of sw_if_index - the callers were supposed to validate */
2296 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2297 u32 macip_acl_index)
2299 macip_acl_list_t *a;
2301 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2303 return VNET_API_ERROR_NO_SUCH_ENTRY;
2305 void *oldheap = acl_set_heap (am);
2306 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2307 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2308 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
2309 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
2310 clib_mem_set_heap (oldheap);
2311 /* If there already a MACIP ACL applied, unapply it */
2312 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2313 macip_acl_interface_del_acl (am, sw_if_index);
2314 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2316 /* Apply the classifier tables for L2 ACLs */
2318 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2319 a->ip6_table_index, a->l2_table_index, 1);
2321 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2322 a->out_ip4_table_index, a->out_ip6_table_index,
2323 a->out_l2_table_index, 1);
2328 macip_acl_del_list (u32 acl_list_index)
2330 acl_main_t *am = &acl_main;
2331 macip_acl_list_t *a;
2333 if (pool_is_free_index (am->macip_acls, acl_list_index))
2335 return VNET_API_ERROR_NO_SUCH_ENTRY;
2338 /* delete any references to the ACL */
2339 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2341 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2343 macip_acl_interface_del_acl (am, i);
2347 void *oldheap = acl_set_heap (am);
2348 /* Now that classifier tables are detached, clean them up */
2349 macip_destroy_classify_tables (am, acl_list_index);
2351 /* now we can delete the ACL itself */
2352 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2355 vec_free (a->rules);
2357 pool_put (am->macip_acls, a);
2358 clib_mem_set_heap (oldheap);
2364 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2367 acl_main_t *am = &acl_main;
2371 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2375 rv = macip_acl_interface_del_acl (am, sw_if_index);
2381 * If the client does not allocate enough memory for a variable-length
2382 * message, and then proceed to use it as if the full memory allocated,
2383 * absent the check we happily consume that on the VPP side, and go
2384 * along as if nothing happened. However, the resulting
2385 * effects range from just garbage in the API decode
2386 * (because the decoder snoops too far), to potential memory
2389 * This verifies that the actual length of the message is
2390 * at least expected_len, and complains loudly if it is not.
2392 * A failing check here is 100% a software bug on the API user side,
2393 * so we might as well yell.
2397 verify_message_len (void *mp, u32 expected_len, char *where)
2399 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2400 if (supplied_len < expected_len)
2402 clib_warning ("%s: Supplied message length %d is less than expected %d",
2403 where, supplied_len, expected_len);
2412 /* API message handler */
2414 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2416 vl_api_acl_add_replace_reply_t *rmp;
2417 acl_main_t *am = &acl_main;
2419 u32 acl_list_index = ntohl (mp->acl_index);
2420 u32 acl_count = ntohl (mp->count);
2421 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2423 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2425 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2429 rv = VNET_API_ERROR_INVALID_VALUE;
2433 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2435 rmp->acl_index = htonl(acl_list_index);
2441 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2443 acl_main_t *am = &acl_main;
2444 vl_api_acl_del_reply_t *rmp;
2447 rv = acl_del_list (ntohl (mp->acl_index));
2449 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2453 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2455 acl_main_t *am = &acl_main;
2456 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2457 u32 sw_if_index = ntohl (mp->sw_if_index);
2458 vl_api_acl_interface_add_del_reply_t *rmp;
2461 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2462 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2465 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2466 mp->is_input, ntohl (mp->acl_index));
2468 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2472 vl_api_acl_interface_set_acl_list_t_handler
2473 (vl_api_acl_interface_set_acl_list_t * mp)
2475 acl_main_t *am = &acl_main;
2476 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2479 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2480 u32 sw_if_index = ntohl (mp->sw_if_index);
2482 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2483 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2486 int may_clear_sessions = 1;
2487 for (i = 0; i < mp->count; i++)
2489 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2491 /* ACL does not exist, so we can not apply it */
2492 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2497 void *oldheap = acl_set_heap (am);
2499 u32 *in_acl_vec = 0;
2500 u32 *out_acl_vec = 0;
2501 for (i = 0; i < mp->count; i++)
2502 if (i < mp->n_input)
2503 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2505 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2508 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2509 &may_clear_sessions);
2511 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2513 &may_clear_sessions);
2514 vec_free (in_acl_vec);
2515 vec_free (out_acl_vec);
2516 clib_mem_set_heap (oldheap);
2520 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2524 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2526 api_rule->is_permit = r->is_permit;
2527 api_rule->is_ipv6 = r->is_ipv6;
2530 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2531 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2535 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2536 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2538 api_rule->src_ip_prefix_len = r->src_prefixlen;
2539 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2540 api_rule->proto = r->proto;
2541 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2542 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2543 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2544 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2545 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2546 api_rule->tcp_flags_value = r->tcp_flags_value;
2550 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2551 acl_list_t * acl, u32 context)
2553 vl_api_acl_details_t *mp;
2554 vl_api_acl_rule_t *rules;
2556 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2557 void *oldheap = acl_set_heap (am);
2559 mp = vl_msg_api_alloc (msg_size);
2560 memset (mp, 0, msg_size);
2561 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2563 /* fill in the message */
2564 mp->context = context;
2565 mp->count = htonl (acl->count);
2566 mp->acl_index = htonl (acl - am->acls);
2567 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2568 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2570 for (i = 0; i < acl->count; i++)
2572 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2575 clib_mem_set_heap (oldheap);
2576 vl_api_send_msg (reg, (u8 *) mp);
2581 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2583 acl_main_t *am = &acl_main;
2587 vl_api_registration_t *reg;
2589 reg = vl_api_client_index_to_registration (mp->client_index);
2593 if (mp->acl_index == ~0)
2596 /* Just dump all ACLs */
2597 pool_foreach (acl, am->acls,
2599 send_acl_details(am, reg, acl, mp->context);
2605 acl_index = ntohl (mp->acl_index);
2606 if (!pool_is_free_index (am->acls, acl_index))
2608 acl = pool_elt_at_index (am->acls, acl_index);
2609 send_acl_details (am, reg, acl, mp->context);
2615 /* FIXME API: should we signal an error here at all ? */
2621 send_acl_interface_list_details (acl_main_t * am,
2622 vl_api_registration_t * reg,
2623 u32 sw_if_index, u32 context)
2625 vl_api_acl_interface_list_details_t *mp;
2631 void *oldheap = acl_set_heap (am);
2633 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2634 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2636 clib_mem_set_heap (oldheap);
2638 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2639 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2640 count = n_input + n_output;
2642 msg_size = sizeof (*mp);
2643 msg_size += sizeof (mp->acls[0]) * count;
2645 mp = vl_msg_api_alloc (msg_size);
2646 memset (mp, 0, msg_size);
2648 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2650 /* fill in the message */
2651 mp->context = context;
2652 mp->sw_if_index = htonl (sw_if_index);
2654 mp->n_input = n_input;
2655 for (i = 0; i < n_input; i++)
2657 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2659 for (i = 0; i < n_output; i++)
2661 mp->acls[n_input + i] =
2662 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2664 vl_api_send_msg (reg, (u8 *) mp);
2668 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2671 acl_main_t *am = &acl_main;
2672 vnet_sw_interface_t *swif;
2673 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2676 vl_api_registration_t *reg;
2678 reg = vl_api_client_index_to_registration (mp->client_index);
2682 if (mp->sw_if_index == ~0)
2685 pool_foreach (swif, im->sw_interfaces,
2687 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2693 sw_if_index = ntohl (mp->sw_if_index);
2694 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2695 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2699 /* MACIP ACL API handlers */
2702 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2704 vl_api_macip_acl_add_reply_t *rmp;
2705 acl_main_t *am = &acl_main;
2707 u32 acl_list_index = ~0;
2708 u32 acl_count = ntohl (mp->count);
2709 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2711 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2713 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2717 rv = VNET_API_ERROR_INVALID_VALUE;
2721 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2723 rmp->acl_index = htonl(acl_list_index);
2729 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2731 vl_api_macip_acl_add_replace_reply_t *rmp;
2732 acl_main_t *am = &acl_main;
2734 u32 acl_list_index = ntohl (mp->acl_index);
2735 u32 acl_count = ntohl (mp->count);
2736 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2738 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2740 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2744 rv = VNET_API_ERROR_INVALID_VALUE;
2748 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2750 rmp->acl_index = htonl(acl_list_index);
2756 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2758 acl_main_t *am = &acl_main;
2759 vl_api_macip_acl_del_reply_t *rmp;
2762 rv = macip_acl_del_list (ntohl (mp->acl_index));
2764 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2768 vl_api_macip_acl_interface_add_del_t_handler
2769 (vl_api_macip_acl_interface_add_del_t * mp)
2771 acl_main_t *am = &acl_main;
2772 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2774 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2775 u32 sw_if_index = ntohl (mp->sw_if_index);
2777 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2778 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2781 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2782 ntohl (mp->acl_index));
2784 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2788 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2789 macip_acl_list_t * acl, u32 context)
2791 vl_api_macip_acl_details_t *mp;
2792 vl_api_macip_acl_rule_t *rules;
2793 macip_acl_rule_t *r;
2795 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2797 mp = vl_msg_api_alloc (msg_size);
2798 memset (mp, 0, msg_size);
2799 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2801 /* fill in the message */
2802 mp->context = context;
2805 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2806 mp->count = htonl (acl->count);
2807 mp->acl_index = htonl (acl - am->macip_acls);
2809 for (i = 0; i < acl->count; i++)
2812 rules[i].is_permit = r->is_permit;
2813 rules[i].is_ipv6 = r->is_ipv6;
2814 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2815 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2816 sizeof (r->src_mac_mask));
2818 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2819 sizeof (r->src_ip_addr.ip6));
2821 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2822 sizeof (r->src_ip_addr.ip4));
2823 rules[i].src_ip_prefix_len = r->src_prefixlen;
2828 /* No martini, no party - no ACL applied to this interface. */
2833 vl_api_send_msg (reg, (u8 *) mp);
2838 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2840 acl_main_t *am = &acl_main;
2841 macip_acl_list_t *acl;
2843 vl_api_registration_t *reg;
2845 reg = vl_api_client_index_to_registration (mp->client_index);
2849 if (mp->acl_index == ~0)
2851 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2852 pool_foreach (acl, am->macip_acls, (
2854 send_macip_acl_details (am, reg,
2863 u32 acl_index = ntohl (mp->acl_index);
2864 if (!pool_is_free_index (am->macip_acls, acl_index))
2866 acl = pool_elt_at_index (am->macip_acls, acl_index);
2867 send_macip_acl_details (am, reg, acl, mp->context);
2873 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2876 acl_main_t *am = &acl_main;
2877 vl_api_macip_acl_interface_get_reply_t *rmp;
2878 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2879 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2880 vl_api_registration_t *reg;
2883 reg = vl_api_client_index_to_registration (mp->client_index);
2887 rmp = vl_msg_api_alloc (msg_size);
2888 memset (rmp, 0, msg_size);
2890 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2891 rmp->context = mp->context;
2892 rmp->count = htonl (count);
2893 for (i = 0; i < count; i++)
2895 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2898 vl_api_send_msg (reg, (u8 *) rmp);
2902 send_macip_acl_interface_list_details (acl_main_t * am,
2903 vl_api_registration_t * reg,
2905 u32 acl_index, u32 context)
2907 vl_api_macip_acl_interface_list_details_t *rmp;
2908 /* at this time there is only ever 1 mac ip acl per interface */
2909 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2911 rmp = vl_msg_api_alloc (msg_size);
2912 memset (rmp, 0, msg_size);
2914 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2916 /* fill in the message */
2917 rmp->context = context;
2919 rmp->sw_if_index = htonl (sw_if_index);
2920 rmp->acls[0] = htonl (acl_index);
2922 vl_api_send_msg (reg, (u8 *) rmp);
2926 vl_api_macip_acl_interface_list_dump_t_handler
2927 (vl_api_macip_acl_interface_list_dump_t * mp)
2929 vl_api_registration_t *reg;
2930 acl_main_t *am = &acl_main;
2931 u32 sw_if_index = ntohl (mp->sw_if_index);
2933 reg = vl_api_client_index_to_registration (mp->client_index);
2937 if (sw_if_index == ~0)
2939 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2941 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2943 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2944 am->macip_acl_by_sw_if_index
2952 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2954 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2955 am->macip_acl_by_sw_if_index
2956 [sw_if_index], mp->context);
2962 vl_api_acl_interface_set_etype_whitelist_t_handler
2963 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2965 acl_main_t *am = &acl_main;
2966 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2969 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2970 u32 sw_if_index = ntohl (mp->sw_if_index);
2971 u16 *vec_in = 0, *vec_out = 0;
2972 void *oldheap = acl_set_heap (am);
2974 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2975 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2978 for (i = 0; i < mp->count; i++)
2980 if (i < mp->n_input)
2981 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2983 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2985 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2988 clib_mem_set_heap (oldheap);
2989 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2993 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2994 vl_api_registration_t * reg,
2995 u32 sw_if_index, u32 context)
2997 vl_api_acl_interface_etype_whitelist_details_t *mp;
3004 u16 *whitelist_in = 0;
3005 u16 *whitelist_out = 0;
3007 if (intf_has_etype_whitelist (am, sw_if_index, 0))
3009 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
3011 if (intf_has_etype_whitelist (am, sw_if_index, 1))
3013 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
3015 if ((0 == whitelist_in) && (0 == whitelist_out))
3016 return; /* nothing to do */
3018 void *oldheap = acl_set_heap (am);
3020 n_input = vec_len (whitelist_in);
3021 n_output = vec_len (whitelist_out);
3022 count = n_input + n_output;
3024 msg_size = sizeof (*mp);
3025 msg_size += sizeof (mp->whitelist[0]) * count;
3027 mp = vl_msg_api_alloc (msg_size);
3028 memset (mp, 0, msg_size);
3030 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
3032 /* fill in the message */
3033 mp->context = context;
3034 mp->sw_if_index = htonl (sw_if_index);
3036 mp->n_input = n_input;
3037 for (i = 0; i < n_input; i++)
3039 mp->whitelist[i] = htons (whitelist_in[i]);
3041 for (i = 0; i < n_output; i++)
3043 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
3045 clib_mem_set_heap (oldheap);
3046 vl_api_send_msg (reg, (u8 *) mp);
3051 vl_api_acl_interface_etype_whitelist_dump_t_handler
3052 (vl_api_acl_interface_list_dump_t * mp)
3054 acl_main_t *am = &acl_main;
3055 vnet_sw_interface_t *swif;
3056 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3059 vl_api_registration_t *reg;
3061 reg = vl_api_client_index_to_registration (mp->client_index);
3065 if (mp->sw_if_index == ~0)
3068 pool_foreach (swif, im->sw_interfaces,
3070 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
3076 sw_if_index = ntohl (mp->sw_if_index);
3077 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
3078 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
3085 /* Set up the API message handling tables */
3086 static clib_error_t *
3087 acl_plugin_api_hookup (vlib_main_t * vm)
3089 acl_main_t *am = &acl_main;
3091 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
3093 vl_api_##n##_t_handler, \
3095 vl_api_##n##_t_endian, \
3096 vl_api_##n##_t_print, \
3097 sizeof(vl_api_##n##_t), 1);
3098 foreach_acl_plugin_api_msg;
3104 #define vl_msg_name_crc_list
3105 #include <acl/acl_all_api_h.h>
3106 #undef vl_msg_name_crc_list
3109 setup_message_id_table (acl_main_t * am, api_main_t * apim)
3111 #define _(id,n,crc) \
3112 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
3113 foreach_vl_msg_name_crc_acl;
3118 acl_setup_fa_nodes (void)
3120 vlib_main_t *vm = vlib_get_main ();
3121 acl_main_t *am = &acl_main;
3122 vlib_node_t *n, *n4, *n6;
3124 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
3125 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
3126 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
3129 am->l2_input_classify_next_acl_ip4 =
3130 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3131 am->l2_input_classify_next_acl_ip6 =
3132 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3134 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
3135 l2input_get_feat_names (),
3136 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
3138 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
3139 l2input_get_feat_names (),
3140 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
3143 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
3144 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
3145 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
3147 am->l2_output_classify_next_acl_ip4 =
3148 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3149 am->l2_output_classify_next_acl_ip6 =
3150 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3152 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
3153 l2output_get_feat_names (),
3154 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
3156 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
3157 l2output_get_feat_names (),
3158 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
3162 acl_set_timeout_sec (int timeout_type, u32 value)
3164 acl_main_t *am = &acl_main;
3165 clib_time_t *ct = &am->vlib_main->clib_time;
3167 if (timeout_type < ACL_N_TIMEOUTS)
3169 am->session_timeout_sec[timeout_type] = value;
3173 clib_warning ("Unknown timeout type %d", timeout_type);
3176 am->session_timeout[timeout_type] =
3177 (u64) (((f64) value) / ct->seconds_per_clock);
3181 acl_set_session_max_entries (u32 value)
3183 acl_main_t *am = &acl_main;
3184 am->fa_conn_table_max_entries = value;
3188 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3190 acl_main_t *am = &acl_main;
3192 if ((eh < 256) && (value < 2))
3194 am->fa_ipv6_known_eh_bitmap =
3195 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3203 static clib_error_t *
3204 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3206 acl_main_t *am = &acl_main;
3207 if (0 == am->acl_mheap)
3209 /* ACL heap is not initialized, so definitely nothing to do. */
3214 int may_clear_sessions = 1;
3215 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3216 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3218 /* also unapply any ACLs in case the users did not do so. */
3219 macip_acl_interface_del_acl (am, sw_if_index);
3220 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
3221 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
3226 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3230 static clib_error_t *
3231 acl_set_aclplugin_fn (vlib_main_t * vm,
3232 unformat_input_t * input, vlib_cli_command_t * cmd)
3234 clib_error_t *error = 0;
3238 uword memory_size = 0;
3239 acl_main_t *am = &acl_main;
3241 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3243 if (!acl_set_skip_ipv6_eh (eh_val, val))
3245 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3249 if (unformat (input, "use-hash-acl-matching %u", &val))
3251 am->use_hash_acl_matching = (val != 0);
3254 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3256 am->l4_match_nonfirst_fragment = (val != 0);
3259 if (unformat (input, "reclassify-sessions %u", &val))
3261 am->reclassify_sessions = (val != 0);
3264 if (unformat (input, "event-trace"))
3266 if (!unformat (input, "%u", &val))
3268 error = clib_error_return (0,
3269 "expecting trace level, got `%U`",
3270 format_unformat_error, input);
3275 am->trace_acl = val;
3279 if (unformat (input, "heap"))
3281 if (unformat (input, "main"))
3283 if (unformat (input, "validate %u", &val))
3284 acl_plugin_acl_set_validate_heap (am, val);
3285 else if (unformat (input, "trace %u", &val))
3286 acl_plugin_acl_set_trace_heap (am, val);
3289 else if (unformat (input, "hash"))
3291 if (unformat (input, "validate %u", &val))
3292 acl_plugin_hash_acl_set_validate_heap (val);
3293 else if (unformat (input, "trace %u", &val))
3294 acl_plugin_hash_acl_set_trace_heap (val);
3299 if (unformat (input, "session"))
3301 if (unformat (input, "table"))
3303 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3304 if (unformat (input, "max-entries"))
3306 if (!unformat (input, "%u", &val))
3308 error = clib_error_return (0,
3309 "expecting maximum number of entries, got `%U`",
3310 format_unformat_error, input);
3315 acl_set_session_max_entries (val);
3319 if (unformat (input, "hash-table-buckets"))
3321 if (!unformat (input, "%u", &val))
3323 error = clib_error_return (0,
3324 "expecting maximum number of hash table buckets, got `%U`",
3325 format_unformat_error, input);
3330 am->fa_conn_table_hash_num_buckets = val;
3334 if (unformat (input, "hash-table-memory"))
3336 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3338 error = clib_error_return (0,
3339 "expecting maximum amount of hash table memory, got `%U`",
3340 format_unformat_error, input);
3345 am->fa_conn_table_hash_memory_size = memory_size;
3349 if (unformat (input, "event-trace"))
3351 if (!unformat (input, "%u", &val))
3353 error = clib_error_return (0,
3354 "expecting trace level, got `%U`",
3355 format_unformat_error, input);
3360 am->trace_sessions = val;
3366 if (unformat (input, "timeout"))
3368 if (unformat (input, "udp"))
3370 if (unformat (input, "idle"))
3372 if (!unformat (input, "%u", &timeout))
3374 error = clib_error_return (0,
3375 "expecting timeout value in seconds, got `%U`",
3376 format_unformat_error,
3382 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3387 if (unformat (input, "tcp"))
3389 if (unformat (input, "idle"))
3391 if (!unformat (input, "%u", &timeout))
3393 error = clib_error_return (0,
3394 "expecting timeout value in seconds, got `%U`",
3395 format_unformat_error,
3401 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3405 if (unformat (input, "transient"))
3407 if (!unformat (input, "%u", &timeout))
3409 error = clib_error_return (0,
3410 "expecting timeout value in seconds, got `%U`",
3411 format_unformat_error,
3417 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3431 my_format_mac_address (u8 * s, va_list * args)
3433 u8 *a = va_arg (*args, u8 *);
3434 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3435 a[0], a[1], a[2], a[3], a[4], a[5]);
3439 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3441 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3443 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3444 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3445 format_ip46_address, &a->src_ip_addr,
3446 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3448 my_format_mac_address, a->src_mac,
3449 my_format_mac_address, a->src_mac_mask);
3454 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3456 vlib_main_t *vm = am->vlib_main;
3459 /* Don't try to print someone else's memory */
3460 if (macip_acl_index > vec_len (am->macip_acls))
3463 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3464 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3466 vlib_cli_output (vm,
3467 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3468 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3470 vlib_cli_output (vm,
3471 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3472 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3473 vlib_cli_output (vm,
3474 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3475 a->out_ip4_table_index, a->out_ip6_table_index,
3476 a->out_l2_table_index);
3477 for (i = 0; i < vec_len (a->rules); i++)
3478 vlib_cli_output (vm, " rule %d: %U\n", i,
3479 my_macip_acl_rule_t_pretty_format,
3480 vec_elt_at_index (a->rules, i));
3484 static clib_error_t *
3485 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3487 input, vlib_cli_command_t * cmd)
3489 clib_error_t *error = 0;
3490 acl_main_t *am = &acl_main;
3494 (void) unformat (input, "index %u", &acl_index);
3496 for (i = 0; i < vec_len (am->macip_acls); i++)
3498 /* Don't attempt to show the ACLs that do not exist */
3499 if (pool_is_free_index (am->macip_acls, i))
3502 if ((acl_index != ~0) && (acl_index != i))
3507 macip_acl_print (am, i);
3508 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3510 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3512 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3520 static clib_error_t *
3521 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3523 input, vlib_cli_command_t * cmd)
3525 clib_error_t *error = 0;
3526 acl_main_t *am = &acl_main;
3528 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3530 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3531 vec_elt (am->macip_acl_by_sw_if_index, i));
3537 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3540 vlib_main_t *vm = am->vlib_main;
3542 for (i = 0; i < vec_len (am->acls); i++)
3544 if (acl_is_not_defined (am, i))
3546 /* don't attempt to show the ACLs that do not exist */
3549 if ((acl_index != ~0) && (acl_index != i))
3553 acl_print_acl (vm, am, i);
3555 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3557 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3558 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3561 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3563 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3564 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3567 if (i < vec_len (am->lc_index_vec_by_acl))
3569 vlib_cli_output (vm, " used in lookup context index: %U\n",
3570 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3575 static clib_error_t *
3576 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3577 unformat_input_t * input, vlib_cli_command_t * cmd)
3579 clib_error_t *error = 0;
3580 acl_main_t *am = &acl_main;
3583 (void) unformat (input, "index %u", &acl_index);
3585 acl_plugin_show_acl (am, acl_index);
3589 static clib_error_t *
3590 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3591 unformat_input_t * input,
3592 vlib_cli_command_t * cmd)
3594 clib_error_t *error = 0;
3597 (void) unformat (input, "index %u", &lc_index);
3599 acl_plugin_show_lookup_context (lc_index);
3603 static clib_error_t *
3604 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3605 unformat_input_t * input,
3606 vlib_cli_command_t * cmd)
3608 clib_error_t *error = 0;
3611 (void) unformat (input, "index %u", &lc_index);
3613 acl_plugin_show_lookup_user (lc_index);
3619 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3622 vlib_main_t *vm = am->vlib_main;
3625 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3626 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3628 /* if we need a particular interface, skip all the others */
3629 if ((sw_if_index != ~0) && (sw_if_index != swi))
3632 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3633 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3634 vlib_cli_output (vm, " input policy epoch: %x\n",
3635 vec_elt (am->input_policy_epoch_by_sw_if_index,
3637 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3638 vlib_cli_output (vm, " output policy epoch: %x\n",
3639 vec_elt (am->output_policy_epoch_by_sw_if_index,
3643 if (intf_has_etype_whitelist (am, swi, 1))
3645 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3646 am->input_etype_whitelist_by_sw_if_index[swi],
3649 if (intf_has_etype_whitelist (am, swi, 0))
3651 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3652 am->output_etype_whitelist_by_sw_if_index[swi],
3656 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3657 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3659 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3660 am->input_acl_vec_by_sw_if_index[swi], "%d");
3663 vlib_cli_output (vm, "\n");
3664 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3666 acl_print_acl (vm, am, *pj);
3668 vlib_cli_output (vm, "\n");
3672 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3673 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3675 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3676 am->output_acl_vec_by_sw_if_index[swi], "%d");
3679 vlib_cli_output (vm, "\n");
3680 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3682 acl_print_acl (vm, am, *pj);
3684 vlib_cli_output (vm, "\n");
3687 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3689 vlib_cli_output (vm, " input lookup context index: %d",
3690 am->input_lc_index_by_sw_if_index[swi]);
3692 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3694 vlib_cli_output (vm, " output lookup context index: %d",
3695 am->output_lc_index_by_sw_if_index[swi]);
3702 static clib_error_t *
3703 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3704 unformat_input_t * input,
3705 vlib_cli_command_t * cmd)
3707 clib_error_t *error = 0;
3708 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3711 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3712 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3713 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3714 format_acl_plugin_5tuple, five_tuple);
3716 error = clib_error_return (0, "expecting 6 hex integers");
3721 static clib_error_t *
3722 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3724 input, vlib_cli_command_t * cmd)
3726 clib_error_t *error = 0;
3727 acl_main_t *am = &acl_main;
3729 u32 sw_if_index = ~0;
3730 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3731 int show_acl = unformat (input, "acl");
3732 int detail = unformat (input, "detail");
3734 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3738 static clib_error_t *
3739 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3740 unformat_input_t * input,
3741 vlib_cli_command_t * cmd)
3743 clib_error_t *error = 0;
3744 acl_main_t *am = &acl_main;
3746 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3749 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3753 vlib_cli_output (vm, " Not initialized\n");
3755 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3756 if (am->hash_lookup_mheap)
3758 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3762 vlib_cli_output (vm, " Not initialized\n");
3768 acl_plugin_show_sessions (acl_main_t * am,
3769 u32 show_session_thread_id,
3770 u32 show_session_session_index)
3772 vlib_main_t *vm = am->vlib_main;
3774 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3775 vnet_sw_interface_t *swif;
3778 u64 n_adds = am->fa_session_total_adds;
3779 u64 n_dels = am->fa_session_total_dels;
3780 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3781 n_dels, n_adds - n_dels);
3783 vlib_cli_output (vm, "\n\nPer-thread data:");
3784 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3786 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3787 vlib_cli_output (vm, "Thread #%d:", wk);
3788 if (show_session_thread_id == wk
3789 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3791 vlib_cli_output (vm, " session index %u:",
3792 show_session_session_index);
3793 fa_session_t *sess =
3794 pw->fa_sessions_pool + show_session_session_index;
3795 u64 *m = (u64 *) & sess->info;
3796 vlib_cli_output (vm,
3797 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3798 m[0], m[1], m[2], m[3], m[4], m[5]);
3799 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3800 vlib_cli_output (vm, " tcp_flags_seen: %x",
3801 sess->tcp_flags_seen.as_u16);
3802 vlib_cli_output (vm, " last active time: %lu",
3803 sess->last_active_time);
3804 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3805 vlib_cli_output (vm, " link enqueue time: %lu",
3806 sess->link_enqueue_time);
3807 vlib_cli_output (vm, " link next index: %u",
3808 sess->link_next_idx);
3809 vlib_cli_output (vm, " link prev index: %u",
3810 sess->link_prev_idx);
3811 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3813 vlib_cli_output (vm, " connection add/del stats:", wk);
3814 pool_foreach (swif, im->sw_interfaces, (
3821 (pw->fa_session_adds_by_sw_if_index)
3823 pw->fa_session_adds_by_sw_if_index
3828 (pw->fa_session_dels_by_sw_if_index)
3830 pw->fa_session_dels_by_sw_if_index
3832 u64 n_epoch_changes =
3835 (pw->fa_session_epoch_change_by_sw_if_index)
3837 pw->fa_session_epoch_change_by_sw_if_index
3839 vlib_cli_output (vm,
3840 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3850 vlib_cli_output (vm, " connection timeout type lists:", wk);
3852 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3854 u32 head_session_index = pw->fa_conn_list_head[tt];
3855 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3856 head_session_index);
3857 if (~0 != head_session_index)
3859 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3860 vlib_cli_output (vm, " last active time: %lu",
3861 sess->last_active_time);
3862 vlib_cli_output (vm, " link enqueue time: %lu",
3863 sess->link_enqueue_time);
3867 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3868 vlib_cli_output (vm, " Requeue until time: %lu",
3869 pw->requeue_until_time);
3870 vlib_cli_output (vm, " Current time wait interval: %lu",
3871 pw->current_time_wait_interval);
3872 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3873 pw->cnt_deleted_sessions);
3874 vlib_cli_output (vm, " Delete already deleted: %lu",
3875 pw->cnt_already_deleted_sessions);
3876 vlib_cli_output (vm, " Session timers restarted: %lu",
3877 pw->cnt_session_timer_restarted);
3878 vlib_cli_output (vm, " Swipe until this time: %lu",
3879 pw->swipe_end_time);
3880 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3881 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3882 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3884 pw->pending_clear_sw_if_index_bitmap);
3885 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3886 vlib_cli_output (vm, " interrupt is pending: %d",
3887 pw->interrupt_is_pending);
3888 vlib_cli_output (vm, " interrupt is needed: %d",
3889 pw->interrupt_is_needed);
3890 vlib_cli_output (vm, " interrupt is unwanted: %d",
3891 pw->interrupt_is_unwanted);
3892 vlib_cli_output (vm, " interrupt generation: %d",
3893 pw->interrupt_generation);
3895 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3896 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3897 foreach_fa_cleaner_counter;
3899 vlib_cli_output (vm, "Interrupt generation: %d",
3900 am->fa_interrupt_generation);
3901 vlib_cli_output (vm,
3902 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3903 am->fa_min_deleted_sessions_per_interval,
3904 am->fa_max_deleted_sessions_per_interval,
3905 am->fa_cleaner_wait_time_increment * 1000.0,
3906 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3907 1000.0 / (f64) vm->clib_time.clocks_per_second);
3908 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3911 static clib_error_t *
3912 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3913 unformat_input_t * input,
3914 vlib_cli_command_t * cmd)
3916 clib_error_t *error = 0;
3917 acl_main_t *am = &acl_main;
3919 u32 show_bihash_verbose = 0;
3920 u32 show_session_thread_id = ~0;
3921 u32 show_session_session_index = ~0;
3922 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3923 &show_session_session_index);
3924 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3926 acl_plugin_show_sessions (am, show_session_thread_id,
3927 show_session_session_index);
3928 show_fa_sessions_hash (vm, show_bihash_verbose);
3932 static clib_error_t *
3933 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3934 unformat_input_t * input,
3935 vlib_cli_command_t * cmd)
3937 clib_error_t *error = 0;
3940 u32 sw_if_index = ~0;
3941 int show_acl_hash_info = 0;
3942 int show_applied_info = 0;
3943 int show_mask_type = 0;
3944 int show_bihash = 0;
3945 u32 show_bihash_verbose = 0;
3947 if (unformat (input, "acl"))
3949 show_acl_hash_info = 1;
3950 /* mask-type is handy to see as well right there */
3952 unformat (input, "index %u", &acl_index);
3954 else if (unformat (input, "applied"))
3956 show_applied_info = 1;
3957 unformat (input, "sw_if_index %u", &sw_if_index);
3959 else if (unformat (input, "mask"))
3963 else if (unformat (input, "hash"))
3966 unformat (input, "verbose %u", &show_bihash_verbose);
3970 (show_mask_type || show_acl_hash_info || show_applied_info
3973 /* if no qualifiers specified, show all */
3975 show_acl_hash_info = 1;
3976 show_applied_info = 1;
3980 acl_plugin_show_tables_mask_type ();
3981 if (show_acl_hash_info)
3982 acl_plugin_show_tables_acl_hash_info (acl_index);
3983 if (show_applied_info)
3984 acl_plugin_show_tables_applied_info (sw_if_index);
3986 acl_plugin_show_tables_bihash (show_bihash_verbose);
3991 static clib_error_t *
3992 acl_clear_aclplugin_fn (vlib_main_t * vm,
3993 unformat_input_t * input, vlib_cli_command_t * cmd)
3995 clib_error_t *error = 0;
3996 acl_main_t *am = &acl_main;
3997 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3998 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
4003 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
4004 .path = "set acl-plugin",
4005 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
4006 .function = acl_set_aclplugin_fn,
4009 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
4010 .path = "show acl-plugin acl",
4011 .short_help = "show acl-plugin acl [index N]",
4012 .function = acl_show_aclplugin_acl_fn,
4015 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
4016 .path = "show acl-plugin lookup context",
4017 .short_help = "show acl-plugin lookup context [index N]",
4018 .function = acl_show_aclplugin_lookup_context_fn,
4021 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
4022 .path = "show acl-plugin lookup user",
4023 .short_help = "show acl-plugin lookup user [index N]",
4024 .function = acl_show_aclplugin_lookup_user_fn,
4027 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
4028 .path = "show acl-plugin decode 5tuple",
4029 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
4030 .function = acl_show_aclplugin_decode_5tuple_fn,
4033 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
4034 .path = "show acl-plugin interface",
4035 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
4036 .function = acl_show_aclplugin_interface_fn,
4039 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
4040 .path = "show acl-plugin memory",
4041 .short_help = "show acl-plugin memory",
4042 .function = acl_show_aclplugin_memory_fn,
4045 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
4046 .path = "show acl-plugin sessions",
4047 .short_help = "show acl-plugin sessions",
4048 .function = acl_show_aclplugin_sessions_fn,
4051 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
4052 .path = "show acl-plugin tables",
4053 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
4054 .function = acl_show_aclplugin_tables_fn,
4057 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
4058 .path = "show acl-plugin macip acl",
4059 .short_help = "show acl-plugin macip acl [index N]",
4060 .function = acl_show_aclplugin_macip_acl_fn,
4063 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
4064 .path = "show acl-plugin macip interface",
4065 .short_help = "show acl-plugin macip interface",
4066 .function = acl_show_aclplugin_macip_interface_fn,
4069 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
4070 .path = "clear acl-plugin sessions",
4071 .short_help = "clear acl-plugin sessions",
4072 .function = acl_clear_aclplugin_fn,
4076 static clib_error_t *
4077 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
4079 acl_main_t *am = &acl_main;
4080 u32 conn_table_hash_buckets;
4081 u32 conn_table_hash_memory_size;
4082 u32 conn_table_max_entries;
4083 uword main_heap_size;
4084 uword hash_heap_size;
4085 u32 hash_lookup_hash_buckets;
4086 u32 hash_lookup_hash_memory;
4087 u32 reclassify_sessions;
4089 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4092 (input, "connection hash buckets %d", &conn_table_hash_buckets))
4093 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
4094 else if (unformat (input, "connection hash memory %d",
4095 &conn_table_hash_memory_size))
4096 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
4097 else if (unformat (input, "connection count max %d",
4098 &conn_table_max_entries))
4099 am->fa_conn_table_max_entries = conn_table_max_entries;
4102 (input, "main heap size %U", unformat_memory_size,
4104 am->acl_mheap_size = main_heap_size;
4107 (input, "hash lookup heap size %U", unformat_memory_size,
4109 am->hash_lookup_mheap_size = hash_heap_size;
4110 else if (unformat (input, "hash lookup hash buckets %d",
4111 &hash_lookup_hash_buckets))
4112 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
4113 else if (unformat (input, "hash lookup hash memory %d",
4114 &hash_lookup_hash_memory))
4115 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4116 else if (unformat (input, "reclassify sessions %d",
4117 &reclassify_sessions))
4118 am->reclassify_sessions = reclassify_sessions;
4121 return clib_error_return (0, "unknown input '%U'",
4122 format_unformat_error, input);
4127 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4129 static clib_error_t *
4130 acl_init (vlib_main_t * vm)
4132 acl_main_t *am = &acl_main;
4133 clib_error_t *error = 0;
4134 memset (am, 0, sizeof (*am));
4136 am->vnet_main = vnet_get_main ();
4138 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4140 /* Ask for a correctly-sized block of API message decode slots */
4141 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4142 VL_MSG_FIRST_AVAILABLE);
4144 error = acl_plugin_api_hookup (vm);
4146 /* Add our API messages to the global name_crc hash table */
4147 setup_message_id_table (am, &api_main);
4151 acl_setup_fa_nodes ();
4153 am->acl_mheap_size = 0; /* auto size when initializing */
4154 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4156 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4157 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4159 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4160 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4161 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4162 TCP_SESSION_IDLE_TIMEOUT_SEC;
4163 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4164 UDP_SESSION_IDLE_TIMEOUT_SEC;
4166 am->fa_conn_table_hash_num_buckets =
4167 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4168 am->fa_conn_table_hash_memory_size =
4169 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4170 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4171 am->reclassify_sessions = 0;
4172 vlib_thread_main_t *tm = vlib_get_thread_main ();
4173 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4177 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4179 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4180 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4181 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4182 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4184 pw->fa_conn_list_head[tt] = ~0;
4185 pw->fa_conn_list_tail[tt] = ~0;
4190 am->fa_min_deleted_sessions_per_interval =
4191 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4192 am->fa_max_deleted_sessions_per_interval =
4193 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4194 am->fa_cleaner_wait_time_increment =
4195 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4197 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4198 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4199 am->fa_cleaner_cnt_unknown_event = 0;
4200 am->fa_cleaner_cnt_timer_restarted = 0;
4201 am->fa_cleaner_cnt_wait_with_timeout = 0;
4204 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4207 am->l4_match_nonfirst_fragment = 1;
4209 /* use the new fancy hash-based matching */
4210 am->use_hash_acl_matching = 1;
4212 am->interface_acl_user_id = ~0; /* defer till the first use */
4217 VLIB_INIT_FUNCTION (acl_init);
4221 * fd.io coding-style-patch-verification: ON
4224 * eval: (c-set-style "gnu")