acl-plugin: shrink the L2 classifier tables used for traffic redirect
[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
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_DEL, macip_acl_del) \
74 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
75 _(MACIP_ACL_DUMP, macip_acl_dump) \
76 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
77
78 /* *INDENT-OFF* */
79 VLIB_PLUGIN_REGISTER () = {
80     .version = VPP_BUILD_VER,
81     .description = "Access Control Lists",
82 };
83 /* *INDENT-ON* */
84
85 static void
86 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
87 {
88   acl_main_t *am = &acl_main;
89   vl_api_acl_plugin_get_version_reply_t *rmp;
90   int msg_size = sizeof (*rmp);
91   unix_shared_memory_queue_t *q;
92
93   q = vl_api_client_index_to_input_queue (mp->client_index);
94   if (q == 0)
95     {
96       return;
97     }
98
99   rmp = vl_msg_api_alloc (msg_size);
100   memset (rmp, 0, msg_size);
101   rmp->_vl_msg_id =
102     ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
103   rmp->context = mp->context;
104   rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
105   rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
106
107   vl_msg_api_send_shmem (q, (u8 *) & rmp);
108 }
109
110 static void
111 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
112 {
113   vl_api_acl_plugin_control_ping_reply_t *rmp;
114   acl_main_t *am = &acl_main;
115   int rv = 0;
116
117   /* *INDENT-OFF* */
118   REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
119   ({
120     rmp->vpe_pid = ntohl (getpid ());
121   }));
122   /* *INDENT-ON* */
123 }
124
125 static int
126 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
127               u32 * acl_list_index, u8 * tag)
128 {
129   acl_main_t *am = &acl_main;
130   acl_list_t *a;
131   acl_rule_t *r;
132   acl_rule_t *acl_new_rules;
133   int i;
134
135   if (*acl_list_index != ~0)
136     {
137       /* They supplied some number, let's see if this ACL exists */
138       if (pool_is_free_index (am->acls, *acl_list_index))
139         {
140           /* tried to replace a non-existent ACL, no point doing anything */
141           return -1;
142         }
143     }
144
145   /* Create and populate the rules */
146   acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
147                                           CLIB_CACHE_LINE_BYTES);
148   if (!acl_new_rules)
149     {
150       /* Could not allocate rules. New or existing ACL - bail out regardless */
151       return -1;
152     }
153
154   for (i = 0; i < count; i++)
155     {
156       r = &acl_new_rules[i];
157       r->is_permit = rules[i].is_permit;
158       r->is_ipv6 = rules[i].is_ipv6;
159       if (r->is_ipv6)
160         {
161           memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
162           memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
163         }
164       else
165         {
166           memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
167           memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
168         }
169       r->src_prefixlen = rules[i].src_ip_prefix_len;
170       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
171       r->proto = rules[i].proto;
172       r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
173       r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
174       r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
175       r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
176       r->tcp_flags_value = rules[i].tcp_flags_value;
177       r->tcp_flags_mask = rules[i].tcp_flags_mask;
178     }
179
180   if (~0 == *acl_list_index)
181     {
182       /* Get ACL index */
183       pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
184       memset (a, 0, sizeof (*a));
185       /* Will return the newly allocated ACL index */
186       *acl_list_index = a - am->acls;
187     }
188   else
189     {
190       a = am->acls + *acl_list_index;
191       /* Get rid of the old rules */
192       clib_mem_free (a->rules);
193     }
194   a->rules = acl_new_rules;
195   a->count = count;
196   memcpy (a->tag, tag, sizeof (a->tag));
197
198   return 0;
199 }
200
201 static int
202 acl_del_list (u32 acl_list_index)
203 {
204   acl_main_t *am = &acl_main;
205   acl_list_t *a;
206   int i, ii;
207   if (pool_is_free_index (am->acls, acl_list_index))
208     {
209       return -1;
210     }
211
212   /* delete any references to the ACL */
213   for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
214     {
215       for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
216            /* see body */ )
217         {
218           if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
219             {
220               vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
221             }
222           else
223             {
224               ii++;
225             }
226         }
227     }
228   for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
229     {
230       for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
231            /* see body */ )
232         {
233           if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
234             {
235               vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
236             }
237           else
238             {
239               ii++;
240             }
241         }
242     }
243
244   /* now we can delete the ACL itself */
245   a = &am->acls[acl_list_index];
246   if (a->rules)
247     {
248       clib_mem_free (a->rules);
249     }
250   pool_put (am->acls, a);
251   return 0;
252 }
253
254 /* Some aids in ASCII graphing the content */
255 #define XX "\377"
256 #define __ "\000"
257 #define _(x)
258 #define v
259
260 u8 ip4_5tuple_mask[] =
261 _("             dmac               smac            etype ")
262 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
263   _("        v ihl totlen   ")
264   _(0x0000)
265   __ __ __ __
266   _("        ident fl+fo    ")
267   _(0x0004)
268   __ __ __ __
269   _("       ttl pr checksum ")
270   _(0x0008)
271   __ XX __ __
272   _("        src address    ")
273   _(0x000C)
274   XX XX XX XX
275   _("        dst address    ")
276   _(0x0010)
277   XX XX XX XX
278   _("L4 T/U  sport dport    ")
279   _(tcpudp)
280   XX XX XX XX
281   _(padpad)
282   __ __ __ __
283   _(padpad)
284   __ __ __ __
285   _(padeth)
286   __ __;
287
288      u8 ip6_5tuple_mask[] =
289        _("             dmac               smac            etype ")
290   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
291   _("        v  tc + flow ")
292   _(0x0000) __ __ __ __
293   _("        plen  nh hl  ")
294   _(0x0004) __ __ XX __
295   _("        src address  ")
296   _(0x0008) XX XX XX XX
297   _(0x000C) XX XX XX XX
298   _(0x0010) XX XX XX XX
299   _(0x0014) XX XX XX XX
300   _("        dst address  ")
301   _(0x0018) XX XX XX XX
302   _(0x001C) XX XX XX XX
303   _(0x0020) XX XX XX XX
304   _(0x0024) XX XX XX XX
305   _("L4T/U  sport dport   ")
306   _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
307
308 #undef XX
309 #undef __
310 #undef _
311 #undef v
312
313      static int count_skip (u8 * p, u32 size)
314 {
315   u64 *p64 = (u64 *) p;
316   /* Be tolerant to null pointer */
317   if (0 == p)
318     return 0;
319
320   while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
321     {
322       p64++;
323     }
324   return (p64 - (u64 *) p) / 2;
325 }
326
327 static int
328 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
329                             u32 mask_len, u32 next_table_index,
330                             u32 miss_next_index, u32 * table_index,
331                             int is_add)
332 {
333   u32 nbuckets = 1;
334   u32 memory_size = 2 << 13;
335   u32 skip = count_skip (mask, mask_len);
336   u32 match = (mask_len / 16) - skip;
337   u8 *skip_mask_ptr = mask + 16 * skip;
338   u32 current_data_flag = 0;
339   int current_data_offset = 0;
340
341   if (0 == match)
342     match = 1;
343
344   return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
345                                       memory_size, skip, match,
346                                       next_table_index, miss_next_index,
347                                       table_index, current_data_flag,
348                                       current_data_offset, is_add,
349                                       1 /* delete_chain */);
350 }
351
352 static int
353 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
354                             u32 mask_len, u32 next_table_index,
355                             u32 miss_next_index, u32 * table_index,
356                             int is_add)
357 {
358   u32 nbuckets = 32;
359   u32 memory_size = 2 << 20;
360   u32 skip = count_skip (mask, mask_len);
361   u32 match = (mask_len / 16) - skip;
362   u8 *skip_mask_ptr = mask + 16 * skip;
363   u32 current_data_flag = 0;
364   int current_data_offset = 0;
365
366   if (0 == match)
367     match = 1;
368
369   return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
370                                       memory_size, skip, match,
371                                       next_table_index, miss_next_index,
372                                       table_index, current_data_flag,
373                                       current_data_offset, is_add,
374                                       1 /* delete_chain */);
375 }
376
377
378 static int
379 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
380 {
381   vnet_classify_main_t *cm = &vnet_classify_main;
382   u32 ip4_table_index = ~0;
383   u32 ip6_table_index = ~0;
384
385   vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
386                            sw_if_index, ~0);
387   vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
388                            sw_if_index, ~0);
389
390   vnet_l2_input_classify_enable_disable (sw_if_index, 0);
391
392   if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
393     {
394       ip4_table_index =
395         am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
396       am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
397       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
398                                   sizeof (ip4_5tuple_mask) - 1, ~0,
399                                   am->l2_input_classify_next_acl_ip4,
400                                   &ip4_table_index, 0);
401     }
402   if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
403     {
404       ip6_table_index =
405         am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
406       am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
407       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
408                                   sizeof (ip6_5tuple_mask) - 1, ~0,
409                                   am->l2_input_classify_next_acl_ip6,
410                                   &ip6_table_index, 0);
411     }
412
413   return 0;
414 }
415
416 static int
417 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
418 {
419   vnet_classify_main_t *cm = &vnet_classify_main;
420   u32 ip4_table_index = ~0;
421   u32 ip6_table_index = ~0;
422
423   vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
424                            sw_if_index, ~0);
425   vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
426                            sw_if_index, ~0);
427
428   vnet_l2_output_classify_enable_disable (sw_if_index, 0);
429
430   if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
431     {
432       ip4_table_index =
433         am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
434       am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
435       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
436                                   sizeof (ip4_5tuple_mask) - 1, ~0,
437                                   am->l2_output_classify_next_acl_ip4,
438                                   &ip4_table_index, 0);
439     }
440   if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
441     {
442       ip6_table_index =
443         am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
444       am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
445       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
446                                   sizeof (ip6_5tuple_mask) - 1, ~0,
447                                   am->l2_output_classify_next_acl_ip6,
448                                   &ip6_table_index, 0);
449     }
450
451   return 0;
452 }
453
454 static int
455 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
456 {
457   vnet_classify_main_t *cm = &vnet_classify_main;
458   u32 ip4_table_index = ~0;
459   u32 ip6_table_index = ~0;
460   int rv;
461
462   /* in case there were previous tables attached */
463   acl_unhook_l2_input_classify (am, sw_if_index);
464   rv =
465     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
466                                 sizeof (ip4_5tuple_mask) - 1, ~0,
467                                 am->l2_input_classify_next_acl_ip4,
468                                 &ip4_table_index, 1);
469   if (rv)
470     return rv;
471   rv =
472     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
473                                 sizeof (ip6_5tuple_mask) - 1, ~0,
474                                 am->l2_input_classify_next_acl_ip6,
475                                 &ip6_table_index, 1);
476   if (rv)
477     {
478       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
479                                   sizeof (ip4_5tuple_mask) - 1, ~0,
480                                   am->l2_input_classify_next_acl_ip4,
481                                   &ip4_table_index, 0);
482       return rv;
483     }
484   rv =
485     vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
486                                        ip6_table_index, ~0);
487   clib_warning
488     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
489      sw_if_index, ip4_table_index, ip6_table_index);
490   if (rv)
491     {
492       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
493                                   sizeof (ip6_5tuple_mask) - 1, ~0,
494                                   am->l2_input_classify_next_acl_ip6,
495                                   &ip6_table_index, 0);
496       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
497                                   sizeof (ip4_5tuple_mask) - 1, ~0,
498                                   am->l2_input_classify_next_acl_ip4,
499                                   &ip4_table_index, 0);
500       return rv;
501     }
502
503   am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
504     ip4_table_index;
505   am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
506     ip6_table_index;
507
508   vnet_l2_input_classify_enable_disable (sw_if_index, 1);
509   return rv;
510 }
511
512 static int
513 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
514 {
515   vnet_classify_main_t *cm = &vnet_classify_main;
516   u32 ip4_table_index = ~0;
517   u32 ip6_table_index = ~0;
518   int rv;
519
520   /* in case there were previous tables attached */
521   acl_unhook_l2_output_classify (am, sw_if_index);
522   rv =
523     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
524                                 sizeof (ip4_5tuple_mask) - 1, ~0,
525                                 am->l2_output_classify_next_acl_ip4,
526                                 &ip4_table_index, 1);
527   if (rv)
528     return rv;
529   rv =
530     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
531                                 sizeof (ip6_5tuple_mask) - 1, ~0,
532                                 am->l2_output_classify_next_acl_ip6,
533                                 &ip6_table_index, 1);
534   if (rv)
535     {
536       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
537                                   sizeof (ip4_5tuple_mask) - 1, ~0,
538                                   am->l2_output_classify_next_acl_ip4,
539                                   &ip4_table_index, 0);
540       return rv;
541     }
542   rv =
543     vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
544                                         ip6_table_index, ~0);
545   clib_warning
546     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
547      sw_if_index, ip4_table_index, ip6_table_index);
548   if (rv)
549     {
550       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
551                                   sizeof (ip6_5tuple_mask) - 1, ~0,
552                                   am->l2_output_classify_next_acl_ip6,
553                                   &ip6_table_index, 0);
554       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
555                                   sizeof (ip4_5tuple_mask) - 1, ~0,
556                                   am->l2_output_classify_next_acl_ip4,
557                                   &ip4_table_index, 0);
558       return rv;
559     }
560
561   am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
562     ip4_table_index;
563   am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
564     ip6_table_index;
565
566   vnet_l2_output_classify_enable_disable (sw_if_index, 1);
567   return rv;
568 }
569
570
571
572 int
573 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
574                                  int enable_disable)
575 {
576   int rv;
577
578   /* Utterly wrong? */
579   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
580                           sw_if_index))
581     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
582
583   acl_fa_enable_disable(sw_if_index, 1, enable_disable);
584
585   if (enable_disable)
586     {
587       rv = acl_hook_l2_input_classify (am, sw_if_index);
588     }
589   else
590     {
591       rv = acl_unhook_l2_input_classify (am, sw_if_index);
592     }
593
594   return rv;
595 }
596
597 int
598 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
599                                   int enable_disable)
600 {
601   int rv;
602
603   /* Utterly wrong? */
604   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
605                           sw_if_index))
606     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
607
608   acl_fa_enable_disable(sw_if_index, 0, enable_disable);
609
610   if (enable_disable)
611     {
612       rv = acl_hook_l2_output_classify (am, sw_if_index);
613     }
614   else
615     {
616       rv = acl_unhook_l2_output_classify (am, sw_if_index);
617     }
618
619   return rv;
620 }
621
622
623 static int
624 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
625 {
626   acl_main_t *am = &acl_main;
627   if (is_input)
628     {
629       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
630       vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
631                1);
632       acl_interface_in_enable_disable (am, sw_if_index, 1);
633     }
634   else
635     {
636       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
637       vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
638                &acl_list_index, 1);
639       acl_interface_out_enable_disable (am, sw_if_index, 1);
640     }
641   return 0;
642 }
643
644 static int
645 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
646 {
647   acl_main_t *am = &acl_main;
648   int i;
649   int rv = -1;
650   if (is_input)
651     {
652       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
653       for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
654            i++)
655         {
656           if (acl_list_index ==
657               am->input_acl_vec_by_sw_if_index[sw_if_index][i])
658             {
659               vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
660               rv = 0;
661               break;
662             }
663         }
664       if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
665         {
666           acl_interface_in_enable_disable (am, sw_if_index, 0);
667         }
668     }
669   else
670     {
671       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
672       for (i = 0;
673            i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
674         {
675           if (acl_list_index ==
676               am->output_acl_vec_by_sw_if_index[sw_if_index][i])
677             {
678               vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
679               rv = 0;
680               break;
681             }
682         }
683       if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
684         {
685           acl_interface_out_enable_disable (am, sw_if_index, 0);
686         }
687     }
688   return rv;
689 }
690
691 static void
692 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
693 {
694   acl_main_t *am = &acl_main;
695   if (is_input)
696     {
697       acl_interface_in_enable_disable (am, sw_if_index, 0);
698       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
699       vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
700     }
701   else
702     {
703       acl_interface_out_enable_disable (am, sw_if_index, 0);
704       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
705       vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
706     }
707 }
708
709 static int
710 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
711                                  u32 acl_list_index)
712 {
713   int rv = -1;
714   if (is_add)
715     {
716       rv =
717         acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
718     }
719   else
720     {
721       rv =
722         acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
723     }
724   return rv;
725 }
726
727
728 typedef struct
729 {
730   u8 is_ipv6;
731   u8 mac_mask[6];
732   u8 prefix_len;
733   u32 count;
734   u32 table_index;
735   u32 arp_table_index;
736 } macip_match_type_t;
737
738 static u32
739 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
740                        u8 is_ipv6)
741 {
742   u32 i;
743   if (mv)
744     {
745       for (i = 0; i < vec_len (mv); i++)
746         {
747           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
748               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
749             {
750               return i;
751             }
752         }
753     }
754   return ~0;
755 }
756
757
758 /* Get metric used to sort match types.
759    The more specific and the more often seen - the bigger the metric */
760 static int
761 match_type_metric (macip_match_type_t * m)
762 {
763   /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
764   return m->prefix_len + m->is_ipv6 + 10 * m->count;
765 }
766
767 static int
768 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
769 {
770   /* Ascending sort based on the metric values */
771   return match_type_metric (m1) - match_type_metric (m2);
772 }
773
774 /* Get the offset of L3 source within ethernet packet */
775 static int
776 get_l3_src_offset(int is6)
777 {
778   if(is6)
779     return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
780   else
781     return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
782 }
783
784 static int
785 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
786 {
787   macip_match_type_t *mvec = NULL;
788   macip_match_type_t *mt;
789   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
790   int i;
791   u32 match_type_index;
792   u32 last_table;
793   u8 mask[5 * 16];
794   vnet_classify_main_t *cm = &vnet_classify_main;
795
796   /* Count the number of different types of rules */
797   for (i = 0; i < a->count; i++)
798     {
799       if (~0 ==
800           (match_type_index =
801            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
802                                   a->rules[i].src_prefixlen,
803                                   a->rules[i].is_ipv6)))
804         {
805           match_type_index = vec_len (mvec);
806           vec_validate (mvec, match_type_index);
807           memcpy (mvec[match_type_index].mac_mask,
808                   a->rules[match_type_index].src_mac_mask, 6);
809           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
810           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
811           mvec[match_type_index].table_index = ~0;
812         }
813       mvec[match_type_index].count++;
814     }
815   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
816   vec_sort_with_function (mvec, match_type_compare);
817   /* Create the classifier tables */
818   last_table = ~0;
819   /* First add ARP tables */
820   vec_foreach (mt, mvec)
821   {
822     int mask_len;
823     int is6 = mt->is_ipv6;
824
825     mt->arp_table_index = ~0;
826     if (!is6)
827       {
828         memset (mask, 0, sizeof (mask));
829         memcpy (&mask[6], mt->mac_mask, 6);
830         memset (&mask[12], 0xff, 2); /* ethernet protocol */
831         memcpy (&mask[14 + 8], mt->mac_mask, 6);
832
833         for (i = 0; i < (mt->prefix_len / 8); i++)
834           mask[14 + 14 + i] = 0xff;
835         if (mt->prefix_len % 8)
836           mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
837
838         mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
839                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
840         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
841                                (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
842                                1);
843         last_table = mt->arp_table_index;
844       }
845   }
846   /* Now add IP[46] tables */
847   vec_foreach (mt, mvec)
848   {
849     int mask_len;
850     int is6 = mt->is_ipv6;
851     int l3_src_offs = get_l3_src_offset(is6);
852     memset (mask, 0, sizeof (mask));
853     memcpy (&mask[6], mt->mac_mask, 6);
854     for (i = 0; i < (mt->prefix_len / 8); i++)
855       {
856         mask[l3_src_offs + i] = 0xff;
857       }
858     if (mt->prefix_len % 8)
859       {
860         mask[l3_src_offs + (mt->prefix_len / 8)] =
861           0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
862       }
863     /*
864      * Round-up the number of bytes needed to store the prefix,
865      * and round up the number of vectors too
866      */
867     mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
868                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
869     acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
870                                 (~0 == last_table) ? 0 : ~0, &mt->table_index,
871                                 1);
872     last_table = mt->table_index;
873   }
874   a->ip4_table_index = ~0;
875   a->ip6_table_index = ~0;
876   a->l2_table_index = last_table;
877
878   /* Populate the classifier tables with rules from the MACIP ACL */
879   for (i = 0; i < a->count; i++)
880     {
881       u32 action = 0;
882       u32 metadata = 0;
883       int is6 = a->rules[i].is_ipv6;
884       int l3_src_offs = get_l3_src_offset(is6);
885       memset (mask, 0, sizeof (mask));
886       memcpy (&mask[6], a->rules[i].src_mac, 6);
887       memset (&mask[12], 0xff, 2); /* ethernet protocol */
888       if (is6)
889         {
890           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
891           mask[12] = 0x86;
892           mask[13] = 0xdd;
893         }
894       else
895         {
896           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
897           mask[12] = 0x08;
898           mask[13] = 0x00;
899         }
900       match_type_index =
901         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
902                                a->rules[i].src_prefixlen,
903                                a->rules[i].is_ipv6);
904       /* add session to table mvec[match_type_index].table_index; */
905       vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
906                                      mask, a->rules[i].is_permit ? ~0 : 0, i,
907                                      0, action, metadata, 1);
908       /* add ARP table entry too */
909       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
910         {
911           memset (mask, 0, sizeof (mask));
912           memcpy (&mask[6], a->rules[i].src_mac, 6);
913           mask[12] = 0x08;
914           mask[13] = 0x06;
915           memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
916           memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
917           vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
918                                     mask, a->rules[i].is_permit ? ~0 : 0, i,
919                                     0, action, metadata, 1);
920         }
921     }
922   return 0;
923 }
924
925 static void
926 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
927 {
928   vnet_classify_main_t *cm = &vnet_classify_main;
929   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
930
931   if (a->ip4_table_index != ~0)
932     {
933       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
934       a->ip4_table_index = ~0;
935     }
936   if (a->ip6_table_index != ~0)
937     {
938       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
939       a->ip6_table_index = ~0;
940     }
941   if (a->l2_table_index != ~0)
942     {
943       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
944       a->l2_table_index = ~0;
945     }
946 }
947
948 static int
949 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
950                     u32 * acl_list_index, u8 * tag)
951 {
952   acl_main_t *am = &acl_main;
953   macip_acl_list_t *a;
954   macip_acl_rule_t *r;
955   macip_acl_rule_t *acl_new_rules;
956   int i;
957
958   /* Create and populate the rules */
959   acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
960                                           CLIB_CACHE_LINE_BYTES);
961   if (!acl_new_rules)
962     {
963       /* Could not allocate rules. New or existing ACL - bail out regardless */
964       return -1;
965     }
966
967   for (i = 0; i < count; i++)
968     {
969       r = &acl_new_rules[i];
970       r->is_permit = rules[i].is_permit;
971       r->is_ipv6 = rules[i].is_ipv6;
972       memcpy (&r->src_mac, rules[i].src_mac, 6);
973       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
974       if(rules[i].is_ipv6)
975         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
976       else
977         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
978       r->src_prefixlen = rules[i].src_ip_prefix_len;
979     }
980
981   /* Get ACL index */
982   pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
983   memset (a, 0, sizeof (*a));
984   /* Will return the newly allocated ACL index */
985   *acl_list_index = a - am->macip_acls;
986
987   a->rules = acl_new_rules;
988   a->count = count;
989   memcpy (a->tag, tag, sizeof (a->tag));
990
991   /* Create and populate the classifer tables */
992   macip_create_classify_tables (am, *acl_list_index);
993
994   return 0;
995 }
996
997
998 /* No check for validity of sw_if_index - the callers were supposed to validate */
999
1000 static int
1001 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1002 {
1003   int rv;
1004   u32 macip_acl_index;
1005   macip_acl_list_t *a;
1006   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1007   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1008   /* No point in deleting MACIP ACL which is not applied */
1009   if (~0 == macip_acl_index)
1010     return -1;
1011   a = &am->macip_acls[macip_acl_index];
1012   /* remove the classifier tables off the interface L2 ACL */
1013   rv =
1014     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1015                               a->ip6_table_index, a->l2_table_index, 0);
1016   /* Unset the MACIP ACL index */
1017   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1018   return rv;
1019 }
1020
1021 /* No check for validity of sw_if_index - the callers were supposed to validate */
1022
1023 static int
1024 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1025                              u32 macip_acl_index)
1026 {
1027   macip_acl_list_t *a;
1028   int rv;
1029   if (pool_is_free_index (am->macip_acls, macip_acl_index))
1030     {
1031       return -1;
1032     }
1033   a = &am->macip_acls[macip_acl_index];
1034   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1035   /* If there already a MACIP ACL applied, unapply it */
1036   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1037     macip_acl_interface_del_acl(am, sw_if_index);
1038   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1039   /* Apply the classifier tables for L2 ACLs */
1040   rv =
1041     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1042                               a->ip6_table_index, a->l2_table_index, 1);
1043   return rv;
1044 }
1045
1046 static int
1047 macip_acl_del_list (u32 acl_list_index)
1048 {
1049   acl_main_t *am = &acl_main;
1050   macip_acl_list_t *a;
1051   int i;
1052   if (pool_is_free_index (am->macip_acls, acl_list_index))
1053     {
1054       return -1;
1055     }
1056
1057   /* delete any references to the ACL */
1058   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1059     {
1060       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1061         {
1062           macip_acl_interface_del_acl (am, i);
1063         }
1064     }
1065
1066   /* Now that classifier tables are detached, clean them up */
1067   macip_destroy_classify_tables (am, acl_list_index);
1068
1069   /* now we can delete the ACL itself */
1070   a = &am->macip_acls[acl_list_index];
1071   if (a->rules)
1072     {
1073       clib_mem_free (a->rules);
1074     }
1075   pool_put (am->macip_acls, a);
1076   return 0;
1077 }
1078
1079
1080 static int
1081 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1082                                  u32 acl_list_index)
1083 {
1084   acl_main_t *am = &acl_main;
1085   int rv = -1;
1086   if (is_add)
1087     {
1088       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1089     }
1090   else
1091     {
1092       rv = macip_acl_interface_del_acl (am, sw_if_index);
1093     }
1094   return rv;
1095 }
1096
1097 /*
1098  * If the client does not allocate enough memory for a variable-length
1099  * message, and then proceed to use it as if the full memory allocated,
1100  * absent the check we happily consume that on the VPP side, and go
1101  * along as if nothing happened. However, the resulting
1102  * effects range from just garbage in the API decode
1103  * (because the decoder snoops too far), to potential memory
1104  * corruptions.
1105  *
1106  * This verifies that the actual length of the message is
1107  * at least expected_len, and complains loudly if it is not.
1108  *
1109  * A failing check here is 100% a software bug on the API user side,
1110  * so we might as well yell.
1111  *
1112  */
1113 static int verify_message_len(void *mp, u32 expected_len, char *where)
1114 {
1115   u32 supplied_len = vl_msg_api_get_msg_length (mp);
1116   if (supplied_len < expected_len) {
1117       clib_warning("%s: Supplied message length %d is less than expected %d",
1118                    where, supplied_len, expected_len);
1119       return 0;
1120   } else {
1121       return 1;
1122   }
1123 }
1124
1125 /* API message handler */
1126 static void
1127 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1128 {
1129   vl_api_acl_add_replace_reply_t *rmp;
1130   acl_main_t *am = &acl_main;
1131   int rv;
1132   u32 acl_list_index = ntohl (mp->acl_index);
1133   u32 acl_count = ntohl (mp->count);
1134   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1135
1136   if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1137       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1138   } else {
1139       rv = VNET_API_ERROR_INVALID_VALUE;
1140   }
1141
1142   /* *INDENT-OFF* */
1143   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1144   ({
1145     rmp->acl_index = htonl(acl_list_index);
1146   }));
1147   /* *INDENT-ON* */
1148 }
1149
1150 static void
1151 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1152 {
1153   acl_main_t *am = &acl_main;
1154   vl_api_acl_del_reply_t *rmp;
1155   int rv;
1156
1157   rv = acl_del_list (ntohl (mp->acl_index));
1158
1159   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1160 }
1161
1162 static void
1163 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1164 {
1165   acl_main_t *am = &acl_main;
1166   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1167   u32 sw_if_index = ntohl (mp->sw_if_index);
1168   vl_api_acl_interface_add_del_reply_t *rmp;
1169   int rv = -1;
1170
1171   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1172     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1173   else
1174     rv =
1175       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1176                                      mp->is_input, ntohl (mp->acl_index));
1177
1178   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1179 }
1180
1181 static void
1182 vl_api_acl_interface_set_acl_list_t_handler
1183   (vl_api_acl_interface_set_acl_list_t * mp)
1184 {
1185   acl_main_t *am = &acl_main;
1186   vl_api_acl_interface_set_acl_list_reply_t *rmp;
1187   int rv = 0;
1188   int i;
1189   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1190   u32 sw_if_index = ntohl (mp->sw_if_index);
1191
1192   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1193     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1194   else
1195     {
1196       acl_interface_reset_inout_acls (sw_if_index, 0);
1197       acl_interface_reset_inout_acls (sw_if_index, 1);
1198
1199       for (i = 0; i < mp->count; i++)
1200         {
1201           acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1202                                        ntohl (mp->acls[i]));
1203         }
1204     }
1205
1206   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1207 }
1208
1209 static void
1210 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1211 {
1212   api_rule->is_permit = r->is_permit;
1213   api_rule->is_ipv6 = r->is_ipv6;
1214   if(r->is_ipv6)
1215     {
1216       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1217       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1218     }
1219   else
1220     {
1221       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1222       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1223     }
1224   api_rule->src_ip_prefix_len = r->src_prefixlen;
1225   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1226   api_rule->proto = r->proto;
1227   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1228   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1229   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1230   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1231   api_rule->tcp_flags_mask = r->tcp_flags_mask;
1232   api_rule->tcp_flags_value = r->tcp_flags_value;
1233 }
1234
1235 static void
1236 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1237                   acl_list_t * acl, u32 context)
1238 {
1239   vl_api_acl_details_t *mp;
1240   vl_api_acl_rule_t *rules;
1241   int i;
1242   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1243
1244   mp = vl_msg_api_alloc (msg_size);
1245   memset (mp, 0, msg_size);
1246   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1247
1248   /* fill in the message */
1249   mp->context = context;
1250   mp->count = htonl (acl->count);
1251   mp->acl_index = htonl (acl - am->acls);
1252   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1253   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1254   rules = mp->r;
1255   for (i = 0; i < acl->count; i++)
1256     {
1257       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1258     }
1259
1260   clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1261   vl_msg_api_send_shmem (q, (u8 *) & mp);
1262 }
1263
1264
1265 static void
1266 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1267 {
1268   acl_main_t *am = &acl_main;
1269   u32 acl_index;
1270   acl_list_t *acl;
1271
1272   int rv = -1;
1273   unix_shared_memory_queue_t *q;
1274
1275   q = vl_api_client_index_to_input_queue (mp->client_index);
1276   if (q == 0)
1277     {
1278       return;
1279     }
1280
1281   if (mp->acl_index == ~0)
1282     {
1283     /* *INDENT-OFF* */
1284     /* Just dump all ACLs */
1285     pool_foreach (acl, am->acls,
1286     ({
1287       send_acl_details(am, q, acl, mp->context);
1288     }));
1289     /* *INDENT-ON* */
1290     }
1291   else
1292     {
1293       acl_index = ntohl (mp->acl_index);
1294       if (!pool_is_free_index (am->acls, acl_index))
1295         {
1296           acl = &am->acls[acl_index];
1297           send_acl_details (am, q, acl, mp->context);
1298         }
1299     }
1300
1301   if (rv == -1)
1302     {
1303       /* FIXME API: should we signal an error here at all ? */
1304       return;
1305     }
1306 }
1307
1308 static void
1309 send_acl_interface_list_details (acl_main_t * am,
1310                                  unix_shared_memory_queue_t * q,
1311                                  u32 sw_if_index, u32 context)
1312 {
1313   vl_api_acl_interface_list_details_t *mp;
1314   int msg_size;
1315   int n_input;
1316   int n_output;
1317   int count;
1318   int i = 0;
1319
1320   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1321   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1322
1323   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1324   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1325   count = n_input + n_output;
1326
1327   msg_size = sizeof (*mp);
1328   msg_size += sizeof (mp->acls[0]) * count;
1329
1330   mp = vl_msg_api_alloc (msg_size);
1331   memset (mp, 0, msg_size);
1332   mp->_vl_msg_id =
1333     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1334
1335   /* fill in the message */
1336   mp->context = context;
1337   mp->sw_if_index = htonl (sw_if_index);
1338   mp->count = count;
1339   mp->n_input = n_input;
1340   for (i = 0; i < n_input; i++)
1341     {
1342       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1343     }
1344   for (i = 0; i < n_output; i++)
1345     {
1346       mp->acls[n_input + i] =
1347         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1348     }
1349
1350   vl_msg_api_send_shmem (q, (u8 *) & mp);
1351 }
1352
1353 static void
1354 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1355                                           mp)
1356 {
1357   acl_main_t *am = &acl_main;
1358   vnet_sw_interface_t *swif;
1359   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1360
1361   u32 sw_if_index;
1362   unix_shared_memory_queue_t *q;
1363
1364   q = vl_api_client_index_to_input_queue (mp->client_index);
1365   if (q == 0)
1366     {
1367       return;
1368     }
1369
1370   if (mp->sw_if_index == ~0)
1371     {
1372     /* *INDENT-OFF* */
1373     pool_foreach (swif, im->sw_interfaces,
1374     ({
1375       send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1376     }));
1377     /* *INDENT-ON* */
1378     }
1379   else
1380     {
1381       sw_if_index = ntohl (mp->sw_if_index);
1382       if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1383         send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1384     }
1385 }
1386
1387 /* MACIP ACL API handlers */
1388
1389 static void
1390 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1391 {
1392   vl_api_macip_acl_add_reply_t *rmp;
1393   acl_main_t *am = &acl_main;
1394   int rv;
1395   u32 acl_list_index = ~0;
1396   u32 acl_count = ntohl (mp->count);
1397   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1398
1399   if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1400       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1401   } else {
1402       rv = VNET_API_ERROR_INVALID_VALUE;
1403   }
1404
1405   /* *INDENT-OFF* */
1406   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1407   ({
1408     rmp->acl_index = htonl(acl_list_index);
1409   }));
1410   /* *INDENT-ON* */
1411 }
1412
1413 static void
1414 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1415 {
1416   acl_main_t *am = &acl_main;
1417   vl_api_macip_acl_del_reply_t *rmp;
1418   int rv;
1419
1420   rv = macip_acl_del_list (ntohl (mp->acl_index));
1421
1422   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1423 }
1424
1425 static void
1426 vl_api_macip_acl_interface_add_del_t_handler
1427   (vl_api_macip_acl_interface_add_del_t * mp)
1428 {
1429   acl_main_t *am = &acl_main;
1430   vl_api_macip_acl_interface_add_del_reply_t *rmp;
1431   int rv = -1;
1432   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1433   u32 sw_if_index = ntohl (mp->sw_if_index);
1434
1435   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1436     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1437   else
1438     rv =
1439       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1440                                      ntohl (mp->acl_index));
1441
1442   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1443 }
1444
1445 static void
1446 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1447                         macip_acl_list_t * acl, u32 context)
1448 {
1449   vl_api_macip_acl_details_t *mp;
1450   vl_api_macip_acl_rule_t *rules;
1451   macip_acl_rule_t *r;
1452   int i;
1453   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1454
1455   mp = vl_msg_api_alloc (msg_size);
1456   memset (mp, 0, msg_size);
1457   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1458
1459   /* fill in the message */
1460   mp->context = context;
1461   if (acl)
1462     {
1463       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1464       mp->count = htonl (acl->count);
1465       mp->acl_index = htonl (acl - am->macip_acls);
1466       rules = mp->r;
1467       for (i = 0; i < acl->count; i++)
1468         {
1469           r = &acl->rules[i];
1470           rules[i].is_permit = r->is_permit;
1471           rules[i].is_ipv6 = r->is_ipv6;
1472           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1473           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1474                   sizeof (r->src_mac_mask));
1475           if (r->is_ipv6)
1476             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1477                   sizeof (r->src_ip_addr.ip6));
1478           else
1479             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1480                   sizeof (r->src_ip_addr.ip4));
1481           rules[i].src_ip_prefix_len = r->src_prefixlen;
1482         }
1483     }
1484   else
1485     {
1486       /* No martini, no party - no ACL applied to this interface. */
1487       mp->acl_index = ~0;
1488       mp->count = 0;
1489     }
1490
1491   vl_msg_api_send_shmem (q, (u8 *) & mp);
1492 }
1493
1494
1495 static void
1496 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1497 {
1498   acl_main_t *am = &acl_main;
1499   macip_acl_list_t *acl;
1500
1501   unix_shared_memory_queue_t *q;
1502
1503   q = vl_api_client_index_to_input_queue (mp->client_index);
1504   if (q == 0)
1505     {
1506       return;
1507     }
1508
1509   if (mp->acl_index == ~0)
1510     {
1511       /* Just dump all ACLs for now, with sw_if_index = ~0 */
1512       pool_foreach (acl, am->macip_acls, (
1513                                            {
1514                                            send_macip_acl_details (am, q, acl,
1515                                                                    mp->
1516                                                                    context);}
1517                     ));
1518       /* *INDENT-ON* */
1519     }
1520   else
1521     {
1522       u32 acl_index = ntohl (mp->acl_index);
1523       if (!pool_is_free_index (am->macip_acls, acl_index))
1524         {
1525           acl = &am->macip_acls[acl_index];
1526           send_macip_acl_details (am, q, acl, mp->context);
1527         }
1528     }
1529 }
1530
1531 static void
1532 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1533                                           mp)
1534 {
1535   acl_main_t *am = &acl_main;
1536   vl_api_macip_acl_interface_get_reply_t *rmp;
1537   u32 count = vec_len (am->macip_acl_by_sw_if_index);
1538   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1539   unix_shared_memory_queue_t *q;
1540   int i;
1541
1542   q = vl_api_client_index_to_input_queue (mp->client_index);
1543   if (q == 0)
1544     {
1545       return;
1546     }
1547
1548   rmp = vl_msg_api_alloc (msg_size);
1549   memset (rmp, 0, msg_size);
1550   rmp->_vl_msg_id =
1551     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1552   rmp->context = mp->context;
1553   rmp->count = htonl (count);
1554   for (i = 0; i < count; i++)
1555     {
1556       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1557     }
1558
1559   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1560 }
1561
1562 /* Set up the API message handling tables */
1563 static clib_error_t *
1564 acl_plugin_api_hookup (vlib_main_t * vm)
1565 {
1566   acl_main_t *am = &acl_main;
1567 #define _(N,n)                                                  \
1568     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
1569                            #n,                                  \
1570                            vl_api_##n##_t_handler,              \
1571                            vl_noop_handler,                     \
1572                            vl_api_##n##_t_endian,               \
1573                            vl_api_##n##_t_print,                \
1574                            sizeof(vl_api_##n##_t), 1);
1575   foreach_acl_plugin_api_msg;
1576 #undef _
1577
1578   return 0;
1579 }
1580
1581 #define vl_msg_name_crc_list
1582 #include <acl/acl_all_api_h.h>
1583 #undef vl_msg_name_crc_list
1584
1585 static void
1586 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1587 {
1588 #define _(id,n,crc) \
1589   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1590   foreach_vl_msg_name_crc_acl;
1591 #undef _
1592 }
1593
1594 static void
1595 acl_setup_fa_nodes (void)
1596 {
1597   vlib_main_t *vm = vlib_get_main ();
1598   acl_main_t *am = &acl_main;
1599   vlib_node_t *n, *n4, *n6;
1600
1601   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1602   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1603   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1604
1605
1606   am->l2_input_classify_next_acl_ip4 =
1607     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1608   am->l2_input_classify_next_acl_ip6 =
1609     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1610
1611   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1612                                l2input_get_feat_names (),
1613                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1614
1615   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1616                                l2input_get_feat_names (),
1617                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1618
1619
1620   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1621   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1622   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1623
1624   am->l2_output_classify_next_acl_ip4 =
1625     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1626   am->l2_output_classify_next_acl_ip6 =
1627     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1628
1629   feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1630                                l2output_get_feat_names (),
1631                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1632
1633   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1634                                l2output_get_feat_names (),
1635                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1636 }
1637
1638 static void
1639 acl_set_timeout_sec(int timeout_type, u32 value)
1640 {
1641   acl_main_t *am = &acl_main;
1642   clib_time_t *ct = &am->vlib_main->clib_time;
1643
1644   if (timeout_type < ACL_N_TIMEOUTS) {
1645     am->session_timeout_sec[timeout_type] = value;
1646   } else {
1647     clib_warning("Unknown timeout type %d", timeout_type);
1648     return;
1649   }
1650   am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1651 }
1652
1653 static void
1654 acl_set_session_max_entries(u32 value)
1655 {
1656   acl_main_t *am = &acl_main;
1657   am->fa_conn_table_max_entries = value;
1658 }
1659
1660 static int
1661 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1662 {
1663   acl_main_t *am = &acl_main;
1664   if ((eh < 256) && (value < 2))
1665     {
1666       am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1667       return 1;
1668     }
1669   else
1670     return 0;
1671 }
1672
1673
1674 static clib_error_t *
1675 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1676 {
1677   acl_main_t *am = &acl_main;
1678   if (0 == is_add) {
1679     vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1680                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1681     /* also unapply any ACLs in case the users did not do so. */
1682     macip_acl_interface_del_acl(am, sw_if_index);
1683     acl_interface_reset_inout_acls (sw_if_index, 0);
1684     acl_interface_reset_inout_acls (sw_if_index, 1);
1685   }
1686   return 0;
1687 }
1688
1689 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1690
1691 static clib_error_t *
1692 acl_set_aclplugin_fn (vlib_main_t * vm,
1693                               unformat_input_t * input,
1694                               vlib_cli_command_t * cmd)
1695 {
1696   clib_error_t *error = 0;
1697   u32 timeout = 0;
1698   u32 val = 0;
1699   u32 eh_val = 0;
1700   uword memory_size = 0;
1701   acl_main_t *am = &acl_main;
1702
1703   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1704     if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1705       error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1706     }
1707     goto done;
1708   }
1709   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1710     {
1711       am->l4_match_nonfirst_fragment = (val != 0);
1712       goto done;
1713     }
1714   if (unformat (input, "session")) {
1715     if (unformat (input, "clear")) {
1716       acl_main_t *am = &acl_main;
1717       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1718                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
1719           goto done;
1720     }
1721     if (unformat (input, "table")) {
1722       /* The commands here are for tuning/testing. No user-serviceable parts inside */
1723       if (unformat (input, "max-entries")) {
1724         if (!unformat(input, "%u", &val)) {
1725           error = clib_error_return(0,
1726                                     "expecting maximum number of entries, got `%U`",
1727                                     format_unformat_error, input);
1728           goto done;
1729         } else {
1730           acl_set_session_max_entries(val);
1731           goto done;
1732         }
1733       }
1734       if (unformat (input, "hash-table-buckets")) {
1735         if (!unformat(input, "%u", &val)) {
1736           error = clib_error_return(0,
1737                                     "expecting maximum number of hash table buckets, got `%U`",
1738                                     format_unformat_error, input);
1739           goto done;
1740         } else {
1741           am->fa_conn_table_hash_num_buckets = val;
1742           goto done;
1743         }
1744       }
1745       if (unformat (input, "hash-table-memory")) {
1746         if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1747           error = clib_error_return(0,
1748                                     "expecting maximum amount of hash table memory, got `%U`",
1749                                     format_unformat_error, input);
1750           goto done;
1751         } else {
1752           am->fa_conn_table_hash_memory_size = memory_size;
1753           goto done;
1754         }
1755       }
1756       goto done;
1757     }
1758     if (unformat (input, "timeout")) {
1759       if (unformat(input, "udp")) {
1760         if(unformat(input, "idle")) {
1761           if (!unformat(input, "%u", &timeout)) {
1762             error = clib_error_return(0,
1763                                       "expecting timeout value in seconds, got `%U`",
1764                                       format_unformat_error, input);
1765             goto done;
1766           } else {
1767             acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
1768             goto done;
1769           }
1770         }
1771       }
1772       if (unformat(input, "tcp")) {
1773         if(unformat(input, "idle")) {
1774           if (!unformat(input, "%u", &timeout)) {
1775             error = clib_error_return(0,
1776                                       "expecting timeout value in seconds, got `%U`",
1777                                       format_unformat_error, input);
1778             goto done;
1779           } else {
1780             acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
1781             goto done;
1782           }
1783         }
1784         if(unformat(input, "transient")) {
1785           if (!unformat(input, "%u", &timeout)) {
1786             error = clib_error_return(0,
1787                                       "expecting timeout value in seconds, got `%U`",
1788                                       format_unformat_error, input);
1789             goto done;
1790           } else {
1791             acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
1792             goto done;
1793           }
1794         }
1795       }
1796       goto done;
1797     }
1798   }
1799 done:
1800   return error;
1801 }
1802
1803
1804 static clib_error_t *
1805 acl_show_aclplugin_fn (vlib_main_t * vm,
1806                               unformat_input_t * input,
1807                               vlib_cli_command_t * cmd)
1808 {
1809   clib_error_t *error = 0;
1810   acl_main_t *am = &acl_main;
1811   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1812
1813   vnet_sw_interface_t *swif;
1814
1815   if (unformat (input, "sessions"))
1816     {
1817       u8 * out0 = 0;
1818       u16 wk;
1819       pool_foreach (swif, im->sw_interfaces,
1820       ({
1821         u32 sw_if_index =  swif->sw_if_index;
1822         u64 n_adds = sw_if_index < vec_len(am->fa_session_adds_by_sw_if_index) ? am->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
1823         u64 n_dels = sw_if_index < vec_len(am->fa_session_dels_by_sw_if_index) ? am->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
1824         out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
1825       }));
1826       out0 = format(out0, "\n\nPer-worker data:\n");
1827       for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1828         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1829         out0 = format(out0, "Worker #%d:\n", wk);
1830         out0 = format(out0, "  Next expiry time: %lu\n", pw->next_expiry_time);
1831         out0 = format(out0, "  Requeue until time: %lu\n", pw->requeue_until_time);
1832         out0 = format(out0, "  Current time wait interval: %lu\n", pw->current_time_wait_interval);
1833         out0 = format(out0, "  Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
1834         out0 = format(out0, "  Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
1835         out0 = format(out0, "  Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
1836         out0 = format(out0, "  Swipe until this time: %lu\n", pw->swipe_end_time);
1837         out0 = format(out0, "  sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1838         out0 = format(out0, "  pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
1839         out0 = format(out0, "  clear in progress: %u\n", pw->clear_in_process);
1840         out0 = format(out0, "  interrupt is pending: %d\n", pw->interrupt_is_pending);
1841         out0 = format(out0, "  interrupt is needed: %d\n", pw->interrupt_is_needed);
1842         out0 = format(out0, "  interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
1843       }
1844       out0 = format(out0, "\n\nConn cleaner thread counters:\n");
1845 #define _(cnt, desc) out0 = format(out0, "             %20lu: %s\n", am->cnt, desc);
1846       foreach_fa_cleaner_counter;
1847 #undef _
1848       vlib_cli_output(vm, "\n\n%s\n\n", out0);
1849       vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
1850               am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
1851               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);
1852
1853       vec_free(out0);
1854     }
1855   return error;
1856 }
1857
1858
1859  /* *INDENT-OFF* */
1860 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
1861     .path = "set acl-plugin",
1862     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
1863     .function = acl_set_aclplugin_fn,
1864 };
1865
1866 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
1867     .path = "show acl-plugin",
1868     .short_help = "show acl-plugin sessions",
1869     .function = acl_show_aclplugin_fn,
1870 };
1871 /* *INDENT-ON* */
1872
1873
1874
1875 static clib_error_t *
1876 acl_init (vlib_main_t * vm)
1877 {
1878   acl_main_t *am = &acl_main;
1879   clib_error_t *error = 0;
1880   memset (am, 0, sizeof (*am));
1881   am->vlib_main = vm;
1882   am->vnet_main = vnet_get_main ();
1883
1884   u8 *name = format (0, "acl_%08x%c", api_version, 0);
1885
1886   /* Ask for a correctly-sized block of API message decode slots */
1887   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1888                                             VL_MSG_FIRST_AVAILABLE);
1889
1890   error = acl_plugin_api_hookup (vm);
1891
1892  /* Add our API messages to the global name_crc hash table */
1893   setup_message_id_table (am, &api_main);
1894
1895   vec_free (name);
1896
1897   acl_setup_fa_nodes();
1898   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
1899   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
1900   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
1901
1902   am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
1903   am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
1904   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
1905   vlib_thread_main_t *tm = vlib_get_thread_main ();
1906   vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
1907   {
1908     u16 wk;
1909     u8 tt;
1910     for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1911       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1912       vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
1913       vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
1914       for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
1915         pw->fa_conn_list_head[tt] = ~0;
1916         pw->fa_conn_list_tail[tt] = ~0;
1917       }
1918     }
1919   }
1920
1921   am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
1922   am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
1923   am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
1924
1925   am->fa_cleaner_cnt_delete_by_sw_index = 0;
1926   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
1927   am->fa_cleaner_cnt_unknown_event = 0;
1928   am->fa_cleaner_cnt_timer_restarted = 0;
1929   am->fa_cleaner_cnt_wait_with_timeout = 0;
1930
1931
1932 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
1933   foreach_acl_eh
1934 #undef _
1935
1936   am->l4_match_nonfirst_fragment = 1;
1937
1938   return error;
1939 }
1940
1941 VLIB_INIT_FUNCTION (acl_init);