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.
17 /* ICMPv4 invert type for stateful ACL */
18 static const u8 icmp4_invmap[] = {
19 [ICMP4_echo_request] = ICMP4_echo_reply + 1,
20 [ICMP4_timestamp_request] = ICMP4_timestamp_reply + 1,
21 [ICMP4_information_request] = ICMP4_information_reply + 1,
22 [ICMP4_address_mask_request] = ICMP4_address_mask_reply + 1
25 /* Supported ICMPv4 messages for session creation */
26 static const u8 icmp4_valid_new[] = {
27 [ICMP4_echo_request] = 1,
28 [ICMP4_timestamp_request] = 1,
29 [ICMP4_information_request] = 1,
30 [ICMP4_address_mask_request] = 1
33 /* ICMPv6 invert type for stateful ACL */
34 static const u8 icmp6_invmap[] = {
35 [ICMP6_echo_request - 128] = ICMP6_echo_reply + 1,
36 [ICMP6_node_information_request - 128] = ICMP6_node_information_response + 1
39 /* Supported ICMPv6 messages for session creation */
40 static const u8 icmp6_valid_new[] = {
41 [ICMP6_echo_request - 128] = 1,
42 [ICMP6_node_information_request - 128] = 1
45 /* IP4 and IP6 protocol numbers of ICMP */
46 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
51 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
53 return am->fa_sessions_hash_is_initialized;
57 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
59 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
64 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
66 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
71 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
73 /* seen both SYNs and ACKs but not FINs means we are in established state */
75 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
76 TCP_FLAGS_RSTFINACKSYN);
77 switch (sess->info.l4.proto)
80 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
82 return ACL_TIMEOUT_TCP_IDLE;
86 return ACL_TIMEOUT_TCP_TRANSIENT;
90 return ACL_TIMEOUT_UDP_IDLE;
93 return ACL_TIMEOUT_UDP_IDLE;
98 * Get the idle timeout of a session.
102 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
104 u64 timeout = (am->vlib_main->clib_time.clocks_per_second);
105 if (sess->link_list_id == ACL_TIMEOUT_PURGATORY)
107 timeout /= (1000000 / SESSION_PURGATORY_TIMEOUT_USEC);
111 int timeout_type = fa_session_get_timeout_type (am, sess);
112 timeout *= am->session_timeout_sec[timeout_type];
117 always_inline fa_session_t *
118 get_session_ptr_no_check (acl_main_t * am, u16 thread_index,
121 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
122 return pool_elt_at_index (pw->fa_sessions_pool, session_index);
126 always_inline fa_session_t *
127 get_session_ptr (acl_main_t * am, u16 thread_index, u32 session_index)
129 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
131 if (PREDICT_FALSE (session_index >= vec_len (pw->fa_sessions_pool)))
134 return pool_elt_at_index (pw->fa_sessions_pool, session_index);
138 is_valid_session_ptr (acl_main_t * am, u16 thread_index, fa_session_t * sess)
140 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
142 && ((sess - pw->fa_sessions_pool) <
143 pool_len (pw->fa_sessions_pool)));
147 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id,
151 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
153 sess->deleted ? ACL_TIMEOUT_PURGATORY : fa_session_get_timeout_type (am,
155 uword thread_index = os_get_thread_index ();
156 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
157 /* the retrieved session thread index must be necessarily the same as the one in the key */
158 ASSERT (sess->thread_index == sess_id.thread_index);
159 /* the retrieved session thread index must be the same as current thread */
160 ASSERT (sess->thread_index == thread_index);
161 sess->link_enqueue_time = now;
162 sess->link_list_id = list_id;
163 sess->link_next_idx = FA_SESSION_BOGUS_INDEX;
164 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
165 if (FA_SESSION_BOGUS_INDEX != pw->fa_conn_list_tail[list_id])
167 fa_session_t *prev_sess =
168 get_session_ptr (am, thread_index, pw->fa_conn_list_tail[list_id]);
169 prev_sess->link_next_idx = sess_id.session_index;
170 /* We should never try to link with a session on another thread */
171 ASSERT (prev_sess->thread_index == sess->thread_index);
173 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
175 #ifdef FA_NODE_VERBOSE_DEBUG
177 ("FA-SESSION-DEBUG: add session id %d on thread %d sw_if_index %d",
178 sess_id.session_index, thread_index, sess->sw_if_index);
180 pw->serviced_sw_if_index_bitmap =
181 clib_bitmap_set (pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
183 if (FA_SESSION_BOGUS_INDEX == pw->fa_conn_list_head[list_id])
185 pw->fa_conn_list_head[list_id] = sess_id.session_index;
186 /* set the head expiry time because it is the first element */
187 pw->fa_conn_list_head_expiry_time[list_id] =
188 now + fa_session_get_timeout (am, sess);
193 acl_fa_conn_list_delete_session (acl_main_t * am,
194 fa_full_session_id_t sess_id, u64 now)
196 uword thread_index = os_get_thread_index ();
197 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
198 if (thread_index != sess_id.thread_index)
200 /* If another thread attempts to delete the session, fail it. */
201 #ifdef FA_NODE_VERBOSE_DEBUG
202 clib_warning ("thread id in key %d != curr thread index, not deleting");
207 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
208 u64 next_expiry_time = ~0ULL;
209 /* we should never try to delete the session with another thread index */
210 if (sess->thread_index != os_get_thread_index ())
213 ("Attempting to delete session belonging to thread %d by thread %d",
214 sess->thread_index, thread_index);
216 if (FA_SESSION_BOGUS_INDEX != sess->link_prev_idx)
218 fa_session_t *prev_sess =
219 get_session_ptr (am, thread_index, sess->link_prev_idx);
220 /* the previous session must be in the same list as this one */
221 ASSERT (prev_sess->link_list_id == sess->link_list_id);
222 prev_sess->link_next_idx = sess->link_next_idx;
224 if (FA_SESSION_BOGUS_INDEX != sess->link_next_idx)
226 fa_session_t *next_sess =
227 get_session_ptr (am, thread_index, sess->link_next_idx);
228 /* The next session must be in the same list as the one we are deleting */
229 ASSERT (next_sess->link_list_id == sess->link_list_id);
230 next_sess->link_prev_idx = sess->link_prev_idx;
231 next_expiry_time = now + fa_session_get_timeout (am, next_sess);
233 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index)
235 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
236 pw->fa_conn_list_head_expiry_time[sess->link_list_id] =
239 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index)
241 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
247 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now,
248 fa_full_session_id_t sess_id)
250 if (acl_fa_conn_list_delete_session (am, sess_id, now))
252 acl_fa_conn_list_add_session (am, sess_id, now);
258 * Our thread does not own this connection, so we can not requeue
259 * The session. So we post the signal to the owner.
261 aclp_post_session_change_request (am, sess_id.thread_index,
262 sess_id.session_index,
263 ACL_FA_REQ_SESS_RESCHEDULE);
269 is_ip6_5tuple (fa_5tuple_t * p5t)
271 return (p5t->l3_zero_pad[0] | p5t->
272 l3_zero_pad[1] | p5t->l3_zero_pad[2] | p5t->l3_zero_pad[3] | p5t->
273 l3_zero_pad[4] | p5t->l3_zero_pad[5]) != 0;
277 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
278 fa_session_t * sess, fa_5tuple_t * pkt_5tuple,
281 sess->last_active_time = now;
282 u8 old_flags = sess->tcp_flags_seen.as_u8[is_input];
283 u8 new_flags = old_flags | pkt_5tuple->pkt.tcp_flags;
285 int flags_need_update = pkt_5tuple->pkt.tcp_flags_valid
286 && (old_flags != new_flags);
287 if (PREDICT_FALSE (flags_need_update))
289 sess->tcp_flags_seen.as_u8[is_input] = new_flags;
295 reverse_l4_u64_fastpath (u64 l4, int is_ip6)
297 fa_session_l4_key_t l4i = {.as_u64 = l4 };
298 fa_session_l4_key_t l4o;
300 l4o.port[1] = l4i.port[0];
301 l4o.port[0] = l4i.port[1];
303 l4o.non_port_l4_data = l4i.non_port_l4_data;
304 l4o.l4_flags = l4i.l4_flags ^ FA_SK_L4_FLAG_IS_INPUT;
309 reverse_l4_u64_slowpath_valid (u64 l4, int is_ip6, u64 * out)
311 fa_session_l4_key_t l4i = {.as_u64 = l4 };
312 fa_session_l4_key_t l4o;
314 if (l4i.proto == icmp_protos[is_ip6])
316 static const u8 *icmp_invmap[] = { icmp4_invmap, icmp6_invmap };
317 static const u8 *icmp_valid_new[] =
318 { icmp4_valid_new, icmp6_valid_new };
319 static const u8 icmp_invmap_size[] = { sizeof (icmp4_invmap),
320 sizeof (icmp6_invmap)
322 static const u8 icmp_valid_new_size[] = { sizeof (icmp4_valid_new),
323 sizeof (icmp6_valid_new)
325 int type = is_ip6 ? l4i.port[0] - 128 : l4i.port[0];
327 l4o.non_port_l4_data = l4i.non_port_l4_data;
328 l4o.port[0] = l4i.port[0];
329 l4o.port[1] = l4i.port[1];
333 * ONLY ICMP messages defined in icmp4_valid_new/icmp6_valid_new table
334 * are allowed to create stateful ACL.
335 * The other messages will be forwarded without creating a reverse session.
338 int valid_reverse_sess = (type >= 0
339 && (type <= icmp_valid_new_size[is_ip6])
340 && (icmp_valid_new[is_ip6][type])
341 && (type <= icmp_invmap_size[is_ip6])
342 && icmp_invmap[is_ip6][type]);
343 if (valid_reverse_sess)
345 l4o.l4_flags = l4i.l4_flags ^ FA_SK_L4_FLAG_IS_INPUT;
346 l4o.port[0] = icmp_invmap[is_ip6][type] - 1;
350 return valid_reverse_sess;
353 *out = reverse_l4_u64_fastpath (l4, is_ip6);
359 reverse_session_add_del_ip6 (acl_main_t * am,
360 clib_bihash_kv_40_8_t * pkv, int is_add)
362 clib_bihash_kv_40_8_t kv2;
363 kv2.key[0] = pkv->key[2];
364 kv2.key[1] = pkv->key[3];
365 kv2.key[2] = pkv->key[0];
366 kv2.key[3] = pkv->key[1];
367 /* the last u64 needs special treatment (ports, etc.) so we do it last */
368 kv2.value = pkv->value;
369 if (PREDICT_FALSE (is_session_l4_key_u64_slowpath (pkv->key[4])))
371 if (reverse_l4_u64_slowpath_valid (pkv->key[4], 1, &kv2.key[4]))
372 clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &kv2, is_add);
376 kv2.key[4] = reverse_l4_u64_fastpath (pkv->key[4], 1);
377 clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &kv2, is_add);
382 reverse_session_add_del_ip4 (acl_main_t * am,
383 clib_bihash_kv_16_8_t * pkv, int is_add)
385 clib_bihash_kv_16_8_t kv2;
387 ((pkv->key[0] & 0xffffffff) << 32) | ((pkv->key[0] >> 32) & 0xffffffff);
388 /* the last u64 needs special treatment (ports, etc.) so we do it last */
389 kv2.value = pkv->value;
390 if (PREDICT_FALSE (is_session_l4_key_u64_slowpath (pkv->key[1])))
392 if (reverse_l4_u64_slowpath_valid (pkv->key[1], 0, &kv2.key[1]))
393 clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &kv2, is_add);
397 kv2.key[1] = reverse_l4_u64_fastpath (pkv->key[1], 0);
398 clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &kv2, is_add);
403 acl_fa_deactivate_session (acl_main_t * am, u32 sw_if_index,
404 fa_full_session_id_t sess_id)
407 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
408 ASSERT (sess->thread_index == os_get_thread_index ());
411 clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash,
412 &sess->info.kv_40_8, 0);
413 reverse_session_add_del_ip6 (am, &sess->info.kv_40_8, 0);
417 clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash,
418 &sess->info.kv_16_8, 0);
419 reverse_session_add_del_ip4 (am, &sess->info.kv_16_8, 0);
423 clib_atomic_fetch_add (&am->fa_session_total_deactivations, 1);
427 acl_fa_put_session (acl_main_t * am, u32 sw_if_index,
428 fa_full_session_id_t sess_id)
430 if (sess_id.thread_index != os_get_thread_index ())
433 ("Attempting to delete session belonging to thread %d by thread %d",
434 sess_id.thread_index, os_get_thread_index ());
436 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
437 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
438 /* Deleting from timer structures not needed,
439 as the caller must have dealt with the timers. */
440 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
441 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
442 clib_atomic_fetch_add (&am->fa_session_total_dels, 1);
446 acl_fa_two_stage_delete_session (acl_main_t * am, u32 sw_if_index,
447 fa_full_session_id_t sess_id, u64 now)
450 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
453 acl_fa_put_session (am, sw_if_index, sess_id);
458 acl_fa_deactivate_session (am, sw_if_index, sess_id);
459 acl_fa_conn_list_add_session (am, sess_id, now);
465 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
468 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
469 return (curr_sess_count + vec_len (vlib_mains) <
470 am->fa_conn_table_max_entries);
475 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index,
476 u32 sw_if_index, u64 now)
478 /* try to recycle a TCP transient session */
479 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
480 fa_full_session_id_t volatile sess_id;
483 /* clean up sessions from purgatory, if we can */
484 sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_PURGATORY];
485 while ((FA_SESSION_BOGUS_INDEX != sess_id.session_index)
486 && n_recycled < am->fa_max_deleted_sessions_per_interval)
488 sess_id.thread_index = thread_index;
490 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
491 if (sess->link_enqueue_time + fa_session_get_timeout (am, sess) < now)
493 acl_fa_conn_list_delete_session (am, sess_id, now);
494 /* interface that needs the sessions may not be the interface of the session. */
495 acl_fa_put_session (am, sess->sw_if_index, sess_id);
499 break; /* too early to try to recycle from here, bail out */
500 sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_PURGATORY];
502 sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_TCP_TRANSIENT];
503 if (FA_SESSION_BOGUS_INDEX != sess_id.session_index)
505 sess_id.thread_index = thread_index;
506 acl_fa_conn_list_delete_session (am, sess_id, now);
507 acl_fa_deactivate_session (am, sw_if_index, sess_id);
508 /* this goes to purgatory list */
509 acl_fa_conn_list_add_session (am, sess_id, now);
514 always_inline fa_full_session_id_t
515 acl_fa_add_session (acl_main_t * am, int is_input, int is_ip6,
516 u32 sw_if_index, u64 now, fa_5tuple_t * p5tuple,
517 u16 current_policy_epoch)
519 fa_full_session_id_t f_sess_id;
520 uword thread_index = os_get_thread_index ();
521 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
523 f_sess_id.thread_index = thread_index;
526 if (f_sess_id.as_u64 == ~0)
528 clib_error ("Adding session with invalid value");
531 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
532 f_sess_id.session_index = sess - pw->fa_sessions_pool;
533 f_sess_id.intf_policy_epoch = current_policy_epoch;
537 sess->info.kv_40_8.key[0] = p5tuple->kv_40_8.key[0];
538 sess->info.kv_40_8.key[1] = p5tuple->kv_40_8.key[1];
539 sess->info.kv_40_8.key[2] = p5tuple->kv_40_8.key[2];
540 sess->info.kv_40_8.key[3] = p5tuple->kv_40_8.key[3];
541 sess->info.kv_40_8.key[4] = p5tuple->kv_40_8.key[4];
542 sess->info.kv_40_8.value = f_sess_id.as_u64;
546 sess->info.kv_16_8.key[0] = p5tuple->kv_16_8.key[0];
547 sess->info.kv_16_8.key[1] = p5tuple->kv_16_8.key[1];
548 sess->info.kv_16_8.value = f_sess_id.as_u64;
551 sess->last_active_time = now;
552 sess->sw_if_index = sw_if_index;
553 sess->tcp_flags_seen.as_u16 = 0;
554 sess->thread_index = thread_index;
555 sess->link_list_id = ACL_TIMEOUT_UNUSED;
556 sess->link_prev_idx = FA_SESSION_BOGUS_INDEX;
557 sess->link_next_idx = FA_SESSION_BOGUS_INDEX;
559 sess->is_ip6 = is_ip6;
561 acl_fa_conn_list_add_session (am, f_sess_id, now);
563 ASSERT (am->fa_sessions_hash_is_initialized == 1);
566 reverse_session_add_del_ip6 (am, &sess->info.kv_40_8, 1);
567 clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash,
568 &sess->info.kv_40_8, 1);
572 reverse_session_add_del_ip4 (am, &sess->info.kv_16_8, 1);
573 clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash,
574 &sess->info.kv_16_8, 1);
577 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
578 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
579 clib_atomic_fetch_add (&am->fa_session_total_adds, 1);
584 acl_fa_find_session (acl_main_t * am, int is_ip6, u32 sw_if_index0,
585 fa_5tuple_t * p5tuple, u64 * pvalue_sess)
590 clib_bihash_kv_40_8_t kv_result;
591 res = (clib_bihash_search_inline_2_40_8
592 (&am->fa_ip6_sessions_hash, &p5tuple->kv_40_8, &kv_result) == 0);
593 *pvalue_sess = kv_result.value;
597 clib_bihash_kv_16_8_t kv_result;
598 res = (clib_bihash_search_inline_2_16_8
599 (&am->fa_ip4_sessions_hash, &p5tuple->kv_16_8, &kv_result) == 0);
600 *pvalue_sess = kv_result.value;
606 acl_fa_make_session_hash (acl_main_t * am, int is_ip6, u32 sw_if_index0,
607 fa_5tuple_t * p5tuple)
610 return clib_bihash_hash_40_8 (&p5tuple->kv_40_8);
612 return clib_bihash_hash_16_8 (&p5tuple->kv_16_8);
616 acl_fa_prefetch_session_bucket_for_hash (acl_main_t * am, int is_ip6,
620 clib_bihash_prefetch_bucket_40_8 (&am->fa_ip6_sessions_hash, hash);
622 clib_bihash_prefetch_bucket_16_8 (&am->fa_ip4_sessions_hash, hash);
626 acl_fa_prefetch_session_data_for_hash (acl_main_t * am, int is_ip6, u64 hash)
629 clib_bihash_prefetch_data_40_8 (&am->fa_ip6_sessions_hash, hash);
631 clib_bihash_prefetch_data_16_8 (&am->fa_ip4_sessions_hash, hash);
635 acl_fa_find_session_with_hash (acl_main_t * am, int is_ip6, u32 sw_if_index0,
636 u64 hash, fa_5tuple_t * p5tuple,
642 clib_bihash_kv_40_8_t kv_result;
643 kv_result.value = ~0ULL;
644 res = (clib_bihash_search_inline_2_with_hash_40_8
645 (&am->fa_ip6_sessions_hash, hash, &p5tuple->kv_40_8,
647 *pvalue_sess = kv_result.value;
651 clib_bihash_kv_16_8_t kv_result;
652 kv_result.value = ~0ULL;
653 res = (clib_bihash_search_inline_2_with_hash_16_8
654 (&am->fa_ip4_sessions_hash, hash, &p5tuple->kv_16_8,
656 *pvalue_sess = kv_result.value;
663 * fd.io coding-style-patch-verification: ON
666 * eval: (c-set-style "gnu")