acl-plugin: VAT: add an option to load entire ACL from a ClassBench ruleset file...
[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 <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26 #include <arpa/inet.h>
27
28 #define __plugin_msg_base acl_test_main.msg_id_base
29 #include <vlibapi/vat_helper_macros.h>
30
31 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
32
33 /* Declare message IDs */
34 #include <acl/acl_msg_enum.h>
35
36 /* define message structures */
37 #define vl_typedefs
38 #include <acl/acl_all_api_h.h>
39 #undef vl_typedefs
40
41 /* define message structures */
42 #define vl_endianfun
43 #include <acl/acl_all_api_h.h>
44 #undef vl_endianfun
45
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
48 #define vl_printfun
49 #include <acl/acl_all_api_h.h>
50 #undef vl_printfun
51
52 /* Get the API version number. */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <acl/acl_all_api_h.h>
55 #undef vl_api_version
56
57 typedef struct {
58     /* API message ID base */
59     u16 msg_id_base;
60     vat_main_t *vat_main;
61 } acl_test_main_t;
62
63 acl_test_main_t acl_test_main;
64
65 #define foreach_standard_reply_retval_handler   \
66 _(acl_del_reply) \
67 _(acl_interface_add_del_reply) \
68 _(macip_acl_interface_add_del_reply) \
69 _(acl_interface_set_acl_list_reply) \
70 _(acl_interface_set_etype_whitelist_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 _(macip_acl_add_replace_reply)
77
78 #define _(n)                                            \
79     static void vl_api_##n##_t_handler                  \
80     (vl_api_##n##_t * mp)                               \
81     {                                                   \
82         vat_main_t * vam = acl_test_main.vat_main;   \
83         i32 retval = ntohl(mp->retval);                 \
84         if (vam->async_mode) {                          \
85             vam->async_errors += (retval < 0);          \
86         } else {                                        \
87             vam->retval = retval;                       \
88             vam->result_ready = 1;                      \
89         }                                               \
90     }
91 foreach_standard_reply_retval_handler;
92 #undef _
93
94 #define _(n)                                            \
95     static void vl_api_##n##_t_handler                  \
96     (vl_api_##n##_t * mp)                               \
97     {                                                   \
98         vat_main_t * vam = acl_test_main.vat_main;   \
99         i32 retval = ntohl(mp->retval);                 \
100         if (vam->async_mode) {                          \
101             vam->async_errors += (retval < 0);          \
102         } else {                                        \
103             clib_warning("ACL index: %d", ntohl(mp->acl_index)); \
104             vam->retval = retval;                       \
105             vam->result_ready = 1;                      \
106         }                                               \
107     }
108 foreach_reply_retval_aclindex_handler;
109 #undef _
110
111 /* These two ought to be in a library somewhere but they aren't */
112 static uword
113 my_unformat_mac_address (unformat_input_t * input, va_list * args)
114 {
115   u8 *a = va_arg (*args, u8 *);
116   return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
117                    &a[4], &a[5]);
118 }
119
120 static u8 *
121 my_format_mac_address (u8 * s, va_list * args)
122 {
123   u8 *a = va_arg (*args, u8 *);
124   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
125                  a[0], a[1], a[2], a[3], a[4], a[5]);
126 }
127
128
129
130 static void vl_api_acl_plugin_get_version_reply_t_handler
131     (vl_api_acl_plugin_get_version_reply_t * mp)
132     {
133         vat_main_t * vam = acl_test_main.vat_main;
134         clib_warning("ACL plugin version: %d.%d", ntohl(mp->major), ntohl(mp->minor));
135         vam->result_ready = 1;
136     }
137
138 static void vl_api_acl_interface_list_details_t_handler
139     (vl_api_acl_interface_list_details_t * mp)
140     {
141         int i;
142         vat_main_t * vam = acl_test_main.vat_main;
143         u8 *out = 0;
144         vl_api_acl_interface_list_details_t_endian(mp);
145         out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
146         out = format(out, "   input ");
147         for(i=0; i<mp->count; i++) {
148           if (i == mp->n_input)
149             out = format(out, "\n  output ");
150           out = format(out, "%d ", ntohl (mp->acls[i]));
151         }
152         out = format(out, "\n");
153         clib_warning("%s", out);
154         vec_free(out);
155         vam->result_ready = 1;
156     }
157
158 static void vl_api_acl_interface_etype_whitelist_details_t_handler
159     (vl_api_acl_interface_etype_whitelist_details_t * mp)
160     {
161         int i;
162         vat_main_t * vam = acl_test_main.vat_main;
163         u8 *out = 0;
164         vl_api_acl_interface_etype_whitelist_details_t_endian(mp);
165         out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
166         out = format(out, "   input ");
167         for(i=0; i<mp->count; i++) {
168           if (i == mp->n_input)
169             out = format(out, "\n  output ");
170           out = format(out, "%04x ", ntohs(mp->whitelist[i]));
171         }
172         out = format(out, "\n");
173         clib_warning("%s", out);
174         vec_free(out);
175         vam->result_ready = 1;
176     }
177
178
179
180 static inline u8 *
181 vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
182 {
183   int af = a->is_ipv6 ? AF_INET6 : AF_INET;
184   u8 src[INET6_ADDRSTRLEN];
185   u8 dst[INET6_ADDRSTRLEN];
186   inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
187   inet_ntop(af, a->dst_ip_addr, (void *)dst, sizeof(dst));
188
189   out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
190                      a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
191                      src, a->src_ip_prefix_len,
192                      dst, a->dst_ip_prefix_len,
193                      a->proto,
194                      a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
195                      a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
196                      a->tcp_flags_value, a->tcp_flags_mask);
197   return(out);
198 }
199
200
201
202 static void vl_api_acl_details_t_handler
203     (vl_api_acl_details_t * mp)
204     {
205         int i;
206         vat_main_t * vam = acl_test_main.vat_main;
207         vl_api_acl_details_t_endian(mp);
208         u8 *out = 0;
209         out = format(0, "acl_index: %d, count: %d\n   tag {%s}\n", mp->acl_index, mp->count, mp->tag);
210         for(i=0; i<mp->count; i++) {
211           out = format(out, "   ");
212           out = vl_api_acl_rule_t_pretty_format(out, &mp->r[i]);
213           out = format(out, "%s\n", i<mp->count-1 ? "," : "");
214         }
215         clib_warning("%s", out);
216         vec_free(out);
217         vam->result_ready = 1;
218     }
219
220 static inline u8 *
221 vl_api_macip_acl_rule_t_pretty_format (u8 *out, vl_api_macip_acl_rule_t * a)
222 {
223   int af = a->is_ipv6 ? AF_INET6 : AF_INET;
224   u8 src[INET6_ADDRSTRLEN];
225   inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
226
227   out = format(out, "%s action %d ip %s/%d mac %U mask %U",
228                      a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
229                      src, a->src_ip_prefix_len,
230                      my_format_mac_address, a->src_mac,
231                      my_format_mac_address, a->src_mac_mask);
232   return(out);
233 }
234
235
236 static void vl_api_macip_acl_details_t_handler
237     (vl_api_macip_acl_details_t * mp)
238     {
239         int i;
240         vat_main_t * vam = acl_test_main.vat_main;
241         vl_api_macip_acl_details_t_endian(mp);
242         u8 *out = format(0,"MACIP acl_index: %d, count: %d\n   tag {%s}\n", mp->acl_index, mp->count, mp->tag);
243         for(i=0; i<mp->count; i++) {
244           out = format(out, "   ");
245           out = vl_api_macip_acl_rule_t_pretty_format(out, &mp->r[i]);
246           out = format(out, "%s\n", i<mp->count-1 ? "," : "");
247         }
248         clib_warning("%s", out);
249         vec_free(out);
250         vam->result_ready = 1;
251     }
252
253 static void vl_api_macip_acl_interface_get_reply_t_handler
254     (vl_api_macip_acl_interface_get_reply_t * mp)
255     {
256         int i;
257         vat_main_t * vam = acl_test_main.vat_main;
258         u8 *out = format(0, "sw_if_index with MACIP ACL count: %d\n", ntohl(mp->count));
259         for(i=0; i<ntohl(mp->count); i++) {
260           out = format(out, "  macip_acl_interface_add_del sw_if_index %d add acl %d\n", i, ntohl(mp->acls[i]));
261         }
262         out = format(out, "\n");
263         clib_warning("%s", out);
264         vec_free(out);
265         vam->result_ready = 1;
266     }
267
268 static void vl_api_acl_plugin_control_ping_reply_t_handler
269   (vl_api_acl_plugin_control_ping_reply_t * mp)
270 {
271   vat_main_t *vam = &vat_main;
272   i32 retval = ntohl (mp->retval);
273   if (vam->async_mode)
274     {
275       vam->async_errors += (retval < 0);
276     }
277   else
278     {
279       vam->retval = retval;
280       vam->result_ready = 1;
281     }
282 }
283
284
285 /*
286  * Table of message reply handlers, must include boilerplate handlers
287  * we just generated
288  */
289 #define foreach_vpe_api_reply_msg                                       \
290 _(ACL_ADD_REPLACE_REPLY, acl_add_replace_reply) \
291 _(ACL_DEL_REPLY, acl_del_reply) \
292 _(ACL_INTERFACE_ADD_DEL_REPLY, acl_interface_add_del_reply)  \
293 _(ACL_INTERFACE_SET_ACL_LIST_REPLY, acl_interface_set_acl_list_reply) \
294 _(ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY, acl_interface_set_etype_whitelist_reply) \
295 _(ACL_INTERFACE_ETYPE_WHITELIST_DETAILS, acl_interface_etype_whitelist_details)  \
296 _(ACL_INTERFACE_LIST_DETAILS, acl_interface_list_details)  \
297 _(ACL_DETAILS, acl_details)  \
298 _(MACIP_ACL_ADD_REPLY, macip_acl_add_reply) \
299 _(MACIP_ACL_ADD_REPLACE_REPLY, macip_acl_add_replace_reply) \
300 _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
301 _(MACIP_ACL_DETAILS, macip_acl_details)  \
302 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply)  \
303 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply)  \
304 _(ACL_PLUGIN_CONTROL_PING_REPLY, acl_plugin_control_ping_reply) \
305 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
306
307 static int api_acl_plugin_get_version (vat_main_t * vam)
308 {
309     acl_test_main_t * sm = &acl_test_main;
310     vl_api_acl_plugin_get_version_t * mp;
311     u32 msg_size = sizeof(*mp);
312     int ret;
313
314     vam->result_ready = 0;
315     mp = vl_msg_api_alloc_as_if_client(msg_size);
316     memset (mp, 0, msg_size);
317     mp->_vl_msg_id = ntohs (VL_API_ACL_PLUGIN_GET_VERSION + sm->msg_id_base);
318     mp->client_index = vam->my_client_index;
319
320     /* send it... */
321     S(mp);
322
323     /* Wait for a reply... */
324     W (ret);
325     return ret;
326 }
327
328 static int api_macip_acl_interface_get (vat_main_t * vam)
329 {
330     acl_test_main_t * sm = &acl_test_main;
331     vl_api_acl_plugin_get_version_t * mp;
332     u32 msg_size = sizeof(*mp);
333     int ret;
334
335     vam->result_ready = 0;
336     mp = vl_msg_api_alloc_as_if_client(msg_size);
337     memset (mp, 0, msg_size);
338     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_GET + sm->msg_id_base);
339     mp->client_index = vam->my_client_index;
340
341     /* send it... */
342     S(mp);
343
344     /* Wait for a reply... */
345     W (ret);
346     return ret;
347 }
348
349 #define vec_validate_acl_rules(v, idx) \
350   do {                                 \
351     if (vec_len(v) < idx+1) {  \
352       vec_validate(v, idx); \
353       v[idx].is_permit = 0x1; \
354       v[idx].srcport_or_icmptype_last = 0xffff; \
355       v[idx].dstport_or_icmpcode_last = 0xffff; \
356     } \
357   } while (0)
358
359
360 static int api_acl_add_replace (vat_main_t * vam)
361 {
362     acl_test_main_t * sm = &acl_test_main;
363     unformat_input_t * i = vam->input;
364     vl_api_acl_add_replace_t * mp;
365     u32 acl_index = ~0;
366     u32 msg_size = sizeof (*mp); /* without the rules */
367
368     vl_api_acl_rule_t *rules = 0;
369     int rule_idx = 0;
370     int n_rules = 0;
371     int n_rules_override = -1;
372     u32 proto = 0;
373     u32 port1 = 0;
374     u32 port2 = 0;
375     u32 action = 0;
376     u32 tcpflags, tcpmask;
377     u32 src_prefix_length = 0, dst_prefix_length = 0;
378     ip4_address_t src_v4address, dst_v4address;
379     ip6_address_t src_v6address, dst_v6address;
380     u8 *tag = 0;
381     int ret;
382
383     if (!unformat (i, "%d", &acl_index)) {
384         /* Just assume -1 */
385     }
386
387     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
388     {
389         if (unformat (i, "ipv6"))
390           {
391             vec_validate_acl_rules(rules, rule_idx);
392             rules[rule_idx].is_ipv6 = 1;
393           }
394         else if (unformat (i, "ipv4"))
395           {
396             vec_validate_acl_rules(rules, rule_idx);
397             rules[rule_idx].is_ipv6 = 0;
398           }
399         else if (unformat (i, "permit+reflect"))
400           {
401             vec_validate_acl_rules(rules, rule_idx);
402             rules[rule_idx].is_permit = 2;
403           }
404         else if (unformat (i, "permit"))
405           {
406             vec_validate_acl_rules(rules, rule_idx);
407             rules[rule_idx].is_permit = 1;
408           }
409         else if (unformat (i, "deny"))
410           {
411             vec_validate_acl_rules(rules, rule_idx);
412             rules[rule_idx].is_permit = 0;
413           }
414         else if (unformat (i, "count %d", &n_rules_override))
415           {
416             /* we will use this later */
417           }
418         else if (unformat (i, "action %d", &action))
419           {
420             vec_validate_acl_rules(rules, rule_idx);
421             rules[rule_idx].is_permit = action;
422           }
423         else if (unformat (i, "src %U/%d",
424          unformat_ip4_address, &src_v4address, &src_prefix_length))
425           {
426             vec_validate_acl_rules(rules, rule_idx);
427             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
428             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
429             rules[rule_idx].is_ipv6 = 0;
430           }
431         else if (unformat (i, "src %U/%d",
432          unformat_ip6_address, &src_v6address, &src_prefix_length))
433           {
434             vec_validate_acl_rules(rules, rule_idx);
435             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
436             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
437             rules[rule_idx].is_ipv6 = 1;
438           }
439         else if (unformat (i, "dst %U/%d",
440          unformat_ip4_address, &dst_v4address, &dst_prefix_length))
441           {
442             vec_validate_acl_rules(rules, rule_idx);
443             memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
444             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
445             rules[rule_idx].is_ipv6 = 0;
446           }
447         else if (unformat (i, "dst %U/%d",
448          unformat_ip6_address, &dst_v6address, &dst_prefix_length))
449           {
450             vec_validate_acl_rules(rules, rule_idx);
451             memcpy (rules[rule_idx].dst_ip_addr, &dst_v6address, 16);
452             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
453             rules[rule_idx].is_ipv6 = 1;
454           }
455         else if (unformat (i, "sport %d-%d", &port1, &port2))
456           {
457             vec_validate_acl_rules(rules, rule_idx);
458             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
459             rules[rule_idx].srcport_or_icmptype_last = htons(port2);
460           }
461         else if (unformat (i, "sport %d", &port1))
462           {
463             vec_validate_acl_rules(rules, rule_idx);
464             rules[rule_idx].srcport_or_icmptype_first = htons(port1);
465             rules[rule_idx].srcport_or_icmptype_last = htons(port1);
466           }
467         else if (unformat (i, "dport %d-%d", &port1, &port2))
468           {
469             vec_validate_acl_rules(rules, rule_idx);
470             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
471             rules[rule_idx].dstport_or_icmpcode_last = htons(port2);
472           }
473         else if (unformat (i, "dport %d", &port1))
474           {
475             vec_validate_acl_rules(rules, rule_idx);
476             rules[rule_idx].dstport_or_icmpcode_first = htons(port1);
477             rules[rule_idx].dstport_or_icmpcode_last = htons(port1);
478           }
479         else if (unformat (i, "tcpflags %d %d", &tcpflags, &tcpmask))
480           {
481             vec_validate_acl_rules(rules, rule_idx);
482             rules[rule_idx].tcp_flags_value = tcpflags;
483             rules[rule_idx].tcp_flags_mask = tcpmask;
484           }
485         else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
486           {
487             vec_validate_acl_rules(rules, rule_idx);
488             rules[rule_idx].tcp_flags_value = tcpflags;
489             rules[rule_idx].tcp_flags_mask = tcpmask;
490           }
491         else if (unformat (i, "proto %d", &proto))
492           {
493             vec_validate_acl_rules(rules, rule_idx);
494             rules[rule_idx].proto = proto;
495           }
496         else if (unformat (i, "tag %s", &tag))
497           {
498           }
499         else if (unformat (i, ","))
500           {
501             rule_idx++;
502             vec_validate_acl_rules(rules, rule_idx);
503           }
504         else
505     break;
506     }
507
508     /* Construct the API message */
509     vam->result_ready = 0;
510
511     if(rules)
512       n_rules = vec_len(rules);
513     else
514       n_rules = 0;
515
516     if (n_rules_override >= 0)
517       n_rules = n_rules_override;
518
519     msg_size += n_rules*sizeof(rules[0]);
520
521     mp = vl_msg_api_alloc_as_if_client(msg_size);
522     memset (mp, 0, msg_size);
523     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
524     mp->client_index = vam->my_client_index;
525     if ((n_rules > 0) && rules)
526       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
527     if (tag)
528       {
529         if (vec_len(tag) >= sizeof(mp->tag))
530           {
531             tag[sizeof(mp->tag)-1] = 0;
532             _vec_len(tag) = sizeof(mp->tag);
533           }
534         clib_memcpy(mp->tag, tag, vec_len(tag));
535         vec_free(tag);
536       }
537     mp->acl_index = ntohl(acl_index);
538     mp->count = htonl(n_rules);
539
540     /* send it... */
541     S(mp);
542
543     /* Wait for a reply... */
544     W (ret);
545     return ret;
546 }
547
548
549 /*
550  * Read the series of ACL entries from file in the following format:
551  *
552
553 @0.0.0.0/1      131.179.121.0/24        0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
554 @128.0.0.0/1    85.54.226.0/23  0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
555 @128.0.0.0/1    85.54.48.0/23   0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
556 @128.0.0.0/1    31.237.44.0/23  0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
557 @0.0.0.0/1      255.84.184.0/23 0 : 65535       0 : 65535       0x00/0x00       0x0000/0x0000
558 @132.92.0.0/16  0.0.0.0/0       0 : 65535       0 : 65535       0x01/0xFF       0x0000/0x0000
559
560  *
561  */
562
563 static int
564 api_acl_add_replace_from_file (vat_main_t * vam)
565 {
566     int ret = -1;
567     unformat_input_t * input = vam->input;
568     acl_test_main_t * sm = &acl_test_main;
569     vl_api_acl_add_replace_t * mp;
570     u32 acl_index = ~0;
571     u32 msg_size = sizeof (*mp); /* without the rules */
572
573     vl_api_acl_rule_t *rules = 0;
574     int rule_idx = -1;
575     int n_rules = 0;
576     int n_rules_override = -1;
577     int is_permit = 0;
578     int append_default_permit = 0;
579     u32 tcpflags = 0, tcpmask = 0;
580     ip4_address_t src_v4address, dst_v4address;
581     u8 *tag = 0;
582     int fd = -1;
583
584     char *file_name = NULL;
585     unformat_input_t file_input;
586
587     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
588       {
589         if (unformat (input, "filename %s", &file_name))
590           {
591             /* we will use this later */
592           }
593         else if (unformat (input, "acl-index %d", &acl_index))
594           {
595             /* we will try to replace an existing ACL */
596           }
597         else if (unformat (input, "permit+reflect"))
598           {
599             is_permit = 2;
600           }
601         else if (unformat (input, "permit"))
602           {
603             is_permit = 1;
604           }
605         else if (unformat (input, "append-default-permit"))
606           {
607             append_default_permit = 1;
608           }
609         else
610           break;
611       }
612
613     fd = open(file_name, O_RDONLY);
614     if (fd < 0)
615       {
616         clib_warning("Could not open file '%s'");
617         goto done;
618       }
619
620     /* input from file */
621     input =  &file_input;
622     unformat_init_clib_file(input, fd);
623
624     unsigned sport_low, sport_high, dport_low, dport_high;
625     unsigned proto, protomask;
626     u32 src_prefix_length, dst_prefix_length;
627     u32 unused1, unused2;
628
629     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
630       {
631             if (!unformat(input, "@%U/%d\t%U/%d\t%d : %d\t%d : %d\t0x%x/0x%x\t0x%x/0x%x",
632                                  unformat_ip4_address, &src_v4address, &src_prefix_length,
633                                  unformat_ip4_address, &dst_v4address, &dst_prefix_length,
634                                  &sport_low, &sport_high, &dport_low, &dport_high, &proto, &protomask, &unused1, &unused2)) {
635               clib_warning("Error parsing");
636               break;
637             }
638
639             rule_idx++;
640             vec_validate_acl_rules(rules, rule_idx);
641
642             rules[rule_idx].is_ipv6 = 0;
643             rules[rule_idx].is_permit = is_permit;
644             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
645             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
646             memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
647             rules[rule_idx].dst_ip_prefix_len = dst_prefix_length;
648             rules[rule_idx].srcport_or_icmptype_first = htons(sport_low);
649             rules[rule_idx].srcport_or_icmptype_last = htons(sport_high);
650             rules[rule_idx].dstport_or_icmpcode_first = htons(dport_low);
651             rules[rule_idx].dstport_or_icmpcode_last = htons(dport_high);
652             rules[rule_idx].tcp_flags_value = tcpflags;
653             rules[rule_idx].tcp_flags_mask = tcpmask;
654             rules[rule_idx].proto = proto;
655
656       }
657     rules[rule_idx].is_permit = is_permit;
658
659     if (append_default_permit) {
660         rule_idx++;
661         vec_validate_acl_rules(rules, rule_idx);
662
663         rules[rule_idx].is_ipv6 = 0;
664         rules[rule_idx].is_permit = is_permit == 2 ? 2 : 1;
665
666         src_v4address.data[0]=0;
667         src_v4address.data[1]=0;
668         src_v4address.data[2]=0;
669         src_v4address.data[3]=0;
670         memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
671         rules[rule_idx].src_ip_prefix_len = 0;
672
673         dst_v4address.data[0]=0;
674         dst_v4address.data[1]=0;
675         dst_v4address.data[2]=0;
676         dst_v4address.data[3]=0;
677         memcpy (rules[rule_idx].dst_ip_addr, &dst_v4address, 4);
678         rules[rule_idx].dst_ip_prefix_len = 0;
679
680         rules[rule_idx].srcport_or_icmptype_first = htons(0);
681         rules[rule_idx].srcport_or_icmptype_last = htons(65535);
682         rules[rule_idx].dstport_or_icmpcode_first = htons(0);
683         rules[rule_idx].dstport_or_icmpcode_last = htons(65535);
684         rules[rule_idx].tcp_flags_value = 0;
685         rules[rule_idx].tcp_flags_mask = 0;
686         rules[rule_idx].proto = 0;
687     }
688
689     /* Construct the API message */
690
691     vam->result_ready = 0;
692
693     if(rules)
694       n_rules = vec_len(rules);
695     else
696       n_rules = 0;
697
698     if (n_rules_override >= 0)
699       n_rules = n_rules_override;
700
701     msg_size += n_rules*sizeof(rules[0]);
702
703     mp = vl_msg_api_alloc_as_if_client(msg_size);
704     memset (mp, 0, msg_size);
705     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
706     mp->client_index = vam->my_client_index;
707     mp->client_index = 0;
708     if (n_rules > 0)
709       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
710     if (tag)
711       {
712         if (vec_len(tag) >= sizeof(mp->tag))
713           {
714             tag[sizeof(mp->tag)-1] = 0;
715             _vec_len(tag) = sizeof(mp->tag);
716           }
717         clib_memcpy(mp->tag, tag, vec_len(tag));
718         vec_free(tag);
719       }
720     mp->acl_index = ntohl(acl_index);
721     mp->count = htonl(n_rules);
722
723     /* send it... */
724     S(mp);
725
726     /* Wait for a reply... */
727     W (ret);
728 done:
729     if (fd > 0)
730       close (fd);
731     vec_free(file_name);
732
733     return ret;
734 }
735
736
737 static int api_acl_del (vat_main_t * vam)
738 {
739     unformat_input_t * i = vam->input;
740     vl_api_acl_del_t * mp;
741     u32 acl_index = ~0;
742     int ret;
743
744     if (!unformat (i, "%d", &acl_index)) {
745       errmsg ("missing acl index\n");
746       return -99;
747     }
748
749     /* Construct the API message */
750     M(ACL_DEL, mp);
751     mp->acl_index = ntohl(acl_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_macip_acl_del (vat_main_t * vam)
762 {
763     unformat_input_t * i = vam->input;
764     vl_api_acl_del_t * mp;
765     u32 acl_index = ~0;
766     int ret;
767
768     if (!unformat (i, "%d", &acl_index)) {
769       errmsg ("missing acl index\n");
770       return -99;
771     }
772
773     /* Construct the API message */
774     M(MACIP_ACL_DEL, mp);
775     mp->acl_index = ntohl(acl_index);
776
777     /* send it... */
778     S(mp);
779
780     /* Wait for a reply... */
781     W (ret);
782     return ret;
783 }
784
785 static int api_acl_interface_add_del (vat_main_t * vam)
786 {
787     unformat_input_t * i = vam->input;
788     vl_api_acl_interface_add_del_t * mp;
789     u32 sw_if_index = ~0;
790     u32 acl_index = ~0;
791     u8 is_input = 0;
792     u8 is_add = 0;
793     int ret;
794
795 //    acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
796
797     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
798     {
799         if (unformat (i, "%d", &acl_index))
800     ;
801         else
802     break;
803     }
804
805
806     /* Parse args required to build the message */
807     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
808         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
809             ;
810         else if (unformat (i, "sw_if_index %d", &sw_if_index))
811             ;
812         else if (unformat (i, "add"))
813             is_add = 1;
814         else if (unformat (i, "del"))
815             is_add = 0;
816         else if (unformat (i, "acl %d", &acl_index))
817             ;
818         else if (unformat (i, "input"))
819             is_input = 1;
820         else if (unformat (i, "output"))
821             is_input = 0;
822         else
823             break;
824     }
825
826     if (sw_if_index == ~0) {
827         errmsg ("missing interface name / explicit sw_if_index number \n");
828         return -99;
829     }
830
831     if (acl_index == ~0) {
832         errmsg ("missing ACL index\n");
833         return -99;
834     }
835
836
837
838     /* Construct the API message */
839     M(ACL_INTERFACE_ADD_DEL, mp);
840     mp->acl_index = ntohl(acl_index);
841     mp->sw_if_index = ntohl(sw_if_index);
842     mp->is_add = is_add;
843     mp->is_input = is_input;
844
845     /* send it... */
846     S(mp);
847
848     /* Wait for a reply... */
849     W (ret);
850     return ret;
851 }
852
853 static int api_macip_acl_interface_add_del (vat_main_t * vam)
854 {
855     unformat_input_t * i = vam->input;
856     vl_api_macip_acl_interface_add_del_t * mp;
857     u32 sw_if_index = ~0;
858     u32 acl_index = ~0;
859     u8 is_add = 0;
860     int ret;
861
862     /* Parse args required to build the message */
863     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
864         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
865             ;
866         else if (unformat (i, "sw_if_index %d", &sw_if_index))
867             ;
868         else if (unformat (i, "add"))
869             is_add = 1;
870         else if (unformat (i, "del"))
871             is_add = 0;
872         else if (unformat (i, "acl %d", &acl_index))
873             ;
874         else
875             break;
876     }
877
878     if (sw_if_index == ~0) {
879         errmsg ("missing interface name / explicit sw_if_index number \n");
880         return -99;
881     }
882
883     if (acl_index == ~0) {
884         errmsg ("missing ACL index\n");
885         return -99;
886     }
887
888
889
890     /* Construct the API message */
891     M(MACIP_ACL_INTERFACE_ADD_DEL, mp);
892     mp->acl_index = ntohl(acl_index);
893     mp->sw_if_index = ntohl(sw_if_index);
894     mp->is_add = is_add;
895
896     /* send it... */
897     S(mp);
898
899     /* Wait for a reply... */
900     W (ret);
901     return ret;
902 }
903
904 static int api_acl_interface_set_acl_list (vat_main_t * vam)
905 {
906     unformat_input_t * i = vam->input;
907     vl_api_acl_interface_set_acl_list_t * mp;
908     u32 sw_if_index = ~0;
909     u32 acl_index = ~0;
910     u32 *inacls = 0;
911     u32 *outacls = 0;
912     u8 is_input = 0;
913     int ret;
914
915 //  acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
916
917     /* Parse args required to build the message */
918     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
919         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
920             ;
921         else if (unformat (i, "sw_if_index %d", &sw_if_index))
922             ;
923         else if (unformat (i, "%d", &acl_index))
924           {
925             if(is_input)
926               vec_add1(inacls, htonl(acl_index));
927             else
928               vec_add1(outacls, htonl(acl_index));
929           }
930         else if (unformat (i, "acl %d", &acl_index))
931             ;
932         else if (unformat (i, "input"))
933             is_input = 1;
934         else if (unformat (i, "output"))
935             is_input = 0;
936         else
937             break;
938     }
939
940     if (sw_if_index == ~0) {
941         errmsg ("missing interface name / explicit sw_if_index number \n");
942         return -99;
943     }
944
945     /* Construct the API message */
946     M2(ACL_INTERFACE_SET_ACL_LIST, mp, sizeof(u32) * (vec_len(inacls) + vec_len(outacls)));
947     mp->sw_if_index = ntohl(sw_if_index);
948     mp->n_input = vec_len(inacls);
949     mp->count = vec_len(inacls) + vec_len(outacls);
950     vec_append(inacls, outacls);
951     if (vec_len(inacls) > 0)
952       clib_memcpy(mp->acls, inacls, vec_len(inacls)*sizeof(u32));
953
954     /* send it... */
955     S(mp);
956
957     /* Wait for a reply... */
958     W (ret);
959     return ret;
960 }
961
962 static int api_acl_interface_set_etype_whitelist (vat_main_t * vam)
963 {
964     unformat_input_t * i = vam->input;
965     vl_api_acl_interface_set_etype_whitelist_t * mp;
966     u32 sw_if_index = ~0;
967     u32 ethertype = ~0;
968     u16 *etypes_in = 0;
969     u16 *etypes_out = 0;
970     u8 is_input = 1;
971     int ret;
972
973 //  acl_interface_set_etype_whitelist <intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]
974
975     /* Parse args required to build the message */
976     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
977         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
978             ;
979         else if (unformat (i, "sw_if_index %d", &sw_if_index))
980             ;
981         else if (unformat (i, "%x", &ethertype))
982           {
983             ethertype = ethertype & 0xffff;
984             if(is_input)
985               vec_add1(etypes_in, htons(ethertype));
986             else
987               vec_add1(etypes_out, htons(ethertype));
988           }
989         else if (unformat (i, "input"))
990             is_input = 1;
991         else if (unformat (i, "output"))
992             is_input = 0;
993         else
994             break;
995     }
996
997     if (sw_if_index == ~0) {
998         errmsg ("missing interface name / explicit sw_if_index number \n");
999         return -99;
1000     }
1001
1002     /* Construct the API message */
1003     M2(ACL_INTERFACE_SET_ETYPE_WHITELIST, mp, sizeof(u32) * (vec_len(etypes_in) + vec_len(etypes_out)));
1004     mp->sw_if_index = ntohl(sw_if_index);
1005     mp->n_input = vec_len(etypes_in);
1006     mp->count = vec_len(etypes_in) + vec_len(etypes_out);
1007     vec_append(etypes_in, etypes_out);
1008     if (vec_len(etypes_in) > 0)
1009       clib_memcpy(mp->whitelist, etypes_in, vec_len(etypes_in)*sizeof(etypes_in[0]));
1010
1011     /* send it... */
1012     S(mp);
1013
1014     /* Wait for a reply... */
1015     W (ret);
1016     return ret;
1017 }
1018
1019 static void
1020 api_acl_send_control_ping(vat_main_t *vam)
1021 {
1022   vl_api_acl_plugin_control_ping_t *mp_ping;
1023
1024   M(ACL_PLUGIN_CONTROL_PING, mp_ping);
1025   S(mp_ping);
1026 }
1027
1028
1029 static int api_acl_interface_list_dump (vat_main_t * vam)
1030 {
1031     unformat_input_t * i = vam->input;
1032     u32 sw_if_index = ~0;
1033     vl_api_acl_interface_list_dump_t * mp;
1034     int ret;
1035
1036     /* Parse args required to build the message */
1037     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1038         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1039             ;
1040         else if (unformat (i, "sw_if_index %d", &sw_if_index))
1041             ;
1042         else
1043             break;
1044     }
1045
1046     /* Construct the API message */
1047     M(ACL_INTERFACE_LIST_DUMP, mp);
1048     mp->sw_if_index = ntohl (sw_if_index);
1049
1050     /* send it... */
1051     S(mp);
1052
1053     /* Use control ping for synchronization */
1054     api_acl_send_control_ping(vam);
1055
1056     /* Wait for a reply... */
1057     W (ret);
1058     return ret;
1059 }
1060
1061 static int api_acl_dump (vat_main_t * vam)
1062 {
1063     unformat_input_t * i = vam->input;
1064     u32 acl_index = ~0;
1065     vl_api_acl_dump_t * mp;
1066     int ret;
1067
1068     /* Parse args required to build the message */
1069     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1070         if (unformat (i, "%d", &acl_index))
1071             ;
1072         else
1073             break;
1074     }
1075
1076     /* Construct the API message */
1077     M(ACL_DUMP, mp);
1078     mp->acl_index = ntohl (acl_index);
1079
1080     /* send it... */
1081     S(mp);
1082
1083     /* Use control ping for synchronization */
1084     api_acl_send_control_ping(vam);
1085
1086     /* Wait for a reply... */
1087     W (ret);
1088     return ret;
1089 }
1090
1091 static int api_macip_acl_dump (vat_main_t * vam)
1092 {
1093     unformat_input_t * i = vam->input;
1094     u32 acl_index = ~0;
1095     vl_api_acl_dump_t * mp;
1096     int ret;
1097
1098     /* Parse args required to build the message */
1099     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1100         if (unformat (i, "%d", &acl_index))
1101             ;
1102         else
1103             break;
1104     }
1105
1106     /* Construct the API message */
1107     M(MACIP_ACL_DUMP, mp);
1108     mp->acl_index = ntohl (acl_index);
1109
1110     /* send it... */
1111     S(mp);
1112
1113     /* Use control ping for synchronization */
1114     api_acl_send_control_ping(vam);
1115
1116     /* Wait for a reply... */
1117     W (ret);
1118     return ret;
1119 }
1120
1121 static int api_acl_interface_etype_whitelist_dump (vat_main_t * vam)
1122 {
1123     unformat_input_t * i = vam->input;
1124     u32 sw_if_index = ~0;
1125     vl_api_acl_interface_etype_whitelist_dump_t * mp;
1126     int ret;
1127
1128     /* Parse args required to build the message */
1129     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
1130         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
1131             ;
1132         else if (unformat (i, "sw_if_index %d", &sw_if_index))
1133             ;
1134         else
1135             break;
1136     }
1137
1138     /* Construct the API message */
1139     M(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, mp);
1140     mp->sw_if_index = ntohl (sw_if_index);
1141
1142     /* send it... */
1143     S(mp);
1144
1145     /* Use control ping for synchronization */
1146     api_acl_send_control_ping(vam);
1147
1148     /* Wait for a reply... */
1149     W (ret);
1150     return ret;
1151 }
1152
1153
1154 #define vec_validate_macip_acl_rules(v, idx) \
1155   do {                                 \
1156     if (vec_len(v) < idx+1) {  \
1157       vec_validate(v, idx); \
1158       v[idx].is_permit = 0x1; \
1159     } \
1160   } while (0)
1161
1162
1163 static int api_macip_acl_add (vat_main_t * vam)
1164 {
1165     acl_test_main_t * sm = &acl_test_main;
1166     unformat_input_t * i = vam->input;
1167     vl_api_macip_acl_add_t * mp;
1168     u32 msg_size = sizeof (*mp); /* without the rules */
1169
1170     vl_api_macip_acl_rule_t *rules = 0;
1171     int rule_idx = 0;
1172     int n_rules = 0;
1173     int n_rules_override = -1;
1174     u32 src_prefix_length = 0;
1175     u32 action = 0;
1176     ip4_address_t src_v4address;
1177     ip6_address_t src_v6address;
1178     u8 src_mac[6];
1179     u8 *tag = 0;
1180     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1181     int ret;
1182
1183     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1184     {
1185         if (unformat (i, "ipv6"))
1186           {
1187             vec_validate_macip_acl_rules(rules, rule_idx);
1188             rules[rule_idx].is_ipv6 = 1;
1189           }
1190         else if (unformat (i, "ipv4"))
1191           {
1192             vec_validate_macip_acl_rules(rules, rule_idx);
1193             rules[rule_idx].is_ipv6 = 0;
1194           }
1195         else if (unformat (i, "permit"))
1196           {
1197             vec_validate_macip_acl_rules(rules, rule_idx);
1198             rules[rule_idx].is_permit = 1;
1199           }
1200         else if (unformat (i, "deny"))
1201           {
1202             vec_validate_macip_acl_rules(rules, rule_idx);
1203             rules[rule_idx].is_permit = 0;
1204           }
1205         else if (unformat (i, "count %d", &n_rules_override))
1206           {
1207             /* we will use this later */
1208           }
1209         else if (unformat (i, "action %d", &action))
1210           {
1211             vec_validate_macip_acl_rules(rules, rule_idx);
1212             rules[rule_idx].is_permit = action;
1213           }
1214         else if (unformat (i, "ip %U/%d",
1215          unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1216                  unformat (i, "ip %U",
1217          unformat_ip4_address, &src_v4address))
1218           {
1219             if (src_prefix_length == 0)
1220               src_prefix_length = 32;
1221             vec_validate_macip_acl_rules(rules, rule_idx);
1222             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1223             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1224             rules[rule_idx].is_ipv6 = 0;
1225           }
1226         else if (unformat (i, "src"))
1227           {
1228             /* Everything in MACIP is "source" but allow this verbosity */
1229           }
1230         else if (unformat (i, "ip %U/%d",
1231          unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1232                  unformat (i, "ip %U",
1233          unformat_ip6_address, &src_v6address))
1234           {
1235             if (src_prefix_length == 0)
1236               src_prefix_length = 128;
1237             vec_validate_macip_acl_rules(rules, rule_idx);
1238             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1239             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1240             rules[rule_idx].is_ipv6 = 1;
1241           }
1242         else if (unformat (i, "mac %U",
1243          my_unformat_mac_address, &src_mac))
1244           {
1245             vec_validate_macip_acl_rules(rules, rule_idx);
1246             memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1247             memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1248           }
1249         else if (unformat (i, "mask %U",
1250          my_unformat_mac_address, &src_mac))
1251           {
1252             vec_validate_macip_acl_rules(rules, rule_idx);
1253             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1254           }
1255         else if (unformat (i, "tag %s", &tag))
1256           {
1257           }
1258         else if (unformat (i, ","))
1259           {
1260             rule_idx++;
1261             vec_validate_macip_acl_rules(rules, rule_idx);
1262           }
1263         else
1264     break;
1265     }
1266
1267     /* Construct the API message */
1268     vam->result_ready = 0;
1269
1270     if(rules)
1271       n_rules = vec_len(rules);
1272
1273     if (n_rules_override >= 0)
1274       n_rules = n_rules_override;
1275
1276     msg_size += n_rules*sizeof(rules[0]);
1277
1278     mp = vl_msg_api_alloc_as_if_client(msg_size);
1279     memset (mp, 0, msg_size);
1280     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
1281     mp->client_index = vam->my_client_index;
1282     if ((n_rules > 0) && rules)
1283       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1284     if (tag)
1285       {
1286         if (vec_len(tag) >= sizeof(mp->tag))
1287           {
1288             tag[sizeof(mp->tag)-1] = 0;
1289             _vec_len(tag) = sizeof(mp->tag);
1290           }
1291         clib_memcpy(mp->tag, tag, vec_len(tag));
1292         vec_free(tag);
1293       }
1294
1295     mp->count = htonl(n_rules);
1296
1297     /* send it... */
1298     S(mp);
1299
1300     /* Wait for a reply... */
1301     W (ret);
1302     return ret;
1303 }
1304
1305 static int api_macip_acl_add_replace (vat_main_t * vam)
1306 {
1307     acl_test_main_t * sm = &acl_test_main;
1308     unformat_input_t * i = vam->input;
1309     vl_api_macip_acl_add_replace_t * mp;
1310     u32 acl_index = ~0;
1311     u32 msg_size = sizeof (*mp); /* without the rules */
1312
1313     vl_api_macip_acl_rule_t *rules = 0;
1314     int rule_idx = 0;
1315     int n_rules = 0;
1316     int n_rules_override = -1;
1317     u32 src_prefix_length = 0;
1318     u32 action = 0;
1319     ip4_address_t src_v4address;
1320     ip6_address_t src_v6address;
1321     u8 src_mac[6];
1322     u8 *tag = 0;
1323     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1324     int ret;
1325
1326     if (!unformat (i, "%d", &acl_index)) {
1327         /* Just assume -1 */
1328     }
1329
1330     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
1331     {
1332         if (unformat (i, "ipv6"))
1333           {
1334             vec_validate_macip_acl_rules(rules, rule_idx);
1335             rules[rule_idx].is_ipv6 = 1;
1336           }
1337         else if (unformat (i, "ipv4"))
1338           {
1339             vec_validate_macip_acl_rules(rules, rule_idx);
1340             rules[rule_idx].is_ipv6 = 0;
1341           }
1342         else if (unformat (i, "permit"))
1343           {
1344             vec_validate_macip_acl_rules(rules, rule_idx);
1345             rules[rule_idx].is_permit = 1;
1346           }
1347         else if (unformat (i, "deny"))
1348           {
1349             vec_validate_macip_acl_rules(rules, rule_idx);
1350             rules[rule_idx].is_permit = 0;
1351           }
1352         else if (unformat (i, "count %d", &n_rules_override))
1353           {
1354             /* we will use this later */
1355           }
1356         else if (unformat (i, "action %d", &action))
1357           {
1358             vec_validate_macip_acl_rules(rules, rule_idx);
1359             rules[rule_idx].is_permit = action;
1360           }
1361         else if (unformat (i, "ip %U/%d",
1362          unformat_ip4_address, &src_v4address, &src_prefix_length) ||
1363                  unformat (i, "ip %U",
1364          unformat_ip4_address, &src_v4address))
1365           {
1366             if (src_prefix_length == 0)
1367               src_prefix_length = 32;
1368             vec_validate_macip_acl_rules(rules, rule_idx);
1369             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
1370             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1371             rules[rule_idx].is_ipv6 = 0;
1372           }
1373         else if (unformat (i, "src"))
1374           {
1375             /* Everything in MACIP is "source" but allow this verbosity */
1376           }
1377         else if (unformat (i, "ip %U/%d",
1378          unformat_ip6_address, &src_v6address, &src_prefix_length) ||
1379                  unformat (i, "ip %U",
1380          unformat_ip6_address, &src_v6address))
1381           {
1382             if (src_prefix_length == 0)
1383               src_prefix_length = 128;
1384             vec_validate_macip_acl_rules(rules, rule_idx);
1385             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
1386             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
1387             rules[rule_idx].is_ipv6 = 1;
1388           }
1389         else if (unformat (i, "mac %U",
1390          my_unformat_mac_address, &src_mac))
1391           {
1392             vec_validate_macip_acl_rules(rules, rule_idx);
1393             memcpy (rules[rule_idx].src_mac, &src_mac, 6);
1394             memcpy (rules[rule_idx].src_mac_mask, &mac_mask_all_1, 6);
1395           }
1396         else if (unformat (i, "mask %U",
1397          my_unformat_mac_address, &src_mac))
1398           {
1399             vec_validate_macip_acl_rules(rules, rule_idx);
1400             memcpy (rules[rule_idx].src_mac_mask, &src_mac, 6);
1401           }
1402         else if (unformat (i, "tag %s", &tag))
1403           {
1404           }
1405         else if (unformat (i, ","))
1406           {
1407             rule_idx++;
1408             vec_validate_macip_acl_rules(rules, rule_idx);
1409           }
1410         else
1411     break;
1412     }
1413
1414     if (!rules)
1415       {
1416       errmsg ("rule/s required\n");
1417       return -99;
1418       }
1419     /* Construct the API message */
1420     vam->result_ready = 0;
1421
1422     if(rules)
1423       n_rules = vec_len(rules);
1424
1425     if (n_rules_override >= 0)
1426       n_rules = n_rules_override;
1427
1428     msg_size += n_rules*sizeof(rules[0]);
1429
1430     mp = vl_msg_api_alloc_as_if_client(msg_size);
1431     memset (mp, 0, msg_size);
1432     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD_REPLACE + sm->msg_id_base);
1433     mp->client_index = vam->my_client_index;
1434     if ((n_rules > 0) && rules)
1435       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
1436     if (tag)
1437       {
1438         if (vec_len(tag) >= sizeof(mp->tag))
1439           {
1440             tag[sizeof(mp->tag)-1] = 0;
1441             _vec_len(tag) = sizeof(mp->tag);
1442           }
1443         clib_memcpy(mp->tag, tag, vec_len(tag));
1444         vec_free(tag);
1445       }
1446
1447     mp->acl_index = ntohl(acl_index);
1448     mp->count = htonl(n_rules);
1449
1450     /* send it... */
1451     S(mp);
1452
1453     /* Wait for a reply... */
1454     W (ret);
1455     return ret;
1456 }
1457
1458 /*
1459  * List of messages that the api test plugin sends,
1460  * and that the data plane plugin processes
1461  */
1462 #define foreach_vpe_api_msg \
1463 _(acl_plugin_get_version, "") \
1464 _(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], ... , ...") \
1465 _(acl_add_replace_from_file, "filename <file> [permit] [append-default-permit]") \
1466 _(acl_del, "<acl-idx>") \
1467 _(acl_dump, "[<acl-idx>]") \
1468 _(acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] [input|output] acl <acl-idx>") \
1469 _(acl_interface_set_acl_list, "<intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]") \
1470 _(acl_interface_set_etype_whitelist, "<intfc> | sw_if_index <if-idx> input [ethertype list] output [ethertype list]") \
1471 _(acl_interface_etype_whitelist_dump, "[<intfc> | sw_if_index <if-idx>]") \
1472 _(acl_interface_list_dump, "[<intfc> | sw_if_index <if-idx>]") \
1473 _(macip_acl_add, "...") \
1474 _(macip_acl_add_replace, "<acl-idx> [<ipv4|ipv6> <permit|deny|action N> [count <count>] [src] ip <ipaddress/[plen]> mac <mac> mask <mac_mask>, ... , ...") \
1475 _(macip_acl_del, "<acl-idx>")\
1476 _(macip_acl_dump, "[<acl-idx>]") \
1477 _(macip_acl_interface_add_del, "<intfc> | sw_if_index <if-idx> [add|del] acl <acl-idx>") \
1478 _(macip_acl_interface_get, "")
1479
1480
1481 static
1482 void acl_vat_api_hookup (vat_main_t *vam)
1483 {
1484     acl_test_main_t * sm = &acl_test_main;
1485     /* Hook up handlers for replies from the data plane plug-in */
1486 #define _(N,n)                                                  \
1487     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
1488                            #n,                                  \
1489                            vl_api_##n##_t_handler,              \
1490                            vl_noop_handler,                     \
1491                            vl_api_##n##_t_endian,               \
1492                            vl_api_##n##_t_print,                \
1493                            sizeof(vl_api_##n##_t), 1);
1494     foreach_vpe_api_reply_msg;
1495 #undef _
1496
1497     /* API messages we can send */
1498 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
1499     foreach_vpe_api_msg;
1500 #undef _
1501
1502     /* Help strings */
1503 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1504     foreach_vpe_api_msg;
1505 #undef _
1506 }
1507
1508 clib_error_t * vat_plugin_register (vat_main_t *vam)
1509 {
1510   acl_test_main_t * sm = &acl_test_main;
1511   u8 * name;
1512
1513   sm->vat_main = vam;
1514
1515   name = format (0, "acl_%08x%c", api_version, 0);
1516   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1517
1518   if (sm->msg_id_base != (u16) ~0)
1519     acl_vat_api_hookup (vam);
1520
1521   vec_free(name);
1522
1523   return 0;
1524 }