2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/in_out_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
32 /* define message structures */
34 #include <acl/acl_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <acl/acl_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <acl/acl_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
54 #include "hash_lookup.h"
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
61 /* List of message types that this plugin understands */
63 #define foreach_acl_plugin_api_msg \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace) \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
70 _(ACL_DUMP, acl_dump) \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
79 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
80 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
84 VLIB_PLUGIN_REGISTER () = {
85 .version = VPP_BUILD_VER,
86 .description = "Access Control Lists",
92 format_vec16 (u8 * s, va_list * va)
94 u16 *v = va_arg (*va, u16 *);
95 char *fmt = va_arg (*va, char *);
97 for (i = 0; i < vec_len (v); i++)
100 s = format (s, ", ");
101 s = format (s, fmt, v[i]);
110 acl_set_heap (acl_main_t * am)
112 if (0 == am->acl_mheap)
114 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
115 mheap_t *h = mheap_header (am->acl_mheap);
116 h->flags |= MHEAP_FLAG_THREAD_SAFE;
118 void *oldheap = clib_mem_set_heap (am->acl_mheap);
123 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
125 clib_mem_set_heap (acl_set_heap (am));
126 mheap_t *h = mheap_header (am->acl_mheap);
129 h->flags |= MHEAP_FLAG_VALIDATE;
130 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
135 h->flags &= ~MHEAP_FLAG_VALIDATE;
136 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
141 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
143 clib_mem_set_heap (acl_set_heap (am));
144 mheap_t *h = mheap_header (am->acl_mheap);
147 h->flags |= MHEAP_FLAG_TRACE;
151 h->flags &= ~MHEAP_FLAG_TRACE;
156 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
158 acl_main_t *am = &acl_main;
159 vl_api_acl_plugin_get_version_reply_t *rmp;
160 int msg_size = sizeof (*rmp);
161 vl_api_registration_t *reg;
163 reg = vl_api_client_index_to_registration (mp->client_index);
167 rmp = vl_msg_api_alloc (msg_size);
168 memset (rmp, 0, msg_size);
170 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
171 rmp->context = mp->context;
172 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
173 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
175 vl_api_send_msg (reg, (u8 *) rmp);
179 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
182 vl_api_acl_plugin_control_ping_reply_t *rmp;
183 acl_main_t *am = &acl_main;
187 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
189 rmp->vpe_pid = ntohl (getpid ());
195 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
196 u32 * acl_list_index, u8 * tag)
198 acl_main_t *am = &acl_main;
201 acl_rule_t *acl_new_rules = 0;
204 if (*acl_list_index != ~0)
206 /* They supplied some number, let's see if this ACL exists */
207 if (pool_is_free_index (am->acls, *acl_list_index))
209 /* tried to replace a non-existent ACL, no point doing anything */
211 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
212 *acl_list_index, tag);
213 return VNET_API_ERROR_NO_SUCH_ENTRY;
219 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
220 *acl_list_index, tag);
223 void *oldheap = acl_set_heap (am);
225 /* Create and populate the rules */
227 vec_validate (acl_new_rules, count - 1);
229 for (i = 0; i < count; i++)
231 r = vec_elt_at_index (acl_new_rules, i);
232 memset (r, 0, sizeof (*r));
233 r->is_permit = rules[i].is_permit;
234 r->is_ipv6 = rules[i].is_ipv6;
237 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
238 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
242 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
243 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
245 r->src_prefixlen = rules[i].src_ip_prefix_len;
246 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
247 r->proto = rules[i].proto;
248 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
249 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
250 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
251 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
252 r->tcp_flags_value = rules[i].tcp_flags_value;
253 r->tcp_flags_mask = rules[i].tcp_flags_mask;
256 if (~0 == *acl_list_index)
259 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
260 memset (a, 0, sizeof (*a));
261 /* Will return the newly allocated ACL index */
262 *acl_list_index = a - am->acls;
266 a = am->acls + *acl_list_index;
267 hash_acl_delete (am, *acl_list_index);
268 /* Get rid of the old rules */
272 a->rules = acl_new_rules;
274 memcpy (a->tag, tag, sizeof (a->tag));
275 hash_acl_add (am, *acl_list_index);
276 clib_mem_set_heap (oldheap);
281 acl_del_list (u32 acl_list_index)
283 acl_main_t *am = &acl_main;
286 if (pool_is_free_index (am->acls, acl_list_index))
288 return VNET_API_ERROR_NO_SUCH_ENTRY;
291 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
293 if (vec_len (vec_elt (am->input_sw_if_index_vec_by_acl, acl_list_index))
296 /* ACL is applied somewhere inbound. Refuse to delete */
297 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
300 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
303 (vec_elt (am->output_sw_if_index_vec_by_acl, acl_list_index)) > 0)
305 /* ACL is applied somewhere outbound. Refuse to delete */
306 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
310 void *oldheap = acl_set_heap (am);
311 /* delete any references to the ACL */
312 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
314 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
317 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
319 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
327 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
329 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
332 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
334 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
342 /* delete the hash table data */
344 hash_acl_delete (am, acl_list_index);
345 /* now we can delete the ACL itself */
346 a = pool_elt_at_index (am->acls, acl_list_index);
350 pool_put (am->acls, a);
351 clib_mem_set_heap (oldheap);
355 /* Some aids in ASCII graphing the content */
362 u8 ip4_5tuple_mask[] =
363 _(" dmac smac etype ")
364 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
371 _(" ttl pr checksum ")
380 _("L4 T/U sport dport ")
390 u8 ip6_5tuple_mask[] =
391 _(" dmac smac etype ")
392 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
394 _(0x0000) __ __ __ __
396 _(0x0004) __ __ XX __
398 _(0x0008) XX XX XX XX
399 _(0x000C) XX XX XX XX
400 _(0x0010) XX XX XX XX
401 _(0x0014) XX XX XX XX
403 _(0x0018) XX XX XX XX
404 _(0x001C) XX XX XX XX
405 _(0x0020) XX XX XX XX
406 _(0x0024) XX XX XX XX
407 _("L4T/U sport dport ")
408 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
410 u8 dot1q_5tuple_mask[] =
411 _(" dmac smac dot1q etype ")
412 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
413 _(padpad) __ __ __ __
414 _(padpad) __ __ __ __
415 _(padpad) __ __ __ __
418 u8 dot1ad_5tuple_mask[] =
419 _(" dmac smac dot1ad dot1q etype ")
420 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
421 _(padpad) __ __ __ __
422 _(padpad) __ __ __ __
425 u8 ethertype_mask[] =
426 _(" dmac smac etype ")
427 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
436 count_skip (u8 * p, u32 size)
438 u64 *p64 = (u64 *) p;
439 /* Be tolerant to null pointer */
443 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
447 return (p64 - (u64 *) p) / 2;
451 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
452 u32 mask_len, u32 next_table_index,
453 u32 miss_next_index, u32 * table_index,
457 u32 memory_size = 2 << 13;
458 u32 skip = count_skip (mask, mask_len);
459 u32 match = (mask_len / 16) - skip;
460 u8 *skip_mask_ptr = mask + 16 * skip;
461 u32 current_data_flag = 0;
462 int current_data_offset = 0;
466 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
467 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
468 memory_size, skip, match,
469 next_table_index, miss_next_index,
470 table_index, current_data_flag,
471 current_data_offset, is_add,
472 1 /* delete_chain */ );
473 clib_mem_set_heap (oldheap);
478 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
479 u32 mask_len, u32 next_table_index,
480 u32 miss_next_index, u32 * table_index,
484 u32 memory_size = 2 << 22;
485 u32 skip = count_skip (mask, mask_len);
486 u32 match = (mask_len / 16) - skip;
487 u8 *skip_mask_ptr = mask + 16 * skip;
488 u32 current_data_flag = 0;
489 int current_data_offset = 0;
494 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
495 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
496 memory_size, skip, match,
497 next_table_index, miss_next_index,
498 table_index, current_data_flag,
499 current_data_offset, is_add,
500 1 /* delete_chain */ );
501 clib_mem_set_heap (oldheap);
506 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
508 vnet_classify_main_t *cm = &vnet_classify_main;
509 u32 ip4_table_index = ~0;
510 u32 ip6_table_index = ~0;
511 u32 dot1q_table_index = ~0;
512 u32 dot1ad_table_index = ~0;
513 u32 etype_table_index = ~0;
514 void *oldheap = acl_set_heap (am);
516 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
518 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
520 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
522 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
524 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
527 /* switch to global heap while calling vnet_* functions */
528 clib_mem_set_heap (cm->vlib_main->heap_base);
529 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
531 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
534 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
535 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
536 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
537 sizeof (ip4_5tuple_mask) - 1, ~0,
538 am->l2_input_classify_next_acl_ip4,
539 &ip4_table_index, 0);
541 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
544 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
545 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
546 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
547 sizeof (ip6_5tuple_mask) - 1, ~0,
548 am->l2_input_classify_next_acl_ip6,
549 &ip6_table_index, 0);
551 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
554 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
555 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
556 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
557 sizeof (ip6_5tuple_mask) - 1, ~0,
558 ~0, &dot1q_table_index, 0);
560 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
563 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
564 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
565 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
566 sizeof (dot1ad_5tuple_mask) - 1, ~0,
567 ~0, &dot1ad_table_index, 0);
569 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
572 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
573 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
574 acl_classify_add_del_table_tiny (cm, ethertype_mask,
575 sizeof (ethertype_mask) - 1, ~0,
576 ~0, &etype_table_index, 0);
578 clib_mem_set_heap (oldheap);
583 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
585 vnet_classify_main_t *cm = &vnet_classify_main;
586 u32 ip4_table_index = ~0;
587 u32 ip6_table_index = ~0;
588 u32 dot1q_table_index = ~0;
589 u32 dot1ad_table_index = ~0;
590 u32 etype_table_index = ~0;
591 void *oldheap = acl_set_heap (am);
593 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
595 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
597 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
599 vec_validate_init_empty
600 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
601 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
604 /* switch to global heap while calling vnet_* functions */
605 clib_mem_set_heap (cm->vlib_main->heap_base);
607 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
609 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
612 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
613 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
614 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
615 sizeof (ip4_5tuple_mask) - 1, ~0,
616 am->l2_output_classify_next_acl_ip4,
617 &ip4_table_index, 0);
619 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
622 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
623 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
624 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
625 sizeof (ip6_5tuple_mask) - 1, ~0,
626 am->l2_output_classify_next_acl_ip6,
627 &ip6_table_index, 0);
629 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
632 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
633 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
634 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
635 sizeof (ip6_5tuple_mask) - 1, ~0,
636 ~0, &dot1q_table_index, 0);
638 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
641 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
642 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
643 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
644 sizeof (dot1ad_5tuple_mask) - 1, ~0,
645 ~0, &dot1ad_table_index, 0);
647 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
650 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
651 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
652 acl_classify_add_del_table_tiny (cm, ethertype_mask,
653 sizeof (ethertype_mask) - 1, ~0,
654 ~0, &etype_table_index, 0);
656 clib_mem_set_heap (oldheap);
661 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
662 u8 is_dot1ad, u8 is_ip6)
664 vnet_classify_main_t *cm = &vnet_classify_main;
674 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
680 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
682 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
683 idx = (is_dot1ad) ? 20 : 16;
686 /* 802.1ad ethertype */
689 /* 802.1q ethertype */
695 /* 802.1q ethertype */
700 /* add sessions to vlan tables per ethernet_type */
704 match[idx + 1] = 0xdd;
710 match[idx + 1] = 0x00;
713 vnet_classify_add_del_session (cm, table_index, match, next_acl,
714 session_idx, 0, 0, 0, 1);
715 /* reset the mask back to being a mask */
717 match[idx + 1] = 0xff;
728 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
731 ? am->input_etype_whitelist_by_sw_if_index
732 : am->output_etype_whitelist_by_sw_if_index;
733 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
737 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
738 u32 etype_table_index)
740 vnet_classify_main_t *cm = &vnet_classify_main;
742 ? am->input_etype_whitelist_by_sw_if_index
743 : am->output_etype_whitelist_by_sw_if_index;
744 u8 *match = ethertype_mask;
748 u16 *whitelist = vec_elt (v, sw_if_index);
749 u32 next = ~0; /* permit */
750 for (i = 0; i < vec_len (whitelist); i++)
753 match[12] = (whitelist[i] >> 8) & 0xff;
754 match[13] = whitelist[i] & 0xff;
756 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
757 whitelist[i], 0, 0, 0, 1);
760 /* restore the mask */
767 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
769 vnet_classify_main_t *cm = &vnet_classify_main;
770 u32 ip4_table_index = ~0;
771 u32 ip6_table_index = ~0;
772 u32 dot1q_table_index = ~0;
773 u32 dot1ad_table_index = ~0;
774 u32 etype_table_index = ~0;
777 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
779 /* in case there were previous tables attached */
780 acl_unhook_l2_input_classify (am, sw_if_index);
782 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
783 sizeof (ip4_5tuple_mask) - 1, ~0,
784 am->l2_input_classify_next_acl_ip4,
785 &ip4_table_index, 1);
790 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
791 sizeof (ip6_5tuple_mask) - 1, ~0,
792 am->l2_input_classify_next_acl_ip6,
793 &ip6_table_index, 1);
796 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
797 sizeof (ip4_5tuple_mask) - 1, ~0,
798 am->l2_input_classify_next_acl_ip4,
799 &ip4_table_index, 0);
803 if (intf_has_etype_whitelist (am, sw_if_index, 1))
805 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
806 &etype_table_index, 1);
807 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
811 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
812 sizeof (dot1ad_5tuple_mask) - 1,
813 etype_table_index, ~0,
814 &dot1ad_table_index, 1);
816 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
817 sizeof (dot1q_5tuple_mask) - 1,
818 dot1ad_table_index, ~0,
819 &dot1q_table_index, 1);
822 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
823 sizeof (dot1ad_5tuple_mask) - 1, ~0,
824 ~0, &dot1ad_table_index, 0);
825 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
826 sizeof (ip6_5tuple_mask) - 1, ~0,
827 am->l2_input_classify_next_acl_ip6,
828 &ip6_table_index, 0);
829 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
830 sizeof (ip4_5tuple_mask) - 1, ~0,
831 am->l2_input_classify_next_acl_ip4,
832 &ip4_table_index, 0);
837 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
838 ip6_table_index, dot1q_table_index);
842 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
843 sizeof (ip4_5tuple_mask) - 1, ~0,
844 am->l2_input_classify_next_acl_ip4,
845 &ip4_table_index, 0);
846 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
847 sizeof (ip6_5tuple_mask) - 1, ~0,
848 am->l2_input_classify_next_acl_ip6,
849 &ip6_table_index, 0);
850 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
851 sizeof (dot1q_5tuple_mask) - 1, ~0,
852 ~0, &dot1q_table_index, 0);
853 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
854 sizeof (dot1ad_5tuple_mask) - 1, ~0,
855 ~0, &dot1ad_table_index, 0);
859 /* add sessions to vlan tables per ethernet_type */
860 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
861 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
862 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
863 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
865 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
867 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
869 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
871 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
873 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
875 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
878 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
880 clib_mem_set_heap (prevheap);
885 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
887 vnet_classify_main_t *cm = &vnet_classify_main;
888 u32 ip4_table_index = ~0;
889 u32 ip6_table_index = ~0;
890 u32 dot1q_table_index = ~0;
891 u32 dot1ad_table_index = ~0;
892 u32 etype_table_index = ~0;
895 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
897 /* in case there were previous tables attached */
898 acl_unhook_l2_output_classify (am, sw_if_index);
900 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
901 sizeof (ip4_5tuple_mask) - 1, ~0,
902 am->l2_output_classify_next_acl_ip4,
903 &ip4_table_index, 1);
907 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
908 sizeof (ip6_5tuple_mask) - 1, ~0,
909 am->l2_output_classify_next_acl_ip6,
910 &ip6_table_index, 1);
913 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
914 sizeof (ip4_5tuple_mask) - 1, ~0,
915 am->l2_output_classify_next_acl_ip4,
916 &ip4_table_index, 0);
920 if (intf_has_etype_whitelist (am, sw_if_index, 0))
922 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
923 &etype_table_index, 1);
924 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
929 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
930 sizeof (dot1ad_5tuple_mask) - 1,
931 etype_table_index, ~0,
932 &dot1ad_table_index, 1);
934 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
935 sizeof (dot1q_5tuple_mask) - 1,
936 dot1ad_table_index, ~0,
937 &dot1q_table_index, 1);
940 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
941 sizeof (dot1ad_5tuple_mask) - 1, ~0,
942 ~0, &dot1ad_table_index, 0);
943 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
944 sizeof (ip6_5tuple_mask) - 1, ~0,
945 am->l2_output_classify_next_acl_ip6,
946 &ip6_table_index, 0);
947 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
948 sizeof (ip4_5tuple_mask) - 1, ~0,
949 am->l2_output_classify_next_acl_ip4,
950 &ip4_table_index, 0);
955 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
956 ip6_table_index, dot1q_table_index);
959 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
960 sw_if_index, ip4_table_index, ip6_table_index);
963 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
964 sizeof (ip6_5tuple_mask) - 1, ~0,
965 am->l2_output_classify_next_acl_ip6,
966 &ip6_table_index, 0);
967 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
968 sizeof (ip4_5tuple_mask) - 1, ~0,
969 am->l2_output_classify_next_acl_ip4,
970 &ip4_table_index, 0);
971 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
972 sizeof (dot1q_5tuple_mask) - 1, ~0,
973 ~0, &dot1q_table_index, 0);
974 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
975 sizeof (dot1ad_5tuple_mask) - 1, ~0,
976 ~0, &dot1ad_table_index, 0);
980 /* add sessions to vlan tables per ethernet_type */
981 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
982 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
983 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
984 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
986 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
988 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
990 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
992 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
994 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
997 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
999 clib_mem_set_heap (prevheap);
1004 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1009 /* Utterly wrong? */
1010 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1012 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1014 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1018 rv = acl_hook_l2_input_classify (am, sw_if_index);
1022 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1029 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1034 /* Utterly wrong? */
1035 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1037 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1039 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1043 rv = acl_hook_l2_output_classify (am, sw_if_index);
1047 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1054 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1056 return (pool_is_free_index (am->acls, acl_list_index));
1061 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1063 acl_main_t *am = &acl_main;
1064 if (acl_is_not_defined (am, acl_list_index))
1066 /* ACL is not defined. Can not apply */
1067 return VNET_API_ERROR_NO_SUCH_ENTRY;
1069 void *oldheap = acl_set_heap (am);
1073 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1075 u32 index = vec_search (am->input_acl_vec_by_sw_if_index[sw_if_index],
1077 if (index < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1080 ("ACL %d is already applied inbound on sw_if_index %d (index %d)",
1081 acl_list_index, sw_if_index, index);
1082 /* the entry is already there */
1083 clib_mem_set_heap (oldheap);
1084 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
1086 /* if there was no ACL applied before, enable the ACL processing */
1087 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1089 acl_interface_in_enable_disable (am, sw_if_index, 1);
1091 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
1093 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
1094 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
1099 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1101 u32 index = vec_search (am->output_acl_vec_by_sw_if_index[sw_if_index],
1103 if (index < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1106 ("ACL %d is already applied outbound on sw_if_index %d (index %d)",
1107 acl_list_index, sw_if_index, index);
1108 /* the entry is already there */
1109 clib_mem_set_heap (oldheap);
1110 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1112 /* if there was no ACL applied before, enable the ACL processing */
1113 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1115 acl_interface_out_enable_disable (am, sw_if_index, 1);
1117 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
1118 &acl_list_index, 1);
1119 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
1120 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index],
1123 clib_mem_set_heap (oldheap);
1129 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1131 acl_main_t *am = &acl_main;
1133 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1134 void *oldheap = acl_set_heap (am);
1137 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1138 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1141 if (acl_list_index ==
1142 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
1144 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
1150 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1153 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1156 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1158 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1159 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1164 /* If there is no more ACLs applied on an interface, disable ACL processing */
1165 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1167 acl_interface_in_enable_disable (am, sw_if_index, 0);
1172 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1174 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
1176 if (acl_list_index ==
1177 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
1179 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
1185 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1188 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1191 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1193 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1194 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1199 /* If there is no more ACLs applied on an interface, disable ACL processing */
1200 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1202 acl_interface_out_enable_disable (am, sw_if_index, 0);
1205 clib_mem_set_heap (oldheap);
1210 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
1212 acl_main_t *am = &acl_main;
1214 void *oldheap = acl_set_heap (am);
1217 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1218 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1220 acl_interface_in_enable_disable (am, sw_if_index, 0);
1223 for (i = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1226 u32 acl_list_index =
1227 am->input_acl_vec_by_sw_if_index[sw_if_index][i];
1228 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1229 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1232 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1235 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1237 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1243 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1247 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1248 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1250 acl_interface_out_enable_disable (am, sw_if_index, 0);
1253 for (i = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1256 u32 acl_list_index =
1257 am->output_acl_vec_by_sw_if_index[sw_if_index][i];
1258 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1259 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1262 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1265 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1267 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1273 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1275 clib_mem_set_heap (oldheap);
1279 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1282 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1283 acl_main_t *am = &acl_main;
1287 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
1290 hash_acl_apply (am, sw_if_index, is_input, acl_list_index);
1295 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1297 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
1303 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1306 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1307 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1309 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1310 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1312 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1313 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1316 * if there are already inbound/outbound ACLs applied, toggle the
1317 * enable/disable - this will recreate the necessary tables.
1320 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1322 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1324 acl_interface_in_enable_disable (am, sw_if_index, 0);
1325 acl_interface_in_enable_disable (am, sw_if_index, 1);
1328 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1330 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1332 acl_interface_out_enable_disable (am, sw_if_index, 0);
1333 acl_interface_out_enable_disable (am, sw_if_index, 1);
1348 u32 arp_table_index;
1349 u32 dot1q_table_index;
1350 u32 dot1ad_table_index;
1351 u32 arp_dot1q_table_index;
1352 u32 arp_dot1ad_table_index;
1354 u32 out_table_index;
1355 u32 out_arp_table_index;
1356 u32 out_dot1q_table_index;
1357 u32 out_dot1ad_table_index;
1358 u32 out_arp_dot1q_table_index;
1359 u32 out_arp_dot1ad_table_index;
1360 } macip_match_type_t;
1363 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1369 for (i = 0; i < vec_len (mv); i++)
1371 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1372 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1382 /* Get metric used to sort match types.
1383 The more specific and the more often seen - the bigger the metric */
1385 match_type_metric (macip_match_type_t * m)
1387 unsigned int mac_bits_set = 0;
1388 unsigned int mac_byte;
1390 for (i = 0; i < 6; i++)
1392 mac_byte = m->mac_mask[i];
1393 for (; mac_byte; mac_byte >>= 1)
1394 mac_bits_set += mac_byte & 1;
1397 * Attempt to place the more specific and the more used rules on top.
1398 * There are obvious caveat corner cases to this, but they do not
1399 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1400 * going with a wildcard IPv4 with a specific MAC).
1402 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1406 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1408 /* Ascending sort based on the metric values */
1409 return match_type_metric (m1) - match_type_metric (m2);
1412 /* Get the offset of L3 source within ethernet packet */
1414 get_l3_src_offset (int is6)
1417 return (sizeof (ethernet_header_t) +
1418 offsetof (ip6_header_t, src_address));
1420 return (sizeof (ethernet_header_t) +
1421 offsetof (ip4_header_t, src_address));
1425 get_l3_dst_offset (int is6)
1428 return (sizeof (ethernet_header_t) +
1429 offsetof (ip6_header_t, dst_address));
1431 return (sizeof (ethernet_header_t) +
1432 offsetof (ip4_header_t, dst_address));
1436 * return if the is_permit value also requires to create the egress tables
1437 * For backwards compatibility, we keep the is_permit = 1 to only
1438 * create the ingress tables, and the new value of 3 will also
1439 * create the egress tables based on destination.
1442 macip_permit_also_egress (u8 is_permit)
1444 return (is_permit == 3);
1448 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1450 macip_match_type_t *mvec = NULL;
1451 macip_match_type_t *mt;
1452 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1454 u32 match_type_index;
1458 vnet_classify_main_t *cm = &vnet_classify_main;
1460 /* Count the number of different types of rules */
1461 for (i = 0; i < a->count; i++)
1465 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1466 a->rules[i].src_prefixlen,
1467 a->rules[i].is_ipv6)))
1469 match_type_index = vec_len (mvec);
1470 vec_validate (mvec, match_type_index);
1471 memcpy (mvec[match_type_index].mac_mask,
1472 a->rules[i].src_mac_mask, 6);
1473 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1474 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1475 mvec[match_type_index].has_egress = 0;
1476 mvec[match_type_index].table_index = ~0;
1477 mvec[match_type_index].arp_table_index = ~0;
1478 mvec[match_type_index].dot1q_table_index = ~0;
1479 mvec[match_type_index].dot1ad_table_index = ~0;
1480 mvec[match_type_index].arp_dot1q_table_index = ~0;
1481 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1482 mvec[match_type_index].out_table_index = ~0;
1483 mvec[match_type_index].out_arp_table_index = ~0;
1484 mvec[match_type_index].out_dot1q_table_index = ~0;
1485 mvec[match_type_index].out_dot1ad_table_index = ~0;
1486 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1487 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1489 mvec[match_type_index].count++;
1490 mvec[match_type_index].has_egress |=
1491 macip_permit_also_egress (a->rules[i].is_permit);
1493 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1494 vec_sort_with_function (mvec, match_type_compare);
1495 /* Create the classifier tables */
1497 out_last_table = ~0;
1498 /* First add ARP tables */
1499 vec_foreach (mt, mvec)
1502 int is6 = mt->is_ipv6;
1504 u32 *last_tag_table;
1505 u32 *out_last_tag_table;
1512 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1513 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1514 | Destination Address |
1515 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1517 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1519 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1520 | EtherType | Hardware Type |
1521 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1522 | Protocol Type | Hw addr len | Proto addr len|
1523 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1525 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1526 | Sender Hardware Address |
1527 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1528 | Sender Protocol Address |
1529 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1530 | Target Hardware Address |
1531 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1532 | | TargetProtocolAddress |
1533 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1535 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1537 for (tags = 2; tags >= 0; tags--)
1539 memset (mask, 0, sizeof (mask));
1540 /* source MAC address */
1541 memcpy (&mask[6], mt->mac_mask, 6);
1547 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1549 last_tag_table = &mt->arp_table_index;
1552 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1553 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1555 last_tag_table = &mt->arp_dot1q_table_index;
1558 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1559 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1560 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1562 last_tag_table = &mt->arp_dot1ad_table_index;
1566 /* sender hardware address within ARP */
1567 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1568 /* sender protocol address within ARP */
1569 for (i = 0; i < (mt->prefix_len / 8); i++)
1570 mask[l3_offset + 14 + i] = 0xff;
1571 if (mt->prefix_len % 8)
1572 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1573 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1575 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1577 1)) / sizeof (u32x4)) * sizeof (u32x4);
1578 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1579 (~0 == last_table) ? 0 : ~0,
1581 last_table = *last_tag_table;
1584 /* egress ARP table */
1585 memset (mask, 0, sizeof (mask));
1591 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1593 out_last_tag_table = &mt->out_arp_table_index;
1596 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1597 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1599 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1602 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1603 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1604 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1606 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1610 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1611 /* mask captures just the destination and the ethertype */
1612 mask_len = ((l3_offset +
1614 1)) / sizeof (u32x4)) * sizeof (u32x4);
1615 acl_classify_add_del_table_small (cm, mask, mask_len,
1618 out_last_table) ? 0 : ~0,
1619 out_last_tag_table, 1);
1620 out_last_table = *out_last_tag_table;
1625 /* Now add IP[46] tables */
1626 vec_foreach (mt, mvec)
1629 int is6 = mt->is_ipv6;
1633 u32 *last_tag_table;
1634 u32 *out_last_tag_table;
1637 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1639 for (tags = 2; tags >= 0; tags--)
1641 memset (mask, 0, sizeof (mask));
1642 memcpy (&mask[6], mt->mac_mask, 6);
1643 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1648 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1649 last_tag_table = &mt->table_index;
1652 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1653 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1654 last_tag_table = &mt->dot1q_table_index;
1657 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1658 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1659 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1660 last_tag_table = &mt->dot1ad_table_index;
1663 for (i = 0; i < (mt->prefix_len / 8); i++)
1665 mask[l3_src_offs + i] = 0xff;
1667 if (mt->prefix_len % 8)
1669 mask[l3_src_offs + (mt->prefix_len / 8)] =
1670 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1673 * Round-up the number of bytes needed to store the prefix,
1674 * and round up the number of vectors too
1676 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1677 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1678 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1679 (~0 == last_table) ? 0 : ~0,
1681 last_table = *last_tag_table;
1685 for (tags = 2; tags >= 0; tags--)
1687 memset (mask, 0, sizeof (mask));
1688 /* MAC destination */
1689 memcpy (&mask[0], mt->mac_mask, 6);
1690 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1695 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1696 out_last_tag_table = &mt->out_table_index;
1699 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1700 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1701 out_last_tag_table = &mt->out_dot1q_table_index;
1704 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1705 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1706 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1707 out_last_tag_table = &mt->out_dot1ad_table_index;
1710 for (i = 0; i < (mt->prefix_len / 8); i++)
1712 mask[l3_dst_offs + i] = 0xff;
1714 if (mt->prefix_len % 8)
1716 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1717 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1720 * Round-up the number of bytes needed to store the prefix,
1721 * and round up the number of vectors too
1723 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1725 1)) / sizeof (u32x4)) * sizeof (u32x4);
1726 acl_classify_add_del_table_small (cm, mask, mask_len,
1728 (~0 == out_last_table) ? 0 : ~0,
1729 out_last_tag_table, 1);
1730 out_last_table = *out_last_tag_table;
1734 a->ip4_table_index = last_table;
1735 a->ip6_table_index = last_table;
1736 a->l2_table_index = last_table;
1738 a->out_ip4_table_index = out_last_table;
1739 a->out_ip6_table_index = out_last_table;
1740 a->out_l2_table_index = out_last_table;
1742 /* Populate the classifier tables with rules from the MACIP ACL */
1743 for (i = 0; i < a->count; i++)
1747 int is6 = a->rules[i].is_ipv6;
1754 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1755 a->rules[i].src_prefixlen,
1756 a->rules[i].is_ipv6);
1757 ASSERT (match_type_index != ~0);
1759 for (tags = 2; tags >= 0; tags--)
1761 memset (mask, 0, sizeof (mask));
1762 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1763 memcpy (&mask[6], a->rules[i].src_mac, 6);
1768 tag_table = mvec[match_type_index].table_index;
1772 tag_table = mvec[match_type_index].dot1q_table_index;
1778 tag_table = mvec[match_type_index].dot1ad_table_index;
1788 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1790 mask[eth + 1] = 0xdd;
1794 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1796 mask[eth + 1] = 0x00;
1799 /* add session to table mvec[match_type_index].table_index; */
1800 vnet_classify_add_del_session (cm, tag_table,
1801 mask, a->rules[i].is_permit ? ~0 : 0,
1802 i, 0, action, metadata, 1);
1803 memset (&mask[12], 0, sizeof (mask) - 12);
1806 /* add ARP table entry too */
1807 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1809 memset (mask, 0, sizeof (mask));
1810 memcpy (&mask[6], a->rules[i].src_mac, 6);
1812 for (tags = 2; tags >= 0; tags--)
1818 tag_table = mvec[match_type_index].arp_table_index;
1824 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1832 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1843 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1844 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1846 vnet_classify_add_del_session (cm, tag_table, mask,
1847 a->rules[i].is_permit ? ~0 : 0,
1848 i, 0, action, metadata, 1);
1851 if (macip_permit_also_egress (a->rules[i].is_permit))
1853 /* Add the egress entry with destination set */
1854 for (tags = 2; tags >= 0; tags--)
1856 memset (mask, 0, sizeof (mask));
1857 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1858 /* src mac in the other direction becomes dst */
1859 memcpy (&mask[0], a->rules[i].src_mac, 6);
1864 tag_table = mvec[match_type_index].out_table_index;
1868 tag_table = mvec[match_type_index].out_dot1q_table_index;
1874 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1884 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1887 mask[eth + 1] = 0xdd;
1891 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1894 mask[eth + 1] = 0x00;
1897 /* add session to table mvec[match_type_index].table_index; */
1898 vnet_classify_add_del_session (cm, tag_table,
1900 a->rules[i].is_permit ? ~0 : 0,
1901 i, 0, action, metadata, 1);
1902 // memset (&mask[12], 0, sizeof (mask) - 12);
1905 /* add ARP table entry too */
1906 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1908 for (tags = 2; tags >= 0; tags--)
1910 memset (mask, 0, sizeof (mask));
1915 tag_table = mvec[match_type_index].out_arp_table_index;
1921 mvec[match_type_index].out_arp_dot1q_table_index;
1929 mvec[match_type_index].out_arp_dot1ad_table_index;
1939 vnet_classify_add_del_session (cm, tag_table,
1942 rules[i].is_permit ? ~0 : 0,
1943 i, 0, action, metadata, 1);
1952 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1954 vnet_classify_main_t *cm = &vnet_classify_main;
1955 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1957 if (a->ip4_table_index != ~0)
1959 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1960 &a->ip4_table_index, 0);
1961 a->ip4_table_index = ~0;
1963 if (a->ip6_table_index != ~0)
1965 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1966 &a->ip6_table_index, 0);
1967 a->ip6_table_index = ~0;
1969 if (a->l2_table_index != ~0)
1971 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1973 a->l2_table_index = ~0;
1975 if (a->out_ip4_table_index != ~0)
1977 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1978 &a->out_ip4_table_index, 0);
1979 a->out_ip4_table_index = ~0;
1981 if (a->out_ip6_table_index != ~0)
1983 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1984 &a->out_ip6_table_index, 0);
1985 a->out_ip6_table_index = ~0;
1987 if (a->out_l2_table_index != ~0)
1989 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1990 &a->out_l2_table_index, 0);
1991 a->out_l2_table_index = ~0;
1996 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2002 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2004 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2005 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2007 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2008 a->ip6_table_index, a->l2_table_index,
2010 /* return the first unhappy outcome but make try to plough through. */
2013 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2014 a->out_ip6_table_index,
2015 a->out_l2_table_index, is_apply);
2016 /* return the first unhappy outcome but make try to plough through. */
2023 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2024 u32 * acl_list_index, u8 * tag)
2026 acl_main_t *am = &acl_main;
2027 macip_acl_list_t *a;
2028 macip_acl_rule_t *r;
2029 macip_acl_rule_t *acl_new_rules = 0;
2033 if (*acl_list_index != ~0)
2035 /* They supplied some number, let's see if this MACIP ACL exists */
2036 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2038 /* tried to replace a non-existent ACL, no point doing anything */
2040 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2041 *acl_list_index, tag);
2042 return VNET_API_ERROR_NO_SUCH_ENTRY;
2049 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2052 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2053 if (~0 != *acl_list_index)
2054 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2055 void *oldheap = acl_set_heap (am);
2056 /* Create and populate the rules */
2058 vec_validate (acl_new_rules, count - 1);
2060 for (i = 0; i < count; i++)
2062 r = &acl_new_rules[i];
2063 r->is_permit = rules[i].is_permit;
2064 r->is_ipv6 = rules[i].is_ipv6;
2065 memcpy (&r->src_mac, rules[i].src_mac, 6);
2066 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2067 if (rules[i].is_ipv6)
2068 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2070 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2071 r->src_prefixlen = rules[i].src_ip_prefix_len;
2074 if (~0 == *acl_list_index)
2077 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2078 memset (a, 0, sizeof (*a));
2079 /* Will return the newly allocated ACL index */
2080 *acl_list_index = a - am->macip_acls;
2084 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2087 vec_free (a->rules);
2089 macip_destroy_classify_tables (am, *acl_list_index);
2092 a->rules = acl_new_rules;
2094 memcpy (a->tag, tag, sizeof (a->tag));
2096 /* Create and populate the classifer tables */
2097 macip_create_classify_tables (am, *acl_list_index);
2098 clib_mem_set_heap (oldheap);
2099 /* If the ACL was already applied somewhere, reapply the newly created tables */
2101 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2106 /* No check for validity of sw_if_index - the callers were supposed to validate */
2109 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2112 u32 macip_acl_index;
2113 macip_acl_list_t *a;
2114 void *oldheap = acl_set_heap (am);
2115 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2116 clib_mem_set_heap (oldheap);
2117 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2118 /* No point in deleting MACIP ACL which is not applied */
2119 if (~0 == macip_acl_index)
2120 return VNET_API_ERROR_NO_SUCH_ENTRY;
2121 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2122 /* remove the classifier tables off the interface L2 ACL */
2124 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2125 a->ip6_table_index, a->l2_table_index, 0);
2127 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2128 a->out_ip4_table_index, a->out_ip6_table_index,
2129 a->out_l2_table_index, 0);
2130 /* Unset the MACIP ACL index */
2131 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2135 /* No check for validity of sw_if_index - the callers were supposed to validate */
2138 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2139 u32 macip_acl_index)
2141 macip_acl_list_t *a;
2143 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2145 return VNET_API_ERROR_NO_SUCH_ENTRY;
2147 void *oldheap = acl_set_heap (am);
2148 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2149 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2150 clib_mem_set_heap (oldheap);
2151 /* If there already a MACIP ACL applied, unapply it */
2152 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2153 macip_acl_interface_del_acl (am, sw_if_index);
2154 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2156 /* Apply the classifier tables for L2 ACLs */
2158 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2159 a->ip6_table_index, a->l2_table_index, 1);
2161 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2162 a->out_ip4_table_index, a->out_ip6_table_index,
2163 a->out_l2_table_index, 1);
2168 macip_acl_del_list (u32 acl_list_index)
2170 acl_main_t *am = &acl_main;
2171 macip_acl_list_t *a;
2173 if (pool_is_free_index (am->macip_acls, acl_list_index))
2175 return VNET_API_ERROR_NO_SUCH_ENTRY;
2178 /* delete any references to the ACL */
2179 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2181 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2183 macip_acl_interface_del_acl (am, i);
2187 void *oldheap = acl_set_heap (am);
2188 /* Now that classifier tables are detached, clean them up */
2189 macip_destroy_classify_tables (am, acl_list_index);
2191 /* now we can delete the ACL itself */
2192 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2195 vec_free (a->rules);
2197 pool_put (am->macip_acls, a);
2198 clib_mem_set_heap (oldheap);
2204 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2207 acl_main_t *am = &acl_main;
2211 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2215 rv = macip_acl_interface_del_acl (am, sw_if_index);
2221 * If the client does not allocate enough memory for a variable-length
2222 * message, and then proceed to use it as if the full memory allocated,
2223 * absent the check we happily consume that on the VPP side, and go
2224 * along as if nothing happened. However, the resulting
2225 * effects range from just garbage in the API decode
2226 * (because the decoder snoops too far), to potential memory
2229 * This verifies that the actual length of the message is
2230 * at least expected_len, and complains loudly if it is not.
2232 * A failing check here is 100% a software bug on the API user side,
2233 * so we might as well yell.
2237 verify_message_len (void *mp, u32 expected_len, char *where)
2239 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2240 if (supplied_len < expected_len)
2242 clib_warning ("%s: Supplied message length %d is less than expected %d",
2243 where, supplied_len, expected_len);
2252 /* API message handler */
2254 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2256 vl_api_acl_add_replace_reply_t *rmp;
2257 acl_main_t *am = &acl_main;
2259 u32 acl_list_index = ntohl (mp->acl_index);
2260 u32 acl_count = ntohl (mp->count);
2261 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2263 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2265 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2269 rv = VNET_API_ERROR_INVALID_VALUE;
2273 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2275 rmp->acl_index = htonl(acl_list_index);
2281 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2283 acl_main_t *am = &acl_main;
2284 vl_api_acl_del_reply_t *rmp;
2287 rv = acl_del_list (ntohl (mp->acl_index));
2289 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2293 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2295 acl_main_t *am = &acl_main;
2296 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2297 u32 sw_if_index = ntohl (mp->sw_if_index);
2298 vl_api_acl_interface_add_del_reply_t *rmp;
2301 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2302 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2305 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2306 mp->is_input, ntohl (mp->acl_index));
2308 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2312 vl_api_acl_interface_set_acl_list_t_handler
2313 (vl_api_acl_interface_set_acl_list_t * mp)
2315 acl_main_t *am = &acl_main;
2316 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2319 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2320 u32 sw_if_index = ntohl (mp->sw_if_index);
2322 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2323 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2326 acl_interface_reset_inout_acls (sw_if_index, 0);
2327 acl_interface_reset_inout_acls (sw_if_index, 1);
2329 for (i = 0; i < mp->count; i++)
2331 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2333 /* ACL does not exist, so we can not apply it */
2334 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2339 for (i = 0; i < mp->count; i++)
2341 acl_interface_add_del_inout_acl (sw_if_index, 1,
2343 ntohl (mp->acls[i]));
2348 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2352 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2354 api_rule->is_permit = r->is_permit;
2355 api_rule->is_ipv6 = r->is_ipv6;
2358 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2359 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2363 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2364 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2366 api_rule->src_ip_prefix_len = r->src_prefixlen;
2367 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2368 api_rule->proto = r->proto;
2369 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2370 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2371 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2372 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2373 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2374 api_rule->tcp_flags_value = r->tcp_flags_value;
2378 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2379 acl_list_t * acl, u32 context)
2381 vl_api_acl_details_t *mp;
2382 vl_api_acl_rule_t *rules;
2384 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2385 void *oldheap = acl_set_heap (am);
2387 mp = vl_msg_api_alloc (msg_size);
2388 memset (mp, 0, msg_size);
2389 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2391 /* fill in the message */
2392 mp->context = context;
2393 mp->count = htonl (acl->count);
2394 mp->acl_index = htonl (acl - am->acls);
2395 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2396 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2398 for (i = 0; i < acl->count; i++)
2400 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2403 clib_mem_set_heap (oldheap);
2404 vl_api_send_msg (reg, (u8 *) mp);
2409 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2411 acl_main_t *am = &acl_main;
2415 vl_api_registration_t *reg;
2417 reg = vl_api_client_index_to_registration (mp->client_index);
2421 if (mp->acl_index == ~0)
2424 /* Just dump all ACLs */
2425 pool_foreach (acl, am->acls,
2427 send_acl_details(am, reg, acl, mp->context);
2433 acl_index = ntohl (mp->acl_index);
2434 if (!pool_is_free_index (am->acls, acl_index))
2436 acl = pool_elt_at_index (am->acls, acl_index);
2437 send_acl_details (am, reg, acl, mp->context);
2443 /* FIXME API: should we signal an error here at all ? */
2449 send_acl_interface_list_details (acl_main_t * am,
2450 vl_api_registration_t * reg,
2451 u32 sw_if_index, u32 context)
2453 vl_api_acl_interface_list_details_t *mp;
2459 void *oldheap = acl_set_heap (am);
2461 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2462 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2464 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2465 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2466 count = n_input + n_output;
2468 msg_size = sizeof (*mp);
2469 msg_size += sizeof (mp->acls[0]) * count;
2471 mp = vl_msg_api_alloc (msg_size);
2472 memset (mp, 0, msg_size);
2474 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2476 /* fill in the message */
2477 mp->context = context;
2478 mp->sw_if_index = htonl (sw_if_index);
2480 mp->n_input = n_input;
2481 for (i = 0; i < n_input; i++)
2483 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2485 for (i = 0; i < n_output; i++)
2487 mp->acls[n_input + i] =
2488 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2490 clib_mem_set_heap (oldheap);
2491 vl_api_send_msg (reg, (u8 *) mp);
2495 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2498 acl_main_t *am = &acl_main;
2499 vnet_sw_interface_t *swif;
2500 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2503 vl_api_registration_t *reg;
2505 reg = vl_api_client_index_to_registration (mp->client_index);
2509 if (mp->sw_if_index == ~0)
2512 pool_foreach (swif, im->sw_interfaces,
2514 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2520 sw_if_index = ntohl (mp->sw_if_index);
2521 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2522 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2526 /* MACIP ACL API handlers */
2529 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2531 vl_api_macip_acl_add_reply_t *rmp;
2532 acl_main_t *am = &acl_main;
2534 u32 acl_list_index = ~0;
2535 u32 acl_count = ntohl (mp->count);
2536 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2538 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2540 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2544 rv = VNET_API_ERROR_INVALID_VALUE;
2548 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2550 rmp->acl_index = htonl(acl_list_index);
2556 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2558 vl_api_macip_acl_add_replace_reply_t *rmp;
2559 acl_main_t *am = &acl_main;
2561 u32 acl_list_index = ntohl (mp->acl_index);
2562 u32 acl_count = ntohl (mp->count);
2563 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2565 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2567 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2571 rv = VNET_API_ERROR_INVALID_VALUE;
2575 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2577 rmp->acl_index = htonl(acl_list_index);
2583 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2585 acl_main_t *am = &acl_main;
2586 vl_api_macip_acl_del_reply_t *rmp;
2589 rv = macip_acl_del_list (ntohl (mp->acl_index));
2591 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2595 vl_api_macip_acl_interface_add_del_t_handler
2596 (vl_api_macip_acl_interface_add_del_t * mp)
2598 acl_main_t *am = &acl_main;
2599 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2601 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2602 u32 sw_if_index = ntohl (mp->sw_if_index);
2604 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2605 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2608 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2609 ntohl (mp->acl_index));
2611 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2615 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2616 macip_acl_list_t * acl, u32 context)
2618 vl_api_macip_acl_details_t *mp;
2619 vl_api_macip_acl_rule_t *rules;
2620 macip_acl_rule_t *r;
2622 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2624 mp = vl_msg_api_alloc (msg_size);
2625 memset (mp, 0, msg_size);
2626 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2628 /* fill in the message */
2629 mp->context = context;
2632 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2633 mp->count = htonl (acl->count);
2634 mp->acl_index = htonl (acl - am->macip_acls);
2636 for (i = 0; i < acl->count; i++)
2639 rules[i].is_permit = r->is_permit;
2640 rules[i].is_ipv6 = r->is_ipv6;
2641 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2642 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2643 sizeof (r->src_mac_mask));
2645 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2646 sizeof (r->src_ip_addr.ip6));
2648 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2649 sizeof (r->src_ip_addr.ip4));
2650 rules[i].src_ip_prefix_len = r->src_prefixlen;
2655 /* No martini, no party - no ACL applied to this interface. */
2660 vl_api_send_msg (reg, (u8 *) mp);
2665 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2667 acl_main_t *am = &acl_main;
2668 macip_acl_list_t *acl;
2670 vl_api_registration_t *reg;
2672 reg = vl_api_client_index_to_registration (mp->client_index);
2676 if (mp->acl_index == ~0)
2678 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2679 pool_foreach (acl, am->macip_acls, (
2681 send_macip_acl_details (am, reg,
2690 u32 acl_index = ntohl (mp->acl_index);
2691 if (!pool_is_free_index (am->macip_acls, acl_index))
2693 acl = pool_elt_at_index (am->macip_acls, acl_index);
2694 send_macip_acl_details (am, reg, acl, mp->context);
2700 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2703 acl_main_t *am = &acl_main;
2704 vl_api_macip_acl_interface_get_reply_t *rmp;
2705 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2706 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2707 vl_api_registration_t *reg;
2710 reg = vl_api_client_index_to_registration (mp->client_index);
2714 rmp = vl_msg_api_alloc (msg_size);
2715 memset (rmp, 0, msg_size);
2717 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2718 rmp->context = mp->context;
2719 rmp->count = htonl (count);
2720 for (i = 0; i < count; i++)
2722 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2725 vl_api_send_msg (reg, (u8 *) rmp);
2729 send_macip_acl_interface_list_details (acl_main_t * am,
2730 vl_api_registration_t * reg,
2732 u32 acl_index, u32 context)
2734 vl_api_macip_acl_interface_list_details_t *rmp;
2735 /* at this time there is only ever 1 mac ip acl per interface */
2736 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2738 rmp = vl_msg_api_alloc (msg_size);
2739 memset (rmp, 0, msg_size);
2741 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2743 /* fill in the message */
2744 rmp->context = context;
2746 rmp->sw_if_index = htonl (sw_if_index);
2747 rmp->acls[0] = htonl (acl_index);
2749 vl_api_send_msg (reg, (u8 *) rmp);
2753 vl_api_macip_acl_interface_list_dump_t_handler
2754 (vl_api_macip_acl_interface_list_dump_t * mp)
2756 vl_api_registration_t *reg;
2757 acl_main_t *am = &acl_main;
2758 u32 sw_if_index = ntohl (mp->sw_if_index);
2760 reg = vl_api_client_index_to_registration (mp->client_index);
2764 if (sw_if_index == ~0)
2766 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2768 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2770 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2771 am->macip_acl_by_sw_if_index
2779 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2781 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2782 am->macip_acl_by_sw_if_index
2783 [sw_if_index], mp->context);
2789 vl_api_acl_interface_set_etype_whitelist_t_handler
2790 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2792 acl_main_t *am = &acl_main;
2793 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2796 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2797 u32 sw_if_index = ntohl (mp->sw_if_index);
2798 u16 *vec_in = 0, *vec_out = 0;
2799 void *oldheap = acl_set_heap (am);
2801 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2802 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2805 for (i = 0; i < mp->count; i++)
2807 if (i < mp->n_input)
2808 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2810 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2812 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2815 clib_mem_set_heap (oldheap);
2816 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2820 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2821 vl_api_registration_t * reg,
2822 u32 sw_if_index, u32 context)
2824 vl_api_acl_interface_etype_whitelist_details_t *mp;
2831 u16 *whitelist_in = 0;
2832 u16 *whitelist_out = 0;
2834 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2836 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2838 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2840 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2842 if ((0 == whitelist_in) && (0 == whitelist_out))
2843 return; /* nothing to do */
2845 void *oldheap = acl_set_heap (am);
2847 n_input = vec_len (whitelist_in);
2848 n_output = vec_len (whitelist_out);
2849 count = n_input + n_output;
2851 msg_size = sizeof (*mp);
2852 msg_size += sizeof (mp->whitelist[0]) * count;
2854 mp = vl_msg_api_alloc (msg_size);
2855 memset (mp, 0, msg_size);
2857 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2859 /* fill in the message */
2860 mp->context = context;
2861 mp->sw_if_index = htonl (sw_if_index);
2863 mp->n_input = n_input;
2864 for (i = 0; i < n_input; i++)
2866 mp->whitelist[i] = htons (whitelist_in[i]);
2868 for (i = 0; i < n_output; i++)
2870 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2872 clib_mem_set_heap (oldheap);
2873 vl_api_send_msg (reg, (u8 *) mp);
2878 vl_api_acl_interface_etype_whitelist_dump_t_handler
2879 (vl_api_acl_interface_list_dump_t * mp)
2881 acl_main_t *am = &acl_main;
2882 vnet_sw_interface_t *swif;
2883 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2886 vl_api_registration_t *reg;
2888 reg = vl_api_client_index_to_registration (mp->client_index);
2892 if (mp->sw_if_index == ~0)
2895 pool_foreach (swif, im->sw_interfaces,
2897 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2903 sw_if_index = ntohl (mp->sw_if_index);
2904 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2905 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2912 /* Set up the API message handling tables */
2913 static clib_error_t *
2914 acl_plugin_api_hookup (vlib_main_t * vm)
2916 acl_main_t *am = &acl_main;
2918 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2920 vl_api_##n##_t_handler, \
2922 vl_api_##n##_t_endian, \
2923 vl_api_##n##_t_print, \
2924 sizeof(vl_api_##n##_t), 1);
2925 foreach_acl_plugin_api_msg;
2931 #define vl_msg_name_crc_list
2932 #include <acl/acl_all_api_h.h>
2933 #undef vl_msg_name_crc_list
2936 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2938 #define _(id,n,crc) \
2939 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2940 foreach_vl_msg_name_crc_acl;
2945 acl_setup_fa_nodes (void)
2947 vlib_main_t *vm = vlib_get_main ();
2948 acl_main_t *am = &acl_main;
2949 vlib_node_t *n, *n4, *n6;
2951 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2952 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2953 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2956 am->l2_input_classify_next_acl_ip4 =
2957 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2958 am->l2_input_classify_next_acl_ip6 =
2959 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2961 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2962 l2input_get_feat_names (),
2963 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2965 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2966 l2input_get_feat_names (),
2967 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2970 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2971 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2972 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2974 am->l2_output_classify_next_acl_ip4 =
2975 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2976 am->l2_output_classify_next_acl_ip6 =
2977 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2979 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2980 l2output_get_feat_names (),
2981 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2983 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2984 l2output_get_feat_names (),
2985 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2989 acl_set_timeout_sec (int timeout_type, u32 value)
2991 acl_main_t *am = &acl_main;
2992 clib_time_t *ct = &am->vlib_main->clib_time;
2994 if (timeout_type < ACL_N_TIMEOUTS)
2996 am->session_timeout_sec[timeout_type] = value;
3000 clib_warning ("Unknown timeout type %d", timeout_type);
3003 am->session_timeout[timeout_type] =
3004 (u64) (((f64) value) / ct->seconds_per_clock);
3008 acl_set_session_max_entries (u32 value)
3010 acl_main_t *am = &acl_main;
3011 am->fa_conn_table_max_entries = value;
3015 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3017 acl_main_t *am = &acl_main;
3019 if ((eh < 256) && (value < 2))
3021 am->fa_ipv6_known_eh_bitmap =
3022 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3030 static clib_error_t *
3031 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3033 acl_main_t *am = &acl_main;
3034 if (0 == am->acl_mheap)
3036 /* ACL heap is not initialized, so definitely nothing to do. */
3041 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3042 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3044 /* also unapply any ACLs in case the users did not do so. */
3045 macip_acl_interface_del_acl (am, sw_if_index);
3046 acl_interface_reset_inout_acls (sw_if_index, 0);
3047 acl_interface_reset_inout_acls (sw_if_index, 1);
3052 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3056 static clib_error_t *
3057 acl_set_aclplugin_fn (vlib_main_t * vm,
3058 unformat_input_t * input, vlib_cli_command_t * cmd)
3060 clib_error_t *error = 0;
3064 uword memory_size = 0;
3065 acl_main_t *am = &acl_main;
3067 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3069 if (!acl_set_skip_ipv6_eh (eh_val, val))
3071 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3075 if (unformat (input, "use-hash-acl-matching %u", &val))
3077 am->use_hash_acl_matching = (val != 0);
3080 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3082 am->l4_match_nonfirst_fragment = (val != 0);
3085 if (unformat (input, "heap"))
3087 if (unformat (input, "main"))
3089 if (unformat (input, "validate %u", &val))
3090 acl_plugin_acl_set_validate_heap (am, val);
3091 else if (unformat (input, "trace %u", &val))
3092 acl_plugin_acl_set_trace_heap (am, val);
3095 else if (unformat (input, "hash"))
3097 if (unformat (input, "validate %u", &val))
3098 acl_plugin_hash_acl_set_validate_heap (am, val);
3099 else if (unformat (input, "trace %u", &val))
3100 acl_plugin_hash_acl_set_trace_heap (am, val);
3105 if (unformat (input, "session"))
3107 if (unformat (input, "table"))
3109 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3110 if (unformat (input, "max-entries"))
3112 if (!unformat (input, "%u", &val))
3114 error = clib_error_return (0,
3115 "expecting maximum number of entries, got `%U`",
3116 format_unformat_error, input);
3121 acl_set_session_max_entries (val);
3125 if (unformat (input, "hash-table-buckets"))
3127 if (!unformat (input, "%u", &val))
3129 error = clib_error_return (0,
3130 "expecting maximum number of hash table buckets, got `%U`",
3131 format_unformat_error, input);
3136 am->fa_conn_table_hash_num_buckets = val;
3140 if (unformat (input, "hash-table-memory"))
3142 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3144 error = clib_error_return (0,
3145 "expecting maximum amount of hash table memory, got `%U`",
3146 format_unformat_error, input);
3151 am->fa_conn_table_hash_memory_size = memory_size;
3155 if (unformat (input, "event-trace"))
3157 if (!unformat (input, "%u", &val))
3159 error = clib_error_return (0,
3160 "expecting trace level, got `%U`",
3161 format_unformat_error, input);
3166 am->trace_sessions = val;
3172 if (unformat (input, "timeout"))
3174 if (unformat (input, "udp"))
3176 if (unformat (input, "idle"))
3178 if (!unformat (input, "%u", &timeout))
3180 error = clib_error_return (0,
3181 "expecting timeout value in seconds, got `%U`",
3182 format_unformat_error,
3188 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3193 if (unformat (input, "tcp"))
3195 if (unformat (input, "idle"))
3197 if (!unformat (input, "%u", &timeout))
3199 error = clib_error_return (0,
3200 "expecting timeout value in seconds, got `%U`",
3201 format_unformat_error,
3207 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3211 if (unformat (input, "transient"))
3213 if (!unformat (input, "%u", &timeout))
3215 error = clib_error_return (0,
3216 "expecting timeout value in seconds, got `%U`",
3217 format_unformat_error,
3223 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3237 my_format_mac_address (u8 * s, va_list * args)
3239 u8 *a = va_arg (*args, u8 *);
3240 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3241 a[0], a[1], a[2], a[3], a[4], a[5]);
3245 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3247 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3249 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3250 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3251 format_ip46_address, &a->src_ip_addr,
3252 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3254 my_format_mac_address, a->src_mac,
3255 my_format_mac_address, a->src_mac_mask);
3260 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3262 vlib_main_t *vm = am->vlib_main;
3265 /* Don't attempt to show the ACLs that do not exist */
3266 if (pool_is_free_index (am->macip_acls, macip_acl_index))
3269 /* Don't try to print someone else's memory */
3270 if (macip_acl_index > vec_len (am->macip_acls))
3273 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3274 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3276 vlib_cli_output (vm,
3277 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3278 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3280 vlib_cli_output (vm,
3281 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3282 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3283 vlib_cli_output (vm,
3284 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3285 a->out_ip4_table_index, a->out_ip6_table_index,
3286 a->out_l2_table_index);
3287 for (i = 0; i < vec_len (a->rules); i++)
3288 vlib_cli_output (vm, " rule %d: %U\n", i,
3289 my_macip_acl_rule_t_pretty_format,
3290 vec_elt_at_index (a->rules, i));
3294 static clib_error_t *
3295 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3297 input, vlib_cli_command_t * cmd)
3299 clib_error_t *error = 0;
3300 acl_main_t *am = &acl_main;
3302 for (i = 0; i < vec_len (am->macip_acls); i++)
3303 macip_acl_print (am, i);
3307 static clib_error_t *
3308 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3310 input, vlib_cli_command_t * cmd)
3312 clib_error_t *error = 0;
3313 acl_main_t *am = &acl_main;
3315 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3317 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3318 vec_elt (am->macip_acl_by_sw_if_index, i));
3323 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
3325 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
3328 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
3329 am->acls[acl_index].count, am->acls[acl_index].tag);
3331 PRINT_AND_RESET (vm, out0);
3332 for (j = 0; j < am->acls[acl_index].count; j++)
3334 r = &am->acls[acl_index].rules[j];
3335 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
3336 out0 = format_acl_action (out0, r->is_permit);
3337 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
3338 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3341 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
3342 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
3343 out0 = format (out0, " proto %d", r->proto);
3344 out0 = format (out0, " sport %d", r->src_port_or_type_first);
3345 if (r->src_port_or_type_first != r->src_port_or_type_last)
3347 out0 = format (out0, "-%d", r->src_port_or_type_last);
3349 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
3350 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
3352 out0 = format (out0, "-%d", r->dst_port_or_code_last);
3354 if (r->tcp_flags_mask || r->tcp_flags_value)
3357 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
3360 out0 = format (out0, "\n");
3361 PRINT_AND_RESET (vm, out0);
3365 #undef PRINT_AND_RESET
3368 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3371 vlib_main_t *vm = am->vlib_main;
3373 for (i = 0; i < vec_len (am->acls); i++)
3375 if (acl_is_not_defined (am, i))
3377 /* don't attempt to show the ACLs that do not exist */
3380 if ((acl_index != ~0) && (acl_index != i))
3384 acl_print_acl (vm, am, i);
3386 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3388 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3389 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3392 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3394 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3395 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3401 static clib_error_t *
3402 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3403 unformat_input_t * input, vlib_cli_command_t * cmd)
3405 clib_error_t *error = 0;
3406 acl_main_t *am = &acl_main;
3409 (void) unformat (input, "index %u", &acl_index);
3411 acl_plugin_show_acl (am, acl_index);
3416 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
3418 vlib_main_t *vm = am->vlib_main;
3421 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3422 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3424 /* if we need a particular interface, skip all the others */
3425 if ((sw_if_index != ~0) && (sw_if_index != swi))
3428 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3430 if (intf_has_etype_whitelist (am, swi, 1))
3432 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3433 am->input_etype_whitelist_by_sw_if_index[swi],
3436 if (intf_has_etype_whitelist (am, swi, 0))
3438 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3439 am->output_etype_whitelist_by_sw_if_index[swi],
3443 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3444 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3446 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3447 am->input_acl_vec_by_sw_if_index[swi], "%d");
3450 vlib_cli_output (vm, "\n");
3451 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3453 acl_print_acl (vm, am, *pj);
3455 vlib_cli_output (vm, "\n");
3459 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3460 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3462 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3463 am->output_acl_vec_by_sw_if_index[swi], "%d");
3466 vlib_cli_output (vm, "\n");
3467 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3469 acl_print_acl (vm, am, *pj);
3471 vlib_cli_output (vm, "\n");
3479 static clib_error_t *
3480 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3481 unformat_input_t * input,
3482 vlib_cli_command_t * cmd)
3484 clib_error_t *error = 0;
3485 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3488 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3489 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3490 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3491 format_acl_plugin_5tuple, five_tuple);
3493 error = clib_error_return (0, "expecting 6 hex integers");
3498 static clib_error_t *
3499 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3501 input, vlib_cli_command_t * cmd)
3503 clib_error_t *error = 0;
3504 acl_main_t *am = &acl_main;
3506 u32 sw_if_index = ~0;
3507 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3508 int show_acl = unformat (input, "acl");
3510 acl_plugin_show_interface (am, sw_if_index, show_acl);
3514 static clib_error_t *
3515 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3516 unformat_input_t * input,
3517 vlib_cli_command_t * cmd)
3519 clib_error_t *error = 0;
3520 acl_main_t *am = &acl_main;
3522 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3525 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3529 vlib_cli_output (vm, " Not initialized\n");
3531 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3532 if (am->hash_lookup_mheap)
3534 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3538 vlib_cli_output (vm, " Not initialized\n");
3544 acl_plugin_show_sessions (acl_main_t * am,
3545 u32 show_session_thread_id,
3546 u32 show_session_session_index)
3548 vlib_main_t *vm = am->vlib_main;
3550 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3551 vnet_sw_interface_t *swif;
3554 u64 n_adds = am->fa_session_total_adds;
3555 u64 n_dels = am->fa_session_total_dels;
3556 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3557 n_dels, n_adds - n_dels);
3559 vlib_cli_output (vm, "\n\nPer-thread data:");
3560 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3562 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3563 vlib_cli_output (vm, "Thread #%d:", wk);
3564 if (show_session_thread_id == wk
3565 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3567 vlib_cli_output (vm, " session index %u:",
3568 show_session_session_index);
3569 fa_session_t *sess =
3570 pw->fa_sessions_pool + show_session_session_index;
3571 u64 *m = (u64 *) & sess->info;
3572 vlib_cli_output (vm,
3573 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3574 m[0], m[1], m[2], m[3], m[4], m[5]);
3575 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3576 vlib_cli_output (vm, " tcp_flags_seen: %x",
3577 sess->tcp_flags_seen.as_u16);
3578 vlib_cli_output (vm, " last active time: %lu",
3579 sess->last_active_time);
3580 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3581 vlib_cli_output (vm, " link enqueue time: %lu",
3582 sess->link_enqueue_time);
3583 vlib_cli_output (vm, " link next index: %u",
3584 sess->link_next_idx);
3585 vlib_cli_output (vm, " link prev index: %u",
3586 sess->link_prev_idx);
3587 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3589 vlib_cli_output (vm, " connection add/del stats:", wk);
3590 pool_foreach (swif, im->sw_interfaces, (
3597 (pw->fa_session_adds_by_sw_if_index)
3599 pw->fa_session_adds_by_sw_if_index
3604 (pw->fa_session_dels_by_sw_if_index)
3606 pw->fa_session_dels_by_sw_if_index
3608 vlib_cli_output (vm,
3609 " sw_if_index %d: add %lu - del %lu = %lu",
3618 vlib_cli_output (vm, " connection timeout type lists:", wk);
3620 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3622 u32 head_session_index = pw->fa_conn_list_head[tt];
3623 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3624 head_session_index);
3625 if (~0 != head_session_index)
3627 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3628 vlib_cli_output (vm, " last active time: %lu",
3629 sess->last_active_time);
3630 vlib_cli_output (vm, " link enqueue time: %lu",
3631 sess->link_enqueue_time);
3635 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3636 vlib_cli_output (vm, " Requeue until time: %lu",
3637 pw->requeue_until_time);
3638 vlib_cli_output (vm, " Current time wait interval: %lu",
3639 pw->current_time_wait_interval);
3640 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3641 pw->cnt_deleted_sessions);
3642 vlib_cli_output (vm, " Delete already deleted: %lu",
3643 pw->cnt_already_deleted_sessions);
3644 vlib_cli_output (vm, " Session timers restarted: %lu",
3645 pw->cnt_session_timer_restarted);
3646 vlib_cli_output (vm, " Swipe until this time: %lu",
3647 pw->swipe_end_time);
3648 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3649 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3650 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3652 pw->pending_clear_sw_if_index_bitmap);
3653 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3654 vlib_cli_output (vm, " interrupt is pending: %d",
3655 pw->interrupt_is_pending);
3656 vlib_cli_output (vm, " interrupt is needed: %d",
3657 pw->interrupt_is_needed);
3658 vlib_cli_output (vm, " interrupt is unwanted: %d",
3659 pw->interrupt_is_unwanted);
3660 vlib_cli_output (vm, " interrupt generation: %d",
3661 pw->interrupt_generation);
3663 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3664 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3665 foreach_fa_cleaner_counter;
3667 vlib_cli_output (vm, "Interrupt generation: %d",
3668 am->fa_interrupt_generation);
3669 vlib_cli_output (vm,
3670 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3671 am->fa_min_deleted_sessions_per_interval,
3672 am->fa_max_deleted_sessions_per_interval,
3673 am->fa_cleaner_wait_time_increment * 1000.0,
3674 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3675 1000.0 / (f64) vm->clib_time.clocks_per_second);
3678 static clib_error_t *
3679 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3680 unformat_input_t * input,
3681 vlib_cli_command_t * cmd)
3683 clib_error_t *error = 0;
3684 acl_main_t *am = &acl_main;
3686 u32 show_bihash_verbose = 0;
3687 u32 show_session_thread_id = ~0;
3688 u32 show_session_session_index = ~0;
3689 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3690 &show_session_session_index);
3691 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3693 acl_plugin_show_sessions (am, show_session_thread_id,
3694 show_session_session_index);
3695 show_fa_sessions_hash (vm, show_bihash_verbose);
3700 acl_plugin_show_tables_mask_type (acl_main_t * am)
3702 vlib_main_t *vm = am->vlib_main;
3703 ace_mask_type_entry_t *mte;
3705 vlib_cli_output (vm, "Mask-type entries:");
3707 pool_foreach(mte, am->ace_mask_type_pool,
3709 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
3710 mte - am->ace_mask_type_pool,
3711 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3712 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3718 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3720 vlib_main_t *vm = am->vlib_main;
3723 vlib_cli_output (vm, "Mask-ready ACL representations\n");
3724 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3726 if ((acl_index != ~0) && (acl_index != i))
3730 hash_acl_info_t *ha = &am->hash_acl_infos[i];
3731 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3732 vlib_cli_output (vm, " applied inbound on sw_if_index list: %U\n",
3733 format_vec32, ha->inbound_sw_if_index_list, "%d");
3734 vlib_cli_output (vm, " applied outbound on sw_if_index list: %U\n",
3735 format_vec32, ha->outbound_sw_if_index_list, "%d");
3736 vlib_cli_output (vm, " mask type index bitmap: %U\n",
3737 format_bitmap_hex, ha->mask_type_index_bitmap);
3738 for (j = 0; j < vec_len (ha->rules); j++)
3740 hash_ace_info_t *pa = &ha->rules[j];
3741 m = (u64 *) & pa->match;
3742 vlib_cli_output (vm,
3743 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3744 j, m[0], m[1], m[2], m[3], m[4], m[5],
3745 pa->mask_type_index, pa->acl_index, pa->ace_index,
3746 pa->action, pa->src_portrange_not_powerof2,
3747 pa->dst_portrange_not_powerof2);
3753 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3755 vlib_cli_output (vm,
3756 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3757 j, pae->acl_index, pae->ace_index, pae->action,
3758 pae->hash_ace_info_index, pae->next_applied_entry_index,
3759 pae->prev_applied_entry_index,
3760 pae->tail_applied_entry_index, pae->hitcount);
3764 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3766 vlib_main_t *vm = am->vlib_main;
3768 vlib_cli_output (vm, "Applied lookup entries for interfaces");
3771 (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3772 || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3773 || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3774 || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3776 if ((sw_if_index != ~0) && (sw_if_index != swi))
3780 vlib_cli_output (vm, "sw_if_index %d:", swi);
3781 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3783 applied_hash_acl_info_t *pal =
3784 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3785 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
3786 format_bitmap_hex, pal->mask_type_index_bitmap);
3787 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
3788 pal->applied_acls, "%d");
3790 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3792 vlib_cli_output (vm, " input lookup applied entries:");
3794 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3797 acl_plugin_print_pae (vm, j,
3798 &am->input_hash_entry_vec_by_sw_if_index
3803 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3805 applied_hash_acl_info_t *pal =
3806 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3807 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
3808 format_bitmap_hex, pal->mask_type_index_bitmap);
3809 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
3810 pal->applied_acls, "%d");
3812 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3814 vlib_cli_output (vm, " output lookup applied entries:");
3816 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3819 acl_plugin_print_pae (vm, j,
3820 &am->output_hash_entry_vec_by_sw_if_index
3828 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3830 vlib_main_t *vm = am->vlib_main;
3831 show_hash_acl_hash (vm, am, show_bihash_verbose);
3834 static clib_error_t *
3835 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3836 unformat_input_t * input,
3837 vlib_cli_command_t * cmd)
3839 clib_error_t *error = 0;
3840 acl_main_t *am = &acl_main;
3843 u32 sw_if_index = ~0;
3844 int show_acl_hash_info = 0;
3845 int show_applied_info = 0;
3846 int show_mask_type = 0;
3847 int show_bihash = 0;
3848 u32 show_bihash_verbose = 0;
3850 if (unformat (input, "acl"))
3852 show_acl_hash_info = 1;
3853 /* mask-type is handy to see as well right there */
3855 unformat (input, "index %u", &acl_index);
3857 else if (unformat (input, "applied"))
3859 show_applied_info = 1;
3860 unformat (input, "sw_if_index %u", &sw_if_index);
3862 else if (unformat (input, "mask"))
3866 else if (unformat (input, "hash"))
3869 unformat (input, "verbose %u", &show_bihash_verbose);
3873 (show_mask_type || show_acl_hash_info || show_applied_info
3876 /* if no qualifiers specified, show all */
3878 show_acl_hash_info = 1;
3879 show_applied_info = 1;
3883 acl_plugin_show_tables_mask_type (am);
3884 if (show_acl_hash_info)
3885 acl_plugin_show_tables_acl_hash_info (am, acl_index);
3886 if (show_applied_info)
3887 acl_plugin_show_tables_applied_info (am, sw_if_index);
3889 acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3894 static clib_error_t *
3895 acl_clear_aclplugin_fn (vlib_main_t * vm,
3896 unformat_input_t * input, vlib_cli_command_t * cmd)
3898 clib_error_t *error = 0;
3899 acl_main_t *am = &acl_main;
3900 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3901 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3906 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3907 .path = "set acl-plugin",
3908 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3909 .function = acl_set_aclplugin_fn,
3912 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3913 .path = "show acl-plugin acl",
3914 .short_help = "show acl-plugin acl [index N]",
3915 .function = acl_show_aclplugin_acl_fn,
3918 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3919 .path = "show acl-plugin decode 5tuple",
3920 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3921 .function = acl_show_aclplugin_decode_5tuple_fn,
3924 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3925 .path = "show acl-plugin interface",
3926 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3927 .function = acl_show_aclplugin_interface_fn,
3930 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3931 .path = "show acl-plugin memory",
3932 .short_help = "show acl-plugin memory",
3933 .function = acl_show_aclplugin_memory_fn,
3936 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3937 .path = "show acl-plugin sessions",
3938 .short_help = "show acl-plugin sessions",
3939 .function = acl_show_aclplugin_sessions_fn,
3942 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3943 .path = "show acl-plugin tables",
3944 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3945 .function = acl_show_aclplugin_tables_fn,
3948 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3949 .path = "show acl-plugin macip acl",
3950 .short_help = "show acl-plugin macip acl",
3951 .function = acl_show_aclplugin_macip_acl_fn,
3954 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3955 .path = "show acl-plugin macip interface",
3956 .short_help = "show acl-plugin macip interface",
3957 .function = acl_show_aclplugin_macip_interface_fn,
3960 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3961 .path = "clear acl-plugin sessions",
3962 .short_help = "clear acl-plugin sessions",
3963 .function = acl_clear_aclplugin_fn,
3967 static clib_error_t *
3968 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3970 acl_main_t *am = &acl_main;
3971 u32 conn_table_hash_buckets;
3972 u32 conn_table_hash_memory_size;
3973 u32 conn_table_max_entries;
3976 u32 hash_lookup_hash_buckets;
3977 u32 hash_lookup_hash_memory;
3979 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3982 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3983 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3984 else if (unformat (input, "connection hash memory %d",
3985 &conn_table_hash_memory_size))
3986 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3987 else if (unformat (input, "connection count max %d",
3988 &conn_table_max_entries))
3989 am->fa_conn_table_max_entries = conn_table_max_entries;
3990 else if (unformat (input, "main heap size %d", &main_heap_size))
3991 am->acl_mheap_size = main_heap_size;
3992 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3993 am->hash_lookup_mheap_size = hash_heap_size;
3994 else if (unformat (input, "hash lookup hash buckets %d",
3995 &hash_lookup_hash_buckets))
3996 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3997 else if (unformat (input, "hash lookup hash memory %d",
3998 &hash_lookup_hash_memory))
3999 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4001 return clib_error_return (0, "unknown input '%U'",
4002 format_unformat_error, input);
4007 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4009 static clib_error_t *
4010 acl_init (vlib_main_t * vm)
4012 acl_main_t *am = &acl_main;
4013 clib_error_t *error = 0;
4014 memset (am, 0, sizeof (*am));
4016 am->vnet_main = vnet_get_main ();
4018 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4020 /* Ask for a correctly-sized block of API message decode slots */
4021 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4022 VL_MSG_FIRST_AVAILABLE);
4024 error = acl_plugin_api_hookup (vm);
4026 /* Add our API messages to the global name_crc hash table */
4027 setup_message_id_table (am, &api_main);
4031 acl_setup_fa_nodes ();
4033 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
4034 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4036 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4037 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4039 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4040 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4041 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4042 TCP_SESSION_IDLE_TIMEOUT_SEC;
4043 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4044 UDP_SESSION_IDLE_TIMEOUT_SEC;
4046 am->fa_conn_table_hash_num_buckets =
4047 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4048 am->fa_conn_table_hash_memory_size =
4049 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4050 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4051 vlib_thread_main_t *tm = vlib_get_thread_main ();
4052 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4056 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4058 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4059 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4060 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4061 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4063 pw->fa_conn_list_head[tt] = ~0;
4064 pw->fa_conn_list_tail[tt] = ~0;
4069 am->fa_min_deleted_sessions_per_interval =
4070 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4071 am->fa_max_deleted_sessions_per_interval =
4072 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4073 am->fa_cleaner_wait_time_increment =
4074 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4076 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4077 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4078 am->fa_cleaner_cnt_unknown_event = 0;
4079 am->fa_cleaner_cnt_timer_restarted = 0;
4080 am->fa_cleaner_cnt_wait_with_timeout = 0;
4083 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4086 am->l4_match_nonfirst_fragment = 1;
4088 /* use the new fancy hash-based matching */
4089 am->use_hash_acl_matching = 1;
4094 VLIB_INIT_FUNCTION (acl_init);
4098 * fd.io coding-style-patch-verification: ON
4101 * eval: (c-set-style "gnu")