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>
61 /* List of message types that this plugin understands */
63 #define foreach_acl_plugin_api_msg \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace) \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
70 _(ACL_DUMP, acl_dump) \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
79 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
80 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
84 VLIB_PLUGIN_REGISTER () = {
85 .version = VPP_BUILD_VER,
86 .description = "Access Control Lists",
92 format_vec16 (u8 * s, va_list * va)
94 u16 *v = va_arg (*va, u16 *);
95 char *fmt = va_arg (*va, char *);
97 for (i = 0; i < vec_len (v); i++)
100 s = format (s, ", ");
101 s = format (s, fmt, v[i]);
109 acl_plugin_acl_exists (u32 acl_index)
111 acl_main_t *am = &acl_main;
113 if (pool_is_free_index (am->acls, acl_index))
120 acl_set_heap (acl_main_t * am)
122 if (0 == am->acl_mheap)
124 if (0 == am->acl_mheap_size)
126 vlib_thread_main_t *tm = vlib_get_thread_main ();
127 u64 per_worker_slack = 1000000LL;
128 u64 per_worker_size =
130 ((u64) am->fa_conn_table_max_entries) * sizeof (fa_session_t);
131 u64 per_worker_size_with_slack = per_worker_slack + per_worker_size;
132 u64 main_slack = 2000000LL;
133 u64 bihash_size = (u64) am->fa_conn_table_hash_memory_size;
136 per_worker_size_with_slack * tm->n_vlib_mains + bihash_size +
139 u64 max_possible = ((uword) ~ 0);
140 if (am->acl_mheap_size > max_possible)
142 clib_warning ("ACL heap size requested: %lld, max possible %lld",
143 am->acl_mheap_size, max_possible);
145 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
146 if (0 == am->acl_mheap)
149 ("ACL plugin failed to allocate main heap of %U bytes, abort",
150 format_memory_size, am->acl_mheap_size);
152 mheap_t *h = mheap_header (am->acl_mheap);
153 h->flags |= MHEAP_FLAG_THREAD_SAFE;
155 void *oldheap = clib_mem_set_heap (am->acl_mheap);
160 acl_plugin_set_heap ()
162 acl_main_t *am = &acl_main;
163 return acl_set_heap (am);
167 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
169 clib_mem_set_heap (acl_set_heap (am));
170 mheap_t *h = mheap_header (am->acl_mheap);
173 h->flags |= MHEAP_FLAG_VALIDATE;
174 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
179 h->flags &= ~MHEAP_FLAG_VALIDATE;
180 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
185 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
187 clib_mem_set_heap (acl_set_heap (am));
188 mheap_t *h = mheap_header (am->acl_mheap);
191 h->flags |= MHEAP_FLAG_TRACE;
195 h->flags &= ~MHEAP_FLAG_TRACE;
200 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
202 acl_main_t *am = &acl_main;
203 vl_api_acl_plugin_get_version_reply_t *rmp;
204 int msg_size = sizeof (*rmp);
205 vl_api_registration_t *reg;
207 reg = vl_api_client_index_to_registration (mp->client_index);
211 rmp = vl_msg_api_alloc (msg_size);
212 memset (rmp, 0, msg_size);
214 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
215 rmp->context = mp->context;
216 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
217 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
219 vl_api_send_msg (reg, (u8 *) rmp);
223 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
226 vl_api_acl_plugin_control_ping_reply_t *rmp;
227 acl_main_t *am = &acl_main;
231 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
233 rmp->vpe_pid = ntohl (getpid ());
239 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
241 clib_warning ("%v", out0);
242 vec_reset_length (out0);
246 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
248 vlib_cli_output (vm, "%v", out0);
249 vec_reset_length (out0);
252 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
255 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
256 acl_main_t * am, int acl_index)
259 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
260 am->acls[acl_index].count, am->acls[acl_index].tag);
263 for (j = 0; j < am->acls[acl_index].count; j++)
265 r = &am->acls[acl_index].rules[j];
266 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
267 out0 = format_acl_action (out0, r->is_permit);
268 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
269 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
272 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
273 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
274 out0 = format (out0, " proto %d", r->proto);
275 out0 = format (out0, " sport %d", r->src_port_or_type_first);
276 if (r->src_port_or_type_first != r->src_port_or_type_last)
278 out0 = format (out0, "-%d", r->src_port_or_type_last);
280 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
281 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
283 out0 = format (out0, "-%d", r->dst_port_or_code_last);
285 if (r->tcp_flags_mask || r->tcp_flags_value)
288 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
291 out0 = format (out0, "\n");
297 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
299 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
303 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
305 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
309 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
312 u32 **ppolicy_epoch_by_swi =
313 is_input ? &am->input_policy_epoch_by_sw_if_index :
314 &am->output_policy_epoch_by_sw_if_index;
315 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
317 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
319 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
320 (is_input * FA_POLICY_EPOCH_IS_INPUT);
324 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
326 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
327 : &am->output_sw_if_index_vec_by_acl;
328 if (acl_num < vec_len (*p_swi_vec_by_acl))
331 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
333 increment_policy_epoch (am, *p_swi, is_input);
340 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
342 try_increment_acl_policy_epoch (am, acl_num, 0);
343 try_increment_acl_policy_epoch (am, acl_num, 1);
349 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
350 u32 * acl_list_index, u8 * tag)
352 acl_main_t *am = &acl_main;
355 acl_rule_t *acl_new_rules = 0;
358 if (am->trace_acl > 255)
359 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
362 if (*acl_list_index != ~0)
364 /* They supplied some number, let's see if this ACL exists */
365 if (pool_is_free_index (am->acls, *acl_list_index))
367 /* tried to replace a non-existent ACL, no point doing anything */
369 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
370 *acl_list_index, tag);
371 return VNET_API_ERROR_NO_SUCH_ENTRY;
377 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
378 *acl_list_index, tag);
381 void *oldheap = acl_set_heap (am);
383 /* Create and populate the rules */
385 vec_validate (acl_new_rules, count - 1);
387 for (i = 0; i < count; i++)
389 r = vec_elt_at_index (acl_new_rules, i);
390 memset (r, 0, sizeof (*r));
391 r->is_permit = rules[i].is_permit;
392 r->is_ipv6 = rules[i].is_ipv6;
395 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
396 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
400 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
401 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
403 r->src_prefixlen = rules[i].src_ip_prefix_len;
404 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
405 r->proto = rules[i].proto;
406 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
407 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
408 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
409 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
410 r->tcp_flags_value = rules[i].tcp_flags_value;
411 r->tcp_flags_mask = rules[i].tcp_flags_mask;
414 if (~0 == *acl_list_index)
417 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
418 memset (a, 0, sizeof (*a));
419 /* Will return the newly allocated ACL index */
420 *acl_list_index = a - am->acls;
424 a = am->acls + *acl_list_index;
425 /* Get rid of the old rules */
429 a->rules = acl_new_rules;
431 memcpy (a->tag, tag, sizeof (a->tag));
432 if (am->trace_acl > 255)
433 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
434 if (am->reclassify_sessions)
436 /* a change in an ACLs if they are applied may mean a new policy epoch */
437 policy_notify_acl_change (am, *acl_list_index);
440 /* notify the lookup contexts about the ACL changes */
441 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
442 clib_mem_set_heap (oldheap);
447 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
449 if (acl_index < vec_len (foo_index_vec_by_acl))
451 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
453 /* ACL is applied somewhere. */
461 acl_del_list (u32 acl_list_index)
463 acl_main_t *am = &acl_main;
465 if (pool_is_free_index (am->acls, acl_list_index))
467 return VNET_API_ERROR_NO_SUCH_ENTRY;
469 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
470 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
471 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
472 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
473 /* lookup contexts cover other cases, not just inbound/oubound, so check that */
474 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
475 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
477 void *oldheap = acl_set_heap (am);
479 /* now we can delete the ACL itself */
480 a = pool_elt_at_index (am->acls, acl_list_index);
483 pool_put (am->acls, a);
484 /* acl_list_index is now free, notify the lookup contexts */
485 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
486 clib_mem_set_heap (oldheap);
490 /* Some aids in ASCII graphing the content */
497 u8 ip4_5tuple_mask[] =
498 _(" dmac smac etype ")
499 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
506 _(" ttl pr checksum ")
515 _("L4 T/U sport dport ")
525 u8 ip6_5tuple_mask[] =
526 _(" dmac smac etype ")
527 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
529 _(0x0000) __ __ __ __
531 _(0x0004) __ __ XX __
533 _(0x0008) XX XX XX XX
534 _(0x000C) XX XX XX XX
535 _(0x0010) XX XX XX XX
536 _(0x0014) XX XX XX XX
538 _(0x0018) XX XX XX XX
539 _(0x001C) XX XX XX XX
540 _(0x0020) XX XX XX XX
541 _(0x0024) XX XX XX XX
542 _("L4T/U sport dport ")
543 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
545 u8 dot1q_5tuple_mask[] =
546 _(" dmac smac dot1q etype ")
547 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
548 _(padpad) __ __ __ __
549 _(padpad) __ __ __ __
550 _(padpad) __ __ __ __
553 u8 dot1ad_5tuple_mask[] =
554 _(" dmac smac dot1ad dot1q etype ")
555 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
556 _(padpad) __ __ __ __
557 _(padpad) __ __ __ __
560 u8 ethertype_mask[] =
561 _(" dmac smac etype ")
562 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
571 count_skip (u8 * p, u32 size)
573 u64 *p64 = (u64 *) p;
574 /* Be tolerant to null pointer */
578 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
582 return (p64 - (u64 *) p) / 2;
586 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
587 u32 mask_len, u32 next_table_index,
588 u32 miss_next_index, u32 * table_index,
592 u32 memory_size = 2 << 13;
593 u32 skip = count_skip (mask, mask_len);
594 u32 match = (mask_len / 16) - skip;
595 u8 *skip_mask_ptr = mask + 16 * skip;
596 u32 current_data_flag = 0;
597 int current_data_offset = 0;
601 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
602 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
603 memory_size, skip, match,
604 next_table_index, miss_next_index,
605 table_index, current_data_flag,
606 current_data_offset, is_add,
607 1 /* delete_chain */ );
608 clib_mem_set_heap (oldheap);
613 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
614 u32 mask_len, u32 next_table_index,
615 u32 miss_next_index, u32 * table_index,
619 u32 memory_size = 2 << 22;
620 u32 skip = count_skip (mask, mask_len);
621 u32 match = (mask_len / 16) - skip;
622 u8 *skip_mask_ptr = mask + 16 * skip;
623 u32 current_data_flag = 0;
624 int current_data_offset = 0;
629 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
630 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
631 memory_size, skip, match,
632 next_table_index, miss_next_index,
633 table_index, current_data_flag,
634 current_data_offset, is_add,
635 1 /* delete_chain */ );
636 clib_mem_set_heap (oldheap);
641 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
643 vnet_classify_main_t *cm = &vnet_classify_main;
644 u32 ip4_table_index = ~0;
645 u32 ip6_table_index = ~0;
646 u32 dot1q_table_index = ~0;
647 u32 dot1ad_table_index = ~0;
648 u32 etype_table_index = ~0;
649 void *oldheap = acl_set_heap (am);
651 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
653 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
655 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
657 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
659 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
662 /* switch to global heap while calling vnet_* functions */
663 clib_mem_set_heap (cm->vlib_main->heap_base);
664 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
666 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
669 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
670 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
671 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
672 sizeof (ip4_5tuple_mask) - 1, ~0,
673 am->l2_input_classify_next_acl_ip4,
674 &ip4_table_index, 0);
676 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
679 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
680 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
681 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
682 sizeof (ip6_5tuple_mask) - 1, ~0,
683 am->l2_input_classify_next_acl_ip6,
684 &ip6_table_index, 0);
686 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
689 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
690 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
691 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
692 sizeof (ip6_5tuple_mask) - 1, ~0,
693 ~0, &dot1q_table_index, 0);
695 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
698 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
699 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
700 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
701 sizeof (dot1ad_5tuple_mask) - 1, ~0,
702 ~0, &dot1ad_table_index, 0);
704 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
707 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
708 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
709 acl_classify_add_del_table_tiny (cm, ethertype_mask,
710 sizeof (ethertype_mask) - 1, ~0,
711 ~0, &etype_table_index, 0);
713 clib_mem_set_heap (oldheap);
718 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
720 vnet_classify_main_t *cm = &vnet_classify_main;
721 u32 ip4_table_index = ~0;
722 u32 ip6_table_index = ~0;
723 u32 dot1q_table_index = ~0;
724 u32 dot1ad_table_index = ~0;
725 u32 etype_table_index = ~0;
726 void *oldheap = acl_set_heap (am);
728 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
730 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
732 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
734 vec_validate_init_empty
735 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
736 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
739 /* switch to global heap while calling vnet_* functions */
740 clib_mem_set_heap (cm->vlib_main->heap_base);
742 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
744 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
747 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
748 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
749 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
750 sizeof (ip4_5tuple_mask) - 1, ~0,
751 am->l2_output_classify_next_acl_ip4,
752 &ip4_table_index, 0);
754 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
757 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
758 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
759 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
760 sizeof (ip6_5tuple_mask) - 1, ~0,
761 am->l2_output_classify_next_acl_ip6,
762 &ip6_table_index, 0);
764 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
767 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
768 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
769 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
770 sizeof (ip6_5tuple_mask) - 1, ~0,
771 ~0, &dot1q_table_index, 0);
773 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
776 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
777 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
778 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
779 sizeof (dot1ad_5tuple_mask) - 1, ~0,
780 ~0, &dot1ad_table_index, 0);
782 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
785 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
786 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
787 acl_classify_add_del_table_tiny (cm, ethertype_mask,
788 sizeof (ethertype_mask) - 1, ~0,
789 ~0, &etype_table_index, 0);
791 clib_mem_set_heap (oldheap);
796 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
797 u8 is_dot1ad, u8 is_ip6)
799 vnet_classify_main_t *cm = &vnet_classify_main;
809 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
815 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
817 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
818 idx = (is_dot1ad) ? 20 : 16;
821 /* 802.1ad ethertype */
824 /* 802.1q ethertype */
830 /* 802.1q ethertype */
835 /* add sessions to vlan tables per ethernet_type */
839 match[idx + 1] = 0xdd;
845 match[idx + 1] = 0x00;
848 vnet_classify_add_del_session (cm, table_index, match, next_acl,
849 session_idx, 0, 0, 0, 1);
850 /* reset the mask back to being a mask */
852 match[idx + 1] = 0xff;
863 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
866 ? am->input_etype_whitelist_by_sw_if_index
867 : am->output_etype_whitelist_by_sw_if_index;
868 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
872 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
873 u32 etype_table_index)
875 vnet_classify_main_t *cm = &vnet_classify_main;
877 ? am->input_etype_whitelist_by_sw_if_index
878 : am->output_etype_whitelist_by_sw_if_index;
879 u8 *match = ethertype_mask;
883 u16 *whitelist = vec_elt (v, sw_if_index);
884 u32 next = ~0; /* permit */
885 for (i = 0; i < vec_len (whitelist); i++)
888 match[12] = (whitelist[i] >> 8) & 0xff;
889 match[13] = whitelist[i] & 0xff;
891 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
892 whitelist[i], 0, 0, 0, 1);
895 /* restore the mask */
902 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
904 vnet_classify_main_t *cm = &vnet_classify_main;
905 u32 ip4_table_index = ~0;
906 u32 ip6_table_index = ~0;
907 u32 dot1q_table_index = ~0;
908 u32 dot1ad_table_index = ~0;
909 u32 etype_table_index = ~0;
912 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
914 /* in case there were previous tables attached */
915 acl_unhook_l2_input_classify (am, sw_if_index);
917 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
918 sizeof (ip4_5tuple_mask) - 1, ~0,
919 am->l2_input_classify_next_acl_ip4,
920 &ip4_table_index, 1);
925 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
926 sizeof (ip6_5tuple_mask) - 1, ~0,
927 am->l2_input_classify_next_acl_ip6,
928 &ip6_table_index, 1);
931 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
932 sizeof (ip4_5tuple_mask) - 1, ~0,
933 am->l2_input_classify_next_acl_ip4,
934 &ip4_table_index, 0);
938 if (intf_has_etype_whitelist (am, sw_if_index, 1))
940 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
941 &etype_table_index, 1);
942 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
946 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
947 sizeof (dot1ad_5tuple_mask) - 1,
948 etype_table_index, ~0,
949 &dot1ad_table_index, 1);
951 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
952 sizeof (dot1q_5tuple_mask) - 1,
953 dot1ad_table_index, ~0,
954 &dot1q_table_index, 1);
957 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
958 sizeof (dot1ad_5tuple_mask) - 1, ~0,
959 ~0, &dot1ad_table_index, 0);
960 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
961 sizeof (ip6_5tuple_mask) - 1, ~0,
962 am->l2_input_classify_next_acl_ip6,
963 &ip6_table_index, 0);
964 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
965 sizeof (ip4_5tuple_mask) - 1, ~0,
966 am->l2_input_classify_next_acl_ip4,
967 &ip4_table_index, 0);
972 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
973 ip6_table_index, dot1q_table_index);
977 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
978 sizeof (ip4_5tuple_mask) - 1, ~0,
979 am->l2_input_classify_next_acl_ip4,
980 &ip4_table_index, 0);
981 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
982 sizeof (ip6_5tuple_mask) - 1, ~0,
983 am->l2_input_classify_next_acl_ip6,
984 &ip6_table_index, 0);
985 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
986 sizeof (dot1q_5tuple_mask) - 1, ~0,
987 ~0, &dot1q_table_index, 0);
988 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
989 sizeof (dot1ad_5tuple_mask) - 1, ~0,
990 ~0, &dot1ad_table_index, 0);
994 /* add sessions to vlan tables per ethernet_type */
995 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
996 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
997 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
998 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
1000 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
1002 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
1004 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
1006 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1008 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
1010 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
1013 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
1015 clib_mem_set_heap (prevheap);
1020 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
1022 vnet_classify_main_t *cm = &vnet_classify_main;
1023 u32 ip4_table_index = ~0;
1024 u32 ip6_table_index = ~0;
1025 u32 dot1q_table_index = ~0;
1026 u32 dot1ad_table_index = ~0;
1027 u32 etype_table_index = ~0;
1030 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
1032 /* in case there were previous tables attached */
1033 acl_unhook_l2_output_classify (am, sw_if_index);
1035 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1036 sizeof (ip4_5tuple_mask) - 1, ~0,
1037 am->l2_output_classify_next_acl_ip4,
1038 &ip4_table_index, 1);
1042 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1043 sizeof (ip6_5tuple_mask) - 1, ~0,
1044 am->l2_output_classify_next_acl_ip6,
1045 &ip6_table_index, 1);
1048 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1049 sizeof (ip4_5tuple_mask) - 1, ~0,
1050 am->l2_output_classify_next_acl_ip4,
1051 &ip4_table_index, 0);
1055 if (intf_has_etype_whitelist (am, sw_if_index, 0))
1057 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
1058 &etype_table_index, 1);
1059 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
1064 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1065 sizeof (dot1ad_5tuple_mask) - 1,
1066 etype_table_index, ~0,
1067 &dot1ad_table_index, 1);
1069 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1070 sizeof (dot1q_5tuple_mask) - 1,
1071 dot1ad_table_index, ~0,
1072 &dot1q_table_index, 1);
1075 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1076 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1077 ~0, &dot1ad_table_index, 0);
1078 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1079 sizeof (ip6_5tuple_mask) - 1, ~0,
1080 am->l2_output_classify_next_acl_ip6,
1081 &ip6_table_index, 0);
1082 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1083 sizeof (ip4_5tuple_mask) - 1, ~0,
1084 am->l2_output_classify_next_acl_ip4,
1085 &ip4_table_index, 0);
1090 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1091 ip6_table_index, dot1q_table_index);
1094 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
1095 sw_if_index, ip4_table_index, ip6_table_index);
1099 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1100 sizeof (ip6_5tuple_mask) - 1, ~0,
1101 am->l2_output_classify_next_acl_ip6,
1102 &ip6_table_index, 0);
1103 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1104 sizeof (ip4_5tuple_mask) - 1, ~0,
1105 am->l2_output_classify_next_acl_ip4,
1106 &ip4_table_index, 0);
1107 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1108 sizeof (dot1q_5tuple_mask) - 1, ~0,
1109 ~0, &dot1q_table_index, 0);
1110 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1111 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1112 ~0, &dot1ad_table_index, 0);
1116 /* add sessions to vlan tables per ethernet_type */
1117 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
1118 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
1119 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
1120 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
1122 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
1124 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
1126 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
1128 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
1130 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
1133 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
1135 clib_mem_set_heap (prevheap);
1140 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
1142 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1143 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1144 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1146 clib_mem_set_heap (oldheap);
1151 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1156 /* Utterly wrong? */
1157 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1159 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1161 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
1165 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1169 rv = acl_hook_l2_input_classify (am, sw_if_index);
1173 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1176 am->in_acl_on_sw_if_index =
1177 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
1183 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1188 /* Utterly wrong? */
1189 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1191 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1193 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
1197 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1201 rv = acl_hook_l2_output_classify (am, sw_if_index);
1205 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1208 am->out_acl_on_sw_if_index =
1209 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
1215 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
1216 int is_input, int enable_disable)
1219 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
1221 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
1225 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1227 return (pool_is_free_index (am->acls, acl_list_index));
1231 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
1232 u8 is_input, u32 * vec_acl_list_index,
1233 int *may_clear_sessions)
1236 uword *seen_acl_bitmap = 0;
1237 uword *old_seen_acl_bitmap = 0;
1238 uword *change_acl_bitmap = 0;
1243 if (am->trace_acl > 255)
1245 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
1246 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
1248 vec_foreach (pacln, vec_acl_list_index)
1250 if (acl_is_not_defined (am, *pacln))
1252 /* ACL is not defined. Can not apply */
1253 clib_warning ("ERROR: ACL %d not defined", *pacln);
1254 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1257 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
1259 /* ACL being applied twice within the list. error. */
1260 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
1261 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1264 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
1268 u32 **pinout_lc_index_by_sw_if_index =
1270 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
1272 u32 ***pinout_acl_vec_by_sw_if_index =
1274 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1276 u32 ***pinout_sw_if_index_vec_by_acl =
1278 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
1280 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1282 clib_bitmap_validate (old_seen_acl_bitmap, 1);
1284 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
1286 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
1289 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
1291 if (am->trace_acl > 255)
1292 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
1293 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
1294 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
1297 clib_bitmap_foreach(acln, change_acl_bitmap, ({
1298 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
1299 /* ACL is being removed. */
1300 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
1301 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1302 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
1305 /* ACL is being added. */
1306 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
1307 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1312 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1313 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
1314 vec_dup (vec_acl_list_index);
1316 if (am->reclassify_sessions)
1318 /* re-applying ACLs means a new policy epoch */
1319 increment_policy_epoch (am, sw_if_index, is_input);
1323 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
1324 if (may_clear_sessions && *may_clear_sessions
1325 && !clib_bitmap_is_zero (change_acl_bitmap))
1327 acl_clear_sessions (am, sw_if_index);
1328 *may_clear_sessions = 0;
1333 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
1335 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
1337 if (vec_len (vec_acl_list_index) > 0)
1339 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
1342 if (~0 == am->interface_acl_user_id)
1343 am->interface_acl_user_id =
1344 acl_plugin_register_user_module ("interface ACL", "sw_if_index",
1347 acl_plugin_get_lookup_context_index (am->interface_acl_user_id,
1348 sw_if_index, is_input);
1349 ASSERT (lc_index >= 0);
1350 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
1352 acl_plugin_set_acl_vec_for_context (lc_index, vec_acl_list_index);
1356 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
1358 acl_plugin_put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
1359 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
1363 /* ensure ACL processing is enabled/disabled as needed */
1364 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
1365 vec_len (vec_acl_list_index) > 0);
1368 clib_bitmap_free (change_acl_bitmap);
1369 clib_bitmap_free (seen_acl_bitmap);
1370 clib_bitmap_free (old_seen_acl_bitmap);
1375 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
1376 int *may_clear_sessions)
1378 acl_main_t *am = &acl_main;
1379 void *oldheap = acl_set_heap (am);
1380 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
1381 may_clear_sessions);
1382 clib_mem_set_heap (oldheap);
1386 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1390 acl_main_t *am = &acl_main;
1392 int may_clear_sessions = 1;
1394 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
1395 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1397 u32 ***pinout_acl_vec_by_sw_if_index =
1399 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1401 void *oldheap = acl_set_heap (am);
1405 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1406 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1411 rv = error_already_applied;
1415 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1416 vec_add1 (acl_vec, acl_list_index);
1420 if (sw_if_index > vec_len (*pinout_acl_vec_by_sw_if_index))
1422 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1426 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1431 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1435 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1436 vec_del1 (acl_vec, index);
1439 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
1440 &may_clear_sessions);
1443 clib_mem_set_heap (oldheap);
1448 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1451 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1452 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1454 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1455 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1457 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1458 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1461 * if there are already inbound/outbound ACLs applied, toggle the
1462 * enable/disable - this will recreate the necessary tables.
1465 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1467 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1469 acl_interface_in_enable_disable (am, sw_if_index, 0);
1470 acl_interface_in_enable_disable (am, sw_if_index, 1);
1473 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1475 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1477 acl_interface_out_enable_disable (am, sw_if_index, 0);
1478 acl_interface_out_enable_disable (am, sw_if_index, 1);
1493 u32 arp_table_index;
1494 u32 dot1q_table_index;
1495 u32 dot1ad_table_index;
1496 u32 arp_dot1q_table_index;
1497 u32 arp_dot1ad_table_index;
1499 u32 out_table_index;
1500 u32 out_arp_table_index;
1501 u32 out_dot1q_table_index;
1502 u32 out_dot1ad_table_index;
1503 u32 out_arp_dot1q_table_index;
1504 u32 out_arp_dot1ad_table_index;
1505 } macip_match_type_t;
1508 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1514 for (i = 0; i < vec_len (mv); i++)
1516 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1517 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1527 /* Get metric used to sort match types.
1528 The more specific and the more often seen - the bigger the metric */
1530 match_type_metric (macip_match_type_t * m)
1532 unsigned int mac_bits_set = 0;
1533 unsigned int mac_byte;
1535 for (i = 0; i < 6; i++)
1537 mac_byte = m->mac_mask[i];
1538 for (; mac_byte; mac_byte >>= 1)
1539 mac_bits_set += mac_byte & 1;
1542 * Attempt to place the more specific and the more used rules on top.
1543 * There are obvious caveat corner cases to this, but they do not
1544 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1545 * going with a wildcard IPv4 with a specific MAC).
1547 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1551 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1553 /* Ascending sort based on the metric values */
1554 return match_type_metric (m1) - match_type_metric (m2);
1557 /* Get the offset of L3 source within ethernet packet */
1559 get_l3_src_offset (int is6)
1562 return (sizeof (ethernet_header_t) +
1563 offsetof (ip6_header_t, src_address));
1565 return (sizeof (ethernet_header_t) +
1566 offsetof (ip4_header_t, src_address));
1570 get_l3_dst_offset (int is6)
1573 return (sizeof (ethernet_header_t) +
1574 offsetof (ip6_header_t, dst_address));
1576 return (sizeof (ethernet_header_t) +
1577 offsetof (ip4_header_t, dst_address));
1581 * return if the is_permit value also requires to create the egress tables
1582 * For backwards compatibility, we keep the is_permit = 1 to only
1583 * create the ingress tables, and the new value of 3 will also
1584 * create the egress tables based on destination.
1587 macip_permit_also_egress (u8 is_permit)
1589 return (is_permit == 3);
1593 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1595 macip_match_type_t *mvec = NULL;
1596 macip_match_type_t *mt;
1597 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1599 u32 match_type_index;
1603 vnet_classify_main_t *cm = &vnet_classify_main;
1605 /* Count the number of different types of rules */
1606 for (i = 0; i < a->count; i++)
1610 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1611 a->rules[i].src_prefixlen,
1612 a->rules[i].is_ipv6)))
1614 match_type_index = vec_len (mvec);
1615 vec_validate (mvec, match_type_index);
1616 memcpy (mvec[match_type_index].mac_mask,
1617 a->rules[i].src_mac_mask, 6);
1618 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1619 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1620 mvec[match_type_index].has_egress = 0;
1621 mvec[match_type_index].table_index = ~0;
1622 mvec[match_type_index].arp_table_index = ~0;
1623 mvec[match_type_index].dot1q_table_index = ~0;
1624 mvec[match_type_index].dot1ad_table_index = ~0;
1625 mvec[match_type_index].arp_dot1q_table_index = ~0;
1626 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1627 mvec[match_type_index].out_table_index = ~0;
1628 mvec[match_type_index].out_arp_table_index = ~0;
1629 mvec[match_type_index].out_dot1q_table_index = ~0;
1630 mvec[match_type_index].out_dot1ad_table_index = ~0;
1631 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1632 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1634 mvec[match_type_index].count++;
1635 mvec[match_type_index].has_egress |=
1636 macip_permit_also_egress (a->rules[i].is_permit);
1638 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1639 vec_sort_with_function (mvec, match_type_compare);
1640 /* Create the classifier tables */
1642 out_last_table = ~0;
1643 /* First add ARP tables */
1644 vec_foreach (mt, mvec)
1647 int is6 = mt->is_ipv6;
1649 u32 *last_tag_table;
1650 u32 *out_last_tag_table;
1657 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
1658 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1659 | Destination Address |
1660 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1662 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1664 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1665 | EtherType | Hardware Type |
1666 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1667 | Protocol Type | Hw addr len | Proto addr len|
1668 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1670 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1671 | Sender Hardware Address |
1672 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1673 | Sender Protocol Address |
1674 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1675 | Target Hardware Address |
1676 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1677 | | TargetProtocolAddress |
1678 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1680 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1682 for (tags = 2; tags >= 0; tags--)
1684 memset (mask, 0, sizeof (mask));
1685 /* source MAC address */
1686 memcpy (&mask[6], mt->mac_mask, 6);
1692 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1694 last_tag_table = &mt->arp_table_index;
1697 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1698 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1700 last_tag_table = &mt->arp_dot1q_table_index;
1703 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1704 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1705 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1707 last_tag_table = &mt->arp_dot1ad_table_index;
1711 /* sender hardware address within ARP */
1712 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1713 /* sender protocol address within ARP */
1714 for (i = 0; i < (mt->prefix_len / 8); i++)
1715 mask[l3_offset + 14 + i] = 0xff;
1716 if (mt->prefix_len % 8)
1717 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1718 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1720 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1722 1)) / sizeof (u32x4)) * sizeof (u32x4);
1723 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1724 (~0 == last_table) ? 0 : ~0,
1726 last_table = *last_tag_table;
1729 /* egress ARP table */
1730 memset (mask, 0, sizeof (mask));
1736 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1738 out_last_tag_table = &mt->out_arp_table_index;
1741 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1742 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1744 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1747 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1748 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1749 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1751 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1755 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1756 /* mask captures just the destination and the ethertype */
1757 mask_len = ((l3_offset +
1759 1)) / sizeof (u32x4)) * sizeof (u32x4);
1760 acl_classify_add_del_table_small (cm, mask, mask_len,
1763 out_last_table) ? 0 : ~0,
1764 out_last_tag_table, 1);
1765 out_last_table = *out_last_tag_table;
1770 /* Now add IP[46] tables */
1771 vec_foreach (mt, mvec)
1774 int is6 = mt->is_ipv6;
1778 u32 *last_tag_table;
1779 u32 *out_last_tag_table;
1782 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1784 for (tags = 2; tags >= 0; tags--)
1786 memset (mask, 0, sizeof (mask));
1787 memcpy (&mask[6], mt->mac_mask, 6);
1788 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1793 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1794 last_tag_table = &mt->table_index;
1797 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1798 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1799 last_tag_table = &mt->dot1q_table_index;
1802 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1803 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1804 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1805 last_tag_table = &mt->dot1ad_table_index;
1808 for (i = 0; i < (mt->prefix_len / 8); i++)
1810 mask[l3_src_offs + i] = 0xff;
1812 if (mt->prefix_len % 8)
1814 mask[l3_src_offs + (mt->prefix_len / 8)] =
1815 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1818 * Round-up the number of bytes needed to store the prefix,
1819 * and round up the number of vectors too
1821 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1822 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1823 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1824 (~0 == last_table) ? 0 : ~0,
1826 last_table = *last_tag_table;
1830 for (tags = 2; tags >= 0; tags--)
1832 memset (mask, 0, sizeof (mask));
1833 /* MAC destination */
1834 memcpy (&mask[0], mt->mac_mask, 6);
1835 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1840 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1841 out_last_tag_table = &mt->out_table_index;
1844 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1845 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1846 out_last_tag_table = &mt->out_dot1q_table_index;
1849 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1850 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1851 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1852 out_last_tag_table = &mt->out_dot1ad_table_index;
1855 for (i = 0; i < (mt->prefix_len / 8); i++)
1857 mask[l3_dst_offs + i] = 0xff;
1859 if (mt->prefix_len % 8)
1861 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1862 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1865 * Round-up the number of bytes needed to store the prefix,
1866 * and round up the number of vectors too
1868 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1870 1)) / sizeof (u32x4)) * sizeof (u32x4);
1871 acl_classify_add_del_table_small (cm, mask, mask_len,
1873 (~0 == out_last_table) ? 0 : ~0,
1874 out_last_tag_table, 1);
1875 out_last_table = *out_last_tag_table;
1879 a->ip4_table_index = last_table;
1880 a->ip6_table_index = last_table;
1881 a->l2_table_index = last_table;
1883 a->out_ip4_table_index = out_last_table;
1884 a->out_ip6_table_index = out_last_table;
1885 a->out_l2_table_index = out_last_table;
1887 /* Populate the classifier tables with rules from the MACIP ACL */
1888 for (i = 0; i < a->count; i++)
1892 int is6 = a->rules[i].is_ipv6;
1899 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1900 a->rules[i].src_prefixlen,
1901 a->rules[i].is_ipv6);
1902 ASSERT (match_type_index != ~0);
1904 for (tags = 2; tags >= 0; tags--)
1906 memset (mask, 0, sizeof (mask));
1907 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1908 memcpy (&mask[6], a->rules[i].src_mac, 6);
1913 tag_table = mvec[match_type_index].table_index;
1917 tag_table = mvec[match_type_index].dot1q_table_index;
1923 tag_table = mvec[match_type_index].dot1ad_table_index;
1933 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1935 mask[eth + 1] = 0xdd;
1939 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1941 mask[eth + 1] = 0x00;
1944 /* add session to table mvec[match_type_index].table_index; */
1945 vnet_classify_add_del_session (cm, tag_table,
1946 mask, a->rules[i].is_permit ? ~0 : 0,
1947 i, 0, action, metadata, 1);
1948 memset (&mask[12], 0, sizeof (mask) - 12);
1951 /* add ARP table entry too */
1952 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1954 memset (mask, 0, sizeof (mask));
1955 memcpy (&mask[6], a->rules[i].src_mac, 6);
1957 for (tags = 2; tags >= 0; tags--)
1963 tag_table = mvec[match_type_index].arp_table_index;
1969 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1977 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1988 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1989 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1991 vnet_classify_add_del_session (cm, tag_table, mask,
1992 a->rules[i].is_permit ? ~0 : 0,
1993 i, 0, action, metadata, 1);
1996 if (macip_permit_also_egress (a->rules[i].is_permit))
1998 /* Add the egress entry with destination set */
1999 for (tags = 2; tags >= 0; tags--)
2001 memset (mask, 0, sizeof (mask));
2002 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
2003 /* src mac in the other direction becomes dst */
2004 memcpy (&mask[0], a->rules[i].src_mac, 6);
2009 tag_table = mvec[match_type_index].out_table_index;
2013 tag_table = mvec[match_type_index].out_dot1q_table_index;
2019 tag_table = mvec[match_type_index].out_dot1ad_table_index;
2029 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
2032 mask[eth + 1] = 0xdd;
2036 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
2039 mask[eth + 1] = 0x00;
2042 /* add session to table mvec[match_type_index].table_index; */
2043 vnet_classify_add_del_session (cm, tag_table,
2045 a->rules[i].is_permit ? ~0 : 0,
2046 i, 0, action, metadata, 1);
2047 // memset (&mask[12], 0, sizeof (mask) - 12);
2050 /* add ARP table entry too */
2051 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
2053 for (tags = 2; tags >= 0; tags--)
2055 memset (mask, 0, sizeof (mask));
2060 tag_table = mvec[match_type_index].out_arp_table_index;
2066 mvec[match_type_index].out_arp_dot1q_table_index;
2074 mvec[match_type_index].out_arp_dot1ad_table_index;
2084 vnet_classify_add_del_session (cm, tag_table,
2087 rules[i].is_permit ? ~0 : 0,
2088 i, 0, action, metadata, 1);
2097 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
2099 vnet_classify_main_t *cm = &vnet_classify_main;
2100 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2102 if (a->ip4_table_index != ~0)
2104 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2105 &a->ip4_table_index, 0);
2106 a->ip4_table_index = ~0;
2108 if (a->ip6_table_index != ~0)
2110 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2111 &a->ip6_table_index, 0);
2112 a->ip6_table_index = ~0;
2114 if (a->l2_table_index != ~0)
2116 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
2118 a->l2_table_index = ~0;
2120 if (a->out_ip4_table_index != ~0)
2122 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2123 &a->out_ip4_table_index, 0);
2124 a->out_ip4_table_index = ~0;
2126 if (a->out_ip6_table_index != ~0)
2128 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2129 &a->out_ip6_table_index, 0);
2130 a->out_ip6_table_index = ~0;
2132 if (a->out_l2_table_index != ~0)
2134 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2135 &a->out_l2_table_index, 0);
2136 a->out_l2_table_index = ~0;
2141 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2147 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2149 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2150 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2152 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2153 a->ip6_table_index, a->l2_table_index,
2155 /* return the first unhappy outcome but make try to plough through. */
2158 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2159 a->out_ip6_table_index,
2160 a->out_l2_table_index, is_apply);
2161 /* return the first unhappy outcome but make try to plough through. */
2168 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2169 u32 * acl_list_index, u8 * tag)
2171 acl_main_t *am = &acl_main;
2172 macip_acl_list_t *a;
2173 macip_acl_rule_t *r;
2174 macip_acl_rule_t *acl_new_rules = 0;
2178 if (*acl_list_index != ~0)
2180 /* They supplied some number, let's see if this MACIP ACL exists */
2181 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2183 /* tried to replace a non-existent ACL, no point doing anything */
2185 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2186 *acl_list_index, tag);
2187 return VNET_API_ERROR_NO_SUCH_ENTRY;
2194 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2197 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2198 if (~0 != *acl_list_index)
2199 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2200 void *oldheap = acl_set_heap (am);
2201 /* Create and populate the rules */
2203 vec_validate (acl_new_rules, count - 1);
2205 for (i = 0; i < count; i++)
2207 r = &acl_new_rules[i];
2208 r->is_permit = rules[i].is_permit;
2209 r->is_ipv6 = rules[i].is_ipv6;
2210 memcpy (&r->src_mac, rules[i].src_mac, 6);
2211 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2212 if (rules[i].is_ipv6)
2213 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2215 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2216 r->src_prefixlen = rules[i].src_ip_prefix_len;
2219 if (~0 == *acl_list_index)
2222 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2223 memset (a, 0, sizeof (*a));
2224 /* Will return the newly allocated ACL index */
2225 *acl_list_index = a - am->macip_acls;
2229 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2232 vec_free (a->rules);
2234 macip_destroy_classify_tables (am, *acl_list_index);
2237 a->rules = acl_new_rules;
2239 memcpy (a->tag, tag, sizeof (a->tag));
2241 /* Create and populate the classifer tables */
2242 macip_create_classify_tables (am, *acl_list_index);
2243 clib_mem_set_heap (oldheap);
2244 /* If the ACL was already applied somewhere, reapply the newly created tables */
2246 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2250 /* No check that sw_if_index denotes a valid interface - the callers
2251 * were supposed to validate.
2253 * That said, if sw_if_index corresponds to an interface that exists at all,
2254 * this function must return errors accordingly if the ACL is not applied.
2258 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2261 u32 macip_acl_index;
2262 macip_acl_list_t *a;
2264 /* The vector is too short - MACIP ACL is not applied */
2265 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
2266 return VNET_API_ERROR_NO_SUCH_ENTRY;
2268 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2269 /* No point in deleting MACIP ACL which is not applied */
2270 if (~0 == macip_acl_index)
2271 return VNET_API_ERROR_NO_SUCH_ENTRY;
2273 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2274 /* remove the classifier tables off the interface L2 ACL */
2276 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2277 a->ip6_table_index, a->l2_table_index, 0);
2279 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2280 a->out_ip4_table_index, a->out_ip6_table_index,
2281 a->out_l2_table_index, 0);
2282 /* Unset the MACIP ACL index */
2283 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2284 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
2285 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
2288 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
2292 /* No check for validity of sw_if_index - the callers were supposed to validate */
2295 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2296 u32 macip_acl_index)
2298 macip_acl_list_t *a;
2300 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2302 return VNET_API_ERROR_NO_SUCH_ENTRY;
2304 void *oldheap = acl_set_heap (am);
2305 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2306 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2307 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
2308 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
2309 clib_mem_set_heap (oldheap);
2310 /* If there already a MACIP ACL applied, unapply it */
2311 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2312 macip_acl_interface_del_acl (am, sw_if_index);
2313 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2315 /* Apply the classifier tables for L2 ACLs */
2317 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2318 a->ip6_table_index, a->l2_table_index, 1);
2320 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2321 a->out_ip4_table_index, a->out_ip6_table_index,
2322 a->out_l2_table_index, 1);
2327 macip_acl_del_list (u32 acl_list_index)
2329 acl_main_t *am = &acl_main;
2330 macip_acl_list_t *a;
2332 if (pool_is_free_index (am->macip_acls, acl_list_index))
2334 return VNET_API_ERROR_NO_SUCH_ENTRY;
2337 /* delete any references to the ACL */
2338 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2340 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2342 macip_acl_interface_del_acl (am, i);
2346 void *oldheap = acl_set_heap (am);
2347 /* Now that classifier tables are detached, clean them up */
2348 macip_destroy_classify_tables (am, acl_list_index);
2350 /* now we can delete the ACL itself */
2351 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2354 vec_free (a->rules);
2356 pool_put (am->macip_acls, a);
2357 clib_mem_set_heap (oldheap);
2363 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2366 acl_main_t *am = &acl_main;
2370 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2374 rv = macip_acl_interface_del_acl (am, sw_if_index);
2380 * If the client does not allocate enough memory for a variable-length
2381 * message, and then proceed to use it as if the full memory allocated,
2382 * absent the check we happily consume that on the VPP side, and go
2383 * along as if nothing happened. However, the resulting
2384 * effects range from just garbage in the API decode
2385 * (because the decoder snoops too far), to potential memory
2388 * This verifies that the actual length of the message is
2389 * at least expected_len, and complains loudly if it is not.
2391 * A failing check here is 100% a software bug on the API user side,
2392 * so we might as well yell.
2396 verify_message_len (void *mp, u32 expected_len, char *where)
2398 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2399 if (supplied_len < expected_len)
2401 clib_warning ("%s: Supplied message length %d is less than expected %d",
2402 where, supplied_len, expected_len);
2411 /* API message handler */
2413 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2415 vl_api_acl_add_replace_reply_t *rmp;
2416 acl_main_t *am = &acl_main;
2418 u32 acl_list_index = ntohl (mp->acl_index);
2419 u32 acl_count = ntohl (mp->count);
2420 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2422 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2424 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2428 rv = VNET_API_ERROR_INVALID_VALUE;
2432 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2434 rmp->acl_index = htonl(acl_list_index);
2440 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2442 acl_main_t *am = &acl_main;
2443 vl_api_acl_del_reply_t *rmp;
2446 rv = acl_del_list (ntohl (mp->acl_index));
2448 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2452 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2454 acl_main_t *am = &acl_main;
2455 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2456 u32 sw_if_index = ntohl (mp->sw_if_index);
2457 vl_api_acl_interface_add_del_reply_t *rmp;
2460 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2461 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2464 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2465 mp->is_input, ntohl (mp->acl_index));
2467 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2471 vl_api_acl_interface_set_acl_list_t_handler
2472 (vl_api_acl_interface_set_acl_list_t * mp)
2474 acl_main_t *am = &acl_main;
2475 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2478 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2479 u32 sw_if_index = ntohl (mp->sw_if_index);
2481 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2482 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2485 int may_clear_sessions = 1;
2486 for (i = 0; i < mp->count; i++)
2488 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2490 /* ACL does not exist, so we can not apply it */
2491 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2496 void *oldheap = acl_set_heap (am);
2498 u32 *in_acl_vec = 0;
2499 u32 *out_acl_vec = 0;
2500 for (i = 0; i < mp->count; i++)
2501 if (i < mp->n_input)
2502 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2504 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2507 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2508 &may_clear_sessions);
2510 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2512 &may_clear_sessions);
2513 vec_free (in_acl_vec);
2514 vec_free (out_acl_vec);
2515 clib_mem_set_heap (oldheap);
2519 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2523 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2525 api_rule->is_permit = r->is_permit;
2526 api_rule->is_ipv6 = r->is_ipv6;
2529 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2530 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2534 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2535 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2537 api_rule->src_ip_prefix_len = r->src_prefixlen;
2538 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2539 api_rule->proto = r->proto;
2540 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2541 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2542 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2543 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2544 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2545 api_rule->tcp_flags_value = r->tcp_flags_value;
2549 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2550 acl_list_t * acl, u32 context)
2552 vl_api_acl_details_t *mp;
2553 vl_api_acl_rule_t *rules;
2555 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2556 void *oldheap = acl_set_heap (am);
2558 mp = vl_msg_api_alloc (msg_size);
2559 memset (mp, 0, msg_size);
2560 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2562 /* fill in the message */
2563 mp->context = context;
2564 mp->count = htonl (acl->count);
2565 mp->acl_index = htonl (acl - am->acls);
2566 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2567 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2569 for (i = 0; i < acl->count; i++)
2571 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2574 clib_mem_set_heap (oldheap);
2575 vl_api_send_msg (reg, (u8 *) mp);
2580 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2582 acl_main_t *am = &acl_main;
2586 vl_api_registration_t *reg;
2588 reg = vl_api_client_index_to_registration (mp->client_index);
2592 if (mp->acl_index == ~0)
2595 /* Just dump all ACLs */
2596 pool_foreach (acl, am->acls,
2598 send_acl_details(am, reg, acl, mp->context);
2604 acl_index = ntohl (mp->acl_index);
2605 if (!pool_is_free_index (am->acls, acl_index))
2607 acl = pool_elt_at_index (am->acls, acl_index);
2608 send_acl_details (am, reg, acl, mp->context);
2614 /* FIXME API: should we signal an error here at all ? */
2620 send_acl_interface_list_details (acl_main_t * am,
2621 vl_api_registration_t * reg,
2622 u32 sw_if_index, u32 context)
2624 vl_api_acl_interface_list_details_t *mp;
2630 void *oldheap = acl_set_heap (am);
2632 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2633 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2635 clib_mem_set_heap (oldheap);
2637 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2638 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2639 count = n_input + n_output;
2641 msg_size = sizeof (*mp);
2642 msg_size += sizeof (mp->acls[0]) * count;
2644 mp = vl_msg_api_alloc (msg_size);
2645 memset (mp, 0, msg_size);
2647 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2649 /* fill in the message */
2650 mp->context = context;
2651 mp->sw_if_index = htonl (sw_if_index);
2653 mp->n_input = n_input;
2654 for (i = 0; i < n_input; i++)
2656 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2658 for (i = 0; i < n_output; i++)
2660 mp->acls[n_input + i] =
2661 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2663 vl_api_send_msg (reg, (u8 *) mp);
2667 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2670 acl_main_t *am = &acl_main;
2671 vnet_sw_interface_t *swif;
2672 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2675 vl_api_registration_t *reg;
2677 reg = vl_api_client_index_to_registration (mp->client_index);
2681 if (mp->sw_if_index == ~0)
2684 pool_foreach (swif, im->sw_interfaces,
2686 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2692 sw_if_index = ntohl (mp->sw_if_index);
2693 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2694 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2698 /* MACIP ACL API handlers */
2701 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2703 vl_api_macip_acl_add_reply_t *rmp;
2704 acl_main_t *am = &acl_main;
2706 u32 acl_list_index = ~0;
2707 u32 acl_count = ntohl (mp->count);
2708 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2710 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2712 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2716 rv = VNET_API_ERROR_INVALID_VALUE;
2720 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2722 rmp->acl_index = htonl(acl_list_index);
2728 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2730 vl_api_macip_acl_add_replace_reply_t *rmp;
2731 acl_main_t *am = &acl_main;
2733 u32 acl_list_index = ntohl (mp->acl_index);
2734 u32 acl_count = ntohl (mp->count);
2735 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2737 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2739 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2743 rv = VNET_API_ERROR_INVALID_VALUE;
2747 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2749 rmp->acl_index = htonl(acl_list_index);
2755 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2757 acl_main_t *am = &acl_main;
2758 vl_api_macip_acl_del_reply_t *rmp;
2761 rv = macip_acl_del_list (ntohl (mp->acl_index));
2763 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2767 vl_api_macip_acl_interface_add_del_t_handler
2768 (vl_api_macip_acl_interface_add_del_t * mp)
2770 acl_main_t *am = &acl_main;
2771 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2773 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2774 u32 sw_if_index = ntohl (mp->sw_if_index);
2776 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2777 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2780 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2781 ntohl (mp->acl_index));
2783 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2787 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2788 macip_acl_list_t * acl, u32 context)
2790 vl_api_macip_acl_details_t *mp;
2791 vl_api_macip_acl_rule_t *rules;
2792 macip_acl_rule_t *r;
2794 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2796 mp = vl_msg_api_alloc (msg_size);
2797 memset (mp, 0, msg_size);
2798 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2800 /* fill in the message */
2801 mp->context = context;
2804 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2805 mp->count = htonl (acl->count);
2806 mp->acl_index = htonl (acl - am->macip_acls);
2808 for (i = 0; i < acl->count; i++)
2811 rules[i].is_permit = r->is_permit;
2812 rules[i].is_ipv6 = r->is_ipv6;
2813 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2814 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2815 sizeof (r->src_mac_mask));
2817 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2818 sizeof (r->src_ip_addr.ip6));
2820 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2821 sizeof (r->src_ip_addr.ip4));
2822 rules[i].src_ip_prefix_len = r->src_prefixlen;
2827 /* No martini, no party - no ACL applied to this interface. */
2832 vl_api_send_msg (reg, (u8 *) mp);
2837 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2839 acl_main_t *am = &acl_main;
2840 macip_acl_list_t *acl;
2842 vl_api_registration_t *reg;
2844 reg = vl_api_client_index_to_registration (mp->client_index);
2848 if (mp->acl_index == ~0)
2850 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2851 pool_foreach (acl, am->macip_acls, (
2853 send_macip_acl_details (am, reg,
2862 u32 acl_index = ntohl (mp->acl_index);
2863 if (!pool_is_free_index (am->macip_acls, acl_index))
2865 acl = pool_elt_at_index (am->macip_acls, acl_index);
2866 send_macip_acl_details (am, reg, acl, mp->context);
2872 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2875 acl_main_t *am = &acl_main;
2876 vl_api_macip_acl_interface_get_reply_t *rmp;
2877 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2878 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2879 vl_api_registration_t *reg;
2882 reg = vl_api_client_index_to_registration (mp->client_index);
2886 rmp = vl_msg_api_alloc (msg_size);
2887 memset (rmp, 0, msg_size);
2889 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2890 rmp->context = mp->context;
2891 rmp->count = htonl (count);
2892 for (i = 0; i < count; i++)
2894 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2897 vl_api_send_msg (reg, (u8 *) rmp);
2901 send_macip_acl_interface_list_details (acl_main_t * am,
2902 vl_api_registration_t * reg,
2904 u32 acl_index, u32 context)
2906 vl_api_macip_acl_interface_list_details_t *rmp;
2907 /* at this time there is only ever 1 mac ip acl per interface */
2908 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2910 rmp = vl_msg_api_alloc (msg_size);
2911 memset (rmp, 0, msg_size);
2913 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2915 /* fill in the message */
2916 rmp->context = context;
2918 rmp->sw_if_index = htonl (sw_if_index);
2919 rmp->acls[0] = htonl (acl_index);
2921 vl_api_send_msg (reg, (u8 *) rmp);
2925 vl_api_macip_acl_interface_list_dump_t_handler
2926 (vl_api_macip_acl_interface_list_dump_t * mp)
2928 vl_api_registration_t *reg;
2929 acl_main_t *am = &acl_main;
2930 u32 sw_if_index = ntohl (mp->sw_if_index);
2932 reg = vl_api_client_index_to_registration (mp->client_index);
2936 if (sw_if_index == ~0)
2938 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2940 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2942 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2943 am->macip_acl_by_sw_if_index
2951 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2953 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2954 am->macip_acl_by_sw_if_index
2955 [sw_if_index], mp->context);
2961 vl_api_acl_interface_set_etype_whitelist_t_handler
2962 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2964 acl_main_t *am = &acl_main;
2965 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2968 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2969 u32 sw_if_index = ntohl (mp->sw_if_index);
2970 u16 *vec_in = 0, *vec_out = 0;
2971 void *oldheap = acl_set_heap (am);
2973 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2974 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2977 for (i = 0; i < mp->count; i++)
2979 if (i < mp->n_input)
2980 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2982 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2984 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2987 clib_mem_set_heap (oldheap);
2988 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2992 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2993 vl_api_registration_t * reg,
2994 u32 sw_if_index, u32 context)
2996 vl_api_acl_interface_etype_whitelist_details_t *mp;
3003 u16 *whitelist_in = 0;
3004 u16 *whitelist_out = 0;
3006 if (intf_has_etype_whitelist (am, sw_if_index, 0))
3008 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
3010 if (intf_has_etype_whitelist (am, sw_if_index, 1))
3012 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
3014 if ((0 == whitelist_in) && (0 == whitelist_out))
3015 return; /* nothing to do */
3017 void *oldheap = acl_set_heap (am);
3019 n_input = vec_len (whitelist_in);
3020 n_output = vec_len (whitelist_out);
3021 count = n_input + n_output;
3023 msg_size = sizeof (*mp);
3024 msg_size += sizeof (mp->whitelist[0]) * count;
3026 mp = vl_msg_api_alloc (msg_size);
3027 memset (mp, 0, msg_size);
3029 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
3031 /* fill in the message */
3032 mp->context = context;
3033 mp->sw_if_index = htonl (sw_if_index);
3035 mp->n_input = n_input;
3036 for (i = 0; i < n_input; i++)
3038 mp->whitelist[i] = htons (whitelist_in[i]);
3040 for (i = 0; i < n_output; i++)
3042 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
3044 clib_mem_set_heap (oldheap);
3045 vl_api_send_msg (reg, (u8 *) mp);
3050 vl_api_acl_interface_etype_whitelist_dump_t_handler
3051 (vl_api_acl_interface_list_dump_t * mp)
3053 acl_main_t *am = &acl_main;
3054 vnet_sw_interface_t *swif;
3055 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3058 vl_api_registration_t *reg;
3060 reg = vl_api_client_index_to_registration (mp->client_index);
3064 if (mp->sw_if_index == ~0)
3067 pool_foreach (swif, im->sw_interfaces,
3069 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
3075 sw_if_index = ntohl (mp->sw_if_index);
3076 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
3077 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
3084 /* Set up the API message handling tables */
3085 static clib_error_t *
3086 acl_plugin_api_hookup (vlib_main_t * vm)
3088 acl_main_t *am = &acl_main;
3090 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
3092 vl_api_##n##_t_handler, \
3094 vl_api_##n##_t_endian, \
3095 vl_api_##n##_t_print, \
3096 sizeof(vl_api_##n##_t), 1);
3097 foreach_acl_plugin_api_msg;
3103 #define vl_msg_name_crc_list
3104 #include <acl/acl_all_api_h.h>
3105 #undef vl_msg_name_crc_list
3108 setup_message_id_table (acl_main_t * am, api_main_t * apim)
3110 #define _(id,n,crc) \
3111 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
3112 foreach_vl_msg_name_crc_acl;
3117 acl_setup_fa_nodes (void)
3119 vlib_main_t *vm = vlib_get_main ();
3120 acl_main_t *am = &acl_main;
3121 vlib_node_t *n, *n4, *n6;
3123 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
3124 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
3125 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
3128 am->l2_input_classify_next_acl_ip4 =
3129 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3130 am->l2_input_classify_next_acl_ip6 =
3131 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3133 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
3134 l2input_get_feat_names (),
3135 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
3137 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
3138 l2input_get_feat_names (),
3139 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
3142 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
3143 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
3144 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
3146 am->l2_output_classify_next_acl_ip4 =
3147 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3148 am->l2_output_classify_next_acl_ip6 =
3149 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3151 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
3152 l2output_get_feat_names (),
3153 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
3155 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
3156 l2output_get_feat_names (),
3157 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
3161 acl_set_timeout_sec (int timeout_type, u32 value)
3163 acl_main_t *am = &acl_main;
3164 clib_time_t *ct = &am->vlib_main->clib_time;
3166 if (timeout_type < ACL_N_TIMEOUTS)
3168 am->session_timeout_sec[timeout_type] = value;
3172 clib_warning ("Unknown timeout type %d", timeout_type);
3175 am->session_timeout[timeout_type] =
3176 (u64) (((f64) value) / ct->seconds_per_clock);
3180 acl_set_session_max_entries (u32 value)
3182 acl_main_t *am = &acl_main;
3183 am->fa_conn_table_max_entries = value;
3187 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3189 acl_main_t *am = &acl_main;
3191 if ((eh < 256) && (value < 2))
3193 am->fa_ipv6_known_eh_bitmap =
3194 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3202 static clib_error_t *
3203 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3205 acl_main_t *am = &acl_main;
3206 if (0 == am->acl_mheap)
3208 /* ACL heap is not initialized, so definitely nothing to do. */
3213 int may_clear_sessions = 1;
3214 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3215 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3217 /* also unapply any ACLs in case the users did not do so. */
3218 macip_acl_interface_del_acl (am, sw_if_index);
3219 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
3220 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
3225 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3229 static clib_error_t *
3230 acl_set_aclplugin_fn (vlib_main_t * vm,
3231 unformat_input_t * input, vlib_cli_command_t * cmd)
3233 clib_error_t *error = 0;
3237 uword memory_size = 0;
3238 acl_main_t *am = &acl_main;
3240 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3242 if (!acl_set_skip_ipv6_eh (eh_val, val))
3244 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3248 if (unformat (input, "use-hash-acl-matching %u", &val))
3250 am->use_hash_acl_matching = (val != 0);
3253 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3255 am->l4_match_nonfirst_fragment = (val != 0);
3258 if (unformat (input, "reclassify-sessions %u", &val))
3260 am->reclassify_sessions = (val != 0);
3263 if (unformat (input, "event-trace"))
3265 if (!unformat (input, "%u", &val))
3267 error = clib_error_return (0,
3268 "expecting trace level, got `%U`",
3269 format_unformat_error, input);
3274 am->trace_acl = val;
3278 if (unformat (input, "heap"))
3280 if (unformat (input, "main"))
3282 if (unformat (input, "validate %u", &val))
3283 acl_plugin_acl_set_validate_heap (am, val);
3284 else if (unformat (input, "trace %u", &val))
3285 acl_plugin_acl_set_trace_heap (am, val);
3288 else if (unformat (input, "hash"))
3290 if (unformat (input, "validate %u", &val))
3291 acl_plugin_hash_acl_set_validate_heap (val);
3292 else if (unformat (input, "trace %u", &val))
3293 acl_plugin_hash_acl_set_trace_heap (val);
3298 if (unformat (input, "session"))
3300 if (unformat (input, "table"))
3302 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3303 if (unformat (input, "max-entries"))
3305 if (!unformat (input, "%u", &val))
3307 error = clib_error_return (0,
3308 "expecting maximum number of entries, got `%U`",
3309 format_unformat_error, input);
3314 acl_set_session_max_entries (val);
3318 if (unformat (input, "hash-table-buckets"))
3320 if (!unformat (input, "%u", &val))
3322 error = clib_error_return (0,
3323 "expecting maximum number of hash table buckets, got `%U`",
3324 format_unformat_error, input);
3329 am->fa_conn_table_hash_num_buckets = val;
3333 if (unformat (input, "hash-table-memory"))
3335 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3337 error = clib_error_return (0,
3338 "expecting maximum amount of hash table memory, got `%U`",
3339 format_unformat_error, input);
3344 am->fa_conn_table_hash_memory_size = memory_size;
3348 if (unformat (input, "event-trace"))
3350 if (!unformat (input, "%u", &val))
3352 error = clib_error_return (0,
3353 "expecting trace level, got `%U`",
3354 format_unformat_error, input);
3359 am->trace_sessions = val;
3365 if (unformat (input, "timeout"))
3367 if (unformat (input, "udp"))
3369 if (unformat (input, "idle"))
3371 if (!unformat (input, "%u", &timeout))
3373 error = clib_error_return (0,
3374 "expecting timeout value in seconds, got `%U`",
3375 format_unformat_error,
3381 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3386 if (unformat (input, "tcp"))
3388 if (unformat (input, "idle"))
3390 if (!unformat (input, "%u", &timeout))
3392 error = clib_error_return (0,
3393 "expecting timeout value in seconds, got `%U`",
3394 format_unformat_error,
3400 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3404 if (unformat (input, "transient"))
3406 if (!unformat (input, "%u", &timeout))
3408 error = clib_error_return (0,
3409 "expecting timeout value in seconds, got `%U`",
3410 format_unformat_error,
3416 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3430 my_format_mac_address (u8 * s, va_list * args)
3432 u8 *a = va_arg (*args, u8 *);
3433 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3434 a[0], a[1], a[2], a[3], a[4], a[5]);
3438 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3440 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3442 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3443 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3444 format_ip46_address, &a->src_ip_addr,
3445 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3447 my_format_mac_address, a->src_mac,
3448 my_format_mac_address, a->src_mac_mask);
3453 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3455 vlib_main_t *vm = am->vlib_main;
3458 /* Don't try to print someone else's memory */
3459 if (macip_acl_index > vec_len (am->macip_acls))
3462 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3463 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3465 vlib_cli_output (vm,
3466 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3467 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3469 vlib_cli_output (vm,
3470 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3471 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3472 vlib_cli_output (vm,
3473 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3474 a->out_ip4_table_index, a->out_ip6_table_index,
3475 a->out_l2_table_index);
3476 for (i = 0; i < vec_len (a->rules); i++)
3477 vlib_cli_output (vm, " rule %d: %U\n", i,
3478 my_macip_acl_rule_t_pretty_format,
3479 vec_elt_at_index (a->rules, i));
3483 static clib_error_t *
3484 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3486 input, vlib_cli_command_t * cmd)
3488 clib_error_t *error = 0;
3489 acl_main_t *am = &acl_main;
3493 (void) unformat (input, "index %u", &acl_index);
3495 for (i = 0; i < vec_len (am->macip_acls); i++)
3497 /* Don't attempt to show the ACLs that do not exist */
3498 if (pool_is_free_index (am->macip_acls, i))
3501 if ((acl_index != ~0) && (acl_index != i))
3506 macip_acl_print (am, i);
3507 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3509 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3511 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3519 static clib_error_t *
3520 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3522 input, vlib_cli_command_t * cmd)
3524 clib_error_t *error = 0;
3525 acl_main_t *am = &acl_main;
3527 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3529 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3530 vec_elt (am->macip_acl_by_sw_if_index, i));
3536 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3539 vlib_main_t *vm = am->vlib_main;
3541 for (i = 0; i < vec_len (am->acls); i++)
3543 if (acl_is_not_defined (am, i))
3545 /* don't attempt to show the ACLs that do not exist */
3548 if ((acl_index != ~0) && (acl_index != i))
3552 acl_print_acl (vm, am, i);
3554 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3556 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3557 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3560 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3562 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3563 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3566 if (i < vec_len (am->lc_index_vec_by_acl))
3568 vlib_cli_output (vm, " used in lookup context index: %U\n",
3569 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3574 static clib_error_t *
3575 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3576 unformat_input_t * input, vlib_cli_command_t * cmd)
3578 clib_error_t *error = 0;
3579 acl_main_t *am = &acl_main;
3582 (void) unformat (input, "index %u", &acl_index);
3584 acl_plugin_show_acl (am, acl_index);
3588 static clib_error_t *
3589 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3590 unformat_input_t * input,
3591 vlib_cli_command_t * cmd)
3593 clib_error_t *error = 0;
3596 (void) unformat (input, "index %u", &lc_index);
3598 acl_plugin_show_lookup_context (lc_index);
3602 static clib_error_t *
3603 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3604 unformat_input_t * input,
3605 vlib_cli_command_t * cmd)
3607 clib_error_t *error = 0;
3610 (void) unformat (input, "index %u", &lc_index);
3612 acl_plugin_show_lookup_user (lc_index);
3618 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3621 vlib_main_t *vm = am->vlib_main;
3624 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3625 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3627 /* if we need a particular interface, skip all the others */
3628 if ((sw_if_index != ~0) && (sw_if_index != swi))
3631 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3632 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3633 vlib_cli_output (vm, " input policy epoch: %x\n",
3634 vec_elt (am->input_policy_epoch_by_sw_if_index,
3636 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3637 vlib_cli_output (vm, " output policy epoch: %x\n",
3638 vec_elt (am->output_policy_epoch_by_sw_if_index,
3642 if (intf_has_etype_whitelist (am, swi, 1))
3644 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3645 am->input_etype_whitelist_by_sw_if_index[swi],
3648 if (intf_has_etype_whitelist (am, swi, 0))
3650 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3651 am->output_etype_whitelist_by_sw_if_index[swi],
3655 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3656 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3658 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3659 am->input_acl_vec_by_sw_if_index[swi], "%d");
3662 vlib_cli_output (vm, "\n");
3663 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3665 acl_print_acl (vm, am, *pj);
3667 vlib_cli_output (vm, "\n");
3671 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3672 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3674 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3675 am->output_acl_vec_by_sw_if_index[swi], "%d");
3678 vlib_cli_output (vm, "\n");
3679 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3681 acl_print_acl (vm, am, *pj);
3683 vlib_cli_output (vm, "\n");
3686 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3688 vlib_cli_output (vm, " input lookup context index: %d",
3689 am->input_lc_index_by_sw_if_index[swi]);
3691 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3693 vlib_cli_output (vm, " output lookup context index: %d",
3694 am->output_lc_index_by_sw_if_index[swi]);
3701 static clib_error_t *
3702 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3703 unformat_input_t * input,
3704 vlib_cli_command_t * cmd)
3706 clib_error_t *error = 0;
3707 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3710 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3711 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3712 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3713 format_acl_plugin_5tuple, five_tuple);
3715 error = clib_error_return (0, "expecting 6 hex integers");
3720 static clib_error_t *
3721 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3723 input, vlib_cli_command_t * cmd)
3725 clib_error_t *error = 0;
3726 acl_main_t *am = &acl_main;
3728 u32 sw_if_index = ~0;
3729 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3730 int show_acl = unformat (input, "acl");
3731 int detail = unformat (input, "detail");
3733 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3737 static clib_error_t *
3738 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3739 unformat_input_t * input,
3740 vlib_cli_command_t * cmd)
3742 clib_error_t *error = 0;
3743 acl_main_t *am = &acl_main;
3745 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3748 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3752 vlib_cli_output (vm, " Not initialized\n");
3754 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3755 if (am->hash_lookup_mheap)
3757 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3761 vlib_cli_output (vm, " Not initialized\n");
3767 acl_plugin_show_sessions (acl_main_t * am,
3768 u32 show_session_thread_id,
3769 u32 show_session_session_index)
3771 vlib_main_t *vm = am->vlib_main;
3773 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3774 vnet_sw_interface_t *swif;
3777 u64 n_adds = am->fa_session_total_adds;
3778 u64 n_dels = am->fa_session_total_dels;
3779 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3780 n_dels, n_adds - n_dels);
3782 vlib_cli_output (vm, "\n\nPer-thread data:");
3783 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3785 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3786 vlib_cli_output (vm, "Thread #%d:", wk);
3787 if (show_session_thread_id == wk
3788 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3790 vlib_cli_output (vm, " session index %u:",
3791 show_session_session_index);
3792 fa_session_t *sess =
3793 pw->fa_sessions_pool + show_session_session_index;
3794 u64 *m = (u64 *) & sess->info;
3795 vlib_cli_output (vm,
3796 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3797 m[0], m[1], m[2], m[3], m[4], m[5]);
3798 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3799 vlib_cli_output (vm, " tcp_flags_seen: %x",
3800 sess->tcp_flags_seen.as_u16);
3801 vlib_cli_output (vm, " last active time: %lu",
3802 sess->last_active_time);
3803 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3804 vlib_cli_output (vm, " link enqueue time: %lu",
3805 sess->link_enqueue_time);
3806 vlib_cli_output (vm, " link next index: %u",
3807 sess->link_next_idx);
3808 vlib_cli_output (vm, " link prev index: %u",
3809 sess->link_prev_idx);
3810 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3812 vlib_cli_output (vm, " connection add/del stats:", wk);
3813 pool_foreach (swif, im->sw_interfaces, (
3820 (pw->fa_session_adds_by_sw_if_index)
3822 pw->fa_session_adds_by_sw_if_index
3827 (pw->fa_session_dels_by_sw_if_index)
3829 pw->fa_session_dels_by_sw_if_index
3831 u64 n_epoch_changes =
3834 (pw->fa_session_epoch_change_by_sw_if_index)
3836 pw->fa_session_epoch_change_by_sw_if_index
3838 vlib_cli_output (vm,
3839 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3849 vlib_cli_output (vm, " connection timeout type lists:", wk);
3851 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3853 u32 head_session_index = pw->fa_conn_list_head[tt];
3854 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3855 head_session_index);
3856 if (~0 != head_session_index)
3858 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3859 vlib_cli_output (vm, " last active time: %lu",
3860 sess->last_active_time);
3861 vlib_cli_output (vm, " link enqueue time: %lu",
3862 sess->link_enqueue_time);
3866 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3867 vlib_cli_output (vm, " Requeue until time: %lu",
3868 pw->requeue_until_time);
3869 vlib_cli_output (vm, " Current time wait interval: %lu",
3870 pw->current_time_wait_interval);
3871 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3872 pw->cnt_deleted_sessions);
3873 vlib_cli_output (vm, " Delete already deleted: %lu",
3874 pw->cnt_already_deleted_sessions);
3875 vlib_cli_output (vm, " Session timers restarted: %lu",
3876 pw->cnt_session_timer_restarted);
3877 vlib_cli_output (vm, " Swipe until this time: %lu",
3878 pw->swipe_end_time);
3879 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3880 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3881 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3883 pw->pending_clear_sw_if_index_bitmap);
3884 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3885 vlib_cli_output (vm, " interrupt is pending: %d",
3886 pw->interrupt_is_pending);
3887 vlib_cli_output (vm, " interrupt is needed: %d",
3888 pw->interrupt_is_needed);
3889 vlib_cli_output (vm, " interrupt is unwanted: %d",
3890 pw->interrupt_is_unwanted);
3891 vlib_cli_output (vm, " interrupt generation: %d",
3892 pw->interrupt_generation);
3894 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3895 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3896 foreach_fa_cleaner_counter;
3898 vlib_cli_output (vm, "Interrupt generation: %d",
3899 am->fa_interrupt_generation);
3900 vlib_cli_output (vm,
3901 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3902 am->fa_min_deleted_sessions_per_interval,
3903 am->fa_max_deleted_sessions_per_interval,
3904 am->fa_cleaner_wait_time_increment * 1000.0,
3905 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3906 1000.0 / (f64) vm->clib_time.clocks_per_second);
3907 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3910 static clib_error_t *
3911 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3912 unformat_input_t * input,
3913 vlib_cli_command_t * cmd)
3915 clib_error_t *error = 0;
3916 acl_main_t *am = &acl_main;
3918 u32 show_bihash_verbose = 0;
3919 u32 show_session_thread_id = ~0;
3920 u32 show_session_session_index = ~0;
3921 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3922 &show_session_session_index);
3923 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3925 acl_plugin_show_sessions (am, show_session_thread_id,
3926 show_session_session_index);
3927 show_fa_sessions_hash (vm, show_bihash_verbose);
3931 static clib_error_t *
3932 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3933 unformat_input_t * input,
3934 vlib_cli_command_t * cmd)
3936 clib_error_t *error = 0;
3939 u32 sw_if_index = ~0;
3940 int show_acl_hash_info = 0;
3941 int show_applied_info = 0;
3942 int show_mask_type = 0;
3943 int show_bihash = 0;
3944 u32 show_bihash_verbose = 0;
3946 if (unformat (input, "acl"))
3948 show_acl_hash_info = 1;
3949 /* mask-type is handy to see as well right there */
3951 unformat (input, "index %u", &acl_index);
3953 else if (unformat (input, "applied"))
3955 show_applied_info = 1;
3956 unformat (input, "sw_if_index %u", &sw_if_index);
3958 else if (unformat (input, "mask"))
3962 else if (unformat (input, "hash"))
3965 unformat (input, "verbose %u", &show_bihash_verbose);
3969 (show_mask_type || show_acl_hash_info || show_applied_info
3972 /* if no qualifiers specified, show all */
3974 show_acl_hash_info = 1;
3975 show_applied_info = 1;
3979 acl_plugin_show_tables_mask_type ();
3980 if (show_acl_hash_info)
3981 acl_plugin_show_tables_acl_hash_info (acl_index);
3982 if (show_applied_info)
3983 acl_plugin_show_tables_applied_info (sw_if_index);
3985 acl_plugin_show_tables_bihash (show_bihash_verbose);
3990 static clib_error_t *
3991 acl_clear_aclplugin_fn (vlib_main_t * vm,
3992 unformat_input_t * input, vlib_cli_command_t * cmd)
3994 clib_error_t *error = 0;
3995 acl_main_t *am = &acl_main;
3996 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3997 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
4002 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
4003 .path = "set acl-plugin",
4004 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
4005 .function = acl_set_aclplugin_fn,
4008 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
4009 .path = "show acl-plugin acl",
4010 .short_help = "show acl-plugin acl [index N]",
4011 .function = acl_show_aclplugin_acl_fn,
4014 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
4015 .path = "show acl-plugin lookup context",
4016 .short_help = "show acl-plugin lookup context [index N]",
4017 .function = acl_show_aclplugin_lookup_context_fn,
4020 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
4021 .path = "show acl-plugin lookup user",
4022 .short_help = "show acl-plugin lookup user [index N]",
4023 .function = acl_show_aclplugin_lookup_user_fn,
4026 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
4027 .path = "show acl-plugin decode 5tuple",
4028 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
4029 .function = acl_show_aclplugin_decode_5tuple_fn,
4032 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
4033 .path = "show acl-plugin interface",
4034 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
4035 .function = acl_show_aclplugin_interface_fn,
4038 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
4039 .path = "show acl-plugin memory",
4040 .short_help = "show acl-plugin memory",
4041 .function = acl_show_aclplugin_memory_fn,
4044 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
4045 .path = "show acl-plugin sessions",
4046 .short_help = "show acl-plugin sessions",
4047 .function = acl_show_aclplugin_sessions_fn,
4050 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
4051 .path = "show acl-plugin tables",
4052 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
4053 .function = acl_show_aclplugin_tables_fn,
4056 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
4057 .path = "show acl-plugin macip acl",
4058 .short_help = "show acl-plugin macip acl [index N]",
4059 .function = acl_show_aclplugin_macip_acl_fn,
4062 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
4063 .path = "show acl-plugin macip interface",
4064 .short_help = "show acl-plugin macip interface",
4065 .function = acl_show_aclplugin_macip_interface_fn,
4068 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
4069 .path = "clear acl-plugin sessions",
4070 .short_help = "clear acl-plugin sessions",
4071 .function = acl_clear_aclplugin_fn,
4075 static clib_error_t *
4076 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
4078 acl_main_t *am = &acl_main;
4079 u32 conn_table_hash_buckets;
4080 u32 conn_table_hash_memory_size;
4081 u32 conn_table_max_entries;
4082 uword main_heap_size;
4083 uword hash_heap_size;
4084 u32 hash_lookup_hash_buckets;
4085 u32 hash_lookup_hash_memory;
4086 u32 reclassify_sessions;
4088 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4091 (input, "connection hash buckets %d", &conn_table_hash_buckets))
4092 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
4093 else if (unformat (input, "connection hash memory %d",
4094 &conn_table_hash_memory_size))
4095 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
4096 else if (unformat (input, "connection count max %d",
4097 &conn_table_max_entries))
4098 am->fa_conn_table_max_entries = conn_table_max_entries;
4101 (input, "main heap size %U", unformat_memory_size,
4103 am->acl_mheap_size = main_heap_size;
4106 (input, "hash lookup heap size %U", unformat_memory_size,
4108 am->hash_lookup_mheap_size = hash_heap_size;
4109 else if (unformat (input, "hash lookup hash buckets %d",
4110 &hash_lookup_hash_buckets))
4111 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
4112 else if (unformat (input, "hash lookup hash memory %d",
4113 &hash_lookup_hash_memory))
4114 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4115 else if (unformat (input, "reclassify sessions %d",
4116 &reclassify_sessions))
4117 am->reclassify_sessions = reclassify_sessions;
4120 return clib_error_return (0, "unknown input '%U'",
4121 format_unformat_error, input);
4126 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4128 static clib_error_t *
4129 acl_init (vlib_main_t * vm)
4131 acl_main_t *am = &acl_main;
4132 clib_error_t *error = 0;
4133 memset (am, 0, sizeof (*am));
4135 am->vnet_main = vnet_get_main ();
4137 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4139 /* Ask for a correctly-sized block of API message decode slots */
4140 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4141 VL_MSG_FIRST_AVAILABLE);
4143 error = acl_plugin_api_hookup (vm);
4145 /* Add our API messages to the global name_crc hash table */
4146 setup_message_id_table (am, &api_main);
4150 acl_setup_fa_nodes ();
4152 am->acl_mheap_size = 0; /* auto size when initializing */
4153 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4155 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4156 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4158 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4159 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4160 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4161 TCP_SESSION_IDLE_TIMEOUT_SEC;
4162 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4163 UDP_SESSION_IDLE_TIMEOUT_SEC;
4165 am->fa_conn_table_hash_num_buckets =
4166 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4167 am->fa_conn_table_hash_memory_size =
4168 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4169 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4170 am->reclassify_sessions = 0;
4171 vlib_thread_main_t *tm = vlib_get_thread_main ();
4172 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4176 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4178 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4179 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4180 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4181 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4183 pw->fa_conn_list_head[tt] = ~0;
4184 pw->fa_conn_list_tail[tt] = ~0;
4189 am->fa_min_deleted_sessions_per_interval =
4190 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4191 am->fa_max_deleted_sessions_per_interval =
4192 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4193 am->fa_cleaner_wait_time_increment =
4194 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4196 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4197 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4198 am->fa_cleaner_cnt_unknown_event = 0;
4199 am->fa_cleaner_cnt_timer_restarted = 0;
4200 am->fa_cleaner_cnt_wait_with_timeout = 0;
4203 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4206 am->l4_match_nonfirst_fragment = 1;
4208 /* use the new fancy hash-based matching */
4209 am->use_hash_acl_matching = 1;
4211 am->interface_acl_user_id = ~0; /* defer till the first use */
4216 VLIB_INIT_FUNCTION (acl_init);
4220 * fd.io coding-style-patch-verification: ON
4223 * eval: (c-set-style "gnu")