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