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