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