c71429e76c68f7687d83502cfa96332c27ee0ceb
[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 "bihash_40_8.h"
24
25 #include <vppinfra/bihash_template.h>
26 #include <vppinfra/bihash_template.c>
27
28 #include "fa_node.h"
29
30 typedef struct
31 {
32   u32 next_index;
33   u32 sw_if_index;
34   u32 match_acl_in_index;
35   u32 match_rule_index;
36   u64 packet_info[6];
37   u32 trace_bitmap;
38   u8 action;
39 } acl_fa_trace_t;
40
41 /* packet trace format function */
42 static u8 *
43 format_acl_fa_trace (u8 * s, va_list * args)
44 {
45   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47   acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
48
49   s =
50     format (s,
51             "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
52             "  pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
53             t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
54             t->match_rule_index, t->trace_bitmap,
55             t->packet_info[0], t->packet_info[1], t->packet_info[2],
56             t->packet_info[3], t->packet_info[4], t->packet_info[5]);
57   return s;
58 }
59
60 /* *INDENT-OFF* */
61 #define foreach_acl_fa_error \
62 _(ACL_DROP, "ACL deny packets")  \
63 _(ACL_PERMIT, "ACL permit packets")  \
64 _(ACL_NEW_SESSION, "new sessions added") \
65 _(ACL_EXIST_SESSION, "existing session packets") \
66 _(ACL_CHECK, "checked packets") \
67 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
68 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
69 /* end  of errors */
70
71 typedef enum
72 {
73 #define _(sym,str) ACL_FA_ERROR_##sym,
74   foreach_acl_fa_error
75 #undef _
76     ACL_FA_N_ERROR,
77 } acl_fa_error_t;
78
79 static char *acl_fa_error_strings[] = {
80 #define _(sym,string) string,
81   foreach_acl_fa_error
82 #undef _
83 };
84 /* *INDENT-ON* */
85
86 static void *
87 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
88 {
89   u8 *p = vlib_buffer_get_current (b0) + offset;
90   return p;
91 }
92
93
94 static int
95 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
96                    int prefixlen, int is_ip6)
97 {
98   if (prefixlen == 0)
99     {
100       /* match any always succeeds */
101       return 1;
102     }
103   if (is_ip6)
104     {
105       if (memcmp (addr1, addr2, prefixlen / 8))
106         {
107           /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
108           return 0;
109         }
110       if (prefixlen % 8)
111         {
112           u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
113           u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
114           u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
115           return (b1 & mask0) == b2;
116         }
117       else
118         {
119           /* The prefix fits into integer number of bytes, so nothing left to do */
120           return 1;
121         }
122     }
123   else
124     {
125       uint32_t a1 = ntohl (addr1->ip4.as_u32);
126       uint32_t a2 = ntohl (addr2->ip4.as_u32);
127       uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
128       return (a1 & mask0) == a2;
129     }
130 }
131
132 static int
133 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
134 {
135   return ((port >= port_first) && (port <= port_last));
136 }
137
138 int
139 acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
140                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
141                   u32 * r_rule_match_p, u32 * trace_bitmap)
142 {
143   int i;
144   acl_list_t *a;
145   acl_rule_t *r;
146
147   if (pool_is_free_index (am->acls, acl_index))
148     {
149       if (r_acl_match_p)
150         *r_acl_match_p = acl_index;
151       if (r_rule_match_p)
152         *r_rule_match_p = -1;
153       /* the ACL does not exist but is used for policy. Block traffic. */
154       return 0;
155     }
156   a = am->acls + acl_index;
157   for (i = 0; i < a->count; i++)
158     {
159       r = a->rules + i;
160       if (is_ip6 != r->is_ipv6)
161         {
162           continue;
163         }
164       if (!fa_acl_match_addr
165           (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
166         continue;
167
168 #ifdef FA_NODE_VERBOSE_DEBUG
169       clib_warning
170         ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
171          acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
172          IP46_TYPE_ANY, format_ip46_address, &r->dst, IP46_TYPE_ANY,
173          r->dst_prefixlen);
174 #endif
175
176       if (!fa_acl_match_addr
177           (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
178         continue;
179
180 #ifdef FA_NODE_VERBOSE_DEBUG
181       clib_warning
182         ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
183          acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
184          IP46_TYPE_ANY, format_ip46_address, &r->src, IP46_TYPE_ANY,
185          r->src_prefixlen);
186       clib_warning
187         ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
188          acl_index, i, pkt_5tuple->l4.proto, r->proto);
189 #endif
190       if (r->proto)
191         {
192           if (pkt_5tuple->l4.proto != r->proto)
193             continue;
194
195           if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
196                      am->l4_match_nonfirst_fragment))
197           {
198             /* non-initial fragment with frag match configured - match this rule */
199             *trace_bitmap |= 0x80000000;
200             *r_action = r->is_permit;
201             if (r_acl_match_p)
202               *r_acl_match_p = acl_index;
203             if (r_rule_match_p)
204               *r_rule_match_p = i;
205             return 1;
206           }
207
208           /* A sanity check just to ensure we are about to match the ports extracted from the packet */
209           if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
210             continue;
211
212 #ifdef FA_NODE_VERBOSE_DEBUG
213           clib_warning
214             ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
215              acl_index, i, pkt_5tuple->l4.proto, r->proto);
216 #endif
217
218           if (!fa_acl_match_port
219               (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
220                r->src_port_or_type_last, is_ip6))
221             continue;
222
223 #ifdef FA_NODE_VERBOSE_DEBUG
224           clib_warning
225             ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
226              acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
227              r->src_port_or_type_last);
228 #endif
229
230           if (!fa_acl_match_port
231               (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
232                r->dst_port_or_code_last, is_ip6))
233             continue;
234
235 #ifdef FA_NODE_VERBOSE_DEBUG
236           clib_warning
237             ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
238              acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
239              r->dst_port_or_code_last);
240 #endif
241           if (pkt_5tuple->pkt.tcp_flags_valid
242               && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
243                   r->tcp_flags_value))
244             continue;
245         }
246       /* everything matches! */
247 #ifdef FA_NODE_VERBOSE_DEBUG
248       clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
249                     acl_index, i, r->is_permit);
250 #endif
251       *r_action = r->is_permit;
252       if (r_acl_match_p)
253         *r_acl_match_p = acl_index;
254       if (r_rule_match_p)
255         *r_rule_match_p = i;
256       return 1;
257     }
258   return 0;
259 }
260
261 static u8
262 full_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
263                        int is_ip6, int is_input, u32 * acl_match_p,
264                        u32 * rule_match_p, u32 * trace_bitmap)
265 {
266   acl_main_t *am = &acl_main;
267   int i;
268   u32 *acl_vector;
269   u8 action = 0;
270
271   if (is_input)
272     {
273       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
274       acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
275     }
276   else
277     {
278       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
279       acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
280     }
281   for (i = 0; i < vec_len (acl_vector); i++)
282     {
283 #ifdef FA_NODE_VERBOSE_DEBUG
284       clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
285                     acl_vector[i]);
286 #endif
287       if (acl_match_5tuple
288           (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
289            acl_match_p, rule_match_p, trace_bitmap))
290         {
291           return action;
292         }
293     }
294   if (vec_len (acl_vector) > 0)
295     {
296       /* If there are ACLs and none matched, deny by default */
297       return 0;
298     }
299 #ifdef FA_NODE_VERBOSE_DEBUG
300   clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
301 #endif
302   /* Deny by default. If there are no ACLs defined we should not be here. */
303   return 0;
304 }
305
306 static int
307 offset_within_packet (vlib_buffer_t * b0, int offset)
308 {
309   /* For the purposes of this code, "within" means we have at least 8 bytes after it */
310   return (offset <= (b0->current_length - 8));
311 }
312
313 static void
314 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
315                  int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
316 {
317   int l3_offset = 14;
318   int l4_offset;
319   u16 ports[2];
320   u16 proto;
321   /* IP4 and IP6 protocol numbers of ICMP */
322   static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
323
324   if (is_input && !(is_l2_path))
325     {
326       l3_offset = 0;
327     }
328
329   /* key[0..3] contains src/dst address and is cleared/set below */
330   /* Remainder of the key and per-packet non-key data */
331   p5tuple_pkt->kv.key[4] = 0;
332   p5tuple_pkt->kv.value = 0;
333
334   if (is_ip6)
335     {
336       clib_memcpy (&p5tuple_pkt->addr,
337                    get_ptr_to_offset (b0,
338                                       offsetof (ip6_header_t,
339                                                 src_address) + l3_offset),
340                    sizeof (p5tuple_pkt->addr));
341       proto =
342         *(u8 *) get_ptr_to_offset (b0,
343                                    offsetof (ip6_header_t,
344                                              protocol) + l3_offset);
345       l4_offset = l3_offset + sizeof (ip6_header_t);
346 #ifdef FA_NODE_VERBOSE_DEBUG
347       clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
348                     l4_offset);
349 #endif
350       /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
351       int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
352       if (PREDICT_FALSE (need_skip_eh))
353         {
354           while (need_skip_eh && offset_within_packet (b0, l4_offset))
355             {
356               /* Fragment header needs special handling */
357               if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
358                 {
359                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
360                   u16 frag_offset;
361                   clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
362                   frag_offset = ntohs(frag_offset) >> 3;
363                   if (frag_offset)
364                     {
365                       p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
366                       /* invalidate L4 offset so we don't try to find L4 info */
367                       l4_offset += b0->current_length;
368                     }
369                   else
370                     {
371                       /* First fragment: skip the frag header and move on. */
372                       l4_offset += 8;
373                     }
374                 }
375               else
376                 {
377                   u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
378                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
379                   l4_offset += 8 * (1 + (u16) nwords);
380                 }
381 #ifdef FA_NODE_VERBOSE_DEBUG
382               clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
383                             proto, l4_offset);
384 #endif
385               need_skip_eh =
386                 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
387             }
388         }
389     }
390   else
391     {
392       p5tuple_pkt->kv.key[0] = 0;
393       p5tuple_pkt->kv.key[1] = 0;
394       p5tuple_pkt->kv.key[2] = 0;
395       p5tuple_pkt->kv.key[3] = 0;
396       clib_memcpy (&p5tuple_pkt->addr[0].ip4,
397                    get_ptr_to_offset (b0,
398                                       offsetof (ip4_header_t,
399                                                 src_address) + l3_offset),
400                    sizeof (p5tuple_pkt->addr[0].ip4));
401       clib_memcpy (&p5tuple_pkt->addr[1].ip4,
402                    get_ptr_to_offset (b0,
403                                       offsetof (ip4_header_t,
404                                                 dst_address) + l3_offset),
405                    sizeof (p5tuple_pkt->addr[1].ip4));
406       proto =
407         *(u8 *) get_ptr_to_offset (b0,
408                                    offsetof (ip4_header_t,
409                                              protocol) + l3_offset);
410       l4_offset = l3_offset + sizeof (ip4_header_t);
411       u16 flags_and_fragment_offset;
412       clib_memcpy (&flags_and_fragment_offset,
413                    get_ptr_to_offset (b0,
414                                       offsetof (ip4_header_t,
415                                                 flags_and_fragment_offset)) + l3_offset,
416                                                 sizeof(flags_and_fragment_offset));
417       flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
418
419       /* non-initial fragments have non-zero offset */
420       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
421         {
422           p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
423           /* invalidate L4 offset so we don't try to find L4 info */
424           l4_offset += b0->current_length;
425         }
426
427     }
428   p5tuple_pkt->l4.proto = proto;
429   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
430     {
431       p5tuple_pkt->pkt.l4_valid = 1;
432       if (icmp_protos[is_ip6] == proto)
433         {
434           /* type */
435           p5tuple_pkt->l4.port[0] =
436             *(u8 *) get_ptr_to_offset (b0,
437                                        l4_offset + offsetof (icmp46_header_t,
438                                                              type));
439           /* code */
440           p5tuple_pkt->l4.port[1] =
441             *(u8 *) get_ptr_to_offset (b0,
442                                        l4_offset + offsetof (icmp46_header_t,
443                                                              code));
444         }
445       else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
446         {
447           clib_memcpy (&ports,
448                        get_ptr_to_offset (b0,
449                                           l4_offset + offsetof (tcp_header_t,
450                                                                 src_port)),
451                        sizeof (ports));
452           p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
453           p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
454
455           p5tuple_pkt->pkt.tcp_flags =
456             *(u8 *) get_ptr_to_offset (b0,
457                                        l4_offset + offsetof (tcp_header_t,
458                                                              flags));
459           p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
460         }
461       /*
462        * FIXME: rather than the above conditional, here could
463        * be a nice generic mechanism to extract two L4 values:
464        *
465        * have a per-protocol array of 4 elements like this:
466        *   u8 offset; to take the byte from, off L4 header
467        *   u8 mask; to mask it with, before storing
468        *
469        * this way we can describe UDP, TCP and ICMP[46] semantics,
470        * and add a sort of FPM-type behavior for other protocols.
471        *
472        * Of course, is it faster ? and is it needed ?
473        *
474        */
475     }
476 }
477
478
479 /* Session keys match the packets received, and mirror the packets sent */
480 static void
481 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
482                              fa_5tuple_t * p5tuple_sess)
483 {
484   int src_index = is_input ? 0 : 1;
485   int dst_index = is_input ? 1 : 0;
486   p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
487   p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
488   p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
489   p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
490   p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
491 }
492
493
494 static int
495 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
496 {
497   int has_sessions =
498     clib_bitmap_get (am->fa_sessions_on_sw_if_index, sw_if_index0);
499   return has_sessions;
500 }
501
502 static int
503 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
504 {
505   int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
506   return it_has;
507 }
508
509 static int
510 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
511 {
512   int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
513   return it_has;
514 }
515
516
517 static int
518 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
519 {
520   /* seen both SYNs and ACKs but not FINs means we are in establshed state */
521   u16 masked_flags =
522     sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
523                                    TCP_FLAGS_RSTFINACKSYN);
524   switch (sess->info.l4.proto)
525     {
526     case IPPROTO_TCP:
527       if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
528         {
529           return ACL_TIMEOUT_TCP_IDLE;
530         }
531       else
532         {
533           return ACL_TIMEOUT_TCP_TRANSIENT;
534         }
535       break;
536     case IPPROTO_UDP:
537       return ACL_TIMEOUT_UDP_IDLE;
538       break;
539     default:
540       return ACL_TIMEOUT_UDP_IDLE;
541     }
542 }
543
544
545 static u64
546 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
547 {
548   u64 timeout = am->vlib_main->clib_time.clocks_per_second;
549   int timeout_type = fa_session_get_timeout_type (am, sess);
550   timeout *= am->session_timeout_sec[timeout_type];
551   return timeout;
552 }
553
554 static void
555 acl_fa_ifc_init_sessions (acl_main_t * am, int sw_if_index0)
556 {
557 #ifdef FA_NODE_VERBOSE_DEBUG
558   clib_warning
559     ("Initializing bihash for sw_if_index %d num buckets %lu memory size %llu",
560      sw_if_index0, am->fa_conn_table_hash_num_buckets,
561      am->fa_conn_table_hash_memory_size);
562 #endif
563   vec_validate (am->fa_sessions_by_sw_if_index, sw_if_index0);
564   BV (clib_bihash_init) (&am->fa_sessions_by_sw_if_index
565                          [sw_if_index0], "ACL plugin FA session bihash",
566                          am->fa_conn_table_hash_num_buckets,
567                          am->fa_conn_table_hash_memory_size);
568   am->fa_sessions_on_sw_if_index =
569     clib_bitmap_set (am->fa_sessions_on_sw_if_index, sw_if_index0, 1);
570 }
571
572 static void
573 acl_fa_conn_list_add_session (acl_main_t * am, u32 sess_id, u64 now)
574 {
575   fa_session_t *sess = am->fa_sessions_pool + sess_id;
576   u8 list_id = fa_session_get_timeout_type(am, sess);
577   sess->link_enqueue_time = now;
578   sess->link_list_id = list_id;
579   sess->link_next_idx = ~0;
580   sess->link_prev_idx = am->fa_conn_list_tail[list_id];
581   if (~0 != am->fa_conn_list_tail[list_id]) {
582     fa_session_t *prev_sess = am->fa_sessions_pool + am->fa_conn_list_tail[list_id];
583     prev_sess->link_next_idx = sess_id;
584   }
585   am->fa_conn_list_tail[list_id] = sess_id;
586
587   if (~0 == am->fa_conn_list_head[list_id]) {
588     am->fa_conn_list_head[list_id] = sess_id;
589     /* If it is a first conn in any list, kick off the cleaner */
590     vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
591                                  ACL_FA_CLEANER_RESCHEDULE, 0);
592
593   }
594 }
595
596 static void
597 acl_fa_conn_list_delete_session (acl_main_t *am, u32 sess_id)
598 {
599   fa_session_t *sess = am->fa_sessions_pool + sess_id;
600   if (~0 != sess->link_prev_idx) {
601     fa_session_t *prev_sess = am->fa_sessions_pool + sess->link_prev_idx;
602     prev_sess->link_next_idx = sess->link_next_idx;
603     if (prev_sess->link_list_id != sess->link_list_id)
604       clib_warning("(prev_sess->link_list_id != sess->link_list_id)");
605   }
606   if (~0 != sess->link_next_idx) {
607     fa_session_t *next_sess = am->fa_sessions_pool + sess->link_next_idx;
608     next_sess->link_prev_idx = sess->link_prev_idx;
609     if (next_sess->link_list_id != sess->link_list_id)
610       clib_warning("(next_sess->link_list_id != sess->link_list_id)");
611   }
612   if (am->fa_conn_list_head[sess->link_list_id] == sess_id) {
613     am->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
614   }
615   if (am->fa_conn_list_tail[sess->link_list_id] == sess_id) {
616     am->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
617   }
618 }
619
620
621 int
622 acl_fa_session_is_dead (acl_main_t * am, u32 sw_if_index, u64 now,
623                         u32 sess_id)
624 {
625   return 0;
626 }
627
628 static void
629 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, u32 sess_id)
630 {
631   // fa_session_t *sess = am->fa_sessions_pool + sess_id;
632   acl_fa_conn_list_delete_session(am, sess_id);
633   acl_fa_conn_list_add_session(am, sess_id, now);
634 }
635
636
637 static u8
638 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
639                       fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
640 {
641   sess->last_active_time = now;
642   if (pkt_5tuple->pkt.tcp_flags_valid)
643     {
644       sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
645     }
646   return 3;
647 }
648
649
650 static void
651 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, u32 sess_id)
652 {
653   fa_session_t *sess = (fa_session_t *) am->fa_sessions_pool + sess_id;
654   BV (clib_bihash_add_del) (&am->fa_sessions_by_sw_if_index[sw_if_index],
655                             &sess->info.kv, 0);
656   pool_put_index (am->fa_sessions_pool, sess_id);
657   /* Deleting from timer wheel not needed, as the cleaner deals with the timers. */
658   vec_validate (am->fa_session_dels_by_sw_if_index, sw_if_index);
659   am->fa_session_dels_by_sw_if_index[sw_if_index]++;
660 }
661
662 static int
663 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
664 {
665   u64 curr_sess;
666   vec_validate (am->fa_session_adds_by_sw_if_index, sw_if_index);
667   vec_validate (am->fa_session_dels_by_sw_if_index, sw_if_index);
668   curr_sess =
669     am->fa_session_adds_by_sw_if_index[sw_if_index] -
670     am->fa_session_dels_by_sw_if_index[sw_if_index];
671   return (curr_sess < am->fa_conn_table_max_entries);
672 }
673
674 always_inline void
675 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u32 sw_if_index)
676 {
677   /* try to recycle a TCP transient session */
678   u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
679   u32 sess_id = am->fa_conn_list_head[timeout_type];
680   if (~0 != sess_id) {
681     acl_fa_conn_list_delete_session(am, sess_id);
682     acl_fa_delete_session(am, sw_if_index, sess_id);
683   }
684 }
685
686 static void
687 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
688                     fa_5tuple_t * p5tuple)
689 {
690   clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
691   clib_bihash_kv_40_8_t kv;
692   u32 sess_id;
693   fa_session_t *sess;
694
695   pool_get (am->fa_sessions_pool, sess);
696   sess_id = sess - am->fa_sessions_pool;
697
698
699   kv.key[0] = pkv->key[0];
700   kv.key[1] = pkv->key[1];
701   kv.key[2] = pkv->key[2];
702   kv.key[3] = pkv->key[3];
703   kv.key[4] = pkv->key[4];
704   kv.value = sess_id;
705
706   memcpy (sess, pkv, sizeof (pkv->key));
707   sess->last_active_time = now;
708   sess->sw_if_index = sw_if_index;
709   sess->tcp_flags_seen.as_u16 = 0;
710   sess->reserved1 = 0;
711   sess->link_list_id = ~0;
712   sess->link_prev_idx = ~0;
713   sess->link_next_idx = ~0;
714
715
716
717   if (!acl_fa_ifc_has_sessions (am, sw_if_index))
718     {
719       acl_fa_ifc_init_sessions (am, sw_if_index);
720     }
721
722   BV (clib_bihash_add_del) (&am->fa_sessions_by_sw_if_index[sw_if_index],
723                             &kv, 1);
724   acl_fa_conn_list_add_session(am, sess_id, now);
725
726   vec_validate (am->fa_session_adds_by_sw_if_index, sw_if_index);
727   am->fa_session_adds_by_sw_if_index[sw_if_index]++;
728 }
729
730 static int
731 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
732                      clib_bihash_kv_40_8_t * pvalue_sess)
733 {
734   return (BV (clib_bihash_search)
735           (&am->fa_sessions_by_sw_if_index[sw_if_index0], &p5tuple->kv,
736            pvalue_sess) == 0);
737 }
738
739
740 always_inline uword
741 acl_fa_node_fn (vlib_main_t * vm,
742                 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
743                 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
744                 vlib_node_registration_t * acl_fa_node)
745 {
746   u32 n_left_from, *from, *to_next;
747   acl_fa_next_t next_index;
748   u32 pkts_acl_checked = 0;
749   u32 pkts_new_session = 0;
750   u32 pkts_exist_session = 0;
751   u32 pkts_acl_permit = 0;
752   u32 pkts_restart_session_timer = 0;
753   u32 trace_bitmap = 0;
754   u32 feature_bitmap0;
755   acl_main_t *am = &acl_main;
756   fa_5tuple_t fa_5tuple, kv_sess;
757   clib_bihash_kv_40_8_t value_sess;
758   vlib_node_runtime_t *error_node;
759   u64 now = clib_cpu_time_now ();
760
761   from = vlib_frame_vector_args (frame);
762   n_left_from = frame->n_vectors;
763   next_index = node->cached_next_index;
764
765   error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
766
767   while (n_left_from > 0)
768     {
769       u32 n_left_to_next;
770
771       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
772
773       while (n_left_from > 0 && n_left_to_next > 0)
774         {
775           u32 bi0;
776           vlib_buffer_t *b0;
777           u32 next0 = 0;
778           u8 action = 0;
779           u32 sw_if_index0;
780           int acl_check_needed = 1;
781           u32 match_acl_in_index = ~0;
782           u32 match_rule_index = ~0;
783           u8 error0 = 0;
784
785           /* speculatively enqueue b0 to the current next frame */
786           bi0 = from[0];
787           to_next[0] = bi0;
788           from += 1;
789           to_next += 1;
790           n_left_from -= 1;
791           n_left_to_next -= 1;
792
793           b0 = vlib_get_buffer (vm, bi0);
794
795           if (is_input)
796             sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
797           else
798             sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
799           if (is_l2_path)
800             feature_bitmap0 = vnet_buffer (b0)->l2.feature_bitmap;
801
802           /*
803            * Extract the L3/L4 matching info into a 5-tuple structure,
804            * then create a session key whose layout is independent on forward or reverse
805            * direction of the packet.
806            */
807
808           acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
809           acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
810 #ifdef FA_NODE_VERBOSE_DEBUG
811           clib_warning
812             ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
813              kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
814              kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
815           clib_warning
816             ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
817              fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
818              fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
819 #endif
820
821           /* Try to match an existing session first */
822
823           if (acl_fa_ifc_has_sessions (am, sw_if_index0))
824             {
825               if (acl_fa_find_session
826                   (am, sw_if_index0, &kv_sess, &value_sess))
827                 {
828                   trace_bitmap |= 0x80000000;
829                   error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
830                   // FIXME assert(value_sess.value == (0xffffffff & value_sess.value));
831                   u32 sess_id = value_sess.value;
832                   fa_session_t *sess = am->fa_sessions_pool + sess_id;
833                   int old_timeout_type =
834                     fa_session_get_timeout_type (am, sess);
835                   action =
836                     acl_fa_track_session (am, is_input, sw_if_index0, now,
837                                           sess, &fa_5tuple);
838                   /* expose the session id to the tracer */
839                   match_rule_index = sess_id;
840                   int new_timeout_type =
841                     fa_session_get_timeout_type (am, sess);
842                   acl_check_needed = 0;
843                   pkts_exist_session += 1;
844                   /* Tracking might have changed the session timeout type, e.g. from transient to established */
845                   if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
846                     {
847                       acl_fa_restart_timer_for_session (am, now, sess_id);
848                       pkts_restart_session_timer++;
849                       trace_bitmap |=
850                         0x00010000 + ((0xff & old_timeout_type) << 8) +
851                         (0xff & new_timeout_type);
852                     }
853                 }
854             }
855
856           if (acl_check_needed)
857             {
858               action =
859                 full_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
860                                        is_ip6, is_input, &match_acl_in_index,
861                                        &match_rule_index, &trace_bitmap);
862               error0 = action;
863               if (1 == action)
864                 pkts_acl_permit += 1;
865               if (2 == action)
866                 {
867                   if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
868                     acl_fa_try_recycle_session (am, is_input, sw_if_index0);
869
870                   if (acl_fa_can_add_session (am, is_input, sw_if_index0))
871                     {
872                       acl_fa_add_session (am, is_input, sw_if_index0, now,
873                                           &kv_sess);
874                       pkts_new_session += 1;
875                     }
876                   else
877                     {
878                       action = 0;
879                       error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
880                     }
881                 }
882             }
883
884
885
886           if (action > 0)
887             {
888               if (is_l2_path)
889                 next0 =
890                   feat_bitmap_get_next_node_index (l2_feat_next_node_index,
891                                                    feature_bitmap0);
892               else
893                 vnet_feature_next (sw_if_index0, &next0, b0);
894             }
895
896           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
897                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
898             {
899               acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
900               t->sw_if_index = sw_if_index0;
901               t->next_index = next0;
902               t->match_acl_in_index = match_acl_in_index;
903               t->match_rule_index = match_rule_index;
904               t->packet_info[0] = fa_5tuple.kv.key[0];
905               t->packet_info[1] = fa_5tuple.kv.key[1];
906               t->packet_info[2] = fa_5tuple.kv.key[2];
907               t->packet_info[3] = fa_5tuple.kv.key[3];
908               t->packet_info[4] = fa_5tuple.kv.key[4];
909               t->packet_info[5] = fa_5tuple.kv.value;
910               t->action = action;
911               t->trace_bitmap = trace_bitmap;
912             }
913
914           next0 = next0 < node->n_next_nodes ? next0 : 0;
915           if (0 == next0)
916             b0->error = error_node->errors[error0];
917
918           pkts_acl_checked += 1;
919
920           /* verify speculative enqueue, maybe switch current next frame */
921           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
922                                            to_next, n_left_to_next, bi0,
923                                            next0);
924         }
925
926       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
927     }
928
929   vlib_node_increment_counter (vm, acl_fa_node->index,
930                                ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
931   vlib_node_increment_counter (vm, acl_fa_node->index,
932                                ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
933   vlib_node_increment_counter (vm, acl_fa_node->index,
934                                ACL_FA_ERROR_ACL_NEW_SESSION,
935                                pkts_new_session);
936   vlib_node_increment_counter (vm, acl_fa_node->index,
937                                ACL_FA_ERROR_ACL_EXIST_SESSION,
938                                pkts_exist_session);
939   vlib_node_increment_counter (vm, acl_fa_node->index,
940                                ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
941                                pkts_restart_session_timer);
942   return frame->n_vectors;
943 }
944
945
946 vlib_node_registration_t acl_in_l2_ip6_node;
947 static uword
948 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
949                        vlib_node_runtime_t * node, vlib_frame_t * frame)
950 {
951   acl_main_t *am = &acl_main;
952   return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
953                          am->fa_acl_in_ip6_l2_node_feat_next_node_index,
954                          &acl_in_l2_ip6_node);
955 }
956
957 vlib_node_registration_t acl_in_l2_ip4_node;
958 static uword
959 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
960                        vlib_node_runtime_t * node, vlib_frame_t * frame)
961 {
962   acl_main_t *am = &acl_main;
963   return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
964                          am->fa_acl_in_ip4_l2_node_feat_next_node_index,
965                          &acl_in_l2_ip4_node);
966 }
967
968 vlib_node_registration_t acl_out_l2_ip6_node;
969 static uword
970 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
971                         vlib_node_runtime_t * node, vlib_frame_t * frame)
972 {
973   acl_main_t *am = &acl_main;
974   return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
975                          am->fa_acl_out_ip6_l2_node_feat_next_node_index,
976                          &acl_out_l2_ip6_node);
977 }
978
979 vlib_node_registration_t acl_out_l2_ip4_node;
980 static uword
981 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
982                         vlib_node_runtime_t * node, vlib_frame_t * frame)
983 {
984   acl_main_t *am = &acl_main;
985   return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
986                          am->fa_acl_out_ip4_l2_node_feat_next_node_index,
987                          &acl_out_l2_ip4_node);
988 }
989
990
991 /**** L3 processing path nodes ****/
992
993
994 vlib_node_registration_t acl_in_fa_ip6_node;
995 static uword
996 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
997                        vlib_node_runtime_t * node, vlib_frame_t * frame)
998 {
999   return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1000 }
1001
1002 vlib_node_registration_t acl_in_fa_ip4_node;
1003 static uword
1004 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1005                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1006 {
1007   return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1008 }
1009
1010 vlib_node_registration_t acl_out_fa_ip6_node;
1011 static uword
1012 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1013                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1014 {
1015   return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1016 }
1017
1018 vlib_node_registration_t acl_out_fa_ip4_node;
1019 static uword
1020 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1021                         vlib_node_runtime_t * node, vlib_frame_t * frame)
1022 {
1023   return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1024 }
1025
1026 /*
1027  * This process performs all the connection clean up - both for idle connections,
1028  * as well as receiving the signals to clean up the connections in case of sw_if_index deletion,
1029  * or (maybe in the future) the connection deletion due to policy reasons.
1030  *
1031  * The previous iteration (l2sess) attempted to clean up the connections in small increments,
1032  * in-band, but the problem it tried to preemptively address (process starvation) is yet to be seen.
1033  *
1034  * The approach with a single thread deleting the connections is simpler, thus we use it until
1035  * there is a real starvation problem to solve.
1036  *
1037  */
1038
1039
1040 /* *INDENT-OFF* */
1041 #define foreach_acl_fa_cleaner_error \
1042 _(UNKNOWN_EVENT, "unknown event received")  \
1043 /* end  of errors */
1044
1045 typedef enum
1046 {
1047 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1048   foreach_acl_fa_cleaner_error
1049 #undef _
1050     ACL_FA_CLEANER_N_ERROR,
1051 } acl_fa_cleaner_error_t;
1052
1053 static char *acl_fa_cleaner_error_strings[] = {
1054 #define _(sym,string) string,
1055   foreach_acl_fa_cleaner_error
1056 #undef _
1057 };
1058
1059 static int
1060 acl_fa_clean_sessions_by_sw_if_index (acl_main_t *am, u32 sw_if_index, u32 *count)
1061 {
1062
1063   int undeleted = 0;
1064   fa_session_t *sess;
1065   uword *dv = NULL;
1066   uword *ii;
1067
1068   pool_foreach(sess, am->fa_sessions_pool, ({
1069     if ( (~0 == sw_if_index) || (sw_if_index == sess->sw_if_index) )
1070       vec_add1(dv, sess-am->fa_sessions_pool);
1071   }));
1072   vec_foreach(ii, dv)
1073   {
1074     sess =  pool_elt_at_index(am->fa_sessions_pool, *ii);
1075     acl_fa_delete_session(am, sess->sw_if_index, *ii);
1076     (*count)++;
1077   }
1078
1079   pool_foreach(sess, am->fa_sessions_pool, ({
1080     if ( (~0 == sw_if_index) || (sw_if_index == sess->sw_if_index) )
1081       undeleted++;
1082   }));
1083   if (undeleted == 0)
1084     {
1085       if (~0 == sw_if_index)
1086         {
1087           /* FIXME: clean-up tables ? */
1088         }
1089       else
1090         {
1091           /* FIXME: clean-up tables ? */
1092         }
1093     }
1094   return (undeleted == 0);
1095 }
1096 /* *INDENT-ON* */
1097
1098 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1099
1100 static int
1101 acl_fa_conn_time_to_check (acl_main_t *am, u64 now, u32 session_index)
1102 {
1103   fa_session_t *sess = am->fa_sessions_pool + session_index;
1104   u64 timeout_time =
1105               sess->link_enqueue_time + fa_session_get_timeout (am, sess);
1106   return (timeout_time < now);
1107 }
1108
1109
1110 static uword
1111 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1112                                 vlib_frame_t * f)
1113 {
1114   acl_main_t *am = &acl_main;
1115   u64 now = clib_cpu_time_now ();
1116   f64 cpu_cps = vm->clib_time.clocks_per_second;
1117   u64 next_expire;
1118   /* We should call timer wheel at least twice a second */
1119   u64 max_timer_wait_interval = cpu_cps / 2;
1120   am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1121
1122   u32 *expired = NULL;
1123   uword event_type, *event_data = 0;
1124
1125   am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1126
1127   while (1)
1128     {
1129       u32 count_deleted_sessions = 0;
1130       u32 count_already_deleted = 0;
1131       now = clib_cpu_time_now ();
1132       next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1133       int has_pending_conns = 0;
1134       u8 tt;
1135       for(tt = 0; tt < ACL_N_TIMEOUTS; tt++)
1136         {
1137           if (~0 != am->fa_conn_list_head[tt])
1138             has_pending_conns = 1;
1139         }
1140
1141       /* If no pending connections then no point in timing out */
1142       if (!has_pending_conns)
1143         {
1144           am->fa_cleaner_cnt_wait_without_timeout++;
1145           (void) vlib_process_wait_for_event (vm);
1146           event_type = vlib_process_get_events (vm, &event_data);
1147         }
1148       else
1149         {
1150           f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1151           if (timeout <= 0)
1152             {
1153               /* skip waiting altogether */
1154               event_type = ~0;
1155             }
1156           else
1157             {
1158               /* Timing wheel code is happier if it is called regularly */
1159               if (timeout > 0.5)
1160                 timeout = 0.5;
1161               am->fa_cleaner_cnt_wait_with_timeout++;
1162               (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1163               event_type = vlib_process_get_events (vm, &event_data);
1164             }
1165         }
1166
1167       now = clib_cpu_time_now ();
1168       switch (event_type)
1169         {
1170         case ~0:
1171           /* nothing to do */
1172           break;
1173         case ACL_FA_CLEANER_RESCHEDULE:
1174           /* Nothing to do. */
1175           break;
1176         case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1177           {
1178             uword *sw_if_index0;
1179             vec_foreach (sw_if_index0, event_data)
1180             {
1181               am->fa_cleaner_cnt_delete_by_sw_index++;
1182 #ifdef FA_NODE_VERBOSE_DEBUG
1183               clib_warning
1184                 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1185                  *sw_if_index0);
1186 #endif
1187               u32 count = 0;
1188               int result =
1189                 acl_fa_clean_sessions_by_sw_if_index (am, *sw_if_index0,
1190                                                       &count);
1191               count_deleted_sessions += count;
1192               am->fa_cleaner_cnt_delete_by_sw_index_ok += result;
1193             }
1194           }
1195           break;
1196         default:
1197 #ifdef FA_NODE_VERBOSE_DEBUG
1198           clib_warning ("ACL plugin connection cleaner: unknown event %u",
1199                         event_type);
1200 #endif
1201           vlib_node_increment_counter (vm,
1202                                        acl_fa_session_cleaner_process_node.
1203                                        index,
1204                                        ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1205           am->fa_cleaner_cnt_unknown_event++;
1206           break;
1207         }
1208
1209       {
1210         u8 tt = 0;
1211         for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
1212           while((vec_len(expired) < 2*am->fa_max_deleted_sessions_per_interval)
1213                 && (~0 != am->fa_conn_list_head[tt])
1214                 && (acl_fa_conn_time_to_check(am, now,
1215                                               am->fa_conn_list_head[tt]))) {
1216             u32 sess_id = am->fa_conn_list_head[tt];
1217             vec_add1(expired, sess_id);
1218             acl_fa_conn_list_delete_session(am, sess_id);
1219           }
1220         }
1221       }
1222
1223       u32 *psid = NULL;
1224       vec_foreach (psid, expired)
1225       {
1226         u32 session_index = *psid;
1227         if (!pool_is_free_index (am->fa_sessions_pool, session_index))
1228           {
1229             fa_session_t *sess = am->fa_sessions_pool + session_index;
1230             u32 sw_if_index = sess->sw_if_index;
1231             u64 sess_timeout_time =
1232               sess->last_active_time + fa_session_get_timeout (am, sess);
1233             if (now < sess_timeout_time)
1234               {
1235                 /* clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
1236                    (int) session_index); */
1237
1238                 /* There was activity on the session, so the idle timeout
1239                    has not passed. Enqueue for another time period. */
1240
1241                 acl_fa_conn_list_add_session(am, session_index, now);
1242
1243                 /* FIXME: When/if moving to timer wheel,
1244                    pretend we did this in the past,
1245                    at last_active moment, so the timer is accurate */
1246                 am->fa_cleaner_cnt_timer_restarted++;
1247               }
1248             else
1249               {
1250                 /* clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
1251                    (int) session_index); */
1252                 acl_fa_delete_session (am, sw_if_index, session_index);
1253                 count_deleted_sessions++;
1254               }
1255           }
1256         else
1257           {
1258             count_already_deleted++;
1259           }
1260       }
1261       if (expired)
1262         _vec_len (expired) = 0;
1263       if (event_data)
1264         _vec_len (event_data) = 0;
1265
1266       if (count_deleted_sessions > am->fa_max_deleted_sessions_per_interval) {
1267         /* if there was too many sessions to delete, do less waiting around next time */
1268         am->fa_current_cleaner_timer_wait_interval /= 2;
1269       } else if (count_deleted_sessions < am->fa_min_deleted_sessions_per_interval) {
1270         /* Too few deleted sessions, slowly increase the amount of sleep up to a limit */
1271         if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1272           am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1273       }
1274       am->fa_cleaner_cnt_event_cycles++;
1275       am->fa_cleaner_cnt_deleted_sessions += count_deleted_sessions;
1276       am->fa_cleaner_cnt_already_deleted += count_already_deleted;
1277     }
1278   /* NOT REACHED */
1279   return 0;
1280 }
1281
1282
1283 void
1284 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1285 {
1286   acl_main_t *am = &acl_main;
1287   if (is_input)
1288     {
1289       vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1290                                    sw_if_index, enable_disable, 0, 0);
1291       vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1292                                    sw_if_index, enable_disable, 0, 0);
1293       am->fa_in_acl_on_sw_if_index =
1294         clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1295                          enable_disable);
1296     }
1297   else
1298     {
1299       vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1300                                    sw_if_index, enable_disable, 0, 0);
1301       vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1302                                    sw_if_index, enable_disable, 0, 0);
1303       am->fa_out_acl_on_sw_if_index =
1304         clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1305                          enable_disable);
1306     }
1307   if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1308       && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1309     {
1310       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1311                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1312                                  sw_if_index);
1313     }
1314 }
1315
1316
1317
1318 /* *INDENT-OFF* */
1319
1320
1321 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1322   .function = acl_fa_session_cleaner_process,
1323   .type = VLIB_NODE_TYPE_PROCESS,
1324   .name = "acl-plugin-fa-cleaner-process",
1325   .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1326   .error_strings = acl_fa_cleaner_error_strings,
1327   .n_next_nodes = 0,
1328   .next_nodes = {},
1329 };
1330
1331
1332 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1333 {
1334   .function = acl_in_ip6_l2_node_fn,
1335   .name = "acl-plugin-in-ip6-l2",
1336   .vector_size = sizeof (u32),
1337   .format_trace = format_acl_fa_trace,
1338   .type = VLIB_NODE_TYPE_INTERNAL,
1339   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1340   .error_strings = acl_fa_error_strings,
1341   .n_next_nodes = ACL_FA_N_NEXT,
1342   .next_nodes =
1343   {
1344     [ACL_FA_ERROR_DROP] = "error-drop",
1345   }
1346 };
1347
1348 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1349 {
1350   .function = acl_in_ip4_l2_node_fn,
1351   .name = "acl-plugin-in-ip4-l2",
1352   .vector_size = sizeof (u32),
1353   .format_trace = format_acl_fa_trace,
1354   .type = VLIB_NODE_TYPE_INTERNAL,
1355   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1356   .error_strings = acl_fa_error_strings,
1357   .n_next_nodes = ACL_FA_N_NEXT,
1358   .next_nodes =
1359   {
1360     [ACL_FA_ERROR_DROP] = "error-drop",
1361   }
1362 };
1363
1364 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1365 {
1366   .function = acl_out_ip6_l2_node_fn,
1367   .name = "acl-plugin-out-ip6-l2",
1368   .vector_size = sizeof (u32),
1369   .format_trace = format_acl_fa_trace,
1370   .type = VLIB_NODE_TYPE_INTERNAL,
1371   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1372   .error_strings = acl_fa_error_strings,
1373   .n_next_nodes = ACL_FA_N_NEXT,
1374   .next_nodes =
1375   {
1376     [ACL_FA_ERROR_DROP] = "error-drop",
1377   }
1378 };
1379
1380 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1381 {
1382   .function = acl_out_ip4_l2_node_fn,
1383   .name = "acl-plugin-out-ip4-l2",
1384   .vector_size = sizeof (u32),
1385   .format_trace = format_acl_fa_trace,
1386   .type = VLIB_NODE_TYPE_INTERNAL,
1387   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1388   .error_strings = acl_fa_error_strings,
1389   .n_next_nodes = ACL_FA_N_NEXT,
1390   .next_nodes =
1391   {
1392     [ACL_FA_ERROR_DROP] = "error-drop",
1393   }
1394 };
1395
1396
1397 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1398 {
1399   .function = acl_in_ip6_fa_node_fn,
1400   .name = "acl-plugin-in-ip6-fa",
1401   .vector_size = sizeof (u32),
1402   .format_trace = format_acl_fa_trace,
1403   .type = VLIB_NODE_TYPE_INTERNAL,
1404   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1405   .error_strings = acl_fa_error_strings,
1406   .n_next_nodes = ACL_FA_N_NEXT,
1407   .next_nodes =
1408   {
1409     [ACL_FA_ERROR_DROP] = "error-drop",
1410   }
1411 };
1412
1413 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1414 {
1415   .arc_name = "ip6-unicast",
1416   .node_name = "acl-plugin-in-ip6-fa",
1417   .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1418 };
1419
1420 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1421 {
1422   .function = acl_in_ip4_fa_node_fn,
1423   .name = "acl-plugin-in-ip4-fa",
1424   .vector_size = sizeof (u32),
1425   .format_trace = format_acl_fa_trace,
1426   .type = VLIB_NODE_TYPE_INTERNAL,
1427   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1428   .error_strings = acl_fa_error_strings,
1429   .n_next_nodes = ACL_FA_N_NEXT,
1430   .next_nodes =
1431   {
1432     [ACL_FA_ERROR_DROP] = "error-drop",
1433   }
1434 };
1435
1436 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1437 {
1438   .arc_name = "ip4-unicast",
1439   .node_name = "acl-plugin-in-ip4-fa",
1440   .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1441 };
1442
1443
1444 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1445 {
1446   .function = acl_out_ip6_fa_node_fn,
1447   .name = "acl-plugin-out-ip6-fa",
1448   .vector_size = sizeof (u32),
1449   .format_trace = format_acl_fa_trace,
1450   .type = VLIB_NODE_TYPE_INTERNAL,
1451   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1452   .error_strings = acl_fa_error_strings,
1453   .n_next_nodes = ACL_FA_N_NEXT,
1454   .next_nodes =
1455   {
1456     [ACL_FA_ERROR_DROP] = "error-drop",
1457   }
1458 };
1459
1460 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1461 {
1462   .arc_name = "ip6-output",
1463   .node_name = "acl-plugin-out-ip6-fa",
1464   .runs_before = VNET_FEATURES ("interface-output"),
1465 };
1466
1467 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1468 {
1469   .function = acl_out_ip4_fa_node_fn,
1470   .name = "acl-plugin-out-ip4-fa",
1471   .vector_size = sizeof (u32),
1472   .format_trace = format_acl_fa_trace,
1473   .type = VLIB_NODE_TYPE_INTERNAL,
1474   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1475   .error_strings = acl_fa_error_strings,
1476   .n_next_nodes = ACL_FA_N_NEXT,
1477     /* edit / add dispositions here */
1478   .next_nodes =
1479   {
1480     [ACL_FA_ERROR_DROP] = "error-drop",
1481   }
1482 };
1483
1484 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1485 {
1486   .arc_name = "ip4-output",
1487   .node_name = "acl-plugin-out-ip4-fa",
1488   .runs_before = VNET_FEATURES ("interface-output"),
1489 };
1490
1491
1492 /* *INDENT-ON* */