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);
61 /* packet trace format function */
63 format_acl_fa_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
71 "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
72 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
73 t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
74 t->match_rule_index, t->trace_bitmap,
75 t->packet_info[0], t->packet_info[1], t->packet_info[2],
76 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
78 /* Now also print out the packet_info in a form usable by humans */
79 s = format (s, "\n %U", format_fa_5tuple, t->packet_info);
84 #define foreach_acl_fa_error \
85 _(ACL_DROP, "ACL deny packets") \
86 _(ACL_PERMIT, "ACL permit packets") \
87 _(ACL_NEW_SESSION, "new sessions added") \
88 _(ACL_EXIST_SESSION, "existing session packets") \
89 _(ACL_CHECK, "checked packets") \
90 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
91 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
96 #define _(sym,str) ACL_FA_ERROR_##sym,
102 static char *acl_fa_error_strings[] = {
103 #define _(sym,string) string,
110 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
112 u8 *p = vlib_buffer_get_current (b0) + offset;
118 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
119 int prefixlen, int is_ip6)
123 /* match any always succeeds */
128 if (memcmp (addr1, addr2, prefixlen / 8))
130 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
135 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
136 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
137 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
138 return (b1 & mask0) == b2;
142 /* The prefix fits into integer number of bytes, so nothing left to do */
148 uint32_t a1 = ntohl (addr1->ip4.as_u32);
149 uint32_t a2 = ntohl (addr2->ip4.as_u32);
150 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
151 return (a1 & mask0) == a2;
156 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
158 return ((port >= port_first) && (port <= port_last));
162 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
163 int is_ip6, u8 * r_action, u32 * r_acl_match_p,
164 u32 * r_rule_match_p, u32 * trace_bitmap)
170 if (pool_is_free_index (am->acls, acl_index))
173 *r_acl_match_p = acl_index;
175 *r_rule_match_p = -1;
176 /* the ACL does not exist but is used for policy. Block traffic. */
179 a = am->acls + acl_index;
180 for (i = 0; i < a->count; i++)
183 if (is_ip6 != r->is_ipv6)
187 if (!fa_acl_match_addr
188 (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
191 #ifdef FA_NODE_VERBOSE_DEBUG
193 ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
194 acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
195 r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
196 &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
200 if (!fa_acl_match_addr
201 (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
204 #ifdef FA_NODE_VERBOSE_DEBUG
206 ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
207 acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
208 r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
209 &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
212 ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
213 acl_index, i, pkt_5tuple->l4.proto, r->proto);
217 if (pkt_5tuple->l4.proto != r->proto)
220 if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
221 am->l4_match_nonfirst_fragment))
223 /* non-initial fragment with frag match configured - match this rule */
224 *trace_bitmap |= 0x80000000;
225 *r_action = r->is_permit;
227 *r_acl_match_p = acl_index;
233 /* A sanity check just to ensure we are about to match the ports extracted from the packet */
234 if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
237 #ifdef FA_NODE_VERBOSE_DEBUG
239 ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
240 acl_index, i, pkt_5tuple->l4.proto, r->proto);
243 if (!fa_acl_match_port
244 (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
245 r->src_port_or_type_last, is_ip6))
248 #ifdef FA_NODE_VERBOSE_DEBUG
250 ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
251 acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
252 r->src_port_or_type_last);
255 if (!fa_acl_match_port
256 (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
257 r->dst_port_or_code_last, is_ip6))
260 #ifdef FA_NODE_VERBOSE_DEBUG
262 ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
263 acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
264 r->dst_port_or_code_last);
266 if (pkt_5tuple->pkt.tcp_flags_valid
267 && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
271 /* everything matches! */
272 #ifdef FA_NODE_VERBOSE_DEBUG
273 clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
274 acl_index, i, r->is_permit);
276 *r_action = r->is_permit;
278 *r_acl_match_p = acl_index;
287 linear_multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
288 int is_ip6, int is_input, u32 * acl_match_p,
289 u32 * rule_match_p, u32 * trace_bitmap)
291 acl_main_t *am = &acl_main;
298 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
299 acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
303 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
304 acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
306 for (i = 0; i < vec_len (acl_vector); i++)
308 #ifdef FA_NODE_VERBOSE_DEBUG
309 clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
312 if (single_acl_match_5tuple
313 (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
314 acl_match_p, rule_match_p, trace_bitmap))
319 if (vec_len (acl_vector) > 0)
321 /* If there are ACLs and none matched, deny by default */
324 #ifdef FA_NODE_VERBOSE_DEBUG
325 clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
327 /* Deny by default. If there are no ACLs defined we should not be here. */
332 multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
333 int is_ip6, int is_input, u32 * acl_match_p,
334 u32 * rule_match_p, u32 * trace_bitmap)
336 acl_main_t *am = &acl_main;
337 if (am->use_hash_acl_matching) {
338 return hash_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
339 is_input, acl_match_p, rule_match_p, trace_bitmap);
341 return linear_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
342 is_input, acl_match_p, rule_match_p, trace_bitmap);
347 offset_within_packet (vlib_buffer_t * b0, int offset)
349 /* For the purposes of this code, "within" means we have at least 8 bytes after it */
350 return (offset <= (b0->current_length - 8));
354 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
355 int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
357 int l3_offset = ethernet_buffer_header_size(b0);
361 /* IP4 and IP6 protocol numbers of ICMP */
362 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
364 if (is_input && !(is_l2_path))
369 /* key[0..3] contains src/dst address and is cleared/set below */
370 /* Remainder of the key and per-packet non-key data */
371 p5tuple_pkt->kv.key[4] = 0;
372 p5tuple_pkt->kv.value = 0;
376 clib_memcpy (&p5tuple_pkt->addr,
377 get_ptr_to_offset (b0,
378 offsetof (ip6_header_t,
379 src_address) + l3_offset),
380 sizeof (p5tuple_pkt->addr));
382 *(u8 *) get_ptr_to_offset (b0,
383 offsetof (ip6_header_t,
384 protocol) + l3_offset);
385 l4_offset = l3_offset + sizeof (ip6_header_t);
386 #ifdef FA_NODE_VERBOSE_DEBUG
387 clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
390 /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
391 int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
392 if (PREDICT_FALSE (need_skip_eh))
394 while (need_skip_eh && offset_within_packet (b0, l4_offset))
396 /* Fragment header needs special handling */
397 if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
399 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
401 clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
402 frag_offset = ntohs(frag_offset) >> 3;
405 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
406 /* invalidate L4 offset so we don't try to find L4 info */
407 l4_offset += b0->current_length;
411 /* First fragment: skip the frag header and move on. */
417 u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
418 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
419 l4_offset += 8 * (1 + (u16) nwords);
421 #ifdef FA_NODE_VERBOSE_DEBUG
422 clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
426 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
432 p5tuple_pkt->kv.key[0] = 0;
433 p5tuple_pkt->kv.key[1] = 0;
434 p5tuple_pkt->kv.key[2] = 0;
435 p5tuple_pkt->kv.key[3] = 0;
436 clib_memcpy (&p5tuple_pkt->addr[0].ip4,
437 get_ptr_to_offset (b0,
438 offsetof (ip4_header_t,
439 src_address) + l3_offset),
440 sizeof (p5tuple_pkt->addr[0].ip4));
441 clib_memcpy (&p5tuple_pkt->addr[1].ip4,
442 get_ptr_to_offset (b0,
443 offsetof (ip4_header_t,
444 dst_address) + l3_offset),
445 sizeof (p5tuple_pkt->addr[1].ip4));
447 *(u8 *) get_ptr_to_offset (b0,
448 offsetof (ip4_header_t,
449 protocol) + l3_offset);
450 l4_offset = l3_offset + sizeof (ip4_header_t);
451 u16 flags_and_fragment_offset;
452 clib_memcpy (&flags_and_fragment_offset,
453 get_ptr_to_offset (b0,
454 offsetof (ip4_header_t,
455 flags_and_fragment_offset)) + l3_offset,
456 sizeof(flags_and_fragment_offset));
457 flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
459 /* non-initial fragments have non-zero offset */
460 if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
462 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
463 /* invalidate L4 offset so we don't try to find L4 info */
464 l4_offset += b0->current_length;
468 p5tuple_pkt->l4.proto = proto;
469 if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
471 p5tuple_pkt->pkt.l4_valid = 1;
472 if (icmp_protos[is_ip6] == proto)
475 p5tuple_pkt->l4.port[0] =
476 *(u8 *) get_ptr_to_offset (b0,
477 l4_offset + offsetof (icmp46_header_t,
480 p5tuple_pkt->l4.port[1] =
481 *(u8 *) get_ptr_to_offset (b0,
482 l4_offset + offsetof (icmp46_header_t,
485 else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
488 get_ptr_to_offset (b0,
489 l4_offset + offsetof (tcp_header_t,
492 p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
493 p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
495 p5tuple_pkt->pkt.tcp_flags =
496 *(u8 *) get_ptr_to_offset (b0,
497 l4_offset + offsetof (tcp_header_t,
499 p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
502 * FIXME: rather than the above conditional, here could
503 * be a nice generic mechanism to extract two L4 values:
505 * have a per-protocol array of 4 elements like this:
506 * u8 offset; to take the byte from, off L4 header
507 * u8 mask; to mask it with, before storing
509 * this way we can describe UDP, TCP and ICMP[46] semantics,
510 * and add a sort of FPM-type behavior for other protocols.
512 * Of course, is it faster ? and is it needed ?
519 /* Session keys match the packets received, and mirror the packets sent */
521 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
522 fa_5tuple_t * p5tuple_sess)
524 int src_index = is_input ? 0 : 1;
525 int dst_index = is_input ? 1 : 0;
526 p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
527 p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
528 p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
529 p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
530 p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
535 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
537 return am->fa_sessions_hash_is_initialized;
541 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
543 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
548 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
550 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
556 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
558 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
560 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
561 TCP_FLAGS_RSTFINACKSYN);
562 switch (sess->info.l4.proto)
565 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
567 return ACL_TIMEOUT_TCP_IDLE;
571 return ACL_TIMEOUT_TCP_TRANSIENT;
575 return ACL_TIMEOUT_UDP_IDLE;
578 return ACL_TIMEOUT_UDP_IDLE;
584 fa_session_get_shortest_timeout(acl_main_t * am)
588 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
589 if (timeout > am->session_timeout_sec[timeout_type]) {
590 timeout = am->session_timeout_sec[timeout_type];
597 * Get the timeout of the session in a list since its enqueue time.
601 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
603 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
605 * we have the shortest possible timeout type in all the lists
606 * (see README-multicore for the rationale)
608 timeout *= fa_session_get_shortest_timeout(am);
613 * Get the idle timeout of a session.
617 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
619 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
620 int timeout_type = fa_session_get_timeout_type (am, sess);
621 timeout *= am->session_timeout_sec[timeout_type];
626 acl_fa_verify_init_sessions (acl_main_t * am)
628 if (!am->fa_sessions_hash_is_initialized) {
630 /* Allocate the per-worker sessions pools */
631 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
632 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
633 pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
636 /* ... and the interface session hash table */
637 BV (clib_bihash_init) (&am->fa_sessions_hash,
638 "ACL plugin FA session bihash",
639 am->fa_conn_table_hash_num_buckets,
640 am->fa_conn_table_hash_memory_size);
641 am->fa_sessions_hash_is_initialized = 1;
645 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
647 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
648 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);
652 static inline int is_valid_session_ptr(acl_main_t *am, u16 thread_index, fa_session_t *sess)
654 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
655 return ((sess != 0) && ((sess - pw->fa_sessions_pool) < pool_len(pw->fa_sessions_pool)));
659 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
661 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
662 u8 list_id = fa_session_get_timeout_type(am, sess);
663 uword thread_index = os_get_thread_index ();
664 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
665 /* the retrieved session thread index must be necessarily the same as the one in the key */
666 ASSERT (sess->thread_index == sess_id.thread_index);
667 /* the retrieved session thread index must be the same as current thread */
668 ASSERT (sess->thread_index == thread_index);
669 sess->link_enqueue_time = now;
670 sess->link_list_id = list_id;
671 sess->link_next_idx = ~0;
672 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
673 if (~0 != pw->fa_conn_list_tail[list_id]) {
674 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
675 prev_sess->link_next_idx = sess_id.session_index;
676 /* We should never try to link with a session on another thread */
677 ASSERT(prev_sess->thread_index == sess->thread_index);
679 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
680 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
682 if (~0 == pw->fa_conn_list_head[list_id]) {
683 pw->fa_conn_list_head[list_id] = sess_id.session_index;
688 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
690 uword thread_index = os_get_thread_index ();
691 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
692 if (thread_index != sess_id.thread_index) {
693 /* If another thread attempts to delete the session, fail it. */
694 #ifdef FA_NODE_VERBOSE_DEBUG
695 clib_warning("thread id in key %d != curr thread index, not deleting");
699 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
700 /* we should never try to delete the session with another thread index */
701 ASSERT(sess->thread_index == thread_index);
702 if (~0 != sess->link_prev_idx) {
703 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
704 /* the previous session must be in the same list as this one */
705 ASSERT(prev_sess->link_list_id == sess->link_list_id);
706 prev_sess->link_next_idx = sess->link_next_idx;
708 if (~0 != sess->link_next_idx) {
709 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
710 /* The next session must be in the same list as the one we are deleting */
711 ASSERT(next_sess->link_list_id == sess->link_list_id);
712 next_sess->link_prev_idx = sess->link_prev_idx;
714 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
715 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
717 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
718 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
724 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
726 if (acl_fa_conn_list_delete_session(am, sess_id)) {
727 acl_fa_conn_list_add_session(am, sess_id, now);
731 * Our thread does not own this connection, so we can not delete
732 * The session. To avoid the complicated signaling, we simply
733 * pick the list waiting time to be the shortest of the timeouts.
734 * This way we do not have to do anything special, and let
735 * the regular requeue check take care of everything.
743 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
744 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
746 sess->last_active_time = now;
747 if (pkt_5tuple->pkt.tcp_flags_valid)
749 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
756 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
758 void *oldheap = clib_mem_set_heap(am->acl_mheap);
759 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
760 ASSERT(sess->thread_index == os_get_thread_index ());
761 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
763 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
764 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
765 /* Deleting from timer structures not needed,
766 as the caller must have dealt with the timers. */
767 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
768 clib_mem_set_heap (oldheap);
769 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
770 clib_smp_atomic_add(&am->fa_session_total_dels, 1);
774 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
777 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
778 return (curr_sess_count < am->fa_conn_table_max_entries);
782 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)
784 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
786 * We can not check just the index here because inbetween the worker thread might
787 * dequeue the connection from the head just as we are about to check it.
789 if (!is_valid_session_ptr(am, thread_index, sess)) {
790 return ~0LL; // infinity.
793 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
799 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)
801 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
803 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
804 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
808 * see if there are sessions ready to be checked,
809 * do the maintenance (requeue or delete), and
810 * return the total number of sessions reclaimed.
813 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
815 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
816 fa_full_session_id_t fsid;
817 fsid.thread_index = thread_index;
818 int total_expired = 0;
822 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
823 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
824 && (~0 != pw->fa_conn_list_head[tt])
825 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
826 pw->fa_conn_list_head[tt]))) {
827 fsid.session_index = pw->fa_conn_list_head[tt];
828 vec_add1(pw->expired, fsid.session_index);
829 acl_fa_conn_list_delete_session(am, fsid);
835 vec_foreach (psid, pw->expired)
837 fsid.session_index = *psid;
838 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
840 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
841 u32 sw_if_index = sess->sw_if_index;
842 u64 sess_timeout_time =
843 sess->last_active_time + fa_session_get_timeout (am, sess);
844 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
846 #ifdef FA_NODE_VERBOSE_DEBUG
847 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
848 (int) session_index);
850 /* There was activity on the session, so the idle timeout
851 has not passed. Enqueue for another time period. */
853 acl_fa_conn_list_add_session(am, fsid, now);
854 pw->cnt_session_timer_restarted++;
858 #ifdef FA_NODE_VERBOSE_DEBUG
859 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
860 (int) session_index);
862 acl_fa_delete_session (am, sw_if_index, fsid);
863 pw->cnt_deleted_sessions++;
868 pw->cnt_already_deleted_sessions++;
871 total_expired = vec_len(pw->expired);
872 /* zero out the vector which we have acted on */
874 _vec_len (pw->expired) = 0;
875 /* if we were advancing and reached the end
876 * (no more sessions to recycle), reset the fast-forward timestamp */
878 if (pw->swipe_end_time && 0 == total_expired)
879 pw->swipe_end_time = 0;
880 return (total_expired);
884 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
886 /* try to recycle a TCP transient session */
887 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
888 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
889 fa_full_session_id_t sess_id;
890 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
891 if (~0 != sess_id.session_index) {
892 sess_id.thread_index = thread_index;
893 acl_fa_conn_list_delete_session(am, sess_id);
894 acl_fa_delete_session(am, sw_if_index, sess_id);
898 static fa_session_t *
899 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
900 fa_5tuple_t * p5tuple)
902 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
903 clib_bihash_kv_40_8_t kv;
904 fa_full_session_id_t f_sess_id;
905 uword thread_index = os_get_thread_index();
906 void *oldheap = clib_mem_set_heap(am->acl_mheap);
907 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
909 f_sess_id.thread_index = thread_index;
912 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
913 f_sess_id.session_index = sess - pw->fa_sessions_pool;
915 kv.key[0] = pkv->key[0];
916 kv.key[1] = pkv->key[1];
917 kv.key[2] = pkv->key[2];
918 kv.key[3] = pkv->key[3];
919 kv.key[4] = pkv->key[4];
920 kv.value = f_sess_id.as_u64;
922 memcpy (sess, pkv, sizeof (pkv->key));
923 sess->last_active_time = now;
924 sess->sw_if_index = sw_if_index;
925 sess->tcp_flags_seen.as_u16 = 0;
926 sess->thread_index = thread_index;
927 sess->link_list_id = ~0;
928 sess->link_prev_idx = ~0;
929 sess->link_next_idx = ~0;
933 ASSERT(am->fa_sessions_hash_is_initialized == 1);
934 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
936 acl_fa_conn_list_add_session(am, f_sess_id, now);
938 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
939 clib_mem_set_heap (oldheap);
940 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
941 clib_smp_atomic_add(&am->fa_session_total_adds, 1);
946 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
947 clib_bihash_kv_40_8_t * pvalue_sess)
949 return (BV (clib_bihash_search)
950 (&am->fa_sessions_hash, &p5tuple->kv,
956 acl_fa_node_fn (vlib_main_t * vm,
957 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
958 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
959 vlib_node_registration_t * acl_fa_node)
961 u32 n_left_from, *from, *to_next;
962 acl_fa_next_t next_index;
963 u32 pkts_acl_checked = 0;
964 u32 pkts_new_session = 0;
965 u32 pkts_exist_session = 0;
966 u32 pkts_acl_permit = 0;
967 u32 pkts_restart_session_timer = 0;
968 u32 trace_bitmap = 0;
969 acl_main_t *am = &acl_main;
970 fa_5tuple_t fa_5tuple, kv_sess;
971 clib_bihash_kv_40_8_t value_sess;
972 vlib_node_runtime_t *error_node;
973 u64 now = clib_cpu_time_now ();
974 uword thread_index = os_get_thread_index ();
976 from = vlib_frame_vector_args (frame);
977 n_left_from = frame->n_vectors;
978 next_index = node->cached_next_index;
980 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
982 while (n_left_from > 0)
986 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
988 while (n_left_from > 0 && n_left_to_next > 0)
995 int acl_check_needed = 1;
996 u32 match_acl_in_index = ~0;
997 u32 match_rule_index = ~0;
1000 /* speculatively enqueue b0 to the current next frame */
1006 n_left_to_next -= 1;
1008 b0 = vlib_get_buffer (vm, bi0);
1011 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1013 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1016 * Extract the L3/L4 matching info into a 5-tuple structure,
1017 * then create a session key whose layout is independent on forward or reverse
1018 * direction of the packet.
1021 acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
1022 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
1023 acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
1024 fa_5tuple.pkt.sw_if_index = sw_if_index0;
1025 fa_5tuple.pkt.is_ip6 = is_ip6;
1026 fa_5tuple.pkt.is_input = is_input;
1027 fa_5tuple.pkt.mask_type_index_lsb = ~0;
1028 #ifdef FA_NODE_VERBOSE_DEBUG
1030 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1031 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
1032 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
1034 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1035 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
1036 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
1039 /* Try to match an existing session first */
1041 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
1043 if (acl_fa_find_session
1044 (am, sw_if_index0, &kv_sess, &value_sess))
1046 trace_bitmap |= 0x80000000;
1047 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
1048 fa_full_session_id_t f_sess_id;
1050 f_sess_id.as_u64 = value_sess.value;
1051 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1053 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1054 int old_timeout_type =
1055 fa_session_get_timeout_type (am, sess);
1057 acl_fa_track_session (am, is_input, sw_if_index0, now,
1059 /* expose the session id to the tracer */
1060 match_rule_index = f_sess_id.session_index;
1061 int new_timeout_type =
1062 fa_session_get_timeout_type (am, sess);
1063 acl_check_needed = 0;
1064 pkts_exist_session += 1;
1065 /* Tracking might have changed the session timeout type, e.g. from transient to established */
1066 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1068 acl_fa_restart_timer_for_session (am, now, f_sess_id);
1069 pkts_restart_session_timer++;
1071 0x00010000 + ((0xff & old_timeout_type) << 8) +
1072 (0xff & new_timeout_type);
1075 * I estimate the likelihood to be very low - the VPP needs
1076 * to have >64K interfaces to start with and then on
1077 * exactly 64K indices apart needs to be exactly the same
1078 * 5-tuple... Anyway, since this probability is nonzero -
1079 * print an error and drop the unlucky packet.
1080 * If this shows up in real world, we would need to bump
1081 * the hash key length.
1083 if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
1084 clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
1085 acl_check_needed = 0;
1091 if (acl_check_needed)
1094 multi_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1095 is_ip6, is_input, &match_acl_in_index,
1096 &match_rule_index, &trace_bitmap);
1099 pkts_acl_permit += 1;
1102 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1103 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1105 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1107 fa_session_t *sess = acl_fa_add_session (am, is_input, sw_if_index0, now,
1109 acl_fa_track_session (am, is_input, sw_if_index0, now,
1111 pkts_new_session += 1;
1116 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1126 next0 = vnet_l2_feature_next (b0, l2_feat_next_node_index, 0);
1128 vnet_feature_next (sw_if_index0, &next0, b0);
1131 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1132 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1134 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1135 t->sw_if_index = sw_if_index0;
1136 t->next_index = next0;
1137 t->match_acl_in_index = match_acl_in_index;
1138 t->match_rule_index = match_rule_index;
1139 t->packet_info[0] = fa_5tuple.kv.key[0];
1140 t->packet_info[1] = fa_5tuple.kv.key[1];
1141 t->packet_info[2] = fa_5tuple.kv.key[2];
1142 t->packet_info[3] = fa_5tuple.kv.key[3];
1143 t->packet_info[4] = fa_5tuple.kv.key[4];
1144 t->packet_info[5] = fa_5tuple.kv.value;
1146 t->trace_bitmap = trace_bitmap;
1149 next0 = next0 < node->n_next_nodes ? next0 : 0;
1151 b0->error = error_node->errors[error0];
1153 pkts_acl_checked += 1;
1155 /* verify speculative enqueue, maybe switch current next frame */
1156 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1157 to_next, n_left_to_next, bi0,
1161 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1164 vlib_node_increment_counter (vm, acl_fa_node->index,
1165 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1166 vlib_node_increment_counter (vm, acl_fa_node->index,
1167 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1168 vlib_node_increment_counter (vm, acl_fa_node->index,
1169 ACL_FA_ERROR_ACL_NEW_SESSION,
1171 vlib_node_increment_counter (vm, acl_fa_node->index,
1172 ACL_FA_ERROR_ACL_EXIST_SESSION,
1173 pkts_exist_session);
1174 vlib_node_increment_counter (vm, acl_fa_node->index,
1175 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1176 pkts_restart_session_timer);
1177 return frame->n_vectors;
1181 vlib_node_registration_t acl_in_l2_ip6_node;
1183 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1184 vlib_node_runtime_t * node, vlib_frame_t * frame)
1186 acl_main_t *am = &acl_main;
1187 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1188 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1189 &acl_in_l2_ip6_node);
1192 vlib_node_registration_t acl_in_l2_ip4_node;
1194 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1195 vlib_node_runtime_t * node, vlib_frame_t * frame)
1197 acl_main_t *am = &acl_main;
1198 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1199 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1200 &acl_in_l2_ip4_node);
1203 vlib_node_registration_t acl_out_l2_ip6_node;
1205 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1206 vlib_node_runtime_t * node, vlib_frame_t * frame)
1208 acl_main_t *am = &acl_main;
1209 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1210 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1211 &acl_out_l2_ip6_node);
1214 vlib_node_registration_t acl_out_l2_ip4_node;
1216 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1217 vlib_node_runtime_t * node, vlib_frame_t * frame)
1219 acl_main_t *am = &acl_main;
1220 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1221 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1222 &acl_out_l2_ip4_node);
1226 /**** L3 processing path nodes ****/
1229 vlib_node_registration_t acl_in_fa_ip6_node;
1231 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1232 vlib_node_runtime_t * node, vlib_frame_t * frame)
1234 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1237 vlib_node_registration_t acl_in_fa_ip4_node;
1239 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1240 vlib_node_runtime_t * node, vlib_frame_t * frame)
1242 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1245 vlib_node_registration_t acl_out_fa_ip6_node;
1247 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1248 vlib_node_runtime_t * node, vlib_frame_t * frame)
1250 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1253 vlib_node_registration_t acl_out_fa_ip4_node;
1255 acl_out_ip4_fa_node_fn (vlib_main_t * vm,
1256 vlib_node_runtime_t * node, vlib_frame_t * frame)
1258 return acl_fa_node_fn (vm, node, frame, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1262 * This process ensures the connection cleanup happens every so often
1263 * even in absence of traffic, as well as provides general orchestration
1264 * for requests like connection deletion on a given sw_if_index.
1269 #define foreach_acl_fa_cleaner_error \
1270 _(UNKNOWN_EVENT, "unknown event received") \
1275 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1276 foreach_acl_fa_cleaner_error
1278 ACL_FA_CLEANER_N_ERROR,
1279 } acl_fa_cleaner_error_t;
1281 static char *acl_fa_cleaner_error_strings[] = {
1282 #define _(sym,string) string,
1283 foreach_acl_fa_cleaner_error
1289 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1290 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1293 * Per-worker thread interrupt-driven cleaner thread
1294 * to clean idle connections if there are no packets
1297 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1298 vlib_node_runtime_t * rt, vlib_frame_t * f)
1300 acl_main_t *am = &acl_main;
1301 u64 now = clib_cpu_time_now ();
1302 u16 thread_index = os_get_thread_index ();
1303 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1305 #ifdef FA_NODE_VERBOSE_DEBUG
1306 clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1308 /* allow another interrupt to be queued */
1309 pw->interrupt_is_pending = 0;
1310 if (pw->clear_in_process) {
1311 if (0 == pw->swipe_end_time) {
1313 * Someone has just set the flag to start clearing.
1314 * we do this by combing through the connections up to a "time T"
1315 * which is now, and requeueing everything except the expired
1316 * connections and those matching the interface(s) being cleared.
1320 * first filter the sw_if_index bitmap that they want from us, by
1321 * a bitmap of sw_if_index for which we actually have connections.
1323 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1324 || (pw->serviced_sw_if_index_bitmap == 0)) {
1325 #ifdef FA_NODE_VERBOSE_DEBUG
1326 clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1328 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1330 #ifdef FA_NODE_VERBOSE_DEBUG
1331 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1332 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1333 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1335 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1336 pw->serviced_sw_if_index_bitmap);
1339 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1340 /* if the cross-section is a zero vector, no need to do anything. */
1341 #ifdef FA_NODE_VERBOSE_DEBUG
1342 clib_warning("WORKER: clearing done - nothing to do");
1344 pw->clear_in_process = 0;
1346 #ifdef FA_NODE_VERBOSE_DEBUG
1347 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1348 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1349 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1351 /* swipe through the connection lists until enqueue timestamps become above "now" */
1352 pw->swipe_end_time = now;
1356 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1357 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1358 if (pw->clear_in_process) {
1359 if (0 == num_expired) {
1360 /* we were clearing but we could not process any more connections. time to stop. */
1361 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1362 pw->clear_in_process = 0;
1363 #ifdef FA_NODE_VERBOSE_DEBUG
1364 clib_warning("WORKER: clearing done, all done");
1367 #ifdef FA_NODE_VERBOSE_DEBUG
1368 clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1370 /* should continue clearing.. So could they please sent an interrupt again? */
1371 pw->interrupt_is_needed = 1;
1374 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1375 /* there was too much work, we should get an interrupt ASAP */
1376 pw->interrupt_is_needed = 1;
1377 pw->interrupt_is_unwanted = 0;
1378 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1379 /* signal that they should trigger us less */
1380 pw->interrupt_is_needed = 0;
1381 pw->interrupt_is_unwanted = 1;
1383 /* the current rate of interrupts is ok */
1384 pw->interrupt_is_needed = 0;
1385 pw->interrupt_is_unwanted = 0;
1388 pw->interrupt_generation = am->fa_interrupt_generation;
1393 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1395 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1396 if (!pw->interrupt_is_pending) {
1397 pw->interrupt_is_pending = 1;
1398 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1399 acl_fa_worker_session_cleaner_process_node.index);
1400 /* if the interrupt was requested, mark that done. */
1401 /* pw->interrupt_is_needed = 0; */
1406 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1409 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1410 int n_threads = vec_len(vlib_mains);
1411 for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1412 send_one_worker_interrupt(vm, am, i);
1416 /* centralized process to drive per-worker cleaners */
1418 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1421 acl_main_t *am = &acl_main;
1423 f64 cpu_cps = vm->clib_time.clocks_per_second;
1425 /* We should check if there are connections to clean up - at least twice a second */
1426 u64 max_timer_wait_interval = cpu_cps / 2;
1427 uword event_type, *event_data = 0;
1428 acl_fa_per_worker_data_t *pw0;
1430 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1431 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1432 am->fa_interrupt_generation = 1;
1435 now = clib_cpu_time_now ();
1436 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1437 int has_pending_conns = 0;
1442 * walk over all per-thread list heads of different timeouts,
1443 * and see if there are any connections pending.
1444 * If there aren't - we do not need to wake up until the
1445 * worker code signals that it has added a connection.
1447 * Also, while we are at it, calculate the earliest we need to wake up.
1449 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1450 if (ti >= vec_len(am->per_worker_data)) {
1453 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1454 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1455 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1456 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1457 #ifdef FA_NODE_VERBOSE_DEBUG
1458 clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1460 next_expire = head_expiry;
1462 if (~0 != pw->fa_conn_list_head[tt]) {
1463 has_pending_conns = 1;
1468 /* If no pending connections and no ACL applied then no point in timing out */
1469 if (!has_pending_conns && (0 == am->fa_total_enabled_count))
1471 am->fa_cleaner_cnt_wait_without_timeout++;
1472 (void) vlib_process_wait_for_event (vm);
1473 event_type = vlib_process_get_events (vm, &event_data);
1477 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1480 /* skip waiting altogether */
1485 am->fa_cleaner_cnt_wait_with_timeout++;
1486 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1487 event_type = vlib_process_get_events (vm, &event_data);
1496 case ACL_FA_CLEANER_RESCHEDULE:
1497 /* Nothing to do. */
1499 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1501 uword *clear_sw_if_index_bitmap = 0;
1502 uword *sw_if_index0;
1504 #ifdef FA_NODE_VERBOSE_DEBUG
1505 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1507 vec_foreach (sw_if_index0, event_data)
1509 am->fa_cleaner_cnt_delete_by_sw_index++;
1510 #ifdef FA_NODE_VERBOSE_DEBUG
1512 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1515 if (*sw_if_index0 == ~0)
1521 if (!pool_is_free_index (am->vnet_main->interface_main.sw_interfaces, *sw_if_index0))
1523 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1527 #ifdef FA_NODE_VERBOSE_DEBUG
1528 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1530 vec_foreach(pw0, am->per_worker_data) {
1531 CLIB_MEMORY_BARRIER ();
1532 while (pw0->clear_in_process) {
1533 CLIB_MEMORY_BARRIER ();
1534 #ifdef FA_NODE_VERBOSE_DEBUG
1535 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1537 vlib_process_suspend(vm, 0.0001);
1538 if (pw0->interrupt_is_needed) {
1539 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1542 if (pw0->clear_in_process) {
1543 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1547 /* if we need to clear all, then just clear the interfaces that we are servicing */
1548 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(pw0->serviced_sw_if_index_bitmap);
1552 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1554 pw0->clear_in_process = 1;
1557 /* send some interrupts so they can start working */
1558 send_interrupts_to_workers(vm, am);
1560 /* now wait till they all complete */
1561 #ifdef FA_NODE_VERBOSE_DEBUG
1562 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1564 vec_foreach(pw0, am->per_worker_data) {
1565 CLIB_MEMORY_BARRIER ();
1566 while (pw0->clear_in_process) {
1567 CLIB_MEMORY_BARRIER ();
1568 #ifdef FA_NODE_VERBOSE_DEBUG
1569 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1571 vlib_process_suspend(vm, 0.0001);
1572 if (pw0->interrupt_is_needed) {
1573 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1577 #ifdef FA_NODE_VERBOSE_DEBUG
1578 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1580 clib_bitmap_free(clear_sw_if_index_bitmap);
1584 #ifdef FA_NODE_VERBOSE_DEBUG
1585 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1588 vlib_node_increment_counter (vm,
1589 acl_fa_session_cleaner_process_node.
1591 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1592 am->fa_cleaner_cnt_unknown_event++;
1596 send_interrupts_to_workers(vm, am);
1599 _vec_len (event_data) = 0;
1602 * If the interrupts were not processed yet, ensure we wait a bit,
1603 * but up to a point.
1605 int need_more_wait = 0;
1606 int max_wait_cycles = 100;
1609 vec_foreach(pw0, am->per_worker_data) {
1610 if (pw0->interrupt_generation != am->fa_interrupt_generation) {
1614 if (need_more_wait) {
1615 vlib_process_suspend(vm, 0.0001);
1617 } while (need_more_wait && (--max_wait_cycles > 0));
1619 int interrupts_needed = 0;
1620 int interrupts_unwanted = 0;
1622 vec_foreach(pw0, am->per_worker_data) {
1623 if (pw0->interrupt_is_needed) {
1624 interrupts_needed++;
1625 /* the per-worker value is reset when sending the interrupt */
1627 if (pw0->interrupt_is_unwanted) {
1628 interrupts_unwanted++;
1629 pw0->interrupt_is_unwanted = 0;
1632 if (interrupts_needed) {
1633 /* they need more interrupts, do less waiting around next time */
1634 am->fa_current_cleaner_timer_wait_interval /= 2;
1635 /* never go into zero-wait either though - we need to give the space to others */
1636 am->fa_current_cleaner_timer_wait_interval += 1;
1637 } else if (interrupts_unwanted) {
1638 /* slowly increase the amount of sleep up to a limit */
1639 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1640 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1642 am->fa_cleaner_cnt_event_cycles++;
1643 am->fa_interrupt_generation++;
1651 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1653 acl_main_t *am = &acl_main;
1654 if (enable_disable) {
1655 acl_fa_verify_init_sessions(am);
1656 am->fa_total_enabled_count++;
1657 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1658 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1659 ACL_FA_CLEANER_RESCHEDULE, 0);
1660 clib_mem_set_heap (oldheap);
1662 am->fa_total_enabled_count--;
1667 ASSERT(clib_bitmap_get(am->fa_in_acl_on_sw_if_index, sw_if_index) != enable_disable);
1668 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1669 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1670 sw_if_index, enable_disable, 0, 0);
1671 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1672 sw_if_index, enable_disable, 0, 0);
1673 clib_mem_set_heap (oldheap);
1674 am->fa_in_acl_on_sw_if_index =
1675 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1680 ASSERT(clib_bitmap_get(am->fa_out_acl_on_sw_if_index, sw_if_index) != enable_disable);
1681 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1682 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1683 sw_if_index, enable_disable, 0, 0);
1684 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1685 sw_if_index, enable_disable, 0, 0);
1686 clib_mem_set_heap (oldheap);
1687 am->fa_out_acl_on_sw_if_index =
1688 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1691 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1692 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1694 #ifdef FA_NODE_VERBOSE_DEBUG
1695 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1697 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1698 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1699 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1701 clib_mem_set_heap (oldheap);
1706 show_fa_sessions_hash(vlib_main_t * vm, u32 verbose)
1708 acl_main_t *am = &acl_main;
1709 if (am->fa_sessions_hash_is_initialized) {
1710 vlib_cli_output(vm, "\nSession lookup hash table:\n%U\n\n",
1711 BV (format_bihash), &am->fa_sessions_hash, verbose);
1713 vlib_cli_output(vm, "\nSession lookup hash table is not allocated.\n\n");
1720 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1721 .function = acl_fa_worker_conn_cleaner_process,
1722 .name = "acl-plugin-fa-worker-cleaner-process",
1723 .type = VLIB_NODE_TYPE_INPUT,
1724 .state = VLIB_NODE_STATE_INTERRUPT,
1727 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1728 .function = acl_fa_session_cleaner_process,
1729 .type = VLIB_NODE_TYPE_PROCESS,
1730 .name = "acl-plugin-fa-cleaner-process",
1731 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1732 .error_strings = acl_fa_cleaner_error_strings,
1738 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1740 .function = acl_in_ip6_l2_node_fn,
1741 .name = "acl-plugin-in-ip6-l2",
1742 .vector_size = sizeof (u32),
1743 .format_trace = format_acl_fa_trace,
1744 .type = VLIB_NODE_TYPE_INTERNAL,
1745 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1746 .error_strings = acl_fa_error_strings,
1747 .n_next_nodes = ACL_FA_N_NEXT,
1750 [ACL_FA_ERROR_DROP] = "error-drop",
1754 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1756 .function = acl_in_ip4_l2_node_fn,
1757 .name = "acl-plugin-in-ip4-l2",
1758 .vector_size = sizeof (u32),
1759 .format_trace = format_acl_fa_trace,
1760 .type = VLIB_NODE_TYPE_INTERNAL,
1761 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1762 .error_strings = acl_fa_error_strings,
1763 .n_next_nodes = ACL_FA_N_NEXT,
1766 [ACL_FA_ERROR_DROP] = "error-drop",
1770 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1772 .function = acl_out_ip6_l2_node_fn,
1773 .name = "acl-plugin-out-ip6-l2",
1774 .vector_size = sizeof (u32),
1775 .format_trace = format_acl_fa_trace,
1776 .type = VLIB_NODE_TYPE_INTERNAL,
1777 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1778 .error_strings = acl_fa_error_strings,
1779 .n_next_nodes = ACL_FA_N_NEXT,
1782 [ACL_FA_ERROR_DROP] = "error-drop",
1786 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1788 .function = acl_out_ip4_l2_node_fn,
1789 .name = "acl-plugin-out-ip4-l2",
1790 .vector_size = sizeof (u32),
1791 .format_trace = format_acl_fa_trace,
1792 .type = VLIB_NODE_TYPE_INTERNAL,
1793 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1794 .error_strings = acl_fa_error_strings,
1795 .n_next_nodes = ACL_FA_N_NEXT,
1798 [ACL_FA_ERROR_DROP] = "error-drop",
1803 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1805 .function = acl_in_ip6_fa_node_fn,
1806 .name = "acl-plugin-in-ip6-fa",
1807 .vector_size = sizeof (u32),
1808 .format_trace = format_acl_fa_trace,
1809 .type = VLIB_NODE_TYPE_INTERNAL,
1810 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1811 .error_strings = acl_fa_error_strings,
1812 .n_next_nodes = ACL_FA_N_NEXT,
1815 [ACL_FA_ERROR_DROP] = "error-drop",
1819 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1821 .arc_name = "ip6-unicast",
1822 .node_name = "acl-plugin-in-ip6-fa",
1823 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1826 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1828 .function = acl_in_ip4_fa_node_fn,
1829 .name = "acl-plugin-in-ip4-fa",
1830 .vector_size = sizeof (u32),
1831 .format_trace = format_acl_fa_trace,
1832 .type = VLIB_NODE_TYPE_INTERNAL,
1833 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1834 .error_strings = acl_fa_error_strings,
1835 .n_next_nodes = ACL_FA_N_NEXT,
1838 [ACL_FA_ERROR_DROP] = "error-drop",
1842 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1844 .arc_name = "ip4-unicast",
1845 .node_name = "acl-plugin-in-ip4-fa",
1846 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1850 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1852 .function = acl_out_ip6_fa_node_fn,
1853 .name = "acl-plugin-out-ip6-fa",
1854 .vector_size = sizeof (u32),
1855 .format_trace = format_acl_fa_trace,
1856 .type = VLIB_NODE_TYPE_INTERNAL,
1857 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1858 .error_strings = acl_fa_error_strings,
1859 .n_next_nodes = ACL_FA_N_NEXT,
1862 [ACL_FA_ERROR_DROP] = "error-drop",
1866 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1868 .arc_name = "ip6-output",
1869 .node_name = "acl-plugin-out-ip6-fa",
1870 .runs_before = VNET_FEATURES ("interface-output"),
1873 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1875 .function = acl_out_ip4_fa_node_fn,
1876 .name = "acl-plugin-out-ip4-fa",
1877 .vector_size = sizeof (u32),
1878 .format_trace = format_acl_fa_trace,
1879 .type = VLIB_NODE_TYPE_INTERNAL,
1880 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1881 .error_strings = acl_fa_error_strings,
1882 .n_next_nodes = ACL_FA_N_NEXT,
1883 /* edit / add dispositions here */
1886 [ACL_FA_ERROR_DROP] = "error-drop",
1890 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1892 .arc_name = "ip4-output",
1893 .node_name = "acl-plugin-out-ip4-fa",
1894 .runs_before = VNET_FEATURES ("interface-output"),