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 <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26 #include <arpa/inet.h>
28 #define __plugin_msg_base acl_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
31 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
33 /* Declare message IDs */
34 #include <acl/acl_msg_enum.h>
36 /* define message structures */
38 #include <acl/acl_all_api_h.h>
41 /* define message structures */
43 #include <acl/acl_all_api_h.h>
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
49 #include <acl/acl_all_api_h.h>
52 /* Get the API version number. */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <acl/acl_all_api_h.h>
58 /* API message ID base */
63 acl_test_main_t acl_test_main;
65 #define foreach_standard_reply_retval_handler \
67 _(acl_interface_add_del_reply) \
68 _(macip_acl_interface_add_del_reply) \
69 _(acl_interface_set_acl_list_reply) \
70 _(acl_interface_set_etype_whitelist_reply) \
71 _(macip_acl_del_reply)
73 #define foreach_reply_retval_aclindex_handler \
74 _(acl_add_replace_reply) \
75 _(macip_acl_add_reply) \
76 _(macip_acl_add_replace_reply)
79 static void vl_api_##n##_t_handler \
80 (vl_api_##n##_t * mp) \
82 vat_main_t * vam = acl_test_main.vat_main; \
83 i32 retval = ntohl(mp->retval); \
84 if (vam->async_mode) { \
85 vam->async_errors += (retval < 0); \
87 vam->retval = retval; \
88 vam->result_ready = 1; \
91 foreach_standard_reply_retval_handler;
95 static void vl_api_##n##_t_handler \
96 (vl_api_##n##_t * mp) \
98 vat_main_t * vam = acl_test_main.vat_main; \
99 i32 retval = ntohl(mp->retval); \
100 if (vam->async_mode) { \
101 vam->async_errors += (retval < 0); \
103 clib_warning("ACL index: %d", ntohl(mp->acl_index)); \
104 vam->retval = retval; \
105 vam->result_ready = 1; \
108 foreach_reply_retval_aclindex_handler;
111 /* These two ought to be in a library somewhere but they aren't */
113 my_unformat_mac_address (unformat_input_t * input, va_list * args)
115 u8 *a = va_arg (*args, u8 *);
116 return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
121 my_format_mac_address (u8 * s, va_list * args)
123 u8 *a = va_arg (*args, u8 *);
124 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
125 a[0], a[1], a[2], a[3], a[4], a[5]);
130 static void vl_api_acl_plugin_get_version_reply_t_handler
131 (vl_api_acl_plugin_get_version_reply_t * mp)
133 vat_main_t * vam = acl_test_main.vat_main;
134 clib_warning("ACL plugin version: %d.%d", ntohl(mp->major), ntohl(mp->minor));
135 vam->result_ready = 1;
138 static void vl_api_acl_interface_list_details_t_handler
139 (vl_api_acl_interface_list_details_t * mp)
142 vat_main_t * vam = acl_test_main.vat_main;
144 vl_api_acl_interface_list_details_t_endian(mp);
145 out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
146 out = format(out, " input ");
147 for(i=0; i<mp->count; i++) {
148 if (i == mp->n_input)
149 out = format(out, "\n output ");
150 out = format(out, "%d ", ntohl (mp->acls[i]));
152 out = format(out, "\n");
153 clib_warning("%s", out);
155 vam->result_ready = 1;
158 static void vl_api_acl_interface_etype_whitelist_details_t_handler
159 (vl_api_acl_interface_etype_whitelist_details_t * mp)
162 vat_main_t * vam = acl_test_main.vat_main;
164 vl_api_acl_interface_etype_whitelist_details_t_endian(mp);
165 out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
166 out = format(out, " input ");
167 for(i=0; i<mp->count; i++) {
168 if (i == mp->n_input)
169 out = format(out, "\n output ");
170 out = format(out, "%04x ", ntohs(mp->whitelist[i]));
172 out = format(out, "\n");
173 clib_warning("%s", out);
175 vam->result_ready = 1;
181 vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
183 int af = a->is_ipv6 ? AF_INET6 : AF_INET;
184 u8 src[INET6_ADDRSTRLEN];
185 u8 dst[INET6_ADDRSTRLEN];
186 inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
187 inet_ntop(af, a->dst_ip_addr, (void *)dst, sizeof(dst));
189 out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
190 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
191 src, a->src_ip_prefix_len,
192 dst, a->dst_ip_prefix_len,
194 a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
195 a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
196 a->tcp_flags_value, a->tcp_flags_mask);
202 static void vl_api_acl_details_t_handler
203 (vl_api_acl_details_t * mp)
206 vat_main_t * vam = acl_test_main.vat_main;
207 vl_api_acl_details_t_endian(mp);
209 out = format(0, "acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
210 for(i=0; i<mp->count; i++) {
211 out = format(out, " ");
212 out = vl_api_acl_rule_t_pretty_format(out, &mp->r[i]);
213 out = format(out, "%s\n", i<mp->count-1 ? "," : "");
215 clib_warning("%s", out);
217 vam->result_ready = 1;
221 vl_api_macip_acl_rule_t_pretty_format (u8 *out, vl_api_macip_acl_rule_t * a)
223 int af = a->is_ipv6 ? AF_INET6 : AF_INET;
224 u8 src[INET6_ADDRSTRLEN];
225 inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
227 out = format(out, "%s action %d ip %s/%d mac %U mask %U",
228 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
229 src, a->src_ip_prefix_len,
230 my_format_mac_address, a->src_mac,
231 my_format_mac_address, a->src_mac_mask);
236 static void vl_api_macip_acl_details_t_handler
237 (vl_api_macip_acl_details_t * mp)
240 vat_main_t * vam = acl_test_main.vat_main;
241 vl_api_macip_acl_details_t_endian(mp);
242 u8 *out = format(0,"MACIP acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
243 for(i=0; i<mp->count; i++) {
244 out = format(out, " ");
245 out = vl_api_macip_acl_rule_t_pretty_format(out, &mp->r[i]);
246 out = format(out, "%s\n", i<mp->count-1 ? "," : "");
248 clib_warning("%s", out);
250 vam->result_ready = 1;
253 static void vl_api_macip_acl_interface_get_reply_t_handler
254 (vl_api_macip_acl_interface_get_reply_t * mp)
257 vat_main_t * vam = acl_test_main.vat_main;
258 u8 *out = format(0, "sw_if_index with MACIP ACL count: %d\n", ntohl(mp->count));
259 for(i=0; i<ntohl(mp->count); i++) {
260 out = format(out, " macip_acl_interface_add_del sw_if_index %d add acl %d\n", i, ntohl(mp->acls[i]));
262 out = format(out, "\n");
263 clib_warning("%s", out);
265 vam->result_ready = 1;
268 static void vl_api_acl_plugin_control_ping_reply_t_handler
269 (vl_api_acl_plugin_control_ping_reply_t * mp)
271 vat_main_t *vam = &vat_main;
272 i32 retval = ntohl (mp->retval);
275 vam->async_errors += (retval < 0);
279 vam->retval = retval;
280 vam->result_ready = 1;
286 * Table of message reply handlers, must include boilerplate handlers
289 #define foreach_vpe_api_reply_msg \
290 _(ACL_ADD_REPLACE_REPLY, acl_add_replace_reply) \
291 _(ACL_DEL_REPLY, acl_del_reply) \
292 _(ACL_INTERFACE_ADD_DEL_REPLY, acl_interface_add_del_reply) \
293 _(ACL_INTERFACE_SET_ACL_LIST_REPLY, acl_interface_set_acl_list_reply) \
294 _(ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY, acl_interface_set_etype_whitelist_reply) \
295 _(ACL_INTERFACE_ETYPE_WHITELIST_DETAILS, acl_interface_etype_whitelist_details) \
296 _(ACL_INTERFACE_LIST_DETAILS, acl_interface_list_details) \
297 _(ACL_DETAILS, acl_details) \
298 _(MACIP_ACL_ADD_REPLY, macip_acl_add_reply) \
299 _(MACIP_ACL_ADD_REPLACE_REPLY, macip_acl_add_replace_reply) \
300 _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
301 _(MACIP_ACL_DETAILS, macip_acl_details) \
302 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply) \
303 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply) \
304 _(ACL_PLUGIN_CONTROL_PING_REPLY, acl_plugin_control_ping_reply) \
305 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
307 static int api_acl_plugin_get_version (vat_main_t * vam)
309 acl_test_main_t * sm = &acl_test_main;
310 vl_api_acl_plugin_get_version_t * mp;
311 u32 msg_size = sizeof(*mp);
314 vam->result_ready = 0;
315 mp = vl_msg_api_alloc_as_if_client(msg_size);
316 memset (mp, 0, msg_size);
317 mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
318 mp->client_index = vam->my_client_index;
323 /* Wait for a reply... */
328 static int api_macip_acl_interface_get (vat_main_t * vam)
330 acl_test_main_t * sm = &acl_test_main;
331 vl_api_acl_plugin_get_version_t * mp;
332 u32 msg_size = sizeof(*mp);
335 vam->result_ready = 0;
336 mp = vl_msg_api_alloc_as_if_client(msg_size);
337 memset (mp, 0, msg_size);
338 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
339 mp->client_index = vam->my_client_index;
344 /* Wait for a reply... */
349 #define vec_validate_acl_rules(v, idx) \
351 if (vec_len(v) < idx+1) { \
352 vec_validate(v, idx); \
353 v[idx].is_permit = 0x1; \
354 v[idx].srcport_or_icmptype_last = 0xffff; \
355 v[idx].dstport_or_icmpcode_last = 0xffff; \
360 static int api_acl_add_replace (vat_main_t * vam)
362 acl_test_main_t * sm = &acl_test_main;
363 unformat_input_t * i = vam->input;
364 vl_api_acl_add_replace_t * mp;
366 u32 msg_size = sizeof (*mp); /* without the rules */
368 vl_api_acl_rule_t *rules = 0;
371 int n_rules_override = -1;
376 u32 tcpflags, tcpmask;
377 u32 src_prefix_length = 0, dst_prefix_length = 0;
378 ip4_address_t src_v4address, dst_v4address;
379 ip6_address_t src_v6address, dst_v6address;
383 if (!unformat (i, "%d", &acl_index)) {
387 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
389 if (unformat (i, "ipv6"))
391 vec_validate_acl_rules(rules, rule_idx);
392 rules[rule_idx].is_ipv6 = 1;
394 else if (unformat (i, "ipv4"))
396 vec_validate_acl_rules(rules, rule_idx);
397 rules[rule_idx].is_ipv6 = 0;
399 else if (unformat (i, "permit+reflect"))
401 vec_validate_acl_rules(rules, rule_idx);
402 rules[rule_idx].is_permit = 2;
404 else if (unformat (i, "permit"))
406 vec_validate_acl_rules(rules, rule_idx);
407 rules[rule_idx].is_permit = 1;
409 else if (unformat (i, "deny"))
411 vec_validate_acl_rules(rules, rule_idx);
412 rules[rule_idx].is_permit = 0;
414 else if (unformat (i, "count %d", &n_rules_override))
416 /* we will use this later */
418 else if (unformat (i, "action %d", &action))
420 vec_validate_acl_rules(rules, rule_idx);
421 rules[rule_idx].is_permit = action;
423 else if (unformat (i, "src %U/%d",
424 unformat_ip4_address, &src_v4address, &src_prefix_length))
426 vec_validate_acl_rules(rules, rule_idx);
427 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
428 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
429 rules[rule_idx].is_ipv6 = 0;
431 else if (unformat (i, "src %U/%d",
432 unformat_ip6_address, &src_v6address, &src_prefix_length))
434 vec_validate_acl_rules(rules, rule_idx);
435 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
436 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
437 rules[rule_idx].is_ipv6 = 1;
439 else if (unformat (i, "dst %U/%d",
440 unformat_ip4_address, &dst_v4address, &dst_prefix_length))
442 vec_validate_acl_rules(rules, rule_idx);
443 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
444 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
445 rules[rule_idx].is_ipv6 = 0;
447 else if (unformat (i, "dst %U/%d",
448 unformat_ip6_address, &dst_v6address, &dst_prefix_length))
450 vec_validate_acl_rules(rules, rule_idx);
451 memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
452 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
453 rules[rule_idx].is_ipv6 = 1;
455 else if (unformat (i, "sport %d-%d", &port1, &port2))
457 vec_validate_acl_rules(rules, rule_idx);
458 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
459 rules[rule_idx].srcport_or_icmptype_last = htons(port2);
461 else if (unformat (i, "sport %d", &port1))
463 vec_validate_acl_rules(rules, rule_idx);
464 rules[rule_idx].srcport_or_icmptype_first = htons(port1);
465 rules[rule_idx].srcport_or_icmptype_last = htons(port1);
467 else if (unformat (i, "dport %d-%d", &port1, &port2))
469 vec_validate_acl_rules(rules, rule_idx);
470 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
471 rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
473 else if (unformat (i, "dport %d", &port1))
475 vec_validate_acl_rules(rules, rule_idx);
476 rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
477 rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
479 else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
481 vec_validate_acl_rules(rules, rule_idx);
482 rules[rule_idx].tcp_flags_value = tcpflags;
483 rules[rule_idx].tcp_flags_mask = tcpmask;
485 else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
487 vec_validate_acl_rules(rules, rule_idx);
488 rules[rule_idx].tcp_flags_value = tcpflags;
489 rules[rule_idx].tcp_flags_mask = tcpmask;
491 else if (unformat (i, "proto %d", &proto))
493 vec_validate_acl_rules(rules, rule_idx);
494 rules[rule_idx].proto = proto;
496 else if (unformat (i, "tag %s", &tag))
499 else if (unformat (i, ","))
502 vec_validate_acl_rules(rules, rule_idx);
508 /* Construct the API message */
509 vam->result_ready = 0;
512 n_rules = vec_len(rules);
516 if (n_rules_override >= 0)
517 n_rules = n_rules_override;
519 msg_size += n_rules*sizeof(rules[0]);
521 mp = vl_msg_api_alloc_as_if_client(msg_size);
522 memset (mp, 0, msg_size);
523 mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
524 mp->client_index = vam->my_client_index;
525 if ((n_rules > 0) && rules)
526 clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
529 if (vec_len(tag) >= sizeof(mp->tag))
531 tag[sizeof(mp->tag)-1] = 0;
532 _vec_len(tag) = sizeof(mp->tag);
534 clib_memcpy(mp->tag, tag, vec_len(tag));
537 mp->acl_index = ntohl(acl_index);
538 mp->count = htonl(n_rules);
543 /* Wait for a reply... */
550 * Read the series of ACL entries from file in the following format:
553 @0.0.0.0/1 131.179.121.0/24 0 : 65535 0 : 65535 0x00/0x00 0x0000/0x0000
554 @128.0.0.0/1 85.54.226.0/23 0 : 65535 0 : 65535 0x00/0x00 0x0000/0x0000
555 @128.0.0.0/1 85.54.48.0/23 0 : 65535 0 : 65535 0x00/0x00 0x0000/0x0000
556 @128.0.0.0/1 31.237.44.0/23 0 : 65535 0 : 65535 0x00/0x00 0x0000/0x0000
557 @0.0.0.0/1 255.84.184.0/23 0 : 65535 0 : 65535 0x00/0x00 0x0000/0x0000
558 @132.92.0.0/16 0.0.0.0/0 0 : 65535 0 : 65535 0x01/0xFF 0x0000/0x0000
564 api_acl_add_replace_from_file (vat_main_t * vam)
567 unformat_input_t * input = vam->input;
568 acl_test_main_t * sm = &acl_test_main;
569 vl_api_acl_add_replace_t * mp;
571 u32 msg_size = sizeof (*mp); /* without the rules */
573 vl_api_acl_rule_t *rules = 0;
577 int append_default_permit = 0;
578 u32 tcpflags = 0, tcpmask = 0;
579 ip4_address_t src_v4address, dst_v4address;
582 char *file_name = NULL;
583 unformat_input_t file_input;
585 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
587 if (unformat (input, "filename %s", &file_name))
589 /* we will use this later */
591 else if (unformat (input, "acl-index %d", &acl_index))
593 /* we will try to replace an existing ACL */
595 else if (unformat (input, "permit+reflect"))
599 else if (unformat (input, "permit"))
603 else if (unformat (input, "append-default-permit"))
605 append_default_permit = 1;
611 fd = open(file_name, O_RDONLY);
614 clib_warning("Could not open file '%s'");
618 /* input from file */
620 unformat_init_clib_file(input, fd);
622 unsigned sport_low, sport_high, dport_low, dport_high;
623 unsigned proto, protomask;
624 u32 src_prefix_length, dst_prefix_length;
625 u32 unused1, unused2;
627 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
629 if (!unformat(input, "@%U/%d\t%U/%d\t%d : %d\t%d : %d\t0x%x/0x%x\t0x%x/0x%x",
630 unformat_ip4_address, &src_v4address, &src_prefix_length,
631 unformat_ip4_address, &dst_v4address, &dst_prefix_length,
632 &sport_low, &sport_high, &dport_low, &dport_high, &proto, &protomask, &unused1, &unused2)) {
633 clib_warning("Error parsing");
638 vec_validate_acl_rules(rules, rule_idx);
640 rules[rule_idx].is_ipv6 = 0;
641 rules[rule_idx].is_permit = is_permit;
642 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
643 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
644 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
645 rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
646 rules[rule_idx].srcport_or_icmptype_first = htons(sport_low);
647 rules[rule_idx].srcport_or_icmptype_last = htons(sport_high);
648 rules[rule_idx].dstport_or_icmpcode_first = htons(dport_low);
649 rules[rule_idx].dstport_or_icmpcode_last = htons(dport_high);
650 rules[rule_idx].tcp_flags_value = tcpflags;
651 rules[rule_idx].tcp_flags_mask = tcpmask;
652 rules[rule_idx].proto = proto;
655 rules[rule_idx].is_permit = is_permit;
657 if (append_default_permit) {
659 vec_validate_acl_rules(rules, rule_idx);
661 rules[rule_idx].is_ipv6 = 0;
662 rules[rule_idx].is_permit = is_permit == 2 ? 2 : 1;
664 src_v4address.data[0]=0;
665 src_v4address.data[1]=0;
666 src_v4address.data[2]=0;
667 src_v4address.data[3]=0;
668 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
669 rules[rule_idx].src_ip_prefix_len = 0;
671 dst_v4address.data[0]=0;
672 dst_v4address.data[1]=0;
673 dst_v4address.data[2]=0;
674 dst_v4address.data[3]=0;
675 memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
676 rules[rule_idx].dst_ip_prefix_len = 0;
678 rules[rule_idx].srcport_or_icmptype_first = htons(0);
679 rules[rule_idx].srcport_or_icmptype_last = htons(65535);
680 rules[rule_idx].dstport_or_icmpcode_first = htons(0);
681 rules[rule_idx].dstport_or_icmpcode_last = htons(65535);
682 rules[rule_idx].tcp_flags_value = 0;
683 rules[rule_idx].tcp_flags_mask = 0;
684 rules[rule_idx].proto = 0;
687 /* Construct the API message */
689 vam->result_ready = 0;
691 n_rules = vec_len(rules);
693 msg_size += n_rules*sizeof(rules[0]);
695 mp = vl_msg_api_alloc_as_if_client(msg_size);
696 memset (mp, 0, msg_size);
697 mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
698 mp->client_index = vam->my_client_index;
700 clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
701 mp->acl_index = ntohl(acl_index);
702 mp->count = htonl(n_rules);
707 /* Wait for a reply... */
718 static int api_acl_del (vat_main_t * vam)
720 unformat_input_t * i = vam->input;
721 vl_api_acl_del_t * mp;
725 if (!unformat (i, "%d", &acl_index)) {
726 errmsg ("missing acl index\n");
730 /* Construct the API message */
732 mp->acl_index = ntohl(acl_index);
737 /* Wait for a reply... */
742 static int api_macip_acl_del (vat_main_t * vam)
744 unformat_input_t * i = vam->input;
745 vl_api_acl_del_t * mp;
749 if (!unformat (i, "%d", &acl_index)) {
750 errmsg ("missing acl index\n");
754 /* Construct the API message */
755 M(MACIP_ACL_DEL, mp);
756 mp->acl_index = ntohl(acl_index);
761 /* Wait for a reply... */
766 static int api_acl_interface_add_del (vat_main_t * vam)
768 unformat_input_t * i = vam->input;
769 vl_api_acl_interface_add_del_t * mp;
770 u32 sw_if_index = ~0;
776 // acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
778 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
780 if (unformat (i, "%d", &acl_index))
787 /* Parse args required to build the message */
788 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
789 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
791 else if (unformat (i, "sw_if_index %d", &sw_if_index))
793 else if (unformat (i, "add"))
795 else if (unformat (i, "del"))
797 else if (unformat (i, "acl %d", &acl_index))
799 else if (unformat (i, "input"))
801 else if (unformat (i, "output"))
807 if (sw_if_index == ~0) {
808 errmsg ("missing interface name / explicit sw_if_index number \n");
812 if (acl_index == ~0) {
813 errmsg ("missing ACL index\n");
819 /* Construct the API message */
820 M(ACL_INTERFACE_ADD_DEL, mp);
821 mp->acl_index = ntohl(acl_index);
822 mp->sw_if_index = ntohl(sw_if_index);
824 mp->is_input = is_input;
829 /* Wait for a reply... */
834 static int api_macip_acl_interface_add_del (vat_main_t * vam)
836 unformat_input_t * i = vam->input;
837 vl_api_macip_acl_interface_add_del_t * mp;
838 u32 sw_if_index = ~0;
843 /* Parse args required to build the message */
844 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
845 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
847 else if (unformat (i, "sw_if_index %d", &sw_if_index))
849 else if (unformat (i, "add"))
851 else if (unformat (i, "del"))
853 else if (unformat (i, "acl %d", &acl_index))
859 if (sw_if_index == ~0) {
860 errmsg ("missing interface name / explicit sw_if_index number \n");
864 if (acl_index == ~0) {
865 errmsg ("missing ACL index\n");
871 /* Construct the API message */
872 M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
873 mp->acl_index = ntohl(acl_index);
874 mp->sw_if_index = ntohl(sw_if_index);
880 /* Wait for a reply... */
885 static int api_acl_interface_set_acl_list (vat_main_t * vam)
887 unformat_input_t * i = vam->input;
888 vl_api_acl_interface_set_acl_list_t * mp;
889 u32 sw_if_index = ~0;
896 // acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
898 /* Parse args required to build the message */
899 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
900 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
902 else if (unformat (i, "sw_if_index %d", &sw_if_index))
904 else if (unformat (i, "%d", &acl_index))
907 vec_add1(inacls, htonl(acl_index));
909 vec_add1(outacls, htonl(acl_index));
911 else if (unformat (i, "acl %d", &acl_index))
913 else if (unformat (i, "input"))
915 else if (unformat (i, "output"))
921 if (sw_if_index == ~0) {
922 errmsg ("missing interface name / explicit sw_if_index number \n");
926 /* Construct the API message */
927 M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
928 mp->sw_if_index = ntohl(sw_if_index);
929 mp->n_input = vec_len(inacls);
930 mp->count = vec_len(inacls) + vec_len(outacls);
931 vec_append(inacls, outacls);
932 if (vec_len(inacls) > 0)
933 clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
938 /* Wait for a reply... */
943 static int api_acl_interface_set_etype_whitelist (vat_main_t * vam)
945 unformat_input_t * i = vam->input;
946 vl_api_acl_interface_set_etype_whitelist_t * mp;
947 u32 sw_if_index = ~0;
954 // acl_interface_set_etype_whitelist <intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]
956 /* Parse args required to build the message */
957 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
958 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
960 else if (unformat (i, "sw_if_index %d", &sw_if_index))
962 else if (unformat (i, "%x", ðertype))
964 ethertype = ethertype & 0xffff;
966 vec_add1(etypes_in, htons(ethertype));
968 vec_add1(etypes_out, htons(ethertype));
970 else if (unformat (i, "input"))
972 else if (unformat (i, "output"))
978 if (sw_if_index == ~0) {
979 errmsg ("missing interface name / explicit sw_if_index number \n");
983 /* Construct the API message */
984 M2(ACL_INTERFACE_SET_ETYPE_WHITELIST, mp, sizeof(u32) * (vec_len(etypes_in) + vec_len(etypes_out)));
985 mp->sw_if_index = ntohl(sw_if_index);
986 mp->n_input = vec_len(etypes_in);
987 mp->count = vec_len(etypes_in) + vec_len(etypes_out);
988 vec_append(etypes_in, etypes_out);
989 if (vec_len(etypes_in) > 0)
990 clib_memcpy(mp->whitelist, etypes_in, vec_len(etypes_in)*sizeof(etypes_in[0]));
995 /* Wait for a reply... */
1001 api_acl_send_control_ping(vat_main_t *vam)
1003 vl_api_acl_plugin_control_ping_t *mp_ping;
1005 M(ACL_PLUGIN_CONTROL_PING, mp_ping);
1010 static int api_acl_interface_list_dump (vat_main_t * vam)
1012 unformat_input_t * i = vam->input;
1013 u32 sw_if_index = ~0;
1014 vl_api_acl_interface_list_dump_t * mp;
1017 /* Parse args required to build the message */
1018 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1019 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1021 else if (unformat (i, "sw_if_index %d", &sw_if_index))
1027 /* Construct the API message */
1028 M(ACL_INTERFACE_LIST_DUMP, mp);
1029 mp->sw_if_index = ntohl (sw_if_index);
1034 /* Use control ping for synchronization */
1035 api_acl_send_control_ping(vam);
1037 /* Wait for a reply... */
1042 static int api_acl_dump (vat_main_t * vam)
1044 unformat_input_t * i = vam->input;
1046 vl_api_acl_dump_t * mp;
1049 /* Parse args required to build the message */
1050 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1051 if (unformat (i, "%d", &acl_index))
1057 /* Construct the API message */
1059 mp->acl_index = ntohl (acl_index);
1064 /* Use control ping for synchronization */
1065 api_acl_send_control_ping(vam);
1067 /* Wait for a reply... */
1072 static int api_macip_acl_dump (vat_main_t * vam)
1074 unformat_input_t * i = vam->input;
1076 vl_api_acl_dump_t * mp;
1079 /* Parse args required to build the message */
1080 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1081 if (unformat (i, "%d", &acl_index))
1087 /* Construct the API message */
1088 M(MACIP_ACL_DUMP, mp);
1089 mp->acl_index = ntohl (acl_index);
1094 /* Use control ping for synchronization */
1095 api_acl_send_control_ping(vam);
1097 /* Wait for a reply... */
1102 static int api_acl_interface_etype_whitelist_dump (vat_main_t * vam)
1104 unformat_input_t * i = vam->input;
1105 u32 sw_if_index = ~0;
1106 vl_api_acl_interface_etype_whitelist_dump_t * mp;
1109 /* Parse args required to build the message */
1110 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1111 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1113 else if (unformat (i, "sw_if_index %d", &sw_if_index))
1119 /* Construct the API message */
1120 M(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, mp);
1121 mp->sw_if_index = ntohl (sw_if_index);
1126 /* Use control ping for synchronization */
1127 api_acl_send_control_ping(vam);
1129 /* Wait for a reply... */
1135 #define vec_validate_macip_acl_rules(v, idx) \
1137 if (vec_len(v) < idx+1) { \
1138 vec_validate(v, idx); \
1139 v[idx].is_permit = 0x1; \
1144 static int api_macip_acl_add (vat_main_t * vam)
1146 acl_test_main_t * sm = &acl_test_main;
1147 unformat_input_t * i = vam->input;
1148 vl_api_macip_acl_add_t * mp;
1149 u32 msg_size = sizeof (*mp); /* without the rules */
1151 vl_api_macip_acl_rule_t *rules = 0;
1154 int n_rules_override = -1;
1155 u32 src_prefix_length = 0;
1157 ip4_address_t src_v4address;
1158 ip6_address_t src_v6address;
1161 u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1164 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1166 if (unformat (i, "ipv6"))
1168 vec_validate_macip_acl_rules(rules, rule_idx);
1169 rules[rule_idx].is_ipv6 = 1;
1171 else if (unformat (i, "ipv4"))
1173 vec_validate_macip_acl_rules(rules, rule_idx);
1174 rules[rule_idx].is_ipv6 = 0;
1176 else if (unformat (i, "permit"))
1178 vec_validate_macip_acl_rules(rules, rule_idx);
1179 rules[rule_idx].is_permit = 1;
1181 else if (unformat (i, "deny"))
1183 vec_validate_macip_acl_rules(rules, rule_idx);
1184 rules[rule_idx].is_permit = 0;
1186 else if (unformat (i, "count %d", &n_rules_override))
1188 /* we will use this later */
1190 else if (unformat (i, "action %d", &action))
1192 vec_validate_macip_acl_rules(rules, rule_idx);
1193 rules[rule_idx].is_permit = action;
1195 else if (unformat (i, "ip %U/%d",
1196 unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1197 unformat (i, "ip %U",
1198 unformat_ip4_address, &src_v4address))
1200 if (src_prefix_length == 0)
1201 src_prefix_length = 32;
1202 vec_validate_macip_acl_rules(rules, rule_idx);
1203 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1204 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1205 rules[rule_idx].is_ipv6 = 0;
1207 else if (unformat (i, "src"))
1209 /* Everything in MACIP is "source" but allow this verbosity */
1211 else if (unformat (i, "ip %U/%d",
1212 unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1213 unformat (i, "ip %U",
1214 unformat_ip6_address, &src_v6address))
1216 if (src_prefix_length == 0)
1217 src_prefix_length = 128;
1218 vec_validate_macip_acl_rules(rules, rule_idx);
1219 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1220 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1221 rules[rule_idx].is_ipv6 = 1;
1223 else if (unformat (i, "mac %U",
1224 my_unformat_mac_address, &src_mac))
1226 vec_validate_macip_acl_rules(rules, rule_idx);
1227 memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1228 memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1230 else if (unformat (i, "mask %U",
1231 my_unformat_mac_address, &src_mac))
1233 vec_validate_macip_acl_rules(rules, rule_idx);
1234 memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1236 else if (unformat (i, "tag %s", &tag))
1239 else if (unformat (i, ","))
1242 vec_validate_macip_acl_rules(rules, rule_idx);
1248 /* Construct the API message */
1249 vam->result_ready = 0;
1252 n_rules = vec_len(rules);
1254 if (n_rules_override >= 0)
1255 n_rules = n_rules_override;
1257 msg_size += n_rules*sizeof(rules[0]);
1259 mp = vl_msg_api_alloc_as_if_client(msg_size);
1260 memset (mp, 0, msg_size);
1261 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
1262 mp->client_index = vam->my_client_index;
1263 if ((n_rules > 0) && rules)
1264 clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1267 if (vec_len(tag) >= sizeof(mp->tag))
1269 tag[sizeof(mp->tag)-1] = 0;
1270 _vec_len(tag) = sizeof(mp->tag);
1272 clib_memcpy(mp->tag, tag, vec_len(tag));
1276 mp->count = htonl(n_rules);
1281 /* Wait for a reply... */
1286 static int api_macip_acl_add_replace (vat_main_t * vam)
1288 acl_test_main_t * sm = &acl_test_main;
1289 unformat_input_t * i = vam->input;
1290 vl_api_macip_acl_add_replace_t * mp;
1292 u32 msg_size = sizeof (*mp); /* without the rules */
1294 vl_api_macip_acl_rule_t *rules = 0;
1297 int n_rules_override = -1;
1298 u32 src_prefix_length = 0;
1300 ip4_address_t src_v4address;
1301 ip6_address_t src_v6address;
1304 u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1307 if (!unformat (i, "%d", &acl_index)) {
1308 /* Just assume -1 */
1311 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1313 if (unformat (i, "ipv6"))
1315 vec_validate_macip_acl_rules(rules, rule_idx);
1316 rules[rule_idx].is_ipv6 = 1;
1318 else if (unformat (i, "ipv4"))
1320 vec_validate_macip_acl_rules(rules, rule_idx);
1321 rules[rule_idx].is_ipv6 = 0;
1323 else if (unformat (i, "permit"))
1325 vec_validate_macip_acl_rules(rules, rule_idx);
1326 rules[rule_idx].is_permit = 1;
1328 else if (unformat (i, "deny"))
1330 vec_validate_macip_acl_rules(rules, rule_idx);
1331 rules[rule_idx].is_permit = 0;
1333 else if (unformat (i, "count %d", &n_rules_override))
1335 /* we will use this later */
1337 else if (unformat (i, "action %d", &action))
1339 vec_validate_macip_acl_rules(rules, rule_idx);
1340 rules[rule_idx].is_permit = action;
1342 else if (unformat (i, "ip %U/%d",
1343 unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1344 unformat (i, "ip %U",
1345 unformat_ip4_address, &src_v4address))
1347 if (src_prefix_length == 0)
1348 src_prefix_length = 32;
1349 vec_validate_macip_acl_rules(rules, rule_idx);
1350 memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1351 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1352 rules[rule_idx].is_ipv6 = 0;
1354 else if (unformat (i, "src"))
1356 /* Everything in MACIP is "source" but allow this verbosity */
1358 else if (unformat (i, "ip %U/%d",
1359 unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1360 unformat (i, "ip %U",
1361 unformat_ip6_address, &src_v6address))
1363 if (src_prefix_length == 0)
1364 src_prefix_length = 128;
1365 vec_validate_macip_acl_rules(rules, rule_idx);
1366 memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1367 rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1368 rules[rule_idx].is_ipv6 = 1;
1370 else if (unformat (i, "mac %U",
1371 my_unformat_mac_address, &src_mac))
1373 vec_validate_macip_acl_rules(rules, rule_idx);
1374 memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1375 memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1377 else if (unformat (i, "mask %U",
1378 my_unformat_mac_address, &src_mac))
1380 vec_validate_macip_acl_rules(rules, rule_idx);
1381 memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1383 else if (unformat (i, "tag %s", &tag))
1386 else if (unformat (i, ","))
1389 vec_validate_macip_acl_rules(rules, rule_idx);
1397 errmsg ("rule/s required\n");
1400 /* Construct the API message */
1401 vam->result_ready = 0;
1404 n_rules = vec_len(rules);
1406 if (n_rules_override >= 0)
1407 n_rules = n_rules_override;
1409 msg_size += n_rules*sizeof(rules[0]);
1411 mp = vl_msg_api_alloc_as_if_client(msg_size);
1412 memset (mp, 0, msg_size);
1413 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD_REPLACE + sm->msg_id_base);
1414 mp->client_index = vam->my_client_index;
1415 if ((n_rules > 0) && rules)
1416 clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1419 if (vec_len(tag) >= sizeof(mp->tag))
1421 tag[sizeof(mp->tag)-1] = 0;
1422 _vec_len(tag) = sizeof(mp->tag);
1424 clib_memcpy(mp->tag, tag, vec_len(tag));
1428 mp->acl_index = ntohl(acl_index);
1429 mp->count = htonl(n_rules);
1434 /* Wait for a reply... */
1440 * List of messages that the api test plugin sends,
1441 * and that the data plane plugin processes
1443 #define foreach_vpe_api_msg \
1444 _(acl_plugin_get_version, "") \
1445 _(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], ... , ...") \
1446 _(acl_add_replace_from_file, "filename <file> [permit] [append-default-permit]") \
1447 _(acl_del, "<acl-idx>") \
1448 _(acl_dump, "[<acl-idx>]") \
1449 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
1450 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
1451 _(acl_interface_set_etype_whitelist, "<intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]") \
1452 _(acl_interface_etype_whitelist_dump, "[<intfc> | sw_if_index <if-idx>]") \
1453 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
1454 _(macip_acl_add, "...") \
1455 _(macip_acl_add_replace, "<acl-idx> [<ipv4|ipv6> <permit|deny|action N> [count <count>] [src] ip <ipaddress/[plen]> mac <mac> mask <mac_mask>, ... , ...") \
1456 _(macip_acl_del, "<acl-idx>")\
1457 _(macip_acl_dump, "[<acl-idx>]") \
1458 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
1459 _(macip_acl_interface_get, "")
1463 void acl_vat_api_hookup (vat_main_t *vam)
1465 acl_test_main_t * sm = &acl_test_main;
1466 /* Hook up handlers for replies from the data plane plug-in */
1468 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1470 vl_api_##n##_t_handler, \
1472 vl_api_##n##_t_endian, \
1473 vl_api_##n##_t_print, \
1474 sizeof(vl_api_##n##_t), 1);
1475 foreach_vpe_api_reply_msg;
1478 /* API messages we can send */
1479 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
1480 foreach_vpe_api_msg;
1484 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1485 foreach_vpe_api_msg;
1489 clib_error_t * vat_plugin_register (vat_main_t *vam)
1491 acl_test_main_t * sm = &acl_test_main;
1496 name = format (0, "acl_%08x%c", api_version, 0);
1497 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1499 if (sm->msg_id_base != (u16) ~0)
1500 acl_vat_api_hookup (vam);