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);
93 void *oldheap = clib_mem_set_heap(am->acl_mheap);
99 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
101 acl_main_t *am = &acl_main;
102 vl_api_acl_plugin_get_version_reply_t *rmp;
103 int msg_size = sizeof (*rmp);
104 unix_shared_memory_queue_t *q;
106 q = vl_api_client_index_to_input_queue (mp->client_index);
112 rmp = vl_msg_api_alloc (msg_size);
113 memset (rmp, 0, msg_size);
115 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
116 rmp->context = mp->context;
117 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
118 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
120 vl_msg_api_send_shmem (q, (u8 *) & rmp);
124 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
126 vl_api_acl_plugin_control_ping_reply_t *rmp;
127 acl_main_t *am = &acl_main;
131 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
133 rmp->vpe_pid = ntohl (getpid ());
139 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
140 u32 * acl_list_index, u8 * tag)
142 acl_main_t *am = &acl_main;
145 acl_rule_t *acl_new_rules = 0;
148 if (*acl_list_index != ~0)
150 /* They supplied some number, let's see if this ACL exists */
151 if (pool_is_free_index (am->acls, *acl_list_index))
153 /* tried to replace a non-existent ACL, no point doing anything */
154 clib_warning("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)", *acl_list_index, tag);
159 clib_warning("acl-plugin-warning: supplied no rules for ACL %d (tag %s)", *acl_list_index, tag);
162 void *oldheap = acl_set_heap(am);
164 /* Create and populate the rules */
166 vec_validate(acl_new_rules, count-1);
168 for (i = 0; i < count; i++)
170 r = vec_elt_at_index(acl_new_rules, i);
171 memset(r, 0, sizeof(*r));
172 r->is_permit = rules[i].is_permit;
173 r->is_ipv6 = rules[i].is_ipv6;
176 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
177 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
181 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
182 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
184 r->src_prefixlen = rules[i].src_ip_prefix_len;
185 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
186 r->proto = rules[i].proto;
187 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
188 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
189 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
190 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
191 r->tcp_flags_value = rules[i].tcp_flags_value;
192 r->tcp_flags_mask = rules[i].tcp_flags_mask;
195 if (~0 == *acl_list_index)
198 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
199 memset (a, 0, sizeof (*a));
200 /* Will return the newly allocated ACL index */
201 *acl_list_index = a - am->acls;
205 a = am->acls + *acl_list_index;
206 hash_acl_delete(am, *acl_list_index);
207 /* Get rid of the old rules */
211 a->rules = acl_new_rules;
213 memcpy (a->tag, tag, sizeof (a->tag));
214 hash_acl_add(am, *acl_list_index);
215 clib_mem_set_heap (oldheap);
220 acl_del_list (u32 acl_list_index)
222 acl_main_t *am = &acl_main;
225 if (pool_is_free_index (am->acls, acl_list_index))
230 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
231 if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
232 /* ACL is applied somewhere inbound. Refuse to delete */
236 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
237 if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
238 /* ACL is applied somewhere outbound. Refuse to delete */
243 void *oldheap = acl_set_heap(am);
244 /* delete any references to the ACL */
245 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
247 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
250 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
252 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
260 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
262 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
265 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
267 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
275 /* delete the hash table data */
277 hash_acl_delete(am, acl_list_index);
278 /* now we can delete the ACL itself */
279 a = &am->acls[acl_list_index];
283 pool_put (am->acls, a);
284 clib_mem_set_heap (oldheap);
288 /* Some aids in ASCII graphing the content */
294 u8 ip4_5tuple_mask[] =
295 _(" dmac smac etype ")
296 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
303 _(" ttl pr checksum ")
312 _("L4 T/U sport dport ")
322 u8 ip6_5tuple_mask[] =
323 _(" dmac smac etype ")
324 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
326 _(0x0000) __ __ __ __
328 _(0x0004) __ __ XX __
330 _(0x0008) XX XX XX XX
331 _(0x000C) XX XX XX XX
332 _(0x0010) XX XX XX XX
333 _(0x0014) XX XX XX XX
335 _(0x0018) XX XX XX XX
336 _(0x001C) XX XX XX XX
337 _(0x0020) XX XX XX XX
338 _(0x0024) XX XX XX XX
339 _("L4T/U sport dport ")
340 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
347 static int count_skip (u8 * p, u32 size)
349 u64 *p64 = (u64 *) p;
350 /* Be tolerant to null pointer */
354 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
358 return (p64 - (u64 *) p) / 2;
362 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
363 u32 mask_len, u32 next_table_index,
364 u32 miss_next_index, u32 * table_index,
368 u32 memory_size = 2 << 13;
369 u32 skip = count_skip (mask, mask_len);
370 u32 match = (mask_len / 16) - skip;
371 u8 *skip_mask_ptr = mask + 16 * skip;
372 u32 current_data_flag = 0;
373 int current_data_offset = 0;
377 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
378 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
379 memory_size, skip, match,
380 next_table_index, miss_next_index,
381 table_index, current_data_flag,
382 current_data_offset, is_add,
383 1 /* delete_chain */);
384 clib_mem_set_heap (oldheap);
389 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
390 u32 mask_len, u32 next_table_index,
391 u32 miss_next_index, u32 * table_index,
395 u32 memory_size = 2 << 20;
396 u32 skip = count_skip (mask, mask_len);
397 u32 match = (mask_len / 16) - skip;
398 u8 *skip_mask_ptr = mask + 16 * skip;
399 u32 current_data_flag = 0;
400 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);
418 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
420 vnet_classify_main_t *cm = &vnet_classify_main;
421 u32 ip4_table_index = ~0;
422 u32 ip6_table_index = ~0;
423 void *oldheap = acl_set_heap(am);
425 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
427 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
430 /* switch to global heap while calling vnet_* functions */
431 clib_mem_set_heap (cm->vlib_main->heap_base);
432 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
434 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
437 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
438 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
439 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
440 sizeof (ip4_5tuple_mask) - 1, ~0,
441 am->l2_input_classify_next_acl_ip4,
442 &ip4_table_index, 0);
444 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
447 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
448 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
449 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
450 sizeof (ip6_5tuple_mask) - 1, ~0,
451 am->l2_input_classify_next_acl_ip6,
452 &ip6_table_index, 0);
454 clib_mem_set_heap (oldheap);
459 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
461 vnet_classify_main_t *cm = &vnet_classify_main;
462 u32 ip4_table_index = ~0;
463 u32 ip6_table_index = ~0;
464 void *oldheap = acl_set_heap(am);
466 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
468 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
471 /* switch to global heap while calling vnet_* functions */
472 clib_mem_set_heap (cm->vlib_main->heap_base);
474 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
476 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
479 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
480 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
481 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
482 sizeof (ip4_5tuple_mask) - 1, ~0,
483 am->l2_output_classify_next_acl_ip4,
484 &ip4_table_index, 0);
486 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
489 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
490 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
491 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
492 sizeof (ip6_5tuple_mask) - 1, ~0,
493 am->l2_output_classify_next_acl_ip6,
494 &ip6_table_index, 0);
496 clib_mem_set_heap (oldheap);
501 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
503 vnet_classify_main_t *cm = &vnet_classify_main;
504 u32 ip4_table_index = ~0;
505 u32 ip6_table_index = ~0;
508 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
510 /* in case there were previous tables attached */
511 acl_unhook_l2_input_classify (am, sw_if_index);
513 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
514 sizeof (ip4_5tuple_mask) - 1, ~0,
515 am->l2_input_classify_next_acl_ip4,
516 &ip4_table_index, 1);
520 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
521 sizeof (ip6_5tuple_mask) - 1, ~0,
522 am->l2_input_classify_next_acl_ip6,
523 &ip6_table_index, 1);
526 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
527 sizeof (ip4_5tuple_mask) - 1, ~0,
528 am->l2_input_classify_next_acl_ip4,
529 &ip4_table_index, 0);
533 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
534 ip6_table_index, ~0);
536 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
537 sw_if_index, ip4_table_index, ip6_table_index);
540 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
541 sizeof (ip6_5tuple_mask) - 1, ~0,
542 am->l2_input_classify_next_acl_ip6,
543 &ip6_table_index, 0);
544 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
545 sizeof (ip4_5tuple_mask) - 1, ~0,
546 am->l2_input_classify_next_acl_ip4,
547 &ip4_table_index, 0);
551 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
553 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
556 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
558 clib_mem_set_heap (prevheap);
563 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
565 vnet_classify_main_t *cm = &vnet_classify_main;
566 u32 ip4_table_index = ~0;
567 u32 ip6_table_index = ~0;
570 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
572 /* in case there were previous tables attached */
573 acl_unhook_l2_output_classify (am, sw_if_index);
575 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
576 sizeof (ip4_5tuple_mask) - 1, ~0,
577 am->l2_output_classify_next_acl_ip4,
578 &ip4_table_index, 1);
582 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
583 sizeof (ip6_5tuple_mask) - 1, ~0,
584 am->l2_output_classify_next_acl_ip6,
585 &ip6_table_index, 1);
588 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
589 sizeof (ip4_5tuple_mask) - 1, ~0,
590 am->l2_output_classify_next_acl_ip4,
591 &ip4_table_index, 0);
595 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
596 ip6_table_index, ~0);
598 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
599 sw_if_index, ip4_table_index, ip6_table_index);
602 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
603 sizeof (ip6_5tuple_mask) - 1, ~0,
604 am->l2_output_classify_next_acl_ip6,
605 &ip6_table_index, 0);
606 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
607 sizeof (ip4_5tuple_mask) - 1, ~0,
608 am->l2_output_classify_next_acl_ip4,
609 &ip4_table_index, 0);
613 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
615 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
618 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
620 clib_mem_set_heap (prevheap);
627 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
633 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
635 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
637 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
641 rv = acl_hook_l2_input_classify (am, sw_if_index);
645 rv = acl_unhook_l2_input_classify (am, sw_if_index);
652 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
658 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
660 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
662 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
666 rv = acl_hook_l2_output_classify (am, sw_if_index);
670 rv = acl_unhook_l2_output_classify (am, sw_if_index);
677 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
679 return (pool_is_free_index (am->acls, acl_list_index));
684 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
686 acl_main_t *am = &acl_main;
687 if (acl_is_not_defined(am, acl_list_index)) {
688 /* ACL is not defined. Can not apply */
691 void *oldheap = acl_set_heap(am);
695 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
697 u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
698 if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
699 clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
700 acl_list_index, sw_if_index, index);
701 /* the entry is already there */
702 clib_mem_set_heap (oldheap);
705 /* if there was no ACL applied before, enable the ACL processing */
706 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
707 acl_interface_in_enable_disable (am, sw_if_index, 1);
709 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
711 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
712 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
717 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
719 u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
720 if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
721 clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
722 acl_list_index, sw_if_index, index);
723 /* the entry is already there */
724 clib_mem_set_heap (oldheap);
727 /* if there was no ACL applied before, enable the ACL processing */
728 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
729 acl_interface_out_enable_disable (am, sw_if_index, 1);
731 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
733 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
734 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
737 clib_mem_set_heap (oldheap);
743 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
745 acl_main_t *am = &acl_main;
748 void *oldheap = acl_set_heap(am);
751 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
752 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
755 if (acl_list_index ==
756 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
758 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
764 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
765 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
766 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
767 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
768 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
772 /* If there is no more ACLs applied on an interface, disable ACL processing */
773 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
775 acl_interface_in_enable_disable (am, sw_if_index, 0);
780 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
782 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
784 if (acl_list_index ==
785 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
787 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
793 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
794 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
795 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
796 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
797 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
801 /* If there is no more ACLs applied on an interface, disable ACL processing */
802 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
804 acl_interface_out_enable_disable (am, sw_if_index, 0);
807 clib_mem_set_heap (oldheap);
812 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
814 acl_main_t *am = &acl_main;
816 void *oldheap = acl_set_heap(am);
819 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
820 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
821 acl_interface_in_enable_disable (am, sw_if_index, 0);
824 for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
825 u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
826 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
827 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
828 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
829 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
830 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
835 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
839 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
840 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
841 acl_interface_out_enable_disable (am, sw_if_index, 0);
844 for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
845 u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
846 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
847 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
848 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
849 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
850 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
855 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
857 clib_mem_set_heap (oldheap);
861 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
865 acl_main_t *am = &acl_main;
866 void *oldheap = acl_set_heap(am);
870 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
873 hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
878 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
880 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
882 clib_mem_set_heap (oldheap);
895 } macip_match_type_t;
898 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
904 for (i = 0; i < vec_len (mv); i++)
906 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
907 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
917 /* Get metric used to sort match types.
918 The more specific and the more often seen - the bigger the metric */
920 match_type_metric (macip_match_type_t * m)
922 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
923 return m->prefix_len + m->is_ipv6 + 10 * m->count;
927 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
929 /* Ascending sort based on the metric values */
930 return match_type_metric (m1) - match_type_metric (m2);
933 /* Get the offset of L3 source within ethernet packet */
935 get_l3_src_offset(int is6)
938 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
940 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
944 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
946 macip_match_type_t *mvec = NULL;
947 macip_match_type_t *mt;
948 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
950 u32 match_type_index;
953 vnet_classify_main_t *cm = &vnet_classify_main;
955 /* Count the number of different types of rules */
956 for (i = 0; i < a->count; i++)
960 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
961 a->rules[i].src_prefixlen,
962 a->rules[i].is_ipv6)))
964 match_type_index = vec_len (mvec);
965 vec_validate (mvec, match_type_index);
966 memcpy (mvec[match_type_index].mac_mask,
967 a->rules[match_type_index].src_mac_mask, 6);
968 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
969 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
970 mvec[match_type_index].table_index = ~0;
972 mvec[match_type_index].count++;
974 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
975 vec_sort_with_function (mvec, match_type_compare);
976 /* Create the classifier tables */
978 /* First add ARP tables */
979 vec_foreach (mt, mvec)
982 int is6 = mt->is_ipv6;
984 mt->arp_table_index = ~0;
987 memset (mask, 0, sizeof (mask));
988 memcpy (&mask[6], mt->mac_mask, 6);
989 memset (&mask[12], 0xff, 2); /* ethernet protocol */
990 memcpy (&mask[14 + 8], mt->mac_mask, 6);
992 for (i = 0; i < (mt->prefix_len / 8); i++)
993 mask[14 + 14 + i] = 0xff;
994 if (mt->prefix_len % 8)
995 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
997 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
998 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
999 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1000 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1002 last_table = mt->arp_table_index;
1005 /* Now add IP[46] tables */
1006 vec_foreach (mt, mvec)
1009 int is6 = mt->is_ipv6;
1010 int l3_src_offs = get_l3_src_offset(is6);
1011 memset (mask, 0, sizeof (mask));
1012 memcpy (&mask[6], mt->mac_mask, 6);
1013 for (i = 0; i < (mt->prefix_len / 8); i++)
1015 mask[l3_src_offs + i] = 0xff;
1017 if (mt->prefix_len % 8)
1019 mask[l3_src_offs + (mt->prefix_len / 8)] =
1020 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1023 * Round-up the number of bytes needed to store the prefix,
1024 * and round up the number of vectors too
1026 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1027 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1028 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1029 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1031 last_table = mt->table_index;
1033 a->ip4_table_index = ~0;
1034 a->ip6_table_index = ~0;
1035 a->l2_table_index = last_table;
1037 /* Populate the classifier tables with rules from the MACIP ACL */
1038 for (i = 0; i < a->count; i++)
1042 int is6 = a->rules[i].is_ipv6;
1043 int l3_src_offs = get_l3_src_offset(is6);
1044 memset (mask, 0, sizeof (mask));
1045 memcpy (&mask[6], a->rules[i].src_mac, 6);
1046 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1049 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1055 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1060 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1061 a->rules[i].src_prefixlen,
1062 a->rules[i].is_ipv6);
1063 ASSERT(match_type_index != ~0);
1064 /* add session to table mvec[match_type_index].table_index; */
1065 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1066 mask, a->rules[i].is_permit ? ~0 : 0, i,
1067 0, action, metadata, 1);
1068 /* add ARP table entry too */
1069 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1071 memset (mask, 0, sizeof (mask));
1072 memcpy (&mask[6], a->rules[i].src_mac, 6);
1075 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1076 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1077 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1078 mask, a->rules[i].is_permit ? ~0 : 0, i,
1079 0, action, metadata, 1);
1086 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1088 vnet_classify_main_t *cm = &vnet_classify_main;
1089 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1091 if (a->ip4_table_index != ~0)
1093 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1094 a->ip4_table_index = ~0;
1096 if (a->ip6_table_index != ~0)
1098 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1099 a->ip6_table_index = ~0;
1101 if (a->l2_table_index != ~0)
1103 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1104 a->l2_table_index = ~0;
1109 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1110 u32 * acl_list_index, u8 * tag)
1112 acl_main_t *am = &acl_main;
1113 macip_acl_list_t *a;
1114 macip_acl_rule_t *r;
1115 macip_acl_rule_t *acl_new_rules = 0;
1118 clib_warning("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)", tag);
1120 void *oldheap = acl_set_heap(am);
1121 /* Create and populate the rules */
1123 vec_validate(acl_new_rules, count-1);
1125 for (i = 0; i < count; i++)
1127 r = &acl_new_rules[i];
1128 r->is_permit = rules[i].is_permit;
1129 r->is_ipv6 = rules[i].is_ipv6;
1130 memcpy (&r->src_mac, rules[i].src_mac, 6);
1131 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1132 if(rules[i].is_ipv6)
1133 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1135 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1136 r->src_prefixlen = rules[i].src_ip_prefix_len;
1140 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1141 memset (a, 0, sizeof (*a));
1142 /* Will return the newly allocated ACL index */
1143 *acl_list_index = a - am->macip_acls;
1145 a->rules = acl_new_rules;
1147 memcpy (a->tag, tag, sizeof (a->tag));
1149 /* Create and populate the classifer tables */
1150 macip_create_classify_tables (am, *acl_list_index);
1151 clib_mem_set_heap (oldheap);
1156 /* No check for validity of sw_if_index - the callers were supposed to validate */
1159 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1162 u32 macip_acl_index;
1163 macip_acl_list_t *a;
1164 void *oldheap = acl_set_heap(am);
1165 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1166 clib_mem_set_heap (oldheap);
1167 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1168 /* No point in deleting MACIP ACL which is not applied */
1169 if (~0 == macip_acl_index)
1171 a = &am->macip_acls[macip_acl_index];
1172 /* remove the classifier tables off the interface L2 ACL */
1174 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1175 a->ip6_table_index, a->l2_table_index, 0);
1176 /* Unset the MACIP ACL index */
1177 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1181 /* No check for validity of sw_if_index - the callers were supposed to validate */
1184 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1185 u32 macip_acl_index)
1187 macip_acl_list_t *a;
1189 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1193 void *oldheap = acl_set_heap(am);
1194 a = &am->macip_acls[macip_acl_index];
1195 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1196 /* If there already a MACIP ACL applied, unapply it */
1197 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1198 macip_acl_interface_del_acl(am, sw_if_index);
1199 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1200 clib_mem_set_heap (oldheap);
1202 /* Apply the classifier tables for L2 ACLs */
1204 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1205 a->ip6_table_index, a->l2_table_index, 1);
1210 macip_acl_del_list (u32 acl_list_index)
1212 acl_main_t *am = &acl_main;
1213 void *oldheap = acl_set_heap(am);
1214 macip_acl_list_t *a;
1216 if (pool_is_free_index (am->macip_acls, acl_list_index))
1221 /* delete any references to the ACL */
1222 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1224 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1226 macip_acl_interface_del_acl (am, i);
1230 /* Now that classifier tables are detached, clean them up */
1231 macip_destroy_classify_tables (am, acl_list_index);
1233 /* now we can delete the ACL itself */
1234 a = &am->macip_acls[acl_list_index];
1237 vec_free (a->rules);
1239 pool_put (am->macip_acls, a);
1240 clib_mem_set_heap (oldheap);
1246 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1249 acl_main_t *am = &acl_main;
1250 void *oldheap = acl_set_heap(am);
1254 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1258 rv = macip_acl_interface_del_acl (am, sw_if_index);
1260 clib_mem_set_heap (oldheap);
1265 * If the client does not allocate enough memory for a variable-length
1266 * message, and then proceed to use it as if the full memory allocated,
1267 * absent the check we happily consume that on the VPP side, and go
1268 * along as if nothing happened. However, the resulting
1269 * effects range from just garbage in the API decode
1270 * (because the decoder snoops too far), to potential memory
1273 * This verifies that the actual length of the message is
1274 * at least expected_len, and complains loudly if it is not.
1276 * A failing check here is 100% a software bug on the API user side,
1277 * so we might as well yell.
1280 static int verify_message_len(void *mp, u32 expected_len, char *where)
1282 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1283 if (supplied_len < expected_len) {
1284 clib_warning("%s: Supplied message length %d is less than expected %d",
1285 where, supplied_len, expected_len);
1292 /* API message handler */
1294 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1296 vl_api_acl_add_replace_reply_t *rmp;
1297 acl_main_t *am = &acl_main;
1299 u32 acl_list_index = ntohl (mp->acl_index);
1300 u32 acl_count = ntohl (mp->count);
1301 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1303 if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1304 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1306 rv = VNET_API_ERROR_INVALID_VALUE;
1310 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1312 rmp->acl_index = htonl(acl_list_index);
1318 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1320 acl_main_t *am = &acl_main;
1321 vl_api_acl_del_reply_t *rmp;
1324 rv = acl_del_list (ntohl (mp->acl_index));
1326 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1330 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1332 acl_main_t *am = &acl_main;
1333 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1334 u32 sw_if_index = ntohl (mp->sw_if_index);
1335 vl_api_acl_interface_add_del_reply_t *rmp;
1338 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1339 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1342 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1343 mp->is_input, ntohl (mp->acl_index));
1345 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1349 vl_api_acl_interface_set_acl_list_t_handler
1350 (vl_api_acl_interface_set_acl_list_t * mp)
1352 acl_main_t *am = &acl_main;
1353 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1356 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1357 u32 sw_if_index = ntohl (mp->sw_if_index);
1359 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1360 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1363 acl_interface_reset_inout_acls (sw_if_index, 0);
1364 acl_interface_reset_inout_acls (sw_if_index, 1);
1366 for (i = 0; i < mp->count; i++)
1368 if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1369 /* ACL does not exist, so we can not apply it */
1374 for (i = 0; i < mp->count; i++)
1376 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1377 ntohl (mp->acls[i]));
1382 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1386 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1388 api_rule->is_permit = r->is_permit;
1389 api_rule->is_ipv6 = r->is_ipv6;
1392 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1393 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1397 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1398 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1400 api_rule->src_ip_prefix_len = r->src_prefixlen;
1401 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1402 api_rule->proto = r->proto;
1403 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1404 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1405 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1406 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1407 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1408 api_rule->tcp_flags_value = r->tcp_flags_value;
1412 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1413 acl_list_t * acl, u32 context)
1415 vl_api_acl_details_t *mp;
1416 vl_api_acl_rule_t *rules;
1418 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1419 void *oldheap = acl_set_heap(am);
1421 mp = vl_msg_api_alloc (msg_size);
1422 memset (mp, 0, msg_size);
1423 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1425 /* fill in the message */
1426 mp->context = context;
1427 mp->count = htonl (acl->count);
1428 mp->acl_index = htonl (acl - am->acls);
1429 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1430 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1432 for (i = 0; i < acl->count; i++)
1434 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1437 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1438 clib_mem_set_heap (oldheap);
1439 vl_msg_api_send_shmem (q, (u8 *) & mp);
1444 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1446 acl_main_t *am = &acl_main;
1451 unix_shared_memory_queue_t *q;
1453 q = vl_api_client_index_to_input_queue (mp->client_index);
1459 if (mp->acl_index == ~0)
1462 /* Just dump all ACLs */
1463 pool_foreach (acl, am->acls,
1465 send_acl_details(am, q, acl, mp->context);
1471 acl_index = ntohl (mp->acl_index);
1472 if (!pool_is_free_index (am->acls, acl_index))
1474 acl = &am->acls[acl_index];
1475 send_acl_details (am, q, acl, mp->context);
1481 /* FIXME API: should we signal an error here at all ? */
1487 send_acl_interface_list_details (acl_main_t * am,
1488 unix_shared_memory_queue_t * q,
1489 u32 sw_if_index, u32 context)
1491 vl_api_acl_interface_list_details_t *mp;
1497 void *oldheap = acl_set_heap(am);
1499 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1500 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1502 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1503 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1504 count = n_input + n_output;
1506 msg_size = sizeof (*mp);
1507 msg_size += sizeof (mp->acls[0]) * count;
1509 mp = vl_msg_api_alloc (msg_size);
1510 memset (mp, 0, msg_size);
1512 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1514 /* fill in the message */
1515 mp->context = context;
1516 mp->sw_if_index = htonl (sw_if_index);
1518 mp->n_input = n_input;
1519 for (i = 0; i < n_input; i++)
1521 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1523 for (i = 0; i < n_output; i++)
1525 mp->acls[n_input + i] =
1526 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1528 clib_mem_set_heap (oldheap);
1529 vl_msg_api_send_shmem (q, (u8 *) & mp);
1533 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1536 acl_main_t *am = &acl_main;
1537 vnet_sw_interface_t *swif;
1538 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1541 unix_shared_memory_queue_t *q;
1543 q = vl_api_client_index_to_input_queue (mp->client_index);
1549 if (mp->sw_if_index == ~0)
1552 pool_foreach (swif, im->sw_interfaces,
1554 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1560 sw_if_index = ntohl (mp->sw_if_index);
1561 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1562 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1566 /* MACIP ACL API handlers */
1569 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1571 vl_api_macip_acl_add_reply_t *rmp;
1572 acl_main_t *am = &acl_main;
1574 u32 acl_list_index = ~0;
1575 u32 acl_count = ntohl (mp->count);
1576 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1578 if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1579 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1581 rv = VNET_API_ERROR_INVALID_VALUE;
1585 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1587 rmp->acl_index = htonl(acl_list_index);
1593 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1595 acl_main_t *am = &acl_main;
1596 vl_api_macip_acl_del_reply_t *rmp;
1599 rv = macip_acl_del_list (ntohl (mp->acl_index));
1601 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1605 vl_api_macip_acl_interface_add_del_t_handler
1606 (vl_api_macip_acl_interface_add_del_t * mp)
1608 acl_main_t *am = &acl_main;
1609 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1611 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1612 u32 sw_if_index = ntohl (mp->sw_if_index);
1614 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1615 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1618 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1619 ntohl (mp->acl_index));
1621 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1625 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1626 macip_acl_list_t * acl, u32 context)
1628 vl_api_macip_acl_details_t *mp;
1629 vl_api_macip_acl_rule_t *rules;
1630 macip_acl_rule_t *r;
1632 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1634 mp = vl_msg_api_alloc (msg_size);
1635 memset (mp, 0, msg_size);
1636 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1638 /* fill in the message */
1639 mp->context = context;
1642 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1643 mp->count = htonl (acl->count);
1644 mp->acl_index = htonl (acl - am->macip_acls);
1646 for (i = 0; i < acl->count; i++)
1649 rules[i].is_permit = r->is_permit;
1650 rules[i].is_ipv6 = r->is_ipv6;
1651 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1652 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1653 sizeof (r->src_mac_mask));
1655 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1656 sizeof (r->src_ip_addr.ip6));
1658 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1659 sizeof (r->src_ip_addr.ip4));
1660 rules[i].src_ip_prefix_len = r->src_prefixlen;
1665 /* No martini, no party - no ACL applied to this interface. */
1670 vl_msg_api_send_shmem (q, (u8 *) & mp);
1675 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1677 acl_main_t *am = &acl_main;
1678 macip_acl_list_t *acl;
1680 unix_shared_memory_queue_t *q;
1682 q = vl_api_client_index_to_input_queue (mp->client_index);
1688 if (mp->acl_index == ~0)
1690 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1691 pool_foreach (acl, am->macip_acls, (
1693 send_macip_acl_details (am, q, acl,
1701 u32 acl_index = ntohl (mp->acl_index);
1702 if (!pool_is_free_index (am->macip_acls, acl_index))
1704 acl = &am->macip_acls[acl_index];
1705 send_macip_acl_details (am, q, acl, mp->context);
1711 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1714 acl_main_t *am = &acl_main;
1715 vl_api_macip_acl_interface_get_reply_t *rmp;
1716 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1717 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1718 unix_shared_memory_queue_t *q;
1721 q = vl_api_client_index_to_input_queue (mp->client_index);
1727 rmp = vl_msg_api_alloc (msg_size);
1728 memset (rmp, 0, msg_size);
1730 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1731 rmp->context = mp->context;
1732 rmp->count = htonl (count);
1733 for (i = 0; i < count; i++)
1735 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1738 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1741 /* Set up the API message handling tables */
1742 static clib_error_t *
1743 acl_plugin_api_hookup (vlib_main_t * vm)
1745 acl_main_t *am = &acl_main;
1747 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1749 vl_api_##n##_t_handler, \
1751 vl_api_##n##_t_endian, \
1752 vl_api_##n##_t_print, \
1753 sizeof(vl_api_##n##_t), 1);
1754 foreach_acl_plugin_api_msg;
1760 #define vl_msg_name_crc_list
1761 #include <acl/acl_all_api_h.h>
1762 #undef vl_msg_name_crc_list
1765 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1767 #define _(id,n,crc) \
1768 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1769 foreach_vl_msg_name_crc_acl;
1774 acl_setup_fa_nodes (void)
1776 vlib_main_t *vm = vlib_get_main ();
1777 acl_main_t *am = &acl_main;
1778 vlib_node_t *n, *n4, *n6;
1780 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1781 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1782 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1785 am->l2_input_classify_next_acl_ip4 =
1786 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1787 am->l2_input_classify_next_acl_ip6 =
1788 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1790 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1791 l2input_get_feat_names (),
1792 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1794 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1795 l2input_get_feat_names (),
1796 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1799 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1800 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1801 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1803 am->l2_output_classify_next_acl_ip4 =
1804 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1805 am->l2_output_classify_next_acl_ip6 =
1806 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1808 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1809 l2output_get_feat_names (),
1810 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1812 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1813 l2output_get_feat_names (),
1814 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1818 acl_set_timeout_sec(int timeout_type, u32 value)
1820 acl_main_t *am = &acl_main;
1821 clib_time_t *ct = &am->vlib_main->clib_time;
1823 if (timeout_type < ACL_N_TIMEOUTS) {
1824 am->session_timeout_sec[timeout_type] = value;
1826 clib_warning("Unknown timeout type %d", timeout_type);
1829 am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1833 acl_set_session_max_entries(u32 value)
1835 acl_main_t *am = &acl_main;
1836 am->fa_conn_table_max_entries = value;
1840 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1842 acl_main_t *am = &acl_main;
1844 if ((eh < 256) && (value < 2))
1846 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1854 static clib_error_t *
1855 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1857 acl_main_t *am = &acl_main;
1859 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1860 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1861 /* also unapply any ACLs in case the users did not do so. */
1862 macip_acl_interface_del_acl(am, sw_if_index);
1863 acl_interface_reset_inout_acls (sw_if_index, 0);
1864 acl_interface_reset_inout_acls (sw_if_index, 1);
1869 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1871 static clib_error_t *
1872 acl_set_aclplugin_fn (vlib_main_t * vm,
1873 unformat_input_t * input,
1874 vlib_cli_command_t * cmd)
1876 clib_error_t *error = 0;
1880 uword memory_size = 0;
1881 acl_main_t *am = &acl_main;
1883 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1884 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1885 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1889 if (unformat (input, "use-hash-acl-matching %u", &val))
1891 am->use_hash_acl_matching = (val !=0);
1894 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1896 am->l4_match_nonfirst_fragment = (val != 0);
1899 if (unformat (input, "session")) {
1900 if (unformat (input, "table")) {
1901 /* The commands here are for tuning/testing. No user-serviceable parts inside */
1902 if (unformat (input, "max-entries")) {
1903 if (!unformat(input, "%u", &val)) {
1904 error = clib_error_return(0,
1905 "expecting maximum number of entries, got `%U`",
1906 format_unformat_error, input);
1909 acl_set_session_max_entries(val);
1913 if (unformat (input, "hash-table-buckets")) {
1914 if (!unformat(input, "%u", &val)) {
1915 error = clib_error_return(0,
1916 "expecting maximum number of hash table buckets, got `%U`",
1917 format_unformat_error, input);
1920 am->fa_conn_table_hash_num_buckets = val;
1924 if (unformat (input, "hash-table-memory")) {
1925 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1926 error = clib_error_return(0,
1927 "expecting maximum amount of hash table memory, got `%U`",
1928 format_unformat_error, input);
1931 am->fa_conn_table_hash_memory_size = memory_size;
1937 if (unformat (input, "timeout")) {
1938 if (unformat(input, "udp")) {
1939 if(unformat(input, "idle")) {
1940 if (!unformat(input, "%u", &timeout)) {
1941 error = clib_error_return(0,
1942 "expecting timeout value in seconds, got `%U`",
1943 format_unformat_error, input);
1946 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
1951 if (unformat(input, "tcp")) {
1952 if(unformat(input, "idle")) {
1953 if (!unformat(input, "%u", &timeout)) {
1954 error = clib_error_return(0,
1955 "expecting timeout value in seconds, got `%U`",
1956 format_unformat_error, input);
1959 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
1963 if(unformat(input, "transient")) {
1964 if (!unformat(input, "%u", &timeout)) {
1965 error = clib_error_return(0,
1966 "expecting timeout value in seconds, got `%U`",
1967 format_unformat_error, input);
1970 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
1982 static clib_error_t *
1983 acl_show_aclplugin_fn (vlib_main_t * vm,
1984 unformat_input_t * input,
1985 vlib_cli_command_t * cmd)
1987 clib_error_t *error = 0;
1988 acl_main_t *am = &acl_main;
1989 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1992 vnet_sw_interface_t *swif;
1994 if (unformat (input, "sessions"))
1996 u8 * out0 = format(0, "");
1998 u32 show_bihash_verbose = 0;
1999 u32 show_session_thread_id = ~0;
2000 u32 show_session_session_index = ~0;
2001 unformat (input, "thread %u index %u", &show_session_thread_id, &show_session_session_index);
2002 unformat (input, "verbose %u", &show_bihash_verbose);
2004 u64 n_adds = am->fa_session_total_adds;
2005 u64 n_dels = am->fa_session_total_dels;
2006 out0 = format(out0, "Sessions total: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
2008 out0 = format(out0, "\n\nPer-thread data:\n");
2009 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2010 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2011 out0 = format(out0, "Thread #%d:\n", wk);
2012 if (show_session_thread_id == wk && show_session_session_index < pool_len(pw->fa_sessions_pool)) {
2013 out0 = format(out0, " session index %u:\n", show_session_session_index);
2014 fa_session_t *sess = pw->fa_sessions_pool + show_session_session_index;
2015 u64 *m = (u64 *)&sess->info;
2016 out0 = format(out0, " info: %016llx %016llx %016llx %016llx %016llx %016llx\n", m[0], m[1], m[2], m[3], m[4], m[5]);
2017 out0 = format(out0, " sw_if_index: %u\n", sess->sw_if_index);
2018 out0 = format(out0, " tcp_flags_seen: %x\n", sess->tcp_flags_seen.as_u16);
2019 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2020 out0 = format(out0, " thread index: %u\n", sess->thread_index);
2021 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2022 out0 = format(out0, " link next index: %u\n", sess->link_next_idx);
2023 out0 = format(out0, " link prev index: %u\n", sess->link_prev_idx);
2024 out0 = format(out0, " link list id: %u\n", sess->link_list_id);
2026 out0 = format(out0, " connection add/del stats:\n", wk);
2027 pool_foreach (swif, im->sw_interfaces,
2029 u32 sw_if_index = swif->sw_if_index;
2030 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;
2031 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;
2032 out0 = format(out0, " sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2035 out0 = format(out0, " connection timeout type lists:\n", wk);
2037 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2038 u32 head_session_index = pw->fa_conn_list_head[tt];
2039 out0 = format(out0, " fa_conn_list_head[%d]: %d\n", tt, head_session_index);
2040 if (~0 != head_session_index) {
2041 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2042 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2043 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2047 out0 = format(out0, " Next expiry time: %lu\n", pw->next_expiry_time);
2048 out0 = format(out0, " Requeue until time: %lu\n", pw->requeue_until_time);
2049 out0 = format(out0, " Current time wait interval: %lu\n", pw->current_time_wait_interval);
2050 out0 = format(out0, " Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
2051 out0 = format(out0, " Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
2052 out0 = format(out0, " Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
2053 out0 = format(out0, " Swipe until this time: %lu\n", pw->swipe_end_time);
2054 out0 = format(out0, " sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2055 out0 = format(out0, " pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
2056 out0 = format(out0, " clear in progress: %u\n", pw->clear_in_process);
2057 out0 = format(out0, " interrupt is pending: %d\n", pw->interrupt_is_pending);
2058 out0 = format(out0, " interrupt is needed: %d\n", pw->interrupt_is_needed);
2059 out0 = format(out0, " interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
2061 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
2062 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
2063 foreach_fa_cleaner_counter;
2065 vec_terminate_c_string(out0);
2066 vlib_cli_output(vm, "\n\n%s\n\n", out0);
2067 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2068 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2069 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);
2072 show_fa_sessions_hash(vm, show_bihash_verbose);
2074 else if (unformat (input, "interface"))
2076 u32 sw_if_index = ~0;
2078 u8 * out0 = format(0, "");
2079 unformat (input, "sw_if_index %u", &sw_if_index);
2080 for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
2081 (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
2082 out0 = format(out0, "sw_if_index %d:\n", swi);
2084 if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
2085 (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
2086 out0 = format(out0, " input acl(s): ");
2087 vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
2088 out0 = format(out0, "%d ", *pj);
2090 out0 = format(out0, "\n");
2093 if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
2094 (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2095 out0 = format(out0, " output acl(s): ");
2096 vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2097 out0 = format(out0, "%d ", *pj);
2099 out0 = format(out0, "\n");
2103 vec_terminate_c_string(out0);
2104 vlib_cli_output(vm, "\n%s\n", out0);
2107 else if (unformat (input, "acl"))
2111 u8 * out0 = format(0, "");
2112 unformat (input, "index %u", &acl_index);
2113 for(i=0; i<vec_len(am->acls); i++) {
2114 if (acl_is_not_defined(am, i)) {
2115 /* don't attempt to show the ACLs that do not exist */
2118 if ((acl_index != ~0) && (acl_index != i)) {
2121 out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2124 for(j=0; j<am->acls[i].count; j++) {
2125 r = &am->acls[i].rules[j];
2126 out0 = format(out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2127 out0 = format_acl_action(out0, r->is_permit);
2128 out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2129 out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2130 out0 = format(out0, " proto %d", r->proto);
2131 out0 = format(out0, " sport %d", r->src_port_or_type_first);
2132 if (r->src_port_or_type_first != r->src_port_or_type_last) {
2133 out0 = format(out0, "-%d", r->src_port_or_type_last);
2135 out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2136 if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2137 out0 = format(out0, "-%d", r->dst_port_or_code_last);
2139 if (r->tcp_flags_mask || r->tcp_flags_value) {
2140 out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2142 out0 = format(out0, "\n");
2145 if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2146 out0 = format(out0, " applied inbound on sw_if_index: ");
2147 vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2148 out0 = format(out0, "%d ", *pj);
2150 out0 = format(out0, "\n");
2152 if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2153 out0 = format(out0, " applied outbound on sw_if_index: ");
2154 vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2155 out0 = format(out0, "%d ", *pj);
2157 out0 = format(out0, "\n");
2160 vec_terminate_c_string(out0);
2161 vlib_cli_output(vm, "\n%s\n", out0);
2164 else if (unformat (input, "memory"))
2166 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2167 if (am->acl_mheap) {
2168 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2170 vlib_cli_output (vm, " Not initialized\n");
2172 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2173 if (am->hash_lookup_mheap) {
2174 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2176 vlib_cli_output (vm, " Not initialized\n");
2179 else if (unformat (input, "tables"))
2181 ace_mask_type_entry_t *mte;
2183 u32 sw_if_index = ~0;
2184 int show_acl_hash_info = 0;
2185 int show_applied_info = 0;
2186 int show_mask_type = 0;
2187 int show_bihash = 0;
2188 u32 show_bihash_verbose = 0;
2190 if (unformat (input, "acl")) {
2191 show_acl_hash_info = 1;
2192 /* mask-type is handy to see as well right there */
2194 unformat (input, "index %u", &acl_index);
2195 } else if (unformat (input, "applied")) {
2196 show_applied_info = 1;
2197 unformat (input, "sw_if_index %u", &sw_if_index);
2198 } else if (unformat (input, "mask")) {
2200 } else if (unformat (input, "hash")) {
2202 unformat (input, "verbose %u", &show_bihash_verbose);
2205 if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2206 /* if no qualifiers specified, show all */
2208 show_acl_hash_info = 1;
2209 show_applied_info = 1;
2213 if (show_mask_type) {
2214 vlib_cli_output(vm, "Mask-type entries:");
2216 pool_foreach(mte, am->ace_mask_type_pool,
2218 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
2219 mte - am->ace_mask_type_pool,
2220 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2221 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2226 if (show_acl_hash_info) {
2228 u8 * out0 = format(0, "");
2230 out0 = format(out0, "Mask-ready ACL representations\n");
2231 for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2232 if ((acl_index != ~0) && (acl_index != i)) {
2235 hash_acl_info_t *ha = &am->hash_acl_infos[i];
2236 out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2237 out0 = format(out0, " mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2238 for(j=0; j<vec_len(ha->rules); j++) {
2239 hash_ace_info_t *pa = &ha->rules[j];
2240 m = (u64 *)&pa->match;
2241 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",
2242 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2243 pa->acl_index, pa->ace_index, pa->action,
2244 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2247 vec_terminate_c_string(out0);
2248 vlib_cli_output(vm, "\n%s\n", out0);
2252 if (show_applied_info) {
2254 u8 * out0 = format(0, "");
2255 out0 = format(out0, "Applied lookup entries for interfaces\n");
2257 for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2258 (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2259 (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2260 (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2261 if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2264 out0 = format(out0, "sw_if_index %d:\n", swi);
2265 if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2266 applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2267 out0 = format(out0, " input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2269 if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2270 out0 = format(out0, " input lookup applied entries:\n");
2271 for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2272 applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2273 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2274 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2275 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2279 if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2280 applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2281 out0 = format(out0, " output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2283 if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2284 out0 = format(out0, " output lookup applied entries:\n");
2285 for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2286 applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2287 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2288 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2289 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2294 vec_terminate_c_string(out0);
2295 vlib_cli_output(vm, "\n%s\n", out0);
2300 show_hash_acl_hash(vm, am, show_bihash_verbose);
2306 static clib_error_t *
2307 acl_clear_aclplugin_fn (vlib_main_t * vm,
2308 unformat_input_t * input,
2309 vlib_cli_command_t * cmd)
2311 clib_error_t *error = 0;
2312 acl_main_t *am = &acl_main;
2313 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2314 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2319 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2320 .path = "set acl-plugin",
2321 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2322 .function = acl_set_aclplugin_fn,
2325 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2326 .path = "show acl-plugin",
2327 .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2328 .function = acl_show_aclplugin_fn,
2331 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
2332 .path = "clear acl-plugin sessions",
2333 .short_help = "clear acl-plugin sessions",
2334 .function = acl_clear_aclplugin_fn,
2340 static clib_error_t *
2341 acl_init (vlib_main_t * vm)
2343 acl_main_t *am = &acl_main;
2344 clib_error_t *error = 0;
2345 memset (am, 0, sizeof (*am));
2347 am->vnet_main = vnet_get_main ();
2349 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2351 /* Ask for a correctly-sized block of API message decode slots */
2352 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2353 VL_MSG_FIRST_AVAILABLE);
2355 error = acl_plugin_api_hookup (vm);
2357 /* Add our API messages to the global name_crc hash table */
2358 setup_message_id_table (am, &api_main);
2362 acl_setup_fa_nodes();
2364 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2365 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2366 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2368 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2369 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2370 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2371 vlib_thread_main_t *tm = vlib_get_thread_main ();
2372 vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2376 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2377 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2378 vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2379 vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2380 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2381 pw->fa_conn_list_head[tt] = ~0;
2382 pw->fa_conn_list_tail[tt] = ~0;
2387 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2388 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2389 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2391 am->fa_cleaner_cnt_delete_by_sw_index = 0;
2392 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2393 am->fa_cleaner_cnt_unknown_event = 0;
2394 am->fa_cleaner_cnt_timer_restarted = 0;
2395 am->fa_cleaner_cnt_wait_with_timeout = 0;
2398 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2402 am->l4_match_nonfirst_fragment = 1;
2404 /* use the new fancy hash-based matching */
2405 am->use_hash_acl_matching = 1;
2410 VLIB_INIT_FUNCTION (acl_init);