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) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump)
82 VLIB_PLUGIN_REGISTER () = {
83 .version = VPP_BUILD_VER,
84 .description = "Access Control Lists",
90 acl_set_heap(acl_main_t *am)
92 if (0 == am->acl_mheap) {
93 am->acl_mheap = mheap_alloc (0 /* use VM */ , 2 << 29);
95 void *oldheap = clib_mem_set_heap(am->acl_mheap);
101 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
103 acl_main_t *am = &acl_main;
104 vl_api_acl_plugin_get_version_reply_t *rmp;
105 int msg_size = sizeof (*rmp);
106 unix_shared_memory_queue_t *q;
108 q = vl_api_client_index_to_input_queue (mp->client_index);
114 rmp = vl_msg_api_alloc (msg_size);
115 memset (rmp, 0, msg_size);
117 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
118 rmp->context = mp->context;
119 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
120 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
122 vl_msg_api_send_shmem (q, (u8 *) & rmp);
126 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
128 vl_api_acl_plugin_control_ping_reply_t *rmp;
129 acl_main_t *am = &acl_main;
133 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
135 rmp->vpe_pid = ntohl (getpid ());
141 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
142 u32 * acl_list_index, u8 * tag)
144 acl_main_t *am = &acl_main;
147 acl_rule_t *acl_new_rules = 0;
150 if (*acl_list_index != ~0)
152 /* They supplied some number, let's see if this ACL exists */
153 if (pool_is_free_index (am->acls, *acl_list_index))
155 /* tried to replace a non-existent ACL, no point doing anything */
156 clib_warning("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)", *acl_list_index, tag);
161 clib_warning("acl-plugin-warning: supplied no rules for ACL %d (tag %s)", *acl_list_index, tag);
164 void *oldheap = acl_set_heap(am);
166 /* Create and populate the rules */
168 vec_validate(acl_new_rules, count-1);
170 for (i = 0; i < count; i++)
172 r = vec_elt_at_index(acl_new_rules, i);
173 memset(r, 0, sizeof(*r));
174 r->is_permit = rules[i].is_permit;
175 r->is_ipv6 = rules[i].is_ipv6;
178 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
179 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
183 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
184 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
186 r->src_prefixlen = rules[i].src_ip_prefix_len;
187 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
188 r->proto = rules[i].proto;
189 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
190 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
191 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
192 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
193 r->tcp_flags_value = rules[i].tcp_flags_value;
194 r->tcp_flags_mask = rules[i].tcp_flags_mask;
197 if (~0 == *acl_list_index)
200 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
201 memset (a, 0, sizeof (*a));
202 /* Will return the newly allocated ACL index */
203 *acl_list_index = a - am->acls;
207 a = am->acls + *acl_list_index;
208 hash_acl_delete(am, *acl_list_index);
209 /* Get rid of the old rules */
213 a->rules = acl_new_rules;
215 memcpy (a->tag, tag, sizeof (a->tag));
216 hash_acl_add(am, *acl_list_index);
217 clib_mem_set_heap (oldheap);
222 acl_del_list (u32 acl_list_index)
224 acl_main_t *am = &acl_main;
227 if (pool_is_free_index (am->acls, acl_list_index))
232 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
233 if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
234 /* ACL is applied somewhere inbound. Refuse to delete */
238 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
239 if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
240 /* ACL is applied somewhere outbound. Refuse to delete */
245 void *oldheap = acl_set_heap(am);
246 /* delete any references to the ACL */
247 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
249 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
252 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
254 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
262 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
264 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
267 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
269 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
277 /* delete the hash table data */
279 hash_acl_delete(am, acl_list_index);
280 /* now we can delete the ACL itself */
281 a = &am->acls[acl_list_index];
285 pool_put (am->acls, a);
286 clib_mem_set_heap (oldheap);
290 /* Some aids in ASCII graphing the content */
296 u8 ip4_5tuple_mask[] =
297 _(" dmac smac etype ")
298 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
305 _(" ttl pr checksum ")
314 _("L4 T/U sport dport ")
324 u8 ip6_5tuple_mask[] =
325 _(" dmac smac etype ")
326 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
328 _(0x0000) __ __ __ __
330 _(0x0004) __ __ XX __
332 _(0x0008) XX XX XX XX
333 _(0x000C) XX XX XX XX
334 _(0x0010) XX XX XX XX
335 _(0x0014) XX XX XX XX
337 _(0x0018) XX XX XX XX
338 _(0x001C) XX XX XX XX
339 _(0x0020) XX XX XX XX
340 _(0x0024) XX XX XX XX
341 _("L4T/U sport dport ")
342 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
349 static int count_skip (u8 * p, u32 size)
351 u64 *p64 = (u64 *) p;
352 /* Be tolerant to null pointer */
356 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
360 return (p64 - (u64 *) p) / 2;
364 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
365 u32 mask_len, u32 next_table_index,
366 u32 miss_next_index, u32 * table_index,
370 u32 memory_size = 2 << 13;
371 u32 skip = count_skip (mask, mask_len);
372 u32 match = (mask_len / 16) - skip;
373 u8 *skip_mask_ptr = mask + 16 * skip;
374 u32 current_data_flag = 0;
375 int current_data_offset = 0;
379 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
380 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
381 memory_size, skip, match,
382 next_table_index, miss_next_index,
383 table_index, current_data_flag,
384 current_data_offset, is_add,
385 1 /* delete_chain */);
386 clib_mem_set_heap (oldheap);
391 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
392 u32 mask_len, u32 next_table_index,
393 u32 miss_next_index, u32 * table_index,
397 u32 memory_size = 2 << 20;
398 u32 skip = count_skip (mask, mask_len);
399 u32 match = (mask_len / 16) - skip;
400 u8 *skip_mask_ptr = mask + 16 * skip;
401 u32 current_data_flag = 0;
402 int current_data_offset = 0;
407 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
408 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
409 memory_size, skip, match,
410 next_table_index, miss_next_index,
411 table_index, current_data_flag,
412 current_data_offset, is_add,
413 1 /* delete_chain */);
415 clib_mem_set_heap (oldheap);
420 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
422 vnet_classify_main_t *cm = &vnet_classify_main;
423 u32 ip4_table_index = ~0;
424 u32 ip6_table_index = ~0;
425 void *oldheap = acl_set_heap(am);
427 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
429 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
432 /* switch to global heap while calling vnet_* functions */
433 clib_mem_set_heap (cm->vlib_main->heap_base);
434 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
436 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
439 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
440 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
441 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
442 sizeof (ip4_5tuple_mask) - 1, ~0,
443 am->l2_input_classify_next_acl_ip4,
444 &ip4_table_index, 0);
446 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
449 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
450 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
451 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
452 sizeof (ip6_5tuple_mask) - 1, ~0,
453 am->l2_input_classify_next_acl_ip6,
454 &ip6_table_index, 0);
456 clib_mem_set_heap (oldheap);
461 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
463 vnet_classify_main_t *cm = &vnet_classify_main;
464 u32 ip4_table_index = ~0;
465 u32 ip6_table_index = ~0;
466 void *oldheap = acl_set_heap(am);
468 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
470 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
473 /* switch to global heap while calling vnet_* functions */
474 clib_mem_set_heap (cm->vlib_main->heap_base);
476 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
478 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
481 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
482 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
483 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
484 sizeof (ip4_5tuple_mask) - 1, ~0,
485 am->l2_output_classify_next_acl_ip4,
486 &ip4_table_index, 0);
488 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
491 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
492 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
493 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
494 sizeof (ip6_5tuple_mask) - 1, ~0,
495 am->l2_output_classify_next_acl_ip6,
496 &ip6_table_index, 0);
498 clib_mem_set_heap (oldheap);
503 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
505 vnet_classify_main_t *cm = &vnet_classify_main;
506 u32 ip4_table_index = ~0;
507 u32 ip6_table_index = ~0;
510 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
512 /* in case there were previous tables attached */
513 acl_unhook_l2_input_classify (am, sw_if_index);
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, 1);
522 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
523 sizeof (ip6_5tuple_mask) - 1, ~0,
524 am->l2_input_classify_next_acl_ip6,
525 &ip6_table_index, 1);
528 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
529 sizeof (ip4_5tuple_mask) - 1, ~0,
530 am->l2_input_classify_next_acl_ip4,
531 &ip4_table_index, 0);
535 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
536 ip6_table_index, ~0);
538 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
539 sw_if_index, ip4_table_index, ip6_table_index);
542 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
543 sizeof (ip6_5tuple_mask) - 1, ~0,
544 am->l2_input_classify_next_acl_ip6,
545 &ip6_table_index, 0);
546 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
547 sizeof (ip4_5tuple_mask) - 1, ~0,
548 am->l2_input_classify_next_acl_ip4,
549 &ip4_table_index, 0);
553 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
555 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
558 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
560 clib_mem_set_heap (prevheap);
565 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
567 vnet_classify_main_t *cm = &vnet_classify_main;
568 u32 ip4_table_index = ~0;
569 u32 ip6_table_index = ~0;
572 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
574 /* in case there were previous tables attached */
575 acl_unhook_l2_output_classify (am, sw_if_index);
577 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
578 sizeof (ip4_5tuple_mask) - 1, ~0,
579 am->l2_output_classify_next_acl_ip4,
580 &ip4_table_index, 1);
584 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
585 sizeof (ip6_5tuple_mask) - 1, ~0,
586 am->l2_output_classify_next_acl_ip6,
587 &ip6_table_index, 1);
590 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
591 sizeof (ip4_5tuple_mask) - 1, ~0,
592 am->l2_output_classify_next_acl_ip4,
593 &ip4_table_index, 0);
597 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
598 ip6_table_index, ~0);
600 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
601 sw_if_index, ip4_table_index, ip6_table_index);
604 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
605 sizeof (ip6_5tuple_mask) - 1, ~0,
606 am->l2_output_classify_next_acl_ip6,
607 &ip6_table_index, 0);
608 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
609 sizeof (ip4_5tuple_mask) - 1, ~0,
610 am->l2_output_classify_next_acl_ip4,
611 &ip4_table_index, 0);
615 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
617 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
620 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
622 clib_mem_set_heap (prevheap);
629 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
635 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
637 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
639 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
643 rv = acl_hook_l2_input_classify (am, sw_if_index);
647 rv = acl_unhook_l2_input_classify (am, sw_if_index);
654 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
660 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
662 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
664 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
668 rv = acl_hook_l2_output_classify (am, sw_if_index);
672 rv = acl_unhook_l2_output_classify (am, sw_if_index);
679 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
681 return (pool_is_free_index (am->acls, acl_list_index));
686 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
688 acl_main_t *am = &acl_main;
689 if (acl_is_not_defined(am, acl_list_index)) {
690 /* ACL is not defined. Can not apply */
693 void *oldheap = acl_set_heap(am);
697 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
699 u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
700 if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
701 clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
702 acl_list_index, sw_if_index, index);
703 /* the entry is already there */
704 clib_mem_set_heap (oldheap);
707 /* if there was no ACL applied before, enable the ACL processing */
708 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
709 acl_interface_in_enable_disable (am, sw_if_index, 1);
711 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
713 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
714 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
719 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
721 u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
722 if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
723 clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
724 acl_list_index, sw_if_index, index);
725 /* the entry is already there */
726 clib_mem_set_heap (oldheap);
729 /* if there was no ACL applied before, enable the ACL processing */
730 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
731 acl_interface_out_enable_disable (am, sw_if_index, 1);
733 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
735 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
736 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
739 clib_mem_set_heap (oldheap);
745 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
747 acl_main_t *am = &acl_main;
750 void *oldheap = acl_set_heap(am);
753 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
754 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
757 if (acl_list_index ==
758 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
760 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
766 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
767 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
768 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
769 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
770 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
774 /* If there is no more ACLs applied on an interface, disable ACL processing */
775 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
777 acl_interface_in_enable_disable (am, sw_if_index, 0);
782 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
784 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
786 if (acl_list_index ==
787 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
789 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
795 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
796 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
797 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
798 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
799 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
803 /* If there is no more ACLs applied on an interface, disable ACL processing */
804 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
806 acl_interface_out_enable_disable (am, sw_if_index, 0);
809 clib_mem_set_heap (oldheap);
814 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
816 acl_main_t *am = &acl_main;
818 void *oldheap = acl_set_heap(am);
821 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
822 if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
823 acl_interface_in_enable_disable (am, sw_if_index, 0);
826 for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
827 u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
828 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
829 if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
830 u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
831 if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
832 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
837 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
841 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
842 if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
843 acl_interface_out_enable_disable (am, sw_if_index, 0);
846 for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
847 u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
848 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
849 if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
850 u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
851 if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
852 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
857 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
859 clib_mem_set_heap (oldheap);
863 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
867 acl_main_t *am = &acl_main;
868 void *oldheap = acl_set_heap(am);
872 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
875 hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
880 hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
882 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
884 clib_mem_set_heap (oldheap);
897 } macip_match_type_t;
900 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
906 for (i = 0; i < vec_len (mv); i++)
908 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
909 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
919 /* Get metric used to sort match types.
920 The more specific and the more often seen - the bigger the metric */
922 match_type_metric (macip_match_type_t * m)
924 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
925 return m->prefix_len + m->is_ipv6 + 10 * m->count;
929 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
931 /* Ascending sort based on the metric values */
932 return match_type_metric (m1) - match_type_metric (m2);
935 /* Get the offset of L3 source within ethernet packet */
937 get_l3_src_offset(int is6)
940 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
942 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
946 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
948 macip_match_type_t *mvec = NULL;
949 macip_match_type_t *mt;
950 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
952 u32 match_type_index;
955 vnet_classify_main_t *cm = &vnet_classify_main;
957 /* Count the number of different types of rules */
958 for (i = 0; i < a->count; i++)
962 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
963 a->rules[i].src_prefixlen,
964 a->rules[i].is_ipv6)))
966 match_type_index = vec_len (mvec);
967 vec_validate (mvec, match_type_index);
968 memcpy (mvec[match_type_index].mac_mask,
969 a->rules[match_type_index].src_mac_mask, 6);
970 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
971 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
972 mvec[match_type_index].table_index = ~0;
974 mvec[match_type_index].count++;
976 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
977 vec_sort_with_function (mvec, match_type_compare);
978 /* Create the classifier tables */
980 /* First add ARP tables */
981 vec_foreach (mt, mvec)
984 int is6 = mt->is_ipv6;
986 mt->arp_table_index = ~0;
989 memset (mask, 0, sizeof (mask));
990 memcpy (&mask[6], mt->mac_mask, 6);
991 memset (&mask[12], 0xff, 2); /* ethernet protocol */
992 memcpy (&mask[14 + 8], mt->mac_mask, 6);
994 for (i = 0; i < (mt->prefix_len / 8); i++)
995 mask[14 + 14 + i] = 0xff;
996 if (mt->prefix_len % 8)
997 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
999 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1000 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1001 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1002 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1004 last_table = mt->arp_table_index;
1007 /* Now add IP[46] tables */
1008 vec_foreach (mt, mvec)
1011 int is6 = mt->is_ipv6;
1012 int l3_src_offs = get_l3_src_offset(is6);
1013 memset (mask, 0, sizeof (mask));
1014 memcpy (&mask[6], mt->mac_mask, 6);
1015 for (i = 0; i < (mt->prefix_len / 8); i++)
1017 mask[l3_src_offs + i] = 0xff;
1019 if (mt->prefix_len % 8)
1021 mask[l3_src_offs + (mt->prefix_len / 8)] =
1022 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1025 * Round-up the number of bytes needed to store the prefix,
1026 * and round up the number of vectors too
1028 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1029 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1030 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1031 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1033 last_table = mt->table_index;
1035 a->ip4_table_index = ~0;
1036 a->ip6_table_index = ~0;
1037 a->l2_table_index = last_table;
1039 /* Populate the classifier tables with rules from the MACIP ACL */
1040 for (i = 0; i < a->count; i++)
1044 int is6 = a->rules[i].is_ipv6;
1045 int l3_src_offs = get_l3_src_offset(is6);
1046 memset (mask, 0, sizeof (mask));
1047 memcpy (&mask[6], a->rules[i].src_mac, 6);
1048 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1051 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1057 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1062 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1063 a->rules[i].src_prefixlen,
1064 a->rules[i].is_ipv6);
1065 ASSERT(match_type_index != ~0);
1066 /* add session to table mvec[match_type_index].table_index; */
1067 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1068 mask, a->rules[i].is_permit ? ~0 : 0, i,
1069 0, action, metadata, 1);
1070 /* add ARP table entry too */
1071 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1073 memset (mask, 0, sizeof (mask));
1074 memcpy (&mask[6], a->rules[i].src_mac, 6);
1077 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1078 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1079 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1080 mask, a->rules[i].is_permit ? ~0 : 0, i,
1081 0, action, metadata, 1);
1088 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1090 vnet_classify_main_t *cm = &vnet_classify_main;
1091 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1093 if (a->ip4_table_index != ~0)
1095 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1096 a->ip4_table_index = ~0;
1098 if (a->ip6_table_index != ~0)
1100 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1101 a->ip6_table_index = ~0;
1103 if (a->l2_table_index != ~0)
1105 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1106 a->l2_table_index = ~0;
1111 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1112 u32 * acl_list_index, u8 * tag)
1114 acl_main_t *am = &acl_main;
1115 macip_acl_list_t *a;
1116 macip_acl_rule_t *r;
1117 macip_acl_rule_t *acl_new_rules = 0;
1120 clib_warning("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)", tag);
1122 void *oldheap = acl_set_heap(am);
1123 /* Create and populate the rules */
1125 vec_validate(acl_new_rules, count-1);
1127 for (i = 0; i < count; i++)
1129 r = &acl_new_rules[i];
1130 r->is_permit = rules[i].is_permit;
1131 r->is_ipv6 = rules[i].is_ipv6;
1132 memcpy (&r->src_mac, rules[i].src_mac, 6);
1133 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1134 if(rules[i].is_ipv6)
1135 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1137 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1138 r->src_prefixlen = rules[i].src_ip_prefix_len;
1142 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1143 memset (a, 0, sizeof (*a));
1144 /* Will return the newly allocated ACL index */
1145 *acl_list_index = a - am->macip_acls;
1147 a->rules = acl_new_rules;
1149 memcpy (a->tag, tag, sizeof (a->tag));
1151 /* Create and populate the classifer tables */
1152 macip_create_classify_tables (am, *acl_list_index);
1153 clib_mem_set_heap (oldheap);
1158 /* No check for validity of sw_if_index - the callers were supposed to validate */
1161 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1164 u32 macip_acl_index;
1165 macip_acl_list_t *a;
1166 void *oldheap = acl_set_heap(am);
1167 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1168 clib_mem_set_heap (oldheap);
1169 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1170 /* No point in deleting MACIP ACL which is not applied */
1171 if (~0 == macip_acl_index)
1173 a = &am->macip_acls[macip_acl_index];
1174 /* remove the classifier tables off the interface L2 ACL */
1176 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1177 a->ip6_table_index, a->l2_table_index, 0);
1178 /* Unset the MACIP ACL index */
1179 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1183 /* No check for validity of sw_if_index - the callers were supposed to validate */
1186 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1187 u32 macip_acl_index)
1189 macip_acl_list_t *a;
1191 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1195 void *oldheap = acl_set_heap(am);
1196 a = &am->macip_acls[macip_acl_index];
1197 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1198 /* If there already a MACIP ACL applied, unapply it */
1199 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1200 macip_acl_interface_del_acl(am, sw_if_index);
1201 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1202 clib_mem_set_heap (oldheap);
1204 /* Apply the classifier tables for L2 ACLs */
1206 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1207 a->ip6_table_index, a->l2_table_index, 1);
1212 macip_acl_del_list (u32 acl_list_index)
1214 acl_main_t *am = &acl_main;
1215 void *oldheap = acl_set_heap(am);
1216 macip_acl_list_t *a;
1218 if (pool_is_free_index (am->macip_acls, acl_list_index))
1223 /* delete any references to the ACL */
1224 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1226 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1228 macip_acl_interface_del_acl (am, i);
1232 /* Now that classifier tables are detached, clean them up */
1233 macip_destroy_classify_tables (am, acl_list_index);
1235 /* now we can delete the ACL itself */
1236 a = &am->macip_acls[acl_list_index];
1239 vec_free (a->rules);
1241 pool_put (am->macip_acls, a);
1242 clib_mem_set_heap (oldheap);
1248 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1251 acl_main_t *am = &acl_main;
1252 void *oldheap = acl_set_heap(am);
1256 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1260 rv = macip_acl_interface_del_acl (am, sw_if_index);
1262 clib_mem_set_heap (oldheap);
1267 * If the client does not allocate enough memory for a variable-length
1268 * message, and then proceed to use it as if the full memory allocated,
1269 * absent the check we happily consume that on the VPP side, and go
1270 * along as if nothing happened. However, the resulting
1271 * effects range from just garbage in the API decode
1272 * (because the decoder snoops too far), to potential memory
1275 * This verifies that the actual length of the message is
1276 * at least expected_len, and complains loudly if it is not.
1278 * A failing check here is 100% a software bug on the API user side,
1279 * so we might as well yell.
1282 static int verify_message_len(void *mp, u32 expected_len, char *where)
1284 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1285 if (supplied_len < expected_len) {
1286 clib_warning("%s: Supplied message length %d is less than expected %d",
1287 where, supplied_len, expected_len);
1294 /* API message handler */
1296 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1298 vl_api_acl_add_replace_reply_t *rmp;
1299 acl_main_t *am = &acl_main;
1301 u32 acl_list_index = ntohl (mp->acl_index);
1302 u32 acl_count = ntohl (mp->count);
1303 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1305 if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1306 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1308 rv = VNET_API_ERROR_INVALID_VALUE;
1312 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1314 rmp->acl_index = htonl(acl_list_index);
1320 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1322 acl_main_t *am = &acl_main;
1323 vl_api_acl_del_reply_t *rmp;
1326 rv = acl_del_list (ntohl (mp->acl_index));
1328 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1332 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1334 acl_main_t *am = &acl_main;
1335 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1336 u32 sw_if_index = ntohl (mp->sw_if_index);
1337 vl_api_acl_interface_add_del_reply_t *rmp;
1340 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1341 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1344 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1345 mp->is_input, ntohl (mp->acl_index));
1347 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1351 vl_api_acl_interface_set_acl_list_t_handler
1352 (vl_api_acl_interface_set_acl_list_t * mp)
1354 acl_main_t *am = &acl_main;
1355 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1358 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1359 u32 sw_if_index = ntohl (mp->sw_if_index);
1361 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1362 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1365 acl_interface_reset_inout_acls (sw_if_index, 0);
1366 acl_interface_reset_inout_acls (sw_if_index, 1);
1368 for (i = 0; i < mp->count; i++)
1370 if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1371 /* ACL does not exist, so we can not apply it */
1376 for (i = 0; i < mp->count; i++)
1378 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1379 ntohl (mp->acls[i]));
1384 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1388 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1390 api_rule->is_permit = r->is_permit;
1391 api_rule->is_ipv6 = r->is_ipv6;
1394 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1395 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1399 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1400 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1402 api_rule->src_ip_prefix_len = r->src_prefixlen;
1403 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1404 api_rule->proto = r->proto;
1405 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1406 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1407 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1408 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1409 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1410 api_rule->tcp_flags_value = r->tcp_flags_value;
1414 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1415 acl_list_t * acl, u32 context)
1417 vl_api_acl_details_t *mp;
1418 vl_api_acl_rule_t *rules;
1420 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1421 void *oldheap = acl_set_heap(am);
1423 mp = vl_msg_api_alloc (msg_size);
1424 memset (mp, 0, msg_size);
1425 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1427 /* fill in the message */
1428 mp->context = context;
1429 mp->count = htonl (acl->count);
1430 mp->acl_index = htonl (acl - am->acls);
1431 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1432 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1434 for (i = 0; i < acl->count; i++)
1436 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1439 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1440 clib_mem_set_heap (oldheap);
1441 vl_msg_api_send_shmem (q, (u8 *) & mp);
1446 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1448 acl_main_t *am = &acl_main;
1453 unix_shared_memory_queue_t *q;
1455 q = vl_api_client_index_to_input_queue (mp->client_index);
1461 if (mp->acl_index == ~0)
1464 /* Just dump all ACLs */
1465 pool_foreach (acl, am->acls,
1467 send_acl_details(am, q, acl, mp->context);
1473 acl_index = ntohl (mp->acl_index);
1474 if (!pool_is_free_index (am->acls, acl_index))
1476 acl = &am->acls[acl_index];
1477 send_acl_details (am, q, acl, mp->context);
1483 /* FIXME API: should we signal an error here at all ? */
1489 send_acl_interface_list_details (acl_main_t * am,
1490 unix_shared_memory_queue_t * q,
1491 u32 sw_if_index, u32 context)
1493 vl_api_acl_interface_list_details_t *mp;
1499 void *oldheap = acl_set_heap(am);
1501 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1502 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1504 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1505 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1506 count = n_input + n_output;
1508 msg_size = sizeof (*mp);
1509 msg_size += sizeof (mp->acls[0]) * count;
1511 mp = vl_msg_api_alloc (msg_size);
1512 memset (mp, 0, msg_size);
1514 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1516 /* fill in the message */
1517 mp->context = context;
1518 mp->sw_if_index = htonl (sw_if_index);
1520 mp->n_input = n_input;
1521 for (i = 0; i < n_input; i++)
1523 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1525 for (i = 0; i < n_output; i++)
1527 mp->acls[n_input + i] =
1528 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1530 clib_mem_set_heap (oldheap);
1531 vl_msg_api_send_shmem (q, (u8 *) & mp);
1535 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1538 acl_main_t *am = &acl_main;
1539 vnet_sw_interface_t *swif;
1540 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1543 unix_shared_memory_queue_t *q;
1545 q = vl_api_client_index_to_input_queue (mp->client_index);
1551 if (mp->sw_if_index == ~0)
1554 pool_foreach (swif, im->sw_interfaces,
1556 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1562 sw_if_index = ntohl (mp->sw_if_index);
1563 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1564 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1568 /* MACIP ACL API handlers */
1571 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1573 vl_api_macip_acl_add_reply_t *rmp;
1574 acl_main_t *am = &acl_main;
1576 u32 acl_list_index = ~0;
1577 u32 acl_count = ntohl (mp->count);
1578 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1580 if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1581 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1583 rv = VNET_API_ERROR_INVALID_VALUE;
1587 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1589 rmp->acl_index = htonl(acl_list_index);
1595 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1597 acl_main_t *am = &acl_main;
1598 vl_api_macip_acl_del_reply_t *rmp;
1601 rv = macip_acl_del_list (ntohl (mp->acl_index));
1603 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1607 vl_api_macip_acl_interface_add_del_t_handler
1608 (vl_api_macip_acl_interface_add_del_t * mp)
1610 acl_main_t *am = &acl_main;
1611 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1613 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1614 u32 sw_if_index = ntohl (mp->sw_if_index);
1616 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1617 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1620 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1621 ntohl (mp->acl_index));
1623 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1627 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1628 macip_acl_list_t * acl, u32 context)
1630 vl_api_macip_acl_details_t *mp;
1631 vl_api_macip_acl_rule_t *rules;
1632 macip_acl_rule_t *r;
1634 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1636 mp = vl_msg_api_alloc (msg_size);
1637 memset (mp, 0, msg_size);
1638 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1640 /* fill in the message */
1641 mp->context = context;
1644 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1645 mp->count = htonl (acl->count);
1646 mp->acl_index = htonl (acl - am->macip_acls);
1648 for (i = 0; i < acl->count; i++)
1651 rules[i].is_permit = r->is_permit;
1652 rules[i].is_ipv6 = r->is_ipv6;
1653 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1654 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1655 sizeof (r->src_mac_mask));
1657 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1658 sizeof (r->src_ip_addr.ip6));
1660 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1661 sizeof (r->src_ip_addr.ip4));
1662 rules[i].src_ip_prefix_len = r->src_prefixlen;
1667 /* No martini, no party - no ACL applied to this interface. */
1672 vl_msg_api_send_shmem (q, (u8 *) & mp);
1677 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1679 acl_main_t *am = &acl_main;
1680 macip_acl_list_t *acl;
1682 unix_shared_memory_queue_t *q;
1684 q = vl_api_client_index_to_input_queue (mp->client_index);
1690 if (mp->acl_index == ~0)
1692 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1693 pool_foreach (acl, am->macip_acls, (
1695 send_macip_acl_details (am, q, acl,
1703 u32 acl_index = ntohl (mp->acl_index);
1704 if (!pool_is_free_index (am->macip_acls, acl_index))
1706 acl = &am->macip_acls[acl_index];
1707 send_macip_acl_details (am, q, acl, mp->context);
1713 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1716 acl_main_t *am = &acl_main;
1717 vl_api_macip_acl_interface_get_reply_t *rmp;
1718 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1719 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1720 unix_shared_memory_queue_t *q;
1723 q = vl_api_client_index_to_input_queue (mp->client_index);
1729 rmp = vl_msg_api_alloc (msg_size);
1730 memset (rmp, 0, msg_size);
1732 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1733 rmp->context = mp->context;
1734 rmp->count = htonl (count);
1735 for (i = 0; i < count; i++)
1737 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1740 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1744 send_macip_acl_interface_list_details (acl_main_t * am,
1745 unix_shared_memory_queue_t * q,
1750 vl_api_macip_acl_interface_list_details_t *rmp;
1751 /* at this time there is only ever 1 mac ip acl per interface */
1752 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
1754 rmp = vl_msg_api_alloc (msg_size);
1755 memset (rmp, 0, msg_size);
1756 rmp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1758 /* fill in the message */
1759 rmp->context = context;
1761 rmp->sw_if_index = htonl (sw_if_index);
1762 rmp->acls[0] = htonl (acl_index);
1764 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1768 vl_api_macip_acl_interface_list_dump_t_handler (vl_api_macip_acl_interface_list_dump_t *mp)
1770 unix_shared_memory_queue_t *q;
1771 acl_main_t *am = &acl_main;
1772 u32 sw_if_index = ntohl (mp->sw_if_index);
1774 q = vl_api_client_index_to_input_queue (mp->client_index);
1780 if (sw_if_index == ~0)
1782 vec_foreach_index(sw_if_index, am->macip_acl_by_sw_if_index)
1784 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1786 send_macip_acl_interface_list_details(am, q, sw_if_index,
1787 am->macip_acl_by_sw_if_index[sw_if_index],
1794 if (vec_len(am->macip_acl_by_sw_if_index) > sw_if_index)
1796 send_macip_acl_interface_list_details(am, q, sw_if_index,
1797 am->macip_acl_by_sw_if_index[sw_if_index],
1803 /* Set up the API message handling tables */
1804 static clib_error_t *
1805 acl_plugin_api_hookup (vlib_main_t * vm)
1807 acl_main_t *am = &acl_main;
1809 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1811 vl_api_##n##_t_handler, \
1813 vl_api_##n##_t_endian, \
1814 vl_api_##n##_t_print, \
1815 sizeof(vl_api_##n##_t), 1);
1816 foreach_acl_plugin_api_msg;
1822 #define vl_msg_name_crc_list
1823 #include <acl/acl_all_api_h.h>
1824 #undef vl_msg_name_crc_list
1827 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1829 #define _(id,n,crc) \
1830 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1831 foreach_vl_msg_name_crc_acl;
1836 acl_setup_fa_nodes (void)
1838 vlib_main_t *vm = vlib_get_main ();
1839 acl_main_t *am = &acl_main;
1840 vlib_node_t *n, *n4, *n6;
1842 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1843 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1844 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1847 am->l2_input_classify_next_acl_ip4 =
1848 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1849 am->l2_input_classify_next_acl_ip6 =
1850 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1852 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1853 l2input_get_feat_names (),
1854 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1856 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1857 l2input_get_feat_names (),
1858 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1861 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1862 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1863 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1865 am->l2_output_classify_next_acl_ip4 =
1866 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1867 am->l2_output_classify_next_acl_ip6 =
1868 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1870 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1871 l2output_get_feat_names (),
1872 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1874 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1875 l2output_get_feat_names (),
1876 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1880 acl_set_timeout_sec(int timeout_type, u32 value)
1882 acl_main_t *am = &acl_main;
1883 clib_time_t *ct = &am->vlib_main->clib_time;
1885 if (timeout_type < ACL_N_TIMEOUTS) {
1886 am->session_timeout_sec[timeout_type] = value;
1888 clib_warning("Unknown timeout type %d", timeout_type);
1891 am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1895 acl_set_session_max_entries(u32 value)
1897 acl_main_t *am = &acl_main;
1898 am->fa_conn_table_max_entries = value;
1902 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1904 acl_main_t *am = &acl_main;
1906 if ((eh < 256) && (value < 2))
1908 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1916 static clib_error_t *
1917 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1919 acl_main_t *am = &acl_main;
1921 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1922 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1923 /* also unapply any ACLs in case the users did not do so. */
1924 macip_acl_interface_del_acl(am, sw_if_index);
1925 acl_interface_reset_inout_acls (sw_if_index, 0);
1926 acl_interface_reset_inout_acls (sw_if_index, 1);
1931 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1933 static clib_error_t *
1934 acl_set_aclplugin_fn (vlib_main_t * vm,
1935 unformat_input_t * input,
1936 vlib_cli_command_t * cmd)
1938 clib_error_t *error = 0;
1942 uword memory_size = 0;
1943 acl_main_t *am = &acl_main;
1945 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1946 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1947 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1951 if (unformat (input, "use-hash-acl-matching %u", &val))
1953 am->use_hash_acl_matching = (val !=0);
1956 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1958 am->l4_match_nonfirst_fragment = (val != 0);
1961 if (unformat (input, "session")) {
1962 if (unformat (input, "table")) {
1963 /* The commands here are for tuning/testing. No user-serviceable parts inside */
1964 if (unformat (input, "max-entries")) {
1965 if (!unformat(input, "%u", &val)) {
1966 error = clib_error_return(0,
1967 "expecting maximum number of entries, got `%U`",
1968 format_unformat_error, input);
1971 acl_set_session_max_entries(val);
1975 if (unformat (input, "hash-table-buckets")) {
1976 if (!unformat(input, "%u", &val)) {
1977 error = clib_error_return(0,
1978 "expecting maximum number of hash table buckets, got `%U`",
1979 format_unformat_error, input);
1982 am->fa_conn_table_hash_num_buckets = val;
1986 if (unformat (input, "hash-table-memory")) {
1987 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1988 error = clib_error_return(0,
1989 "expecting maximum amount of hash table memory, got `%U`",
1990 format_unformat_error, input);
1993 am->fa_conn_table_hash_memory_size = memory_size;
1999 if (unformat (input, "timeout")) {
2000 if (unformat(input, "udp")) {
2001 if(unformat(input, "idle")) {
2002 if (!unformat(input, "%u", &timeout)) {
2003 error = clib_error_return(0,
2004 "expecting timeout value in seconds, got `%U`",
2005 format_unformat_error, input);
2008 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
2013 if (unformat(input, "tcp")) {
2014 if(unformat(input, "idle")) {
2015 if (!unformat(input, "%u", &timeout)) {
2016 error = clib_error_return(0,
2017 "expecting timeout value in seconds, got `%U`",
2018 format_unformat_error, input);
2021 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
2025 if(unformat(input, "transient")) {
2026 if (!unformat(input, "%u", &timeout)) {
2027 error = clib_error_return(0,
2028 "expecting timeout value in seconds, got `%U`",
2029 format_unformat_error, input);
2032 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
2044 static clib_error_t *
2045 acl_show_aclplugin_fn (vlib_main_t * vm,
2046 unformat_input_t * input,
2047 vlib_cli_command_t * cmd)
2049 clib_error_t *error = 0;
2050 acl_main_t *am = &acl_main;
2051 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2054 vnet_sw_interface_t *swif;
2056 if (unformat (input, "sessions"))
2058 u8 * out0 = format(0, "");
2060 u32 show_bihash_verbose = 0;
2061 u32 show_session_thread_id = ~0;
2062 u32 show_session_session_index = ~0;
2063 unformat (input, "thread %u index %u", &show_session_thread_id, &show_session_session_index);
2064 unformat (input, "verbose %u", &show_bihash_verbose);
2066 u64 n_adds = am->fa_session_total_adds;
2067 u64 n_dels = am->fa_session_total_dels;
2068 out0 = format(out0, "Sessions total: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
2070 out0 = format(out0, "\n\nPer-thread data:\n");
2071 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2072 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2073 out0 = format(out0, "Thread #%d:\n", wk);
2074 if (show_session_thread_id == wk && show_session_session_index < pool_len(pw->fa_sessions_pool)) {
2075 out0 = format(out0, " session index %u:\n", show_session_session_index);
2076 fa_session_t *sess = pw->fa_sessions_pool + show_session_session_index;
2077 u64 *m = (u64 *)&sess->info;
2078 out0 = format(out0, " info: %016llx %016llx %016llx %016llx %016llx %016llx\n", m[0], m[1], m[2], m[3], m[4], m[5]);
2079 out0 = format(out0, " sw_if_index: %u\n", sess->sw_if_index);
2080 out0 = format(out0, " tcp_flags_seen: %x\n", sess->tcp_flags_seen.as_u16);
2081 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2082 out0 = format(out0, " thread index: %u\n", sess->thread_index);
2083 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2084 out0 = format(out0, " link next index: %u\n", sess->link_next_idx);
2085 out0 = format(out0, " link prev index: %u\n", sess->link_prev_idx);
2086 out0 = format(out0, " link list id: %u\n", sess->link_list_id);
2088 out0 = format(out0, " connection add/del stats:\n", wk);
2089 pool_foreach (swif, im->sw_interfaces,
2091 u32 sw_if_index = swif->sw_if_index;
2092 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;
2093 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;
2094 out0 = format(out0, " sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2097 out0 = format(out0, " connection timeout type lists:\n", wk);
2099 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2100 u32 head_session_index = pw->fa_conn_list_head[tt];
2101 out0 = format(out0, " fa_conn_list_head[%d]: %d\n", tt, head_session_index);
2102 if (~0 != head_session_index) {
2103 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2104 out0 = format(out0, " last active time: %lu\n", sess->last_active_time);
2105 out0 = format(out0, " link enqueue time: %lu\n", sess->link_enqueue_time);
2109 out0 = format(out0, " Next expiry time: %lu\n", pw->next_expiry_time);
2110 out0 = format(out0, " Requeue until time: %lu\n", pw->requeue_until_time);
2111 out0 = format(out0, " Current time wait interval: %lu\n", pw->current_time_wait_interval);
2112 out0 = format(out0, " Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
2113 out0 = format(out0, " Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
2114 out0 = format(out0, " Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
2115 out0 = format(out0, " Swipe until this time: %lu\n", pw->swipe_end_time);
2116 out0 = format(out0, " sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2117 out0 = format(out0, " pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
2118 out0 = format(out0, " clear in progress: %u\n", pw->clear_in_process);
2119 out0 = format(out0, " interrupt is pending: %d\n", pw->interrupt_is_pending);
2120 out0 = format(out0, " interrupt is needed: %d\n", pw->interrupt_is_needed);
2121 out0 = format(out0, " interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
2123 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
2124 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
2125 foreach_fa_cleaner_counter;
2127 vec_terminate_c_string(out0);
2128 vlib_cli_output(vm, "\n\n%s\n\n", out0);
2129 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2130 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2131 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);
2134 show_fa_sessions_hash(vm, show_bihash_verbose);
2136 else if (unformat (input, "interface"))
2138 u32 sw_if_index = ~0;
2140 u8 * out0 = format(0, "");
2141 unformat (input, "sw_if_index %u", &sw_if_index);
2142 for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
2143 (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
2144 out0 = format(out0, "sw_if_index %d:\n", swi);
2146 if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
2147 (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
2148 out0 = format(out0, " input acl(s): ");
2149 vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
2150 out0 = format(out0, "%d ", *pj);
2152 out0 = format(out0, "\n");
2155 if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
2156 (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2157 out0 = format(out0, " output acl(s): ");
2158 vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2159 out0 = format(out0, "%d ", *pj);
2161 out0 = format(out0, "\n");
2165 vec_terminate_c_string(out0);
2166 vlib_cli_output(vm, "\n%s\n", out0);
2169 else if (unformat (input, "acl"))
2173 u8 * out0 = format(0, "");
2174 unformat (input, "index %u", &acl_index);
2175 for(i=0; i<vec_len(am->acls); i++) {
2176 if (acl_is_not_defined(am, i)) {
2177 /* don't attempt to show the ACLs that do not exist */
2180 if ((acl_index != ~0) && (acl_index != i)) {
2183 out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2186 for(j=0; j<am->acls[i].count; j++) {
2187 r = &am->acls[i].rules[j];
2188 out0 = format(out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2189 out0 = format_acl_action(out0, r->is_permit);
2190 out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2191 out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2192 out0 = format(out0, " proto %d", r->proto);
2193 out0 = format(out0, " sport %d", r->src_port_or_type_first);
2194 if (r->src_port_or_type_first != r->src_port_or_type_last) {
2195 out0 = format(out0, "-%d", r->src_port_or_type_last);
2197 out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2198 if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2199 out0 = format(out0, "-%d", r->dst_port_or_code_last);
2201 if (r->tcp_flags_mask || r->tcp_flags_value) {
2202 out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2204 out0 = format(out0, "\n");
2207 if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2208 out0 = format(out0, " applied inbound on sw_if_index: ");
2209 vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2210 out0 = format(out0, "%d ", *pj);
2212 out0 = format(out0, "\n");
2214 if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2215 out0 = format(out0, " applied outbound on sw_if_index: ");
2216 vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2217 out0 = format(out0, "%d ", *pj);
2219 out0 = format(out0, "\n");
2222 vec_terminate_c_string(out0);
2223 vlib_cli_output(vm, "\n%s\n", out0);
2226 else if (unformat (input, "memory"))
2228 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2229 if (am->acl_mheap) {
2230 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2232 vlib_cli_output (vm, " Not initialized\n");
2234 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2235 if (am->hash_lookup_mheap) {
2236 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2238 vlib_cli_output (vm, " Not initialized\n");
2241 else if (unformat (input, "tables"))
2243 ace_mask_type_entry_t *mte;
2245 u32 sw_if_index = ~0;
2246 int show_acl_hash_info = 0;
2247 int show_applied_info = 0;
2248 int show_mask_type = 0;
2249 int show_bihash = 0;
2250 u32 show_bihash_verbose = 0;
2252 if (unformat (input, "acl")) {
2253 show_acl_hash_info = 1;
2254 /* mask-type is handy to see as well right there */
2256 unformat (input, "index %u", &acl_index);
2257 } else if (unformat (input, "applied")) {
2258 show_applied_info = 1;
2259 unformat (input, "sw_if_index %u", &sw_if_index);
2260 } else if (unformat (input, "mask")) {
2262 } else if (unformat (input, "hash")) {
2264 unformat (input, "verbose %u", &show_bihash_verbose);
2267 if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2268 /* if no qualifiers specified, show all */
2270 show_acl_hash_info = 1;
2271 show_applied_info = 1;
2275 if (show_mask_type) {
2276 vlib_cli_output(vm, "Mask-type entries:");
2278 pool_foreach(mte, am->ace_mask_type_pool,
2280 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
2281 mte - am->ace_mask_type_pool,
2282 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2283 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2288 if (show_acl_hash_info) {
2290 u8 * out0 = format(0, "");
2292 out0 = format(out0, "Mask-ready ACL representations\n");
2293 for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2294 if ((acl_index != ~0) && (acl_index != i)) {
2297 hash_acl_info_t *ha = &am->hash_acl_infos[i];
2298 out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2299 out0 = format(out0, " mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2300 for(j=0; j<vec_len(ha->rules); j++) {
2301 hash_ace_info_t *pa = &ha->rules[j];
2302 m = (u64 *)&pa->match;
2303 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",
2304 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2305 pa->acl_index, pa->ace_index, pa->action,
2306 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2309 vec_terminate_c_string(out0);
2310 vlib_cli_output(vm, "\n%s\n", out0);
2314 if (show_applied_info) {
2316 u8 * out0 = format(0, "");
2317 out0 = format(out0, "Applied lookup entries for interfaces\n");
2319 for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2320 (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2321 (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2322 (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2323 if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2326 out0 = format(out0, "sw_if_index %d:\n", swi);
2327 if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2328 applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2329 out0 = format(out0, " input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2331 if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2332 out0 = format(out0, " input lookup applied entries:\n");
2333 for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2334 applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2335 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2336 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2337 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2341 if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2342 applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2343 out0 = format(out0, " output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2345 if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2346 out0 = format(out0, " output lookup applied entries:\n");
2347 for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2348 applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2349 out0 = format(out0, " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2350 j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2351 pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2356 vec_terminate_c_string(out0);
2357 vlib_cli_output(vm, "\n%s\n", out0);
2362 show_hash_acl_hash(vm, am, show_bihash_verbose);
2368 static clib_error_t *
2369 acl_clear_aclplugin_fn (vlib_main_t * vm,
2370 unformat_input_t * input,
2371 vlib_cli_command_t * cmd)
2373 clib_error_t *error = 0;
2374 acl_main_t *am = &acl_main;
2375 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2376 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2381 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2382 .path = "set acl-plugin",
2383 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2384 .function = acl_set_aclplugin_fn,
2387 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2388 .path = "show acl-plugin",
2389 .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2390 .function = acl_show_aclplugin_fn,
2393 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
2394 .path = "clear acl-plugin sessions",
2395 .short_help = "clear acl-plugin sessions",
2396 .function = acl_clear_aclplugin_fn,
2402 static clib_error_t *
2403 acl_init (vlib_main_t * vm)
2405 acl_main_t *am = &acl_main;
2406 clib_error_t *error = 0;
2407 memset (am, 0, sizeof (*am));
2409 am->vnet_main = vnet_get_main ();
2411 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2413 /* Ask for a correctly-sized block of API message decode slots */
2414 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2415 VL_MSG_FIRST_AVAILABLE);
2417 error = acl_plugin_api_hookup (vm);
2419 /* Add our API messages to the global name_crc hash table */
2420 setup_message_id_table (am, &api_main);
2424 acl_setup_fa_nodes();
2426 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2427 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2428 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2430 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2431 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2432 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2433 vlib_thread_main_t *tm = vlib_get_thread_main ();
2434 vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2438 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2439 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2440 vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2441 vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2442 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2443 pw->fa_conn_list_head[tt] = ~0;
2444 pw->fa_conn_list_tail[tt] = ~0;
2449 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2450 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2451 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2453 am->fa_cleaner_cnt_delete_by_sw_index = 0;
2454 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2455 am->fa_cleaner_cnt_unknown_event = 0;
2456 am->fa_cleaner_cnt_timer_restarted = 0;
2457 am->fa_cleaner_cnt_wait_with_timeout = 0;
2460 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2464 am->l4_match_nonfirst_fragment = 1;
2466 /* use the new fancy hash-based matching */
2467 am->use_hash_acl_matching = 1;
2472 VLIB_INIT_FUNCTION (acl_init);