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