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