acl-plugin: fix coverity 177970-177973,178891, and incorrect reset of dot1q/dot1ad...
[vpp.git] / src / plugins / acl / acl.c
1 /*
2  * Copyright (c) 2016 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 #include <stddef.h>
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <acl/acl.h>
21
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/input_acl.h>
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <acl/acl_all_api_h.h>
35 #undef vl_typedefs
36
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <acl/acl_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <acl/acl_all_api_h.h>
46 #undef vl_printfun
47
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
51 #undef vl_api_version
52
53 #include "fa_node.h"
54 #include "hash_lookup.h"
55
56 acl_main_t acl_main;
57
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
60
61 /* List of message types that this plugin understands */
62
63 #define foreach_acl_plugin_api_msg              \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace)                             \
67 _(ACL_DEL, acl_del)                             \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list)       \
70 _(ACL_DUMP, acl_dump)  \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump)
79
80
81 /* *INDENT-OFF* */
82 VLIB_PLUGIN_REGISTER () = {
83     .version = VPP_BUILD_VER,
84     .description = "Access Control Lists",
85 };
86 /* *INDENT-ON* */
87
88
89 static void *
90 acl_set_heap (acl_main_t * am)
91 {
92   if (0 == am->acl_mheap)
93     {
94       am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
95       mheap_t *h = mheap_header (am->acl_mheap);
96       h->flags |= MHEAP_FLAG_THREAD_SAFE;
97     }
98   void *oldheap = clib_mem_set_heap (am->acl_mheap);
99   return oldheap;
100 }
101
102 void
103 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
104 {
105   clib_mem_set_heap (acl_set_heap (am));
106   mheap_t *h = mheap_header (am->acl_mheap);
107   if (on)
108     {
109       h->flags |= MHEAP_FLAG_VALIDATE;
110       h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
111       mheap_validate (h);
112     }
113   else
114     {
115       h->flags &= ~MHEAP_FLAG_VALIDATE;
116       h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
117     }
118 }
119
120 void
121 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
122 {
123   clib_mem_set_heap (acl_set_heap (am));
124   mheap_t *h = mheap_header (am->acl_mheap);
125   if (on)
126     {
127       h->flags |= MHEAP_FLAG_TRACE;
128     }
129   else
130     {
131       h->flags &= ~MHEAP_FLAG_TRACE;
132     }
133 }
134
135 static void
136 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
137 {
138   acl_main_t *am = &acl_main;
139   vl_api_acl_plugin_get_version_reply_t *rmp;
140   int msg_size = sizeof (*rmp);
141   unix_shared_memory_queue_t *q;
142
143   q = vl_api_client_index_to_input_queue (mp->client_index);
144   if (q == 0)
145     {
146       return;
147     }
148
149   rmp = vl_msg_api_alloc (msg_size);
150   memset (rmp, 0, msg_size);
151   rmp->_vl_msg_id =
152     ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
153   rmp->context = mp->context;
154   rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
155   rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
156
157   vl_msg_api_send_shmem (q, (u8 *) & rmp);
158 }
159
160 static void
161 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
162                                           mp)
163 {
164   vl_api_acl_plugin_control_ping_reply_t *rmp;
165   acl_main_t *am = &acl_main;
166   int rv = 0;
167
168   /* *INDENT-OFF* */
169   REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
170   ({
171     rmp->vpe_pid = ntohl (getpid ());
172   }));
173   /* *INDENT-ON* */
174 }
175
176 static int
177 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
178               u32 * acl_list_index, u8 * tag)
179 {
180   acl_main_t *am = &acl_main;
181   acl_list_t *a;
182   acl_rule_t *r;
183   acl_rule_t *acl_new_rules = 0;
184   int i;
185
186   if (*acl_list_index != ~0)
187     {
188       /* They supplied some number, let's see if this ACL exists */
189       if (pool_is_free_index (am->acls, *acl_list_index))
190         {
191           /* tried to replace a non-existent ACL, no point doing anything */
192           clib_warning
193             ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
194              *acl_list_index, tag);
195           return VNET_API_ERROR_NO_SUCH_ENTRY;
196         }
197     }
198   if (0 == count)
199     {
200       clib_warning
201         ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
202          *acl_list_index, tag);
203     }
204
205   void *oldheap = acl_set_heap (am);
206
207   /* Create and populate the rules */
208   if (count > 0)
209     vec_validate (acl_new_rules, count - 1);
210
211   for (i = 0; i < count; i++)
212     {
213       r = vec_elt_at_index (acl_new_rules, i);
214       memset (r, 0, sizeof (*r));
215       r->is_permit = rules[i].is_permit;
216       r->is_ipv6 = rules[i].is_ipv6;
217       if (r->is_ipv6)
218         {
219           memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
220           memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
221         }
222       else
223         {
224           memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
225           memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
226         }
227       r->src_prefixlen = rules[i].src_ip_prefix_len;
228       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
229       r->proto = rules[i].proto;
230       r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
231       r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
232       r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
233       r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
234       r->tcp_flags_value = rules[i].tcp_flags_value;
235       r->tcp_flags_mask = rules[i].tcp_flags_mask;
236     }
237
238   if (~0 == *acl_list_index)
239     {
240       /* Get ACL index */
241       pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
242       memset (a, 0, sizeof (*a));
243       /* Will return the newly allocated ACL index */
244       *acl_list_index = a - am->acls;
245     }
246   else
247     {
248       a = am->acls + *acl_list_index;
249       hash_acl_delete (am, *acl_list_index);
250       /* Get rid of the old rules */
251       if (a->rules)
252         vec_free (a->rules);
253     }
254   a->rules = acl_new_rules;
255   a->count = count;
256   memcpy (a->tag, tag, sizeof (a->tag));
257   hash_acl_add (am, *acl_list_index);
258   clib_mem_set_heap (oldheap);
259   return 0;
260 }
261
262 static int
263 acl_del_list (u32 acl_list_index)
264 {
265   acl_main_t *am = &acl_main;
266   acl_list_t *a;
267   int i, ii;
268   if (pool_is_free_index (am->acls, acl_list_index))
269     {
270       return VNET_API_ERROR_NO_SUCH_ENTRY;
271     }
272
273   if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
274     {
275       if (vec_len (vec_elt (am->input_sw_if_index_vec_by_acl, acl_list_index))
276           > 0)
277         {
278           /* ACL is applied somewhere inbound. Refuse to delete */
279           return VNET_API_ERROR_ACL_IN_USE_INBOUND;
280         }
281     }
282   if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
283     {
284       if (vec_len
285           (vec_elt (am->output_sw_if_index_vec_by_acl, acl_list_index)) > 0)
286         {
287           /* ACL is applied somewhere outbound. Refuse to delete */
288           return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
289         }
290     }
291
292   void *oldheap = acl_set_heap (am);
293   /* delete any references to the ACL */
294   for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
295     {
296       for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
297            /* see body */ )
298         {
299           if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
300             {
301               vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
302             }
303           else
304             {
305               ii++;
306             }
307         }
308     }
309   for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
310     {
311       for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
312            /* see body */ )
313         {
314           if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
315             {
316               vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
317             }
318           else
319             {
320               ii++;
321             }
322         }
323     }
324   /* delete the hash table data */
325
326   hash_acl_delete (am, acl_list_index);
327   /* now we can delete the ACL itself */
328   a = pool_elt_at_index (am->acls, acl_list_index);
329   if (a->rules)
330     vec_free (a->rules);
331
332   pool_put (am->acls, a);
333   clib_mem_set_heap (oldheap);
334   return 0;
335 }
336
337 /* Some aids in ASCII graphing the content */
338 #define XX "\377"
339 #define __ "\000"
340 #define _(x)
341 #define v
342 /* *INDENT-OFF* */
343
344 u8 ip4_5tuple_mask[] =
345   _("             dmac               smac            etype ")
346   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
347   _("        v ihl totlen   ")
348   _(0x0000)
349   __ __ __ __
350   _("        ident fl+fo    ")
351   _(0x0004)
352   __ __ __ __
353   _("       ttl pr checksum ")
354   _(0x0008)
355   __ XX __ __
356   _("        src address    ")
357   _(0x000C)
358   XX XX XX XX
359   _("        dst address    ")
360   _(0x0010)
361   XX XX XX XX
362   _("L4 T/U  sport dport    ")
363   _(tcpudp)
364   XX XX XX XX
365   _(padpad)
366   __ __ __ __
367   _(padpad)
368   __ __ __ __
369   _(padeth)
370   __ __;
371
372  u8 ip6_5tuple_mask[] =
373   _("             dmac               smac            etype ")
374   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
375   _("        v  tc + flow ")
376   _(0x0000) __ __ __ __
377   _("        plen  nh hl  ")
378   _(0x0004) __ __ XX __
379   _("        src address  ")
380   _(0x0008) XX XX XX XX
381   _(0x000C) XX XX XX XX
382   _(0x0010) XX XX XX XX
383   _(0x0014) XX XX XX XX
384   _("        dst address  ")
385   _(0x0018) XX XX XX XX
386   _(0x001C) XX XX XX XX
387   _(0x0020) XX XX XX XX
388   _(0x0024) XX XX XX XX
389   _("L4T/U  sport dport   ")
390   _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
391
392  u8 dot1q_5tuple_mask[] =
393    _("             dmac               smac          dot1q         etype ")
394    _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
395    _(padpad) __ __ __ __
396    _(padpad) __ __ __ __
397    _(padpad) __ __ __ __
398    _(padeth) __ __;
399
400  u8 dot1ad_5tuple_mask[] =
401    _("             dmac               smac          dot1ad      dot1q         etype ")
402    _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
403    _(padpad) __ __ __ __
404    _(padpad) __ __ __ __
405    _(padeth) __ __;
406
407 /* *INDENT-ON* */
408 #undef XX
409 #undef __
410 #undef _
411 #undef v
412
413 static int
414 count_skip (u8 * p, u32 size)
415 {
416   u64 *p64 = (u64 *) p;
417   /* Be tolerant to null pointer */
418   if (0 == p)
419     return 0;
420
421   while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
422     {
423       p64++;
424     }
425   return (p64 - (u64 *) p) / 2;
426 }
427
428 static int
429 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
430                                  u32 mask_len, u32 next_table_index,
431                                  u32 miss_next_index, u32 * table_index,
432                                  int is_add)
433 {
434   u32 nbuckets = 1;
435   u32 memory_size = 2 << 13;
436   u32 skip = count_skip (mask, mask_len);
437   u32 match = (mask_len / 16) - skip;
438   u8 *skip_mask_ptr = mask + 16 * skip;
439   u32 current_data_flag = 0;
440   int current_data_offset = 0;
441
442   if (0 == match)
443     match = 1;
444   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
445   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
446                                          memory_size, skip, match,
447                                          next_table_index, miss_next_index,
448                                          table_index, current_data_flag,
449                                          current_data_offset, is_add,
450                                          1 /* delete_chain */ );
451   clib_mem_set_heap (oldheap);
452   return ret;
453 }
454
455 static int
456 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
457                                   u32 mask_len, u32 next_table_index,
458                                   u32 miss_next_index, u32 * table_index,
459                                   int is_add)
460 {
461   u32 nbuckets = 32;
462   u32 memory_size = 2 << 22;
463   u32 skip = count_skip (mask, mask_len);
464   u32 match = (mask_len / 16) - skip;
465   u8 *skip_mask_ptr = mask + 16 * skip;
466   u32 current_data_flag = 0;
467   int current_data_offset = 0;
468
469   if (0 == match)
470     match = 1;
471
472   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
473   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
474                                          memory_size, skip, match,
475                                          next_table_index, miss_next_index,
476                                          table_index, current_data_flag,
477                                          current_data_offset, is_add,
478                                          1 /* delete_chain */ );
479   clib_mem_set_heap (oldheap);
480   return ret;
481 }
482
483 static int
484 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
485 {
486   vnet_classify_main_t *cm = &vnet_classify_main;
487   u32 ip4_table_index = ~0;
488   u32 ip6_table_index = ~0;
489   u32 dot1q_table_index = ~0;
490   u32 dot1ad_table_index = ~0;
491   void *oldheap = acl_set_heap (am);
492
493   vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
494                            sw_if_index, ~0);
495   vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
496                            sw_if_index, ~0);
497   vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
498                            sw_if_index, ~0);
499   vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
500                            sw_if_index, ~0);
501
502   /* switch to global heap while calling vnet_* functions */
503   clib_mem_set_heap (cm->vlib_main->heap_base);
504   vnet_l2_input_classify_enable_disable (sw_if_index, 0);
505
506   if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
507     {
508       ip4_table_index =
509         am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
510       am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
511       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
512                                        sizeof (ip4_5tuple_mask) - 1, ~0,
513                                        am->l2_input_classify_next_acl_ip4,
514                                        &ip4_table_index, 0);
515     }
516   if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
517     {
518       ip6_table_index =
519         am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
520       am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
521       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
522                                        sizeof (ip6_5tuple_mask) - 1, ~0,
523                                        am->l2_input_classify_next_acl_ip6,
524                                        &ip6_table_index, 0);
525     }
526   if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
527     {
528       dot1q_table_index =
529         am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
530       am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
531       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
532                                        sizeof (ip6_5tuple_mask) - 1, ~0,
533                                        ~0, &dot1q_table_index, 0);
534     }
535   if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
536     {
537       dot1ad_table_index =
538         am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
539       am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
540       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
541                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
542                                        ~0, &dot1ad_table_index, 0);
543     }
544   clib_mem_set_heap (oldheap);
545   return 0;
546 }
547
548 static int
549 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
550 {
551   vnet_classify_main_t *cm = &vnet_classify_main;
552   u32 ip4_table_index = ~0;
553   u32 ip6_table_index = ~0;
554   u32 dot1q_table_index = ~0;
555   u32 dot1ad_table_index = ~0;
556   void *oldheap = acl_set_heap (am);
557
558   vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
559                            sw_if_index, ~0);
560   vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
561                            sw_if_index, ~0);
562   vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
563                            sw_if_index, ~0);
564   vec_validate_init_empty
565     (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
566
567   /* switch to global heap while calling vnet_* functions */
568   clib_mem_set_heap (cm->vlib_main->heap_base);
569
570   vnet_l2_output_classify_enable_disable (sw_if_index, 0);
571
572   if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
573     {
574       ip4_table_index =
575         am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
576       am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
577       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
578                                        sizeof (ip4_5tuple_mask) - 1, ~0,
579                                        am->l2_output_classify_next_acl_ip4,
580                                        &ip4_table_index, 0);
581     }
582   if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
583     {
584       ip6_table_index =
585         am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
586       am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
587       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
588                                        sizeof (ip6_5tuple_mask) - 1, ~0,
589                                        am->l2_output_classify_next_acl_ip6,
590                                        &ip6_table_index, 0);
591     }
592   if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
593     {
594       dot1q_table_index =
595         am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
596       am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
597       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
598                                        sizeof (ip6_5tuple_mask) - 1, ~0,
599                                        ~0, &dot1q_table_index, 0);
600     }
601   if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
602     {
603       dot1ad_table_index =
604         am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
605       am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
606       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
607                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
608                                        ~0, &dot1ad_table_index, 0);
609     }
610   clib_mem_set_heap (oldheap);
611   return 0;
612 }
613
614 static void
615 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
616                       u8 is_dot1ad, u8 is_ip6)
617 {
618   vnet_classify_main_t *cm = &vnet_classify_main;
619   u8 *match;
620   u32 next_acl;
621   u8 idx;
622   u8 session_idx;
623
624   if (is_ip6)
625     {
626       next_acl =
627         (is_output) ? am->
628         l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
629     }
630   else
631     {
632       next_acl =
633         (is_output) ? am->
634         l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
635     }
636   match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
637   idx = (is_dot1ad) ? 20 : 16;
638   if (is_dot1ad)
639     {
640       /* 802.1ad ethertype */
641       match[12] = 0x88;
642       match[13] = 0xa8;
643       /* 802.1q ethertype */
644       match[16] = 0x81;
645       match[17] = 0x00;
646     }
647   else
648     {
649       /* 802.1q ethertype */
650       match[12] = 0x81;
651       match[13] = 0x00;
652     }
653
654   /* add sessions to vlan tables per ethernet_type */
655   if (is_ip6)
656     {
657       match[idx] = 0x86;
658       match[idx + 1] = 0xdd;
659       session_idx = 1;
660     }
661   else
662     {
663       match[idx] = 0x08;
664       match[idx + 1] = 0x00;
665       session_idx = 0;
666     }
667   vnet_classify_add_del_session (cm, table_index, match, next_acl,
668                                  session_idx, 0, 0, 0, 1);
669   /* reset the mask back to being a mask */
670   match[idx] = 0xff;
671   match[idx + 1] = 0xff;
672   match[12] = 0xff;
673   match[13] = 0xff;
674   if (is_dot1ad)
675     {
676       match[16] = 0xff;
677       match[17] = 0xff;
678     }
679 }
680
681 static int
682 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
683 {
684   vnet_classify_main_t *cm = &vnet_classify_main;
685   u32 ip4_table_index = ~0;
686   u32 ip6_table_index = ~0;
687   u32 dot1q_table_index = ~0;
688   u32 dot1ad_table_index = ~0;
689   int rv;
690
691   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
692
693   /* in case there were previous tables attached */
694   acl_unhook_l2_input_classify (am, sw_if_index);
695   rv =
696     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
697                                      sizeof (ip4_5tuple_mask) - 1, ~0,
698                                      am->l2_input_classify_next_acl_ip4,
699                                      &ip4_table_index, 1);
700   if (rv)
701     goto done;
702
703   rv =
704     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
705                                      sizeof (ip6_5tuple_mask) - 1, ~0,
706                                      am->l2_input_classify_next_acl_ip6,
707                                      &ip6_table_index, 1);
708   if (rv)
709     {
710       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
711                                        sizeof (ip4_5tuple_mask) - 1, ~0,
712                                        am->l2_input_classify_next_acl_ip4,
713                                        &ip4_table_index, 0);
714       goto done;
715     }
716
717   rv =
718     acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
719                                      sizeof (dot1ad_5tuple_mask) - 1, ~0,
720                                      ~0, &dot1ad_table_index, 1);
721   rv =
722     acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
723                                      sizeof (dot1q_5tuple_mask) - 1,
724                                      dot1ad_table_index, ~0,
725                                      &dot1q_table_index, 1);
726   if (rv)
727     {
728       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
729                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
730                                        ~0, &dot1ad_table_index, 0);
731       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
732                                        sizeof (ip6_5tuple_mask) - 1, ~0,
733                                        am->l2_input_classify_next_acl_ip6,
734                                        &ip6_table_index, 0);
735       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
736                                        sizeof (ip4_5tuple_mask) - 1, ~0,
737                                        am->l2_input_classify_next_acl_ip4,
738                                        &ip4_table_index, 0);
739       goto done;
740     }
741
742   rv =
743     vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
744                                        ip6_table_index, dot1q_table_index);
745
746   if (rv)
747     {
748       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
749                                        sizeof (ip4_5tuple_mask) - 1, ~0,
750                                        am->l2_input_classify_next_acl_ip4,
751                                        &ip4_table_index, 0);
752       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
753                                        sizeof (ip6_5tuple_mask) - 1, ~0,
754                                        am->l2_input_classify_next_acl_ip6,
755                                        &ip6_table_index, 0);
756       acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
757                                        sizeof (dot1q_5tuple_mask) - 1, ~0,
758                                        ~0, &dot1q_table_index, 0);
759       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
760                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
761                                        ~0, &dot1ad_table_index, 0);
762       goto done;
763     }
764
765   /* add sessions to vlan tables per ethernet_type */
766   acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
767   acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
768   acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
769   acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
770
771   am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
772     ip4_table_index;
773   am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
774     ip6_table_index;
775   am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
776     dot1q_table_index;
777   am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
778     dot1ad_table_index;
779
780   vnet_l2_input_classify_enable_disable (sw_if_index, 1);
781 done:
782   clib_mem_set_heap (prevheap);
783   return rv;
784 }
785
786 static int
787 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
788 {
789   vnet_classify_main_t *cm = &vnet_classify_main;
790   u32 ip4_table_index = ~0;
791   u32 ip6_table_index = ~0;
792   u32 dot1q_table_index = ~0;
793   u32 dot1ad_table_index = ~0;
794   int rv;
795
796   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
797
798   /* in case there were previous tables attached */
799   acl_unhook_l2_output_classify (am, sw_if_index);
800   rv =
801     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
802                                      sizeof (ip4_5tuple_mask) - 1, ~0,
803                                      am->l2_output_classify_next_acl_ip4,
804                                      &ip4_table_index, 1);
805   if (rv)
806     goto done;
807   rv =
808     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
809                                      sizeof (ip6_5tuple_mask) - 1, ~0,
810                                      am->l2_output_classify_next_acl_ip6,
811                                      &ip6_table_index, 1);
812   if (rv)
813     {
814       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
815                                        sizeof (ip4_5tuple_mask) - 1, ~0,
816                                        am->l2_output_classify_next_acl_ip4,
817                                        &ip4_table_index, 0);
818       goto done;
819     }
820
821   rv =
822     acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
823                                      sizeof (dot1ad_5tuple_mask) - 1, ~0,
824                                      ~0, &dot1ad_table_index, 1);
825   rv =
826     acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
827                                      sizeof (dot1q_5tuple_mask) - 1,
828                                      dot1ad_table_index, ~0,
829                                      &dot1q_table_index, 1);
830   if (rv)
831     {
832       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
833                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
834                                        ~0, &dot1ad_table_index, 0);
835       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
836                                        sizeof (ip6_5tuple_mask) - 1, ~0,
837                                        am->l2_output_classify_next_acl_ip6,
838                                        &ip6_table_index, 0);
839       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
840                                        sizeof (ip4_5tuple_mask) - 1, ~0,
841                                        am->l2_output_classify_next_acl_ip4,
842                                        &ip4_table_index, 0);
843       goto done;
844     }
845
846   rv =
847     vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
848                                         ip6_table_index, dot1q_table_index);
849
850   clib_warning
851     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
852      sw_if_index, ip4_table_index, ip6_table_index);
853   if (rv)
854     {
855       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
856                                        sizeof (ip6_5tuple_mask) - 1, ~0,
857                                        am->l2_output_classify_next_acl_ip6,
858                                        &ip6_table_index, 0);
859       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
860                                        sizeof (ip4_5tuple_mask) - 1, ~0,
861                                        am->l2_output_classify_next_acl_ip4,
862                                        &ip4_table_index, 0);
863       acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
864                                        sizeof (dot1q_5tuple_mask) - 1, ~0,
865                                        ~0, &dot1q_table_index, 0);
866       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
867                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
868                                        ~0, &dot1ad_table_index, 0);
869       goto done;
870     }
871
872   /* add sessions to vlan tables per ethernet_type */
873   acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
874   acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
875   acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
876   acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
877
878   am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
879     ip4_table_index;
880   am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
881     ip6_table_index;
882   am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
883     dot1q_table_index;
884   am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
885     dot1ad_table_index;
886
887   vnet_l2_output_classify_enable_disable (sw_if_index, 1);
888 done:
889   clib_mem_set_heap (prevheap);
890   return rv;
891 }
892
893 int
894 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
895                                  int enable_disable)
896 {
897   int rv;
898
899   /* Utterly wrong? */
900   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
901                           sw_if_index))
902     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
903
904   acl_fa_enable_disable (sw_if_index, 1, enable_disable);
905
906   if (enable_disable)
907     {
908       rv = acl_hook_l2_input_classify (am, sw_if_index);
909     }
910   else
911     {
912       rv = acl_unhook_l2_input_classify (am, sw_if_index);
913     }
914
915   return rv;
916 }
917
918 int
919 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
920                                   int enable_disable)
921 {
922   int rv;
923
924   /* Utterly wrong? */
925   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
926                           sw_if_index))
927     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
928
929   acl_fa_enable_disable (sw_if_index, 0, enable_disable);
930
931   if (enable_disable)
932     {
933       rv = acl_hook_l2_output_classify (am, sw_if_index);
934     }
935   else
936     {
937       rv = acl_unhook_l2_output_classify (am, sw_if_index);
938     }
939
940   return rv;
941 }
942
943 static int
944 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
945 {
946   return (pool_is_free_index (am->acls, acl_list_index));
947 }
948
949
950 static int
951 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
952 {
953   acl_main_t *am = &acl_main;
954   if (acl_is_not_defined (am, acl_list_index))
955     {
956       /* ACL is not defined. Can not apply */
957       return VNET_API_ERROR_NO_SUCH_ENTRY;
958     }
959   void *oldheap = acl_set_heap (am);
960
961   if (is_input)
962     {
963       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
964
965       u32 index = vec_search (am->input_acl_vec_by_sw_if_index[sw_if_index],
966                               acl_list_index);
967       if (index < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
968         {
969           clib_warning
970             ("ACL %d is already applied inbound on sw_if_index %d (index %d)",
971              acl_list_index, sw_if_index, index);
972           /* the entry is already there */
973           clib_mem_set_heap (oldheap);
974           return VNET_API_ERROR_ACL_IN_USE_INBOUND;
975         }
976       /* if there was no ACL applied before, enable the ACL processing */
977       if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0)
978         {
979           acl_interface_in_enable_disable (am, sw_if_index, 1);
980         }
981       vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
982                1);
983       vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
984       vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
985                1);
986     }
987   else
988     {
989       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
990
991       u32 index = vec_search (am->output_acl_vec_by_sw_if_index[sw_if_index],
992                               acl_list_index);
993       if (index < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
994         {
995           clib_warning
996             ("ACL %d is already applied outbound on sw_if_index %d (index %d)",
997              acl_list_index, sw_if_index, index);
998           /* the entry is already there */
999           clib_mem_set_heap (oldheap);
1000           return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1001         }
1002       /* if there was no ACL applied before, enable the ACL processing */
1003       if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0)
1004         {
1005           acl_interface_out_enable_disable (am, sw_if_index, 1);
1006         }
1007       vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
1008                &acl_list_index, 1);
1009       vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
1010       vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index],
1011                &sw_if_index, 1);
1012     }
1013   clib_mem_set_heap (oldheap);
1014   return 0;
1015 }
1016
1017
1018 static int
1019 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
1020 {
1021   acl_main_t *am = &acl_main;
1022   int i;
1023   int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1024   void *oldheap = acl_set_heap (am);
1025   if (is_input)
1026     {
1027       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1028       for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1029            i++)
1030         {
1031           if (acl_list_index ==
1032               am->input_acl_vec_by_sw_if_index[sw_if_index][i])
1033             {
1034               vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
1035               rv = 0;
1036               break;
1037             }
1038         }
1039
1040       if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1041         {
1042           u32 index =
1043             vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1044                         sw_if_index);
1045           if (index <
1046               vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1047             {
1048               hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1049               vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1050                         index);
1051             }
1052         }
1053
1054       /* If there is no more ACLs applied on an interface, disable ACL processing */
1055       if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
1056         {
1057           acl_interface_in_enable_disable (am, sw_if_index, 0);
1058         }
1059     }
1060   else
1061     {
1062       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1063       for (i = 0;
1064            i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
1065         {
1066           if (acl_list_index ==
1067               am->output_acl_vec_by_sw_if_index[sw_if_index][i])
1068             {
1069               vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
1070               rv = 0;
1071               break;
1072             }
1073         }
1074
1075       if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1076         {
1077           u32 index =
1078             vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1079                         sw_if_index);
1080           if (index <
1081               vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1082             {
1083               hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1084               vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1085                         index);
1086             }
1087         }
1088
1089       /* If there is no more ACLs applied on an interface, disable ACL processing */
1090       if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
1091         {
1092           acl_interface_out_enable_disable (am, sw_if_index, 0);
1093         }
1094     }
1095   clib_mem_set_heap (oldheap);
1096   return rv;
1097 }
1098
1099 static void
1100 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
1101 {
1102   acl_main_t *am = &acl_main;
1103   int i;
1104   void *oldheap = acl_set_heap (am);
1105   if (is_input)
1106     {
1107       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1108       if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1109         {
1110           acl_interface_in_enable_disable (am, sw_if_index, 0);
1111         }
1112
1113       for (i = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1114            i >= 0; i--)
1115         {
1116           u32 acl_list_index =
1117             am->input_acl_vec_by_sw_if_index[sw_if_index][i];
1118           hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1119           if (acl_list_index < vec_len (am->input_sw_if_index_vec_by_acl))
1120             {
1121               u32 index =
1122                 vec_search (am->input_sw_if_index_vec_by_acl[acl_list_index],
1123                             sw_if_index);
1124               if (index <
1125                   vec_len (am->input_sw_if_index_vec_by_acl[acl_list_index]))
1126                 {
1127                   vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index],
1128                             index);
1129                 }
1130             }
1131         }
1132
1133       vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1134     }
1135   else
1136     {
1137       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1138       if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1139         {
1140           acl_interface_out_enable_disable (am, sw_if_index, 0);
1141         }
1142
1143       for (i = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) - 1;
1144            i >= 0; i--)
1145         {
1146           u32 acl_list_index =
1147             am->output_acl_vec_by_sw_if_index[sw_if_index][i];
1148           hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1149           if (acl_list_index < vec_len (am->output_sw_if_index_vec_by_acl))
1150             {
1151               u32 index =
1152                 vec_search (am->output_sw_if_index_vec_by_acl[acl_list_index],
1153                             sw_if_index);
1154               if (index <
1155                   vec_len (am->output_sw_if_index_vec_by_acl[acl_list_index]))
1156                 {
1157                   vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index],
1158                             index);
1159                 }
1160             }
1161         }
1162
1163       vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1164     }
1165   clib_mem_set_heap (oldheap);
1166 }
1167
1168 static int
1169 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1170                                  u32 acl_list_index)
1171 {
1172   int rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1173   acl_main_t *am = &acl_main;
1174   if (is_add)
1175     {
1176       rv =
1177         acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
1178       if (rv == 0)
1179         {
1180           hash_acl_apply (am, sw_if_index, is_input, acl_list_index);
1181         }
1182     }
1183   else
1184     {
1185       hash_acl_unapply (am, sw_if_index, is_input, acl_list_index);
1186       rv =
1187         acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
1188     }
1189   return rv;
1190 }
1191
1192
1193 typedef struct
1194 {
1195   u8 is_ipv6;
1196   u8 mac_mask[6];
1197   u8 prefix_len;
1198   u32 count;
1199   u32 table_index;
1200   u32 arp_table_index;
1201   u32 dot1q_table_index;
1202   u32 dot1ad_table_index;
1203 } macip_match_type_t;
1204
1205 static u32
1206 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1207                        u8 is_ipv6)
1208 {
1209   u32 i;
1210   if (mv)
1211     {
1212       for (i = 0; i < vec_len (mv); i++)
1213         {
1214           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1215               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1216             {
1217               return i;
1218             }
1219         }
1220     }
1221   return ~0;
1222 }
1223
1224
1225 /* Get metric used to sort match types.
1226    The more specific and the more often seen - the bigger the metric */
1227 static int
1228 match_type_metric (macip_match_type_t * m)
1229 {
1230   unsigned int mac_bits_set = 0;
1231   unsigned int mac_byte;
1232   int i;
1233   for (i = 0; i < 6; i++)
1234     {
1235       mac_byte = m->mac_mask[i];
1236       for (; mac_byte; mac_byte >>= 1)
1237         mac_bits_set += mac_byte & 1;
1238     }
1239   /*
1240    * Attempt to place the more specific and the more used rules on top.
1241    * There are obvious caveat corner cases to this, but they do not
1242    * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1243    * going with a wildcard IPv4 with a specific MAC).
1244    */
1245   return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1246 }
1247
1248 static int
1249 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1250 {
1251   /* Ascending sort based on the metric values */
1252   return match_type_metric (m1) - match_type_metric (m2);
1253 }
1254
1255 /* Get the offset of L3 source within ethernet packet */
1256 static int
1257 get_l3_src_offset (int is6)
1258 {
1259   if (is6)
1260     return (sizeof (ethernet_header_t) +
1261             offsetof (ip6_header_t, src_address));
1262   else
1263     return (sizeof (ethernet_header_t) +
1264             offsetof (ip4_header_t, src_address));
1265 }
1266
1267 static int
1268 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1269 {
1270   macip_match_type_t *mvec = NULL;
1271   macip_match_type_t *mt;
1272   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1273   int i;
1274   u32 match_type_index;
1275   u32 last_table;
1276   u8 mask[5 * 16];
1277   vnet_classify_main_t *cm = &vnet_classify_main;
1278
1279   /* Count the number of different types of rules */
1280   for (i = 0; i < a->count; i++)
1281     {
1282       if (~0 ==
1283           (match_type_index =
1284            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1285                                   a->rules[i].src_prefixlen,
1286                                   a->rules[i].is_ipv6)))
1287         {
1288           match_type_index = vec_len (mvec);
1289           vec_validate (mvec, match_type_index);
1290           memcpy (mvec[match_type_index].mac_mask,
1291                   a->rules[i].src_mac_mask, 6);
1292           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1293           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1294           mvec[match_type_index].table_index = ~0;
1295           mvec[match_type_index].dot1q_table_index = ~0;
1296           mvec[match_type_index].dot1ad_table_index = ~0;
1297         }
1298       mvec[match_type_index].count++;
1299     }
1300   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1301   vec_sort_with_function (mvec, match_type_compare);
1302   /* Create the classifier tables */
1303   last_table = ~0;
1304   /* First add ARP tables */
1305   vec_foreach (mt, mvec)
1306   {
1307     int mask_len;
1308     int is6 = mt->is_ipv6;
1309
1310     mt->arp_table_index = ~0;
1311     if (!is6)
1312       {
1313         memset (mask, 0, sizeof (mask));
1314         memcpy (&mask[6], mt->mac_mask, 6);
1315         memset (&mask[12], 0xff, 2);    /* ethernet protocol */
1316         memcpy (&mask[14 + 8], mt->mac_mask, 6);
1317
1318         for (i = 0; i < (mt->prefix_len / 8); i++)
1319           mask[14 + 14 + i] = 0xff;
1320         if (mt->prefix_len % 8)
1321           mask[14 + 14 + (mt->prefix_len / 8)] =
1322             0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1323
1324         mask_len = ((14 + 14 + ((mt->prefix_len + 7) / 8) +
1325                      (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1326         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1327                                           (~0 == last_table) ? 0 : ~0,
1328                                           &mt->arp_table_index, 1);
1329         last_table = mt->arp_table_index;
1330       }
1331   }
1332   /* Now add IP[46] tables */
1333   vec_foreach (mt, mvec)
1334   {
1335     int mask_len;
1336     int is6 = mt->is_ipv6;
1337     int l3_src_offs = get_l3_src_offset (is6);
1338     int tags;
1339     u32 *last_tag_table;
1340
1341     /*
1342      * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1343      */
1344     l3_src_offs += 8;
1345     for (tags = 2; tags >= 0; tags--)
1346       {
1347         memset (mask, 0, sizeof (mask));
1348         memcpy (&mask[6], mt->mac_mask, 6);
1349         switch (tags)
1350           {
1351           case 0:
1352           default:
1353             memset (&mask[12], 0xff, 2);        /* ethernet protocol */
1354             last_tag_table = &mt->table_index;
1355             break;
1356           case 1:
1357             memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1358             memset (&mask[16], 0xff, 2);        /* ethernet protocol */
1359             last_tag_table = &mt->dot1q_table_index;
1360             break;
1361           case 2:
1362             memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1363             memset (&mask[16], 0xff, 2);        /* VLAN tag2 */
1364             memset (&mask[20], 0xff, 2);        /* ethernet protocol */
1365             last_tag_table = &mt->dot1ad_table_index;
1366             break;
1367           }
1368         for (i = 0; i < (mt->prefix_len / 8); i++)
1369           {
1370             mask[l3_src_offs + i] = 0xff;
1371           }
1372         if (mt->prefix_len % 8)
1373           {
1374             mask[l3_src_offs + (mt->prefix_len / 8)] =
1375               0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1376           }
1377         /*
1378          * Round-up the number of bytes needed to store the prefix,
1379          * and round up the number of vectors too
1380          */
1381         mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1382                      (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1383         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1384                                           (~0 == last_table) ? 0 : ~0,
1385                                           last_tag_table, 1);
1386         last_table = *last_tag_table;
1387
1388         memset (&mask[12], 0, sizeof (mask) - 12);
1389         l3_src_offs -= 4;
1390       }
1391   }
1392   a->ip4_table_index = last_table;
1393   a->ip6_table_index = last_table;
1394   a->l2_table_index = last_table;
1395
1396   /* Populate the classifier tables with rules from the MACIP ACL */
1397   for (i = 0; i < a->count; i++)
1398     {
1399       u32 action = 0;
1400       u32 metadata = 0;
1401       int is6 = a->rules[i].is_ipv6;
1402       int l3_src_offs = get_l3_src_offset (is6);
1403       u32 tag_table;
1404       int tags, eth;
1405
1406       match_type_index =
1407         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1408                                a->rules[i].src_prefixlen,
1409                                a->rules[i].is_ipv6);
1410       ASSERT (match_type_index != ~0);
1411
1412       l3_src_offs += 8;
1413       for (tags = 2; tags >= 0; tags--)
1414         {
1415           memset (mask, 0, sizeof (mask));
1416           memcpy (&mask[6], a->rules[i].src_mac, 6);
1417           switch (tags)
1418             {
1419             case 0:
1420             default:
1421               tag_table = mvec[match_type_index].table_index;
1422               eth = 12;
1423               break;
1424             case 1:
1425               tag_table = mvec[match_type_index].dot1q_table_index;
1426               mask[12] = 0x81;
1427               mask[13] = 0x00;
1428               eth = 16;
1429               break;
1430             case 2:
1431               tag_table = mvec[match_type_index].dot1ad_table_index;
1432               mask[12] = 0x88;
1433               mask[13] = 0xa8;
1434               mask[16] = 0x81;
1435               mask[17] = 0x00;
1436               eth = 20;
1437               break;
1438             }
1439           if (is6)
1440             {
1441               memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1442               mask[eth] = 0x86;
1443               mask[eth + 1] = 0xdd;
1444             }
1445           else
1446             {
1447               memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1448               mask[eth] = 0x08;
1449               mask[eth + 1] = 0x00;
1450             }
1451
1452           /* add session to table mvec[match_type_index].table_index; */
1453           vnet_classify_add_del_session (cm, tag_table,
1454                                          mask, a->rules[i].is_permit ? ~0 : 0,
1455                                          i, 0, action, metadata, 1);
1456           memset (&mask[12], 0, sizeof (mask) - 12);
1457           l3_src_offs -= 4;
1458         }
1459
1460       /* add ARP table entry too */
1461       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1462         {
1463           memset (mask, 0, sizeof (mask));
1464           memcpy (&mask[6], a->rules[i].src_mac, 6);
1465           mask[12] = 0x08;
1466           mask[13] = 0x06;
1467           memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1468           memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1469           vnet_classify_add_del_session (cm,
1470                                          mvec
1471                                          [match_type_index].arp_table_index,
1472                                          mask, a->rules[i].is_permit ? ~0 : 0,
1473                                          i, 0, action, metadata, 1);
1474         }
1475     }
1476   return 0;
1477 }
1478
1479 static void
1480 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1481 {
1482   vnet_classify_main_t *cm = &vnet_classify_main;
1483   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1484
1485   if (a->ip4_table_index != ~0)
1486     {
1487       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1488                                         &a->ip4_table_index, 0);
1489       a->ip4_table_index = ~0;
1490     }
1491   if (a->ip6_table_index != ~0)
1492     {
1493       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1494                                         &a->ip6_table_index, 0);
1495       a->ip6_table_index = ~0;
1496     }
1497   if (a->l2_table_index != ~0)
1498     {
1499       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1500                                         0);
1501       a->l2_table_index = ~0;
1502     }
1503 }
1504
1505 static int
1506 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1507                     u32 * acl_list_index, u8 * tag)
1508 {
1509   acl_main_t *am = &acl_main;
1510   macip_acl_list_t *a;
1511   macip_acl_rule_t *r;
1512   macip_acl_rule_t *acl_new_rules = 0;
1513   int i;
1514
1515   if (*acl_list_index != ~0)
1516     {
1517       /* They supplied some number, let's see if this MACIP ACL exists */
1518       if (pool_is_free_index (am->macip_acls, *acl_list_index))
1519         {
1520           /* tried to replace a non-existent ACL, no point doing anything */
1521           clib_warning
1522             ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1523              *acl_list_index, tag);
1524           return VNET_API_ERROR_NO_SUCH_ENTRY;
1525         }
1526     }
1527
1528   if (0 == count)
1529     {
1530       clib_warning
1531         ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1532          tag);
1533     }
1534   void *oldheap = acl_set_heap (am);
1535   /* Create and populate the rules */
1536   if (count > 0)
1537     vec_validate (acl_new_rules, count - 1);
1538
1539   for (i = 0; i < count; i++)
1540     {
1541       r = &acl_new_rules[i];
1542       r->is_permit = rules[i].is_permit;
1543       r->is_ipv6 = rules[i].is_ipv6;
1544       memcpy (&r->src_mac, rules[i].src_mac, 6);
1545       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1546       if (rules[i].is_ipv6)
1547         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1548       else
1549         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1550       r->src_prefixlen = rules[i].src_ip_prefix_len;
1551     }
1552
1553   if (~0 == *acl_list_index)
1554     {
1555       /* Get ACL index */
1556       pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1557       memset (a, 0, sizeof (*a));
1558       /* Will return the newly allocated ACL index */
1559       *acl_list_index = a - am->macip_acls;
1560     }
1561   else
1562     {
1563       a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1564       if (a->rules)
1565         {
1566           vec_free (a->rules);
1567         }
1568       macip_destroy_classify_tables (am, *acl_list_index);
1569     }
1570
1571   a->rules = acl_new_rules;
1572   a->count = count;
1573   memcpy (a->tag, tag, sizeof (a->tag));
1574
1575   /* Create and populate the classifer tables */
1576   macip_create_classify_tables (am, *acl_list_index);
1577   clib_mem_set_heap (oldheap);
1578   return 0;
1579 }
1580
1581
1582 /* No check for validity of sw_if_index - the callers were supposed to validate */
1583
1584 static int
1585 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1586 {
1587   int rv;
1588   u32 macip_acl_index;
1589   macip_acl_list_t *a;
1590   void *oldheap = acl_set_heap (am);
1591   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1592   clib_mem_set_heap (oldheap);
1593   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1594   /* No point in deleting MACIP ACL which is not applied */
1595   if (~0 == macip_acl_index)
1596     return VNET_API_ERROR_NO_SUCH_ENTRY;
1597   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1598   /* remove the classifier tables off the interface L2 ACL */
1599   rv =
1600     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1601                               a->ip6_table_index, a->l2_table_index, 0);
1602   /* Unset the MACIP ACL index */
1603   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1604   return rv;
1605 }
1606
1607 /* No check for validity of sw_if_index - the callers were supposed to validate */
1608
1609 static int
1610 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1611                              u32 macip_acl_index)
1612 {
1613   macip_acl_list_t *a;
1614   int rv;
1615   if (pool_is_free_index (am->macip_acls, macip_acl_index))
1616     {
1617       return VNET_API_ERROR_NO_SUCH_ENTRY;
1618     }
1619   void *oldheap = acl_set_heap (am);
1620   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1621   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1622   clib_mem_set_heap (oldheap);
1623   /* If there already a MACIP ACL applied, unapply it */
1624   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1625     macip_acl_interface_del_acl (am, sw_if_index);
1626   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1627
1628   /* Apply the classifier tables for L2 ACLs */
1629   rv =
1630     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1631                               a->ip6_table_index, a->l2_table_index, 1);
1632   return rv;
1633 }
1634
1635 static int
1636 macip_acl_del_list (u32 acl_list_index)
1637 {
1638   acl_main_t *am = &acl_main;
1639   macip_acl_list_t *a;
1640   int i;
1641   if (pool_is_free_index (am->macip_acls, acl_list_index))
1642     {
1643       return VNET_API_ERROR_NO_SUCH_ENTRY;
1644     }
1645
1646   /* delete any references to the ACL */
1647   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1648     {
1649       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1650         {
1651           macip_acl_interface_del_acl (am, i);
1652         }
1653     }
1654
1655   void *oldheap = acl_set_heap (am);
1656   /* Now that classifier tables are detached, clean them up */
1657   macip_destroy_classify_tables (am, acl_list_index);
1658
1659   /* now we can delete the ACL itself */
1660   a = pool_elt_at_index (am->macip_acls, acl_list_index);
1661   if (a->rules)
1662     {
1663       vec_free (a->rules);
1664     }
1665   pool_put (am->macip_acls, a);
1666   clib_mem_set_heap (oldheap);
1667   return 0;
1668 }
1669
1670
1671 static int
1672 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1673                                  u32 acl_list_index)
1674 {
1675   acl_main_t *am = &acl_main;
1676   int rv = -1;
1677   if (is_add)
1678     {
1679       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1680     }
1681   else
1682     {
1683       rv = macip_acl_interface_del_acl (am, sw_if_index);
1684     }
1685   return rv;
1686 }
1687
1688 /*
1689  * If the client does not allocate enough memory for a variable-length
1690  * message, and then proceed to use it as if the full memory allocated,
1691  * absent the check we happily consume that on the VPP side, and go
1692  * along as if nothing happened. However, the resulting
1693  * effects range from just garbage in the API decode
1694  * (because the decoder snoops too far), to potential memory
1695  * corruptions.
1696  *
1697  * This verifies that the actual length of the message is
1698  * at least expected_len, and complains loudly if it is not.
1699  *
1700  * A failing check here is 100% a software bug on the API user side,
1701  * so we might as well yell.
1702  *
1703  */
1704 static int
1705 verify_message_len (void *mp, u32 expected_len, char *where)
1706 {
1707   u32 supplied_len = vl_msg_api_get_msg_length (mp);
1708   if (supplied_len < expected_len)
1709     {
1710       clib_warning ("%s: Supplied message length %d is less than expected %d",
1711                     where, supplied_len, expected_len);
1712       return 0;
1713     }
1714   else
1715     {
1716       return 1;
1717     }
1718 }
1719
1720 /* API message handler */
1721 static void
1722 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1723 {
1724   vl_api_acl_add_replace_reply_t *rmp;
1725   acl_main_t *am = &acl_main;
1726   int rv;
1727   u32 acl_list_index = ntohl (mp->acl_index);
1728   u32 acl_count = ntohl (mp->count);
1729   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1730
1731   if (verify_message_len (mp, expected_len, "acl_add_replace"))
1732     {
1733       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1734     }
1735   else
1736     {
1737       rv = VNET_API_ERROR_INVALID_VALUE;
1738     }
1739
1740   /* *INDENT-OFF* */
1741   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1742   ({
1743     rmp->acl_index = htonl(acl_list_index);
1744   }));
1745   /* *INDENT-ON* */
1746 }
1747
1748 static void
1749 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1750 {
1751   acl_main_t *am = &acl_main;
1752   vl_api_acl_del_reply_t *rmp;
1753   int rv;
1754
1755   rv = acl_del_list (ntohl (mp->acl_index));
1756
1757   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1758 }
1759
1760 static void
1761 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1762 {
1763   acl_main_t *am = &acl_main;
1764   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1765   u32 sw_if_index = ntohl (mp->sw_if_index);
1766   vl_api_acl_interface_add_del_reply_t *rmp;
1767   int rv = -1;
1768
1769   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1770     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1771   else
1772     rv =
1773       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1774                                        mp->is_input, ntohl (mp->acl_index));
1775
1776   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1777 }
1778
1779 static void
1780   vl_api_acl_interface_set_acl_list_t_handler
1781   (vl_api_acl_interface_set_acl_list_t * mp)
1782 {
1783   acl_main_t *am = &acl_main;
1784   vl_api_acl_interface_set_acl_list_reply_t *rmp;
1785   int rv = 0;
1786   int i;
1787   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1788   u32 sw_if_index = ntohl (mp->sw_if_index);
1789
1790   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1791     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1792   else
1793     {
1794       acl_interface_reset_inout_acls (sw_if_index, 0);
1795       acl_interface_reset_inout_acls (sw_if_index, 1);
1796
1797       for (i = 0; i < mp->count; i++)
1798         {
1799           if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1800             {
1801               /* ACL does not exist, so we can not apply it */
1802               rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1803             }
1804         }
1805       if (0 == rv)
1806         {
1807           for (i = 0; i < mp->count; i++)
1808             {
1809               acl_interface_add_del_inout_acl (sw_if_index, 1,
1810                                                (i < mp->n_input),
1811                                                ntohl (mp->acls[i]));
1812             }
1813         }
1814     }
1815
1816   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1817 }
1818
1819 static void
1820 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1821 {
1822   api_rule->is_permit = r->is_permit;
1823   api_rule->is_ipv6 = r->is_ipv6;
1824   if (r->is_ipv6)
1825     {
1826       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1827       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1828     }
1829   else
1830     {
1831       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1832       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1833     }
1834   api_rule->src_ip_prefix_len = r->src_prefixlen;
1835   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1836   api_rule->proto = r->proto;
1837   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1838   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1839   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1840   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1841   api_rule->tcp_flags_mask = r->tcp_flags_mask;
1842   api_rule->tcp_flags_value = r->tcp_flags_value;
1843 }
1844
1845 static void
1846 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1847                   acl_list_t * acl, u32 context)
1848 {
1849   vl_api_acl_details_t *mp;
1850   vl_api_acl_rule_t *rules;
1851   int i;
1852   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1853   void *oldheap = acl_set_heap (am);
1854
1855   mp = vl_msg_api_alloc (msg_size);
1856   memset (mp, 0, msg_size);
1857   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1858
1859   /* fill in the message */
1860   mp->context = context;
1861   mp->count = htonl (acl->count);
1862   mp->acl_index = htonl (acl - am->acls);
1863   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1864   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1865   rules = mp->r;
1866   for (i = 0; i < acl->count; i++)
1867     {
1868       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1869     }
1870
1871   clib_mem_set_heap (oldheap);
1872   vl_msg_api_send_shmem (q, (u8 *) & mp);
1873 }
1874
1875
1876 static void
1877 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1878 {
1879   acl_main_t *am = &acl_main;
1880   u32 acl_index;
1881   acl_list_t *acl;
1882
1883   int rv = -1;
1884   unix_shared_memory_queue_t *q;
1885
1886   q = vl_api_client_index_to_input_queue (mp->client_index);
1887   if (q == 0)
1888     {
1889       return;
1890     }
1891
1892   if (mp->acl_index == ~0)
1893     {
1894     /* *INDENT-OFF* */
1895     /* Just dump all ACLs */
1896     pool_foreach (acl, am->acls,
1897     ({
1898       send_acl_details(am, q, acl, mp->context);
1899     }));
1900     /* *INDENT-ON* */
1901     }
1902   else
1903     {
1904       acl_index = ntohl (mp->acl_index);
1905       if (!pool_is_free_index (am->acls, acl_index))
1906         {
1907           acl = pool_elt_at_index (am->acls, acl_index);
1908           send_acl_details (am, q, acl, mp->context);
1909         }
1910     }
1911
1912   if (rv == -1)
1913     {
1914       /* FIXME API: should we signal an error here at all ? */
1915       return;
1916     }
1917 }
1918
1919 static void
1920 send_acl_interface_list_details (acl_main_t * am,
1921                                  unix_shared_memory_queue_t * q,
1922                                  u32 sw_if_index, u32 context)
1923 {
1924   vl_api_acl_interface_list_details_t *mp;
1925   int msg_size;
1926   int n_input;
1927   int n_output;
1928   int count;
1929   int i = 0;
1930   void *oldheap = acl_set_heap (am);
1931
1932   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1933   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1934
1935   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1936   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1937   count = n_input + n_output;
1938
1939   msg_size = sizeof (*mp);
1940   msg_size += sizeof (mp->acls[0]) * count;
1941
1942   mp = vl_msg_api_alloc (msg_size);
1943   memset (mp, 0, msg_size);
1944   mp->_vl_msg_id =
1945     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1946
1947   /* fill in the message */
1948   mp->context = context;
1949   mp->sw_if_index = htonl (sw_if_index);
1950   mp->count = count;
1951   mp->n_input = n_input;
1952   for (i = 0; i < n_input; i++)
1953     {
1954       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1955     }
1956   for (i = 0; i < n_output; i++)
1957     {
1958       mp->acls[n_input + i] =
1959         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1960     }
1961   clib_mem_set_heap (oldheap);
1962   vl_msg_api_send_shmem (q, (u8 *) & mp);
1963 }
1964
1965 static void
1966 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1967                                           mp)
1968 {
1969   acl_main_t *am = &acl_main;
1970   vnet_sw_interface_t *swif;
1971   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1972
1973   u32 sw_if_index;
1974   unix_shared_memory_queue_t *q;
1975
1976   q = vl_api_client_index_to_input_queue (mp->client_index);
1977   if (q == 0)
1978     {
1979       return;
1980     }
1981
1982   if (mp->sw_if_index == ~0)
1983     {
1984     /* *INDENT-OFF* */
1985     pool_foreach (swif, im->sw_interfaces,
1986     ({
1987       send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1988     }));
1989     /* *INDENT-ON* */
1990     }
1991   else
1992     {
1993       sw_if_index = ntohl (mp->sw_if_index);
1994       if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
1995         send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1996     }
1997 }
1998
1999 /* MACIP ACL API handlers */
2000
2001 static void
2002 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2003 {
2004   vl_api_macip_acl_add_reply_t *rmp;
2005   acl_main_t *am = &acl_main;
2006   int rv;
2007   u32 acl_list_index = ~0;
2008   u32 acl_count = ntohl (mp->count);
2009   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2010
2011   if (verify_message_len (mp, expected_len, "macip_acl_add"))
2012     {
2013       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2014     }
2015   else
2016     {
2017       rv = VNET_API_ERROR_INVALID_VALUE;
2018     }
2019
2020   /* *INDENT-OFF* */
2021   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2022   ({
2023     rmp->acl_index = htonl(acl_list_index);
2024   }));
2025   /* *INDENT-ON* */
2026 }
2027
2028 static void
2029 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2030 {
2031   vl_api_macip_acl_add_replace_reply_t *rmp;
2032   acl_main_t *am = &acl_main;
2033   int rv;
2034   u32 acl_list_index = ntohl (mp->acl_index);
2035   u32 acl_count = ntohl (mp->count);
2036   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2037
2038   if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2039     {
2040       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2041     }
2042   else
2043     {
2044       rv = VNET_API_ERROR_INVALID_VALUE;
2045     }
2046
2047   /* *INDENT-OFF* */
2048   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2049   ({
2050     rmp->acl_index = htonl(acl_list_index);
2051   }));
2052   /* *INDENT-ON* */
2053 }
2054
2055 static void
2056 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2057 {
2058   acl_main_t *am = &acl_main;
2059   vl_api_macip_acl_del_reply_t *rmp;
2060   int rv;
2061
2062   rv = macip_acl_del_list (ntohl (mp->acl_index));
2063
2064   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2065 }
2066
2067 static void
2068   vl_api_macip_acl_interface_add_del_t_handler
2069   (vl_api_macip_acl_interface_add_del_t * mp)
2070 {
2071   acl_main_t *am = &acl_main;
2072   vl_api_macip_acl_interface_add_del_reply_t *rmp;
2073   int rv = -1;
2074   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2075   u32 sw_if_index = ntohl (mp->sw_if_index);
2076
2077   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2078     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2079   else
2080     rv =
2081       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2082                                        ntohl (mp->acl_index));
2083
2084   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2085 }
2086
2087 static void
2088 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
2089                         macip_acl_list_t * acl, u32 context)
2090 {
2091   vl_api_macip_acl_details_t *mp;
2092   vl_api_macip_acl_rule_t *rules;
2093   macip_acl_rule_t *r;
2094   int i;
2095   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2096
2097   mp = vl_msg_api_alloc (msg_size);
2098   memset (mp, 0, msg_size);
2099   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2100
2101   /* fill in the message */
2102   mp->context = context;
2103   if (acl)
2104     {
2105       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2106       mp->count = htonl (acl->count);
2107       mp->acl_index = htonl (acl - am->macip_acls);
2108       rules = mp->r;
2109       for (i = 0; i < acl->count; i++)
2110         {
2111           r = &acl->rules[i];
2112           rules[i].is_permit = r->is_permit;
2113           rules[i].is_ipv6 = r->is_ipv6;
2114           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2115           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2116                   sizeof (r->src_mac_mask));
2117           if (r->is_ipv6)
2118             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2119                     sizeof (r->src_ip_addr.ip6));
2120           else
2121             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2122                     sizeof (r->src_ip_addr.ip4));
2123           rules[i].src_ip_prefix_len = r->src_prefixlen;
2124         }
2125     }
2126   else
2127     {
2128       /* No martini, no party - no ACL applied to this interface. */
2129       mp->acl_index = ~0;
2130       mp->count = 0;
2131     }
2132
2133   vl_msg_api_send_shmem (q, (u8 *) & mp);
2134 }
2135
2136
2137 static void
2138 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2139 {
2140   acl_main_t *am = &acl_main;
2141   macip_acl_list_t *acl;
2142
2143   unix_shared_memory_queue_t *q;
2144
2145   q = vl_api_client_index_to_input_queue (mp->client_index);
2146   if (q == 0)
2147     {
2148       return;
2149     }
2150
2151   if (mp->acl_index == ~0)
2152     {
2153       /* Just dump all ACLs for now, with sw_if_index = ~0 */
2154       pool_foreach (acl, am->macip_acls, (
2155                                            {
2156                                            send_macip_acl_details (am, q, acl,
2157                                                                    mp->
2158                                                                    context);}
2159                     ));
2160       /* *INDENT-ON* */
2161     }
2162   else
2163     {
2164       u32 acl_index = ntohl (mp->acl_index);
2165       if (!pool_is_free_index (am->macip_acls, acl_index))
2166         {
2167           acl = pool_elt_at_index (am->macip_acls, acl_index);
2168           send_macip_acl_details (am, q, acl, mp->context);
2169         }
2170     }
2171 }
2172
2173 static void
2174 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2175                                           mp)
2176 {
2177   acl_main_t *am = &acl_main;
2178   vl_api_macip_acl_interface_get_reply_t *rmp;
2179   u32 count = vec_len (am->macip_acl_by_sw_if_index);
2180   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2181   unix_shared_memory_queue_t *q;
2182   int i;
2183
2184   q = vl_api_client_index_to_input_queue (mp->client_index);
2185   if (q == 0)
2186     {
2187       return;
2188     }
2189
2190   rmp = vl_msg_api_alloc (msg_size);
2191   memset (rmp, 0, msg_size);
2192   rmp->_vl_msg_id =
2193     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2194   rmp->context = mp->context;
2195   rmp->count = htonl (count);
2196   for (i = 0; i < count; i++)
2197     {
2198       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2199     }
2200
2201   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2202 }
2203
2204 static void
2205 send_macip_acl_interface_list_details (acl_main_t * am,
2206                                        unix_shared_memory_queue_t * q,
2207                                        u32 sw_if_index,
2208                                        u32 acl_index, u32 context)
2209 {
2210   vl_api_macip_acl_interface_list_details_t *rmp;
2211   /* at this time there is only ever 1 mac ip acl per interface */
2212   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2213
2214   rmp = vl_msg_api_alloc (msg_size);
2215   memset (rmp, 0, msg_size);
2216   rmp->_vl_msg_id =
2217     ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2218
2219   /* fill in the message */
2220   rmp->context = context;
2221   rmp->count = 1;
2222   rmp->sw_if_index = htonl (sw_if_index);
2223   rmp->acls[0] = htonl (acl_index);
2224
2225   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2226 }
2227
2228 static void
2229   vl_api_macip_acl_interface_list_dump_t_handler
2230   (vl_api_macip_acl_interface_list_dump_t * mp)
2231 {
2232   unix_shared_memory_queue_t *q;
2233   acl_main_t *am = &acl_main;
2234   u32 sw_if_index = ntohl (mp->sw_if_index);
2235
2236   q = vl_api_client_index_to_input_queue (mp->client_index);
2237   if (q == 0)
2238     {
2239       return;
2240     }
2241
2242   if (sw_if_index == ~0)
2243     {
2244       vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2245       {
2246         if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2247           {
2248             send_macip_acl_interface_list_details (am, q, sw_if_index,
2249                                                    am->macip_acl_by_sw_if_index
2250                                                    [sw_if_index],
2251                                                    mp->context);
2252           }
2253       }
2254     }
2255   else
2256     {
2257       if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2258         {
2259           send_macip_acl_interface_list_details (am, q, sw_if_index,
2260                                                  am->macip_acl_by_sw_if_index
2261                                                  [sw_if_index], mp->context);
2262         }
2263     }
2264 }
2265
2266 /* Set up the API message handling tables */
2267 static clib_error_t *
2268 acl_plugin_api_hookup (vlib_main_t * vm)
2269 {
2270   acl_main_t *am = &acl_main;
2271 #define _(N,n)                                                  \
2272     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
2273                            #n,                                  \
2274                            vl_api_##n##_t_handler,              \
2275                            vl_noop_handler,                     \
2276                            vl_api_##n##_t_endian,               \
2277                            vl_api_##n##_t_print,                \
2278                            sizeof(vl_api_##n##_t), 1);
2279   foreach_acl_plugin_api_msg;
2280 #undef _
2281
2282   return 0;
2283 }
2284
2285 #define vl_msg_name_crc_list
2286 #include <acl/acl_all_api_h.h>
2287 #undef vl_msg_name_crc_list
2288
2289 static void
2290 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2291 {
2292 #define _(id,n,crc) \
2293   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2294   foreach_vl_msg_name_crc_acl;
2295 #undef _
2296 }
2297
2298 static void
2299 acl_setup_fa_nodes (void)
2300 {
2301   vlib_main_t *vm = vlib_get_main ();
2302   acl_main_t *am = &acl_main;
2303   vlib_node_t *n, *n4, *n6;
2304
2305   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2306   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2307   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2308
2309
2310   am->l2_input_classify_next_acl_ip4 =
2311     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2312   am->l2_input_classify_next_acl_ip6 =
2313     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2314
2315   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2316                                l2input_get_feat_names (),
2317                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2318
2319   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2320                                l2input_get_feat_names (),
2321                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2322
2323
2324   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2325   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2326   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2327
2328   am->l2_output_classify_next_acl_ip4 =
2329     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2330   am->l2_output_classify_next_acl_ip6 =
2331     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2332
2333   feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
2334                                l2output_get_feat_names (),
2335                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2336
2337   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2338                                l2output_get_feat_names (),
2339                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2340 }
2341
2342 static void
2343 acl_set_timeout_sec (int timeout_type, u32 value)
2344 {
2345   acl_main_t *am = &acl_main;
2346   clib_time_t *ct = &am->vlib_main->clib_time;
2347
2348   if (timeout_type < ACL_N_TIMEOUTS)
2349     {
2350       am->session_timeout_sec[timeout_type] = value;
2351     }
2352   else
2353     {
2354       clib_warning ("Unknown timeout type %d", timeout_type);
2355       return;
2356     }
2357   am->session_timeout[timeout_type] =
2358     (u64) (((f64) value) / ct->seconds_per_clock);
2359 }
2360
2361 static void
2362 acl_set_session_max_entries (u32 value)
2363 {
2364   acl_main_t *am = &acl_main;
2365   am->fa_conn_table_max_entries = value;
2366 }
2367
2368 static int
2369 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2370 {
2371   acl_main_t *am = &acl_main;
2372
2373   if ((eh < 256) && (value < 2))
2374     {
2375       am->fa_ipv6_known_eh_bitmap =
2376         clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2377       return 1;
2378     }
2379   else
2380     return 0;
2381 }
2382
2383
2384 static clib_error_t *
2385 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2386 {
2387   acl_main_t *am = &acl_main;
2388   if (0 == am->acl_mheap)
2389     {
2390       /* ACL heap is not initialized, so definitely nothing to do. */
2391       return 0;
2392     }
2393   if (0 == is_add)
2394     {
2395       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2396                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2397                                  sw_if_index);
2398       /* also unapply any ACLs in case the users did not do so. */
2399       macip_acl_interface_del_acl (am, sw_if_index);
2400       acl_interface_reset_inout_acls (sw_if_index, 0);
2401       acl_interface_reset_inout_acls (sw_if_index, 1);
2402     }
2403   return 0;
2404 }
2405
2406 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2407
2408
2409
2410 static clib_error_t *
2411 acl_set_aclplugin_fn (vlib_main_t * vm,
2412                       unformat_input_t * input, vlib_cli_command_t * cmd)
2413 {
2414   clib_error_t *error = 0;
2415   u32 timeout = 0;
2416   u32 val = 0;
2417   u32 eh_val = 0;
2418   uword memory_size = 0;
2419   acl_main_t *am = &acl_main;
2420
2421   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2422     {
2423       if (!acl_set_skip_ipv6_eh (eh_val, val))
2424         {
2425           error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2426         }
2427       goto done;
2428     }
2429   if (unformat (input, "use-hash-acl-matching %u", &val))
2430     {
2431       am->use_hash_acl_matching = (val != 0);
2432       goto done;
2433     }
2434   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2435     {
2436       am->l4_match_nonfirst_fragment = (val != 0);
2437       goto done;
2438     }
2439   if (unformat (input, "heap"))
2440     {
2441       if (unformat (input, "main"))
2442         {
2443           if (unformat (input, "validate %u", &val))
2444             acl_plugin_acl_set_validate_heap (am, val);
2445           else if (unformat (input, "trace %u", &val))
2446             acl_plugin_acl_set_trace_heap (am, val);
2447           goto done;
2448         }
2449       else if (unformat (input, "hash"))
2450         {
2451           if (unformat (input, "validate %u", &val))
2452             acl_plugin_hash_acl_set_validate_heap (am, val);
2453           else if (unformat (input, "trace %u", &val))
2454             acl_plugin_hash_acl_set_trace_heap (am, val);
2455           goto done;
2456         }
2457       goto done;
2458     }
2459   if (unformat (input, "session"))
2460     {
2461       if (unformat (input, "table"))
2462         {
2463           /* The commands here are for tuning/testing. No user-serviceable parts inside */
2464           if (unformat (input, "max-entries"))
2465             {
2466               if (!unformat (input, "%u", &val))
2467                 {
2468                   error = clib_error_return (0,
2469                                              "expecting maximum number of entries, got `%U`",
2470                                              format_unformat_error, input);
2471                   goto done;
2472                 }
2473               else
2474                 {
2475                   acl_set_session_max_entries (val);
2476                   goto done;
2477                 }
2478             }
2479           if (unformat (input, "hash-table-buckets"))
2480             {
2481               if (!unformat (input, "%u", &val))
2482                 {
2483                   error = clib_error_return (0,
2484                                              "expecting maximum number of hash table buckets, got `%U`",
2485                                              format_unformat_error, input);
2486                   goto done;
2487                 }
2488               else
2489                 {
2490                   am->fa_conn_table_hash_num_buckets = val;
2491                   goto done;
2492                 }
2493             }
2494           if (unformat (input, "hash-table-memory"))
2495             {
2496               if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2497                 {
2498                   error = clib_error_return (0,
2499                                              "expecting maximum amount of hash table memory, got `%U`",
2500                                              format_unformat_error, input);
2501                   goto done;
2502                 }
2503               else
2504                 {
2505                   am->fa_conn_table_hash_memory_size = memory_size;
2506                   goto done;
2507                 }
2508             }
2509           goto done;
2510         }
2511       if (unformat (input, "timeout"))
2512         {
2513           if (unformat (input, "udp"))
2514             {
2515               if (unformat (input, "idle"))
2516                 {
2517                   if (!unformat (input, "%u", &timeout))
2518                     {
2519                       error = clib_error_return (0,
2520                                                  "expecting timeout value in seconds, got `%U`",
2521                                                  format_unformat_error,
2522                                                  input);
2523                       goto done;
2524                     }
2525                   else
2526                     {
2527                       acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2528                       goto done;
2529                     }
2530                 }
2531             }
2532           if (unformat (input, "tcp"))
2533             {
2534               if (unformat (input, "idle"))
2535                 {
2536                   if (!unformat (input, "%u", &timeout))
2537                     {
2538                       error = clib_error_return (0,
2539                                                  "expecting timeout value in seconds, got `%U`",
2540                                                  format_unformat_error,
2541                                                  input);
2542                       goto done;
2543                     }
2544                   else
2545                     {
2546                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2547                       goto done;
2548                     }
2549                 }
2550               if (unformat (input, "transient"))
2551                 {
2552                   if (!unformat (input, "%u", &timeout))
2553                     {
2554                       error = clib_error_return (0,
2555                                                  "expecting timeout value in seconds, got `%U`",
2556                                                  format_unformat_error,
2557                                                  input);
2558                       goto done;
2559                     }
2560                   else
2561                     {
2562                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2563                                            timeout);
2564                       goto done;
2565                     }
2566                 }
2567             }
2568           goto done;
2569         }
2570     }
2571 done:
2572   return error;
2573 }
2574
2575 static u8 *
2576 my_format_mac_address (u8 * s, va_list * args)
2577 {
2578   u8 *a = va_arg (*args, u8 *);
2579   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2580                  a[0], a[1], a[2], a[3], a[4], a[5]);
2581 }
2582
2583 static inline u8 *
2584 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2585 {
2586   macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2587
2588   out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2589                 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2590                 format_ip46_address, &a->src_ip_addr,
2591                 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2592                 a->src_prefixlen,
2593                 my_format_mac_address, a->src_mac,
2594                 my_format_mac_address, a->src_mac_mask);
2595   return (out);
2596 }
2597
2598 static void
2599 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2600 {
2601   vlib_main_t *vm = am->vlib_main;
2602   int i;
2603
2604   /* Don't try to print someone else's memory */
2605   if (macip_acl_index > vec_len (am->macip_acls))
2606     return;
2607
2608   macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2609   int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2610
2611   vlib_cli_output (vm,
2612                    "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2613                    macip_acl_index, a->count, vec_len (a->rules), a->tag,
2614                    free_pool_slot);
2615   vlib_cli_output (vm,
2616                    "  ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2617                    a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2618   for (i = 0; i < vec_len (a->rules); i++)
2619     vlib_cli_output (vm, "    rule %d: %U\n", i,
2620                      my_macip_acl_rule_t_pretty_format,
2621                      vec_elt_at_index (a->rules, i));
2622
2623 }
2624
2625 static clib_error_t *
2626 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2627                                  unformat_input_t * input,
2628                                  vlib_cli_command_t * cmd)
2629 {
2630   clib_error_t *error = 0;
2631   acl_main_t *am = &acl_main;
2632   int i;
2633   for (i = 0; i < vec_len (am->macip_acls); i++)
2634     macip_acl_print (am, i);
2635   return error;
2636 }
2637
2638 static clib_error_t *
2639 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2640                                        unformat_input_t * input,
2641                                        vlib_cli_command_t * cmd)
2642 {
2643   clib_error_t *error = 0;
2644   acl_main_t *am = &acl_main;
2645   int i;
2646   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2647     {
2648       vlib_cli_output (vm, "  sw_if_index %d: %d\n", i,
2649                        vec_elt (am->macip_acl_by_sw_if_index, i));
2650     }
2651   return error;
2652 }
2653
2654 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
2655 static void
2656 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
2657 {
2658   acl_rule_t *r;
2659   u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
2660                      am->acls[acl_index].count, am->acls[acl_index].tag);
2661   int j;
2662   PRINT_AND_RESET (vm, out0);
2663   for (j = 0; j < am->acls[acl_index].count; j++)
2664     {
2665       r = &am->acls[acl_index].rules[j];
2666       out0 = format (out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2667       out0 = format_acl_action (out0, r->is_permit);
2668       out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
2669                      r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2670                      r->src_prefixlen);
2671       out0 =
2672         format (out0, " dst %U/%d", format_ip46_address, &r->dst,
2673                 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
2674       out0 = format (out0, " proto %d", r->proto);
2675       out0 = format (out0, " sport %d", r->src_port_or_type_first);
2676       if (r->src_port_or_type_first != r->src_port_or_type_last)
2677         {
2678           out0 = format (out0, "-%d", r->src_port_or_type_last);
2679         }
2680       out0 = format (out0, " dport %d", r->dst_port_or_code_first);
2681       if (r->dst_port_or_code_first != r->dst_port_or_code_last)
2682         {
2683           out0 = format (out0, "-%d", r->dst_port_or_code_last);
2684         }
2685       if (r->tcp_flags_mask || r->tcp_flags_value)
2686         {
2687           out0 =
2688             format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
2689                     r->tcp_flags_mask);
2690         }
2691       out0 = format (out0, "\n");
2692       PRINT_AND_RESET (vm, out0);
2693     }
2694 }
2695
2696 #undef PRINT_AND_RESET
2697
2698 static void
2699 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2700 {
2701   u32 i;
2702   vlib_main_t *vm = am->vlib_main;
2703
2704   for (i = 0; i < vec_len (am->acls); i++)
2705     {
2706       if (acl_is_not_defined (am, i))
2707         {
2708           /* don't attempt to show the ACLs that do not exist */
2709           continue;
2710         }
2711       if ((acl_index != ~0) && (acl_index != i))
2712         {
2713           continue;
2714         }
2715       acl_print_acl (vm, am, i);
2716
2717       if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2718         {
2719           vlib_cli_output (vm, "  applied inbound on sw_if_index: %U\n",
2720                            format_vec32, am->input_sw_if_index_vec_by_acl[i],
2721                            "%d");
2722         }
2723       if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2724         {
2725           vlib_cli_output (vm, "  applied outbound on sw_if_index: %U\n",
2726                            format_vec32, am->output_sw_if_index_vec_by_acl[i],
2727                            "%d");
2728         }
2729     }
2730 }
2731
2732 static clib_error_t *
2733 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2734                            unformat_input_t * input, vlib_cli_command_t * cmd)
2735 {
2736   clib_error_t *error = 0;
2737   acl_main_t *am = &acl_main;
2738
2739   u32 acl_index = ~0;
2740   (void) unformat (input, "index %u", &acl_index);
2741
2742   acl_plugin_show_acl (am, acl_index);
2743   return error;
2744 }
2745
2746 static void
2747 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
2748 {
2749   vlib_main_t *vm = am->vlib_main;
2750   u32 swi;
2751   u32 *pj;
2752   for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
2753        (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
2754     {
2755       /* if we need a particular interface, skip all the others */
2756       if ((sw_if_index != ~0) && (sw_if_index != swi))
2757         continue;
2758
2759       vlib_cli_output (vm, "sw_if_index %d:\n", swi);
2760
2761       if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
2762           (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
2763         {
2764           vlib_cli_output (vm, "  input acl(s): %U", format_vec32,
2765                            am->input_acl_vec_by_sw_if_index[swi], "%d");
2766           if (show_acl)
2767             {
2768               vlib_cli_output (vm, "\n");
2769               vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
2770               {
2771                 acl_print_acl (vm, am, *pj);
2772               }
2773               vlib_cli_output (vm, "\n");
2774             }
2775         }
2776
2777       if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
2778           (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
2779         {
2780           vlib_cli_output (vm, "  output acl(s): %U", format_vec32,
2781                            am->output_acl_vec_by_sw_if_index[swi], "%d");
2782           if (show_acl)
2783             {
2784               vlib_cli_output (vm, "\n");
2785               vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
2786               {
2787                 acl_print_acl (vm, am, *pj);
2788               }
2789               vlib_cli_output (vm, "\n");
2790             }
2791         }
2792     }
2793
2794 }
2795
2796 static clib_error_t *
2797 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
2798                                  unformat_input_t * input,
2799                                  vlib_cli_command_t * cmd)
2800 {
2801   clib_error_t *error = 0;
2802   acl_main_t *am = &acl_main;
2803
2804   u32 sw_if_index = ~0;
2805   (void) unformat (input, "sw_if_index %u", &sw_if_index);
2806   int show_acl = unformat (input, "acl");
2807
2808   acl_plugin_show_interface (am, sw_if_index, show_acl);
2809   return error;
2810 }
2811
2812 static clib_error_t *
2813 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
2814                               unformat_input_t * input,
2815                               vlib_cli_command_t * cmd)
2816 {
2817   clib_error_t *error = 0;
2818   acl_main_t *am = &acl_main;
2819
2820   vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2821   if (am->acl_mheap)
2822     {
2823       vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2824     }
2825   else
2826     {
2827       vlib_cli_output (vm, " Not initialized\n");
2828     }
2829   vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2830   if (am->hash_lookup_mheap)
2831     {
2832       vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2833     }
2834   else
2835     {
2836       vlib_cli_output (vm, " Not initialized\n");
2837     }
2838   return error;
2839 }
2840
2841 static void
2842 acl_plugin_show_sessions (acl_main_t * am,
2843                           u32 show_session_thread_id,
2844                           u32 show_session_session_index)
2845 {
2846   vlib_main_t *vm = am->vlib_main;
2847   u16 wk;
2848   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2849   vnet_sw_interface_t *swif;
2850
2851   {
2852     u64 n_adds = am->fa_session_total_adds;
2853     u64 n_dels = am->fa_session_total_dels;
2854     vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
2855                      n_dels, n_adds - n_dels);
2856   }
2857   vlib_cli_output (vm, "\n\nPer-thread data:");
2858   for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
2859     {
2860       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2861       vlib_cli_output (vm, "Thread #%d:", wk);
2862       if (show_session_thread_id == wk
2863           && show_session_session_index < pool_len (pw->fa_sessions_pool))
2864         {
2865           vlib_cli_output (vm, "  session index %u:",
2866                            show_session_session_index);
2867           fa_session_t *sess =
2868             pw->fa_sessions_pool + show_session_session_index;
2869           u64 *m = (u64 *) & sess->info;
2870           vlib_cli_output (vm,
2871                            "    info: %016llx %016llx %016llx %016llx %016llx %016llx",
2872                            m[0], m[1], m[2], m[3], m[4], m[5]);
2873           vlib_cli_output (vm, "    sw_if_index: %u", sess->sw_if_index);
2874           vlib_cli_output (vm, "    tcp_flags_seen: %x",
2875                            sess->tcp_flags_seen.as_u16);
2876           vlib_cli_output (vm, "    last active time: %lu",
2877                            sess->last_active_time);
2878           vlib_cli_output (vm, "    thread index: %u", sess->thread_index);
2879           vlib_cli_output (vm, "    link enqueue time: %lu",
2880                            sess->link_enqueue_time);
2881           vlib_cli_output (vm, "    link next index: %u",
2882                            sess->link_next_idx);
2883           vlib_cli_output (vm, "    link prev index: %u",
2884                            sess->link_prev_idx);
2885           vlib_cli_output (vm, "    link list id: %u", sess->link_list_id);
2886         }
2887       vlib_cli_output (vm, "  connection add/del stats:", wk);
2888       pool_foreach (swif, im->sw_interfaces, (
2889                                                {
2890                                                u32 sw_if_index =
2891                                                swif->sw_if_index;
2892                                                u64 n_adds =
2893                                                sw_if_index <
2894                                                vec_len
2895                                                (pw->fa_session_adds_by_sw_if_index)
2896                                                ?
2897                                                pw->fa_session_adds_by_sw_if_index
2898                                                [sw_if_index] : 0;
2899                                                u64 n_dels =
2900                                                sw_if_index <
2901                                                vec_len
2902                                                (pw->fa_session_dels_by_sw_if_index)
2903                                                ?
2904                                                pw->fa_session_dels_by_sw_if_index
2905                                                [sw_if_index] : 0;
2906                                                vlib_cli_output (vm,
2907                                                                 "    sw_if_index %d: add %lu - del %lu = %lu",
2908                                                                 sw_if_index,
2909                                                                 n_adds,
2910                                                                 n_dels,
2911                                                                 n_adds -
2912                                                                 n_dels);
2913                                                }
2914                     ));
2915
2916       vlib_cli_output (vm, "  connection timeout type lists:", wk);
2917       u8 tt = 0;
2918       for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
2919         {
2920           u32 head_session_index = pw->fa_conn_list_head[tt];
2921           vlib_cli_output (vm, "  fa_conn_list_head[%d]: %d", tt,
2922                            head_session_index);
2923           if (~0 != head_session_index)
2924             {
2925               fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2926               vlib_cli_output (vm, "    last active time: %lu",
2927                                sess->last_active_time);
2928               vlib_cli_output (vm, "    link enqueue time: %lu",
2929                                sess->link_enqueue_time);
2930             }
2931         }
2932
2933       vlib_cli_output (vm, "  Next expiry time: %lu", pw->next_expiry_time);
2934       vlib_cli_output (vm, "  Requeue until time: %lu",
2935                        pw->requeue_until_time);
2936       vlib_cli_output (vm, "  Current time wait interval: %lu",
2937                        pw->current_time_wait_interval);
2938       vlib_cli_output (vm, "  Count of deleted sessions: %lu",
2939                        pw->cnt_deleted_sessions);
2940       vlib_cli_output (vm, "  Delete already deleted: %lu",
2941                        pw->cnt_already_deleted_sessions);
2942       vlib_cli_output (vm, "  Session timers restarted: %lu",
2943                        pw->cnt_session_timer_restarted);
2944       vlib_cli_output (vm, "  Swipe until this time: %lu",
2945                        pw->swipe_end_time);
2946       vlib_cli_output (vm, "  sw_if_index serviced bitmap: %U",
2947                        format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2948       vlib_cli_output (vm, "  pending clear intfc bitmap : %U",
2949                        format_bitmap_hex,
2950                        pw->pending_clear_sw_if_index_bitmap);
2951       vlib_cli_output (vm, "  clear in progress: %u", pw->clear_in_process);
2952       vlib_cli_output (vm, "  interrupt is pending: %d",
2953                        pw->interrupt_is_pending);
2954       vlib_cli_output (vm, "  interrupt is needed: %d",
2955                        pw->interrupt_is_needed);
2956       vlib_cli_output (vm, "  interrupt is unwanted: %d",
2957                        pw->interrupt_is_unwanted);
2958       vlib_cli_output (vm, "  interrupt generation: %d",
2959                        pw->interrupt_generation);
2960     }
2961   vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
2962 #define _(cnt, desc) vlib_cli_output(vm, "             %20lu: %s", am->cnt, desc);
2963   foreach_fa_cleaner_counter;
2964 #undef _
2965   vlib_cli_output (vm, "Interrupt generation: %d",
2966                    am->fa_interrupt_generation);
2967   vlib_cli_output (vm,
2968                    "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2969                    am->fa_min_deleted_sessions_per_interval,
2970                    am->fa_max_deleted_sessions_per_interval,
2971                    am->fa_cleaner_wait_time_increment * 1000.0,
2972                    ((f64) am->fa_current_cleaner_timer_wait_interval) *
2973                    1000.0 / (f64) vm->clib_time.clocks_per_second);
2974 }
2975
2976 static clib_error_t *
2977 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
2978                                 unformat_input_t * input,
2979                                 vlib_cli_command_t * cmd)
2980 {
2981   clib_error_t *error = 0;
2982   acl_main_t *am = &acl_main;
2983
2984   u32 show_bihash_verbose = 0;
2985   u32 show_session_thread_id = ~0;
2986   u32 show_session_session_index = ~0;
2987   (void) unformat (input, "thread %u index %u", &show_session_thread_id,
2988                    &show_session_session_index);
2989   (void) unformat (input, "verbose %u", &show_bihash_verbose);
2990
2991   acl_plugin_show_sessions (am, show_session_thread_id,
2992                             show_session_session_index);
2993   show_fa_sessions_hash (vm, show_bihash_verbose);
2994   return error;
2995 }
2996
2997 static void
2998 acl_plugin_show_tables_mask_type (acl_main_t * am)
2999 {
3000   vlib_main_t *vm = am->vlib_main;
3001   ace_mask_type_entry_t *mte;
3002
3003   vlib_cli_output (vm, "Mask-type entries:");
3004     /* *INDENT-OFF* */
3005     pool_foreach(mte, am->ace_mask_type_pool,
3006     ({
3007       vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
3008                     mte - am->ace_mask_type_pool,
3009                     mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3010                     mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3011     }));
3012     /* *INDENT-ON* */
3013 }
3014
3015 static void
3016 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3017 {
3018   vlib_main_t *vm = am->vlib_main;
3019   u32 i, j;
3020   u64 *m;
3021   vlib_cli_output (vm, "Mask-ready ACL representations\n");
3022   for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3023     {
3024       if ((acl_index != ~0) && (acl_index != i))
3025         {
3026           continue;
3027         }
3028       hash_acl_info_t *ha = &am->hash_acl_infos[i];
3029       vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3030       vlib_cli_output (vm, "  applied  inbound on sw_if_index list: %U\n",
3031                        format_vec32, ha->inbound_sw_if_index_list, "%d");
3032       vlib_cli_output (vm, "  applied outbound on sw_if_index list: %U\n",
3033                        format_vec32, ha->outbound_sw_if_index_list, "%d");
3034       vlib_cli_output (vm, "  mask type index bitmap: %U\n",
3035                        format_bitmap_hex, ha->mask_type_index_bitmap);
3036       for (j = 0; j < vec_len (ha->rules); j++)
3037         {
3038           hash_ace_info_t *pa = &ha->rules[j];
3039           m = (u64 *) & pa->match;
3040           vlib_cli_output (vm,
3041                            "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3042                            j, m[0], m[1], m[2], m[3], m[4], m[5],
3043                            pa->mask_type_index, pa->acl_index, pa->ace_index,
3044                            pa->action, pa->src_portrange_not_powerof2,
3045                            pa->dst_portrange_not_powerof2);
3046         }
3047     }
3048 }
3049
3050 static void
3051 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3052 {
3053   vlib_cli_output (vm,
3054                    "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3055                    j, pae->acl_index, pae->ace_index, pae->action,
3056                    pae->hash_ace_info_index, pae->next_applied_entry_index,
3057                    pae->prev_applied_entry_index,
3058                    pae->tail_applied_entry_index, pae->hitcount);
3059 }
3060
3061 static void
3062 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3063 {
3064   vlib_main_t *vm = am->vlib_main;
3065   u32 swi, j;
3066   vlib_cli_output (vm, "Applied lookup entries for interfaces");
3067
3068   for (swi = 0;
3069        (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3070        || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3071        || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3072        || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3073     {
3074       if ((sw_if_index != ~0) && (sw_if_index != swi))
3075         {
3076           continue;
3077         }
3078       vlib_cli_output (vm, "sw_if_index %d:", swi);
3079       if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3080         {
3081           applied_hash_acl_info_t *pal =
3082             &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3083           vlib_cli_output (vm, "  input lookup mask_type_index_bitmap: %U",
3084                            format_bitmap_hex, pal->mask_type_index_bitmap);
3085           vlib_cli_output (vm, "  input applied acls: %U", format_vec32,
3086                            pal->applied_acls, "%d");
3087         }
3088       if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3089         {
3090           vlib_cli_output (vm, "  input lookup applied entries:");
3091           for (j = 0;
3092                j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3093                j++)
3094             {
3095               acl_plugin_print_pae (vm, j,
3096                                     &am->input_hash_entry_vec_by_sw_if_index
3097                                     [swi][j]);
3098             }
3099         }
3100
3101       if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3102         {
3103           applied_hash_acl_info_t *pal =
3104             &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3105           vlib_cli_output (vm, "  output lookup mask_type_index_bitmap: %U",
3106                            format_bitmap_hex, pal->mask_type_index_bitmap);
3107           vlib_cli_output (vm, "  output applied acls: %U", format_vec32,
3108                            pal->applied_acls, "%d");
3109         }
3110       if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3111         {
3112           vlib_cli_output (vm, "  output lookup applied entries:");
3113           for (j = 0;
3114                j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3115                j++)
3116             {
3117               acl_plugin_print_pae (vm, j,
3118                                     &am->output_hash_entry_vec_by_sw_if_index
3119                                     [swi][j]);
3120             }
3121         }
3122     }
3123 }
3124
3125 static void
3126 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3127 {
3128   vlib_main_t *vm = am->vlib_main;
3129   show_hash_acl_hash (vm, am, show_bihash_verbose);
3130 }
3131
3132 static clib_error_t *
3133 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3134                               unformat_input_t * input,
3135                               vlib_cli_command_t * cmd)
3136 {
3137   clib_error_t *error = 0;
3138   acl_main_t *am = &acl_main;
3139
3140   u32 acl_index = ~0;
3141   u32 sw_if_index = ~0;
3142   int show_acl_hash_info = 0;
3143   int show_applied_info = 0;
3144   int show_mask_type = 0;
3145   int show_bihash = 0;
3146   u32 show_bihash_verbose = 0;
3147
3148   if (unformat (input, "acl"))
3149     {
3150       show_acl_hash_info = 1;
3151       /* mask-type is handy to see as well right there */
3152       show_mask_type = 1;
3153       unformat (input, "index %u", &acl_index);
3154     }
3155   else if (unformat (input, "applied"))
3156     {
3157       show_applied_info = 1;
3158       unformat (input, "sw_if_index %u", &sw_if_index);
3159     }
3160   else if (unformat (input, "mask"))
3161     {
3162       show_mask_type = 1;
3163     }
3164   else if (unformat (input, "hash"))
3165     {
3166       show_bihash = 1;
3167       unformat (input, "verbose %u", &show_bihash_verbose);
3168     }
3169
3170   if (!
3171       (show_mask_type || show_acl_hash_info || show_applied_info
3172        || show_bihash))
3173     {
3174       /* if no qualifiers specified, show all */
3175       show_mask_type = 1;
3176       show_acl_hash_info = 1;
3177       show_applied_info = 1;
3178       show_bihash = 1;
3179     }
3180   if (show_mask_type)
3181     acl_plugin_show_tables_mask_type (am);
3182   if (show_acl_hash_info)
3183     acl_plugin_show_tables_acl_hash_info (am, acl_index);
3184   if (show_applied_info)
3185     acl_plugin_show_tables_applied_info (am, sw_if_index);
3186   if (show_bihash)
3187     acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3188
3189   return error;
3190 }
3191
3192 static clib_error_t *
3193 acl_clear_aclplugin_fn (vlib_main_t * vm,
3194                         unformat_input_t * input, vlib_cli_command_t * cmd)
3195 {
3196   clib_error_t *error = 0;
3197   acl_main_t *am = &acl_main;
3198   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3199                              ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3200   return error;
3201 }
3202
3203  /* *INDENT-OFF* */
3204 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3205     .path = "set acl-plugin",
3206     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3207     .function = acl_set_aclplugin_fn,
3208 };
3209
3210 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3211     .path = "show acl-plugin acl",
3212     .short_help = "show acl-plugin acl [index N]",
3213     .function = acl_show_aclplugin_acl_fn,
3214 };
3215
3216 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3217     .path = "show acl-plugin interface",
3218     .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3219     .function = acl_show_aclplugin_interface_fn,
3220 };
3221
3222 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3223     .path = "show acl-plugin memory",
3224     .short_help = "show acl-plugin memory",
3225     .function = acl_show_aclplugin_memory_fn,
3226 };
3227
3228 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3229     .path = "show acl-plugin sessions",
3230     .short_help = "show acl-plugin sessions",
3231     .function = acl_show_aclplugin_sessions_fn,
3232 };
3233
3234 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3235     .path = "show acl-plugin tables",
3236     .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3237     .function = acl_show_aclplugin_tables_fn,
3238 };
3239
3240 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3241     .path = "show acl-plugin macip acl",
3242     .short_help = "show acl-plugin macip acl",
3243     .function = acl_show_aclplugin_macip_acl_fn,
3244 };
3245
3246 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3247     .path = "show acl-plugin macip interface",
3248     .short_help = "show acl-plugin macip interface",
3249     .function = acl_show_aclplugin_macip_interface_fn,
3250 };
3251
3252 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3253     .path = "clear acl-plugin sessions",
3254     .short_help = "clear acl-plugin sessions",
3255     .function = acl_clear_aclplugin_fn,
3256 };
3257 /* *INDENT-ON* */
3258
3259 static clib_error_t *
3260 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3261 {
3262   acl_main_t *am = &acl_main;
3263   u32 conn_table_hash_buckets;
3264   u32 conn_table_hash_memory_size;
3265   u32 conn_table_max_entries;
3266   u32 main_heap_size;
3267   u32 hash_heap_size;
3268   u32 hash_lookup_hash_buckets;
3269   u32 hash_lookup_hash_memory;
3270
3271   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3272     {
3273       if (unformat
3274           (input, "connection hash buckets %d", &conn_table_hash_buckets))
3275         am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3276       else if (unformat (input, "connection hash memory %d",
3277                          &conn_table_hash_memory_size))
3278         am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3279       else if (unformat (input, "connection count max %d",
3280                          &conn_table_max_entries))
3281         am->fa_conn_table_max_entries = conn_table_max_entries;
3282       else if (unformat (input, "main heap size %d", &main_heap_size))
3283         am->acl_mheap_size = main_heap_size;
3284       else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3285         am->hash_lookup_mheap_size = hash_heap_size;
3286       else if (unformat (input, "hash lookup hash buckets %d",
3287                          &hash_lookup_hash_buckets))
3288         am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3289       else if (unformat (input, "hash lookup hash memory %d",
3290                          &hash_lookup_hash_memory))
3291         am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3292       else
3293         return clib_error_return (0, "unknown input '%U'",
3294                                   format_unformat_error, input);
3295     }
3296   return 0;
3297 }
3298
3299 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3300
3301 static clib_error_t *
3302 acl_init (vlib_main_t * vm)
3303 {
3304   acl_main_t *am = &acl_main;
3305   clib_error_t *error = 0;
3306   memset (am, 0, sizeof (*am));
3307   am->vlib_main = vm;
3308   am->vnet_main = vnet_get_main ();
3309
3310   u8 *name = format (0, "acl_%08x%c", api_version, 0);
3311
3312   /* Ask for a correctly-sized block of API message decode slots */
3313   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3314                                             VL_MSG_FIRST_AVAILABLE);
3315
3316   error = acl_plugin_api_hookup (vm);
3317
3318   /* Add our API messages to the global name_crc hash table */
3319   setup_message_id_table (am, &api_main);
3320
3321   vec_free (name);
3322
3323   acl_setup_fa_nodes ();
3324
3325   am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3326   am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3327
3328   am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3329   am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3330
3331   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3332     TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3333   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3334     TCP_SESSION_IDLE_TIMEOUT_SEC;
3335   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3336     UDP_SESSION_IDLE_TIMEOUT_SEC;
3337
3338   am->fa_conn_table_hash_num_buckets =
3339     ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3340   am->fa_conn_table_hash_memory_size =
3341     ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3342   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3343   vlib_thread_main_t *tm = vlib_get_thread_main ();
3344   vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3345   {
3346     u16 wk;
3347     u8 tt;
3348     for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3349       {
3350         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3351         vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3352         vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3353         for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3354           {
3355             pw->fa_conn_list_head[tt] = ~0;
3356             pw->fa_conn_list_tail[tt] = ~0;
3357           }
3358       }
3359   }
3360
3361   am->fa_min_deleted_sessions_per_interval =
3362     ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3363   am->fa_max_deleted_sessions_per_interval =
3364     ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3365   am->fa_cleaner_wait_time_increment =
3366     ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3367
3368   am->fa_cleaner_cnt_delete_by_sw_index = 0;
3369   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3370   am->fa_cleaner_cnt_unknown_event = 0;
3371   am->fa_cleaner_cnt_timer_restarted = 0;
3372   am->fa_cleaner_cnt_wait_with_timeout = 0;
3373
3374
3375 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3376   foreach_acl_eh
3377 #undef _
3378     am->l4_match_nonfirst_fragment = 1;
3379
3380   /* use the new fancy hash-based matching */
3381   am->use_hash_acl_matching = 1;
3382
3383   return error;
3384 }
3385
3386 VLIB_INIT_FUNCTION (acl_init);
3387
3388
3389 /*
3390  * fd.io coding-style-patch-verification: ON
3391  *
3392  * Local Variables:
3393  * eval: (c-set-style "gnu")
3394  * End:
3395  */