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