2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/in_out_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
32 /* define message structures */
34 #include <acl/acl_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <acl/acl_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <acl/acl_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
54 #include "hash_lookup.h"
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
61 /* List of message types that this plugin understands */
63 #define foreach_acl_plugin_api_msg \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace) \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
70 _(ACL_DUMP, acl_dump) \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump)
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)
94 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
95 mheap_t *h = mheap_header (am->acl_mheap);
96 h->flags |= MHEAP_FLAG_THREAD_SAFE;
98 void *oldheap = clib_mem_set_heap (am->acl_mheap);
103 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
105 clib_mem_set_heap (acl_set_heap (am));
106 mheap_t *h = mheap_header (am->acl_mheap);
109 h->flags |= MHEAP_FLAG_VALIDATE;
110 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
115 h->flags &= ~MHEAP_FLAG_VALIDATE;
116 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
121 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
123 clib_mem_set_heap (acl_set_heap (am));
124 mheap_t *h = mheap_header (am->acl_mheap);
127 h->flags |= MHEAP_FLAG_TRACE;
131 h->flags &= ~MHEAP_FLAG_TRACE;
136 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
138 acl_main_t *am = &acl_main;
139 vl_api_acl_plugin_get_version_reply_t *rmp;
140 int msg_size = sizeof (*rmp);
141 vl_api_registration_t *reg;
143 reg = vl_api_client_index_to_registration (mp->client_index);
147 rmp = vl_msg_api_alloc (msg_size);
148 memset (rmp, 0, msg_size);
150 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
151 rmp->context = mp->context;
152 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
153 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
155 vl_api_send_msg (reg, (u8 *) rmp);
159 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
162 vl_api_acl_plugin_control_ping_reply_t *rmp;
163 acl_main_t *am = &acl_main;
167 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
169 rmp->vpe_pid = ntohl (getpid ());
175 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
176 u32 * acl_list_index, u8 * tag)
178 acl_main_t *am = &acl_main;
181 acl_rule_t *acl_new_rules = 0;
184 if (*acl_list_index != ~0)
186 /* They supplied some number, let's see if this ACL exists */
187 if (pool_is_free_index (am->acls, *acl_list_index))
189 /* tried to replace a non-existent ACL, no point doing anything */
191 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
192 *acl_list_index, tag);
193 return VNET_API_ERROR_NO_SUCH_ENTRY;
199 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
200 *acl_list_index, tag);
203 void *oldheap = acl_set_heap (am);
205 /* Create and populate the rules */
207 vec_validate (acl_new_rules, count - 1);
209 for (i = 0; i < count; i++)
211 r = vec_elt_at_index (acl_new_rules, i);
212 memset (r, 0, sizeof (*r));
213 r->is_permit = rules[i].is_permit;
214 r->is_ipv6 = rules[i].is_ipv6;
217 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
218 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
222 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
223 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
225 r->src_prefixlen = rules[i].src_ip_prefix_len;
226 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
227 r->proto = rules[i].proto;
228 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
229 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
230 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
231 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
232 r->tcp_flags_value = rules[i].tcp_flags_value;
233 r->tcp_flags_mask = rules[i].tcp_flags_mask;
236 if (~0 == *acl_list_index)
239 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
240 memset (a, 0, sizeof (*a));
241 /* Will return the newly allocated ACL index */
242 *acl_list_index = a - am->acls;
246 a = am->acls + *acl_list_index;
247 hash_acl_delete (am, *acl_list_index);
248 /* Get rid of the old rules */
252 a->rules = acl_new_rules;
254 memcpy (a->tag, tag, sizeof (a->tag));
255 hash_acl_add (am, *acl_list_index);
256 clib_mem_set_heap (oldheap);
261 acl_del_list (u32 acl_list_index)
263 acl_main_t *am = &acl_main;
266 if (pool_is_free_index (am->acls, acl_list_index))
268 return VNET_API_ERROR_NO_SUCH_ENTRY;
271 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
273 if (vec_len (vec_elt (am->input_sw_if_index_vec_by_acl, acl_list_index))
276 /* ACL is applied somewhere inbound. Refuse to delete */
277 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
280 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
283 (vec_elt (am->output_sw_if_index_vec_by_acl, acl_list_index)) > 0)
285 /* ACL is applied somewhere outbound. Refuse to delete */
286 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
290 void *oldheap = acl_set_heap (am);
291 /* delete any references to the ACL */
292 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
294 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
297 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
299 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
307 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
309 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
312 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
314 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
322 /* delete the hash table data */
324 hash_acl_delete (am, acl_list_index);
325 /* now we can delete the ACL itself */
326 a = pool_elt_at_index (am->acls, acl_list_index);
330 pool_put (am->acls, a);
331 clib_mem_set_heap (oldheap);
335 /* Some aids in ASCII graphing the content */
342 u8 ip4_5tuple_mask[] =
343 _(" dmac smac etype ")
344 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
351 _(" ttl pr checksum ")
360 _("L4 T/U sport dport ")
370 u8 ip6_5tuple_mask[] =
371 _(" dmac smac etype ")
372 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
374 _(0x0000) __ __ __ __
376 _(0x0004) __ __ XX __
378 _(0x0008) XX XX XX XX
379 _(0x000C) XX XX XX XX
380 _(0x0010) XX XX XX XX
381 _(0x0014) XX XX XX XX
383 _(0x0018) XX XX XX XX
384 _(0x001C) XX XX XX XX
385 _(0x0020) XX XX XX XX
386 _(0x0024) XX XX XX XX
387 _("L4T/U sport dport ")
388 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
390 u8 dot1q_5tuple_mask[] =
391 _(" dmac smac dot1q etype ")
392 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
393 _(padpad) __ __ __ __
394 _(padpad) __ __ __ __
395 _(padpad) __ __ __ __
398 u8 dot1ad_5tuple_mask[] =
399 _(" dmac smac dot1ad dot1q etype ")
400 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
401 _(padpad) __ __ __ __
402 _(padpad) __ __ __ __
412 count_skip (u8 * p, u32 size)
414 u64 *p64 = (u64 *) p;
415 /* Be tolerant to null pointer */
419 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
423 return (p64 - (u64 *) p) / 2;
427 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
428 u32 mask_len, u32 next_table_index,
429 u32 miss_next_index, u32 * table_index,
433 u32 memory_size = 2 << 13;
434 u32 skip = count_skip (mask, mask_len);
435 u32 match = (mask_len / 16) - skip;
436 u8 *skip_mask_ptr = mask + 16 * skip;
437 u32 current_data_flag = 0;
438 int current_data_offset = 0;
442 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
443 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
444 memory_size, skip, match,
445 next_table_index, miss_next_index,
446 table_index, current_data_flag,
447 current_data_offset, is_add,
448 1 /* delete_chain */ );
449 clib_mem_set_heap (oldheap);
454 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
455 u32 mask_len, u32 next_table_index,
456 u32 miss_next_index, u32 * table_index,
460 u32 memory_size = 2 << 22;
461 u32 skip = count_skip (mask, mask_len);
462 u32 match = (mask_len / 16) - skip;
463 u8 *skip_mask_ptr = mask + 16 * skip;
464 u32 current_data_flag = 0;
465 int current_data_offset = 0;
470 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
471 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
472 memory_size, skip, match,
473 next_table_index, miss_next_index,
474 table_index, current_data_flag,
475 current_data_offset, is_add,
476 1 /* delete_chain */ );
477 clib_mem_set_heap (oldheap);
482 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
484 vnet_classify_main_t *cm = &vnet_classify_main;
485 u32 ip4_table_index = ~0;
486 u32 ip6_table_index = ~0;
487 u32 dot1q_table_index = ~0;
488 u32 dot1ad_table_index = ~0;
489 void *oldheap = acl_set_heap (am);
491 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
493 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
495 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
497 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
500 /* switch to global heap while calling vnet_* functions */
501 clib_mem_set_heap (cm->vlib_main->heap_base);
502 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
504 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
507 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
508 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
509 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
510 sizeof (ip4_5tuple_mask) - 1, ~0,
511 am->l2_input_classify_next_acl_ip4,
512 &ip4_table_index, 0);
514 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
517 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
518 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
519 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
520 sizeof (ip6_5tuple_mask) - 1, ~0,
521 am->l2_input_classify_next_acl_ip6,
522 &ip6_table_index, 0);
524 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
527 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
528 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
529 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
530 sizeof (ip6_5tuple_mask) - 1, ~0,
531 ~0, &dot1q_table_index, 0);
533 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
536 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
537 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
538 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
539 sizeof (dot1ad_5tuple_mask) - 1, ~0,
540 ~0, &dot1ad_table_index, 0);
542 clib_mem_set_heap (oldheap);
547 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
549 vnet_classify_main_t *cm = &vnet_classify_main;
550 u32 ip4_table_index = ~0;
551 u32 ip6_table_index = ~0;
552 u32 dot1q_table_index = ~0;
553 u32 dot1ad_table_index = ~0;
554 void *oldheap = acl_set_heap (am);
556 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
558 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
560 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
562 vec_validate_init_empty
563 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
565 /* switch to global heap while calling vnet_* functions */
566 clib_mem_set_heap (cm->vlib_main->heap_base);
568 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
570 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
573 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
574 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
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, 0);
580 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
583 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
584 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
585 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
586 sizeof (ip6_5tuple_mask) - 1, ~0,
587 am->l2_output_classify_next_acl_ip6,
588 &ip6_table_index, 0);
590 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
593 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
594 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
595 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
596 sizeof (ip6_5tuple_mask) - 1, ~0,
597 ~0, &dot1q_table_index, 0);
599 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
602 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
603 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
604 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
605 sizeof (dot1ad_5tuple_mask) - 1, ~0,
606 ~0, &dot1ad_table_index, 0);
608 clib_mem_set_heap (oldheap);
613 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
614 u8 is_dot1ad, u8 is_ip6)
616 vnet_classify_main_t *cm = &vnet_classify_main;
626 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
632 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
634 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
635 idx = (is_dot1ad) ? 20 : 16;
638 /* 802.1ad ethertype */
641 /* 802.1q ethertype */
647 /* 802.1q ethertype */
652 /* add sessions to vlan tables per ethernet_type */
656 match[idx + 1] = 0xdd;
662 match[idx + 1] = 0x00;
665 vnet_classify_add_del_session (cm, table_index, match, next_acl,
666 session_idx, 0, 0, 0, 1);
667 /* reset the mask back to being a mask */
669 match[idx + 1] = 0xff;
680 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
682 vnet_classify_main_t *cm = &vnet_classify_main;
683 u32 ip4_table_index = ~0;
684 u32 ip6_table_index = ~0;
685 u32 dot1q_table_index = ~0;
686 u32 dot1ad_table_index = ~0;
689 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
691 /* in case there were previous tables attached */
692 acl_unhook_l2_input_classify (am, sw_if_index);
694 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
695 sizeof (ip4_5tuple_mask) - 1, ~0,
696 am->l2_input_classify_next_acl_ip4,
697 &ip4_table_index, 1);
702 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
703 sizeof (ip6_5tuple_mask) - 1, ~0,
704 am->l2_input_classify_next_acl_ip6,
705 &ip6_table_index, 1);
708 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
709 sizeof (ip4_5tuple_mask) - 1, ~0,
710 am->l2_input_classify_next_acl_ip4,
711 &ip4_table_index, 0);
716 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
717 sizeof (dot1ad_5tuple_mask) - 1, ~0,
718 ~0, &dot1ad_table_index, 1);
720 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
721 sizeof (dot1q_5tuple_mask) - 1,
722 dot1ad_table_index, ~0,
723 &dot1q_table_index, 1);
726 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
727 sizeof (dot1ad_5tuple_mask) - 1, ~0,
728 ~0, &dot1ad_table_index, 0);
729 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
730 sizeof (ip6_5tuple_mask) - 1, ~0,
731 am->l2_input_classify_next_acl_ip6,
732 &ip6_table_index, 0);
733 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
734 sizeof (ip4_5tuple_mask) - 1, ~0,
735 am->l2_input_classify_next_acl_ip4,
736 &ip4_table_index, 0);
741 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
742 ip6_table_index, dot1q_table_index);
746 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
747 sizeof (ip4_5tuple_mask) - 1, ~0,
748 am->l2_input_classify_next_acl_ip4,
749 &ip4_table_index, 0);
750 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
751 sizeof (ip6_5tuple_mask) - 1, ~0,
752 am->l2_input_classify_next_acl_ip6,
753 &ip6_table_index, 0);
754 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
755 sizeof (dot1q_5tuple_mask) - 1, ~0,
756 ~0, &dot1q_table_index, 0);
757 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
758 sizeof (dot1ad_5tuple_mask) - 1, ~0,
759 ~0, &dot1ad_table_index, 0);
763 /* add sessions to vlan tables per ethernet_type */
764 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
765 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
766 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
767 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
769 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
771 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
773 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
775 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
778 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
780 clib_mem_set_heap (prevheap);
785 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
787 vnet_classify_main_t *cm = &vnet_classify_main;
788 u32 ip4_table_index = ~0;
789 u32 ip6_table_index = ~0;
790 u32 dot1q_table_index = ~0;
791 u32 dot1ad_table_index = ~0;
794 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
796 /* in case there were previous tables attached */
797 acl_unhook_l2_output_classify (am, sw_if_index);
799 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
800 sizeof (ip4_5tuple_mask) - 1, ~0,
801 am->l2_output_classify_next_acl_ip4,
802 &ip4_table_index, 1);
806 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
807 sizeof (ip6_5tuple_mask) - 1, ~0,
808 am->l2_output_classify_next_acl_ip6,
809 &ip6_table_index, 1);
812 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
813 sizeof (ip4_5tuple_mask) - 1, ~0,
814 am->l2_output_classify_next_acl_ip4,
815 &ip4_table_index, 0);
820 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
821 sizeof (dot1ad_5tuple_mask) - 1, ~0,
822 ~0, &dot1ad_table_index, 1);
824 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
825 sizeof (dot1q_5tuple_mask) - 1,
826 dot1ad_table_index, ~0,
827 &dot1q_table_index, 1);
830 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
831 sizeof (dot1ad_5tuple_mask) - 1, ~0,
832 ~0, &dot1ad_table_index, 0);
833 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
834 sizeof (ip6_5tuple_mask) - 1, ~0,
835 am->l2_output_classify_next_acl_ip6,
836 &ip6_table_index, 0);
837 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
838 sizeof (ip4_5tuple_mask) - 1, ~0,
839 am->l2_output_classify_next_acl_ip4,
840 &ip4_table_index, 0);
845 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
846 ip6_table_index, dot1q_table_index);
849 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
850 sw_if_index, ip4_table_index, ip6_table_index);
853 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
854 sizeof (ip6_5tuple_mask) - 1, ~0,
855 am->l2_output_classify_next_acl_ip6,
856 &ip6_table_index, 0);
857 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
858 sizeof (ip4_5tuple_mask) - 1, ~0,
859 am->l2_output_classify_next_acl_ip4,
860 &ip4_table_index, 0);
861 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
862 sizeof (dot1q_5tuple_mask) - 1, ~0,
863 ~0, &dot1q_table_index, 0);
864 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
865 sizeof (dot1ad_5tuple_mask) - 1, ~0,
866 ~0, &dot1ad_table_index, 0);
870 /* add sessions to vlan tables per ethernet_type */
871 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
872 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
873 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
874 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
876 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
878 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
880 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
882 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
885 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
887 clib_mem_set_heap (prevheap);
892 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
898 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
900 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
902 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
906 rv = acl_hook_l2_input_classify (am, sw_if_index);
910 rv = acl_unhook_l2_input_classify (am, sw_if_index);
917 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
923 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
925 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
927 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
931 rv = acl_hook_l2_output_classify (am, sw_if_index);
935 rv = acl_unhook_l2_output_classify (am, sw_if_index);
942 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
944 return (pool_is_free_index (am->acls, acl_list_index));
949 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
951 acl_main_t *am = &acl_main;
952 if (acl_is_not_defined (am, acl_list_index))
954 /* ACL is not defined. Can not apply */
955 return VNET_API_ERROR_NO_SUCH_ENTRY;
957 void *oldheap = acl_set_heap (am);
961 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
963 u32 index = vec_search (am->input_acl_vec_by_sw_if_index[sw_if_index],
965 if (index < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
968 ("ACL %d is already applied inbound on sw_if_index %d (index %d)",
969 acl_list_index, sw_if_index, index);
970 /* the entry is already there */
971 clib_mem_set_heap (oldheap);
972 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
974 /* if there was no ACL applied before, enable the ACL processing */
975 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0)
977 acl_interface_in_enable_disable (am, sw_if_index, 1);
979 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
981 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
982 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
987 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
989 u32 index = vec_search (am->output_acl_vec_by_sw_if_index[sw_if_index],
991 if (index < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
994 ("ACL %d is already applied outbound on sw_if_index %d (index %d)",
995 acl_list_index, sw_if_index, index);
996 /* the entry is already there */
997 clib_mem_set_heap (oldheap);
998 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1000 /* if there was no ACL applied before, enable the ACL processing */
1001 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1003 acl_interface_out_enable_disable (am, sw_if_index, 1);
1005 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
1006 &acl_list_index, 1);
1007 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
1008 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index],
1011 clib_mem_set_heap (oldheap);
1017 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1019 acl_main_t *am = &acl_main;
1021 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1022 void *oldheap = acl_set_heap (am);
1025 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1026 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1029 if (acl_list_index ==
1030 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
1032 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
1038 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1041 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1044 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1046 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1047 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1052 /* If there is no more ACLs applied on an interface, disable ACL processing */
1053 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1055 acl_interface_in_enable_disable (am, sw_if_index, 0);
1060 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1062 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
1064 if (acl_list_index ==
1065 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
1067 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
1073 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1076 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1079 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1081 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1082 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1087 /* If there is no more ACLs applied on an interface, disable ACL processing */
1088 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1090 acl_interface_out_enable_disable (am, sw_if_index, 0);
1093 clib_mem_set_heap (oldheap);
1098 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
1100 acl_main_t *am = &acl_main;
1102 void *oldheap = acl_set_heap (am);
1105 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1106 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1108 acl_interface_in_enable_disable (am, sw_if_index, 0);
1111 for (i = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1114 u32 acl_list_index =
1115 am->input_acl_vec_by_sw_if_index[sw_if_index][i];
1116 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1117 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1120 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1123 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1125 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1131 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1135 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1136 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1138 acl_interface_out_enable_disable (am, sw_if_index, 0);
1141 for (i = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1144 u32 acl_list_index =
1145 am->output_acl_vec_by_sw_if_index[sw_if_index][i];
1146 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1147 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1150 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1153 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1155 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1161 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1163 clib_mem_set_heap (oldheap);
1167 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1170 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1171 acl_main_t *am = &acl_main;
1175 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
1178 hash_acl_apply (am, sw_if_index, is_input, acl_list_index);
1183 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1185 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
1198 u32 arp_table_index;
1199 u32 dot1q_table_index;
1200 u32 dot1ad_table_index;
1201 } macip_match_type_t;
1204 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1210 for (i = 0; i < vec_len (mv); i++)
1212 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1213 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1223 /* Get metric used to sort match types.
1224 The more specific and the more often seen - the bigger the metric */
1226 match_type_metric (macip_match_type_t * m)
1228 unsigned int mac_bits_set = 0;
1229 unsigned int mac_byte;
1231 for (i = 0; i < 6; i++)
1233 mac_byte = m->mac_mask[i];
1234 for (; mac_byte; mac_byte >>= 1)
1235 mac_bits_set += mac_byte & 1;
1238 * Attempt to place the more specific and the more used rules on top.
1239 * There are obvious caveat corner cases to this, but they do not
1240 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1241 * going with a wildcard IPv4 with a specific MAC).
1243 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1247 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1249 /* Ascending sort based on the metric values */
1250 return match_type_metric (m1) - match_type_metric (m2);
1253 /* Get the offset of L3 source within ethernet packet */
1255 get_l3_src_offset (int is6)
1258 return (sizeof (ethernet_header_t) +
1259 offsetof (ip6_header_t, src_address));
1261 return (sizeof (ethernet_header_t) +
1262 offsetof (ip4_header_t, src_address));
1266 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1268 macip_match_type_t *mvec = NULL;
1269 macip_match_type_t *mt;
1270 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1272 u32 match_type_index;
1275 vnet_classify_main_t *cm = &vnet_classify_main;
1277 /* Count the number of different types of rules */
1278 for (i = 0; i < a->count; i++)
1282 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1283 a->rules[i].src_prefixlen,
1284 a->rules[i].is_ipv6)))
1286 match_type_index = vec_len (mvec);
1287 vec_validate (mvec, match_type_index);
1288 memcpy (mvec[match_type_index].mac_mask,
1289 a->rules[i].src_mac_mask, 6);
1290 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1291 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1292 mvec[match_type_index].table_index = ~0;
1293 mvec[match_type_index].dot1q_table_index = ~0;
1294 mvec[match_type_index].dot1ad_table_index = ~0;
1296 mvec[match_type_index].count++;
1298 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1299 vec_sort_with_function (mvec, match_type_compare);
1300 /* Create the classifier tables */
1302 /* First add ARP tables */
1303 vec_foreach (mt, mvec)
1306 int is6 = mt->is_ipv6;
1308 mt->arp_table_index = ~0;
1311 memset (mask, 0, sizeof (mask));
1312 memcpy (&mask[6], mt->mac_mask, 6);
1313 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1314 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1316 for (i = 0; i < (mt->prefix_len / 8); i++)
1317 mask[14 + 14 + i] = 0xff;
1318 if (mt->prefix_len % 8)
1319 mask[14 + 14 + (mt->prefix_len / 8)] =
1320 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1322 mask_len = ((14 + 14 + ((mt->prefix_len + 7) / 8) +
1323 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1324 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1325 (~0 == last_table) ? 0 : ~0,
1326 &mt->arp_table_index, 1);
1327 last_table = mt->arp_table_index;
1330 /* Now add IP[46] tables */
1331 vec_foreach (mt, mvec)
1334 int is6 = mt->is_ipv6;
1335 int l3_src_offs = get_l3_src_offset (is6);
1337 u32 *last_tag_table;
1340 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1343 for (tags = 2; tags >= 0; tags--)
1345 memset (mask, 0, sizeof (mask));
1346 memcpy (&mask[6], mt->mac_mask, 6);
1351 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1352 last_tag_table = &mt->table_index;
1355 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1356 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1357 last_tag_table = &mt->dot1q_table_index;
1360 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1361 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1362 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1363 last_tag_table = &mt->dot1ad_table_index;
1366 for (i = 0; i < (mt->prefix_len / 8); i++)
1368 mask[l3_src_offs + i] = 0xff;
1370 if (mt->prefix_len % 8)
1372 mask[l3_src_offs + (mt->prefix_len / 8)] =
1373 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1376 * Round-up the number of bytes needed to store the prefix,
1377 * and round up the number of vectors too
1379 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1380 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1381 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1382 (~0 == last_table) ? 0 : ~0,
1384 last_table = *last_tag_table;
1386 memset (&mask[12], 0, sizeof (mask) - 12);
1390 a->ip4_table_index = last_table;
1391 a->ip6_table_index = last_table;
1392 a->l2_table_index = last_table;
1394 /* Populate the classifier tables with rules from the MACIP ACL */
1395 for (i = 0; i < a->count; i++)
1399 int is6 = a->rules[i].is_ipv6;
1400 int l3_src_offs = get_l3_src_offset (is6);
1405 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1406 a->rules[i].src_prefixlen,
1407 a->rules[i].is_ipv6);
1408 ASSERT (match_type_index != ~0);
1411 for (tags = 2; tags >= 0; tags--)
1413 memset (mask, 0, sizeof (mask));
1414 memcpy (&mask[6], a->rules[i].src_mac, 6);
1419 tag_table = mvec[match_type_index].table_index;
1423 tag_table = mvec[match_type_index].dot1q_table_index;
1429 tag_table = mvec[match_type_index].dot1ad_table_index;
1439 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1441 mask[eth + 1] = 0xdd;
1445 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1447 mask[eth + 1] = 0x00;
1450 /* add session to table mvec[match_type_index].table_index; */
1451 vnet_classify_add_del_session (cm, tag_table,
1452 mask, a->rules[i].is_permit ? ~0 : 0,
1453 i, 0, action, metadata, 1);
1454 memset (&mask[12], 0, sizeof (mask) - 12);
1458 /* add ARP table entry too */
1459 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1461 memset (mask, 0, sizeof (mask));
1462 memcpy (&mask[6], a->rules[i].src_mac, 6);
1465 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1466 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1467 vnet_classify_add_del_session (cm,
1469 [match_type_index].arp_table_index,
1470 mask, a->rules[i].is_permit ? ~0 : 0,
1471 i, 0, action, metadata, 1);
1478 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1480 vnet_classify_main_t *cm = &vnet_classify_main;
1481 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1483 if (a->ip4_table_index != ~0)
1485 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1486 &a->ip4_table_index, 0);
1487 a->ip4_table_index = ~0;
1489 if (a->ip6_table_index != ~0)
1491 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1492 &a->ip6_table_index, 0);
1493 a->ip6_table_index = ~0;
1495 if (a->l2_table_index != ~0)
1497 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1499 a->l2_table_index = ~0;
1504 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1510 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1512 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1513 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1515 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1516 a->ip6_table_index, a->l2_table_index,
1518 /* return the first unhappy outcome but make try to plough through. */
1525 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1526 u32 * acl_list_index, u8 * tag)
1528 acl_main_t *am = &acl_main;
1529 macip_acl_list_t *a;
1530 macip_acl_rule_t *r;
1531 macip_acl_rule_t *acl_new_rules = 0;
1535 if (*acl_list_index != ~0)
1537 /* They supplied some number, let's see if this MACIP ACL exists */
1538 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1540 /* tried to replace a non-existent ACL, no point doing anything */
1542 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1543 *acl_list_index, tag);
1544 return VNET_API_ERROR_NO_SUCH_ENTRY;
1551 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1554 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1555 if (~0 != *acl_list_index)
1556 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1557 void *oldheap = acl_set_heap (am);
1558 /* Create and populate the rules */
1560 vec_validate (acl_new_rules, count - 1);
1562 for (i = 0; i < count; i++)
1564 r = &acl_new_rules[i];
1565 r->is_permit = rules[i].is_permit;
1566 r->is_ipv6 = rules[i].is_ipv6;
1567 memcpy (&r->src_mac, rules[i].src_mac, 6);
1568 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1569 if (rules[i].is_ipv6)
1570 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1572 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1573 r->src_prefixlen = rules[i].src_ip_prefix_len;
1576 if (~0 == *acl_list_index)
1579 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1580 memset (a, 0, sizeof (*a));
1581 /* Will return the newly allocated ACL index */
1582 *acl_list_index = a - am->macip_acls;
1586 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1589 vec_free (a->rules);
1591 macip_destroy_classify_tables (am, *acl_list_index);
1594 a->rules = acl_new_rules;
1596 memcpy (a->tag, tag, sizeof (a->tag));
1598 /* Create and populate the classifer tables */
1599 macip_create_classify_tables (am, *acl_list_index);
1600 clib_mem_set_heap (oldheap);
1601 /* If the ACL was already applied somewhere, reapply the newly created tables */
1603 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1608 /* No check for validity of sw_if_index - the callers were supposed to validate */
1611 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1614 u32 macip_acl_index;
1615 macip_acl_list_t *a;
1616 void *oldheap = acl_set_heap (am);
1617 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1618 clib_mem_set_heap (oldheap);
1619 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1620 /* No point in deleting MACIP ACL which is not applied */
1621 if (~0 == macip_acl_index)
1622 return VNET_API_ERROR_NO_SUCH_ENTRY;
1623 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1624 /* remove the classifier tables off the interface L2 ACL */
1626 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1627 a->ip6_table_index, a->l2_table_index, 0);
1628 /* Unset the MACIP ACL index */
1629 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1633 /* No check for validity of sw_if_index - the callers were supposed to validate */
1636 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1637 u32 macip_acl_index)
1639 macip_acl_list_t *a;
1641 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1643 return VNET_API_ERROR_NO_SUCH_ENTRY;
1645 void *oldheap = acl_set_heap (am);
1646 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1647 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1648 clib_mem_set_heap (oldheap);
1649 /* If there already a MACIP ACL applied, unapply it */
1650 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1651 macip_acl_interface_del_acl (am, sw_if_index);
1652 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1654 /* Apply the classifier tables for L2 ACLs */
1656 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1657 a->ip6_table_index, a->l2_table_index, 1);
1662 macip_acl_del_list (u32 acl_list_index)
1664 acl_main_t *am = &acl_main;
1665 macip_acl_list_t *a;
1667 if (pool_is_free_index (am->macip_acls, acl_list_index))
1669 return VNET_API_ERROR_NO_SUCH_ENTRY;
1672 /* delete any references to the ACL */
1673 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1675 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1677 macip_acl_interface_del_acl (am, i);
1681 void *oldheap = acl_set_heap (am);
1682 /* Now that classifier tables are detached, clean them up */
1683 macip_destroy_classify_tables (am, acl_list_index);
1685 /* now we can delete the ACL itself */
1686 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1689 vec_free (a->rules);
1691 pool_put (am->macip_acls, a);
1692 clib_mem_set_heap (oldheap);
1698 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1701 acl_main_t *am = &acl_main;
1705 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1709 rv = macip_acl_interface_del_acl (am, sw_if_index);
1715 * If the client does not allocate enough memory for a variable-length
1716 * message, and then proceed to use it as if the full memory allocated,
1717 * absent the check we happily consume that on the VPP side, and go
1718 * along as if nothing happened. However, the resulting
1719 * effects range from just garbage in the API decode
1720 * (because the decoder snoops too far), to potential memory
1723 * This verifies that the actual length of the message is
1724 * at least expected_len, and complains loudly if it is not.
1726 * A failing check here is 100% a software bug on the API user side,
1727 * so we might as well yell.
1731 verify_message_len (void *mp, u32 expected_len, char *where)
1733 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1734 if (supplied_len < expected_len)
1736 clib_warning ("%s: Supplied message length %d is less than expected %d",
1737 where, supplied_len, expected_len);
1746 /* API message handler */
1748 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1750 vl_api_acl_add_replace_reply_t *rmp;
1751 acl_main_t *am = &acl_main;
1753 u32 acl_list_index = ntohl (mp->acl_index);
1754 u32 acl_count = ntohl (mp->count);
1755 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1757 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1759 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1763 rv = VNET_API_ERROR_INVALID_VALUE;
1767 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1769 rmp->acl_index = htonl(acl_list_index);
1775 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1777 acl_main_t *am = &acl_main;
1778 vl_api_acl_del_reply_t *rmp;
1781 rv = acl_del_list (ntohl (mp->acl_index));
1783 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1787 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1789 acl_main_t *am = &acl_main;
1790 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1791 u32 sw_if_index = ntohl (mp->sw_if_index);
1792 vl_api_acl_interface_add_del_reply_t *rmp;
1795 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1796 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1799 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1800 mp->is_input, ntohl (mp->acl_index));
1802 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1806 vl_api_acl_interface_set_acl_list_t_handler
1807 (vl_api_acl_interface_set_acl_list_t * mp)
1809 acl_main_t *am = &acl_main;
1810 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1813 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1814 u32 sw_if_index = ntohl (mp->sw_if_index);
1816 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1817 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1820 acl_interface_reset_inout_acls (sw_if_index, 0);
1821 acl_interface_reset_inout_acls (sw_if_index, 1);
1823 for (i = 0; i < mp->count; i++)
1825 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1827 /* ACL does not exist, so we can not apply it */
1828 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1833 for (i = 0; i < mp->count; i++)
1835 acl_interface_add_del_inout_acl (sw_if_index, 1,
1837 ntohl (mp->acls[i]));
1842 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1846 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1848 api_rule->is_permit = r->is_permit;
1849 api_rule->is_ipv6 = r->is_ipv6;
1852 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1853 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1857 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1858 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1860 api_rule->src_ip_prefix_len = r->src_prefixlen;
1861 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1862 api_rule->proto = r->proto;
1863 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1864 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1865 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1866 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1867 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1868 api_rule->tcp_flags_value = r->tcp_flags_value;
1872 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
1873 acl_list_t * acl, u32 context)
1875 vl_api_acl_details_t *mp;
1876 vl_api_acl_rule_t *rules;
1878 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1879 void *oldheap = acl_set_heap (am);
1881 mp = vl_msg_api_alloc (msg_size);
1882 memset (mp, 0, msg_size);
1883 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1885 /* fill in the message */
1886 mp->context = context;
1887 mp->count = htonl (acl->count);
1888 mp->acl_index = htonl (acl - am->acls);
1889 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1890 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1892 for (i = 0; i < acl->count; i++)
1894 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1897 clib_mem_set_heap (oldheap);
1898 vl_api_send_msg (reg, (u8 *) mp);
1903 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1905 acl_main_t *am = &acl_main;
1909 vl_api_registration_t *reg;
1911 reg = vl_api_client_index_to_registration (mp->client_index);
1915 if (mp->acl_index == ~0)
1918 /* Just dump all ACLs */
1919 pool_foreach (acl, am->acls,
1921 send_acl_details(am, reg, acl, mp->context);
1927 acl_index = ntohl (mp->acl_index);
1928 if (!pool_is_free_index (am->acls, acl_index))
1930 acl = pool_elt_at_index (am->acls, acl_index);
1931 send_acl_details (am, reg, acl, mp->context);
1937 /* FIXME API: should we signal an error here at all ? */
1943 send_acl_interface_list_details (acl_main_t * am,
1944 vl_api_registration_t * reg,
1945 u32 sw_if_index, u32 context)
1947 vl_api_acl_interface_list_details_t *mp;
1953 void *oldheap = acl_set_heap (am);
1955 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1956 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1958 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1959 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1960 count = n_input + n_output;
1962 msg_size = sizeof (*mp);
1963 msg_size += sizeof (mp->acls[0]) * count;
1965 mp = vl_msg_api_alloc (msg_size);
1966 memset (mp, 0, msg_size);
1968 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1970 /* fill in the message */
1971 mp->context = context;
1972 mp->sw_if_index = htonl (sw_if_index);
1974 mp->n_input = n_input;
1975 for (i = 0; i < n_input; i++)
1977 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1979 for (i = 0; i < n_output; i++)
1981 mp->acls[n_input + i] =
1982 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1984 clib_mem_set_heap (oldheap);
1985 vl_api_send_msg (reg, (u8 *) mp);
1989 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1992 acl_main_t *am = &acl_main;
1993 vnet_sw_interface_t *swif;
1994 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1997 vl_api_registration_t *reg;
1999 reg = vl_api_client_index_to_registration (mp->client_index);
2003 if (mp->sw_if_index == ~0)
2006 pool_foreach (swif, im->sw_interfaces,
2008 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2014 sw_if_index = ntohl (mp->sw_if_index);
2015 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2016 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2020 /* MACIP ACL API handlers */
2023 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2025 vl_api_macip_acl_add_reply_t *rmp;
2026 acl_main_t *am = &acl_main;
2028 u32 acl_list_index = ~0;
2029 u32 acl_count = ntohl (mp->count);
2030 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2032 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2034 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2038 rv = VNET_API_ERROR_INVALID_VALUE;
2042 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2044 rmp->acl_index = htonl(acl_list_index);
2050 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2052 vl_api_macip_acl_add_replace_reply_t *rmp;
2053 acl_main_t *am = &acl_main;
2055 u32 acl_list_index = ntohl (mp->acl_index);
2056 u32 acl_count = ntohl (mp->count);
2057 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2059 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2061 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2065 rv = VNET_API_ERROR_INVALID_VALUE;
2069 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2071 rmp->acl_index = htonl(acl_list_index);
2077 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2079 acl_main_t *am = &acl_main;
2080 vl_api_macip_acl_del_reply_t *rmp;
2083 rv = macip_acl_del_list (ntohl (mp->acl_index));
2085 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2089 vl_api_macip_acl_interface_add_del_t_handler
2090 (vl_api_macip_acl_interface_add_del_t * mp)
2092 acl_main_t *am = &acl_main;
2093 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2095 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2096 u32 sw_if_index = ntohl (mp->sw_if_index);
2098 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2099 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2102 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2103 ntohl (mp->acl_index));
2105 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2109 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2110 macip_acl_list_t * acl, u32 context)
2112 vl_api_macip_acl_details_t *mp;
2113 vl_api_macip_acl_rule_t *rules;
2114 macip_acl_rule_t *r;
2116 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2118 mp = vl_msg_api_alloc (msg_size);
2119 memset (mp, 0, msg_size);
2120 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2122 /* fill in the message */
2123 mp->context = context;
2126 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2127 mp->count = htonl (acl->count);
2128 mp->acl_index = htonl (acl - am->macip_acls);
2130 for (i = 0; i < acl->count; i++)
2133 rules[i].is_permit = r->is_permit;
2134 rules[i].is_ipv6 = r->is_ipv6;
2135 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2136 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2137 sizeof (r->src_mac_mask));
2139 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2140 sizeof (r->src_ip_addr.ip6));
2142 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2143 sizeof (r->src_ip_addr.ip4));
2144 rules[i].src_ip_prefix_len = r->src_prefixlen;
2149 /* No martini, no party - no ACL applied to this interface. */
2154 vl_api_send_msg (reg, (u8 *) mp);
2159 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2161 acl_main_t *am = &acl_main;
2162 macip_acl_list_t *acl;
2164 vl_api_registration_t *reg;
2166 reg = vl_api_client_index_to_registration (mp->client_index);
2170 if (mp->acl_index == ~0)
2172 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2173 pool_foreach (acl, am->macip_acls, (
2175 send_macip_acl_details (am, reg,
2184 u32 acl_index = ntohl (mp->acl_index);
2185 if (!pool_is_free_index (am->macip_acls, acl_index))
2187 acl = pool_elt_at_index (am->macip_acls, acl_index);
2188 send_macip_acl_details (am, reg, acl, mp->context);
2194 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2197 acl_main_t *am = &acl_main;
2198 vl_api_macip_acl_interface_get_reply_t *rmp;
2199 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2200 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2201 vl_api_registration_t *reg;
2204 reg = vl_api_client_index_to_registration (mp->client_index);
2208 rmp = vl_msg_api_alloc (msg_size);
2209 memset (rmp, 0, msg_size);
2211 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2212 rmp->context = mp->context;
2213 rmp->count = htonl (count);
2214 for (i = 0; i < count; i++)
2216 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2219 vl_api_send_msg (reg, (u8 *) rmp);
2223 send_macip_acl_interface_list_details (acl_main_t * am,
2224 vl_api_registration_t * reg,
2226 u32 acl_index, u32 context)
2228 vl_api_macip_acl_interface_list_details_t *rmp;
2229 /* at this time there is only ever 1 mac ip acl per interface */
2230 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2232 rmp = vl_msg_api_alloc (msg_size);
2233 memset (rmp, 0, msg_size);
2235 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2237 /* fill in the message */
2238 rmp->context = context;
2240 rmp->sw_if_index = htonl (sw_if_index);
2241 rmp->acls[0] = htonl (acl_index);
2243 vl_api_send_msg (reg, (u8 *) rmp);
2247 vl_api_macip_acl_interface_list_dump_t_handler
2248 (vl_api_macip_acl_interface_list_dump_t * mp)
2250 vl_api_registration_t *reg;
2251 acl_main_t *am = &acl_main;
2252 u32 sw_if_index = ntohl (mp->sw_if_index);
2254 reg = vl_api_client_index_to_registration (mp->client_index);
2258 if (sw_if_index == ~0)
2260 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2262 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2264 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2265 am->macip_acl_by_sw_if_index
2273 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2275 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2276 am->macip_acl_by_sw_if_index
2277 [sw_if_index], mp->context);
2282 /* Set up the API message handling tables */
2283 static clib_error_t *
2284 acl_plugin_api_hookup (vlib_main_t * vm)
2286 acl_main_t *am = &acl_main;
2288 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2290 vl_api_##n##_t_handler, \
2292 vl_api_##n##_t_endian, \
2293 vl_api_##n##_t_print, \
2294 sizeof(vl_api_##n##_t), 1);
2295 foreach_acl_plugin_api_msg;
2301 #define vl_msg_name_crc_list
2302 #include <acl/acl_all_api_h.h>
2303 #undef vl_msg_name_crc_list
2306 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2308 #define _(id,n,crc) \
2309 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2310 foreach_vl_msg_name_crc_acl;
2315 acl_setup_fa_nodes (void)
2317 vlib_main_t *vm = vlib_get_main ();
2318 acl_main_t *am = &acl_main;
2319 vlib_node_t *n, *n4, *n6;
2321 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2322 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2323 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2326 am->l2_input_classify_next_acl_ip4 =
2327 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2328 am->l2_input_classify_next_acl_ip6 =
2329 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2331 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2332 l2input_get_feat_names (),
2333 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2335 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2336 l2input_get_feat_names (),
2337 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2340 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2341 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2342 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2344 am->l2_output_classify_next_acl_ip4 =
2345 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2346 am->l2_output_classify_next_acl_ip6 =
2347 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2349 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2350 l2output_get_feat_names (),
2351 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2353 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2354 l2output_get_feat_names (),
2355 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2359 acl_set_timeout_sec (int timeout_type, u32 value)
2361 acl_main_t *am = &acl_main;
2362 clib_time_t *ct = &am->vlib_main->clib_time;
2364 if (timeout_type < ACL_N_TIMEOUTS)
2366 am->session_timeout_sec[timeout_type] = value;
2370 clib_warning ("Unknown timeout type %d", timeout_type);
2373 am->session_timeout[timeout_type] =
2374 (u64) (((f64) value) / ct->seconds_per_clock);
2378 acl_set_session_max_entries (u32 value)
2380 acl_main_t *am = &acl_main;
2381 am->fa_conn_table_max_entries = value;
2385 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2387 acl_main_t *am = &acl_main;
2389 if ((eh < 256) && (value < 2))
2391 am->fa_ipv6_known_eh_bitmap =
2392 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2400 static clib_error_t *
2401 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2403 acl_main_t *am = &acl_main;
2404 if (0 == am->acl_mheap)
2406 /* ACL heap is not initialized, so definitely nothing to do. */
2411 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2412 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2414 /* also unapply any ACLs in case the users did not do so. */
2415 macip_acl_interface_del_acl (am, sw_if_index);
2416 acl_interface_reset_inout_acls (sw_if_index, 0);
2417 acl_interface_reset_inout_acls (sw_if_index, 1);
2422 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2426 static clib_error_t *
2427 acl_set_aclplugin_fn (vlib_main_t * vm,
2428 unformat_input_t * input, vlib_cli_command_t * cmd)
2430 clib_error_t *error = 0;
2434 uword memory_size = 0;
2435 acl_main_t *am = &acl_main;
2437 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2439 if (!acl_set_skip_ipv6_eh (eh_val, val))
2441 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2445 if (unformat (input, "use-hash-acl-matching %u", &val))
2447 am->use_hash_acl_matching = (val != 0);
2450 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2452 am->l4_match_nonfirst_fragment = (val != 0);
2455 if (unformat (input, "heap"))
2457 if (unformat (input, "main"))
2459 if (unformat (input, "validate %u", &val))
2460 acl_plugin_acl_set_validate_heap (am, val);
2461 else if (unformat (input, "trace %u", &val))
2462 acl_plugin_acl_set_trace_heap (am, val);
2465 else if (unformat (input, "hash"))
2467 if (unformat (input, "validate %u", &val))
2468 acl_plugin_hash_acl_set_validate_heap (am, val);
2469 else if (unformat (input, "trace %u", &val))
2470 acl_plugin_hash_acl_set_trace_heap (am, val);
2475 if (unformat (input, "session"))
2477 if (unformat (input, "table"))
2479 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2480 if (unformat (input, "max-entries"))
2482 if (!unformat (input, "%u", &val))
2484 error = clib_error_return (0,
2485 "expecting maximum number of entries, got `%U`",
2486 format_unformat_error, input);
2491 acl_set_session_max_entries (val);
2495 if (unformat (input, "hash-table-buckets"))
2497 if (!unformat (input, "%u", &val))
2499 error = clib_error_return (0,
2500 "expecting maximum number of hash table buckets, got `%U`",
2501 format_unformat_error, input);
2506 am->fa_conn_table_hash_num_buckets = val;
2510 if (unformat (input, "hash-table-memory"))
2512 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2514 error = clib_error_return (0,
2515 "expecting maximum amount of hash table memory, got `%U`",
2516 format_unformat_error, input);
2521 am->fa_conn_table_hash_memory_size = memory_size;
2527 if (unformat (input, "timeout"))
2529 if (unformat (input, "udp"))
2531 if (unformat (input, "idle"))
2533 if (!unformat (input, "%u", &timeout))
2535 error = clib_error_return (0,
2536 "expecting timeout value in seconds, got `%U`",
2537 format_unformat_error,
2543 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2548 if (unformat (input, "tcp"))
2550 if (unformat (input, "idle"))
2552 if (!unformat (input, "%u", &timeout))
2554 error = clib_error_return (0,
2555 "expecting timeout value in seconds, got `%U`",
2556 format_unformat_error,
2562 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2566 if (unformat (input, "transient"))
2568 if (!unformat (input, "%u", &timeout))
2570 error = clib_error_return (0,
2571 "expecting timeout value in seconds, got `%U`",
2572 format_unformat_error,
2578 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2592 my_format_mac_address (u8 * s, va_list * args)
2594 u8 *a = va_arg (*args, u8 *);
2595 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2596 a[0], a[1], a[2], a[3], a[4], a[5]);
2600 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2602 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2604 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2605 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2606 format_ip46_address, &a->src_ip_addr,
2607 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2609 my_format_mac_address, a->src_mac,
2610 my_format_mac_address, a->src_mac_mask);
2615 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2617 vlib_main_t *vm = am->vlib_main;
2620 /* Don't try to print someone else's memory */
2621 if (macip_acl_index > vec_len (am->macip_acls))
2624 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2625 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2627 vlib_cli_output (vm,
2628 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2629 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2631 vlib_cli_output (vm,
2632 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2633 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2634 for (i = 0; i < vec_len (a->rules); i++)
2635 vlib_cli_output (vm, " rule %d: %U\n", i,
2636 my_macip_acl_rule_t_pretty_format,
2637 vec_elt_at_index (a->rules, i));
2641 static clib_error_t *
2642 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2643 unformat_input_t * input,
2644 vlib_cli_command_t * cmd)
2646 clib_error_t *error = 0;
2647 acl_main_t *am = &acl_main;
2649 for (i = 0; i < vec_len (am->macip_acls); i++)
2650 macip_acl_print (am, i);
2654 static clib_error_t *
2655 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2656 unformat_input_t * input,
2657 vlib_cli_command_t * cmd)
2659 clib_error_t *error = 0;
2660 acl_main_t *am = &acl_main;
2662 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2664 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
2665 vec_elt (am->macip_acl_by_sw_if_index, i));
2670 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
2672 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
2675 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
2676 am->acls[acl_index].count, am->acls[acl_index].tag);
2678 PRINT_AND_RESET (vm, out0);
2679 for (j = 0; j < am->acls[acl_index].count; j++)
2681 r = &am->acls[acl_index].rules[j];
2682 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2683 out0 = format_acl_action (out0, r->is_permit);
2684 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
2685 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2688 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
2689 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
2690 out0 = format (out0, " proto %d", r->proto);
2691 out0 = format (out0, " sport %d", r->src_port_or_type_first);
2692 if (r->src_port_or_type_first != r->src_port_or_type_last)
2694 out0 = format (out0, "-%d", r->src_port_or_type_last);
2696 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
2697 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
2699 out0 = format (out0, "-%d", r->dst_port_or_code_last);
2701 if (r->tcp_flags_mask || r->tcp_flags_value)
2704 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
2707 out0 = format (out0, "\n");
2708 PRINT_AND_RESET (vm, out0);
2712 #undef PRINT_AND_RESET
2715 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2718 vlib_main_t *vm = am->vlib_main;
2720 for (i = 0; i < vec_len (am->acls); i++)
2722 if (acl_is_not_defined (am, i))
2724 /* don't attempt to show the ACLs that do not exist */
2727 if ((acl_index != ~0) && (acl_index != i))
2731 acl_print_acl (vm, am, i);
2733 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2735 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
2736 format_vec32, am->input_sw_if_index_vec_by_acl[i],
2739 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2741 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
2742 format_vec32, am->output_sw_if_index_vec_by_acl[i],
2748 static clib_error_t *
2749 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2750 unformat_input_t * input, vlib_cli_command_t * cmd)
2752 clib_error_t *error = 0;
2753 acl_main_t *am = &acl_main;
2756 (void) unformat (input, "index %u", &acl_index);
2758 acl_plugin_show_acl (am, acl_index);
2763 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
2765 vlib_main_t *vm = am->vlib_main;
2768 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
2769 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
2771 /* if we need a particular interface, skip all the others */
2772 if ((sw_if_index != ~0) && (sw_if_index != swi))
2775 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
2777 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
2778 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
2780 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
2781 am->input_acl_vec_by_sw_if_index[swi], "%d");
2784 vlib_cli_output (vm, "\n");
2785 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
2787 acl_print_acl (vm, am, *pj);
2789 vlib_cli_output (vm, "\n");
2793 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
2794 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
2796 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
2797 am->output_acl_vec_by_sw_if_index[swi], "%d");
2800 vlib_cli_output (vm, "\n");
2801 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
2803 acl_print_acl (vm, am, *pj);
2805 vlib_cli_output (vm, "\n");
2813 static clib_error_t *
2814 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
2815 unformat_input_t * input,
2816 vlib_cli_command_t * cmd)
2818 clib_error_t *error = 0;
2819 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
2822 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
2823 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
2824 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
2825 format_acl_plugin_5tuple, five_tuple);
2827 error = clib_error_return (0, "expecting 6 hex integers");
2832 static clib_error_t *
2833 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
2834 unformat_input_t * input,
2835 vlib_cli_command_t * cmd)
2837 clib_error_t *error = 0;
2838 acl_main_t *am = &acl_main;
2840 u32 sw_if_index = ~0;
2841 (void) unformat (input, "sw_if_index %u", &sw_if_index);
2842 int show_acl = unformat (input, "acl");
2844 acl_plugin_show_interface (am, sw_if_index, show_acl);
2848 static clib_error_t *
2849 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
2850 unformat_input_t * input,
2851 vlib_cli_command_t * cmd)
2853 clib_error_t *error = 0;
2854 acl_main_t *am = &acl_main;
2856 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2859 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2863 vlib_cli_output (vm, " Not initialized\n");
2865 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2866 if (am->hash_lookup_mheap)
2868 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2872 vlib_cli_output (vm, " Not initialized\n");
2878 acl_plugin_show_sessions (acl_main_t * am,
2879 u32 show_session_thread_id,
2880 u32 show_session_session_index)
2882 vlib_main_t *vm = am->vlib_main;
2884 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2885 vnet_sw_interface_t *swif;
2888 u64 n_adds = am->fa_session_total_adds;
2889 u64 n_dels = am->fa_session_total_dels;
2890 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
2891 n_dels, n_adds - n_dels);
2893 vlib_cli_output (vm, "\n\nPer-thread data:");
2894 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
2896 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2897 vlib_cli_output (vm, "Thread #%d:", wk);
2898 if (show_session_thread_id == wk
2899 && show_session_session_index < pool_len (pw->fa_sessions_pool))
2901 vlib_cli_output (vm, " session index %u:",
2902 show_session_session_index);
2903 fa_session_t *sess =
2904 pw->fa_sessions_pool + show_session_session_index;
2905 u64 *m = (u64 *) & sess->info;
2906 vlib_cli_output (vm,
2907 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
2908 m[0], m[1], m[2], m[3], m[4], m[5]);
2909 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
2910 vlib_cli_output (vm, " tcp_flags_seen: %x",
2911 sess->tcp_flags_seen.as_u16);
2912 vlib_cli_output (vm, " last active time: %lu",
2913 sess->last_active_time);
2914 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
2915 vlib_cli_output (vm, " link enqueue time: %lu",
2916 sess->link_enqueue_time);
2917 vlib_cli_output (vm, " link next index: %u",
2918 sess->link_next_idx);
2919 vlib_cli_output (vm, " link prev index: %u",
2920 sess->link_prev_idx);
2921 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
2923 vlib_cli_output (vm, " connection add/del stats:", wk);
2924 pool_foreach (swif, im->sw_interfaces, (
2931 (pw->fa_session_adds_by_sw_if_index)
2933 pw->fa_session_adds_by_sw_if_index
2938 (pw->fa_session_dels_by_sw_if_index)
2940 pw->fa_session_dels_by_sw_if_index
2942 vlib_cli_output (vm,
2943 " sw_if_index %d: add %lu - del %lu = %lu",
2952 vlib_cli_output (vm, " connection timeout type lists:", wk);
2954 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
2956 u32 head_session_index = pw->fa_conn_list_head[tt];
2957 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
2958 head_session_index);
2959 if (~0 != head_session_index)
2961 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2962 vlib_cli_output (vm, " last active time: %lu",
2963 sess->last_active_time);
2964 vlib_cli_output (vm, " link enqueue time: %lu",
2965 sess->link_enqueue_time);
2969 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
2970 vlib_cli_output (vm, " Requeue until time: %lu",
2971 pw->requeue_until_time);
2972 vlib_cli_output (vm, " Current time wait interval: %lu",
2973 pw->current_time_wait_interval);
2974 vlib_cli_output (vm, " Count of deleted sessions: %lu",
2975 pw->cnt_deleted_sessions);
2976 vlib_cli_output (vm, " Delete already deleted: %lu",
2977 pw->cnt_already_deleted_sessions);
2978 vlib_cli_output (vm, " Session timers restarted: %lu",
2979 pw->cnt_session_timer_restarted);
2980 vlib_cli_output (vm, " Swipe until this time: %lu",
2981 pw->swipe_end_time);
2982 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
2983 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2984 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
2986 pw->pending_clear_sw_if_index_bitmap);
2987 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
2988 vlib_cli_output (vm, " interrupt is pending: %d",
2989 pw->interrupt_is_pending);
2990 vlib_cli_output (vm, " interrupt is needed: %d",
2991 pw->interrupt_is_needed);
2992 vlib_cli_output (vm, " interrupt is unwanted: %d",
2993 pw->interrupt_is_unwanted);
2994 vlib_cli_output (vm, " interrupt generation: %d",
2995 pw->interrupt_generation);
2997 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
2998 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
2999 foreach_fa_cleaner_counter;
3001 vlib_cli_output (vm, "Interrupt generation: %d",
3002 am->fa_interrupt_generation);
3003 vlib_cli_output (vm,
3004 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3005 am->fa_min_deleted_sessions_per_interval,
3006 am->fa_max_deleted_sessions_per_interval,
3007 am->fa_cleaner_wait_time_increment * 1000.0,
3008 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3009 1000.0 / (f64) vm->clib_time.clocks_per_second);
3012 static clib_error_t *
3013 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3014 unformat_input_t * input,
3015 vlib_cli_command_t * cmd)
3017 clib_error_t *error = 0;
3018 acl_main_t *am = &acl_main;
3020 u32 show_bihash_verbose = 0;
3021 u32 show_session_thread_id = ~0;
3022 u32 show_session_session_index = ~0;
3023 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3024 &show_session_session_index);
3025 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3027 acl_plugin_show_sessions (am, show_session_thread_id,
3028 show_session_session_index);
3029 show_fa_sessions_hash (vm, show_bihash_verbose);
3034 acl_plugin_show_tables_mask_type (acl_main_t * am)
3036 vlib_main_t *vm = am->vlib_main;
3037 ace_mask_type_entry_t *mte;
3039 vlib_cli_output (vm, "Mask-type entries:");
3041 pool_foreach(mte, am->ace_mask_type_pool,
3043 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
3044 mte - am->ace_mask_type_pool,
3045 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3046 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3052 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3054 vlib_main_t *vm = am->vlib_main;
3057 vlib_cli_output (vm, "Mask-ready ACL representations\n");
3058 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3060 if ((acl_index != ~0) && (acl_index != i))
3064 hash_acl_info_t *ha = &am->hash_acl_infos[i];
3065 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3066 vlib_cli_output (vm, " applied inbound on sw_if_index list: %U\n",
3067 format_vec32, ha->inbound_sw_if_index_list, "%d");
3068 vlib_cli_output (vm, " applied outbound on sw_if_index list: %U\n",
3069 format_vec32, ha->outbound_sw_if_index_list, "%d");
3070 vlib_cli_output (vm, " mask type index bitmap: %U\n",
3071 format_bitmap_hex, ha->mask_type_index_bitmap);
3072 for (j = 0; j < vec_len (ha->rules); j++)
3074 hash_ace_info_t *pa = &ha->rules[j];
3075 m = (u64 *) & pa->match;
3076 vlib_cli_output (vm,
3077 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3078 j, m[0], m[1], m[2], m[3], m[4], m[5],
3079 pa->mask_type_index, pa->acl_index, pa->ace_index,
3080 pa->action, pa->src_portrange_not_powerof2,
3081 pa->dst_portrange_not_powerof2);
3087 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3089 vlib_cli_output (vm,
3090 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3091 j, pae->acl_index, pae->ace_index, pae->action,
3092 pae->hash_ace_info_index, pae->next_applied_entry_index,
3093 pae->prev_applied_entry_index,
3094 pae->tail_applied_entry_index, pae->hitcount);
3098 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3100 vlib_main_t *vm = am->vlib_main;
3102 vlib_cli_output (vm, "Applied lookup entries for interfaces");
3105 (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3106 || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3107 || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3108 || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3110 if ((sw_if_index != ~0) && (sw_if_index != swi))
3114 vlib_cli_output (vm, "sw_if_index %d:", swi);
3115 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3117 applied_hash_acl_info_t *pal =
3118 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3119 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
3120 format_bitmap_hex, pal->mask_type_index_bitmap);
3121 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
3122 pal->applied_acls, "%d");
3124 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3126 vlib_cli_output (vm, " input lookup applied entries:");
3128 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3131 acl_plugin_print_pae (vm, j,
3132 &am->input_hash_entry_vec_by_sw_if_index
3137 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3139 applied_hash_acl_info_t *pal =
3140 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3141 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
3142 format_bitmap_hex, pal->mask_type_index_bitmap);
3143 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
3144 pal->applied_acls, "%d");
3146 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3148 vlib_cli_output (vm, " output lookup applied entries:");
3150 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3153 acl_plugin_print_pae (vm, j,
3154 &am->output_hash_entry_vec_by_sw_if_index
3162 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3164 vlib_main_t *vm = am->vlib_main;
3165 show_hash_acl_hash (vm, am, show_bihash_verbose);
3168 static clib_error_t *
3169 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3170 unformat_input_t * input,
3171 vlib_cli_command_t * cmd)
3173 clib_error_t *error = 0;
3174 acl_main_t *am = &acl_main;
3177 u32 sw_if_index = ~0;
3178 int show_acl_hash_info = 0;
3179 int show_applied_info = 0;
3180 int show_mask_type = 0;
3181 int show_bihash = 0;
3182 u32 show_bihash_verbose = 0;
3184 if (unformat (input, "acl"))
3186 show_acl_hash_info = 1;
3187 /* mask-type is handy to see as well right there */
3189 unformat (input, "index %u", &acl_index);
3191 else if (unformat (input, "applied"))
3193 show_applied_info = 1;
3194 unformat (input, "sw_if_index %u", &sw_if_index);
3196 else if (unformat (input, "mask"))
3200 else if (unformat (input, "hash"))
3203 unformat (input, "verbose %u", &show_bihash_verbose);
3207 (show_mask_type || show_acl_hash_info || show_applied_info
3210 /* if no qualifiers specified, show all */
3212 show_acl_hash_info = 1;
3213 show_applied_info = 1;
3217 acl_plugin_show_tables_mask_type (am);
3218 if (show_acl_hash_info)
3219 acl_plugin_show_tables_acl_hash_info (am, acl_index);
3220 if (show_applied_info)
3221 acl_plugin_show_tables_applied_info (am, sw_if_index);
3223 acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3228 static clib_error_t *
3229 acl_clear_aclplugin_fn (vlib_main_t * vm,
3230 unformat_input_t * input, vlib_cli_command_t * cmd)
3232 clib_error_t *error = 0;
3233 acl_main_t *am = &acl_main;
3234 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3235 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3240 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3241 .path = "set acl-plugin",
3242 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3243 .function = acl_set_aclplugin_fn,
3246 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3247 .path = "show acl-plugin acl",
3248 .short_help = "show acl-plugin acl [index N]",
3249 .function = acl_show_aclplugin_acl_fn,
3252 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3253 .path = "show acl-plugin decode 5tuple",
3254 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3255 .function = acl_show_aclplugin_decode_5tuple_fn,
3258 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3259 .path = "show acl-plugin interface",
3260 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3261 .function = acl_show_aclplugin_interface_fn,
3264 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3265 .path = "show acl-plugin memory",
3266 .short_help = "show acl-plugin memory",
3267 .function = acl_show_aclplugin_memory_fn,
3270 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3271 .path = "show acl-plugin sessions",
3272 .short_help = "show acl-plugin sessions",
3273 .function = acl_show_aclplugin_sessions_fn,
3276 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3277 .path = "show acl-plugin tables",
3278 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3279 .function = acl_show_aclplugin_tables_fn,
3282 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3283 .path = "show acl-plugin macip acl",
3284 .short_help = "show acl-plugin macip acl",
3285 .function = acl_show_aclplugin_macip_acl_fn,
3288 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3289 .path = "show acl-plugin macip interface",
3290 .short_help = "show acl-plugin macip interface",
3291 .function = acl_show_aclplugin_macip_interface_fn,
3294 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3295 .path = "clear acl-plugin sessions",
3296 .short_help = "clear acl-plugin sessions",
3297 .function = acl_clear_aclplugin_fn,
3301 static clib_error_t *
3302 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3304 acl_main_t *am = &acl_main;
3305 u32 conn_table_hash_buckets;
3306 u32 conn_table_hash_memory_size;
3307 u32 conn_table_max_entries;
3310 u32 hash_lookup_hash_buckets;
3311 u32 hash_lookup_hash_memory;
3313 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3316 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3317 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3318 else if (unformat (input, "connection hash memory %d",
3319 &conn_table_hash_memory_size))
3320 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3321 else if (unformat (input, "connection count max %d",
3322 &conn_table_max_entries))
3323 am->fa_conn_table_max_entries = conn_table_max_entries;
3324 else if (unformat (input, "main heap size %d", &main_heap_size))
3325 am->acl_mheap_size = main_heap_size;
3326 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3327 am->hash_lookup_mheap_size = hash_heap_size;
3328 else if (unformat (input, "hash lookup hash buckets %d",
3329 &hash_lookup_hash_buckets))
3330 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3331 else if (unformat (input, "hash lookup hash memory %d",
3332 &hash_lookup_hash_memory))
3333 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3335 return clib_error_return (0, "unknown input '%U'",
3336 format_unformat_error, input);
3341 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3343 static clib_error_t *
3344 acl_init (vlib_main_t * vm)
3346 acl_main_t *am = &acl_main;
3347 clib_error_t *error = 0;
3348 memset (am, 0, sizeof (*am));
3350 am->vnet_main = vnet_get_main ();
3352 u8 *name = format (0, "acl_%08x%c", api_version, 0);
3354 /* Ask for a correctly-sized block of API message decode slots */
3355 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3356 VL_MSG_FIRST_AVAILABLE);
3358 error = acl_plugin_api_hookup (vm);
3360 /* Add our API messages to the global name_crc hash table */
3361 setup_message_id_table (am, &api_main);
3365 acl_setup_fa_nodes ();
3367 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3368 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3370 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3371 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3373 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3374 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3375 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3376 TCP_SESSION_IDLE_TIMEOUT_SEC;
3377 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3378 UDP_SESSION_IDLE_TIMEOUT_SEC;
3380 am->fa_conn_table_hash_num_buckets =
3381 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3382 am->fa_conn_table_hash_memory_size =
3383 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3384 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3385 vlib_thread_main_t *tm = vlib_get_thread_main ();
3386 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3390 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3392 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3393 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3394 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3395 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3397 pw->fa_conn_list_head[tt] = ~0;
3398 pw->fa_conn_list_tail[tt] = ~0;
3403 am->fa_min_deleted_sessions_per_interval =
3404 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3405 am->fa_max_deleted_sessions_per_interval =
3406 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3407 am->fa_cleaner_wait_time_increment =
3408 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3410 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3411 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3412 am->fa_cleaner_cnt_unknown_event = 0;
3413 am->fa_cleaner_cnt_timer_restarted = 0;
3414 am->fa_cleaner_cnt_wait_with_timeout = 0;
3417 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3420 am->l4_match_nonfirst_fragment = 1;
3422 /* use the new fancy hash-based matching */
3423 am->use_hash_acl_matching = 1;
3428 VLIB_INIT_FUNCTION (acl_init);
3432 * fd.io coding-style-patch-verification: ON
3435 * eval: (c-set-style "gnu")