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",
87 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
89 acl_main_t *am = &acl_main;
90 vl_api_acl_plugin_get_version_reply_t *rmp;
91 int msg_size = sizeof (*rmp);
92 unix_shared_memory_queue_t *q;
94 q = vl_api_client_index_to_input_queue (mp->client_index);
100 rmp = vl_msg_api_alloc (msg_size);
101 memset (rmp, 0, msg_size);
103 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
104 rmp->context = mp->context;
105 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
106 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
108 vl_msg_api_send_shmem (q, (u8 *) & rmp);
112 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
114 vl_api_acl_plugin_control_ping_reply_t *rmp;
115 acl_main_t *am = &acl_main;
119 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
121 rmp->vpe_pid = ntohl (getpid ());
127 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
128 u32 * acl_list_index, u8 * tag)
130 acl_main_t *am = &acl_main;
133 acl_rule_t *acl_new_rules;
136 if (*acl_list_index != ~0)
138 /* They supplied some number, let's see if this ACL exists */
139 if (pool_is_free_index (am->acls, *acl_list_index))
141 /* tried to replace a non-existent ACL, no point doing anything */
146 /* Create and populate the rules */
147 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
148 CLIB_CACHE_LINE_BYTES);
151 /* Could not allocate rules. New or existing ACL - bail out regardless */
155 for (i = 0; i < count; i++)
157 r = &acl_new_rules[i];
158 memset(r, 0, sizeof(*r));
159 r->is_permit = rules[i].is_permit;
160 r->is_ipv6 = rules[i].is_ipv6;
163 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
164 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
168 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
169 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
171 r->src_prefixlen = rules[i].src_ip_prefix_len;
172 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
173 r->proto = rules[i].proto;
174 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
175 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
176 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
177 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
178 r->tcp_flags_value = rules[i].tcp_flags_value;
179 r->tcp_flags_mask = rules[i].tcp_flags_mask;
182 if (~0 == *acl_list_index)
185 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
186 memset (a, 0, sizeof (*a));
187 /* Will return the newly allocated ACL index */
188 *acl_list_index = a - am->acls;
192 a = am->acls + *acl_list_index;
193 hash_acl_delete(am, *acl_list_index);
194 /* Get rid of the old rules */
195 clib_mem_free (a->rules);
197 a->rules = acl_new_rules;
199 memcpy (a->tag, tag, sizeof (a->tag));
200 hash_acl_add(am, *acl_list_index);
206 acl_del_list (u32 acl_list_index)
208 acl_main_t *am = &acl_main;
211 if (pool_is_free_index (am->acls, acl_list_index))
216 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
217 if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
218 /* ACL is applied somewhere inbound. Refuse to delete */
222 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
223 if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
224 /* ACL is applied somewhere outbound. Refuse to delete */
229 /* delete any references to the ACL */
230 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
232 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
235 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
237 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
245 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
247 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
250 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
252 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
260 /* delete the hash table data */
262 hash_acl_delete(am, acl_list_index);
263 /* now we can delete the ACL itself */
264 a = &am->acls[acl_list_index];
267 clib_mem_free (a->rules);
269 pool_put (am->acls, a);
273 /* Some aids in ASCII graphing the content */
279 u8 ip4_5tuple_mask[] =
280 _(" dmac smac etype ")
281 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
288 _(" ttl pr checksum ")
297 _("L4 T/U sport dport ")
307 u8 ip6_5tuple_mask[] =
308 _(" dmac smac etype ")
309 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
311 _(0x0000) __ __ __ __
313 _(0x0004) __ __ XX __
315 _(0x0008) XX XX XX XX
316 _(0x000C) XX XX XX XX
317 _(0x0010) XX XX XX XX
318 _(0x0014) XX XX XX XX
320 _(0x0018) XX XX XX XX
321 _(0x001C) XX XX XX XX
322 _(0x0020) XX XX XX XX
323 _(0x0024) XX XX XX XX
324 _("L4T/U sport dport ")
325 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
332 static int count_skip (u8 * p, u32 size)
334 u64 *p64 = (u64 *) p;
335 /* Be tolerant to null pointer */
339 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
343 return (p64 - (u64 *) p) / 2;
347 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
348 u32 mask_len, u32 next_table_index,
349 u32 miss_next_index, u32 * table_index,
353 u32 memory_size = 2 << 13;
354 u32 skip = count_skip (mask, mask_len);
355 u32 match = (mask_len / 16) - skip;
356 u8 *skip_mask_ptr = mask + 16 * skip;
357 u32 current_data_flag = 0;
358 int current_data_offset = 0;
363 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
364 memory_size, skip, match,
365 next_table_index, miss_next_index,
366 table_index, current_data_flag,
367 current_data_offset, is_add,
368 1 /* delete_chain */);
372 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
373 u32 mask_len, u32 next_table_index,
374 u32 miss_next_index, u32 * table_index,
378 u32 memory_size = 2 << 20;
379 u32 skip = count_skip (mask, mask_len);
380 u32 match = (mask_len / 16) - skip;
381 u8 *skip_mask_ptr = mask + 16 * skip;
382 u32 current_data_flag = 0;
383 int current_data_offset = 0;
388 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
389 memory_size, skip, match,
390 next_table_index, miss_next_index,
391 table_index, current_data_flag,
392 current_data_offset, is_add,
393 1 /* delete_chain */);
398 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
400 vnet_classify_main_t *cm = &vnet_classify_main;
401 u32 ip4_table_index = ~0;
402 u32 ip6_table_index = ~0;
404 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
406 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
409 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
411 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
414 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
415 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
416 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
417 sizeof (ip4_5tuple_mask) - 1, ~0,
418 am->l2_input_classify_next_acl_ip4,
419 &ip4_table_index, 0);
421 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
424 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
425 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
426 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
427 sizeof (ip6_5tuple_mask) - 1, ~0,
428 am->l2_input_classify_next_acl_ip6,
429 &ip6_table_index, 0);
436 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
438 vnet_classify_main_t *cm = &vnet_classify_main;
439 u32 ip4_table_index = ~0;
440 u32 ip6_table_index = ~0;
442 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
444 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
447 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
449 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
452 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
453 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
454 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
455 sizeof (ip4_5tuple_mask) - 1, ~0,
456 am->l2_output_classify_next_acl_ip4,
457 &ip4_table_index, 0);
459 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
462 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
463 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
464 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
465 sizeof (ip6_5tuple_mask) - 1, ~0,
466 am->l2_output_classify_next_acl_ip6,
467 &ip6_table_index, 0);
474 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
476 vnet_classify_main_t *cm = &vnet_classify_main;
477 u32 ip4_table_index = ~0;
478 u32 ip6_table_index = ~0;
481 /* in case there were previous tables attached */
482 acl_unhook_l2_input_classify (am, sw_if_index);
484 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
485 sizeof (ip4_5tuple_mask) - 1, ~0,
486 am->l2_input_classify_next_acl_ip4,
487 &ip4_table_index, 1);
491 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
492 sizeof (ip6_5tuple_mask) - 1, ~0,
493 am->l2_input_classify_next_acl_ip6,
494 &ip6_table_index, 1);
497 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
498 sizeof (ip4_5tuple_mask) - 1, ~0,
499 am->l2_input_classify_next_acl_ip4,
500 &ip4_table_index, 0);
504 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
505 ip6_table_index, ~0);
507 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
508 sw_if_index, ip4_table_index, ip6_table_index);
511 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
512 sizeof (ip6_5tuple_mask) - 1, ~0,
513 am->l2_input_classify_next_acl_ip6,
514 &ip6_table_index, 0);
515 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
516 sizeof (ip4_5tuple_mask) - 1, ~0,
517 am->l2_input_classify_next_acl_ip4,
518 &ip4_table_index, 0);
522 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
524 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
527 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
532 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
534 vnet_classify_main_t *cm = &vnet_classify_main;
535 u32 ip4_table_index = ~0;
536 u32 ip6_table_index = ~0;
539 /* in case there were previous tables attached */
540 acl_unhook_l2_output_classify (am, sw_if_index);
542 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
543 sizeof (ip4_5tuple_mask) - 1, ~0,
544 am->l2_output_classify_next_acl_ip4,
545 &ip4_table_index, 1);
549 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
550 sizeof (ip6_5tuple_mask) - 1, ~0,
551 am->l2_output_classify_next_acl_ip6,
552 &ip6_table_index, 1);
555 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
556 sizeof (ip4_5tuple_mask) - 1, ~0,
557 am->l2_output_classify_next_acl_ip4,
558 &ip4_table_index, 0);
562 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
563 ip6_table_index, ~0);
565 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
566 sw_if_index, ip4_table_index, ip6_table_index);
569 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
570 sizeof (ip6_5tuple_mask) - 1, ~0,
571 am->l2_output_classify_next_acl_ip6,
572 &ip6_table_index, 0);
573 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
574 sizeof (ip4_5tuple_mask) - 1, ~0,
575 am->l2_output_classify_next_acl_ip4,
576 &ip4_table_index, 0);
580 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
582 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
585 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
592 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
598 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
600 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
602 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
606 rv = acl_hook_l2_input_classify (am, sw_if_index);
610 rv = acl_unhook_l2_input_classify (am, sw_if_index);
617 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
623 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
625 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
627 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
631 rv = acl_hook_l2_output_classify (am, sw_if_index);
635 rv = acl_unhook_l2_output_classify (am, sw_if_index);
642 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
644 return (pool_is_free_index (am->acls, acl_list_index));
649 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
651 acl_main_t *am = &acl_main;
652 if (acl_is_not_defined(am, acl_list_index)) {
653 /* ACL is not defined. Can not apply */
659 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
661 u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
662 if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
663 clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
664 acl_list_index, sw_if_index, index);
665 /* the entry is already there */
668 /* if there was no ACL applied before, enable the ACL processing */
669 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
670 acl_interface_in_enable_disable (am, sw_if_index, 1);
672 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
674 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
675 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
680 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
682 u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
683 if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
684 clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
685 acl_list_index, sw_if_index, index);
686 /* the entry is already there */
689 /* if there was no ACL applied before, enable the ACL processing */
690 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
691 acl_interface_out_enable_disable (am, sw_if_index, 1);
693 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
695 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
696 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
704 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
706 acl_main_t *am = &acl_main;
711 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
712 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
715 if (acl_list_index ==
716 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
718 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
724 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
725 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
726 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
727 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
728 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
732 /* If there is no more ACLs applied on an interface, disable ACL processing */
733 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
735 acl_interface_in_enable_disable (am, sw_if_index, 0);
740 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
742 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
744 if (acl_list_index ==
745 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
747 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
753 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
754 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
755 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
756 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
757 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
761 /* If there is no more ACLs applied on an interface, disable ACL processing */
762 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
764 acl_interface_out_enable_disable (am, sw_if_index, 0);
771 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
773 acl_main_t *am = &acl_main;
777 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
778 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
779 acl_interface_in_enable_disable (am, sw_if_index, 0);
782 for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
783 u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
784 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
785 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
786 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
787 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
788 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
793 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
797 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
798 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
799 acl_interface_out_enable_disable (am, sw_if_index, 0);
802 for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
803 u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
804 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
805 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
806 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
807 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
808 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
813 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
818 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
822 acl_main_t *am = &acl_main;
826 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
829 hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
834 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
836 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
850 } macip_match_type_t;
853 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
859 for (i = 0; i < vec_len (mv); i++)
861 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
862 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
872 /* Get metric used to sort match types.
873 The more specific and the more often seen - the bigger the metric */
875 match_type_metric (macip_match_type_t * m)
877 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
878 return m->prefix_len + m->is_ipv6 + 10 * m->count;
882 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
884 /* Ascending sort based on the metric values */
885 return match_type_metric (m1) - match_type_metric (m2);
888 /* Get the offset of L3 source within ethernet packet */
890 get_l3_src_offset(int is6)
893 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
895 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
899 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
901 macip_match_type_t *mvec = NULL;
902 macip_match_type_t *mt;
903 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
905 u32 match_type_index;
908 vnet_classify_main_t *cm = &vnet_classify_main;
910 /* Count the number of different types of rules */
911 for (i = 0; i < a->count; i++)
915 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
916 a->rules[i].src_prefixlen,
917 a->rules[i].is_ipv6)))
919 match_type_index = vec_len (mvec);
920 vec_validate (mvec, match_type_index);
921 memcpy (mvec[match_type_index].mac_mask,
922 a->rules[match_type_index].src_mac_mask, 6);
923 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
924 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
925 mvec[match_type_index].table_index = ~0;
927 mvec[match_type_index].count++;
929 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
930 vec_sort_with_function (mvec, match_type_compare);
931 /* Create the classifier tables */
933 /* First add ARP tables */
934 vec_foreach (mt, mvec)
937 int is6 = mt->is_ipv6;
939 mt->arp_table_index = ~0;
942 memset (mask, 0, sizeof (mask));
943 memcpy (&mask[6], mt->mac_mask, 6);
944 memset (&mask[12], 0xff, 2); /* ethernet protocol */
945 memcpy (&mask[14 + 8], mt->mac_mask, 6);
947 for (i = 0; i < (mt->prefix_len / 8); i++)
948 mask[14 + 14 + i] = 0xff;
949 if (mt->prefix_len % 8)
950 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
952 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
953 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
954 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
955 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
957 last_table = mt->arp_table_index;
960 /* Now add IP[46] tables */
961 vec_foreach (mt, mvec)
964 int is6 = mt->is_ipv6;
965 int l3_src_offs = get_l3_src_offset(is6);
966 memset (mask, 0, sizeof (mask));
967 memcpy (&mask[6], mt->mac_mask, 6);
968 for (i = 0; i < (mt->prefix_len / 8); i++)
970 mask[l3_src_offs + i] = 0xff;
972 if (mt->prefix_len % 8)
974 mask[l3_src_offs + (mt->prefix_len / 8)] =
975 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
978 * Round-up the number of bytes needed to store the prefix,
979 * and round up the number of vectors too
981 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
982 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
983 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
984 (~0 == last_table) ? 0 : ~0, &mt->table_index,
986 last_table = mt->table_index;
988 a->ip4_table_index = ~0;
989 a->ip6_table_index = ~0;
990 a->l2_table_index = last_table;
992 /* Populate the classifier tables with rules from the MACIP ACL */
993 for (i = 0; i < a->count; i++)
997 int is6 = a->rules[i].is_ipv6;
998 int l3_src_offs = get_l3_src_offset(is6);
999 memset (mask, 0, sizeof (mask));
1000 memcpy (&mask[6], a->rules[i].src_mac, 6);
1001 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1004 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1010 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1015 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1016 a->rules[i].src_prefixlen,
1017 a->rules[i].is_ipv6);
1018 /* add session to table mvec[match_type_index].table_index; */
1019 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1020 mask, a->rules[i].is_permit ? ~0 : 0, i,
1021 0, action, metadata, 1);
1022 /* add ARP table entry too */
1023 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1025 memset (mask, 0, sizeof (mask));
1026 memcpy (&mask[6], a->rules[i].src_mac, 6);
1029 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1030 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1031 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1032 mask, a->rules[i].is_permit ? ~0 : 0, i,
1033 0, action, metadata, 1);
1040 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1042 vnet_classify_main_t *cm = &vnet_classify_main;
1043 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1045 if (a->ip4_table_index != ~0)
1047 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1048 a->ip4_table_index = ~0;
1050 if (a->ip6_table_index != ~0)
1052 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1053 a->ip6_table_index = ~0;
1055 if (a->l2_table_index != ~0)
1057 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1058 a->l2_table_index = ~0;
1063 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1064 u32 * acl_list_index, u8 * tag)
1066 acl_main_t *am = &acl_main;
1067 macip_acl_list_t *a;
1068 macip_acl_rule_t *r;
1069 macip_acl_rule_t *acl_new_rules;
1072 /* Create and populate the rules */
1073 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1074 CLIB_CACHE_LINE_BYTES);
1077 /* Could not allocate rules. New or existing ACL - bail out regardless */
1081 for (i = 0; i < count; i++)
1083 r = &acl_new_rules[i];
1084 r->is_permit = rules[i].is_permit;
1085 r->is_ipv6 = rules[i].is_ipv6;
1086 memcpy (&r->src_mac, rules[i].src_mac, 6);
1087 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1088 if(rules[i].is_ipv6)
1089 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1091 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1092 r->src_prefixlen = rules[i].src_ip_prefix_len;
1096 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1097 memset (a, 0, sizeof (*a));
1098 /* Will return the newly allocated ACL index */
1099 *acl_list_index = a - am->macip_acls;
1101 a->rules = acl_new_rules;
1103 memcpy (a->tag, tag, sizeof (a->tag));
1105 /* Create and populate the classifer tables */
1106 macip_create_classify_tables (am, *acl_list_index);
1112 /* No check for validity of sw_if_index - the callers were supposed to validate */
1115 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1118 u32 macip_acl_index;
1119 macip_acl_list_t *a;
1120 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1121 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1122 /* No point in deleting MACIP ACL which is not applied */
1123 if (~0 == macip_acl_index)
1125 a = &am->macip_acls[macip_acl_index];
1126 /* remove the classifier tables off the interface L2 ACL */
1128 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1129 a->ip6_table_index, a->l2_table_index, 0);
1130 /* Unset the MACIP ACL index */
1131 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1135 /* No check for validity of sw_if_index - the callers were supposed to validate */
1138 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1139 u32 macip_acl_index)
1141 macip_acl_list_t *a;
1143 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1147 a = &am->macip_acls[macip_acl_index];
1148 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1149 /* If there already a MACIP ACL applied, unapply it */
1150 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1151 macip_acl_interface_del_acl(am, sw_if_index);
1152 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1153 /* Apply the classifier tables for L2 ACLs */
1155 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1156 a->ip6_table_index, a->l2_table_index, 1);
1161 macip_acl_del_list (u32 acl_list_index)
1163 acl_main_t *am = &acl_main;
1164 macip_acl_list_t *a;
1166 if (pool_is_free_index (am->macip_acls, acl_list_index))
1171 /* delete any references to the ACL */
1172 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1174 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1176 macip_acl_interface_del_acl (am, i);
1180 /* Now that classifier tables are detached, clean them up */
1181 macip_destroy_classify_tables (am, acl_list_index);
1183 /* now we can delete the ACL itself */
1184 a = &am->macip_acls[acl_list_index];
1187 clib_mem_free (a->rules);
1189 pool_put (am->macip_acls, a);
1195 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1198 acl_main_t *am = &acl_main;
1202 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1206 rv = macip_acl_interface_del_acl (am, sw_if_index);
1212 * If the client does not allocate enough memory for a variable-length
1213 * message, and then proceed to use it as if the full memory allocated,
1214 * absent the check we happily consume that on the VPP side, and go
1215 * along as if nothing happened. However, the resulting
1216 * effects range from just garbage in the API decode
1217 * (because the decoder snoops too far), to potential memory
1220 * This verifies that the actual length of the message is
1221 * at least expected_len, and complains loudly if it is not.
1223 * A failing check here is 100% a software bug on the API user side,
1224 * so we might as well yell.
1227 static int verify_message_len(void *mp, u32 expected_len, char *where)
1229 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1230 if (supplied_len < expected_len) {
1231 clib_warning("%s: Supplied message length %d is less than expected %d",
1232 where, supplied_len, expected_len);
1239 /* API message handler */
1241 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1243 vl_api_acl_add_replace_reply_t *rmp;
1244 acl_main_t *am = &acl_main;
1246 u32 acl_list_index = ntohl (mp->acl_index);
1247 u32 acl_count = ntohl (mp->count);
1248 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1250 if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1251 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1253 rv = VNET_API_ERROR_INVALID_VALUE;
1257 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1259 rmp->acl_index = htonl(acl_list_index);
1265 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1267 acl_main_t *am = &acl_main;
1268 vl_api_acl_del_reply_t *rmp;
1271 rv = acl_del_list (ntohl (mp->acl_index));
1273 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1277 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1279 acl_main_t *am = &acl_main;
1280 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1281 u32 sw_if_index = ntohl (mp->sw_if_index);
1282 vl_api_acl_interface_add_del_reply_t *rmp;
1285 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1286 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1289 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1290 mp->is_input, ntohl (mp->acl_index));
1292 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1296 vl_api_acl_interface_set_acl_list_t_handler
1297 (vl_api_acl_interface_set_acl_list_t * mp)
1299 acl_main_t *am = &acl_main;
1300 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1303 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1304 u32 sw_if_index = ntohl (mp->sw_if_index);
1306 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1307 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1310 acl_interface_reset_inout_acls (sw_if_index, 0);
1311 acl_interface_reset_inout_acls (sw_if_index, 1);
1313 for (i = 0; i < mp->count; i++)
1315 if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1316 /* ACL does not exist, so we can not apply it */
1321 for (i = 0; i < mp->count; i++)
1323 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1324 ntohl (mp->acls[i]));
1329 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1333 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1335 api_rule->is_permit = r->is_permit;
1336 api_rule->is_ipv6 = r->is_ipv6;
1339 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1340 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1344 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1345 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1347 api_rule->src_ip_prefix_len = r->src_prefixlen;
1348 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1349 api_rule->proto = r->proto;
1350 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1351 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1352 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1353 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1354 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1355 api_rule->tcp_flags_value = r->tcp_flags_value;
1359 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1360 acl_list_t * acl, u32 context)
1362 vl_api_acl_details_t *mp;
1363 vl_api_acl_rule_t *rules;
1365 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1367 mp = vl_msg_api_alloc (msg_size);
1368 memset (mp, 0, msg_size);
1369 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1371 /* fill in the message */
1372 mp->context = context;
1373 mp->count = htonl (acl->count);
1374 mp->acl_index = htonl (acl - am->acls);
1375 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1376 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1378 for (i = 0; i < acl->count; i++)
1380 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1383 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1384 vl_msg_api_send_shmem (q, (u8 *) & mp);
1389 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1391 acl_main_t *am = &acl_main;
1396 unix_shared_memory_queue_t *q;
1398 q = vl_api_client_index_to_input_queue (mp->client_index);
1404 if (mp->acl_index == ~0)
1407 /* Just dump all ACLs */
1408 pool_foreach (acl, am->acls,
1410 send_acl_details(am, q, acl, mp->context);
1416 acl_index = ntohl (mp->acl_index);
1417 if (!pool_is_free_index (am->acls, acl_index))
1419 acl = &am->acls[acl_index];
1420 send_acl_details (am, q, acl, mp->context);
1426 /* FIXME API: should we signal an error here at all ? */
1432 send_acl_interface_list_details (acl_main_t * am,
1433 unix_shared_memory_queue_t * q,
1434 u32 sw_if_index, u32 context)
1436 vl_api_acl_interface_list_details_t *mp;
1443 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1444 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1446 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1447 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1448 count = n_input + n_output;
1450 msg_size = sizeof (*mp);
1451 msg_size += sizeof (mp->acls[0]) * count;
1453 mp = vl_msg_api_alloc (msg_size);
1454 memset (mp, 0, msg_size);
1456 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1458 /* fill in the message */
1459 mp->context = context;
1460 mp->sw_if_index = htonl (sw_if_index);
1462 mp->n_input = n_input;
1463 for (i = 0; i < n_input; i++)
1465 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1467 for (i = 0; i < n_output; i++)
1469 mp->acls[n_input + i] =
1470 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1473 vl_msg_api_send_shmem (q, (u8 *) & mp);
1477 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1480 acl_main_t *am = &acl_main;
1481 vnet_sw_interface_t *swif;
1482 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1485 unix_shared_memory_queue_t *q;
1487 q = vl_api_client_index_to_input_queue (mp->client_index);
1493 if (mp->sw_if_index == ~0)
1496 pool_foreach (swif, im->sw_interfaces,
1498 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1504 sw_if_index = ntohl (mp->sw_if_index);
1505 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1506 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1510 /* MACIP ACL API handlers */
1513 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1515 vl_api_macip_acl_add_reply_t *rmp;
1516 acl_main_t *am = &acl_main;
1518 u32 acl_list_index = ~0;
1519 u32 acl_count = ntohl (mp->count);
1520 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1522 if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1523 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1525 rv = VNET_API_ERROR_INVALID_VALUE;
1529 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1531 rmp->acl_index = htonl(acl_list_index);
1537 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1539 acl_main_t *am = &acl_main;
1540 vl_api_macip_acl_del_reply_t *rmp;
1543 rv = macip_acl_del_list (ntohl (mp->acl_index));
1545 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1549 vl_api_macip_acl_interface_add_del_t_handler
1550 (vl_api_macip_acl_interface_add_del_t * mp)
1552 acl_main_t *am = &acl_main;
1553 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1555 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1556 u32 sw_if_index = ntohl (mp->sw_if_index);
1558 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1559 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1562 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1563 ntohl (mp->acl_index));
1565 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1569 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1570 macip_acl_list_t * acl, u32 context)
1572 vl_api_macip_acl_details_t *mp;
1573 vl_api_macip_acl_rule_t *rules;
1574 macip_acl_rule_t *r;
1576 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1578 mp = vl_msg_api_alloc (msg_size);
1579 memset (mp, 0, msg_size);
1580 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1582 /* fill in the message */
1583 mp->context = context;
1586 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1587 mp->count = htonl (acl->count);
1588 mp->acl_index = htonl (acl - am->macip_acls);
1590 for (i = 0; i < acl->count; i++)
1593 rules[i].is_permit = r->is_permit;
1594 rules[i].is_ipv6 = r->is_ipv6;
1595 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1596 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1597 sizeof (r->src_mac_mask));
1599 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1600 sizeof (r->src_ip_addr.ip6));
1602 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1603 sizeof (r->src_ip_addr.ip4));
1604 rules[i].src_ip_prefix_len = r->src_prefixlen;
1609 /* No martini, no party - no ACL applied to this interface. */
1614 vl_msg_api_send_shmem (q, (u8 *) & mp);
1619 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1621 acl_main_t *am = &acl_main;
1622 macip_acl_list_t *acl;
1624 unix_shared_memory_queue_t *q;
1626 q = vl_api_client_index_to_input_queue (mp->client_index);
1632 if (mp->acl_index == ~0)
1634 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1635 pool_foreach (acl, am->macip_acls, (
1637 send_macip_acl_details (am, q, acl,
1645 u32 acl_index = ntohl (mp->acl_index);
1646 if (!pool_is_free_index (am->macip_acls, acl_index))
1648 acl = &am->macip_acls[acl_index];
1649 send_macip_acl_details (am, q, acl, mp->context);
1655 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1658 acl_main_t *am = &acl_main;
1659 vl_api_macip_acl_interface_get_reply_t *rmp;
1660 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1661 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1662 unix_shared_memory_queue_t *q;
1665 q = vl_api_client_index_to_input_queue (mp->client_index);
1671 rmp = vl_msg_api_alloc (msg_size);
1672 memset (rmp, 0, msg_size);
1674 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1675 rmp->context = mp->context;
1676 rmp->count = htonl (count);
1677 for (i = 0; i < count; i++)
1679 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1682 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1685 /* Set up the API message handling tables */
1686 static clib_error_t *
1687 acl_plugin_api_hookup (vlib_main_t * vm)
1689 acl_main_t *am = &acl_main;
1691 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1693 vl_api_##n##_t_handler, \
1695 vl_api_##n##_t_endian, \
1696 vl_api_##n##_t_print, \
1697 sizeof(vl_api_##n##_t), 1);
1698 foreach_acl_plugin_api_msg;
1704 #define vl_msg_name_crc_list
1705 #include <acl/acl_all_api_h.h>
1706 #undef vl_msg_name_crc_list
1709 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1711 #define _(id,n,crc) \
1712 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1713 foreach_vl_msg_name_crc_acl;
1718 acl_setup_fa_nodes (void)
1720 vlib_main_t *vm = vlib_get_main ();
1721 acl_main_t *am = &acl_main;
1722 vlib_node_t *n, *n4, *n6;
1724 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1725 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1726 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1729 am->l2_input_classify_next_acl_ip4 =
1730 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1731 am->l2_input_classify_next_acl_ip6 =
1732 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1734 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1735 l2input_get_feat_names (),
1736 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1738 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1739 l2input_get_feat_names (),
1740 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1743 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1744 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1745 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1747 am->l2_output_classify_next_acl_ip4 =
1748 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1749 am->l2_output_classify_next_acl_ip6 =
1750 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1752 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1753 l2output_get_feat_names (),
1754 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1756 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1757 l2output_get_feat_names (),
1758 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1762 acl_set_timeout_sec(int timeout_type, u32 value)
1764 acl_main_t *am = &acl_main;
1765 clib_time_t *ct = &am->vlib_main->clib_time;
1767 if (timeout_type < ACL_N_TIMEOUTS) {
1768 am->session_timeout_sec[timeout_type] = value;
1770 clib_warning("Unknown timeout type %d", timeout_type);
1773 am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1777 acl_set_session_max_entries(u32 value)
1779 acl_main_t *am = &acl_main;
1780 am->fa_conn_table_max_entries = value;
1784 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1786 acl_main_t *am = &acl_main;
1787 if ((eh < 256) && (value < 2))
1789 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1797 static clib_error_t *
1798 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1800 acl_main_t *am = &acl_main;
1802 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1803 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1804 /* also unapply any ACLs in case the users did not do so. */
1805 macip_acl_interface_del_acl(am, sw_if_index);
1806 acl_interface_reset_inout_acls (sw_if_index, 0);
1807 acl_interface_reset_inout_acls (sw_if_index, 1);
1812 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1814 static clib_error_t *
1815 acl_set_aclplugin_fn (vlib_main_t * vm,
1816 unformat_input_t * input,
1817 vlib_cli_command_t * cmd)
1819 clib_error_t *error = 0;
1823 uword memory_size = 0;
1824 acl_main_t *am = &acl_main;
1826 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1827 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1828 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1832 if (unformat (input, "use-hash-acl-matching %u", &val))
1834 am->use_hash_acl_matching = (val !=0);
1837 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1839 am->l4_match_nonfirst_fragment = (val != 0);
1842 if (unformat (input, "session")) {
1843 if (unformat (input, "table")) {
1844 /* The commands here are for tuning/testing. No user-serviceable parts inside */
1845 if (unformat (input, "max-entries")) {
1846 if (!unformat(input, "%u", &val)) {
1847 error = clib_error_return(0,
1848 "expecting maximum number of entries, got `%U`",
1849 format_unformat_error, input);
1852 acl_set_session_max_entries(val);
1856 if (unformat (input, "hash-table-buckets")) {
1857 if (!unformat(input, "%u", &val)) {
1858 error = clib_error_return(0,
1859 "expecting maximum number of hash table buckets, got `%U`",
1860 format_unformat_error, input);
1863 am->fa_conn_table_hash_num_buckets = val;
1867 if (unformat (input, "hash-table-memory")) {
1868 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1869 error = clib_error_return(0,
1870 "expecting maximum amount of hash table memory, got `%U`",
1871 format_unformat_error, input);
1874 am->fa_conn_table_hash_memory_size = memory_size;
1880 if (unformat (input, "timeout")) {
1881 if (unformat(input, "udp")) {
1882 if(unformat(input, "idle")) {
1883 if (!unformat(input, "%u", &timeout)) {
1884 error = clib_error_return(0,
1885 "expecting timeout value in seconds, got `%U`",
1886 format_unformat_error, input);
1889 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
1894 if (unformat(input, "tcp")) {
1895 if(unformat(input, "idle")) {
1896 if (!unformat(input, "%u", &timeout)) {
1897 error = clib_error_return(0,
1898 "expecting timeout value in seconds, got `%U`",
1899 format_unformat_error, input);
1902 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
1906 if(unformat(input, "transient")) {
1907 if (!unformat(input, "%u", &timeout)) {
1908 error = clib_error_return(0,
1909 "expecting timeout value in seconds, got `%U`",
1910 format_unformat_error, input);
1913 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
1925 static clib_error_t *
1926 acl_show_aclplugin_fn (vlib_main_t * vm,
1927 unformat_input_t * input,
1928 vlib_cli_command_t * cmd)
1930 clib_error_t *error = 0;
1931 acl_main_t *am = &acl_main;
1932 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1935 vnet_sw_interface_t *swif;
1937 if (unformat (input, "sessions"))
1939 u8 * out0 = format(0, "");
1941 u32 show_bihash_verbose = 0;
1942 unformat (input, "verbose %u", &show_bihash_verbose);
1943 pool_foreach (swif, im->sw_interfaces,
1945 u32 sw_if_index = swif->sw_if_index;
1946 u64 n_adds = sw_if_index < vec_len(am->fa_session_adds_by_sw_if_index) ? am->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
1947 u64 n_dels = sw_if_index < vec_len(am->fa_session_dels_by_sw_if_index) ? am->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
1948 out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
1951 u64 n_adds = am->fa_session_total_adds;
1952 u64 n_dels = am->fa_session_total_dels;
1953 out0 = format(out0, "TOTAL: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
1955 out0 = format(out0, "\n\nPer-worker data:\n");
1956 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1957 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1958 out0 = format(out0, "Worker #%d:\n", wk);
1959 out0 = format(out0, " Next expiry time: %lu\n", pw->next_expiry_time);
1960 out0 = format(out0, " Requeue until time: %lu\n", pw->requeue_until_time);
1961 out0 = format(out0, " Current time wait interval: %lu\n", pw->current_time_wait_interval);
1962 out0 = format(out0, " Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
1963 out0 = format(out0, " Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
1964 out0 = format(out0, " Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
1965 out0 = format(out0, " Swipe until this time: %lu\n", pw->swipe_end_time);
1966 out0 = format(out0, " sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1967 out0 = format(out0, " pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
1968 out0 = format(out0, " clear in progress: %u\n", pw->clear_in_process);
1969 out0 = format(out0, " interrupt is pending: %d\n", pw->interrupt_is_pending);
1970 out0 = format(out0, " interrupt is needed: %d\n", pw->interrupt_is_needed);
1971 out0 = format(out0, " interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
1973 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
1974 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
1975 foreach_fa_cleaner_counter;
1977 vlib_cli_output(vm, "\n\n%s\n\n", out0);
1978 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
1979 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
1980 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);
1983 show_fa_sessions_hash(vm, show_bihash_verbose);
1985 else if (unformat (input, "interface"))
1987 u32 sw_if_index = ~0;
1989 u8 * out0 = format(0, "");
1990 unformat (input, "sw_if_index %u", &sw_if_index);
1991 for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
1992 (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
1993 out0 = format(out0, "sw_if_index %d:\n", swi);
1995 if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
1996 (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
1997 out0 = format(out0, " input acl(s): ");
1998 vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
1999 out0 = format(out0, "%d ", *pj);
2001 out0 = format(out0, "\n");
2004 if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
2005 (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2006 out0 = format(out0, " output acl(s): ");
2007 vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2008 out0 = format(out0, "%d ", *pj);
2010 out0 = format(out0, "\n");
2014 vlib_cli_output(vm, "\n%s\n", out0);
2017 else if (unformat (input, "acl"))
2021 u8 * out0 = format(0, "");
2022 unformat (input, "index %u", &acl_index);
2023 for(i=0; i<vec_len(am->acls); i++) {
2024 if (acl_is_not_defined(am, i)) {
2025 /* don't attempt to show the ACLs that do not exist */
2028 if ((acl_index != ~0) && (acl_index != i)) {
2031 out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2034 for(j=0; j<am->acls[i].count; j++) {
2035 r = &am->acls[i].rules[j];
2036 out0 = format(out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2037 out0 = format_acl_action(out0, r->is_permit);
2038 out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2039 out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2040 out0 = format(out0, " proto %d", r->proto);
2041 out0 = format(out0, " sport %d", r->src_port_or_type_first);
2042 if (r->src_port_or_type_first != r->src_port_or_type_last) {
2043 out0 = format(out0, "-%d", r->src_port_or_type_last);
2045 out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2046 if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2047 out0 = format(out0, "-%d", r->dst_port_or_code_last);
2049 if (r->tcp_flags_mask || r->tcp_flags_value) {
2050 out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2052 out0 = format(out0, "\n");
2055 if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2056 out0 = format(out0, " applied inbound on sw_if_index: ");
2057 vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2058 out0 = format(out0, "%d ", *pj);
2060 out0 = format(out0, "\n");
2062 if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2063 out0 = format(out0, " applied outbound on sw_if_index: ");
2064 vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2065 out0 = format(out0, "%d ", *pj);
2067 out0 = format(out0, "\n");
2070 vlib_cli_output(vm, "\n%s\n", out0);
2073 else if (unformat (input, "tables"))
2075 ace_mask_type_entry_t *mte;
2077 u32 sw_if_index = ~0;
2078 int show_acl_hash_info = 0;
2079 int show_applied_info = 0;
2080 int show_mask_type = 0;
2081 int show_bihash = 0;
2082 u32 show_bihash_verbose = 0;
2084 if (unformat (input, "acl")) {
2085 show_acl_hash_info = 1;
2086 /* mask-type is handy to see as well right there */
2088 unformat (input, "index %u", &acl_index);
2089 } else if (unformat (input, "applied")) {
2090 show_applied_info = 1;
2091 unformat (input, "sw_if_index %u", &sw_if_index);
2092 } else if (unformat (input, "mask")) {
2094 } else if (unformat (input, "hash")) {
2096 unformat (input, "verbose %u", &show_bihash_verbose);
2099 if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2100 /* if no qualifiers specified, show all */
2102 show_acl_hash_info = 1;
2103 show_applied_info = 1;
2107 if (show_mask_type) {
2108 vlib_cli_output(vm, "Mask-type entries:");
2110 pool_foreach(mte, am->ace_mask_type_pool,
2112 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
2113 mte - am->ace_mask_type_pool,
2114 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2115 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2120 if (show_acl_hash_info) {
2122 u8 * out0 = format(0, "");
2124 out0 = format(out0, "Mask-ready ACL representations\n");
2125 for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2126 if ((acl_index != ~0) && (acl_index != i)) {
2129 hash_acl_info_t *ha = &am->hash_acl_infos[i];
2130 out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2131 out0 = format(out0, " mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2132 for(j=0; j<vec_len(ha->rules); j++) {
2133 hash_ace_info_t *pa = &ha->rules[j];
2134 m = (u64 *)&pa->match;
2135 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",
2136 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2137 pa->acl_index, pa->ace_index, pa->action,
2138 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2141 vlib_cli_output(vm, "\n%s\n", out0);
2145 if (show_applied_info) {
2147 u8 * out0 = format(0, "");
2148 out0 = format(out0, "Applied lookup entries for interfaces\n");
2150 for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2151 (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2152 (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2153 (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2154 if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2157 out0 = format(out0, "sw_if_index %d:\n", swi);
2158 if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2159 applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2160 out0 = format(out0, " input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2162 if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2163 out0 = format(out0, " input lookup applied entries:\n");
2164 for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2165 applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2166 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d\n",
2167 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2168 pae->next_applied_entry_index, pae->prev_applied_entry_index);
2172 if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2173 applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2174 out0 = format(out0, " output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2176 if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2177 out0 = format(out0, " output lookup applied entries:\n");
2178 for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2179 applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2180 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d\n",
2181 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2182 pae->next_applied_entry_index, pae->prev_applied_entry_index);
2187 vlib_cli_output(vm, "\n%s\n", out0);
2192 show_hash_acl_hash(vm, am, show_bihash_verbose);
2198 static clib_error_t *
2199 acl_clear_aclplugin_fn (vlib_main_t * vm,
2200 unformat_input_t * input,
2201 vlib_cli_command_t * cmd)
2203 clib_error_t *error = 0;
2204 acl_main_t *am = &acl_main;
2205 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2206 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2211 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2212 .path = "set acl-plugin",
2213 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2214 .function = acl_set_aclplugin_fn,
2217 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2218 .path = "show acl-plugin",
2219 .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2220 .function = acl_show_aclplugin_fn,
2223 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
2224 .path = "clear acl-plugin sessions",
2225 .short_help = "clear acl-plugin sessions",
2226 .function = acl_clear_aclplugin_fn,
2232 static clib_error_t *
2233 acl_init (vlib_main_t * vm)
2235 acl_main_t *am = &acl_main;
2236 clib_error_t *error = 0;
2237 memset (am, 0, sizeof (*am));
2239 am->vnet_main = vnet_get_main ();
2241 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2243 /* Ask for a correctly-sized block of API message decode slots */
2244 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2245 VL_MSG_FIRST_AVAILABLE);
2247 error = acl_plugin_api_hookup (vm);
2249 /* Add our API messages to the global name_crc hash table */
2250 setup_message_id_table (am, &api_main);
2254 acl_setup_fa_nodes();
2255 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2256 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2257 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2259 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2260 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2261 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2262 vlib_thread_main_t *tm = vlib_get_thread_main ();
2263 vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2267 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2268 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2269 vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2270 vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2271 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2272 pw->fa_conn_list_head[tt] = ~0;
2273 pw->fa_conn_list_tail[tt] = ~0;
2278 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2279 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2280 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2282 am->fa_cleaner_cnt_delete_by_sw_index = 0;
2283 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2284 am->fa_cleaner_cnt_unknown_event = 0;
2285 am->fa_cleaner_cnt_timer_restarted = 0;
2286 am->fa_cleaner_cnt_wait_with_timeout = 0;
2289 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2293 am->l4_match_nonfirst_fragment = 1;
2295 /* use the new fancy hash-based matching */
2297 am->use_hash_acl_matching = 1;
2302 VLIB_INIT_FUNCTION (acl_init);