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