2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/input_acl.h>
24 #include <vpp/app/version.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <acl/acl_msg_enum.h>
33 /* define message structures */
35 #include <acl/acl_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <acl/acl_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <acl/acl_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.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_DEL, macip_acl_del) \
74 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
75 _(MACIP_ACL_DUMP, macip_acl_dump) \
76 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
79 VLIB_PLUGIN_REGISTER () = {
80 .version = VPP_BUILD_VER,
81 .description = "Access Control Lists",
86 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
88 acl_main_t *am = &acl_main;
89 vl_api_acl_plugin_get_version_reply_t *rmp;
90 int msg_size = sizeof (*rmp);
91 unix_shared_memory_queue_t *q;
93 q = vl_api_client_index_to_input_queue (mp->client_index);
99 rmp = vl_msg_api_alloc (msg_size);
100 memset (rmp, 0, msg_size);
102 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
103 rmp->context = mp->context;
104 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
105 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
107 vl_msg_api_send_shmem (q, (u8 *) & rmp);
111 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
113 vl_api_acl_plugin_control_ping_reply_t *rmp;
114 acl_main_t *am = &acl_main;
118 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
120 rmp->vpe_pid = ntohl (getpid ());
126 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
127 u32 * acl_list_index, u8 * tag)
129 acl_main_t *am = &acl_main;
132 acl_rule_t *acl_new_rules;
135 if (*acl_list_index != ~0)
137 /* They supplied some number, let's see if this ACL exists */
138 if (pool_is_free_index (am->acls, *acl_list_index))
140 /* tried to replace a non-existent ACL, no point doing anything */
145 /* Create and populate the rules */
146 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
147 CLIB_CACHE_LINE_BYTES);
150 /* Could not allocate rules. New or existing ACL - bail out regardless */
154 for (i = 0; i < count; i++)
156 r = &acl_new_rules[i];
157 r->is_permit = rules[i].is_permit;
158 r->is_ipv6 = rules[i].is_ipv6;
161 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
162 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
166 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
167 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
169 r->src_prefixlen = rules[i].src_ip_prefix_len;
170 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
171 r->proto = rules[i].proto;
172 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
173 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
174 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
175 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
176 r->tcp_flags_value = rules[i].tcp_flags_value;
177 r->tcp_flags_mask = rules[i].tcp_flags_mask;
180 if (~0 == *acl_list_index)
183 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
184 memset (a, 0, sizeof (*a));
185 /* Will return the newly allocated ACL index */
186 *acl_list_index = a - am->acls;
190 a = am->acls + *acl_list_index;
191 /* Get rid of the old rules */
192 clib_mem_free (a->rules);
194 a->rules = acl_new_rules;
196 memcpy (a->tag, tag, sizeof (a->tag));
202 acl_del_list (u32 acl_list_index)
204 acl_main_t *am = &acl_main;
207 if (pool_is_free_index (am->acls, acl_list_index))
212 /* delete any references to the ACL */
213 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
215 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
218 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
220 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
228 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
230 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
233 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
235 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
244 /* now we can delete the ACL itself */
245 a = &am->acls[acl_list_index];
248 clib_mem_free (a->rules);
250 pool_put (am->acls, a);
254 /* Some aids in ASCII graphing the content */
260 u8 ip4_5tuple_mask[] =
261 _(" dmac smac etype ")
262 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
269 _(" ttl pr checksum ")
278 _("L4 T/U sport dport ")
288 u8 ip6_5tuple_mask[] =
289 _(" dmac smac etype ")
290 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
292 _(0x0000) __ __ __ __
294 _(0x0004) __ __ XX __
296 _(0x0008) XX XX XX XX
297 _(0x000C) XX XX XX XX
298 _(0x0010) XX XX XX XX
299 _(0x0014) XX XX XX XX
301 _(0x0018) XX XX XX XX
302 _(0x001C) XX XX XX XX
303 _(0x0020) XX XX XX XX
304 _(0x0024) XX XX XX XX
305 _("L4T/U sport dport ")
306 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
313 static int count_skip (u8 * p, u32 size)
315 u64 *p64 = (u64 *) p;
316 /* Be tolerant to null pointer */
320 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
324 return (p64 - (u64 *) p) / 2;
328 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
329 u32 mask_len, u32 next_table_index,
330 u32 miss_next_index, u32 * table_index,
334 u32 memory_size = 2 << 13;
335 u32 skip = count_skip (mask, mask_len);
336 u32 match = (mask_len / 16) - skip;
337 u8 *skip_mask_ptr = mask + 16 * skip;
338 u32 current_data_flag = 0;
339 int current_data_offset = 0;
344 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
345 memory_size, skip, match,
346 next_table_index, miss_next_index,
347 table_index, current_data_flag,
348 current_data_offset, is_add,
349 1 /* delete_chain */);
353 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
354 u32 mask_len, u32 next_table_index,
355 u32 miss_next_index, u32 * table_index,
359 u32 memory_size = 2 << 20;
360 u32 skip = count_skip (mask, mask_len);
361 u32 match = (mask_len / 16) - skip;
362 u8 *skip_mask_ptr = mask + 16 * skip;
363 u32 current_data_flag = 0;
364 int current_data_offset = 0;
369 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
370 memory_size, skip, match,
371 next_table_index, miss_next_index,
372 table_index, current_data_flag,
373 current_data_offset, is_add,
374 1 /* delete_chain */);
379 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
381 vnet_classify_main_t *cm = &vnet_classify_main;
382 u32 ip4_table_index = ~0;
383 u32 ip6_table_index = ~0;
385 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
387 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
390 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
392 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
395 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
396 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
397 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
398 sizeof (ip4_5tuple_mask) - 1, ~0,
399 am->l2_input_classify_next_acl_ip4,
400 &ip4_table_index, 0);
402 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
405 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
406 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
407 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
408 sizeof (ip6_5tuple_mask) - 1, ~0,
409 am->l2_input_classify_next_acl_ip6,
410 &ip6_table_index, 0);
417 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
419 vnet_classify_main_t *cm = &vnet_classify_main;
420 u32 ip4_table_index = ~0;
421 u32 ip6_table_index = ~0;
423 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
425 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
428 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
430 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
433 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
434 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
435 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
436 sizeof (ip4_5tuple_mask) - 1, ~0,
437 am->l2_output_classify_next_acl_ip4,
438 &ip4_table_index, 0);
440 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
443 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
444 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
445 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
446 sizeof (ip6_5tuple_mask) - 1, ~0,
447 am->l2_output_classify_next_acl_ip6,
448 &ip6_table_index, 0);
455 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
457 vnet_classify_main_t *cm = &vnet_classify_main;
458 u32 ip4_table_index = ~0;
459 u32 ip6_table_index = ~0;
462 /* in case there were previous tables attached */
463 acl_unhook_l2_input_classify (am, sw_if_index);
465 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
466 sizeof (ip4_5tuple_mask) - 1, ~0,
467 am->l2_input_classify_next_acl_ip4,
468 &ip4_table_index, 1);
472 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
473 sizeof (ip6_5tuple_mask) - 1, ~0,
474 am->l2_input_classify_next_acl_ip6,
475 &ip6_table_index, 1);
478 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
479 sizeof (ip4_5tuple_mask) - 1, ~0,
480 am->l2_input_classify_next_acl_ip4,
481 &ip4_table_index, 0);
485 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
486 ip6_table_index, ~0);
488 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
489 sw_if_index, ip4_table_index, ip6_table_index);
492 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
493 sizeof (ip6_5tuple_mask) - 1, ~0,
494 am->l2_input_classify_next_acl_ip6,
495 &ip6_table_index, 0);
496 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
497 sizeof (ip4_5tuple_mask) - 1, ~0,
498 am->l2_input_classify_next_acl_ip4,
499 &ip4_table_index, 0);
503 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
505 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
508 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
513 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
515 vnet_classify_main_t *cm = &vnet_classify_main;
516 u32 ip4_table_index = ~0;
517 u32 ip6_table_index = ~0;
520 /* in case there were previous tables attached */
521 acl_unhook_l2_output_classify (am, sw_if_index);
523 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
524 sizeof (ip4_5tuple_mask) - 1, ~0,
525 am->l2_output_classify_next_acl_ip4,
526 &ip4_table_index, 1);
530 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
531 sizeof (ip6_5tuple_mask) - 1, ~0,
532 am->l2_output_classify_next_acl_ip6,
533 &ip6_table_index, 1);
536 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
537 sizeof (ip4_5tuple_mask) - 1, ~0,
538 am->l2_output_classify_next_acl_ip4,
539 &ip4_table_index, 0);
543 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
544 ip6_table_index, ~0);
546 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
547 sw_if_index, ip4_table_index, ip6_table_index);
550 acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
551 sizeof (ip6_5tuple_mask) - 1, ~0,
552 am->l2_output_classify_next_acl_ip6,
553 &ip6_table_index, 0);
554 acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
555 sizeof (ip4_5tuple_mask) - 1, ~0,
556 am->l2_output_classify_next_acl_ip4,
557 &ip4_table_index, 0);
561 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
563 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
566 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
573 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
579 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
581 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
583 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
587 rv = acl_hook_l2_input_classify (am, sw_if_index);
591 rv = acl_unhook_l2_input_classify (am, sw_if_index);
598 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
604 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
606 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
608 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
612 rv = acl_hook_l2_output_classify (am, sw_if_index);
616 rv = acl_unhook_l2_output_classify (am, sw_if_index);
624 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
626 acl_main_t *am = &acl_main;
629 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
630 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
632 acl_interface_in_enable_disable (am, sw_if_index, 1);
636 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
637 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
639 acl_interface_out_enable_disable (am, sw_if_index, 1);
645 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
647 acl_main_t *am = &acl_main;
652 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
653 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
656 if (acl_list_index ==
657 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
659 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
664 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
666 acl_interface_in_enable_disable (am, sw_if_index, 0);
671 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
673 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
675 if (acl_list_index ==
676 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
678 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
683 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
685 acl_interface_out_enable_disable (am, sw_if_index, 0);
692 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
694 acl_main_t *am = &acl_main;
697 acl_interface_in_enable_disable (am, sw_if_index, 0);
698 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
699 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
703 acl_interface_out_enable_disable (am, sw_if_index, 0);
704 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
705 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
710 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
717 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
722 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
736 } macip_match_type_t;
739 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
745 for (i = 0; i < vec_len (mv); i++)
747 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
748 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
758 /* Get metric used to sort match types.
759 The more specific and the more often seen - the bigger the metric */
761 match_type_metric (macip_match_type_t * m)
763 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
764 return m->prefix_len + m->is_ipv6 + 10 * m->count;
768 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
770 /* Ascending sort based on the metric values */
771 return match_type_metric (m1) - match_type_metric (m2);
774 /* Get the offset of L3 source within ethernet packet */
776 get_l3_src_offset(int is6)
779 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
781 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
785 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
787 macip_match_type_t *mvec = NULL;
788 macip_match_type_t *mt;
789 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
791 u32 match_type_index;
794 vnet_classify_main_t *cm = &vnet_classify_main;
796 /* Count the number of different types of rules */
797 for (i = 0; i < a->count; i++)
801 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
802 a->rules[i].src_prefixlen,
803 a->rules[i].is_ipv6)))
805 match_type_index = vec_len (mvec);
806 vec_validate (mvec, match_type_index);
807 memcpy (mvec[match_type_index].mac_mask,
808 a->rules[match_type_index].src_mac_mask, 6);
809 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
810 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
811 mvec[match_type_index].table_index = ~0;
813 mvec[match_type_index].count++;
815 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
816 vec_sort_with_function (mvec, match_type_compare);
817 /* Create the classifier tables */
819 /* First add ARP tables */
820 vec_foreach (mt, mvec)
823 int is6 = mt->is_ipv6;
825 mt->arp_table_index = ~0;
828 memset (mask, 0, sizeof (mask));
829 memcpy (&mask[6], mt->mac_mask, 6);
830 memset (&mask[12], 0xff, 2); /* ethernet protocol */
831 memcpy (&mask[14 + 8], mt->mac_mask, 6);
833 for (i = 0; i < (mt->prefix_len / 8); i++)
834 mask[14 + 14 + i] = 0xff;
835 if (mt->prefix_len % 8)
836 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
838 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
839 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
840 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
841 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
843 last_table = mt->arp_table_index;
846 /* Now add IP[46] tables */
847 vec_foreach (mt, mvec)
850 int is6 = mt->is_ipv6;
851 int l3_src_offs = get_l3_src_offset(is6);
852 memset (mask, 0, sizeof (mask));
853 memcpy (&mask[6], mt->mac_mask, 6);
854 for (i = 0; i < (mt->prefix_len / 8); i++)
856 mask[l3_src_offs + i] = 0xff;
858 if (mt->prefix_len % 8)
860 mask[l3_src_offs + (mt->prefix_len / 8)] =
861 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
864 * Round-up the number of bytes needed to store the prefix,
865 * and round up the number of vectors too
867 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
868 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
869 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
870 (~0 == last_table) ? 0 : ~0, &mt->table_index,
872 last_table = mt->table_index;
874 a->ip4_table_index = ~0;
875 a->ip6_table_index = ~0;
876 a->l2_table_index = last_table;
878 /* Populate the classifier tables with rules from the MACIP ACL */
879 for (i = 0; i < a->count; i++)
883 int is6 = a->rules[i].is_ipv6;
884 int l3_src_offs = get_l3_src_offset(is6);
885 memset (mask, 0, sizeof (mask));
886 memcpy (&mask[6], a->rules[i].src_mac, 6);
887 memset (&mask[12], 0xff, 2); /* ethernet protocol */
890 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
896 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
901 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
902 a->rules[i].src_prefixlen,
903 a->rules[i].is_ipv6);
904 /* add session to table mvec[match_type_index].table_index; */
905 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
906 mask, a->rules[i].is_permit ? ~0 : 0, i,
907 0, action, metadata, 1);
908 /* add ARP table entry too */
909 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
911 memset (mask, 0, sizeof (mask));
912 memcpy (&mask[6], a->rules[i].src_mac, 6);
915 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
916 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
917 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
918 mask, a->rules[i].is_permit ? ~0 : 0, i,
919 0, action, metadata, 1);
926 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
928 vnet_classify_main_t *cm = &vnet_classify_main;
929 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
931 if (a->ip4_table_index != ~0)
933 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
934 a->ip4_table_index = ~0;
936 if (a->ip6_table_index != ~0)
938 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
939 a->ip6_table_index = ~0;
941 if (a->l2_table_index != ~0)
943 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
944 a->l2_table_index = ~0;
949 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
950 u32 * acl_list_index, u8 * tag)
952 acl_main_t *am = &acl_main;
955 macip_acl_rule_t *acl_new_rules;
958 /* Create and populate the rules */
959 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
960 CLIB_CACHE_LINE_BYTES);
963 /* Could not allocate rules. New or existing ACL - bail out regardless */
967 for (i = 0; i < count; i++)
969 r = &acl_new_rules[i];
970 r->is_permit = rules[i].is_permit;
971 r->is_ipv6 = rules[i].is_ipv6;
972 memcpy (&r->src_mac, rules[i].src_mac, 6);
973 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
975 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
977 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
978 r->src_prefixlen = rules[i].src_ip_prefix_len;
982 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
983 memset (a, 0, sizeof (*a));
984 /* Will return the newly allocated ACL index */
985 *acl_list_index = a - am->macip_acls;
987 a->rules = acl_new_rules;
989 memcpy (a->tag, tag, sizeof (a->tag));
991 /* Create and populate the classifer tables */
992 macip_create_classify_tables (am, *acl_list_index);
998 /* No check for validity of sw_if_index - the callers were supposed to validate */
1001 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1004 u32 macip_acl_index;
1005 macip_acl_list_t *a;
1006 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1007 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1008 /* No point in deleting MACIP ACL which is not applied */
1009 if (~0 == macip_acl_index)
1011 a = &am->macip_acls[macip_acl_index];
1012 /* remove the classifier tables off the interface L2 ACL */
1014 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1015 a->ip6_table_index, a->l2_table_index, 0);
1016 /* Unset the MACIP ACL index */
1017 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1021 /* No check for validity of sw_if_index - the callers were supposed to validate */
1024 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1025 u32 macip_acl_index)
1027 macip_acl_list_t *a;
1029 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1033 a = &am->macip_acls[macip_acl_index];
1034 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1035 /* If there already a MACIP ACL applied, unapply it */
1036 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1037 macip_acl_interface_del_acl(am, sw_if_index);
1038 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1039 /* Apply the classifier tables for L2 ACLs */
1041 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1042 a->ip6_table_index, a->l2_table_index, 1);
1047 macip_acl_del_list (u32 acl_list_index)
1049 acl_main_t *am = &acl_main;
1050 macip_acl_list_t *a;
1052 if (pool_is_free_index (am->macip_acls, acl_list_index))
1057 /* delete any references to the ACL */
1058 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1060 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1062 macip_acl_interface_del_acl (am, i);
1066 /* Now that classifier tables are detached, clean them up */
1067 macip_destroy_classify_tables (am, acl_list_index);
1069 /* now we can delete the ACL itself */
1070 a = &am->macip_acls[acl_list_index];
1073 clib_mem_free (a->rules);
1075 pool_put (am->macip_acls, a);
1081 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1084 acl_main_t *am = &acl_main;
1088 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1092 rv = macip_acl_interface_del_acl (am, sw_if_index);
1098 * If the client does not allocate enough memory for a variable-length
1099 * message, and then proceed to use it as if the full memory allocated,
1100 * absent the check we happily consume that on the VPP side, and go
1101 * along as if nothing happened. However, the resulting
1102 * effects range from just garbage in the API decode
1103 * (because the decoder snoops too far), to potential memory
1106 * This verifies that the actual length of the message is
1107 * at least expected_len, and complains loudly if it is not.
1109 * A failing check here is 100% a software bug on the API user side,
1110 * so we might as well yell.
1113 static int verify_message_len(void *mp, u32 expected_len, char *where)
1115 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1116 if (supplied_len < expected_len) {
1117 clib_warning("%s: Supplied message length %d is less than expected %d",
1118 where, supplied_len, expected_len);
1125 /* API message handler */
1127 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1129 vl_api_acl_add_replace_reply_t *rmp;
1130 acl_main_t *am = &acl_main;
1132 u32 acl_list_index = ntohl (mp->acl_index);
1133 u32 acl_count = ntohl (mp->count);
1134 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1136 if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1137 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1139 rv = VNET_API_ERROR_INVALID_VALUE;
1143 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1145 rmp->acl_index = htonl(acl_list_index);
1151 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1153 acl_main_t *am = &acl_main;
1154 vl_api_acl_del_reply_t *rmp;
1157 rv = acl_del_list (ntohl (mp->acl_index));
1159 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1163 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1165 acl_main_t *am = &acl_main;
1166 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1167 u32 sw_if_index = ntohl (mp->sw_if_index);
1168 vl_api_acl_interface_add_del_reply_t *rmp;
1171 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1172 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1175 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1176 mp->is_input, ntohl (mp->acl_index));
1178 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1182 vl_api_acl_interface_set_acl_list_t_handler
1183 (vl_api_acl_interface_set_acl_list_t * mp)
1185 acl_main_t *am = &acl_main;
1186 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1189 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1190 u32 sw_if_index = ntohl (mp->sw_if_index);
1192 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1193 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1196 acl_interface_reset_inout_acls (sw_if_index, 0);
1197 acl_interface_reset_inout_acls (sw_if_index, 1);
1199 for (i = 0; i < mp->count; i++)
1201 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1202 ntohl (mp->acls[i]));
1206 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1210 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1212 api_rule->is_permit = r->is_permit;
1213 api_rule->is_ipv6 = r->is_ipv6;
1216 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1217 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1221 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1222 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1224 api_rule->src_ip_prefix_len = r->src_prefixlen;
1225 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1226 api_rule->proto = r->proto;
1227 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1228 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1229 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1230 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1231 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1232 api_rule->tcp_flags_value = r->tcp_flags_value;
1236 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1237 acl_list_t * acl, u32 context)
1239 vl_api_acl_details_t *mp;
1240 vl_api_acl_rule_t *rules;
1242 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1244 mp = vl_msg_api_alloc (msg_size);
1245 memset (mp, 0, msg_size);
1246 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1248 /* fill in the message */
1249 mp->context = context;
1250 mp->count = htonl (acl->count);
1251 mp->acl_index = htonl (acl - am->acls);
1252 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1253 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1255 for (i = 0; i < acl->count; i++)
1257 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1260 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1261 vl_msg_api_send_shmem (q, (u8 *) & mp);
1266 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1268 acl_main_t *am = &acl_main;
1273 unix_shared_memory_queue_t *q;
1275 q = vl_api_client_index_to_input_queue (mp->client_index);
1281 if (mp->acl_index == ~0)
1284 /* Just dump all ACLs */
1285 pool_foreach (acl, am->acls,
1287 send_acl_details(am, q, acl, mp->context);
1293 acl_index = ntohl (mp->acl_index);
1294 if (!pool_is_free_index (am->acls, acl_index))
1296 acl = &am->acls[acl_index];
1297 send_acl_details (am, q, acl, mp->context);
1303 /* FIXME API: should we signal an error here at all ? */
1309 send_acl_interface_list_details (acl_main_t * am,
1310 unix_shared_memory_queue_t * q,
1311 u32 sw_if_index, u32 context)
1313 vl_api_acl_interface_list_details_t *mp;
1320 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1321 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1323 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1324 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1325 count = n_input + n_output;
1327 msg_size = sizeof (*mp);
1328 msg_size += sizeof (mp->acls[0]) * count;
1330 mp = vl_msg_api_alloc (msg_size);
1331 memset (mp, 0, msg_size);
1333 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1335 /* fill in the message */
1336 mp->context = context;
1337 mp->sw_if_index = htonl (sw_if_index);
1339 mp->n_input = n_input;
1340 for (i = 0; i < n_input; i++)
1342 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1344 for (i = 0; i < n_output; i++)
1346 mp->acls[n_input + i] =
1347 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1350 vl_msg_api_send_shmem (q, (u8 *) & mp);
1354 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1357 acl_main_t *am = &acl_main;
1358 vnet_sw_interface_t *swif;
1359 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1362 unix_shared_memory_queue_t *q;
1364 q = vl_api_client_index_to_input_queue (mp->client_index);
1370 if (mp->sw_if_index == ~0)
1373 pool_foreach (swif, im->sw_interfaces,
1375 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1381 sw_if_index = ntohl (mp->sw_if_index);
1382 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1383 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1387 /* MACIP ACL API handlers */
1390 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1392 vl_api_macip_acl_add_reply_t *rmp;
1393 acl_main_t *am = &acl_main;
1395 u32 acl_list_index = ~0;
1396 u32 acl_count = ntohl (mp->count);
1397 u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1399 if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1400 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1402 rv = VNET_API_ERROR_INVALID_VALUE;
1406 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1408 rmp->acl_index = htonl(acl_list_index);
1414 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1416 acl_main_t *am = &acl_main;
1417 vl_api_macip_acl_del_reply_t *rmp;
1420 rv = macip_acl_del_list (ntohl (mp->acl_index));
1422 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1426 vl_api_macip_acl_interface_add_del_t_handler
1427 (vl_api_macip_acl_interface_add_del_t * mp)
1429 acl_main_t *am = &acl_main;
1430 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1432 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1433 u32 sw_if_index = ntohl (mp->sw_if_index);
1435 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1436 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1439 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1440 ntohl (mp->acl_index));
1442 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1446 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1447 macip_acl_list_t * acl, u32 context)
1449 vl_api_macip_acl_details_t *mp;
1450 vl_api_macip_acl_rule_t *rules;
1451 macip_acl_rule_t *r;
1453 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1455 mp = vl_msg_api_alloc (msg_size);
1456 memset (mp, 0, msg_size);
1457 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1459 /* fill in the message */
1460 mp->context = context;
1463 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1464 mp->count = htonl (acl->count);
1465 mp->acl_index = htonl (acl - am->macip_acls);
1467 for (i = 0; i < acl->count; i++)
1470 rules[i].is_permit = r->is_permit;
1471 rules[i].is_ipv6 = r->is_ipv6;
1472 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1473 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1474 sizeof (r->src_mac_mask));
1476 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1477 sizeof (r->src_ip_addr.ip6));
1479 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1480 sizeof (r->src_ip_addr.ip4));
1481 rules[i].src_ip_prefix_len = r->src_prefixlen;
1486 /* No martini, no party - no ACL applied to this interface. */
1491 vl_msg_api_send_shmem (q, (u8 *) & mp);
1496 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1498 acl_main_t *am = &acl_main;
1499 macip_acl_list_t *acl;
1501 unix_shared_memory_queue_t *q;
1503 q = vl_api_client_index_to_input_queue (mp->client_index);
1509 if (mp->acl_index == ~0)
1511 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1512 pool_foreach (acl, am->macip_acls, (
1514 send_macip_acl_details (am, q, acl,
1522 u32 acl_index = ntohl (mp->acl_index);
1523 if (!pool_is_free_index (am->macip_acls, acl_index))
1525 acl = &am->macip_acls[acl_index];
1526 send_macip_acl_details (am, q, acl, mp->context);
1532 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1535 acl_main_t *am = &acl_main;
1536 vl_api_macip_acl_interface_get_reply_t *rmp;
1537 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1538 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1539 unix_shared_memory_queue_t *q;
1542 q = vl_api_client_index_to_input_queue (mp->client_index);
1548 rmp = vl_msg_api_alloc (msg_size);
1549 memset (rmp, 0, msg_size);
1551 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1552 rmp->context = mp->context;
1553 rmp->count = htonl (count);
1554 for (i = 0; i < count; i++)
1556 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1559 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1562 /* Set up the API message handling tables */
1563 static clib_error_t *
1564 acl_plugin_api_hookup (vlib_main_t * vm)
1566 acl_main_t *am = &acl_main;
1568 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1570 vl_api_##n##_t_handler, \
1572 vl_api_##n##_t_endian, \
1573 vl_api_##n##_t_print, \
1574 sizeof(vl_api_##n##_t), 1);
1575 foreach_acl_plugin_api_msg;
1581 #define vl_msg_name_crc_list
1582 #include <acl/acl_all_api_h.h>
1583 #undef vl_msg_name_crc_list
1586 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1588 #define _(id,n,crc) \
1589 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1590 foreach_vl_msg_name_crc_acl;
1595 acl_setup_fa_nodes (void)
1597 vlib_main_t *vm = vlib_get_main ();
1598 acl_main_t *am = &acl_main;
1599 vlib_node_t *n, *n4, *n6;
1601 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1602 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1603 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1606 am->l2_input_classify_next_acl_ip4 =
1607 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1608 am->l2_input_classify_next_acl_ip6 =
1609 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1611 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1612 l2input_get_feat_names (),
1613 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1615 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1616 l2input_get_feat_names (),
1617 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1620 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1621 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1622 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1624 am->l2_output_classify_next_acl_ip4 =
1625 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1626 am->l2_output_classify_next_acl_ip6 =
1627 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1629 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1630 l2output_get_feat_names (),
1631 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1633 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1634 l2output_get_feat_names (),
1635 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1639 acl_set_timeout_sec(int timeout_type, u32 value)
1641 acl_main_t *am = &acl_main;
1642 clib_time_t *ct = &am->vlib_main->clib_time;
1644 if (timeout_type < ACL_N_TIMEOUTS) {
1645 am->session_timeout_sec[timeout_type] = value;
1647 clib_warning("Unknown timeout type %d", timeout_type);
1650 am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1654 acl_set_session_max_entries(u32 value)
1656 acl_main_t *am = &acl_main;
1657 am->fa_conn_table_max_entries = value;
1661 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1663 acl_main_t *am = &acl_main;
1664 if ((eh < 256) && (value < 2))
1666 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1674 static clib_error_t *
1675 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1677 acl_main_t *am = &acl_main;
1679 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1680 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1681 /* also unapply any ACLs in case the users did not do so. */
1682 macip_acl_interface_del_acl(am, sw_if_index);
1683 acl_interface_reset_inout_acls (sw_if_index, 0);
1684 acl_interface_reset_inout_acls (sw_if_index, 1);
1689 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1691 static clib_error_t *
1692 acl_set_aclplugin_fn (vlib_main_t * vm,
1693 unformat_input_t * input,
1694 vlib_cli_command_t * cmd)
1696 clib_error_t *error = 0;
1700 uword memory_size = 0;
1701 acl_main_t *am = &acl_main;
1703 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1704 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1705 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1709 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1711 am->l4_match_nonfirst_fragment = (val != 0);
1714 if (unformat (input, "session")) {
1715 if (unformat (input, "clear")) {
1716 acl_main_t *am = &acl_main;
1717 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1718 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
1721 if (unformat (input, "table")) {
1722 /* The commands here are for tuning/testing. No user-serviceable parts inside */
1723 if (unformat (input, "max-entries")) {
1724 if (!unformat(input, "%u", &val)) {
1725 error = clib_error_return(0,
1726 "expecting maximum number of entries, got `%U`",
1727 format_unformat_error, input);
1730 acl_set_session_max_entries(val);
1734 if (unformat (input, "hash-table-buckets")) {
1735 if (!unformat(input, "%u", &val)) {
1736 error = clib_error_return(0,
1737 "expecting maximum number of hash table buckets, got `%U`",
1738 format_unformat_error, input);
1741 am->fa_conn_table_hash_num_buckets = val;
1745 if (unformat (input, "hash-table-memory")) {
1746 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1747 error = clib_error_return(0,
1748 "expecting maximum amount of hash table memory, got `%U`",
1749 format_unformat_error, input);
1752 am->fa_conn_table_hash_memory_size = memory_size;
1758 if (unformat (input, "timeout")) {
1759 if (unformat(input, "udp")) {
1760 if(unformat(input, "idle")) {
1761 if (!unformat(input, "%u", &timeout)) {
1762 error = clib_error_return(0,
1763 "expecting timeout value in seconds, got `%U`",
1764 format_unformat_error, input);
1767 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
1772 if (unformat(input, "tcp")) {
1773 if(unformat(input, "idle")) {
1774 if (!unformat(input, "%u", &timeout)) {
1775 error = clib_error_return(0,
1776 "expecting timeout value in seconds, got `%U`",
1777 format_unformat_error, input);
1780 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
1784 if(unformat(input, "transient")) {
1785 if (!unformat(input, "%u", &timeout)) {
1786 error = clib_error_return(0,
1787 "expecting timeout value in seconds, got `%U`",
1788 format_unformat_error, input);
1791 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
1804 static clib_error_t *
1805 acl_show_aclplugin_fn (vlib_main_t * vm,
1806 unformat_input_t * input,
1807 vlib_cli_command_t * cmd)
1809 clib_error_t *error = 0;
1810 acl_main_t *am = &acl_main;
1811 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1813 vnet_sw_interface_t *swif;
1815 if (unformat (input, "sessions"))
1819 pool_foreach (swif, im->sw_interfaces,
1821 u32 sw_if_index = swif->sw_if_index;
1822 u64 n_adds = sw_if_index < vec_len(am->fa_session_adds_by_sw_if_index) ? am->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
1823 u64 n_dels = sw_if_index < vec_len(am->fa_session_dels_by_sw_if_index) ? am->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
1824 out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
1826 out0 = format(out0, "\n\nPer-worker data:\n");
1827 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1828 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1829 out0 = format(out0, "Worker #%d:\n", wk);
1830 out0 = format(out0, " Next expiry time: %lu\n", pw->next_expiry_time);
1831 out0 = format(out0, " Requeue until time: %lu\n", pw->requeue_until_time);
1832 out0 = format(out0, " Current time wait interval: %lu\n", pw->current_time_wait_interval);
1833 out0 = format(out0, " Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
1834 out0 = format(out0, " Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
1835 out0 = format(out0, " Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
1836 out0 = format(out0, " Swipe until this time: %lu\n", pw->swipe_end_time);
1837 out0 = format(out0, " sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1838 out0 = format(out0, " pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
1839 out0 = format(out0, " clear in progress: %u\n", pw->clear_in_process);
1840 out0 = format(out0, " interrupt is pending: %d\n", pw->interrupt_is_pending);
1841 out0 = format(out0, " interrupt is needed: %d\n", pw->interrupt_is_needed);
1842 out0 = format(out0, " interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
1844 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
1845 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
1846 foreach_fa_cleaner_counter;
1848 vlib_cli_output(vm, "\n\n%s\n\n", out0);
1849 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
1850 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
1851 am->fa_cleaner_wait_time_increment * 1000.0, ((f64)am->fa_current_cleaner_timer_wait_interval) * 1000.0/(f64)vm->clib_time.clocks_per_second);
1860 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
1861 .path = "set acl-plugin",
1862 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
1863 .function = acl_set_aclplugin_fn,
1866 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
1867 .path = "show acl-plugin",
1868 .short_help = "show acl-plugin sessions",
1869 .function = acl_show_aclplugin_fn,
1875 static clib_error_t *
1876 acl_init (vlib_main_t * vm)
1878 acl_main_t *am = &acl_main;
1879 clib_error_t *error = 0;
1880 memset (am, 0, sizeof (*am));
1882 am->vnet_main = vnet_get_main ();
1884 u8 *name = format (0, "acl_%08x%c", api_version, 0);
1886 /* Ask for a correctly-sized block of API message decode slots */
1887 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1888 VL_MSG_FIRST_AVAILABLE);
1890 error = acl_plugin_api_hookup (vm);
1892 /* Add our API messages to the global name_crc hash table */
1893 setup_message_id_table (am, &api_main);
1897 acl_setup_fa_nodes();
1898 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
1899 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
1900 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
1902 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
1903 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
1904 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
1905 vlib_thread_main_t *tm = vlib_get_thread_main ();
1906 vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
1910 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1911 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1912 vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
1913 vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
1914 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
1915 pw->fa_conn_list_head[tt] = ~0;
1916 pw->fa_conn_list_tail[tt] = ~0;
1921 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
1922 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
1923 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
1925 am->fa_cleaner_cnt_delete_by_sw_index = 0;
1926 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
1927 am->fa_cleaner_cnt_unknown_event = 0;
1928 am->fa_cleaner_cnt_timer_restarted = 0;
1929 am->fa_cleaner_cnt_wait_with_timeout = 0;
1932 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
1936 am->l4_match_nonfirst_fragment = 1;
1941 VLIB_INIT_FUNCTION (acl_init);