2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <netinet/in.h>
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
25 #include <vnet/ip/icmp46_packet.h>
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>
32 #include <vppinfra/bihash_40_8.h>
33 #include <vppinfra/bihash_template.h>
34 #include <vppinfra/bihash_template.c>
41 u32 match_acl_in_index;
48 /* ICMPv4 invert type for stateful ACL */
49 static const u8 icmp4_invmap[] = {
50 [ICMP4_echo_reply] = ICMP4_echo_request + 1,
51 [ICMP4_timestamp_reply] = ICMP4_timestamp_request + 1,
52 [ICMP4_information_reply] = ICMP4_information_request + 1,
53 [ICMP4_address_mask_reply] = ICMP4_address_mask_request + 1
56 /* Supported ICMPv4 messages for session creation */
57 static const u8 icmp4_valid_new[] = {
58 [ICMP4_echo_request] = 1,
59 [ICMP4_timestamp_request] = 1,
60 [ICMP4_information_request] = 1,
61 [ICMP4_address_mask_request] = 1
64 /* ICMPv6 invert type for stateful ACL */
65 static const u8 icmp6_invmap[] = {
66 [ICMP6_echo_reply - 128] = ICMP6_echo_request + 1,
67 [ICMP6_node_information_response - 128] = ICMP6_node_information_request + 1
70 /* Supported ICMPv6 messages for session creation */
71 static const u8 icmp6_valid_new[] = {
72 [ICMP6_echo_request - 128] = 1,
73 [ICMP6_node_information_request - 128] = 1
76 /* IP4 and IP6 protocol numbers of ICMP */
77 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
80 format_fa_5tuple (u8 * s, va_list * args)
82 fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
84 return format(s, "lc_index %d (lsb16 of sw_if_index %d) l3 %s%s %U -> %U"
85 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
86 p5t->pkt.lc_index, p5t->l4.lsb_of_sw_if_index, p5t->pkt.is_ip6 ? "ip6" : "ip4",
87 p5t->pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
88 format_ip46_address, &p5t->addr[0], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
89 format_ip46_address, &p5t->addr[1], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
90 p5t->l4.proto, p5t->pkt.l4_valid,
91 p5t->l4.port[0], p5t->l4.port[1],
92 p5t->pkt.tcp_flags_valid ? "valid": "invalid",
94 p5t->pkt.flags_reserved);
98 format_acl_plugin_5tuple (u8 * s, va_list * args)
100 return format_fa_5tuple(s, args);
103 /* packet trace format function */
105 format_acl_fa_trace (u8 * s, va_list * args)
107 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
108 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
109 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
113 "acl-plugin: lc_index: %d, sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
114 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
115 t->lc_index, t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
116 t->match_rule_index, t->trace_bitmap,
117 t->packet_info[0], t->packet_info[1], t->packet_info[2],
118 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
120 /* Now also print out the packet_info in a form usable by humans */
121 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
126 #define foreach_acl_fa_error \
127 _(ACL_DROP, "ACL deny packets") \
128 _(ACL_PERMIT, "ACL permit packets") \
129 _(ACL_NEW_SESSION, "new sessions added") \
130 _(ACL_EXIST_SESSION, "existing session packets") \
131 _(ACL_CHECK, "checked packets") \
132 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
133 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
138 #define _(sym,str) ACL_FA_ERROR_##sym,
144 static char *acl_fa_error_strings[] = {
145 #define _(sym,string) string,
152 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
154 return am->fa_sessions_hash_is_initialized;
158 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
160 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
165 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
167 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
171 /* Session keys match the packets received, and mirror the packets sent */
173 acl_make_5tuple_session_key (acl_main_t * am, int is_input, int is_ip6,
174 u32 sw_if_index, fa_5tuple_t * p5tuple_pkt,
175 fa_5tuple_t * p5tuple_sess)
177 int src_index = is_input ? 0 : 1;
178 int dst_index = is_input ? 1 : 0;
179 u32 valid_new_sess = 1;
180 p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
181 p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
182 p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
184 if (PREDICT_TRUE(p5tuple_pkt->l4.proto != icmp_protos[is_ip6]))
186 p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
187 p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
191 static const u8 * icmp_invmap[] = { icmp4_invmap, icmp6_invmap };
192 static const u8 * icmp_valid_new[] = { icmp4_valid_new, icmp6_valid_new };
193 static const u8 icmp_invmap_size[] = { sizeof(icmp4_invmap),
194 sizeof(icmp6_invmap) };
195 static const u8 icmp_valid_new_size[] = { sizeof(icmp4_valid_new),
196 sizeof(icmp6_valid_new) };
197 int type = is_ip6 ? p5tuple_pkt->l4.port[0]-128: p5tuple_pkt->l4.port[0];
199 p5tuple_sess->l4.port[0] = p5tuple_pkt->l4.port[0];
200 p5tuple_sess->l4.port[1] = p5tuple_pkt->l4.port[1];
203 * Invert ICMP type for valid icmp_invmap messages:
204 * 1) input node with outbound ACL interface
205 * 2) output node with inbound ACL interface
208 if ((is_input && acl_fa_ifc_has_out_acl(am, sw_if_index)) ||
209 (!is_input && acl_fa_ifc_has_in_acl(am, sw_if_index)))
212 type <= icmp_invmap_size[is_ip6] &&
213 icmp_invmap[is_ip6][type])
215 p5tuple_sess->l4.port[0] = icmp_invmap[is_ip6][type] - 1;
220 * ONLY ICMP messages defined in icmp4_valid_new/icmp6_valid_new table
221 * are allowed to create stateful ACL.
222 * The other messages will be forwarded without creating a reflexive ACL.
225 type > icmp_valid_new_size[is_ip6] ||
226 !icmp_valid_new[is_ip6][type])
232 return valid_new_sess;
237 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
239 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
241 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
242 TCP_FLAGS_RSTFINACKSYN);
243 switch (sess->info.l4.proto)
246 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
248 return ACL_TIMEOUT_TCP_IDLE;
252 return ACL_TIMEOUT_TCP_TRANSIENT;
256 return ACL_TIMEOUT_UDP_IDLE;
259 return ACL_TIMEOUT_UDP_IDLE;
265 fa_session_get_shortest_timeout(acl_main_t * am)
269 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
270 if (timeout > am->session_timeout_sec[timeout_type]) {
271 timeout = am->session_timeout_sec[timeout_type];
278 * Get the timeout of the session in a list since its enqueue time.
282 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
284 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
286 * we have the shortest possible timeout type in all the lists
287 * (see README-multicore for the rationale)
289 timeout *= fa_session_get_shortest_timeout(am);
294 * Get the idle timeout of a session.
298 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
300 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
301 int timeout_type = fa_session_get_timeout_type (am, sess);
302 timeout *= am->session_timeout_sec[timeout_type];
307 acl_fa_verify_init_sessions (acl_main_t * am)
309 if (!am->fa_sessions_hash_is_initialized) {
311 /* Allocate the per-worker sessions pools */
312 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
313 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
316 * // In lieu of trying to preallocate the pool and its free bitmap, rather use pool_init_fixed
317 * pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
318 * clib_bitmap_validate(pool_header(pw->fa_sessions_pool)->free_bitmap, am->fa_conn_table_max_entries);
320 pool_init_fixed(pw->fa_sessions_pool, am->fa_conn_table_max_entries);
323 /* ... and the interface session hash table */
324 BV (clib_bihash_init) (&am->fa_sessions_hash,
325 "ACL plugin FA session bihash",
326 am->fa_conn_table_hash_num_buckets,
327 am->fa_conn_table_hash_memory_size);
328 am->fa_sessions_hash_is_initialized = 1;
332 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
334 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
335 fa_session_t *sess = pool_is_free_index (pw->fa_sessions_pool, session_index) ? 0 : pool_elt_at_index(pw->fa_sessions_pool, session_index);
339 static inline int is_valid_session_ptr(acl_main_t *am, u16 thread_index, fa_session_t *sess)
341 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
342 return ((sess != 0) && ((sess - pw->fa_sessions_pool) < pool_len(pw->fa_sessions_pool)));
346 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
348 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
349 u8 list_id = fa_session_get_timeout_type(am, sess);
350 uword thread_index = os_get_thread_index ();
351 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
352 /* the retrieved session thread index must be necessarily the same as the one in the key */
353 ASSERT (sess->thread_index == sess_id.thread_index);
354 /* the retrieved session thread index must be the same as current thread */
355 ASSERT (sess->thread_index == thread_index);
356 sess->link_enqueue_time = now;
357 sess->link_list_id = list_id;
358 sess->link_next_idx = ~0;
359 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
360 if (~0 != pw->fa_conn_list_tail[list_id]) {
361 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
362 prev_sess->link_next_idx = sess_id.session_index;
363 /* We should never try to link with a session on another thread */
364 ASSERT(prev_sess->thread_index == sess->thread_index);
366 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
368 #ifdef FA_NODE_VERBOSE_DEBUG
369 clib_warning("FA-SESSION-DEBUG: add session id %d on thread %d sw_if_index %d", sess_id.session_index, thread_index, sess->sw_if_index);
371 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
373 if (~0 == pw->fa_conn_list_head[list_id]) {
374 pw->fa_conn_list_head[list_id] = sess_id.session_index;
379 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
381 uword thread_index = os_get_thread_index ();
382 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
383 if (thread_index != sess_id.thread_index) {
384 /* If another thread attempts to delete the session, fail it. */
385 #ifdef FA_NODE_VERBOSE_DEBUG
386 clib_warning("thread id in key %d != curr thread index, not deleting");
390 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
391 /* we should never try to delete the session with another thread index */
392 ASSERT(sess->thread_index == thread_index);
393 if (~0 != sess->link_prev_idx) {
394 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
395 /* the previous session must be in the same list as this one */
396 ASSERT(prev_sess->link_list_id == sess->link_list_id);
397 prev_sess->link_next_idx = sess->link_next_idx;
399 if (~0 != sess->link_next_idx) {
400 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
401 /* The next session must be in the same list as the one we are deleting */
402 ASSERT(next_sess->link_list_id == sess->link_list_id);
403 next_sess->link_prev_idx = sess->link_prev_idx;
405 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
406 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
408 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
409 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
415 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
417 if (acl_fa_conn_list_delete_session(am, sess_id)) {
418 acl_fa_conn_list_add_session(am, sess_id, now);
422 * Our thread does not own this connection, so we can not delete
423 * The session. To avoid the complicated signaling, we simply
424 * pick the list waiting time to be the shortest of the timeouts.
425 * This way we do not have to do anything special, and let
426 * the regular requeue check take care of everything.
434 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
435 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
437 sess->last_active_time = now;
438 if (pkt_5tuple->pkt.tcp_flags_valid)
440 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
447 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
449 void *oldheap = clib_mem_set_heap(am->acl_mheap);
450 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
451 ASSERT(sess->thread_index == os_get_thread_index ());
452 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
454 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
455 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
456 /* Deleting from timer structures not needed,
457 as the caller must have dealt with the timers. */
458 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
459 clib_mem_set_heap (oldheap);
460 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
461 clib_smp_atomic_add(&am->fa_session_total_dels, 1);
465 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
468 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
469 return (curr_sess_count < am->fa_conn_table_max_entries);
473 acl_fa_get_list_head_expiry_time(acl_main_t *am, acl_fa_per_worker_data_t *pw, u64 now, u16 thread_index, int timeout_type)
475 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
477 * We can not check just the index here because inbetween the worker thread might
478 * dequeue the connection from the head just as we are about to check it.
480 if (!is_valid_session_ptr(am, thread_index, sess)) {
481 return ~0LL; // infinity.
484 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
490 acl_fa_conn_time_to_check (acl_main_t *am, acl_fa_per_worker_data_t *pw, u64 now, u16 thread_index, u32 session_index)
492 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
494 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
495 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
499 * see if there are sessions ready to be checked,
500 * do the maintenance (requeue or delete), and
501 * return the total number of sessions reclaimed.
504 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
506 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
507 fa_full_session_id_t fsid;
508 fsid.thread_index = thread_index;
509 int total_expired = 0;
513 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
514 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
515 && (~0 != pw->fa_conn_list_head[tt])
516 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
517 pw->fa_conn_list_head[tt]))) {
518 fsid.session_index = pw->fa_conn_list_head[tt];
519 elog_acl_maybe_trace_X2(am, "acl_fa_check_idle_sessions: expire session %d on thread %d", "i4i4", (u32)fsid.session_index, (u32)thread_index);
520 vec_add1(pw->expired, fsid.session_index);
521 acl_fa_conn_list_delete_session(am, fsid);
527 vec_foreach (psid, pw->expired)
529 fsid.session_index = *psid;
530 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
532 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
533 u32 sw_if_index = sess->sw_if_index;
534 u64 sess_timeout_time =
535 sess->last_active_time + fa_session_get_timeout (am, sess);
536 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
538 #ifdef FA_NODE_VERBOSE_DEBUG
539 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d, sw_if_index %d",
540 (int) fsid.session_index, sess->sw_if_index);
542 /* There was activity on the session, so the idle timeout
543 has not passed. Enqueue for another time period. */
545 acl_fa_conn_list_add_session(am, fsid, now);
546 pw->cnt_session_timer_restarted++;
550 #ifdef FA_NODE_VERBOSE_DEBUG
551 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d, sw_if_index %d",
552 (int) fsid.session_index, sess->sw_if_index);
554 acl_fa_delete_session (am, sw_if_index, fsid);
555 pw->cnt_deleted_sessions++;
560 pw->cnt_already_deleted_sessions++;
563 total_expired = vec_len(pw->expired);
564 /* zero out the vector which we have acted on */
566 _vec_len (pw->expired) = 0;
567 /* if we were advancing and reached the end
568 * (no more sessions to recycle), reset the fast-forward timestamp */
570 if (pw->swipe_end_time && 0 == total_expired)
571 pw->swipe_end_time = 0;
572 return (total_expired);
576 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
578 /* try to recycle a TCP transient session */
579 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
580 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
581 fa_full_session_id_t sess_id;
582 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
583 if (~0 != sess_id.session_index) {
584 sess_id.thread_index = thread_index;
585 acl_fa_conn_list_delete_session(am, sess_id);
586 acl_fa_delete_session(am, sw_if_index, sess_id);
590 static fa_session_t *
591 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
592 fa_5tuple_t * p5tuple, u16 current_policy_epoch)
594 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
595 clib_bihash_kv_40_8_t kv;
596 fa_full_session_id_t f_sess_id;
597 uword thread_index = os_get_thread_index();
598 void *oldheap = clib_mem_set_heap(am->acl_mheap);
599 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
601 f_sess_id.thread_index = thread_index;
604 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
605 f_sess_id.session_index = sess - pw->fa_sessions_pool;
606 f_sess_id.intf_policy_epoch = current_policy_epoch;
608 kv.key[0] = pkv->key[0];
609 kv.key[1] = pkv->key[1];
610 kv.key[2] = pkv->key[2];
611 kv.key[3] = pkv->key[3];
612 kv.key[4] = pkv->key[4];
613 kv.value = f_sess_id.as_u64;
615 memcpy (sess, pkv, sizeof (pkv->key));
616 sess->last_active_time = now;
617 sess->sw_if_index = sw_if_index;
618 sess->tcp_flags_seen.as_u16 = 0;
619 sess->thread_index = thread_index;
620 sess->link_list_id = ~0;
621 sess->link_prev_idx = ~0;
622 sess->link_next_idx = ~0;
626 ASSERT(am->fa_sessions_hash_is_initialized == 1);
627 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
629 acl_fa_conn_list_add_session(am, f_sess_id, now);
631 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
632 clib_mem_set_heap (oldheap);
633 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
634 clib_smp_atomic_add(&am->fa_session_total_adds, 1);
639 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
640 clib_bihash_kv_40_8_t * pvalue_sess)
642 return (clib_bihash_search_40_8 (&am->fa_sessions_hash, &p5tuple->kv, pvalue_sess) == 0);
647 acl_fa_node_fn (vlib_main_t * vm,
648 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
649 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
650 vlib_node_registration_t * acl_fa_node)
652 u32 n_left_from, *from, *to_next;
653 acl_fa_next_t next_index;
654 u32 pkts_acl_checked = 0;
655 u32 pkts_new_session = 0;
656 u32 pkts_exist_session = 0;
657 u32 pkts_acl_permit = 0;
658 u32 pkts_restart_session_timer = 0;
659 u32 trace_bitmap = 0;
660 acl_main_t *am = &acl_main;
661 fa_5tuple_t fa_5tuple, kv_sess;
662 clib_bihash_kv_40_8_t value_sess;
663 vlib_node_runtime_t *error_node;
664 u64 now = clib_cpu_time_now ();
665 uword thread_index = os_get_thread_index ();
666 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
668 from = vlib_frame_vector_args (frame);
669 n_left_from = frame->n_vectors;
670 next_index = node->cached_next_index;
672 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
674 while (n_left_from > 0)
678 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
680 while (n_left_from > 0 && n_left_to_next > 0)
688 int acl_check_needed = 1;
689 u32 match_acl_in_index = ~0;
690 u32 match_acl_pos = ~0;
691 u32 match_rule_index = ~0;
695 /* speculatively enqueue b0 to the current next frame */
703 b0 = vlib_get_buffer (vm, bi0);
706 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
708 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
711 lc_index0 = am->input_lc_index_by_sw_if_index[sw_if_index0];
713 lc_index0 = am->output_lc_index_by_sw_if_index[sw_if_index0];
715 u32 **p_epoch_vec = is_input ? &am->input_policy_epoch_by_sw_if_index
716 : &am->output_policy_epoch_by_sw_if_index;
717 u16 current_policy_epoch = sw_if_index0 < vec_len(*p_epoch_vec) ? vec_elt(*p_epoch_vec, sw_if_index0) : (is_input * FA_POLICY_EPOCH_IS_INPUT);
719 * Extract the L3/L4 matching info into a 5-tuple structure,
720 * then create a session key whose layout is independent on forward or reverse
721 * direction of the packet.
724 acl_plugin_fill_5tuple_inline (lc_index0, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_opaque_t *)&fa_5tuple);
725 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
726 valid_new_sess = acl_make_5tuple_session_key (am, is_input, is_ip6, sw_if_index0, &fa_5tuple, &kv_sess);
727 // XXDEL fa_5tuple.pkt.is_input = is_input;
728 fa_5tuple.pkt.mask_type_index_lsb = ~0;
729 #ifdef FA_NODE_VERBOSE_DEBUG
731 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx %016llx",
732 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
733 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
735 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx %016llx",
736 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
737 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
740 /* Try to match an existing session first */
742 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
744 if (acl_fa_find_session
745 (am, sw_if_index0, &kv_sess, &value_sess))
747 trace_bitmap |= 0x80000000;
748 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
749 fa_full_session_id_t f_sess_id;
751 f_sess_id.as_u64 = value_sess.value;
752 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
754 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
755 int old_timeout_type =
756 fa_session_get_timeout_type (am, sess);
758 acl_fa_track_session (am, is_input, sw_if_index0, now,
760 /* expose the session id to the tracer */
761 match_rule_index = f_sess_id.session_index;
762 int new_timeout_type =
763 fa_session_get_timeout_type (am, sess);
764 acl_check_needed = 0;
765 pkts_exist_session += 1;
766 /* Tracking might have changed the session timeout type, e.g. from transient to established */
767 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
769 acl_fa_restart_timer_for_session (am, now, f_sess_id);
770 pkts_restart_session_timer++;
772 0x00010000 + ((0xff & old_timeout_type) << 8) +
773 (0xff & new_timeout_type);
776 * I estimate the likelihood to be very low - the VPP needs
777 * to have >64K interfaces to start with and then on
778 * exactly 64K indices apart needs to be exactly the same
779 * 5-tuple... Anyway, since this probability is nonzero -
780 * print an error and drop the unlucky packet.
781 * If this shows up in real world, we would need to bump
782 * the hash key length.
784 if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
785 clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
786 acl_check_needed = 0;
789 if (PREDICT_FALSE(am->reclassify_sessions)) {
790 /* if the MSB of policy epoch matches but not the LSB means it is a stale session */
791 if ( (0 == ((current_policy_epoch ^ f_sess_id.intf_policy_epoch) & FA_POLICY_EPOCH_IS_INPUT))
792 && (current_policy_epoch != f_sess_id.intf_policy_epoch) ) {
793 /* delete session and increment the counter */
794 vec_validate (pw->fa_session_epoch_change_by_sw_if_index, sw_if_index0);
795 vec_elt (pw->fa_session_epoch_change_by_sw_if_index, sw_if_index0)++;
796 if(acl_fa_conn_list_delete_session(am, f_sess_id)) {
797 /* delete the session only if we were able to unlink it */
798 acl_fa_delete_session (am, sw_if_index0, f_sess_id);
800 acl_check_needed = 1;
801 trace_bitmap |= 0x40000000;
807 if (acl_check_needed)
809 action = 0; /* deny by default */
810 acl_plugin_match_5tuple_inline (lc_index0, (fa_5tuple_opaque_t *)&fa_5tuple,
811 is_ip6, &action, &match_acl_pos, &match_acl_in_index,
812 &match_rule_index, &trace_bitmap);
815 pkts_acl_permit += 1;
818 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
819 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
821 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
823 if (PREDICT_TRUE (valid_new_sess)) {
824 fa_session_t *sess = acl_fa_add_session (am, is_input,
826 now, &kv_sess, current_policy_epoch);
827 acl_fa_track_session (am, is_input, sw_if_index0, now,
829 pkts_new_session += 1;
832 * ICMP packets with non-icmp_valid_new type will be
833 * forwared without being dropped.
836 pkts_acl_permit += 1;
842 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
852 next0 = vnet_l2_feature_next (b0, l2_feat_next_node_index, 0);
854 vnet_feature_next (sw_if_index0, &next0, b0);
856 #ifdef FA_NODE_VERBOSE_DEBUG
857 clib_warning("ACL_FA_NODE_DBG: sw_if_index %d lc_index %d action %d acl_index %d rule_index %d", sw_if_index0, lc_index0, action, match_acl_in_index, match_rule_index);
860 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
861 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
863 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
864 t->sw_if_index = sw_if_index0;
865 t->lc_index = lc_index0;
866 t->next_index = next0;
867 t->match_acl_in_index = match_acl_in_index;
868 t->match_rule_index = match_rule_index;
869 t->packet_info[0] = fa_5tuple.kv.key[0];
870 t->packet_info[1] = fa_5tuple.kv.key[1];
871 t->packet_info[2] = fa_5tuple.kv.key[2];
872 t->packet_info[3] = fa_5tuple.kv.key[3];
873 t->packet_info[4] = fa_5tuple.kv.key[4];
874 t->packet_info[5] = fa_5tuple.kv.value;
876 t->trace_bitmap = trace_bitmap;
879 next0 = next0 < node->n_next_nodes ? next0 : 0;
881 b0->error = error_node->errors[error0];
883 pkts_acl_checked += 1;
885 /* verify speculative enqueue, maybe switch current next frame */
886 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
887 to_next, n_left_to_next, bi0,
891 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
894 vlib_node_increment_counter (vm, acl_fa_node->index,
895 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
896 vlib_node_increment_counter (vm, acl_fa_node->index,
897 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
898 vlib_node_increment_counter (vm, acl_fa_node->index,
899 ACL_FA_ERROR_ACL_NEW_SESSION,
901 vlib_node_increment_counter (vm, acl_fa_node->index,
902 ACL_FA_ERROR_ACL_EXIST_SESSION,
904 vlib_node_increment_counter (vm, acl_fa_node->index,
905 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
906 pkts_restart_session_timer);
907 return frame->n_vectors;
911 vlib_node_registration_t acl_in_l2_ip6_node;
913 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
914 vlib_node_runtime_t * node, vlib_frame_t * frame)
916 acl_main_t *am = &acl_main;
917 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
918 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
919 &acl_in_l2_ip6_node);
922 vlib_node_registration_t acl_in_l2_ip4_node;
924 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
925 vlib_node_runtime_t * node, vlib_frame_t * frame)
927 acl_main_t *am = &acl_main;
928 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
929 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
930 &acl_in_l2_ip4_node);
933 vlib_node_registration_t acl_out_l2_ip6_node;
935 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
936 vlib_node_runtime_t * node, vlib_frame_t * frame)
938 acl_main_t *am = &acl_main;
939 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
940 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
941 &acl_out_l2_ip6_node);
944 vlib_node_registration_t acl_out_l2_ip4_node;
946 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
947 vlib_node_runtime_t * node, vlib_frame_t * frame)
949 acl_main_t *am = &acl_main;
950 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
951 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
952 &acl_out_l2_ip4_node);
956 /**** L3 processing path nodes ****/
959 vlib_node_registration_t acl_in_fa_ip6_node;
961 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
962 vlib_node_runtime_t * node, vlib_frame_t * frame)
964 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
967 vlib_node_registration_t acl_in_fa_ip4_node;
969 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
970 vlib_node_runtime_t * node, vlib_frame_t * frame)
972 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
975 vlib_node_registration_t acl_out_fa_ip6_node;
977 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
978 vlib_node_runtime_t * node, vlib_frame_t * frame)
980 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
983 vlib_node_registration_t acl_out_fa_ip4_node;
985 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
986 vlib_node_runtime_t * node, vlib_frame_t * frame)
988 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
992 * This process ensures the connection cleanup happens every so often
993 * even in absence of traffic, as well as provides general orchestration
994 * for requests like connection deletion on a given sw_if_index.
999 #define foreach_acl_fa_cleaner_error \
1000 _(UNKNOWN_EVENT, "unknown event received") \
1005 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1006 foreach_acl_fa_cleaner_error
1008 ACL_FA_CLEANER_N_ERROR,
1009 } acl_fa_cleaner_error_t;
1011 static char *acl_fa_cleaner_error_strings[] = {
1012 #define _(sym,string) string,
1013 foreach_acl_fa_cleaner_error
1019 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1020 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1023 * Per-worker thread interrupt-driven cleaner thread
1024 * to clean idle connections if there are no packets
1027 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1028 vlib_node_runtime_t * rt, vlib_frame_t * f)
1030 acl_main_t *am = &acl_main;
1031 u64 now = clib_cpu_time_now ();
1032 u16 thread_index = os_get_thread_index ();
1033 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1035 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner interrupt: now %lu", "i8", now);
1036 /* allow another interrupt to be queued */
1037 pw->interrupt_is_pending = 0;
1038 if (pw->clear_in_process) {
1039 if (0 == pw->swipe_end_time) {
1041 * Someone has just set the flag to start clearing.
1042 * we do this by combing through the connections up to a "time T"
1043 * which is now, and requeueing everything except the expired
1044 * connections and those matching the interface(s) being cleared.
1048 * first filter the sw_if_index bitmap that they want from us, by
1049 * a bitmap of sw_if_index for which we actually have connections.
1051 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1052 || (pw->serviced_sw_if_index_bitmap == 0)) {
1053 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner: now %lu, someone tried to call clear but one of the bitmaps are empty", "i8", now);
1054 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1056 #ifdef FA_NODE_VERBOSE_DEBUG
1057 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1058 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1059 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1061 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1062 pw->serviced_sw_if_index_bitmap);
1065 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1066 /* if the cross-section is a zero vector, no need to do anything. */
1067 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner: now %lu, clearing done, nothing to do", "i8", now);
1068 pw->clear_in_process = 0;
1070 #ifdef FA_NODE_VERBOSE_DEBUG
1071 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1072 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1073 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1075 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner: swiping until %lu", "i8", now);
1076 /* swipe through the connection lists until enqueue timestamps become above "now" */
1077 pw->swipe_end_time = now;
1081 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1082 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1083 elog_acl_maybe_trace_X2(am, "acl_fa_worker_conn_cleaner: checked %d sessions (clear_in_process: %d)", "i4i4", (u32)num_expired, (u32)pw->clear_in_process);
1084 if (pw->clear_in_process) {
1085 if (0 == num_expired) {
1086 /* we were clearing but we could not process any more connections. time to stop. */
1087 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1088 pw->clear_in_process = 0;
1089 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner: now %lu, clearing done - all done", "i8", now);
1091 elog_acl_maybe_trace_X1(am, "acl_fa_worker_conn_cleaner: now %lu, more work to do - requesting interrupt", "i8", now);
1092 /* should continue clearing.. So could they please sent an interrupt again? */
1093 pw->interrupt_is_needed = 1;
1096 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1097 /* there was too much work, we should get an interrupt ASAP */
1098 pw->interrupt_is_needed = 1;
1099 pw->interrupt_is_unwanted = 0;
1100 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1101 /* signal that they should trigger us less */
1102 pw->interrupt_is_needed = 0;
1103 pw->interrupt_is_unwanted = 1;
1105 /* the current rate of interrupts is ok */
1106 pw->interrupt_is_needed = 0;
1107 pw->interrupt_is_unwanted = 0;
1109 elog_acl_maybe_trace_X3(am, "acl_fa_worker_conn_cleaner: now %lu, interrupt needed: %u, interrupt unwanted: %u", "i8i4i4", now, ((u32)pw->interrupt_is_needed), ((u32)pw->interrupt_is_unwanted));
1111 pw->interrupt_generation = am->fa_interrupt_generation;
1116 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1118 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1119 if (!pw->interrupt_is_pending) {
1120 pw->interrupt_is_pending = 1;
1121 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1122 acl_fa_worker_session_cleaner_process_node.index);
1123 elog_acl_maybe_trace_X1(am, "send_one_worker_interrupt: send interrupt to worker %d", "i4", ((u32)thread_index));
1124 /* if the interrupt was requested, mark that done. */
1125 /* pw->interrupt_is_needed = 0; */
1130 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1133 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1134 int n_threads = vec_len(vlib_mains);
1135 for (i = 0; i < n_threads; i++) {
1136 send_one_worker_interrupt(vm, am, i);
1140 /* centralized process to drive per-worker cleaners */
1142 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1145 acl_main_t *am = &acl_main;
1147 f64 cpu_cps = vm->clib_time.clocks_per_second;
1149 /* We should check if there are connections to clean up - at least twice a second */
1150 u64 max_timer_wait_interval = cpu_cps / 2;
1151 uword event_type, *event_data = 0;
1152 acl_fa_per_worker_data_t *pw0;
1154 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1155 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1156 am->fa_interrupt_generation = 1;
1159 now = clib_cpu_time_now ();
1160 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1161 int has_pending_conns = 0;
1166 * walk over all per-thread list heads of different timeouts,
1167 * and see if there are any connections pending.
1168 * If there aren't - we do not need to wake up until the
1169 * worker code signals that it has added a connection.
1171 * Also, while we are at it, calculate the earliest we need to wake up.
1173 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1174 if (ti >= vec_len(am->per_worker_data)) {
1177 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1178 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1179 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1180 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1181 elog_acl_maybe_trace_X3(am, "acl_fa_session_cleaner_process: now %lu, worker: %d tt: %d", "i8i2i2", now, ti, tt);
1182 elog_acl_maybe_trace_X2(am, "acl_fa_session_cleaner_process: head expiry: %lu, is earlier than curr next expire: %lu", "i8i8", head_expiry, next_expire);
1183 next_expire = head_expiry;
1185 if (~0 != pw->fa_conn_list_head[tt]) {
1186 has_pending_conns = 1;
1191 /* If no pending connections and no ACL applied then no point in timing out */
1192 if (!has_pending_conns && (0 == am->fa_total_enabled_count))
1194 am->fa_cleaner_cnt_wait_without_timeout++;
1195 elog_acl_maybe_trace_X1(am, "acl_conn_cleaner: now %lu entering wait without timeout", "i8", now);
1196 (void) vlib_process_wait_for_event (vm);
1197 event_type = vlib_process_get_events (vm, &event_data);
1201 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1204 /* skip waiting altogether */
1209 am->fa_cleaner_cnt_wait_with_timeout++;
1210 elog_acl_maybe_trace_X2(am, "acl_conn_cleaner: now %lu entering wait with timeout %.6f sec", "i8f8", now, timeout);
1211 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1212 event_type = vlib_process_get_events (vm, &event_data);
1221 case ACL_FA_CLEANER_RESCHEDULE:
1222 /* Nothing to do. */
1224 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1226 uword *clear_sw_if_index_bitmap = 0;
1227 uword *sw_if_index0;
1229 now = clib_cpu_time_now ();
1230 elog_acl_maybe_trace_X1(am, "acl_fa_session_cleaner_process: now %lu, received ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX", "i8", now);
1231 vec_foreach (sw_if_index0, event_data)
1233 am->fa_cleaner_cnt_delete_by_sw_index++;
1234 elog_acl_maybe_trace_X1(am, "acl_fa_session_cleaner_process: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX %d", "i4", *sw_if_index0);
1235 if (*sw_if_index0 == ~0)
1241 if (!pool_is_free_index (am->vnet_main->interface_main.sw_interfaces, *sw_if_index0))
1243 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1247 #ifdef FA_NODE_VERBOSE_DEBUG
1248 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1250 vec_foreach(pw0, am->per_worker_data) {
1251 CLIB_MEMORY_BARRIER ();
1252 while (pw0->clear_in_process) {
1253 CLIB_MEMORY_BARRIER ();
1254 elog_acl_maybe_trace_X1(am, "ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d", "i4", (u32)(pw0 - am->per_worker_data));
1255 vlib_process_suspend(vm, 0.0001);
1256 if (pw0->interrupt_is_needed) {
1257 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1260 if (pw0->clear_in_process) {
1261 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1265 /* if we need to clear all, then just clear the interfaces that we are servicing */
1266 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(pw0->serviced_sw_if_index_bitmap);
1270 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1272 pw0->clear_in_process = 1;
1275 /* send some interrupts so they can start working */
1276 send_interrupts_to_workers(vm, am);
1278 /* now wait till they all complete */
1279 #ifdef FA_NODE_VERBOSE_DEBUG
1280 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1282 vec_foreach(pw0, am->per_worker_data) {
1283 CLIB_MEMORY_BARRIER ();
1284 while (pw0->clear_in_process) {
1285 CLIB_MEMORY_BARRIER ();
1286 elog_acl_maybe_trace_X1(am, "ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d", "i4", (u32)(pw0 - am->per_worker_data));
1287 vlib_process_suspend(vm, 0.0001);
1288 if (pw0->interrupt_is_needed) {
1289 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1293 #ifdef FA_NODE_VERBOSE_DEBUG
1294 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1296 clib_bitmap_free(clear_sw_if_index_bitmap);
1300 #ifdef FA_NODE_VERBOSE_DEBUG
1301 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1304 vlib_node_increment_counter (vm,
1305 acl_fa_session_cleaner_process_node.
1307 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1308 am->fa_cleaner_cnt_unknown_event++;
1312 send_interrupts_to_workers(vm, am);
1315 _vec_len (event_data) = 0;
1318 * If the interrupts were not processed yet, ensure we wait a bit,
1319 * but up to a point.
1321 int need_more_wait = 0;
1322 int max_wait_cycles = 100;
1325 vec_foreach(pw0, am->per_worker_data) {
1326 if (pw0->interrupt_generation != am->fa_interrupt_generation) {
1330 if (need_more_wait) {
1331 vlib_process_suspend(vm, 0.0001);
1333 } while (need_more_wait && (--max_wait_cycles > 0));
1335 int interrupts_needed = 0;
1336 int interrupts_unwanted = 0;
1338 vec_foreach(pw0, am->per_worker_data) {
1339 if (pw0->interrupt_is_needed) {
1340 interrupts_needed++;
1341 /* the per-worker value is reset when sending the interrupt */
1343 if (pw0->interrupt_is_unwanted) {
1344 interrupts_unwanted++;
1345 pw0->interrupt_is_unwanted = 0;
1348 if (interrupts_needed) {
1349 /* they need more interrupts, do less waiting around next time */
1350 am->fa_current_cleaner_timer_wait_interval /= 2;
1351 /* never go into zero-wait either though - we need to give the space to others */
1352 am->fa_current_cleaner_timer_wait_interval += 1;
1353 } else if (interrupts_unwanted) {
1354 /* slowly increase the amount of sleep up to a limit */
1355 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1356 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1358 am->fa_cleaner_cnt_event_cycles++;
1359 am->fa_interrupt_generation++;
1367 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1369 acl_main_t *am = &acl_main;
1370 if (enable_disable) {
1371 acl_fa_verify_init_sessions(am);
1372 am->fa_total_enabled_count++;
1373 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1374 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1375 ACL_FA_CLEANER_RESCHEDULE, 0);
1376 clib_mem_set_heap (oldheap);
1378 am->fa_total_enabled_count--;
1383 ASSERT(clib_bitmap_get(am->fa_in_acl_on_sw_if_index, sw_if_index) != enable_disable);
1384 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1385 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1386 sw_if_index, enable_disable, 0, 0);
1387 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1388 sw_if_index, enable_disable, 0, 0);
1389 clib_mem_set_heap (oldheap);
1390 am->fa_in_acl_on_sw_if_index =
1391 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1396 ASSERT(clib_bitmap_get(am->fa_out_acl_on_sw_if_index, sw_if_index) != enable_disable);
1397 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1398 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1399 sw_if_index, enable_disable, 0, 0);
1400 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1401 sw_if_index, enable_disable, 0, 0);
1402 clib_mem_set_heap (oldheap);
1403 am->fa_out_acl_on_sw_if_index =
1404 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1407 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1408 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1410 #ifdef FA_NODE_VERBOSE_DEBUG
1411 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1413 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1414 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1415 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1417 clib_mem_set_heap (oldheap);
1422 show_fa_sessions_hash(vlib_main_t * vm, u32 verbose)
1424 acl_main_t *am = &acl_main;
1425 if (am->fa_sessions_hash_is_initialized) {
1426 vlib_cli_output(vm, "\nSession lookup hash table:\n%U\n\n",
1427 BV (format_bihash), &am->fa_sessions_hash, verbose);
1429 vlib_cli_output(vm, "\nSession lookup hash table is not allocated.\n\n");
1436 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1437 .function = acl_fa_worker_conn_cleaner_process,
1438 .name = "acl-plugin-fa-worker-cleaner-process",
1439 .type = VLIB_NODE_TYPE_INPUT,
1440 .state = VLIB_NODE_STATE_INTERRUPT,
1443 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1444 .function = acl_fa_session_cleaner_process,
1445 .type = VLIB_NODE_TYPE_PROCESS,
1446 .name = "acl-plugin-fa-cleaner-process",
1447 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1448 .error_strings = acl_fa_cleaner_error_strings,
1454 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1456 .function = acl_in_ip6_l2_node_fn,
1457 .name = "acl-plugin-in-ip6-l2",
1458 .vector_size = sizeof (u32),
1459 .format_trace = format_acl_fa_trace,
1460 .type = VLIB_NODE_TYPE_INTERNAL,
1461 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1462 .error_strings = acl_fa_error_strings,
1463 .n_next_nodes = ACL_FA_N_NEXT,
1466 [ACL_FA_ERROR_DROP] = "error-drop",
1470 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1472 .function = acl_in_ip4_l2_node_fn,
1473 .name = "acl-plugin-in-ip4-l2",
1474 .vector_size = sizeof (u32),
1475 .format_trace = format_acl_fa_trace,
1476 .type = VLIB_NODE_TYPE_INTERNAL,
1477 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1478 .error_strings = acl_fa_error_strings,
1479 .n_next_nodes = ACL_FA_N_NEXT,
1482 [ACL_FA_ERROR_DROP] = "error-drop",
1486 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1488 .function = acl_out_ip6_l2_node_fn,
1489 .name = "acl-plugin-out-ip6-l2",
1490 .vector_size = sizeof (u32),
1491 .format_trace = format_acl_fa_trace,
1492 .type = VLIB_NODE_TYPE_INTERNAL,
1493 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1494 .error_strings = acl_fa_error_strings,
1495 .n_next_nodes = ACL_FA_N_NEXT,
1498 [ACL_FA_ERROR_DROP] = "error-drop",
1502 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1504 .function = acl_out_ip4_l2_node_fn,
1505 .name = "acl-plugin-out-ip4-l2",
1506 .vector_size = sizeof (u32),
1507 .format_trace = format_acl_fa_trace,
1508 .type = VLIB_NODE_TYPE_INTERNAL,
1509 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1510 .error_strings = acl_fa_error_strings,
1511 .n_next_nodes = ACL_FA_N_NEXT,
1514 [ACL_FA_ERROR_DROP] = "error-drop",
1519 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1521 .function = acl_in_ip6_fa_node_fn,
1522 .name = "acl-plugin-in-ip6-fa",
1523 .vector_size = sizeof (u32),
1524 .format_trace = format_acl_fa_trace,
1525 .type = VLIB_NODE_TYPE_INTERNAL,
1526 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1527 .error_strings = acl_fa_error_strings,
1528 .n_next_nodes = ACL_FA_N_NEXT,
1531 [ACL_FA_ERROR_DROP] = "error-drop",
1535 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1537 .arc_name = "ip6-unicast",
1538 .node_name = "acl-plugin-in-ip6-fa",
1539 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1542 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1544 .function = acl_in_ip4_fa_node_fn,
1545 .name = "acl-plugin-in-ip4-fa",
1546 .vector_size = sizeof (u32),
1547 .format_trace = format_acl_fa_trace,
1548 .type = VLIB_NODE_TYPE_INTERNAL,
1549 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1550 .error_strings = acl_fa_error_strings,
1551 .n_next_nodes = ACL_FA_N_NEXT,
1554 [ACL_FA_ERROR_DROP] = "error-drop",
1558 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1560 .arc_name = "ip4-unicast",
1561 .node_name = "acl-plugin-in-ip4-fa",
1562 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1566 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1568 .function = acl_out_ip6_fa_node_fn,
1569 .name = "acl-plugin-out-ip6-fa",
1570 .vector_size = sizeof (u32),
1571 .format_trace = format_acl_fa_trace,
1572 .type = VLIB_NODE_TYPE_INTERNAL,
1573 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1574 .error_strings = acl_fa_error_strings,
1575 .n_next_nodes = ACL_FA_N_NEXT,
1578 [ACL_FA_ERROR_DROP] = "error-drop",
1582 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1584 .arc_name = "ip6-output",
1585 .node_name = "acl-plugin-out-ip6-fa",
1586 .runs_before = VNET_FEATURES ("interface-output"),
1589 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1591 .function = acl_out_ip4_fa_node_fn,
1592 .name = "acl-plugin-out-ip4-fa",
1593 .vector_size = sizeof (u32),
1594 .format_trace = format_acl_fa_trace,
1595 .type = VLIB_NODE_TYPE_INTERNAL,
1596 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1597 .error_strings = acl_fa_error_strings,
1598 .n_next_nodes = ACL_FA_N_NEXT,
1599 /* edit / add dispositions here */
1602 [ACL_FA_ERROR_DROP] = "error-drop",
1606 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1608 .arc_name = "ip4-output",
1609 .node_name = "acl-plugin-out-ip4-fa",
1610 .runs_before = VNET_FEATURES ("interface-output"),