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>
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 unix_shared_memory_queue_t *q;
143 q = vl_api_client_index_to_input_queue (mp->client_index);
149 rmp = vl_msg_api_alloc (msg_size);
150 memset (rmp, 0, msg_size);
152 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
153 rmp->context = mp->context;
154 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
155 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
157 vl_msg_api_send_shmem (q, (u8 *) & rmp);
161 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
164 vl_api_acl_plugin_control_ping_reply_t *rmp;
165 acl_main_t *am = &acl_main;
169 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
171 rmp->vpe_pid = ntohl (getpid ());
177 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
178 u32 * acl_list_index, u8 * tag)
180 acl_main_t *am = &acl_main;
183 acl_rule_t *acl_new_rules = 0;
186 if (*acl_list_index != ~0)
188 /* They supplied some number, let's see if this ACL exists */
189 if (pool_is_free_index (am->acls, *acl_list_index))
191 /* tried to replace a non-existent ACL, no point doing anything */
193 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
194 *acl_list_index, tag);
195 return VNET_API_ERROR_NO_SUCH_ENTRY;
201 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
202 *acl_list_index, tag);
205 void *oldheap = acl_set_heap (am);
207 /* Create and populate the rules */
209 vec_validate (acl_new_rules, count - 1);
211 for (i = 0; i < count; i++)
213 r = vec_elt_at_index (acl_new_rules, i);
214 memset (r, 0, sizeof (*r));
215 r->is_permit = rules[i].is_permit;
216 r->is_ipv6 = rules[i].is_ipv6;
219 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
220 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
224 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
225 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
227 r->src_prefixlen = rules[i].src_ip_prefix_len;
228 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
229 r->proto = rules[i].proto;
230 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
231 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
232 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
233 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
234 r->tcp_flags_value = rules[i].tcp_flags_value;
235 r->tcp_flags_mask = rules[i].tcp_flags_mask;
238 if (~0 == *acl_list_index)
241 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
242 memset (a, 0, sizeof (*a));
243 /* Will return the newly allocated ACL index */
244 *acl_list_index = a - am->acls;
248 a = am->acls + *acl_list_index;
249 hash_acl_delete (am, *acl_list_index);
250 /* Get rid of the old rules */
254 a->rules = acl_new_rules;
256 memcpy (a->tag, tag, sizeof (a->tag));
257 hash_acl_add (am, *acl_list_index);
258 clib_mem_set_heap (oldheap);
263 acl_del_list (u32 acl_list_index)
265 acl_main_t *am = &acl_main;
268 if (pool_is_free_index (am->acls, acl_list_index))
270 return VNET_API_ERROR_NO_SUCH_ENTRY;
273 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
275 if (vec_len (vec_elt (am->input_sw_if_index_vec_by_acl, acl_list_index))
278 /* ACL is applied somewhere inbound. Refuse to delete */
279 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
282 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
285 (vec_elt (am->output_sw_if_index_vec_by_acl, acl_list_index)) > 0)
287 /* ACL is applied somewhere outbound. Refuse to delete */
288 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
292 void *oldheap = acl_set_heap (am);
293 /* delete any references to the ACL */
294 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
296 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
299 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
301 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
309 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
311 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
314 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
316 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
324 /* delete the hash table data */
326 hash_acl_delete (am, acl_list_index);
327 /* now we can delete the ACL itself */
328 a = pool_elt_at_index (am->acls, acl_list_index);
332 pool_put (am->acls, a);
333 clib_mem_set_heap (oldheap);
337 /* Some aids in ASCII graphing the content */
344 u8 ip4_5tuple_mask[] =
345 _(" dmac smac etype ")
346 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
353 _(" ttl pr checksum ")
362 _("L4 T/U sport dport ")
372 u8 ip6_5tuple_mask[] =
373 _(" dmac smac etype ")
374 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
376 _(0x0000) __ __ __ __
378 _(0x0004) __ __ XX __
380 _(0x0008) XX XX XX XX
381 _(0x000C) XX XX XX XX
382 _(0x0010) XX XX XX XX
383 _(0x0014) XX XX XX XX
385 _(0x0018) XX XX XX XX
386 _(0x001C) XX XX XX XX
387 _(0x0020) XX XX XX XX
388 _(0x0024) XX XX XX XX
389 _("L4T/U sport dport ")
390 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
392 u8 dot1q_5tuple_mask[] =
393 _(" dmac smac dot1q etype ")
394 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
395 _(padpad) __ __ __ __
396 _(padpad) __ __ __ __
397 _(padpad) __ __ __ __
400 u8 dot1ad_5tuple_mask[] =
401 _(" dmac smac dot1ad dot1q etype ")
402 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
403 _(padpad) __ __ __ __
404 _(padpad) __ __ __ __
414 count_skip (u8 * p, u32 size)
416 u64 *p64 = (u64 *) p;
417 /* Be tolerant to null pointer */
421 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
425 return (p64 - (u64 *) p) / 2;
429 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
430 u32 mask_len, u32 next_table_index,
431 u32 miss_next_index, u32 * table_index,
435 u32 memory_size = 2 << 13;
436 u32 skip = count_skip (mask, mask_len);
437 u32 match = (mask_len / 16) - skip;
438 u8 *skip_mask_ptr = mask + 16 * skip;
439 u32 current_data_flag = 0;
440 int current_data_offset = 0;
444 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
445 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
446 memory_size, skip, match,
447 next_table_index, miss_next_index,
448 table_index, current_data_flag,
449 current_data_offset, is_add,
450 1 /* delete_chain */ );
451 clib_mem_set_heap (oldheap);
456 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
457 u32 mask_len, u32 next_table_index,
458 u32 miss_next_index, u32 * table_index,
462 u32 memory_size = 2 << 22;
463 u32 skip = count_skip (mask, mask_len);
464 u32 match = (mask_len / 16) - skip;
465 u8 *skip_mask_ptr = mask + 16 * skip;
466 u32 current_data_flag = 0;
467 int current_data_offset = 0;
472 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
473 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
474 memory_size, skip, match,
475 next_table_index, miss_next_index,
476 table_index, current_data_flag,
477 current_data_offset, is_add,
478 1 /* delete_chain */ );
479 clib_mem_set_heap (oldheap);
484 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
486 vnet_classify_main_t *cm = &vnet_classify_main;
487 u32 ip4_table_index = ~0;
488 u32 ip6_table_index = ~0;
489 u32 dot1q_table_index = ~0;
490 u32 dot1ad_table_index = ~0;
491 void *oldheap = acl_set_heap (am);
493 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
495 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
497 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
499 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
502 /* switch to global heap while calling vnet_* functions */
503 clib_mem_set_heap (cm->vlib_main->heap_base);
504 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
506 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
509 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
510 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
511 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
512 sizeof (ip4_5tuple_mask) - 1, ~0,
513 am->l2_input_classify_next_acl_ip4,
514 &ip4_table_index, 0);
516 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
519 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
520 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
521 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
522 sizeof (ip6_5tuple_mask) - 1, ~0,
523 am->l2_input_classify_next_acl_ip6,
524 &ip6_table_index, 0);
526 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
529 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
530 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
531 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
532 sizeof (ip6_5tuple_mask) - 1, ~0,
533 ~0, &dot1q_table_index, 0);
535 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
538 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
539 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
540 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
541 sizeof (dot1ad_5tuple_mask) - 1, ~0,
542 ~0, &dot1ad_table_index, 0);
544 clib_mem_set_heap (oldheap);
549 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
551 vnet_classify_main_t *cm = &vnet_classify_main;
552 u32 ip4_table_index = ~0;
553 u32 ip6_table_index = ~0;
554 u32 dot1q_table_index = ~0;
555 u32 dot1ad_table_index = ~0;
556 void *oldheap = acl_set_heap (am);
558 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
560 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
562 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
564 vec_validate_init_empty
565 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
567 /* switch to global heap while calling vnet_* functions */
568 clib_mem_set_heap (cm->vlib_main->heap_base);
570 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
572 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
575 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
576 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
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, 0);
582 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
585 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
586 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
587 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
588 sizeof (ip6_5tuple_mask) - 1, ~0,
589 am->l2_output_classify_next_acl_ip6,
590 &ip6_table_index, 0);
592 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
595 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
596 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
597 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
598 sizeof (ip6_5tuple_mask) - 1, ~0,
599 ~0, &dot1q_table_index, 0);
601 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
604 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
605 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
606 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
607 sizeof (dot1ad_5tuple_mask) - 1, ~0,
608 ~0, &dot1ad_table_index, 0);
610 clib_mem_set_heap (oldheap);
615 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
616 u8 is_dot1ad, u8 is_ip6)
618 vnet_classify_main_t *cm = &vnet_classify_main;
628 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
634 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
636 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
637 idx = (is_dot1ad) ? 20 : 16;
640 /* 802.1ad ethertype */
643 /* 802.1q ethertype */
649 /* 802.1q ethertype */
654 /* add sessions to vlan tables per ethernet_type */
658 match[idx + 1] = 0xdd;
664 match[idx + 1] = 0x00;
667 vnet_classify_add_del_session (cm, table_index, match, next_acl,
668 session_idx, 0, 0, 0, 1);
669 /* reset the mask back to being a mask */
671 match[idx + 1] = 0xff;
682 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
684 vnet_classify_main_t *cm = &vnet_classify_main;
685 u32 ip4_table_index = ~0;
686 u32 ip6_table_index = ~0;
687 u32 dot1q_table_index = ~0;
688 u32 dot1ad_table_index = ~0;
691 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
693 /* in case there were previous tables attached */
694 acl_unhook_l2_input_classify (am, sw_if_index);
696 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
697 sizeof (ip4_5tuple_mask) - 1, ~0,
698 am->l2_input_classify_next_acl_ip4,
699 &ip4_table_index, 1);
704 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
705 sizeof (ip6_5tuple_mask) - 1, ~0,
706 am->l2_input_classify_next_acl_ip6,
707 &ip6_table_index, 1);
710 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
711 sizeof (ip4_5tuple_mask) - 1, ~0,
712 am->l2_input_classify_next_acl_ip4,
713 &ip4_table_index, 0);
718 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
719 sizeof (dot1ad_5tuple_mask) - 1, ~0,
720 ~0, &dot1ad_table_index, 1);
722 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
723 sizeof (dot1q_5tuple_mask) - 1,
724 dot1ad_table_index, ~0,
725 &dot1q_table_index, 1);
728 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
729 sizeof (dot1ad_5tuple_mask) - 1, ~0,
730 ~0, &dot1ad_table_index, 0);
731 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
732 sizeof (ip6_5tuple_mask) - 1, ~0,
733 am->l2_input_classify_next_acl_ip6,
734 &ip6_table_index, 0);
735 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
736 sizeof (ip4_5tuple_mask) - 1, ~0,
737 am->l2_input_classify_next_acl_ip4,
738 &ip4_table_index, 0);
743 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
744 ip6_table_index, dot1q_table_index);
748 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
749 sizeof (ip4_5tuple_mask) - 1, ~0,
750 am->l2_input_classify_next_acl_ip4,
751 &ip4_table_index, 0);
752 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
753 sizeof (ip6_5tuple_mask) - 1, ~0,
754 am->l2_input_classify_next_acl_ip6,
755 &ip6_table_index, 0);
756 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
757 sizeof (dot1q_5tuple_mask) - 1, ~0,
758 ~0, &dot1q_table_index, 0);
759 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
760 sizeof (dot1ad_5tuple_mask) - 1, ~0,
761 ~0, &dot1ad_table_index, 0);
765 /* add sessions to vlan tables per ethernet_type */
766 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
767 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
768 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
769 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
771 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
773 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
775 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
777 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
780 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
782 clib_mem_set_heap (prevheap);
787 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
789 vnet_classify_main_t *cm = &vnet_classify_main;
790 u32 ip4_table_index = ~0;
791 u32 ip6_table_index = ~0;
792 u32 dot1q_table_index = ~0;
793 u32 dot1ad_table_index = ~0;
796 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
798 /* in case there were previous tables attached */
799 acl_unhook_l2_output_classify (am, sw_if_index);
801 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
802 sizeof (ip4_5tuple_mask) - 1, ~0,
803 am->l2_output_classify_next_acl_ip4,
804 &ip4_table_index, 1);
808 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
809 sizeof (ip6_5tuple_mask) - 1, ~0,
810 am->l2_output_classify_next_acl_ip6,
811 &ip6_table_index, 1);
814 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
815 sizeof (ip4_5tuple_mask) - 1, ~0,
816 am->l2_output_classify_next_acl_ip4,
817 &ip4_table_index, 0);
822 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
823 sizeof (dot1ad_5tuple_mask) - 1, ~0,
824 ~0, &dot1ad_table_index, 1);
826 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
827 sizeof (dot1q_5tuple_mask) - 1,
828 dot1ad_table_index, ~0,
829 &dot1q_table_index, 1);
832 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
833 sizeof (dot1ad_5tuple_mask) - 1, ~0,
834 ~0, &dot1ad_table_index, 0);
835 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
836 sizeof (ip6_5tuple_mask) - 1, ~0,
837 am->l2_output_classify_next_acl_ip6,
838 &ip6_table_index, 0);
839 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
840 sizeof (ip4_5tuple_mask) - 1, ~0,
841 am->l2_output_classify_next_acl_ip4,
842 &ip4_table_index, 0);
847 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
848 ip6_table_index, dot1q_table_index);
851 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
852 sw_if_index, ip4_table_index, ip6_table_index);
855 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
856 sizeof (ip6_5tuple_mask) - 1, ~0,
857 am->l2_output_classify_next_acl_ip6,
858 &ip6_table_index, 0);
859 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
860 sizeof (ip4_5tuple_mask) - 1, ~0,
861 am->l2_output_classify_next_acl_ip4,
862 &ip4_table_index, 0);
863 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
864 sizeof (dot1q_5tuple_mask) - 1, ~0,
865 ~0, &dot1q_table_index, 0);
866 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
867 sizeof (dot1ad_5tuple_mask) - 1, ~0,
868 ~0, &dot1ad_table_index, 0);
872 /* add sessions to vlan tables per ethernet_type */
873 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
874 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
875 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
876 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
878 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
880 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
882 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
884 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
887 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
889 clib_mem_set_heap (prevheap);
894 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
900 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
902 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
904 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
908 rv = acl_hook_l2_input_classify (am, sw_if_index);
912 rv = acl_unhook_l2_input_classify (am, sw_if_index);
919 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
925 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
927 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
929 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
933 rv = acl_hook_l2_output_classify (am, sw_if_index);
937 rv = acl_unhook_l2_output_classify (am, sw_if_index);
944 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
946 return (pool_is_free_index (am->acls, acl_list_index));
951 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
953 acl_main_t *am = &acl_main;
954 if (acl_is_not_defined (am, acl_list_index))
956 /* ACL is not defined. Can not apply */
957 return VNET_API_ERROR_NO_SUCH_ENTRY;
959 void *oldheap = acl_set_heap (am);
963 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
965 u32 index = vec_search (am->input_acl_vec_by_sw_if_index[sw_if_index],
967 if (index < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
970 ("ACL %d is already applied inbound on sw_if_index %d (index %d)",
971 acl_list_index, sw_if_index, index);
972 /* the entry is already there */
973 clib_mem_set_heap (oldheap);
974 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
976 /* if there was no ACL applied before, enable the ACL processing */
977 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0)
979 acl_interface_in_enable_disable (am, sw_if_index, 1);
981 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
983 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
984 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
989 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
991 u32 index = vec_search (am->output_acl_vec_by_sw_if_index[sw_if_index],
993 if (index < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
996 ("ACL %d is already applied outbound on sw_if_index %d (index %d)",
997 acl_list_index, sw_if_index, index);
998 /* the entry is already there */
999 clib_mem_set_heap (oldheap);
1000 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1002 /* if there was no ACL applied before, enable the ACL processing */
1003 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1005 acl_interface_out_enable_disable (am, sw_if_index, 1);
1007 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
1008 &acl_list_index, 1);
1009 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
1010 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index],
1013 clib_mem_set_heap (oldheap);
1019 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1021 acl_main_t *am = &acl_main;
1023 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1024 void *oldheap = acl_set_heap (am);
1027 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1028 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1031 if (acl_list_index ==
1032 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
1034 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
1040 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1043 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1046 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1048 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1049 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1054 /* If there is no more ACLs applied on an interface, disable ACL processing */
1055 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1057 acl_interface_in_enable_disable (am, sw_if_index, 0);
1062 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1064 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
1066 if (acl_list_index ==
1067 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
1069 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
1075 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1078 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1081 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1083 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1084 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1089 /* If there is no more ACLs applied on an interface, disable ACL processing */
1090 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1092 acl_interface_out_enable_disable (am, sw_if_index, 0);
1095 clib_mem_set_heap (oldheap);
1100 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
1102 acl_main_t *am = &acl_main;
1104 void *oldheap = acl_set_heap (am);
1107 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1108 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1110 acl_interface_in_enable_disable (am, sw_if_index, 0);
1113 for (i = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1116 u32 acl_list_index =
1117 am->input_acl_vec_by_sw_if_index[sw_if_index][i];
1118 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1119 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1122 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1125 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1127 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1133 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1137 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1138 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1140 acl_interface_out_enable_disable (am, sw_if_index, 0);
1143 for (i = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1146 u32 acl_list_index =
1147 am->output_acl_vec_by_sw_if_index[sw_if_index][i];
1148 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1149 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1152 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1155 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1157 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1163 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1165 clib_mem_set_heap (oldheap);
1169 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1172 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1173 acl_main_t *am = &acl_main;
1177 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
1180 hash_acl_apply (am, sw_if_index, is_input, acl_list_index);
1185 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1187 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
1200 u32 arp_table_index;
1201 u32 dot1q_table_index;
1202 u32 dot1ad_table_index;
1203 } macip_match_type_t;
1206 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1212 for (i = 0; i < vec_len (mv); i++)
1214 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1215 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1225 /* Get metric used to sort match types.
1226 The more specific and the more often seen - the bigger the metric */
1228 match_type_metric (macip_match_type_t * m)
1230 unsigned int mac_bits_set = 0;
1231 unsigned int mac_byte;
1233 for (i = 0; i < 6; i++)
1235 mac_byte = m->mac_mask[i];
1236 for (; mac_byte; mac_byte >>= 1)
1237 mac_bits_set += mac_byte & 1;
1240 * Attempt to place the more specific and the more used rules on top.
1241 * There are obvious caveat corner cases to this, but they do not
1242 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1243 * going with a wildcard IPv4 with a specific MAC).
1245 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1249 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1251 /* Ascending sort based on the metric values */
1252 return match_type_metric (m1) - match_type_metric (m2);
1255 /* Get the offset of L3 source within ethernet packet */
1257 get_l3_src_offset (int is6)
1260 return (sizeof (ethernet_header_t) +
1261 offsetof (ip6_header_t, src_address));
1263 return (sizeof (ethernet_header_t) +
1264 offsetof (ip4_header_t, src_address));
1268 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1270 macip_match_type_t *mvec = NULL;
1271 macip_match_type_t *mt;
1272 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1274 u32 match_type_index;
1277 vnet_classify_main_t *cm = &vnet_classify_main;
1279 /* Count the number of different types of rules */
1280 for (i = 0; i < a->count; i++)
1284 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1285 a->rules[i].src_prefixlen,
1286 a->rules[i].is_ipv6)))
1288 match_type_index = vec_len (mvec);
1289 vec_validate (mvec, match_type_index);
1290 memcpy (mvec[match_type_index].mac_mask,
1291 a->rules[i].src_mac_mask, 6);
1292 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1293 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1294 mvec[match_type_index].table_index = ~0;
1295 mvec[match_type_index].dot1q_table_index = ~0;
1296 mvec[match_type_index].dot1ad_table_index = ~0;
1298 mvec[match_type_index].count++;
1300 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1301 vec_sort_with_function (mvec, match_type_compare);
1302 /* Create the classifier tables */
1304 /* First add ARP tables */
1305 vec_foreach (mt, mvec)
1308 int is6 = mt->is_ipv6;
1310 mt->arp_table_index = ~0;
1313 memset (mask, 0, sizeof (mask));
1314 memcpy (&mask[6], mt->mac_mask, 6);
1315 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1316 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1318 for (i = 0; i < (mt->prefix_len / 8); i++)
1319 mask[14 + 14 + i] = 0xff;
1320 if (mt->prefix_len % 8)
1321 mask[14 + 14 + (mt->prefix_len / 8)] =
1322 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1324 mask_len = ((14 + 14 + ((mt->prefix_len + 7) / 8) +
1325 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1326 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1327 (~0 == last_table) ? 0 : ~0,
1328 &mt->arp_table_index, 1);
1329 last_table = mt->arp_table_index;
1332 /* Now add IP[46] tables */
1333 vec_foreach (mt, mvec)
1336 int is6 = mt->is_ipv6;
1337 int l3_src_offs = get_l3_src_offset (is6);
1339 u32 *last_tag_table;
1342 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1345 for (tags = 2; tags >= 0; tags--)
1347 memset (mask, 0, sizeof (mask));
1348 memcpy (&mask[6], mt->mac_mask, 6);
1353 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1354 last_tag_table = &mt->table_index;
1357 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1358 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1359 last_tag_table = &mt->dot1q_table_index;
1362 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1363 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1364 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1365 last_tag_table = &mt->dot1ad_table_index;
1368 for (i = 0; i < (mt->prefix_len / 8); i++)
1370 mask[l3_src_offs + i] = 0xff;
1372 if (mt->prefix_len % 8)
1374 mask[l3_src_offs + (mt->prefix_len / 8)] =
1375 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1378 * Round-up the number of bytes needed to store the prefix,
1379 * and round up the number of vectors too
1381 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1382 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1383 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1384 (~0 == last_table) ? 0 : ~0,
1386 last_table = *last_tag_table;
1388 memset (&mask[12], 0, sizeof (mask) - 12);
1392 a->ip4_table_index = last_table;
1393 a->ip6_table_index = last_table;
1394 a->l2_table_index = last_table;
1396 /* Populate the classifier tables with rules from the MACIP ACL */
1397 for (i = 0; i < a->count; i++)
1401 int is6 = a->rules[i].is_ipv6;
1402 int l3_src_offs = get_l3_src_offset (is6);
1407 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1408 a->rules[i].src_prefixlen,
1409 a->rules[i].is_ipv6);
1410 ASSERT (match_type_index != ~0);
1413 for (tags = 2; tags >= 0; tags--)
1415 memset (mask, 0, sizeof (mask));
1416 memcpy (&mask[6], a->rules[i].src_mac, 6);
1421 tag_table = mvec[match_type_index].table_index;
1425 tag_table = mvec[match_type_index].dot1q_table_index;
1431 tag_table = mvec[match_type_index].dot1ad_table_index;
1441 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1443 mask[eth + 1] = 0xdd;
1447 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1449 mask[eth + 1] = 0x00;
1452 /* add session to table mvec[match_type_index].table_index; */
1453 vnet_classify_add_del_session (cm, tag_table,
1454 mask, a->rules[i].is_permit ? ~0 : 0,
1455 i, 0, action, metadata, 1);
1456 memset (&mask[12], 0, sizeof (mask) - 12);
1460 /* add ARP table entry too */
1461 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1463 memset (mask, 0, sizeof (mask));
1464 memcpy (&mask[6], a->rules[i].src_mac, 6);
1467 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1468 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1469 vnet_classify_add_del_session (cm,
1471 [match_type_index].arp_table_index,
1472 mask, a->rules[i].is_permit ? ~0 : 0,
1473 i, 0, action, metadata, 1);
1480 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1482 vnet_classify_main_t *cm = &vnet_classify_main;
1483 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1485 if (a->ip4_table_index != ~0)
1487 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1488 &a->ip4_table_index, 0);
1489 a->ip4_table_index = ~0;
1491 if (a->ip6_table_index != ~0)
1493 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1494 &a->ip6_table_index, 0);
1495 a->ip6_table_index = ~0;
1497 if (a->l2_table_index != ~0)
1499 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1501 a->l2_table_index = ~0;
1506 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1507 u32 * acl_list_index, u8 * tag)
1509 acl_main_t *am = &acl_main;
1510 macip_acl_list_t *a;
1511 macip_acl_rule_t *r;
1512 macip_acl_rule_t *acl_new_rules = 0;
1515 if (*acl_list_index != ~0)
1517 /* They supplied some number, let's see if this MACIP ACL exists */
1518 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1520 /* tried to replace a non-existent ACL, no point doing anything */
1522 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1523 *acl_list_index, tag);
1524 return VNET_API_ERROR_NO_SUCH_ENTRY;
1531 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1534 void *oldheap = acl_set_heap (am);
1535 /* Create and populate the rules */
1537 vec_validate (acl_new_rules, count - 1);
1539 for (i = 0; i < count; i++)
1541 r = &acl_new_rules[i];
1542 r->is_permit = rules[i].is_permit;
1543 r->is_ipv6 = rules[i].is_ipv6;
1544 memcpy (&r->src_mac, rules[i].src_mac, 6);
1545 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1546 if (rules[i].is_ipv6)
1547 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1549 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1550 r->src_prefixlen = rules[i].src_ip_prefix_len;
1553 if (~0 == *acl_list_index)
1556 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1557 memset (a, 0, sizeof (*a));
1558 /* Will return the newly allocated ACL index */
1559 *acl_list_index = a - am->macip_acls;
1563 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1566 vec_free (a->rules);
1568 macip_destroy_classify_tables (am, *acl_list_index);
1571 a->rules = acl_new_rules;
1573 memcpy (a->tag, tag, sizeof (a->tag));
1575 /* Create and populate the classifer tables */
1576 macip_create_classify_tables (am, *acl_list_index);
1577 clib_mem_set_heap (oldheap);
1582 /* No check for validity of sw_if_index - the callers were supposed to validate */
1585 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1588 u32 macip_acl_index;
1589 macip_acl_list_t *a;
1590 void *oldheap = acl_set_heap (am);
1591 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1592 clib_mem_set_heap (oldheap);
1593 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1594 /* No point in deleting MACIP ACL which is not applied */
1595 if (~0 == macip_acl_index)
1596 return VNET_API_ERROR_NO_SUCH_ENTRY;
1597 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1598 /* remove the classifier tables off the interface L2 ACL */
1600 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1601 a->ip6_table_index, a->l2_table_index, 0);
1602 /* Unset the MACIP ACL index */
1603 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1607 /* No check for validity of sw_if_index - the callers were supposed to validate */
1610 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1611 u32 macip_acl_index)
1613 macip_acl_list_t *a;
1615 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1617 return VNET_API_ERROR_NO_SUCH_ENTRY;
1619 void *oldheap = acl_set_heap (am);
1620 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1621 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1622 clib_mem_set_heap (oldheap);
1623 /* If there already a MACIP ACL applied, unapply it */
1624 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1625 macip_acl_interface_del_acl (am, sw_if_index);
1626 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1628 /* Apply the classifier tables for L2 ACLs */
1630 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1631 a->ip6_table_index, a->l2_table_index, 1);
1636 macip_acl_del_list (u32 acl_list_index)
1638 acl_main_t *am = &acl_main;
1639 macip_acl_list_t *a;
1641 if (pool_is_free_index (am->macip_acls, acl_list_index))
1643 return VNET_API_ERROR_NO_SUCH_ENTRY;
1646 /* delete any references to the ACL */
1647 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1649 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1651 macip_acl_interface_del_acl (am, i);
1655 void *oldheap = acl_set_heap (am);
1656 /* Now that classifier tables are detached, clean them up */
1657 macip_destroy_classify_tables (am, acl_list_index);
1659 /* now we can delete the ACL itself */
1660 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1663 vec_free (a->rules);
1665 pool_put (am->macip_acls, a);
1666 clib_mem_set_heap (oldheap);
1672 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1675 acl_main_t *am = &acl_main;
1679 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1683 rv = macip_acl_interface_del_acl (am, sw_if_index);
1689 * If the client does not allocate enough memory for a variable-length
1690 * message, and then proceed to use it as if the full memory allocated,
1691 * absent the check we happily consume that on the VPP side, and go
1692 * along as if nothing happened. However, the resulting
1693 * effects range from just garbage in the API decode
1694 * (because the decoder snoops too far), to potential memory
1697 * This verifies that the actual length of the message is
1698 * at least expected_len, and complains loudly if it is not.
1700 * A failing check here is 100% a software bug on the API user side,
1701 * so we might as well yell.
1705 verify_message_len (void *mp, u32 expected_len, char *where)
1707 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1708 if (supplied_len < expected_len)
1710 clib_warning ("%s: Supplied message length %d is less than expected %d",
1711 where, supplied_len, expected_len);
1720 /* API message handler */
1722 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1724 vl_api_acl_add_replace_reply_t *rmp;
1725 acl_main_t *am = &acl_main;
1727 u32 acl_list_index = ntohl (mp->acl_index);
1728 u32 acl_count = ntohl (mp->count);
1729 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1731 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1733 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1737 rv = VNET_API_ERROR_INVALID_VALUE;
1741 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1743 rmp->acl_index = htonl(acl_list_index);
1749 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1751 acl_main_t *am = &acl_main;
1752 vl_api_acl_del_reply_t *rmp;
1755 rv = acl_del_list (ntohl (mp->acl_index));
1757 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1761 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1763 acl_main_t *am = &acl_main;
1764 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1765 u32 sw_if_index = ntohl (mp->sw_if_index);
1766 vl_api_acl_interface_add_del_reply_t *rmp;
1769 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1770 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1773 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1774 mp->is_input, ntohl (mp->acl_index));
1776 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1780 vl_api_acl_interface_set_acl_list_t_handler
1781 (vl_api_acl_interface_set_acl_list_t * mp)
1783 acl_main_t *am = &acl_main;
1784 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1787 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1788 u32 sw_if_index = ntohl (mp->sw_if_index);
1790 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1791 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1794 acl_interface_reset_inout_acls (sw_if_index, 0);
1795 acl_interface_reset_inout_acls (sw_if_index, 1);
1797 for (i = 0; i < mp->count; i++)
1799 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1801 /* ACL does not exist, so we can not apply it */
1802 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1807 for (i = 0; i < mp->count; i++)
1809 acl_interface_add_del_inout_acl (sw_if_index, 1,
1811 ntohl (mp->acls[i]));
1816 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1820 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1822 api_rule->is_permit = r->is_permit;
1823 api_rule->is_ipv6 = r->is_ipv6;
1826 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1827 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1831 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1832 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1834 api_rule->src_ip_prefix_len = r->src_prefixlen;
1835 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1836 api_rule->proto = r->proto;
1837 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1838 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1839 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1840 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1841 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1842 api_rule->tcp_flags_value = r->tcp_flags_value;
1846 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1847 acl_list_t * acl, u32 context)
1849 vl_api_acl_details_t *mp;
1850 vl_api_acl_rule_t *rules;
1852 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1853 void *oldheap = acl_set_heap (am);
1855 mp = vl_msg_api_alloc (msg_size);
1856 memset (mp, 0, msg_size);
1857 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1859 /* fill in the message */
1860 mp->context = context;
1861 mp->count = htonl (acl->count);
1862 mp->acl_index = htonl (acl - am->acls);
1863 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1864 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1866 for (i = 0; i < acl->count; i++)
1868 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1871 clib_mem_set_heap (oldheap);
1872 vl_msg_api_send_shmem (q, (u8 *) & mp);
1877 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1879 acl_main_t *am = &acl_main;
1884 unix_shared_memory_queue_t *q;
1886 q = vl_api_client_index_to_input_queue (mp->client_index);
1892 if (mp->acl_index == ~0)
1895 /* Just dump all ACLs */
1896 pool_foreach (acl, am->acls,
1898 send_acl_details(am, q, acl, mp->context);
1904 acl_index = ntohl (mp->acl_index);
1905 if (!pool_is_free_index (am->acls, acl_index))
1907 acl = pool_elt_at_index (am->acls, acl_index);
1908 send_acl_details (am, q, acl, mp->context);
1914 /* FIXME API: should we signal an error here at all ? */
1920 send_acl_interface_list_details (acl_main_t * am,
1921 unix_shared_memory_queue_t * q,
1922 u32 sw_if_index, u32 context)
1924 vl_api_acl_interface_list_details_t *mp;
1930 void *oldheap = acl_set_heap (am);
1932 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1933 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1935 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1936 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1937 count = n_input + n_output;
1939 msg_size = sizeof (*mp);
1940 msg_size += sizeof (mp->acls[0]) * count;
1942 mp = vl_msg_api_alloc (msg_size);
1943 memset (mp, 0, msg_size);
1945 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1947 /* fill in the message */
1948 mp->context = context;
1949 mp->sw_if_index = htonl (sw_if_index);
1951 mp->n_input = n_input;
1952 for (i = 0; i < n_input; i++)
1954 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1956 for (i = 0; i < n_output; i++)
1958 mp->acls[n_input + i] =
1959 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1961 clib_mem_set_heap (oldheap);
1962 vl_msg_api_send_shmem (q, (u8 *) & mp);
1966 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1969 acl_main_t *am = &acl_main;
1970 vnet_sw_interface_t *swif;
1971 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1974 unix_shared_memory_queue_t *q;
1976 q = vl_api_client_index_to_input_queue (mp->client_index);
1982 if (mp->sw_if_index == ~0)
1985 pool_foreach (swif, im->sw_interfaces,
1987 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1993 sw_if_index = ntohl (mp->sw_if_index);
1994 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
1995 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1999 /* MACIP ACL API handlers */
2002 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2004 vl_api_macip_acl_add_reply_t *rmp;
2005 acl_main_t *am = &acl_main;
2007 u32 acl_list_index = ~0;
2008 u32 acl_count = ntohl (mp->count);
2009 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2011 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2013 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2017 rv = VNET_API_ERROR_INVALID_VALUE;
2021 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2023 rmp->acl_index = htonl(acl_list_index);
2029 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2031 vl_api_macip_acl_add_replace_reply_t *rmp;
2032 acl_main_t *am = &acl_main;
2034 u32 acl_list_index = ntohl (mp->acl_index);
2035 u32 acl_count = ntohl (mp->count);
2036 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2038 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2040 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2044 rv = VNET_API_ERROR_INVALID_VALUE;
2048 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2050 rmp->acl_index = htonl(acl_list_index);
2056 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2058 acl_main_t *am = &acl_main;
2059 vl_api_macip_acl_del_reply_t *rmp;
2062 rv = macip_acl_del_list (ntohl (mp->acl_index));
2064 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2068 vl_api_macip_acl_interface_add_del_t_handler
2069 (vl_api_macip_acl_interface_add_del_t * mp)
2071 acl_main_t *am = &acl_main;
2072 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2074 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2075 u32 sw_if_index = ntohl (mp->sw_if_index);
2077 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2078 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2081 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2082 ntohl (mp->acl_index));
2084 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2088 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
2089 macip_acl_list_t * acl, u32 context)
2091 vl_api_macip_acl_details_t *mp;
2092 vl_api_macip_acl_rule_t *rules;
2093 macip_acl_rule_t *r;
2095 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2097 mp = vl_msg_api_alloc (msg_size);
2098 memset (mp, 0, msg_size);
2099 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2101 /* fill in the message */
2102 mp->context = context;
2105 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2106 mp->count = htonl (acl->count);
2107 mp->acl_index = htonl (acl - am->macip_acls);
2109 for (i = 0; i < acl->count; i++)
2112 rules[i].is_permit = r->is_permit;
2113 rules[i].is_ipv6 = r->is_ipv6;
2114 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2115 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2116 sizeof (r->src_mac_mask));
2118 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2119 sizeof (r->src_ip_addr.ip6));
2121 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2122 sizeof (r->src_ip_addr.ip4));
2123 rules[i].src_ip_prefix_len = r->src_prefixlen;
2128 /* No martini, no party - no ACL applied to this interface. */
2133 vl_msg_api_send_shmem (q, (u8 *) & mp);
2138 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2140 acl_main_t *am = &acl_main;
2141 macip_acl_list_t *acl;
2143 unix_shared_memory_queue_t *q;
2145 q = vl_api_client_index_to_input_queue (mp->client_index);
2151 if (mp->acl_index == ~0)
2153 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2154 pool_foreach (acl, am->macip_acls, (
2156 send_macip_acl_details (am, q, acl,
2164 u32 acl_index = ntohl (mp->acl_index);
2165 if (!pool_is_free_index (am->macip_acls, acl_index))
2167 acl = pool_elt_at_index (am->macip_acls, acl_index);
2168 send_macip_acl_details (am, q, acl, mp->context);
2174 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2177 acl_main_t *am = &acl_main;
2178 vl_api_macip_acl_interface_get_reply_t *rmp;
2179 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2180 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2181 unix_shared_memory_queue_t *q;
2184 q = vl_api_client_index_to_input_queue (mp->client_index);
2190 rmp = vl_msg_api_alloc (msg_size);
2191 memset (rmp, 0, msg_size);
2193 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2194 rmp->context = mp->context;
2195 rmp->count = htonl (count);
2196 for (i = 0; i < count; i++)
2198 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2201 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2205 send_macip_acl_interface_list_details (acl_main_t * am,
2206 unix_shared_memory_queue_t * q,
2208 u32 acl_index, u32 context)
2210 vl_api_macip_acl_interface_list_details_t *rmp;
2211 /* at this time there is only ever 1 mac ip acl per interface */
2212 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2214 rmp = vl_msg_api_alloc (msg_size);
2215 memset (rmp, 0, msg_size);
2217 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2219 /* fill in the message */
2220 rmp->context = context;
2222 rmp->sw_if_index = htonl (sw_if_index);
2223 rmp->acls[0] = htonl (acl_index);
2225 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2229 vl_api_macip_acl_interface_list_dump_t_handler
2230 (vl_api_macip_acl_interface_list_dump_t * mp)
2232 unix_shared_memory_queue_t *q;
2233 acl_main_t *am = &acl_main;
2234 u32 sw_if_index = ntohl (mp->sw_if_index);
2236 q = vl_api_client_index_to_input_queue (mp->client_index);
2242 if (sw_if_index == ~0)
2244 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2246 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2248 send_macip_acl_interface_list_details (am, q, sw_if_index,
2249 am->macip_acl_by_sw_if_index
2257 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2259 send_macip_acl_interface_list_details (am, q, sw_if_index,
2260 am->macip_acl_by_sw_if_index
2261 [sw_if_index], mp->context);
2266 /* Set up the API message handling tables */
2267 static clib_error_t *
2268 acl_plugin_api_hookup (vlib_main_t * vm)
2270 acl_main_t *am = &acl_main;
2272 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2274 vl_api_##n##_t_handler, \
2276 vl_api_##n##_t_endian, \
2277 vl_api_##n##_t_print, \
2278 sizeof(vl_api_##n##_t), 1);
2279 foreach_acl_plugin_api_msg;
2285 #define vl_msg_name_crc_list
2286 #include <acl/acl_all_api_h.h>
2287 #undef vl_msg_name_crc_list
2290 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2292 #define _(id,n,crc) \
2293 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2294 foreach_vl_msg_name_crc_acl;
2299 acl_setup_fa_nodes (void)
2301 vlib_main_t *vm = vlib_get_main ();
2302 acl_main_t *am = &acl_main;
2303 vlib_node_t *n, *n4, *n6;
2305 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2306 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2307 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2310 am->l2_input_classify_next_acl_ip4 =
2311 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2312 am->l2_input_classify_next_acl_ip6 =
2313 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2315 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2316 l2input_get_feat_names (),
2317 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2319 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2320 l2input_get_feat_names (),
2321 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2324 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2325 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2326 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2328 am->l2_output_classify_next_acl_ip4 =
2329 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2330 am->l2_output_classify_next_acl_ip6 =
2331 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2333 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2334 l2output_get_feat_names (),
2335 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2337 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2338 l2output_get_feat_names (),
2339 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2343 acl_set_timeout_sec (int timeout_type, u32 value)
2345 acl_main_t *am = &acl_main;
2346 clib_time_t *ct = &am->vlib_main->clib_time;
2348 if (timeout_type < ACL_N_TIMEOUTS)
2350 am->session_timeout_sec[timeout_type] = value;
2354 clib_warning ("Unknown timeout type %d", timeout_type);
2357 am->session_timeout[timeout_type] =
2358 (u64) (((f64) value) / ct->seconds_per_clock);
2362 acl_set_session_max_entries (u32 value)
2364 acl_main_t *am = &acl_main;
2365 am->fa_conn_table_max_entries = value;
2369 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2371 acl_main_t *am = &acl_main;
2373 if ((eh < 256) && (value < 2))
2375 am->fa_ipv6_known_eh_bitmap =
2376 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2384 static clib_error_t *
2385 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2387 acl_main_t *am = &acl_main;
2388 if (0 == am->acl_mheap)
2390 /* ACL heap is not initialized, so definitely nothing to do. */
2395 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2396 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2398 /* also unapply any ACLs in case the users did not do so. */
2399 macip_acl_interface_del_acl (am, sw_if_index);
2400 acl_interface_reset_inout_acls (sw_if_index, 0);
2401 acl_interface_reset_inout_acls (sw_if_index, 1);
2406 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2410 static clib_error_t *
2411 acl_set_aclplugin_fn (vlib_main_t * vm,
2412 unformat_input_t * input, vlib_cli_command_t * cmd)
2414 clib_error_t *error = 0;
2418 uword memory_size = 0;
2419 acl_main_t *am = &acl_main;
2421 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2423 if (!acl_set_skip_ipv6_eh (eh_val, val))
2425 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2429 if (unformat (input, "use-hash-acl-matching %u", &val))
2431 am->use_hash_acl_matching = (val != 0);
2434 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2436 am->l4_match_nonfirst_fragment = (val != 0);
2439 if (unformat (input, "heap"))
2441 if (unformat (input, "main"))
2443 if (unformat (input, "validate %u", &val))
2444 acl_plugin_acl_set_validate_heap (am, val);
2445 else if (unformat (input, "trace %u", &val))
2446 acl_plugin_acl_set_trace_heap (am, val);
2449 else if (unformat (input, "hash"))
2451 if (unformat (input, "validate %u", &val))
2452 acl_plugin_hash_acl_set_validate_heap (am, val);
2453 else if (unformat (input, "trace %u", &val))
2454 acl_plugin_hash_acl_set_trace_heap (am, val);
2459 if (unformat (input, "session"))
2461 if (unformat (input, "table"))
2463 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2464 if (unformat (input, "max-entries"))
2466 if (!unformat (input, "%u", &val))
2468 error = clib_error_return (0,
2469 "expecting maximum number of entries, got `%U`",
2470 format_unformat_error, input);
2475 acl_set_session_max_entries (val);
2479 if (unformat (input, "hash-table-buckets"))
2481 if (!unformat (input, "%u", &val))
2483 error = clib_error_return (0,
2484 "expecting maximum number of hash table buckets, got `%U`",
2485 format_unformat_error, input);
2490 am->fa_conn_table_hash_num_buckets = val;
2494 if (unformat (input, "hash-table-memory"))
2496 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2498 error = clib_error_return (0,
2499 "expecting maximum amount of hash table memory, got `%U`",
2500 format_unformat_error, input);
2505 am->fa_conn_table_hash_memory_size = memory_size;
2511 if (unformat (input, "timeout"))
2513 if (unformat (input, "udp"))
2515 if (unformat (input, "idle"))
2517 if (!unformat (input, "%u", &timeout))
2519 error = clib_error_return (0,
2520 "expecting timeout value in seconds, got `%U`",
2521 format_unformat_error,
2527 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2532 if (unformat (input, "tcp"))
2534 if (unformat (input, "idle"))
2536 if (!unformat (input, "%u", &timeout))
2538 error = clib_error_return (0,
2539 "expecting timeout value in seconds, got `%U`",
2540 format_unformat_error,
2546 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2550 if (unformat (input, "transient"))
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_TRANSIENT,
2576 my_format_mac_address (u8 * s, va_list * args)
2578 u8 *a = va_arg (*args, u8 *);
2579 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2580 a[0], a[1], a[2], a[3], a[4], a[5]);
2584 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2586 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2588 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2589 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2590 format_ip46_address, &a->src_ip_addr,
2591 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2593 my_format_mac_address, a->src_mac,
2594 my_format_mac_address, a->src_mac_mask);
2599 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2601 vlib_main_t *vm = am->vlib_main;
2604 /* Don't try to print someone else's memory */
2605 if (macip_acl_index > vec_len (am->macip_acls))
2608 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2609 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2611 vlib_cli_output (vm,
2612 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2613 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2615 vlib_cli_output (vm,
2616 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2617 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2618 for (i = 0; i < vec_len (a->rules); i++)
2619 vlib_cli_output (vm, " rule %d: %U\n", i,
2620 my_macip_acl_rule_t_pretty_format,
2621 vec_elt_at_index (a->rules, i));
2625 static clib_error_t *
2626 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2627 unformat_input_t * input,
2628 vlib_cli_command_t * cmd)
2630 clib_error_t *error = 0;
2631 acl_main_t *am = &acl_main;
2633 for (i = 0; i < vec_len (am->macip_acls); i++)
2634 macip_acl_print (am, i);
2638 static clib_error_t *
2639 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2640 unformat_input_t * input,
2641 vlib_cli_command_t * cmd)
2643 clib_error_t *error = 0;
2644 acl_main_t *am = &acl_main;
2646 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2648 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
2649 vec_elt (am->macip_acl_by_sw_if_index, i));
2654 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
2656 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
2659 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
2660 am->acls[acl_index].count, am->acls[acl_index].tag);
2662 PRINT_AND_RESET (vm, out0);
2663 for (j = 0; j < am->acls[acl_index].count; j++)
2665 r = &am->acls[acl_index].rules[j];
2666 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2667 out0 = format_acl_action (out0, r->is_permit);
2668 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
2669 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2672 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
2673 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
2674 out0 = format (out0, " proto %d", r->proto);
2675 out0 = format (out0, " sport %d", r->src_port_or_type_first);
2676 if (r->src_port_or_type_first != r->src_port_or_type_last)
2678 out0 = format (out0, "-%d", r->src_port_or_type_last);
2680 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
2681 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
2683 out0 = format (out0, "-%d", r->dst_port_or_code_last);
2685 if (r->tcp_flags_mask || r->tcp_flags_value)
2688 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
2691 out0 = format (out0, "\n");
2692 PRINT_AND_RESET (vm, out0);
2696 #undef PRINT_AND_RESET
2699 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2702 vlib_main_t *vm = am->vlib_main;
2704 for (i = 0; i < vec_len (am->acls); i++)
2706 if (acl_is_not_defined (am, i))
2708 /* don't attempt to show the ACLs that do not exist */
2711 if ((acl_index != ~0) && (acl_index != i))
2715 acl_print_acl (vm, am, i);
2717 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2719 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
2720 format_vec32, am->input_sw_if_index_vec_by_acl[i],
2723 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2725 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
2726 format_vec32, am->output_sw_if_index_vec_by_acl[i],
2732 static clib_error_t *
2733 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2734 unformat_input_t * input, vlib_cli_command_t * cmd)
2736 clib_error_t *error = 0;
2737 acl_main_t *am = &acl_main;
2740 (void) unformat (input, "index %u", &acl_index);
2742 acl_plugin_show_acl (am, acl_index);
2747 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
2749 vlib_main_t *vm = am->vlib_main;
2752 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
2753 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
2755 /* if we need a particular interface, skip all the others */
2756 if ((sw_if_index != ~0) && (sw_if_index != swi))
2759 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
2761 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
2762 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
2764 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
2765 am->input_acl_vec_by_sw_if_index[swi], "%d");
2768 vlib_cli_output (vm, "\n");
2769 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
2771 acl_print_acl (vm, am, *pj);
2773 vlib_cli_output (vm, "\n");
2777 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
2778 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
2780 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
2781 am->output_acl_vec_by_sw_if_index[swi], "%d");
2784 vlib_cli_output (vm, "\n");
2785 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
2787 acl_print_acl (vm, am, *pj);
2789 vlib_cli_output (vm, "\n");
2796 static clib_error_t *
2797 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
2798 unformat_input_t * input,
2799 vlib_cli_command_t * cmd)
2801 clib_error_t *error = 0;
2802 acl_main_t *am = &acl_main;
2804 u32 sw_if_index = ~0;
2805 (void) unformat (input, "sw_if_index %u", &sw_if_index);
2806 int show_acl = unformat (input, "acl");
2808 acl_plugin_show_interface (am, sw_if_index, show_acl);
2812 static clib_error_t *
2813 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
2814 unformat_input_t * input,
2815 vlib_cli_command_t * cmd)
2817 clib_error_t *error = 0;
2818 acl_main_t *am = &acl_main;
2820 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2823 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2827 vlib_cli_output (vm, " Not initialized\n");
2829 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2830 if (am->hash_lookup_mheap)
2832 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2836 vlib_cli_output (vm, " Not initialized\n");
2842 acl_plugin_show_sessions (acl_main_t * am,
2843 u32 show_session_thread_id,
2844 u32 show_session_session_index)
2846 vlib_main_t *vm = am->vlib_main;
2848 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2849 vnet_sw_interface_t *swif;
2852 u64 n_adds = am->fa_session_total_adds;
2853 u64 n_dels = am->fa_session_total_dels;
2854 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
2855 n_dels, n_adds - n_dels);
2857 vlib_cli_output (vm, "\n\nPer-thread data:");
2858 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
2860 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2861 vlib_cli_output (vm, "Thread #%d:", wk);
2862 if (show_session_thread_id == wk
2863 && show_session_session_index < pool_len (pw->fa_sessions_pool))
2865 vlib_cli_output (vm, " session index %u:",
2866 show_session_session_index);
2867 fa_session_t *sess =
2868 pw->fa_sessions_pool + show_session_session_index;
2869 u64 *m = (u64 *) & sess->info;
2870 vlib_cli_output (vm,
2871 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
2872 m[0], m[1], m[2], m[3], m[4], m[5]);
2873 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
2874 vlib_cli_output (vm, " tcp_flags_seen: %x",
2875 sess->tcp_flags_seen.as_u16);
2876 vlib_cli_output (vm, " last active time: %lu",
2877 sess->last_active_time);
2878 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
2879 vlib_cli_output (vm, " link enqueue time: %lu",
2880 sess->link_enqueue_time);
2881 vlib_cli_output (vm, " link next index: %u",
2882 sess->link_next_idx);
2883 vlib_cli_output (vm, " link prev index: %u",
2884 sess->link_prev_idx);
2885 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
2887 vlib_cli_output (vm, " connection add/del stats:", wk);
2888 pool_foreach (swif, im->sw_interfaces, (
2895 (pw->fa_session_adds_by_sw_if_index)
2897 pw->fa_session_adds_by_sw_if_index
2902 (pw->fa_session_dels_by_sw_if_index)
2904 pw->fa_session_dels_by_sw_if_index
2906 vlib_cli_output (vm,
2907 " sw_if_index %d: add %lu - del %lu = %lu",
2916 vlib_cli_output (vm, " connection timeout type lists:", wk);
2918 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
2920 u32 head_session_index = pw->fa_conn_list_head[tt];
2921 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
2922 head_session_index);
2923 if (~0 != head_session_index)
2925 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2926 vlib_cli_output (vm, " last active time: %lu",
2927 sess->last_active_time);
2928 vlib_cli_output (vm, " link enqueue time: %lu",
2929 sess->link_enqueue_time);
2933 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
2934 vlib_cli_output (vm, " Requeue until time: %lu",
2935 pw->requeue_until_time);
2936 vlib_cli_output (vm, " Current time wait interval: %lu",
2937 pw->current_time_wait_interval);
2938 vlib_cli_output (vm, " Count of deleted sessions: %lu",
2939 pw->cnt_deleted_sessions);
2940 vlib_cli_output (vm, " Delete already deleted: %lu",
2941 pw->cnt_already_deleted_sessions);
2942 vlib_cli_output (vm, " Session timers restarted: %lu",
2943 pw->cnt_session_timer_restarted);
2944 vlib_cli_output (vm, " Swipe until this time: %lu",
2945 pw->swipe_end_time);
2946 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
2947 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2948 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
2950 pw->pending_clear_sw_if_index_bitmap);
2951 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
2952 vlib_cli_output (vm, " interrupt is pending: %d",
2953 pw->interrupt_is_pending);
2954 vlib_cli_output (vm, " interrupt is needed: %d",
2955 pw->interrupt_is_needed);
2956 vlib_cli_output (vm, " interrupt is unwanted: %d",
2957 pw->interrupt_is_unwanted);
2958 vlib_cli_output (vm, " interrupt generation: %d",
2959 pw->interrupt_generation);
2961 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
2962 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
2963 foreach_fa_cleaner_counter;
2965 vlib_cli_output (vm, "Interrupt generation: %d",
2966 am->fa_interrupt_generation);
2967 vlib_cli_output (vm,
2968 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2969 am->fa_min_deleted_sessions_per_interval,
2970 am->fa_max_deleted_sessions_per_interval,
2971 am->fa_cleaner_wait_time_increment * 1000.0,
2972 ((f64) am->fa_current_cleaner_timer_wait_interval) *
2973 1000.0 / (f64) vm->clib_time.clocks_per_second);
2976 static clib_error_t *
2977 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
2978 unformat_input_t * input,
2979 vlib_cli_command_t * cmd)
2981 clib_error_t *error = 0;
2982 acl_main_t *am = &acl_main;
2984 u32 show_bihash_verbose = 0;
2985 u32 show_session_thread_id = ~0;
2986 u32 show_session_session_index = ~0;
2987 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
2988 &show_session_session_index);
2989 (void) unformat (input, "verbose %u", &show_bihash_verbose);
2991 acl_plugin_show_sessions (am, show_session_thread_id,
2992 show_session_session_index);
2993 show_fa_sessions_hash (vm, show_bihash_verbose);
2998 acl_plugin_show_tables_mask_type (acl_main_t * am)
3000 vlib_main_t *vm = am->vlib_main;
3001 ace_mask_type_entry_t *mte;
3003 vlib_cli_output (vm, "Mask-type entries:");
3005 pool_foreach(mte, am->ace_mask_type_pool,
3007 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
3008 mte - am->ace_mask_type_pool,
3009 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3010 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3016 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3018 vlib_main_t *vm = am->vlib_main;
3021 vlib_cli_output (vm, "Mask-ready ACL representations\n");
3022 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3024 if ((acl_index != ~0) && (acl_index != i))
3028 hash_acl_info_t *ha = &am->hash_acl_infos[i];
3029 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3030 vlib_cli_output (vm, " applied inbound on sw_if_index list: %U\n",
3031 format_vec32, ha->inbound_sw_if_index_list, "%d");
3032 vlib_cli_output (vm, " applied outbound on sw_if_index list: %U\n",
3033 format_vec32, ha->outbound_sw_if_index_list, "%d");
3034 vlib_cli_output (vm, " mask type index bitmap: %U\n",
3035 format_bitmap_hex, ha->mask_type_index_bitmap);
3036 for (j = 0; j < vec_len (ha->rules); j++)
3038 hash_ace_info_t *pa = &ha->rules[j];
3039 m = (u64 *) & pa->match;
3040 vlib_cli_output (vm,
3041 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3042 j, m[0], m[1], m[2], m[3], m[4], m[5],
3043 pa->mask_type_index, pa->acl_index, pa->ace_index,
3044 pa->action, pa->src_portrange_not_powerof2,
3045 pa->dst_portrange_not_powerof2);
3051 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3053 vlib_cli_output (vm,
3054 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3055 j, pae->acl_index, pae->ace_index, pae->action,
3056 pae->hash_ace_info_index, pae->next_applied_entry_index,
3057 pae->prev_applied_entry_index,
3058 pae->tail_applied_entry_index, pae->hitcount);
3062 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3064 vlib_main_t *vm = am->vlib_main;
3066 vlib_cli_output (vm, "Applied lookup entries for interfaces");
3069 (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3070 || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3071 || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3072 || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3074 if ((sw_if_index != ~0) && (sw_if_index != swi))
3078 vlib_cli_output (vm, "sw_if_index %d:", swi);
3079 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3081 applied_hash_acl_info_t *pal =
3082 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3083 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
3084 format_bitmap_hex, pal->mask_type_index_bitmap);
3085 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
3086 pal->applied_acls, "%d");
3088 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3090 vlib_cli_output (vm, " input lookup applied entries:");
3092 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3095 acl_plugin_print_pae (vm, j,
3096 &am->input_hash_entry_vec_by_sw_if_index
3101 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3103 applied_hash_acl_info_t *pal =
3104 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3105 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
3106 format_bitmap_hex, pal->mask_type_index_bitmap);
3107 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
3108 pal->applied_acls, "%d");
3110 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3112 vlib_cli_output (vm, " output lookup applied entries:");
3114 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3117 acl_plugin_print_pae (vm, j,
3118 &am->output_hash_entry_vec_by_sw_if_index
3126 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3128 vlib_main_t *vm = am->vlib_main;
3129 show_hash_acl_hash (vm, am, show_bihash_verbose);
3132 static clib_error_t *
3133 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3134 unformat_input_t * input,
3135 vlib_cli_command_t * cmd)
3137 clib_error_t *error = 0;
3138 acl_main_t *am = &acl_main;
3141 u32 sw_if_index = ~0;
3142 int show_acl_hash_info = 0;
3143 int show_applied_info = 0;
3144 int show_mask_type = 0;
3145 int show_bihash = 0;
3146 u32 show_bihash_verbose = 0;
3148 if (unformat (input, "acl"))
3150 show_acl_hash_info = 1;
3151 /* mask-type is handy to see as well right there */
3153 unformat (input, "index %u", &acl_index);
3155 else if (unformat (input, "applied"))
3157 show_applied_info = 1;
3158 unformat (input, "sw_if_index %u", &sw_if_index);
3160 else if (unformat (input, "mask"))
3164 else if (unformat (input, "hash"))
3167 unformat (input, "verbose %u", &show_bihash_verbose);
3171 (show_mask_type || show_acl_hash_info || show_applied_info
3174 /* if no qualifiers specified, show all */
3176 show_acl_hash_info = 1;
3177 show_applied_info = 1;
3181 acl_plugin_show_tables_mask_type (am);
3182 if (show_acl_hash_info)
3183 acl_plugin_show_tables_acl_hash_info (am, acl_index);
3184 if (show_applied_info)
3185 acl_plugin_show_tables_applied_info (am, sw_if_index);
3187 acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3192 static clib_error_t *
3193 acl_clear_aclplugin_fn (vlib_main_t * vm,
3194 unformat_input_t * input, vlib_cli_command_t * cmd)
3196 clib_error_t *error = 0;
3197 acl_main_t *am = &acl_main;
3198 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3199 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3204 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3205 .path = "set acl-plugin",
3206 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3207 .function = acl_set_aclplugin_fn,
3210 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3211 .path = "show acl-plugin acl",
3212 .short_help = "show acl-plugin acl [index N]",
3213 .function = acl_show_aclplugin_acl_fn,
3216 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3217 .path = "show acl-plugin interface",
3218 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3219 .function = acl_show_aclplugin_interface_fn,
3222 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3223 .path = "show acl-plugin memory",
3224 .short_help = "show acl-plugin memory",
3225 .function = acl_show_aclplugin_memory_fn,
3228 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3229 .path = "show acl-plugin sessions",
3230 .short_help = "show acl-plugin sessions",
3231 .function = acl_show_aclplugin_sessions_fn,
3234 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3235 .path = "show acl-plugin tables",
3236 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3237 .function = acl_show_aclplugin_tables_fn,
3240 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3241 .path = "show acl-plugin macip acl",
3242 .short_help = "show acl-plugin macip acl",
3243 .function = acl_show_aclplugin_macip_acl_fn,
3246 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3247 .path = "show acl-plugin macip interface",
3248 .short_help = "show acl-plugin macip interface",
3249 .function = acl_show_aclplugin_macip_interface_fn,
3252 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3253 .path = "clear acl-plugin sessions",
3254 .short_help = "clear acl-plugin sessions",
3255 .function = acl_clear_aclplugin_fn,
3259 static clib_error_t *
3260 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3262 acl_main_t *am = &acl_main;
3263 u32 conn_table_hash_buckets;
3264 u32 conn_table_hash_memory_size;
3265 u32 conn_table_max_entries;
3268 u32 hash_lookup_hash_buckets;
3269 u32 hash_lookup_hash_memory;
3271 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3274 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3275 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3276 else if (unformat (input, "connection hash memory %d",
3277 &conn_table_hash_memory_size))
3278 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3279 else if (unformat (input, "connection count max %d",
3280 &conn_table_max_entries))
3281 am->fa_conn_table_max_entries = conn_table_max_entries;
3282 else if (unformat (input, "main heap size %d", &main_heap_size))
3283 am->acl_mheap_size = main_heap_size;
3284 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3285 am->hash_lookup_mheap_size = hash_heap_size;
3286 else if (unformat (input, "hash lookup hash buckets %d",
3287 &hash_lookup_hash_buckets))
3288 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3289 else if (unformat (input, "hash lookup hash memory %d",
3290 &hash_lookup_hash_memory))
3291 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3293 return clib_error_return (0, "unknown input '%U'",
3294 format_unformat_error, input);
3299 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3301 static clib_error_t *
3302 acl_init (vlib_main_t * vm)
3304 acl_main_t *am = &acl_main;
3305 clib_error_t *error = 0;
3306 memset (am, 0, sizeof (*am));
3308 am->vnet_main = vnet_get_main ();
3310 u8 *name = format (0, "acl_%08x%c", api_version, 0);
3312 /* Ask for a correctly-sized block of API message decode slots */
3313 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3314 VL_MSG_FIRST_AVAILABLE);
3316 error = acl_plugin_api_hookup (vm);
3318 /* Add our API messages to the global name_crc hash table */
3319 setup_message_id_table (am, &api_main);
3323 acl_setup_fa_nodes ();
3325 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3326 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3328 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3329 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3331 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3332 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3333 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3334 TCP_SESSION_IDLE_TIMEOUT_SEC;
3335 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3336 UDP_SESSION_IDLE_TIMEOUT_SEC;
3338 am->fa_conn_table_hash_num_buckets =
3339 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3340 am->fa_conn_table_hash_memory_size =
3341 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3342 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3343 vlib_thread_main_t *tm = vlib_get_thread_main ();
3344 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3348 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3350 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3351 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3352 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3353 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3355 pw->fa_conn_list_head[tt] = ~0;
3356 pw->fa_conn_list_tail[tt] = ~0;
3361 am->fa_min_deleted_sessions_per_interval =
3362 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3363 am->fa_max_deleted_sessions_per_interval =
3364 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3365 am->fa_cleaner_wait_time_increment =
3366 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3368 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3369 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3370 am->fa_cleaner_cnt_unknown_event = 0;
3371 am->fa_cleaner_cnt_timer_restarted = 0;
3372 am->fa_cleaner_cnt_wait_with_timeout = 0;
3375 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3378 am->l4_match_nonfirst_fragment = 1;
3380 /* use the new fancy hash-based matching */
3381 am->use_hash_acl_matching = 1;
3386 VLIB_INIT_FUNCTION (acl_init);
3390 * fd.io coding-style-patch-verification: ON
3393 * eval: (c-set-style "gnu")