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