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