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