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>
23 #include "bihash_40_8.h"
25 #include <vppinfra/bihash_template.h>
26 #include <vppinfra/bihash_template.c>
34 u32 match_acl_in_index;
41 /* packet trace format function */
43 format_acl_fa_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
51 "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
52 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
53 t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
54 t->match_rule_index, t->trace_bitmap,
55 t->packet_info[0], t->packet_info[1], t->packet_info[2],
56 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
61 #define foreach_acl_fa_error \
62 _(ACL_DROP, "ACL deny packets") \
63 _(ACL_PERMIT, "ACL permit packets") \
64 _(ACL_NEW_SESSION, "new sessions added") \
65 _(ACL_EXIST_SESSION, "existing session packets") \
66 _(ACL_CHECK, "checked packets") \
67 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
68 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
73 #define _(sym,str) ACL_FA_ERROR_##sym,
79 static char *acl_fa_error_strings[] = {
80 #define _(sym,string) string,
87 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
89 u8 *p = vlib_buffer_get_current (b0) + offset;
95 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
96 int prefixlen, int is_ip6)
100 /* match any always succeeds */
105 if (memcmp (addr1, addr2, prefixlen / 8))
107 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
112 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
113 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
114 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
115 return (b1 & mask0) == b2;
119 /* The prefix fits into integer number of bytes, so nothing left to do */
125 uint32_t a1 = ntohl (addr1->ip4.as_u32);
126 uint32_t a2 = ntohl (addr2->ip4.as_u32);
127 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
128 return (a1 & mask0) == a2;
133 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
135 return ((port >= port_first) && (port <= port_last));
139 acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
140 int is_ip6, u8 * r_action, u32 * r_acl_match_p,
141 u32 * r_rule_match_p, u32 * trace_bitmap)
147 if (pool_is_free_index (am->acls, acl_index))
150 *r_acl_match_p = acl_index;
152 *r_rule_match_p = -1;
153 /* the ACL does not exist but is used for policy. Block traffic. */
156 a = am->acls + acl_index;
157 for (i = 0; i < a->count; i++)
160 if (is_ip6 != r->is_ipv6)
164 if (!fa_acl_match_addr
165 (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
168 #ifdef FA_NODE_VERBOSE_DEBUG
170 ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
171 acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
172 IP46_TYPE_ANY, format_ip46_address, &r->dst, IP46_TYPE_ANY,
176 if (!fa_acl_match_addr
177 (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
180 #ifdef FA_NODE_VERBOSE_DEBUG
182 ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
183 acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
184 IP46_TYPE_ANY, format_ip46_address, &r->src, IP46_TYPE_ANY,
187 ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
188 acl_index, i, pkt_5tuple->l4.proto, r->proto);
192 if (pkt_5tuple->l4.proto != r->proto)
195 if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
196 am->l4_match_nonfirst_fragment))
198 /* non-initial fragment with frag match configured - match this rule */
199 *trace_bitmap |= 0x80000000;
200 *r_action = r->is_permit;
202 *r_acl_match_p = acl_index;
208 /* A sanity check just to ensure we are about to match the ports extracted from the packet */
209 if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
212 #ifdef FA_NODE_VERBOSE_DEBUG
214 ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
215 acl_index, i, pkt_5tuple->l4.proto, r->proto);
218 if (!fa_acl_match_port
219 (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
220 r->src_port_or_type_last, is_ip6))
223 #ifdef FA_NODE_VERBOSE_DEBUG
225 ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
226 acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
227 r->src_port_or_type_last);
230 if (!fa_acl_match_port
231 (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
232 r->dst_port_or_code_last, is_ip6))
235 #ifdef FA_NODE_VERBOSE_DEBUG
237 ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
238 acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
239 r->dst_port_or_code_last);
241 if (pkt_5tuple->pkt.tcp_flags_valid
242 && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
246 /* everything matches! */
247 #ifdef FA_NODE_VERBOSE_DEBUG
248 clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
249 acl_index, i, r->is_permit);
251 *r_action = r->is_permit;
253 *r_acl_match_p = acl_index;
262 full_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
263 int is_ip6, int is_input, u32 * acl_match_p,
264 u32 * rule_match_p, u32 * trace_bitmap)
266 acl_main_t *am = &acl_main;
273 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
274 acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
278 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
279 acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
281 for (i = 0; i < vec_len (acl_vector); i++)
283 #ifdef FA_NODE_VERBOSE_DEBUG
284 clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
288 (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
289 acl_match_p, rule_match_p, trace_bitmap))
294 if (vec_len (acl_vector) > 0)
296 /* If there are ACLs and none matched, deny by default */
299 #ifdef FA_NODE_VERBOSE_DEBUG
300 clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
302 /* Deny by default. If there are no ACLs defined we should not be here. */
307 offset_within_packet (vlib_buffer_t * b0, int offset)
309 /* For the purposes of this code, "within" means we have at least 8 bytes after it */
310 return (offset <= (b0->current_length - 8));
314 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
315 int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
321 /* IP4 and IP6 protocol numbers of ICMP */
322 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
324 if (is_input && !(is_l2_path))
329 /* key[0..3] contains src/dst address and is cleared/set below */
330 /* Remainder of the key and per-packet non-key data */
331 p5tuple_pkt->kv.key[4] = 0;
332 p5tuple_pkt->kv.value = 0;
336 clib_memcpy (&p5tuple_pkt->addr,
337 get_ptr_to_offset (b0,
338 offsetof (ip6_header_t,
339 src_address) + l3_offset),
340 sizeof (p5tuple_pkt->addr));
342 *(u8 *) get_ptr_to_offset (b0,
343 offsetof (ip6_header_t,
344 protocol) + l3_offset);
345 l4_offset = l3_offset + sizeof (ip6_header_t);
346 #ifdef FA_NODE_VERBOSE_DEBUG
347 clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
350 /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
351 int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
352 if (PREDICT_FALSE (need_skip_eh))
354 while (need_skip_eh && offset_within_packet (b0, l4_offset))
356 /* Fragment header needs special handling */
357 if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
359 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
361 clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
362 frag_offset = ntohs(frag_offset) >> 3;
365 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
366 /* invalidate L4 offset so we don't try to find L4 info */
367 l4_offset += b0->current_length;
371 /* First fragment: skip the frag header and move on. */
377 u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
378 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
379 l4_offset += 8 * (1 + (u16) nwords);
381 #ifdef FA_NODE_VERBOSE_DEBUG
382 clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
386 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
392 p5tuple_pkt->kv.key[0] = 0;
393 p5tuple_pkt->kv.key[1] = 0;
394 p5tuple_pkt->kv.key[2] = 0;
395 p5tuple_pkt->kv.key[3] = 0;
396 clib_memcpy (&p5tuple_pkt->addr[0].ip4,
397 get_ptr_to_offset (b0,
398 offsetof (ip4_header_t,
399 src_address) + l3_offset),
400 sizeof (p5tuple_pkt->addr[0].ip4));
401 clib_memcpy (&p5tuple_pkt->addr[1].ip4,
402 get_ptr_to_offset (b0,
403 offsetof (ip4_header_t,
404 dst_address) + l3_offset),
405 sizeof (p5tuple_pkt->addr[1].ip4));
407 *(u8 *) get_ptr_to_offset (b0,
408 offsetof (ip4_header_t,
409 protocol) + l3_offset);
410 l4_offset = l3_offset + sizeof (ip4_header_t);
411 u16 flags_and_fragment_offset;
412 clib_memcpy (&flags_and_fragment_offset,
413 get_ptr_to_offset (b0,
414 offsetof (ip4_header_t,
415 flags_and_fragment_offset)) + l3_offset,
416 sizeof(flags_and_fragment_offset));
417 flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
419 /* non-initial fragments have non-zero offset */
420 if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
422 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
423 /* invalidate L4 offset so we don't try to find L4 info */
424 l4_offset += b0->current_length;
428 p5tuple_pkt->l4.proto = proto;
429 if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
431 p5tuple_pkt->pkt.l4_valid = 1;
432 if (icmp_protos[is_ip6] == proto)
435 p5tuple_pkt->l4.port[0] =
436 *(u8 *) get_ptr_to_offset (b0,
437 l4_offset + offsetof (icmp46_header_t,
440 p5tuple_pkt->l4.port[1] =
441 *(u8 *) get_ptr_to_offset (b0,
442 l4_offset + offsetof (icmp46_header_t,
445 else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
448 get_ptr_to_offset (b0,
449 l4_offset + offsetof (tcp_header_t,
452 p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
453 p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
455 p5tuple_pkt->pkt.tcp_flags =
456 *(u8 *) get_ptr_to_offset (b0,
457 l4_offset + offsetof (tcp_header_t,
459 p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
462 * FIXME: rather than the above conditional, here could
463 * be a nice generic mechanism to extract two L4 values:
465 * have a per-protocol array of 4 elements like this:
466 * u8 offset; to take the byte from, off L4 header
467 * u8 mask; to mask it with, before storing
469 * this way we can describe UDP, TCP and ICMP[46] semantics,
470 * and add a sort of FPM-type behavior for other protocols.
472 * Of course, is it faster ? and is it needed ?
479 /* Session keys match the packets received, and mirror the packets sent */
481 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
482 fa_5tuple_t * p5tuple_sess)
484 int src_index = is_input ? 0 : 1;
485 int dst_index = is_input ? 1 : 0;
486 p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
487 p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
488 p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
489 p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
490 p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
495 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
498 clib_bitmap_get (am->fa_sessions_on_sw_if_index, sw_if_index0);
503 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
505 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
510 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
512 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
518 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
520 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
522 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
523 TCP_FLAGS_RSTFINACKSYN);
524 switch (sess->info.l4.proto)
527 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
529 return ACL_TIMEOUT_TCP_IDLE;
533 return ACL_TIMEOUT_TCP_TRANSIENT;
537 return ACL_TIMEOUT_UDP_IDLE;
540 return ACL_TIMEOUT_UDP_IDLE;
546 fa_session_get_shortest_timeout(acl_main_t * am)
550 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
551 if (timeout > am->session_timeout_sec[timeout_type]) {
552 timeout = am->session_timeout_sec[timeout_type];
559 * Get the timeout of the session in a list since its enqueue time.
563 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
565 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
567 * we have the shortest possible timeout type in all the lists
568 * (see README-multicore for the rationale)
570 timeout *= fa_session_get_shortest_timeout(am);
575 * Get the idle timeout of a session.
579 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
581 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
582 int timeout_type = fa_session_get_timeout_type (am, sess);
583 timeout *= am->session_timeout_sec[timeout_type];
588 acl_fa_ifc_init_sessions (acl_main_t * am, int sw_if_index0)
590 /// FIXME-MULTICORE: lock around this function
591 #ifdef FA_NODE_VERBOSE_DEBUG
593 ("Initializing bihash for sw_if_index %d num buckets %lu memory size %llu",
594 sw_if_index0, am->fa_conn_table_hash_num_buckets,
595 am->fa_conn_table_hash_memory_size);
597 vec_validate (am->fa_sessions_by_sw_if_index, sw_if_index0);
598 BV (clib_bihash_init) (&am->fa_sessions_by_sw_if_index
599 [sw_if_index0], "ACL plugin FA session bihash",
600 am->fa_conn_table_hash_num_buckets,
601 am->fa_conn_table_hash_memory_size);
602 am->fa_sessions_on_sw_if_index =
603 clib_bitmap_set (am->fa_sessions_on_sw_if_index, sw_if_index0, 1);
606 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
608 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
609 fa_session_t *sess = pw->fa_sessions_pool + session_index;
614 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
616 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
617 u8 list_id = fa_session_get_timeout_type(am, sess);
618 uword thread_index = os_get_thread_index ();
619 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
620 /* the retrieved session thread index must be necessarily the same as the one in the key */
621 ASSERT (sess->thread_index == sess_id.thread_index);
622 /* the retrieved session thread index must be the same as current thread */
623 ASSERT (sess->thread_index == thread_index);
624 sess->link_enqueue_time = now;
625 sess->link_list_id = list_id;
626 sess->link_next_idx = ~0;
627 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
628 if (~0 != pw->fa_conn_list_tail[list_id]) {
629 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
630 prev_sess->link_next_idx = sess_id.session_index;
631 /* We should never try to link with a session on another thread */
632 ASSERT(prev_sess->thread_index == sess->thread_index);
634 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
635 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
637 if (~0 == pw->fa_conn_list_head[list_id]) {
638 pw->fa_conn_list_head[list_id] = sess_id.session_index;
639 /* If it is a first conn in any list, kick the cleaner */
640 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
641 ACL_FA_CLEANER_RESCHEDULE, 0);
646 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
648 uword thread_index = os_get_thread_index ();
649 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
650 if (thread_index != sess_id.thread_index) {
651 /* If another thread attempts to delete the session, fail it. */
652 #ifdef FA_NODE_VERBOSE_DEBUG
653 clib_warning("thread id in key %d != curr thread index, not deleting");
657 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
658 /* we should never try to delete the session with another thread index */
659 ASSERT(sess->thread_index == thread_index);
660 if (~0 != sess->link_prev_idx) {
661 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
662 /* the previous session must be in the same list as this one */
663 ASSERT(prev_sess->link_list_id == sess->link_list_id);
664 prev_sess->link_next_idx = sess->link_next_idx;
666 if (~0 != sess->link_next_idx) {
667 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
668 /* The next session must be in the same list as the one we are deleting */
669 ASSERT(next_sess->link_list_id == sess->link_list_id);
670 next_sess->link_prev_idx = sess->link_prev_idx;
672 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
673 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
675 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
676 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
682 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
684 if (acl_fa_conn_list_delete_session(am, sess_id)) {
685 acl_fa_conn_list_add_session(am, sess_id, now);
689 * Our thread does not own this connection, so we can not delete
690 * The session. To avoid the complicated signaling, we simply
691 * pick the list waiting time to be the shortest of the timeouts.
692 * This way we do not have to do anything special, and let
693 * the regular requeue check take care of everything.
701 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
702 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
704 sess->last_active_time = now;
705 if (pkt_5tuple->pkt.tcp_flags_valid)
707 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
714 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
716 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
717 ASSERT(sess->thread_index == os_get_thread_index ());
718 BV (clib_bihash_add_del) (&am->fa_sessions_by_sw_if_index[sw_if_index],
720 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
721 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
722 /* Deleting from timer structures not needed,
723 as the caller must have dealt with the timers. */
724 vec_validate (am->fa_session_dels_by_sw_if_index, sw_if_index);
725 am->fa_session_dels_by_sw_if_index[sw_if_index]++;
729 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
732 vec_validate (am->fa_session_adds_by_sw_if_index, sw_if_index);
733 vec_validate (am->fa_session_dels_by_sw_if_index, sw_if_index);
735 am->fa_session_adds_by_sw_if_index[sw_if_index] -
736 am->fa_session_dels_by_sw_if_index[sw_if_index];
737 return (curr_sess < am->fa_conn_table_max_entries);
741 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)
743 if (~0 == pw->fa_conn_list_head[timeout_type]) {
744 return ~0LL; // infinity.
746 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
748 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
754 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)
756 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
758 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
759 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
763 * see if there are sessions ready to be checked,
764 * do the maintenance (requeue or delete), and
765 * return the total number of sessions reclaimed.
768 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
770 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
771 fa_full_session_id_t fsid;
772 fsid.thread_index = thread_index;
773 int total_expired = 0;
777 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
778 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
779 && (~0 != pw->fa_conn_list_head[tt])
780 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
781 pw->fa_conn_list_head[tt]))) {
782 fsid.session_index = pw->fa_conn_list_head[tt];
783 vec_add1(pw->expired, fsid.session_index);
784 acl_fa_conn_list_delete_session(am, fsid);
790 vec_foreach (psid, pw->expired)
792 fsid.session_index = *psid;
793 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
795 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
796 u32 sw_if_index = sess->sw_if_index;
797 u64 sess_timeout_time =
798 sess->last_active_time + fa_session_get_timeout (am, sess);
799 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
801 #ifdef FA_NODE_VERBOSE_DEBUG
802 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
803 (int) session_index);
805 /* There was activity on the session, so the idle timeout
806 has not passed. Enqueue for another time period. */
808 acl_fa_conn_list_add_session(am, fsid, now);
809 pw->cnt_session_timer_restarted++;
813 #ifdef FA_NODE_VERBOSE_DEBUG
814 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
815 (int) session_index);
817 acl_fa_delete_session (am, sw_if_index, fsid);
818 pw->cnt_deleted_sessions++;
823 pw->cnt_already_deleted_sessions++;
826 total_expired = vec_len(pw->expired);
827 /* zero out the vector which we have acted on */
829 _vec_len (pw->expired) = 0;
830 /* if we were advancing and reached the end
831 * (no more sessions to recycle), reset the fast-forward timestamp */
833 if (pw->swipe_end_time && 0 == total_expired)
834 pw->swipe_end_time = 0;
835 return (total_expired);
839 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
841 /* try to recycle a TCP transient session */
842 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
843 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
844 fa_full_session_id_t sess_id;
845 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
846 if (~0 != sess_id.session_index) {
847 sess_id.thread_index = thread_index;
848 acl_fa_conn_list_delete_session(am, sess_id);
849 acl_fa_delete_session(am, sw_if_index, sess_id);
854 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
855 fa_5tuple_t * p5tuple)
857 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
858 clib_bihash_kv_40_8_t kv;
859 fa_full_session_id_t f_sess_id;
860 uword thread_index = os_get_thread_index();
861 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
863 f_sess_id.thread_index = thread_index;
866 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
867 f_sess_id.session_index = sess - pw->fa_sessions_pool;
869 kv.key[0] = pkv->key[0];
870 kv.key[1] = pkv->key[1];
871 kv.key[2] = pkv->key[2];
872 kv.key[3] = pkv->key[3];
873 kv.key[4] = pkv->key[4];
874 kv.value = f_sess_id.as_u64;
876 memcpy (sess, pkv, sizeof (pkv->key));
877 sess->last_active_time = now;
878 sess->sw_if_index = sw_if_index;
879 sess->tcp_flags_seen.as_u16 = 0;
880 sess->thread_index = thread_index;
881 sess->link_list_id = ~0;
882 sess->link_prev_idx = ~0;
883 sess->link_next_idx = ~0;
887 if (!acl_fa_ifc_has_sessions (am, sw_if_index))
889 acl_fa_ifc_init_sessions (am, sw_if_index);
892 BV (clib_bihash_add_del) (&am->fa_sessions_by_sw_if_index[sw_if_index],
894 acl_fa_conn_list_add_session(am, f_sess_id, now);
896 vec_validate (am->fa_session_adds_by_sw_if_index, sw_if_index);
897 am->fa_session_adds_by_sw_if_index[sw_if_index]++;
901 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
902 clib_bihash_kv_40_8_t * pvalue_sess)
904 return (BV (clib_bihash_search)
905 (&am->fa_sessions_by_sw_if_index[sw_if_index0], &p5tuple->kv,
911 acl_fa_node_fn (vlib_main_t * vm,
912 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
913 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
914 vlib_node_registration_t * acl_fa_node)
916 u32 n_left_from, *from, *to_next;
917 acl_fa_next_t next_index;
918 u32 pkts_acl_checked = 0;
919 u32 pkts_new_session = 0;
920 u32 pkts_exist_session = 0;
921 u32 pkts_acl_permit = 0;
922 u32 pkts_restart_session_timer = 0;
923 u32 trace_bitmap = 0;
925 acl_main_t *am = &acl_main;
926 fa_5tuple_t fa_5tuple, kv_sess;
927 clib_bihash_kv_40_8_t value_sess;
928 vlib_node_runtime_t *error_node;
929 u64 now = clib_cpu_time_now ();
930 uword thread_index = os_get_thread_index ();
932 from = vlib_frame_vector_args (frame);
933 n_left_from = frame->n_vectors;
934 next_index = node->cached_next_index;
936 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
938 while (n_left_from > 0)
942 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
944 while (n_left_from > 0 && n_left_to_next > 0)
951 int acl_check_needed = 1;
952 u32 match_acl_in_index = ~0;
953 u32 match_rule_index = ~0;
956 /* speculatively enqueue b0 to the current next frame */
964 b0 = vlib_get_buffer (vm, bi0);
967 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
969 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
971 feature_bitmap0 = vnet_buffer (b0)->l2.feature_bitmap;
974 * Extract the L3/L4 matching info into a 5-tuple structure,
975 * then create a session key whose layout is independent on forward or reverse
976 * direction of the packet.
979 acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
980 acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
981 #ifdef FA_NODE_VERBOSE_DEBUG
983 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
984 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
985 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
987 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
988 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
989 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
992 /* Try to match an existing session first */
994 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
996 if (acl_fa_find_session
997 (am, sw_if_index0, &kv_sess, &value_sess))
999 trace_bitmap |= 0x80000000;
1000 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
1001 fa_full_session_id_t f_sess_id;
1003 f_sess_id.as_u64 = value_sess.value;
1004 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1006 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1007 int old_timeout_type =
1008 fa_session_get_timeout_type (am, sess);
1010 acl_fa_track_session (am, is_input, sw_if_index0, now,
1012 /* expose the session id to the tracer */
1013 match_rule_index = f_sess_id.session_index;
1014 int new_timeout_type =
1015 fa_session_get_timeout_type (am, sess);
1016 acl_check_needed = 0;
1017 pkts_exist_session += 1;
1018 /* Tracking might have changed the session timeout type, e.g. from transient to established */
1019 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1021 acl_fa_restart_timer_for_session (am, now, f_sess_id);
1022 pkts_restart_session_timer++;
1024 0x00010000 + ((0xff & old_timeout_type) << 8) +
1025 (0xff & new_timeout_type);
1030 if (acl_check_needed)
1033 full_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1034 is_ip6, is_input, &match_acl_in_index,
1035 &match_rule_index, &trace_bitmap);
1038 pkts_acl_permit += 1;
1041 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1042 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1044 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1046 acl_fa_add_session (am, is_input, sw_if_index0, now,
1048 pkts_new_session += 1;
1053 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1064 feat_bitmap_get_next_node_index (l2_feat_next_node_index,
1067 vnet_feature_next (sw_if_index0, &next0, b0);
1070 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1071 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1073 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1074 t->sw_if_index = sw_if_index0;
1075 t->next_index = next0;
1076 t->match_acl_in_index = match_acl_in_index;
1077 t->match_rule_index = match_rule_index;
1078 t->packet_info[0] = fa_5tuple.kv.key[0];
1079 t->packet_info[1] = fa_5tuple.kv.key[1];
1080 t->packet_info[2] = fa_5tuple.kv.key[2];
1081 t->packet_info[3] = fa_5tuple.kv.key[3];
1082 t->packet_info[4] = fa_5tuple.kv.key[4];
1083 t->packet_info[5] = fa_5tuple.kv.value;
1085 t->trace_bitmap = trace_bitmap;
1088 next0 = next0 < node->n_next_nodes ? next0 : 0;
1090 b0->error = error_node->errors[error0];
1092 pkts_acl_checked += 1;
1094 /* verify speculative enqueue, maybe switch current next frame */
1095 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1096 to_next, n_left_to_next, bi0,
1100 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1103 vlib_node_increment_counter (vm, acl_fa_node->index,
1104 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1105 vlib_node_increment_counter (vm, acl_fa_node->index,
1106 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1107 vlib_node_increment_counter (vm, acl_fa_node->index,
1108 ACL_FA_ERROR_ACL_NEW_SESSION,
1110 vlib_node_increment_counter (vm, acl_fa_node->index,
1111 ACL_FA_ERROR_ACL_EXIST_SESSION,
1112 pkts_exist_session);
1113 vlib_node_increment_counter (vm, acl_fa_node->index,
1114 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1115 pkts_restart_session_timer);
1116 return frame->n_vectors;
1120 vlib_node_registration_t acl_in_l2_ip6_node;
1122 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1123 vlib_node_runtime_t * node, vlib_frame_t * frame)
1125 acl_main_t *am = &acl_main;
1126 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1127 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1128 &acl_in_l2_ip6_node);
1131 vlib_node_registration_t acl_in_l2_ip4_node;
1133 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1134 vlib_node_runtime_t * node, vlib_frame_t * frame)
1136 acl_main_t *am = &acl_main;
1137 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1138 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1139 &acl_in_l2_ip4_node);
1142 vlib_node_registration_t acl_out_l2_ip6_node;
1144 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1145 vlib_node_runtime_t * node, vlib_frame_t * frame)
1147 acl_main_t *am = &acl_main;
1148 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1149 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1150 &acl_out_l2_ip6_node);
1153 vlib_node_registration_t acl_out_l2_ip4_node;
1155 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1156 vlib_node_runtime_t * node, vlib_frame_t * frame)
1158 acl_main_t *am = &acl_main;
1159 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1160 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1161 &acl_out_l2_ip4_node);
1165 /**** L3 processing path nodes ****/
1168 vlib_node_registration_t acl_in_fa_ip6_node;
1170 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1171 vlib_node_runtime_t * node, vlib_frame_t * frame)
1173 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1176 vlib_node_registration_t acl_in_fa_ip4_node;
1178 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1179 vlib_node_runtime_t * node, vlib_frame_t * frame)
1181 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1184 vlib_node_registration_t acl_out_fa_ip6_node;
1186 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1187 vlib_node_runtime_t * node, vlib_frame_t * frame)
1189 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1192 vlib_node_registration_t acl_out_fa_ip4_node;
1194 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1195 vlib_node_runtime_t * node, vlib_frame_t * frame)
1197 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1201 * This process ensures the connection cleanup happens every so often
1202 * even in absence of traffic, as well as provides general orchestration
1203 * for requests like connection deletion on a given sw_if_index.
1208 #define foreach_acl_fa_cleaner_error \
1209 _(UNKNOWN_EVENT, "unknown event received") \
1214 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1215 foreach_acl_fa_cleaner_error
1217 ACL_FA_CLEANER_N_ERROR,
1218 } acl_fa_cleaner_error_t;
1220 static char *acl_fa_cleaner_error_strings[] = {
1221 #define _(sym,string) string,
1222 foreach_acl_fa_cleaner_error
1228 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1229 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1232 * Per-worker thread interrupt-driven cleaner thread
1233 * to clean idle connections if there are no packets
1236 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1237 vlib_node_runtime_t * rt, vlib_frame_t * f)
1239 acl_main_t *am = &acl_main;
1240 u64 now = clib_cpu_time_now ();
1241 u16 thread_index = os_get_thread_index ();
1242 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1244 #ifdef FA_NODE_VERBOSE_DEBUG
1245 clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1247 /* allow another interrupt to be queued */
1248 pw->interrupt_is_pending = 0;
1249 if (pw->clear_in_process) {
1250 if (0 == pw->swipe_end_time) {
1252 * Someone has just set the flag to start clearing.
1253 * we do this by combing through the connections up to a "time T"
1254 * which is now, and requeueing everything except the expired
1255 * connections and those matching the interface(s) being cleared.
1259 * first filter the sw_if_index bitmap that they want from us, by
1260 * a bitmap of sw_if_index for which we actually have connections.
1262 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1263 || (pw->serviced_sw_if_index_bitmap == 0)) {
1264 #ifdef FA_NODE_VERBOSE_DEBUG
1265 clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1267 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1269 #ifdef FA_NODE_VERBOSE_DEBUG
1270 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1271 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1272 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1274 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1275 pw->serviced_sw_if_index_bitmap);
1278 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1279 /* if the cross-section is a zero vector, no need to do anything. */
1280 #ifdef FA_NODE_VERBOSE_DEBUG
1281 clib_warning("WORKER: clearing done - nothing to do");
1283 pw->clear_in_process = 0;
1285 #ifdef FA_NODE_VERBOSE_DEBUG
1286 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1287 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1288 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1290 /* swipe through the connection lists until enqueue timestamps become above "now" */
1291 pw->swipe_end_time = now;
1295 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1296 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1297 if (pw->clear_in_process) {
1298 if (0 == num_expired) {
1299 /* we were clearing but we could not process any more connections. time to stop. */
1300 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1301 pw->clear_in_process = 0;
1302 #ifdef FA_NODE_VERBOSE_DEBUG
1303 clib_warning("WORKER: clearing done, all done");
1306 #ifdef FA_NODE_VERBOSE_DEBUG
1307 clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1309 /* should continue clearing.. So could they please sent an interrupt again? */
1310 pw->interrupt_is_needed = 1;
1313 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1314 /* there was too much work, we should get an interrupt ASAP */
1315 pw->interrupt_is_needed = 1;
1316 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1317 /* signal that they should trigger us less */
1318 pw->interrupt_is_unwanted = 1;
1320 /* the current rate of interrupts is ok */
1321 pw->interrupt_is_needed = 0;
1322 pw->interrupt_is_unwanted = 0;
1329 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1331 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1332 if (!pw->interrupt_is_pending) {
1333 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1334 acl_fa_worker_session_cleaner_process_node.index);
1335 pw->interrupt_is_pending = 1;
1336 /* if the interrupt was requested, mark that done. */
1337 pw->interrupt_is_needed = 0;
1342 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1345 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1346 int n_threads = vec_len(vlib_mains);
1347 for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1348 send_one_worker_interrupt(vm, am, i);
1352 /* centralized process to drive per-worker cleaners */
1354 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1357 acl_main_t *am = &acl_main;
1358 u64 now = clib_cpu_time_now ();
1359 f64 cpu_cps = vm->clib_time.clocks_per_second;
1361 /* We should check if there are connections to clean up - at least twice a second */
1362 u64 max_timer_wait_interval = cpu_cps / 2;
1363 uword event_type, *event_data = 0;
1364 acl_fa_per_worker_data_t *pw0;
1366 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1367 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1371 now = clib_cpu_time_now ();
1372 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1373 int has_pending_conns = 0;
1378 * walk over all per-thread list heads of different timeouts,
1379 * and see if there are any connections pending.
1380 * If there aren't - we do not need to wake up until the
1381 * worker code signals that it has added a connection.
1383 * Also, while we are at it, calculate the earliest we need to wake up.
1385 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1386 if (ti >= vec_len(am->per_worker_data)) {
1389 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1390 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1391 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1392 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1393 #ifdef FA_NODE_VERBOSE_DEBUG
1394 clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1396 next_expire = head_expiry;
1398 if (~0 != pw->fa_conn_list_head[tt]) {
1399 has_pending_conns = 1;
1404 /* If no pending connections then no point in timing out */
1405 if (!has_pending_conns)
1407 am->fa_cleaner_cnt_wait_without_timeout++;
1408 (void) vlib_process_wait_for_event (vm);
1409 event_type = vlib_process_get_events (vm, &event_data);
1413 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1416 /* skip waiting altogether */
1421 am->fa_cleaner_cnt_wait_with_timeout++;
1422 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1423 event_type = vlib_process_get_events (vm, &event_data);
1427 now = clib_cpu_time_now ();
1433 case ACL_FA_CLEANER_RESCHEDULE:
1434 /* Nothing to do. */
1436 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1438 uword *clear_sw_if_index_bitmap = 0;
1439 uword *sw_if_index0;
1440 #ifdef FA_NODE_VERBOSE_DEBUG
1441 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1443 vec_foreach (sw_if_index0, event_data)
1445 am->fa_cleaner_cnt_delete_by_sw_index++;
1446 #ifdef FA_NODE_VERBOSE_DEBUG
1448 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1451 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1453 #ifdef FA_NODE_VERBOSE_DEBUG
1454 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1456 vec_foreach(pw0, am->per_worker_data) {
1457 CLIB_MEMORY_BARRIER ();
1458 while (pw0->clear_in_process) {
1459 CLIB_MEMORY_BARRIER ();
1460 #ifdef FA_NODE_VERBOSE_DEBUG
1461 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1463 vlib_process_suspend(vm, 0.0001);
1464 if (pw0->interrupt_is_needed) {
1465 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1468 if (pw0->clear_in_process) {
1469 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1471 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1472 pw0->clear_in_process = 1;
1475 /* send some interrupts so they can start working */
1476 send_interrupts_to_workers(vm, am);
1478 /* now wait till they all complete */
1479 #ifdef FA_NODE_VERBOSE_DEBUG
1480 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1482 vec_foreach(pw0, am->per_worker_data) {
1483 CLIB_MEMORY_BARRIER ();
1484 while (pw0->clear_in_process) {
1485 CLIB_MEMORY_BARRIER ();
1486 #ifdef FA_NODE_VERBOSE_DEBUG
1487 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1489 vlib_process_suspend(vm, 0.0001);
1490 if (pw0->interrupt_is_needed) {
1491 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1495 #ifdef FA_NODE_VERBOSE_DEBUG
1496 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1498 clib_bitmap_free(clear_sw_if_index_bitmap);
1502 #ifdef FA_NODE_VERBOSE_DEBUG
1503 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1506 vlib_node_increment_counter (vm,
1507 acl_fa_session_cleaner_process_node.
1509 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1510 am->fa_cleaner_cnt_unknown_event++;
1514 send_interrupts_to_workers(vm, am);
1517 _vec_len (event_data) = 0;
1520 int interrupts_needed = 0;
1521 int interrupts_unwanted = 0;
1523 vec_foreach(pw0, am->per_worker_data) {
1524 if (pw0->interrupt_is_needed) {
1525 interrupts_needed++;
1526 /* the per-worker value is reset when sending the interrupt */
1528 if (pw0->interrupt_is_unwanted) {
1529 interrupts_unwanted++;
1530 pw0->interrupt_is_unwanted = 0;
1533 if (interrupts_needed) {
1534 /* they need more interrupts, do less waiting around next time */
1535 am->fa_current_cleaner_timer_wait_interval /= 2;
1536 } else if (interrupts_unwanted) {
1537 /* slowly increase the amount of sleep up to a limit */
1538 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1539 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1541 am->fa_cleaner_cnt_event_cycles++;
1549 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1551 acl_main_t *am = &acl_main;
1554 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1555 sw_if_index, enable_disable, 0, 0);
1556 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1557 sw_if_index, enable_disable, 0, 0);
1558 am->fa_in_acl_on_sw_if_index =
1559 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1564 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1565 sw_if_index, enable_disable, 0, 0);
1566 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1567 sw_if_index, enable_disable, 0, 0);
1568 am->fa_out_acl_on_sw_if_index =
1569 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1572 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1573 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1575 #ifdef FA_NODE_VERBOSE_DEBUG
1576 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1578 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1579 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1588 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1589 .function = acl_fa_worker_conn_cleaner_process,
1590 .name = "acl-plugin-fa-worker-cleaner-process",
1591 .type = VLIB_NODE_TYPE_INPUT,
1592 .state = VLIB_NODE_STATE_INTERRUPT,
1595 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1596 .function = acl_fa_session_cleaner_process,
1597 .type = VLIB_NODE_TYPE_PROCESS,
1598 .name = "acl-plugin-fa-cleaner-process",
1599 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1600 .error_strings = acl_fa_cleaner_error_strings,
1606 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1608 .function = acl_in_ip6_l2_node_fn,
1609 .name = "acl-plugin-in-ip6-l2",
1610 .vector_size = sizeof (u32),
1611 .format_trace = format_acl_fa_trace,
1612 .type = VLIB_NODE_TYPE_INTERNAL,
1613 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1614 .error_strings = acl_fa_error_strings,
1615 .n_next_nodes = ACL_FA_N_NEXT,
1618 [ACL_FA_ERROR_DROP] = "error-drop",
1622 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1624 .function = acl_in_ip4_l2_node_fn,
1625 .name = "acl-plugin-in-ip4-l2",
1626 .vector_size = sizeof (u32),
1627 .format_trace = format_acl_fa_trace,
1628 .type = VLIB_NODE_TYPE_INTERNAL,
1629 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1630 .error_strings = acl_fa_error_strings,
1631 .n_next_nodes = ACL_FA_N_NEXT,
1634 [ACL_FA_ERROR_DROP] = "error-drop",
1638 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1640 .function = acl_out_ip6_l2_node_fn,
1641 .name = "acl-plugin-out-ip6-l2",
1642 .vector_size = sizeof (u32),
1643 .format_trace = format_acl_fa_trace,
1644 .type = VLIB_NODE_TYPE_INTERNAL,
1645 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1646 .error_strings = acl_fa_error_strings,
1647 .n_next_nodes = ACL_FA_N_NEXT,
1650 [ACL_FA_ERROR_DROP] = "error-drop",
1654 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1656 .function = acl_out_ip4_l2_node_fn,
1657 .name = "acl-plugin-out-ip4-l2",
1658 .vector_size = sizeof (u32),
1659 .format_trace = format_acl_fa_trace,
1660 .type = VLIB_NODE_TYPE_INTERNAL,
1661 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1662 .error_strings = acl_fa_error_strings,
1663 .n_next_nodes = ACL_FA_N_NEXT,
1666 [ACL_FA_ERROR_DROP] = "error-drop",
1671 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1673 .function = acl_in_ip6_fa_node_fn,
1674 .name = "acl-plugin-in-ip6-fa",
1675 .vector_size = sizeof (u32),
1676 .format_trace = format_acl_fa_trace,
1677 .type = VLIB_NODE_TYPE_INTERNAL,
1678 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1679 .error_strings = acl_fa_error_strings,
1680 .n_next_nodes = ACL_FA_N_NEXT,
1683 [ACL_FA_ERROR_DROP] = "error-drop",
1687 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1689 .arc_name = "ip6-unicast",
1690 .node_name = "acl-plugin-in-ip6-fa",
1691 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1694 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1696 .function = acl_in_ip4_fa_node_fn,
1697 .name = "acl-plugin-in-ip4-fa",
1698 .vector_size = sizeof (u32),
1699 .format_trace = format_acl_fa_trace,
1700 .type = VLIB_NODE_TYPE_INTERNAL,
1701 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1702 .error_strings = acl_fa_error_strings,
1703 .n_next_nodes = ACL_FA_N_NEXT,
1706 [ACL_FA_ERROR_DROP] = "error-drop",
1710 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1712 .arc_name = "ip4-unicast",
1713 .node_name = "acl-plugin-in-ip4-fa",
1714 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1718 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1720 .function = acl_out_ip6_fa_node_fn,
1721 .name = "acl-plugin-out-ip6-fa",
1722 .vector_size = sizeof (u32),
1723 .format_trace = format_acl_fa_trace,
1724 .type = VLIB_NODE_TYPE_INTERNAL,
1725 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1726 .error_strings = acl_fa_error_strings,
1727 .n_next_nodes = ACL_FA_N_NEXT,
1730 [ACL_FA_ERROR_DROP] = "error-drop",
1734 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1736 .arc_name = "ip6-output",
1737 .node_name = "acl-plugin-out-ip6-fa",
1738 .runs_before = VNET_FEATURES ("interface-output"),
1741 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1743 .function = acl_out_ip4_fa_node_fn,
1744 .name = "acl-plugin-out-ip4-fa",
1745 .vector_size = sizeof (u32),
1746 .format_trace = format_acl_fa_trace,
1747 .type = VLIB_NODE_TYPE_INTERNAL,
1748 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1749 .error_strings = acl_fa_error_strings,
1750 .n_next_nodes = ACL_FA_N_NEXT,
1751 /* edit / add dispositions here */
1754 [ACL_FA_ERROR_DROP] = "error-drop",
1758 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1760 .arc_name = "ip4-output",
1761 .node_name = "acl-plugin-out-ip4-fa",
1762 .runs_before = VNET_FEATURES ("interface-output"),