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