misc: Purge unused pg includes
[vpp.git] / src / plugins / acl / sess_mgmt_node.c
1 /*
2  * Copyright (c) 2016-2018 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 <vppinfra/error.h>
21
22
23 #include <acl/acl.h>
24 #include <vnet/ip/icmp46_packet.h>
25
26 #include <plugins/acl/fa_node.h>
27 #include <plugins/acl/acl.h>
28 #include <plugins/acl/lookup_context.h>
29 #include <plugins/acl/public_inlines.h>
30 #include <plugins/acl/session_inlines.h>
31
32
33
34 static_always_inline u8 *
35 format_ip46_session_bihash_kv (u8 * s, va_list * args, int is_ip6)
36 {
37   fa_5tuple_t a5t;
38   void *paddr0;
39   void *paddr1;
40   void *format_addr_func;
41
42   if (is_ip6)
43     {
44       clib_bihash_kv_40_8_t *kv_40_8 =
45         va_arg (*args, clib_bihash_kv_40_8_t *);
46       a5t.kv_40_8 = *kv_40_8;
47       paddr0 = &a5t.ip6_addr[0];
48       paddr1 = &a5t.ip6_addr[1];
49       format_addr_func = format_ip6_address;
50     }
51   else
52     {
53       clib_bihash_kv_16_8_t *kv_16_8 =
54         va_arg (*args, clib_bihash_kv_16_8_t *);
55       a5t.kv_16_8 = *kv_16_8;
56       paddr0 = &a5t.ip4_addr[0];
57       paddr1 = &a5t.ip4_addr[1];
58       format_addr_func = format_ip4_address;
59     }
60
61   fa_full_session_id_t *sess = (fa_full_session_id_t *) & a5t.pkt;
62
63   return (format (s, "l3 %U -> %U %U | sess id %d thread id %d epoch %04x",
64                   format_addr_func, paddr0,
65                   format_addr_func, paddr1,
66                   format_fa_session_l4_key, &a5t.l4,
67                   sess->session_index, sess->thread_index,
68                   sess->intf_policy_epoch));
69 }
70
71 static u8 *
72 format_ip6_session_bihash_kv (u8 * s, va_list * args)
73 {
74   return format_ip46_session_bihash_kv (s, args, 1);
75 }
76
77 static u8 *
78 format_ip4_session_bihash_kv (u8 * s, va_list * args)
79 {
80   return format_ip46_session_bihash_kv (s, args, 0);
81 }
82
83
84 static void
85 acl_fa_verify_init_sessions (acl_main_t * am)
86 {
87   if (!am->fa_sessions_hash_is_initialized)
88     {
89       u16 wk;
90       /* Allocate the per-worker sessions pools */
91       for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
92         {
93           acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
94
95           /*
96            * // In lieu of trying to preallocate the pool and its free bitmap, rather use pool_init_fixed
97            * pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
98            * clib_bitmap_validate(pool_header(pw->fa_sessions_pool)->free_bitmap, am->fa_conn_table_max_entries);
99            */
100           pool_init_fixed (pw->fa_sessions_pool,
101                            am->fa_conn_table_max_entries);
102         }
103
104       /* ... and the interface session hash table */
105       clib_bihash_init_40_8 (&am->fa_ip6_sessions_hash,
106                              "ACL plugin FA IPv6 session bihash",
107                              am->fa_conn_table_hash_num_buckets,
108                              am->fa_conn_table_hash_memory_size);
109       clib_bihash_set_kvp_format_fn_40_8 (&am->fa_ip6_sessions_hash,
110                                           format_ip6_session_bihash_kv);
111
112       clib_bihash_init_16_8 (&am->fa_ip4_sessions_hash,
113                              "ACL plugin FA IPv4 session bihash",
114                              am->fa_conn_table_hash_num_buckets,
115                              am->fa_conn_table_hash_memory_size);
116       clib_bihash_set_kvp_format_fn_16_8 (&am->fa_ip4_sessions_hash,
117                                           format_ip4_session_bihash_kv);
118
119       am->fa_sessions_hash_is_initialized = 1;
120     }
121 }
122
123
124 /*
125  * Get the timeout of the session in a list since its enqueue time.
126  */
127
128 static u64
129 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
130 {
131   u64 timeout = am->vlib_main->clib_time.clocks_per_second / 1000;
132   timeout = fa_session_get_timeout (am, sess);
133   /* for all user lists, check them twice per timeout */
134   timeout >>= (sess->link_list_id != ACL_TIMEOUT_PURGATORY);
135   return timeout;
136 }
137
138 static u64
139 acl_fa_get_list_head_expiry_time (acl_main_t * am,
140                                   acl_fa_per_worker_data_t * pw, u64 now,
141                                   u16 thread_index, int timeout_type)
142 {
143   return pw->fa_conn_list_head_expiry_time[timeout_type];
144 }
145
146 static int
147 acl_fa_conn_time_to_check (acl_main_t * am, acl_fa_per_worker_data_t * pw,
148                            u64 now, u16 thread_index, u32 session_index)
149 {
150   if (session_index == FA_SESSION_BOGUS_INDEX)
151     return 0;
152   fa_session_t *sess = get_session_ptr (am, thread_index, session_index);
153   u64 timeout_time =
154     sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
155   return (timeout_time < now)
156     || (sess->link_enqueue_time <= pw->swipe_end_time);
157 }
158
159 /*
160  * see if there are sessions ready to be checked,
161  * do the maintenance (requeue or delete), and
162  * return the total number of sessions reclaimed.
163  */
164 static int
165 acl_fa_check_idle_sessions (acl_main_t * am, u16 thread_index, u64 now)
166 {
167   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
168   fa_full_session_id_t fsid;
169   fsid.thread_index = thread_index;
170   int total_expired = 0;
171
172   /* let the other threads enqueue more requests while we process, if they like */
173   aclp_swap_wip_and_pending_session_change_requests (am, thread_index);
174   u64 *psr = NULL;
175
176   vec_foreach (psr, pw->wip_session_change_requests)
177   {
178     acl_fa_sess_req_t op = *psr >> 32;
179     fsid.session_index = *psr & 0xffffffff;
180     switch (op)
181       {
182       case ACL_FA_REQ_SESS_RESCHEDULE:
183         acl_fa_restart_timer_for_session (am, now, fsid);
184         break;
185       default:
186         /* do nothing */
187         break;
188       }
189   }
190   if (pw->wip_session_change_requests)
191     _vec_len (pw->wip_session_change_requests) = 0;
192
193
194   {
195     u8 tt = 0;
196     int n_pending_swipes = 0;
197     for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
198       {
199         int n_expired = 0;
200         while (n_expired < am->fa_max_deleted_sessions_per_interval)
201           {
202             fsid.session_index = pw->fa_conn_list_head[tt];
203             if (!acl_fa_conn_time_to_check
204                 (am, pw, now, thread_index, pw->fa_conn_list_head[tt]))
205               {
206                 break;
207               }
208             if (am->trace_sessions > 3)
209               {
210                 elog_acl_maybe_trace_X3 (am,
211                                          "acl_fa_check_idle_sessions: expire session %d in list %d on thread %d",
212                                          "i4i4i4", (u32) fsid.session_index,
213                                          (u32) tt, (u32) thread_index);
214               }
215             vec_add1 (pw->expired, fsid.session_index);
216             n_expired++;
217             acl_fa_conn_list_delete_session (am, fsid, now);
218           }
219       }
220     for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
221       {
222         u32 session_index = pw->fa_conn_list_head[tt];
223         if (session_index == FA_SESSION_BOGUS_INDEX)
224           break;
225         fa_session_t *sess =
226           get_session_ptr (am, thread_index, session_index);
227         n_pending_swipes += sess->link_enqueue_time <= pw->swipe_end_time;
228       }
229     if (n_pending_swipes == 0)
230       {
231         pw->swipe_end_time = 0;
232       }
233   }
234
235   u32 *psid = NULL;
236   vec_foreach (psid, pw->expired)
237   {
238     fsid.session_index = *psid;
239     if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
240       {
241         fa_session_t *sess =
242           get_session_ptr (am, thread_index, fsid.session_index);
243         u32 sw_if_index = sess->sw_if_index;
244         u64 sess_timeout_time =
245           sess->last_active_time + fa_session_get_timeout (am, sess);
246         int timeout_passed = (now >= sess_timeout_time);
247         int clearing_interface =
248           clib_bitmap_get (pw->pending_clear_sw_if_index_bitmap, sw_if_index);
249         if (am->trace_sessions > 3)
250           {
251             elog_acl_maybe_trace_X2 (am,
252                                      "acl_fa_check_idle_sessions: now %lu sess_timeout_time %lu",
253                                      "i8i8", now, sess_timeout_time);
254             elog_acl_maybe_trace_X4 (am,
255                                      "acl_fa_check_idle_sessions: session %d sw_if_index %d timeout_passed %d clearing_interface %d",
256                                      "i4i4i4i4", (u32) fsid.session_index,
257                                      (u32) sess->sw_if_index,
258                                      (u32) timeout_passed,
259                                      (u32) clearing_interface);
260           }
261         if (timeout_passed || clearing_interface)
262           {
263             if (acl_fa_two_stage_delete_session (am, sw_if_index, fsid, now))
264               {
265                 if (am->trace_sessions > 3)
266                   {
267                     elog_acl_maybe_trace_X2 (am,
268                                              "acl_fa_check_idle_sessions: deleted session %d sw_if_index %d",
269                                              "i4i4", (u32) fsid.session_index,
270                                              (u32) sess->sw_if_index);
271                   }
272                 /* the session has been put */
273                 pw->cnt_deleted_sessions++;
274               }
275             else
276               {
277                 /* the connection marked as deleted and put to purgatory */
278                 if (am->trace_sessions > 3)
279                   {
280                     elog_acl_maybe_trace_X2 (am,
281                                              "acl_fa_check_idle_sessions: session %d sw_if_index %d marked as deleted, put to purgatory",
282                                              "i4i4", (u32) fsid.session_index,
283                                              (u32) sess->sw_if_index);
284                   }
285               }
286           }
287         else
288
289           {
290             if (am->trace_sessions > 3)
291               {
292                 elog_acl_maybe_trace_X2 (am,
293                                          "acl_fa_check_idle_sessions: restart timer for session %d sw_if_index %d",
294                                          "i4i4", (u32) fsid.session_index,
295                                          (u32) sess->sw_if_index);
296               }
297             /* There was activity on the session, so the idle timeout
298                has not passed. Enqueue for another time period. */
299
300             acl_fa_conn_list_add_session (am, fsid, now);
301             pw->cnt_session_timer_restarted++;
302           }
303       }
304     else
305       {
306         pw->cnt_already_deleted_sessions++;
307       }
308   }
309   total_expired = vec_len (pw->expired);
310   /* zero out the vector which we have acted on */
311   if (pw->expired)
312     _vec_len (pw->expired) = 0;
313   /* if we were advancing and reached the end
314    * (no more sessions to recycle), reset the fast-forward timestamp */
315
316   if (pw->swipe_end_time && 0 == total_expired)
317     pw->swipe_end_time = 0;
318
319   elog_acl_maybe_trace_X1 (am,
320                            "acl_fa_check_idle_sessions: done, total sessions expired: %d",
321                            "i4", (u32) total_expired);
322   return (total_expired);
323 }
324
325 /*
326  * This process ensures the connection cleanup happens every so often
327  * even in absence of traffic, as well as provides general orchestration
328  * for requests like connection deletion on a given sw_if_index.
329  */
330
331
332 /* *INDENT-OFF* */
333 #define foreach_acl_fa_cleaner_error \
334 _(UNKNOWN_EVENT, "unknown event received")  \
335 /* end  of errors */
336
337 typedef enum
338 {
339 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
340   foreach_acl_fa_cleaner_error
341 #undef _
342     ACL_FA_CLEANER_N_ERROR,
343 } acl_fa_cleaner_error_t;
344
345 static char *acl_fa_cleaner_error_strings[] = {
346 #define _(sym,string) string,
347   foreach_acl_fa_cleaner_error
348 #undef _
349 };
350
351 /* *INDENT-ON* */
352
353 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
354 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
355
356 static void
357 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t * am,
358                            int thread_index)
359 {
360   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
361   if (!pw->interrupt_is_pending)
362     {
363       pw->interrupt_is_pending = 1;
364       vlib_node_set_interrupt_pending (vlib_mains[thread_index],
365                                        acl_fa_worker_session_cleaner_process_node.index);
366       elog_acl_maybe_trace_X1 (am,
367                                "send_one_worker_interrupt: send interrupt to worker %u",
368                                "i4", ((u32) thread_index));
369       /* if the interrupt was requested, mark that done. */
370       /* pw->interrupt_is_needed = 0; */
371       CLIB_MEMORY_BARRIER ();
372     }
373 }
374
375 void
376 aclp_post_session_change_request (acl_main_t * am, u32 target_thread,
377                                   u32 target_session, u32 request_type)
378 {
379   acl_fa_per_worker_data_t *pw_me =
380     &am->per_worker_data[os_get_thread_index ()];
381   acl_fa_per_worker_data_t *pw = &am->per_worker_data[target_thread];
382   clib_spinlock_lock_if_init (&pw->pending_session_change_request_lock);
383   /* vec_add1 might cause a reallocation */
384   vec_add1 (pw->pending_session_change_requests,
385             (((u64) request_type) << 32) | target_session);
386   pw->rcvd_session_change_requests++;
387   pw_me->sent_session_change_requests++;
388   if (vec_len (pw->pending_session_change_requests) == 1)
389     {
390       /* ensure the requests get processed */
391       send_one_worker_interrupt (am->vlib_main, am, target_thread);
392     }
393   clib_spinlock_unlock_if_init (&pw->pending_session_change_request_lock);
394 }
395
396 void
397 aclp_swap_wip_and_pending_session_change_requests (acl_main_t * am,
398                                                    u32 target_thread)
399 {
400   acl_fa_per_worker_data_t *pw = &am->per_worker_data[target_thread];
401   u64 *tmp;
402   clib_spinlock_lock_if_init (&pw->pending_session_change_request_lock);
403   tmp = pw->pending_session_change_requests;
404   pw->pending_session_change_requests = pw->wip_session_change_requests;
405   pw->wip_session_change_requests = tmp;
406   clib_spinlock_unlock_if_init (&pw->pending_session_change_request_lock);
407 }
408
409
410 static int
411 purgatory_has_connections (vlib_main_t * vm, acl_main_t * am,
412                            int thread_index)
413 {
414   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
415
416   return (FA_SESSION_BOGUS_INDEX !=
417           pw->fa_conn_list_head[ACL_TIMEOUT_PURGATORY]);
418
419 }
420
421
422 /*
423  * Per-worker thread interrupt-driven cleaner thread
424  * to clean idle connections if there are no packets
425  */
426 static uword
427 acl_fa_worker_conn_cleaner_process (vlib_main_t * vm,
428                                     vlib_node_runtime_t * rt,
429                                     vlib_frame_t * f)
430 {
431   acl_main_t *am = &acl_main;
432   u64 now = clib_cpu_time_now ();
433   u16 thread_index = os_get_thread_index ();
434   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
435   int num_expired;
436   elog_acl_maybe_trace_X1 (am,
437                            "acl_fa_worker_conn_cleaner interrupt: now %lu",
438                            "i8", now);
439   /* allow another interrupt to be queued */
440   pw->interrupt_is_pending = 0;
441   if (pw->clear_in_process)
442     {
443       if (0 == pw->swipe_end_time)
444         {
445           /*
446            * Someone has just set the flag to start clearing.
447            * we do this by combing through the connections up to a "time T"
448            * which is now, and requeueing everything except the expired
449            * connections and those matching the interface(s) being cleared.
450            */
451
452           /*
453            * first filter the sw_if_index bitmap that they want from us, by
454            * a bitmap of sw_if_index for which we actually have connections.
455            */
456           if ((pw->pending_clear_sw_if_index_bitmap == 0)
457               || (pw->serviced_sw_if_index_bitmap == 0))
458             {
459               elog_acl_maybe_trace_X1 (am,
460                                        "acl_fa_worker_conn_cleaner: now %lu, someone tried to call clear but one of the bitmaps are empty",
461                                        "i8", now);
462               clib_bitmap_zero (pw->pending_clear_sw_if_index_bitmap);
463             }
464           else
465             {
466 #ifdef FA_NODE_VERBOSE_DEBUG
467               clib_warning
468                 ("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
469                  format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
470                  format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
471 #endif
472               pw->pending_clear_sw_if_index_bitmap =
473                 clib_bitmap_and (pw->pending_clear_sw_if_index_bitmap,
474                                  pw->serviced_sw_if_index_bitmap);
475             }
476
477           if (clib_bitmap_is_zero (pw->pending_clear_sw_if_index_bitmap))
478             {
479               /* if the cross-section is a zero vector, no need to do anything. */
480               elog_acl_maybe_trace_X1 (am,
481                                        "acl_fa_worker_conn_cleaner: now %lu, clearing done, nothing to do",
482                                        "i8", now);
483               pw->clear_in_process = 0;
484               pw->swipe_end_time = 0;
485             }
486           else
487             {
488 #ifdef FA_NODE_VERBOSE_DEBUG
489               clib_warning
490                 ("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
491                  format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
492                  format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
493 #endif
494               elog_acl_maybe_trace_X1 (am,
495                                        "acl_fa_worker_conn_cleaner: swiping until %lu",
496                                        "i8", now);
497               /* swipe through the connection lists until enqueue timestamps become above "now" */
498               pw->swipe_end_time = now;
499             }
500         }
501     }
502   num_expired = acl_fa_check_idle_sessions (am, thread_index, now);
503   // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
504   elog_acl_maybe_trace_X2 (am,
505                            "acl_fa_worker_conn_cleaner: checked %d sessions (clear_in_process: %d)",
506                            "i4i4", (u32) num_expired,
507                            (u32) pw->clear_in_process);
508   if (pw->clear_in_process)
509     {
510       if (pw->swipe_end_time == 0)
511         {
512           /* we were clearing but we could not process any more connections. time to stop. */
513           clib_bitmap_zero (pw->pending_clear_sw_if_index_bitmap);
514           pw->clear_in_process = 0;
515           elog_acl_maybe_trace_X1 (am,
516                                    "acl_fa_worker_conn_cleaner: now %lu, clearing done - all done",
517                                    "i8", now);
518         }
519       else
520         {
521           elog_acl_maybe_trace_X1 (am,
522                                    "acl_fa_worker_conn_cleaner: now %lu, more work to do - requesting interrupt",
523                                    "i8", now);
524           /* should continue clearing.. So could they please sent an interrupt again? */
525           send_one_worker_interrupt (vm, am, thread_index);
526           // pw->interrupt_is_needed = 1;
527         }
528     }
529   else
530     {
531       if (num_expired > 0)
532         {
533           /* there was too much work, we should get an interrupt ASAP */
534           // pw->interrupt_is_needed = 1;
535           send_one_worker_interrupt (vm, am, thread_index);
536           pw->interrupt_is_unwanted = 0;
537         }
538       else
539         {
540           /* the current rate of interrupts is ok */
541           pw->interrupt_is_needed = 0;
542           pw->interrupt_is_unwanted = 0;
543         }
544       elog_acl_maybe_trace_X3 (am,
545                                "acl_fa_worker_conn_cleaner: now %lu, interrupt needed: %u, interrupt unwanted: %u",
546                                "i8i4i4", now, ((u32) pw->interrupt_is_needed),
547                                ((u32) pw->interrupt_is_unwanted));
548     }
549   /* be persistent about quickly deleting the connections from the purgatory */
550   if (purgatory_has_connections (vm, am, thread_index))
551     {
552       send_one_worker_interrupt (vm, am, thread_index);
553     }
554   pw->interrupt_generation = am->fa_interrupt_generation;
555   return 0;
556 }
557
558 static void
559 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t * am)
560 {
561   int i;
562   /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
563   int n_threads = vec_len (vlib_mains);
564   for (i = 0; i < n_threads; i++)
565     {
566       send_one_worker_interrupt (vm, am, i);
567     }
568 }
569
570 /* centralized process to drive per-worker cleaners */
571 static uword
572 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
573                                 vlib_frame_t * f)
574 {
575   acl_main_t *am = &acl_main;
576   u64 now;
577   f64 cpu_cps = vm->clib_time.clocks_per_second;
578   u64 next_expire;
579   /* We should check if there are connections to clean up - at least twice a second */
580   u64 max_timer_wait_interval = cpu_cps / 2;
581   uword event_type, *event_data = 0;
582   acl_fa_per_worker_data_t *pw0;
583
584   am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
585   am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
586   am->fa_interrupt_generation = 1;
587   while (1)
588     {
589       now = clib_cpu_time_now ();
590       next_expire = now + am->fa_current_cleaner_timer_wait_interval;
591       int has_pending_conns = 0;
592       u16 ti;
593       u8 tt;
594
595       /*
596        * walk over all per-thread list heads of different timeouts,
597        * and see if there are any connections pending.
598        * If there aren't - we do not need to wake up until the
599        * worker code signals that it has added a connection.
600        *
601        * Also, while we are at it, calculate the earliest we need to wake up.
602        */
603       for (ti = 0; ti < vec_len (vlib_mains); ti++)
604         {
605           if (ti >= vec_len (am->per_worker_data))
606             {
607               continue;
608             }
609           acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
610           for (tt = 0; tt < vec_len (pw->fa_conn_list_head); tt++)
611             {
612               u64 head_expiry =
613                 acl_fa_get_list_head_expiry_time (am, pw, now, ti, tt);
614               if ((head_expiry < next_expire) && !pw->interrupt_is_pending)
615                 {
616                   elog_acl_maybe_trace_X3 (am,
617                                            "acl_fa_session_cleaner_process: now %lu, worker: %u tt: %u",
618                                            "i8i2i2", now, ti, tt);
619                   elog_acl_maybe_trace_X2 (am,
620                                            "acl_fa_session_cleaner_process: head expiry: %lu, is earlier than curr next expire: %lu",
621                                            "i8i8", head_expiry, next_expire);
622                   next_expire = head_expiry;
623                 }
624               if (FA_SESSION_BOGUS_INDEX != pw->fa_conn_list_head[tt])
625                 {
626                   has_pending_conns = 1;
627                 }
628             }
629         }
630
631       /* If no pending connections and no ACL applied then no point in timing out */
632       if (!has_pending_conns && (0 == am->fa_total_enabled_count))
633         {
634           am->fa_cleaner_cnt_wait_without_timeout++;
635           elog_acl_maybe_trace_X1 (am,
636                                    "acl_conn_cleaner: now %lu entering wait without timeout",
637                                    "i8", now);
638           (void) vlib_process_wait_for_event (vm);
639           event_type = vlib_process_get_events (vm, &event_data);
640         }
641       else
642         {
643           f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
644           if (timeout <= 0)
645             {
646               /* skip waiting altogether */
647               event_type = ~0;
648             }
649           else
650             {
651               am->fa_cleaner_cnt_wait_with_timeout++;
652               elog_acl_maybe_trace_X2 (am,
653                                        "acl_conn_cleaner: now %lu entering wait with timeout %.6f sec",
654                                        "i8f8", now, timeout);
655               (void) vlib_process_wait_for_event_or_clock (vm, timeout);
656               event_type = vlib_process_get_events (vm, &event_data);
657             }
658         }
659
660       switch (event_type)
661         {
662         case ~0:
663           /* nothing to do */
664           break;
665         case ACL_FA_CLEANER_RESCHEDULE:
666           /* Nothing to do. */
667           break;
668         case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
669           {
670             uword *clear_sw_if_index_bitmap = 0;
671             uword *sw_if_index0;
672             int clear_all = 0;
673             now = clib_cpu_time_now ();
674             elog_acl_maybe_trace_X1 (am,
675                                      "acl_fa_session_cleaner_process: now %lu, received ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX",
676                                      "i8", now);
677             vec_foreach (sw_if_index0, event_data)
678             {
679               am->fa_cleaner_cnt_delete_by_sw_index++;
680               elog_acl_maybe_trace_X1 (am,
681                                        "acl_fa_session_cleaner_process: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX %u",
682                                        "i4", *sw_if_index0);
683               if (*sw_if_index0 == ~0)
684                 {
685                   clear_all = 1;
686                 }
687               else
688                 {
689                   if (!pool_is_free_index
690                       (am->vnet_main->interface_main.sw_interfaces,
691                        *sw_if_index0))
692                     {
693                       clear_sw_if_index_bitmap =
694                         clib_bitmap_set (clear_sw_if_index_bitmap,
695                                          *sw_if_index0, 1);
696                     }
697                 }
698             }
699             acl_log_info
700               ("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U, clear_all: %u",
701                format_bitmap_hex, clear_sw_if_index_bitmap, clear_all);
702             vec_foreach (pw0, am->per_worker_data)
703             {
704               CLIB_MEMORY_BARRIER ();
705               while (pw0->clear_in_process)
706                 {
707                   CLIB_MEMORY_BARRIER ();
708                   elog_acl_maybe_trace_X1 (am,
709                                            "ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %u",
710                                            "i4",
711                                            (u32) (pw0 - am->per_worker_data));
712                   vlib_process_suspend (vm, 0.0001);
713                   if (pw0->interrupt_is_needed)
714                     {
715                       send_one_worker_interrupt (vm, am,
716                                                  (pw0 - am->per_worker_data));
717                     }
718                 }
719               if (pw0->clear_in_process)
720                 {
721                   acl_log_err
722                     ("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
723                 }
724               else
725                 {
726                   if (clear_all)
727                     {
728                       /* if we need to clear all, then just clear the interfaces that we are servicing */
729                       pw0->pending_clear_sw_if_index_bitmap =
730                         clib_bitmap_dup (pw0->serviced_sw_if_index_bitmap);
731                     }
732                   else
733                     {
734                       pw0->pending_clear_sw_if_index_bitmap =
735                         clib_bitmap_dup (clear_sw_if_index_bitmap);
736                     }
737                   acl_log_info
738                     ("ACL_FA_CLEANER: thread %u, pending clear bitmap: %U",
739                      (am->per_worker_data - pw0), format_bitmap_hex,
740                      pw0->pending_clear_sw_if_index_bitmap);
741                   pw0->clear_in_process = 1;
742                 }
743             }
744             /* send some interrupts so they can start working */
745             send_interrupts_to_workers (vm, am);
746
747             /* now wait till they all complete */
748             acl_log_info ("CLEANER mains len: %u per-worker len: %d",
749                           vec_len (vlib_mains),
750                           vec_len (am->per_worker_data));
751             vec_foreach (pw0, am->per_worker_data)
752             {
753               CLIB_MEMORY_BARRIER ();
754               while (pw0->clear_in_process)
755                 {
756                   CLIB_MEMORY_BARRIER ();
757                   elog_acl_maybe_trace_X1 (am,
758                                            "ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %u",
759                                            "i4",
760                                            (u32) (pw0 - am->per_worker_data));
761                   vlib_process_suspend (vm, 0.0001);
762                   if (pw0->interrupt_is_needed)
763                     {
764                       send_one_worker_interrupt (vm, am,
765                                                  (pw0 - am->per_worker_data));
766                     }
767                 }
768             }
769             acl_log_info ("ACL_FA_NODE_CLEAN: cleaning done");
770             clib_bitmap_free (clear_sw_if_index_bitmap);
771           }
772           am->fa_cleaner_cnt_delete_by_sw_index_ok++;
773           break;
774         default:
775 #ifdef FA_NODE_VERBOSE_DEBUG
776           clib_warning ("ACL plugin connection cleaner: unknown event %u",
777                         event_type);
778 #endif
779           vlib_node_increment_counter (vm,
780                                        acl_fa_session_cleaner_process_node.
781                                        index,
782                                        ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
783           am->fa_cleaner_cnt_unknown_event++;
784           break;
785         }
786
787       send_interrupts_to_workers (vm, am);
788
789       if (event_data)
790         _vec_len (event_data) = 0;
791
792       /*
793        * If the interrupts were not processed yet, ensure we wait a bit,
794        * but up to a point.
795        */
796       int need_more_wait = 0;
797       int max_wait_cycles = 100;
798       do
799         {
800           need_more_wait = 0;
801           vec_foreach (pw0, am->per_worker_data)
802           {
803             if (pw0->interrupt_generation != am->fa_interrupt_generation)
804               {
805                 need_more_wait = 1;
806               }
807           }
808           if (need_more_wait)
809             {
810               vlib_process_suspend (vm, 0.0001);
811             }
812         }
813       while (need_more_wait && (--max_wait_cycles > 0));
814
815       int interrupts_needed = 0;
816       int interrupts_unwanted = 0;
817
818       vec_foreach (pw0, am->per_worker_data)
819       {
820         if (pw0->interrupt_is_needed)
821           {
822             interrupts_needed++;
823             /* the per-worker value is reset when sending the interrupt */
824           }
825         if (pw0->interrupt_is_unwanted)
826           {
827             interrupts_unwanted++;
828             pw0->interrupt_is_unwanted = 0;
829           }
830       }
831       if (interrupts_needed)
832         {
833           /* they need more interrupts, do less waiting around next time */
834           am->fa_current_cleaner_timer_wait_interval /= 2;
835           /* never go into zero-wait either though - we need to give the space to others */
836           am->fa_current_cleaner_timer_wait_interval += 1;
837         }
838       else if (interrupts_unwanted)
839         {
840           /* slowly increase the amount of sleep up to a limit */
841           if (am->fa_current_cleaner_timer_wait_interval <
842               max_timer_wait_interval)
843             am->fa_current_cleaner_timer_wait_interval +=
844               cpu_cps * am->fa_cleaner_wait_time_increment;
845         }
846       am->fa_cleaner_cnt_event_cycles++;
847       am->fa_interrupt_generation++;
848     }
849   /* NOT REACHED */
850   return 0;
851 }
852
853
854 void
855 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
856 {
857   acl_main_t *am = &acl_main;
858   if (enable_disable)
859     {
860       acl_fa_verify_init_sessions (am);
861       am->fa_total_enabled_count++;
862       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
863       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
864                                  ACL_FA_CLEANER_RESCHEDULE, 0);
865       clib_mem_set_heap (oldheap);
866     }
867   else
868     {
869       am->fa_total_enabled_count--;
870     }
871
872   if (is_input)
873     {
874       ASSERT (clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index) !=
875               enable_disable);
876       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
877       vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
878                                    sw_if_index, enable_disable, 0, 0);
879       vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
880                                    sw_if_index, enable_disable, 0, 0);
881       clib_mem_set_heap (oldheap);
882       am->fa_in_acl_on_sw_if_index =
883         clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
884                          enable_disable);
885     }
886   else
887     {
888       ASSERT (clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index) !=
889               enable_disable);
890       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
891       vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
892                                    sw_if_index, enable_disable, 0, 0);
893       vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
894                                    sw_if_index, enable_disable, 0, 0);
895       clib_mem_set_heap (oldheap);
896       am->fa_out_acl_on_sw_if_index =
897         clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
898                          enable_disable);
899     }
900   if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
901       && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
902     {
903 #ifdef FA_NODE_VERBOSE_DEBUG
904       clib_warning ("ENABLE-DISABLE: clean the connections on interface %d",
905                     sw_if_index);
906 #endif
907       void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
908       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
909                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
910                                  sw_if_index);
911       clib_mem_set_heap (oldheap);
912     }
913 }
914
915 void
916 show_fa_sessions_hash (vlib_main_t * vm, u32 verbose)
917 {
918   acl_main_t *am = &acl_main;
919   if (am->fa_sessions_hash_is_initialized)
920     {
921       vlib_cli_output (vm, "\nIPv6 Session lookup hash table:\n%U\n\n",
922                        format_bihash_40_8, &am->fa_ip6_sessions_hash,
923                        verbose);
924
925       vlib_cli_output (vm, "\nIPv4 Session lookup hash table:\n%U\n\n",
926                        format_bihash_16_8, &am->fa_ip4_sessions_hash,
927                        verbose);
928     }
929   else
930     {
931       vlib_cli_output (vm,
932                        "\nSession lookup hash table is not allocated.\n\n");
933     }
934 }
935
936
937 /* *INDENT-OFF* */
938
939 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
940   .function = acl_fa_worker_conn_cleaner_process,
941   .name = "acl-plugin-fa-worker-cleaner-process",
942   .type = VLIB_NODE_TYPE_INPUT,
943   .state = VLIB_NODE_STATE_INTERRUPT,
944 };
945
946 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
947   .function = acl_fa_session_cleaner_process,
948   .type = VLIB_NODE_TYPE_PROCESS,
949   .name = "acl-plugin-fa-cleaner-process",
950   .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
951   .error_strings = acl_fa_cleaner_error_strings,
952   .n_next_nodes = 0,
953   .next_nodes = {},
954 };
955
956
957 /* *INDENT-ON* */
958
959 /*
960  * fd.io coding-style-patch-verification: ON
961  *
962  * Local Variables:
963  * eval: (c-set-style "gnu")
964  * End:
965  */