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 <vppinfra/bihash_40_8.h>
25 #include <vppinfra/bihash_template.h>
26 #include <vppinfra/bihash_template.c>
29 #include "hash_lookup.h"
35 u32 match_acl_in_index;
43 format_fa_5tuple (u8 * s, va_list * args)
45 fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
47 return format(s, "%s sw_if_index %d (lsb16 %d) l3 %s%s %U -> %U"
48 " l4 proto %d l4_valid %d port %d -> %d tcp flags (%s) %02x rsvd %x",
49 p5t->pkt.is_input ? "input" : "output",
50 p5t->pkt.sw_if_index, p5t->l4.lsb_of_sw_if_index, p5t->pkt.is_ip6 ? "ip6" : "ip4",
51 p5t->pkt.is_nonfirst_fragment ? " non-initial fragment" : "",
52 format_ip46_address, &p5t->addr[0], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
53 format_ip46_address, &p5t->addr[1], p5t->pkt.is_ip6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
54 p5t->l4.proto, p5t->pkt.l4_valid,
55 p5t->l4.port[0], p5t->l4.port[1],
56 p5t->pkt.tcp_flags_valid ? "valid": "invalid",
58 p5t->pkt.flags_reserved);
62 format_acl_plugin_5tuple (u8 * s, va_list * args)
64 return format_fa_5tuple(s, args);
67 /* packet trace format function */
69 format_acl_fa_trace (u8 * s, va_list * args)
71 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
77 "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
78 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
79 t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
80 t->match_rule_index, t->trace_bitmap,
81 t->packet_info[0], t->packet_info[1], t->packet_info[2],
82 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
84 /* Now also print out the packet_info in a form usable by humans */
85 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
90 #define foreach_acl_fa_error \
91 _(ACL_DROP, "ACL deny packets") \
92 _(ACL_PERMIT, "ACL permit packets") \
93 _(ACL_NEW_SESSION, "new sessions added") \
94 _(ACL_EXIST_SESSION, "existing session packets") \
95 _(ACL_CHECK, "checked packets") \
96 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
97 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
102 #define _(sym,str) ACL_FA_ERROR_##sym,
108 static char *acl_fa_error_strings[] = {
109 #define _(sym,string) string,
116 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
118 u8 *p = vlib_buffer_get_current (b0) + offset;
124 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
125 int prefixlen, int is_ip6)
129 /* match any always succeeds */
134 if (memcmp (addr1, addr2, prefixlen / 8))
136 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
141 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
142 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
143 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
144 return (b1 & mask0) == b2;
148 /* The prefix fits into integer number of bytes, so nothing left to do */
154 uint32_t a1 = ntohl (addr1->ip4.as_u32);
155 uint32_t a2 = ntohl (addr2->ip4.as_u32);
156 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
157 return (a1 & mask0) == a2;
162 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
164 return ((port >= port_first) && (port <= port_last));
168 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
169 int is_ip6, u8 * r_action, u32 * r_acl_match_p,
170 u32 * r_rule_match_p, u32 * trace_bitmap)
176 if (pool_is_free_index (am->acls, acl_index))
179 *r_acl_match_p = acl_index;
181 *r_rule_match_p = -1;
182 /* the ACL does not exist but is used for policy. Block traffic. */
185 a = am->acls + acl_index;
186 for (i = 0; i < a->count; i++)
189 if (is_ip6 != r->is_ipv6)
193 if (!fa_acl_match_addr
194 (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
197 #ifdef FA_NODE_VERBOSE_DEBUG
199 ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
200 acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
201 r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
202 &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
206 if (!fa_acl_match_addr
207 (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
210 #ifdef FA_NODE_VERBOSE_DEBUG
212 ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
213 acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
214 r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
215 &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
218 ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
219 acl_index, i, pkt_5tuple->l4.proto, r->proto);
223 if (pkt_5tuple->l4.proto != r->proto)
226 if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
227 am->l4_match_nonfirst_fragment))
229 /* non-initial fragment with frag match configured - match this rule */
230 *trace_bitmap |= 0x80000000;
231 *r_action = r->is_permit;
233 *r_acl_match_p = acl_index;
239 /* A sanity check just to ensure we are about to match the ports extracted from the packet */
240 if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
243 #ifdef FA_NODE_VERBOSE_DEBUG
245 ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
246 acl_index, i, pkt_5tuple->l4.proto, r->proto);
249 if (!fa_acl_match_port
250 (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
251 r->src_port_or_type_last, is_ip6))
254 #ifdef FA_NODE_VERBOSE_DEBUG
256 ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
257 acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
258 r->src_port_or_type_last);
261 if (!fa_acl_match_port
262 (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
263 r->dst_port_or_code_last, is_ip6))
266 #ifdef FA_NODE_VERBOSE_DEBUG
268 ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
269 acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
270 r->dst_port_or_code_last);
272 if (pkt_5tuple->pkt.tcp_flags_valid
273 && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
277 /* everything matches! */
278 #ifdef FA_NODE_VERBOSE_DEBUG
279 clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
280 acl_index, i, r->is_permit);
282 *r_action = r->is_permit;
284 *r_acl_match_p = acl_index;
293 linear_multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
294 int is_ip6, int is_input, u32 * acl_match_p,
295 u32 * rule_match_p, u32 * trace_bitmap)
297 acl_main_t *am = &acl_main;
304 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
305 acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
309 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
310 acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
312 for (i = 0; i < vec_len (acl_vector); i++)
314 #ifdef FA_NODE_VERBOSE_DEBUG
315 clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
318 if (single_acl_match_5tuple
319 (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
320 acl_match_p, rule_match_p, trace_bitmap))
325 if (vec_len (acl_vector) > 0)
327 /* If there are ACLs and none matched, deny by default */
330 #ifdef FA_NODE_VERBOSE_DEBUG
331 clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
333 /* Deny by default. If there are no ACLs defined we should not be here. */
338 multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
339 int is_ip6, int is_input, u32 * acl_match_p,
340 u32 * rule_match_p, u32 * trace_bitmap)
342 acl_main_t *am = &acl_main;
343 if (am->use_hash_acl_matching) {
344 return hash_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
345 is_input, acl_match_p, rule_match_p, trace_bitmap);
347 return linear_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
348 is_input, acl_match_p, rule_match_p, trace_bitmap);
353 offset_within_packet (vlib_buffer_t * b0, int offset)
355 /* For the purposes of this code, "within" means we have at least 8 bytes after it */
356 return (offset <= (b0->current_length - 8));
360 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
361 int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
368 /* IP4 and IP6 protocol numbers of ICMP */
369 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
373 l3_offset = ethernet_buffer_header_size(b0);
380 l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
383 /* key[0..3] contains src/dst address and is cleared/set below */
384 /* Remainder of the key and per-packet non-key data */
385 p5tuple_pkt->kv.key[4] = 0;
386 p5tuple_pkt->kv.value = 0;
390 clib_memcpy (&p5tuple_pkt->addr,
391 get_ptr_to_offset (b0,
392 offsetof (ip6_header_t,
393 src_address) + l3_offset),
394 sizeof (p5tuple_pkt->addr));
396 *(u8 *) get_ptr_to_offset (b0,
397 offsetof (ip6_header_t,
398 protocol) + l3_offset);
399 l4_offset = l3_offset + sizeof (ip6_header_t);
400 #ifdef FA_NODE_VERBOSE_DEBUG
401 clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
404 /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
405 int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
406 if (PREDICT_FALSE (need_skip_eh))
408 while (need_skip_eh && offset_within_packet (b0, l4_offset))
410 /* Fragment header needs special handling */
411 if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
413 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
415 clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
416 frag_offset = ntohs(frag_offset) >> 3;
419 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
420 /* invalidate L4 offset so we don't try to find L4 info */
421 l4_offset += b0->current_length;
425 /* First fragment: skip the frag header and move on. */
431 u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
432 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
433 l4_offset += 8 * (1 + (u16) nwords);
435 #ifdef FA_NODE_VERBOSE_DEBUG
436 clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
440 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
446 p5tuple_pkt->kv.key[0] = 0;
447 p5tuple_pkt->kv.key[1] = 0;
448 p5tuple_pkt->kv.key[2] = 0;
449 p5tuple_pkt->kv.key[3] = 0;
450 clib_memcpy (&p5tuple_pkt->addr[0].ip4,
451 get_ptr_to_offset (b0,
452 offsetof (ip4_header_t,
453 src_address) + l3_offset),
454 sizeof (p5tuple_pkt->addr[0].ip4));
455 clib_memcpy (&p5tuple_pkt->addr[1].ip4,
456 get_ptr_to_offset (b0,
457 offsetof (ip4_header_t,
458 dst_address) + l3_offset),
459 sizeof (p5tuple_pkt->addr[1].ip4));
461 *(u8 *) get_ptr_to_offset (b0,
462 offsetof (ip4_header_t,
463 protocol) + l3_offset);
464 l4_offset = l3_offset + sizeof (ip4_header_t);
465 u16 flags_and_fragment_offset;
466 clib_memcpy (&flags_and_fragment_offset,
467 get_ptr_to_offset (b0,
468 offsetof (ip4_header_t,
469 flags_and_fragment_offset)) + l3_offset,
470 sizeof(flags_and_fragment_offset));
471 flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
473 /* non-initial fragments have non-zero offset */
474 if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
476 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
477 /* invalidate L4 offset so we don't try to find L4 info */
478 l4_offset += b0->current_length;
482 p5tuple_pkt->l4.proto = proto;
483 if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
485 p5tuple_pkt->pkt.l4_valid = 1;
486 if (icmp_protos[is_ip6] == proto)
489 p5tuple_pkt->l4.port[0] =
490 *(u8 *) get_ptr_to_offset (b0,
491 l4_offset + offsetof (icmp46_header_t,
494 p5tuple_pkt->l4.port[1] =
495 *(u8 *) get_ptr_to_offset (b0,
496 l4_offset + offsetof (icmp46_header_t,
499 else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
502 get_ptr_to_offset (b0,
503 l4_offset + offsetof (tcp_header_t,
506 p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
507 p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
509 p5tuple_pkt->pkt.tcp_flags =
510 *(u8 *) get_ptr_to_offset (b0,
511 l4_offset + offsetof (tcp_header_t,
513 p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
516 * FIXME: rather than the above conditional, here could
517 * be a nice generic mechanism to extract two L4 values:
519 * have a per-protocol array of 4 elements like this:
520 * u8 offset; to take the byte from, off L4 header
521 * u8 mask; to mask it with, before storing
523 * this way we can describe UDP, TCP and ICMP[46] semantics,
524 * and add a sort of FPM-type behavior for other protocols.
526 * Of course, is it faster ? and is it needed ?
533 /* Session keys match the packets received, and mirror the packets sent */
535 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
536 fa_5tuple_t * p5tuple_sess)
538 int src_index = is_input ? 0 : 1;
539 int dst_index = is_input ? 1 : 0;
540 p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
541 p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
542 p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
543 p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
544 p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
549 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
551 return am->fa_sessions_hash_is_initialized;
555 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
557 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
562 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
564 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
570 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
572 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
574 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
575 TCP_FLAGS_RSTFINACKSYN);
576 switch (sess->info.l4.proto)
579 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
581 return ACL_TIMEOUT_TCP_IDLE;
585 return ACL_TIMEOUT_TCP_TRANSIENT;
589 return ACL_TIMEOUT_UDP_IDLE;
592 return ACL_TIMEOUT_UDP_IDLE;
598 fa_session_get_shortest_timeout(acl_main_t * am)
602 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
603 if (timeout > am->session_timeout_sec[timeout_type]) {
604 timeout = am->session_timeout_sec[timeout_type];
611 * Get the timeout of the session in a list since its enqueue time.
615 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
617 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
619 * we have the shortest possible timeout type in all the lists
620 * (see README-multicore for the rationale)
622 timeout *= fa_session_get_shortest_timeout(am);
627 * Get the idle timeout of a session.
631 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
633 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
634 int timeout_type = fa_session_get_timeout_type (am, sess);
635 timeout *= am->session_timeout_sec[timeout_type];
640 acl_fa_verify_init_sessions (acl_main_t * am)
642 if (!am->fa_sessions_hash_is_initialized) {
644 /* Allocate the per-worker sessions pools */
645 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
646 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
647 pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
650 /* ... and the interface session hash table */
651 BV (clib_bihash_init) (&am->fa_sessions_hash,
652 "ACL plugin FA session bihash",
653 am->fa_conn_table_hash_num_buckets,
654 am->fa_conn_table_hash_memory_size);
655 am->fa_sessions_hash_is_initialized = 1;
659 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
661 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
662 fa_session_t *sess = pool_is_free_index (pw->fa_sessions_pool, session_index) ? 0 : pool_elt_at_index(pw->fa_sessions_pool, session_index);
666 static inline int is_valid_session_ptr(acl_main_t *am, u16 thread_index, fa_session_t *sess)
668 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
669 return ((sess != 0) && ((sess - pw->fa_sessions_pool) < pool_len(pw->fa_sessions_pool)));
673 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
675 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
676 u8 list_id = fa_session_get_timeout_type(am, sess);
677 uword thread_index = os_get_thread_index ();
678 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
679 /* the retrieved session thread index must be necessarily the same as the one in the key */
680 ASSERT (sess->thread_index == sess_id.thread_index);
681 /* the retrieved session thread index must be the same as current thread */
682 ASSERT (sess->thread_index == thread_index);
683 sess->link_enqueue_time = now;
684 sess->link_list_id = list_id;
685 sess->link_next_idx = ~0;
686 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
687 if (~0 != pw->fa_conn_list_tail[list_id]) {
688 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
689 prev_sess->link_next_idx = sess_id.session_index;
690 /* We should never try to link with a session on another thread */
691 ASSERT(prev_sess->thread_index == sess->thread_index);
693 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
694 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
696 if (~0 == pw->fa_conn_list_head[list_id]) {
697 pw->fa_conn_list_head[list_id] = sess_id.session_index;
702 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
704 uword thread_index = os_get_thread_index ();
705 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
706 if (thread_index != sess_id.thread_index) {
707 /* If another thread attempts to delete the session, fail it. */
708 #ifdef FA_NODE_VERBOSE_DEBUG
709 clib_warning("thread id in key %d != curr thread index, not deleting");
713 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
714 /* we should never try to delete the session with another thread index */
715 ASSERT(sess->thread_index == thread_index);
716 if (~0 != sess->link_prev_idx) {
717 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
718 /* the previous session must be in the same list as this one */
719 ASSERT(prev_sess->link_list_id == sess->link_list_id);
720 prev_sess->link_next_idx = sess->link_next_idx;
722 if (~0 != sess->link_next_idx) {
723 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
724 /* The next session must be in the same list as the one we are deleting */
725 ASSERT(next_sess->link_list_id == sess->link_list_id);
726 next_sess->link_prev_idx = sess->link_prev_idx;
728 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
729 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
731 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
732 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
738 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
740 if (acl_fa_conn_list_delete_session(am, sess_id)) {
741 acl_fa_conn_list_add_session(am, sess_id, now);
745 * Our thread does not own this connection, so we can not delete
746 * The session. To avoid the complicated signaling, we simply
747 * pick the list waiting time to be the shortest of the timeouts.
748 * This way we do not have to do anything special, and let
749 * the regular requeue check take care of everything.
757 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
758 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
760 sess->last_active_time = now;
761 if (pkt_5tuple->pkt.tcp_flags_valid)
763 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
770 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
772 void *oldheap = clib_mem_set_heap(am->acl_mheap);
773 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
774 ASSERT(sess->thread_index == os_get_thread_index ());
775 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
777 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
778 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
779 /* Deleting from timer structures not needed,
780 as the caller must have dealt with the timers. */
781 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
782 clib_mem_set_heap (oldheap);
783 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
784 clib_smp_atomic_add(&am->fa_session_total_dels, 1);
788 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
791 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
792 return (curr_sess_count < am->fa_conn_table_max_entries);
796 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)
798 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
800 * We can not check just the index here because inbetween the worker thread might
801 * dequeue the connection from the head just as we are about to check it.
803 if (!is_valid_session_ptr(am, thread_index, sess)) {
804 return ~0LL; // infinity.
807 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
813 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)
815 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
817 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
818 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
822 * see if there are sessions ready to be checked,
823 * do the maintenance (requeue or delete), and
824 * return the total number of sessions reclaimed.
827 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
829 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
830 fa_full_session_id_t fsid;
831 fsid.thread_index = thread_index;
832 int total_expired = 0;
836 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
837 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
838 && (~0 != pw->fa_conn_list_head[tt])
839 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
840 pw->fa_conn_list_head[tt]))) {
841 fsid.session_index = pw->fa_conn_list_head[tt];
842 vec_add1(pw->expired, fsid.session_index);
843 acl_fa_conn_list_delete_session(am, fsid);
849 vec_foreach (psid, pw->expired)
851 fsid.session_index = *psid;
852 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
854 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
855 u32 sw_if_index = sess->sw_if_index;
856 u64 sess_timeout_time =
857 sess->last_active_time + fa_session_get_timeout (am, sess);
858 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
860 #ifdef FA_NODE_VERBOSE_DEBUG
861 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
862 (int) session_index);
864 /* There was activity on the session, so the idle timeout
865 has not passed. Enqueue for another time period. */
867 acl_fa_conn_list_add_session(am, fsid, now);
868 pw->cnt_session_timer_restarted++;
872 #ifdef FA_NODE_VERBOSE_DEBUG
873 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
874 (int) session_index);
876 acl_fa_delete_session (am, sw_if_index, fsid);
877 pw->cnt_deleted_sessions++;
882 pw->cnt_already_deleted_sessions++;
885 total_expired = vec_len(pw->expired);
886 /* zero out the vector which we have acted on */
888 _vec_len (pw->expired) = 0;
889 /* if we were advancing and reached the end
890 * (no more sessions to recycle), reset the fast-forward timestamp */
892 if (pw->swipe_end_time && 0 == total_expired)
893 pw->swipe_end_time = 0;
894 return (total_expired);
898 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
900 /* try to recycle a TCP transient session */
901 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
902 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
903 fa_full_session_id_t sess_id;
904 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
905 if (~0 != sess_id.session_index) {
906 sess_id.thread_index = thread_index;
907 acl_fa_conn_list_delete_session(am, sess_id);
908 acl_fa_delete_session(am, sw_if_index, sess_id);
912 static fa_session_t *
913 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
914 fa_5tuple_t * p5tuple)
916 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
917 clib_bihash_kv_40_8_t kv;
918 fa_full_session_id_t f_sess_id;
919 uword thread_index = os_get_thread_index();
920 void *oldheap = clib_mem_set_heap(am->acl_mheap);
921 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
923 f_sess_id.thread_index = thread_index;
926 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
927 f_sess_id.session_index = sess - pw->fa_sessions_pool;
929 kv.key[0] = pkv->key[0];
930 kv.key[1] = pkv->key[1];
931 kv.key[2] = pkv->key[2];
932 kv.key[3] = pkv->key[3];
933 kv.key[4] = pkv->key[4];
934 kv.value = f_sess_id.as_u64;
936 memcpy (sess, pkv, sizeof (pkv->key));
937 sess->last_active_time = now;
938 sess->sw_if_index = sw_if_index;
939 sess->tcp_flags_seen.as_u16 = 0;
940 sess->thread_index = thread_index;
941 sess->link_list_id = ~0;
942 sess->link_prev_idx = ~0;
943 sess->link_next_idx = ~0;
947 ASSERT(am->fa_sessions_hash_is_initialized == 1);
948 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
950 acl_fa_conn_list_add_session(am, f_sess_id, now);
952 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
953 clib_mem_set_heap (oldheap);
954 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
955 clib_smp_atomic_add(&am->fa_session_total_adds, 1);
960 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
961 clib_bihash_kv_40_8_t * pvalue_sess)
963 return (BV (clib_bihash_search)
964 (&am->fa_sessions_hash, &p5tuple->kv,
970 acl_fa_node_fn (vlib_main_t * vm,
971 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
972 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
973 vlib_node_registration_t * acl_fa_node)
975 u32 n_left_from, *from, *to_next;
976 acl_fa_next_t next_index;
977 u32 pkts_acl_checked = 0;
978 u32 pkts_new_session = 0;
979 u32 pkts_exist_session = 0;
980 u32 pkts_acl_permit = 0;
981 u32 pkts_restart_session_timer = 0;
982 u32 trace_bitmap = 0;
983 acl_main_t *am = &acl_main;
984 fa_5tuple_t fa_5tuple, kv_sess;
985 clib_bihash_kv_40_8_t value_sess;
986 vlib_node_runtime_t *error_node;
987 u64 now = clib_cpu_time_now ();
988 uword thread_index = os_get_thread_index ();
990 from = vlib_frame_vector_args (frame);
991 n_left_from = frame->n_vectors;
992 next_index = node->cached_next_index;
994 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
996 while (n_left_from > 0)
1000 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1002 while (n_left_from > 0 && n_left_to_next > 0)
1009 int acl_check_needed = 1;
1010 u32 match_acl_in_index = ~0;
1011 u32 match_rule_index = ~0;
1014 /* speculatively enqueue b0 to the current next frame */
1020 n_left_to_next -= 1;
1022 b0 = vlib_get_buffer (vm, bi0);
1025 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1027 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1030 * Extract the L3/L4 matching info into a 5-tuple structure,
1031 * then create a session key whose layout is independent on forward or reverse
1032 * direction of the packet.
1035 acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
1036 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
1037 acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
1038 fa_5tuple.pkt.sw_if_index = sw_if_index0;
1039 fa_5tuple.pkt.is_ip6 = is_ip6;
1040 fa_5tuple.pkt.is_input = is_input;
1041 fa_5tuple.pkt.mask_type_index_lsb = ~0;
1042 #ifdef FA_NODE_VERBOSE_DEBUG
1044 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1045 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
1046 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
1048 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1049 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
1050 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
1053 /* Try to match an existing session first */
1055 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
1057 if (acl_fa_find_session
1058 (am, sw_if_index0, &kv_sess, &value_sess))
1060 trace_bitmap |= 0x80000000;
1061 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
1062 fa_full_session_id_t f_sess_id;
1064 f_sess_id.as_u64 = value_sess.value;
1065 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1067 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1068 int old_timeout_type =
1069 fa_session_get_timeout_type (am, sess);
1071 acl_fa_track_session (am, is_input, sw_if_index0, now,
1073 /* expose the session id to the tracer */
1074 match_rule_index = f_sess_id.session_index;
1075 int new_timeout_type =
1076 fa_session_get_timeout_type (am, sess);
1077 acl_check_needed = 0;
1078 pkts_exist_session += 1;
1079 /* Tracking might have changed the session timeout type, e.g. from transient to established */
1080 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1082 acl_fa_restart_timer_for_session (am, now, f_sess_id);
1083 pkts_restart_session_timer++;
1085 0x00010000 + ((0xff & old_timeout_type) << 8) +
1086 (0xff & new_timeout_type);
1089 * I estimate the likelihood to be very low - the VPP needs
1090 * to have >64K interfaces to start with and then on
1091 * exactly 64K indices apart needs to be exactly the same
1092 * 5-tuple... Anyway, since this probability is nonzero -
1093 * print an error and drop the unlucky packet.
1094 * If this shows up in real world, we would need to bump
1095 * the hash key length.
1097 if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
1098 clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
1099 acl_check_needed = 0;
1105 if (acl_check_needed)
1108 multi_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1109 is_ip6, is_input, &match_acl_in_index,
1110 &match_rule_index, &trace_bitmap);
1113 pkts_acl_permit += 1;
1116 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1117 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1119 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1121 fa_session_t *sess = acl_fa_add_session (am, is_input, sw_if_index0, now,
1123 acl_fa_track_session (am, is_input, sw_if_index0, now,
1125 pkts_new_session += 1;
1130 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1140 next0 = vnet_l2_feature_next (b0, l2_feat_next_node_index, 0);
1142 vnet_feature_next (sw_if_index0, &next0, b0);
1145 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1146 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1148 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1149 t->sw_if_index = sw_if_index0;
1150 t->next_index = next0;
1151 t->match_acl_in_index = match_acl_in_index;
1152 t->match_rule_index = match_rule_index;
1153 t->packet_info[0] = fa_5tuple.kv.key[0];
1154 t->packet_info[1] = fa_5tuple.kv.key[1];
1155 t->packet_info[2] = fa_5tuple.kv.key[2];
1156 t->packet_info[3] = fa_5tuple.kv.key[3];
1157 t->packet_info[4] = fa_5tuple.kv.key[4];
1158 t->packet_info[5] = fa_5tuple.kv.value;
1160 t->trace_bitmap = trace_bitmap;
1163 next0 = next0 < node->n_next_nodes ? next0 : 0;
1165 b0->error = error_node->errors[error0];
1167 pkts_acl_checked += 1;
1169 /* verify speculative enqueue, maybe switch current next frame */
1170 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1171 to_next, n_left_to_next, bi0,
1175 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1178 vlib_node_increment_counter (vm, acl_fa_node->index,
1179 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1180 vlib_node_increment_counter (vm, acl_fa_node->index,
1181 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1182 vlib_node_increment_counter (vm, acl_fa_node->index,
1183 ACL_FA_ERROR_ACL_NEW_SESSION,
1185 vlib_node_increment_counter (vm, acl_fa_node->index,
1186 ACL_FA_ERROR_ACL_EXIST_SESSION,
1187 pkts_exist_session);
1188 vlib_node_increment_counter (vm, acl_fa_node->index,
1189 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1190 pkts_restart_session_timer);
1191 return frame->n_vectors;
1195 vlib_node_registration_t acl_in_l2_ip6_node;
1197 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1198 vlib_node_runtime_t * node, vlib_frame_t * frame)
1200 acl_main_t *am = &acl_main;
1201 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1202 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1203 &acl_in_l2_ip6_node);
1206 vlib_node_registration_t acl_in_l2_ip4_node;
1208 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1209 vlib_node_runtime_t * node, vlib_frame_t * frame)
1211 acl_main_t *am = &acl_main;
1212 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1213 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1214 &acl_in_l2_ip4_node);
1217 vlib_node_registration_t acl_out_l2_ip6_node;
1219 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1220 vlib_node_runtime_t * node, vlib_frame_t * frame)
1222 acl_main_t *am = &acl_main;
1223 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1224 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1225 &acl_out_l2_ip6_node);
1228 vlib_node_registration_t acl_out_l2_ip4_node;
1230 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1231 vlib_node_runtime_t * node, vlib_frame_t * frame)
1233 acl_main_t *am = &acl_main;
1234 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1235 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1236 &acl_out_l2_ip4_node);
1240 /**** L3 processing path nodes ****/
1243 vlib_node_registration_t acl_in_fa_ip6_node;
1245 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1246 vlib_node_runtime_t * node, vlib_frame_t * frame)
1248 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1251 vlib_node_registration_t acl_in_fa_ip4_node;
1253 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1254 vlib_node_runtime_t * node, vlib_frame_t * frame)
1256 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1259 vlib_node_registration_t acl_out_fa_ip6_node;
1261 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1262 vlib_node_runtime_t * node, vlib_frame_t * frame)
1264 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1267 vlib_node_registration_t acl_out_fa_ip4_node;
1269 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1270 vlib_node_runtime_t * node, vlib_frame_t * frame)
1272 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1276 * This process ensures the connection cleanup happens every so often
1277 * even in absence of traffic, as well as provides general orchestration
1278 * for requests like connection deletion on a given sw_if_index.
1283 #define foreach_acl_fa_cleaner_error \
1284 _(UNKNOWN_EVENT, "unknown event received") \
1289 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1290 foreach_acl_fa_cleaner_error
1292 ACL_FA_CLEANER_N_ERROR,
1293 } acl_fa_cleaner_error_t;
1295 static char *acl_fa_cleaner_error_strings[] = {
1296 #define _(sym,string) string,
1297 foreach_acl_fa_cleaner_error
1303 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1304 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1307 * Per-worker thread interrupt-driven cleaner thread
1308 * to clean idle connections if there are no packets
1311 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1312 vlib_node_runtime_t * rt, vlib_frame_t * f)
1314 acl_main_t *am = &acl_main;
1315 u64 now = clib_cpu_time_now ();
1316 u16 thread_index = os_get_thread_index ();
1317 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1319 #ifdef FA_NODE_VERBOSE_DEBUG
1320 clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1322 /* allow another interrupt to be queued */
1323 pw->interrupt_is_pending = 0;
1324 if (pw->clear_in_process) {
1325 if (0 == pw->swipe_end_time) {
1327 * Someone has just set the flag to start clearing.
1328 * we do this by combing through the connections up to a "time T"
1329 * which is now, and requeueing everything except the expired
1330 * connections and those matching the interface(s) being cleared.
1334 * first filter the sw_if_index bitmap that they want from us, by
1335 * a bitmap of sw_if_index for which we actually have connections.
1337 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1338 || (pw->serviced_sw_if_index_bitmap == 0)) {
1339 #ifdef FA_NODE_VERBOSE_DEBUG
1340 clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1342 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1344 #ifdef FA_NODE_VERBOSE_DEBUG
1345 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1346 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1347 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1349 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1350 pw->serviced_sw_if_index_bitmap);
1353 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1354 /* if the cross-section is a zero vector, no need to do anything. */
1355 #ifdef FA_NODE_VERBOSE_DEBUG
1356 clib_warning("WORKER: clearing done - nothing to do");
1358 pw->clear_in_process = 0;
1360 #ifdef FA_NODE_VERBOSE_DEBUG
1361 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1362 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1363 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1365 /* swipe through the connection lists until enqueue timestamps become above "now" */
1366 pw->swipe_end_time = now;
1370 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1371 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1372 if (pw->clear_in_process) {
1373 if (0 == num_expired) {
1374 /* we were clearing but we could not process any more connections. time to stop. */
1375 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1376 pw->clear_in_process = 0;
1377 #ifdef FA_NODE_VERBOSE_DEBUG
1378 clib_warning("WORKER: clearing done, all done");
1381 #ifdef FA_NODE_VERBOSE_DEBUG
1382 clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1384 /* should continue clearing.. So could they please sent an interrupt again? */
1385 pw->interrupt_is_needed = 1;
1388 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1389 /* there was too much work, we should get an interrupt ASAP */
1390 pw->interrupt_is_needed = 1;
1391 pw->interrupt_is_unwanted = 0;
1392 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1393 /* signal that they should trigger us less */
1394 pw->interrupt_is_needed = 0;
1395 pw->interrupt_is_unwanted = 1;
1397 /* the current rate of interrupts is ok */
1398 pw->interrupt_is_needed = 0;
1399 pw->interrupt_is_unwanted = 0;
1402 pw->interrupt_generation = am->fa_interrupt_generation;
1407 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1409 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1410 if (!pw->interrupt_is_pending) {
1411 pw->interrupt_is_pending = 1;
1412 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1413 acl_fa_worker_session_cleaner_process_node.index);
1414 /* if the interrupt was requested, mark that done. */
1415 /* pw->interrupt_is_needed = 0; */
1420 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1423 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1424 int n_threads = vec_len(vlib_mains);
1425 for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1426 send_one_worker_interrupt(vm, am, i);
1430 /* centralized process to drive per-worker cleaners */
1432 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1435 acl_main_t *am = &acl_main;
1437 f64 cpu_cps = vm->clib_time.clocks_per_second;
1439 /* We should check if there are connections to clean up - at least twice a second */
1440 u64 max_timer_wait_interval = cpu_cps / 2;
1441 uword event_type, *event_data = 0;
1442 acl_fa_per_worker_data_t *pw0;
1444 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1445 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1446 am->fa_interrupt_generation = 1;
1449 now = clib_cpu_time_now ();
1450 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1451 int has_pending_conns = 0;
1456 * walk over all per-thread list heads of different timeouts,
1457 * and see if there are any connections pending.
1458 * If there aren't - we do not need to wake up until the
1459 * worker code signals that it has added a connection.
1461 * Also, while we are at it, calculate the earliest we need to wake up.
1463 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1464 if (ti >= vec_len(am->per_worker_data)) {
1467 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1468 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1469 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1470 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1471 #ifdef FA_NODE_VERBOSE_DEBUG
1472 clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1474 next_expire = head_expiry;
1476 if (~0 != pw->fa_conn_list_head[tt]) {
1477 has_pending_conns = 1;
1482 /* If no pending connections and no ACL applied then no point in timing out */
1483 if (!has_pending_conns && (0 == am->fa_total_enabled_count))
1485 am->fa_cleaner_cnt_wait_without_timeout++;
1486 (void) vlib_process_wait_for_event (vm);
1487 event_type = vlib_process_get_events (vm, &event_data);
1491 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1494 /* skip waiting altogether */
1499 am->fa_cleaner_cnt_wait_with_timeout++;
1500 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1501 event_type = vlib_process_get_events (vm, &event_data);
1510 case ACL_FA_CLEANER_RESCHEDULE:
1511 /* Nothing to do. */
1513 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1515 uword *clear_sw_if_index_bitmap = 0;
1516 uword *sw_if_index0;
1518 #ifdef FA_NODE_VERBOSE_DEBUG
1519 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1521 vec_foreach (sw_if_index0, event_data)
1523 am->fa_cleaner_cnt_delete_by_sw_index++;
1524 #ifdef FA_NODE_VERBOSE_DEBUG
1526 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1529 if (*sw_if_index0 == ~0)
1535 if (!pool_is_free_index (am->vnet_main->interface_main.sw_interfaces, *sw_if_index0))
1537 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1541 #ifdef FA_NODE_VERBOSE_DEBUG
1542 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1544 vec_foreach(pw0, am->per_worker_data) {
1545 CLIB_MEMORY_BARRIER ();
1546 while (pw0->clear_in_process) {
1547 CLIB_MEMORY_BARRIER ();
1548 #ifdef FA_NODE_VERBOSE_DEBUG
1549 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1551 vlib_process_suspend(vm, 0.0001);
1552 if (pw0->interrupt_is_needed) {
1553 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1556 if (pw0->clear_in_process) {
1557 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1561 /* if we need to clear all, then just clear the interfaces that we are servicing */
1562 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(pw0->serviced_sw_if_index_bitmap);
1566 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1568 pw0->clear_in_process = 1;
1571 /* send some interrupts so they can start working */
1572 send_interrupts_to_workers(vm, am);
1574 /* now wait till they all complete */
1575 #ifdef FA_NODE_VERBOSE_DEBUG
1576 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1578 vec_foreach(pw0, am->per_worker_data) {
1579 CLIB_MEMORY_BARRIER ();
1580 while (pw0->clear_in_process) {
1581 CLIB_MEMORY_BARRIER ();
1582 #ifdef FA_NODE_VERBOSE_DEBUG
1583 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1585 vlib_process_suspend(vm, 0.0001);
1586 if (pw0->interrupt_is_needed) {
1587 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1591 #ifdef FA_NODE_VERBOSE_DEBUG
1592 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1594 clib_bitmap_free(clear_sw_if_index_bitmap);
1598 #ifdef FA_NODE_VERBOSE_DEBUG
1599 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1602 vlib_node_increment_counter (vm,
1603 acl_fa_session_cleaner_process_node.
1605 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1606 am->fa_cleaner_cnt_unknown_event++;
1610 send_interrupts_to_workers(vm, am);
1613 _vec_len (event_data) = 0;
1616 * If the interrupts were not processed yet, ensure we wait a bit,
1617 * but up to a point.
1619 int need_more_wait = 0;
1620 int max_wait_cycles = 100;
1623 vec_foreach(pw0, am->per_worker_data) {
1624 if (pw0->interrupt_generation != am->fa_interrupt_generation) {
1628 if (need_more_wait) {
1629 vlib_process_suspend(vm, 0.0001);
1631 } while (need_more_wait && (--max_wait_cycles > 0));
1633 int interrupts_needed = 0;
1634 int interrupts_unwanted = 0;
1636 vec_foreach(pw0, am->per_worker_data) {
1637 if (pw0->interrupt_is_needed) {
1638 interrupts_needed++;
1639 /* the per-worker value is reset when sending the interrupt */
1641 if (pw0->interrupt_is_unwanted) {
1642 interrupts_unwanted++;
1643 pw0->interrupt_is_unwanted = 0;
1646 if (interrupts_needed) {
1647 /* they need more interrupts, do less waiting around next time */
1648 am->fa_current_cleaner_timer_wait_interval /= 2;
1649 /* never go into zero-wait either though - we need to give the space to others */
1650 am->fa_current_cleaner_timer_wait_interval += 1;
1651 } else if (interrupts_unwanted) {
1652 /* slowly increase the amount of sleep up to a limit */
1653 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1654 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1656 am->fa_cleaner_cnt_event_cycles++;
1657 am->fa_interrupt_generation++;
1665 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1667 acl_main_t *am = &acl_main;
1668 if (enable_disable) {
1669 acl_fa_verify_init_sessions(am);
1670 am->fa_total_enabled_count++;
1671 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1672 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1673 ACL_FA_CLEANER_RESCHEDULE, 0);
1674 clib_mem_set_heap (oldheap);
1676 am->fa_total_enabled_count--;
1681 ASSERT(clib_bitmap_get(am->fa_in_acl_on_sw_if_index, sw_if_index) != enable_disable);
1682 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1683 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1684 sw_if_index, enable_disable, 0, 0);
1685 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1686 sw_if_index, enable_disable, 0, 0);
1687 clib_mem_set_heap (oldheap);
1688 am->fa_in_acl_on_sw_if_index =
1689 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1694 ASSERT(clib_bitmap_get(am->fa_out_acl_on_sw_if_index, sw_if_index) != enable_disable);
1695 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1696 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1697 sw_if_index, enable_disable, 0, 0);
1698 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1699 sw_if_index, enable_disable, 0, 0);
1700 clib_mem_set_heap (oldheap);
1701 am->fa_out_acl_on_sw_if_index =
1702 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1705 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1706 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1708 #ifdef FA_NODE_VERBOSE_DEBUG
1709 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1711 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1712 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1713 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1715 clib_mem_set_heap (oldheap);
1720 show_fa_sessions_hash(vlib_main_t * vm, u32 verbose)
1722 acl_main_t *am = &acl_main;
1723 if (am->fa_sessions_hash_is_initialized) {
1724 vlib_cli_output(vm, "\nSession lookup hash table:\n%U\n\n",
1725 BV (format_bihash), &am->fa_sessions_hash, verbose);
1727 vlib_cli_output(vm, "\nSession lookup hash table is not allocated.\n\n");
1734 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1735 .function = acl_fa_worker_conn_cleaner_process,
1736 .name = "acl-plugin-fa-worker-cleaner-process",
1737 .type = VLIB_NODE_TYPE_INPUT,
1738 .state = VLIB_NODE_STATE_INTERRUPT,
1741 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1742 .function = acl_fa_session_cleaner_process,
1743 .type = VLIB_NODE_TYPE_PROCESS,
1744 .name = "acl-plugin-fa-cleaner-process",
1745 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1746 .error_strings = acl_fa_cleaner_error_strings,
1752 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1754 .function = acl_in_ip6_l2_node_fn,
1755 .name = "acl-plugin-in-ip6-l2",
1756 .vector_size = sizeof (u32),
1757 .format_trace = format_acl_fa_trace,
1758 .type = VLIB_NODE_TYPE_INTERNAL,
1759 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1760 .error_strings = acl_fa_error_strings,
1761 .n_next_nodes = ACL_FA_N_NEXT,
1764 [ACL_FA_ERROR_DROP] = "error-drop",
1768 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1770 .function = acl_in_ip4_l2_node_fn,
1771 .name = "acl-plugin-in-ip4-l2",
1772 .vector_size = sizeof (u32),
1773 .format_trace = format_acl_fa_trace,
1774 .type = VLIB_NODE_TYPE_INTERNAL,
1775 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1776 .error_strings = acl_fa_error_strings,
1777 .n_next_nodes = ACL_FA_N_NEXT,
1780 [ACL_FA_ERROR_DROP] = "error-drop",
1784 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1786 .function = acl_out_ip6_l2_node_fn,
1787 .name = "acl-plugin-out-ip6-l2",
1788 .vector_size = sizeof (u32),
1789 .format_trace = format_acl_fa_trace,
1790 .type = VLIB_NODE_TYPE_INTERNAL,
1791 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1792 .error_strings = acl_fa_error_strings,
1793 .n_next_nodes = ACL_FA_N_NEXT,
1796 [ACL_FA_ERROR_DROP] = "error-drop",
1800 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1802 .function = acl_out_ip4_l2_node_fn,
1803 .name = "acl-plugin-out-ip4-l2",
1804 .vector_size = sizeof (u32),
1805 .format_trace = format_acl_fa_trace,
1806 .type = VLIB_NODE_TYPE_INTERNAL,
1807 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1808 .error_strings = acl_fa_error_strings,
1809 .n_next_nodes = ACL_FA_N_NEXT,
1812 [ACL_FA_ERROR_DROP] = "error-drop",
1817 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1819 .function = acl_in_ip6_fa_node_fn,
1820 .name = "acl-plugin-in-ip6-fa",
1821 .vector_size = sizeof (u32),
1822 .format_trace = format_acl_fa_trace,
1823 .type = VLIB_NODE_TYPE_INTERNAL,
1824 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1825 .error_strings = acl_fa_error_strings,
1826 .n_next_nodes = ACL_FA_N_NEXT,
1829 [ACL_FA_ERROR_DROP] = "error-drop",
1833 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1835 .arc_name = "ip6-unicast",
1836 .node_name = "acl-plugin-in-ip6-fa",
1837 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1840 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1842 .function = acl_in_ip4_fa_node_fn,
1843 .name = "acl-plugin-in-ip4-fa",
1844 .vector_size = sizeof (u32),
1845 .format_trace = format_acl_fa_trace,
1846 .type = VLIB_NODE_TYPE_INTERNAL,
1847 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1848 .error_strings = acl_fa_error_strings,
1849 .n_next_nodes = ACL_FA_N_NEXT,
1852 [ACL_FA_ERROR_DROP] = "error-drop",
1856 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1858 .arc_name = "ip4-unicast",
1859 .node_name = "acl-plugin-in-ip4-fa",
1860 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1864 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1866 .function = acl_out_ip6_fa_node_fn,
1867 .name = "acl-plugin-out-ip6-fa",
1868 .vector_size = sizeof (u32),
1869 .format_trace = format_acl_fa_trace,
1870 .type = VLIB_NODE_TYPE_INTERNAL,
1871 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1872 .error_strings = acl_fa_error_strings,
1873 .n_next_nodes = ACL_FA_N_NEXT,
1876 [ACL_FA_ERROR_DROP] = "error-drop",
1880 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1882 .arc_name = "ip6-output",
1883 .node_name = "acl-plugin-out-ip6-fa",
1884 .runs_before = VNET_FEATURES ("interface-output"),
1887 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1889 .function = acl_out_ip4_fa_node_fn,
1890 .name = "acl-plugin-out-ip4-fa",
1891 .vector_size = sizeof (u32),
1892 .format_trace = format_acl_fa_trace,
1893 .type = VLIB_NODE_TYPE_INTERNAL,
1894 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1895 .error_strings = acl_fa_error_strings,
1896 .n_next_nodes = ACL_FA_N_NEXT,
1897 /* edit / add dispositions here */
1900 [ACL_FA_ERROR_DROP] = "error-drop",
1904 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1906 .arc_name = "ip4-output",
1907 .node_name = "acl-plugin-out-ip4-fa",
1908 .runs_before = VNET_FEATURES ("interface-output"),