9c3db8d81fd7af6ff857345882890de865681381
[vpp.git] / src / plugins / acl / fa_node.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 #include <stddef.h>
16 #include <netinet/in.h>
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
22 #include <acl/acl.h>
23 #include <vppinfra/bihash_40_8.h>
24
25 #include <vppinfra/bihash_template.h>
26 #include <vppinfra/bihash_template.c>
27
28 #include "fa_node.h"
29 #include "hash_lookup.h"
30
31 typedef struct
32 {
33   u32 next_index;
34   u32 sw_if_index;
35   u32 match_acl_in_index;
36   u32 match_rule_index;
37   u64 packet_info[6];
38   u32 trace_bitmap;
39   u8 action;
40 } acl_fa_trace_t;
41
42 static u8 *
43 format_fa_5tuple (u8 * s, va_list * args)
44 {
45   fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
46
47   return format(s, "%s sw_if_index %d (lsb16 %d) l3 %s%s %U -> %U"
48                    " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
49                 p5t->pkt.is_input ? "input" : "output",
50                 p5t->pkt.sw_if_index, p5t->l4.lsb_of_sw_if_index, p5t->pkt.is_ip6 ? "ip6" : "ip4",
51                 p5t->pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
52                 format_ip46_address, &p5t->addr[0], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
53                 format_ip46_address, &p5t->addr[1], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
54                 p5t->l4.proto, p5t->pkt.l4_valid,
55                 p5t->l4.port[0], p5t->l4.port[1],
56                 p5t->pkt.tcp_flags_valid ? "valid": "invalid",
57                 p5t->pkt.tcp_flags,
58                 p5t->pkt.flags_reserved);
59 }
60
61 /* packet trace format function */
62 static u8 *
63 format_acl_fa_trace (u8 * s, va_list * args)
64 {
65   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67   acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
68
69   s =
70     format (s,
71             "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
72             "  pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
73             t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
74             t->match_rule_index, t->trace_bitmap,
75             t->packet_info[0], t->packet_info[1], t->packet_info[2],
76             t->packet_info[3], t->packet_info[4], t->packet_info[5]);
77
78   /* Now also print out the packet_info in a form usable by humans */
79   s = format (s, "\n   %U", format_fa_5tuple, t->packet_info);
80   return s;
81 }
82
83 /* *INDENT-OFF* */
84 #define foreach_acl_fa_error \
85 _(ACL_DROP, "ACL deny packets")  \
86 _(ACL_PERMIT, "ACL permit packets")  \
87 _(ACL_NEW_SESSION, "new sessions added") \
88 _(ACL_EXIST_SESSION, "existing session packets") \
89 _(ACL_CHECK, "checked packets") \
90 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
91 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
92 /* end  of errors */
93
94 typedef enum
95 {
96 #define _(sym,str) ACL_FA_ERROR_##sym,
97   foreach_acl_fa_error
98 #undef _
99     ACL_FA_N_ERROR,
100 } acl_fa_error_t;
101
102 static char *acl_fa_error_strings[] = {
103 #define _(sym,string) string,
104   foreach_acl_fa_error
105 #undef _
106 };
107 /* *INDENT-ON* */
108
109 static void *
110 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
111 {
112   u8 *p = vlib_buffer_get_current (b0) + offset;
113   return p;
114 }
115
116
117 static int
118 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
119                    int prefixlen, int is_ip6)
120 {
121   if (prefixlen == 0)
122     {
123       /* match any always succeeds */
124       return 1;
125     }
126   if (is_ip6)
127     {
128       if (memcmp (addr1, addr2, prefixlen / 8))
129         {
130           /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
131           return 0;
132         }
133       if (prefixlen % 8)
134         {
135           u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
136           u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
137           u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
138           return (b1 & mask0) == b2;
139         }
140       else
141         {
142           /* The prefix fits into integer number of bytes, so nothing left to do */
143           return 1;
144         }
145     }
146   else
147     {
148       uint32_t a1 = ntohl (addr1->ip4.as_u32);
149       uint32_t a2 = ntohl (addr2->ip4.as_u32);
150       uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
151       return (a1 & mask0) == a2;
152     }
153 }
154
155 static int
156 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
157 {
158   return ((port >= port_first) && (port <= port_last));
159 }
160
161 int
162 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
163                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
164                   u32 * r_rule_match_p, u32 * trace_bitmap)
165 {
166   int i;
167   acl_list_t *a;
168   acl_rule_t *r;
169
170   if (pool_is_free_index (am->acls, acl_index))
171     {
172       if (r_acl_match_p)
173         *r_acl_match_p = acl_index;
174       if (r_rule_match_p)
175         *r_rule_match_p = -1;
176       /* the ACL does not exist but is used for policy. Block traffic. */
177       return 0;
178     }
179   a = am->acls + acl_index;
180   for (i = 0; i < a->count; i++)
181     {
182       r = a->rules + i;
183       if (is_ip6 != r->is_ipv6)
184         {
185           continue;
186         }
187       if (!fa_acl_match_addr
188           (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
189         continue;
190
191 #ifdef FA_NODE_VERBOSE_DEBUG
192       clib_warning
193         ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
194          acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
195          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
196          &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
197          r->dst_prefixlen);
198 #endif
199
200       if (!fa_acl_match_addr
201           (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
202         continue;
203
204 #ifdef FA_NODE_VERBOSE_DEBUG
205       clib_warning
206         ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
207          acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
208          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
209          &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
210          r->src_prefixlen);
211       clib_warning
212         ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
213          acl_index, i, pkt_5tuple->l4.proto, r->proto);
214 #endif
215       if (r->proto)
216         {
217           if (pkt_5tuple->l4.proto != r->proto)
218             continue;
219
220           if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
221                      am->l4_match_nonfirst_fragment))
222           {
223             /* non-initial fragment with frag match configured - match this rule */
224             *trace_bitmap |= 0x80000000;
225             *r_action = r->is_permit;
226             if (r_acl_match_p)
227               *r_acl_match_p = acl_index;
228             if (r_rule_match_p)
229               *r_rule_match_p = i;
230             return 1;
231           }
232
233           /* A sanity check just to ensure we are about to match the ports extracted from the packet */
234           if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
235             continue;
236
237 #ifdef FA_NODE_VERBOSE_DEBUG
238           clib_warning
239             ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
240              acl_index, i, pkt_5tuple->l4.proto, r->proto);
241 #endif
242
243           if (!fa_acl_match_port
244               (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
245                r->src_port_or_type_last, is_ip6))
246             continue;
247
248 #ifdef FA_NODE_VERBOSE_DEBUG
249           clib_warning
250             ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
251              acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
252              r->src_port_or_type_last);
253 #endif
254
255           if (!fa_acl_match_port
256               (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
257                r->dst_port_or_code_last, is_ip6))
258             continue;
259
260 #ifdef FA_NODE_VERBOSE_DEBUG
261           clib_warning
262             ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
263              acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
264              r->dst_port_or_code_last);
265 #endif
266           if (pkt_5tuple->pkt.tcp_flags_valid
267               && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
268                   r->tcp_flags_value))
269             continue;
270         }
271       /* everything matches! */
272 #ifdef FA_NODE_VERBOSE_DEBUG
273       clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
274                     acl_index, i, r->is_permit);
275 #endif
276       *r_action = r->is_permit;
277       if (r_acl_match_p)
278         *r_acl_match_p = acl_index;
279       if (r_rule_match_p)
280         *r_rule_match_p = i;
281       return 1;
282     }
283   return 0;
284 }
285
286 static u8
287 linear_multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
288                        int is_ip6, int is_input, u32 * acl_match_p,
289                        u32 * rule_match_p, u32 * trace_bitmap)
290 {
291   acl_main_t *am = &acl_main;
292   int i;
293   u32 *acl_vector;
294   u8 action = 0;
295
296   if (is_input)
297     {
298       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
299       acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
300     }
301   else
302     {
303       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
304       acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
305     }
306   for (i = 0; i < vec_len (acl_vector); i++)
307     {
308 #ifdef FA_NODE_VERBOSE_DEBUG
309       clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
310                     acl_vector[i]);
311 #endif
312       if (single_acl_match_5tuple
313           (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
314            acl_match_p, rule_match_p, trace_bitmap))
315         {
316           return action;
317         }
318     }
319   if (vec_len (acl_vector) > 0)
320     {
321       /* If there are ACLs and none matched, deny by default */
322       return 0;
323     }
324 #ifdef FA_NODE_VERBOSE_DEBUG
325   clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
326 #endif
327   /* Deny by default. If there are no ACLs defined we should not be here. */
328   return 0;
329 }
330
331 static u8
332 multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
333                        int is_ip6, int is_input, u32 * acl_match_p,
334                        u32 * rule_match_p, u32 * trace_bitmap)
335 {
336   acl_main_t *am = &acl_main;
337   if (am->use_hash_acl_matching) {
338     return hash_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
339                                  is_input, acl_match_p, rule_match_p, trace_bitmap);
340   } else {
341     return linear_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
342                                  is_input, acl_match_p, rule_match_p, trace_bitmap);
343   }
344 }
345
346 static int
347 offset_within_packet (vlib_buffer_t * b0, int offset)
348 {
349   /* For the purposes of this code, "within" means we have at least 8 bytes after it */
350   return (offset <= (b0->current_length - 8));
351 }
352
353 static void
354 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
355                  int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
356 {
357   int l3_offset;
358   int l4_offset;
359   u16 ports[2];
360   u16 proto;
361
362   /* IP4 and IP6 protocol numbers of ICMP */
363   static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
364
365   if (is_l2_path)
366     {
367       l3_offset = ethernet_buffer_header_size(b0);
368     }
369   else
370     {
371       if (is_input)
372         l3_offset = 0;
373       else
374         l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
375     }
376
377   /* key[0..3] contains src/dst address and is cleared/set below */
378   /* Remainder of the key and per-packet non-key data */
379   p5tuple_pkt->kv.key[4] = 0;
380   p5tuple_pkt->kv.value = 0;
381
382   if (is_ip6)
383     {
384       clib_memcpy (&p5tuple_pkt->addr,
385                    get_ptr_to_offset (b0,
386                                       offsetof (ip6_header_t,
387                                                 src_address) + l3_offset),
388                    sizeof (p5tuple_pkt->addr));
389       proto =
390         *(u8 *) get_ptr_to_offset (b0,
391                                    offsetof (ip6_header_t,
392                                              protocol) + l3_offset);
393       l4_offset = l3_offset + sizeof (ip6_header_t);
394 #ifdef FA_NODE_VERBOSE_DEBUG
395       clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
396                     l4_offset);
397 #endif
398       /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
399       int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
400       if (PREDICT_FALSE (need_skip_eh))
401         {
402           while (need_skip_eh && offset_within_packet (b0, l4_offset))
403             {
404               /* Fragment header needs special handling */
405               if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
406                 {
407                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
408                   u16 frag_offset;
409                   clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
410                   frag_offset = ntohs(frag_offset) >> 3;
411                   if (frag_offset)
412                     {
413                       p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
414                       /* invalidate L4 offset so we don't try to find L4 info */
415                       l4_offset += b0->current_length;
416                     }
417                   else
418                     {
419                       /* First fragment: skip the frag header and move on. */
420                       l4_offset += 8;
421                     }
422                 }
423               else
424                 {
425                   u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
426                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
427                   l4_offset += 8 * (1 + (u16) nwords);
428                 }
429 #ifdef FA_NODE_VERBOSE_DEBUG
430               clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
431                             proto, l4_offset);
432 #endif
433               need_skip_eh =
434                 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
435             }
436         }
437     }
438   else
439     {
440       p5tuple_pkt->kv.key[0] = 0;
441       p5tuple_pkt->kv.key[1] = 0;
442       p5tuple_pkt->kv.key[2] = 0;
443       p5tuple_pkt->kv.key[3] = 0;
444       clib_memcpy (&p5tuple_pkt->addr[0].ip4,
445                    get_ptr_to_offset (b0,
446                                       offsetof (ip4_header_t,
447                                                 src_address) + l3_offset),
448                    sizeof (p5tuple_pkt->addr[0].ip4));
449       clib_memcpy (&p5tuple_pkt->addr[1].ip4,
450                    get_ptr_to_offset (b0,
451                                       offsetof (ip4_header_t,
452                                                 dst_address) + l3_offset),
453                    sizeof (p5tuple_pkt->addr[1].ip4));
454       proto =
455         *(u8 *) get_ptr_to_offset (b0,
456                                    offsetof (ip4_header_t,
457                                              protocol) + l3_offset);
458       l4_offset = l3_offset + sizeof (ip4_header_t);
459       u16 flags_and_fragment_offset;
460       clib_memcpy (&flags_and_fragment_offset,
461                    get_ptr_to_offset (b0,
462                                       offsetof (ip4_header_t,
463                                                 flags_and_fragment_offset)) + l3_offset,
464                                                 sizeof(flags_and_fragment_offset));
465       flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
466
467       /* non-initial fragments have non-zero offset */
468       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
469         {
470           p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
471           /* invalidate L4 offset so we don't try to find L4 info */
472           l4_offset += b0->current_length;
473         }
474
475     }
476   p5tuple_pkt->l4.proto = proto;
477   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
478     {
479       p5tuple_pkt->pkt.l4_valid = 1;
480       if (icmp_protos[is_ip6] == proto)
481         {
482           /* type */
483           p5tuple_pkt->l4.port[0] =
484             *(u8 *) get_ptr_to_offset (b0,
485                                        l4_offset + offsetof (icmp46_header_t,
486                                                              type));
487           /* code */
488           p5tuple_pkt->l4.port[1] =
489             *(u8 *) get_ptr_to_offset (b0,
490                                        l4_offset + offsetof (icmp46_header_t,
491                                                              code));
492         }
493       else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
494         {
495           clib_memcpy (&ports,
496                        get_ptr_to_offset (b0,
497                                           l4_offset + offsetof (tcp_header_t,
498                                                                 src_port)),
499                        sizeof (ports));
500           p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
501           p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
502
503           p5tuple_pkt->pkt.tcp_flags =
504             *(u8 *) get_ptr_to_offset (b0,
505                                        l4_offset + offsetof (tcp_header_t,
506                                                              flags));
507           p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
508         }
509       /*
510        * FIXME: rather than the above conditional, here could
511        * be a nice generic mechanism to extract two L4 values:
512        *
513        * have a per-protocol array of 4 elements like this:
514        *   u8 offset; to take the byte from, off L4 header
515        *   u8 mask; to mask it with, before storing
516        *
517        * this way we can describe UDP, TCP and ICMP[46] semantics,
518        * and add a sort of FPM-type behavior for other protocols.
519        *
520        * Of course, is it faster ? and is it needed ?
521        *
522        */
523     }
524 }
525
526
527 /* Session keys match the packets received, and mirror the packets sent */
528 static void
529 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
530                              fa_5tuple_t * p5tuple_sess)
531 {
532   int src_index = is_input ? 0 : 1;
533   int dst_index = is_input ? 1 : 0;
534   p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
535   p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
536   p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
537   p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
538   p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
539 }
540
541
542 static int
543 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
544 {
545   return am->fa_sessions_hash_is_initialized;
546 }
547
548 static int
549 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
550 {
551   int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
552   return it_has;
553 }
554
555 static int
556 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
557 {
558   int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
559   return it_has;
560 }
561
562
563 static int
564 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
565 {
566   /* seen both SYNs and ACKs but not FINs means we are in establshed state */
567   u16 masked_flags =
568     sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
569                                    TCP_FLAGS_RSTFINACKSYN);
570   switch (sess->info.l4.proto)
571     {
572     case IPPROTO_TCP:
573       if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
574         {
575           return ACL_TIMEOUT_TCP_IDLE;
576         }
577       else
578         {
579           return ACL_TIMEOUT_TCP_TRANSIENT;
580         }
581       break;
582     case IPPROTO_UDP:
583       return ACL_TIMEOUT_UDP_IDLE;
584       break;
585     default:
586       return ACL_TIMEOUT_UDP_IDLE;
587     }
588 }
589
590
591 static u64
592 fa_session_get_shortest_timeout(acl_main_t * am)
593 {
594   int timeout_type;
595   u64 timeout = ~0LL;
596   for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
597     if (timeout > am->session_timeout_sec[timeout_type]) {
598       timeout = am->session_timeout_sec[timeout_type];
599     }
600   }
601   return timeout;
602 }
603
604 /*
605  * Get the timeout of the session in a list since its enqueue time.
606  */
607
608 static u64
609 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
610 {
611   u64 timeout = am->vlib_main->clib_time.clocks_per_second;
612   /*
613    * we have the shortest possible timeout type in all the lists
614    * (see README-multicore for the rationale)
615    */
616   timeout *= fa_session_get_shortest_timeout(am);
617   return timeout;
618 }
619
620 /*
621  * Get the idle timeout of a session.
622  */
623
624 static u64
625 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
626 {
627   u64 timeout = am->vlib_main->clib_time.clocks_per_second;
628   int timeout_type = fa_session_get_timeout_type (am, sess);
629   timeout *= am->session_timeout_sec[timeout_type];
630   return timeout;
631 }
632
633 static void
634 acl_fa_verify_init_sessions (acl_main_t * am)
635 {
636   if (!am->fa_sessions_hash_is_initialized) {
637     u16 wk;
638     /* Allocate the per-worker sessions pools */
639     for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
640       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
641       pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
642     }
643
644     /* ... and the interface session hash table */
645     BV (clib_bihash_init) (&am->fa_sessions_hash,
646                          "ACL plugin FA session bihash",
647                          am->fa_conn_table_hash_num_buckets,
648                          am->fa_conn_table_hash_memory_size);
649     am->fa_sessions_hash_is_initialized = 1;
650   }
651 }
652
653 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
654 {
655   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
656   fa_session_t *sess = pool_is_free_index (pw->fa_sessions_pool, session_index) ? 0 : pool_elt_at_index(pw->fa_sessions_pool, session_index);
657   return sess;
658 }
659
660 static inline int is_valid_session_ptr(acl_main_t *am, u16 thread_index, fa_session_t *sess)
661 {
662   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
663   return ((sess != 0) && ((sess - pw->fa_sessions_pool) < pool_len(pw->fa_sessions_pool)));
664 }
665
666 static void
667 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
668 {
669   fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
670   u8 list_id = fa_session_get_timeout_type(am, sess);
671   uword thread_index = os_get_thread_index ();
672   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
673   /* the retrieved session thread index must be necessarily the same as the one in the key */
674   ASSERT (sess->thread_index == sess_id.thread_index);
675   /* the retrieved session thread index must be the same as current thread */
676   ASSERT (sess->thread_index == thread_index);
677   sess->link_enqueue_time = now;
678   sess->link_list_id = list_id;
679   sess->link_next_idx = ~0;
680   sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
681   if (~0 != pw->fa_conn_list_tail[list_id]) {
682     fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
683     prev_sess->link_next_idx = sess_id.session_index;
684     /* We should never try to link with a session on another thread */
685     ASSERT(prev_sess->thread_index == sess->thread_index);
686   }
687   pw->fa_conn_list_tail[list_id] = sess_id.session_index;
688   pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
689
690   if (~0 == pw->fa_conn_list_head[list_id]) {
691     pw->fa_conn_list_head[list_id] = sess_id.session_index;
692   }
693 }
694
695 static int
696 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
697 {
698   uword thread_index = os_get_thread_index ();
699   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
700   if (thread_index != sess_id.thread_index) {
701     /* If another thread attempts to delete the session, fail it. */
702 #ifdef FA_NODE_VERBOSE_DEBUG
703     clib_warning("thread id in key %d != curr thread index, not deleting");
704 #endif
705     return 0;
706   }
707   fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
708   /* we should never try to delete the session with another thread index */
709   ASSERT(sess->thread_index == thread_index);
710   if (~0 != sess->link_prev_idx) {
711     fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
712     /* the previous session must be in the same list as this one */
713     ASSERT(prev_sess->link_list_id == sess->link_list_id);
714     prev_sess->link_next_idx = sess->link_next_idx;
715   }
716   if (~0 != sess->link_next_idx) {
717     fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
718     /* The next session must be in the same list as the one we are deleting */
719     ASSERT(next_sess->link_list_id == sess->link_list_id);
720     next_sess->link_prev_idx = sess->link_prev_idx;
721   }
722   if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
723     pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
724   }
725   if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
726     pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
727   }
728   return 1;
729 }
730
731 static int
732 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
733 {
734   if (acl_fa_conn_list_delete_session(am, sess_id)) {
735     acl_fa_conn_list_add_session(am, sess_id, now);
736     return 1;
737   } else {
738     /*
739      * Our thread does not own this connection, so we can not delete
740      * The session. To avoid the complicated signaling, we simply
741      * pick the list waiting time to be the shortest of the timeouts.
742      * This way we do not have to do anything special, and let
743      * the regular requeue check take care of everything.
744      */
745     return 0;
746   }
747 }
748
749
750 static u8
751 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
752                       fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
753 {
754   sess->last_active_time = now;
755   if (pkt_5tuple->pkt.tcp_flags_valid)
756     {
757       sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
758     }
759   return 3;
760 }
761
762
763 static void
764 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
765 {
766   void *oldheap = clib_mem_set_heap(am->acl_mheap);
767   fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
768   ASSERT(sess->thread_index == os_get_thread_index ());
769   BV (clib_bihash_add_del) (&am->fa_sessions_hash,
770                             &sess->info.kv, 0);
771   acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
772   pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
773   /* Deleting from timer structures not needed,
774      as the caller must have dealt with the timers. */
775   vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
776   clib_mem_set_heap (oldheap);
777   pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
778   clib_smp_atomic_add(&am->fa_session_total_dels, 1);
779 }
780
781 static int
782 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
783 {
784   u64 curr_sess_count;
785   curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
786   return (curr_sess_count < am->fa_conn_table_max_entries);
787 }
788
789 static u64
790 acl_fa_get_list_head_expiry_time(acl_main_t *am, acl_fa_per_worker_data_t *pw, u64 now, u16 thread_index, int timeout_type)
791 {
792   fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
793   /*
794    * We can not check just the index here because inbetween the worker thread might
795    * dequeue the connection from the head just as we are about to check it.
796    */
797   if (!is_valid_session_ptr(am, thread_index, sess)) {
798     return ~0LL; // infinity.
799   } else {
800     u64 timeout_time =
801               sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
802     return timeout_time;
803   }
804 }
805
806 static int
807 acl_fa_conn_time_to_check (acl_main_t *am, acl_fa_per_worker_data_t *pw, u64 now, u16 thread_index, u32 session_index)
808 {
809   fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
810   u64 timeout_time =
811               sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
812   return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
813 }
814
815 /*
816  * see if there are sessions ready to be checked,
817  * do the maintenance (requeue or delete), and
818  * return the total number of sessions reclaimed.
819  */
820 static int
821 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
822 {
823   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
824   fa_full_session_id_t fsid;
825   fsid.thread_index = thread_index;
826   int total_expired = 0;
827
828   {
829     u8 tt = 0;
830     for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
831       while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
832             && (~0 != pw->fa_conn_list_head[tt])
833             && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
834                                           pw->fa_conn_list_head[tt]))) {
835         fsid.session_index = pw->fa_conn_list_head[tt];
836         vec_add1(pw->expired, fsid.session_index);
837         acl_fa_conn_list_delete_session(am, fsid);
838       }
839     }
840   }
841
842   u32 *psid = NULL;
843   vec_foreach (psid, pw->expired)
844   {
845     fsid.session_index = *psid;
846     if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
847       {
848         fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
849         u32 sw_if_index = sess->sw_if_index;
850         u64 sess_timeout_time =
851           sess->last_active_time + fa_session_get_timeout (am, sess);
852         if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
853           {
854 #ifdef FA_NODE_VERBOSE_DEBUG
855             clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
856                (int) session_index);
857 #endif
858             /* There was activity on the session, so the idle timeout
859                has not passed. Enqueue for another time period. */
860
861             acl_fa_conn_list_add_session(am, fsid, now);
862             pw->cnt_session_timer_restarted++;
863           }
864         else
865           {
866 #ifdef FA_NODE_VERBOSE_DEBUG
867             clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
868                (int) session_index);
869 #endif
870             acl_fa_delete_session (am, sw_if_index, fsid);
871             pw->cnt_deleted_sessions++;
872           }
873       }
874     else
875       {
876         pw->cnt_already_deleted_sessions++;
877       }
878   }
879   total_expired = vec_len(pw->expired);
880   /* zero out the vector which we have acted on */
881   if (pw->expired)
882     _vec_len (pw->expired) = 0;
883   /* if we were advancing and reached the end
884    * (no more sessions to recycle), reset the fast-forward timestamp */
885
886   if (pw->swipe_end_time && 0 == total_expired)
887     pw->swipe_end_time = 0;
888   return (total_expired);
889 }
890
891 always_inline void
892 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
893 {
894   /* try to recycle a TCP transient session */
895   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
896   u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
897   fa_full_session_id_t sess_id;
898   sess_id.session_index = pw->fa_conn_list_head[timeout_type];
899   if (~0 != sess_id.session_index) {
900     sess_id.thread_index = thread_index;
901     acl_fa_conn_list_delete_session(am, sess_id);
902     acl_fa_delete_session(am, sw_if_index, sess_id);
903   }
904 }
905
906 static fa_session_t *
907 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
908                     fa_5tuple_t * p5tuple)
909 {
910   clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
911   clib_bihash_kv_40_8_t kv;
912   fa_full_session_id_t f_sess_id;
913   uword thread_index = os_get_thread_index();
914   void *oldheap = clib_mem_set_heap(am->acl_mheap);
915   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
916
917   f_sess_id.thread_index = thread_index;
918   fa_session_t *sess;
919
920   pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
921   f_sess_id.session_index = sess - pw->fa_sessions_pool;
922
923   kv.key[0] = pkv->key[0];
924   kv.key[1] = pkv->key[1];
925   kv.key[2] = pkv->key[2];
926   kv.key[3] = pkv->key[3];
927   kv.key[4] = pkv->key[4];
928   kv.value = f_sess_id.as_u64;
929
930   memcpy (sess, pkv, sizeof (pkv->key));
931   sess->last_active_time = now;
932   sess->sw_if_index = sw_if_index;
933   sess->tcp_flags_seen.as_u16 = 0;
934   sess->thread_index = thread_index;
935   sess->link_list_id = ~0;
936   sess->link_prev_idx = ~0;
937   sess->link_next_idx = ~0;
938
939
940
941   ASSERT(am->fa_sessions_hash_is_initialized == 1);
942   BV (clib_bihash_add_del) (&am->fa_sessions_hash,
943                             &kv, 1);
944   acl_fa_conn_list_add_session(am, f_sess_id, now);
945
946   vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
947   clib_mem_set_heap (oldheap);
948   pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
949   clib_smp_atomic_add(&am->fa_session_total_adds, 1);
950   return sess;
951 }
952
953 static int
954 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
955                      clib_bihash_kv_40_8_t * pvalue_sess)
956 {
957   return (BV (clib_bihash_search)
958           (&am->fa_sessions_hash, &p5tuple->kv,
959            pvalue_sess) == 0);
960 }
961
962
963 always_inline uword
964 acl_fa_node_fn (vlib_main_t * vm,
965                 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
966                 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
967                 vlib_node_registration_t * acl_fa_node)
968 {
969   u32 n_left_from, *from, *to_next;
970   acl_fa_next_t next_index;
971   u32 pkts_acl_checked = 0;
972   u32 pkts_new_session = 0;
973   u32 pkts_exist_session = 0;
974   u32 pkts_acl_permit = 0;
975   u32 pkts_restart_session_timer = 0;
976   u32 trace_bitmap = 0;
977   acl_main_t *am = &acl_main;
978   fa_5tuple_t fa_5tuple, kv_sess;
979   clib_bihash_kv_40_8_t value_sess;
980   vlib_node_runtime_t *error_node;
981   u64 now = clib_cpu_time_now ();
982   uword thread_index = os_get_thread_index ();
983
984   from = vlib_frame_vector_args (frame);
985   n_left_from = frame->n_vectors;
986   next_index = node->cached_next_index;
987
988   error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
989
990   while (n_left_from > 0)
991     {
992       u32 n_left_to_next;
993
994       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
995
996       while (n_left_from > 0 && n_left_to_next > 0)
997         {
998           u32 bi0;
999           vlib_buffer_t *b0;
1000           u32 next0 = 0;
1001           u8 action = 0;
1002           u32 sw_if_index0;
1003           int acl_check_needed = 1;
1004           u32 match_acl_in_index = ~0;
1005           u32 match_rule_index = ~0;
1006           u8 error0 = 0;
1007
1008           /* speculatively enqueue b0 to the current next frame */
1009           bi0 = from[0];
1010           to_next[0] = bi0;
1011           from += 1;
1012           to_next += 1;
1013           n_left_from -= 1;
1014           n_left_to_next -= 1;
1015
1016           b0 = vlib_get_buffer (vm, bi0);
1017
1018           if (is_input)
1019             sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1020           else
1021             sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1022
1023           /*
1024            * Extract the L3/L4 matching info into a 5-tuple structure,
1025            * then create a session key whose layout is independent on forward or reverse
1026            * direction of the packet.
1027            */
1028
1029           acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
1030           fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
1031           acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
1032           fa_5tuple.pkt.sw_if_index = sw_if_index0;
1033           fa_5tuple.pkt.is_ip6 = is_ip6;
1034           fa_5tuple.pkt.is_input = is_input;
1035           fa_5tuple.pkt.mask_type_index_lsb = ~0;
1036 #ifdef FA_NODE_VERBOSE_DEBUG
1037           clib_warning
1038             ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1039              kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
1040              kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
1041           clib_warning
1042             ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1043              fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
1044              fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
1045 #endif
1046
1047           /* Try to match an existing session first */
1048
1049           if (acl_fa_ifc_has_sessions (am, sw_if_index0))
1050             {
1051               if (acl_fa_find_session
1052                   (am, sw_if_index0, &kv_sess, &value_sess))
1053                 {
1054                   trace_bitmap |= 0x80000000;
1055                   error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
1056                   fa_full_session_id_t f_sess_id;
1057
1058                   f_sess_id.as_u64 = value_sess.value;
1059                   ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1060
1061                   fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1062                   int old_timeout_type =
1063                     fa_session_get_timeout_type (am, sess);
1064                   action =
1065                     acl_fa_track_session (am, is_input, sw_if_index0, now,
1066                                           sess, &fa_5tuple);
1067                   /* expose the session id to the tracer */
1068                   match_rule_index = f_sess_id.session_index;
1069                   int new_timeout_type =
1070                     fa_session_get_timeout_type (am, sess);
1071                   acl_check_needed = 0;
1072                   pkts_exist_session += 1;
1073                   /* Tracking might have changed the session timeout type, e.g. from transient to established */
1074                   if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1075                     {
1076                       acl_fa_restart_timer_for_session (am, now, f_sess_id);
1077                       pkts_restart_session_timer++;
1078                       trace_bitmap |=
1079                         0x00010000 + ((0xff & old_timeout_type) << 8) +
1080                         (0xff & new_timeout_type);
1081                     }
1082                   /*
1083                    * I estimate the likelihood to be very low - the VPP needs
1084                    * to have >64K interfaces to start with and then on
1085                    * exactly 64K indices apart needs to be exactly the same
1086                    * 5-tuple... Anyway, since this probability is nonzero -
1087                    * print an error and drop the unlucky packet.
1088                    * If this shows up in real world, we would need to bump
1089                    * the hash key length.
1090                    */
1091                   if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
1092                     clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
1093                     acl_check_needed = 0;
1094                     action = 0;
1095                   }
1096                 }
1097             }
1098
1099           if (acl_check_needed)
1100             {
1101               action =
1102                 multi_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1103                                        is_ip6, is_input, &match_acl_in_index,
1104                                        &match_rule_index, &trace_bitmap);
1105               error0 = action;
1106               if (1 == action)
1107                 pkts_acl_permit += 1;
1108               if (2 == action)
1109                 {
1110                   if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1111                     acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1112
1113                   if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1114                     {
1115                       fa_session_t *sess = acl_fa_add_session (am, is_input, sw_if_index0, now,
1116                                                                &kv_sess);
1117                       acl_fa_track_session (am, is_input, sw_if_index0, now,
1118                                             sess, &fa_5tuple);
1119                       pkts_new_session += 1;
1120                     }
1121                   else
1122                     {
1123                       action = 0;
1124                       error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1125                     }
1126                 }
1127             }
1128
1129
1130
1131           if (action > 0)
1132             {
1133               if (is_l2_path)
1134                 next0 = vnet_l2_feature_next (b0, l2_feat_next_node_index, 0);
1135               else
1136                 vnet_feature_next (sw_if_index0, &next0, b0);
1137             }
1138
1139           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1140                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1141             {
1142               acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1143               t->sw_if_index = sw_if_index0;
1144               t->next_index = next0;
1145               t->match_acl_in_index = match_acl_in_index;
1146               t->match_rule_index = match_rule_index;
1147               t->packet_info[0] = fa_5tuple.kv.key[0];
1148               t->packet_info[1] = fa_5tuple.kv.key[1];
1149               t->packet_info[2] = fa_5tuple.kv.key[2];
1150               t->packet_info[3] = fa_5tuple.kv.key[3];
1151               t->packet_info[4] = fa_5tuple.kv.key[4];
1152               t->packet_info[5] = fa_5tuple.kv.value;
1153               t->action = action;
1154               t->trace_bitmap = trace_bitmap;
1155             }
1156
1157           next0 = next0 < node->n_next_nodes ? next0 : 0;
1158           if (0 == next0)
1159             b0->error = error_node->errors[error0];
1160
1161           pkts_acl_checked += 1;
1162
1163           /* verify speculative enqueue, maybe switch current next frame */
1164           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1165                                            to_next, n_left_to_next, bi0,
1166                                            next0);
1167         }
1168
1169       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1170     }
1171
1172   vlib_node_increment_counter (vm, acl_fa_node->index,
1173                                ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1174   vlib_node_increment_counter (vm, acl_fa_node->index,
1175                                ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1176   vlib_node_increment_counter (vm, acl_fa_node->index,
1177                                ACL_FA_ERROR_ACL_NEW_SESSION,
1178                                pkts_new_session);
1179   vlib_node_increment_counter (vm, acl_fa_node->index,
1180                                ACL_FA_ERROR_ACL_EXIST_SESSION,
1181                                pkts_exist_session);
1182   vlib_node_increment_counter (vm, acl_fa_node->index,
1183                                ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1184                                pkts_restart_session_timer);
1185   return frame->n_vectors;
1186 }
1187
1188
1189 vlib_node_registration_t acl_in_l2_ip6_node;
1190 static uword
1191 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1192                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1193 {
1194   acl_main_t *am = &acl_main;
1195   return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1196                          am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1197                          &acl_in_l2_ip6_node);
1198 }
1199
1200 vlib_node_registration_t acl_in_l2_ip4_node;
1201 static uword
1202 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1203                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1204 {
1205   acl_main_t *am = &acl_main;
1206   return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1207                          am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1208                          &acl_in_l2_ip4_node);
1209 }
1210
1211 vlib_node_registration_t acl_out_l2_ip6_node;
1212 static uword
1213 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1214                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1215 {
1216   acl_main_t *am = &acl_main;
1217   return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1218                          am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1219                          &acl_out_l2_ip6_node);
1220 }
1221
1222 vlib_node_registration_t acl_out_l2_ip4_node;
1223 static uword
1224 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1225                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1226 {
1227   acl_main_t *am = &acl_main;
1228   return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1229                          am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1230                          &acl_out_l2_ip4_node);
1231 }
1232
1233
1234 /**** L3 processing path nodes ****/
1235
1236
1237 vlib_node_registration_t acl_in_fa_ip6_node;
1238 static uword
1239 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1240                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1241 {
1242   return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1243 }
1244
1245 vlib_node_registration_t acl_in_fa_ip4_node;
1246 static uword
1247 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1248                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1249 {
1250   return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1251 }
1252
1253 vlib_node_registration_t acl_out_fa_ip6_node;
1254 static uword
1255 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1256                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1257 {
1258   return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1259 }
1260
1261 vlib_node_registration_t acl_out_fa_ip4_node;
1262 static uword
1263 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1264                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1265 {
1266   return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1267 }
1268
1269 /*
1270  * This process ensures the connection cleanup happens every so often
1271  * even in absence of traffic, as well as provides general orchestration
1272  * for requests like connection deletion on a given sw_if_index.
1273  */
1274
1275
1276 /* *INDENT-OFF* */
1277 #define foreach_acl_fa_cleaner_error \
1278 _(UNKNOWN_EVENT, "unknown event received")  \
1279 /* end  of errors */
1280
1281 typedef enum
1282 {
1283 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1284   foreach_acl_fa_cleaner_error
1285 #undef _
1286     ACL_FA_CLEANER_N_ERROR,
1287 } acl_fa_cleaner_error_t;
1288
1289 static char *acl_fa_cleaner_error_strings[] = {
1290 #define _(sym,string) string,
1291   foreach_acl_fa_cleaner_error
1292 #undef _
1293 };
1294
1295 /* *INDENT-ON* */
1296
1297 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1298 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1299
1300 /*
1301  * Per-worker thread interrupt-driven cleaner thread
1302  * to clean idle connections if there are no packets
1303  */
1304 static uword
1305 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1306               vlib_node_runtime_t * rt, vlib_frame_t * f)
1307 {
1308    acl_main_t *am = &acl_main;
1309    u64 now = clib_cpu_time_now ();
1310    u16 thread_index = os_get_thread_index ();
1311    acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1312    int num_expired;
1313 #ifdef FA_NODE_VERBOSE_DEBUG
1314    clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1315 #endif
1316    /* allow another interrupt to be queued */
1317    pw->interrupt_is_pending = 0;
1318    if (pw->clear_in_process) {
1319      if (0 == pw->swipe_end_time) {
1320        /*
1321         * Someone has just set the flag to start clearing.
1322         * we do this by combing through the connections up to a "time T"
1323         * which is now, and requeueing everything except the expired
1324         * connections and those matching the interface(s) being cleared.
1325         */
1326
1327        /*
1328         * first filter the sw_if_index bitmap that they want from us, by
1329         * a bitmap of sw_if_index for which we actually have connections.
1330         */
1331        if ((pw->pending_clear_sw_if_index_bitmap == 0)
1332            || (pw->serviced_sw_if_index_bitmap == 0)) {
1333 #ifdef FA_NODE_VERBOSE_DEBUG
1334          clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1335 #endif
1336          clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1337        } else {
1338 #ifdef FA_NODE_VERBOSE_DEBUG
1339          clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1340                       format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1341                       format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1342 #endif
1343          pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1344                                                               pw->serviced_sw_if_index_bitmap);
1345        }
1346
1347        if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1348          /* if the cross-section is a zero vector, no need to do anything. */
1349 #ifdef FA_NODE_VERBOSE_DEBUG
1350          clib_warning("WORKER: clearing done - nothing to do");
1351 #endif
1352          pw->clear_in_process = 0;
1353        } else {
1354 #ifdef FA_NODE_VERBOSE_DEBUG
1355          clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1356                       format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1357                       format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1358 #endif
1359          /* swipe through the connection lists until enqueue timestamps become above "now" */
1360          pw->swipe_end_time = now;
1361        }
1362      }
1363    }
1364    num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1365    // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1366    if (pw->clear_in_process) {
1367      if (0 == num_expired) {
1368        /* we were clearing but we could not process any more connections. time to stop. */
1369        clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1370        pw->clear_in_process = 0;
1371 #ifdef FA_NODE_VERBOSE_DEBUG
1372        clib_warning("WORKER: clearing done, all done");
1373 #endif
1374      } else {
1375 #ifdef FA_NODE_VERBOSE_DEBUG
1376        clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1377 #endif
1378        /* should continue clearing.. So could they please sent an interrupt again? */
1379        pw->interrupt_is_needed = 1;
1380      }
1381    } else {
1382      if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1383        /* there was too much work, we should get an interrupt ASAP */
1384        pw->interrupt_is_needed = 1;
1385        pw->interrupt_is_unwanted = 0;
1386      } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1387        /* signal that they should trigger us less */
1388        pw->interrupt_is_needed = 0;
1389        pw->interrupt_is_unwanted = 1;
1390      } else {
1391        /* the current rate of interrupts is ok */
1392        pw->interrupt_is_needed = 0;
1393        pw->interrupt_is_unwanted = 0;
1394      }
1395    }
1396    pw->interrupt_generation = am->fa_interrupt_generation;
1397    return 0;
1398 }
1399
1400 static void
1401 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1402 {
1403   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1404   if (!pw->interrupt_is_pending) {
1405     pw->interrupt_is_pending = 1;
1406     vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1407                   acl_fa_worker_session_cleaner_process_node.index);
1408     /* if the interrupt was requested, mark that done. */
1409     /* pw->interrupt_is_needed = 0; */
1410   }
1411 }
1412
1413 static void
1414 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1415 {
1416   int i;
1417   /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1418   int n_threads = vec_len(vlib_mains);
1419   for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1420     send_one_worker_interrupt(vm, am, i);
1421   }
1422 }
1423
1424 /* centralized process to drive per-worker cleaners */
1425 static uword
1426 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1427                                 vlib_frame_t * f)
1428 {
1429   acl_main_t *am = &acl_main;
1430   u64 now;
1431   f64 cpu_cps = vm->clib_time.clocks_per_second;
1432   u64 next_expire;
1433   /* We should check if there are connections to clean up - at least twice a second */
1434   u64 max_timer_wait_interval = cpu_cps / 2;
1435   uword event_type, *event_data = 0;
1436   acl_fa_per_worker_data_t *pw0;
1437
1438   am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1439   am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1440   am->fa_interrupt_generation = 1;
1441   while (1)
1442     {
1443       now = clib_cpu_time_now ();
1444       next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1445       int has_pending_conns = 0;
1446       u16 ti;
1447       u8 tt;
1448
1449       /*
1450        * walk over all per-thread list heads of different timeouts,
1451        * and see if there are any connections pending.
1452        * If there aren't - we do not need to wake up until the
1453        * worker code signals that it has added a connection.
1454        *
1455        * Also, while we are at it, calculate the earliest we need to wake up.
1456        */
1457       for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1458         if (ti >= vec_len(am->per_worker_data)) {
1459           continue;
1460         }
1461         acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1462         for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1463           u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1464           if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1465 #ifdef FA_NODE_VERBOSE_DEBUG
1466             clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1467 #endif
1468             next_expire = head_expiry;
1469           }
1470           if (~0 != pw->fa_conn_list_head[tt]) {
1471             has_pending_conns = 1;
1472           }
1473         }
1474       }
1475
1476       /* If no pending connections and no ACL applied then no point in timing out */
1477       if (!has_pending_conns && (0 == am->fa_total_enabled_count))
1478         {
1479           am->fa_cleaner_cnt_wait_without_timeout++;
1480           (void) vlib_process_wait_for_event (vm);
1481           event_type = vlib_process_get_events (vm, &event_data);
1482         }
1483       else
1484         {
1485           f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1486           if (timeout <= 0)
1487             {
1488               /* skip waiting altogether */
1489               event_type = ~0;
1490             }
1491           else
1492             {
1493               am->fa_cleaner_cnt_wait_with_timeout++;
1494               (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1495               event_type = vlib_process_get_events (vm, &event_data);
1496             }
1497         }
1498
1499       switch (event_type)
1500         {
1501         case ~0:
1502           /* nothing to do */
1503           break;
1504         case ACL_FA_CLEANER_RESCHEDULE:
1505           /* Nothing to do. */
1506           break;
1507         case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1508           {
1509             uword *clear_sw_if_index_bitmap = 0;
1510             uword *sw_if_index0;
1511             int clear_all = 0;
1512 #ifdef FA_NODE_VERBOSE_DEBUG
1513             clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1514 #endif
1515             vec_foreach (sw_if_index0, event_data)
1516             {
1517               am->fa_cleaner_cnt_delete_by_sw_index++;
1518 #ifdef FA_NODE_VERBOSE_DEBUG
1519               clib_warning
1520                 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1521                  *sw_if_index0);
1522 #endif
1523               if (*sw_if_index0 == ~0)
1524                 {
1525                   clear_all = 1;
1526                 }
1527               else
1528                 {
1529                   if (!pool_is_free_index (am->vnet_main->interface_main.sw_interfaces, *sw_if_index0))
1530                     {
1531                       clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1532                     }
1533                 }
1534             }
1535 #ifdef FA_NODE_VERBOSE_DEBUG
1536             clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1537 #endif
1538             vec_foreach(pw0, am->per_worker_data) {
1539               CLIB_MEMORY_BARRIER ();
1540               while (pw0->clear_in_process) {
1541                 CLIB_MEMORY_BARRIER ();
1542 #ifdef FA_NODE_VERBOSE_DEBUG
1543                 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1544 #endif
1545                 vlib_process_suspend(vm, 0.0001);
1546                 if (pw0->interrupt_is_needed) {
1547                   send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1548                 }
1549               }
1550               if (pw0->clear_in_process) {
1551                 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1552               } else {
1553                 if (clear_all)
1554                   {
1555                     /* if we need to clear all, then just clear the interfaces that we are servicing */
1556                     pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(pw0->serviced_sw_if_index_bitmap);
1557                   }
1558                 else
1559                   {
1560                     pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1561                   }
1562                 pw0->clear_in_process = 1;
1563               }
1564             }
1565             /* send some interrupts so they can start working */
1566             send_interrupts_to_workers(vm, am);
1567
1568             /* now wait till they all complete */
1569 #ifdef FA_NODE_VERBOSE_DEBUG
1570             clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1571 #endif
1572             vec_foreach(pw0, am->per_worker_data) {
1573               CLIB_MEMORY_BARRIER ();
1574               while (pw0->clear_in_process) {
1575                 CLIB_MEMORY_BARRIER ();
1576 #ifdef FA_NODE_VERBOSE_DEBUG
1577                 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1578 #endif
1579                 vlib_process_suspend(vm, 0.0001);
1580                 if (pw0->interrupt_is_needed) {
1581                   send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1582                 }
1583               }
1584             }
1585 #ifdef FA_NODE_VERBOSE_DEBUG
1586             clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1587 #endif
1588             clib_bitmap_free(clear_sw_if_index_bitmap);
1589           }
1590           break;
1591         default:
1592 #ifdef FA_NODE_VERBOSE_DEBUG
1593           clib_warning ("ACL plugin connection cleaner: unknown event %u",
1594                         event_type);
1595 #endif
1596           vlib_node_increment_counter (vm,
1597                                        acl_fa_session_cleaner_process_node.
1598                                        index,
1599                                        ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1600           am->fa_cleaner_cnt_unknown_event++;
1601           break;
1602         }
1603
1604       send_interrupts_to_workers(vm, am);
1605
1606       if (event_data)
1607         _vec_len (event_data) = 0;
1608
1609       /*
1610        * If the interrupts were not processed yet, ensure we wait a bit,
1611        * but up to a point.
1612        */
1613       int need_more_wait = 0;
1614       int max_wait_cycles = 100;
1615       do {
1616         need_more_wait = 0;
1617         vec_foreach(pw0, am->per_worker_data) {
1618           if (pw0->interrupt_generation != am->fa_interrupt_generation) {
1619             need_more_wait = 1;
1620           }
1621         }
1622         if (need_more_wait) {
1623           vlib_process_suspend(vm, 0.0001);
1624         }
1625       } while (need_more_wait && (--max_wait_cycles > 0));
1626
1627       int interrupts_needed = 0;
1628       int interrupts_unwanted = 0;
1629
1630       vec_foreach(pw0, am->per_worker_data) {
1631         if (pw0->interrupt_is_needed) {
1632           interrupts_needed++;
1633           /* the per-worker value is reset when sending the interrupt */
1634         }
1635         if (pw0->interrupt_is_unwanted) {
1636           interrupts_unwanted++;
1637           pw0->interrupt_is_unwanted = 0;
1638         }
1639       }
1640       if (interrupts_needed) {
1641         /* they need more interrupts, do less waiting around next time */
1642         am->fa_current_cleaner_timer_wait_interval /= 2;
1643         /* never go into zero-wait either though - we need to give the space to others */
1644         am->fa_current_cleaner_timer_wait_interval += 1; 
1645       } else if (interrupts_unwanted) {
1646         /* slowly increase the amount of sleep up to a limit */
1647         if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1648           am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1649       }
1650       am->fa_cleaner_cnt_event_cycles++;
1651       am->fa_interrupt_generation++;
1652     }
1653   /* NOT REACHED */
1654   return 0;
1655 }
1656
1657
1658 void
1659 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1660 {
1661   acl_main_t *am = &acl_main;
1662   if (enable_disable) {
1663     acl_fa_verify_init_sessions(am);
1664     am->fa_total_enabled_count++;
1665     void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1666     vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1667                                  ACL_FA_CLEANER_RESCHEDULE, 0);
1668     clib_mem_set_heap (oldheap);
1669   } else {
1670     am->fa_total_enabled_count--;
1671   }
1672
1673   if (is_input)
1674     {
1675       ASSERT(clib_bitmap_get(am->fa_in_acl_on_sw_if_index, sw_if_index) != enable_disable);
1676       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1677       vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1678                                    sw_if_index, enable_disable, 0, 0);
1679       vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1680                                    sw_if_index, enable_disable, 0, 0);
1681       clib_mem_set_heap (oldheap);
1682       am->fa_in_acl_on_sw_if_index =
1683         clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1684                          enable_disable);
1685     }
1686   else
1687     {
1688       ASSERT(clib_bitmap_get(am->fa_out_acl_on_sw_if_index, sw_if_index) != enable_disable);
1689       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1690       vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1691                                    sw_if_index, enable_disable, 0, 0);
1692       vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1693                                    sw_if_index, enable_disable, 0, 0);
1694       clib_mem_set_heap (oldheap);
1695       am->fa_out_acl_on_sw_if_index =
1696         clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1697                          enable_disable);
1698     }
1699   if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1700       && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1701     {
1702 #ifdef FA_NODE_VERBOSE_DEBUG
1703       clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1704 #endif
1705       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1706       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1707                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1708                                  sw_if_index);
1709       clib_mem_set_heap (oldheap);
1710     }
1711 }
1712
1713 void
1714 show_fa_sessions_hash(vlib_main_t * vm, u32 verbose)
1715 {
1716   acl_main_t *am = &acl_main;
1717   if (am->fa_sessions_hash_is_initialized) {
1718     vlib_cli_output(vm, "\nSession lookup hash table:\n%U\n\n",
1719                   BV (format_bihash), &am->fa_sessions_hash, verbose);
1720   } else {
1721     vlib_cli_output(vm, "\nSession lookup hash table is not allocated.\n\n");
1722   }
1723 }
1724
1725
1726 /* *INDENT-OFF* */
1727
1728 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1729   .function = acl_fa_worker_conn_cleaner_process,
1730   .name = "acl-plugin-fa-worker-cleaner-process",
1731   .type = VLIB_NODE_TYPE_INPUT,
1732   .state = VLIB_NODE_STATE_INTERRUPT,
1733 };
1734
1735 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1736   .function = acl_fa_session_cleaner_process,
1737   .type = VLIB_NODE_TYPE_PROCESS,
1738   .name = "acl-plugin-fa-cleaner-process",
1739   .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1740   .error_strings = acl_fa_cleaner_error_strings,
1741   .n_next_nodes = 0,
1742   .next_nodes = {},
1743 };
1744
1745
1746 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1747 {
1748   .function = acl_in_ip6_l2_node_fn,
1749   .name = "acl-plugin-in-ip6-l2",
1750   .vector_size = sizeof (u32),
1751   .format_trace = format_acl_fa_trace,
1752   .type = VLIB_NODE_TYPE_INTERNAL,
1753   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1754   .error_strings = acl_fa_error_strings,
1755   .n_next_nodes = ACL_FA_N_NEXT,
1756   .next_nodes =
1757   {
1758     [ACL_FA_ERROR_DROP] = "error-drop",
1759   }
1760 };
1761
1762 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1763 {
1764   .function = acl_in_ip4_l2_node_fn,
1765   .name = "acl-plugin-in-ip4-l2",
1766   .vector_size = sizeof (u32),
1767   .format_trace = format_acl_fa_trace,
1768   .type = VLIB_NODE_TYPE_INTERNAL,
1769   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1770   .error_strings = acl_fa_error_strings,
1771   .n_next_nodes = ACL_FA_N_NEXT,
1772   .next_nodes =
1773   {
1774     [ACL_FA_ERROR_DROP] = "error-drop",
1775   }
1776 };
1777
1778 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1779 {
1780   .function = acl_out_ip6_l2_node_fn,
1781   .name = "acl-plugin-out-ip6-l2",
1782   .vector_size = sizeof (u32),
1783   .format_trace = format_acl_fa_trace,
1784   .type = VLIB_NODE_TYPE_INTERNAL,
1785   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1786   .error_strings = acl_fa_error_strings,
1787   .n_next_nodes = ACL_FA_N_NEXT,
1788   .next_nodes =
1789   {
1790     [ACL_FA_ERROR_DROP] = "error-drop",
1791   }
1792 };
1793
1794 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1795 {
1796   .function = acl_out_ip4_l2_node_fn,
1797   .name = "acl-plugin-out-ip4-l2",
1798   .vector_size = sizeof (u32),
1799   .format_trace = format_acl_fa_trace,
1800   .type = VLIB_NODE_TYPE_INTERNAL,
1801   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1802   .error_strings = acl_fa_error_strings,
1803   .n_next_nodes = ACL_FA_N_NEXT,
1804   .next_nodes =
1805   {
1806     [ACL_FA_ERROR_DROP] = "error-drop",
1807   }
1808 };
1809
1810
1811 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1812 {
1813   .function = acl_in_ip6_fa_node_fn,
1814   .name = "acl-plugin-in-ip6-fa",
1815   .vector_size = sizeof (u32),
1816   .format_trace = format_acl_fa_trace,
1817   .type = VLIB_NODE_TYPE_INTERNAL,
1818   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1819   .error_strings = acl_fa_error_strings,
1820   .n_next_nodes = ACL_FA_N_NEXT,
1821   .next_nodes =
1822   {
1823     [ACL_FA_ERROR_DROP] = "error-drop",
1824   }
1825 };
1826
1827 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1828 {
1829   .arc_name = "ip6-unicast",
1830   .node_name = "acl-plugin-in-ip6-fa",
1831   .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1832 };
1833
1834 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1835 {
1836   .function = acl_in_ip4_fa_node_fn,
1837   .name = "acl-plugin-in-ip4-fa",
1838   .vector_size = sizeof (u32),
1839   .format_trace = format_acl_fa_trace,
1840   .type = VLIB_NODE_TYPE_INTERNAL,
1841   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1842   .error_strings = acl_fa_error_strings,
1843   .n_next_nodes = ACL_FA_N_NEXT,
1844   .next_nodes =
1845   {
1846     [ACL_FA_ERROR_DROP] = "error-drop",
1847   }
1848 };
1849
1850 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1851 {
1852   .arc_name = "ip4-unicast",
1853   .node_name = "acl-plugin-in-ip4-fa",
1854   .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1855 };
1856
1857
1858 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1859 {
1860   .function = acl_out_ip6_fa_node_fn,
1861   .name = "acl-plugin-out-ip6-fa",
1862   .vector_size = sizeof (u32),
1863   .format_trace = format_acl_fa_trace,
1864   .type = VLIB_NODE_TYPE_INTERNAL,
1865   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1866   .error_strings = acl_fa_error_strings,
1867   .n_next_nodes = ACL_FA_N_NEXT,
1868   .next_nodes =
1869   {
1870     [ACL_FA_ERROR_DROP] = "error-drop",
1871   }
1872 };
1873
1874 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1875 {
1876   .arc_name = "ip6-output",
1877   .node_name = "acl-plugin-out-ip6-fa",
1878   .runs_before = VNET_FEATURES ("interface-output"),
1879 };
1880
1881 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1882 {
1883   .function = acl_out_ip4_fa_node_fn,
1884   .name = "acl-plugin-out-ip4-fa",
1885   .vector_size = sizeof (u32),
1886   .format_trace = format_acl_fa_trace,
1887   .type = VLIB_NODE_TYPE_INTERNAL,
1888   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1889   .error_strings = acl_fa_error_strings,
1890   .n_next_nodes = ACL_FA_N_NEXT,
1891     /* edit / add dispositions here */
1892   .next_nodes =
1893   {
1894     [ACL_FA_ERROR_DROP] = "error-drop",
1895   }
1896 };
1897
1898 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1899 {
1900   .arc_name = "ip4-output",
1901   .node_name = "acl-plugin-out-ip4-fa",
1902   .runs_before = VNET_FEATURES ("interface-output"),
1903 };
1904
1905
1906 /* *INDENT-ON* */