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)
497 return am->fa_sessions_hash_is_initialized;
501 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
503 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
508 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
510 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
516 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
518 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
520 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
521 TCP_FLAGS_RSTFINACKSYN);
522 switch (sess->info.l4.proto)
525 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
527 return ACL_TIMEOUT_TCP_IDLE;
531 return ACL_TIMEOUT_TCP_TRANSIENT;
535 return ACL_TIMEOUT_UDP_IDLE;
538 return ACL_TIMEOUT_UDP_IDLE;
544 fa_session_get_shortest_timeout(acl_main_t * am)
548 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
549 if (timeout > am->session_timeout_sec[timeout_type]) {
550 timeout = am->session_timeout_sec[timeout_type];
557 * Get the timeout of the session in a list since its enqueue time.
561 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
563 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
565 * we have the shortest possible timeout type in all the lists
566 * (see README-multicore for the rationale)
568 timeout *= fa_session_get_shortest_timeout(am);
573 * Get the idle timeout of a session.
577 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
579 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
580 int timeout_type = fa_session_get_timeout_type (am, sess);
581 timeout *= am->session_timeout_sec[timeout_type];
586 acl_fa_ifc_init_sessions (acl_main_t * am, int sw_if_index0)
588 /// FIXME-MULTICORE: lock around this function
589 #ifdef FA_NODE_VERBOSE_DEBUG
591 ("Initializing bihash for sw_if_index %d num buckets %lu memory size %llu",
592 sw_if_index0, am->fa_conn_table_hash_num_buckets,
593 am->fa_conn_table_hash_memory_size);
595 BV (clib_bihash_init) (&am->fa_sessions_hash,
596 "ACL plugin FA session bihash",
597 am->fa_conn_table_hash_num_buckets,
598 am->fa_conn_table_hash_memory_size);
599 am->fa_sessions_hash_is_initialized = 1;
602 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
604 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
605 fa_session_t *sess = pw->fa_sessions_pool + session_index;
610 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
612 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
613 u8 list_id = fa_session_get_timeout_type(am, sess);
614 uword thread_index = os_get_thread_index ();
615 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
616 /* the retrieved session thread index must be necessarily the same as the one in the key */
617 ASSERT (sess->thread_index == sess_id.thread_index);
618 /* the retrieved session thread index must be the same as current thread */
619 ASSERT (sess->thread_index == thread_index);
620 sess->link_enqueue_time = now;
621 sess->link_list_id = list_id;
622 sess->link_next_idx = ~0;
623 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
624 if (~0 != pw->fa_conn_list_tail[list_id]) {
625 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
626 prev_sess->link_next_idx = sess_id.session_index;
627 /* We should never try to link with a session on another thread */
628 ASSERT(prev_sess->thread_index == sess->thread_index);
630 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
631 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
633 if (~0 == pw->fa_conn_list_head[list_id]) {
634 pw->fa_conn_list_head[list_id] = sess_id.session_index;
635 /* If it is a first conn in any list, kick the cleaner */
636 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
637 ACL_FA_CLEANER_RESCHEDULE, 0);
642 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
644 uword thread_index = os_get_thread_index ();
645 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
646 if (thread_index != sess_id.thread_index) {
647 /* If another thread attempts to delete the session, fail it. */
648 #ifdef FA_NODE_VERBOSE_DEBUG
649 clib_warning("thread id in key %d != curr thread index, not deleting");
653 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
654 /* we should never try to delete the session with another thread index */
655 ASSERT(sess->thread_index == thread_index);
656 if (~0 != sess->link_prev_idx) {
657 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
658 /* the previous session must be in the same list as this one */
659 ASSERT(prev_sess->link_list_id == sess->link_list_id);
660 prev_sess->link_next_idx = sess->link_next_idx;
662 if (~0 != sess->link_next_idx) {
663 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
664 /* The next session must be in the same list as the one we are deleting */
665 ASSERT(next_sess->link_list_id == sess->link_list_id);
666 next_sess->link_prev_idx = sess->link_prev_idx;
668 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
669 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
671 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
672 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
678 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
680 if (acl_fa_conn_list_delete_session(am, sess_id)) {
681 acl_fa_conn_list_add_session(am, sess_id, now);
685 * Our thread does not own this connection, so we can not delete
686 * The session. To avoid the complicated signaling, we simply
687 * pick the list waiting time to be the shortest of the timeouts.
688 * This way we do not have to do anything special, and let
689 * the regular requeue check take care of everything.
697 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
698 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
700 sess->last_active_time = now;
701 if (pkt_5tuple->pkt.tcp_flags_valid)
703 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
710 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
712 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
713 ASSERT(sess->thread_index == os_get_thread_index ());
714 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
716 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
717 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
718 /* Deleting from timer structures not needed,
719 as the caller must have dealt with the timers. */
720 vec_validate (am->fa_session_dels_by_sw_if_index, sw_if_index);
721 am->fa_session_dels_by_sw_if_index[sw_if_index]++;
722 clib_smp_atomic_add(&am->fa_session_total_dels, 1);
726 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
729 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
730 return (curr_sess_count < am->fa_conn_table_max_entries);
734 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)
736 if (~0 == pw->fa_conn_list_head[timeout_type]) {
737 return ~0LL; // infinity.
739 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
741 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
747 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)
749 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
751 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
752 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
756 * see if there are sessions ready to be checked,
757 * do the maintenance (requeue or delete), and
758 * return the total number of sessions reclaimed.
761 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
763 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
764 fa_full_session_id_t fsid;
765 fsid.thread_index = thread_index;
766 int total_expired = 0;
770 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
771 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
772 && (~0 != pw->fa_conn_list_head[tt])
773 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
774 pw->fa_conn_list_head[tt]))) {
775 fsid.session_index = pw->fa_conn_list_head[tt];
776 vec_add1(pw->expired, fsid.session_index);
777 acl_fa_conn_list_delete_session(am, fsid);
783 vec_foreach (psid, pw->expired)
785 fsid.session_index = *psid;
786 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
788 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
789 u32 sw_if_index = sess->sw_if_index;
790 u64 sess_timeout_time =
791 sess->last_active_time + fa_session_get_timeout (am, sess);
792 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
794 #ifdef FA_NODE_VERBOSE_DEBUG
795 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
796 (int) session_index);
798 /* There was activity on the session, so the idle timeout
799 has not passed. Enqueue for another time period. */
801 acl_fa_conn_list_add_session(am, fsid, now);
802 pw->cnt_session_timer_restarted++;
806 #ifdef FA_NODE_VERBOSE_DEBUG
807 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
808 (int) session_index);
810 acl_fa_delete_session (am, sw_if_index, fsid);
811 pw->cnt_deleted_sessions++;
816 pw->cnt_already_deleted_sessions++;
819 total_expired = vec_len(pw->expired);
820 /* zero out the vector which we have acted on */
822 _vec_len (pw->expired) = 0;
823 /* if we were advancing and reached the end
824 * (no more sessions to recycle), reset the fast-forward timestamp */
826 if (pw->swipe_end_time && 0 == total_expired)
827 pw->swipe_end_time = 0;
828 return (total_expired);
832 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
834 /* try to recycle a TCP transient session */
835 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
836 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
837 fa_full_session_id_t sess_id;
838 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
839 if (~0 != sess_id.session_index) {
840 sess_id.thread_index = thread_index;
841 acl_fa_conn_list_delete_session(am, sess_id);
842 acl_fa_delete_session(am, sw_if_index, sess_id);
847 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
848 fa_5tuple_t * p5tuple)
850 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
851 clib_bihash_kv_40_8_t kv;
852 fa_full_session_id_t f_sess_id;
853 uword thread_index = os_get_thread_index();
854 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
856 f_sess_id.thread_index = thread_index;
859 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
860 f_sess_id.session_index = sess - pw->fa_sessions_pool;
862 kv.key[0] = pkv->key[0];
863 kv.key[1] = pkv->key[1];
864 kv.key[2] = pkv->key[2];
865 kv.key[3] = pkv->key[3];
866 kv.key[4] = pkv->key[4];
867 kv.value = f_sess_id.as_u64;
869 memcpy (sess, pkv, sizeof (pkv->key));
870 sess->last_active_time = now;
871 sess->sw_if_index = sw_if_index;
872 sess->tcp_flags_seen.as_u16 = 0;
873 sess->thread_index = thread_index;
874 sess->link_list_id = ~0;
875 sess->link_prev_idx = ~0;
876 sess->link_next_idx = ~0;
880 if (!acl_fa_ifc_has_sessions (am, sw_if_index))
882 acl_fa_ifc_init_sessions (am, sw_if_index);
885 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
887 acl_fa_conn_list_add_session(am, f_sess_id, now);
889 vec_validate (am->fa_session_adds_by_sw_if_index, sw_if_index);
890 am->fa_session_adds_by_sw_if_index[sw_if_index]++;
891 clib_smp_atomic_add(&am->fa_session_total_adds, 1);
895 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
896 clib_bihash_kv_40_8_t * pvalue_sess)
898 return (BV (clib_bihash_search)
899 (&am->fa_sessions_hash, &p5tuple->kv,
905 acl_fa_node_fn (vlib_main_t * vm,
906 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
907 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
908 vlib_node_registration_t * acl_fa_node)
910 u32 n_left_from, *from, *to_next;
911 acl_fa_next_t next_index;
912 u32 pkts_acl_checked = 0;
913 u32 pkts_new_session = 0;
914 u32 pkts_exist_session = 0;
915 u32 pkts_acl_permit = 0;
916 u32 pkts_restart_session_timer = 0;
917 u32 trace_bitmap = 0;
919 acl_main_t *am = &acl_main;
920 fa_5tuple_t fa_5tuple, kv_sess;
921 clib_bihash_kv_40_8_t value_sess;
922 vlib_node_runtime_t *error_node;
923 u64 now = clib_cpu_time_now ();
924 uword thread_index = os_get_thread_index ();
926 from = vlib_frame_vector_args (frame);
927 n_left_from = frame->n_vectors;
928 next_index = node->cached_next_index;
930 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
932 while (n_left_from > 0)
936 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
938 while (n_left_from > 0 && n_left_to_next > 0)
945 int acl_check_needed = 1;
946 u32 match_acl_in_index = ~0;
947 u32 match_rule_index = ~0;
950 /* speculatively enqueue b0 to the current next frame */
958 b0 = vlib_get_buffer (vm, bi0);
961 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
963 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
965 feature_bitmap0 = vnet_buffer (b0)->l2.feature_bitmap;
968 * Extract the L3/L4 matching info into a 5-tuple structure,
969 * then create a session key whose layout is independent on forward or reverse
970 * direction of the packet.
973 acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
974 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
975 acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
976 #ifdef FA_NODE_VERBOSE_DEBUG
978 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
979 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
980 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
982 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
983 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
984 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
987 /* Try to match an existing session first */
989 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
991 if (acl_fa_find_session
992 (am, sw_if_index0, &kv_sess, &value_sess))
994 trace_bitmap |= 0x80000000;
995 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
996 fa_full_session_id_t f_sess_id;
998 f_sess_id.as_u64 = value_sess.value;
999 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1001 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1002 int old_timeout_type =
1003 fa_session_get_timeout_type (am, sess);
1005 acl_fa_track_session (am, is_input, sw_if_index0, now,
1007 /* expose the session id to the tracer */
1008 match_rule_index = f_sess_id.session_index;
1009 int new_timeout_type =
1010 fa_session_get_timeout_type (am, sess);
1011 acl_check_needed = 0;
1012 pkts_exist_session += 1;
1013 /* Tracking might have changed the session timeout type, e.g. from transient to established */
1014 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1016 acl_fa_restart_timer_for_session (am, now, f_sess_id);
1017 pkts_restart_session_timer++;
1019 0x00010000 + ((0xff & old_timeout_type) << 8) +
1020 (0xff & new_timeout_type);
1023 * I estimate the likelihood to be very low - the VPP needs
1024 * to have >64K interfaces to start with and then on
1025 * exactly 64K indices apart needs to be exactly the same
1026 * 5-tuple... Anyway, since this probability is nonzero -
1027 * print an error and drop the unlucky packet.
1028 * If this shows up in real world, we would need to bump
1029 * the hash key length.
1031 if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
1032 clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
1033 acl_check_needed = 0;
1039 if (acl_check_needed)
1042 full_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1043 is_ip6, is_input, &match_acl_in_index,
1044 &match_rule_index, &trace_bitmap);
1047 pkts_acl_permit += 1;
1050 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1051 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1053 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1055 acl_fa_add_session (am, is_input, sw_if_index0, now,
1057 pkts_new_session += 1;
1062 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1073 feat_bitmap_get_next_node_index (l2_feat_next_node_index,
1076 vnet_feature_next (sw_if_index0, &next0, b0);
1079 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1080 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1082 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1083 t->sw_if_index = sw_if_index0;
1084 t->next_index = next0;
1085 t->match_acl_in_index = match_acl_in_index;
1086 t->match_rule_index = match_rule_index;
1087 t->packet_info[0] = fa_5tuple.kv.key[0];
1088 t->packet_info[1] = fa_5tuple.kv.key[1];
1089 t->packet_info[2] = fa_5tuple.kv.key[2];
1090 t->packet_info[3] = fa_5tuple.kv.key[3];
1091 t->packet_info[4] = fa_5tuple.kv.key[4];
1092 t->packet_info[5] = fa_5tuple.kv.value;
1094 t->trace_bitmap = trace_bitmap;
1097 next0 = next0 < node->n_next_nodes ? next0 : 0;
1099 b0->error = error_node->errors[error0];
1101 pkts_acl_checked += 1;
1103 /* verify speculative enqueue, maybe switch current next frame */
1104 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1105 to_next, n_left_to_next, bi0,
1109 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1112 vlib_node_increment_counter (vm, acl_fa_node->index,
1113 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1114 vlib_node_increment_counter (vm, acl_fa_node->index,
1115 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1116 vlib_node_increment_counter (vm, acl_fa_node->index,
1117 ACL_FA_ERROR_ACL_NEW_SESSION,
1119 vlib_node_increment_counter (vm, acl_fa_node->index,
1120 ACL_FA_ERROR_ACL_EXIST_SESSION,
1121 pkts_exist_session);
1122 vlib_node_increment_counter (vm, acl_fa_node->index,
1123 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1124 pkts_restart_session_timer);
1125 return frame->n_vectors;
1129 vlib_node_registration_t acl_in_l2_ip6_node;
1131 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1132 vlib_node_runtime_t * node, vlib_frame_t * frame)
1134 acl_main_t *am = &acl_main;
1135 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1136 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1137 &acl_in_l2_ip6_node);
1140 vlib_node_registration_t acl_in_l2_ip4_node;
1142 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1143 vlib_node_runtime_t * node, vlib_frame_t * frame)
1145 acl_main_t *am = &acl_main;
1146 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1147 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1148 &acl_in_l2_ip4_node);
1151 vlib_node_registration_t acl_out_l2_ip6_node;
1153 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1154 vlib_node_runtime_t * node, vlib_frame_t * frame)
1156 acl_main_t *am = &acl_main;
1157 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1158 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1159 &acl_out_l2_ip6_node);
1162 vlib_node_registration_t acl_out_l2_ip4_node;
1164 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1165 vlib_node_runtime_t * node, vlib_frame_t * frame)
1167 acl_main_t *am = &acl_main;
1168 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1169 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1170 &acl_out_l2_ip4_node);
1174 /**** L3 processing path nodes ****/
1177 vlib_node_registration_t acl_in_fa_ip6_node;
1179 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1180 vlib_node_runtime_t * node, vlib_frame_t * frame)
1182 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1185 vlib_node_registration_t acl_in_fa_ip4_node;
1187 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1188 vlib_node_runtime_t * node, vlib_frame_t * frame)
1190 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1193 vlib_node_registration_t acl_out_fa_ip6_node;
1195 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1196 vlib_node_runtime_t * node, vlib_frame_t * frame)
1198 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1201 vlib_node_registration_t acl_out_fa_ip4_node;
1203 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1204 vlib_node_runtime_t * node, vlib_frame_t * frame)
1206 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1210 * This process ensures the connection cleanup happens every so often
1211 * even in absence of traffic, as well as provides general orchestration
1212 * for requests like connection deletion on a given sw_if_index.
1217 #define foreach_acl_fa_cleaner_error \
1218 _(UNKNOWN_EVENT, "unknown event received") \
1223 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1224 foreach_acl_fa_cleaner_error
1226 ACL_FA_CLEANER_N_ERROR,
1227 } acl_fa_cleaner_error_t;
1229 static char *acl_fa_cleaner_error_strings[] = {
1230 #define _(sym,string) string,
1231 foreach_acl_fa_cleaner_error
1237 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1238 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1241 * Per-worker thread interrupt-driven cleaner thread
1242 * to clean idle connections if there are no packets
1245 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1246 vlib_node_runtime_t * rt, vlib_frame_t * f)
1248 acl_main_t *am = &acl_main;
1249 u64 now = clib_cpu_time_now ();
1250 u16 thread_index = os_get_thread_index ();
1251 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1253 #ifdef FA_NODE_VERBOSE_DEBUG
1254 clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1256 /* allow another interrupt to be queued */
1257 pw->interrupt_is_pending = 0;
1258 if (pw->clear_in_process) {
1259 if (0 == pw->swipe_end_time) {
1261 * Someone has just set the flag to start clearing.
1262 * we do this by combing through the connections up to a "time T"
1263 * which is now, and requeueing everything except the expired
1264 * connections and those matching the interface(s) being cleared.
1268 * first filter the sw_if_index bitmap that they want from us, by
1269 * a bitmap of sw_if_index for which we actually have connections.
1271 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1272 || (pw->serviced_sw_if_index_bitmap == 0)) {
1273 #ifdef FA_NODE_VERBOSE_DEBUG
1274 clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1276 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1278 #ifdef FA_NODE_VERBOSE_DEBUG
1279 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1280 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1281 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1283 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1284 pw->serviced_sw_if_index_bitmap);
1287 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1288 /* if the cross-section is a zero vector, no need to do anything. */
1289 #ifdef FA_NODE_VERBOSE_DEBUG
1290 clib_warning("WORKER: clearing done - nothing to do");
1292 pw->clear_in_process = 0;
1294 #ifdef FA_NODE_VERBOSE_DEBUG
1295 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1296 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1297 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1299 /* swipe through the connection lists until enqueue timestamps become above "now" */
1300 pw->swipe_end_time = now;
1304 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1305 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1306 if (pw->clear_in_process) {
1307 if (0 == num_expired) {
1308 /* we were clearing but we could not process any more connections. time to stop. */
1309 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1310 pw->clear_in_process = 0;
1311 #ifdef FA_NODE_VERBOSE_DEBUG
1312 clib_warning("WORKER: clearing done, all done");
1315 #ifdef FA_NODE_VERBOSE_DEBUG
1316 clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1318 /* should continue clearing.. So could they please sent an interrupt again? */
1319 pw->interrupt_is_needed = 1;
1322 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1323 /* there was too much work, we should get an interrupt ASAP */
1324 pw->interrupt_is_needed = 1;
1325 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1326 /* signal that they should trigger us less */
1327 pw->interrupt_is_unwanted = 1;
1329 /* the current rate of interrupts is ok */
1330 pw->interrupt_is_needed = 0;
1331 pw->interrupt_is_unwanted = 0;
1338 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1340 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1341 if (!pw->interrupt_is_pending) {
1342 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1343 acl_fa_worker_session_cleaner_process_node.index);
1344 pw->interrupt_is_pending = 1;
1345 /* if the interrupt was requested, mark that done. */
1346 pw->interrupt_is_needed = 0;
1351 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1354 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1355 int n_threads = vec_len(vlib_mains);
1356 for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1357 send_one_worker_interrupt(vm, am, i);
1361 /* centralized process to drive per-worker cleaners */
1363 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1366 acl_main_t *am = &acl_main;
1367 u64 now = clib_cpu_time_now ();
1368 f64 cpu_cps = vm->clib_time.clocks_per_second;
1370 /* We should check if there are connections to clean up - at least twice a second */
1371 u64 max_timer_wait_interval = cpu_cps / 2;
1372 uword event_type, *event_data = 0;
1373 acl_fa_per_worker_data_t *pw0;
1375 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1376 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1380 now = clib_cpu_time_now ();
1381 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1382 int has_pending_conns = 0;
1387 * walk over all per-thread list heads of different timeouts,
1388 * and see if there are any connections pending.
1389 * If there aren't - we do not need to wake up until the
1390 * worker code signals that it has added a connection.
1392 * Also, while we are at it, calculate the earliest we need to wake up.
1394 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1395 if (ti >= vec_len(am->per_worker_data)) {
1398 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1399 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1400 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1401 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1402 #ifdef FA_NODE_VERBOSE_DEBUG
1403 clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1405 next_expire = head_expiry;
1407 if (~0 != pw->fa_conn_list_head[tt]) {
1408 has_pending_conns = 1;
1413 /* If no pending connections then no point in timing out */
1414 if (!has_pending_conns)
1416 am->fa_cleaner_cnt_wait_without_timeout++;
1417 (void) vlib_process_wait_for_event (vm);
1418 event_type = vlib_process_get_events (vm, &event_data);
1422 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1425 /* skip waiting altogether */
1430 am->fa_cleaner_cnt_wait_with_timeout++;
1431 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1432 event_type = vlib_process_get_events (vm, &event_data);
1436 now = clib_cpu_time_now ();
1442 case ACL_FA_CLEANER_RESCHEDULE:
1443 /* Nothing to do. */
1445 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1447 uword *clear_sw_if_index_bitmap = 0;
1448 uword *sw_if_index0;
1449 #ifdef FA_NODE_VERBOSE_DEBUG
1450 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1452 vec_foreach (sw_if_index0, event_data)
1454 am->fa_cleaner_cnt_delete_by_sw_index++;
1455 #ifdef FA_NODE_VERBOSE_DEBUG
1457 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1460 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1462 #ifdef FA_NODE_VERBOSE_DEBUG
1463 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1465 vec_foreach(pw0, am->per_worker_data) {
1466 CLIB_MEMORY_BARRIER ();
1467 while (pw0->clear_in_process) {
1468 CLIB_MEMORY_BARRIER ();
1469 #ifdef FA_NODE_VERBOSE_DEBUG
1470 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1472 vlib_process_suspend(vm, 0.0001);
1473 if (pw0->interrupt_is_needed) {
1474 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1477 if (pw0->clear_in_process) {
1478 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1480 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1481 pw0->clear_in_process = 1;
1484 /* send some interrupts so they can start working */
1485 send_interrupts_to_workers(vm, am);
1487 /* now wait till they all complete */
1488 #ifdef FA_NODE_VERBOSE_DEBUG
1489 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1491 vec_foreach(pw0, am->per_worker_data) {
1492 CLIB_MEMORY_BARRIER ();
1493 while (pw0->clear_in_process) {
1494 CLIB_MEMORY_BARRIER ();
1495 #ifdef FA_NODE_VERBOSE_DEBUG
1496 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1498 vlib_process_suspend(vm, 0.0001);
1499 if (pw0->interrupt_is_needed) {
1500 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1504 #ifdef FA_NODE_VERBOSE_DEBUG
1505 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1507 clib_bitmap_free(clear_sw_if_index_bitmap);
1511 #ifdef FA_NODE_VERBOSE_DEBUG
1512 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1515 vlib_node_increment_counter (vm,
1516 acl_fa_session_cleaner_process_node.
1518 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1519 am->fa_cleaner_cnt_unknown_event++;
1523 send_interrupts_to_workers(vm, am);
1526 _vec_len (event_data) = 0;
1529 int interrupts_needed = 0;
1530 int interrupts_unwanted = 0;
1532 vec_foreach(pw0, am->per_worker_data) {
1533 if (pw0->interrupt_is_needed) {
1534 interrupts_needed++;
1535 /* the per-worker value is reset when sending the interrupt */
1537 if (pw0->interrupt_is_unwanted) {
1538 interrupts_unwanted++;
1539 pw0->interrupt_is_unwanted = 0;
1542 if (interrupts_needed) {
1543 /* they need more interrupts, do less waiting around next time */
1544 am->fa_current_cleaner_timer_wait_interval /= 2;
1545 } else if (interrupts_unwanted) {
1546 /* slowly increase the amount of sleep up to a limit */
1547 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1548 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1550 am->fa_cleaner_cnt_event_cycles++;
1558 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1560 acl_main_t *am = &acl_main;
1563 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1564 sw_if_index, enable_disable, 0, 0);
1565 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1566 sw_if_index, enable_disable, 0, 0);
1567 am->fa_in_acl_on_sw_if_index =
1568 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1573 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1574 sw_if_index, enable_disable, 0, 0);
1575 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1576 sw_if_index, enable_disable, 0, 0);
1577 am->fa_out_acl_on_sw_if_index =
1578 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1581 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1582 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1584 #ifdef FA_NODE_VERBOSE_DEBUG
1585 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1587 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1588 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1597 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1598 .function = acl_fa_worker_conn_cleaner_process,
1599 .name = "acl-plugin-fa-worker-cleaner-process",
1600 .type = VLIB_NODE_TYPE_INPUT,
1601 .state = VLIB_NODE_STATE_INTERRUPT,
1604 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1605 .function = acl_fa_session_cleaner_process,
1606 .type = VLIB_NODE_TYPE_PROCESS,
1607 .name = "acl-plugin-fa-cleaner-process",
1608 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1609 .error_strings = acl_fa_cleaner_error_strings,
1615 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1617 .function = acl_in_ip6_l2_node_fn,
1618 .name = "acl-plugin-in-ip6-l2",
1619 .vector_size = sizeof (u32),
1620 .format_trace = format_acl_fa_trace,
1621 .type = VLIB_NODE_TYPE_INTERNAL,
1622 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1623 .error_strings = acl_fa_error_strings,
1624 .n_next_nodes = ACL_FA_N_NEXT,
1627 [ACL_FA_ERROR_DROP] = "error-drop",
1631 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1633 .function = acl_in_ip4_l2_node_fn,
1634 .name = "acl-plugin-in-ip4-l2",
1635 .vector_size = sizeof (u32),
1636 .format_trace = format_acl_fa_trace,
1637 .type = VLIB_NODE_TYPE_INTERNAL,
1638 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1639 .error_strings = acl_fa_error_strings,
1640 .n_next_nodes = ACL_FA_N_NEXT,
1643 [ACL_FA_ERROR_DROP] = "error-drop",
1647 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1649 .function = acl_out_ip6_l2_node_fn,
1650 .name = "acl-plugin-out-ip6-l2",
1651 .vector_size = sizeof (u32),
1652 .format_trace = format_acl_fa_trace,
1653 .type = VLIB_NODE_TYPE_INTERNAL,
1654 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1655 .error_strings = acl_fa_error_strings,
1656 .n_next_nodes = ACL_FA_N_NEXT,
1659 [ACL_FA_ERROR_DROP] = "error-drop",
1663 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1665 .function = acl_out_ip4_l2_node_fn,
1666 .name = "acl-plugin-out-ip4-l2",
1667 .vector_size = sizeof (u32),
1668 .format_trace = format_acl_fa_trace,
1669 .type = VLIB_NODE_TYPE_INTERNAL,
1670 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1671 .error_strings = acl_fa_error_strings,
1672 .n_next_nodes = ACL_FA_N_NEXT,
1675 [ACL_FA_ERROR_DROP] = "error-drop",
1680 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1682 .function = acl_in_ip6_fa_node_fn,
1683 .name = "acl-plugin-in-ip6-fa",
1684 .vector_size = sizeof (u32),
1685 .format_trace = format_acl_fa_trace,
1686 .type = VLIB_NODE_TYPE_INTERNAL,
1687 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1688 .error_strings = acl_fa_error_strings,
1689 .n_next_nodes = ACL_FA_N_NEXT,
1692 [ACL_FA_ERROR_DROP] = "error-drop",
1696 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1698 .arc_name = "ip6-unicast",
1699 .node_name = "acl-plugin-in-ip6-fa",
1700 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1703 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1705 .function = acl_in_ip4_fa_node_fn,
1706 .name = "acl-plugin-in-ip4-fa",
1707 .vector_size = sizeof (u32),
1708 .format_trace = format_acl_fa_trace,
1709 .type = VLIB_NODE_TYPE_INTERNAL,
1710 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1711 .error_strings = acl_fa_error_strings,
1712 .n_next_nodes = ACL_FA_N_NEXT,
1715 [ACL_FA_ERROR_DROP] = "error-drop",
1719 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1721 .arc_name = "ip4-unicast",
1722 .node_name = "acl-plugin-in-ip4-fa",
1723 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1727 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1729 .function = acl_out_ip6_fa_node_fn,
1730 .name = "acl-plugin-out-ip6-fa",
1731 .vector_size = sizeof (u32),
1732 .format_trace = format_acl_fa_trace,
1733 .type = VLIB_NODE_TYPE_INTERNAL,
1734 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1735 .error_strings = acl_fa_error_strings,
1736 .n_next_nodes = ACL_FA_N_NEXT,
1739 [ACL_FA_ERROR_DROP] = "error-drop",
1743 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1745 .arc_name = "ip6-output",
1746 .node_name = "acl-plugin-out-ip6-fa",
1747 .runs_before = VNET_FEATURES ("interface-output"),
1750 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1752 .function = acl_out_ip4_fa_node_fn,
1753 .name = "acl-plugin-out-ip4-fa",
1754 .vector_size = sizeof (u32),
1755 .format_trace = format_acl_fa_trace,
1756 .type = VLIB_NODE_TYPE_INTERNAL,
1757 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1758 .error_strings = acl_fa_error_strings,
1759 .n_next_nodes = ACL_FA_N_NEXT,
1760 /* edit / add dispositions here */
1763 [ACL_FA_ERROR_DROP] = "error-drop",
1767 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1769 .arc_name = "ip4-output",
1770 .node_name = "acl-plugin-out-ip4-fa",
1771 .runs_before = VNET_FEATURES ("interface-output"),