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"
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
62 * The code for the bihash, used by the session management.
64 #include <vppinfra/bihash_40_8.h>
65 #include <vppinfra/bihash_template.h>
66 #include <vppinfra/bihash_template.c>
68 /* List of message types that this plugin understands */
70 #define foreach_acl_plugin_api_msg \
71 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
72 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
73 _(ACL_ADD_REPLACE, acl_add_replace) \
75 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
76 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
77 _(ACL_DUMP, acl_dump) \
78 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
79 _(MACIP_ACL_ADD, macip_acl_add) \
80 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
81 _(MACIP_ACL_DEL, macip_acl_del) \
82 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
83 _(MACIP_ACL_DUMP, macip_acl_dump) \
84 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
85 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
86 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
87 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
91 VLIB_PLUGIN_REGISTER () = {
92 .version = VPP_BUILD_VER,
93 .description = "Access Control Lists",
97 /* methods exported from ACL-as-a-service */
98 static acl_plugin_methods_t acl_plugin;
102 format_vec16 (u8 * s, va_list * va)
104 u16 *v = va_arg (*va, u16 *);
105 char *fmt = va_arg (*va, char *);
107 for (i = 0; i < vec_len (v); i++)
110 s = format (s, ", ");
111 s = format (s, fmt, v[i]);
117 acl_set_heap (acl_main_t * am)
119 if (0 == am->acl_mheap)
121 if (0 == am->acl_mheap_size)
123 vlib_thread_main_t *tm = vlib_get_thread_main ();
124 u64 per_worker_slack = 1000000LL;
125 u64 per_worker_size =
127 ((u64) am->fa_conn_table_max_entries) * sizeof (fa_session_t);
128 u64 per_worker_size_with_slack = per_worker_slack + per_worker_size;
129 u64 main_slack = 2000000LL;
130 u64 bihash_size = (u64) am->fa_conn_table_hash_memory_size;
133 per_worker_size_with_slack * tm->n_vlib_mains + bihash_size +
136 u64 max_possible = ((uword) ~ 0);
137 if (am->acl_mheap_size > max_possible)
139 clib_warning ("ACL heap size requested: %lld, max possible %lld",
140 am->acl_mheap_size, max_possible);
142 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
143 if (0 == am->acl_mheap)
146 ("ACL plugin failed to allocate main heap of %U bytes, abort",
147 format_memory_size, am->acl_mheap_size);
149 mheap_t *h = mheap_header (am->acl_mheap);
150 h->flags |= MHEAP_FLAG_THREAD_SAFE;
152 void *oldheap = clib_mem_set_heap (am->acl_mheap);
157 acl_plugin_set_heap ()
159 acl_main_t *am = &acl_main;
160 return acl_set_heap (am);
164 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
166 clib_mem_set_heap (acl_set_heap (am));
167 mheap_t *h = mheap_header (am->acl_mheap);
170 h->flags |= MHEAP_FLAG_VALIDATE;
171 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
176 h->flags &= ~MHEAP_FLAG_VALIDATE;
177 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
182 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
184 clib_mem_set_heap (acl_set_heap (am));
185 mheap_t *h = mheap_header (am->acl_mheap);
188 h->flags |= MHEAP_FLAG_TRACE;
192 h->flags &= ~MHEAP_FLAG_TRACE;
197 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
199 acl_main_t *am = &acl_main;
200 vl_api_acl_plugin_get_version_reply_t *rmp;
201 int msg_size = sizeof (*rmp);
202 vl_api_registration_t *reg;
204 reg = vl_api_client_index_to_registration (mp->client_index);
208 rmp = vl_msg_api_alloc (msg_size);
209 memset (rmp, 0, msg_size);
211 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
212 rmp->context = mp->context;
213 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
214 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
216 vl_api_send_msg (reg, (u8 *) rmp);
220 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
223 vl_api_acl_plugin_control_ping_reply_t *rmp;
224 acl_main_t *am = &acl_main;
228 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
230 rmp->vpe_pid = ntohl (getpid ());
236 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
238 clib_warning ("%v", out0);
239 vec_reset_length (out0);
243 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
245 vlib_cli_output (vm, "%v", out0);
246 vec_reset_length (out0);
249 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
252 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
253 acl_main_t * am, int acl_index)
256 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
257 am->acls[acl_index].count, am->acls[acl_index].tag);
260 for (j = 0; j < am->acls[acl_index].count; j++)
262 r = &am->acls[acl_index].rules[j];
263 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
264 out0 = format_acl_action (out0, r->is_permit);
265 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
266 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
269 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
270 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
271 out0 = format (out0, " proto %d", r->proto);
272 out0 = format (out0, " sport %d", r->src_port_or_type_first);
273 if (r->src_port_or_type_first != r->src_port_or_type_last)
275 out0 = format (out0, "-%d", r->src_port_or_type_last);
277 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
278 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
280 out0 = format (out0, "-%d", r->dst_port_or_code_last);
282 if (r->tcp_flags_mask || r->tcp_flags_value)
285 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
288 out0 = format (out0, "\n");
294 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
296 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
300 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
302 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
306 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
309 u32 **ppolicy_epoch_by_swi =
310 is_input ? &am->input_policy_epoch_by_sw_if_index :
311 &am->output_policy_epoch_by_sw_if_index;
312 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
314 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
316 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
317 (is_input * FA_POLICY_EPOCH_IS_INPUT);
321 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
323 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
324 : &am->output_sw_if_index_vec_by_acl;
325 if (acl_num < vec_len (*p_swi_vec_by_acl))
328 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
330 increment_policy_epoch (am, *p_swi, is_input);
337 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
339 try_increment_acl_policy_epoch (am, acl_num, 0);
340 try_increment_acl_policy_epoch (am, acl_num, 1);
346 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
347 u32 * acl_list_index, u8 * tag)
349 acl_main_t *am = &acl_main;
352 acl_rule_t *acl_new_rules = 0;
355 if (am->trace_acl > 255)
356 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
359 if (*acl_list_index != ~0)
361 /* They supplied some number, let's see if this ACL exists */
362 if (pool_is_free_index (am->acls, *acl_list_index))
364 /* tried to replace a non-existent ACL, no point doing anything */
366 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
367 *acl_list_index, tag);
368 return VNET_API_ERROR_NO_SUCH_ENTRY;
374 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
375 *acl_list_index, tag);
378 void *oldheap = acl_set_heap (am);
380 /* Create and populate the rules */
382 vec_validate (acl_new_rules, count - 1);
384 for (i = 0; i < count; i++)
386 r = vec_elt_at_index (acl_new_rules, i);
387 memset (r, 0, sizeof (*r));
388 r->is_permit = rules[i].is_permit;
389 r->is_ipv6 = rules[i].is_ipv6;
392 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
393 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
397 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
398 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
400 r->src_prefixlen = rules[i].src_ip_prefix_len;
401 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
402 r->proto = rules[i].proto;
403 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
404 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
405 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
406 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
407 r->tcp_flags_value = rules[i].tcp_flags_value;
408 r->tcp_flags_mask = rules[i].tcp_flags_mask;
411 if (~0 == *acl_list_index)
414 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
415 memset (a, 0, sizeof (*a));
416 /* Will return the newly allocated ACL index */
417 *acl_list_index = a - am->acls;
421 a = am->acls + *acl_list_index;
422 /* Get rid of the old rules */
426 a->rules = acl_new_rules;
428 memcpy (a->tag, tag, sizeof (a->tag));
429 if (am->trace_acl > 255)
430 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
431 if (am->reclassify_sessions)
433 /* a change in an ACLs if they are applied may mean a new policy epoch */
434 policy_notify_acl_change (am, *acl_list_index);
437 /* notify the lookup contexts about the ACL changes */
438 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
439 clib_mem_set_heap (oldheap);
444 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
446 if (acl_index < vec_len (foo_index_vec_by_acl))
448 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
450 /* ACL is applied somewhere. */
458 acl_del_list (u32 acl_list_index)
460 acl_main_t *am = &acl_main;
462 if (pool_is_free_index (am->acls, acl_list_index))
464 return VNET_API_ERROR_NO_SUCH_ENTRY;
466 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
467 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
468 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
469 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
470 /* lookup contexts cover other cases, not just inbound/oubound, so check that */
471 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
472 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
474 void *oldheap = acl_set_heap (am);
476 /* now we can delete the ACL itself */
477 a = pool_elt_at_index (am->acls, acl_list_index);
480 pool_put (am->acls, a);
481 /* acl_list_index is now free, notify the lookup contexts */
482 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
483 clib_mem_set_heap (oldheap);
487 /* Some aids in ASCII graphing the content */
494 u8 ip4_5tuple_mask[] =
495 _(" dmac smac etype ")
496 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
503 _(" ttl pr checksum ")
512 _("L4 T/U sport dport ")
522 u8 ip6_5tuple_mask[] =
523 _(" dmac smac etype ")
524 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
526 _(0x0000) __ __ __ __
528 _(0x0004) __ __ XX __
530 _(0x0008) XX XX XX XX
531 _(0x000C) XX XX XX XX
532 _(0x0010) XX XX XX XX
533 _(0x0014) XX XX XX XX
535 _(0x0018) XX XX XX XX
536 _(0x001C) XX XX XX XX
537 _(0x0020) XX XX XX XX
538 _(0x0024) XX XX XX XX
539 _("L4T/U sport dport ")
540 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
542 u8 dot1q_5tuple_mask[] =
543 _(" dmac smac dot1q etype ")
544 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
545 _(padpad) __ __ __ __
546 _(padpad) __ __ __ __
547 _(padpad) __ __ __ __
550 u8 dot1ad_5tuple_mask[] =
551 _(" dmac smac dot1ad dot1q etype ")
552 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
553 _(padpad) __ __ __ __
554 _(padpad) __ __ __ __
557 u8 ethertype_mask[] =
558 _(" dmac smac etype ")
559 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
568 count_skip (u8 * p, u32 size)
570 u64 *p64 = (u64 *) p;
571 /* Be tolerant to null pointer */
575 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
579 return (p64 - (u64 *) p) / 2;
583 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
584 u32 mask_len, u32 next_table_index,
585 u32 miss_next_index, u32 * table_index,
589 u32 memory_size = 2 << 13;
590 u32 skip = count_skip (mask, mask_len);
591 u32 match = (mask_len / 16) - skip;
592 u8 *skip_mask_ptr = mask + 16 * skip;
593 u32 current_data_flag = 0;
594 int current_data_offset = 0;
598 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
599 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
600 memory_size, skip, match,
601 next_table_index, miss_next_index,
602 table_index, current_data_flag,
603 current_data_offset, is_add,
604 1 /* delete_chain */ );
605 clib_mem_set_heap (oldheap);
610 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
611 u32 mask_len, u32 next_table_index,
612 u32 miss_next_index, u32 * table_index,
616 u32 memory_size = 2 << 22;
617 u32 skip = count_skip (mask, mask_len);
618 u32 match = (mask_len / 16) - skip;
619 u8 *skip_mask_ptr = mask + 16 * skip;
620 u32 current_data_flag = 0;
621 int current_data_offset = 0;
626 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
627 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
628 memory_size, skip, match,
629 next_table_index, miss_next_index,
630 table_index, current_data_flag,
631 current_data_offset, is_add,
632 1 /* delete_chain */ );
633 clib_mem_set_heap (oldheap);
638 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
640 vnet_classify_main_t *cm = &vnet_classify_main;
641 u32 ip4_table_index = ~0;
642 u32 ip6_table_index = ~0;
643 u32 dot1q_table_index = ~0;
644 u32 dot1ad_table_index = ~0;
645 u32 etype_table_index = ~0;
646 void *oldheap = acl_set_heap (am);
648 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
650 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
652 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
654 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
656 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
659 /* switch to global heap while calling vnet_* functions */
660 clib_mem_set_heap (cm->vlib_main->heap_base);
661 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
663 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
666 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
667 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
668 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
669 sizeof (ip4_5tuple_mask) - 1, ~0,
670 am->l2_input_classify_next_acl_ip4,
671 &ip4_table_index, 0);
673 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
676 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
677 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
678 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
679 sizeof (ip6_5tuple_mask) - 1, ~0,
680 am->l2_input_classify_next_acl_ip6,
681 &ip6_table_index, 0);
683 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
686 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
687 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
688 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
689 sizeof (ip6_5tuple_mask) - 1, ~0,
690 ~0, &dot1q_table_index, 0);
692 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
695 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
696 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
697 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
698 sizeof (dot1ad_5tuple_mask) - 1, ~0,
699 ~0, &dot1ad_table_index, 0);
701 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
704 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
705 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
706 acl_classify_add_del_table_tiny (cm, ethertype_mask,
707 sizeof (ethertype_mask) - 1, ~0,
708 ~0, &etype_table_index, 0);
710 clib_mem_set_heap (oldheap);
715 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
717 vnet_classify_main_t *cm = &vnet_classify_main;
718 u32 ip4_table_index = ~0;
719 u32 ip6_table_index = ~0;
720 u32 dot1q_table_index = ~0;
721 u32 dot1ad_table_index = ~0;
722 u32 etype_table_index = ~0;
723 void *oldheap = acl_set_heap (am);
725 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
727 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
729 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
731 vec_validate_init_empty
732 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
733 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
736 /* switch to global heap while calling vnet_* functions */
737 clib_mem_set_heap (cm->vlib_main->heap_base);
739 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
741 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
744 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
745 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
746 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
747 sizeof (ip4_5tuple_mask) - 1, ~0,
748 am->l2_output_classify_next_acl_ip4,
749 &ip4_table_index, 0);
751 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
754 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
755 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
756 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
757 sizeof (ip6_5tuple_mask) - 1, ~0,
758 am->l2_output_classify_next_acl_ip6,
759 &ip6_table_index, 0);
761 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
764 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
765 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
766 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
767 sizeof (ip6_5tuple_mask) - 1, ~0,
768 ~0, &dot1q_table_index, 0);
770 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
773 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
774 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
775 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
776 sizeof (dot1ad_5tuple_mask) - 1, ~0,
777 ~0, &dot1ad_table_index, 0);
779 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
782 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
783 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
784 acl_classify_add_del_table_tiny (cm, ethertype_mask,
785 sizeof (ethertype_mask) - 1, ~0,
786 ~0, &etype_table_index, 0);
788 clib_mem_set_heap (oldheap);
793 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
794 u8 is_dot1ad, u8 is_ip6)
796 vnet_classify_main_t *cm = &vnet_classify_main;
806 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
812 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
814 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
815 idx = (is_dot1ad) ? 20 : 16;
818 /* 802.1ad ethertype */
821 /* 802.1q ethertype */
827 /* 802.1q ethertype */
832 /* add sessions to vlan tables per ethernet_type */
836 match[idx + 1] = 0xdd;
842 match[idx + 1] = 0x00;
845 vnet_classify_add_del_session (cm, table_index, match, next_acl,
846 session_idx, 0, 0, 0, 1);
847 /* reset the mask back to being a mask */
849 match[idx + 1] = 0xff;
860 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
863 ? am->input_etype_whitelist_by_sw_if_index
864 : am->output_etype_whitelist_by_sw_if_index;
865 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
869 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
870 u32 etype_table_index)
872 vnet_classify_main_t *cm = &vnet_classify_main;
874 ? am->input_etype_whitelist_by_sw_if_index
875 : am->output_etype_whitelist_by_sw_if_index;
876 u8 *match = ethertype_mask;
880 u16 *whitelist = vec_elt (v, sw_if_index);
881 u32 next = ~0; /* permit */
882 for (i = 0; i < vec_len (whitelist); i++)
885 match[12] = (whitelist[i] >> 8) & 0xff;
886 match[13] = whitelist[i] & 0xff;
888 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
889 whitelist[i], 0, 0, 0, 1);
892 /* restore the mask */
899 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
901 vnet_classify_main_t *cm = &vnet_classify_main;
902 u32 ip4_table_index = ~0;
903 u32 ip6_table_index = ~0;
904 u32 dot1q_table_index = ~0;
905 u32 dot1ad_table_index = ~0;
906 u32 etype_table_index = ~0;
909 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
911 /* in case there were previous tables attached */
912 acl_unhook_l2_input_classify (am, sw_if_index);
914 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
915 sizeof (ip4_5tuple_mask) - 1, ~0,
916 am->l2_input_classify_next_acl_ip4,
917 &ip4_table_index, 1);
922 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
923 sizeof (ip6_5tuple_mask) - 1, ~0,
924 am->l2_input_classify_next_acl_ip6,
925 &ip6_table_index, 1);
928 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
929 sizeof (ip4_5tuple_mask) - 1, ~0,
930 am->l2_input_classify_next_acl_ip4,
931 &ip4_table_index, 0);
935 if (intf_has_etype_whitelist (am, sw_if_index, 1))
937 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
938 &etype_table_index, 1);
939 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
943 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
944 sizeof (dot1ad_5tuple_mask) - 1,
945 etype_table_index, ~0,
946 &dot1ad_table_index, 1);
948 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
949 sizeof (dot1q_5tuple_mask) - 1,
950 dot1ad_table_index, ~0,
951 &dot1q_table_index, 1);
954 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
955 sizeof (dot1ad_5tuple_mask) - 1, ~0,
956 ~0, &dot1ad_table_index, 0);
957 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
958 sizeof (ip6_5tuple_mask) - 1, ~0,
959 am->l2_input_classify_next_acl_ip6,
960 &ip6_table_index, 0);
961 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
962 sizeof (ip4_5tuple_mask) - 1, ~0,
963 am->l2_input_classify_next_acl_ip4,
964 &ip4_table_index, 0);
969 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
970 ip6_table_index, dot1q_table_index);
974 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
975 sizeof (ip4_5tuple_mask) - 1, ~0,
976 am->l2_input_classify_next_acl_ip4,
977 &ip4_table_index, 0);
978 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
979 sizeof (ip6_5tuple_mask) - 1, ~0,
980 am->l2_input_classify_next_acl_ip6,
981 &ip6_table_index, 0);
982 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
983 sizeof (dot1q_5tuple_mask) - 1, ~0,
984 ~0, &dot1q_table_index, 0);
985 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
986 sizeof (dot1ad_5tuple_mask) - 1, ~0,
987 ~0, &dot1ad_table_index, 0);
991 /* add sessions to vlan tables per ethernet_type */
992 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
993 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
994 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
995 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
997 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
999 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
1001 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
1003 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1005 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1007 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
1010 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
1012 clib_mem_set_heap (prevheap);
1017 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
1019 vnet_classify_main_t *cm = &vnet_classify_main;
1020 u32 ip4_table_index = ~0;
1021 u32 ip6_table_index = ~0;
1022 u32 dot1q_table_index = ~0;
1023 u32 dot1ad_table_index = ~0;
1024 u32 etype_table_index = ~0;
1027 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
1029 /* in case there were previous tables attached */
1030 acl_unhook_l2_output_classify (am, sw_if_index);
1032 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1033 sizeof (ip4_5tuple_mask) - 1, ~0,
1034 am->l2_output_classify_next_acl_ip4,
1035 &ip4_table_index, 1);
1039 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1040 sizeof (ip6_5tuple_mask) - 1, ~0,
1041 am->l2_output_classify_next_acl_ip6,
1042 &ip6_table_index, 1);
1045 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1046 sizeof (ip4_5tuple_mask) - 1, ~0,
1047 am->l2_output_classify_next_acl_ip4,
1048 &ip4_table_index, 0);
1052 if (intf_has_etype_whitelist (am, sw_if_index, 0))
1054 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
1055 &etype_table_index, 1);
1056 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
1061 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1062 sizeof (dot1ad_5tuple_mask) - 1,
1063 etype_table_index, ~0,
1064 &dot1ad_table_index, 1);
1066 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1067 sizeof (dot1q_5tuple_mask) - 1,
1068 dot1ad_table_index, ~0,
1069 &dot1q_table_index, 1);
1072 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1073 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1074 ~0, &dot1ad_table_index, 0);
1075 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1076 sizeof (ip6_5tuple_mask) - 1, ~0,
1077 am->l2_output_classify_next_acl_ip6,
1078 &ip6_table_index, 0);
1079 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1080 sizeof (ip4_5tuple_mask) - 1, ~0,
1081 am->l2_output_classify_next_acl_ip4,
1082 &ip4_table_index, 0);
1087 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1088 ip6_table_index, dot1q_table_index);
1091 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
1092 sw_if_index, ip4_table_index, ip6_table_index);
1096 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1097 sizeof (ip6_5tuple_mask) - 1, ~0,
1098 am->l2_output_classify_next_acl_ip6,
1099 &ip6_table_index, 0);
1100 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1101 sizeof (ip4_5tuple_mask) - 1, ~0,
1102 am->l2_output_classify_next_acl_ip4,
1103 &ip4_table_index, 0);
1104 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1105 sizeof (dot1q_5tuple_mask) - 1, ~0,
1106 ~0, &dot1q_table_index, 0);
1107 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1108 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1109 ~0, &dot1ad_table_index, 0);
1113 /* add sessions to vlan tables per ethernet_type */
1114 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
1115 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
1116 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
1117 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
1119 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
1121 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
1123 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
1125 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
1127 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
1130 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
1132 clib_mem_set_heap (prevheap);
1137 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
1139 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1140 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1141 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1143 clib_mem_set_heap (oldheap);
1148 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1153 /* Utterly wrong? */
1154 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1156 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1158 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
1162 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1166 rv = acl_hook_l2_input_classify (am, sw_if_index);
1170 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1173 am->in_acl_on_sw_if_index =
1174 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
1180 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1185 /* Utterly wrong? */
1186 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1188 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1190 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
1194 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1198 rv = acl_hook_l2_output_classify (am, sw_if_index);
1202 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1205 am->out_acl_on_sw_if_index =
1206 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
1212 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
1213 int is_input, int enable_disable)
1216 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
1218 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
1222 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1224 return (pool_is_free_index (am->acls, acl_list_index));
1228 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
1229 u8 is_input, u32 * vec_acl_list_index,
1230 int *may_clear_sessions)
1233 uword *seen_acl_bitmap = 0;
1234 uword *old_seen_acl_bitmap = 0;
1235 uword *change_acl_bitmap = 0;
1240 if (am->trace_acl > 255)
1242 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
1243 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
1245 vec_foreach (pacln, vec_acl_list_index)
1247 if (acl_is_not_defined (am, *pacln))
1249 /* ACL is not defined. Can not apply */
1250 clib_warning ("ERROR: ACL %d not defined", *pacln);
1251 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1254 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
1256 /* ACL being applied twice within the list. error. */
1257 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
1258 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1261 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
1265 u32 **pinout_lc_index_by_sw_if_index =
1267 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
1269 u32 ***pinout_acl_vec_by_sw_if_index =
1271 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1273 u32 ***pinout_sw_if_index_vec_by_acl =
1275 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
1277 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1279 clib_bitmap_validate (old_seen_acl_bitmap, 1);
1281 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
1283 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
1286 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
1288 if (am->trace_acl > 255)
1289 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
1290 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
1291 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
1294 clib_bitmap_foreach(acln, change_acl_bitmap, ({
1295 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
1296 /* ACL is being removed. */
1297 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
1298 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1299 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
1302 /* ACL is being added. */
1303 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
1304 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1309 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1310 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
1311 vec_dup (vec_acl_list_index);
1313 if (am->reclassify_sessions)
1315 /* re-applying ACLs means a new policy epoch */
1316 increment_policy_epoch (am, sw_if_index, is_input);
1320 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
1321 if (may_clear_sessions && *may_clear_sessions
1322 && !clib_bitmap_is_zero (change_acl_bitmap))
1324 acl_clear_sessions (am, sw_if_index);
1325 *may_clear_sessions = 0;
1330 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
1332 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
1334 if (vec_len (vec_acl_list_index) > 0)
1336 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
1339 if (~0 == am->interface_acl_user_id)
1340 am->interface_acl_user_id =
1341 acl_plugin.register_user_module ("interface ACL", "sw_if_index",
1344 acl_plugin.get_lookup_context_index (am->interface_acl_user_id,
1345 sw_if_index, is_input);
1346 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
1348 acl_plugin.set_acl_vec_for_context (lc_index, vec_acl_list_index);
1352 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
1354 acl_plugin.put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
1355 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
1359 /* ensure ACL processing is enabled/disabled as needed */
1360 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
1361 vec_len (vec_acl_list_index) > 0);
1364 clib_bitmap_free (change_acl_bitmap);
1365 clib_bitmap_free (seen_acl_bitmap);
1366 clib_bitmap_free (old_seen_acl_bitmap);
1371 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
1372 int *may_clear_sessions)
1374 acl_main_t *am = &acl_main;
1375 void *oldheap = acl_set_heap (am);
1376 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
1377 may_clear_sessions);
1378 clib_mem_set_heap (oldheap);
1382 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1386 acl_main_t *am = &acl_main;
1388 int may_clear_sessions = 1;
1390 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
1391 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1393 u32 ***pinout_acl_vec_by_sw_if_index =
1395 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1397 void *oldheap = acl_set_heap (am);
1401 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1402 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1407 rv = error_already_applied;
1411 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1412 vec_add1 (acl_vec, acl_list_index);
1416 if (sw_if_index > vec_len (*pinout_acl_vec_by_sw_if_index))
1418 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1422 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1427 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1431 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1432 vec_del1 (acl_vec, index);
1435 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
1436 &may_clear_sessions);
1439 clib_mem_set_heap (oldheap);
1444 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1447 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1448 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1450 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1451 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1453 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1454 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1457 * if there are already inbound/outbound ACLs applied, toggle the
1458 * enable/disable - this will recreate the necessary tables.
1461 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1463 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1465 acl_interface_in_enable_disable (am, sw_if_index, 0);
1466 acl_interface_in_enable_disable (am, sw_if_index, 1);
1469 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1471 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1473 acl_interface_out_enable_disable (am, sw_if_index, 0);
1474 acl_interface_out_enable_disable (am, sw_if_index, 1);
1489 u32 arp_table_index;
1490 u32 dot1q_table_index;
1491 u32 dot1ad_table_index;
1492 u32 arp_dot1q_table_index;
1493 u32 arp_dot1ad_table_index;
1495 u32 out_table_index;
1496 u32 out_arp_table_index;
1497 u32 out_dot1q_table_index;
1498 u32 out_dot1ad_table_index;
1499 u32 out_arp_dot1q_table_index;
1500 u32 out_arp_dot1ad_table_index;
1501 } macip_match_type_t;
1504 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1510 for (i = 0; i < vec_len (mv); i++)
1512 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1513 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1523 /* Get metric used to sort match types.
1524 The more specific and the more often seen - the bigger the metric */
1526 match_type_metric (macip_match_type_t * m)
1528 unsigned int mac_bits_set = 0;
1529 unsigned int mac_byte;
1531 for (i = 0; i < 6; i++)
1533 mac_byte = m->mac_mask[i];
1534 for (; mac_byte; mac_byte >>= 1)
1535 mac_bits_set += mac_byte & 1;
1538 * Attempt to place the more specific and the more used rules on top.
1539 * There are obvious caveat corner cases to this, but they do not
1540 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1541 * going with a wildcard IPv4 with a specific MAC).
1543 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1547 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1549 /* Ascending sort based on the metric values */
1550 return match_type_metric (m1) - match_type_metric (m2);
1553 /* Get the offset of L3 source within ethernet packet */
1555 get_l3_src_offset (int is6)
1558 return (sizeof (ethernet_header_t) +
1559 offsetof (ip6_header_t, src_address));
1561 return (sizeof (ethernet_header_t) +
1562 offsetof (ip4_header_t, src_address));
1566 get_l3_dst_offset (int is6)
1569 return (sizeof (ethernet_header_t) +
1570 offsetof (ip6_header_t, dst_address));
1572 return (sizeof (ethernet_header_t) +
1573 offsetof (ip4_header_t, dst_address));
1577 * return if the is_permit value also requires to create the egress tables
1578 * For backwards compatibility, we keep the is_permit = 1 to only
1579 * create the ingress tables, and the new value of 3 will also
1580 * create the egress tables based on destination.
1583 macip_permit_also_egress (u8 is_permit)
1585 return (is_permit == 3);
1589 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1591 macip_match_type_t *mvec = NULL;
1592 macip_match_type_t *mt;
1593 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1595 u32 match_type_index;
1599 vnet_classify_main_t *cm = &vnet_classify_main;
1601 /* Count the number of different types of rules */
1602 for (i = 0; i < a->count; i++)
1606 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1607 a->rules[i].src_prefixlen,
1608 a->rules[i].is_ipv6)))
1610 match_type_index = vec_len (mvec);
1611 vec_validate (mvec, match_type_index);
1612 memcpy (mvec[match_type_index].mac_mask,
1613 a->rules[i].src_mac_mask, 6);
1614 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1615 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1616 mvec[match_type_index].has_egress = 0;
1617 mvec[match_type_index].table_index = ~0;
1618 mvec[match_type_index].arp_table_index = ~0;
1619 mvec[match_type_index].dot1q_table_index = ~0;
1620 mvec[match_type_index].dot1ad_table_index = ~0;
1621 mvec[match_type_index].arp_dot1q_table_index = ~0;
1622 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1623 mvec[match_type_index].out_table_index = ~0;
1624 mvec[match_type_index].out_arp_table_index = ~0;
1625 mvec[match_type_index].out_dot1q_table_index = ~0;
1626 mvec[match_type_index].out_dot1ad_table_index = ~0;
1627 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1628 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1630 mvec[match_type_index].count++;
1631 mvec[match_type_index].has_egress |=
1632 macip_permit_also_egress (a->rules[i].is_permit);
1634 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1635 vec_sort_with_function (mvec, match_type_compare);
1636 /* Create the classifier tables */
1638 out_last_table = ~0;
1639 /* First add ARP tables */
1640 vec_foreach (mt, mvec)
1643 int is6 = mt->is_ipv6;
1645 u32 *last_tag_table;
1646 u32 *out_last_tag_table;
1653 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
1654 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1655 | Destination Address |
1656 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1658 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1660 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1661 | EtherType | Hardware Type |
1662 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1663 | Protocol Type | Hw addr len | Proto addr len|
1664 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1666 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1667 | Sender Hardware Address |
1668 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1669 | Sender Protocol Address |
1670 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1671 | Target Hardware Address |
1672 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1673 | | TargetProtocolAddress |
1674 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1676 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1678 for (tags = 2; tags >= 0; tags--)
1680 memset (mask, 0, sizeof (mask));
1681 /* source MAC address */
1682 memcpy (&mask[6], mt->mac_mask, 6);
1688 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1690 last_tag_table = &mt->arp_table_index;
1693 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1694 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1696 last_tag_table = &mt->arp_dot1q_table_index;
1699 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1700 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1701 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1703 last_tag_table = &mt->arp_dot1ad_table_index;
1707 /* sender hardware address within ARP */
1708 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1709 /* sender protocol address within ARP */
1710 for (i = 0; i < (mt->prefix_len / 8); i++)
1711 mask[l3_offset + 14 + i] = 0xff;
1712 if (mt->prefix_len % 8)
1713 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1714 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1716 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1718 1)) / sizeof (u32x4)) * sizeof (u32x4);
1719 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1720 (~0 == last_table) ? 0 : ~0,
1722 last_table = *last_tag_table;
1725 /* egress ARP table */
1726 memset (mask, 0, sizeof (mask));
1732 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1734 out_last_tag_table = &mt->out_arp_table_index;
1737 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1738 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1740 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1743 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1744 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1745 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1747 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1751 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1752 /* mask captures just the destination and the ethertype */
1753 mask_len = ((l3_offset +
1755 1)) / sizeof (u32x4)) * sizeof (u32x4);
1756 acl_classify_add_del_table_small (cm, mask, mask_len,
1759 out_last_table) ? 0 : ~0,
1760 out_last_tag_table, 1);
1761 out_last_table = *out_last_tag_table;
1766 /* Now add IP[46] tables */
1767 vec_foreach (mt, mvec)
1770 int is6 = mt->is_ipv6;
1774 u32 *last_tag_table;
1775 u32 *out_last_tag_table;
1778 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1780 for (tags = 2; tags >= 0; tags--)
1782 memset (mask, 0, sizeof (mask));
1783 memcpy (&mask[6], mt->mac_mask, 6);
1784 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1789 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1790 last_tag_table = &mt->table_index;
1793 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1794 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1795 last_tag_table = &mt->dot1q_table_index;
1798 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1799 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1800 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1801 last_tag_table = &mt->dot1ad_table_index;
1804 for (i = 0; i < (mt->prefix_len / 8); i++)
1806 mask[l3_src_offs + i] = 0xff;
1808 if (mt->prefix_len % 8)
1810 mask[l3_src_offs + (mt->prefix_len / 8)] =
1811 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1814 * Round-up the number of bytes needed to store the prefix,
1815 * and round up the number of vectors too
1817 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1818 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1819 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1820 (~0 == last_table) ? 0 : ~0,
1822 last_table = *last_tag_table;
1826 for (tags = 2; tags >= 0; tags--)
1828 memset (mask, 0, sizeof (mask));
1829 /* MAC destination */
1830 memcpy (&mask[0], mt->mac_mask, 6);
1831 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1836 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1837 out_last_tag_table = &mt->out_table_index;
1840 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1841 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1842 out_last_tag_table = &mt->out_dot1q_table_index;
1845 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1846 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1847 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1848 out_last_tag_table = &mt->out_dot1ad_table_index;
1851 for (i = 0; i < (mt->prefix_len / 8); i++)
1853 mask[l3_dst_offs + i] = 0xff;
1855 if (mt->prefix_len % 8)
1857 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1858 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1861 * Round-up the number of bytes needed to store the prefix,
1862 * and round up the number of vectors too
1864 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1866 1)) / sizeof (u32x4)) * sizeof (u32x4);
1867 acl_classify_add_del_table_small (cm, mask, mask_len,
1869 (~0 == out_last_table) ? 0 : ~0,
1870 out_last_tag_table, 1);
1871 out_last_table = *out_last_tag_table;
1875 a->ip4_table_index = last_table;
1876 a->ip6_table_index = last_table;
1877 a->l2_table_index = last_table;
1879 a->out_ip4_table_index = out_last_table;
1880 a->out_ip6_table_index = out_last_table;
1881 a->out_l2_table_index = out_last_table;
1883 /* Populate the classifier tables with rules from the MACIP ACL */
1884 for (i = 0; i < a->count; i++)
1888 int is6 = a->rules[i].is_ipv6;
1895 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1896 a->rules[i].src_prefixlen,
1897 a->rules[i].is_ipv6);
1898 ASSERT (match_type_index != ~0);
1900 for (tags = 2; tags >= 0; tags--)
1902 memset (mask, 0, sizeof (mask));
1903 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1904 memcpy (&mask[6], a->rules[i].src_mac, 6);
1909 tag_table = mvec[match_type_index].table_index;
1913 tag_table = mvec[match_type_index].dot1q_table_index;
1919 tag_table = mvec[match_type_index].dot1ad_table_index;
1929 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1931 mask[eth + 1] = 0xdd;
1935 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1937 mask[eth + 1] = 0x00;
1940 /* add session to table mvec[match_type_index].table_index; */
1941 vnet_classify_add_del_session (cm, tag_table,
1942 mask, a->rules[i].is_permit ? ~0 : 0,
1943 i, 0, action, metadata, 1);
1944 memset (&mask[12], 0, sizeof (mask) - 12);
1947 /* add ARP table entry too */
1948 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1950 memset (mask, 0, sizeof (mask));
1951 memcpy (&mask[6], a->rules[i].src_mac, 6);
1953 for (tags = 2; tags >= 0; tags--)
1959 tag_table = mvec[match_type_index].arp_table_index;
1965 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1973 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1984 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1985 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1987 vnet_classify_add_del_session (cm, tag_table, mask,
1988 a->rules[i].is_permit ? ~0 : 0,
1989 i, 0, action, metadata, 1);
1992 if (macip_permit_also_egress (a->rules[i].is_permit))
1994 /* Add the egress entry with destination set */
1995 for (tags = 2; tags >= 0; tags--)
1997 memset (mask, 0, sizeof (mask));
1998 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1999 /* src mac in the other direction becomes dst */
2000 memcpy (&mask[0], a->rules[i].src_mac, 6);
2005 tag_table = mvec[match_type_index].out_table_index;
2009 tag_table = mvec[match_type_index].out_dot1q_table_index;
2015 tag_table = mvec[match_type_index].out_dot1ad_table_index;
2025 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
2028 mask[eth + 1] = 0xdd;
2032 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
2035 mask[eth + 1] = 0x00;
2038 /* add session to table mvec[match_type_index].table_index; */
2039 vnet_classify_add_del_session (cm, tag_table,
2041 a->rules[i].is_permit ? ~0 : 0,
2042 i, 0, action, metadata, 1);
2043 // memset (&mask[12], 0, sizeof (mask) - 12);
2046 /* add ARP table entry too */
2047 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
2049 for (tags = 2; tags >= 0; tags--)
2051 memset (mask, 0, sizeof (mask));
2056 tag_table = mvec[match_type_index].out_arp_table_index;
2062 mvec[match_type_index].out_arp_dot1q_table_index;
2070 mvec[match_type_index].out_arp_dot1ad_table_index;
2080 vnet_classify_add_del_session (cm, tag_table,
2083 rules[i].is_permit ? ~0 : 0,
2084 i, 0, action, metadata, 1);
2093 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
2095 vnet_classify_main_t *cm = &vnet_classify_main;
2096 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2098 if (a->ip4_table_index != ~0)
2100 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2101 &a->ip4_table_index, 0);
2102 a->ip4_table_index = ~0;
2104 if (a->ip6_table_index != ~0)
2106 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2107 &a->ip6_table_index, 0);
2108 a->ip6_table_index = ~0;
2110 if (a->l2_table_index != ~0)
2112 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
2114 a->l2_table_index = ~0;
2116 if (a->out_ip4_table_index != ~0)
2118 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2119 &a->out_ip4_table_index, 0);
2120 a->out_ip4_table_index = ~0;
2122 if (a->out_ip6_table_index != ~0)
2124 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2125 &a->out_ip6_table_index, 0);
2126 a->out_ip6_table_index = ~0;
2128 if (a->out_l2_table_index != ~0)
2130 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2131 &a->out_l2_table_index, 0);
2132 a->out_l2_table_index = ~0;
2137 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2143 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2145 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2146 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2148 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2149 a->ip6_table_index, a->l2_table_index,
2151 /* return the first unhappy outcome but make try to plough through. */
2154 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2155 a->out_ip6_table_index,
2156 a->out_l2_table_index, is_apply);
2157 /* return the first unhappy outcome but make try to plough through. */
2164 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2165 u32 * acl_list_index, u8 * tag)
2167 acl_main_t *am = &acl_main;
2168 macip_acl_list_t *a;
2169 macip_acl_rule_t *r;
2170 macip_acl_rule_t *acl_new_rules = 0;
2174 if (*acl_list_index != ~0)
2176 /* They supplied some number, let's see if this MACIP ACL exists */
2177 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2179 /* tried to replace a non-existent ACL, no point doing anything */
2181 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2182 *acl_list_index, tag);
2183 return VNET_API_ERROR_NO_SUCH_ENTRY;
2190 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2193 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2194 if (~0 != *acl_list_index)
2195 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2196 void *oldheap = acl_set_heap (am);
2197 /* Create and populate the rules */
2199 vec_validate (acl_new_rules, count - 1);
2201 for (i = 0; i < count; i++)
2203 r = &acl_new_rules[i];
2204 r->is_permit = rules[i].is_permit;
2205 r->is_ipv6 = rules[i].is_ipv6;
2206 memcpy (&r->src_mac, rules[i].src_mac, 6);
2207 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2208 if (rules[i].is_ipv6)
2209 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2211 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2212 r->src_prefixlen = rules[i].src_ip_prefix_len;
2215 if (~0 == *acl_list_index)
2218 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2219 memset (a, 0, sizeof (*a));
2220 /* Will return the newly allocated ACL index */
2221 *acl_list_index = a - am->macip_acls;
2225 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2228 vec_free (a->rules);
2230 macip_destroy_classify_tables (am, *acl_list_index);
2233 a->rules = acl_new_rules;
2235 memcpy (a->tag, tag, sizeof (a->tag));
2237 /* Create and populate the classifer tables */
2238 macip_create_classify_tables (am, *acl_list_index);
2239 clib_mem_set_heap (oldheap);
2240 /* If the ACL was already applied somewhere, reapply the newly created tables */
2242 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2246 /* No check that sw_if_index denotes a valid interface - the callers
2247 * were supposed to validate.
2249 * That said, if sw_if_index corresponds to an interface that exists at all,
2250 * this function must return errors accordingly if the ACL is not applied.
2254 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2257 u32 macip_acl_index;
2258 macip_acl_list_t *a;
2260 /* The vector is too short - MACIP ACL is not applied */
2261 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
2262 return VNET_API_ERROR_NO_SUCH_ENTRY;
2264 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2265 /* No point in deleting MACIP ACL which is not applied */
2266 if (~0 == macip_acl_index)
2267 return VNET_API_ERROR_NO_SUCH_ENTRY;
2269 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2270 /* remove the classifier tables off the interface L2 ACL */
2272 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2273 a->ip6_table_index, a->l2_table_index, 0);
2275 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2276 a->out_ip4_table_index, a->out_ip6_table_index,
2277 a->out_l2_table_index, 0);
2278 /* Unset the MACIP ACL index */
2279 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2280 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
2281 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
2284 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
2288 /* No check for validity of sw_if_index - the callers were supposed to validate */
2291 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2292 u32 macip_acl_index)
2294 macip_acl_list_t *a;
2296 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2298 return VNET_API_ERROR_NO_SUCH_ENTRY;
2300 void *oldheap = acl_set_heap (am);
2301 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2302 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2303 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
2304 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
2305 clib_mem_set_heap (oldheap);
2306 /* If there already a MACIP ACL applied, unapply it */
2307 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2308 macip_acl_interface_del_acl (am, sw_if_index);
2309 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2311 /* Apply the classifier tables for L2 ACLs */
2313 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2314 a->ip6_table_index, a->l2_table_index, 1);
2316 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2317 a->out_ip4_table_index, a->out_ip6_table_index,
2318 a->out_l2_table_index, 1);
2323 macip_acl_del_list (u32 acl_list_index)
2325 acl_main_t *am = &acl_main;
2326 macip_acl_list_t *a;
2328 if (pool_is_free_index (am->macip_acls, acl_list_index))
2330 return VNET_API_ERROR_NO_SUCH_ENTRY;
2333 /* delete any references to the ACL */
2334 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2336 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2338 macip_acl_interface_del_acl (am, i);
2342 void *oldheap = acl_set_heap (am);
2343 /* Now that classifier tables are detached, clean them up */
2344 macip_destroy_classify_tables (am, acl_list_index);
2346 /* now we can delete the ACL itself */
2347 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2350 vec_free (a->rules);
2352 pool_put (am->macip_acls, a);
2353 clib_mem_set_heap (oldheap);
2359 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2362 acl_main_t *am = &acl_main;
2366 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2370 rv = macip_acl_interface_del_acl (am, sw_if_index);
2376 * If the client does not allocate enough memory for a variable-length
2377 * message, and then proceed to use it as if the full memory allocated,
2378 * absent the check we happily consume that on the VPP side, and go
2379 * along as if nothing happened. However, the resulting
2380 * effects range from just garbage in the API decode
2381 * (because the decoder snoops too far), to potential memory
2384 * This verifies that the actual length of the message is
2385 * at least expected_len, and complains loudly if it is not.
2387 * A failing check here is 100% a software bug on the API user side,
2388 * so we might as well yell.
2392 verify_message_len (void *mp, u32 expected_len, char *where)
2394 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2395 if (supplied_len < expected_len)
2397 clib_warning ("%s: Supplied message length %d is less than expected %d",
2398 where, supplied_len, expected_len);
2407 /* API message handler */
2409 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2411 vl_api_acl_add_replace_reply_t *rmp;
2412 acl_main_t *am = &acl_main;
2414 u32 acl_list_index = ntohl (mp->acl_index);
2415 u32 acl_count = ntohl (mp->count);
2416 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2418 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2420 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2424 rv = VNET_API_ERROR_INVALID_VALUE;
2428 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2430 rmp->acl_index = htonl(acl_list_index);
2436 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2438 acl_main_t *am = &acl_main;
2439 vl_api_acl_del_reply_t *rmp;
2442 rv = acl_del_list (ntohl (mp->acl_index));
2444 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2448 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2450 acl_main_t *am = &acl_main;
2451 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2452 u32 sw_if_index = ntohl (mp->sw_if_index);
2453 vl_api_acl_interface_add_del_reply_t *rmp;
2456 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2457 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2460 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2461 mp->is_input, ntohl (mp->acl_index));
2463 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2467 vl_api_acl_interface_set_acl_list_t_handler
2468 (vl_api_acl_interface_set_acl_list_t * mp)
2470 acl_main_t *am = &acl_main;
2471 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2474 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2475 u32 sw_if_index = ntohl (mp->sw_if_index);
2477 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2478 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2481 int may_clear_sessions = 1;
2482 for (i = 0; i < mp->count; i++)
2484 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2486 /* ACL does not exist, so we can not apply it */
2487 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2492 void *oldheap = acl_set_heap (am);
2494 u32 *in_acl_vec = 0;
2495 u32 *out_acl_vec = 0;
2496 for (i = 0; i < mp->count; i++)
2497 if (i < mp->n_input)
2498 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2500 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2503 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2504 &may_clear_sessions);
2506 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2508 &may_clear_sessions);
2509 vec_free (in_acl_vec);
2510 vec_free (out_acl_vec);
2511 clib_mem_set_heap (oldheap);
2515 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2519 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2521 api_rule->is_permit = r->is_permit;
2522 api_rule->is_ipv6 = r->is_ipv6;
2525 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2526 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2530 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2531 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2533 api_rule->src_ip_prefix_len = r->src_prefixlen;
2534 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2535 api_rule->proto = r->proto;
2536 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2537 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2538 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2539 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2540 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2541 api_rule->tcp_flags_value = r->tcp_flags_value;
2545 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2546 acl_list_t * acl, u32 context)
2548 vl_api_acl_details_t *mp;
2549 vl_api_acl_rule_t *rules;
2551 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2552 void *oldheap = acl_set_heap (am);
2554 mp = vl_msg_api_alloc (msg_size);
2555 memset (mp, 0, msg_size);
2556 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2558 /* fill in the message */
2559 mp->context = context;
2560 mp->count = htonl (acl->count);
2561 mp->acl_index = htonl (acl - am->acls);
2562 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2563 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2565 for (i = 0; i < acl->count; i++)
2567 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2570 clib_mem_set_heap (oldheap);
2571 vl_api_send_msg (reg, (u8 *) mp);
2576 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2578 acl_main_t *am = &acl_main;
2582 vl_api_registration_t *reg;
2584 reg = vl_api_client_index_to_registration (mp->client_index);
2588 if (mp->acl_index == ~0)
2591 /* Just dump all ACLs */
2592 pool_foreach (acl, am->acls,
2594 send_acl_details(am, reg, acl, mp->context);
2600 acl_index = ntohl (mp->acl_index);
2601 if (!pool_is_free_index (am->acls, acl_index))
2603 acl = pool_elt_at_index (am->acls, acl_index);
2604 send_acl_details (am, reg, acl, mp->context);
2610 /* FIXME API: should we signal an error here at all ? */
2616 send_acl_interface_list_details (acl_main_t * am,
2617 vl_api_registration_t * reg,
2618 u32 sw_if_index, u32 context)
2620 vl_api_acl_interface_list_details_t *mp;
2626 void *oldheap = acl_set_heap (am);
2628 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2629 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2631 clib_mem_set_heap (oldheap);
2633 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2634 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2635 count = n_input + n_output;
2637 msg_size = sizeof (*mp);
2638 msg_size += sizeof (mp->acls[0]) * count;
2640 mp = vl_msg_api_alloc (msg_size);
2641 memset (mp, 0, msg_size);
2643 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2645 /* fill in the message */
2646 mp->context = context;
2647 mp->sw_if_index = htonl (sw_if_index);
2649 mp->n_input = n_input;
2650 for (i = 0; i < n_input; i++)
2652 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2654 for (i = 0; i < n_output; i++)
2656 mp->acls[n_input + i] =
2657 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2659 vl_api_send_msg (reg, (u8 *) mp);
2663 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2666 acl_main_t *am = &acl_main;
2667 vnet_sw_interface_t *swif;
2668 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2671 vl_api_registration_t *reg;
2673 reg = vl_api_client_index_to_registration (mp->client_index);
2677 if (mp->sw_if_index == ~0)
2680 pool_foreach (swif, im->sw_interfaces,
2682 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2688 sw_if_index = ntohl (mp->sw_if_index);
2689 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2690 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2694 /* MACIP ACL API handlers */
2697 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2699 vl_api_macip_acl_add_reply_t *rmp;
2700 acl_main_t *am = &acl_main;
2702 u32 acl_list_index = ~0;
2703 u32 acl_count = ntohl (mp->count);
2704 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2706 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2708 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2712 rv = VNET_API_ERROR_INVALID_VALUE;
2716 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2718 rmp->acl_index = htonl(acl_list_index);
2724 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2726 vl_api_macip_acl_add_replace_reply_t *rmp;
2727 acl_main_t *am = &acl_main;
2729 u32 acl_list_index = ntohl (mp->acl_index);
2730 u32 acl_count = ntohl (mp->count);
2731 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2733 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2735 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2739 rv = VNET_API_ERROR_INVALID_VALUE;
2743 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2745 rmp->acl_index = htonl(acl_list_index);
2751 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2753 acl_main_t *am = &acl_main;
2754 vl_api_macip_acl_del_reply_t *rmp;
2757 rv = macip_acl_del_list (ntohl (mp->acl_index));
2759 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2763 vl_api_macip_acl_interface_add_del_t_handler
2764 (vl_api_macip_acl_interface_add_del_t * mp)
2766 acl_main_t *am = &acl_main;
2767 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2769 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2770 u32 sw_if_index = ntohl (mp->sw_if_index);
2772 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2773 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2776 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2777 ntohl (mp->acl_index));
2779 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2783 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2784 macip_acl_list_t * acl, u32 context)
2786 vl_api_macip_acl_details_t *mp;
2787 vl_api_macip_acl_rule_t *rules;
2788 macip_acl_rule_t *r;
2790 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2792 mp = vl_msg_api_alloc (msg_size);
2793 memset (mp, 0, msg_size);
2794 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2796 /* fill in the message */
2797 mp->context = context;
2800 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2801 mp->count = htonl (acl->count);
2802 mp->acl_index = htonl (acl - am->macip_acls);
2804 for (i = 0; i < acl->count; i++)
2807 rules[i].is_permit = r->is_permit;
2808 rules[i].is_ipv6 = r->is_ipv6;
2809 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2810 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2811 sizeof (r->src_mac_mask));
2813 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2814 sizeof (r->src_ip_addr.ip6));
2816 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2817 sizeof (r->src_ip_addr.ip4));
2818 rules[i].src_ip_prefix_len = r->src_prefixlen;
2823 /* No martini, no party - no ACL applied to this interface. */
2828 vl_api_send_msg (reg, (u8 *) mp);
2833 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2835 acl_main_t *am = &acl_main;
2836 macip_acl_list_t *acl;
2838 vl_api_registration_t *reg;
2840 reg = vl_api_client_index_to_registration (mp->client_index);
2844 if (mp->acl_index == ~0)
2846 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2847 pool_foreach (acl, am->macip_acls, (
2849 send_macip_acl_details (am, reg,
2858 u32 acl_index = ntohl (mp->acl_index);
2859 if (!pool_is_free_index (am->macip_acls, acl_index))
2861 acl = pool_elt_at_index (am->macip_acls, acl_index);
2862 send_macip_acl_details (am, reg, acl, mp->context);
2868 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2871 acl_main_t *am = &acl_main;
2872 vl_api_macip_acl_interface_get_reply_t *rmp;
2873 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2874 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2875 vl_api_registration_t *reg;
2878 reg = vl_api_client_index_to_registration (mp->client_index);
2882 rmp = vl_msg_api_alloc (msg_size);
2883 memset (rmp, 0, msg_size);
2885 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2886 rmp->context = mp->context;
2887 rmp->count = htonl (count);
2888 for (i = 0; i < count; i++)
2890 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2893 vl_api_send_msg (reg, (u8 *) rmp);
2897 send_macip_acl_interface_list_details (acl_main_t * am,
2898 vl_api_registration_t * reg,
2900 u32 acl_index, u32 context)
2902 vl_api_macip_acl_interface_list_details_t *rmp;
2903 /* at this time there is only ever 1 mac ip acl per interface */
2904 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2906 rmp = vl_msg_api_alloc (msg_size);
2907 memset (rmp, 0, msg_size);
2909 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2911 /* fill in the message */
2912 rmp->context = context;
2914 rmp->sw_if_index = htonl (sw_if_index);
2915 rmp->acls[0] = htonl (acl_index);
2917 vl_api_send_msg (reg, (u8 *) rmp);
2921 vl_api_macip_acl_interface_list_dump_t_handler
2922 (vl_api_macip_acl_interface_list_dump_t * mp)
2924 vl_api_registration_t *reg;
2925 acl_main_t *am = &acl_main;
2926 u32 sw_if_index = ntohl (mp->sw_if_index);
2928 reg = vl_api_client_index_to_registration (mp->client_index);
2932 if (sw_if_index == ~0)
2934 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2936 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2938 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2939 am->macip_acl_by_sw_if_index
2947 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2949 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2950 am->macip_acl_by_sw_if_index
2951 [sw_if_index], mp->context);
2957 vl_api_acl_interface_set_etype_whitelist_t_handler
2958 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2960 acl_main_t *am = &acl_main;
2961 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2964 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2965 u32 sw_if_index = ntohl (mp->sw_if_index);
2966 u16 *vec_in = 0, *vec_out = 0;
2967 void *oldheap = acl_set_heap (am);
2969 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2970 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2973 for (i = 0; i < mp->count; i++)
2975 if (i < mp->n_input)
2976 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2978 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2980 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2983 clib_mem_set_heap (oldheap);
2984 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2988 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2989 vl_api_registration_t * reg,
2990 u32 sw_if_index, u32 context)
2992 vl_api_acl_interface_etype_whitelist_details_t *mp;
2999 u16 *whitelist_in = 0;
3000 u16 *whitelist_out = 0;
3002 if (intf_has_etype_whitelist (am, sw_if_index, 0))
3004 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
3006 if (intf_has_etype_whitelist (am, sw_if_index, 1))
3008 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
3010 if ((0 == whitelist_in) && (0 == whitelist_out))
3011 return; /* nothing to do */
3013 void *oldheap = acl_set_heap (am);
3015 n_input = vec_len (whitelist_in);
3016 n_output = vec_len (whitelist_out);
3017 count = n_input + n_output;
3019 msg_size = sizeof (*mp);
3020 msg_size += sizeof (mp->whitelist[0]) * count;
3022 mp = vl_msg_api_alloc (msg_size);
3023 memset (mp, 0, msg_size);
3025 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
3027 /* fill in the message */
3028 mp->context = context;
3029 mp->sw_if_index = htonl (sw_if_index);
3031 mp->n_input = n_input;
3032 for (i = 0; i < n_input; i++)
3034 mp->whitelist[i] = htons (whitelist_in[i]);
3036 for (i = 0; i < n_output; i++)
3038 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
3040 clib_mem_set_heap (oldheap);
3041 vl_api_send_msg (reg, (u8 *) mp);
3046 vl_api_acl_interface_etype_whitelist_dump_t_handler
3047 (vl_api_acl_interface_list_dump_t * mp)
3049 acl_main_t *am = &acl_main;
3050 vnet_sw_interface_t *swif;
3051 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3054 vl_api_registration_t *reg;
3056 reg = vl_api_client_index_to_registration (mp->client_index);
3060 if (mp->sw_if_index == ~0)
3063 pool_foreach (swif, im->sw_interfaces,
3065 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
3071 sw_if_index = ntohl (mp->sw_if_index);
3072 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
3073 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
3080 /* Set up the API message handling tables */
3081 static clib_error_t *
3082 acl_plugin_api_hookup (vlib_main_t * vm)
3084 acl_main_t *am = &acl_main;
3086 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
3088 vl_api_##n##_t_handler, \
3090 vl_api_##n##_t_endian, \
3091 vl_api_##n##_t_print, \
3092 sizeof(vl_api_##n##_t), 1);
3093 foreach_acl_plugin_api_msg;
3099 #define vl_msg_name_crc_list
3100 #include <acl/acl_all_api_h.h>
3101 #undef vl_msg_name_crc_list
3104 setup_message_id_table (acl_main_t * am, api_main_t * apim)
3106 #define _(id,n,crc) \
3107 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
3108 foreach_vl_msg_name_crc_acl;
3113 acl_setup_fa_nodes (void)
3115 vlib_main_t *vm = vlib_get_main ();
3116 acl_main_t *am = &acl_main;
3117 vlib_node_t *n, *n4, *n6;
3119 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
3120 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
3121 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
3124 am->l2_input_classify_next_acl_ip4 =
3125 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3126 am->l2_input_classify_next_acl_ip6 =
3127 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3129 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
3130 l2input_get_feat_names (),
3131 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
3133 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
3134 l2input_get_feat_names (),
3135 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
3138 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
3139 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
3140 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
3142 am->l2_output_classify_next_acl_ip4 =
3143 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3144 am->l2_output_classify_next_acl_ip6 =
3145 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3147 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
3148 l2output_get_feat_names (),
3149 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
3151 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
3152 l2output_get_feat_names (),
3153 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
3157 acl_set_timeout_sec (int timeout_type, u32 value)
3159 acl_main_t *am = &acl_main;
3160 clib_time_t *ct = &am->vlib_main->clib_time;
3162 if (timeout_type < ACL_N_TIMEOUTS)
3164 am->session_timeout_sec[timeout_type] = value;
3168 clib_warning ("Unknown timeout type %d", timeout_type);
3171 am->session_timeout[timeout_type] =
3172 (u64) (((f64) value) / ct->seconds_per_clock);
3176 acl_set_session_max_entries (u32 value)
3178 acl_main_t *am = &acl_main;
3179 am->fa_conn_table_max_entries = value;
3183 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3185 acl_main_t *am = &acl_main;
3187 if ((eh < 256) && (value < 2))
3189 am->fa_ipv6_known_eh_bitmap =
3190 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3198 static clib_error_t *
3199 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3201 acl_main_t *am = &acl_main;
3202 if (0 == am->acl_mheap)
3204 /* ACL heap is not initialized, so definitely nothing to do. */
3209 int may_clear_sessions = 1;
3210 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3211 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3213 /* also unapply any ACLs in case the users did not do so. */
3214 macip_acl_interface_del_acl (am, sw_if_index);
3215 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
3216 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
3221 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3225 static clib_error_t *
3226 acl_set_aclplugin_fn (vlib_main_t * vm,
3227 unformat_input_t * input, vlib_cli_command_t * cmd)
3229 clib_error_t *error = 0;
3233 uword memory_size = 0;
3234 acl_main_t *am = &acl_main;
3236 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3238 if (!acl_set_skip_ipv6_eh (eh_val, val))
3240 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3244 if (unformat (input, "use-hash-acl-matching %u", &val))
3246 am->use_hash_acl_matching = (val != 0);
3249 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3251 am->l4_match_nonfirst_fragment = (val != 0);
3254 if (unformat (input, "reclassify-sessions %u", &val))
3256 am->reclassify_sessions = (val != 0);
3259 if (unformat (input, "event-trace"))
3261 if (!unformat (input, "%u", &val))
3263 error = clib_error_return (0,
3264 "expecting trace level, got `%U`",
3265 format_unformat_error, input);
3270 am->trace_acl = val;
3274 if (unformat (input, "heap"))
3276 if (unformat (input, "main"))
3278 if (unformat (input, "validate %u", &val))
3279 acl_plugin_acl_set_validate_heap (am, val);
3280 else if (unformat (input, "trace %u", &val))
3281 acl_plugin_acl_set_trace_heap (am, val);
3284 else if (unformat (input, "hash"))
3286 if (unformat (input, "validate %u", &val))
3287 acl_plugin_hash_acl_set_validate_heap (val);
3288 else if (unformat (input, "trace %u", &val))
3289 acl_plugin_hash_acl_set_trace_heap (val);
3294 if (unformat (input, "session"))
3296 if (unformat (input, "table"))
3298 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3299 if (unformat (input, "max-entries"))
3301 if (!unformat (input, "%u", &val))
3303 error = clib_error_return (0,
3304 "expecting maximum number of entries, got `%U`",
3305 format_unformat_error, input);
3310 acl_set_session_max_entries (val);
3314 if (unformat (input, "hash-table-buckets"))
3316 if (!unformat (input, "%u", &val))
3318 error = clib_error_return (0,
3319 "expecting maximum number of hash table buckets, got `%U`",
3320 format_unformat_error, input);
3325 am->fa_conn_table_hash_num_buckets = val;
3329 if (unformat (input, "hash-table-memory"))
3331 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3333 error = clib_error_return (0,
3334 "expecting maximum amount of hash table memory, got `%U`",
3335 format_unformat_error, input);
3340 am->fa_conn_table_hash_memory_size = memory_size;
3344 if (unformat (input, "event-trace"))
3346 if (!unformat (input, "%u", &val))
3348 error = clib_error_return (0,
3349 "expecting trace level, got `%U`",
3350 format_unformat_error, input);
3355 am->trace_sessions = val;
3361 if (unformat (input, "timeout"))
3363 if (unformat (input, "udp"))
3365 if (unformat (input, "idle"))
3367 if (!unformat (input, "%u", &timeout))
3369 error = clib_error_return (0,
3370 "expecting timeout value in seconds, got `%U`",
3371 format_unformat_error,
3377 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3382 if (unformat (input, "tcp"))
3384 if (unformat (input, "idle"))
3386 if (!unformat (input, "%u", &timeout))
3388 error = clib_error_return (0,
3389 "expecting timeout value in seconds, got `%U`",
3390 format_unformat_error,
3396 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3400 if (unformat (input, "transient"))
3402 if (!unformat (input, "%u", &timeout))
3404 error = clib_error_return (0,
3405 "expecting timeout value in seconds, got `%U`",
3406 format_unformat_error,
3412 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3426 my_format_mac_address (u8 * s, va_list * args)
3428 u8 *a = va_arg (*args, u8 *);
3429 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3430 a[0], a[1], a[2], a[3], a[4], a[5]);
3434 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3436 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3438 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3439 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3440 format_ip46_address, &a->src_ip_addr,
3441 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3443 my_format_mac_address, a->src_mac,
3444 my_format_mac_address, a->src_mac_mask);
3449 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3451 vlib_main_t *vm = am->vlib_main;
3454 /* Don't try to print someone else's memory */
3455 if (macip_acl_index > vec_len (am->macip_acls))
3458 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3459 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3461 vlib_cli_output (vm,
3462 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3463 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3465 vlib_cli_output (vm,
3466 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3467 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3468 vlib_cli_output (vm,
3469 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3470 a->out_ip4_table_index, a->out_ip6_table_index,
3471 a->out_l2_table_index);
3472 for (i = 0; i < vec_len (a->rules); i++)
3473 vlib_cli_output (vm, " rule %d: %U\n", i,
3474 my_macip_acl_rule_t_pretty_format,
3475 vec_elt_at_index (a->rules, i));
3479 static clib_error_t *
3480 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3482 input, vlib_cli_command_t * cmd)
3484 clib_error_t *error = 0;
3485 acl_main_t *am = &acl_main;
3489 (void) unformat (input, "index %u", &acl_index);
3491 for (i = 0; i < vec_len (am->macip_acls); i++)
3493 /* Don't attempt to show the ACLs that do not exist */
3494 if (pool_is_free_index (am->macip_acls, i))
3497 if ((acl_index != ~0) && (acl_index != i))
3502 macip_acl_print (am, i);
3503 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3505 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3507 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3515 static clib_error_t *
3516 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3518 input, vlib_cli_command_t * cmd)
3520 clib_error_t *error = 0;
3521 acl_main_t *am = &acl_main;
3523 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3525 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3526 vec_elt (am->macip_acl_by_sw_if_index, i));
3532 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3535 vlib_main_t *vm = am->vlib_main;
3537 for (i = 0; i < vec_len (am->acls); i++)
3539 if (acl_is_not_defined (am, i))
3541 /* don't attempt to show the ACLs that do not exist */
3544 if ((acl_index != ~0) && (acl_index != i))
3548 acl_print_acl (vm, am, i);
3550 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3552 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3553 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3556 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3558 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3559 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3562 if (i < vec_len (am->lc_index_vec_by_acl))
3564 vlib_cli_output (vm, " used in lookup context index: %U\n",
3565 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3570 static clib_error_t *
3571 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3572 unformat_input_t * input, vlib_cli_command_t * cmd)
3574 clib_error_t *error = 0;
3575 acl_main_t *am = &acl_main;
3578 (void) unformat (input, "index %u", &acl_index);
3580 acl_plugin_show_acl (am, acl_index);
3584 static clib_error_t *
3585 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3586 unformat_input_t * input,
3587 vlib_cli_command_t * cmd)
3589 clib_error_t *error = 0;
3592 (void) unformat (input, "index %u", &lc_index);
3594 acl_plugin_show_lookup_context (lc_index);
3598 static clib_error_t *
3599 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3600 unformat_input_t * input,
3601 vlib_cli_command_t * cmd)
3603 clib_error_t *error = 0;
3606 (void) unformat (input, "index %u", &lc_index);
3608 acl_plugin_show_lookup_user (lc_index);
3614 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3617 vlib_main_t *vm = am->vlib_main;
3620 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3621 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3623 /* if we need a particular interface, skip all the others */
3624 if ((sw_if_index != ~0) && (sw_if_index != swi))
3627 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3628 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3629 vlib_cli_output (vm, " input policy epoch: %x\n",
3630 vec_elt (am->input_policy_epoch_by_sw_if_index,
3632 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3633 vlib_cli_output (vm, " output policy epoch: %x\n",
3634 vec_elt (am->output_policy_epoch_by_sw_if_index,
3638 if (intf_has_etype_whitelist (am, swi, 1))
3640 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3641 am->input_etype_whitelist_by_sw_if_index[swi],
3644 if (intf_has_etype_whitelist (am, swi, 0))
3646 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3647 am->output_etype_whitelist_by_sw_if_index[swi],
3651 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3652 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3654 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3655 am->input_acl_vec_by_sw_if_index[swi], "%d");
3658 vlib_cli_output (vm, "\n");
3659 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3661 acl_print_acl (vm, am, *pj);
3663 vlib_cli_output (vm, "\n");
3667 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3668 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3670 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3671 am->output_acl_vec_by_sw_if_index[swi], "%d");
3674 vlib_cli_output (vm, "\n");
3675 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3677 acl_print_acl (vm, am, *pj);
3679 vlib_cli_output (vm, "\n");
3682 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3684 vlib_cli_output (vm, " input lookup context index: %d",
3685 am->input_lc_index_by_sw_if_index[swi]);
3687 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3689 vlib_cli_output (vm, " output lookup context index: %d",
3690 am->output_lc_index_by_sw_if_index[swi]);
3697 static clib_error_t *
3698 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3699 unformat_input_t * input,
3700 vlib_cli_command_t * cmd)
3702 clib_error_t *error = 0;
3703 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3706 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3707 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3708 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3709 format_acl_plugin_5tuple, five_tuple);
3711 error = clib_error_return (0, "expecting 6 hex integers");
3716 static clib_error_t *
3717 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3719 input, vlib_cli_command_t * cmd)
3721 clib_error_t *error = 0;
3722 acl_main_t *am = &acl_main;
3724 u32 sw_if_index = ~0;
3725 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3726 int show_acl = unformat (input, "acl");
3727 int detail = unformat (input, "detail");
3729 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3733 static clib_error_t *
3734 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3735 unformat_input_t * input,
3736 vlib_cli_command_t * cmd)
3738 clib_error_t *error = 0;
3739 acl_main_t *am = &acl_main;
3741 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3744 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3748 vlib_cli_output (vm, " Not initialized\n");
3750 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3751 if (am->hash_lookup_mheap)
3753 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3757 vlib_cli_output (vm, " Not initialized\n");
3763 acl_plugin_show_sessions (acl_main_t * am,
3764 u32 show_session_thread_id,
3765 u32 show_session_session_index)
3767 vlib_main_t *vm = am->vlib_main;
3769 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3770 vnet_sw_interface_t *swif;
3771 u64 now = clib_cpu_time_now ();
3772 u64 clocks_per_second = am->vlib_main->clib_time.clocks_per_second;
3775 u64 n_adds = am->fa_session_total_adds;
3776 u64 n_dels = am->fa_session_total_dels;
3777 u64 n_deact = am->fa_session_total_deactivations;
3778 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3779 n_dels, n_adds - n_dels);
3780 vlib_cli_output (vm, "Sessions active: add %lu - deact %lu = %lu", n_adds,
3781 n_deact, n_adds - n_deact);
3782 vlib_cli_output (vm, "Sessions being purged: deact %lu - del %lu = %lu",
3783 n_deact, n_dels, n_deact - n_dels);
3785 vlib_cli_output (vm, "now: %lu clocks per second: %lu", now,
3787 vlib_cli_output (vm, "\n\nPer-thread data:");
3788 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3790 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3791 vlib_cli_output (vm, "Thread #%d:", wk);
3792 if (show_session_thread_id == wk
3793 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3795 vlib_cli_output (vm, " session index %u:",
3796 show_session_session_index);
3797 fa_session_t *sess =
3798 pw->fa_sessions_pool + show_session_session_index;
3799 u64 *m = (u64 *) & sess->info;
3800 vlib_cli_output (vm,
3801 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3802 m[0], m[1], m[2], m[3], m[4], m[5]);
3803 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3804 vlib_cli_output (vm, " tcp_flags_seen: %x",
3805 sess->tcp_flags_seen.as_u16);
3806 vlib_cli_output (vm, " last active time: %lu",
3807 sess->last_active_time);
3808 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3809 vlib_cli_output (vm, " link enqueue time: %lu",
3810 sess->link_enqueue_time);
3811 vlib_cli_output (vm, " link next index: %u",
3812 sess->link_next_idx);
3813 vlib_cli_output (vm, " link prev index: %u",
3814 sess->link_prev_idx);
3815 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3817 vlib_cli_output (vm, " connection add/del stats:", wk);
3818 pool_foreach (swif, im->sw_interfaces, (
3825 (pw->fa_session_adds_by_sw_if_index)
3827 pw->fa_session_adds_by_sw_if_index
3832 (pw->fa_session_dels_by_sw_if_index)
3834 pw->fa_session_dels_by_sw_if_index
3836 u64 n_epoch_changes =
3839 (pw->fa_session_epoch_change_by_sw_if_index)
3841 pw->fa_session_epoch_change_by_sw_if_index
3843 vlib_cli_output (vm,
3844 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3854 vlib_cli_output (vm, " connection timeout type lists:", wk);
3856 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3858 u32 head_session_index = pw->fa_conn_list_head[tt];
3859 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3860 head_session_index);
3861 if (~0 != head_session_index)
3863 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3864 vlib_cli_output (vm, " last active time: %lu",
3865 sess->last_active_time);
3866 vlib_cli_output (vm, " link enqueue time: %lu",
3867 sess->link_enqueue_time);
3871 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3872 vlib_cli_output (vm, " Requeue until time: %lu",
3873 pw->requeue_until_time);
3874 vlib_cli_output (vm, " Current time wait interval: %lu",
3875 pw->current_time_wait_interval);
3876 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3877 pw->cnt_deleted_sessions);
3878 vlib_cli_output (vm, " Delete already deleted: %lu",
3879 pw->cnt_already_deleted_sessions);
3880 vlib_cli_output (vm, " Session timers restarted: %lu",
3881 pw->cnt_session_timer_restarted);
3882 vlib_cli_output (vm, " Swipe until this time: %lu",
3883 pw->swipe_end_time);
3884 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3885 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3886 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3888 pw->pending_clear_sw_if_index_bitmap);
3889 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3890 vlib_cli_output (vm, " interrupt is pending: %d",
3891 pw->interrupt_is_pending);
3892 vlib_cli_output (vm, " interrupt is needed: %d",
3893 pw->interrupt_is_needed);
3894 vlib_cli_output (vm, " interrupt is unwanted: %d",
3895 pw->interrupt_is_unwanted);
3896 vlib_cli_output (vm, " interrupt generation: %d",
3897 pw->interrupt_generation);
3899 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3900 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3901 foreach_fa_cleaner_counter;
3903 vlib_cli_output (vm, "Interrupt generation: %d",
3904 am->fa_interrupt_generation);
3905 vlib_cli_output (vm,
3906 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3907 am->fa_min_deleted_sessions_per_interval,
3908 am->fa_max_deleted_sessions_per_interval,
3909 am->fa_cleaner_wait_time_increment * 1000.0,
3910 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3911 1000.0 / (f64) vm->clib_time.clocks_per_second);
3912 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3915 static clib_error_t *
3916 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3917 unformat_input_t * input,
3918 vlib_cli_command_t * cmd)
3920 clib_error_t *error = 0;
3921 acl_main_t *am = &acl_main;
3923 u32 show_bihash_verbose = 0;
3924 u32 show_session_thread_id = ~0;
3925 u32 show_session_session_index = ~0;
3926 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3927 &show_session_session_index);
3928 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3930 acl_plugin_show_sessions (am, show_session_thread_id,
3931 show_session_session_index);
3932 show_fa_sessions_hash (vm, show_bihash_verbose);
3936 static clib_error_t *
3937 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3938 unformat_input_t * input,
3939 vlib_cli_command_t * cmd)
3941 clib_error_t *error = 0;
3945 int show_acl_hash_info = 0;
3946 int show_applied_info = 0;
3947 int show_mask_type = 0;
3948 int show_bihash = 0;
3949 u32 show_bihash_verbose = 0;
3951 if (unformat (input, "acl"))
3953 show_acl_hash_info = 1;
3954 /* mask-type is handy to see as well right there */
3956 unformat (input, "index %u", &acl_index);
3958 else if (unformat (input, "applied"))
3960 show_applied_info = 1;
3961 unformat (input, "lc_index %u", &lc_index);
3963 else if (unformat (input, "mask"))
3967 else if (unformat (input, "hash"))
3970 unformat (input, "verbose %u", &show_bihash_verbose);
3974 (show_mask_type || show_acl_hash_info || show_applied_info
3977 /* if no qualifiers specified, show all */
3979 show_acl_hash_info = 1;
3980 show_applied_info = 1;
3984 acl_plugin_show_tables_mask_type ();
3985 if (show_acl_hash_info)
3986 acl_plugin_show_tables_acl_hash_info (acl_index);
3987 if (show_applied_info)
3988 acl_plugin_show_tables_applied_info (lc_index);
3990 acl_plugin_show_tables_bihash (show_bihash_verbose);
3995 static clib_error_t *
3996 acl_clear_aclplugin_fn (vlib_main_t * vm,
3997 unformat_input_t * input, vlib_cli_command_t * cmd)
3999 clib_error_t *error = 0;
4000 acl_main_t *am = &acl_main;
4001 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
4002 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
4007 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
4008 .path = "set acl-plugin",
4009 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
4010 .function = acl_set_aclplugin_fn,
4013 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
4014 .path = "show acl-plugin acl",
4015 .short_help = "show acl-plugin acl [index N]",
4016 .function = acl_show_aclplugin_acl_fn,
4019 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
4020 .path = "show acl-plugin lookup context",
4021 .short_help = "show acl-plugin lookup context [index N]",
4022 .function = acl_show_aclplugin_lookup_context_fn,
4025 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
4026 .path = "show acl-plugin lookup user",
4027 .short_help = "show acl-plugin lookup user [index N]",
4028 .function = acl_show_aclplugin_lookup_user_fn,
4031 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
4032 .path = "show acl-plugin decode 5tuple",
4033 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
4034 .function = acl_show_aclplugin_decode_5tuple_fn,
4037 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
4038 .path = "show acl-plugin interface",
4039 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
4040 .function = acl_show_aclplugin_interface_fn,
4043 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
4044 .path = "show acl-plugin memory",
4045 .short_help = "show acl-plugin memory",
4046 .function = acl_show_aclplugin_memory_fn,
4049 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
4050 .path = "show acl-plugin sessions",
4051 .short_help = "show acl-plugin sessions",
4052 .function = acl_show_aclplugin_sessions_fn,
4055 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
4056 .path = "show acl-plugin tables",
4057 .short_help = "show acl-plugin tables [ acl [index N] | applied [ lc_index N ] | mask | hash [verbose N] ]",
4058 .function = acl_show_aclplugin_tables_fn,
4061 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
4062 .path = "show acl-plugin macip acl",
4063 .short_help = "show acl-plugin macip acl [index N]",
4064 .function = acl_show_aclplugin_macip_acl_fn,
4067 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
4068 .path = "show acl-plugin macip interface",
4069 .short_help = "show acl-plugin macip interface",
4070 .function = acl_show_aclplugin_macip_interface_fn,
4073 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
4074 .path = "clear acl-plugin sessions",
4075 .short_help = "clear acl-plugin sessions",
4076 .function = acl_clear_aclplugin_fn,
4080 static clib_error_t *
4081 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
4083 acl_main_t *am = &acl_main;
4084 u32 conn_table_hash_buckets;
4085 u32 conn_table_hash_memory_size;
4086 u32 conn_table_max_entries;
4087 uword main_heap_size;
4088 uword hash_heap_size;
4089 u32 hash_lookup_hash_buckets;
4090 u32 hash_lookup_hash_memory;
4091 u32 reclassify_sessions;
4092 u32 use_tuple_merge;
4093 u32 tuple_merge_split_threshold;
4095 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4098 (input, "connection hash buckets %d", &conn_table_hash_buckets))
4099 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
4100 else if (unformat (input, "connection hash memory %d",
4101 &conn_table_hash_memory_size))
4102 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
4103 else if (unformat (input, "connection count max %d",
4104 &conn_table_max_entries))
4105 am->fa_conn_table_max_entries = conn_table_max_entries;
4108 (input, "main heap size %U", unformat_memory_size,
4110 am->acl_mheap_size = main_heap_size;
4113 (input, "hash lookup heap size %U", unformat_memory_size,
4115 am->hash_lookup_mheap_size = hash_heap_size;
4116 else if (unformat (input, "hash lookup hash buckets %d",
4117 &hash_lookup_hash_buckets))
4118 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
4119 else if (unformat (input, "hash lookup hash memory %d",
4120 &hash_lookup_hash_memory))
4121 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4122 else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
4123 am->use_tuple_merge = use_tuple_merge;
4126 (input, "tuple merge split threshold %d",
4127 &tuple_merge_split_threshold))
4128 am->tuple_merge_split_threshold = tuple_merge_split_threshold;
4130 else if (unformat (input, "reclassify sessions %d",
4131 &reclassify_sessions))
4132 am->reclassify_sessions = reclassify_sessions;
4135 return clib_error_return (0, "unknown input '%U'",
4136 format_unformat_error, input);
4141 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4143 static clib_error_t *
4144 acl_init (vlib_main_t * vm)
4146 acl_main_t *am = &acl_main;
4147 clib_error_t *error = 0;
4148 memset (am, 0, sizeof (*am));
4150 am->vnet_main = vnet_get_main ();
4151 am->log_default = vlib_log_register_class ("acl_plugin", 0);
4153 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4155 /* Ask for a correctly-sized block of API message decode slots */
4156 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4157 VL_MSG_FIRST_AVAILABLE);
4159 error = acl_plugin_api_hookup (vm);
4161 /* Add our API messages to the global name_crc hash table */
4162 setup_message_id_table (am, &api_main);
4169 error = acl_plugin_exports_init (&acl_plugin);
4174 acl_setup_fa_nodes ();
4176 am->acl_mheap_size = 0; /* auto size when initializing */
4177 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4179 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4180 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4182 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4183 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4184 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4185 TCP_SESSION_IDLE_TIMEOUT_SEC;
4186 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4187 UDP_SESSION_IDLE_TIMEOUT_SEC;
4189 am->fa_conn_table_hash_num_buckets =
4190 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4191 am->fa_conn_table_hash_memory_size =
4192 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4193 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4194 am->reclassify_sessions = 0;
4195 vlib_thread_main_t *tm = vlib_get_thread_main ();
4197 am->fa_min_deleted_sessions_per_interval =
4198 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4199 am->fa_max_deleted_sessions_per_interval =
4200 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4201 am->fa_cleaner_wait_time_increment =
4202 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4204 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4207 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4209 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4210 vec_validate (pw->expired,
4212 am->fa_max_deleted_sessions_per_interval);
4213 _vec_len (pw->expired) = 0;
4214 vec_validate_init_empty (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1,
4215 FA_SESSION_BOGUS_INDEX);
4216 vec_validate_init_empty (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1,
4217 FA_SESSION_BOGUS_INDEX);
4218 vec_validate_init_empty (pw->fa_conn_list_head_expiry_time,
4219 ACL_N_TIMEOUTS - 1, ~0ULL);
4223 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4224 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4225 am->fa_cleaner_cnt_unknown_event = 0;
4226 am->fa_cleaner_cnt_timer_restarted = 0;
4227 am->fa_cleaner_cnt_wait_with_timeout = 0;
4230 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4233 am->l4_match_nonfirst_fragment = 1;
4235 /* use the new fancy hash-based matching */
4236 am->use_hash_acl_matching = 1;
4237 /* use tuplemerge by default */
4238 am->use_tuple_merge = 1;
4239 /* Set the default threshold */
4240 am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
4242 am->interface_acl_user_id = ~0; /* defer till the first use */
4247 VLIB_INIT_FUNCTION (acl_init);
4251 * fd.io coding-style-patch-verification: ON
4254 * eval: (c-set-style "gnu")