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 if (i == mp->n_input)
148 out = format(out, "\n output ");
149 out = format(out, "%d ", ntohl (mp->acls[i]));
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;
246 static void vl_api_acl_plugin_control_ping_reply_t_handler
247 (vl_api_acl_plugin_control_ping_reply_t * mp)
249 vat_main_t *vam = &vat_main;
250 i32 retval = ntohl (mp->retval);
253 vam->async_errors += (retval < 0);
257 vam->retval = retval;
258 vam->result_ready = 1;
264 * Table of message reply handlers, must include boilerplate handlers
267 #define foreach_vpe_api_reply_msg \
268 _(ACL_ADD_REPLACE_REPLY, acl_add_replace_reply) \
269 _(ACL_DEL_REPLY, acl_del_reply) \
270 _(ACL_INTERFACE_ADD_DEL_REPLY, acl_interface_add_del_reply) \
271 _(ACL_INTERFACE_SET_ACL_LIST_REPLY, acl_interface_set_acl_list_reply) \
272 _(ACL_INTERFACE_LIST_DETAILS, acl_interface_list_details) \
273 _(ACL_DETAILS, acl_details) \
274 _(MACIP_ACL_ADD_REPLY, macip_acl_add_reply) \
275 _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
276 _(MACIP_ACL_DETAILS, macip_acl_details) \
277 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply) \
278 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply) \
279 _(ACL_PLUGIN_CONTROL_PING_REPLY, acl_plugin_control_ping_reply) \
280 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
282 static int api_acl_plugin_get_version (vat_main_t * vam)
284 acl_test_main_t * sm = &acl_test_main;
285 vl_api_acl_plugin_get_version_t * mp;
286 u32 msg_size = sizeof(*mp);
289 vam->result_ready = 0;
290 mp = vl_msg_api_alloc_as_if_client(msg_size);
291 memset (mp, 0, msg_size);
292 mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
293 mp->client_index = vam->my_client_index;
298 /* Wait for a reply... */
303 static int api_macip_acl_interface_get (vat_main_t * vam)
305 acl_test_main_t * sm = &acl_test_main;
306 vl_api_acl_plugin_get_version_t * mp;
307 u32 msg_size = sizeof(*mp);
310 vam->result_ready = 0;
311 mp = vl_msg_api_alloc_as_if_client(msg_size);
312 memset (mp, 0, msg_size);
313 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
314 mp->client_index = vam->my_client_index;
319 /* Wait for a reply... */
324 #define vec_validate_acl_rules(v, idx) \
326 if (vec_len(v) < idx+1) { \
327 vec_validate(v, idx); \
328 v[idx].is_permit = 0x1; \
329 v[idx].srcport_or_icmptype_last = 0xffff; \
330 v[idx].dstport_or_icmpcode_last = 0xffff; \
335 static int api_acl_add_replace (vat_main_t * vam)
337 acl_test_main_t * sm = &acl_test_main;
338 unformat_input_t * i = vam->input;
339 vl_api_acl_add_replace_t * mp;
341 u32 msg_size = sizeof (*mp); /* without the rules */
343 vl_api_acl_rule_t *rules = 0;
346 int n_rules_override = -1;
351 u32 tcpflags, tcpmask;
352 u32 src_prefix_length = 0, dst_prefix_length = 0;
353 ip4_address_t src_v4address, dst_v4address;
354 ip6_address_t src_v6address, dst_v6address;
358 if (!unformat (i, "%d", &acl_index)) {
362 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
364 if (unformat (i, "ipv6"))
366 vec_validate_acl_rules(rules, rule_idx);
367 rules[rule_idx].is_ipv6 = 1;
369 else if (unformat (i, "ipv4"))
371 vec_validate_acl_rules(rules, rule_idx);
372 rules[rule_idx].is_ipv6 = 0;
374 else if (unformat (i, "permit+reflect"))
376 vec_validate_acl_rules(rules, rule_idx);
377 rules[rule_idx].is_permit = 2;
379 else if (unformat (i, "permit"))
381 vec_validate_acl_rules(rules, rule_idx);
382 rules[rule_idx].is_permit = 1;
384 else if (unformat (i, "deny"))
386 vec_validate_acl_rules(rules, rule_idx);
387 rules[rule_idx].is_permit = 0;
389 else if (unformat (i, "count %d", &n_rules_override))
391 /* we will use this later */
393 else if (unformat (i, "action %d", &action))
395 vec_validate_acl_rules(rules, rule_idx);
396 rules[rule_idx].is_permit = action;
398 else if (unformat (i, "src %U/%d",
399 unformat_ip4_address, &src_v4address, &src_prefix_length))
401 vec_validate_acl_rules(rules, rule_idx);
402 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
403 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
404 rules[rule_idx].is_ipv6 = 0;
406 else if (unformat (i, "src %U/%d",
407 unformat_ip6_address, &src_v6address, &src_prefix_length))
409 vec_validate_acl_rules(rules, rule_idx);
410 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
411 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
412 rules[rule_idx].is_ipv6 = 1;
414 else if (unformat (i, "dst %U/%d",
415 unformat_ip4_address, &dst_v4address, &dst_prefix_length))
417 vec_validate_acl_rules(rules, rule_idx);
418 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
419 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
420 rules[rule_idx].is_ipv6 = 0;
422 else if (unformat (i, "dst %U/%d",
423 unformat_ip6_address, &dst_v6address, &dst_prefix_length))
425 vec_validate_acl_rules(rules, rule_idx);
426 memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
427 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
428 rules[rule_idx].is_ipv6 = 1;
430 else if (unformat (i, "sport %d-%d", &port1, &port2))
432 vec_validate_acl_rules(rules, rule_idx);
433 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
434 rules[rule_idx].srcport_or_icmptype_last = htons(port2);
436 else if (unformat (i, "sport %d", &port1))
438 vec_validate_acl_rules(rules, rule_idx);
439 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
440 rules[rule_idx].srcport_or_icmptype_last = htons(port1);
442 else if (unformat (i, "dport %d-%d", &port1, &port2))
444 vec_validate_acl_rules(rules, rule_idx);
445 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
446 rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
448 else if (unformat (i, "dport %d", &port1))
450 vec_validate_acl_rules(rules, rule_idx);
451 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
452 rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
454 else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
456 vec_validate_acl_rules(rules, rule_idx);
457 rules[rule_idx].tcp_flags_value = tcpflags;
458 rules[rule_idx].tcp_flags_mask = tcpmask;
460 else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
462 vec_validate_acl_rules(rules, rule_idx);
463 rules[rule_idx].tcp_flags_value = tcpflags;
464 rules[rule_idx].tcp_flags_mask = tcpmask;
466 else if (unformat (i, "proto %d", &proto))
468 vec_validate_acl_rules(rules, rule_idx);
469 rules[rule_idx].proto = proto;
471 else if (unformat (i, "tag %s", &tag))
474 else if (unformat (i, ","))
477 vec_validate_acl_rules(rules, rule_idx);
483 /* Construct the API message */
484 vam->result_ready = 0;
487 n_rules = vec_len(rules);
491 if (n_rules_override >= 0)
492 n_rules = n_rules_override;
494 msg_size += n_rules*sizeof(rules[0]);
496 mp = vl_msg_api_alloc_as_if_client(msg_size);
497 memset (mp, 0, msg_size);
498 mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
499 mp->client_index = vam->my_client_index;
500 if ((n_rules > 0) && rules)
501 clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
504 if (vec_len(tag) >= sizeof(mp->tag))
506 tag[sizeof(mp->tag)-1] = 0;
507 _vec_len(tag) = sizeof(mp->tag);
509 clib_memcpy(mp->tag, tag, vec_len(tag));
512 mp->acl_index = ntohl(acl_index);
513 mp->count = htonl(n_rules);
518 /* Wait for a reply... */
523 static int api_acl_del (vat_main_t * vam)
525 unformat_input_t * i = vam->input;
526 vl_api_acl_del_t * mp;
530 if (!unformat (i, "%d", &acl_index)) {
531 errmsg ("missing acl index\n");
535 /* Construct the API message */
537 mp->acl_index = ntohl(acl_index);
542 /* Wait for a reply... */
547 static int api_macip_acl_del (vat_main_t * vam)
549 unformat_input_t * i = vam->input;
550 vl_api_acl_del_t * mp;
554 if (!unformat (i, "%d", &acl_index)) {
555 errmsg ("missing acl index\n");
559 /* Construct the API message */
560 M(MACIP_ACL_DEL, mp);
561 mp->acl_index = ntohl(acl_index);
566 /* Wait for a reply... */
571 static int api_acl_interface_add_del (vat_main_t * vam)
573 unformat_input_t * i = vam->input;
574 vl_api_acl_interface_add_del_t * mp;
575 u32 sw_if_index = ~0;
581 // acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
583 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
585 if (unformat (i, "%d", &acl_index))
592 /* Parse args required to build the message */
593 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
594 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
596 else if (unformat (i, "sw_if_index %d", &sw_if_index))
598 else if (unformat (i, "add"))
600 else if (unformat (i, "del"))
602 else if (unformat (i, "acl %d", &acl_index))
604 else if (unformat (i, "input"))
606 else if (unformat (i, "output"))
612 if (sw_if_index == ~0) {
613 errmsg ("missing interface name / explicit sw_if_index number \n");
617 if (acl_index == ~0) {
618 errmsg ("missing ACL index\n");
624 /* Construct the API message */
625 M(ACL_INTERFACE_ADD_DEL, mp);
626 mp->acl_index = ntohl(acl_index);
627 mp->sw_if_index = ntohl(sw_if_index);
629 mp->is_input = is_input;
634 /* Wait for a reply... */
639 static int api_macip_acl_interface_add_del (vat_main_t * vam)
641 unformat_input_t * i = vam->input;
642 vl_api_macip_acl_interface_add_del_t * mp;
643 u32 sw_if_index = ~0;
648 /* Parse args required to build the message */
649 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
650 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
652 else if (unformat (i, "sw_if_index %d", &sw_if_index))
654 else if (unformat (i, "add"))
656 else if (unformat (i, "del"))
658 else if (unformat (i, "acl %d", &acl_index))
664 if (sw_if_index == ~0) {
665 errmsg ("missing interface name / explicit sw_if_index number \n");
669 if (acl_index == ~0) {
670 errmsg ("missing ACL index\n");
676 /* Construct the API message */
677 M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
678 mp->acl_index = ntohl(acl_index);
679 mp->sw_if_index = ntohl(sw_if_index);
685 /* Wait for a reply... */
690 static int api_acl_interface_set_acl_list (vat_main_t * vam)
692 unformat_input_t * i = vam->input;
693 vl_api_acl_interface_set_acl_list_t * mp;
694 u32 sw_if_index = ~0;
701 // acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
703 /* Parse args required to build the message */
704 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
705 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
707 else if (unformat (i, "sw_if_index %d", &sw_if_index))
709 else if (unformat (i, "%d", &acl_index))
712 vec_add1(inacls, htonl(acl_index));
714 vec_add1(outacls, htonl(acl_index));
716 else if (unformat (i, "acl %d", &acl_index))
718 else if (unformat (i, "input"))
720 else if (unformat (i, "output"))
726 if (sw_if_index == ~0) {
727 errmsg ("missing interface name / explicit sw_if_index number \n");
731 /* Construct the API message */
732 M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
733 mp->sw_if_index = ntohl(sw_if_index);
734 mp->n_input = vec_len(inacls);
735 mp->count = vec_len(inacls) + vec_len(outacls);
736 vec_append(inacls, outacls);
737 if (vec_len(inacls) > 0)
738 clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
743 /* Wait for a reply... */
749 api_acl_send_control_ping(vat_main_t *vam)
751 vl_api_acl_plugin_control_ping_t *mp_ping;
753 M(ACL_PLUGIN_CONTROL_PING, mp_ping);
758 static int api_acl_interface_list_dump (vat_main_t * vam)
760 unformat_input_t * i = vam->input;
761 u32 sw_if_index = ~0;
762 vl_api_acl_interface_list_dump_t * mp;
765 /* Parse args required to build the message */
766 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
767 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
769 else if (unformat (i, "sw_if_index %d", &sw_if_index))
775 /* Construct the API message */
776 M(ACL_INTERFACE_LIST_DUMP, mp);
777 mp->sw_if_index = ntohl (sw_if_index);
782 /* Use control ping for synchronization */
783 api_acl_send_control_ping(vam);
785 /* Wait for a reply... */
790 static int api_acl_dump (vat_main_t * vam)
792 unformat_input_t * i = vam->input;
794 vl_api_acl_dump_t * mp;
797 /* Parse args required to build the message */
798 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
799 if (unformat (i, "%d", &acl_index))
805 /* Construct the API message */
807 mp->acl_index = ntohl (acl_index);
812 /* Use control ping for synchronization */
813 api_acl_send_control_ping(vam);
815 /* Wait for a reply... */
820 static int api_macip_acl_dump (vat_main_t * vam)
822 unformat_input_t * i = vam->input;
824 vl_api_acl_dump_t * mp;
827 /* Parse args required to build the message */
828 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
829 if (unformat (i, "%d", &acl_index))
835 /* Construct the API message */
836 M(MACIP_ACL_DUMP, mp);
837 mp->acl_index = ntohl (acl_index);
842 /* Use control ping for synchronization */
843 api_acl_send_control_ping(vam);
845 /* Wait for a reply... */
850 #define vec_validate_macip_acl_rules(v, idx) \
852 if (vec_len(v) < idx+1) { \
853 vec_validate(v, idx); \
854 v[idx].is_permit = 0x1; \
859 static int api_macip_acl_add (vat_main_t * vam)
861 acl_test_main_t * sm = &acl_test_main;
862 unformat_input_t * i = vam->input;
863 vl_api_macip_acl_add_t * mp;
864 u32 msg_size = sizeof (*mp); /* without the rules */
866 vl_api_macip_acl_rule_t *rules = 0;
869 int n_rules_override = -1;
870 u32 src_prefix_length = 0;
872 ip4_address_t src_v4address;
873 ip6_address_t src_v6address;
876 u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
879 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
881 if (unformat (i, "ipv6"))
883 vec_validate_macip_acl_rules(rules, rule_idx);
884 rules[rule_idx].is_ipv6 = 1;
886 else if (unformat (i, "ipv4"))
888 vec_validate_macip_acl_rules(rules, rule_idx);
889 rules[rule_idx].is_ipv6 = 1;
891 else if (unformat (i, "permit"))
893 vec_validate_macip_acl_rules(rules, rule_idx);
894 rules[rule_idx].is_permit = 1;
896 else if (unformat (i, "deny"))
898 vec_validate_macip_acl_rules(rules, rule_idx);
899 rules[rule_idx].is_permit = 0;
901 else if (unformat (i, "count %d", &n_rules_override))
903 /* we will use this later */
905 else if (unformat (i, "action %d", &action))
907 vec_validate_macip_acl_rules(rules, rule_idx);
908 rules[rule_idx].is_permit = action;
910 else if (unformat (i, "ip %U/%d",
911 unformat_ip4_address, &src_v4address, &src_prefix_length))
913 vec_validate_macip_acl_rules(rules, rule_idx);
914 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
915 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
916 rules[rule_idx].is_ipv6 = 0;
918 else if (unformat (i, "src"))
920 /* Everything in MACIP is "source" but allow this verbosity */
922 else if (unformat (i, "ip %U/%d",
923 unformat_ip6_address, &src_v6address, &src_prefix_length))
925 vec_validate_macip_acl_rules(rules, rule_idx);
926 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
927 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
928 rules[rule_idx].is_ipv6 = 1;
930 else if (unformat (i, "mac %U",
931 my_unformat_mac_address, &src_mac))
933 vec_validate_macip_acl_rules(rules, rule_idx);
934 memcpy (rules[rule_idx].src_mac, &src_mac, 6);
935 memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
937 else if (unformat (i, "mask %U",
938 my_unformat_mac_address, &src_mac))
940 vec_validate_macip_acl_rules(rules, rule_idx);
941 memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
943 else if (unformat (i, "tag %s", &tag))
946 else if (unformat (i, ","))
949 vec_validate_macip_acl_rules(rules, rule_idx);
955 /* Construct the API message */
956 vam->result_ready = 0;
959 n_rules = vec_len(rules);
963 if (n_rules_override >= 0)
964 n_rules = n_rules_override;
966 msg_size += n_rules*sizeof(rules[0]);
968 mp = vl_msg_api_alloc_as_if_client(msg_size);
969 memset (mp, 0, msg_size);
970 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
971 mp->client_index = vam->my_client_index;
972 if ((n_rules > 0) && rules)
973 clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
976 if (vec_len(tag) >= sizeof(mp->tag))
978 tag[sizeof(mp->tag)-1] = 0;
979 _vec_len(tag) = sizeof(mp->tag);
981 clib_memcpy(mp->tag, tag, vec_len(tag));
985 mp->count = htonl(n_rules);
990 /* Wait for a reply... */
996 * List of messages that the api test plugin sends,
997 * and that the data plane plugin processes
999 #define foreach_vpe_api_msg \
1000 _(acl_plugin_get_version, "") \
1001 _(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], ... , ...") \
1002 _(acl_del, "<acl-idx>") \
1003 _(acl_dump, "[<acl-idx>]") \
1004 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
1005 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
1006 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
1007 _(macip_acl_add, "...") \
1008 _(macip_acl_del, "<acl-idx>")\
1009 _(macip_acl_dump, "[<acl-idx>]") \
1010 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
1011 _(macip_acl_interface_get, "")
1015 void acl_vat_api_hookup (vat_main_t *vam)
1017 acl_test_main_t * sm = &acl_test_main;
1018 /* Hook up handlers for replies from the data plane plug-in */
1020 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1022 vl_api_##n##_t_handler, \
1024 vl_api_##n##_t_endian, \
1025 vl_api_##n##_t_print, \
1026 sizeof(vl_api_##n##_t), 1);
1027 foreach_vpe_api_reply_msg;
1030 /* API messages we can send */
1031 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
1032 foreach_vpe_api_msg;
1036 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1037 foreach_vpe_api_msg;
1041 clib_error_t * vat_plugin_register (vat_main_t *vam)
1043 acl_test_main_t * sm = &acl_test_main;
1048 name = format (0, "acl_%08x%c", api_version, 0);
1049 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1051 if (sm->msg_id_base != (u16) ~0)
1052 acl_vat_api_hookup (vam);