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