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