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/input_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <acl/acl_msg_enum.h>
33 /* define message structures */
35 #include <acl/acl_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <acl/acl_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <acl/acl_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.h>
55 #include "hash_lookup.h"
59 #define REPLY_MSG_ID_BASE am->msg_id_base
60 #include <vlibapi/api_helper_macros.h>
62 /* List of message types that this plugin understands */
64 #define foreach_acl_plugin_api_msg \
65 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
66 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
67 _(ACL_ADD_REPLACE, acl_add_replace) \
69 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
70 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
71 _(ACL_DUMP, acl_dump) \
72 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
73 _(MACIP_ACL_ADD, macip_acl_add) \
74 _(MACIP_ACL_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)
80 VLIB_PLUGIN_REGISTER () = {
81 .version = VPP_BUILD_VER,
82 .description = "Access Control Lists",
88 acl_set_heap(acl_main_t *am)
90 if (0 == am->acl_mheap) {
91 am->acl_mheap = mheap_alloc (0 /* use VM */ , 2 << 29);
92 mheap_t *h = mheap_header (am->acl_mheap);
93 h->flags |= MHEAP_FLAG_THREAD_SAFE;
95 void *oldheap = clib_mem_set_heap(am->acl_mheap);
100 acl_plugin_acl_set_validate_heap(acl_main_t *am, int on)
102 clib_mem_set_heap(acl_set_heap(am));
103 mheap_t *h = mheap_header (am->acl_mheap);
105 h->flags |= MHEAP_FLAG_VALIDATE;
106 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
109 h->flags &= ~MHEAP_FLAG_VALIDATE;
110 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
115 acl_plugin_acl_set_trace_heap(acl_main_t *am, int on)
117 clib_mem_set_heap(acl_set_heap(am));
118 mheap_t *h = mheap_header (am->acl_mheap);
120 h->flags |= MHEAP_FLAG_TRACE;
122 h->flags &= ~MHEAP_FLAG_TRACE;
127 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
129 acl_main_t *am = &acl_main;
130 vl_api_acl_plugin_get_version_reply_t *rmp;
131 int msg_size = sizeof (*rmp);
132 unix_shared_memory_queue_t *q;
134 q = vl_api_client_index_to_input_queue (mp->client_index);
140 rmp = vl_msg_api_alloc (msg_size);
141 memset (rmp, 0, msg_size);
143 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
144 rmp->context = mp->context;
145 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
146 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
148 vl_msg_api_send_shmem (q, (u8 *) & rmp);
152 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
154 vl_api_acl_plugin_control_ping_reply_t *rmp;
155 acl_main_t *am = &acl_main;
159 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
161 rmp->vpe_pid = ntohl (getpid ());
167 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
168 u32 * acl_list_index, u8 * tag)
170 acl_main_t *am = &acl_main;
173 acl_rule_t *acl_new_rules = 0;
176 if (*acl_list_index != ~0)
178 /* They supplied some number, let's see if this ACL exists */
179 if (pool_is_free_index (am->acls, *acl_list_index))
181 /* tried to replace a non-existent ACL, no point doing anything */
182 clib_warning("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)", *acl_list_index, tag);
187 clib_warning("acl-plugin-warning: supplied no rules for ACL %d (tag %s)", *acl_list_index, tag);
190 void *oldheap = acl_set_heap(am);
192 /* Create and populate the rules */
194 vec_validate(acl_new_rules, count-1);
196 for (i = 0; i < count; i++)
198 r = vec_elt_at_index(acl_new_rules, i);
199 memset(r, 0, sizeof(*r));
200 r->is_permit = rules[i].is_permit;
201 r->is_ipv6 = rules[i].is_ipv6;
204 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
205 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
209 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
210 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
212 r->src_prefixlen = rules[i].src_ip_prefix_len;
213 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
214 r->proto = rules[i].proto;
215 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
216 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
217 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
218 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
219 r->tcp_flags_value = rules[i].tcp_flags_value;
220 r->tcp_flags_mask = rules[i].tcp_flags_mask;
223 if (~0 == *acl_list_index)
226 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
227 memset (a, 0, sizeof (*a));
228 /* Will return the newly allocated ACL index */
229 *acl_list_index = a - am->acls;
233 a = am->acls + *acl_list_index;
234 hash_acl_delete(am, *acl_list_index);
235 /* Get rid of the old rules */
239 a->rules = acl_new_rules;
241 memcpy (a->tag, tag, sizeof (a->tag));
242 hash_acl_add(am, *acl_list_index);
243 clib_mem_set_heap (oldheap);
248 acl_del_list (u32 acl_list_index)
250 acl_main_t *am = &acl_main;
253 if (pool_is_free_index (am->acls, acl_list_index))
258 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
259 if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
260 /* ACL is applied somewhere inbound. Refuse to delete */
264 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
265 if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
266 /* ACL is applied somewhere outbound. Refuse to delete */
271 void *oldheap = acl_set_heap(am);
272 /* delete any references to the ACL */
273 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
275 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
278 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
280 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
288 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
290 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
293 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
295 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
303 /* delete the hash table data */
305 hash_acl_delete(am, acl_list_index);
306 /* now we can delete the ACL itself */
307 a = &am->acls[acl_list_index];
311 pool_put (am->acls, a);
312 clib_mem_set_heap (oldheap);
316 /* Some aids in ASCII graphing the content */
322 u8 ip4_5tuple_mask[] =
323 _(" dmac smac etype ")
324 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
331 _(" ttl pr checksum ")
340 _("L4 T/U sport dport ")
350 u8 ip6_5tuple_mask[] =
351 _(" dmac smac etype ")
352 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
354 _(0x0000) __ __ __ __
356 _(0x0004) __ __ XX __
358 _(0x0008) XX XX XX XX
359 _(0x000C) XX XX XX XX
360 _(0x0010) XX XX XX XX
361 _(0x0014) XX XX XX XX
363 _(0x0018) XX XX XX XX
364 _(0x001C) XX XX XX XX
365 _(0x0020) XX XX XX XX
366 _(0x0024) XX XX XX XX
367 _("L4T/U sport dport ")
368 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
375 static int count_skip (u8 * p, u32 size)
377 u64 *p64 = (u64 *) p;
378 /* Be tolerant to null pointer */
382 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
386 return (p64 - (u64 *) p) / 2;
390 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
391 u32 mask_len, u32 next_table_index,
392 u32 miss_next_index, u32 * table_index,
396 u32 memory_size = 2 << 13;
397 u32 skip = count_skip (mask, mask_len);
398 u32 match = (mask_len / 16) - skip;
399 u8 *skip_mask_ptr = mask + 16 * skip;
400 u32 current_data_flag = 0;
401 int current_data_offset = 0;
405 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
406 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
407 memory_size, skip, match,
408 next_table_index, miss_next_index,
409 table_index, current_data_flag,
410 current_data_offset, is_add,
411 1 /* delete_chain */);
412 clib_mem_set_heap (oldheap);
417 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
418 u32 mask_len, u32 next_table_index,
419 u32 miss_next_index, u32 * table_index,
423 u32 memory_size = 2 << 20;
424 u32 skip = count_skip (mask, mask_len);
425 u32 match = (mask_len / 16) - skip;
426 u8 *skip_mask_ptr = mask + 16 * skip;
427 u32 current_data_flag = 0;
428 int current_data_offset = 0;
433 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
434 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
435 memory_size, skip, match,
436 next_table_index, miss_next_index,
437 table_index, current_data_flag,
438 current_data_offset, is_add,
439 1 /* delete_chain */);
440 clib_mem_set_heap (oldheap);
446 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
448 vnet_classify_main_t *cm = &vnet_classify_main;
449 u32 ip4_table_index = ~0;
450 u32 ip6_table_index = ~0;
451 void *oldheap = acl_set_heap(am);
453 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
455 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
458 /* switch to global heap while calling vnet_* functions */
459 clib_mem_set_heap (cm->vlib_main->heap_base);
460 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
462 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
465 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
466 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
467 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
468 sizeof (ip4_5tuple_mask) - 1, ~0,
469 am->l2_input_classify_next_acl_ip4,
470 &ip4_table_index, 0);
472 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
475 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
476 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
477 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
478 sizeof (ip6_5tuple_mask) - 1, ~0,
479 am->l2_input_classify_next_acl_ip6,
480 &ip6_table_index, 0);
482 clib_mem_set_heap (oldheap);
487 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
489 vnet_classify_main_t *cm = &vnet_classify_main;
490 u32 ip4_table_index = ~0;
491 u32 ip6_table_index = ~0;
492 void *oldheap = acl_set_heap(am);
494 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
496 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
499 /* switch to global heap while calling vnet_* functions */
500 clib_mem_set_heap (cm->vlib_main->heap_base);
502 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
504 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
507 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
508 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
509 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
510 sizeof (ip4_5tuple_mask) - 1, ~0,
511 am->l2_output_classify_next_acl_ip4,
512 &ip4_table_index, 0);
514 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
517 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
518 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
519 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
520 sizeof (ip6_5tuple_mask) - 1, ~0,
521 am->l2_output_classify_next_acl_ip6,
522 &ip6_table_index, 0);
524 clib_mem_set_heap (oldheap);
529 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
531 vnet_classify_main_t *cm = &vnet_classify_main;
532 u32 ip4_table_index = ~0;
533 u32 ip6_table_index = ~0;
536 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
538 /* in case there were previous tables attached */
539 acl_unhook_l2_input_classify (am, sw_if_index);
541 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
542 sizeof (ip4_5tuple_mask) - 1, ~0,
543 am->l2_input_classify_next_acl_ip4,
544 &ip4_table_index, 1);
548 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
549 sizeof (ip6_5tuple_mask) - 1, ~0,
550 am->l2_input_classify_next_acl_ip6,
551 &ip6_table_index, 1);
554 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
555 sizeof (ip4_5tuple_mask) - 1, ~0,
556 am->l2_input_classify_next_acl_ip4,
557 &ip4_table_index, 0);
561 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
562 ip6_table_index, ~0);
564 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
565 sw_if_index, ip4_table_index, ip6_table_index);
568 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
569 sizeof (ip6_5tuple_mask) - 1, ~0,
570 am->l2_input_classify_next_acl_ip6,
571 &ip6_table_index, 0);
572 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
573 sizeof (ip4_5tuple_mask) - 1, ~0,
574 am->l2_input_classify_next_acl_ip4,
575 &ip4_table_index, 0);
579 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
581 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
584 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
586 clib_mem_set_heap (prevheap);
591 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
593 vnet_classify_main_t *cm = &vnet_classify_main;
594 u32 ip4_table_index = ~0;
595 u32 ip6_table_index = ~0;
598 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
600 /* in case there were previous tables attached */
601 acl_unhook_l2_output_classify (am, sw_if_index);
603 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
604 sizeof (ip4_5tuple_mask) - 1, ~0,
605 am->l2_output_classify_next_acl_ip4,
606 &ip4_table_index, 1);
610 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
611 sizeof (ip6_5tuple_mask) - 1, ~0,
612 am->l2_output_classify_next_acl_ip6,
613 &ip6_table_index, 1);
616 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
617 sizeof (ip4_5tuple_mask) - 1, ~0,
618 am->l2_output_classify_next_acl_ip4,
619 &ip4_table_index, 0);
623 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
624 ip6_table_index, ~0);
626 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
627 sw_if_index, ip4_table_index, ip6_table_index);
630 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
631 sizeof (ip6_5tuple_mask) - 1, ~0,
632 am->l2_output_classify_next_acl_ip6,
633 &ip6_table_index, 0);
634 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
635 sizeof (ip4_5tuple_mask) - 1, ~0,
636 am->l2_output_classify_next_acl_ip4,
637 &ip4_table_index, 0);
641 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
643 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
646 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
648 clib_mem_set_heap (prevheap);
655 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
661 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
663 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
665 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
669 rv = acl_hook_l2_input_classify (am, sw_if_index);
673 rv = acl_unhook_l2_input_classify (am, sw_if_index);
680 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
686 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
688 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
690 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
694 rv = acl_hook_l2_output_classify (am, sw_if_index);
698 rv = acl_unhook_l2_output_classify (am, sw_if_index);
705 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
707 return (pool_is_free_index (am->acls, acl_list_index));
712 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
714 acl_main_t *am = &acl_main;
715 if (acl_is_not_defined(am, acl_list_index)) {
716 /* ACL is not defined. Can not apply */
719 void *oldheap = acl_set_heap(am);
723 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
725 u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
726 if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
727 clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
728 acl_list_index, sw_if_index, index);
729 /* the entry is already there */
730 clib_mem_set_heap (oldheap);
733 /* if there was no ACL applied before, enable the ACL processing */
734 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
735 acl_interface_in_enable_disable (am, sw_if_index, 1);
737 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
739 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
740 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
745 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
747 u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
748 if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
749 clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
750 acl_list_index, sw_if_index, index);
751 /* the entry is already there */
752 clib_mem_set_heap (oldheap);
755 /* if there was no ACL applied before, enable the ACL processing */
756 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
757 acl_interface_out_enable_disable (am, sw_if_index, 1);
759 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
761 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
762 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
765 clib_mem_set_heap (oldheap);
771 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
773 acl_main_t *am = &acl_main;
776 void *oldheap = acl_set_heap(am);
779 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
780 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
783 if (acl_list_index ==
784 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
786 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
792 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
793 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
794 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
795 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
796 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
800 /* If there is no more ACLs applied on an interface, disable ACL processing */
801 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
803 acl_interface_in_enable_disable (am, sw_if_index, 0);
808 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
810 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
812 if (acl_list_index ==
813 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
815 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
821 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
822 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
823 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
824 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
825 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
829 /* If there is no more ACLs applied on an interface, disable ACL processing */
830 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
832 acl_interface_out_enable_disable (am, sw_if_index, 0);
835 clib_mem_set_heap (oldheap);
840 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
842 acl_main_t *am = &acl_main;
844 void *oldheap = acl_set_heap(am);
847 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
848 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
849 acl_interface_in_enable_disable (am, sw_if_index, 0);
852 for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
853 u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
854 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
855 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
856 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
857 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
858 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
863 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
867 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
868 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
869 acl_interface_out_enable_disable (am, sw_if_index, 0);
872 for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
873 u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
874 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
875 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
876 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
877 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
878 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
883 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
885 clib_mem_set_heap (oldheap);
889 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
893 acl_main_t *am = &acl_main;
894 void *oldheap = acl_set_heap(am);
898 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
901 hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
906 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
908 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
910 clib_mem_set_heap (oldheap);
923 } macip_match_type_t;
926 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
932 for (i = 0; i < vec_len (mv); i++)
934 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
935 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
945 /* Get metric used to sort match types.
946 The more specific and the more often seen - the bigger the metric */
948 match_type_metric (macip_match_type_t * m)
950 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
951 return m->prefix_len + m->is_ipv6 + 10 * m->count;
955 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
957 /* Ascending sort based on the metric values */
958 return match_type_metric (m1) - match_type_metric (m2);
961 /* Get the offset of L3 source within ethernet packet */
963 get_l3_src_offset(int is6)
966 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
968 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
972 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
974 macip_match_type_t *mvec = NULL;
975 macip_match_type_t *mt;
976 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
978 u32 match_type_index;
981 vnet_classify_main_t *cm = &vnet_classify_main;
983 /* Count the number of different types of rules */
984 for (i = 0; i < a->count; i++)
988 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
989 a->rules[i].src_prefixlen,
990 a->rules[i].is_ipv6)))
992 match_type_index = vec_len (mvec);
993 vec_validate (mvec, match_type_index);
994 memcpy (mvec[match_type_index].mac_mask,
995 a->rules[match_type_index].src_mac_mask, 6);
996 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
997 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
998 mvec[match_type_index].table_index = ~0;
1000 mvec[match_type_index].count++;
1002 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1003 vec_sort_with_function (mvec, match_type_compare);
1004 /* Create the classifier tables */
1006 /* First add ARP tables */
1007 vec_foreach (mt, mvec)
1010 int is6 = mt->is_ipv6;
1012 mt->arp_table_index = ~0;
1015 memset (mask, 0, sizeof (mask));
1016 memcpy (&mask[6], mt->mac_mask, 6);
1017 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1018 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1020 for (i = 0; i < (mt->prefix_len / 8); i++)
1021 mask[14 + 14 + i] = 0xff;
1022 if (mt->prefix_len % 8)
1023 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1025 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1026 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1027 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1028 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1030 last_table = mt->arp_table_index;
1033 /* Now add IP[46] tables */
1034 vec_foreach (mt, mvec)
1037 int is6 = mt->is_ipv6;
1038 int l3_src_offs = get_l3_src_offset(is6);
1039 memset (mask, 0, sizeof (mask));
1040 memcpy (&mask[6], mt->mac_mask, 6);
1041 for (i = 0; i < (mt->prefix_len / 8); i++)
1043 mask[l3_src_offs + i] = 0xff;
1045 if (mt->prefix_len % 8)
1047 mask[l3_src_offs + (mt->prefix_len / 8)] =
1048 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1051 * Round-up the number of bytes needed to store the prefix,
1052 * and round up the number of vectors too
1054 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1055 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1056 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1057 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1059 last_table = mt->table_index;
1061 a->ip4_table_index = ~0;
1062 a->ip6_table_index = ~0;
1063 a->l2_table_index = last_table;
1065 /* Populate the classifier tables with rules from the MACIP ACL */
1066 for (i = 0; i < a->count; i++)
1070 int is6 = a->rules[i].is_ipv6;
1071 int l3_src_offs = get_l3_src_offset(is6);
1072 memset (mask, 0, sizeof (mask));
1073 memcpy (&mask[6], a->rules[i].src_mac, 6);
1074 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1077 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1083 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1088 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1089 a->rules[i].src_prefixlen,
1090 a->rules[i].is_ipv6);
1091 ASSERT(match_type_index != ~0);
1092 /* add session to table mvec[match_type_index].table_index; */
1093 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1094 mask, a->rules[i].is_permit ? ~0 : 0, i,
1095 0, action, metadata, 1);
1096 /* add ARP table entry too */
1097 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1099 memset (mask, 0, sizeof (mask));
1100 memcpy (&mask[6], a->rules[i].src_mac, 6);
1103 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1104 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1105 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1106 mask, a->rules[i].is_permit ? ~0 : 0, i,
1107 0, action, metadata, 1);
1114 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1116 vnet_classify_main_t *cm = &vnet_classify_main;
1117 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1119 if (a->ip4_table_index != ~0)
1121 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1122 a->ip4_table_index = ~0;
1124 if (a->ip6_table_index != ~0)
1126 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1127 a->ip6_table_index = ~0;
1129 if (a->l2_table_index != ~0)
1131 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1132 a->l2_table_index = ~0;
1137 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1138 u32 * acl_list_index, u8 * tag)
1140 acl_main_t *am = &acl_main;
1141 macip_acl_list_t *a;
1142 macip_acl_rule_t *r;
1143 macip_acl_rule_t *acl_new_rules = 0;
1146 clib_warning("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)", tag);
1148 void *oldheap = acl_set_heap(am);
1149 /* Create and populate the rules */
1151 vec_validate(acl_new_rules, count-1);
1153 for (i = 0; i < count; i++)
1155 r = &acl_new_rules[i];
1156 r->is_permit = rules[i].is_permit;
1157 r->is_ipv6 = rules[i].is_ipv6;
1158 memcpy (&r->src_mac, rules[i].src_mac, 6);
1159 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1160 if(rules[i].is_ipv6)
1161 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1163 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1164 r->src_prefixlen = rules[i].src_ip_prefix_len;
1168 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1169 memset (a, 0, sizeof (*a));
1170 /* Will return the newly allocated ACL index */
1171 *acl_list_index = a - am->macip_acls;
1173 a->rules = acl_new_rules;
1175 memcpy (a->tag, tag, sizeof (a->tag));
1177 /* Create and populate the classifer tables */
1178 macip_create_classify_tables (am, *acl_list_index);
1179 clib_mem_set_heap (oldheap);
1184 /* No check for validity of sw_if_index - the callers were supposed to validate */
1187 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1190 u32 macip_acl_index;
1191 macip_acl_list_t *a;
1192 void *oldheap = acl_set_heap(am);
1193 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1194 clib_mem_set_heap (oldheap);
1195 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1196 /* No point in deleting MACIP ACL which is not applied */
1197 if (~0 == macip_acl_index)
1199 a = &am->macip_acls[macip_acl_index];
1200 /* remove the classifier tables off the interface L2 ACL */
1202 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1203 a->ip6_table_index, a->l2_table_index, 0);
1204 /* Unset the MACIP ACL index */
1205 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1209 /* No check for validity of sw_if_index - the callers were supposed to validate */
1212 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1213 u32 macip_acl_index)
1215 macip_acl_list_t *a;
1217 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1221 void *oldheap = acl_set_heap(am);
1222 a = &am->macip_acls[macip_acl_index];
1223 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1224 /* If there already a MACIP ACL applied, unapply it */
1225 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1226 macip_acl_interface_del_acl(am, sw_if_index);
1227 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1228 clib_mem_set_heap (oldheap);
1230 /* Apply the classifier tables for L2 ACLs */
1232 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1233 a->ip6_table_index, a->l2_table_index, 1);
1238 macip_acl_del_list (u32 acl_list_index)
1240 acl_main_t *am = &acl_main;
1241 void *oldheap = acl_set_heap(am);
1242 macip_acl_list_t *a;
1244 if (pool_is_free_index (am->macip_acls, acl_list_index))
1249 /* delete any references to the ACL */
1250 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1252 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1254 macip_acl_interface_del_acl (am, i);
1258 /* Now that classifier tables are detached, clean them up */
1259 macip_destroy_classify_tables (am, acl_list_index);
1261 /* now we can delete the ACL itself */
1262 a = &am->macip_acls[acl_list_index];
1265 vec_free (a->rules);
1267 pool_put (am->macip_acls, a);
1268 clib_mem_set_heap (oldheap);
1274 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1277 acl_main_t *am = &acl_main;
1278 void *oldheap = acl_set_heap(am);
1282 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1286 rv = macip_acl_interface_del_acl (am, sw_if_index);
1288 clib_mem_set_heap (oldheap);
1293 * If the client does not allocate enough memory for a variable-length
1294 * message, and then proceed to use it as if the full memory allocated,
1295 * absent the check we happily consume that on the VPP side, and go
1296 * along as if nothing happened. However, the resulting
1297 * effects range from just garbage in the API decode
1298 * (because the decoder snoops too far), to potential memory
1301 * This verifies that the actual length of the message is
1302 * at least expected_len, and complains loudly if it is not.
1304 * A failing check here is 100% a software bug on the API user side,
1305 * so we might as well yell.
1308 static int verify_message_len(void *mp, u32 expected_len, char *where)
1310 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1311 if (supplied_len < expected_len) {
1312 clib_warning("%s: Supplied message length %d is less than expected %d",
1313 where, supplied_len, expected_len);
1320 /* API message handler */
1322 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1324 vl_api_acl_add_replace_reply_t *rmp;
1325 acl_main_t *am = &acl_main;
1327 u32 acl_list_index = ntohl (mp->acl_index);
1328 u32 acl_count = ntohl (mp->count);
1329 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1331 if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1332 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1334 rv = VNET_API_ERROR_INVALID_VALUE;
1338 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1340 rmp->acl_index = htonl(acl_list_index);
1346 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1348 acl_main_t *am = &acl_main;
1349 vl_api_acl_del_reply_t *rmp;
1352 rv = acl_del_list (ntohl (mp->acl_index));
1354 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1358 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1360 acl_main_t *am = &acl_main;
1361 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1362 u32 sw_if_index = ntohl (mp->sw_if_index);
1363 vl_api_acl_interface_add_del_reply_t *rmp;
1366 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1367 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1370 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1371 mp->is_input, ntohl (mp->acl_index));
1373 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1377 vl_api_acl_interface_set_acl_list_t_handler
1378 (vl_api_acl_interface_set_acl_list_t * mp)
1380 acl_main_t *am = &acl_main;
1381 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1384 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1385 u32 sw_if_index = ntohl (mp->sw_if_index);
1387 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1388 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1391 acl_interface_reset_inout_acls (sw_if_index, 0);
1392 acl_interface_reset_inout_acls (sw_if_index, 1);
1394 for (i = 0; i < mp->count; i++)
1396 if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1397 /* ACL does not exist, so we can not apply it */
1402 for (i = 0; i < mp->count; i++)
1404 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1405 ntohl (mp->acls[i]));
1410 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1414 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1416 api_rule->is_permit = r->is_permit;
1417 api_rule->is_ipv6 = r->is_ipv6;
1420 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1421 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1425 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1426 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1428 api_rule->src_ip_prefix_len = r->src_prefixlen;
1429 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1430 api_rule->proto = r->proto;
1431 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1432 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1433 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1434 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1435 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1436 api_rule->tcp_flags_value = r->tcp_flags_value;
1440 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1441 acl_list_t * acl, u32 context)
1443 vl_api_acl_details_t *mp;
1444 vl_api_acl_rule_t *rules;
1446 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1447 void *oldheap = acl_set_heap(am);
1449 mp = vl_msg_api_alloc (msg_size);
1450 memset (mp, 0, msg_size);
1451 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1453 /* fill in the message */
1454 mp->context = context;
1455 mp->count = htonl (acl->count);
1456 mp->acl_index = htonl (acl - am->acls);
1457 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1458 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1460 for (i = 0; i < acl->count; i++)
1462 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1465 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1466 clib_mem_set_heap (oldheap);
1467 vl_msg_api_send_shmem (q, (u8 *) & mp);
1472 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1474 acl_main_t *am = &acl_main;
1479 unix_shared_memory_queue_t *q;
1481 q = vl_api_client_index_to_input_queue (mp->client_index);
1487 if (mp->acl_index == ~0)
1490 /* Just dump all ACLs */
1491 pool_foreach (acl, am->acls,
1493 send_acl_details(am, q, acl, mp->context);
1499 acl_index = ntohl (mp->acl_index);
1500 if (!pool_is_free_index (am->acls, acl_index))
1502 acl = &am->acls[acl_index];
1503 send_acl_details (am, q, acl, mp->context);
1509 /* FIXME API: should we signal an error here at all ? */
1515 send_acl_interface_list_details (acl_main_t * am,
1516 unix_shared_memory_queue_t * q,
1517 u32 sw_if_index, u32 context)
1519 vl_api_acl_interface_list_details_t *mp;
1525 void *oldheap = acl_set_heap(am);
1527 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1528 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1530 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1531 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1532 count = n_input + n_output;
1534 msg_size = sizeof (*mp);
1535 msg_size += sizeof (mp->acls[0]) * count;
1537 mp = vl_msg_api_alloc (msg_size);
1538 memset (mp, 0, msg_size);
1540 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1542 /* fill in the message */
1543 mp->context = context;
1544 mp->sw_if_index = htonl (sw_if_index);
1546 mp->n_input = n_input;
1547 for (i = 0; i < n_input; i++)
1549 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1551 for (i = 0; i < n_output; i++)
1553 mp->acls[n_input + i] =
1554 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1556 clib_mem_set_heap (oldheap);
1557 vl_msg_api_send_shmem (q, (u8 *) & mp);
1561 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1564 acl_main_t *am = &acl_main;
1565 vnet_sw_interface_t *swif;
1566 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1569 unix_shared_memory_queue_t *q;
1571 q = vl_api_client_index_to_input_queue (mp->client_index);
1577 if (mp->sw_if_index == ~0)
1580 pool_foreach (swif, im->sw_interfaces,
1582 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1588 sw_if_index = ntohl (mp->sw_if_index);
1589 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1590 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1594 /* MACIP ACL API handlers */
1597 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1599 vl_api_macip_acl_add_reply_t *rmp;
1600 acl_main_t *am = &acl_main;
1602 u32 acl_list_index = ~0;
1603 u32 acl_count = ntohl (mp->count);
1604 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1606 if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1607 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1609 rv = VNET_API_ERROR_INVALID_VALUE;
1613 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1615 rmp->acl_index = htonl(acl_list_index);
1621 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1623 acl_main_t *am = &acl_main;
1624 vl_api_macip_acl_del_reply_t *rmp;
1627 rv = macip_acl_del_list (ntohl (mp->acl_index));
1629 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1633 vl_api_macip_acl_interface_add_del_t_handler
1634 (vl_api_macip_acl_interface_add_del_t * mp)
1636 acl_main_t *am = &acl_main;
1637 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1639 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1640 u32 sw_if_index = ntohl (mp->sw_if_index);
1642 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1643 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1646 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1647 ntohl (mp->acl_index));
1649 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1653 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1654 macip_acl_list_t * acl, u32 context)
1656 vl_api_macip_acl_details_t *mp;
1657 vl_api_macip_acl_rule_t *rules;
1658 macip_acl_rule_t *r;
1660 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1662 mp = vl_msg_api_alloc (msg_size);
1663 memset (mp, 0, msg_size);
1664 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1666 /* fill in the message */
1667 mp->context = context;
1670 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1671 mp->count = htonl (acl->count);
1672 mp->acl_index = htonl (acl - am->macip_acls);
1674 for (i = 0; i < acl->count; i++)
1677 rules[i].is_permit = r->is_permit;
1678 rules[i].is_ipv6 = r->is_ipv6;
1679 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1680 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1681 sizeof (r->src_mac_mask));
1683 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1684 sizeof (r->src_ip_addr.ip6));
1686 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1687 sizeof (r->src_ip_addr.ip4));
1688 rules[i].src_ip_prefix_len = r->src_prefixlen;
1693 /* No martini, no party - no ACL applied to this interface. */
1698 vl_msg_api_send_shmem (q, (u8 *) & mp);
1703 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1705 acl_main_t *am = &acl_main;
1706 macip_acl_list_t *acl;
1708 unix_shared_memory_queue_t *q;
1710 q = vl_api_client_index_to_input_queue (mp->client_index);
1716 if (mp->acl_index == ~0)
1718 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1719 pool_foreach (acl, am->macip_acls, (
1721 send_macip_acl_details (am, q, acl,
1729 u32 acl_index = ntohl (mp->acl_index);
1730 if (!pool_is_free_index (am->macip_acls, acl_index))
1732 acl = &am->macip_acls[acl_index];
1733 send_macip_acl_details (am, q, acl, mp->context);
1739 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1742 acl_main_t *am = &acl_main;
1743 vl_api_macip_acl_interface_get_reply_t *rmp;
1744 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1745 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1746 unix_shared_memory_queue_t *q;
1749 q = vl_api_client_index_to_input_queue (mp->client_index);
1755 rmp = vl_msg_api_alloc (msg_size);
1756 memset (rmp, 0, msg_size);
1758 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1759 rmp->context = mp->context;
1760 rmp->count = htonl (count);
1761 for (i = 0; i < count; i++)
1763 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1766 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1769 /* Set up the API message handling tables */
1770 static clib_error_t *
1771 acl_plugin_api_hookup (vlib_main_t * vm)
1773 acl_main_t *am = &acl_main;
1775 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1777 vl_api_##n##_t_handler, \
1779 vl_api_##n##_t_endian, \
1780 vl_api_##n##_t_print, \
1781 sizeof(vl_api_##n##_t), 1);
1782 foreach_acl_plugin_api_msg;
1788 #define vl_msg_name_crc_list
1789 #include <acl/acl_all_api_h.h>
1790 #undef vl_msg_name_crc_list
1793 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1795 #define _(id,n,crc) \
1796 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1797 foreach_vl_msg_name_crc_acl;
1802 acl_setup_fa_nodes (void)
1804 vlib_main_t *vm = vlib_get_main ();
1805 acl_main_t *am = &acl_main;
1806 vlib_node_t *n, *n4, *n6;
1808 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1809 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1810 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1813 am->l2_input_classify_next_acl_ip4 =
1814 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1815 am->l2_input_classify_next_acl_ip6 =
1816 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1818 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1819 l2input_get_feat_names (),
1820 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1822 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1823 l2input_get_feat_names (),
1824 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1827 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1828 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1829 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1831 am->l2_output_classify_next_acl_ip4 =
1832 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1833 am->l2_output_classify_next_acl_ip6 =
1834 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1836 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1837 l2output_get_feat_names (),
1838 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1840 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1841 l2output_get_feat_names (),
1842 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1846 acl_set_timeout_sec(int timeout_type, u32 value)
1848 acl_main_t *am = &acl_main;
1849 clib_time_t *ct = &am->vlib_main->clib_time;
1851 if (timeout_type < ACL_N_TIMEOUTS) {
1852 am->session_timeout_sec[timeout_type] = value;
1854 clib_warning("Unknown timeout type %d", timeout_type);
1857 am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1861 acl_set_session_max_entries(u32 value)
1863 acl_main_t *am = &acl_main;
1864 am->fa_conn_table_max_entries = value;
1868 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1870 acl_main_t *am = &acl_main;
1872 if ((eh < 256) && (value < 2))
1874 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1882 static clib_error_t *
1883 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1885 acl_main_t *am = &acl_main;
1887 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1888 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1889 /* also unapply any ACLs in case the users did not do so. */
1890 macip_acl_interface_del_acl(am, sw_if_index);
1891 acl_interface_reset_inout_acls (sw_if_index, 0);
1892 acl_interface_reset_inout_acls (sw_if_index, 1);
1897 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1901 static clib_error_t *
1902 acl_set_aclplugin_fn (vlib_main_t * vm,
1903 unformat_input_t * input,
1904 vlib_cli_command_t * cmd)
1906 clib_error_t *error = 0;
1910 uword memory_size = 0;
1911 acl_main_t *am = &acl_main;
1913 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1914 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1915 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1919 if (unformat (input, "use-hash-acl-matching %u", &val))
1921 am->use_hash_acl_matching = (val !=0);
1924 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1926 am->l4_match_nonfirst_fragment = (val != 0);
1929 if (unformat (input, "heap"))
1931 if (unformat(input, "main"))
1933 if (unformat(input, "validate %u", &val))
1934 acl_plugin_acl_set_validate_heap(am, val);
1935 else if (unformat(input, "trace %u", &val))
1936 acl_plugin_acl_set_trace_heap(am, val);
1939 else if (unformat(input, "hash"))
1941 if (unformat(input, "validate %u", &val))
1942 acl_plugin_hash_acl_set_validate_heap(am, val);
1943 else if (unformat(input, "trace %u", &val))
1944 acl_plugin_hash_acl_set_trace_heap(am, val);
1949 if (unformat (input, "session")) {
1950 if (unformat (input, "table")) {
1951 /* The commands here are for tuning/testing. No user-serviceable parts inside */
1952 if (unformat (input, "max-entries")) {
1953 if (!unformat(input, "%u", &val)) {
1954 error = clib_error_return(0,
1955 "expecting maximum number of entries, got `%U`",
1956 format_unformat_error, input);
1959 acl_set_session_max_entries(val);
1963 if (unformat (input, "hash-table-buckets")) {
1964 if (!unformat(input, "%u", &val)) {
1965 error = clib_error_return(0,
1966 "expecting maximum number of hash table buckets, got `%U`",
1967 format_unformat_error, input);
1970 am->fa_conn_table_hash_num_buckets = val;
1974 if (unformat (input, "hash-table-memory")) {
1975 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1976 error = clib_error_return(0,
1977 "expecting maximum amount of hash table memory, got `%U`",
1978 format_unformat_error, input);
1981 am->fa_conn_table_hash_memory_size = memory_size;
1987 if (unformat (input, "timeout")) {
1988 if (unformat(input, "udp")) {
1989 if(unformat(input, "idle")) {
1990 if (!unformat(input, "%u", &timeout)) {
1991 error = clib_error_return(0,
1992 "expecting timeout value in seconds, got `%U`",
1993 format_unformat_error, input);
1996 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
2001 if (unformat(input, "tcp")) {
2002 if(unformat(input, "idle")) {
2003 if (!unformat(input, "%u", &timeout)) {
2004 error = clib_error_return(0,
2005 "expecting timeout value in seconds, got `%U`",
2006 format_unformat_error, input);
2009 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
2013 if(unformat(input, "transient")) {
2014 if (!unformat(input, "%u", &timeout)) {
2015 error = clib_error_return(0,
2016 "expecting timeout value in seconds, got `%U`",
2017 format_unformat_error, input);
2020 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
2032 static clib_error_t *
2033 acl_show_aclplugin_fn (vlib_main_t * vm,
2034 unformat_input_t * input,
2035 vlib_cli_command_t * cmd)
2037 clib_error_t *error = 0;
2038 acl_main_t *am = &acl_main;
2039 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2042 vnet_sw_interface_t *swif;
2044 if (unformat (input, "sessions"))
2046 u8 * out0 = format(0, "");
2048 u32 show_bihash_verbose = 0;
2049 u32 show_session_thread_id = ~0;
2050 u32 show_session_session_index = ~0;
2051 unformat (input, "thread %u index %u", &show_session_thread_id, &show_session_session_index);
2052 unformat (input, "verbose %u", &show_bihash_verbose);
2054 u64 n_adds = am->fa_session_total_adds;
2055 u64 n_dels = am->fa_session_total_dels;
2056 out0 = format(out0, "Sessions total: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
2058 out0 = format(out0, "\n\nPer-thread data:\n");
2059 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2060 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2061 out0 = format(out0, "Thread #%d:\n", wk);
2062 if (show_session_thread_id == wk && show_session_session_index < pool_len(pw->fa_sessions_pool)) {
2063 out0 = format(out0, " session index %u:\n", show_session_session_index);
2064 fa_session_t *sess = pw->fa_sessions_pool + show_session_session_index;
2065 u64 *m = (u64 *)&sess->info;
2066 out0 = format(out0, " info: %016llx %016llx %016llx %016llx %016llx %016llx\n", m[0], m[1], m[2], m[3], m[4], m[5]);
2067 out0 = format(out0, " sw_if_index: %u\n", sess->sw_if_index);
2068 out0 = format(out0, " tcp_flags_seen: %x\n", sess->tcp_flags_seen.as_u16);
2069 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2070 out0 = format(out0, " thread index: %u\n", sess->thread_index);
2071 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2072 out0 = format(out0, " link next index: %u\n", sess->link_next_idx);
2073 out0 = format(out0, " link prev index: %u\n", sess->link_prev_idx);
2074 out0 = format(out0, " link list id: %u\n", sess->link_list_id);
2076 out0 = format(out0, " connection add/del stats:\n", wk);
2077 pool_foreach (swif, im->sw_interfaces,
2079 u32 sw_if_index = swif->sw_if_index;
2080 u64 n_adds = sw_if_index < vec_len(pw->fa_session_adds_by_sw_if_index) ? pw->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
2081 u64 n_dels = sw_if_index < vec_len(pw->fa_session_dels_by_sw_if_index) ? pw->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
2082 out0 = format(out0, " sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2085 out0 = format(out0, " connection timeout type lists:\n", wk);
2087 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2088 u32 head_session_index = pw->fa_conn_list_head[tt];
2089 out0 = format(out0, " fa_conn_list_head[%d]: %d\n", tt, head_session_index);
2090 if (~0 != head_session_index) {
2091 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2092 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2093 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2097 out0 = format(out0, " Next expiry time: %lu\n", pw->next_expiry_time);
2098 out0 = format(out0, " Requeue until time: %lu\n", pw->requeue_until_time);
2099 out0 = format(out0, " Current time wait interval: %lu\n", pw->current_time_wait_interval);
2100 out0 = format(out0, " Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
2101 out0 = format(out0, " Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
2102 out0 = format(out0, " Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
2103 out0 = format(out0, " Swipe until this time: %lu\n", pw->swipe_end_time);
2104 out0 = format(out0, " sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2105 out0 = format(out0, " pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
2106 out0 = format(out0, " clear in progress: %u\n", pw->clear_in_process);
2107 out0 = format(out0, " interrupt is pending: %d\n", pw->interrupt_is_pending);
2108 out0 = format(out0, " interrupt is needed: %d\n", pw->interrupt_is_needed);
2109 out0 = format(out0, " interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
2111 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
2112 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
2113 foreach_fa_cleaner_counter;
2115 vec_terminate_c_string(out0);
2116 vlib_cli_output(vm, "\n\n%s\n\n", out0);
2117 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2118 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2119 am->fa_cleaner_wait_time_increment * 1000.0, ((f64)am->fa_current_cleaner_timer_wait_interval) * 1000.0/(f64)vm->clib_time.clocks_per_second);
2122 show_fa_sessions_hash(vm, show_bihash_verbose);
2124 else if (unformat (input, "interface"))
2126 u32 sw_if_index = ~0;
2128 u8 * out0 = format(0, "");
2129 unformat (input, "sw_if_index %u", &sw_if_index);
2130 for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
2131 (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
2132 out0 = format(out0, "sw_if_index %d:\n", swi);
2134 if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
2135 (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
2136 out0 = format(out0, " input acl(s): ");
2137 vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
2138 out0 = format(out0, "%d ", *pj);
2140 out0 = format(out0, "\n");
2143 if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
2144 (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2145 out0 = format(out0, " output acl(s): ");
2146 vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2147 out0 = format(out0, "%d ", *pj);
2149 out0 = format(out0, "\n");
2153 vec_terminate_c_string(out0);
2154 vlib_cli_output(vm, "\n%s\n", out0);
2157 else if (unformat (input, "acl"))
2161 u8 * out0 = format(0, "");
2162 unformat (input, "index %u", &acl_index);
2163 for(i=0; i<vec_len(am->acls); i++) {
2164 if (acl_is_not_defined(am, i)) {
2165 /* don't attempt to show the ACLs that do not exist */
2168 if ((acl_index != ~0) && (acl_index != i)) {
2171 out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2174 for(j=0; j<am->acls[i].count; j++) {
2175 r = &am->acls[i].rules[j];
2176 out0 = format(out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2177 out0 = format_acl_action(out0, r->is_permit);
2178 out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2179 out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2180 out0 = format(out0, " proto %d", r->proto);
2181 out0 = format(out0, " sport %d", r->src_port_or_type_first);
2182 if (r->src_port_or_type_first != r->src_port_or_type_last) {
2183 out0 = format(out0, "-%d", r->src_port_or_type_last);
2185 out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2186 if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2187 out0 = format(out0, "-%d", r->dst_port_or_code_last);
2189 if (r->tcp_flags_mask || r->tcp_flags_value) {
2190 out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2192 out0 = format(out0, "\n");
2195 if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2196 out0 = format(out0, " applied inbound on sw_if_index: ");
2197 vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2198 out0 = format(out0, "%d ", *pj);
2200 out0 = format(out0, "\n");
2202 if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2203 out0 = format(out0, " applied outbound on sw_if_index: ");
2204 vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2205 out0 = format(out0, "%d ", *pj);
2207 out0 = format(out0, "\n");
2210 vec_terminate_c_string(out0);
2211 vlib_cli_output(vm, "\n%s\n", out0);
2214 else if (unformat (input, "memory"))
2216 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2217 if (am->acl_mheap) {
2218 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2220 vlib_cli_output (vm, " Not initialized\n");
2222 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2223 if (am->hash_lookup_mheap) {
2224 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2226 vlib_cli_output (vm, " Not initialized\n");
2229 else if (unformat (input, "tables"))
2231 ace_mask_type_entry_t *mte;
2233 u32 sw_if_index = ~0;
2234 int show_acl_hash_info = 0;
2235 int show_applied_info = 0;
2236 int show_mask_type = 0;
2237 int show_bihash = 0;
2238 u32 show_bihash_verbose = 0;
2240 if (unformat (input, "acl")) {
2241 show_acl_hash_info = 1;
2242 /* mask-type is handy to see as well right there */
2244 unformat (input, "index %u", &acl_index);
2245 } else if (unformat (input, "applied")) {
2246 show_applied_info = 1;
2247 unformat (input, "sw_if_index %u", &sw_if_index);
2248 } else if (unformat (input, "mask")) {
2250 } else if (unformat (input, "hash")) {
2252 unformat (input, "verbose %u", &show_bihash_verbose);
2255 if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2256 /* if no qualifiers specified, show all */
2258 show_acl_hash_info = 1;
2259 show_applied_info = 1;
2263 if (show_mask_type) {
2264 vlib_cli_output(vm, "Mask-type entries:");
2266 pool_foreach(mte, am->ace_mask_type_pool,
2268 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
2269 mte - am->ace_mask_type_pool,
2270 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2271 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2276 if (show_acl_hash_info) {
2278 u8 * out0 = format(0, "");
2280 out0 = format(out0, "Mask-ready ACL representations\n");
2281 for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2282 if ((acl_index != ~0) && (acl_index != i)) {
2285 hash_acl_info_t *ha = &am->hash_acl_infos[i];
2286 out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2287 out0 = format(out0, " mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2288 for(j=0; j<vec_len(ha->rules); j++) {
2289 hash_ace_info_t *pa = &ha->rules[j];
2290 m = (u64 *)&pa->match;
2291 out0 = format(out0, " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
2292 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2293 pa->acl_index, pa->ace_index, pa->action,
2294 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2297 vec_terminate_c_string(out0);
2298 vlib_cli_output(vm, "\n%s\n", out0);
2302 if (show_applied_info) {
2304 u8 * out0 = format(0, "");
2305 out0 = format(out0, "Applied lookup entries for interfaces\n");
2307 for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2308 (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2309 (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2310 (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2311 if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2314 out0 = format(out0, "sw_if_index %d:\n", swi);
2315 if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2316 applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2317 out0 = format(out0, " input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2318 out0 = format(out0, " input applied acls: %U\n", format_vec32, pal->applied_acls, "%d");
2320 if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2321 out0 = format(out0, " input lookup applied entries:\n");
2322 for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2323 applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2324 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2325 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2326 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2330 if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2331 applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2332 out0 = format(out0, " output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2333 out0 = format(out0, " output applied acls: %U\n", format_vec32, pal->applied_acls, "%d");
2335 if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2336 out0 = format(out0, " output lookup applied entries:\n");
2337 for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2338 applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2339 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2340 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2341 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2346 vec_terminate_c_string(out0);
2347 vlib_cli_output(vm, "\n%s\n", out0);
2352 show_hash_acl_hash(vm, am, show_bihash_verbose);
2358 static clib_error_t *
2359 acl_clear_aclplugin_fn (vlib_main_t * vm,
2360 unformat_input_t * input,
2361 vlib_cli_command_t * cmd)
2363 clib_error_t *error = 0;
2364 acl_main_t *am = &acl_main;
2365 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2366 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2371 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2372 .path = "set acl-plugin",
2373 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2374 .function = acl_set_aclplugin_fn,
2377 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2378 .path = "show acl-plugin",
2379 .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2380 .function = acl_show_aclplugin_fn,
2383 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
2384 .path = "clear acl-plugin sessions",
2385 .short_help = "clear acl-plugin sessions",
2386 .function = acl_clear_aclplugin_fn,
2392 static clib_error_t *
2393 acl_init (vlib_main_t * vm)
2395 acl_main_t *am = &acl_main;
2396 clib_error_t *error = 0;
2397 memset (am, 0, sizeof (*am));
2399 am->vnet_main = vnet_get_main ();
2401 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2403 /* Ask for a correctly-sized block of API message decode slots */
2404 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2405 VL_MSG_FIRST_AVAILABLE);
2407 error = acl_plugin_api_hookup (vm);
2409 /* Add our API messages to the global name_crc hash table */
2410 setup_message_id_table (am, &api_main);
2414 acl_setup_fa_nodes();
2416 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2417 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2418 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2420 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2421 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2422 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2423 vlib_thread_main_t *tm = vlib_get_thread_main ();
2424 vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2428 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2429 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2430 vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2431 vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2432 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2433 pw->fa_conn_list_head[tt] = ~0;
2434 pw->fa_conn_list_tail[tt] = ~0;
2439 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2440 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2441 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2443 am->fa_cleaner_cnt_delete_by_sw_index = 0;
2444 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2445 am->fa_cleaner_cnt_unknown_event = 0;
2446 am->fa_cleaner_cnt_timer_restarted = 0;
2447 am->fa_cleaner_cnt_wait_with_timeout = 0;
2450 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2454 am->l4_match_nonfirst_fragment = 1;
2456 /* use the new fancy hash-based matching */
2457 am->use_hash_acl_matching = 1;
2462 VLIB_INIT_FUNCTION (acl_init);