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 "public_inlines.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]);
109 acl_plugin_acl_exists (u32 acl_index)
111 acl_main_t *am = &acl_main;
113 if (pool_is_free_index (am->acls, acl_index))
120 acl_set_heap (acl_main_t * am)
122 if (0 == am->acl_mheap)
124 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
125 mheap_t *h = mheap_header (am->acl_mheap);
126 h->flags |= MHEAP_FLAG_THREAD_SAFE;
128 void *oldheap = clib_mem_set_heap (am->acl_mheap);
133 acl_plugin_set_heap ()
135 acl_main_t *am = &acl_main;
136 return acl_set_heap (am);
140 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
142 clib_mem_set_heap (acl_set_heap (am));
143 mheap_t *h = mheap_header (am->acl_mheap);
146 h->flags |= MHEAP_FLAG_VALIDATE;
147 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
152 h->flags &= ~MHEAP_FLAG_VALIDATE;
153 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
158 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
160 clib_mem_set_heap (acl_set_heap (am));
161 mheap_t *h = mheap_header (am->acl_mheap);
164 h->flags |= MHEAP_FLAG_TRACE;
168 h->flags &= ~MHEAP_FLAG_TRACE;
173 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
175 acl_main_t *am = &acl_main;
176 vl_api_acl_plugin_get_version_reply_t *rmp;
177 int msg_size = sizeof (*rmp);
178 vl_api_registration_t *reg;
180 reg = vl_api_client_index_to_registration (mp->client_index);
184 rmp = vl_msg_api_alloc (msg_size);
185 memset (rmp, 0, msg_size);
187 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
188 rmp->context = mp->context;
189 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
190 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
192 vl_api_send_msg (reg, (u8 *) rmp);
196 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
199 vl_api_acl_plugin_control_ping_reply_t *rmp;
200 acl_main_t *am = &acl_main;
204 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
206 rmp->vpe_pid = ntohl (getpid ());
212 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
214 clib_warning ("%v", out0);
215 vec_reset_length (out0);
219 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
221 vlib_cli_output (vm, "%v", out0);
222 vec_reset_length (out0);
225 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
228 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
229 acl_main_t * am, int acl_index)
232 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
233 am->acls[acl_index].count, am->acls[acl_index].tag);
236 for (j = 0; j < am->acls[acl_index].count; j++)
238 r = &am->acls[acl_index].rules[j];
239 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
240 out0 = format_acl_action (out0, r->is_permit);
241 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
242 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
245 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
246 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
247 out0 = format (out0, " proto %d", r->proto);
248 out0 = format (out0, " sport %d", r->src_port_or_type_first);
249 if (r->src_port_or_type_first != r->src_port_or_type_last)
251 out0 = format (out0, "-%d", r->src_port_or_type_last);
253 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
254 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
256 out0 = format (out0, "-%d", r->dst_port_or_code_last);
258 if (r->tcp_flags_mask || r->tcp_flags_value)
261 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
264 out0 = format (out0, "\n");
270 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
272 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
276 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
278 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
283 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
284 u32 * acl_list_index, u8 * tag)
286 acl_main_t *am = &acl_main;
289 acl_rule_t *acl_new_rules = 0;
292 if (am->trace_acl > 255)
293 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
296 if (*acl_list_index != ~0)
298 /* They supplied some number, let's see if this ACL exists */
299 if (pool_is_free_index (am->acls, *acl_list_index))
301 /* tried to replace a non-existent ACL, no point doing anything */
303 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
304 *acl_list_index, tag);
305 return VNET_API_ERROR_NO_SUCH_ENTRY;
311 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
312 *acl_list_index, tag);
315 void *oldheap = acl_set_heap (am);
317 /* Create and populate the rules */
319 vec_validate (acl_new_rules, count - 1);
321 for (i = 0; i < count; i++)
323 r = vec_elt_at_index (acl_new_rules, i);
324 memset (r, 0, sizeof (*r));
325 r->is_permit = rules[i].is_permit;
326 r->is_ipv6 = rules[i].is_ipv6;
329 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
330 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
334 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
335 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
337 r->src_prefixlen = rules[i].src_ip_prefix_len;
338 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
339 r->proto = rules[i].proto;
340 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
341 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
342 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
343 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
344 r->tcp_flags_value = rules[i].tcp_flags_value;
345 r->tcp_flags_mask = rules[i].tcp_flags_mask;
348 if (~0 == *acl_list_index)
351 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
352 memset (a, 0, sizeof (*a));
353 /* Will return the newly allocated ACL index */
354 *acl_list_index = a - am->acls;
358 a = am->acls + *acl_list_index;
359 /* Get rid of the old rules */
363 a->rules = acl_new_rules;
365 memcpy (a->tag, tag, sizeof (a->tag));
366 if (am->trace_acl > 255)
367 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
368 /* notify the lookup contexts about the ACL changes */
369 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
370 clib_mem_set_heap (oldheap);
375 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
377 if (acl_index < vec_len (foo_index_vec_by_acl))
379 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
381 /* ACL is applied somewhere. */
389 acl_del_list (u32 acl_list_index)
391 acl_main_t *am = &acl_main;
393 if (pool_is_free_index (am->acls, acl_list_index))
395 return VNET_API_ERROR_NO_SUCH_ENTRY;
397 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
398 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
399 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
400 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
401 /* lookup contexts cover other cases, not just inbound/oubound, so check that */
402 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
403 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
405 void *oldheap = acl_set_heap (am);
407 /* now we can delete the ACL itself */
408 a = pool_elt_at_index (am->acls, acl_list_index);
411 pool_put (am->acls, a);
412 /* acl_list_index is now free, notify the lookup contexts */
413 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
414 clib_mem_set_heap (oldheap);
418 /* Some aids in ASCII graphing the content */
425 u8 ip4_5tuple_mask[] =
426 _(" dmac smac etype ")
427 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
434 _(" ttl pr checksum ")
443 _("L4 T/U sport dport ")
453 u8 ip6_5tuple_mask[] =
454 _(" dmac smac etype ")
455 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
457 _(0x0000) __ __ __ __
459 _(0x0004) __ __ XX __
461 _(0x0008) XX XX XX XX
462 _(0x000C) XX XX XX XX
463 _(0x0010) XX XX XX XX
464 _(0x0014) XX XX XX XX
466 _(0x0018) XX XX XX XX
467 _(0x001C) XX XX XX XX
468 _(0x0020) XX XX XX XX
469 _(0x0024) XX XX XX XX
470 _("L4T/U sport dport ")
471 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
473 u8 dot1q_5tuple_mask[] =
474 _(" dmac smac dot1q etype ")
475 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
476 _(padpad) __ __ __ __
477 _(padpad) __ __ __ __
478 _(padpad) __ __ __ __
481 u8 dot1ad_5tuple_mask[] =
482 _(" dmac smac dot1ad dot1q etype ")
483 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
484 _(padpad) __ __ __ __
485 _(padpad) __ __ __ __
488 u8 ethertype_mask[] =
489 _(" dmac smac etype ")
490 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
499 count_skip (u8 * p, u32 size)
501 u64 *p64 = (u64 *) p;
502 /* Be tolerant to null pointer */
506 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
510 return (p64 - (u64 *) p) / 2;
514 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
515 u32 mask_len, u32 next_table_index,
516 u32 miss_next_index, u32 * table_index,
520 u32 memory_size = 2 << 13;
521 u32 skip = count_skip (mask, mask_len);
522 u32 match = (mask_len / 16) - skip;
523 u8 *skip_mask_ptr = mask + 16 * skip;
524 u32 current_data_flag = 0;
525 int current_data_offset = 0;
529 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
530 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
531 memory_size, skip, match,
532 next_table_index, miss_next_index,
533 table_index, current_data_flag,
534 current_data_offset, is_add,
535 1 /* delete_chain */ );
536 clib_mem_set_heap (oldheap);
541 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
542 u32 mask_len, u32 next_table_index,
543 u32 miss_next_index, u32 * table_index,
547 u32 memory_size = 2 << 22;
548 u32 skip = count_skip (mask, mask_len);
549 u32 match = (mask_len / 16) - skip;
550 u8 *skip_mask_ptr = mask + 16 * skip;
551 u32 current_data_flag = 0;
552 int current_data_offset = 0;
557 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
558 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
559 memory_size, skip, match,
560 next_table_index, miss_next_index,
561 table_index, current_data_flag,
562 current_data_offset, is_add,
563 1 /* delete_chain */ );
564 clib_mem_set_heap (oldheap);
569 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
571 vnet_classify_main_t *cm = &vnet_classify_main;
572 u32 ip4_table_index = ~0;
573 u32 ip6_table_index = ~0;
574 u32 dot1q_table_index = ~0;
575 u32 dot1ad_table_index = ~0;
576 u32 etype_table_index = ~0;
577 void *oldheap = acl_set_heap (am);
579 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
581 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
583 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
585 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
587 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
590 /* switch to global heap while calling vnet_* functions */
591 clib_mem_set_heap (cm->vlib_main->heap_base);
592 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
594 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
597 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
598 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
599 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
600 sizeof (ip4_5tuple_mask) - 1, ~0,
601 am->l2_input_classify_next_acl_ip4,
602 &ip4_table_index, 0);
604 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
607 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
608 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
609 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
610 sizeof (ip6_5tuple_mask) - 1, ~0,
611 am->l2_input_classify_next_acl_ip6,
612 &ip6_table_index, 0);
614 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
617 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
618 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
619 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
620 sizeof (ip6_5tuple_mask) - 1, ~0,
621 ~0, &dot1q_table_index, 0);
623 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
626 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
627 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
628 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
629 sizeof (dot1ad_5tuple_mask) - 1, ~0,
630 ~0, &dot1ad_table_index, 0);
632 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
635 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
636 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
637 acl_classify_add_del_table_tiny (cm, ethertype_mask,
638 sizeof (ethertype_mask) - 1, ~0,
639 ~0, &etype_table_index, 0);
641 clib_mem_set_heap (oldheap);
646 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
648 vnet_classify_main_t *cm = &vnet_classify_main;
649 u32 ip4_table_index = ~0;
650 u32 ip6_table_index = ~0;
651 u32 dot1q_table_index = ~0;
652 u32 dot1ad_table_index = ~0;
653 u32 etype_table_index = ~0;
654 void *oldheap = acl_set_heap (am);
656 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
658 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
660 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
662 vec_validate_init_empty
663 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
664 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
667 /* switch to global heap while calling vnet_* functions */
668 clib_mem_set_heap (cm->vlib_main->heap_base);
670 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
672 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
675 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
676 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
677 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
678 sizeof (ip4_5tuple_mask) - 1, ~0,
679 am->l2_output_classify_next_acl_ip4,
680 &ip4_table_index, 0);
682 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
685 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
686 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
687 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
688 sizeof (ip6_5tuple_mask) - 1, ~0,
689 am->l2_output_classify_next_acl_ip6,
690 &ip6_table_index, 0);
692 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
695 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
696 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
697 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
698 sizeof (ip6_5tuple_mask) - 1, ~0,
699 ~0, &dot1q_table_index, 0);
701 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
704 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
705 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
706 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
707 sizeof (dot1ad_5tuple_mask) - 1, ~0,
708 ~0, &dot1ad_table_index, 0);
710 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
713 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
714 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
715 acl_classify_add_del_table_tiny (cm, ethertype_mask,
716 sizeof (ethertype_mask) - 1, ~0,
717 ~0, &etype_table_index, 0);
719 clib_mem_set_heap (oldheap);
724 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
725 u8 is_dot1ad, u8 is_ip6)
727 vnet_classify_main_t *cm = &vnet_classify_main;
737 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
743 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
745 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
746 idx = (is_dot1ad) ? 20 : 16;
749 /* 802.1ad ethertype */
752 /* 802.1q ethertype */
758 /* 802.1q ethertype */
763 /* add sessions to vlan tables per ethernet_type */
767 match[idx + 1] = 0xdd;
773 match[idx + 1] = 0x00;
776 vnet_classify_add_del_session (cm, table_index, match, next_acl,
777 session_idx, 0, 0, 0, 1);
778 /* reset the mask back to being a mask */
780 match[idx + 1] = 0xff;
791 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
794 ? am->input_etype_whitelist_by_sw_if_index
795 : am->output_etype_whitelist_by_sw_if_index;
796 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
800 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
801 u32 etype_table_index)
803 vnet_classify_main_t *cm = &vnet_classify_main;
805 ? am->input_etype_whitelist_by_sw_if_index
806 : am->output_etype_whitelist_by_sw_if_index;
807 u8 *match = ethertype_mask;
811 u16 *whitelist = vec_elt (v, sw_if_index);
812 u32 next = ~0; /* permit */
813 for (i = 0; i < vec_len (whitelist); i++)
816 match[12] = (whitelist[i] >> 8) & 0xff;
817 match[13] = whitelist[i] & 0xff;
819 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
820 whitelist[i], 0, 0, 0, 1);
823 /* restore the mask */
830 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
832 vnet_classify_main_t *cm = &vnet_classify_main;
833 u32 ip4_table_index = ~0;
834 u32 ip6_table_index = ~0;
835 u32 dot1q_table_index = ~0;
836 u32 dot1ad_table_index = ~0;
837 u32 etype_table_index = ~0;
840 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
842 /* in case there were previous tables attached */
843 acl_unhook_l2_input_classify (am, sw_if_index);
845 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
846 sizeof (ip4_5tuple_mask) - 1, ~0,
847 am->l2_input_classify_next_acl_ip4,
848 &ip4_table_index, 1);
853 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
854 sizeof (ip6_5tuple_mask) - 1, ~0,
855 am->l2_input_classify_next_acl_ip6,
856 &ip6_table_index, 1);
859 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
860 sizeof (ip4_5tuple_mask) - 1, ~0,
861 am->l2_input_classify_next_acl_ip4,
862 &ip4_table_index, 0);
866 if (intf_has_etype_whitelist (am, sw_if_index, 1))
868 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
869 &etype_table_index, 1);
870 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
874 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
875 sizeof (dot1ad_5tuple_mask) - 1,
876 etype_table_index, ~0,
877 &dot1ad_table_index, 1);
879 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
880 sizeof (dot1q_5tuple_mask) - 1,
881 dot1ad_table_index, ~0,
882 &dot1q_table_index, 1);
885 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
886 sizeof (dot1ad_5tuple_mask) - 1, ~0,
887 ~0, &dot1ad_table_index, 0);
888 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
889 sizeof (ip6_5tuple_mask) - 1, ~0,
890 am->l2_input_classify_next_acl_ip6,
891 &ip6_table_index, 0);
892 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
893 sizeof (ip4_5tuple_mask) - 1, ~0,
894 am->l2_input_classify_next_acl_ip4,
895 &ip4_table_index, 0);
900 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
901 ip6_table_index, dot1q_table_index);
905 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
906 sizeof (ip4_5tuple_mask) - 1, ~0,
907 am->l2_input_classify_next_acl_ip4,
908 &ip4_table_index, 0);
909 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
910 sizeof (ip6_5tuple_mask) - 1, ~0,
911 am->l2_input_classify_next_acl_ip6,
912 &ip6_table_index, 0);
913 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
914 sizeof (dot1q_5tuple_mask) - 1, ~0,
915 ~0, &dot1q_table_index, 0);
916 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
917 sizeof (dot1ad_5tuple_mask) - 1, ~0,
918 ~0, &dot1ad_table_index, 0);
922 /* add sessions to vlan tables per ethernet_type */
923 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
924 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
925 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
926 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
928 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
930 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
932 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
934 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
936 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
938 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
941 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
943 clib_mem_set_heap (prevheap);
948 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
950 vnet_classify_main_t *cm = &vnet_classify_main;
951 u32 ip4_table_index = ~0;
952 u32 ip6_table_index = ~0;
953 u32 dot1q_table_index = ~0;
954 u32 dot1ad_table_index = ~0;
955 u32 etype_table_index = ~0;
958 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
960 /* in case there were previous tables attached */
961 acl_unhook_l2_output_classify (am, sw_if_index);
963 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
964 sizeof (ip4_5tuple_mask) - 1, ~0,
965 am->l2_output_classify_next_acl_ip4,
966 &ip4_table_index, 1);
970 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
971 sizeof (ip6_5tuple_mask) - 1, ~0,
972 am->l2_output_classify_next_acl_ip6,
973 &ip6_table_index, 1);
976 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
977 sizeof (ip4_5tuple_mask) - 1, ~0,
978 am->l2_output_classify_next_acl_ip4,
979 &ip4_table_index, 0);
983 if (intf_has_etype_whitelist (am, sw_if_index, 0))
985 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
986 &etype_table_index, 1);
987 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
992 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
993 sizeof (dot1ad_5tuple_mask) - 1,
994 etype_table_index, ~0,
995 &dot1ad_table_index, 1);
997 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
998 sizeof (dot1q_5tuple_mask) - 1,
999 dot1ad_table_index, ~0,
1000 &dot1q_table_index, 1);
1003 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1004 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1005 ~0, &dot1ad_table_index, 0);
1006 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1007 sizeof (ip6_5tuple_mask) - 1, ~0,
1008 am->l2_output_classify_next_acl_ip6,
1009 &ip6_table_index, 0);
1010 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1011 sizeof (ip4_5tuple_mask) - 1, ~0,
1012 am->l2_output_classify_next_acl_ip4,
1013 &ip4_table_index, 0);
1018 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1019 ip6_table_index, dot1q_table_index);
1022 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
1023 sw_if_index, ip4_table_index, ip6_table_index);
1027 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1028 sizeof (ip6_5tuple_mask) - 1, ~0,
1029 am->l2_output_classify_next_acl_ip6,
1030 &ip6_table_index, 0);
1031 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1032 sizeof (ip4_5tuple_mask) - 1, ~0,
1033 am->l2_output_classify_next_acl_ip4,
1034 &ip4_table_index, 0);
1035 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1036 sizeof (dot1q_5tuple_mask) - 1, ~0,
1037 ~0, &dot1q_table_index, 0);
1038 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1039 sizeof (dot1ad_5tuple_mask) - 1, ~0,
1040 ~0, &dot1ad_table_index, 0);
1044 /* add sessions to vlan tables per ethernet_type */
1045 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
1046 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
1047 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
1048 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
1050 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
1052 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
1054 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
1056 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
1058 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
1061 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
1063 clib_mem_set_heap (prevheap);
1068 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
1070 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1071 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1072 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1074 clib_mem_set_heap (oldheap);
1079 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1084 /* Utterly wrong? */
1085 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1087 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1089 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
1093 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1097 rv = acl_hook_l2_input_classify (am, sw_if_index);
1101 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1104 am->in_acl_on_sw_if_index =
1105 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
1111 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1116 /* Utterly wrong? */
1117 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1119 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1121 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
1125 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1129 rv = acl_hook_l2_output_classify (am, sw_if_index);
1133 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1136 am->out_acl_on_sw_if_index =
1137 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
1143 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
1144 int is_input, int enable_disable)
1147 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
1149 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
1153 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1155 return (pool_is_free_index (am->acls, acl_list_index));
1159 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
1160 u8 is_input, u32 * vec_acl_list_index,
1161 int *may_clear_sessions)
1164 uword *seen_acl_bitmap = 0;
1165 uword *old_seen_acl_bitmap = 0;
1166 uword *change_acl_bitmap = 0;
1171 if (am->trace_acl > 255)
1173 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
1174 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
1176 vec_foreach (pacln, vec_acl_list_index)
1178 if (acl_is_not_defined (am, *pacln))
1180 /* ACL is not defined. Can not apply */
1181 clib_warning ("ERROR: ACL %d not defined", *pacln);
1182 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1185 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
1187 /* ACL being applied twice within the list. error. */
1188 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
1189 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1192 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
1196 u32 **pinout_lc_index_by_sw_if_index =
1198 input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
1200 u32 ***pinout_acl_vec_by_sw_if_index =
1202 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1204 u32 ***pinout_sw_if_index_vec_by_acl =
1206 input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
1208 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1210 clib_bitmap_validate (old_seen_acl_bitmap, 1);
1212 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
1214 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
1217 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
1219 if (am->trace_acl > 255)
1220 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
1221 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
1222 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
1225 clib_bitmap_foreach(acln, change_acl_bitmap, ({
1226 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
1227 /* ACL is being removed. */
1228 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
1229 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1230 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
1233 /* ACL is being added. */
1234 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
1235 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1240 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1241 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
1242 vec_dup (vec_acl_list_index);
1244 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
1245 if (may_clear_sessions && *may_clear_sessions
1246 && !clib_bitmap_is_zero (change_acl_bitmap))
1248 acl_clear_sessions (am, sw_if_index);
1249 *may_clear_sessions = 0;
1253 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
1255 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
1257 if (vec_len (vec_acl_list_index) > 0)
1259 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
1263 acl_plugin_get_lookup_context_index (am->interface_acl_user_id,
1264 sw_if_index, is_input);
1265 ASSERT (lc_index >= 0);
1266 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
1268 acl_plugin_set_acl_vec_for_context (lc_index, vec_acl_list_index);
1272 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
1274 acl_plugin_put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
1275 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
1279 /* ensure ACL processing is enabled/disabled as needed */
1280 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
1281 vec_len (vec_acl_list_index) > 0);
1284 clib_bitmap_free (change_acl_bitmap);
1285 clib_bitmap_free (seen_acl_bitmap);
1286 clib_bitmap_free (old_seen_acl_bitmap);
1291 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
1292 int *may_clear_sessions)
1294 acl_main_t *am = &acl_main;
1295 void *oldheap = acl_set_heap (am);
1296 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
1297 may_clear_sessions);
1298 clib_mem_set_heap (oldheap);
1302 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1306 acl_main_t *am = &acl_main;
1308 int may_clear_sessions = 1;
1310 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
1311 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1313 u32 ***pinout_acl_vec_by_sw_if_index =
1315 input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1317 void *oldheap = acl_set_heap (am);
1321 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1322 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1327 rv = error_already_applied;
1331 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1332 vec_add1 (acl_vec, acl_list_index);
1336 if (sw_if_index > vec_len (*pinout_acl_vec_by_sw_if_index))
1338 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1342 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1347 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1351 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1352 vec_del1 (acl_vec, index);
1355 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
1356 &may_clear_sessions);
1359 clib_mem_set_heap (oldheap);
1364 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1367 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1368 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1370 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1371 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1373 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1374 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1377 * if there are already inbound/outbound ACLs applied, toggle the
1378 * enable/disable - this will recreate the necessary tables.
1381 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1383 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1385 acl_interface_in_enable_disable (am, sw_if_index, 0);
1386 acl_interface_in_enable_disable (am, sw_if_index, 1);
1389 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1391 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1393 acl_interface_out_enable_disable (am, sw_if_index, 0);
1394 acl_interface_out_enable_disable (am, sw_if_index, 1);
1409 u32 arp_table_index;
1410 u32 dot1q_table_index;
1411 u32 dot1ad_table_index;
1412 u32 arp_dot1q_table_index;
1413 u32 arp_dot1ad_table_index;
1415 u32 out_table_index;
1416 u32 out_arp_table_index;
1417 u32 out_dot1q_table_index;
1418 u32 out_dot1ad_table_index;
1419 u32 out_arp_dot1q_table_index;
1420 u32 out_arp_dot1ad_table_index;
1421 } macip_match_type_t;
1424 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1430 for (i = 0; i < vec_len (mv); i++)
1432 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1433 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1443 /* Get metric used to sort match types.
1444 The more specific and the more often seen - the bigger the metric */
1446 match_type_metric (macip_match_type_t * m)
1448 unsigned int mac_bits_set = 0;
1449 unsigned int mac_byte;
1451 for (i = 0; i < 6; i++)
1453 mac_byte = m->mac_mask[i];
1454 for (; mac_byte; mac_byte >>= 1)
1455 mac_bits_set += mac_byte & 1;
1458 * Attempt to place the more specific and the more used rules on top.
1459 * There are obvious caveat corner cases to this, but they do not
1460 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1461 * going with a wildcard IPv4 with a specific MAC).
1463 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1467 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1469 /* Ascending sort based on the metric values */
1470 return match_type_metric (m1) - match_type_metric (m2);
1473 /* Get the offset of L3 source within ethernet packet */
1475 get_l3_src_offset (int is6)
1478 return (sizeof (ethernet_header_t) +
1479 offsetof (ip6_header_t, src_address));
1481 return (sizeof (ethernet_header_t) +
1482 offsetof (ip4_header_t, src_address));
1486 get_l3_dst_offset (int is6)
1489 return (sizeof (ethernet_header_t) +
1490 offsetof (ip6_header_t, dst_address));
1492 return (sizeof (ethernet_header_t) +
1493 offsetof (ip4_header_t, dst_address));
1497 * return if the is_permit value also requires to create the egress tables
1498 * For backwards compatibility, we keep the is_permit = 1 to only
1499 * create the ingress tables, and the new value of 3 will also
1500 * create the egress tables based on destination.
1503 macip_permit_also_egress (u8 is_permit)
1505 return (is_permit == 3);
1509 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1511 macip_match_type_t *mvec = NULL;
1512 macip_match_type_t *mt;
1513 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1515 u32 match_type_index;
1519 vnet_classify_main_t *cm = &vnet_classify_main;
1521 /* Count the number of different types of rules */
1522 for (i = 0; i < a->count; i++)
1526 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1527 a->rules[i].src_prefixlen,
1528 a->rules[i].is_ipv6)))
1530 match_type_index = vec_len (mvec);
1531 vec_validate (mvec, match_type_index);
1532 memcpy (mvec[match_type_index].mac_mask,
1533 a->rules[i].src_mac_mask, 6);
1534 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1535 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1536 mvec[match_type_index].has_egress = 0;
1537 mvec[match_type_index].table_index = ~0;
1538 mvec[match_type_index].arp_table_index = ~0;
1539 mvec[match_type_index].dot1q_table_index = ~0;
1540 mvec[match_type_index].dot1ad_table_index = ~0;
1541 mvec[match_type_index].arp_dot1q_table_index = ~0;
1542 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1543 mvec[match_type_index].out_table_index = ~0;
1544 mvec[match_type_index].out_arp_table_index = ~0;
1545 mvec[match_type_index].out_dot1q_table_index = ~0;
1546 mvec[match_type_index].out_dot1ad_table_index = ~0;
1547 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1548 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1550 mvec[match_type_index].count++;
1551 mvec[match_type_index].has_egress |=
1552 macip_permit_also_egress (a->rules[i].is_permit);
1554 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1555 vec_sort_with_function (mvec, match_type_compare);
1556 /* Create the classifier tables */
1558 out_last_table = ~0;
1559 /* First add ARP tables */
1560 vec_foreach (mt, mvec)
1563 int is6 = mt->is_ipv6;
1565 u32 *last_tag_table;
1566 u32 *out_last_tag_table;
1573 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
1574 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1575 | Destination Address |
1576 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1578 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1580 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1581 | EtherType | Hardware Type |
1582 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1583 | Protocol Type | Hw addr len | Proto addr len|
1584 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1586 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1587 | Sender Hardware Address |
1588 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1589 | Sender Protocol Address |
1590 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1591 | Target Hardware Address |
1592 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1593 | | TargetProtocolAddress |
1594 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1596 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1598 for (tags = 2; tags >= 0; tags--)
1600 memset (mask, 0, sizeof (mask));
1601 /* source MAC address */
1602 memcpy (&mask[6], mt->mac_mask, 6);
1608 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1610 last_tag_table = &mt->arp_table_index;
1613 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1614 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1616 last_tag_table = &mt->arp_dot1q_table_index;
1619 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1620 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1621 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1623 last_tag_table = &mt->arp_dot1ad_table_index;
1627 /* sender hardware address within ARP */
1628 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1629 /* sender protocol address within ARP */
1630 for (i = 0; i < (mt->prefix_len / 8); i++)
1631 mask[l3_offset + 14 + i] = 0xff;
1632 if (mt->prefix_len % 8)
1633 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1634 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1636 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1638 1)) / sizeof (u32x4)) * sizeof (u32x4);
1639 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1640 (~0 == last_table) ? 0 : ~0,
1642 last_table = *last_tag_table;
1645 /* egress ARP table */
1646 memset (mask, 0, sizeof (mask));
1652 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1654 out_last_tag_table = &mt->out_arp_table_index;
1657 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1658 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1660 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1663 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1664 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1665 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1667 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1671 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1672 /* mask captures just the destination and the ethertype */
1673 mask_len = ((l3_offset +
1675 1)) / sizeof (u32x4)) * sizeof (u32x4);
1676 acl_classify_add_del_table_small (cm, mask, mask_len,
1679 out_last_table) ? 0 : ~0,
1680 out_last_tag_table, 1);
1681 out_last_table = *out_last_tag_table;
1686 /* Now add IP[46] tables */
1687 vec_foreach (mt, mvec)
1690 int is6 = mt->is_ipv6;
1694 u32 *last_tag_table;
1695 u32 *out_last_tag_table;
1698 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1700 for (tags = 2; tags >= 0; tags--)
1702 memset (mask, 0, sizeof (mask));
1703 memcpy (&mask[6], mt->mac_mask, 6);
1704 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1709 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1710 last_tag_table = &mt->table_index;
1713 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1714 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1715 last_tag_table = &mt->dot1q_table_index;
1718 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1719 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1720 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1721 last_tag_table = &mt->dot1ad_table_index;
1724 for (i = 0; i < (mt->prefix_len / 8); i++)
1726 mask[l3_src_offs + i] = 0xff;
1728 if (mt->prefix_len % 8)
1730 mask[l3_src_offs + (mt->prefix_len / 8)] =
1731 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1734 * Round-up the number of bytes needed to store the prefix,
1735 * and round up the number of vectors too
1737 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1738 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1739 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1740 (~0 == last_table) ? 0 : ~0,
1742 last_table = *last_tag_table;
1746 for (tags = 2; tags >= 0; tags--)
1748 memset (mask, 0, sizeof (mask));
1749 /* MAC destination */
1750 memcpy (&mask[0], mt->mac_mask, 6);
1751 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1756 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1757 out_last_tag_table = &mt->out_table_index;
1760 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1761 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1762 out_last_tag_table = &mt->out_dot1q_table_index;
1765 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1766 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1767 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1768 out_last_tag_table = &mt->out_dot1ad_table_index;
1771 for (i = 0; i < (mt->prefix_len / 8); i++)
1773 mask[l3_dst_offs + i] = 0xff;
1775 if (mt->prefix_len % 8)
1777 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1778 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1781 * Round-up the number of bytes needed to store the prefix,
1782 * and round up the number of vectors too
1784 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1786 1)) / sizeof (u32x4)) * sizeof (u32x4);
1787 acl_classify_add_del_table_small (cm, mask, mask_len,
1789 (~0 == out_last_table) ? 0 : ~0,
1790 out_last_tag_table, 1);
1791 out_last_table = *out_last_tag_table;
1795 a->ip4_table_index = last_table;
1796 a->ip6_table_index = last_table;
1797 a->l2_table_index = last_table;
1799 a->out_ip4_table_index = out_last_table;
1800 a->out_ip6_table_index = out_last_table;
1801 a->out_l2_table_index = out_last_table;
1803 /* Populate the classifier tables with rules from the MACIP ACL */
1804 for (i = 0; i < a->count; i++)
1808 int is6 = a->rules[i].is_ipv6;
1815 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1816 a->rules[i].src_prefixlen,
1817 a->rules[i].is_ipv6);
1818 ASSERT (match_type_index != ~0);
1820 for (tags = 2; tags >= 0; tags--)
1822 memset (mask, 0, sizeof (mask));
1823 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1824 memcpy (&mask[6], a->rules[i].src_mac, 6);
1829 tag_table = mvec[match_type_index].table_index;
1833 tag_table = mvec[match_type_index].dot1q_table_index;
1839 tag_table = mvec[match_type_index].dot1ad_table_index;
1849 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1851 mask[eth + 1] = 0xdd;
1855 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1857 mask[eth + 1] = 0x00;
1860 /* add session to table mvec[match_type_index].table_index; */
1861 vnet_classify_add_del_session (cm, tag_table,
1862 mask, a->rules[i].is_permit ? ~0 : 0,
1863 i, 0, action, metadata, 1);
1864 memset (&mask[12], 0, sizeof (mask) - 12);
1867 /* add ARP table entry too */
1868 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1870 memset (mask, 0, sizeof (mask));
1871 memcpy (&mask[6], a->rules[i].src_mac, 6);
1873 for (tags = 2; tags >= 0; tags--)
1879 tag_table = mvec[match_type_index].arp_table_index;
1885 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1893 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1904 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1905 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1907 vnet_classify_add_del_session (cm, tag_table, mask,
1908 a->rules[i].is_permit ? ~0 : 0,
1909 i, 0, action, metadata, 1);
1912 if (macip_permit_also_egress (a->rules[i].is_permit))
1914 /* Add the egress entry with destination set */
1915 for (tags = 2; tags >= 0; tags--)
1917 memset (mask, 0, sizeof (mask));
1918 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1919 /* src mac in the other direction becomes dst */
1920 memcpy (&mask[0], a->rules[i].src_mac, 6);
1925 tag_table = mvec[match_type_index].out_table_index;
1929 tag_table = mvec[match_type_index].out_dot1q_table_index;
1935 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1945 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1948 mask[eth + 1] = 0xdd;
1952 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1955 mask[eth + 1] = 0x00;
1958 /* add session to table mvec[match_type_index].table_index; */
1959 vnet_classify_add_del_session (cm, tag_table,
1961 a->rules[i].is_permit ? ~0 : 0,
1962 i, 0, action, metadata, 1);
1963 // memset (&mask[12], 0, sizeof (mask) - 12);
1966 /* add ARP table entry too */
1967 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1969 for (tags = 2; tags >= 0; tags--)
1971 memset (mask, 0, sizeof (mask));
1976 tag_table = mvec[match_type_index].out_arp_table_index;
1982 mvec[match_type_index].out_arp_dot1q_table_index;
1990 mvec[match_type_index].out_arp_dot1ad_table_index;
2000 vnet_classify_add_del_session (cm, tag_table,
2003 rules[i].is_permit ? ~0 : 0,
2004 i, 0, action, metadata, 1);
2013 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
2015 vnet_classify_main_t *cm = &vnet_classify_main;
2016 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2018 if (a->ip4_table_index != ~0)
2020 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2021 &a->ip4_table_index, 0);
2022 a->ip4_table_index = ~0;
2024 if (a->ip6_table_index != ~0)
2026 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2027 &a->ip6_table_index, 0);
2028 a->ip6_table_index = ~0;
2030 if (a->l2_table_index != ~0)
2032 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
2034 a->l2_table_index = ~0;
2036 if (a->out_ip4_table_index != ~0)
2038 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2039 &a->out_ip4_table_index, 0);
2040 a->out_ip4_table_index = ~0;
2042 if (a->out_ip6_table_index != ~0)
2044 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2045 &a->out_ip6_table_index, 0);
2046 a->out_ip6_table_index = ~0;
2048 if (a->out_l2_table_index != ~0)
2050 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2051 &a->out_l2_table_index, 0);
2052 a->out_l2_table_index = ~0;
2057 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2063 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2065 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2066 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2068 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2069 a->ip6_table_index, a->l2_table_index,
2071 /* return the first unhappy outcome but make try to plough through. */
2074 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2075 a->out_ip6_table_index,
2076 a->out_l2_table_index, is_apply);
2077 /* return the first unhappy outcome but make try to plough through. */
2084 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2085 u32 * acl_list_index, u8 * tag)
2087 acl_main_t *am = &acl_main;
2088 macip_acl_list_t *a;
2089 macip_acl_rule_t *r;
2090 macip_acl_rule_t *acl_new_rules = 0;
2094 if (*acl_list_index != ~0)
2096 /* They supplied some number, let's see if this MACIP ACL exists */
2097 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2099 /* tried to replace a non-existent ACL, no point doing anything */
2101 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2102 *acl_list_index, tag);
2103 return VNET_API_ERROR_NO_SUCH_ENTRY;
2110 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2113 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2114 if (~0 != *acl_list_index)
2115 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2116 void *oldheap = acl_set_heap (am);
2117 /* Create and populate the rules */
2119 vec_validate (acl_new_rules, count - 1);
2121 for (i = 0; i < count; i++)
2123 r = &acl_new_rules[i];
2124 r->is_permit = rules[i].is_permit;
2125 r->is_ipv6 = rules[i].is_ipv6;
2126 memcpy (&r->src_mac, rules[i].src_mac, 6);
2127 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2128 if (rules[i].is_ipv6)
2129 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2131 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2132 r->src_prefixlen = rules[i].src_ip_prefix_len;
2135 if (~0 == *acl_list_index)
2138 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2139 memset (a, 0, sizeof (*a));
2140 /* Will return the newly allocated ACL index */
2141 *acl_list_index = a - am->macip_acls;
2145 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2148 vec_free (a->rules);
2150 macip_destroy_classify_tables (am, *acl_list_index);
2153 a->rules = acl_new_rules;
2155 memcpy (a->tag, tag, sizeof (a->tag));
2157 /* Create and populate the classifer tables */
2158 macip_create_classify_tables (am, *acl_list_index);
2159 clib_mem_set_heap (oldheap);
2160 /* If the ACL was already applied somewhere, reapply the newly created tables */
2162 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2166 /* No check that sw_if_index denotes a valid interface - the callers
2167 * were supposed to validate.
2169 * That said, if sw_if_index corresponds to an interface that exists at all,
2170 * this function must return errors accordingly if the ACL is not applied.
2174 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2177 u32 macip_acl_index;
2178 macip_acl_list_t *a;
2180 /* The vector is too short - MACIP ACL is not applied */
2181 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
2182 return VNET_API_ERROR_NO_SUCH_ENTRY;
2184 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2185 /* No point in deleting MACIP ACL which is not applied */
2186 if (~0 == macip_acl_index)
2187 return VNET_API_ERROR_NO_SUCH_ENTRY;
2189 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2190 /* remove the classifier tables off the interface L2 ACL */
2192 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2193 a->ip6_table_index, a->l2_table_index, 0);
2195 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2196 a->out_ip4_table_index, a->out_ip6_table_index,
2197 a->out_l2_table_index, 0);
2198 /* Unset the MACIP ACL index */
2199 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2200 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
2201 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
2204 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
2208 /* No check for validity of sw_if_index - the callers were supposed to validate */
2211 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2212 u32 macip_acl_index)
2214 macip_acl_list_t *a;
2216 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2218 return VNET_API_ERROR_NO_SUCH_ENTRY;
2220 void *oldheap = acl_set_heap (am);
2221 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2222 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2223 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
2224 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
2225 clib_mem_set_heap (oldheap);
2226 /* If there already a MACIP ACL applied, unapply it */
2227 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2228 macip_acl_interface_del_acl (am, sw_if_index);
2229 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2231 /* Apply the classifier tables for L2 ACLs */
2233 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2234 a->ip6_table_index, a->l2_table_index, 1);
2236 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2237 a->out_ip4_table_index, a->out_ip6_table_index,
2238 a->out_l2_table_index, 1);
2243 macip_acl_del_list (u32 acl_list_index)
2245 acl_main_t *am = &acl_main;
2246 macip_acl_list_t *a;
2248 if (pool_is_free_index (am->macip_acls, acl_list_index))
2250 return VNET_API_ERROR_NO_SUCH_ENTRY;
2253 /* delete any references to the ACL */
2254 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2256 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2258 macip_acl_interface_del_acl (am, i);
2262 void *oldheap = acl_set_heap (am);
2263 /* Now that classifier tables are detached, clean them up */
2264 macip_destroy_classify_tables (am, acl_list_index);
2266 /* now we can delete the ACL itself */
2267 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2270 vec_free (a->rules);
2272 pool_put (am->macip_acls, a);
2273 clib_mem_set_heap (oldheap);
2279 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2282 acl_main_t *am = &acl_main;
2286 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2290 rv = macip_acl_interface_del_acl (am, sw_if_index);
2296 * If the client does not allocate enough memory for a variable-length
2297 * message, and then proceed to use it as if the full memory allocated,
2298 * absent the check we happily consume that on the VPP side, and go
2299 * along as if nothing happened. However, the resulting
2300 * effects range from just garbage in the API decode
2301 * (because the decoder snoops too far), to potential memory
2304 * This verifies that the actual length of the message is
2305 * at least expected_len, and complains loudly if it is not.
2307 * A failing check here is 100% a software bug on the API user side,
2308 * so we might as well yell.
2312 verify_message_len (void *mp, u32 expected_len, char *where)
2314 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2315 if (supplied_len < expected_len)
2317 clib_warning ("%s: Supplied message length %d is less than expected %d",
2318 where, supplied_len, expected_len);
2327 /* API message handler */
2329 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2331 vl_api_acl_add_replace_reply_t *rmp;
2332 acl_main_t *am = &acl_main;
2334 u32 acl_list_index = ntohl (mp->acl_index);
2335 u32 acl_count = ntohl (mp->count);
2336 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2338 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2340 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2344 rv = VNET_API_ERROR_INVALID_VALUE;
2348 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2350 rmp->acl_index = htonl(acl_list_index);
2356 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2358 acl_main_t *am = &acl_main;
2359 vl_api_acl_del_reply_t *rmp;
2362 rv = acl_del_list (ntohl (mp->acl_index));
2364 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2368 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2370 acl_main_t *am = &acl_main;
2371 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2372 u32 sw_if_index = ntohl (mp->sw_if_index);
2373 vl_api_acl_interface_add_del_reply_t *rmp;
2376 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2377 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2380 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2381 mp->is_input, ntohl (mp->acl_index));
2383 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2387 vl_api_acl_interface_set_acl_list_t_handler
2388 (vl_api_acl_interface_set_acl_list_t * mp)
2390 acl_main_t *am = &acl_main;
2391 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2394 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2395 u32 sw_if_index = ntohl (mp->sw_if_index);
2397 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2398 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2401 int may_clear_sessions = 1;
2402 for (i = 0; i < mp->count; i++)
2404 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2406 /* ACL does not exist, so we can not apply it */
2407 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2412 void *oldheap = acl_set_heap (am);
2414 u32 *in_acl_vec = 0;
2415 u32 *out_acl_vec = 0;
2416 for (i = 0; i < mp->count; i++)
2417 if (i < mp->n_input)
2418 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2420 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2423 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2424 &may_clear_sessions);
2426 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2428 &may_clear_sessions);
2429 vec_free (in_acl_vec);
2430 vec_free (out_acl_vec);
2431 clib_mem_set_heap (oldheap);
2435 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2439 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2441 api_rule->is_permit = r->is_permit;
2442 api_rule->is_ipv6 = r->is_ipv6;
2445 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2446 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2450 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2451 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2453 api_rule->src_ip_prefix_len = r->src_prefixlen;
2454 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2455 api_rule->proto = r->proto;
2456 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2457 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2458 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2459 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2460 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2461 api_rule->tcp_flags_value = r->tcp_flags_value;
2465 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2466 acl_list_t * acl, u32 context)
2468 vl_api_acl_details_t *mp;
2469 vl_api_acl_rule_t *rules;
2471 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2472 void *oldheap = acl_set_heap (am);
2474 mp = vl_msg_api_alloc (msg_size);
2475 memset (mp, 0, msg_size);
2476 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2478 /* fill in the message */
2479 mp->context = context;
2480 mp->count = htonl (acl->count);
2481 mp->acl_index = htonl (acl - am->acls);
2482 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2483 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2485 for (i = 0; i < acl->count; i++)
2487 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2490 clib_mem_set_heap (oldheap);
2491 vl_api_send_msg (reg, (u8 *) mp);
2496 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2498 acl_main_t *am = &acl_main;
2502 vl_api_registration_t *reg;
2504 reg = vl_api_client_index_to_registration (mp->client_index);
2508 if (mp->acl_index == ~0)
2511 /* Just dump all ACLs */
2512 pool_foreach (acl, am->acls,
2514 send_acl_details(am, reg, acl, mp->context);
2520 acl_index = ntohl (mp->acl_index);
2521 if (!pool_is_free_index (am->acls, acl_index))
2523 acl = pool_elt_at_index (am->acls, acl_index);
2524 send_acl_details (am, reg, acl, mp->context);
2530 /* FIXME API: should we signal an error here at all ? */
2536 send_acl_interface_list_details (acl_main_t * am,
2537 vl_api_registration_t * reg,
2538 u32 sw_if_index, u32 context)
2540 vl_api_acl_interface_list_details_t *mp;
2546 void *oldheap = acl_set_heap (am);
2548 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2549 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2551 clib_mem_set_heap (oldheap);
2553 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2554 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2555 count = n_input + n_output;
2557 msg_size = sizeof (*mp);
2558 msg_size += sizeof (mp->acls[0]) * count;
2560 mp = vl_msg_api_alloc (msg_size);
2561 memset (mp, 0, msg_size);
2563 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2565 /* fill in the message */
2566 mp->context = context;
2567 mp->sw_if_index = htonl (sw_if_index);
2569 mp->n_input = n_input;
2570 for (i = 0; i < n_input; i++)
2572 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2574 for (i = 0; i < n_output; i++)
2576 mp->acls[n_input + i] =
2577 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2579 vl_api_send_msg (reg, (u8 *) mp);
2583 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2586 acl_main_t *am = &acl_main;
2587 vnet_sw_interface_t *swif;
2588 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2591 vl_api_registration_t *reg;
2593 reg = vl_api_client_index_to_registration (mp->client_index);
2597 if (mp->sw_if_index == ~0)
2600 pool_foreach (swif, im->sw_interfaces,
2602 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2608 sw_if_index = ntohl (mp->sw_if_index);
2609 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2610 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2614 /* MACIP ACL API handlers */
2617 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2619 vl_api_macip_acl_add_reply_t *rmp;
2620 acl_main_t *am = &acl_main;
2622 u32 acl_list_index = ~0;
2623 u32 acl_count = ntohl (mp->count);
2624 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2626 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2628 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2632 rv = VNET_API_ERROR_INVALID_VALUE;
2636 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2638 rmp->acl_index = htonl(acl_list_index);
2644 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2646 vl_api_macip_acl_add_replace_reply_t *rmp;
2647 acl_main_t *am = &acl_main;
2649 u32 acl_list_index = ntohl (mp->acl_index);
2650 u32 acl_count = ntohl (mp->count);
2651 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2653 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2655 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2659 rv = VNET_API_ERROR_INVALID_VALUE;
2663 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2665 rmp->acl_index = htonl(acl_list_index);
2671 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2673 acl_main_t *am = &acl_main;
2674 vl_api_macip_acl_del_reply_t *rmp;
2677 rv = macip_acl_del_list (ntohl (mp->acl_index));
2679 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2683 vl_api_macip_acl_interface_add_del_t_handler
2684 (vl_api_macip_acl_interface_add_del_t * mp)
2686 acl_main_t *am = &acl_main;
2687 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2689 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2690 u32 sw_if_index = ntohl (mp->sw_if_index);
2692 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2693 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2696 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2697 ntohl (mp->acl_index));
2699 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2703 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2704 macip_acl_list_t * acl, u32 context)
2706 vl_api_macip_acl_details_t *mp;
2707 vl_api_macip_acl_rule_t *rules;
2708 macip_acl_rule_t *r;
2710 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2712 mp = vl_msg_api_alloc (msg_size);
2713 memset (mp, 0, msg_size);
2714 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2716 /* fill in the message */
2717 mp->context = context;
2720 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2721 mp->count = htonl (acl->count);
2722 mp->acl_index = htonl (acl - am->macip_acls);
2724 for (i = 0; i < acl->count; i++)
2727 rules[i].is_permit = r->is_permit;
2728 rules[i].is_ipv6 = r->is_ipv6;
2729 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2730 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2731 sizeof (r->src_mac_mask));
2733 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2734 sizeof (r->src_ip_addr.ip6));
2736 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2737 sizeof (r->src_ip_addr.ip4));
2738 rules[i].src_ip_prefix_len = r->src_prefixlen;
2743 /* No martini, no party - no ACL applied to this interface. */
2748 vl_api_send_msg (reg, (u8 *) mp);
2753 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2755 acl_main_t *am = &acl_main;
2756 macip_acl_list_t *acl;
2758 vl_api_registration_t *reg;
2760 reg = vl_api_client_index_to_registration (mp->client_index);
2764 if (mp->acl_index == ~0)
2766 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2767 pool_foreach (acl, am->macip_acls, (
2769 send_macip_acl_details (am, reg,
2778 u32 acl_index = ntohl (mp->acl_index);
2779 if (!pool_is_free_index (am->macip_acls, acl_index))
2781 acl = pool_elt_at_index (am->macip_acls, acl_index);
2782 send_macip_acl_details (am, reg, acl, mp->context);
2788 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2791 acl_main_t *am = &acl_main;
2792 vl_api_macip_acl_interface_get_reply_t *rmp;
2793 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2794 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2795 vl_api_registration_t *reg;
2798 reg = vl_api_client_index_to_registration (mp->client_index);
2802 rmp = vl_msg_api_alloc (msg_size);
2803 memset (rmp, 0, msg_size);
2805 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2806 rmp->context = mp->context;
2807 rmp->count = htonl (count);
2808 for (i = 0; i < count; i++)
2810 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2813 vl_api_send_msg (reg, (u8 *) rmp);
2817 send_macip_acl_interface_list_details (acl_main_t * am,
2818 vl_api_registration_t * reg,
2820 u32 acl_index, u32 context)
2822 vl_api_macip_acl_interface_list_details_t *rmp;
2823 /* at this time there is only ever 1 mac ip acl per interface */
2824 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2826 rmp = vl_msg_api_alloc (msg_size);
2827 memset (rmp, 0, msg_size);
2829 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2831 /* fill in the message */
2832 rmp->context = context;
2834 rmp->sw_if_index = htonl (sw_if_index);
2835 rmp->acls[0] = htonl (acl_index);
2837 vl_api_send_msg (reg, (u8 *) rmp);
2841 vl_api_macip_acl_interface_list_dump_t_handler
2842 (vl_api_macip_acl_interface_list_dump_t * mp)
2844 vl_api_registration_t *reg;
2845 acl_main_t *am = &acl_main;
2846 u32 sw_if_index = ntohl (mp->sw_if_index);
2848 reg = vl_api_client_index_to_registration (mp->client_index);
2852 if (sw_if_index == ~0)
2854 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2856 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2858 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2859 am->macip_acl_by_sw_if_index
2867 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2869 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2870 am->macip_acl_by_sw_if_index
2871 [sw_if_index], mp->context);
2877 vl_api_acl_interface_set_etype_whitelist_t_handler
2878 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2880 acl_main_t *am = &acl_main;
2881 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2884 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2885 u32 sw_if_index = ntohl (mp->sw_if_index);
2886 u16 *vec_in = 0, *vec_out = 0;
2887 void *oldheap = acl_set_heap (am);
2889 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2890 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2893 for (i = 0; i < mp->count; i++)
2895 if (i < mp->n_input)
2896 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2898 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2900 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2903 clib_mem_set_heap (oldheap);
2904 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2908 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2909 vl_api_registration_t * reg,
2910 u32 sw_if_index, u32 context)
2912 vl_api_acl_interface_etype_whitelist_details_t *mp;
2919 u16 *whitelist_in = 0;
2920 u16 *whitelist_out = 0;
2922 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2924 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2926 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2928 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2930 if ((0 == whitelist_in) && (0 == whitelist_out))
2931 return; /* nothing to do */
2933 void *oldheap = acl_set_heap (am);
2935 n_input = vec_len (whitelist_in);
2936 n_output = vec_len (whitelist_out);
2937 count = n_input + n_output;
2939 msg_size = sizeof (*mp);
2940 msg_size += sizeof (mp->whitelist[0]) * count;
2942 mp = vl_msg_api_alloc (msg_size);
2943 memset (mp, 0, msg_size);
2945 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2947 /* fill in the message */
2948 mp->context = context;
2949 mp->sw_if_index = htonl (sw_if_index);
2951 mp->n_input = n_input;
2952 for (i = 0; i < n_input; i++)
2954 mp->whitelist[i] = htons (whitelist_in[i]);
2956 for (i = 0; i < n_output; i++)
2958 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2960 clib_mem_set_heap (oldheap);
2961 vl_api_send_msg (reg, (u8 *) mp);
2966 vl_api_acl_interface_etype_whitelist_dump_t_handler
2967 (vl_api_acl_interface_list_dump_t * mp)
2969 acl_main_t *am = &acl_main;
2970 vnet_sw_interface_t *swif;
2971 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2974 vl_api_registration_t *reg;
2976 reg = vl_api_client_index_to_registration (mp->client_index);
2980 if (mp->sw_if_index == ~0)
2983 pool_foreach (swif, im->sw_interfaces,
2985 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2991 sw_if_index = ntohl (mp->sw_if_index);
2992 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2993 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
3000 /* Set up the API message handling tables */
3001 static clib_error_t *
3002 acl_plugin_api_hookup (vlib_main_t * vm)
3004 acl_main_t *am = &acl_main;
3006 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
3008 vl_api_##n##_t_handler, \
3010 vl_api_##n##_t_endian, \
3011 vl_api_##n##_t_print, \
3012 sizeof(vl_api_##n##_t), 1);
3013 foreach_acl_plugin_api_msg;
3019 #define vl_msg_name_crc_list
3020 #include <acl/acl_all_api_h.h>
3021 #undef vl_msg_name_crc_list
3024 setup_message_id_table (acl_main_t * am, api_main_t * apim)
3026 #define _(id,n,crc) \
3027 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
3028 foreach_vl_msg_name_crc_acl;
3033 acl_setup_fa_nodes (void)
3035 vlib_main_t *vm = vlib_get_main ();
3036 acl_main_t *am = &acl_main;
3037 vlib_node_t *n, *n4, *n6;
3039 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
3040 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
3041 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
3044 am->l2_input_classify_next_acl_ip4 =
3045 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3046 am->l2_input_classify_next_acl_ip6 =
3047 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3049 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
3050 l2input_get_feat_names (),
3051 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
3053 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
3054 l2input_get_feat_names (),
3055 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
3058 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
3059 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
3060 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
3062 am->l2_output_classify_next_acl_ip4 =
3063 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3064 am->l2_output_classify_next_acl_ip6 =
3065 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3067 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
3068 l2output_get_feat_names (),
3069 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
3071 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
3072 l2output_get_feat_names (),
3073 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
3077 acl_set_timeout_sec (int timeout_type, u32 value)
3079 acl_main_t *am = &acl_main;
3080 clib_time_t *ct = &am->vlib_main->clib_time;
3082 if (timeout_type < ACL_N_TIMEOUTS)
3084 am->session_timeout_sec[timeout_type] = value;
3088 clib_warning ("Unknown timeout type %d", timeout_type);
3091 am->session_timeout[timeout_type] =
3092 (u64) (((f64) value) / ct->seconds_per_clock);
3096 acl_set_session_max_entries (u32 value)
3098 acl_main_t *am = &acl_main;
3099 am->fa_conn_table_max_entries = value;
3103 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3105 acl_main_t *am = &acl_main;
3107 if ((eh < 256) && (value < 2))
3109 am->fa_ipv6_known_eh_bitmap =
3110 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3118 static clib_error_t *
3119 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3121 acl_main_t *am = &acl_main;
3122 if (0 == am->acl_mheap)
3124 /* ACL heap is not initialized, so definitely nothing to do. */
3129 int may_clear_sessions = 1;
3130 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3131 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3133 /* also unapply any ACLs in case the users did not do so. */
3134 macip_acl_interface_del_acl (am, sw_if_index);
3135 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
3136 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
3141 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3145 static clib_error_t *
3146 acl_set_aclplugin_fn (vlib_main_t * vm,
3147 unformat_input_t * input, vlib_cli_command_t * cmd)
3149 clib_error_t *error = 0;
3153 uword memory_size = 0;
3154 acl_main_t *am = &acl_main;
3156 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3158 if (!acl_set_skip_ipv6_eh (eh_val, val))
3160 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3164 if (unformat (input, "use-hash-acl-matching %u", &val))
3166 am->use_hash_acl_matching = (val != 0);
3169 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3171 am->l4_match_nonfirst_fragment = (val != 0);
3174 if (unformat (input, "event-trace"))
3176 if (!unformat (input, "%u", &val))
3178 error = clib_error_return (0,
3179 "expecting trace level, got `%U`",
3180 format_unformat_error, input);
3185 am->trace_acl = val;
3189 if (unformat (input, "heap"))
3191 if (unformat (input, "main"))
3193 if (unformat (input, "validate %u", &val))
3194 acl_plugin_acl_set_validate_heap (am, val);
3195 else if (unformat (input, "trace %u", &val))
3196 acl_plugin_acl_set_trace_heap (am, val);
3199 else if (unformat (input, "hash"))
3201 if (unformat (input, "validate %u", &val))
3202 acl_plugin_hash_acl_set_validate_heap (val);
3203 else if (unformat (input, "trace %u", &val))
3204 acl_plugin_hash_acl_set_trace_heap (val);
3209 if (unformat (input, "session"))
3211 if (unformat (input, "table"))
3213 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3214 if (unformat (input, "max-entries"))
3216 if (!unformat (input, "%u", &val))
3218 error = clib_error_return (0,
3219 "expecting maximum number of entries, got `%U`",
3220 format_unformat_error, input);
3225 acl_set_session_max_entries (val);
3229 if (unformat (input, "hash-table-buckets"))
3231 if (!unformat (input, "%u", &val))
3233 error = clib_error_return (0,
3234 "expecting maximum number of hash table buckets, got `%U`",
3235 format_unformat_error, input);
3240 am->fa_conn_table_hash_num_buckets = val;
3244 if (unformat (input, "hash-table-memory"))
3246 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3248 error = clib_error_return (0,
3249 "expecting maximum amount of hash table memory, got `%U`",
3250 format_unformat_error, input);
3255 am->fa_conn_table_hash_memory_size = memory_size;
3259 if (unformat (input, "event-trace"))
3261 if (!unformat (input, "%u", &val))
3263 error = clib_error_return (0,
3264 "expecting trace level, got `%U`",
3265 format_unformat_error, input);
3270 am->trace_sessions = val;
3276 if (unformat (input, "timeout"))
3278 if (unformat (input, "udp"))
3280 if (unformat (input, "idle"))
3282 if (!unformat (input, "%u", &timeout))
3284 error = clib_error_return (0,
3285 "expecting timeout value in seconds, got `%U`",
3286 format_unformat_error,
3292 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3297 if (unformat (input, "tcp"))
3299 if (unformat (input, "idle"))
3301 if (!unformat (input, "%u", &timeout))
3303 error = clib_error_return (0,
3304 "expecting timeout value in seconds, got `%U`",
3305 format_unformat_error,
3311 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3315 if (unformat (input, "transient"))
3317 if (!unformat (input, "%u", &timeout))
3319 error = clib_error_return (0,
3320 "expecting timeout value in seconds, got `%U`",
3321 format_unformat_error,
3327 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3341 my_format_mac_address (u8 * s, va_list * args)
3343 u8 *a = va_arg (*args, u8 *);
3344 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3345 a[0], a[1], a[2], a[3], a[4], a[5]);
3349 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3351 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3353 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3354 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3355 format_ip46_address, &a->src_ip_addr,
3356 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3358 my_format_mac_address, a->src_mac,
3359 my_format_mac_address, a->src_mac_mask);
3364 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3366 vlib_main_t *vm = am->vlib_main;
3369 /* Don't try to print someone else's memory */
3370 if (macip_acl_index > vec_len (am->macip_acls))
3373 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3374 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3376 vlib_cli_output (vm,
3377 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3378 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3380 vlib_cli_output (vm,
3381 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3382 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3383 vlib_cli_output (vm,
3384 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3385 a->out_ip4_table_index, a->out_ip6_table_index,
3386 a->out_l2_table_index);
3387 for (i = 0; i < vec_len (a->rules); i++)
3388 vlib_cli_output (vm, " rule %d: %U\n", i,
3389 my_macip_acl_rule_t_pretty_format,
3390 vec_elt_at_index (a->rules, i));
3394 static clib_error_t *
3395 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3397 input, vlib_cli_command_t * cmd)
3399 clib_error_t *error = 0;
3400 acl_main_t *am = &acl_main;
3404 (void) unformat (input, "index %u", &acl_index);
3406 for (i = 0; i < vec_len (am->macip_acls); i++)
3408 /* Don't attempt to show the ACLs that do not exist */
3409 if (pool_is_free_index (am->macip_acls, i))
3412 if ((acl_index != ~0) && (acl_index != i))
3417 macip_acl_print (am, i);
3418 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3420 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3422 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3430 static clib_error_t *
3431 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3433 input, vlib_cli_command_t * cmd)
3435 clib_error_t *error = 0;
3436 acl_main_t *am = &acl_main;
3438 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3440 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3441 vec_elt (am->macip_acl_by_sw_if_index, i));
3447 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3450 vlib_main_t *vm = am->vlib_main;
3452 for (i = 0; i < vec_len (am->acls); i++)
3454 if (acl_is_not_defined (am, i))
3456 /* don't attempt to show the ACLs that do not exist */
3459 if ((acl_index != ~0) && (acl_index != i))
3463 acl_print_acl (vm, am, i);
3465 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3467 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3468 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3471 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3473 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3474 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3477 if (i < vec_len (am->lc_index_vec_by_acl))
3479 vlib_cli_output (vm, " used in lookup context index: %U\n",
3480 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3485 static clib_error_t *
3486 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3487 unformat_input_t * input, vlib_cli_command_t * cmd)
3489 clib_error_t *error = 0;
3490 acl_main_t *am = &acl_main;
3493 (void) unformat (input, "index %u", &acl_index);
3495 acl_plugin_show_acl (am, acl_index);
3499 static clib_error_t *
3500 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3501 unformat_input_t * input,
3502 vlib_cli_command_t * cmd)
3504 clib_error_t *error = 0;
3507 (void) unformat (input, "index %u", &lc_index);
3509 acl_plugin_show_lookup_context (lc_index);
3513 static clib_error_t *
3514 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3515 unformat_input_t * input,
3516 vlib_cli_command_t * cmd)
3518 clib_error_t *error = 0;
3521 (void) unformat (input, "index %u", &lc_index);
3523 acl_plugin_show_lookup_user (lc_index);
3529 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3532 vlib_main_t *vm = am->vlib_main;
3535 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3536 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3538 /* if we need a particular interface, skip all the others */
3539 if ((sw_if_index != ~0) && (sw_if_index != swi))
3542 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3544 if (intf_has_etype_whitelist (am, swi, 1))
3546 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3547 am->input_etype_whitelist_by_sw_if_index[swi],
3550 if (intf_has_etype_whitelist (am, swi, 0))
3552 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3553 am->output_etype_whitelist_by_sw_if_index[swi],
3557 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3558 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3560 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3561 am->input_acl_vec_by_sw_if_index[swi], "%d");
3564 vlib_cli_output (vm, "\n");
3565 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3567 acl_print_acl (vm, am, *pj);
3569 vlib_cli_output (vm, "\n");
3573 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3574 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3576 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3577 am->output_acl_vec_by_sw_if_index[swi], "%d");
3580 vlib_cli_output (vm, "\n");
3581 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3583 acl_print_acl (vm, am, *pj);
3585 vlib_cli_output (vm, "\n");
3588 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3590 vlib_cli_output (vm, " input lookup context index: %d",
3591 am->input_lc_index_by_sw_if_index[swi]);
3593 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3595 vlib_cli_output (vm, " output lookup context index: %d",
3596 am->output_lc_index_by_sw_if_index[swi]);
3603 static clib_error_t *
3604 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3605 unformat_input_t * input,
3606 vlib_cli_command_t * cmd)
3608 clib_error_t *error = 0;
3609 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3612 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3613 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3614 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3615 format_acl_plugin_5tuple, five_tuple);
3617 error = clib_error_return (0, "expecting 6 hex integers");
3622 static clib_error_t *
3623 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3625 input, vlib_cli_command_t * cmd)
3627 clib_error_t *error = 0;
3628 acl_main_t *am = &acl_main;
3630 u32 sw_if_index = ~0;
3631 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3632 int show_acl = unformat (input, "acl");
3633 int detail = unformat (input, "detail");
3635 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3639 static clib_error_t *
3640 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3641 unformat_input_t * input,
3642 vlib_cli_command_t * cmd)
3644 clib_error_t *error = 0;
3645 acl_main_t *am = &acl_main;
3647 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3650 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3654 vlib_cli_output (vm, " Not initialized\n");
3656 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3657 if (am->hash_lookup_mheap)
3659 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3663 vlib_cli_output (vm, " Not initialized\n");
3669 acl_plugin_show_sessions (acl_main_t * am,
3670 u32 show_session_thread_id,
3671 u32 show_session_session_index)
3673 vlib_main_t *vm = am->vlib_main;
3675 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3676 vnet_sw_interface_t *swif;
3679 u64 n_adds = am->fa_session_total_adds;
3680 u64 n_dels = am->fa_session_total_dels;
3681 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3682 n_dels, n_adds - n_dels);
3684 vlib_cli_output (vm, "\n\nPer-thread data:");
3685 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3687 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3688 vlib_cli_output (vm, "Thread #%d:", wk);
3689 if (show_session_thread_id == wk
3690 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3692 vlib_cli_output (vm, " session index %u:",
3693 show_session_session_index);
3694 fa_session_t *sess =
3695 pw->fa_sessions_pool + show_session_session_index;
3696 u64 *m = (u64 *) & sess->info;
3697 vlib_cli_output (vm,
3698 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3699 m[0], m[1], m[2], m[3], m[4], m[5]);
3700 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3701 vlib_cli_output (vm, " tcp_flags_seen: %x",
3702 sess->tcp_flags_seen.as_u16);
3703 vlib_cli_output (vm, " last active time: %lu",
3704 sess->last_active_time);
3705 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3706 vlib_cli_output (vm, " link enqueue time: %lu",
3707 sess->link_enqueue_time);
3708 vlib_cli_output (vm, " link next index: %u",
3709 sess->link_next_idx);
3710 vlib_cli_output (vm, " link prev index: %u",
3711 sess->link_prev_idx);
3712 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3714 vlib_cli_output (vm, " connection add/del stats:", wk);
3715 pool_foreach (swif, im->sw_interfaces, (
3722 (pw->fa_session_adds_by_sw_if_index)
3724 pw->fa_session_adds_by_sw_if_index
3729 (pw->fa_session_dels_by_sw_if_index)
3731 pw->fa_session_dels_by_sw_if_index
3733 vlib_cli_output (vm,
3734 " sw_if_index %d: add %lu - del %lu = %lu",
3743 vlib_cli_output (vm, " connection timeout type lists:", wk);
3745 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3747 u32 head_session_index = pw->fa_conn_list_head[tt];
3748 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3749 head_session_index);
3750 if (~0 != head_session_index)
3752 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3753 vlib_cli_output (vm, " last active time: %lu",
3754 sess->last_active_time);
3755 vlib_cli_output (vm, " link enqueue time: %lu",
3756 sess->link_enqueue_time);
3760 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3761 vlib_cli_output (vm, " Requeue until time: %lu",
3762 pw->requeue_until_time);
3763 vlib_cli_output (vm, " Current time wait interval: %lu",
3764 pw->current_time_wait_interval);
3765 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3766 pw->cnt_deleted_sessions);
3767 vlib_cli_output (vm, " Delete already deleted: %lu",
3768 pw->cnt_already_deleted_sessions);
3769 vlib_cli_output (vm, " Session timers restarted: %lu",
3770 pw->cnt_session_timer_restarted);
3771 vlib_cli_output (vm, " Swipe until this time: %lu",
3772 pw->swipe_end_time);
3773 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3774 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3775 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3777 pw->pending_clear_sw_if_index_bitmap);
3778 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3779 vlib_cli_output (vm, " interrupt is pending: %d",
3780 pw->interrupt_is_pending);
3781 vlib_cli_output (vm, " interrupt is needed: %d",
3782 pw->interrupt_is_needed);
3783 vlib_cli_output (vm, " interrupt is unwanted: %d",
3784 pw->interrupt_is_unwanted);
3785 vlib_cli_output (vm, " interrupt generation: %d",
3786 pw->interrupt_generation);
3788 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3789 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3790 foreach_fa_cleaner_counter;
3792 vlib_cli_output (vm, "Interrupt generation: %d",
3793 am->fa_interrupt_generation);
3794 vlib_cli_output (vm,
3795 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3796 am->fa_min_deleted_sessions_per_interval,
3797 am->fa_max_deleted_sessions_per_interval,
3798 am->fa_cleaner_wait_time_increment * 1000.0,
3799 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3800 1000.0 / (f64) vm->clib_time.clocks_per_second);
3803 static clib_error_t *
3804 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3805 unformat_input_t * input,
3806 vlib_cli_command_t * cmd)
3808 clib_error_t *error = 0;
3809 acl_main_t *am = &acl_main;
3811 u32 show_bihash_verbose = 0;
3812 u32 show_session_thread_id = ~0;
3813 u32 show_session_session_index = ~0;
3814 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3815 &show_session_session_index);
3816 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3818 acl_plugin_show_sessions (am, show_session_thread_id,
3819 show_session_session_index);
3820 show_fa_sessions_hash (vm, show_bihash_verbose);
3824 static clib_error_t *
3825 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3826 unformat_input_t * input,
3827 vlib_cli_command_t * cmd)
3829 clib_error_t *error = 0;
3832 u32 sw_if_index = ~0;
3833 int show_acl_hash_info = 0;
3834 int show_applied_info = 0;
3835 int show_mask_type = 0;
3836 int show_bihash = 0;
3837 u32 show_bihash_verbose = 0;
3839 if (unformat (input, "acl"))
3841 show_acl_hash_info = 1;
3842 /* mask-type is handy to see as well right there */
3844 unformat (input, "index %u", &acl_index);
3846 else if (unformat (input, "applied"))
3848 show_applied_info = 1;
3849 unformat (input, "sw_if_index %u", &sw_if_index);
3851 else if (unformat (input, "mask"))
3855 else if (unformat (input, "hash"))
3858 unformat (input, "verbose %u", &show_bihash_verbose);
3862 (show_mask_type || show_acl_hash_info || show_applied_info
3865 /* if no qualifiers specified, show all */
3867 show_acl_hash_info = 1;
3868 show_applied_info = 1;
3872 acl_plugin_show_tables_mask_type ();
3873 if (show_acl_hash_info)
3874 acl_plugin_show_tables_acl_hash_info (acl_index);
3875 if (show_applied_info)
3876 acl_plugin_show_tables_applied_info (sw_if_index);
3878 acl_plugin_show_tables_bihash (show_bihash_verbose);
3883 static clib_error_t *
3884 acl_clear_aclplugin_fn (vlib_main_t * vm,
3885 unformat_input_t * input, vlib_cli_command_t * cmd)
3887 clib_error_t *error = 0;
3888 acl_main_t *am = &acl_main;
3889 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3890 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3895 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3896 .path = "set acl-plugin",
3897 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3898 .function = acl_set_aclplugin_fn,
3901 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3902 .path = "show acl-plugin acl",
3903 .short_help = "show acl-plugin acl [index N]",
3904 .function = acl_show_aclplugin_acl_fn,
3907 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3908 .path = "show acl-plugin lookup context",
3909 .short_help = "show acl-plugin lookup context [index N]",
3910 .function = acl_show_aclplugin_lookup_context_fn,
3913 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3914 .path = "show acl-plugin lookup user",
3915 .short_help = "show acl-plugin lookup user [index N]",
3916 .function = acl_show_aclplugin_lookup_user_fn,
3919 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3920 .path = "show acl-plugin decode 5tuple",
3921 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3922 .function = acl_show_aclplugin_decode_5tuple_fn,
3925 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3926 .path = "show acl-plugin interface",
3927 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3928 .function = acl_show_aclplugin_interface_fn,
3931 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3932 .path = "show acl-plugin memory",
3933 .short_help = "show acl-plugin memory",
3934 .function = acl_show_aclplugin_memory_fn,
3937 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3938 .path = "show acl-plugin sessions",
3939 .short_help = "show acl-plugin sessions",
3940 .function = acl_show_aclplugin_sessions_fn,
3943 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3944 .path = "show acl-plugin tables",
3945 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3946 .function = acl_show_aclplugin_tables_fn,
3949 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3950 .path = "show acl-plugin macip acl",
3951 .short_help = "show acl-plugin macip acl [index N]",
3952 .function = acl_show_aclplugin_macip_acl_fn,
3955 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3956 .path = "show acl-plugin macip interface",
3957 .short_help = "show acl-plugin macip interface",
3958 .function = acl_show_aclplugin_macip_interface_fn,
3961 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3962 .path = "clear acl-plugin sessions",
3963 .short_help = "clear acl-plugin sessions",
3964 .function = acl_clear_aclplugin_fn,
3968 static clib_error_t *
3969 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3971 acl_main_t *am = &acl_main;
3972 u32 conn_table_hash_buckets;
3973 u32 conn_table_hash_memory_size;
3974 u32 conn_table_max_entries;
3977 u32 hash_lookup_hash_buckets;
3978 u32 hash_lookup_hash_memory;
3980 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3983 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3984 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3985 else if (unformat (input, "connection hash memory %d",
3986 &conn_table_hash_memory_size))
3987 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3988 else if (unformat (input, "connection count max %d",
3989 &conn_table_max_entries))
3990 am->fa_conn_table_max_entries = conn_table_max_entries;
3991 else if (unformat (input, "main heap size %d", &main_heap_size))
3992 am->acl_mheap_size = main_heap_size;
3993 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3994 am->hash_lookup_mheap_size = hash_heap_size;
3995 else if (unformat (input, "hash lookup hash buckets %d",
3996 &hash_lookup_hash_buckets))
3997 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3998 else if (unformat (input, "hash lookup hash memory %d",
3999 &hash_lookup_hash_memory))
4000 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4002 return clib_error_return (0, "unknown input '%U'",
4003 format_unformat_error, input);
4008 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4010 static clib_error_t *
4011 acl_init (vlib_main_t * vm)
4013 acl_main_t *am = &acl_main;
4014 clib_error_t *error = 0;
4015 memset (am, 0, sizeof (*am));
4017 am->vnet_main = vnet_get_main ();
4019 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4021 /* Ask for a correctly-sized block of API message decode slots */
4022 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4023 VL_MSG_FIRST_AVAILABLE);
4025 error = acl_plugin_api_hookup (vm);
4027 /* Add our API messages to the global name_crc hash table */
4028 setup_message_id_table (am, &api_main);
4032 acl_setup_fa_nodes ();
4034 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
4035 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4037 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4038 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4040 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4041 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4042 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4043 TCP_SESSION_IDLE_TIMEOUT_SEC;
4044 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4045 UDP_SESSION_IDLE_TIMEOUT_SEC;
4047 am->fa_conn_table_hash_num_buckets =
4048 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4049 am->fa_conn_table_hash_memory_size =
4050 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4051 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4052 vlib_thread_main_t *tm = vlib_get_thread_main ();
4053 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4057 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4059 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4060 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4061 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4062 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4064 pw->fa_conn_list_head[tt] = ~0;
4065 pw->fa_conn_list_tail[tt] = ~0;
4070 am->fa_min_deleted_sessions_per_interval =
4071 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4072 am->fa_max_deleted_sessions_per_interval =
4073 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4074 am->fa_cleaner_wait_time_increment =
4075 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4077 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4078 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4079 am->fa_cleaner_cnt_unknown_event = 0;
4080 am->fa_cleaner_cnt_timer_restarted = 0;
4081 am->fa_cleaner_cnt_wait_with_timeout = 0;
4084 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4087 am->l4_match_nonfirst_fragment = 1;
4089 /* use the new fancy hash-based matching */
4090 am->use_hash_acl_matching = 1;
4092 am->interface_acl_user_id =
4093 acl_plugin_register_user_module ("interface ACL", "sw_if_index",
4099 VLIB_INIT_FUNCTION (acl_init);
4103 * fd.io coding-style-patch-verification: ON
4106 * eval: (c-set-style "gnu")