Skip to content

Commit 07f9966

Browse files
Hadi Rayan Al-Sandidbganne
authored andcommitted
sfdp: add api sfdp_kill_session_batch
* CSIT tests kill all sessions in-between runs. Using existing sfdp_kill_session(is_all=1) api lead to large number of sessions being marked for expiry per worker at once (1M+ per worker), which leads to api/cli hang. * sfdp_kill_session_batch addresses this by marking only up-to N sessions for expiry. This API can be called periodically to progressively kill sfdp sessions. * Introduce per-session flag 'expiry_pending' to notify whether a sfdp session has been marked for expiry. * Remove redundant function sfdp_session_timer_update_unlikely_past which is a duplicate of sfdp_session_timer_update_maybe_past Type: improvement Change-Id: I275decaaf2e76521cb8e6a0898871923aa4a3659 Signed-off-by: Hadi Rayan Al-Sandid <halsandi@cisco.com>
1 parent 07bfb2a commit 07f9966

9 files changed

Lines changed: 200 additions & 33 deletions

File tree

‎src/vnet/sfdp/api.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,21 @@ vl_api_sfdp_kill_session_t_handler (vl_api_sfdp_kill_session_t *mp)
204204
sfdp_main_t *sfdp = &sfdp_main;
205205
u32 session_index = clib_net_to_host_u32 (mp->session_index);
206206
u8 is_all = mp->is_all;
207-
clib_error_t *err = sfdp_kill_session (sfdp, session_index, is_all);
208207
vl_api_sfdp_kill_session_reply_t *rmp;
209-
int rv = err ? -1 : 0;
208+
int rv = sfdp_kill_session (sfdp, session_index, is_all);
210209
REPLY_MACRO (VL_API_SFDP_KILL_SESSION_REPLY);
211210
}
212211

212+
static void
213+
vl_api_sfdp_kill_session_batch_t_handler (vl_api_sfdp_kill_session_batch_t *mp)
214+
{
215+
sfdp_main_t *sfdp = &sfdp_main;
216+
u32 max = clib_net_to_host_u32 (mp->max);
217+
vl_api_sfdp_kill_session_batch_reply_t *rmp;
218+
int rv = sfdp_kill_session_batch (sfdp, max);
219+
REPLY_MACRO (VL_API_SFDP_KILL_SESSION_BATCH_REPLY);
220+
}
221+
213222
static void
214223
sfdp_send_tenant_details (vl_api_registration_t *rp, u32 context,
215224
u16 tenant_index, sfdp_tenant_t *tenant)

‎src/vnet/sfdp/cli.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ sfdp_kill_session_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli
407407
unformat_input_t line_input_, *line_input = &line_input_;
408408
clib_error_t *err = 0;
409409
sfdp_main_t *sfdp = &sfdp_main;
410+
int rv;
410411
u32 session_index = ~0;
411412
bool is_all = false;
412413
bool has_index = false;
@@ -439,7 +440,10 @@ sfdp_kill_session_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli
439440
goto done;
440441
}
441442

442-
err = sfdp_kill_session (sfdp, session_index, is_all);
443+
rv = sfdp_kill_session (sfdp, session_index, is_all);
444+
if (rv)
445+
err = clib_error_return_code (0, rv, 0, "failed to kill SFDP session: %U",
446+
format_vnet_api_errno, rv);
443447

444448
done:
445449
unformat_free (line_input);

‎src/vnet/sfdp/expiry/expiry.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ sfdp_set_expiry_callbacks (const sfdp_expiry_callbacks_t *callbacks)
2525
void
2626
sfdp_enable_disable_expiry_node (u8 is_disable, int skip_main)
2727
{
28+
expiry_is_enabled = !is_disable;
2829
u32 n_vms = vlib_num_workers () + 1;
2930
for (int i = !!skip_main; i < n_vms; i++)
3031
{
@@ -45,7 +46,6 @@ sfdp_enable_disable_expiry (u8 is_disable)
4546

4647
if (!is_disable)
4748
{ /* Init module first */
48-
expiry_is_enabled = true;
4949
sfdp->expiry_callbacks.enable ();
5050
}
5151

@@ -55,7 +55,6 @@ sfdp_enable_disable_expiry (u8 is_disable)
5555
if (is_disable)
5656
{ /* De-init module last */
5757
sfdp->expiry_callbacks.disable ();
58-
expiry_is_enabled = false;
5958
}
6059
}
6160

‎src/vnet/sfdp/sfdp.api‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2025 Cisco Systems, Inc.
33
*/
44

5-
option version = "1.0.0";
5+
option version = "1.0.1";
66
import "vnet/ip/ip_types.api";
77
import "vnet/sfdp/sfdp_types.api";
88

@@ -69,6 +69,18 @@ autoreply define sfdp_kill_session
6969
bool is_all;
7070
};
7171

72+
/** \brief Force expiry while inspecting up to the specified number of active
73+
sfdp sessions
74+
@param max - maximum number of active sfdp sessions to inspect.
75+
*/
76+
autoreply define sfdp_kill_session_batch
77+
{
78+
u32 client_index;
79+
u32 context;
80+
81+
u32 max [default=1000];
82+
};
83+
7284
define sfdp_tenant_dump
7385
{
7486
u32 client_index;

‎src/vnet/sfdp/sfdp.c‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,13 @@ sfdp_set_icmp_error_node (sfdp_main_t *sfdp, u32 tenant_id, u8 is_ip6,
411411
* Both call sites guarantee this:
412412
* - CLI: vlib/cli.c dispatches non-is_mp_safe commands under barrier_sync
413413
* - API: vlibmemory/memory_api.c acquires vl_msg_api_barrier_sync() for
414-
* non-is_mp_safe messages, which includes sfdp_kill_session
414+
* non-is_mp_safe messages, which include sfdp_kill_session and
415+
* sfdp_kill_session_batch
415416
*
416417
* With workers stopped it is safe to:
417418
* - write directly into the worker-owned ptd->expired_sessions vector
418419
* without a lock (no concurrent reader/writer),
419-
* - call sfdp_session_timer_stop() on the worker-owned timer wheel
420-
* (the wheel cannot advance while the worker is paused, so the handle
421-
* is always valid: a session that had already naturally expired would
422-
* have been fully removed in the previous dispatch and would not reach
423-
* this function because sfdp_session_at_index_if_valid() returns NULL).
420+
* - call sfdp_session_timer_stop() on the worker-owned timer wheel.
424421
*/
425422
static void
426423
sfdp_expire_session_now (sfdp_session_t *session, f64 now)
@@ -434,6 +431,12 @@ sfdp_expire_session_now (sfdp_session_t *session, f64 now)
434431
sfdp_timer_per_thread_data_t *tptd = sfdp_timer_get_per_thread_data (thread_index);
435432
sfdp_session_timer_t *timer = SFDP_SESSION_TIMER (session);
436433

434+
/* Check if session has already been marked for expiry */
435+
if (timer->expiry_pending)
436+
return;
437+
438+
timer->expiry_pending = 1;
439+
437440
/* Directly add session to the expired list, bypassing the timing wheel's
438441
* nticks >= 1 constraint (tw_timer_template.c). The session is processed
439442
* on the very next sfdp-expire node execution regardless of when
@@ -448,7 +451,7 @@ sfdp_expire_session_now (sfdp_session_t *session, f64 now)
448451
vlib_node_set_interrupt_pending (vlib_get_main_by_index (thread_index), sfdp_expire_node.index);
449452
}
450453

451-
clib_error_t *
454+
int
452455
sfdp_kill_session (sfdp_main_t *sfdp, u32 session_index, u8 is_all)
453456
{
454457
vlib_main_t *vm = vlib_get_main ();
@@ -457,7 +460,7 @@ sfdp_kill_session (sfdp_main_t *sfdp, u32 session_index, u8 is_all)
457460
uword index;
458461

459462
if (vec_len (sfdp_timer_main.per_thread_data) == 0)
460-
return clib_error_return (0, "sfdp timer module is not initialized");
463+
return VNET_API_ERROR_INIT_FAILED;
461464

462465
if (is_all)
463466
{
@@ -467,16 +470,39 @@ sfdp_kill_session (sfdp_main_t *sfdp, u32 session_index, u8 is_all)
467470

468471
session = sfdp_session_at_index_if_valid (session_index);
469472
if (!session)
470-
return clib_error_return (0, "Session index %u not found", session_index);
473+
return VNET_API_ERROR_NO_SUCH_ENTRY;
471474

472475
sfdp_expire_session_now (session, now);
473476
return 0;
474477
}
475478

476479
int
477-
sfdp_create_session (vlib_main_t *vm, vlib_buffer_t *b, u32 context_id,
478-
u32 thread_index, u32 tenant_index, u32 *session_index,
479-
int is_ipv6)
480+
sfdp_kill_session_batch (sfdp_main_t *sfdp, u32 max)
481+
{
482+
vlib_main_t *vm = vlib_get_main ();
483+
f64 now = vlib_time_now (vm);
484+
sfdp_session_t *session;
485+
uword index;
486+
u32 inspected = 0;
487+
488+
if (vec_len (sfdp_timer_main.per_thread_data) == 0)
489+
return VNET_API_ERROR_INIT_FAILED;
490+
491+
sfdp_foreach_session (sfdp, index, session)
492+
{
493+
if (inspected == max)
494+
break;
495+
496+
inspected++;
497+
sfdp_expire_session_now (session, now);
498+
}
499+
500+
return 0;
501+
}
502+
503+
int
504+
sfdp_create_session (vlib_main_t *vm, vlib_buffer_t *b, u32 context_id, u32 thread_index,
505+
u32 tenant_index, u32 *session_index, int is_ipv6)
480506
{
481507
return sfdp_create_session_with_scope_index (
482508
vm, b, context_id, thread_index, tenant_index, session_index, 0, is_ipv6);

‎src/vnet/sfdp/sfdp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ clib_error_t *sfdp_set_sp_node (sfdp_main_t *sfdp, u32 tenant_id, u32 sp_index,
919919
u32 node_index);
920920
clib_error_t *sfdp_set_icmp_error_node (sfdp_main_t *sfdp, u32 tenant_id,
921921
u8 is_ip6, u32 node_index);
922-
clib_error_t *sfdp_kill_session (sfdp_main_t *sfdp, u32 session_index, u8 is_all);
922+
int sfdp_kill_session (sfdp_main_t *sfdp, u32 session_index, u8 is_all);
923+
int sfdp_kill_session_batch (sfdp_main_t *sfdp, u32 max);
923924
__clib_export void sfdp_normalise_ip4_key (sfdp_session_t *session, sfdp_session_ip4_key_t *result,
924925
u8 key_idx);
925926

‎src/vnet/sfdp/timer/timer.h‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ typedef struct
3030
{
3131
f64 next_expiration;
3232
u32 handle;
33-
u32 __unused;
33+
u8 expiry_pending;
34+
u8 __pad[3];
3435
} __attribute__ ((may_alias)) sfdp_session_timer_t;
3536

3637
#define _(name, val, str) SFDP_TIMEOUT_DECLARE (name);
@@ -104,6 +105,7 @@ sfdp_session_timer_start (sfdp_tw_t *tw, sfdp_session_timer_t *timer, u32 sessio
104105
u32 ticks = SFDP_SECONDS_TO_TICKS (timeout_seconds);
105106
timer->handle = sfdp_timer_start_internal (tw, session_index, 0, ticks);
106107
timer->next_expiration = now + SFDP_TICKS_TO_SECONDS (ticks);
108+
timer->expiry_pending = 0;
107109
}
108110

109111
static_always_inline void
@@ -127,25 +129,18 @@ static_always_inline void
127129
sfdp_session_timer_update_maybe_past (sfdp_tw_t *tw, sfdp_session_timer_t *timer, f64 now,
128130
f64 timeout_seconds)
129131
{
132+
133+
/* Do not update timer for packets marked for expiry */
134+
if (PREDICT_FALSE (timer->expiry_pending))
135+
return;
136+
130137
u32 timeout_ticks = SFDP_SECONDS_TO_TICKS (timeout_seconds);
131138
if (timer->next_expiration > now + SFDP_TICKS_TO_SECONDS (timeout_ticks))
132139
sfdp_timer_update_internal (tw, timer->handle, timeout_ticks);
133140

134141
sfdp_session_timer_update (timer, now, timeout_seconds);
135142
}
136143

137-
static_always_inline void
138-
sfdp_session_timer_update_unlikely_past (sfdp_tw_t *tw, sfdp_session_timer_t *timer, f64 now,
139-
f64 timeout_seconds)
140-
{
141-
u32 timeout_ticks = SFDP_SECONDS_TO_TICKS (timeout_seconds);
142-
if (PREDICT_FALSE (timer->next_expiration > now + SFDP_TICKS_TO_SECONDS (timeout_ticks)))
143-
{
144-
sfdp_timer_update_internal (tw, timer->handle, timeout_ticks);
145-
}
146-
sfdp_session_timer_update (timer, now, timeout_seconds);
147-
}
148-
149144
static_always_inline uword
150145
vec_reset_len_return (u32 *v)
151146
{

‎test/test_sfdp.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,92 @@ def test_sfdp_single_tenant_multiple_tcp_flows(self):
796796

797797
self._cleanup_sfdp()
798798

799+
def test_sfdp_kill_session_batch(self):
800+
"""Test api to kill sfdp sessions in batches"""
801+
self._configure_sfdp()
802+
self.vapi.sfdp_set_timeout(
803+
tenant_id=1,
804+
timeout_id=self.get_timeout_index("embryonic"),
805+
timeout_value=3600,
806+
)
807+
808+
# Create 10 sessions
809+
for sport in range(12346, 12356):
810+
self.send_and_expect(
811+
self.pg0,
812+
self.create_tcp_packet(
813+
src_mac=self.pg0.remote_mac,
814+
dst_mac=self.pg0.local_mac,
815+
src_ip=self.pg0.remote_ip4,
816+
dst_ip=self.pg1.remote_ip4,
817+
sport=sport,
818+
dport=80,
819+
),
820+
self.pg1,
821+
)
822+
823+
# Verify 10 sessions are active
824+
self.assertEqual(len(self.sessions()), 10)
825+
826+
# Expire two consecutive batches of 5 sessions
827+
# can be killed, and verify state is as expected
828+
self.vapi.sfdp_kill_session_batch(max=5)
829+
self.virtual_sleep(2)
830+
self.assertEqual(len(self.sessions()), 5)
831+
832+
self.vapi.sfdp_kill_session_batch(max=5)
833+
self.virtual_sleep(2)
834+
self.assertEqual(len(self.sessions()), 0)
835+
836+
self._cleanup_sfdp()
837+
838+
def test_sfdp_kill_session_batch_consecutive(self):
839+
"""Test api to kill sfdp sessions in consecutive batches"""
840+
self._configure_sfdp()
841+
self.vapi.sfdp_set_timeout(
842+
tenant_id=1,
843+
timeout_id=self.get_timeout_index("embryonic"),
844+
timeout_value=3600,
845+
)
846+
847+
# Create 10 sessions
848+
for sport in range(12346, 12356):
849+
self.send_and_expect(
850+
self.pg0,
851+
self.create_tcp_packet(
852+
src_mac=self.pg0.remote_mac,
853+
dst_mac=self.pg0.local_mac,
854+
src_ip=self.pg0.remote_ip4,
855+
dst_ip=self.pg1.remote_ip4,
856+
sport=sport,
857+
dport=80,
858+
),
859+
self.pg1,
860+
)
861+
862+
# Verify 10 sessions are active
863+
self.assertEqual(len(self.sessions()), 10)
864+
865+
# Disable sfdp expiry node, then stack two requests. The second request
866+
# spends three of its seven inspected-session budget on the first batch.
867+
self.vapi.cli("test sfdp expiry disable")
868+
self.vapi.sfdp_kill_session_batch(max=3)
869+
self.vapi.sfdp_kill_session_batch(max=7)
870+
871+
# Sessions should remain present until we re-enable the expiry node
872+
self.virtual_sleep(2)
873+
self.assertEqual(len(self.sessions()), 10)
874+
875+
# Re-enable expiry; only seven sessions were marked across both requests.
876+
self.vapi.cli("test sfdp expiry enable")
877+
self.virtual_sleep(2)
878+
self.assertEqual(len(self.sessions()), 3)
879+
880+
self.vapi.sfdp_kill_session_batch(max=3)
881+
self.wait_no_sessions()
882+
883+
self._cleanup_sfdp()
884+
799885
def test_sfdp_multiple_tenants_flows(self):
800886
"""Test multiple tenants each handling one flow in parallel"""
801887
# Configure tenant 1 on pg0

‎test/test_sfdp_multiworker.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ def test_no_side_effects_kill_nonexistent(self):
286286
"""
287287
# No sessions exist yet — kill index 0 must fail gracefully
288288
with self.vapi.assert_negative_api_retval():
289-
self.vapi.sfdp_kill_session(session_index=0, is_all=False)
289+
reply = self.vapi.sfdp_kill_session(session_index=0, is_all=False)
290+
self.assertEqual(reply.retval, -6)
290291

291292
# Create a real session and verify the table is still intact
292293
self._send_syns(self._make_syn("198.51.100.2", 30001), worker=0)
@@ -477,6 +478,40 @@ def test_kill_all_sessions_across_two_workers(self):
477478
f"{[(s.thread_index, s.session_idx) for s in remaining]}",
478479
)
479480

481+
def test_kill_session_batch_across_two_workers(self):
482+
"""Test api to kill sfdp sessions in batches with multiple workers"""
483+
# Create four sessions: two across worker 0 and two across worker 1
484+
for worker, sport in ((0, 61001), (0, 61002), (1, 61003), (1, 61004)):
485+
self._send_syns(self._make_syn("198.51.100.2", sport), worker=worker)
486+
self.pg_enable_capture(self.pg_interfaces)
487+
self.pg_start()
488+
489+
# Verify that sessions have been created, with two sessions on each worker thread
490+
sessions = self._sessions()
491+
self.assertEqual(len(sessions), 4)
492+
493+
# Verify that there are two sessions associated with each worker thread
494+
self.assertEqual(sum(s.thread_index == 1 for s in sessions), 2)
495+
self.assertEqual(sum(s.thread_index == 2 for s in sessions), 2)
496+
497+
# Pause expiry so that batching occurs before either worker removes a flow.
498+
self.vapi.cli("test sfdp expiry disable")
499+
self.vapi.sfdp_kill_session_batch(max=3)
500+
self.virtual_sleep(0.2)
501+
self.assertEqual(len(self._sessions()), 4)
502+
503+
# Re-enable expiry, three sessions should have been removed globally across workers
504+
self.vapi.cli("test sfdp expiry enable")
505+
self.virtual_sleep(0.2)
506+
remaining = self._sessions()
507+
self.assertEqual(len(remaining), 1)
508+
509+
# Kill the last remaining session
510+
self.vapi.sfdp_kill_session_batch(max=1)
511+
self.virtual_sleep(0.2)
512+
remaining = self._sessions()
513+
self.assertEqual(len(remaining), 0)
514+
480515
def test_kill_individual_sessions_on_different_workers(self):
481516
"""Killing sessions individually works regardless of owning worker
482517

0 commit comments

Comments
 (0)