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