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