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