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_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1512 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1514 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1515 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1517 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1518 a->ip6_table_index, a->l2_table_index,
1520 /* return the first unhappy outcome but make try to plough through. */
1527 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1528 u32 * acl_list_index, u8 * tag)
1530 acl_main_t *am = &acl_main;
1531 macip_acl_list_t *a;
1532 macip_acl_rule_t *r;
1533 macip_acl_rule_t *acl_new_rules = 0;
1537 if (*acl_list_index != ~0)
1539 /* They supplied some number, let's see if this MACIP ACL exists */
1540 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1542 /* tried to replace a non-existent ACL, no point doing anything */
1544 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1545 *acl_list_index, tag);
1546 return VNET_API_ERROR_NO_SUCH_ENTRY;
1553 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1556 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1557 if (~0 != *acl_list_index)
1558 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1559 void *oldheap = acl_set_heap (am);
1560 /* Create and populate the rules */
1562 vec_validate (acl_new_rules, count - 1);
1564 for (i = 0; i < count; i++)
1566 r = &acl_new_rules[i];
1567 r->is_permit = rules[i].is_permit;
1568 r->is_ipv6 = rules[i].is_ipv6;
1569 memcpy (&r->src_mac, rules[i].src_mac, 6);
1570 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1571 if (rules[i].is_ipv6)
1572 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1574 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1575 r->src_prefixlen = rules[i].src_ip_prefix_len;
1578 if (~0 == *acl_list_index)
1581 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1582 memset (a, 0, sizeof (*a));
1583 /* Will return the newly allocated ACL index */
1584 *acl_list_index = a - am->macip_acls;
1588 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1591 vec_free (a->rules);
1593 macip_destroy_classify_tables (am, *acl_list_index);
1596 a->rules = acl_new_rules;
1598 memcpy (a->tag, tag, sizeof (a->tag));
1600 /* Create and populate the classifer tables */
1601 macip_create_classify_tables (am, *acl_list_index);
1602 clib_mem_set_heap (oldheap);
1603 /* If the ACL was already applied somewhere, reapply the newly created tables */
1605 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1610 /* No check for validity of sw_if_index - the callers were supposed to validate */
1613 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1616 u32 macip_acl_index;
1617 macip_acl_list_t *a;
1618 void *oldheap = acl_set_heap (am);
1619 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1620 clib_mem_set_heap (oldheap);
1621 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1622 /* No point in deleting MACIP ACL which is not applied */
1623 if (~0 == macip_acl_index)
1624 return VNET_API_ERROR_NO_SUCH_ENTRY;
1625 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1626 /* remove the classifier tables off the interface L2 ACL */
1628 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1629 a->ip6_table_index, a->l2_table_index, 0);
1630 /* Unset the MACIP ACL index */
1631 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1635 /* No check for validity of sw_if_index - the callers were supposed to validate */
1638 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1639 u32 macip_acl_index)
1641 macip_acl_list_t *a;
1643 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1645 return VNET_API_ERROR_NO_SUCH_ENTRY;
1647 void *oldheap = acl_set_heap (am);
1648 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1649 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1650 clib_mem_set_heap (oldheap);
1651 /* If there already a MACIP ACL applied, unapply it */
1652 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1653 macip_acl_interface_del_acl (am, sw_if_index);
1654 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1656 /* Apply the classifier tables for L2 ACLs */
1658 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1659 a->ip6_table_index, a->l2_table_index, 1);
1664 macip_acl_del_list (u32 acl_list_index)
1666 acl_main_t *am = &acl_main;
1667 macip_acl_list_t *a;
1669 if (pool_is_free_index (am->macip_acls, acl_list_index))
1671 return VNET_API_ERROR_NO_SUCH_ENTRY;
1674 /* delete any references to the ACL */
1675 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1677 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1679 macip_acl_interface_del_acl (am, i);
1683 void *oldheap = acl_set_heap (am);
1684 /* Now that classifier tables are detached, clean them up */
1685 macip_destroy_classify_tables (am, acl_list_index);
1687 /* now we can delete the ACL itself */
1688 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1691 vec_free (a->rules);
1693 pool_put (am->macip_acls, a);
1694 clib_mem_set_heap (oldheap);
1700 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1703 acl_main_t *am = &acl_main;
1707 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1711 rv = macip_acl_interface_del_acl (am, sw_if_index);
1717 * If the client does not allocate enough memory for a variable-length
1718 * message, and then proceed to use it as if the full memory allocated,
1719 * absent the check we happily consume that on the VPP side, and go
1720 * along as if nothing happened. However, the resulting
1721 * effects range from just garbage in the API decode
1722 * (because the decoder snoops too far), to potential memory
1725 * This verifies that the actual length of the message is
1726 * at least expected_len, and complains loudly if it is not.
1728 * A failing check here is 100% a software bug on the API user side,
1729 * so we might as well yell.
1733 verify_message_len (void *mp, u32 expected_len, char *where)
1735 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1736 if (supplied_len < expected_len)
1738 clib_warning ("%s: Supplied message length %d is less than expected %d",
1739 where, supplied_len, expected_len);
1748 /* API message handler */
1750 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1752 vl_api_acl_add_replace_reply_t *rmp;
1753 acl_main_t *am = &acl_main;
1755 u32 acl_list_index = ntohl (mp->acl_index);
1756 u32 acl_count = ntohl (mp->count);
1757 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1759 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1761 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1765 rv = VNET_API_ERROR_INVALID_VALUE;
1769 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1771 rmp->acl_index = htonl(acl_list_index);
1777 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1779 acl_main_t *am = &acl_main;
1780 vl_api_acl_del_reply_t *rmp;
1783 rv = acl_del_list (ntohl (mp->acl_index));
1785 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1789 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1791 acl_main_t *am = &acl_main;
1792 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1793 u32 sw_if_index = ntohl (mp->sw_if_index);
1794 vl_api_acl_interface_add_del_reply_t *rmp;
1797 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1798 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1801 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1802 mp->is_input, ntohl (mp->acl_index));
1804 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1808 vl_api_acl_interface_set_acl_list_t_handler
1809 (vl_api_acl_interface_set_acl_list_t * mp)
1811 acl_main_t *am = &acl_main;
1812 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1815 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1816 u32 sw_if_index = ntohl (mp->sw_if_index);
1818 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1819 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1822 acl_interface_reset_inout_acls (sw_if_index, 0);
1823 acl_interface_reset_inout_acls (sw_if_index, 1);
1825 for (i = 0; i < mp->count; i++)
1827 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1829 /* ACL does not exist, so we can not apply it */
1830 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1835 for (i = 0; i < mp->count; i++)
1837 acl_interface_add_del_inout_acl (sw_if_index, 1,
1839 ntohl (mp->acls[i]));
1844 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1848 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1850 api_rule->is_permit = r->is_permit;
1851 api_rule->is_ipv6 = r->is_ipv6;
1854 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1855 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1859 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1860 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1862 api_rule->src_ip_prefix_len = r->src_prefixlen;
1863 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1864 api_rule->proto = r->proto;
1865 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1866 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1867 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1868 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1869 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1870 api_rule->tcp_flags_value = r->tcp_flags_value;
1874 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1875 acl_list_t * acl, u32 context)
1877 vl_api_acl_details_t *mp;
1878 vl_api_acl_rule_t *rules;
1880 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1881 void *oldheap = acl_set_heap (am);
1883 mp = vl_msg_api_alloc (msg_size);
1884 memset (mp, 0, msg_size);
1885 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1887 /* fill in the message */
1888 mp->context = context;
1889 mp->count = htonl (acl->count);
1890 mp->acl_index = htonl (acl - am->acls);
1891 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1892 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1894 for (i = 0; i < acl->count; i++)
1896 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1899 clib_mem_set_heap (oldheap);
1900 vl_msg_api_send_shmem (q, (u8 *) & mp);
1905 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1907 acl_main_t *am = &acl_main;
1912 unix_shared_memory_queue_t *q;
1914 q = vl_api_client_index_to_input_queue (mp->client_index);
1920 if (mp->acl_index == ~0)
1923 /* Just dump all ACLs */
1924 pool_foreach (acl, am->acls,
1926 send_acl_details(am, q, acl, mp->context);
1932 acl_index = ntohl (mp->acl_index);
1933 if (!pool_is_free_index (am->acls, acl_index))
1935 acl = pool_elt_at_index (am->acls, acl_index);
1936 send_acl_details (am, q, acl, mp->context);
1942 /* FIXME API: should we signal an error here at all ? */
1948 send_acl_interface_list_details (acl_main_t * am,
1949 unix_shared_memory_queue_t * q,
1950 u32 sw_if_index, u32 context)
1952 vl_api_acl_interface_list_details_t *mp;
1958 void *oldheap = acl_set_heap (am);
1960 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1961 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1963 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1964 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1965 count = n_input + n_output;
1967 msg_size = sizeof (*mp);
1968 msg_size += sizeof (mp->acls[0]) * count;
1970 mp = vl_msg_api_alloc (msg_size);
1971 memset (mp, 0, msg_size);
1973 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1975 /* fill in the message */
1976 mp->context = context;
1977 mp->sw_if_index = htonl (sw_if_index);
1979 mp->n_input = n_input;
1980 for (i = 0; i < n_input; i++)
1982 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1984 for (i = 0; i < n_output; i++)
1986 mp->acls[n_input + i] =
1987 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1989 clib_mem_set_heap (oldheap);
1990 vl_msg_api_send_shmem (q, (u8 *) & mp);
1994 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1997 acl_main_t *am = &acl_main;
1998 vnet_sw_interface_t *swif;
1999 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2002 unix_shared_memory_queue_t *q;
2004 q = vl_api_client_index_to_input_queue (mp->client_index);
2010 if (mp->sw_if_index == ~0)
2013 pool_foreach (swif, im->sw_interfaces,
2015 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
2021 sw_if_index = ntohl (mp->sw_if_index);
2022 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2023 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
2027 /* MACIP ACL API handlers */
2030 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2032 vl_api_macip_acl_add_reply_t *rmp;
2033 acl_main_t *am = &acl_main;
2035 u32 acl_list_index = ~0;
2036 u32 acl_count = ntohl (mp->count);
2037 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2039 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2041 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2045 rv = VNET_API_ERROR_INVALID_VALUE;
2049 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2051 rmp->acl_index = htonl(acl_list_index);
2057 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2059 vl_api_macip_acl_add_replace_reply_t *rmp;
2060 acl_main_t *am = &acl_main;
2062 u32 acl_list_index = ntohl (mp->acl_index);
2063 u32 acl_count = ntohl (mp->count);
2064 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2066 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2068 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2072 rv = VNET_API_ERROR_INVALID_VALUE;
2076 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2078 rmp->acl_index = htonl(acl_list_index);
2084 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2086 acl_main_t *am = &acl_main;
2087 vl_api_macip_acl_del_reply_t *rmp;
2090 rv = macip_acl_del_list (ntohl (mp->acl_index));
2092 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2096 vl_api_macip_acl_interface_add_del_t_handler
2097 (vl_api_macip_acl_interface_add_del_t * mp)
2099 acl_main_t *am = &acl_main;
2100 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2102 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2103 u32 sw_if_index = ntohl (mp->sw_if_index);
2105 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2106 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2109 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2110 ntohl (mp->acl_index));
2112 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2116 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
2117 macip_acl_list_t * acl, u32 context)
2119 vl_api_macip_acl_details_t *mp;
2120 vl_api_macip_acl_rule_t *rules;
2121 macip_acl_rule_t *r;
2123 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2125 mp = vl_msg_api_alloc (msg_size);
2126 memset (mp, 0, msg_size);
2127 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2129 /* fill in the message */
2130 mp->context = context;
2133 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2134 mp->count = htonl (acl->count);
2135 mp->acl_index = htonl (acl - am->macip_acls);
2137 for (i = 0; i < acl->count; i++)
2140 rules[i].is_permit = r->is_permit;
2141 rules[i].is_ipv6 = r->is_ipv6;
2142 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2143 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2144 sizeof (r->src_mac_mask));
2146 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2147 sizeof (r->src_ip_addr.ip6));
2149 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2150 sizeof (r->src_ip_addr.ip4));
2151 rules[i].src_ip_prefix_len = r->src_prefixlen;
2156 /* No martini, no party - no ACL applied to this interface. */
2161 vl_msg_api_send_shmem (q, (u8 *) & mp);
2166 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2168 acl_main_t *am = &acl_main;
2169 macip_acl_list_t *acl;
2171 unix_shared_memory_queue_t *q;
2173 q = vl_api_client_index_to_input_queue (mp->client_index);
2179 if (mp->acl_index == ~0)
2181 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2182 pool_foreach (acl, am->macip_acls, (
2184 send_macip_acl_details (am, q, acl,
2192 u32 acl_index = ntohl (mp->acl_index);
2193 if (!pool_is_free_index (am->macip_acls, acl_index))
2195 acl = pool_elt_at_index (am->macip_acls, acl_index);
2196 send_macip_acl_details (am, q, acl, mp->context);
2202 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2205 acl_main_t *am = &acl_main;
2206 vl_api_macip_acl_interface_get_reply_t *rmp;
2207 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2208 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2209 unix_shared_memory_queue_t *q;
2212 q = vl_api_client_index_to_input_queue (mp->client_index);
2218 rmp = vl_msg_api_alloc (msg_size);
2219 memset (rmp, 0, msg_size);
2221 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2222 rmp->context = mp->context;
2223 rmp->count = htonl (count);
2224 for (i = 0; i < count; i++)
2226 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2229 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2233 send_macip_acl_interface_list_details (acl_main_t * am,
2234 unix_shared_memory_queue_t * q,
2236 u32 acl_index, u32 context)
2238 vl_api_macip_acl_interface_list_details_t *rmp;
2239 /* at this time there is only ever 1 mac ip acl per interface */
2240 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2242 rmp = vl_msg_api_alloc (msg_size);
2243 memset (rmp, 0, msg_size);
2245 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2247 /* fill in the message */
2248 rmp->context = context;
2250 rmp->sw_if_index = htonl (sw_if_index);
2251 rmp->acls[0] = htonl (acl_index);
2253 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2257 vl_api_macip_acl_interface_list_dump_t_handler
2258 (vl_api_macip_acl_interface_list_dump_t * mp)
2260 unix_shared_memory_queue_t *q;
2261 acl_main_t *am = &acl_main;
2262 u32 sw_if_index = ntohl (mp->sw_if_index);
2264 q = vl_api_client_index_to_input_queue (mp->client_index);
2270 if (sw_if_index == ~0)
2272 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2274 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2276 send_macip_acl_interface_list_details (am, q, sw_if_index,
2277 am->macip_acl_by_sw_if_index
2285 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2287 send_macip_acl_interface_list_details (am, q, sw_if_index,
2288 am->macip_acl_by_sw_if_index
2289 [sw_if_index], mp->context);
2294 /* Set up the API message handling tables */
2295 static clib_error_t *
2296 acl_plugin_api_hookup (vlib_main_t * vm)
2298 acl_main_t *am = &acl_main;
2300 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2302 vl_api_##n##_t_handler, \
2304 vl_api_##n##_t_endian, \
2305 vl_api_##n##_t_print, \
2306 sizeof(vl_api_##n##_t), 1);
2307 foreach_acl_plugin_api_msg;
2313 #define vl_msg_name_crc_list
2314 #include <acl/acl_all_api_h.h>
2315 #undef vl_msg_name_crc_list
2318 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2320 #define _(id,n,crc) \
2321 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2322 foreach_vl_msg_name_crc_acl;
2327 acl_setup_fa_nodes (void)
2329 vlib_main_t *vm = vlib_get_main ();
2330 acl_main_t *am = &acl_main;
2331 vlib_node_t *n, *n4, *n6;
2333 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2334 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2335 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2338 am->l2_input_classify_next_acl_ip4 =
2339 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2340 am->l2_input_classify_next_acl_ip6 =
2341 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2343 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2344 l2input_get_feat_names (),
2345 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2347 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2348 l2input_get_feat_names (),
2349 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2352 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2353 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2354 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2356 am->l2_output_classify_next_acl_ip4 =
2357 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2358 am->l2_output_classify_next_acl_ip6 =
2359 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2361 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2362 l2output_get_feat_names (),
2363 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2365 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2366 l2output_get_feat_names (),
2367 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2371 acl_set_timeout_sec (int timeout_type, u32 value)
2373 acl_main_t *am = &acl_main;
2374 clib_time_t *ct = &am->vlib_main->clib_time;
2376 if (timeout_type < ACL_N_TIMEOUTS)
2378 am->session_timeout_sec[timeout_type] = value;
2382 clib_warning ("Unknown timeout type %d", timeout_type);
2385 am->session_timeout[timeout_type] =
2386 (u64) (((f64) value) / ct->seconds_per_clock);
2390 acl_set_session_max_entries (u32 value)
2392 acl_main_t *am = &acl_main;
2393 am->fa_conn_table_max_entries = value;
2397 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2399 acl_main_t *am = &acl_main;
2401 if ((eh < 256) && (value < 2))
2403 am->fa_ipv6_known_eh_bitmap =
2404 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2412 static clib_error_t *
2413 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2415 acl_main_t *am = &acl_main;
2416 if (0 == am->acl_mheap)
2418 /* ACL heap is not initialized, so definitely nothing to do. */
2423 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2424 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2426 /* also unapply any ACLs in case the users did not do so. */
2427 macip_acl_interface_del_acl (am, sw_if_index);
2428 acl_interface_reset_inout_acls (sw_if_index, 0);
2429 acl_interface_reset_inout_acls (sw_if_index, 1);
2434 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2438 static clib_error_t *
2439 acl_set_aclplugin_fn (vlib_main_t * vm,
2440 unformat_input_t * input, vlib_cli_command_t * cmd)
2442 clib_error_t *error = 0;
2446 uword memory_size = 0;
2447 acl_main_t *am = &acl_main;
2449 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2451 if (!acl_set_skip_ipv6_eh (eh_val, val))
2453 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2457 if (unformat (input, "use-hash-acl-matching %u", &val))
2459 am->use_hash_acl_matching = (val != 0);
2462 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2464 am->l4_match_nonfirst_fragment = (val != 0);
2467 if (unformat (input, "heap"))
2469 if (unformat (input, "main"))
2471 if (unformat (input, "validate %u", &val))
2472 acl_plugin_acl_set_validate_heap (am, val);
2473 else if (unformat (input, "trace %u", &val))
2474 acl_plugin_acl_set_trace_heap (am, val);
2477 else if (unformat (input, "hash"))
2479 if (unformat (input, "validate %u", &val))
2480 acl_plugin_hash_acl_set_validate_heap (am, val);
2481 else if (unformat (input, "trace %u", &val))
2482 acl_plugin_hash_acl_set_trace_heap (am, val);
2487 if (unformat (input, "session"))
2489 if (unformat (input, "table"))
2491 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2492 if (unformat (input, "max-entries"))
2494 if (!unformat (input, "%u", &val))
2496 error = clib_error_return (0,
2497 "expecting maximum number of entries, got `%U`",
2498 format_unformat_error, input);
2503 acl_set_session_max_entries (val);
2507 if (unformat (input, "hash-table-buckets"))
2509 if (!unformat (input, "%u", &val))
2511 error = clib_error_return (0,
2512 "expecting maximum number of hash table buckets, got `%U`",
2513 format_unformat_error, input);
2518 am->fa_conn_table_hash_num_buckets = val;
2522 if (unformat (input, "hash-table-memory"))
2524 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2526 error = clib_error_return (0,
2527 "expecting maximum amount of hash table memory, got `%U`",
2528 format_unformat_error, input);
2533 am->fa_conn_table_hash_memory_size = memory_size;
2539 if (unformat (input, "timeout"))
2541 if (unformat (input, "udp"))
2543 if (unformat (input, "idle"))
2545 if (!unformat (input, "%u", &timeout))
2547 error = clib_error_return (0,
2548 "expecting timeout value in seconds, got `%U`",
2549 format_unformat_error,
2555 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2560 if (unformat (input, "tcp"))
2562 if (unformat (input, "idle"))
2564 if (!unformat (input, "%u", &timeout))
2566 error = clib_error_return (0,
2567 "expecting timeout value in seconds, got `%U`",
2568 format_unformat_error,
2574 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2578 if (unformat (input, "transient"))
2580 if (!unformat (input, "%u", &timeout))
2582 error = clib_error_return (0,
2583 "expecting timeout value in seconds, got `%U`",
2584 format_unformat_error,
2590 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2604 my_format_mac_address (u8 * s, va_list * args)
2606 u8 *a = va_arg (*args, u8 *);
2607 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2608 a[0], a[1], a[2], a[3], a[4], a[5]);
2612 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2614 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2616 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2617 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2618 format_ip46_address, &a->src_ip_addr,
2619 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2621 my_format_mac_address, a->src_mac,
2622 my_format_mac_address, a->src_mac_mask);
2627 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2629 vlib_main_t *vm = am->vlib_main;
2632 /* Don't try to print someone else's memory */
2633 if (macip_acl_index > vec_len (am->macip_acls))
2636 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2637 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2639 vlib_cli_output (vm,
2640 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2641 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2643 vlib_cli_output (vm,
2644 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2645 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2646 for (i = 0; i < vec_len (a->rules); i++)
2647 vlib_cli_output (vm, " rule %d: %U\n", i,
2648 my_macip_acl_rule_t_pretty_format,
2649 vec_elt_at_index (a->rules, i));
2653 static clib_error_t *
2654 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2655 unformat_input_t * input,
2656 vlib_cli_command_t * cmd)
2658 clib_error_t *error = 0;
2659 acl_main_t *am = &acl_main;
2661 for (i = 0; i < vec_len (am->macip_acls); i++)
2662 macip_acl_print (am, i);
2666 static clib_error_t *
2667 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2668 unformat_input_t * input,
2669 vlib_cli_command_t * cmd)
2671 clib_error_t *error = 0;
2672 acl_main_t *am = &acl_main;
2674 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2676 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
2677 vec_elt (am->macip_acl_by_sw_if_index, i));
2682 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
2684 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
2687 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
2688 am->acls[acl_index].count, am->acls[acl_index].tag);
2690 PRINT_AND_RESET (vm, out0);
2691 for (j = 0; j < am->acls[acl_index].count; j++)
2693 r = &am->acls[acl_index].rules[j];
2694 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2695 out0 = format_acl_action (out0, r->is_permit);
2696 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
2697 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2700 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
2701 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
2702 out0 = format (out0, " proto %d", r->proto);
2703 out0 = format (out0, " sport %d", r->src_port_or_type_first);
2704 if (r->src_port_or_type_first != r->src_port_or_type_last)
2706 out0 = format (out0, "-%d", r->src_port_or_type_last);
2708 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
2709 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
2711 out0 = format (out0, "-%d", r->dst_port_or_code_last);
2713 if (r->tcp_flags_mask || r->tcp_flags_value)
2716 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
2719 out0 = format (out0, "\n");
2720 PRINT_AND_RESET (vm, out0);
2724 #undef PRINT_AND_RESET
2727 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2730 vlib_main_t *vm = am->vlib_main;
2732 for (i = 0; i < vec_len (am->acls); i++)
2734 if (acl_is_not_defined (am, i))
2736 /* don't attempt to show the ACLs that do not exist */
2739 if ((acl_index != ~0) && (acl_index != i))
2743 acl_print_acl (vm, am, i);
2745 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2747 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
2748 format_vec32, am->input_sw_if_index_vec_by_acl[i],
2751 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2753 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
2754 format_vec32, am->output_sw_if_index_vec_by_acl[i],
2760 static clib_error_t *
2761 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2762 unformat_input_t * input, vlib_cli_command_t * cmd)
2764 clib_error_t *error = 0;
2765 acl_main_t *am = &acl_main;
2768 (void) unformat (input, "index %u", &acl_index);
2770 acl_plugin_show_acl (am, acl_index);
2775 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
2777 vlib_main_t *vm = am->vlib_main;
2780 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
2781 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
2783 /* if we need a particular interface, skip all the others */
2784 if ((sw_if_index != ~0) && (sw_if_index != swi))
2787 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
2789 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
2790 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
2792 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
2793 am->input_acl_vec_by_sw_if_index[swi], "%d");
2796 vlib_cli_output (vm, "\n");
2797 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
2799 acl_print_acl (vm, am, *pj);
2801 vlib_cli_output (vm, "\n");
2805 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
2806 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
2808 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
2809 am->output_acl_vec_by_sw_if_index[swi], "%d");
2812 vlib_cli_output (vm, "\n");
2813 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
2815 acl_print_acl (vm, am, *pj);
2817 vlib_cli_output (vm, "\n");
2824 static clib_error_t *
2825 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
2826 unformat_input_t * input,
2827 vlib_cli_command_t * cmd)
2829 clib_error_t *error = 0;
2830 acl_main_t *am = &acl_main;
2832 u32 sw_if_index = ~0;
2833 (void) unformat (input, "sw_if_index %u", &sw_if_index);
2834 int show_acl = unformat (input, "acl");
2836 acl_plugin_show_interface (am, sw_if_index, show_acl);
2840 static clib_error_t *
2841 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
2842 unformat_input_t * input,
2843 vlib_cli_command_t * cmd)
2845 clib_error_t *error = 0;
2846 acl_main_t *am = &acl_main;
2848 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2851 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2855 vlib_cli_output (vm, " Not initialized\n");
2857 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2858 if (am->hash_lookup_mheap)
2860 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2864 vlib_cli_output (vm, " Not initialized\n");
2870 acl_plugin_show_sessions (acl_main_t * am,
2871 u32 show_session_thread_id,
2872 u32 show_session_session_index)
2874 vlib_main_t *vm = am->vlib_main;
2876 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2877 vnet_sw_interface_t *swif;
2880 u64 n_adds = am->fa_session_total_adds;
2881 u64 n_dels = am->fa_session_total_dels;
2882 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
2883 n_dels, n_adds - n_dels);
2885 vlib_cli_output (vm, "\n\nPer-thread data:");
2886 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
2888 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2889 vlib_cli_output (vm, "Thread #%d:", wk);
2890 if (show_session_thread_id == wk
2891 && show_session_session_index < pool_len (pw->fa_sessions_pool))
2893 vlib_cli_output (vm, " session index %u:",
2894 show_session_session_index);
2895 fa_session_t *sess =
2896 pw->fa_sessions_pool + show_session_session_index;
2897 u64 *m = (u64 *) & sess->info;
2898 vlib_cli_output (vm,
2899 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
2900 m[0], m[1], m[2], m[3], m[4], m[5]);
2901 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
2902 vlib_cli_output (vm, " tcp_flags_seen: %x",
2903 sess->tcp_flags_seen.as_u16);
2904 vlib_cli_output (vm, " last active time: %lu",
2905 sess->last_active_time);
2906 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
2907 vlib_cli_output (vm, " link enqueue time: %lu",
2908 sess->link_enqueue_time);
2909 vlib_cli_output (vm, " link next index: %u",
2910 sess->link_next_idx);
2911 vlib_cli_output (vm, " link prev index: %u",
2912 sess->link_prev_idx);
2913 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
2915 vlib_cli_output (vm, " connection add/del stats:", wk);
2916 pool_foreach (swif, im->sw_interfaces, (
2923 (pw->fa_session_adds_by_sw_if_index)
2925 pw->fa_session_adds_by_sw_if_index
2930 (pw->fa_session_dels_by_sw_if_index)
2932 pw->fa_session_dels_by_sw_if_index
2934 vlib_cli_output (vm,
2935 " sw_if_index %d: add %lu - del %lu = %lu",
2944 vlib_cli_output (vm, " connection timeout type lists:", wk);
2946 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
2948 u32 head_session_index = pw->fa_conn_list_head[tt];
2949 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
2950 head_session_index);
2951 if (~0 != head_session_index)
2953 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2954 vlib_cli_output (vm, " last active time: %lu",
2955 sess->last_active_time);
2956 vlib_cli_output (vm, " link enqueue time: %lu",
2957 sess->link_enqueue_time);
2961 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
2962 vlib_cli_output (vm, " Requeue until time: %lu",
2963 pw->requeue_until_time);
2964 vlib_cli_output (vm, " Current time wait interval: %lu",
2965 pw->current_time_wait_interval);
2966 vlib_cli_output (vm, " Count of deleted sessions: %lu",
2967 pw->cnt_deleted_sessions);
2968 vlib_cli_output (vm, " Delete already deleted: %lu",
2969 pw->cnt_already_deleted_sessions);
2970 vlib_cli_output (vm, " Session timers restarted: %lu",
2971 pw->cnt_session_timer_restarted);
2972 vlib_cli_output (vm, " Swipe until this time: %lu",
2973 pw->swipe_end_time);
2974 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
2975 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2976 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
2978 pw->pending_clear_sw_if_index_bitmap);
2979 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
2980 vlib_cli_output (vm, " interrupt is pending: %d",
2981 pw->interrupt_is_pending);
2982 vlib_cli_output (vm, " interrupt is needed: %d",
2983 pw->interrupt_is_needed);
2984 vlib_cli_output (vm, " interrupt is unwanted: %d",
2985 pw->interrupt_is_unwanted);
2986 vlib_cli_output (vm, " interrupt generation: %d",
2987 pw->interrupt_generation);
2989 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
2990 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
2991 foreach_fa_cleaner_counter;
2993 vlib_cli_output (vm, "Interrupt generation: %d",
2994 am->fa_interrupt_generation);
2995 vlib_cli_output (vm,
2996 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2997 am->fa_min_deleted_sessions_per_interval,
2998 am->fa_max_deleted_sessions_per_interval,
2999 am->fa_cleaner_wait_time_increment * 1000.0,
3000 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3001 1000.0 / (f64) vm->clib_time.clocks_per_second);
3004 static clib_error_t *
3005 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3006 unformat_input_t * input,
3007 vlib_cli_command_t * cmd)
3009 clib_error_t *error = 0;
3010 acl_main_t *am = &acl_main;
3012 u32 show_bihash_verbose = 0;
3013 u32 show_session_thread_id = ~0;
3014 u32 show_session_session_index = ~0;
3015 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3016 &show_session_session_index);
3017 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3019 acl_plugin_show_sessions (am, show_session_thread_id,
3020 show_session_session_index);
3021 show_fa_sessions_hash (vm, show_bihash_verbose);
3026 acl_plugin_show_tables_mask_type (acl_main_t * am)
3028 vlib_main_t *vm = am->vlib_main;
3029 ace_mask_type_entry_t *mte;
3031 vlib_cli_output (vm, "Mask-type entries:");
3033 pool_foreach(mte, am->ace_mask_type_pool,
3035 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
3036 mte - am->ace_mask_type_pool,
3037 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3038 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3044 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3046 vlib_main_t *vm = am->vlib_main;
3049 vlib_cli_output (vm, "Mask-ready ACL representations\n");
3050 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3052 if ((acl_index != ~0) && (acl_index != i))
3056 hash_acl_info_t *ha = &am->hash_acl_infos[i];
3057 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3058 vlib_cli_output (vm, " applied inbound on sw_if_index list: %U\n",
3059 format_vec32, ha->inbound_sw_if_index_list, "%d");
3060 vlib_cli_output (vm, " applied outbound on sw_if_index list: %U\n",
3061 format_vec32, ha->outbound_sw_if_index_list, "%d");
3062 vlib_cli_output (vm, " mask type index bitmap: %U\n",
3063 format_bitmap_hex, ha->mask_type_index_bitmap);
3064 for (j = 0; j < vec_len (ha->rules); j++)
3066 hash_ace_info_t *pa = &ha->rules[j];
3067 m = (u64 *) & pa->match;
3068 vlib_cli_output (vm,
3069 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3070 j, m[0], m[1], m[2], m[3], m[4], m[5],
3071 pa->mask_type_index, pa->acl_index, pa->ace_index,
3072 pa->action, pa->src_portrange_not_powerof2,
3073 pa->dst_portrange_not_powerof2);
3079 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3081 vlib_cli_output (vm,
3082 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3083 j, pae->acl_index, pae->ace_index, pae->action,
3084 pae->hash_ace_info_index, pae->next_applied_entry_index,
3085 pae->prev_applied_entry_index,
3086 pae->tail_applied_entry_index, pae->hitcount);
3090 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3092 vlib_main_t *vm = am->vlib_main;
3094 vlib_cli_output (vm, "Applied lookup entries for interfaces");
3097 (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3098 || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3099 || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3100 || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3102 if ((sw_if_index != ~0) && (sw_if_index != swi))
3106 vlib_cli_output (vm, "sw_if_index %d:", swi);
3107 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3109 applied_hash_acl_info_t *pal =
3110 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3111 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
3112 format_bitmap_hex, pal->mask_type_index_bitmap);
3113 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
3114 pal->applied_acls, "%d");
3116 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3118 vlib_cli_output (vm, " input lookup applied entries:");
3120 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3123 acl_plugin_print_pae (vm, j,
3124 &am->input_hash_entry_vec_by_sw_if_index
3129 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3131 applied_hash_acl_info_t *pal =
3132 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3133 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
3134 format_bitmap_hex, pal->mask_type_index_bitmap);
3135 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
3136 pal->applied_acls, "%d");
3138 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3140 vlib_cli_output (vm, " output lookup applied entries:");
3142 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3145 acl_plugin_print_pae (vm, j,
3146 &am->output_hash_entry_vec_by_sw_if_index
3154 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3156 vlib_main_t *vm = am->vlib_main;
3157 show_hash_acl_hash (vm, am, show_bihash_verbose);
3160 static clib_error_t *
3161 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3162 unformat_input_t * input,
3163 vlib_cli_command_t * cmd)
3165 clib_error_t *error = 0;
3166 acl_main_t *am = &acl_main;
3169 u32 sw_if_index = ~0;
3170 int show_acl_hash_info = 0;
3171 int show_applied_info = 0;
3172 int show_mask_type = 0;
3173 int show_bihash = 0;
3174 u32 show_bihash_verbose = 0;
3176 if (unformat (input, "acl"))
3178 show_acl_hash_info = 1;
3179 /* mask-type is handy to see as well right there */
3181 unformat (input, "index %u", &acl_index);
3183 else if (unformat (input, "applied"))
3185 show_applied_info = 1;
3186 unformat (input, "sw_if_index %u", &sw_if_index);
3188 else if (unformat (input, "mask"))
3192 else if (unformat (input, "hash"))
3195 unformat (input, "verbose %u", &show_bihash_verbose);
3199 (show_mask_type || show_acl_hash_info || show_applied_info
3202 /* if no qualifiers specified, show all */
3204 show_acl_hash_info = 1;
3205 show_applied_info = 1;
3209 acl_plugin_show_tables_mask_type (am);
3210 if (show_acl_hash_info)
3211 acl_plugin_show_tables_acl_hash_info (am, acl_index);
3212 if (show_applied_info)
3213 acl_plugin_show_tables_applied_info (am, sw_if_index);
3215 acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3220 static clib_error_t *
3221 acl_clear_aclplugin_fn (vlib_main_t * vm,
3222 unformat_input_t * input, vlib_cli_command_t * cmd)
3224 clib_error_t *error = 0;
3225 acl_main_t *am = &acl_main;
3226 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3227 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3232 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3233 .path = "set acl-plugin",
3234 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3235 .function = acl_set_aclplugin_fn,
3238 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3239 .path = "show acl-plugin acl",
3240 .short_help = "show acl-plugin acl [index N]",
3241 .function = acl_show_aclplugin_acl_fn,
3244 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3245 .path = "show acl-plugin interface",
3246 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3247 .function = acl_show_aclplugin_interface_fn,
3250 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3251 .path = "show acl-plugin memory",
3252 .short_help = "show acl-plugin memory",
3253 .function = acl_show_aclplugin_memory_fn,
3256 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3257 .path = "show acl-plugin sessions",
3258 .short_help = "show acl-plugin sessions",
3259 .function = acl_show_aclplugin_sessions_fn,
3262 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3263 .path = "show acl-plugin tables",
3264 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3265 .function = acl_show_aclplugin_tables_fn,
3268 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3269 .path = "show acl-plugin macip acl",
3270 .short_help = "show acl-plugin macip acl",
3271 .function = acl_show_aclplugin_macip_acl_fn,
3274 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3275 .path = "show acl-plugin macip interface",
3276 .short_help = "show acl-plugin macip interface",
3277 .function = acl_show_aclplugin_macip_interface_fn,
3280 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3281 .path = "clear acl-plugin sessions",
3282 .short_help = "clear acl-plugin sessions",
3283 .function = acl_clear_aclplugin_fn,
3287 static clib_error_t *
3288 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3290 acl_main_t *am = &acl_main;
3291 u32 conn_table_hash_buckets;
3292 u32 conn_table_hash_memory_size;
3293 u32 conn_table_max_entries;
3296 u32 hash_lookup_hash_buckets;
3297 u32 hash_lookup_hash_memory;
3299 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3302 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3303 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3304 else if (unformat (input, "connection hash memory %d",
3305 &conn_table_hash_memory_size))
3306 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3307 else if (unformat (input, "connection count max %d",
3308 &conn_table_max_entries))
3309 am->fa_conn_table_max_entries = conn_table_max_entries;
3310 else if (unformat (input, "main heap size %d", &main_heap_size))
3311 am->acl_mheap_size = main_heap_size;
3312 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3313 am->hash_lookup_mheap_size = hash_heap_size;
3314 else if (unformat (input, "hash lookup hash buckets %d",
3315 &hash_lookup_hash_buckets))
3316 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3317 else if (unformat (input, "hash lookup hash memory %d",
3318 &hash_lookup_hash_memory))
3319 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3321 return clib_error_return (0, "unknown input '%U'",
3322 format_unformat_error, input);
3327 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3329 static clib_error_t *
3330 acl_init (vlib_main_t * vm)
3332 acl_main_t *am = &acl_main;
3333 clib_error_t *error = 0;
3334 memset (am, 0, sizeof (*am));
3336 am->vnet_main = vnet_get_main ();
3338 u8 *name = format (0, "acl_%08x%c", api_version, 0);
3340 /* Ask for a correctly-sized block of API message decode slots */
3341 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3342 VL_MSG_FIRST_AVAILABLE);
3344 error = acl_plugin_api_hookup (vm);
3346 /* Add our API messages to the global name_crc hash table */
3347 setup_message_id_table (am, &api_main);
3351 acl_setup_fa_nodes ();
3353 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3354 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3356 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3357 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3359 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3360 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3361 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3362 TCP_SESSION_IDLE_TIMEOUT_SEC;
3363 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3364 UDP_SESSION_IDLE_TIMEOUT_SEC;
3366 am->fa_conn_table_hash_num_buckets =
3367 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3368 am->fa_conn_table_hash_memory_size =
3369 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3370 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3371 vlib_thread_main_t *tm = vlib_get_thread_main ();
3372 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3376 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3378 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3379 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3380 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3381 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3383 pw->fa_conn_list_head[tt] = ~0;
3384 pw->fa_conn_list_tail[tt] = ~0;
3389 am->fa_min_deleted_sessions_per_interval =
3390 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3391 am->fa_max_deleted_sessions_per_interval =
3392 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3393 am->fa_cleaner_wait_time_increment =
3394 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3396 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3397 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3398 am->fa_cleaner_cnt_unknown_event = 0;
3399 am->fa_cleaner_cnt_timer_restarted = 0;
3400 am->fa_cleaner_cnt_wait_with_timeout = 0;
3403 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3406 am->l4_match_nonfirst_fragment = 1;
3408 /* use the new fancy hash-based matching */
3409 am->use_hash_acl_matching = 1;
3414 VLIB_INIT_FUNCTION (acl_init);
3418 * fd.io coding-style-patch-verification: ON
3421 * eval: (c-set-style "gnu")