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