2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/in_out_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
32 /* define message structures */
34 #include <acl/acl_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <acl/acl_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <acl/acl_all_api_h.h>
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
54 #include "hash_lookup.h"
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
61 /* List of message types that this plugin understands */
63 #define foreach_acl_plugin_api_msg \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace) \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
70 _(ACL_DUMP, acl_dump) \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
79 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
80 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
84 VLIB_PLUGIN_REGISTER () = {
85 .version = VPP_BUILD_VER,
86 .description = "Access Control Lists",
92 format_vec16 (u8 * s, va_list * va)
94 u16 *v = va_arg (*va, u16 *);
95 char *fmt = va_arg (*va, char *);
97 for (i = 0; i < vec_len (v); i++)
100 s = format (s, ", ");
101 s = format (s, fmt, v[i]);
110 acl_set_heap (acl_main_t * am)
112 if (0 == am->acl_mheap)
114 am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
115 mheap_t *h = mheap_header (am->acl_mheap);
116 h->flags |= MHEAP_FLAG_THREAD_SAFE;
118 void *oldheap = clib_mem_set_heap (am->acl_mheap);
123 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
125 clib_mem_set_heap (acl_set_heap (am));
126 mheap_t *h = mheap_header (am->acl_mheap);
129 h->flags |= MHEAP_FLAG_VALIDATE;
130 h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
135 h->flags &= ~MHEAP_FLAG_VALIDATE;
136 h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
141 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
143 clib_mem_set_heap (acl_set_heap (am));
144 mheap_t *h = mheap_header (am->acl_mheap);
147 h->flags |= MHEAP_FLAG_TRACE;
151 h->flags &= ~MHEAP_FLAG_TRACE;
156 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
158 acl_main_t *am = &acl_main;
159 vl_api_acl_plugin_get_version_reply_t *rmp;
160 int msg_size = sizeof (*rmp);
161 vl_api_registration_t *reg;
163 reg = vl_api_client_index_to_registration (mp->client_index);
167 rmp = vl_msg_api_alloc (msg_size);
168 memset (rmp, 0, msg_size);
170 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
171 rmp->context = mp->context;
172 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
173 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
175 vl_api_send_msg (reg, (u8 *) rmp);
179 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
182 vl_api_acl_plugin_control_ping_reply_t *rmp;
183 acl_main_t *am = &acl_main;
187 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
189 rmp->vpe_pid = ntohl (getpid ());
195 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
196 u32 * acl_list_index, u8 * tag)
198 acl_main_t *am = &acl_main;
201 acl_rule_t *acl_new_rules = 0;
204 if (*acl_list_index != ~0)
206 /* They supplied some number, let's see if this ACL exists */
207 if (pool_is_free_index (am->acls, *acl_list_index))
209 /* tried to replace a non-existent ACL, no point doing anything */
211 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
212 *acl_list_index, tag);
213 return VNET_API_ERROR_NO_SUCH_ENTRY;
219 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
220 *acl_list_index, tag);
223 void *oldheap = acl_set_heap (am);
225 /* Create and populate the rules */
227 vec_validate (acl_new_rules, count - 1);
229 for (i = 0; i < count; i++)
231 r = vec_elt_at_index (acl_new_rules, i);
232 memset (r, 0, sizeof (*r));
233 r->is_permit = rules[i].is_permit;
234 r->is_ipv6 = rules[i].is_ipv6;
237 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
238 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
242 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
243 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
245 r->src_prefixlen = rules[i].src_ip_prefix_len;
246 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
247 r->proto = rules[i].proto;
248 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
249 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
250 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
251 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
252 r->tcp_flags_value = rules[i].tcp_flags_value;
253 r->tcp_flags_mask = rules[i].tcp_flags_mask;
256 if (~0 == *acl_list_index)
259 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
260 memset (a, 0, sizeof (*a));
261 /* Will return the newly allocated ACL index */
262 *acl_list_index = a - am->acls;
266 a = am->acls + *acl_list_index;
267 hash_acl_delete (am, *acl_list_index);
268 /* Get rid of the old rules */
272 a->rules = acl_new_rules;
274 memcpy (a->tag, tag, sizeof (a->tag));
275 hash_acl_add (am, *acl_list_index);
276 clib_mem_set_heap (oldheap);
281 acl_del_list (u32 acl_list_index)
283 acl_main_t *am = &acl_main;
286 if (pool_is_free_index (am->acls, acl_list_index))
288 return VNET_API_ERROR_NO_SUCH_ENTRY;
291 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
293 if (vec_len (vec_elt (am->input_sw_if_index_vec_by_acl, acl_list_index))
296 /* ACL is applied somewhere inbound. Refuse to delete */
297 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
300 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
303 (vec_elt (am->output_sw_if_index_vec_by_acl, acl_list_index)) > 0)
305 /* ACL is applied somewhere outbound. Refuse to delete */
306 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
310 void *oldheap = acl_set_heap (am);
311 /* delete any references to the ACL */
312 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
314 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
317 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
319 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
327 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
329 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
332 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
334 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
342 /* delete the hash table data */
344 hash_acl_delete (am, acl_list_index);
345 /* now we can delete the ACL itself */
346 a = pool_elt_at_index (am->acls, acl_list_index);
350 pool_put (am->acls, a);
351 clib_mem_set_heap (oldheap);
355 /* Some aids in ASCII graphing the content */
362 u8 ip4_5tuple_mask[] =
363 _(" dmac smac etype ")
364 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
371 _(" ttl pr checksum ")
380 _("L4 T/U sport dport ")
390 u8 ip6_5tuple_mask[] =
391 _(" dmac smac etype ")
392 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
394 _(0x0000) __ __ __ __
396 _(0x0004) __ __ XX __
398 _(0x0008) XX XX XX XX
399 _(0x000C) XX XX XX XX
400 _(0x0010) XX XX XX XX
401 _(0x0014) XX XX XX XX
403 _(0x0018) XX XX XX XX
404 _(0x001C) XX XX XX XX
405 _(0x0020) XX XX XX XX
406 _(0x0024) XX XX XX XX
407 _("L4T/U sport dport ")
408 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
410 u8 dot1q_5tuple_mask[] =
411 _(" dmac smac dot1q etype ")
412 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
413 _(padpad) __ __ __ __
414 _(padpad) __ __ __ __
415 _(padpad) __ __ __ __
418 u8 dot1ad_5tuple_mask[] =
419 _(" dmac smac dot1ad dot1q etype ")
420 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
421 _(padpad) __ __ __ __
422 _(padpad) __ __ __ __
425 u8 ethertype_mask[] =
426 _(" dmac smac etype ")
427 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
436 count_skip (u8 * p, u32 size)
438 u64 *p64 = (u64 *) p;
439 /* Be tolerant to null pointer */
443 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
447 return (p64 - (u64 *) p) / 2;
451 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
452 u32 mask_len, u32 next_table_index,
453 u32 miss_next_index, u32 * table_index,
457 u32 memory_size = 2 << 13;
458 u32 skip = count_skip (mask, mask_len);
459 u32 match = (mask_len / 16) - skip;
460 u8 *skip_mask_ptr = mask + 16 * skip;
461 u32 current_data_flag = 0;
462 int current_data_offset = 0;
466 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
467 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
468 memory_size, skip, match,
469 next_table_index, miss_next_index,
470 table_index, current_data_flag,
471 current_data_offset, is_add,
472 1 /* delete_chain */ );
473 clib_mem_set_heap (oldheap);
478 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
479 u32 mask_len, u32 next_table_index,
480 u32 miss_next_index, u32 * table_index,
484 u32 memory_size = 2 << 22;
485 u32 skip = count_skip (mask, mask_len);
486 u32 match = (mask_len / 16) - skip;
487 u8 *skip_mask_ptr = mask + 16 * skip;
488 u32 current_data_flag = 0;
489 int current_data_offset = 0;
494 void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
495 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
496 memory_size, skip, match,
497 next_table_index, miss_next_index,
498 table_index, current_data_flag,
499 current_data_offset, is_add,
500 1 /* delete_chain */ );
501 clib_mem_set_heap (oldheap);
506 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
508 vnet_classify_main_t *cm = &vnet_classify_main;
509 u32 ip4_table_index = ~0;
510 u32 ip6_table_index = ~0;
511 u32 dot1q_table_index = ~0;
512 u32 dot1ad_table_index = ~0;
513 u32 etype_table_index = ~0;
514 void *oldheap = acl_set_heap (am);
516 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
518 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
520 vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
522 vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
524 vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
527 /* switch to global heap while calling vnet_* functions */
528 clib_mem_set_heap (cm->vlib_main->heap_base);
529 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
531 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
534 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
535 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
536 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
537 sizeof (ip4_5tuple_mask) - 1, ~0,
538 am->l2_input_classify_next_acl_ip4,
539 &ip4_table_index, 0);
541 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
544 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
545 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
546 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
547 sizeof (ip6_5tuple_mask) - 1, ~0,
548 am->l2_input_classify_next_acl_ip6,
549 &ip6_table_index, 0);
551 if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
554 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
555 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
556 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
557 sizeof (ip6_5tuple_mask) - 1, ~0,
558 ~0, &dot1q_table_index, 0);
560 if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
563 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
564 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
565 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
566 sizeof (dot1ad_5tuple_mask) - 1, ~0,
567 ~0, &dot1ad_table_index, 0);
569 if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
572 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
573 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
574 acl_classify_add_del_table_tiny (cm, ethertype_mask,
575 sizeof (ethertype_mask) - 1, ~0,
576 ~0, &etype_table_index, 0);
578 clib_mem_set_heap (oldheap);
583 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
585 vnet_classify_main_t *cm = &vnet_classify_main;
586 u32 ip4_table_index = ~0;
587 u32 ip6_table_index = ~0;
588 u32 dot1q_table_index = ~0;
589 u32 dot1ad_table_index = ~0;
590 u32 etype_table_index = ~0;
591 void *oldheap = acl_set_heap (am);
593 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
595 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
597 vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
599 vec_validate_init_empty
600 (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
601 vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
604 /* switch to global heap while calling vnet_* functions */
605 clib_mem_set_heap (cm->vlib_main->heap_base);
607 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
609 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
612 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
613 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
614 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
615 sizeof (ip4_5tuple_mask) - 1, ~0,
616 am->l2_output_classify_next_acl_ip4,
617 &ip4_table_index, 0);
619 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
622 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
623 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
624 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
625 sizeof (ip6_5tuple_mask) - 1, ~0,
626 am->l2_output_classify_next_acl_ip6,
627 &ip6_table_index, 0);
629 if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
632 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
633 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
634 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
635 sizeof (ip6_5tuple_mask) - 1, ~0,
636 ~0, &dot1q_table_index, 0);
638 if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
641 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
642 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
643 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
644 sizeof (dot1ad_5tuple_mask) - 1, ~0,
645 ~0, &dot1ad_table_index, 0);
647 if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
650 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
651 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
652 acl_classify_add_del_table_tiny (cm, ethertype_mask,
653 sizeof (ethertype_mask) - 1, ~0,
654 ~0, &etype_table_index, 0);
656 clib_mem_set_heap (oldheap);
661 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
662 u8 is_dot1ad, u8 is_ip6)
664 vnet_classify_main_t *cm = &vnet_classify_main;
674 l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
680 l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
682 match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
683 idx = (is_dot1ad) ? 20 : 16;
686 /* 802.1ad ethertype */
689 /* 802.1q ethertype */
695 /* 802.1q ethertype */
700 /* add sessions to vlan tables per ethernet_type */
704 match[idx + 1] = 0xdd;
710 match[idx + 1] = 0x00;
713 vnet_classify_add_del_session (cm, table_index, match, next_acl,
714 session_idx, 0, 0, 0, 1);
715 /* reset the mask back to being a mask */
717 match[idx + 1] = 0xff;
728 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
731 ? am->input_etype_whitelist_by_sw_if_index
732 : am->output_etype_whitelist_by_sw_if_index;
733 return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
737 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
738 u32 etype_table_index)
740 vnet_classify_main_t *cm = &vnet_classify_main;
742 ? am->input_etype_whitelist_by_sw_if_index
743 : am->output_etype_whitelist_by_sw_if_index;
744 u8 *match = ethertype_mask;
748 u16 *whitelist = vec_elt (v, sw_if_index);
749 u32 next = ~0; /* permit */
750 for (i = 0; i < vec_len (whitelist); i++)
753 match[12] = (whitelist[i] >> 8) & 0xff;
754 match[13] = whitelist[i] & 0xff;
756 || vnet_classify_add_del_session (cm, etype_table_index, match, next,
757 whitelist[i], 0, 0, 0, 1);
760 /* restore the mask */
767 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
769 vnet_classify_main_t *cm = &vnet_classify_main;
770 u32 ip4_table_index = ~0;
771 u32 ip6_table_index = ~0;
772 u32 dot1q_table_index = ~0;
773 u32 dot1ad_table_index = ~0;
774 u32 etype_table_index = ~0;
777 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
779 /* in case there were previous tables attached */
780 acl_unhook_l2_input_classify (am, sw_if_index);
782 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
783 sizeof (ip4_5tuple_mask) - 1, ~0,
784 am->l2_input_classify_next_acl_ip4,
785 &ip4_table_index, 1);
790 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
791 sizeof (ip6_5tuple_mask) - 1, ~0,
792 am->l2_input_classify_next_acl_ip6,
793 &ip6_table_index, 1);
796 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
797 sizeof (ip4_5tuple_mask) - 1, ~0,
798 am->l2_input_classify_next_acl_ip4,
799 &ip4_table_index, 0);
803 if (intf_has_etype_whitelist (am, sw_if_index, 1))
805 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
806 &etype_table_index, 1);
807 etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
811 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
812 sizeof (dot1ad_5tuple_mask) - 1,
813 etype_table_index, ~0,
814 &dot1ad_table_index, 1);
816 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
817 sizeof (dot1q_5tuple_mask) - 1,
818 dot1ad_table_index, ~0,
819 &dot1q_table_index, 1);
822 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
823 sizeof (dot1ad_5tuple_mask) - 1, ~0,
824 ~0, &dot1ad_table_index, 0);
825 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
826 sizeof (ip6_5tuple_mask) - 1, ~0,
827 am->l2_input_classify_next_acl_ip6,
828 &ip6_table_index, 0);
829 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
830 sizeof (ip4_5tuple_mask) - 1, ~0,
831 am->l2_input_classify_next_acl_ip4,
832 &ip4_table_index, 0);
837 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
838 ip6_table_index, dot1q_table_index);
842 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
843 sizeof (ip4_5tuple_mask) - 1, ~0,
844 am->l2_input_classify_next_acl_ip4,
845 &ip4_table_index, 0);
846 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
847 sizeof (ip6_5tuple_mask) - 1, ~0,
848 am->l2_input_classify_next_acl_ip6,
849 &ip6_table_index, 0);
850 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
851 sizeof (dot1q_5tuple_mask) - 1, ~0,
852 ~0, &dot1q_table_index, 0);
853 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
854 sizeof (dot1ad_5tuple_mask) - 1, ~0,
855 ~0, &dot1ad_table_index, 0);
859 /* add sessions to vlan tables per ethernet_type */
860 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
861 acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
862 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
863 acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
865 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
867 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
869 am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
871 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
873 am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
875 am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
878 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
880 clib_mem_set_heap (prevheap);
885 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
887 vnet_classify_main_t *cm = &vnet_classify_main;
888 u32 ip4_table_index = ~0;
889 u32 ip6_table_index = ~0;
890 u32 dot1q_table_index = ~0;
891 u32 dot1ad_table_index = ~0;
892 u32 etype_table_index = ~0;
895 void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
897 /* in case there were previous tables attached */
898 acl_unhook_l2_output_classify (am, sw_if_index);
900 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
901 sizeof (ip4_5tuple_mask) - 1, ~0,
902 am->l2_output_classify_next_acl_ip4,
903 &ip4_table_index, 1);
907 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
908 sizeof (ip6_5tuple_mask) - 1, ~0,
909 am->l2_output_classify_next_acl_ip6,
910 &ip6_table_index, 1);
913 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
914 sizeof (ip4_5tuple_mask) - 1, ~0,
915 am->l2_output_classify_next_acl_ip4,
916 &ip4_table_index, 0);
920 if (intf_has_etype_whitelist (am, sw_if_index, 0))
922 acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0, /* drop if no match */
923 &etype_table_index, 1);
924 etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
929 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
930 sizeof (dot1ad_5tuple_mask) - 1,
931 etype_table_index, ~0,
932 &dot1ad_table_index, 1);
934 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
935 sizeof (dot1q_5tuple_mask) - 1,
936 dot1ad_table_index, ~0,
937 &dot1q_table_index, 1);
940 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
941 sizeof (dot1ad_5tuple_mask) - 1, ~0,
942 ~0, &dot1ad_table_index, 0);
943 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
944 sizeof (ip6_5tuple_mask) - 1, ~0,
945 am->l2_output_classify_next_acl_ip6,
946 &ip6_table_index, 0);
947 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
948 sizeof (ip4_5tuple_mask) - 1, ~0,
949 am->l2_output_classify_next_acl_ip4,
950 &ip4_table_index, 0);
955 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
956 ip6_table_index, dot1q_table_index);
959 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
960 sw_if_index, ip4_table_index, ip6_table_index);
963 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
964 sizeof (ip6_5tuple_mask) - 1, ~0,
965 am->l2_output_classify_next_acl_ip6,
966 &ip6_table_index, 0);
967 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
968 sizeof (ip4_5tuple_mask) - 1, ~0,
969 am->l2_output_classify_next_acl_ip4,
970 &ip4_table_index, 0);
971 acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
972 sizeof (dot1q_5tuple_mask) - 1, ~0,
973 ~0, &dot1q_table_index, 0);
974 acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
975 sizeof (dot1ad_5tuple_mask) - 1, ~0,
976 ~0, &dot1ad_table_index, 0);
980 /* add sessions to vlan tables per ethernet_type */
981 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
982 acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
983 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
984 acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
986 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
988 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
990 am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
992 am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
994 am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
997 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
999 clib_mem_set_heap (prevheap);
1004 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1009 /* Utterly wrong? */
1010 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1012 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1014 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1018 rv = acl_hook_l2_input_classify (am, sw_if_index);
1022 rv = acl_unhook_l2_input_classify (am, sw_if_index);
1029 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1034 /* Utterly wrong? */
1035 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1037 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1039 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1043 rv = acl_hook_l2_output_classify (am, sw_if_index);
1047 rv = acl_unhook_l2_output_classify (am, sw_if_index);
1054 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1056 return (pool_is_free_index (am->acls, acl_list_index));
1061 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1063 acl_main_t *am = &acl_main;
1064 if (acl_is_not_defined (am, acl_list_index))
1066 /* ACL is not defined. Can not apply */
1067 return VNET_API_ERROR_NO_SUCH_ENTRY;
1069 void *oldheap = acl_set_heap (am);
1073 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1075 u32 index = vec_search (am->input_acl_vec_by_sw_if_index[sw_if_index],
1077 if (index < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1080 ("ACL %d is already applied inbound on sw_if_index %d (index %d)",
1081 acl_list_index, sw_if_index, index);
1082 /* the entry is already there */
1083 clib_mem_set_heap (oldheap);
1084 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
1086 /* if there was no ACL applied before, enable the ACL processing */
1087 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1089 acl_interface_in_enable_disable (am, sw_if_index, 1);
1091 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
1093 vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
1094 vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
1099 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1101 u32 index = vec_search (am->output_acl_vec_by_sw_if_index[sw_if_index],
1103 if (index < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1106 ("ACL %d is already applied outbound on sw_if_index %d (index %d)",
1107 acl_list_index, sw_if_index, index);
1108 /* the entry is already there */
1109 clib_mem_set_heap (oldheap);
1110 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1112 /* if there was no ACL applied before, enable the ACL processing */
1113 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1115 acl_interface_out_enable_disable (am, sw_if_index, 1);
1117 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
1118 &acl_list_index, 1);
1119 vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
1120 vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index],
1123 clib_mem_set_heap (oldheap);
1129 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1131 acl_main_t *am = &acl_main;
1133 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1134 void *oldheap = acl_set_heap (am);
1137 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1138 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1141 if (acl_list_index ==
1142 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
1144 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
1150 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1153 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1156 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1158 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1159 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1164 /* If there is no more ACLs applied on an interface, disable ACL processing */
1165 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1167 acl_interface_in_enable_disable (am, sw_if_index, 0);
1172 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1174 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
1176 if (acl_list_index ==
1177 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
1179 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
1185 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1188 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1191 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1193 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1194 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1199 /* If there is no more ACLs applied on an interface, disable ACL processing */
1200 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1202 acl_interface_out_enable_disable (am, sw_if_index, 0);
1205 clib_mem_set_heap (oldheap);
1210 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
1212 acl_main_t *am = &acl_main;
1214 void *oldheap = acl_set_heap (am);
1217 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1218 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1220 acl_interface_in_enable_disable (am, sw_if_index, 0);
1223 for (i = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1226 u32 acl_list_index =
1227 am->input_acl_vec_by_sw_if_index[sw_if_index][i];
1228 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1229 if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1232 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1235 vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1237 vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1243 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1247 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1248 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1250 acl_interface_out_enable_disable (am, sw_if_index, 0);
1253 for (i = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1256 u32 acl_list_index =
1257 am->output_acl_vec_by_sw_if_index[sw_if_index][i];
1258 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1259 if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1262 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1265 vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1267 vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1273 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1275 clib_mem_set_heap (oldheap);
1279 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1282 int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1283 acl_main_t *am = &acl_main;
1287 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
1290 hash_acl_apply (am, sw_if_index, is_input, acl_list_index);
1295 hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1297 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
1303 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1306 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1307 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1309 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1310 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1312 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1313 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1316 * if there are already inbound/outbound ACLs applied, toggle the
1317 * enable/disable - this will recreate the necessary tables.
1320 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1322 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1324 acl_interface_in_enable_disable (am, sw_if_index, 0);
1325 acl_interface_in_enable_disable (am, sw_if_index, 1);
1328 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1330 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1332 acl_interface_out_enable_disable (am, sw_if_index, 0);
1333 acl_interface_out_enable_disable (am, sw_if_index, 1);
1348 u32 arp_table_index;
1349 u32 dot1q_table_index;
1350 u32 dot1ad_table_index;
1351 u32 arp_dot1q_table_index;
1352 u32 arp_dot1ad_table_index;
1354 u32 out_table_index;
1355 u32 out_arp_table_index;
1356 u32 out_dot1q_table_index;
1357 u32 out_dot1ad_table_index;
1358 u32 out_arp_dot1q_table_index;
1359 u32 out_arp_dot1ad_table_index;
1360 } macip_match_type_t;
1363 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1369 for (i = 0; i < vec_len (mv); i++)
1371 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1372 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1382 /* Get metric used to sort match types.
1383 The more specific and the more often seen - the bigger the metric */
1385 match_type_metric (macip_match_type_t * m)
1387 unsigned int mac_bits_set = 0;
1388 unsigned int mac_byte;
1390 for (i = 0; i < 6; i++)
1392 mac_byte = m->mac_mask[i];
1393 for (; mac_byte; mac_byte >>= 1)
1394 mac_bits_set += mac_byte & 1;
1397 * Attempt to place the more specific and the more used rules on top.
1398 * There are obvious caveat corner cases to this, but they do not
1399 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1400 * going with a wildcard IPv4 with a specific MAC).
1402 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1406 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1408 /* Ascending sort based on the metric values */
1409 return match_type_metric (m1) - match_type_metric (m2);
1412 /* Get the offset of L3 source within ethernet packet */
1414 get_l3_src_offset (int is6)
1417 return (sizeof (ethernet_header_t) +
1418 offsetof (ip6_header_t, src_address));
1420 return (sizeof (ethernet_header_t) +
1421 offsetof (ip4_header_t, src_address));
1425 get_l3_dst_offset (int is6)
1428 return (sizeof (ethernet_header_t) +
1429 offsetof (ip6_header_t, dst_address));
1431 return (sizeof (ethernet_header_t) +
1432 offsetof (ip4_header_t, dst_address));
1436 * return if the is_permit value also requires to create the egress tables
1437 * For backwards compatibility, we keep the is_permit = 1 to only
1438 * create the ingress tables, and the new value of 3 will also
1439 * create the egress tables based on destination.
1442 macip_permit_also_egress (u8 is_permit)
1444 return (is_permit == 3);
1448 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1450 macip_match_type_t *mvec = NULL;
1451 macip_match_type_t *mt;
1452 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1454 u32 match_type_index;
1458 vnet_classify_main_t *cm = &vnet_classify_main;
1460 /* Count the number of different types of rules */
1461 for (i = 0; i < a->count; i++)
1465 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1466 a->rules[i].src_prefixlen,
1467 a->rules[i].is_ipv6)))
1469 match_type_index = vec_len (mvec);
1470 vec_validate (mvec, match_type_index);
1471 memcpy (mvec[match_type_index].mac_mask,
1472 a->rules[i].src_mac_mask, 6);
1473 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1474 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1475 mvec[match_type_index].has_egress = 0;
1476 mvec[match_type_index].table_index = ~0;
1477 mvec[match_type_index].arp_table_index = ~0;
1478 mvec[match_type_index].dot1q_table_index = ~0;
1479 mvec[match_type_index].dot1ad_table_index = ~0;
1480 mvec[match_type_index].arp_dot1q_table_index = ~0;
1481 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1482 mvec[match_type_index].out_table_index = ~0;
1483 mvec[match_type_index].out_arp_table_index = ~0;
1484 mvec[match_type_index].out_dot1q_table_index = ~0;
1485 mvec[match_type_index].out_dot1ad_table_index = ~0;
1486 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1487 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1489 mvec[match_type_index].count++;
1490 mvec[match_type_index].has_egress |=
1491 macip_permit_also_egress (a->rules[i].is_permit);
1493 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1494 vec_sort_with_function (mvec, match_type_compare);
1495 /* Create the classifier tables */
1497 out_last_table = ~0;
1498 /* First add ARP tables */
1499 vec_foreach (mt, mvec)
1502 int is6 = mt->is_ipv6;
1504 u32 *last_tag_table;
1505 u32 *out_last_tag_table;
1512 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1513 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1514 | Destination Address |
1515 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1517 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1519 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1520 | EtherType | Hardware Type |
1521 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1522 | Protocol Type | Hw addr len | Proto addr len|
1523 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1525 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1526 | Sender Hardware Address |
1527 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1528 | Sender Protocol Address |
1529 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1530 | Target Hardware Address |
1531 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1532 | | TargetProtocolAddress |
1533 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1535 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1537 for (tags = 2; tags >= 0; tags--)
1539 memset (mask, 0, sizeof (mask));
1540 /* source MAC address */
1541 memcpy (&mask[6], mt->mac_mask, 6);
1547 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1549 last_tag_table = &mt->arp_table_index;
1552 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1553 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1555 last_tag_table = &mt->arp_dot1q_table_index;
1558 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1559 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1560 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1562 last_tag_table = &mt->arp_dot1ad_table_index;
1566 /* sender hardware address within ARP */
1567 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1568 /* sender protocol address within ARP */
1569 for (i = 0; i < (mt->prefix_len / 8); i++)
1570 mask[l3_offset + 14 + i] = 0xff;
1571 if (mt->prefix_len % 8)
1572 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1573 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1575 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1577 1)) / sizeof (u32x4)) * sizeof (u32x4);
1578 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1579 (~0 == last_table) ? 0 : ~0,
1581 last_table = *last_tag_table;
1584 /* egress ARP table */
1585 memset (mask, 0, sizeof (mask));
1591 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1593 out_last_tag_table = &mt->out_arp_table_index;
1596 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1597 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1599 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1602 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1603 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1604 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1606 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1610 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1611 /* mask captures just the destination and the ethertype */
1612 mask_len = ((l3_offset +
1614 1)) / sizeof (u32x4)) * sizeof (u32x4);
1615 acl_classify_add_del_table_small (cm, mask, mask_len,
1618 out_last_table) ? 0 : ~0,
1619 out_last_tag_table, 1);
1620 out_last_table = *out_last_tag_table;
1625 /* Now add IP[46] tables */
1626 vec_foreach (mt, mvec)
1629 int is6 = mt->is_ipv6;
1633 u32 *last_tag_table;
1634 u32 *out_last_tag_table;
1637 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1639 for (tags = 2; tags >= 0; tags--)
1641 memset (mask, 0, sizeof (mask));
1642 memcpy (&mask[6], mt->mac_mask, 6);
1643 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1648 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1649 last_tag_table = &mt->table_index;
1652 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1653 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1654 last_tag_table = &mt->dot1q_table_index;
1657 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1658 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1659 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1660 last_tag_table = &mt->dot1ad_table_index;
1663 for (i = 0; i < (mt->prefix_len / 8); i++)
1665 mask[l3_src_offs + i] = 0xff;
1667 if (mt->prefix_len % 8)
1669 mask[l3_src_offs + (mt->prefix_len / 8)] =
1670 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1673 * Round-up the number of bytes needed to store the prefix,
1674 * and round up the number of vectors too
1676 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1677 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1678 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1679 (~0 == last_table) ? 0 : ~0,
1681 last_table = *last_tag_table;
1685 for (tags = 2; tags >= 0; tags--)
1687 memset (mask, 0, sizeof (mask));
1688 /* MAC destination */
1689 memcpy (&mask[0], mt->mac_mask, 6);
1690 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1695 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1696 out_last_tag_table = &mt->out_table_index;
1699 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1700 memset (&mask[16], 0xff, 2); /* ethernet protocol */
1701 out_last_tag_table = &mt->out_dot1q_table_index;
1704 memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1705 memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1706 memset (&mask[20], 0xff, 2); /* ethernet protocol */
1707 out_last_tag_table = &mt->out_dot1ad_table_index;
1710 for (i = 0; i < (mt->prefix_len / 8); i++)
1712 mask[l3_dst_offs + i] = 0xff;
1714 if (mt->prefix_len % 8)
1716 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1717 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1720 * Round-up the number of bytes needed to store the prefix,
1721 * and round up the number of vectors too
1723 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1725 1)) / sizeof (u32x4)) * sizeof (u32x4);
1726 acl_classify_add_del_table_small (cm, mask, mask_len,
1728 (~0 == out_last_table) ? 0 : ~0,
1729 out_last_tag_table, 1);
1730 out_last_table = *out_last_tag_table;
1734 a->ip4_table_index = last_table;
1735 a->ip6_table_index = last_table;
1736 a->l2_table_index = last_table;
1738 a->out_ip4_table_index = out_last_table;
1739 a->out_ip6_table_index = out_last_table;
1740 a->out_l2_table_index = out_last_table;
1742 /* Populate the classifier tables with rules from the MACIP ACL */
1743 for (i = 0; i < a->count; i++)
1747 int is6 = a->rules[i].is_ipv6;
1754 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1755 a->rules[i].src_prefixlen,
1756 a->rules[i].is_ipv6);
1757 ASSERT (match_type_index != ~0);
1759 for (tags = 2; tags >= 0; tags--)
1761 memset (mask, 0, sizeof (mask));
1762 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1763 memcpy (&mask[6], a->rules[i].src_mac, 6);
1768 tag_table = mvec[match_type_index].table_index;
1772 tag_table = mvec[match_type_index].dot1q_table_index;
1778 tag_table = mvec[match_type_index].dot1ad_table_index;
1788 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1790 mask[eth + 1] = 0xdd;
1794 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1796 mask[eth + 1] = 0x00;
1799 /* add session to table mvec[match_type_index].table_index; */
1800 vnet_classify_add_del_session (cm, tag_table,
1801 mask, a->rules[i].is_permit ? ~0 : 0,
1802 i, 0, action, metadata, 1);
1803 memset (&mask[12], 0, sizeof (mask) - 12);
1806 /* add ARP table entry too */
1807 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1809 memset (mask, 0, sizeof (mask));
1810 memcpy (&mask[6], a->rules[i].src_mac, 6);
1812 for (tags = 2; tags >= 0; tags--)
1818 tag_table = mvec[match_type_index].arp_table_index;
1824 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1832 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1843 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1844 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1846 vnet_classify_add_del_session (cm, tag_table, mask,
1847 a->rules[i].is_permit ? ~0 : 0,
1848 i, 0, action, metadata, 1);
1851 if (macip_permit_also_egress (a->rules[i].is_permit))
1853 /* Add the egress entry with destination set */
1854 for (tags = 2; tags >= 0; tags--)
1856 memset (mask, 0, sizeof (mask));
1857 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1858 /* src mac in the other direction becomes dst */
1859 memcpy (&mask[0], a->rules[i].src_mac, 6);
1864 tag_table = mvec[match_type_index].out_table_index;
1868 tag_table = mvec[match_type_index].out_dot1q_table_index;
1874 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1884 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1887 mask[eth + 1] = 0xdd;
1891 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1894 mask[eth + 1] = 0x00;
1897 /* add session to table mvec[match_type_index].table_index; */
1898 vnet_classify_add_del_session (cm, tag_table,
1900 a->rules[i].is_permit ? ~0 : 0,
1901 i, 0, action, metadata, 1);
1902 // memset (&mask[12], 0, sizeof (mask) - 12);
1905 /* add ARP table entry too */
1906 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1908 for (tags = 2; tags >= 0; tags--)
1910 memset (mask, 0, sizeof (mask));
1915 tag_table = mvec[match_type_index].out_arp_table_index;
1921 mvec[match_type_index].out_arp_dot1q_table_index;
1929 mvec[match_type_index].out_arp_dot1ad_table_index;
1939 vnet_classify_add_del_session (cm, tag_table,
1942 rules[i].is_permit ? ~0 : 0,
1943 i, 0, action, metadata, 1);
1952 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1954 vnet_classify_main_t *cm = &vnet_classify_main;
1955 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1957 if (a->ip4_table_index != ~0)
1959 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1960 &a->ip4_table_index, 0);
1961 a->ip4_table_index = ~0;
1963 if (a->ip6_table_index != ~0)
1965 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1966 &a->ip6_table_index, 0);
1967 a->ip6_table_index = ~0;
1969 if (a->l2_table_index != ~0)
1971 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1973 a->l2_table_index = ~0;
1975 if (a->out_ip4_table_index != ~0)
1977 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1978 &a->out_ip4_table_index, 0);
1979 a->out_ip4_table_index = ~0;
1981 if (a->out_ip6_table_index != ~0)
1983 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1984 &a->out_ip6_table_index, 0);
1985 a->out_ip6_table_index = ~0;
1987 if (a->out_l2_table_index != ~0)
1989 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1990 &a->out_l2_table_index, 0);
1991 a->out_l2_table_index = ~0;
1996 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2002 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2004 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2005 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2007 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2008 a->ip6_table_index, a->l2_table_index,
2010 /* return the first unhappy outcome but make try to plough through. */
2013 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2014 a->out_ip6_table_index,
2015 a->out_l2_table_index, is_apply);
2016 /* return the first unhappy outcome but make try to plough through. */
2023 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2024 u32 * acl_list_index, u8 * tag)
2026 acl_main_t *am = &acl_main;
2027 macip_acl_list_t *a;
2028 macip_acl_rule_t *r;
2029 macip_acl_rule_t *acl_new_rules = 0;
2033 if (*acl_list_index != ~0)
2035 /* They supplied some number, let's see if this MACIP ACL exists */
2036 if (pool_is_free_index (am->macip_acls, *acl_list_index))
2038 /* tried to replace a non-existent ACL, no point doing anything */
2040 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2041 *acl_list_index, tag);
2042 return VNET_API_ERROR_NO_SUCH_ENTRY;
2049 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2052 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2053 if (~0 != *acl_list_index)
2054 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2055 void *oldheap = acl_set_heap (am);
2056 /* Create and populate the rules */
2058 vec_validate (acl_new_rules, count - 1);
2060 for (i = 0; i < count; i++)
2062 r = &acl_new_rules[i];
2063 r->is_permit = rules[i].is_permit;
2064 r->is_ipv6 = rules[i].is_ipv6;
2065 memcpy (&r->src_mac, rules[i].src_mac, 6);
2066 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2067 if (rules[i].is_ipv6)
2068 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2070 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2071 r->src_prefixlen = rules[i].src_ip_prefix_len;
2074 if (~0 == *acl_list_index)
2077 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2078 memset (a, 0, sizeof (*a));
2079 /* Will return the newly allocated ACL index */
2080 *acl_list_index = a - am->macip_acls;
2084 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2087 vec_free (a->rules);
2089 macip_destroy_classify_tables (am, *acl_list_index);
2092 a->rules = acl_new_rules;
2094 memcpy (a->tag, tag, sizeof (a->tag));
2096 /* Create and populate the classifer tables */
2097 macip_create_classify_tables (am, *acl_list_index);
2098 clib_mem_set_heap (oldheap);
2099 /* If the ACL was already applied somewhere, reapply the newly created tables */
2101 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2106 /* No check for validity of sw_if_index - the callers were supposed to validate */
2109 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2112 u32 macip_acl_index;
2113 macip_acl_list_t *a;
2115 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2116 /* No point in deleting MACIP ACL which is not applied */
2117 if (~0 == macip_acl_index)
2118 return VNET_API_ERROR_NO_SUCH_ENTRY;
2120 void *oldheap = acl_set_heap (am);
2121 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2122 vec_validate_init_empty (am->sw_if_index_vec_by_macip_acl, macip_acl_index,
2124 clib_mem_set_heap (oldheap);
2125 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2126 /* remove the classifier tables off the interface L2 ACL */
2128 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2129 a->ip6_table_index, a->l2_table_index, 0);
2131 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2132 a->out_ip4_table_index, a->out_ip6_table_index,
2133 a->out_l2_table_index, 0);
2134 /* Unset the MACIP ACL index */
2135 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2136 am->sw_if_index_vec_by_macip_acl[macip_acl_index] = ~0;
2140 /* No check for validity of sw_if_index - the callers were supposed to validate */
2143 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2144 u32 macip_acl_index)
2146 macip_acl_list_t *a;
2148 if (pool_is_free_index (am->macip_acls, macip_acl_index))
2150 return VNET_API_ERROR_NO_SUCH_ENTRY;
2152 void *oldheap = acl_set_heap (am);
2153 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2154 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2155 vec_validate_init_empty (am->sw_if_index_vec_by_macip_acl, macip_acl_index,
2157 clib_mem_set_heap (oldheap);
2158 /* If there already a MACIP ACL applied, unapply it */
2159 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2160 macip_acl_interface_del_acl (am, sw_if_index);
2161 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2162 am->sw_if_index_vec_by_macip_acl[macip_acl_index] = sw_if_index;
2164 /* Apply the classifier tables for L2 ACLs */
2166 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2167 a->ip6_table_index, a->l2_table_index, 1);
2169 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2170 a->out_ip4_table_index, a->out_ip6_table_index,
2171 a->out_l2_table_index, 1);
2176 macip_acl_del_list (u32 acl_list_index)
2178 acl_main_t *am = &acl_main;
2179 macip_acl_list_t *a;
2181 if (pool_is_free_index (am->macip_acls, acl_list_index))
2183 return VNET_API_ERROR_NO_SUCH_ENTRY;
2186 /* delete any references to the ACL */
2187 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2189 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2191 macip_acl_interface_del_acl (am, i);
2195 void *oldheap = acl_set_heap (am);
2196 /* Now that classifier tables are detached, clean them up */
2197 macip_destroy_classify_tables (am, acl_list_index);
2199 /* now we can delete the ACL itself */
2200 a = pool_elt_at_index (am->macip_acls, acl_list_index);
2203 vec_free (a->rules);
2205 pool_put (am->macip_acls, a);
2206 clib_mem_set_heap (oldheap);
2212 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2215 acl_main_t *am = &acl_main;
2219 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2223 rv = macip_acl_interface_del_acl (am, sw_if_index);
2229 * If the client does not allocate enough memory for a variable-length
2230 * message, and then proceed to use it as if the full memory allocated,
2231 * absent the check we happily consume that on the VPP side, and go
2232 * along as if nothing happened. However, the resulting
2233 * effects range from just garbage in the API decode
2234 * (because the decoder snoops too far), to potential memory
2237 * This verifies that the actual length of the message is
2238 * at least expected_len, and complains loudly if it is not.
2240 * A failing check here is 100% a software bug on the API user side,
2241 * so we might as well yell.
2245 verify_message_len (void *mp, u32 expected_len, char *where)
2247 u32 supplied_len = vl_msg_api_get_msg_length (mp);
2248 if (supplied_len < expected_len)
2250 clib_warning ("%s: Supplied message length %d is less than expected %d",
2251 where, supplied_len, expected_len);
2260 /* API message handler */
2262 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2264 vl_api_acl_add_replace_reply_t *rmp;
2265 acl_main_t *am = &acl_main;
2267 u32 acl_list_index = ntohl (mp->acl_index);
2268 u32 acl_count = ntohl (mp->count);
2269 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2271 if (verify_message_len (mp, expected_len, "acl_add_replace"))
2273 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2277 rv = VNET_API_ERROR_INVALID_VALUE;
2281 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2283 rmp->acl_index = htonl(acl_list_index);
2289 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2291 acl_main_t *am = &acl_main;
2292 vl_api_acl_del_reply_t *rmp;
2295 rv = acl_del_list (ntohl (mp->acl_index));
2297 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2301 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2303 acl_main_t *am = &acl_main;
2304 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2305 u32 sw_if_index = ntohl (mp->sw_if_index);
2306 vl_api_acl_interface_add_del_reply_t *rmp;
2309 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2310 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2313 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2314 mp->is_input, ntohl (mp->acl_index));
2316 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2320 vl_api_acl_interface_set_acl_list_t_handler
2321 (vl_api_acl_interface_set_acl_list_t * mp)
2323 acl_main_t *am = &acl_main;
2324 vl_api_acl_interface_set_acl_list_reply_t *rmp;
2327 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2328 u32 sw_if_index = ntohl (mp->sw_if_index);
2330 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2331 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2334 acl_interface_reset_inout_acls (sw_if_index, 0);
2335 acl_interface_reset_inout_acls (sw_if_index, 1);
2337 for (i = 0; i < mp->count; i++)
2339 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2341 /* ACL does not exist, so we can not apply it */
2342 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2347 for (i = 0; i < mp->count; i++)
2349 acl_interface_add_del_inout_acl (sw_if_index, 1,
2351 ntohl (mp->acls[i]));
2356 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2360 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2362 api_rule->is_permit = r->is_permit;
2363 api_rule->is_ipv6 = r->is_ipv6;
2366 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2367 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2371 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2372 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2374 api_rule->src_ip_prefix_len = r->src_prefixlen;
2375 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2376 api_rule->proto = r->proto;
2377 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2378 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2379 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2380 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2381 api_rule->tcp_flags_mask = r->tcp_flags_mask;
2382 api_rule->tcp_flags_value = r->tcp_flags_value;
2386 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2387 acl_list_t * acl, u32 context)
2389 vl_api_acl_details_t *mp;
2390 vl_api_acl_rule_t *rules;
2392 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2393 void *oldheap = acl_set_heap (am);
2395 mp = vl_msg_api_alloc (msg_size);
2396 memset (mp, 0, msg_size);
2397 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2399 /* fill in the message */
2400 mp->context = context;
2401 mp->count = htonl (acl->count);
2402 mp->acl_index = htonl (acl - am->acls);
2403 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2404 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2406 for (i = 0; i < acl->count; i++)
2408 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2411 clib_mem_set_heap (oldheap);
2412 vl_api_send_msg (reg, (u8 *) mp);
2417 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2419 acl_main_t *am = &acl_main;
2423 vl_api_registration_t *reg;
2425 reg = vl_api_client_index_to_registration (mp->client_index);
2429 if (mp->acl_index == ~0)
2432 /* Just dump all ACLs */
2433 pool_foreach (acl, am->acls,
2435 send_acl_details(am, reg, acl, mp->context);
2441 acl_index = ntohl (mp->acl_index);
2442 if (!pool_is_free_index (am->acls, acl_index))
2444 acl = pool_elt_at_index (am->acls, acl_index);
2445 send_acl_details (am, reg, acl, mp->context);
2451 /* FIXME API: should we signal an error here at all ? */
2457 send_acl_interface_list_details (acl_main_t * am,
2458 vl_api_registration_t * reg,
2459 u32 sw_if_index, u32 context)
2461 vl_api_acl_interface_list_details_t *mp;
2467 void *oldheap = acl_set_heap (am);
2469 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2470 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2472 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2473 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2474 count = n_input + n_output;
2476 msg_size = sizeof (*mp);
2477 msg_size += sizeof (mp->acls[0]) * count;
2479 mp = vl_msg_api_alloc (msg_size);
2480 memset (mp, 0, msg_size);
2482 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2484 /* fill in the message */
2485 mp->context = context;
2486 mp->sw_if_index = htonl (sw_if_index);
2488 mp->n_input = n_input;
2489 for (i = 0; i < n_input; i++)
2491 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2493 for (i = 0; i < n_output; i++)
2495 mp->acls[n_input + i] =
2496 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2498 clib_mem_set_heap (oldheap);
2499 vl_api_send_msg (reg, (u8 *) mp);
2503 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2506 acl_main_t *am = &acl_main;
2507 vnet_sw_interface_t *swif;
2508 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2511 vl_api_registration_t *reg;
2513 reg = vl_api_client_index_to_registration (mp->client_index);
2517 if (mp->sw_if_index == ~0)
2520 pool_foreach (swif, im->sw_interfaces,
2522 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2528 sw_if_index = ntohl (mp->sw_if_index);
2529 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2530 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2534 /* MACIP ACL API handlers */
2537 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2539 vl_api_macip_acl_add_reply_t *rmp;
2540 acl_main_t *am = &acl_main;
2542 u32 acl_list_index = ~0;
2543 u32 acl_count = ntohl (mp->count);
2544 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2546 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2548 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2552 rv = VNET_API_ERROR_INVALID_VALUE;
2556 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2558 rmp->acl_index = htonl(acl_list_index);
2564 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2566 vl_api_macip_acl_add_replace_reply_t *rmp;
2567 acl_main_t *am = &acl_main;
2569 u32 acl_list_index = ntohl (mp->acl_index);
2570 u32 acl_count = ntohl (mp->count);
2571 u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2573 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2575 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2579 rv = VNET_API_ERROR_INVALID_VALUE;
2583 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2585 rmp->acl_index = htonl(acl_list_index);
2591 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2593 acl_main_t *am = &acl_main;
2594 vl_api_macip_acl_del_reply_t *rmp;
2597 rv = macip_acl_del_list (ntohl (mp->acl_index));
2599 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2603 vl_api_macip_acl_interface_add_del_t_handler
2604 (vl_api_macip_acl_interface_add_del_t * mp)
2606 acl_main_t *am = &acl_main;
2607 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2609 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2610 u32 sw_if_index = ntohl (mp->sw_if_index);
2612 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2613 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2616 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2617 ntohl (mp->acl_index));
2619 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2623 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2624 macip_acl_list_t * acl, u32 context)
2626 vl_api_macip_acl_details_t *mp;
2627 vl_api_macip_acl_rule_t *rules;
2628 macip_acl_rule_t *r;
2630 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2632 mp = vl_msg_api_alloc (msg_size);
2633 memset (mp, 0, msg_size);
2634 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2636 /* fill in the message */
2637 mp->context = context;
2640 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2641 mp->count = htonl (acl->count);
2642 mp->acl_index = htonl (acl - am->macip_acls);
2644 for (i = 0; i < acl->count; i++)
2647 rules[i].is_permit = r->is_permit;
2648 rules[i].is_ipv6 = r->is_ipv6;
2649 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2650 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2651 sizeof (r->src_mac_mask));
2653 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2654 sizeof (r->src_ip_addr.ip6));
2656 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2657 sizeof (r->src_ip_addr.ip4));
2658 rules[i].src_ip_prefix_len = r->src_prefixlen;
2663 /* No martini, no party - no ACL applied to this interface. */
2668 vl_api_send_msg (reg, (u8 *) mp);
2673 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2675 acl_main_t *am = &acl_main;
2676 macip_acl_list_t *acl;
2678 vl_api_registration_t *reg;
2680 reg = vl_api_client_index_to_registration (mp->client_index);
2684 if (mp->acl_index == ~0)
2686 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2687 pool_foreach (acl, am->macip_acls, (
2689 send_macip_acl_details (am, reg,
2698 u32 acl_index = ntohl (mp->acl_index);
2699 if (!pool_is_free_index (am->macip_acls, acl_index))
2701 acl = pool_elt_at_index (am->macip_acls, acl_index);
2702 send_macip_acl_details (am, reg, acl, mp->context);
2708 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2711 acl_main_t *am = &acl_main;
2712 vl_api_macip_acl_interface_get_reply_t *rmp;
2713 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2714 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2715 vl_api_registration_t *reg;
2718 reg = vl_api_client_index_to_registration (mp->client_index);
2722 rmp = vl_msg_api_alloc (msg_size);
2723 memset (rmp, 0, msg_size);
2725 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2726 rmp->context = mp->context;
2727 rmp->count = htonl (count);
2728 for (i = 0; i < count; i++)
2730 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2733 vl_api_send_msg (reg, (u8 *) rmp);
2737 send_macip_acl_interface_list_details (acl_main_t * am,
2738 vl_api_registration_t * reg,
2740 u32 acl_index, u32 context)
2742 vl_api_macip_acl_interface_list_details_t *rmp;
2743 /* at this time there is only ever 1 mac ip acl per interface */
2744 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2746 rmp = vl_msg_api_alloc (msg_size);
2747 memset (rmp, 0, msg_size);
2749 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2751 /* fill in the message */
2752 rmp->context = context;
2754 rmp->sw_if_index = htonl (sw_if_index);
2755 rmp->acls[0] = htonl (acl_index);
2757 vl_api_send_msg (reg, (u8 *) rmp);
2761 vl_api_macip_acl_interface_list_dump_t_handler
2762 (vl_api_macip_acl_interface_list_dump_t * mp)
2764 vl_api_registration_t *reg;
2765 acl_main_t *am = &acl_main;
2766 u32 sw_if_index = ntohl (mp->sw_if_index);
2768 reg = vl_api_client_index_to_registration (mp->client_index);
2772 if (sw_if_index == ~0)
2774 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2776 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2778 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2779 am->macip_acl_by_sw_if_index
2787 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2789 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2790 am->macip_acl_by_sw_if_index
2791 [sw_if_index], mp->context);
2797 vl_api_acl_interface_set_etype_whitelist_t_handler
2798 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2800 acl_main_t *am = &acl_main;
2801 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2804 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2805 u32 sw_if_index = ntohl (mp->sw_if_index);
2806 u16 *vec_in = 0, *vec_out = 0;
2807 void *oldheap = acl_set_heap (am);
2809 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2810 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2813 for (i = 0; i < mp->count; i++)
2815 if (i < mp->n_input)
2816 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2818 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2820 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2823 clib_mem_set_heap (oldheap);
2824 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2828 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2829 vl_api_registration_t * reg,
2830 u32 sw_if_index, u32 context)
2832 vl_api_acl_interface_etype_whitelist_details_t *mp;
2839 u16 *whitelist_in = 0;
2840 u16 *whitelist_out = 0;
2842 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2844 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2846 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2848 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2850 if ((0 == whitelist_in) && (0 == whitelist_out))
2851 return; /* nothing to do */
2853 void *oldheap = acl_set_heap (am);
2855 n_input = vec_len (whitelist_in);
2856 n_output = vec_len (whitelist_out);
2857 count = n_input + n_output;
2859 msg_size = sizeof (*mp);
2860 msg_size += sizeof (mp->whitelist[0]) * count;
2862 mp = vl_msg_api_alloc (msg_size);
2863 memset (mp, 0, msg_size);
2865 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2867 /* fill in the message */
2868 mp->context = context;
2869 mp->sw_if_index = htonl (sw_if_index);
2871 mp->n_input = n_input;
2872 for (i = 0; i < n_input; i++)
2874 mp->whitelist[i] = htons (whitelist_in[i]);
2876 for (i = 0; i < n_output; i++)
2878 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2880 clib_mem_set_heap (oldheap);
2881 vl_api_send_msg (reg, (u8 *) mp);
2886 vl_api_acl_interface_etype_whitelist_dump_t_handler
2887 (vl_api_acl_interface_list_dump_t * mp)
2889 acl_main_t *am = &acl_main;
2890 vnet_sw_interface_t *swif;
2891 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2894 vl_api_registration_t *reg;
2896 reg = vl_api_client_index_to_registration (mp->client_index);
2900 if (mp->sw_if_index == ~0)
2903 pool_foreach (swif, im->sw_interfaces,
2905 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2911 sw_if_index = ntohl (mp->sw_if_index);
2912 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2913 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2920 /* Set up the API message handling tables */
2921 static clib_error_t *
2922 acl_plugin_api_hookup (vlib_main_t * vm)
2924 acl_main_t *am = &acl_main;
2926 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
2928 vl_api_##n##_t_handler, \
2930 vl_api_##n##_t_endian, \
2931 vl_api_##n##_t_print, \
2932 sizeof(vl_api_##n##_t), 1);
2933 foreach_acl_plugin_api_msg;
2939 #define vl_msg_name_crc_list
2940 #include <acl/acl_all_api_h.h>
2941 #undef vl_msg_name_crc_list
2944 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2946 #define _(id,n,crc) \
2947 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2948 foreach_vl_msg_name_crc_acl;
2953 acl_setup_fa_nodes (void)
2955 vlib_main_t *vm = vlib_get_main ();
2956 acl_main_t *am = &acl_main;
2957 vlib_node_t *n, *n4, *n6;
2959 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2960 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2961 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2964 am->l2_input_classify_next_acl_ip4 =
2965 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2966 am->l2_input_classify_next_acl_ip6 =
2967 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2969 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2970 l2input_get_feat_names (),
2971 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2973 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2974 l2input_get_feat_names (),
2975 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2978 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2979 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2980 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2982 am->l2_output_classify_next_acl_ip4 =
2983 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2984 am->l2_output_classify_next_acl_ip6 =
2985 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2987 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2988 l2output_get_feat_names (),
2989 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2991 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2992 l2output_get_feat_names (),
2993 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2997 acl_set_timeout_sec (int timeout_type, u32 value)
2999 acl_main_t *am = &acl_main;
3000 clib_time_t *ct = &am->vlib_main->clib_time;
3002 if (timeout_type < ACL_N_TIMEOUTS)
3004 am->session_timeout_sec[timeout_type] = value;
3008 clib_warning ("Unknown timeout type %d", timeout_type);
3011 am->session_timeout[timeout_type] =
3012 (u64) (((f64) value) / ct->seconds_per_clock);
3016 acl_set_session_max_entries (u32 value)
3018 acl_main_t *am = &acl_main;
3019 am->fa_conn_table_max_entries = value;
3023 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3025 acl_main_t *am = &acl_main;
3027 if ((eh < 256) && (value < 2))
3029 am->fa_ipv6_known_eh_bitmap =
3030 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3038 static clib_error_t *
3039 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3041 acl_main_t *am = &acl_main;
3042 if (0 == am->acl_mheap)
3044 /* ACL heap is not initialized, so definitely nothing to do. */
3049 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3050 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3052 /* also unapply any ACLs in case the users did not do so. */
3053 macip_acl_interface_del_acl (am, sw_if_index);
3054 acl_interface_reset_inout_acls (sw_if_index, 0);
3055 acl_interface_reset_inout_acls (sw_if_index, 1);
3060 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3064 static clib_error_t *
3065 acl_set_aclplugin_fn (vlib_main_t * vm,
3066 unformat_input_t * input, vlib_cli_command_t * cmd)
3068 clib_error_t *error = 0;
3072 uword memory_size = 0;
3073 acl_main_t *am = &acl_main;
3075 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3077 if (!acl_set_skip_ipv6_eh (eh_val, val))
3079 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3083 if (unformat (input, "use-hash-acl-matching %u", &val))
3085 am->use_hash_acl_matching = (val != 0);
3088 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3090 am->l4_match_nonfirst_fragment = (val != 0);
3093 if (unformat (input, "heap"))
3095 if (unformat (input, "main"))
3097 if (unformat (input, "validate %u", &val))
3098 acl_plugin_acl_set_validate_heap (am, val);
3099 else if (unformat (input, "trace %u", &val))
3100 acl_plugin_acl_set_trace_heap (am, val);
3103 else if (unformat (input, "hash"))
3105 if (unformat (input, "validate %u", &val))
3106 acl_plugin_hash_acl_set_validate_heap (am, val);
3107 else if (unformat (input, "trace %u", &val))
3108 acl_plugin_hash_acl_set_trace_heap (am, val);
3113 if (unformat (input, "session"))
3115 if (unformat (input, "table"))
3117 /* The commands here are for tuning/testing. No user-serviceable parts inside */
3118 if (unformat (input, "max-entries"))
3120 if (!unformat (input, "%u", &val))
3122 error = clib_error_return (0,
3123 "expecting maximum number of entries, got `%U`",
3124 format_unformat_error, input);
3129 acl_set_session_max_entries (val);
3133 if (unformat (input, "hash-table-buckets"))
3135 if (!unformat (input, "%u", &val))
3137 error = clib_error_return (0,
3138 "expecting maximum number of hash table buckets, got `%U`",
3139 format_unformat_error, input);
3144 am->fa_conn_table_hash_num_buckets = val;
3148 if (unformat (input, "hash-table-memory"))
3150 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3152 error = clib_error_return (0,
3153 "expecting maximum amount of hash table memory, got `%U`",
3154 format_unformat_error, input);
3159 am->fa_conn_table_hash_memory_size = memory_size;
3163 if (unformat (input, "event-trace"))
3165 if (!unformat (input, "%u", &val))
3167 error = clib_error_return (0,
3168 "expecting trace level, got `%U`",
3169 format_unformat_error, input);
3174 am->trace_sessions = val;
3180 if (unformat (input, "timeout"))
3182 if (unformat (input, "udp"))
3184 if (unformat (input, "idle"))
3186 if (!unformat (input, "%u", &timeout))
3188 error = clib_error_return (0,
3189 "expecting timeout value in seconds, got `%U`",
3190 format_unformat_error,
3196 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3201 if (unformat (input, "tcp"))
3203 if (unformat (input, "idle"))
3205 if (!unformat (input, "%u", &timeout))
3207 error = clib_error_return (0,
3208 "expecting timeout value in seconds, got `%U`",
3209 format_unformat_error,
3215 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3219 if (unformat (input, "transient"))
3221 if (!unformat (input, "%u", &timeout))
3223 error = clib_error_return (0,
3224 "expecting timeout value in seconds, got `%U`",
3225 format_unformat_error,
3231 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3245 my_format_mac_address (u8 * s, va_list * args)
3247 u8 *a = va_arg (*args, u8 *);
3248 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3249 a[0], a[1], a[2], a[3], a[4], a[5]);
3253 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3255 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3257 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3258 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3259 format_ip46_address, &a->src_ip_addr,
3260 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3262 my_format_mac_address, a->src_mac,
3263 my_format_mac_address, a->src_mac_mask);
3268 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3270 vlib_main_t *vm = am->vlib_main;
3273 /* Don't try to print someone else's memory */
3274 if (macip_acl_index > vec_len (am->macip_acls))
3277 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3278 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3280 vlib_cli_output (vm,
3281 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3282 macip_acl_index, a->count, vec_len (a->rules), a->tag,
3284 vlib_cli_output (vm,
3285 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3286 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3287 vlib_cli_output (vm,
3288 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3289 a->out_ip4_table_index, a->out_ip6_table_index,
3290 a->out_l2_table_index);
3291 for (i = 0; i < vec_len (a->rules); i++)
3292 vlib_cli_output (vm, " rule %d: %U\n", i,
3293 my_macip_acl_rule_t_pretty_format,
3294 vec_elt_at_index (a->rules, i));
3298 static clib_error_t *
3299 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3301 input, vlib_cli_command_t * cmd)
3303 clib_error_t *error = 0;
3304 acl_main_t *am = &acl_main;
3308 (void) unformat (input, "index %u", &acl_index);
3310 for (i = 0; i < vec_len (am->macip_acls); i++)
3312 /* Don't attempt to show the ACLs that do not exist */
3313 if (pool_is_free_index (am->macip_acls, i))
3316 if ((acl_index != ~0) && (acl_index != i))
3321 macip_acl_print (am, i);
3322 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3324 vlib_cli_output (vm, " applied on sw_if_index: %d\n",
3325 vec_elt (am->sw_if_index_vec_by_macip_acl, i));
3332 static clib_error_t *
3333 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3335 input, vlib_cli_command_t * cmd)
3337 clib_error_t *error = 0;
3338 acl_main_t *am = &acl_main;
3340 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3342 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3343 vec_elt (am->macip_acl_by_sw_if_index, i));
3348 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
3350 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
3353 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
3354 am->acls[acl_index].count, am->acls[acl_index].tag);
3356 PRINT_AND_RESET (vm, out0);
3357 for (j = 0; j < am->acls[acl_index].count; j++)
3359 r = &am->acls[acl_index].rules[j];
3360 out0 = format (out0, " %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
3361 out0 = format_acl_action (out0, r->is_permit);
3362 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
3363 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3366 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
3367 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
3368 out0 = format (out0, " proto %d", r->proto);
3369 out0 = format (out0, " sport %d", r->src_port_or_type_first);
3370 if (r->src_port_or_type_first != r->src_port_or_type_last)
3372 out0 = format (out0, "-%d", r->src_port_or_type_last);
3374 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
3375 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
3377 out0 = format (out0, "-%d", r->dst_port_or_code_last);
3379 if (r->tcp_flags_mask || r->tcp_flags_value)
3382 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
3385 out0 = format (out0, "\n");
3386 PRINT_AND_RESET (vm, out0);
3390 #undef PRINT_AND_RESET
3393 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3396 vlib_main_t *vm = am->vlib_main;
3398 for (i = 0; i < vec_len (am->acls); i++)
3400 if (acl_is_not_defined (am, i))
3402 /* don't attempt to show the ACLs that do not exist */
3405 if ((acl_index != ~0) && (acl_index != i))
3409 acl_print_acl (vm, am, i);
3411 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3413 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3414 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3417 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3419 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3420 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3426 static clib_error_t *
3427 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3428 unformat_input_t * input, vlib_cli_command_t * cmd)
3430 clib_error_t *error = 0;
3431 acl_main_t *am = &acl_main;
3434 (void) unformat (input, "index %u", &acl_index);
3436 acl_plugin_show_acl (am, acl_index);
3441 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
3443 vlib_main_t *vm = am->vlib_main;
3446 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3447 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3449 /* if we need a particular interface, skip all the others */
3450 if ((sw_if_index != ~0) && (sw_if_index != swi))
3453 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3455 if (intf_has_etype_whitelist (am, swi, 1))
3457 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3458 am->input_etype_whitelist_by_sw_if_index[swi],
3461 if (intf_has_etype_whitelist (am, swi, 0))
3463 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3464 am->output_etype_whitelist_by_sw_if_index[swi],
3468 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3469 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3471 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3472 am->input_acl_vec_by_sw_if_index[swi], "%d");
3475 vlib_cli_output (vm, "\n");
3476 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3478 acl_print_acl (vm, am, *pj);
3480 vlib_cli_output (vm, "\n");
3484 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3485 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3487 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3488 am->output_acl_vec_by_sw_if_index[swi], "%d");
3491 vlib_cli_output (vm, "\n");
3492 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3494 acl_print_acl (vm, am, *pj);
3496 vlib_cli_output (vm, "\n");
3504 static clib_error_t *
3505 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3506 unformat_input_t * input,
3507 vlib_cli_command_t * cmd)
3509 clib_error_t *error = 0;
3510 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3513 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3514 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3515 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3516 format_acl_plugin_5tuple, five_tuple);
3518 error = clib_error_return (0, "expecting 6 hex integers");
3523 static clib_error_t *
3524 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3526 input, vlib_cli_command_t * cmd)
3528 clib_error_t *error = 0;
3529 acl_main_t *am = &acl_main;
3531 u32 sw_if_index = ~0;
3532 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3533 int show_acl = unformat (input, "acl");
3535 acl_plugin_show_interface (am, sw_if_index, show_acl);
3539 static clib_error_t *
3540 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3541 unformat_input_t * input,
3542 vlib_cli_command_t * cmd)
3544 clib_error_t *error = 0;
3545 acl_main_t *am = &acl_main;
3547 vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3550 vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3554 vlib_cli_output (vm, " Not initialized\n");
3556 vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3557 if (am->hash_lookup_mheap)
3559 vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3563 vlib_cli_output (vm, " Not initialized\n");
3569 acl_plugin_show_sessions (acl_main_t * am,
3570 u32 show_session_thread_id,
3571 u32 show_session_session_index)
3573 vlib_main_t *vm = am->vlib_main;
3575 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3576 vnet_sw_interface_t *swif;
3579 u64 n_adds = am->fa_session_total_adds;
3580 u64 n_dels = am->fa_session_total_dels;
3581 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3582 n_dels, n_adds - n_dels);
3584 vlib_cli_output (vm, "\n\nPer-thread data:");
3585 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3587 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3588 vlib_cli_output (vm, "Thread #%d:", wk);
3589 if (show_session_thread_id == wk
3590 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3592 vlib_cli_output (vm, " session index %u:",
3593 show_session_session_index);
3594 fa_session_t *sess =
3595 pw->fa_sessions_pool + show_session_session_index;
3596 u64 *m = (u64 *) & sess->info;
3597 vlib_cli_output (vm,
3598 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3599 m[0], m[1], m[2], m[3], m[4], m[5]);
3600 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3601 vlib_cli_output (vm, " tcp_flags_seen: %x",
3602 sess->tcp_flags_seen.as_u16);
3603 vlib_cli_output (vm, " last active time: %lu",
3604 sess->last_active_time);
3605 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3606 vlib_cli_output (vm, " link enqueue time: %lu",
3607 sess->link_enqueue_time);
3608 vlib_cli_output (vm, " link next index: %u",
3609 sess->link_next_idx);
3610 vlib_cli_output (vm, " link prev index: %u",
3611 sess->link_prev_idx);
3612 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3614 vlib_cli_output (vm, " connection add/del stats:", wk);
3615 pool_foreach (swif, im->sw_interfaces, (
3622 (pw->fa_session_adds_by_sw_if_index)
3624 pw->fa_session_adds_by_sw_if_index
3629 (pw->fa_session_dels_by_sw_if_index)
3631 pw->fa_session_dels_by_sw_if_index
3633 vlib_cli_output (vm,
3634 " sw_if_index %d: add %lu - del %lu = %lu",
3643 vlib_cli_output (vm, " connection timeout type lists:", wk);
3645 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3647 u32 head_session_index = pw->fa_conn_list_head[tt];
3648 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3649 head_session_index);
3650 if (~0 != head_session_index)
3652 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3653 vlib_cli_output (vm, " last active time: %lu",
3654 sess->last_active_time);
3655 vlib_cli_output (vm, " link enqueue time: %lu",
3656 sess->link_enqueue_time);
3660 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3661 vlib_cli_output (vm, " Requeue until time: %lu",
3662 pw->requeue_until_time);
3663 vlib_cli_output (vm, " Current time wait interval: %lu",
3664 pw->current_time_wait_interval);
3665 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3666 pw->cnt_deleted_sessions);
3667 vlib_cli_output (vm, " Delete already deleted: %lu",
3668 pw->cnt_already_deleted_sessions);
3669 vlib_cli_output (vm, " Session timers restarted: %lu",
3670 pw->cnt_session_timer_restarted);
3671 vlib_cli_output (vm, " Swipe until this time: %lu",
3672 pw->swipe_end_time);
3673 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3674 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3675 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3677 pw->pending_clear_sw_if_index_bitmap);
3678 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3679 vlib_cli_output (vm, " interrupt is pending: %d",
3680 pw->interrupt_is_pending);
3681 vlib_cli_output (vm, " interrupt is needed: %d",
3682 pw->interrupt_is_needed);
3683 vlib_cli_output (vm, " interrupt is unwanted: %d",
3684 pw->interrupt_is_unwanted);
3685 vlib_cli_output (vm, " interrupt generation: %d",
3686 pw->interrupt_generation);
3688 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3689 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3690 foreach_fa_cleaner_counter;
3692 vlib_cli_output (vm, "Interrupt generation: %d",
3693 am->fa_interrupt_generation);
3694 vlib_cli_output (vm,
3695 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3696 am->fa_min_deleted_sessions_per_interval,
3697 am->fa_max_deleted_sessions_per_interval,
3698 am->fa_cleaner_wait_time_increment * 1000.0,
3699 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3700 1000.0 / (f64) vm->clib_time.clocks_per_second);
3703 static clib_error_t *
3704 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3705 unformat_input_t * input,
3706 vlib_cli_command_t * cmd)
3708 clib_error_t *error = 0;
3709 acl_main_t *am = &acl_main;
3711 u32 show_bihash_verbose = 0;
3712 u32 show_session_thread_id = ~0;
3713 u32 show_session_session_index = ~0;
3714 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3715 &show_session_session_index);
3716 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3718 acl_plugin_show_sessions (am, show_session_thread_id,
3719 show_session_session_index);
3720 show_fa_sessions_hash (vm, show_bihash_verbose);
3725 acl_plugin_show_tables_mask_type (acl_main_t * am)
3727 vlib_main_t *vm = am->vlib_main;
3728 ace_mask_type_entry_t *mte;
3730 vlib_cli_output (vm, "Mask-type entries:");
3732 pool_foreach(mte, am->ace_mask_type_pool,
3734 vlib_cli_output(vm, " %3d: %016llx %016llx %016llx %016llx %016llx %016llx refcount %d",
3735 mte - am->ace_mask_type_pool,
3736 mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3737 mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3743 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3745 vlib_main_t *vm = am->vlib_main;
3748 vlib_cli_output (vm, "Mask-ready ACL representations\n");
3749 for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3751 if ((acl_index != ~0) && (acl_index != i))
3755 hash_acl_info_t *ha = &am->hash_acl_infos[i];
3756 vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3757 vlib_cli_output (vm, " applied inbound on sw_if_index list: %U\n",
3758 format_vec32, ha->inbound_sw_if_index_list, "%d");
3759 vlib_cli_output (vm, " applied outbound on sw_if_index list: %U\n",
3760 format_vec32, ha->outbound_sw_if_index_list, "%d");
3761 vlib_cli_output (vm, " mask type index bitmap: %U\n",
3762 format_bitmap_hex, ha->mask_type_index_bitmap);
3763 for (j = 0; j < vec_len (ha->rules); j++)
3765 hash_ace_info_t *pa = &ha->rules[j];
3766 m = (u64 *) & pa->match;
3767 vlib_cli_output (vm,
3768 " %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3769 j, m[0], m[1], m[2], m[3], m[4], m[5],
3770 pa->mask_type_index, pa->acl_index, pa->ace_index,
3771 pa->action, pa->src_portrange_not_powerof2,
3772 pa->dst_portrange_not_powerof2);
3778 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3780 vlib_cli_output (vm,
3781 " %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3782 j, pae->acl_index, pae->ace_index, pae->action,
3783 pae->hash_ace_info_index, pae->next_applied_entry_index,
3784 pae->prev_applied_entry_index,
3785 pae->tail_applied_entry_index, pae->hitcount);
3789 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3791 vlib_main_t *vm = am->vlib_main;
3793 vlib_cli_output (vm, "Applied lookup entries for interfaces");
3796 (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3797 || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3798 || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3799 || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3801 if ((sw_if_index != ~0) && (sw_if_index != swi))
3805 vlib_cli_output (vm, "sw_if_index %d:", swi);
3806 if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3808 applied_hash_acl_info_t *pal =
3809 &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3810 vlib_cli_output (vm, " input lookup mask_type_index_bitmap: %U",
3811 format_bitmap_hex, pal->mask_type_index_bitmap);
3812 vlib_cli_output (vm, " input applied acls: %U", format_vec32,
3813 pal->applied_acls, "%d");
3815 if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3817 vlib_cli_output (vm, " input lookup applied entries:");
3819 j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3822 acl_plugin_print_pae (vm, j,
3823 &am->input_hash_entry_vec_by_sw_if_index
3828 if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3830 applied_hash_acl_info_t *pal =
3831 &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3832 vlib_cli_output (vm, " output lookup mask_type_index_bitmap: %U",
3833 format_bitmap_hex, pal->mask_type_index_bitmap);
3834 vlib_cli_output (vm, " output applied acls: %U", format_vec32,
3835 pal->applied_acls, "%d");
3837 if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3839 vlib_cli_output (vm, " output lookup applied entries:");
3841 j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3844 acl_plugin_print_pae (vm, j,
3845 &am->output_hash_entry_vec_by_sw_if_index
3853 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3855 vlib_main_t *vm = am->vlib_main;
3856 show_hash_acl_hash (vm, am, show_bihash_verbose);
3859 static clib_error_t *
3860 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3861 unformat_input_t * input,
3862 vlib_cli_command_t * cmd)
3864 clib_error_t *error = 0;
3865 acl_main_t *am = &acl_main;
3868 u32 sw_if_index = ~0;
3869 int show_acl_hash_info = 0;
3870 int show_applied_info = 0;
3871 int show_mask_type = 0;
3872 int show_bihash = 0;
3873 u32 show_bihash_verbose = 0;
3875 if (unformat (input, "acl"))
3877 show_acl_hash_info = 1;
3878 /* mask-type is handy to see as well right there */
3880 unformat (input, "index %u", &acl_index);
3882 else if (unformat (input, "applied"))
3884 show_applied_info = 1;
3885 unformat (input, "sw_if_index %u", &sw_if_index);
3887 else if (unformat (input, "mask"))
3891 else if (unformat (input, "hash"))
3894 unformat (input, "verbose %u", &show_bihash_verbose);
3898 (show_mask_type || show_acl_hash_info || show_applied_info
3901 /* if no qualifiers specified, show all */
3903 show_acl_hash_info = 1;
3904 show_applied_info = 1;
3908 acl_plugin_show_tables_mask_type (am);
3909 if (show_acl_hash_info)
3910 acl_plugin_show_tables_acl_hash_info (am, acl_index);
3911 if (show_applied_info)
3912 acl_plugin_show_tables_applied_info (am, sw_if_index);
3914 acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3919 static clib_error_t *
3920 acl_clear_aclplugin_fn (vlib_main_t * vm,
3921 unformat_input_t * input, vlib_cli_command_t * cmd)
3923 clib_error_t *error = 0;
3924 acl_main_t *am = &acl_main;
3925 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3926 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3931 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3932 .path = "set acl-plugin",
3933 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3934 .function = acl_set_aclplugin_fn,
3937 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3938 .path = "show acl-plugin acl",
3939 .short_help = "show acl-plugin acl [index N]",
3940 .function = acl_show_aclplugin_acl_fn,
3943 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3944 .path = "show acl-plugin decode 5tuple",
3945 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3946 .function = acl_show_aclplugin_decode_5tuple_fn,
3949 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3950 .path = "show acl-plugin interface",
3951 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3952 .function = acl_show_aclplugin_interface_fn,
3955 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3956 .path = "show acl-plugin memory",
3957 .short_help = "show acl-plugin memory",
3958 .function = acl_show_aclplugin_memory_fn,
3961 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3962 .path = "show acl-plugin sessions",
3963 .short_help = "show acl-plugin sessions",
3964 .function = acl_show_aclplugin_sessions_fn,
3967 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3968 .path = "show acl-plugin tables",
3969 .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3970 .function = acl_show_aclplugin_tables_fn,
3973 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3974 .path = "show acl-plugin macip acl",
3975 .short_help = "show acl-plugin macip acl [index N]",
3976 .function = acl_show_aclplugin_macip_acl_fn,
3979 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3980 .path = "show acl-plugin macip interface",
3981 .short_help = "show acl-plugin macip interface",
3982 .function = acl_show_aclplugin_macip_interface_fn,
3985 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3986 .path = "clear acl-plugin sessions",
3987 .short_help = "clear acl-plugin sessions",
3988 .function = acl_clear_aclplugin_fn,
3992 static clib_error_t *
3993 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3995 acl_main_t *am = &acl_main;
3996 u32 conn_table_hash_buckets;
3997 u32 conn_table_hash_memory_size;
3998 u32 conn_table_max_entries;
4001 u32 hash_lookup_hash_buckets;
4002 u32 hash_lookup_hash_memory;
4004 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4007 (input, "connection hash buckets %d", &conn_table_hash_buckets))
4008 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
4009 else if (unformat (input, "connection hash memory %d",
4010 &conn_table_hash_memory_size))
4011 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
4012 else if (unformat (input, "connection count max %d",
4013 &conn_table_max_entries))
4014 am->fa_conn_table_max_entries = conn_table_max_entries;
4015 else if (unformat (input, "main heap size %d", &main_heap_size))
4016 am->acl_mheap_size = main_heap_size;
4017 else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
4018 am->hash_lookup_mheap_size = hash_heap_size;
4019 else if (unformat (input, "hash lookup hash buckets %d",
4020 &hash_lookup_hash_buckets))
4021 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
4022 else if (unformat (input, "hash lookup hash memory %d",
4023 &hash_lookup_hash_memory))
4024 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4026 return clib_error_return (0, "unknown input '%U'",
4027 format_unformat_error, input);
4032 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4034 static clib_error_t *
4035 acl_init (vlib_main_t * vm)
4037 acl_main_t *am = &acl_main;
4038 clib_error_t *error = 0;
4039 memset (am, 0, sizeof (*am));
4041 am->vnet_main = vnet_get_main ();
4043 u8 *name = format (0, "acl_%08x%c", api_version, 0);
4045 /* Ask for a correctly-sized block of API message decode slots */
4046 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4047 VL_MSG_FIRST_AVAILABLE);
4049 error = acl_plugin_api_hookup (vm);
4051 /* Add our API messages to the global name_crc hash table */
4052 setup_message_id_table (am, &api_main);
4056 acl_setup_fa_nodes ();
4058 am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
4059 am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4061 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4062 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4064 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4065 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4066 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4067 TCP_SESSION_IDLE_TIMEOUT_SEC;
4068 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4069 UDP_SESSION_IDLE_TIMEOUT_SEC;
4071 am->fa_conn_table_hash_num_buckets =
4072 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4073 am->fa_conn_table_hash_memory_size =
4074 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4075 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4076 vlib_thread_main_t *tm = vlib_get_thread_main ();
4077 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4081 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4083 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4084 vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4085 vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4086 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4088 pw->fa_conn_list_head[tt] = ~0;
4089 pw->fa_conn_list_tail[tt] = ~0;
4094 am->fa_min_deleted_sessions_per_interval =
4095 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4096 am->fa_max_deleted_sessions_per_interval =
4097 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4098 am->fa_cleaner_wait_time_increment =
4099 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4101 am->fa_cleaner_cnt_delete_by_sw_index = 0;
4102 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4103 am->fa_cleaner_cnt_unknown_event = 0;
4104 am->fa_cleaner_cnt_timer_restarted = 0;
4105 am->fa_cleaner_cnt_wait_with_timeout = 0;
4108 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4111 am->l4_match_nonfirst_fragment = 1;
4113 /* use the new fancy hash-based matching */
4114 am->use_hash_acl_matching = 1;
4119 VLIB_INIT_FUNCTION (acl_init);
4123 * fd.io coding-style-patch-verification: ON
4126 * eval: (c-set-style "gnu")