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