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