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