2 * Copyright (c) 2015 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.
16 *------------------------------------------------------------------
17 * acl_test.c - test harness plugin
18 *------------------------------------------------------------------
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26 #include <vnet/ip/ip.h>
27 #include <arpa/inet.h>
29 #define __plugin_msg_base acl_test_main.msg_id_base
30 #include <vlibapi/vat_helper_macros.h>
32 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
34 /* Declare message IDs */
35 #include <acl/acl_msg_enum.h>
37 /* define message structures */
39 #include <acl/acl_all_api_h.h>
42 /* define message structures */
44 #include <acl/acl_all_api_h.h>
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...)
50 #include <acl/acl_all_api_h.h>
53 /* Get the API version number. */
54 #define vl_api_version(n,v) static u32 api_version=(v);
55 #include <acl/acl_all_api_h.h>
59 /* API message ID base */
64 acl_test_main_t acl_test_main;
66 #define foreach_standard_reply_retval_handler \
68 _(acl_interface_add_del_reply) \
69 _(macip_acl_interface_add_del_reply) \
70 _(acl_interface_set_acl_list_reply) \
71 _(macip_acl_del_reply)
73 #define foreach_reply_retval_aclindex_handler \
74 _(acl_add_replace_reply) \
75 _(macip_acl_add_reply)
78 static void vl_api_##n##_t_handler \
79 (vl_api_##n##_t * mp) \
81 vat_main_t * vam = acl_test_main.vat_main; \
82 i32 retval = ntohl(mp->retval); \
83 if (vam->async_mode) { \
84 vam->async_errors += (retval < 0); \
86 vam->retval = retval; \
87 vam->result_ready = 1; \
90 foreach_standard_reply_retval_handler;
94 static void vl_api_##n##_t_handler \
95 (vl_api_##n##_t * mp) \
97 vat_main_t * vam = acl_test_main.vat_main; \
98 i32 retval = ntohl(mp->retval); \
99 if (vam->async_mode) { \
100 vam->async_errors += (retval < 0); \
102 clib_warning("ACL index: %d", ntohl(mp->acl_index)); \
103 vam->retval = retval; \
104 vam->result_ready = 1; \
107 foreach_reply_retval_aclindex_handler;
110 /* These two ought to be in a library somewhere but they aren't */
112 my_unformat_mac_address (unformat_input_t * input, va_list * args)
114 u8 *a = va_arg (*args, u8 *);
115 return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
120 my_format_mac_address (u8 * s, va_list * args)
122 u8 *a = va_arg (*args, u8 *);
123 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
124 a[0], a[1], a[2], a[3], a[4], a[5]);
129 static void vl_api_acl_plugin_get_version_reply_t_handler
130 (vl_api_acl_plugin_get_version_reply_t * mp)
132 vat_main_t * vam = acl_test_main.vat_main;
133 clib_warning("ACL plugin version: %d.%d", ntohl(mp->major), ntohl(mp->minor));
134 vam->result_ready = 1;
137 static void vl_api_acl_interface_list_details_t_handler
138 (vl_api_acl_interface_list_details_t * mp)
141 vat_main_t * vam = acl_test_main.vat_main;
143 vl_api_acl_interface_list_details_t_endian(mp);
144 out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
145 out = format(out, " input ");
146 for(i=0; i<mp->count; i++) {
147 out = format(out, "%d ", mp->acls[i]);
148 if (i == mp->n_input-1)
149 out = format(out, "\n output ");
151 out = format(out, "\n");
152 clib_warning("%s", out);
154 vam->result_ready = 1;
159 vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
161 int af = a->is_ipv6 ? AF_INET6 : AF_INET;
162 u8 src[INET6_ADDRSTRLEN];
163 u8 dst[INET6_ADDRSTRLEN];
164 inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
165 inet_ntop(af, a->dst_ip_addr, (void *)dst, sizeof(dst));
167 out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
168 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
169 src, a->src_ip_prefix_len,
170 dst, a->dst_ip_prefix_len,
172 a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
173 a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
174 a->tcp_flags_value, a->tcp_flags_mask);
180 static void vl_api_acl_details_t_handler
181 (vl_api_acl_details_t * mp)
184 vat_main_t * vam = acl_test_main.vat_main;
185 vl_api_acl_details_t_endian(mp);
187 out = format(0, "acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
188 for(i=0; i<mp->count; i++) {
189 out = format(out, " ");
190 out = vl_api_acl_rule_t_pretty_format(out, &mp->r[i]);
191 out = format(out, "%s\n", i<mp->count-1 ? "," : "");
193 clib_warning("%s", out);
195 vam->result_ready = 1;
199 vl_api_macip_acl_rule_t_pretty_format (u8 *out, vl_api_macip_acl_rule_t * a)
201 int af = a->is_ipv6 ? AF_INET6 : AF_INET;
202 u8 src[INET6_ADDRSTRLEN];
203 inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
205 out = format(out, "%s action %d ip %s/%d mac %U mask %U",
206 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
207 src, a->src_ip_prefix_len,
208 my_format_mac_address, a->src_mac,
209 my_format_mac_address, a->src_mac_mask);
214 static void vl_api_macip_acl_details_t_handler
215 (vl_api_macip_acl_details_t * mp)
218 vat_main_t * vam = acl_test_main.vat_main;
219 vl_api_macip_acl_details_t_endian(mp);
220 u8 *out = format(0,"MACIP acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
221 for(i=0; i<mp->count; i++) {
222 out = format(out, " ");
223 out = vl_api_macip_acl_rule_t_pretty_format(out, &mp->r[i]);
224 out = format(out, "%s\n", i<mp->count-1 ? "," : "");
226 clib_warning("%s", out);
228 vam->result_ready = 1;
231 static void vl_api_macip_acl_interface_get_reply_t_handler
232 (vl_api_macip_acl_interface_get_reply_t * mp)
235 vat_main_t * vam = acl_test_main.vat_main;
236 u8 *out = format(0, "sw_if_index with MACIP ACL count: %d\n", ntohl(mp->count));
237 for(i=0; i<ntohl(mp->count); i++) {
238 out = format(out, " macip_acl_interface_add_del sw_if_index %d add acl %d\n", i, ntohl(mp->acls[i]));
240 out = format(out, "\n");
241 clib_warning("%s", out);
243 vam->result_ready = 1;
248 * Table of message reply handlers, must include boilerplate handlers
251 #define foreach_vpe_api_reply_msg \
252 _(ACL_ADD_REPLACE_REPLY, acl_add_replace_reply) \
253 _(ACL_DEL_REPLY, acl_del_reply) \
254 _(ACL_INTERFACE_ADD_DEL_REPLY, acl_interface_add_del_reply) \
255 _(ACL_INTERFACE_SET_ACL_LIST_REPLY, acl_interface_set_acl_list_reply) \
256 _(ACL_INTERFACE_LIST_DETAILS, acl_interface_list_details) \
257 _(ACL_DETAILS, acl_details) \
258 _(MACIP_ACL_ADD_REPLY, macip_acl_add_reply) \
259 _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
260 _(MACIP_ACL_DETAILS, macip_acl_details) \
261 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply) \
262 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply) \
263 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
265 static int api_acl_plugin_get_version (vat_main_t * vam)
267 acl_test_main_t * sm = &acl_test_main;
268 vl_api_acl_plugin_get_version_t * mp;
269 u32 msg_size = sizeof(*mp);
272 vam->result_ready = 0;
273 mp = vl_msg_api_alloc_as_if_client(msg_size);
274 memset (mp, 0, msg_size);
275 mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
276 mp->client_index = vam->my_client_index;
281 /* Wait for a reply... */
286 static int api_macip_acl_interface_get (vat_main_t * vam)
288 acl_test_main_t * sm = &acl_test_main;
289 vl_api_acl_plugin_get_version_t * mp;
290 u32 msg_size = sizeof(*mp);
293 vam->result_ready = 0;
294 mp = vl_msg_api_alloc_as_if_client(msg_size);
295 memset (mp, 0, msg_size);
296 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
297 mp->client_index = vam->my_client_index;
302 /* Wait for a reply... */
307 #define vec_validate_acl_rules(v, idx) \
309 if (vec_len(v) < idx+1) { \
310 vec_validate(v, idx); \
311 v[idx].is_permit = 0x1; \
312 v[idx].srcport_or_icmptype_last = 0xffff; \
313 v[idx].dstport_or_icmpcode_last = 0xffff; \
318 static int api_acl_add_replace (vat_main_t * vam)
320 acl_test_main_t * sm = &acl_test_main;
321 unformat_input_t * i = vam->input;
322 vl_api_acl_add_replace_t * mp;
324 u32 msg_size = sizeof (*mp); /* without the rules */
326 vl_api_acl_rule_t *rules = 0;
329 int n_rules_override = -1;
334 u32 tcpflags, tcpmask;
335 u32 src_prefix_length = 0, dst_prefix_length = 0;
336 ip4_address_t src_v4address, dst_v4address;
337 ip6_address_t src_v6address, dst_v6address;
341 if (!unformat (i, "%d", &acl_index)) {
345 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
347 if (unformat (i, "ipv6"))
349 vec_validate_acl_rules(rules, rule_idx);
350 rules[rule_idx].is_ipv6 = 1;
352 else if (unformat (i, "ipv4"))
354 vec_validate_acl_rules(rules, rule_idx);
355 rules[rule_idx].is_ipv6 = 0;
357 else if (unformat (i, "permit+reflect"))
359 vec_validate_acl_rules(rules, rule_idx);
360 rules[rule_idx].is_permit = 2;
362 else if (unformat (i, "permit"))
364 vec_validate_acl_rules(rules, rule_idx);
365 rules[rule_idx].is_permit = 1;
367 else if (unformat (i, "deny"))
369 vec_validate_acl_rules(rules, rule_idx);
370 rules[rule_idx].is_permit = 0;
372 else if (unformat (i, "count %d", &n_rules_override))
374 /* we will use this later */
376 else if (unformat (i, "action %d", &action))
378 vec_validate_acl_rules(rules, rule_idx);
379 rules[rule_idx].is_permit = action;
381 else if (unformat (i, "src %U/%d",
382 unformat_ip4_address, &src_v4address, &src_prefix_length))
384 vec_validate_acl_rules(rules, rule_idx);
385 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
386 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
387 rules[rule_idx].is_ipv6 = 0;
389 else if (unformat (i, "src %U/%d",
390 unformat_ip6_address, &src_v6address, &src_prefix_length))
392 vec_validate_acl_rules(rules, rule_idx);
393 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
394 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
395 rules[rule_idx].is_ipv6 = 1;
397 else if (unformat (i, "dst %U/%d",
398 unformat_ip4_address, &dst_v4address, &dst_prefix_length))
400 vec_validate_acl_rules(rules, rule_idx);
401 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
402 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
403 rules[rule_idx].is_ipv6 = 0;
405 else if (unformat (i, "dst %U/%d",
406 unformat_ip6_address, &dst_v6address, &dst_prefix_length))
408 vec_validate_acl_rules(rules, rule_idx);
409 memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
410 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
411 rules[rule_idx].is_ipv6 = 1;
413 else if (unformat (i, "sport %d-%d", &port1, &port2))
415 vec_validate_acl_rules(rules, rule_idx);
416 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
417 rules[rule_idx].srcport_or_icmptype_last = htons(port2);
419 else if (unformat (i, "sport %d", &port1))
421 vec_validate_acl_rules(rules, rule_idx);
422 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
423 rules[rule_idx].srcport_or_icmptype_last = htons(port1);
425 else if (unformat (i, "dport %d-%d", &port1, &port2))
427 vec_validate_acl_rules(rules, rule_idx);
428 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
429 rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
431 else if (unformat (i, "dport %d", &port1))
433 vec_validate_acl_rules(rules, rule_idx);
434 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
435 rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
437 else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
439 vec_validate_acl_rules(rules, rule_idx);
440 rules[rule_idx].tcp_flags_value = tcpflags;
441 rules[rule_idx].tcp_flags_mask = tcpmask;
443 else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
445 vec_validate_acl_rules(rules, rule_idx);
446 rules[rule_idx].tcp_flags_value = tcpflags;
447 rules[rule_idx].tcp_flags_mask = tcpmask;
449 else if (unformat (i, "proto %d", &proto))
451 vec_validate_acl_rules(rules, rule_idx);
452 rules[rule_idx].proto = proto;
454 else if (unformat (i, "tag %s", &tag))
457 else if (unformat (i, ","))
460 vec_validate_acl_rules(rules, rule_idx);
466 /* Construct the API message */
467 vam->result_ready = 0;
470 n_rules = vec_len(rules);
474 if (n_rules_override >= 0)
475 n_rules = n_rules_override;
477 msg_size += n_rules*sizeof(rules[0]);
479 mp = vl_msg_api_alloc_as_if_client(msg_size);
480 memset (mp, 0, msg_size);
481 mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
482 mp->client_index = vam->my_client_index;
484 clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
487 if (vec_len(tag) >= sizeof(mp->tag))
489 tag[sizeof(mp->tag)-1] = 0;
490 _vec_len(tag) = sizeof(mp->tag);
492 clib_memcpy(mp->tag, tag, vec_len(tag));
495 mp->acl_index = ntohl(acl_index);
496 mp->count = htonl(n_rules);
501 /* Wait for a reply... */
506 static int api_acl_del (vat_main_t * vam)
508 unformat_input_t * i = vam->input;
509 vl_api_acl_del_t * mp;
513 if (!unformat (i, "%d", &acl_index)) {
514 errmsg ("missing acl index\n");
518 /* Construct the API message */
520 mp->acl_index = ntohl(acl_index);
525 /* Wait for a reply... */
530 static int api_macip_acl_del (vat_main_t * vam)
532 unformat_input_t * i = vam->input;
533 vl_api_acl_del_t * mp;
537 if (!unformat (i, "%d", &acl_index)) {
538 errmsg ("missing acl index\n");
542 /* Construct the API message */
543 M(MACIP_ACL_DEL, mp);
544 mp->acl_index = ntohl(acl_index);
549 /* Wait for a reply... */
554 static int api_acl_interface_add_del (vat_main_t * vam)
556 unformat_input_t * i = vam->input;
557 vl_api_acl_interface_add_del_t * mp;
558 u32 sw_if_index = ~0;
564 // acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
566 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
568 if (unformat (i, "%d", &acl_index))
575 /* Parse args required to build the message */
576 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
577 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
579 else if (unformat (i, "sw_if_index %d", &sw_if_index))
581 else if (unformat (i, "add"))
583 else if (unformat (i, "del"))
585 else if (unformat (i, "acl %d", &acl_index))
587 else if (unformat (i, "input"))
589 else if (unformat (i, "output"))
595 if (sw_if_index == ~0) {
596 errmsg ("missing interface name / explicit sw_if_index number \n");
600 if (acl_index == ~0) {
601 errmsg ("missing ACL index\n");
607 /* Construct the API message */
608 M(ACL_INTERFACE_ADD_DEL, mp);
609 mp->acl_index = ntohl(acl_index);
610 mp->sw_if_index = ntohl(sw_if_index);
612 mp->is_input = is_input;
617 /* Wait for a reply... */
622 static int api_macip_acl_interface_add_del (vat_main_t * vam)
624 unformat_input_t * i = vam->input;
625 vl_api_macip_acl_interface_add_del_t * mp;
626 u32 sw_if_index = ~0;
631 /* Parse args required to build the message */
632 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
633 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
635 else if (unformat (i, "sw_if_index %d", &sw_if_index))
637 else if (unformat (i, "add"))
639 else if (unformat (i, "del"))
641 else if (unformat (i, "acl %d", &acl_index))
647 if (sw_if_index == ~0) {
648 errmsg ("missing interface name / explicit sw_if_index number \n");
652 if (acl_index == ~0) {
653 errmsg ("missing ACL index\n");
659 /* Construct the API message */
660 M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
661 mp->acl_index = ntohl(acl_index);
662 mp->sw_if_index = ntohl(sw_if_index);
668 /* Wait for a reply... */
673 static int api_acl_interface_set_acl_list (vat_main_t * vam)
675 unformat_input_t * i = vam->input;
676 vl_api_acl_interface_set_acl_list_t * mp;
677 u32 sw_if_index = ~0;
684 // acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
686 /* Parse args required to build the message */
687 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
688 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
690 else if (unformat (i, "sw_if_index %d", &sw_if_index))
692 else if (unformat (i, "%d", &acl_index))
695 vec_add1(inacls, htonl(acl_index));
697 vec_add1(outacls, htonl(acl_index));
699 else if (unformat (i, "acl %d", &acl_index))
701 else if (unformat (i, "input"))
703 else if (unformat (i, "output"))
709 if (sw_if_index == ~0) {
710 errmsg ("missing interface name / explicit sw_if_index number \n");
714 /* Construct the API message */
715 M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
716 mp->sw_if_index = ntohl(sw_if_index);
717 mp->n_input = vec_len(inacls);
718 mp->count = vec_len(inacls) + vec_len(outacls);
719 vec_append(inacls, outacls);
720 if (vec_len(inacls) > 0)
721 clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
726 /* Wait for a reply... */
732 static int api_acl_interface_list_dump (vat_main_t * vam)
734 unformat_input_t * i = vam->input;
735 u32 sw_if_index = ~0;
736 vl_api_acl_interface_list_dump_t * mp;
739 /* Parse args required to build the message */
740 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
741 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
743 else if (unformat (i, "sw_if_index %d", &sw_if_index))
749 /* Construct the API message */
750 M(ACL_INTERFACE_LIST_DUMP, mp);
751 mp->sw_if_index = ntohl (sw_if_index);
756 /* Wait for a reply... */
761 static int api_acl_dump (vat_main_t * vam)
763 unformat_input_t * i = vam->input;
765 vl_api_acl_dump_t * mp;
768 /* Parse args required to build the message */
769 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
770 if (unformat (i, "%d", &acl_index))
776 /* Construct the API message */
778 mp->acl_index = ntohl (acl_index);
783 /* Wait for a reply... */
788 static int api_macip_acl_dump (vat_main_t * vam)
790 unformat_input_t * i = vam->input;
792 vl_api_acl_dump_t * mp;
795 /* Parse args required to build the message */
796 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
797 if (unformat (i, "%d", &acl_index))
803 /* Construct the API message */
804 M(MACIP_ACL_DUMP, mp);
805 mp->acl_index = ntohl (acl_index);
810 /* Wait for a reply... */
815 #define vec_validate_macip_acl_rules(v, idx) \
817 if (vec_len(v) < idx+1) { \
818 vec_validate(v, idx); \
819 v[idx].is_permit = 0x1; \
824 static int api_macip_acl_add (vat_main_t * vam)
826 acl_test_main_t * sm = &acl_test_main;
827 unformat_input_t * i = vam->input;
828 vl_api_macip_acl_add_t * mp;
829 u32 msg_size = sizeof (*mp); /* without the rules */
831 vl_api_macip_acl_rule_t *rules = 0;
834 int n_rules_override = -1;
835 u32 src_prefix_length = 0;
837 ip4_address_t src_v4address;
838 ip6_address_t src_v6address;
841 u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
844 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
846 if (unformat (i, "ipv6"))
848 vec_validate_macip_acl_rules(rules, rule_idx);
849 rules[rule_idx].is_ipv6 = 1;
851 else if (unformat (i, "ipv4"))
853 vec_validate_macip_acl_rules(rules, rule_idx);
854 rules[rule_idx].is_ipv6 = 1;
856 else if (unformat (i, "permit"))
858 vec_validate_macip_acl_rules(rules, rule_idx);
859 rules[rule_idx].is_permit = 1;
861 else if (unformat (i, "deny"))
863 vec_validate_macip_acl_rules(rules, rule_idx);
864 rules[rule_idx].is_permit = 0;
866 else if (unformat (i, "count %d", &n_rules_override))
868 /* we will use this later */
870 else if (unformat (i, "action %d", &action))
872 vec_validate_macip_acl_rules(rules, rule_idx);
873 rules[rule_idx].is_permit = action;
875 else if (unformat (i, "ip %U/%d",
876 unformat_ip4_address, &src_v4address, &src_prefix_length))
878 vec_validate_macip_acl_rules(rules, rule_idx);
879 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
880 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
881 rules[rule_idx].is_ipv6 = 0;
883 else if (unformat (i, "src"))
885 /* Everything in MACIP is "source" but allow this verbosity */
887 else if (unformat (i, "ip %U/%d",
888 unformat_ip6_address, &src_v6address, &src_prefix_length))
890 vec_validate_macip_acl_rules(rules, rule_idx);
891 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
892 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
893 rules[rule_idx].is_ipv6 = 1;
895 else if (unformat (i, "mac %U",
896 my_unformat_mac_address, &src_mac))
898 vec_validate_macip_acl_rules(rules, rule_idx);
899 memcpy (rules[rule_idx].src_mac, &src_mac, 6);
900 memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
902 else if (unformat (i, "mask %U",
903 my_unformat_mac_address, &src_mac))
905 vec_validate_macip_acl_rules(rules, rule_idx);
906 memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
908 else if (unformat (i, "tag %s", &tag))
911 else if (unformat (i, ","))
914 vec_validate_macip_acl_rules(rules, rule_idx);
920 /* Construct the API message */
921 vam->result_ready = 0;
924 n_rules = vec_len(rules);
928 if (n_rules_override >= 0)
929 n_rules = n_rules_override;
931 msg_size += n_rules*sizeof(rules[0]);
933 mp = vl_msg_api_alloc_as_if_client(msg_size);
934 memset (mp, 0, msg_size);
935 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
936 mp->client_index = vam->my_client_index;
938 clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
941 if (vec_len(tag) >= sizeof(mp->tag))
943 tag[sizeof(mp->tag)-1] = 0;
944 _vec_len(tag) = sizeof(mp->tag);
946 clib_memcpy(mp->tag, tag, vec_len(tag));
950 mp->count = htonl(n_rules);
955 /* Wait for a reply... */
961 * List of messages that the api test plugin sends,
962 * and that the data plane plugin processes
964 #define foreach_vpe_api_msg \
965 _(acl_plugin_get_version, "") \
966 _(acl_add_replace, "<acl-idx> [<ipv4|ipv6> <permit|permit+reflect|deny|action N> [src IP/plen] [dst IP/plen] [sport X-Y] [dport X-Y] [proto P] [tcpflags FL MASK], ... , ...") \
967 _(acl_del, "<acl-idx>") \
968 _(acl_dump, "[<acl-idx>]") \
969 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
970 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
971 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
972 _(macip_acl_add, "...") \
973 _(macip_acl_del, "<acl-idx>")\
974 _(macip_acl_dump, "[<acl-idx>]") \
975 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
976 _(macip_acl_interface_get, "")
980 void acl_vat_api_hookup (vat_main_t *vam)
982 acl_test_main_t * sm = &acl_test_main;
983 /* Hook up handlers for replies from the data plane plug-in */
985 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
987 vl_api_##n##_t_handler, \
989 vl_api_##n##_t_endian, \
990 vl_api_##n##_t_print, \
991 sizeof(vl_api_##n##_t), 1);
992 foreach_vpe_api_reply_msg;
995 /* API messages we can send */
996 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
1001 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1002 foreach_vpe_api_msg;
1006 clib_error_t * vat_plugin_register (vat_main_t *vam)
1008 acl_test_main_t * sm = &acl_test_main;
1013 name = format (0, "acl_%08x%c", api_version, 0);
1014 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1016 if (sm->msg_id_base != (u16) ~0)
1017 acl_vat_api_hookup (vam);