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