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;
42 /* packet trace format function */
44 format_acl_fa_trace (u8 * s, va_list * args)
46 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
47 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
48 acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
52 "acl-plugin: sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
53 " pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
54 t->sw_if_index, t->next_index, t->action, t->match_acl_in_index,
55 t->match_rule_index, t->trace_bitmap,
56 t->packet_info[0], t->packet_info[1], t->packet_info[2],
57 t->packet_info[3], t->packet_info[4], t->packet_info[5]);
62 #define foreach_acl_fa_error \
63 _(ACL_DROP, "ACL deny packets") \
64 _(ACL_PERMIT, "ACL permit packets") \
65 _(ACL_NEW_SESSION, "new sessions added") \
66 _(ACL_EXIST_SESSION, "existing session packets") \
67 _(ACL_CHECK, "checked packets") \
68 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
69 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
74 #define _(sym,str) ACL_FA_ERROR_##sym,
80 static char *acl_fa_error_strings[] = {
81 #define _(sym,string) string,
88 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
90 u8 *p = vlib_buffer_get_current (b0) + offset;
96 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
97 int prefixlen, int is_ip6)
101 /* match any always succeeds */
106 if (memcmp (addr1, addr2, prefixlen / 8))
108 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
113 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
114 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
115 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
116 return (b1 & mask0) == b2;
120 /* The prefix fits into integer number of bytes, so nothing left to do */
126 uint32_t a1 = ntohl (addr1->ip4.as_u32);
127 uint32_t a2 = ntohl (addr2->ip4.as_u32);
128 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
129 return (a1 & mask0) == a2;
134 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
136 return ((port >= port_first) && (port <= port_last));
140 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
141 int is_ip6, u8 * r_action, u32 * r_acl_match_p,
142 u32 * r_rule_match_p, u32 * trace_bitmap)
148 if (pool_is_free_index (am->acls, acl_index))
151 *r_acl_match_p = acl_index;
153 *r_rule_match_p = -1;
154 /* the ACL does not exist but is used for policy. Block traffic. */
157 a = am->acls + acl_index;
158 for (i = 0; i < a->count; i++)
161 if (is_ip6 != r->is_ipv6)
165 if (!fa_acl_match_addr
166 (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
169 #ifdef FA_NODE_VERBOSE_DEBUG
171 ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
172 acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
173 IP46_TYPE_ANY, format_ip46_address, &r->dst, IP46_TYPE_ANY,
177 if (!fa_acl_match_addr
178 (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
181 #ifdef FA_NODE_VERBOSE_DEBUG
183 ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
184 acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
185 IP46_TYPE_ANY, format_ip46_address, &r->src, IP46_TYPE_ANY,
188 ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
189 acl_index, i, pkt_5tuple->l4.proto, r->proto);
193 if (pkt_5tuple->l4.proto != r->proto)
196 if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
197 am->l4_match_nonfirst_fragment))
199 /* non-initial fragment with frag match configured - match this rule */
200 *trace_bitmap |= 0x80000000;
201 *r_action = r->is_permit;
203 *r_acl_match_p = acl_index;
209 /* A sanity check just to ensure we are about to match the ports extracted from the packet */
210 if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
213 #ifdef FA_NODE_VERBOSE_DEBUG
215 ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
216 acl_index, i, pkt_5tuple->l4.proto, r->proto);
219 if (!fa_acl_match_port
220 (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
221 r->src_port_or_type_last, is_ip6))
224 #ifdef FA_NODE_VERBOSE_DEBUG
226 ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
227 acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
228 r->src_port_or_type_last);
231 if (!fa_acl_match_port
232 (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
233 r->dst_port_or_code_last, is_ip6))
236 #ifdef FA_NODE_VERBOSE_DEBUG
238 ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
239 acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
240 r->dst_port_or_code_last);
242 if (pkt_5tuple->pkt.tcp_flags_valid
243 && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
247 /* everything matches! */
248 #ifdef FA_NODE_VERBOSE_DEBUG
249 clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
250 acl_index, i, r->is_permit);
252 *r_action = r->is_permit;
254 *r_acl_match_p = acl_index;
263 linear_multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
264 int is_ip6, int is_input, u32 * acl_match_p,
265 u32 * rule_match_p, u32 * trace_bitmap)
267 acl_main_t *am = &acl_main;
274 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
275 acl_vector = am->input_acl_vec_by_sw_if_index[sw_if_index];
279 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
280 acl_vector = am->output_acl_vec_by_sw_if_index[sw_if_index];
282 for (i = 0; i < vec_len (acl_vector); i++)
284 #ifdef FA_NODE_VERBOSE_DEBUG
285 clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
288 if (single_acl_match_5tuple
289 (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
290 acl_match_p, rule_match_p, trace_bitmap))
295 if (vec_len (acl_vector) > 0)
297 /* If there are ACLs and none matched, deny by default */
300 #ifdef FA_NODE_VERBOSE_DEBUG
301 clib_warning ("ACL_FA_NODE_DBG: No ACL on sw_if_index %d", sw_if_index);
303 /* Deny by default. If there are no ACLs defined we should not be here. */
308 multi_acl_match_5tuple (u32 sw_if_index, fa_5tuple_t * pkt_5tuple, int is_l2,
309 int is_ip6, int is_input, u32 * acl_match_p,
310 u32 * rule_match_p, u32 * trace_bitmap)
312 acl_main_t *am = &acl_main;
313 if (am->use_hash_acl_matching) {
314 return hash_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
315 is_input, acl_match_p, rule_match_p, trace_bitmap);
317 return linear_multi_acl_match_5tuple(sw_if_index, pkt_5tuple, is_l2, is_ip6,
318 is_input, acl_match_p, rule_match_p, trace_bitmap);
323 offset_within_packet (vlib_buffer_t * b0, int offset)
325 /* For the purposes of this code, "within" means we have at least 8 bytes after it */
326 return (offset <= (b0->current_length - 8));
330 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
331 int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
333 int l3_offset = ethernet_buffer_header_size(b0);
337 /* IP4 and IP6 protocol numbers of ICMP */
338 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
340 if (is_input && !(is_l2_path))
345 /* key[0..3] contains src/dst address and is cleared/set below */
346 /* Remainder of the key and per-packet non-key data */
347 p5tuple_pkt->kv.key[4] = 0;
348 p5tuple_pkt->kv.value = 0;
352 clib_memcpy (&p5tuple_pkt->addr,
353 get_ptr_to_offset (b0,
354 offsetof (ip6_header_t,
355 src_address) + l3_offset),
356 sizeof (p5tuple_pkt->addr));
358 *(u8 *) get_ptr_to_offset (b0,
359 offsetof (ip6_header_t,
360 protocol) + l3_offset);
361 l4_offset = l3_offset + sizeof (ip6_header_t);
362 #ifdef FA_NODE_VERBOSE_DEBUG
363 clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
366 /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
367 int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
368 if (PREDICT_FALSE (need_skip_eh))
370 while (need_skip_eh && offset_within_packet (b0, l4_offset))
372 /* Fragment header needs special handling */
373 if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
375 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
377 clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
378 frag_offset = ntohs(frag_offset) >> 3;
381 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
382 /* invalidate L4 offset so we don't try to find L4 info */
383 l4_offset += b0->current_length;
387 /* First fragment: skip the frag header and move on. */
393 u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
394 proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
395 l4_offset += 8 * (1 + (u16) nwords);
397 #ifdef FA_NODE_VERBOSE_DEBUG
398 clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
402 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
408 p5tuple_pkt->kv.key[0] = 0;
409 p5tuple_pkt->kv.key[1] = 0;
410 p5tuple_pkt->kv.key[2] = 0;
411 p5tuple_pkt->kv.key[3] = 0;
412 clib_memcpy (&p5tuple_pkt->addr[0].ip4,
413 get_ptr_to_offset (b0,
414 offsetof (ip4_header_t,
415 src_address) + l3_offset),
416 sizeof (p5tuple_pkt->addr[0].ip4));
417 clib_memcpy (&p5tuple_pkt->addr[1].ip4,
418 get_ptr_to_offset (b0,
419 offsetof (ip4_header_t,
420 dst_address) + l3_offset),
421 sizeof (p5tuple_pkt->addr[1].ip4));
423 *(u8 *) get_ptr_to_offset (b0,
424 offsetof (ip4_header_t,
425 protocol) + l3_offset);
426 l4_offset = l3_offset + sizeof (ip4_header_t);
427 u16 flags_and_fragment_offset;
428 clib_memcpy (&flags_and_fragment_offset,
429 get_ptr_to_offset (b0,
430 offsetof (ip4_header_t,
431 flags_and_fragment_offset)) + l3_offset,
432 sizeof(flags_and_fragment_offset));
433 flags_and_fragment_offset = ntohs (flags_and_fragment_offset);
435 /* non-initial fragments have non-zero offset */
436 if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
438 p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
439 /* invalidate L4 offset so we don't try to find L4 info */
440 l4_offset += b0->current_length;
444 p5tuple_pkt->l4.proto = proto;
445 if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
447 p5tuple_pkt->pkt.l4_valid = 1;
448 if (icmp_protos[is_ip6] == proto)
451 p5tuple_pkt->l4.port[0] =
452 *(u8 *) get_ptr_to_offset (b0,
453 l4_offset + offsetof (icmp46_header_t,
456 p5tuple_pkt->l4.port[1] =
457 *(u8 *) get_ptr_to_offset (b0,
458 l4_offset + offsetof (icmp46_header_t,
461 else if ((IPPROTO_TCP == proto) || (IPPROTO_UDP == proto))
464 get_ptr_to_offset (b0,
465 l4_offset + offsetof (tcp_header_t,
468 p5tuple_pkt->l4.port[0] = ntohs (ports[0]);
469 p5tuple_pkt->l4.port[1] = ntohs (ports[1]);
471 p5tuple_pkt->pkt.tcp_flags =
472 *(u8 *) get_ptr_to_offset (b0,
473 l4_offset + offsetof (tcp_header_t,
475 p5tuple_pkt->pkt.tcp_flags_valid = (proto == IPPROTO_TCP);
478 * FIXME: rather than the above conditional, here could
479 * be a nice generic mechanism to extract two L4 values:
481 * have a per-protocol array of 4 elements like this:
482 * u8 offset; to take the byte from, off L4 header
483 * u8 mask; to mask it with, before storing
485 * this way we can describe UDP, TCP and ICMP[46] semantics,
486 * and add a sort of FPM-type behavior for other protocols.
488 * Of course, is it faster ? and is it needed ?
495 /* Session keys match the packets received, and mirror the packets sent */
497 acl_make_5tuple_session_key (int is_input, fa_5tuple_t * p5tuple_pkt,
498 fa_5tuple_t * p5tuple_sess)
500 int src_index = is_input ? 0 : 1;
501 int dst_index = is_input ? 1 : 0;
502 p5tuple_sess->addr[src_index] = p5tuple_pkt->addr[0];
503 p5tuple_sess->addr[dst_index] = p5tuple_pkt->addr[1];
504 p5tuple_sess->l4.as_u64 = p5tuple_pkt->l4.as_u64;
505 p5tuple_sess->l4.port[src_index] = p5tuple_pkt->l4.port[0];
506 p5tuple_sess->l4.port[dst_index] = p5tuple_pkt->l4.port[1];
511 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
513 return am->fa_sessions_hash_is_initialized;
517 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
519 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
524 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
526 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
532 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
534 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
536 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
537 TCP_FLAGS_RSTFINACKSYN);
538 switch (sess->info.l4.proto)
541 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
543 return ACL_TIMEOUT_TCP_IDLE;
547 return ACL_TIMEOUT_TCP_TRANSIENT;
551 return ACL_TIMEOUT_UDP_IDLE;
554 return ACL_TIMEOUT_UDP_IDLE;
560 fa_session_get_shortest_timeout(acl_main_t * am)
564 for(timeout_type = 0; timeout_type < ACL_N_TIMEOUTS; timeout_type++) {
565 if (timeout > am->session_timeout_sec[timeout_type]) {
566 timeout = am->session_timeout_sec[timeout_type];
573 * Get the timeout of the session in a list since its enqueue time.
577 fa_session_get_list_timeout (acl_main_t * am, fa_session_t * sess)
579 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
581 * we have the shortest possible timeout type in all the lists
582 * (see README-multicore for the rationale)
584 timeout *= fa_session_get_shortest_timeout(am);
589 * Get the idle timeout of a session.
593 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
595 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
596 int timeout_type = fa_session_get_timeout_type (am, sess);
597 timeout *= am->session_timeout_sec[timeout_type];
602 acl_fa_verify_init_sessions (acl_main_t * am)
604 if (!am->fa_sessions_hash_is_initialized) {
606 /* Allocate the per-worker sessions pools */
607 for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
608 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
609 pool_alloc_aligned(pw->fa_sessions_pool, am->fa_conn_table_max_entries, CLIB_CACHE_LINE_BYTES);
612 /* ... and the interface session hash table */
613 BV (clib_bihash_init) (&am->fa_sessions_hash,
614 "ACL plugin FA session bihash",
615 am->fa_conn_table_hash_num_buckets,
616 am->fa_conn_table_hash_memory_size);
617 am->fa_sessions_hash_is_initialized = 1;
621 static inline fa_session_t *get_session_ptr(acl_main_t *am, u16 thread_index, u32 session_index)
623 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
624 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);
628 static inline int is_valid_session_ptr(acl_main_t *am, u16 thread_index, fa_session_t *sess)
630 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
631 return ((sess != 0) && ((sess - pw->fa_sessions_pool) < pool_len(pw->fa_sessions_pool)));
635 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now)
637 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
638 u8 list_id = fa_session_get_timeout_type(am, sess);
639 uword thread_index = os_get_thread_index ();
640 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
641 /* the retrieved session thread index must be necessarily the same as the one in the key */
642 ASSERT (sess->thread_index == sess_id.thread_index);
643 /* the retrieved session thread index must be the same as current thread */
644 ASSERT (sess->thread_index == thread_index);
645 sess->link_enqueue_time = now;
646 sess->link_list_id = list_id;
647 sess->link_next_idx = ~0;
648 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
649 if (~0 != pw->fa_conn_list_tail[list_id]) {
650 fa_session_t *prev_sess = get_session_ptr(am, thread_index, pw->fa_conn_list_tail[list_id]);
651 prev_sess->link_next_idx = sess_id.session_index;
652 /* We should never try to link with a session on another thread */
653 ASSERT(prev_sess->thread_index == sess->thread_index);
655 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
656 pw->serviced_sw_if_index_bitmap = clib_bitmap_set(pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
658 if (~0 == pw->fa_conn_list_head[list_id]) {
659 pw->fa_conn_list_head[list_id] = sess_id.session_index;
664 acl_fa_conn_list_delete_session (acl_main_t *am, fa_full_session_id_t sess_id)
666 uword thread_index = os_get_thread_index ();
667 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
668 if (thread_index != sess_id.thread_index) {
669 /* If another thread attempts to delete the session, fail it. */
670 #ifdef FA_NODE_VERBOSE_DEBUG
671 clib_warning("thread id in key %d != curr thread index, not deleting");
675 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
676 /* we should never try to delete the session with another thread index */
677 ASSERT(sess->thread_index == thread_index);
678 if (~0 != sess->link_prev_idx) {
679 fa_session_t *prev_sess = get_session_ptr(am, thread_index, sess->link_prev_idx);
680 /* the previous session must be in the same list as this one */
681 ASSERT(prev_sess->link_list_id == sess->link_list_id);
682 prev_sess->link_next_idx = sess->link_next_idx;
684 if (~0 != sess->link_next_idx) {
685 fa_session_t *next_sess = get_session_ptr(am, thread_index, sess->link_next_idx);
686 /* The next session must be in the same list as the one we are deleting */
687 ASSERT(next_sess->link_list_id == sess->link_list_id);
688 next_sess->link_prev_idx = sess->link_prev_idx;
690 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) {
691 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
693 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) {
694 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
700 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id)
702 if (acl_fa_conn_list_delete_session(am, sess_id)) {
703 acl_fa_conn_list_add_session(am, sess_id, now);
707 * Our thread does not own this connection, so we can not delete
708 * The session. To avoid the complicated signaling, we simply
709 * pick the list waiting time to be the shortest of the timeouts.
710 * This way we do not have to do anything special, and let
711 * the regular requeue check take care of everything.
719 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
720 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
722 sess->last_active_time = now;
723 if (pkt_5tuple->pkt.tcp_flags_valid)
725 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
732 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id)
734 void *oldheap = clib_mem_set_heap(am->acl_mheap);
735 fa_session_t *sess = get_session_ptr(am, sess_id.thread_index, sess_id.session_index);
736 ASSERT(sess->thread_index == os_get_thread_index ());
737 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
739 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
740 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
741 /* Deleting from timer structures not needed,
742 as the caller must have dealt with the timers. */
743 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
744 clib_mem_set_heap (oldheap);
745 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
746 clib_smp_atomic_add(&am->fa_session_total_dels, 1);
750 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
753 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
754 return (curr_sess_count < am->fa_conn_table_max_entries);
758 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)
760 fa_session_t *sess = get_session_ptr(am, thread_index, pw->fa_conn_list_head[timeout_type]);
762 * We can not check just the index here because inbetween the worker thread might
763 * dequeue the connection from the head just as we are about to check it.
765 if (!is_valid_session_ptr(am, thread_index, sess)) {
766 return ~0LL; // infinity.
769 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
775 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)
777 fa_session_t *sess = get_session_ptr(am, thread_index, session_index);
779 sess->link_enqueue_time + fa_session_get_list_timeout (am, sess);
780 return (timeout_time < now) || (sess->link_enqueue_time <= pw->swipe_end_time);
784 * see if there are sessions ready to be checked,
785 * do the maintenance (requeue or delete), and
786 * return the total number of sessions reclaimed.
789 acl_fa_check_idle_sessions(acl_main_t *am, u16 thread_index, u64 now)
791 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
792 fa_full_session_id_t fsid;
793 fsid.thread_index = thread_index;
794 int total_expired = 0;
798 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
799 while((vec_len(pw->expired) < am->fa_max_deleted_sessions_per_interval)
800 && (~0 != pw->fa_conn_list_head[tt])
801 && (acl_fa_conn_time_to_check(am, pw, now, thread_index,
802 pw->fa_conn_list_head[tt]))) {
803 fsid.session_index = pw->fa_conn_list_head[tt];
804 vec_add1(pw->expired, fsid.session_index);
805 acl_fa_conn_list_delete_session(am, fsid);
811 vec_foreach (psid, pw->expired)
813 fsid.session_index = *psid;
814 if (!pool_is_free_index (pw->fa_sessions_pool, fsid.session_index))
816 fa_session_t *sess = get_session_ptr(am, thread_index, fsid.session_index);
817 u32 sw_if_index = sess->sw_if_index;
818 u64 sess_timeout_time =
819 sess->last_active_time + fa_session_get_timeout (am, sess);
820 if ((now < sess_timeout_time) && (0 == clib_bitmap_get(pw->pending_clear_sw_if_index_bitmap, sw_if_index)))
822 #ifdef FA_NODE_VERBOSE_DEBUG
823 clib_warning ("ACL_FA_NODE_CLEAN: Restarting timer for session %d",
824 (int) session_index);
826 /* There was activity on the session, so the idle timeout
827 has not passed. Enqueue for another time period. */
829 acl_fa_conn_list_add_session(am, fsid, now);
830 pw->cnt_session_timer_restarted++;
834 #ifdef FA_NODE_VERBOSE_DEBUG
835 clib_warning ("ACL_FA_NODE_CLEAN: Deleting session %d",
836 (int) session_index);
838 acl_fa_delete_session (am, sw_if_index, fsid);
839 pw->cnt_deleted_sessions++;
844 pw->cnt_already_deleted_sessions++;
847 total_expired = vec_len(pw->expired);
848 /* zero out the vector which we have acted on */
850 _vec_len (pw->expired) = 0;
851 /* if we were advancing and reached the end
852 * (no more sessions to recycle), reset the fast-forward timestamp */
854 if (pw->swipe_end_time && 0 == total_expired)
855 pw->swipe_end_time = 0;
856 return (total_expired);
860 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index)
862 /* try to recycle a TCP transient session */
863 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
864 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
865 fa_full_session_id_t sess_id;
866 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
867 if (~0 != sess_id.session_index) {
868 sess_id.thread_index = thread_index;
869 acl_fa_conn_list_delete_session(am, sess_id);
870 acl_fa_delete_session(am, sw_if_index, sess_id);
874 static fa_session_t *
875 acl_fa_add_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
876 fa_5tuple_t * p5tuple)
878 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
879 clib_bihash_kv_40_8_t kv;
880 fa_full_session_id_t f_sess_id;
881 uword thread_index = os_get_thread_index();
882 void *oldheap = clib_mem_set_heap(am->acl_mheap);
883 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
885 f_sess_id.thread_index = thread_index;
888 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
889 f_sess_id.session_index = sess - pw->fa_sessions_pool;
891 kv.key[0] = pkv->key[0];
892 kv.key[1] = pkv->key[1];
893 kv.key[2] = pkv->key[2];
894 kv.key[3] = pkv->key[3];
895 kv.key[4] = pkv->key[4];
896 kv.value = f_sess_id.as_u64;
898 memcpy (sess, pkv, sizeof (pkv->key));
899 sess->last_active_time = now;
900 sess->sw_if_index = sw_if_index;
901 sess->tcp_flags_seen.as_u16 = 0;
902 sess->thread_index = thread_index;
903 sess->link_list_id = ~0;
904 sess->link_prev_idx = ~0;
905 sess->link_next_idx = ~0;
909 ASSERT(am->fa_sessions_hash_is_initialized == 1);
910 BV (clib_bihash_add_del) (&am->fa_sessions_hash,
912 acl_fa_conn_list_add_session(am, f_sess_id, now);
914 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
915 clib_mem_set_heap (oldheap);
916 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
917 clib_smp_atomic_add(&am->fa_session_total_adds, 1);
922 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
923 clib_bihash_kv_40_8_t * pvalue_sess)
925 return (BV (clib_bihash_search)
926 (&am->fa_sessions_hash, &p5tuple->kv,
932 acl_fa_node_fn (vlib_main_t * vm,
933 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
934 int is_input, int is_l2_path, u32 * l2_feat_next_node_index,
935 vlib_node_registration_t * acl_fa_node)
937 u32 n_left_from, *from, *to_next;
938 acl_fa_next_t next_index;
939 u32 pkts_acl_checked = 0;
940 u32 pkts_new_session = 0;
941 u32 pkts_exist_session = 0;
942 u32 pkts_acl_permit = 0;
943 u32 pkts_restart_session_timer = 0;
944 u32 trace_bitmap = 0;
945 acl_main_t *am = &acl_main;
946 fa_5tuple_t fa_5tuple, kv_sess;
947 clib_bihash_kv_40_8_t value_sess;
948 vlib_node_runtime_t *error_node;
949 u64 now = clib_cpu_time_now ();
950 uword thread_index = os_get_thread_index ();
952 from = vlib_frame_vector_args (frame);
953 n_left_from = frame->n_vectors;
954 next_index = node->cached_next_index;
956 error_node = vlib_node_get_runtime (vm, acl_fa_node->index);
958 while (n_left_from > 0)
962 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
964 while (n_left_from > 0 && n_left_to_next > 0)
971 int acl_check_needed = 1;
972 u32 match_acl_in_index = ~0;
973 u32 match_rule_index = ~0;
976 /* speculatively enqueue b0 to the current next frame */
984 b0 = vlib_get_buffer (vm, bi0);
987 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
989 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
992 * Extract the L3/L4 matching info into a 5-tuple structure,
993 * then create a session key whose layout is independent on forward or reverse
994 * direction of the packet.
997 acl_fill_5tuple (am, b0, is_ip6, is_input, is_l2_path, &fa_5tuple);
998 fa_5tuple.l4.lsb_of_sw_if_index = sw_if_index0 & 0xffff;
999 acl_make_5tuple_session_key (is_input, &fa_5tuple, &kv_sess);
1000 fa_5tuple.pkt.sw_if_index = sw_if_index0;
1001 fa_5tuple.pkt.is_ip6 = is_ip6;
1002 fa_5tuple.pkt.is_input = is_input;
1003 fa_5tuple.pkt.mask_type_index_lsb = ~0;
1004 #ifdef FA_NODE_VERBOSE_DEBUG
1006 ("ACL_FA_NODE_DBG: session 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1007 kv_sess.kv.key[0], kv_sess.kv.key[1], kv_sess.kv.key[2],
1008 kv_sess.kv.key[3], kv_sess.kv.key[4], kv_sess.kv.value);
1010 ("ACL_FA_NODE_DBG: packet 5-tuple %016llx %016llx %016llx %016llx %016llx : %016llx",
1011 fa_5tuple.kv.key[0], fa_5tuple.kv.key[1], fa_5tuple.kv.key[2],
1012 fa_5tuple.kv.key[3], fa_5tuple.kv.key[4], fa_5tuple.kv.value);
1015 /* Try to match an existing session first */
1017 if (acl_fa_ifc_has_sessions (am, sw_if_index0))
1019 if (acl_fa_find_session
1020 (am, sw_if_index0, &kv_sess, &value_sess))
1022 trace_bitmap |= 0x80000000;
1023 error0 = ACL_FA_ERROR_ACL_EXIST_SESSION;
1024 fa_full_session_id_t f_sess_id;
1026 f_sess_id.as_u64 = value_sess.value;
1027 ASSERT(f_sess_id.thread_index < vec_len(vlib_mains));
1029 fa_session_t *sess = get_session_ptr(am, f_sess_id.thread_index, f_sess_id.session_index);
1030 int old_timeout_type =
1031 fa_session_get_timeout_type (am, sess);
1033 acl_fa_track_session (am, is_input, sw_if_index0, now,
1035 /* expose the session id to the tracer */
1036 match_rule_index = f_sess_id.session_index;
1037 int new_timeout_type =
1038 fa_session_get_timeout_type (am, sess);
1039 acl_check_needed = 0;
1040 pkts_exist_session += 1;
1041 /* Tracking might have changed the session timeout type, e.g. from transient to established */
1042 if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
1044 acl_fa_restart_timer_for_session (am, now, f_sess_id);
1045 pkts_restart_session_timer++;
1047 0x00010000 + ((0xff & old_timeout_type) << 8) +
1048 (0xff & new_timeout_type);
1051 * I estimate the likelihood to be very low - the VPP needs
1052 * to have >64K interfaces to start with and then on
1053 * exactly 64K indices apart needs to be exactly the same
1054 * 5-tuple... Anyway, since this probability is nonzero -
1055 * print an error and drop the unlucky packet.
1056 * If this shows up in real world, we would need to bump
1057 * the hash key length.
1059 if (PREDICT_FALSE(sess->sw_if_index != sw_if_index0)) {
1060 clib_warning("BUG: session LSB16(sw_if_index) and 5-tuple collision!");
1061 acl_check_needed = 0;
1067 if (acl_check_needed)
1070 multi_acl_match_5tuple (sw_if_index0, &fa_5tuple, is_l2_path,
1071 is_ip6, is_input, &match_acl_in_index,
1072 &match_rule_index, &trace_bitmap);
1075 pkts_acl_permit += 1;
1078 if (!acl_fa_can_add_session (am, is_input, sw_if_index0))
1079 acl_fa_try_recycle_session (am, is_input, thread_index, sw_if_index0);
1081 if (acl_fa_can_add_session (am, is_input, sw_if_index0))
1083 fa_session_t *sess = acl_fa_add_session (am, is_input, sw_if_index0, now,
1085 acl_fa_track_session (am, is_input, sw_if_index0, now,
1087 pkts_new_session += 1;
1092 error0 = ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS;
1102 next0 = vnet_l2_feature_next (b0, l2_feat_next_node_index, 0);
1104 vnet_feature_next (sw_if_index0, &next0, b0);
1107 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1108 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1110 acl_fa_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1111 t->sw_if_index = sw_if_index0;
1112 t->next_index = next0;
1113 t->match_acl_in_index = match_acl_in_index;
1114 t->match_rule_index = match_rule_index;
1115 t->packet_info[0] = fa_5tuple.kv.key[0];
1116 t->packet_info[1] = fa_5tuple.kv.key[1];
1117 t->packet_info[2] = fa_5tuple.kv.key[2];
1118 t->packet_info[3] = fa_5tuple.kv.key[3];
1119 t->packet_info[4] = fa_5tuple.kv.key[4];
1120 t->packet_info[5] = fa_5tuple.kv.value;
1122 t->trace_bitmap = trace_bitmap;
1125 next0 = next0 < node->n_next_nodes ? next0 : 0;
1127 b0->error = error_node->errors[error0];
1129 pkts_acl_checked += 1;
1131 /* verify speculative enqueue, maybe switch current next frame */
1132 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1133 to_next, n_left_to_next, bi0,
1137 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1140 vlib_node_increment_counter (vm, acl_fa_node->index,
1141 ACL_FA_ERROR_ACL_CHECK, pkts_acl_checked);
1142 vlib_node_increment_counter (vm, acl_fa_node->index,
1143 ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
1144 vlib_node_increment_counter (vm, acl_fa_node->index,
1145 ACL_FA_ERROR_ACL_NEW_SESSION,
1147 vlib_node_increment_counter (vm, acl_fa_node->index,
1148 ACL_FA_ERROR_ACL_EXIST_SESSION,
1149 pkts_exist_session);
1150 vlib_node_increment_counter (vm, acl_fa_node->index,
1151 ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER,
1152 pkts_restart_session_timer);
1153 return frame->n_vectors;
1157 vlib_node_registration_t acl_in_l2_ip6_node;
1159 acl_in_ip6_l2_node_fn (vlib_main_t * vm,
1160 vlib_node_runtime_t * node, vlib_frame_t * frame)
1162 acl_main_t *am = &acl_main;
1163 return acl_fa_node_fn (vm, node, frame, 1, 1, 1,
1164 am->fa_acl_in_ip6_l2_node_feat_next_node_index,
1165 &acl_in_l2_ip6_node);
1168 vlib_node_registration_t acl_in_l2_ip4_node;
1170 acl_in_ip4_l2_node_fn (vlib_main_t * vm,
1171 vlib_node_runtime_t * node, vlib_frame_t * frame)
1173 acl_main_t *am = &acl_main;
1174 return acl_fa_node_fn (vm, node, frame, 0, 1, 1,
1175 am->fa_acl_in_ip4_l2_node_feat_next_node_index,
1176 &acl_in_l2_ip4_node);
1179 vlib_node_registration_t acl_out_l2_ip6_node;
1181 acl_out_ip6_l2_node_fn (vlib_main_t * vm,
1182 vlib_node_runtime_t * node, vlib_frame_t * frame)
1184 acl_main_t *am = &acl_main;
1185 return acl_fa_node_fn (vm, node, frame, 1, 0, 1,
1186 am->fa_acl_out_ip6_l2_node_feat_next_node_index,
1187 &acl_out_l2_ip6_node);
1190 vlib_node_registration_t acl_out_l2_ip4_node;
1192 acl_out_ip4_l2_node_fn (vlib_main_t * vm,
1193 vlib_node_runtime_t * node, vlib_frame_t * frame)
1195 acl_main_t *am = &acl_main;
1196 return acl_fa_node_fn (vm, node, frame, 0, 0, 1,
1197 am->fa_acl_out_ip4_l2_node_feat_next_node_index,
1198 &acl_out_l2_ip4_node);
1202 /**** L3 processing path nodes ****/
1205 vlib_node_registration_t acl_in_fa_ip6_node;
1207 acl_in_ip6_fa_node_fn (vlib_main_t * vm,
1208 vlib_node_runtime_t * node, vlib_frame_t * frame)
1210 return acl_fa_node_fn (vm, node, frame, 1, 1, 0, 0, &acl_in_fa_ip6_node);
1213 vlib_node_registration_t acl_in_fa_ip4_node;
1215 acl_in_ip4_fa_node_fn (vlib_main_t * vm,
1216 vlib_node_runtime_t * node, vlib_frame_t * frame)
1218 return acl_fa_node_fn (vm, node, frame, 0, 1, 0, 0, &acl_in_fa_ip4_node);
1221 vlib_node_registration_t acl_out_fa_ip6_node;
1223 acl_out_ip6_fa_node_fn (vlib_main_t * vm,
1224 vlib_node_runtime_t * node, vlib_frame_t * frame)
1226 return acl_fa_node_fn (vm, node, frame, 1, 0, 0, 0, &acl_out_fa_ip6_node);
1229 vlib_node_registration_t acl_out_fa_ip4_node;
1231 acl_out_ip4_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, 0, 0, 0, 0, &acl_out_fa_ip4_node);
1238 * This process ensures the connection cleanup happens every so often
1239 * even in absence of traffic, as well as provides general orchestration
1240 * for requests like connection deletion on a given sw_if_index.
1245 #define foreach_acl_fa_cleaner_error \
1246 _(UNKNOWN_EVENT, "unknown event received") \
1251 #define _(sym,str) ACL_FA_CLEANER_ERROR_##sym,
1252 foreach_acl_fa_cleaner_error
1254 ACL_FA_CLEANER_N_ERROR,
1255 } acl_fa_cleaner_error_t;
1257 static char *acl_fa_cleaner_error_strings[] = {
1258 #define _(sym,string) string,
1259 foreach_acl_fa_cleaner_error
1265 static vlib_node_registration_t acl_fa_session_cleaner_process_node;
1266 static vlib_node_registration_t acl_fa_worker_session_cleaner_process_node;
1269 * Per-worker thread interrupt-driven cleaner thread
1270 * to clean idle connections if there are no packets
1273 acl_fa_worker_conn_cleaner_process(vlib_main_t * vm,
1274 vlib_node_runtime_t * rt, vlib_frame_t * f)
1276 acl_main_t *am = &acl_main;
1277 u64 now = clib_cpu_time_now ();
1278 u16 thread_index = os_get_thread_index ();
1279 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1281 #ifdef FA_NODE_VERBOSE_DEBUG
1282 clib_warning("\nacl_fa_worker_conn_cleaner: thread index %d now %lu\n\n", thread_index, now);
1284 /* allow another interrupt to be queued */
1285 pw->interrupt_is_pending = 0;
1286 if (pw->clear_in_process) {
1287 if (0 == pw->swipe_end_time) {
1289 * Someone has just set the flag to start clearing.
1290 * we do this by combing through the connections up to a "time T"
1291 * which is now, and requeueing everything except the expired
1292 * connections and those matching the interface(s) being cleared.
1296 * first filter the sw_if_index bitmap that they want from us, by
1297 * a bitmap of sw_if_index for which we actually have connections.
1299 if ((pw->pending_clear_sw_if_index_bitmap == 0)
1300 || (pw->serviced_sw_if_index_bitmap == 0)) {
1301 #ifdef FA_NODE_VERBOSE_DEBUG
1302 clib_warning("WORKER-CLEAR: someone tried to call clear, but one of the bitmaps are empty");
1304 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1306 #ifdef FA_NODE_VERBOSE_DEBUG
1307 clib_warning("WORKER-CLEAR: (before and) swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1308 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1309 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1311 pw->pending_clear_sw_if_index_bitmap = clib_bitmap_and(pw->pending_clear_sw_if_index_bitmap,
1312 pw->serviced_sw_if_index_bitmap);
1315 if (clib_bitmap_is_zero(pw->pending_clear_sw_if_index_bitmap)) {
1316 /* if the cross-section is a zero vector, no need to do anything. */
1317 #ifdef FA_NODE_VERBOSE_DEBUG
1318 clib_warning("WORKER: clearing done - nothing to do");
1320 pw->clear_in_process = 0;
1322 #ifdef FA_NODE_VERBOSE_DEBUG
1323 clib_warning("WORKER-CLEAR: swiping sw-if-index bitmap: %U, my serviced bitmap %U",
1324 format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap,
1325 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1327 /* swipe through the connection lists until enqueue timestamps become above "now" */
1328 pw->swipe_end_time = now;
1332 num_expired = acl_fa_check_idle_sessions(am, thread_index, now);
1333 // clib_warning("WORKER-CLEAR: checked %d sessions (clear_in_progress: %d)", num_expired, pw->clear_in_process);
1334 if (pw->clear_in_process) {
1335 if (0 == num_expired) {
1336 /* we were clearing but we could not process any more connections. time to stop. */
1337 clib_bitmap_zero(pw->pending_clear_sw_if_index_bitmap);
1338 pw->clear_in_process = 0;
1339 #ifdef FA_NODE_VERBOSE_DEBUG
1340 clib_warning("WORKER: clearing done, all done");
1343 #ifdef FA_NODE_VERBOSE_DEBUG
1344 clib_warning("WORKER-CLEAR: more work to do, raising interrupt");
1346 /* should continue clearing.. So could they please sent an interrupt again? */
1347 pw->interrupt_is_needed = 1;
1350 if (num_expired >= am->fa_max_deleted_sessions_per_interval) {
1351 /* there was too much work, we should get an interrupt ASAP */
1352 pw->interrupt_is_needed = 1;
1353 pw->interrupt_is_unwanted = 0;
1354 } else if (num_expired <= am->fa_min_deleted_sessions_per_interval) {
1355 /* signal that they should trigger us less */
1356 pw->interrupt_is_needed = 0;
1357 pw->interrupt_is_unwanted = 1;
1359 /* the current rate of interrupts is ok */
1360 pw->interrupt_is_needed = 0;
1361 pw->interrupt_is_unwanted = 0;
1364 pw->interrupt_generation = am->fa_interrupt_generation;
1369 send_one_worker_interrupt (vlib_main_t * vm, acl_main_t *am, int thread_index)
1371 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
1372 if (!pw->interrupt_is_pending) {
1373 pw->interrupt_is_pending = 1;
1374 vlib_node_set_interrupt_pending (vlib_mains[thread_index],
1375 acl_fa_worker_session_cleaner_process_node.index);
1376 /* if the interrupt was requested, mark that done. */
1377 /* pw->interrupt_is_needed = 0; */
1382 send_interrupts_to_workers (vlib_main_t * vm, acl_main_t *am)
1385 /* Can't use vec_len(am->per_worker_data) since the threads might not have come up yet; */
1386 int n_threads = vec_len(vlib_mains);
1387 for (i = n_threads > 1 ? 1 : 0; i < n_threads; i++) {
1388 send_one_worker_interrupt(vm, am, i);
1392 /* centralized process to drive per-worker cleaners */
1394 acl_fa_session_cleaner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1397 acl_main_t *am = &acl_main;
1399 f64 cpu_cps = vm->clib_time.clocks_per_second;
1401 /* We should check if there are connections to clean up - at least twice a second */
1402 u64 max_timer_wait_interval = cpu_cps / 2;
1403 uword event_type, *event_data = 0;
1404 acl_fa_per_worker_data_t *pw0;
1406 am->fa_current_cleaner_timer_wait_interval = max_timer_wait_interval;
1407 am->fa_cleaner_node_index = acl_fa_session_cleaner_process_node.index;
1408 am->fa_interrupt_generation = 1;
1411 now = clib_cpu_time_now ();
1412 next_expire = now + am->fa_current_cleaner_timer_wait_interval;
1413 int has_pending_conns = 0;
1418 * walk over all per-thread list heads of different timeouts,
1419 * and see if there are any connections pending.
1420 * If there aren't - we do not need to wake up until the
1421 * worker code signals that it has added a connection.
1423 * Also, while we are at it, calculate the earliest we need to wake up.
1425 for(ti = 0; ti < vec_len(vlib_mains); ti++) {
1426 if (ti >= vec_len(am->per_worker_data)) {
1429 acl_fa_per_worker_data_t *pw = &am->per_worker_data[ti];
1430 for(tt = 0; tt < vec_len(pw->fa_conn_list_head); tt++) {
1431 u64 head_expiry = acl_fa_get_list_head_expiry_time(am, pw, now, ti, tt);
1432 if ((head_expiry < next_expire) && !pw->interrupt_is_pending) {
1433 #ifdef FA_NODE_VERBOSE_DEBUG
1434 clib_warning("Head expiry: %lu, now: %lu, next_expire: %lu (worker: %d, tt: %d)", head_expiry, now, next_expire, ti, tt);
1436 next_expire = head_expiry;
1438 if (~0 != pw->fa_conn_list_head[tt]) {
1439 has_pending_conns = 1;
1444 /* If no pending connections and no ACL applied then no point in timing out */
1445 if (!has_pending_conns && (0 == am->fa_total_enabled_count))
1447 am->fa_cleaner_cnt_wait_without_timeout++;
1448 (void) vlib_process_wait_for_event (vm);
1449 event_type = vlib_process_get_events (vm, &event_data);
1453 f64 timeout = ((i64) next_expire - (i64) now) / cpu_cps;
1456 /* skip waiting altogether */
1461 am->fa_cleaner_cnt_wait_with_timeout++;
1462 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1463 event_type = vlib_process_get_events (vm, &event_data);
1472 case ACL_FA_CLEANER_RESCHEDULE:
1473 /* Nothing to do. */
1475 case ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX:
1477 uword *clear_sw_if_index_bitmap = 0;
1478 uword *sw_if_index0;
1480 #ifdef FA_NODE_VERBOSE_DEBUG
1481 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX received");
1483 vec_foreach (sw_if_index0, event_data)
1485 am->fa_cleaner_cnt_delete_by_sw_index++;
1486 #ifdef FA_NODE_VERBOSE_DEBUG
1488 ("ACL_FA_NODE_CLEAN: ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX: %d",
1491 if (*sw_if_index0 == ~0)
1497 if (!pool_is_free_index (am->vnet_main->interface_main.sw_interfaces, *sw_if_index0))
1499 clear_sw_if_index_bitmap = clib_bitmap_set(clear_sw_if_index_bitmap, *sw_if_index0, 1);
1503 #ifdef FA_NODE_VERBOSE_DEBUG
1504 clib_warning("ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX bitmap: %U", format_bitmap_hex, clear_sw_if_index_bitmap);
1506 vec_foreach(pw0, am->per_worker_data) {
1507 CLIB_MEMORY_BARRIER ();
1508 while (pw0->clear_in_process) {
1509 CLIB_MEMORY_BARRIER ();
1510 #ifdef FA_NODE_VERBOSE_DEBUG
1511 clib_warning("ACL_FA_NODE_CLEAN: waiting previous cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1513 vlib_process_suspend(vm, 0.0001);
1514 if (pw0->interrupt_is_needed) {
1515 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1518 if (pw0->clear_in_process) {
1519 clib_warning("ERROR-BUG! Could not initiate cleaning on worker because another cleanup in progress");
1523 /* if we need to clear all, then just clear the interfaces that we are servicing */
1524 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(pw0->serviced_sw_if_index_bitmap);
1528 pw0->pending_clear_sw_if_index_bitmap = clib_bitmap_dup(clear_sw_if_index_bitmap);
1530 pw0->clear_in_process = 1;
1533 /* send some interrupts so they can start working */
1534 send_interrupts_to_workers(vm, am);
1536 /* now wait till they all complete */
1537 #ifdef FA_NODE_VERBOSE_DEBUG
1538 clib_warning("CLEANER mains len: %d per-worker len: %d", vec_len(vlib_mains), vec_len(am->per_worker_data));
1540 vec_foreach(pw0, am->per_worker_data) {
1541 CLIB_MEMORY_BARRIER ();
1542 while (pw0->clear_in_process) {
1543 CLIB_MEMORY_BARRIER ();
1544 #ifdef FA_NODE_VERBOSE_DEBUG
1545 clib_warning("ACL_FA_NODE_CLEAN: waiting for my cleaning cycle to finish on %d...", pw0 - am->per_worker_data);
1547 vlib_process_suspend(vm, 0.0001);
1548 if (pw0->interrupt_is_needed) {
1549 send_one_worker_interrupt(vm, am, (pw0 - am->per_worker_data));
1553 #ifdef FA_NODE_VERBOSE_DEBUG
1554 clib_warning("ACL_FA_NODE_CLEAN: cleaning done");
1556 clib_bitmap_free(clear_sw_if_index_bitmap);
1560 #ifdef FA_NODE_VERBOSE_DEBUG
1561 clib_warning ("ACL plugin connection cleaner: unknown event %u",
1564 vlib_node_increment_counter (vm,
1565 acl_fa_session_cleaner_process_node.
1567 ACL_FA_CLEANER_ERROR_UNKNOWN_EVENT, 1);
1568 am->fa_cleaner_cnt_unknown_event++;
1572 send_interrupts_to_workers(vm, am);
1575 _vec_len (event_data) = 0;
1578 * If the interrupts were not processed yet, ensure we wait a bit,
1579 * but up to a point.
1581 int need_more_wait = 0;
1582 int max_wait_cycles = 100;
1585 vec_foreach(pw0, am->per_worker_data) {
1586 if (pw0->interrupt_generation != am->fa_interrupt_generation) {
1590 if (need_more_wait) {
1591 vlib_process_suspend(vm, 0.0001);
1593 } while (need_more_wait && (--max_wait_cycles > 0));
1595 int interrupts_needed = 0;
1596 int interrupts_unwanted = 0;
1598 vec_foreach(pw0, am->per_worker_data) {
1599 if (pw0->interrupt_is_needed) {
1600 interrupts_needed++;
1601 /* the per-worker value is reset when sending the interrupt */
1603 if (pw0->interrupt_is_unwanted) {
1604 interrupts_unwanted++;
1605 pw0->interrupt_is_unwanted = 0;
1608 if (interrupts_needed) {
1609 /* they need more interrupts, do less waiting around next time */
1610 am->fa_current_cleaner_timer_wait_interval /= 2;
1611 /* never go into zero-wait either though - we need to give the space to others */
1612 am->fa_current_cleaner_timer_wait_interval += 1;
1613 } else if (interrupts_unwanted) {
1614 /* slowly increase the amount of sleep up to a limit */
1615 if (am->fa_current_cleaner_timer_wait_interval < max_timer_wait_interval)
1616 am->fa_current_cleaner_timer_wait_interval += cpu_cps * am->fa_cleaner_wait_time_increment;
1618 am->fa_cleaner_cnt_event_cycles++;
1619 am->fa_interrupt_generation++;
1627 acl_fa_enable_disable (u32 sw_if_index, int is_input, int enable_disable)
1629 acl_main_t *am = &acl_main;
1630 if (enable_disable) {
1631 acl_fa_verify_init_sessions(am);
1632 am->fa_total_enabled_count++;
1633 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1634 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1635 ACL_FA_CLEANER_RESCHEDULE, 0);
1636 clib_mem_set_heap (oldheap);
1638 am->fa_total_enabled_count--;
1643 ASSERT(clib_bitmap_get(am->fa_in_acl_on_sw_if_index, sw_if_index) != enable_disable);
1644 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1645 vnet_feature_enable_disable ("ip4-unicast", "acl-plugin-in-ip4-fa",
1646 sw_if_index, enable_disable, 0, 0);
1647 vnet_feature_enable_disable ("ip6-unicast", "acl-plugin-in-ip6-fa",
1648 sw_if_index, enable_disable, 0, 0);
1649 clib_mem_set_heap (oldheap);
1650 am->fa_in_acl_on_sw_if_index =
1651 clib_bitmap_set (am->fa_in_acl_on_sw_if_index, sw_if_index,
1656 ASSERT(clib_bitmap_get(am->fa_out_acl_on_sw_if_index, sw_if_index) != enable_disable);
1657 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1658 vnet_feature_enable_disable ("ip4-output", "acl-plugin-out-ip4-fa",
1659 sw_if_index, enable_disable, 0, 0);
1660 vnet_feature_enable_disable ("ip6-output", "acl-plugin-out-ip6-fa",
1661 sw_if_index, enable_disable, 0, 0);
1662 clib_mem_set_heap (oldheap);
1663 am->fa_out_acl_on_sw_if_index =
1664 clib_bitmap_set (am->fa_out_acl_on_sw_if_index, sw_if_index,
1667 if ((!enable_disable) && (!acl_fa_ifc_has_in_acl (am, sw_if_index))
1668 && (!acl_fa_ifc_has_out_acl (am, sw_if_index)))
1670 #ifdef FA_NODE_VERBOSE_DEBUG
1671 clib_warning("ENABLE-DISABLE: clean the connections on interface %d", sw_if_index);
1673 void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1674 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1675 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1677 clib_mem_set_heap (oldheap);
1682 show_fa_sessions_hash(vlib_main_t * vm, u32 verbose)
1684 acl_main_t *am = &acl_main;
1685 if (am->fa_sessions_hash_is_initialized) {
1686 vlib_cli_output(vm, "\nSession lookup hash table:\n%U\n\n",
1687 BV (format_bihash), &am->fa_sessions_hash, verbose);
1689 vlib_cli_output(vm, "\nSession lookup hash table is not allocated.\n\n");
1696 VLIB_REGISTER_NODE (acl_fa_worker_session_cleaner_process_node, static) = {
1697 .function = acl_fa_worker_conn_cleaner_process,
1698 .name = "acl-plugin-fa-worker-cleaner-process",
1699 .type = VLIB_NODE_TYPE_INPUT,
1700 .state = VLIB_NODE_STATE_INTERRUPT,
1703 VLIB_REGISTER_NODE (acl_fa_session_cleaner_process_node, static) = {
1704 .function = acl_fa_session_cleaner_process,
1705 .type = VLIB_NODE_TYPE_PROCESS,
1706 .name = "acl-plugin-fa-cleaner-process",
1707 .n_errors = ARRAY_LEN (acl_fa_cleaner_error_strings),
1708 .error_strings = acl_fa_cleaner_error_strings,
1714 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
1716 .function = acl_in_ip6_l2_node_fn,
1717 .name = "acl-plugin-in-ip6-l2",
1718 .vector_size = sizeof (u32),
1719 .format_trace = format_acl_fa_trace,
1720 .type = VLIB_NODE_TYPE_INTERNAL,
1721 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1722 .error_strings = acl_fa_error_strings,
1723 .n_next_nodes = ACL_FA_N_NEXT,
1726 [ACL_FA_ERROR_DROP] = "error-drop",
1730 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
1732 .function = acl_in_ip4_l2_node_fn,
1733 .name = "acl-plugin-in-ip4-l2",
1734 .vector_size = sizeof (u32),
1735 .format_trace = format_acl_fa_trace,
1736 .type = VLIB_NODE_TYPE_INTERNAL,
1737 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1738 .error_strings = acl_fa_error_strings,
1739 .n_next_nodes = ACL_FA_N_NEXT,
1742 [ACL_FA_ERROR_DROP] = "error-drop",
1746 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1748 .function = acl_out_ip6_l2_node_fn,
1749 .name = "acl-plugin-out-ip6-l2",
1750 .vector_size = sizeof (u32),
1751 .format_trace = format_acl_fa_trace,
1752 .type = VLIB_NODE_TYPE_INTERNAL,
1753 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1754 .error_strings = acl_fa_error_strings,
1755 .n_next_nodes = ACL_FA_N_NEXT,
1758 [ACL_FA_ERROR_DROP] = "error-drop",
1762 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1764 .function = acl_out_ip4_l2_node_fn,
1765 .name = "acl-plugin-out-ip4-l2",
1766 .vector_size = sizeof (u32),
1767 .format_trace = format_acl_fa_trace,
1768 .type = VLIB_NODE_TYPE_INTERNAL,
1769 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1770 .error_strings = acl_fa_error_strings,
1771 .n_next_nodes = ACL_FA_N_NEXT,
1774 [ACL_FA_ERROR_DROP] = "error-drop",
1779 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1781 .function = acl_in_ip6_fa_node_fn,
1782 .name = "acl-plugin-in-ip6-fa",
1783 .vector_size = sizeof (u32),
1784 .format_trace = format_acl_fa_trace,
1785 .type = VLIB_NODE_TYPE_INTERNAL,
1786 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1787 .error_strings = acl_fa_error_strings,
1788 .n_next_nodes = ACL_FA_N_NEXT,
1791 [ACL_FA_ERROR_DROP] = "error-drop",
1795 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1797 .arc_name = "ip6-unicast",
1798 .node_name = "acl-plugin-in-ip6-fa",
1799 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1802 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1804 .function = acl_in_ip4_fa_node_fn,
1805 .name = "acl-plugin-in-ip4-fa",
1806 .vector_size = sizeof (u32),
1807 .format_trace = format_acl_fa_trace,
1808 .type = VLIB_NODE_TYPE_INTERNAL,
1809 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1810 .error_strings = acl_fa_error_strings,
1811 .n_next_nodes = ACL_FA_N_NEXT,
1814 [ACL_FA_ERROR_DROP] = "error-drop",
1818 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1820 .arc_name = "ip4-unicast",
1821 .node_name = "acl-plugin-in-ip4-fa",
1822 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1826 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1828 .function = acl_out_ip6_fa_node_fn,
1829 .name = "acl-plugin-out-ip6-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_out_ip6_fa_feature, static) =
1844 .arc_name = "ip6-output",
1845 .node_name = "acl-plugin-out-ip6-fa",
1846 .runs_before = VNET_FEATURES ("interface-output"),
1849 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1851 .function = acl_out_ip4_fa_node_fn,
1852 .name = "acl-plugin-out-ip4-fa",
1853 .vector_size = sizeof (u32),
1854 .format_trace = format_acl_fa_trace,
1855 .type = VLIB_NODE_TYPE_INTERNAL,
1856 .n_errors = ARRAY_LEN (acl_fa_error_strings),
1857 .error_strings = acl_fa_error_strings,
1858 .n_next_nodes = ACL_FA_N_NEXT,
1859 /* edit / add dispositions here */
1862 [ACL_FA_ERROR_DROP] = "error-drop",
1866 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1868 .arc_name = "ip4-output",
1869 .node_name = "acl-plugin-out-ip4-fa",
1870 .runs_before = VNET_FEATURES ("interface-output"),