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 %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_mask, a->tcp_flags_value);
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);
271 vam->result_ready = 0;
272 mp = vl_msg_api_alloc_as_if_client(msg_size);
273 memset (mp, 0, msg_size);
274 mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
275 mp->client_index = vam->my_client_index;
280 /* 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);
292 vam->result_ready = 0;
293 mp = vl_msg_api_alloc_as_if_client(msg_size);
294 memset (mp, 0, msg_size);
295 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
296 mp->client_index = vam->my_client_index;
301 /* 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;
333 u32 tcpflags, tcpmask;
334 u32 src_prefix_length = 0, dst_prefix_length = 0;
335 ip4_address_t src_v4address, dst_v4address;
336 ip6_address_t src_v6address, dst_v6address;
339 if (!unformat (i, "%d", &acl_index)) {
343 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
345 if (unformat (i, "ipv6"))
347 vec_validate_acl_rules(rules, rule_idx);
348 rules[rule_idx].is_ipv6 = 1;
350 else if (unformat (i, "ipv4"))
352 vec_validate_acl_rules(rules, rule_idx);
353 rules[rule_idx].is_ipv6 = 0;
355 else if (unformat (i, "permit+reflect"))
357 vec_validate_acl_rules(rules, rule_idx);
358 rules[rule_idx].is_permit = 2;
360 else if (unformat (i, "permit"))
362 vec_validate_acl_rules(rules, rule_idx);
363 rules[rule_idx].is_permit = 1;
365 else if (unformat (i, "action %d", &action))
367 vec_validate_acl_rules(rules, rule_idx);
368 rules[rule_idx].is_permit = action;
370 else if (unformat (i, "src %U/%d",
371 unformat_ip4_address, &src_v4address, &src_prefix_length))
373 vec_validate_acl_rules(rules, rule_idx);
374 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
375 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
376 rules[rule_idx].is_ipv6 = 0;
378 else if (unformat (i, "src %U/%d",
379 unformat_ip6_address, &src_v6address, &src_prefix_length))
381 vec_validate_acl_rules(rules, rule_idx);
382 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
383 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
384 rules[rule_idx].is_ipv6 = 1;
386 else if (unformat (i, "dst %U/%d",
387 unformat_ip4_address, &dst_v4address, &dst_prefix_length))
389 vec_validate_acl_rules(rules, rule_idx);
390 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
391 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
392 rules[rule_idx].is_ipv6 = 0;
394 else if (unformat (i, "dst %U/%d",
395 unformat_ip6_address, &dst_v6address, &dst_prefix_length))
397 vec_validate_acl_rules(rules, rule_idx);
398 memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
399 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
400 rules[rule_idx].is_ipv6 = 1;
402 else if (unformat (i, "sport %d-%d", &port1, &port2))
404 vec_validate_acl_rules(rules, rule_idx);
405 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
406 rules[rule_idx].srcport_or_icmptype_last = htons(port2);
408 else if (unformat (i, "sport %d", &port1))
410 vec_validate_acl_rules(rules, rule_idx);
411 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
412 rules[rule_idx].srcport_or_icmptype_last = htons(port1);
414 else if (unformat (i, "dport %d-%d", &port1, &port2))
416 vec_validate_acl_rules(rules, rule_idx);
417 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
418 rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
420 else if (unformat (i, "dport %d", &port1))
422 vec_validate_acl_rules(rules, rule_idx);
423 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
424 rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
426 else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
428 vec_validate_acl_rules(rules, rule_idx);
429 rules[rule_idx].tcp_flags_value = tcpflags;
430 rules[rule_idx].tcp_flags_mask = tcpmask;
432 else if (unformat (i, "proto %d", &proto))
434 vec_validate_acl_rules(rules, rule_idx);
435 rules[rule_idx].proto = proto;
437 else if (unformat (i, "tag %s", &tag))
440 else if (unformat (i, ","))
443 vec_validate_acl_rules(rules, rule_idx);
449 /* Construct the API message */
450 vam->result_ready = 0;
453 n_rules = vec_len(rules);
457 msg_size += n_rules*sizeof(rules[0]);
459 mp = vl_msg_api_alloc_as_if_client(msg_size);
460 memset (mp, 0, msg_size);
461 mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
462 mp->client_index = vam->my_client_index;
464 clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
467 if (vec_len(tag) >= sizeof(mp->tag))
469 tag[sizeof(mp->tag)-1] = 0;
470 _vec_len(tag) = sizeof(mp->tag);
472 clib_memcpy(mp->tag, tag, vec_len(tag));
475 mp->acl_index = ntohl(acl_index);
476 mp->count = htonl(n_rules);
481 /* Wait for a reply... */
485 static int api_acl_del (vat_main_t * vam)
487 unformat_input_t * i = vam->input;
488 vl_api_acl_del_t * mp;
491 if (!unformat (i, "%d", &acl_index)) {
492 errmsg ("missing acl index\n");
496 /* Construct the API message */
498 mp->acl_index = ntohl(acl_index);
503 /* Wait for a reply... */
507 static int api_macip_acl_del (vat_main_t * vam)
509 unformat_input_t * i = vam->input;
510 vl_api_acl_del_t * mp;
513 if (!unformat (i, "%d", &acl_index)) {
514 errmsg ("missing acl index\n");
518 /* Construct the API message */
519 M(MACIP_ACL_DEL, mp);
520 mp->acl_index = ntohl(acl_index);
525 /* Wait for a reply... */
529 static int api_acl_interface_add_del (vat_main_t * vam)
531 unformat_input_t * i = vam->input;
532 vl_api_acl_interface_add_del_t * mp;
533 u32 sw_if_index = ~0;
538 // acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
540 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
542 if (unformat (i, "%d", &acl_index))
549 /* Parse args required to build the message */
550 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
551 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
553 else if (unformat (i, "sw_if_index %d", &sw_if_index))
555 else if (unformat (i, "add"))
557 else if (unformat (i, "del"))
559 else if (unformat (i, "acl %d", &acl_index))
561 else if (unformat (i, "input"))
563 else if (unformat (i, "output"))
569 if (sw_if_index == ~0) {
570 errmsg ("missing interface name / explicit sw_if_index number \n");
574 if (acl_index == ~0) {
575 errmsg ("missing ACL index\n");
581 /* Construct the API message */
582 M(ACL_INTERFACE_ADD_DEL, mp);
583 mp->acl_index = ntohl(acl_index);
584 mp->sw_if_index = ntohl(sw_if_index);
586 mp->is_input = is_input;
591 /* Wait for a reply... */
595 static int api_macip_acl_interface_add_del (vat_main_t * vam)
597 unformat_input_t * i = vam->input;
598 vl_api_macip_acl_interface_add_del_t * mp;
599 u32 sw_if_index = ~0;
603 /* Parse args required to build the message */
604 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
605 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
607 else if (unformat (i, "sw_if_index %d", &sw_if_index))
609 else if (unformat (i, "add"))
611 else if (unformat (i, "del"))
613 else if (unformat (i, "acl %d", &acl_index))
619 if (sw_if_index == ~0) {
620 errmsg ("missing interface name / explicit sw_if_index number \n");
624 if (acl_index == ~0) {
625 errmsg ("missing ACL index\n");
631 /* Construct the API message */
632 M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
633 mp->acl_index = ntohl(acl_index);
634 mp->sw_if_index = ntohl(sw_if_index);
640 /* Wait for a reply... */
644 static int api_acl_interface_set_acl_list (vat_main_t * vam)
646 unformat_input_t * i = vam->input;
647 vl_api_acl_interface_set_acl_list_t * mp;
648 u32 sw_if_index = ~0;
654 // acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
656 /* Parse args required to build the message */
657 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
658 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
660 else if (unformat (i, "sw_if_index %d", &sw_if_index))
662 else if (unformat (i, "%d", &acl_index))
665 vec_add1(inacls, htonl(acl_index));
667 vec_add1(outacls, htonl(acl_index));
669 else if (unformat (i, "acl %d", &acl_index))
671 else if (unformat (i, "input"))
673 else if (unformat (i, "output"))
679 if (sw_if_index == ~0) {
680 errmsg ("missing interface name / explicit sw_if_index number \n");
684 /* Construct the API message */
685 M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
686 mp->sw_if_index = ntohl(sw_if_index);
687 mp->n_input = vec_len(inacls);
688 mp->count = vec_len(inacls) + vec_len(outacls);
689 vec_append(inacls, outacls);
690 if (vec_len(inacls) > 0)
691 clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
696 /* Wait for a reply... */
701 static int api_acl_interface_list_dump (vat_main_t * vam)
703 unformat_input_t * i = vam->input;
704 u32 sw_if_index = ~0;
705 vl_api_acl_interface_list_dump_t * mp;
707 /* Parse args required to build the message */
708 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
709 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
711 else if (unformat (i, "sw_if_index %d", &sw_if_index))
717 /* Construct the API message */
718 M(ACL_INTERFACE_LIST_DUMP, mp);
719 mp->sw_if_index = ntohl (sw_if_index);
724 /* Wait for a reply... */
728 static int api_acl_dump (vat_main_t * vam)
730 unformat_input_t * i = vam->input;
732 vl_api_acl_dump_t * mp;
734 /* Parse args required to build the message */
735 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
736 if (unformat (i, "%d", &acl_index))
742 /* Construct the API message */
744 mp->acl_index = ntohl (acl_index);
749 /* Wait for a reply... */
753 static int api_macip_acl_dump (vat_main_t * vam)
755 unformat_input_t * i = vam->input;
757 vl_api_acl_dump_t * mp;
759 /* Parse args required to build the message */
760 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
761 if (unformat (i, "%d", &acl_index))
767 /* Construct the API message */
768 M(MACIP_ACL_DUMP, mp);
769 mp->acl_index = ntohl (acl_index);
774 /* Wait for a reply... */
778 #define vec_validate_macip_acl_rules(v, idx) \
780 if (vec_len(v) < idx+1) { \
781 vec_validate(v, idx); \
782 v[idx].is_permit = 0x1; \
787 static int api_macip_acl_add (vat_main_t * vam)
789 acl_test_main_t * sm = &acl_test_main;
790 unformat_input_t * i = vam->input;
791 vl_api_macip_acl_add_t * mp;
792 u32 msg_size = sizeof (*mp); /* without the rules */
794 vl_api_macip_acl_rule_t *rules = 0;
797 u32 src_prefix_length = 0;
799 ip4_address_t src_v4address;
800 ip6_address_t src_v6address;
803 u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
805 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
807 if (unformat (i, "ipv6"))
809 vec_validate_macip_acl_rules(rules, rule_idx);
810 rules[rule_idx].is_ipv6 = 1;
812 else if (unformat (i, "ipv4"))
814 vec_validate_macip_acl_rules(rules, rule_idx);
815 rules[rule_idx].is_ipv6 = 1;
817 else if (unformat (i, "permit"))
819 vec_validate_macip_acl_rules(rules, rule_idx);
820 rules[rule_idx].is_permit = 1;
822 else if (unformat (i, "deny"))
824 vec_validate_macip_acl_rules(rules, rule_idx);
825 rules[rule_idx].is_permit = 0;
827 else if (unformat (i, "action %d", &action))
829 vec_validate_macip_acl_rules(rules, rule_idx);
830 rules[rule_idx].is_permit = action;
832 else if (unformat (i, "ip %U/%d",
833 unformat_ip4_address, &src_v4address, &src_prefix_length))
835 vec_validate_macip_acl_rules(rules, rule_idx);
836 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
837 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
838 rules[rule_idx].is_ipv6 = 0;
840 else if (unformat (i, "ip %U/%d",
841 unformat_ip6_address, &src_v6address, &src_prefix_length))
843 vec_validate_macip_acl_rules(rules, rule_idx);
844 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
845 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
846 rules[rule_idx].is_ipv6 = 1;
848 else if (unformat (i, "mac %U",
849 my_unformat_mac_address, &src_mac))
851 vec_validate_macip_acl_rules(rules, rule_idx);
852 memcpy (rules[rule_idx].src_mac, &src_mac, 6);
853 memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
855 else if (unformat (i, "mask %U",
856 my_unformat_mac_address, &src_mac))
858 vec_validate_macip_acl_rules(rules, rule_idx);
859 memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
861 else if (unformat (i, "tag %s", &tag))
864 else if (unformat (i, ","))
867 vec_validate_macip_acl_rules(rules, rule_idx);
873 /* Construct the API message */
874 vam->result_ready = 0;
877 n_rules = vec_len(rules);
881 msg_size += n_rules*sizeof(rules[0]);
883 mp = vl_msg_api_alloc_as_if_client(msg_size);
884 memset (mp, 0, msg_size);
885 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
886 mp->client_index = vam->my_client_index;
888 clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
891 if (vec_len(tag) >= sizeof(mp->tag))
893 tag[sizeof(mp->tag)-1] = 0;
894 _vec_len(tag) = sizeof(mp->tag);
896 clib_memcpy(mp->tag, tag, vec_len(tag));
900 mp->count = htonl(n_rules);
905 /* Wait for a reply... */
910 * List of messages that the api test plugin sends,
911 * and that the data plane plugin processes
913 #define foreach_vpe_api_msg \
914 _(acl_plugin_get_version, "") \
915 _(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], ... , ...") \
916 _(acl_del, "<acl-idx>") \
917 _(acl_dump, "[<acl-idx>]") \
918 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
919 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
920 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
921 _(macip_acl_add, "...") \
922 _(macip_acl_del, "<acl-idx>")\
923 _(macip_acl_dump, "[<acl-idx>]") \
924 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
925 _(macip_acl_interface_get, "")
929 void acl_vat_api_hookup (vat_main_t *vam)
931 acl_test_main_t * sm = &acl_test_main;
932 /* Hook up handlers for replies from the data plane plug-in */
934 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
936 vl_api_##n##_t_handler, \
938 vl_api_##n##_t_endian, \
939 vl_api_##n##_t_print, \
940 sizeof(vl_api_##n##_t), 1);
941 foreach_vpe_api_reply_msg;
944 /* API messages we can send */
945 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
950 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
955 clib_error_t * vat_plugin_register (vat_main_t *vam)
957 acl_test_main_t * sm = &acl_test_main;
962 name = format (0, "acl_%08x%c", api_version, 0);
963 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
965 if (sm->msg_id_base != (u16) ~0)
966 acl_vat_api_hookup (vam);