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