Localize the timeout variable within the W message macro.
[vpp.git] / src / plugins / acl / acl_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  *------------------------------------------------------------------
17  * acl_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
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>
28
29 #define __plugin_msg_base acl_test_main.msg_id_base
30 #include <vlibapi/vat_helper_macros.h>
31
32 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
33
34 /* Declare message IDs */
35 #include <acl/acl_msg_enum.h>
36
37 /* define message structures */
38 #define vl_typedefs
39 #include <acl/acl_all_api_h.h>
40 #undef vl_typedefs
41
42 /* define message structures */
43 #define vl_endianfun
44 #include <acl/acl_all_api_h.h>
45 #undef vl_endianfun
46
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...)
49 #define vl_printfun
50 #include <acl/acl_all_api_h.h>
51 #undef vl_printfun
52
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>
56 #undef vl_api_version
57
58 typedef struct {
59     /* API message ID base */
60     u16 msg_id_base;
61     vat_main_t *vat_main;
62 } acl_test_main_t;
63
64 acl_test_main_t acl_test_main;
65
66 #define foreach_standard_reply_retval_handler   \
67 _(acl_del_reply) \
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)
72
73 #define foreach_reply_retval_aclindex_handler  \
74 _(acl_add_replace_reply) \
75 _(macip_acl_add_reply)
76
77 #define _(n)                                            \
78     static void vl_api_##n##_t_handler                  \
79     (vl_api_##n##_t * mp)                               \
80     {                                                   \
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);          \
85         } else {                                        \
86             vam->retval = retval;                       \
87             vam->result_ready = 1;                      \
88         }                                               \
89     }
90 foreach_standard_reply_retval_handler;
91 #undef _
92
93 #define _(n)                                            \
94     static void vl_api_##n##_t_handler                  \
95     (vl_api_##n##_t * mp)                               \
96     {                                                   \
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);          \
101         } else {                                        \
102             clib_warning("ACL index: %d", ntohl(mp->acl_index)); \
103             vam->retval = retval;                       \
104             vam->result_ready = 1;                      \
105         }                                               \
106     }
107 foreach_reply_retval_aclindex_handler;
108 #undef _
109
110 /* These two ought to be in a library somewhere but they aren't */
111 static uword
112 my_unformat_mac_address (unformat_input_t * input, va_list * args)
113 {
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],
116                    &a[4], &a[5]);
117 }
118
119 static u8 *
120 my_format_mac_address (u8 * s, va_list * args)
121 {
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]);
125 }
126
127
128
129 static void vl_api_acl_plugin_get_version_reply_t_handler
130     (vl_api_acl_plugin_get_version_reply_t * mp)
131     {
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;
135     }
136
137 static void vl_api_acl_interface_list_details_t_handler
138     (vl_api_acl_interface_list_details_t * mp)
139     {
140         int i;
141         vat_main_t * vam = acl_test_main.vat_main;
142         u8 *out = 0;
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 ");
150         }
151         out = format(out, "\n");
152         clib_warning("%s", out);
153         vec_free(out);
154         vam->result_ready = 1;
155     }
156
157
158 static inline u8 *
159 vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
160 {
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));
166
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,
171                      a->proto,
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);
175   return(out);
176 }
177
178
179
180 static void vl_api_acl_details_t_handler
181     (vl_api_acl_details_t * mp)
182     {
183         int i;
184         vat_main_t * vam = acl_test_main.vat_main;
185         vl_api_acl_details_t_endian(mp);
186         u8 *out = 0;
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 ? "," : "");
192         }
193         clib_warning("%s", out);
194         vec_free(out);
195         vam->result_ready = 1;
196     }
197
198 static inline u8 *
199 vl_api_macip_acl_rule_t_pretty_format (u8 *out, vl_api_macip_acl_rule_t * a)
200 {
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));
204
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);
210   return(out);
211 }
212
213
214 static void vl_api_macip_acl_details_t_handler
215     (vl_api_macip_acl_details_t * mp)
216     {
217         int i;
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 ? "," : "");
225         }
226         clib_warning("%s", out);
227         vec_free(out);
228         vam->result_ready = 1;
229     }
230
231 static void vl_api_macip_acl_interface_get_reply_t_handler
232     (vl_api_macip_acl_interface_get_reply_t * mp)
233     {
234         int i;
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]));
239         }
240         out = format(out, "\n");
241         clib_warning("%s", out);
242         vec_free(out);
243         vam->result_ready = 1;
244     }
245
246
247 /*
248  * Table of message reply handlers, must include boilerplate handlers
249  * we just generated
250  */
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)
264
265 static int api_acl_plugin_get_version (vat_main_t * vam)
266 {
267     acl_test_main_t * sm = &acl_test_main;
268     vl_api_acl_plugin_get_version_t * mp;
269     u32 msg_size = sizeof(*mp);
270
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;
276
277     /* send it... */
278     S(mp);
279
280     /* Wait for a reply... */
281     W;
282
283     return 0;
284 }
285
286 static int api_macip_acl_interface_get (vat_main_t * vam)
287 {
288     acl_test_main_t * sm = &acl_test_main;
289     vl_api_acl_plugin_get_version_t * mp;
290     u32 msg_size = sizeof(*mp);
291
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;
297
298     /* send it... */
299     S(mp);
300
301     /* Wait for a reply... */
302     W;
303
304     return 0;
305 }
306
307 #define vec_validate_acl_rules(v, idx) \
308   do {                                 \
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; \
314     } \
315   } while (0)
316
317
318 static int api_acl_add_replace (vat_main_t * vam)
319 {
320     acl_test_main_t * sm = &acl_test_main;
321     unformat_input_t * i = vam->input;
322     vl_api_acl_add_replace_t * mp;
323     u32 acl_index = ~0;
324     u32 msg_size = sizeof (*mp); /* without the rules */
325
326     vl_api_acl_rule_t *rules = 0;
327     int rule_idx = 0;
328     int n_rules = 0;
329     u32 proto = 0;
330     u32 port1 = 0;
331     u32 port2 = 0;
332     u32 action = 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;
337     u8 *tag = 0;
338
339     if (!unformat (i, "%d", &acl_index)) {
340         /* Just assume -1 */
341     }
342
343     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
344     {
345         if (unformat (i, "ipv6"))
346           {
347             vec_validate_acl_rules(rules, rule_idx);
348             rules[rule_idx].is_ipv6 = 1;
349           }
350         else if (unformat (i, "ipv4"))
351           {
352             vec_validate_acl_rules(rules, rule_idx);
353             rules[rule_idx].is_ipv6 = 0;
354           }
355         else if (unformat (i, "permit+reflect"))
356           {
357             vec_validate_acl_rules(rules, rule_idx);
358             rules[rule_idx].is_permit = 2;
359           }
360         else if (unformat (i, "permit"))
361           {
362             vec_validate_acl_rules(rules, rule_idx);
363             rules[rule_idx].is_permit = 1;
364           }
365         else if (unformat (i, "action %d", &action))
366           {
367             vec_validate_acl_rules(rules, rule_idx);
368             rules[rule_idx].is_permit = action;
369           }
370         else if (unformat (i, "src %U/%d",
371          unformat_ip4_address, &src_v4address, &src_prefix_length))
372           {
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;
377           }
378         else if (unformat (i, "src %U/%d",
379          unformat_ip6_address, &src_v6address, &src_prefix_length))
380           {
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;
385           }
386         else if (unformat (i, "dst %U/%d",
387          unformat_ip4_address, &dst_v4address, &dst_prefix_length))
388           {
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;
393           }
394         else if (unformat (i, "dst %U/%d",
395          unformat_ip6_address, &dst_v6address, &dst_prefix_length))
396           {
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;
401           }
402         else if (unformat (i, "sport %d-%d", &port1, &port2))
403           {
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);
407           }
408         else if (unformat (i, "sport %d", &port1))
409           {
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);
413           }
414         else if (unformat (i, "dport %d-%d", &port1, &port2))
415           {
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);
419           }
420         else if (unformat (i, "dport %d", &port1))
421           {
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);
425           }
426         else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
427           {
428             vec_validate_acl_rules(rules, rule_idx);
429             rules[rule_idx].tcp_flags_value = tcpflags;
430             rules[rule_idx].tcp_flags_mask = tcpmask;
431           }
432         else if (unformat (i, "proto %d", &proto))
433           {
434             vec_validate_acl_rules(rules, rule_idx);
435             rules[rule_idx].proto = proto;
436           }
437         else if (unformat (i, "tag %s", &tag))
438           {
439           }
440         else if (unformat (i, ","))
441           {
442             rule_idx++;
443             vec_validate_acl_rules(rules, rule_idx);
444           }
445         else
446     break;
447     }
448
449     /* Construct the API message */
450     vam->result_ready = 0;
451
452     if(rules)
453       n_rules = vec_len(rules);
454     else
455       n_rules = 0;
456
457     msg_size += n_rules*sizeof(rules[0]);
458
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;
463     if (n_rules > 0)
464       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
465     if (tag)
466       {
467         if (vec_len(tag) >= sizeof(mp->tag))
468           {
469             tag[sizeof(mp->tag)-1] = 0;
470             _vec_len(tag) = sizeof(mp->tag);
471           }
472         clib_memcpy(mp->tag, tag, vec_len(tag));
473         vec_free(tag);
474       }
475     mp->acl_index = ntohl(acl_index);
476     mp->count = htonl(n_rules);
477
478     /* send it... */
479     S(mp);
480
481     /* Wait for a reply... */
482     W;
483 }
484
485 static int api_acl_del (vat_main_t * vam)
486 {
487     unformat_input_t * i = vam->input;
488     vl_api_acl_del_t * mp;
489     u32 acl_index = ~0;
490
491     if (!unformat (i, "%d", &acl_index)) {
492       errmsg ("missing acl index\n");
493       return -99;
494     }
495
496     /* Construct the API message */
497     M(ACL_DEL, mp);
498     mp->acl_index = ntohl(acl_index);
499
500     /* send it... */
501     S(mp);
502
503     /* Wait for a reply... */
504     W;
505 }
506
507 static int api_macip_acl_del (vat_main_t * vam)
508 {
509     unformat_input_t * i = vam->input;
510     vl_api_acl_del_t * mp;
511     u32 acl_index = ~0;
512
513     if (!unformat (i, "%d", &acl_index)) {
514       errmsg ("missing acl index\n");
515       return -99;
516     }
517
518     /* Construct the API message */
519     M(MACIP_ACL_DEL, mp);
520     mp->acl_index = ntohl(acl_index);
521
522     /* send it... */
523     S(mp);
524
525     /* Wait for a reply... */
526     W;
527 }
528
529 static int api_acl_interface_add_del (vat_main_t * vam)
530 {
531     unformat_input_t * i = vam->input;
532     vl_api_acl_interface_add_del_t * mp;
533     u32 sw_if_index = ~0;
534     u32 acl_index = ~0;
535     u8 is_input = 0;
536     u8 is_add = 0;
537
538 //    acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
539
540     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
541     {
542         if (unformat (i, "%d", &acl_index))
543     ;
544         else
545     break;
546     }
547
548
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))
552             ;
553         else if (unformat (i, "sw_if_index %d", &sw_if_index))
554             ;
555         else if (unformat (i, "add"))
556             is_add = 1;
557         else if (unformat (i, "del"))
558             is_add = 0;
559         else if (unformat (i, "acl %d", &acl_index))
560             ;
561         else if (unformat (i, "input"))
562             is_input = 1;
563         else if (unformat (i, "output"))
564             is_input = 0;
565         else
566             break;
567     }
568
569     if (sw_if_index == ~0) {
570         errmsg ("missing interface name / explicit sw_if_index number \n");
571         return -99;
572     }
573
574     if (acl_index == ~0) {
575         errmsg ("missing ACL index\n");
576         return -99;
577     }
578
579
580
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);
585     mp->is_add = is_add;
586     mp->is_input = is_input;
587
588     /* send it... */
589     S(mp);
590
591     /* Wait for a reply... */
592     W;
593 }
594
595 static int api_macip_acl_interface_add_del (vat_main_t * vam)
596 {
597     unformat_input_t * i = vam->input;
598     vl_api_macip_acl_interface_add_del_t * mp;
599     u32 sw_if_index = ~0;
600     u32 acl_index = ~0;
601     u8 is_add = 0;
602
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))
606             ;
607         else if (unformat (i, "sw_if_index %d", &sw_if_index))
608             ;
609         else if (unformat (i, "add"))
610             is_add = 1;
611         else if (unformat (i, "del"))
612             is_add = 0;
613         else if (unformat (i, "acl %d", &acl_index))
614             ;
615         else
616             break;
617     }
618
619     if (sw_if_index == ~0) {
620         errmsg ("missing interface name / explicit sw_if_index number \n");
621         return -99;
622     }
623
624     if (acl_index == ~0) {
625         errmsg ("missing ACL index\n");
626         return -99;
627     }
628
629
630
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);
635     mp->is_add = is_add;
636
637     /* send it... */
638     S(mp);
639
640     /* Wait for a reply... */
641     W;
642 }
643
644 static int api_acl_interface_set_acl_list (vat_main_t * vam)
645 {
646     unformat_input_t * i = vam->input;
647     vl_api_acl_interface_set_acl_list_t * mp;
648     u32 sw_if_index = ~0;
649     u32 acl_index = ~0;
650     u32 *inacls = 0;
651     u32 *outacls = 0;
652     u8 is_input = 0;
653
654 //  acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
655
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))
659             ;
660         else if (unformat (i, "sw_if_index %d", &sw_if_index))
661             ;
662         else if (unformat (i, "%d", &acl_index))
663           {
664             if(is_input)
665               vec_add1(inacls, htonl(acl_index));
666             else
667               vec_add1(outacls, htonl(acl_index));
668           }
669         else if (unformat (i, "acl %d", &acl_index))
670             ;
671         else if (unformat (i, "input"))
672             is_input = 1;
673         else if (unformat (i, "output"))
674             is_input = 0;
675         else
676             break;
677     }
678
679     if (sw_if_index == ~0) {
680         errmsg ("missing interface name / explicit sw_if_index number \n");
681         return -99;
682     }
683
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));
692
693     /* send it... */
694     S(mp);
695
696     /* Wait for a reply... */
697     W;
698 }
699
700
701 static int api_acl_interface_list_dump (vat_main_t * vam)
702 {
703     unformat_input_t * i = vam->input;
704     u32 sw_if_index = ~0;
705     vl_api_acl_interface_list_dump_t * mp;
706
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))
710             ;
711         else if (unformat (i, "sw_if_index %d", &sw_if_index))
712             ;
713         else
714             break;
715     }
716
717     /* Construct the API message */
718     M(ACL_INTERFACE_LIST_DUMP, mp);
719     mp->sw_if_index = ntohl (sw_if_index);
720
721     /* send it... */
722     S(mp);
723
724     /* Wait for a reply... */
725     W;
726 }
727
728 static int api_acl_dump (vat_main_t * vam)
729 {
730     unformat_input_t * i = vam->input;
731     u32 acl_index = ~0;
732     vl_api_acl_dump_t * mp;
733
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))
737             ;
738         else
739             break;
740     }
741
742     /* Construct the API message */
743     M(ACL_DUMP, mp);
744     mp->acl_index = ntohl (acl_index);
745
746     /* send it... */
747     S(mp);
748
749     /* Wait for a reply... */
750     W;
751 }
752
753 static int api_macip_acl_dump (vat_main_t * vam)
754 {
755     unformat_input_t * i = vam->input;
756     u32 acl_index = ~0;
757     vl_api_acl_dump_t * mp;
758
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))
762             ;
763         else
764             break;
765     }
766
767     /* Construct the API message */
768     M(MACIP_ACL_DUMP, mp);
769     mp->acl_index = ntohl (acl_index);
770
771     /* send it... */
772     S(mp);
773
774     /* Wait for a reply... */
775     W;
776 }
777
778 #define vec_validate_macip_acl_rules(v, idx) \
779   do {                                 \
780     if (vec_len(v) < idx+1) {  \
781       vec_validate(v, idx); \
782       v[idx].is_permit = 0x1; \
783     } \
784   } while (0)
785
786
787 static int api_macip_acl_add (vat_main_t * vam)
788 {
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 */
793
794     vl_api_macip_acl_rule_t *rules = 0;
795     int rule_idx = 0;
796     int n_rules = 0;
797     u32 src_prefix_length = 0;
798     u32 action = 0;
799     ip4_address_t src_v4address;
800     ip6_address_t src_v6address;
801     u8 src_mac[6];
802     u8 *tag = 0;
803     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
804
805     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
806     {
807         if (unformat (i, "ipv6"))
808           {
809             vec_validate_macip_acl_rules(rules, rule_idx);
810             rules[rule_idx].is_ipv6 = 1;
811           }
812         else if (unformat (i, "ipv4"))
813           {
814             vec_validate_macip_acl_rules(rules, rule_idx);
815             rules[rule_idx].is_ipv6 = 1;
816           }
817         else if (unformat (i, "permit"))
818           {
819             vec_validate_macip_acl_rules(rules, rule_idx);
820             rules[rule_idx].is_permit = 1;
821           }
822         else if (unformat (i, "deny"))
823           {
824             vec_validate_macip_acl_rules(rules, rule_idx);
825             rules[rule_idx].is_permit = 0;
826           }
827         else if (unformat (i, "action %d", &action))
828           {
829             vec_validate_macip_acl_rules(rules, rule_idx);
830             rules[rule_idx].is_permit = action;
831           }
832         else if (unformat (i, "ip %U/%d",
833          unformat_ip4_address, &src_v4address, &src_prefix_length))
834           {
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;
839           }
840         else if (unformat (i, "ip %U/%d",
841          unformat_ip6_address, &src_v6address, &src_prefix_length))
842           {
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;
847           }
848         else if (unformat (i, "mac %U",
849          my_unformat_mac_address, &src_mac))
850           {
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);
854           }
855         else if (unformat (i, "mask %U",
856          my_unformat_mac_address, &src_mac))
857           {
858             vec_validate_macip_acl_rules(rules, rule_idx);
859             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
860           }
861         else if (unformat (i, "tag %s", &tag))
862           {
863           }
864         else if (unformat (i, ","))
865           {
866             rule_idx++;
867             vec_validate_macip_acl_rules(rules, rule_idx);
868           }
869         else
870     break;
871     }
872
873     /* Construct the API message */
874     vam->result_ready = 0;
875
876     if(rules)
877       n_rules = vec_len(rules);
878     else
879       n_rules = 0;
880
881     msg_size += n_rules*sizeof(rules[0]);
882
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;
887     if (n_rules > 0)
888       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
889     if (tag)
890       {
891         if (vec_len(tag) >= sizeof(mp->tag))
892           {
893             tag[sizeof(mp->tag)-1] = 0;
894             _vec_len(tag) = sizeof(mp->tag);
895           }
896         clib_memcpy(mp->tag, tag, vec_len(tag));
897         vec_free(tag);
898       }
899
900     mp->count = htonl(n_rules);
901
902     /* send it... */
903     S(mp);
904
905     /* Wait for a reply... */
906     W;
907 }
908
909 /*
910  * List of messages that the api test plugin sends,
911  * and that the data plane plugin processes
912  */
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, "")
926
927
928 static
929 void acl_vat_api_hookup (vat_main_t *vam)
930 {
931     acl_test_main_t * sm = &acl_test_main;
932     /* Hook up handlers for replies from the data plane plug-in */
933 #define _(N,n)                                                  \
934     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
935                            #n,                                  \
936                            vl_api_##n##_t_handler,              \
937                            vl_noop_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;
942 #undef _
943
944     /* API messages we can send */
945 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
946     foreach_vpe_api_msg;
947 #undef _
948
949     /* Help strings */
950 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
951     foreach_vpe_api_msg;
952 #undef _
953 }
954
955 clib_error_t * vat_plugin_register (vat_main_t *vam)
956 {
957   acl_test_main_t * sm = &acl_test_main;
958   u8 * name;
959
960   sm->vat_main = vam;
961
962   name = format (0, "acl_%08x%c", api_version, 0);
963   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
964
965   if (sm->msg_id_base != (u16) ~0)
966     acl_vat_api_hookup (vam);
967
968   vec_free(name);
969
970   return 0;
971 }