Convert message macro S to accept a message pointer parameter;
[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     f64 timeout;
271
272     vam->result_ready = 0;
273     mp = vl_msg_api_alloc_as_if_client(msg_size);
274     memset (mp, 0, msg_size);
275     mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
276     mp->client_index = vam->my_client_index;
277
278     /* send it... */
279     S(mp);
280
281     /* Wait for a reply... */
282     W;
283
284     return 0;
285 }
286
287 static int api_macip_acl_interface_get (vat_main_t * vam)
288 {
289     acl_test_main_t * sm = &acl_test_main;
290     vl_api_acl_plugin_get_version_t * mp;
291     u32 msg_size = sizeof(*mp);
292     f64 timeout;
293
294     vam->result_ready = 0;
295     mp = vl_msg_api_alloc_as_if_client(msg_size);
296     memset (mp, 0, msg_size);
297     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
298     mp->client_index = vam->my_client_index;
299
300     /* send it... */
301     S(mp);
302
303     /* Wait for a reply... */
304     W;
305
306     return 0;
307 }
308
309 #define vec_validate_acl_rules(v, idx) \
310   do {                                 \
311     if (vec_len(v) < idx+1) {  \
312       vec_validate(v, idx); \
313       v[idx].is_permit = 0x1; \
314       v[idx].srcport_or_icmptype_last = 0xffff; \
315       v[idx].dstport_or_icmpcode_last = 0xffff; \
316     } \
317   } while (0)
318
319
320 static int api_acl_add_replace (vat_main_t * vam)
321 {
322     acl_test_main_t * sm = &acl_test_main;
323     unformat_input_t * i = vam->input;
324     f64 timeout;
325     vl_api_acl_add_replace_t * mp;
326     u32 acl_index = ~0;
327     u32 msg_size = sizeof (*mp); /* without the rules */
328
329     vl_api_acl_rule_t *rules = 0;
330     int rule_idx = 0;
331     int n_rules = 0;
332     u32 proto = 0;
333     u32 port1 = 0;
334     u32 port2 = 0;
335     u32 action = 0;
336     u32 tcpflags, tcpmask;
337     u32 src_prefix_length = 0, dst_prefix_length = 0;
338     ip4_address_t src_v4address, dst_v4address;
339     ip6_address_t src_v6address, dst_v6address;
340     u8 *tag = 0;
341
342     if (!unformat (i, "%d", &acl_index)) {
343         /* Just assume -1 */
344     }
345
346     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
347     {
348         if (unformat (i, "ipv6"))
349           {
350             vec_validate_acl_rules(rules, rule_idx);
351             rules[rule_idx].is_ipv6 = 1;
352           }
353         else if (unformat (i, "ipv4"))
354           {
355             vec_validate_acl_rules(rules, rule_idx);
356             rules[rule_idx].is_ipv6 = 0;
357           }
358         else if (unformat (i, "permit+reflect"))
359           {
360             vec_validate_acl_rules(rules, rule_idx);
361             rules[rule_idx].is_permit = 2;
362           }
363         else if (unformat (i, "permit"))
364           {
365             vec_validate_acl_rules(rules, rule_idx);
366             rules[rule_idx].is_permit = 1;
367           }
368         else if (unformat (i, "action %d", &action))
369           {
370             vec_validate_acl_rules(rules, rule_idx);
371             rules[rule_idx].is_permit = action;
372           }
373         else if (unformat (i, "src %U/%d",
374          unformat_ip4_address, &src_v4address, &src_prefix_length))
375           {
376             vec_validate_acl_rules(rules, rule_idx);
377             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
378             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
379             rules[rule_idx].is_ipv6 = 0;
380           }
381         else if (unformat (i, "src %U/%d",
382          unformat_ip6_address, &src_v6address, &src_prefix_length))
383           {
384             vec_validate_acl_rules(rules, rule_idx);
385             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
386             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
387             rules[rule_idx].is_ipv6 = 1;
388           }
389         else if (unformat (i, "dst %U/%d",
390          unformat_ip4_address, &dst_v4address, &dst_prefix_length))
391           {
392             vec_validate_acl_rules(rules, rule_idx);
393             memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
394             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
395             rules[rule_idx].is_ipv6 = 0;
396           }
397         else if (unformat (i, "dst %U/%d",
398          unformat_ip6_address, &dst_v6address, &dst_prefix_length))
399           {
400             vec_validate_acl_rules(rules, rule_idx);
401             memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
402             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
403             rules[rule_idx].is_ipv6 = 1;
404           }
405         else if (unformat (i, "sport %d-%d", &port1, &port2))
406           {
407             vec_validate_acl_rules(rules, rule_idx);
408             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
409             rules[rule_idx].srcport_or_icmptype_last = htons(port2);
410           }
411         else if (unformat (i, "sport %d", &port1))
412           {
413             vec_validate_acl_rules(rules, rule_idx);
414             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
415             rules[rule_idx].srcport_or_icmptype_last = htons(port1);
416           }
417         else if (unformat (i, "dport %d-%d", &port1, &port2))
418           {
419             vec_validate_acl_rules(rules, rule_idx);
420             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
421             rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
422           }
423         else if (unformat (i, "dport %d", &port1))
424           {
425             vec_validate_acl_rules(rules, rule_idx);
426             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
427             rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
428           }
429         else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
430           {
431             vec_validate_acl_rules(rules, rule_idx);
432             rules[rule_idx].tcp_flags_value = tcpflags;
433             rules[rule_idx].tcp_flags_mask = tcpmask;
434           }
435         else if (unformat (i, "proto %d", &proto))
436           {
437             vec_validate_acl_rules(rules, rule_idx);
438             rules[rule_idx].proto = proto;
439           }
440         else if (unformat (i, "tag %s", &tag))
441           {
442           }
443         else if (unformat (i, ","))
444           {
445             rule_idx++;
446             vec_validate_acl_rules(rules, rule_idx);
447           }
448         else
449     break;
450     }
451
452     /* Construct the API message */
453     vam->result_ready = 0;
454
455     if(rules)
456       n_rules = vec_len(rules);
457     else
458       n_rules = 0;
459
460     msg_size += n_rules*sizeof(rules[0]);
461
462     mp = vl_msg_api_alloc_as_if_client(msg_size);
463     memset (mp, 0, msg_size);
464     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
465     mp->client_index = vam->my_client_index;
466     if (n_rules > 0)
467       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
468     if (tag)
469       {
470         if (vec_len(tag) >= sizeof(mp->tag))
471           {
472             tag[sizeof(mp->tag)-1] = 0;
473             _vec_len(tag) = sizeof(mp->tag);
474           }
475         clib_memcpy(mp->tag, tag, vec_len(tag));
476         vec_free(tag);
477       }
478     mp->acl_index = ntohl(acl_index);
479     mp->count = htonl(n_rules);
480
481     /* send it... */
482     S(mp);
483
484     /* Wait for a reply... */
485     W;
486 }
487
488 static int api_acl_del (vat_main_t * vam)
489 {
490     unformat_input_t * i = vam->input;
491     f64 timeout;
492     vl_api_acl_del_t * mp;
493     u32 acl_index = ~0;
494
495     if (!unformat (i, "%d", &acl_index)) {
496       errmsg ("missing acl index\n");
497       return -99;
498     }
499
500     /* Construct the API message */
501     M(ACL_DEL, mp);
502     mp->acl_index = ntohl(acl_index);
503
504     /* send it... */
505     S(mp);
506
507     /* Wait for a reply... */
508     W;
509 }
510
511 static int api_macip_acl_del (vat_main_t * vam)
512 {
513     unformat_input_t * i = vam->input;
514     f64 timeout;
515     vl_api_acl_del_t * mp;
516     u32 acl_index = ~0;
517
518     if (!unformat (i, "%d", &acl_index)) {
519       errmsg ("missing acl index\n");
520       return -99;
521     }
522
523     /* Construct the API message */
524     M(MACIP_ACL_DEL, mp);
525     mp->acl_index = ntohl(acl_index);
526
527     /* send it... */
528     S(mp);
529
530     /* Wait for a reply... */
531     W;
532 }
533
534 static int api_acl_interface_add_del (vat_main_t * vam)
535 {
536     unformat_input_t * i = vam->input;
537     f64 timeout;
538     vl_api_acl_interface_add_del_t * mp;
539     u32 sw_if_index = ~0;
540     u32 acl_index = ~0;
541     u8 is_input = 0;
542     u8 is_add = 0;
543
544 //    acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
545
546     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
547     {
548         if (unformat (i, "%d", &acl_index))
549     ;
550         else
551     break;
552     }
553
554
555     /* Parse args required to build the message */
556     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
557         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
558             ;
559         else if (unformat (i, "sw_if_index %d", &sw_if_index))
560             ;
561         else if (unformat (i, "add"))
562             is_add = 1;
563         else if (unformat (i, "del"))
564             is_add = 0;
565         else if (unformat (i, "acl %d", &acl_index))
566             ;
567         else if (unformat (i, "input"))
568             is_input = 1;
569         else if (unformat (i, "output"))
570             is_input = 0;
571         else
572             break;
573     }
574
575     if (sw_if_index == ~0) {
576         errmsg ("missing interface name / explicit sw_if_index number \n");
577         return -99;
578     }
579
580     if (acl_index == ~0) {
581         errmsg ("missing ACL index\n");
582         return -99;
583     }
584
585
586
587     /* Construct the API message */
588     M(ACL_INTERFACE_ADD_DEL, mp);
589     mp->acl_index = ntohl(acl_index);
590     mp->sw_if_index = ntohl(sw_if_index);
591     mp->is_add = is_add;
592     mp->is_input = is_input;
593
594     /* send it... */
595     S(mp);
596
597     /* Wait for a reply... */
598     W;
599 }
600
601 static int api_macip_acl_interface_add_del (vat_main_t * vam)
602 {
603     unformat_input_t * i = vam->input;
604     f64 timeout;
605     vl_api_macip_acl_interface_add_del_t * mp;
606     u32 sw_if_index = ~0;
607     u32 acl_index = ~0;
608     u8 is_add = 0;
609
610     /* Parse args required to build the message */
611     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
612         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
613             ;
614         else if (unformat (i, "sw_if_index %d", &sw_if_index))
615             ;
616         else if (unformat (i, "add"))
617             is_add = 1;
618         else if (unformat (i, "del"))
619             is_add = 0;
620         else if (unformat (i, "acl %d", &acl_index))
621             ;
622         else
623             break;
624     }
625
626     if (sw_if_index == ~0) {
627         errmsg ("missing interface name / explicit sw_if_index number \n");
628         return -99;
629     }
630
631     if (acl_index == ~0) {
632         errmsg ("missing ACL index\n");
633         return -99;
634     }
635
636
637
638     /* Construct the API message */
639     M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
640     mp->acl_index = ntohl(acl_index);
641     mp->sw_if_index = ntohl(sw_if_index);
642     mp->is_add = is_add;
643
644     /* send it... */
645     S(mp);
646
647     /* Wait for a reply... */
648     W;
649 }
650
651 static int api_acl_interface_set_acl_list (vat_main_t * vam)
652 {
653     unformat_input_t * i = vam->input;
654     f64 timeout;
655     vl_api_acl_interface_set_acl_list_t * mp;
656     u32 sw_if_index = ~0;
657     u32 acl_index = ~0;
658     u32 *inacls = 0;
659     u32 *outacls = 0;
660     u8 is_input = 0;
661
662 //  acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
663
664     /* Parse args required to build the message */
665     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
666         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
667             ;
668         else if (unformat (i, "sw_if_index %d", &sw_if_index))
669             ;
670         else if (unformat (i, "%d", &acl_index))
671           {
672             if(is_input)
673               vec_add1(inacls, htonl(acl_index));
674             else
675               vec_add1(outacls, htonl(acl_index));
676           }
677         else if (unformat (i, "acl %d", &acl_index))
678             ;
679         else if (unformat (i, "input"))
680             is_input = 1;
681         else if (unformat (i, "output"))
682             is_input = 0;
683         else
684             break;
685     }
686
687     if (sw_if_index == ~0) {
688         errmsg ("missing interface name / explicit sw_if_index number \n");
689         return -99;
690     }
691
692     /* Construct the API message */
693     M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
694     mp->sw_if_index = ntohl(sw_if_index);
695     mp->n_input = vec_len(inacls);
696     mp->count = vec_len(inacls) + vec_len(outacls);
697     vec_append(inacls, outacls);
698     if (vec_len(inacls) > 0)
699       clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
700
701     /* send it... */
702     S(mp);
703
704     /* Wait for a reply... */
705     W;
706 }
707
708
709 static int api_acl_interface_list_dump (vat_main_t * vam)
710 {
711     unformat_input_t * i = vam->input;
712     f64 timeout;
713     u32 sw_if_index = ~0;
714     vl_api_acl_interface_list_dump_t * mp;
715
716     /* Parse args required to build the message */
717     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
718         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
719             ;
720         else if (unformat (i, "sw_if_index %d", &sw_if_index))
721             ;
722         else
723             break;
724     }
725
726     /* Construct the API message */
727     M(ACL_INTERFACE_LIST_DUMP, mp);
728     mp->sw_if_index = ntohl (sw_if_index);
729
730     /* send it... */
731     S(mp);
732
733     /* Wait for a reply... */
734     W;
735 }
736
737 static int api_acl_dump (vat_main_t * vam)
738 {
739     unformat_input_t * i = vam->input;
740     f64 timeout;
741     u32 acl_index = ~0;
742     vl_api_acl_dump_t * mp;
743
744     /* Parse args required to build the message */
745     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
746         if (unformat (i, "%d", &acl_index))
747             ;
748         else
749             break;
750     }
751
752     /* Construct the API message */
753     M(ACL_DUMP, mp);
754     mp->acl_index = ntohl (acl_index);
755
756     /* send it... */
757     S(mp);
758
759     /* Wait for a reply... */
760     W;
761 }
762
763 static int api_macip_acl_dump (vat_main_t * vam)
764 {
765     unformat_input_t * i = vam->input;
766     f64 timeout;
767     u32 acl_index = ~0;
768     vl_api_acl_dump_t * mp;
769
770     /* Parse args required to build the message */
771     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
772         if (unformat (i, "%d", &acl_index))
773             ;
774         else
775             break;
776     }
777
778     /* Construct the API message */
779     M(MACIP_ACL_DUMP, mp);
780     mp->acl_index = ntohl (acl_index);
781
782     /* send it... */
783     S(mp);
784
785     /* Wait for a reply... */
786     W;
787 }
788
789 #define vec_validate_macip_acl_rules(v, idx) \
790   do {                                 \
791     if (vec_len(v) < idx+1) {  \
792       vec_validate(v, idx); \
793       v[idx].is_permit = 0x1; \
794     } \
795   } while (0)
796
797
798 static int api_macip_acl_add (vat_main_t * vam)
799 {
800     acl_test_main_t * sm = &acl_test_main;
801     unformat_input_t * i = vam->input;
802     f64 timeout;
803     vl_api_macip_acl_add_t * mp;
804     u32 msg_size = sizeof (*mp); /* without the rules */
805
806     vl_api_macip_acl_rule_t *rules = 0;
807     int rule_idx = 0;
808     int n_rules = 0;
809     u32 src_prefix_length = 0;
810     u32 action = 0;
811     ip4_address_t src_v4address;
812     ip6_address_t src_v6address;
813     u8 src_mac[6];
814     u8 *tag = 0;
815     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
816
817     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
818     {
819         if (unformat (i, "ipv6"))
820           {
821             vec_validate_macip_acl_rules(rules, rule_idx);
822             rules[rule_idx].is_ipv6 = 1;
823           }
824         else if (unformat (i, "ipv4"))
825           {
826             vec_validate_macip_acl_rules(rules, rule_idx);
827             rules[rule_idx].is_ipv6 = 1;
828           }
829         else if (unformat (i, "permit"))
830           {
831             vec_validate_macip_acl_rules(rules, rule_idx);
832             rules[rule_idx].is_permit = 1;
833           }
834         else if (unformat (i, "deny"))
835           {
836             vec_validate_macip_acl_rules(rules, rule_idx);
837             rules[rule_idx].is_permit = 0;
838           }
839         else if (unformat (i, "action %d", &action))
840           {
841             vec_validate_macip_acl_rules(rules, rule_idx);
842             rules[rule_idx].is_permit = action;
843           }
844         else if (unformat (i, "ip %U/%d",
845          unformat_ip4_address, &src_v4address, &src_prefix_length))
846           {
847             vec_validate_macip_acl_rules(rules, rule_idx);
848             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
849             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
850             rules[rule_idx].is_ipv6 = 0;
851           }
852         else if (unformat (i, "ip %U/%d",
853          unformat_ip6_address, &src_v6address, &src_prefix_length))
854           {
855             vec_validate_macip_acl_rules(rules, rule_idx);
856             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
857             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
858             rules[rule_idx].is_ipv6 = 1;
859           }
860         else if (unformat (i, "mac %U",
861          my_unformat_mac_address, &src_mac))
862           {
863             vec_validate_macip_acl_rules(rules, rule_idx);
864             memcpy (rules[rule_idx].src_mac, &src_mac, 6);
865             memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
866           }
867         else if (unformat (i, "mask %U",
868          my_unformat_mac_address, &src_mac))
869           {
870             vec_validate_macip_acl_rules(rules, rule_idx);
871             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
872           }
873         else if (unformat (i, "tag %s", &tag))
874           {
875           }
876         else if (unformat (i, ","))
877           {
878             rule_idx++;
879             vec_validate_macip_acl_rules(rules, rule_idx);
880           }
881         else
882     break;
883     }
884
885     /* Construct the API message */
886     vam->result_ready = 0;
887
888     if(rules)
889       n_rules = vec_len(rules);
890     else
891       n_rules = 0;
892
893     msg_size += n_rules*sizeof(rules[0]);
894
895     mp = vl_msg_api_alloc_as_if_client(msg_size);
896     memset (mp, 0, msg_size);
897     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
898     mp->client_index = vam->my_client_index;
899     if (n_rules > 0)
900       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
901     if (tag)
902       {
903         if (vec_len(tag) >= sizeof(mp->tag))
904           {
905             tag[sizeof(mp->tag)-1] = 0;
906             _vec_len(tag) = sizeof(mp->tag);
907           }
908         clib_memcpy(mp->tag, tag, vec_len(tag));
909         vec_free(tag);
910       }
911
912     mp->count = htonl(n_rules);
913
914     /* send it... */
915     S(mp);
916
917     /* Wait for a reply... */
918     W;
919 }
920
921 /*
922  * List of messages that the api test plugin sends,
923  * and that the data plane plugin processes
924  */
925 #define foreach_vpe_api_msg \
926 _(acl_plugin_get_version, "") \
927 _(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], ... , ...") \
928 _(acl_del, "<acl-idx>") \
929 _(acl_dump, "[<acl-idx>]") \
930 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
931 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
932 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
933 _(macip_acl_add, "...") \
934 _(macip_acl_del, "<acl-idx>")\
935 _(macip_acl_dump, "[<acl-idx>]") \
936 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
937 _(macip_acl_interface_get, "")
938
939
940 static
941 void acl_vat_api_hookup (vat_main_t *vam)
942 {
943     acl_test_main_t * sm = &acl_test_main;
944     /* Hook up handlers for replies from the data plane plug-in */
945 #define _(N,n)                                                  \
946     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
947                            #n,                                  \
948                            vl_api_##n##_t_handler,              \
949                            vl_noop_handler,                     \
950                            vl_api_##n##_t_endian,               \
951                            vl_api_##n##_t_print,                \
952                            sizeof(vl_api_##n##_t), 1);
953     foreach_vpe_api_reply_msg;
954 #undef _
955
956     /* API messages we can send */
957 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
958     foreach_vpe_api_msg;
959 #undef _
960
961     /* Help strings */
962 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
963     foreach_vpe_api_msg;
964 #undef _
965 }
966
967 clib_error_t * vat_plugin_register (vat_main_t *vam)
968 {
969   acl_test_main_t * sm = &acl_test_main;
970   u8 * name;
971
972   sm->vat_main = vam;
973
974   name = format (0, "acl_%08x%c", api_version, 0);
975   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
976
977   if (sm->msg_id_base != (u16) ~0)
978     acl_vat_api_hookup (vam);
979
980   vec_free(name);
981
982   return 0;
983 }