api: remove transport specific code from handlers
[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/input_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           goto done;
2526         }
2527       if (unformat (input, "timeout"))
2528         {
2529           if (unformat (input, "udp"))
2530             {
2531               if (unformat (input, "idle"))
2532                 {
2533                   if (!unformat (input, "%u", &timeout))
2534                     {
2535                       error = clib_error_return (0,
2536                                                  "expecting timeout value in seconds, got `%U`",
2537                                                  format_unformat_error,
2538                                                  input);
2539                       goto done;
2540                     }
2541                   else
2542                     {
2543                       acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2544                       goto done;
2545                     }
2546                 }
2547             }
2548           if (unformat (input, "tcp"))
2549             {
2550               if (unformat (input, "idle"))
2551                 {
2552                   if (!unformat (input, "%u", &timeout))
2553                     {
2554                       error = clib_error_return (0,
2555                                                  "expecting timeout value in seconds, got `%U`",
2556                                                  format_unformat_error,
2557                                                  input);
2558                       goto done;
2559                     }
2560                   else
2561                     {
2562                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2563                       goto done;
2564                     }
2565                 }
2566               if (unformat (input, "transient"))
2567                 {
2568                   if (!unformat (input, "%u", &timeout))
2569                     {
2570                       error = clib_error_return (0,
2571                                                  "expecting timeout value in seconds, got `%U`",
2572                                                  format_unformat_error,
2573                                                  input);
2574                       goto done;
2575                     }
2576                   else
2577                     {
2578                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2579                                            timeout);
2580                       goto done;
2581                     }
2582                 }
2583             }
2584           goto done;
2585         }
2586     }
2587 done:
2588   return error;
2589 }
2590
2591 static u8 *
2592 my_format_mac_address (u8 * s, va_list * args)
2593 {
2594   u8 *a = va_arg (*args, u8 *);
2595   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2596                  a[0], a[1], a[2], a[3], a[4], a[5]);
2597 }
2598
2599 static inline u8 *
2600 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2601 {
2602   macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2603
2604   out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2605                 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2606                 format_ip46_address, &a->src_ip_addr,
2607                 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2608                 a->src_prefixlen,
2609                 my_format_mac_address, a->src_mac,
2610                 my_format_mac_address, a->src_mac_mask);
2611   return (out);
2612 }
2613
2614 static void
2615 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2616 {
2617   vlib_main_t *vm = am->vlib_main;
2618   int i;
2619
2620   /* Don't try to print someone else's memory */
2621   if (macip_acl_index > vec_len (am->macip_acls))
2622     return;
2623
2624   macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2625   int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2626
2627   vlib_cli_output (vm,
2628                    "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2629                    macip_acl_index, a->count, vec_len (a->rules), a->tag,
2630                    free_pool_slot);
2631   vlib_cli_output (vm,
2632                    "  ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2633                    a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2634   for (i = 0; i < vec_len (a->rules); i++)
2635     vlib_cli_output (vm, "    rule %d: %U\n", i,
2636                      my_macip_acl_rule_t_pretty_format,
2637                      vec_elt_at_index (a->rules, i));
2638
2639 }
2640
2641 static clib_error_t *
2642 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2643                                  unformat_input_t * input,
2644                                  vlib_cli_command_t * cmd)
2645 {
2646   clib_error_t *error = 0;
2647   acl_main_t *am = &acl_main;
2648   int i;
2649   for (i = 0; i < vec_len (am->macip_acls); i++)
2650     macip_acl_print (am, i);
2651   return error;
2652 }
2653
2654 static clib_error_t *
2655 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
2656                                        unformat_input_t * input,
2657                                        vlib_cli_command_t * cmd)
2658 {
2659   clib_error_t *error = 0;
2660   acl_main_t *am = &acl_main;
2661   int i;
2662   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2663     {
2664       vlib_cli_output (vm, "  sw_if_index %d: %d\n", i,
2665                        vec_elt (am->macip_acl_by_sw_if_index, i));
2666     }
2667   return error;
2668 }
2669
2670 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
2671 static void
2672 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
2673 {
2674   acl_rule_t *r;
2675   u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
2676                      am->acls[acl_index].count, am->acls[acl_index].tag);
2677   int j;
2678   PRINT_AND_RESET (vm, out0);
2679   for (j = 0; j < am->acls[acl_index].count; j++)
2680     {
2681       r = &am->acls[acl_index].rules[j];
2682       out0 = format (out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2683       out0 = format_acl_action (out0, r->is_permit);
2684       out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
2685                      r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2686                      r->src_prefixlen);
2687       out0 =
2688         format (out0, " dst %U/%d", format_ip46_address, &r->dst,
2689                 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
2690       out0 = format (out0, " proto %d", r->proto);
2691       out0 = format (out0, " sport %d", r->src_port_or_type_first);
2692       if (r->src_port_or_type_first != r->src_port_or_type_last)
2693         {
2694           out0 = format (out0, "-%d", r->src_port_or_type_last);
2695         }
2696       out0 = format (out0, " dport %d", r->dst_port_or_code_first);
2697       if (r->dst_port_or_code_first != r->dst_port_or_code_last)
2698         {
2699           out0 = format (out0, "-%d", r->dst_port_or_code_last);
2700         }
2701       if (r->tcp_flags_mask || r->tcp_flags_value)
2702         {
2703           out0 =
2704             format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
2705                     r->tcp_flags_mask);
2706         }
2707       out0 = format (out0, "\n");
2708       PRINT_AND_RESET (vm, out0);
2709     }
2710 }
2711
2712 #undef PRINT_AND_RESET
2713
2714 static void
2715 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
2716 {
2717   u32 i;
2718   vlib_main_t *vm = am->vlib_main;
2719
2720   for (i = 0; i < vec_len (am->acls); i++)
2721     {
2722       if (acl_is_not_defined (am, i))
2723         {
2724           /* don't attempt to show the ACLs that do not exist */
2725           continue;
2726         }
2727       if ((acl_index != ~0) && (acl_index != i))
2728         {
2729           continue;
2730         }
2731       acl_print_acl (vm, am, i);
2732
2733       if (i < vec_len (am->input_sw_if_index_vec_by_acl))
2734         {
2735           vlib_cli_output (vm, "  applied inbound on sw_if_index: %U\n",
2736                            format_vec32, am->input_sw_if_index_vec_by_acl[i],
2737                            "%d");
2738         }
2739       if (i < vec_len (am->output_sw_if_index_vec_by_acl))
2740         {
2741           vlib_cli_output (vm, "  applied outbound on sw_if_index: %U\n",
2742                            format_vec32, am->output_sw_if_index_vec_by_acl[i],
2743                            "%d");
2744         }
2745     }
2746 }
2747
2748 static clib_error_t *
2749 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
2750                            unformat_input_t * input, vlib_cli_command_t * cmd)
2751 {
2752   clib_error_t *error = 0;
2753   acl_main_t *am = &acl_main;
2754
2755   u32 acl_index = ~0;
2756   (void) unformat (input, "index %u", &acl_index);
2757
2758   acl_plugin_show_acl (am, acl_index);
2759   return error;
2760 }
2761
2762 static void
2763 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
2764 {
2765   vlib_main_t *vm = am->vlib_main;
2766   u32 swi;
2767   u32 *pj;
2768   for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
2769        (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
2770     {
2771       /* if we need a particular interface, skip all the others */
2772       if ((sw_if_index != ~0) && (sw_if_index != swi))
2773         continue;
2774
2775       vlib_cli_output (vm, "sw_if_index %d:\n", swi);
2776
2777       if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
2778           (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
2779         {
2780           vlib_cli_output (vm, "  input acl(s): %U", format_vec32,
2781                            am->input_acl_vec_by_sw_if_index[swi], "%d");
2782           if (show_acl)
2783             {
2784               vlib_cli_output (vm, "\n");
2785               vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
2786               {
2787                 acl_print_acl (vm, am, *pj);
2788               }
2789               vlib_cli_output (vm, "\n");
2790             }
2791         }
2792
2793       if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
2794           (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
2795         {
2796           vlib_cli_output (vm, "  output acl(s): %U", format_vec32,
2797                            am->output_acl_vec_by_sw_if_index[swi], "%d");
2798           if (show_acl)
2799             {
2800               vlib_cli_output (vm, "\n");
2801               vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
2802               {
2803                 acl_print_acl (vm, am, *pj);
2804               }
2805               vlib_cli_output (vm, "\n");
2806             }
2807         }
2808     }
2809
2810 }
2811
2812
2813 static clib_error_t *
2814 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
2815                                      unformat_input_t * input,
2816                                      vlib_cli_command_t * cmd)
2817 {
2818   clib_error_t *error = 0;
2819   u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
2820
2821   if (unformat
2822       (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
2823        &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
2824     vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
2825                      format_acl_plugin_5tuple, five_tuple);
2826   else
2827     error = clib_error_return (0, "expecting 6 hex integers");
2828   return error;
2829 }
2830
2831
2832 static clib_error_t *
2833 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
2834                                  unformat_input_t * input,
2835                                  vlib_cli_command_t * cmd)
2836 {
2837   clib_error_t *error = 0;
2838   acl_main_t *am = &acl_main;
2839
2840   u32 sw_if_index = ~0;
2841   (void) unformat (input, "sw_if_index %u", &sw_if_index);
2842   int show_acl = unformat (input, "acl");
2843
2844   acl_plugin_show_interface (am, sw_if_index, show_acl);
2845   return error;
2846 }
2847
2848 static clib_error_t *
2849 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
2850                               unformat_input_t * input,
2851                               vlib_cli_command_t * cmd)
2852 {
2853   clib_error_t *error = 0;
2854   acl_main_t *am = &acl_main;
2855
2856   vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2857   if (am->acl_mheap)
2858     {
2859       vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2860     }
2861   else
2862     {
2863       vlib_cli_output (vm, " Not initialized\n");
2864     }
2865   vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2866   if (am->hash_lookup_mheap)
2867     {
2868       vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2869     }
2870   else
2871     {
2872       vlib_cli_output (vm, " Not initialized\n");
2873     }
2874   return error;
2875 }
2876
2877 static void
2878 acl_plugin_show_sessions (acl_main_t * am,
2879                           u32 show_session_thread_id,
2880                           u32 show_session_session_index)
2881 {
2882   vlib_main_t *vm = am->vlib_main;
2883   u16 wk;
2884   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2885   vnet_sw_interface_t *swif;
2886
2887   {
2888     u64 n_adds = am->fa_session_total_adds;
2889     u64 n_dels = am->fa_session_total_dels;
2890     vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
2891                      n_dels, n_adds - n_dels);
2892   }
2893   vlib_cli_output (vm, "\n\nPer-thread data:");
2894   for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
2895     {
2896       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2897       vlib_cli_output (vm, "Thread #%d:", wk);
2898       if (show_session_thread_id == wk
2899           && show_session_session_index < pool_len (pw->fa_sessions_pool))
2900         {
2901           vlib_cli_output (vm, "  session index %u:",
2902                            show_session_session_index);
2903           fa_session_t *sess =
2904             pw->fa_sessions_pool + show_session_session_index;
2905           u64 *m = (u64 *) & sess->info;
2906           vlib_cli_output (vm,
2907                            "    info: %016llx %016llx %016llx %016llx %016llx %016llx",
2908                            m[0], m[1], m[2], m[3], m[4], m[5]);
2909           vlib_cli_output (vm, "    sw_if_index: %u", sess->sw_if_index);
2910           vlib_cli_output (vm, "    tcp_flags_seen: %x",
2911                            sess->tcp_flags_seen.as_u16);
2912           vlib_cli_output (vm, "    last active time: %lu",
2913                            sess->last_active_time);
2914           vlib_cli_output (vm, "    thread index: %u", sess->thread_index);
2915           vlib_cli_output (vm, "    link enqueue time: %lu",
2916                            sess->link_enqueue_time);
2917           vlib_cli_output (vm, "    link next index: %u",
2918                            sess->link_next_idx);
2919           vlib_cli_output (vm, "    link prev index: %u",
2920                            sess->link_prev_idx);
2921           vlib_cli_output (vm, "    link list id: %u", sess->link_list_id);
2922         }
2923       vlib_cli_output (vm, "  connection add/del stats:", wk);
2924       pool_foreach (swif, im->sw_interfaces, (
2925                                                {
2926                                                u32 sw_if_index =
2927                                                swif->sw_if_index;
2928                                                u64 n_adds =
2929                                                sw_if_index <
2930                                                vec_len
2931                                                (pw->fa_session_adds_by_sw_if_index)
2932                                                ?
2933                                                pw->fa_session_adds_by_sw_if_index
2934                                                [sw_if_index] : 0;
2935                                                u64 n_dels =
2936                                                sw_if_index <
2937                                                vec_len
2938                                                (pw->fa_session_dels_by_sw_if_index)
2939                                                ?
2940                                                pw->fa_session_dels_by_sw_if_index
2941                                                [sw_if_index] : 0;
2942                                                vlib_cli_output (vm,
2943                                                                 "    sw_if_index %d: add %lu - del %lu = %lu",
2944                                                                 sw_if_index,
2945                                                                 n_adds,
2946                                                                 n_dels,
2947                                                                 n_adds -
2948                                                                 n_dels);
2949                                                }
2950                     ));
2951
2952       vlib_cli_output (vm, "  connection timeout type lists:", wk);
2953       u8 tt = 0;
2954       for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
2955         {
2956           u32 head_session_index = pw->fa_conn_list_head[tt];
2957           vlib_cli_output (vm, "  fa_conn_list_head[%d]: %d", tt,
2958                            head_session_index);
2959           if (~0 != head_session_index)
2960             {
2961               fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2962               vlib_cli_output (vm, "    last active time: %lu",
2963                                sess->last_active_time);
2964               vlib_cli_output (vm, "    link enqueue time: %lu",
2965                                sess->link_enqueue_time);
2966             }
2967         }
2968
2969       vlib_cli_output (vm, "  Next expiry time: %lu", pw->next_expiry_time);
2970       vlib_cli_output (vm, "  Requeue until time: %lu",
2971                        pw->requeue_until_time);
2972       vlib_cli_output (vm, "  Current time wait interval: %lu",
2973                        pw->current_time_wait_interval);
2974       vlib_cli_output (vm, "  Count of deleted sessions: %lu",
2975                        pw->cnt_deleted_sessions);
2976       vlib_cli_output (vm, "  Delete already deleted: %lu",
2977                        pw->cnt_already_deleted_sessions);
2978       vlib_cli_output (vm, "  Session timers restarted: %lu",
2979                        pw->cnt_session_timer_restarted);
2980       vlib_cli_output (vm, "  Swipe until this time: %lu",
2981                        pw->swipe_end_time);
2982       vlib_cli_output (vm, "  sw_if_index serviced bitmap: %U",
2983                        format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2984       vlib_cli_output (vm, "  pending clear intfc bitmap : %U",
2985                        format_bitmap_hex,
2986                        pw->pending_clear_sw_if_index_bitmap);
2987       vlib_cli_output (vm, "  clear in progress: %u", pw->clear_in_process);
2988       vlib_cli_output (vm, "  interrupt is pending: %d",
2989                        pw->interrupt_is_pending);
2990       vlib_cli_output (vm, "  interrupt is needed: %d",
2991                        pw->interrupt_is_needed);
2992       vlib_cli_output (vm, "  interrupt is unwanted: %d",
2993                        pw->interrupt_is_unwanted);
2994       vlib_cli_output (vm, "  interrupt generation: %d",
2995                        pw->interrupt_generation);
2996     }
2997   vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
2998 #define _(cnt, desc) vlib_cli_output(vm, "             %20lu: %s", am->cnt, desc);
2999   foreach_fa_cleaner_counter;
3000 #undef _
3001   vlib_cli_output (vm, "Interrupt generation: %d",
3002                    am->fa_interrupt_generation);
3003   vlib_cli_output (vm,
3004                    "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3005                    am->fa_min_deleted_sessions_per_interval,
3006                    am->fa_max_deleted_sessions_per_interval,
3007                    am->fa_cleaner_wait_time_increment * 1000.0,
3008                    ((f64) am->fa_current_cleaner_timer_wait_interval) *
3009                    1000.0 / (f64) vm->clib_time.clocks_per_second);
3010 }
3011
3012 static clib_error_t *
3013 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3014                                 unformat_input_t * input,
3015                                 vlib_cli_command_t * cmd)
3016 {
3017   clib_error_t *error = 0;
3018   acl_main_t *am = &acl_main;
3019
3020   u32 show_bihash_verbose = 0;
3021   u32 show_session_thread_id = ~0;
3022   u32 show_session_session_index = ~0;
3023   (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3024                    &show_session_session_index);
3025   (void) unformat (input, "verbose %u", &show_bihash_verbose);
3026
3027   acl_plugin_show_sessions (am, show_session_thread_id,
3028                             show_session_session_index);
3029   show_fa_sessions_hash (vm, show_bihash_verbose);
3030   return error;
3031 }
3032
3033 static void
3034 acl_plugin_show_tables_mask_type (acl_main_t * am)
3035 {
3036   vlib_main_t *vm = am->vlib_main;
3037   ace_mask_type_entry_t *mte;
3038
3039   vlib_cli_output (vm, "Mask-type entries:");
3040     /* *INDENT-OFF* */
3041     pool_foreach(mte, am->ace_mask_type_pool,
3042     ({
3043       vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
3044                     mte - am->ace_mask_type_pool,
3045                     mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3046                     mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3047     }));
3048     /* *INDENT-ON* */
3049 }
3050
3051 static void
3052 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3053 {
3054   vlib_main_t *vm = am->vlib_main;
3055   u32 i, j;
3056   u64 *m;
3057   vlib_cli_output (vm, "Mask-ready ACL representations\n");
3058   for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3059     {
3060       if ((acl_index != ~0) && (acl_index != i))
3061         {
3062           continue;
3063         }
3064       hash_acl_info_t *ha = &am->hash_acl_infos[i];
3065       vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3066       vlib_cli_output (vm, "  applied  inbound on sw_if_index list: %U\n",
3067                        format_vec32, ha->inbound_sw_if_index_list, "%d");
3068       vlib_cli_output (vm, "  applied outbound on sw_if_index list: %U\n",
3069                        format_vec32, ha->outbound_sw_if_index_list, "%d");
3070       vlib_cli_output (vm, "  mask type index bitmap: %U\n",
3071                        format_bitmap_hex, ha->mask_type_index_bitmap);
3072       for (j = 0; j < vec_len (ha->rules); j++)
3073         {
3074           hash_ace_info_t *pa = &ha->rules[j];
3075           m = (u64 *) & pa->match;
3076           vlib_cli_output (vm,
3077                            "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3078                            j, m[0], m[1], m[2], m[3], m[4], m[5],
3079                            pa->mask_type_index, pa->acl_index, pa->ace_index,
3080                            pa->action, pa->src_portrange_not_powerof2,
3081                            pa->dst_portrange_not_powerof2);
3082         }
3083     }
3084 }
3085
3086 static void
3087 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3088 {
3089   vlib_cli_output (vm,
3090                    "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3091                    j, pae->acl_index, pae->ace_index, pae->action,
3092                    pae->hash_ace_info_index, pae->next_applied_entry_index,
3093                    pae->prev_applied_entry_index,
3094                    pae->tail_applied_entry_index, pae->hitcount);
3095 }
3096
3097 static void
3098 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3099 {
3100   vlib_main_t *vm = am->vlib_main;
3101   u32 swi, j;
3102   vlib_cli_output (vm, "Applied lookup entries for interfaces");
3103
3104   for (swi = 0;
3105        (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3106        || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3107        || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3108        || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3109     {
3110       if ((sw_if_index != ~0) && (sw_if_index != swi))
3111         {
3112           continue;
3113         }
3114       vlib_cli_output (vm, "sw_if_index %d:", swi);
3115       if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3116         {
3117           applied_hash_acl_info_t *pal =
3118             &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3119           vlib_cli_output (vm, "  input lookup mask_type_index_bitmap: %U",
3120                            format_bitmap_hex, pal->mask_type_index_bitmap);
3121           vlib_cli_output (vm, "  input applied acls: %U", format_vec32,
3122                            pal->applied_acls, "%d");
3123         }
3124       if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3125         {
3126           vlib_cli_output (vm, "  input lookup applied entries:");
3127           for (j = 0;
3128                j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3129                j++)
3130             {
3131               acl_plugin_print_pae (vm, j,
3132                                     &am->input_hash_entry_vec_by_sw_if_index
3133                                     [swi][j]);
3134             }
3135         }
3136
3137       if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3138         {
3139           applied_hash_acl_info_t *pal =
3140             &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3141           vlib_cli_output (vm, "  output lookup mask_type_index_bitmap: %U",
3142                            format_bitmap_hex, pal->mask_type_index_bitmap);
3143           vlib_cli_output (vm, "  output applied acls: %U", format_vec32,
3144                            pal->applied_acls, "%d");
3145         }
3146       if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3147         {
3148           vlib_cli_output (vm, "  output lookup applied entries:");
3149           for (j = 0;
3150                j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3151                j++)
3152             {
3153               acl_plugin_print_pae (vm, j,
3154                                     &am->output_hash_entry_vec_by_sw_if_index
3155                                     [swi][j]);
3156             }
3157         }
3158     }
3159 }
3160
3161 static void
3162 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3163 {
3164   vlib_main_t *vm = am->vlib_main;
3165   show_hash_acl_hash (vm, am, show_bihash_verbose);
3166 }
3167
3168 static clib_error_t *
3169 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3170                               unformat_input_t * input,
3171                               vlib_cli_command_t * cmd)
3172 {
3173   clib_error_t *error = 0;
3174   acl_main_t *am = &acl_main;
3175
3176   u32 acl_index = ~0;
3177   u32 sw_if_index = ~0;
3178   int show_acl_hash_info = 0;
3179   int show_applied_info = 0;
3180   int show_mask_type = 0;
3181   int show_bihash = 0;
3182   u32 show_bihash_verbose = 0;
3183
3184   if (unformat (input, "acl"))
3185     {
3186       show_acl_hash_info = 1;
3187       /* mask-type is handy to see as well right there */
3188       show_mask_type = 1;
3189       unformat (input, "index %u", &acl_index);
3190     }
3191   else if (unformat (input, "applied"))
3192     {
3193       show_applied_info = 1;
3194       unformat (input, "sw_if_index %u", &sw_if_index);
3195     }
3196   else if (unformat (input, "mask"))
3197     {
3198       show_mask_type = 1;
3199     }
3200   else if (unformat (input, "hash"))
3201     {
3202       show_bihash = 1;
3203       unformat (input, "verbose %u", &show_bihash_verbose);
3204     }
3205
3206   if (!
3207       (show_mask_type || show_acl_hash_info || show_applied_info
3208        || show_bihash))
3209     {
3210       /* if no qualifiers specified, show all */
3211       show_mask_type = 1;
3212       show_acl_hash_info = 1;
3213       show_applied_info = 1;
3214       show_bihash = 1;
3215     }
3216   if (show_mask_type)
3217     acl_plugin_show_tables_mask_type (am);
3218   if (show_acl_hash_info)
3219     acl_plugin_show_tables_acl_hash_info (am, acl_index);
3220   if (show_applied_info)
3221     acl_plugin_show_tables_applied_info (am, sw_if_index);
3222   if (show_bihash)
3223     acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3224
3225   return error;
3226 }
3227
3228 static clib_error_t *
3229 acl_clear_aclplugin_fn (vlib_main_t * vm,
3230                         unformat_input_t * input, vlib_cli_command_t * cmd)
3231 {
3232   clib_error_t *error = 0;
3233   acl_main_t *am = &acl_main;
3234   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3235                              ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3236   return error;
3237 }
3238
3239  /* *INDENT-OFF* */
3240 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3241     .path = "set acl-plugin",
3242     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3243     .function = acl_set_aclplugin_fn,
3244 };
3245
3246 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3247     .path = "show acl-plugin acl",
3248     .short_help = "show acl-plugin acl [index N]",
3249     .function = acl_show_aclplugin_acl_fn,
3250 };
3251
3252 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3253     .path = "show acl-plugin decode 5tuple",
3254     .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3255     .function = acl_show_aclplugin_decode_5tuple_fn,
3256 };
3257
3258 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3259     .path = "show acl-plugin interface",
3260     .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3261     .function = acl_show_aclplugin_interface_fn,
3262 };
3263
3264 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3265     .path = "show acl-plugin memory",
3266     .short_help = "show acl-plugin memory",
3267     .function = acl_show_aclplugin_memory_fn,
3268 };
3269
3270 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3271     .path = "show acl-plugin sessions",
3272     .short_help = "show acl-plugin sessions",
3273     .function = acl_show_aclplugin_sessions_fn,
3274 };
3275
3276 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3277     .path = "show acl-plugin tables",
3278     .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3279     .function = acl_show_aclplugin_tables_fn,
3280 };
3281
3282 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3283     .path = "show acl-plugin macip acl",
3284     .short_help = "show acl-plugin macip acl",
3285     .function = acl_show_aclplugin_macip_acl_fn,
3286 };
3287
3288 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3289     .path = "show acl-plugin macip interface",
3290     .short_help = "show acl-plugin macip interface",
3291     .function = acl_show_aclplugin_macip_interface_fn,
3292 };
3293
3294 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3295     .path = "clear acl-plugin sessions",
3296     .short_help = "clear acl-plugin sessions",
3297     .function = acl_clear_aclplugin_fn,
3298 };
3299 /* *INDENT-ON* */
3300
3301 static clib_error_t *
3302 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3303 {
3304   acl_main_t *am = &acl_main;
3305   u32 conn_table_hash_buckets;
3306   u32 conn_table_hash_memory_size;
3307   u32 conn_table_max_entries;
3308   u32 main_heap_size;
3309   u32 hash_heap_size;
3310   u32 hash_lookup_hash_buckets;
3311   u32 hash_lookup_hash_memory;
3312
3313   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3314     {
3315       if (unformat
3316           (input, "connection hash buckets %d", &conn_table_hash_buckets))
3317         am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3318       else if (unformat (input, "connection hash memory %d",
3319                          &conn_table_hash_memory_size))
3320         am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3321       else if (unformat (input, "connection count max %d",
3322                          &conn_table_max_entries))
3323         am->fa_conn_table_max_entries = conn_table_max_entries;
3324       else if (unformat (input, "main heap size %d", &main_heap_size))
3325         am->acl_mheap_size = main_heap_size;
3326       else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3327         am->hash_lookup_mheap_size = hash_heap_size;
3328       else if (unformat (input, "hash lookup hash buckets %d",
3329                          &hash_lookup_hash_buckets))
3330         am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3331       else if (unformat (input, "hash lookup hash memory %d",
3332                          &hash_lookup_hash_memory))
3333         am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3334       else
3335         return clib_error_return (0, "unknown input '%U'",
3336                                   format_unformat_error, input);
3337     }
3338   return 0;
3339 }
3340
3341 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3342
3343 static clib_error_t *
3344 acl_init (vlib_main_t * vm)
3345 {
3346   acl_main_t *am = &acl_main;
3347   clib_error_t *error = 0;
3348   memset (am, 0, sizeof (*am));
3349   am->vlib_main = vm;
3350   am->vnet_main = vnet_get_main ();
3351
3352   u8 *name = format (0, "acl_%08x%c", api_version, 0);
3353
3354   /* Ask for a correctly-sized block of API message decode slots */
3355   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3356                                             VL_MSG_FIRST_AVAILABLE);
3357
3358   error = acl_plugin_api_hookup (vm);
3359
3360   /* Add our API messages to the global name_crc hash table */
3361   setup_message_id_table (am, &api_main);
3362
3363   vec_free (name);
3364
3365   acl_setup_fa_nodes ();
3366
3367   am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3368   am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3369
3370   am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3371   am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3372
3373   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3374     TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3375   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3376     TCP_SESSION_IDLE_TIMEOUT_SEC;
3377   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3378     UDP_SESSION_IDLE_TIMEOUT_SEC;
3379
3380   am->fa_conn_table_hash_num_buckets =
3381     ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3382   am->fa_conn_table_hash_memory_size =
3383     ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3384   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3385   vlib_thread_main_t *tm = vlib_get_thread_main ();
3386   vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3387   {
3388     u16 wk;
3389     u8 tt;
3390     for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3391       {
3392         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3393         vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3394         vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3395         for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3396           {
3397             pw->fa_conn_list_head[tt] = ~0;
3398             pw->fa_conn_list_tail[tt] = ~0;
3399           }
3400       }
3401   }
3402
3403   am->fa_min_deleted_sessions_per_interval =
3404     ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3405   am->fa_max_deleted_sessions_per_interval =
3406     ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3407   am->fa_cleaner_wait_time_increment =
3408     ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3409
3410   am->fa_cleaner_cnt_delete_by_sw_index = 0;
3411   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3412   am->fa_cleaner_cnt_unknown_event = 0;
3413   am->fa_cleaner_cnt_timer_restarted = 0;
3414   am->fa_cleaner_cnt_wait_with_timeout = 0;
3415
3416
3417 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3418   foreach_acl_eh
3419 #undef _
3420     am->l4_match_nonfirst_fragment = 1;
3421
3422   /* use the new fancy hash-based matching */
3423   am->use_hash_acl_matching = 1;
3424
3425   return error;
3426 }
3427
3428 VLIB_INIT_FUNCTION (acl_init);
3429
3430
3431 /*
3432  * fd.io coding-style-patch-verification: ON
3433  *
3434  * Local Variables:
3435  * eval: (c-set-style "gnu")
3436  * End:
3437  */