From 2cc90f9422a63a4297906b645c2be905114f42b3 Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Fri, 28 Aug 2026 20:37:33 -0400 Subject: [PATCH 1/7] fix(agent): identify an aperiodic subscription by its whole RIC ID e2_async_event_agent() resolved the subscription an aperiodic indication answers with eq_ind_event_ric_req_id, which compares ric_req_id only. ag->ind_event is keyed on the full ric_gen_id_t and can hold several entries sharing a RIC Requestor ID: E2AP identifies a subscription by {RequestorID, InstanceID}, and the O-RAN SC near-RT RIC's subscription manager sets one fixed Requestor ID on every subscription it sends (ricRequestorId = 123, pkg/control/control.go). First match wins, and cmp_ric_gen_id orders ran_func_id descending, so the highest-numbered RAN function under that Requestor ID captures every aperiodic indication. Its service model is then handed another model's ind_data, decodes a foreign struct and aborts the RAN process. Carry ric_gen_id_t end to end (sm_subs_data_t, wr_rc_sub_data_t, the async API, free_aper_subs) and match on it with the existing eq_ind_event. Delete eq_ind_event_ric_req_id, which has no correct use. The aperiodic fd had the same defect. It was derived as -ric_req_id, so two subscriptions sharing a Requestor ID collide on the bi_map key, and Requestor ID 0 yields fd 0, which stop_ind_event() reads back as a real fd. Count down from -1 instead. An indication for a subscription that was just deleted is a race the RAN can lose legitimately, so log and drop it instead of asserting. It is dormant with FlexRIC's own near-RT RIC, which allocates a new Requestor ID per subscription. test/aperiodic_routing/ routes three subscriptions under one Requestor ID and fails on the Requestor-ID-only matching. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- examples/emulator/agent/sm_rc.c | 17 +-- src/agent/e2_agent.c | 45 ++++++-- src/agent/e2_agent.h | 2 +- src/agent/e2_agent_api.c | 4 +- src/agent/e2_agent_api.h | 5 +- src/agent/msg_handler_agent.c | 24 ++-- src/lib/ind_event.c | 12 -- src/lib/ind_event.h | 4 +- src/sm/agent_if/write/sm_ag_if_wr.h | 3 +- src/sm/agent_if/write/subscription_aperiod.h | 6 +- src/sm/rc_sm/rc_sm_agent.c | 4 +- src/sm/sm_proc_data.h | 6 +- test/CMakeLists.txt | 1 + test/agent-ric-xapp/test_ag_ric_xapp.c | 13 ++- test/agent-ric/test_near_ric.c | 13 ++- test/aperiodic_routing/CMakeLists.txt | 22 ++++ .../test_aperiodic_routing.c | 104 ++++++++++++++++++ test/sm/rc_sm/main.c | 4 +- 18 files changed, 229 insertions(+), 60 deletions(-) create mode 100644 test/aperiodic_routing/CMakeLists.txt create mode 100644 test/aperiodic_routing/test_aperiodic_routing.c diff --git a/examples/emulator/agent/sm_rc.c b/examples/emulator/agent/sm_rc.c index 776a6a4d4..8d9d4e8da 100644 --- a/examples/emulator/agent/sm_rc.c +++ b/examples/emulator/agent/sm_rc.c @@ -477,16 +477,16 @@ sm_ag_if_ans_t write_ctrl_rc_sm(void const* data) } static -uint32_t *ric_req_id_list = NULL; +ric_gen_id_t *ric_id_list = NULL; static size_t sz_ric_req_id = 0; static -void free_aperiodic_subscription(uint32_t ric_req_id) +void free_aperiodic_subscription(ric_gen_id_t ric_id) { for (size_t i = 0; i < sz_ric_req_id; i++) { - if (ric_req_id == ric_req_id_list[i]) - (void)ric_req_id_list[i]; + if (eq_ric_gen_id(&ric_id, &ric_id_list[i])) + (void)ric_id_list[i]; } } @@ -502,8 +502,9 @@ void* emulate_ric_ind(void* ptr) d->hdr = fill_rnd_rc_ind_hdr(); d->msg = fill_rnd_rc_ind_msg(); - async_event_agent_api(ric_req_id_list[j], d); - printf("Event for RIC Req ID %u generated\n", ric_req_id_list[j]); + async_event_agent_api(ric_id_list[j], d); + printf("Event for RAN_FUNC_ID %d RIC_REQ_ID %d INST_ID %d generated\n", + ric_id_list[j].ran_func_id, ric_id_list[j].ric_req_id, ric_id_list[j].ric_inst_id); } } @@ -519,8 +520,8 @@ sm_ag_if_ans_t write_subs_rc_sm(void const* src) wr_rc_sub_data_t* wr_rc = (wr_rc_sub_data_t*)src; - ric_req_id_list = realloc(ric_req_id_list, (sz_ric_req_id + 1) * sizeof(uint32_t)); - (ric_req_id_list)[sz_ric_req_id] = wr_rc->ric_req_id; + ric_id_list = realloc(ric_id_list, (sz_ric_req_id + 1) * sizeof(ric_gen_id_t)); + (ric_id_list)[sz_ric_req_id] = wr_rc->ric_id; (sz_ric_req_id)++; int rc = pthread_create(&t_ran_ctrl, NULL, emulate_ric_ind, NULL); diff --git a/src/agent/e2_agent.c b/src/agent/e2_agent.c index 59a4780b4..2ff3a0d01 100644 --- a/src/agent/e2_agent.c +++ b/src/agent/e2_agent.c @@ -503,7 +503,31 @@ void e2_free_agent(e2_agent_t* ag) free(ag); } -void e2_async_event_agent(e2_agent_t* ag, uint32_t ric_req_id, void* ind_data) +// Nothing is subscribed under this RIC ID. Discard the payload rather than abort: +// the RAN owns no free function for it, so hand it to the RAN function's own SM, +// which frees it on the way out of on_indication, and throw the encoded buffers away. +static +void drop_orphan_aind(e2_agent_t* ag, ric_gen_id_t ric_id, void* ind_data) +{ + printf("[E2 AGENT]: no subscription for RAN_FUNC_ID %d RIC_REQ_ID %d INST_ID %d, dropping indication\n", + ric_id.ran_func_id, ric_id.ric_req_id, ric_id.ric_inst_id); + + sm_agent_t* sm = sm_plugin_ag(&ag->plugin, ric_id.ran_func_id); + if(sm == NULL){ + printf("[E2 AGENT]: RAN_FUNC_ID %d has no service model either, leaking the indication data\n", + ric_id.ran_func_id); + return; + } + + exp_ind_data_t exp = sm->proc.on_indication(sm, ind_data); + if(exp.has_value == true){ + free(exp.data.ind_hdr); + free(exp.data.ind_msg); + free(exp.data.call_process_id); + } +} + +void e2_async_event_agent(e2_agent_t* ag, ric_gen_id_t ric_id, void* ind_data) { assert(ag != NULL); @@ -511,10 +535,12 @@ void e2_async_event_agent(e2_agent_t* ag, uint32_t ric_req_id, void* ind_data) void* l = NULL; void* it = NULL; - ind_event_t tmp = {.ric_id.ric_req_id = ric_req_id, - .sm = NULL, - .action_id = 0 }; - + // Match the whole RIC ID. The Requestor ID alone does not identify a subscription + // -- E2AP identifies one by {RequestorID, InstanceID}, and an xApp may reuse a + // Requestor ID across RAN functions -- so ag->ind_event legitimately holds several + // entries sharing it. Keying on it alone hands the indication to whichever entry + // cmp_ric_gen_id happens to order first, and a service model that is given another + // one's ind_data aborts the RAN process. assoc_rb_tree_t* tree = &ag->ind_event.right; for(size_t i =0; i < 10; ++i){ @@ -523,7 +549,7 @@ void e2_async_event_agent(e2_agent_t* ag, uint32_t ric_req_id, void* ind_data) f = assoc_rb_tree_front(tree); l = assoc_rb_tree_end(tree); - it = find_if_rb_tree(tree, f, l, &tmp, eq_ind_event_ric_req_id); + it = find_if_rb_tree(tree, f, l, &ric_id, eq_ind_event); if(it != l) break; rc = pthread_mutex_unlock(&ag->mtx_ind_event); @@ -534,7 +560,12 @@ void e2_async_event_agent(e2_agent_t* ag, uint32_t ric_req_id, void* ind_data) usleep(10); } - assert(it != l && "Not found RIC Request ID"); + // The loop breaks while still holding the lock on success and releases it at the + // end of every failing iteration, so here the mutex is already unlocked. + if(it == l){ + drop_orphan_aind(ag, ric_id, ind_data); + return; + } ind_event_t* ind_ev = assoc_rb_tree_key(tree, it); diff --git a/src/agent/e2_agent.h b/src/agent/e2_agent.h index fed1209c9..d26e6f48d 100644 --- a/src/agent/e2_agent.h +++ b/src/agent/e2_agent.h @@ -77,7 +77,7 @@ void e2_start_agent(e2_agent_t* ag); void e2_free_agent(e2_agent_t* ag); -void e2_async_event_agent(e2_agent_t* ag, uint32_t ric_req_id, void* ind_data); +void e2_async_event_agent(e2_agent_t* ag, ric_gen_id_t ric_id, void* ind_data); /////////////////////////////////////////////// // E2AP AGENT FUNCTIONAL PROCEDURES MESSAGES // diff --git a/src/agent/e2_agent_api.c b/src/agent/e2_agent_api.c index f5d746aff..3bd169d4f 100644 --- a/src/agent/e2_agent_api.c +++ b/src/agent/e2_agent_api.c @@ -106,9 +106,9 @@ void stop_agent_api(void) assert(rc == 0); } -void async_event_agent_api(uint32_t ric_req_id, void* ind_data) +void async_event_agent_api(ric_gen_id_t ric_id, void* ind_data) { assert(agent != NULL); - e2_async_event_agent(agent, ric_req_id, ind_data); + e2_async_event_agent(agent, ric_id, ind_data); } diff --git a/src/agent/e2_agent_api.h b/src/agent/e2_agent_api.h index 32622248b..9e706fc1e 100644 --- a/src/agent/e2_agent_api.h +++ b/src/agent/e2_agent_api.h @@ -5,6 +5,7 @@ #ifndef E2_AGENT_API_MOSAIC_H #define E2_AGENT_API_MOSAIC_H +#include "../lib/e2ap/ric_gen_id_wrapper.h" #include "../sm/sm_io.h" #include "../util/conf_file.h" #include "../util/ngran_types.h" @@ -20,7 +21,9 @@ void init_agent_api(int mcc, void stop_agent_api(void); -void async_event_agent_api(uint32_t ric_req_id, void* ind_data); +// ric_id must be the full RIC ID of the subscription the indication answers: +// the Requestor ID alone does not identify one (see e2_async_event_agent). +void async_event_agent_api(ric_gen_id_t ric_id, void* ind_data); #endif diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 52570e3f0..0762a96b7 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -11,6 +11,7 @@ #include "util/compare.h" #include +#include #include static @@ -32,6 +33,10 @@ bool check_valid_msg_type(e2_msg_type_t msg_type ) || msg_type == E2_CONNECTION_UPDATE; } +// Aperiodic subscriptions have no real fd; they are keyed by a negative one so +// stop_ind_event() can tell them apart from the timer fds of periodic ones. +static _Atomic int next_aperiodic_fd = -1; + static bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) { @@ -54,7 +59,7 @@ bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) if (ind_ev->sm->free_act_def != NULL) ind_ev->sm->free_act_def(ind_ev->sm, ind_ev->act_def); } else if(ind_ev->type == APERIODIC_SUBSCRIPTION_FLRC) { - ind_ev->free_subs_aperiodic(id.ric_req_id); + ind_ev->free_subs_aperiodic(id); } else { assert(0!=0 && "Unknown subscritpion type"); } @@ -114,7 +119,7 @@ sm_subs_data_t generate_sm_subs_data(ric_subscription_request_t const* sr) assert(sr != NULL); sm_subs_data_t data = { .event_trigger = sr->event_trigger.buf, .len_et = sr->event_trigger.len, - .ric_req_id = sr->ric_id.ric_req_id }; + .ric_id = sr->ric_id }; if(sr->action->definition != NULL){ data.action_def = sr->action->definition->buf; @@ -178,20 +183,25 @@ e2ap_msg_t e2ap_handle_subscription_request_agent(e2_agent_t* ag, const e2ap_msg } else if(ev.type == APERIODIC_SUBSCRIPTION_FLRC){ ev.free_subs_aperiodic = subs.aper.free_aper_subs; // Aperiodic indication generated i.e., the RAN will generate it via - // void async_event_agent_api(uint32_t ric_req_id, void* ind_data); + // void async_event_agent_api(ric_gen_id_t ric_id, void* ind_data); // however, the fd has to be unique per subscription (e.g. two xApp running in parallel // can subscribe to the same parameters, but when deleting one via bi_map_extract_right() - // it would not find the correct one due to cmp_fd() if not different) - int fd = -ev.ric_id.ric_req_id; + // it would not find the correct one due to cmp_fd() if not different). + // The Requestor ID is not that: E2AP identifies a subscription by + // {RequestorID, InstanceID}, so two subscriptions sharing one -- which is what the + // OSC near-RT RIC hands out -- would collide on this key. -ric_req_id also yields 0 + // for Requestor ID 0, which reads back as a real fd. Count down instead. + int fd = atomic_fetch_sub(&next_aperiodic_fd, 1); + assert(fd < 0 && "Too many aperiodic subscriptions"); { lock_guard(&ag->mtx_ind_event); bi_map_insert(&ag->ind_event, &fd, sizeof(int), &ev, sizeof(ev)); } // Some report styles require sending RIC Indication out of on_subscription, - // but can only be sent when ric_req_id is registered. + // but can only be sent when the subscription is registered. if(subs.aper.imm_ind_data != NULL) - e2_async_event_agent(ag, ev.ric_id.ric_req_id, subs.aper.imm_ind_data); + e2_async_event_agent(ag, ev.ric_id, subs.aper.imm_ind_data); } else { assert(0!=0 && "Unknown subscritpion type"); } diff --git a/src/lib/ind_event.c b/src/lib/ind_event.c index c1d9a008f..2e8e3a99c 100644 --- a/src/lib/ind_event.c +++ b/src/lib/ind_event.c @@ -27,18 +27,6 @@ int cmp_ind_event(void const* m0_v, void const* m1_v) return cmp; } -bool eq_ind_event_ric_req_id(const void* value, const void* key) -{ - assert(value != NULL); - assert(key != NULL); - - uint32_t* ric_id = (uint32_t*)value; - ind_event_t* ind_ev = (ind_event_t*)key; - bool eq = (*ric_id == ind_ev->ric_id.ric_req_id); - return eq; -} - - bool eq_ind_event(const void* value, const void* key) { assert(value != NULL); diff --git a/src/lib/ind_event.h b/src/lib/ind_event.h index 40c88b6f8..755a5b453 100644 --- a/src/lib/ind_event.h +++ b/src/lib/ind_event.h @@ -26,15 +26,13 @@ typedef struct{ void* act_def; // i.e., kpm_act_def_t // Free function to call for aperiodic events - void (*free_subs_aperiodic)(uint32_t ric_req_id); + void (*free_subs_aperiodic)(ric_gen_id_t ric_id); }; } ind_event_t; int cmp_ind_event(void const* m0_v, void const* m1_v); -bool eq_ind_event_ric_req_id(const void* value, const void* key); - bool eq_ind_event(const void* value, const void* key); // void free_ind_event(ind_event_t* src); diff --git a/src/sm/agent_if/write/sm_ag_if_wr.h b/src/sm/agent_if/write/sm_ag_if_wr.h index 91fd174e4..b84ef6b32 100644 --- a/src/sm/agent_if/write/sm_ag_if_wr.h +++ b/src/sm/agent_if/write/sm_ag_if_wr.h @@ -16,6 +16,7 @@ #include "../../kpm_sm/kpm_data_ie_wrapper.h" #include "../../rc_sm/ie/rc_data_ie.h" #include "../../ccc_sm/ie/ccc_data_ie.h" +#include "../../../lib/e2ap/ric_gen_id_wrapper.h" ////////////////////// ////////////////////// @@ -71,7 +72,7 @@ typedef enum{ } sm_ag_if_subs_e; typedef struct{ - uint32_t ric_req_id; + ric_gen_id_t ric_id; rc_sub_data_t rc; } wr_rc_sub_data_t; diff --git a/src/sm/agent_if/write/subscription_aperiod.h b/src/sm/agent_if/write/subscription_aperiod.h index 04a232413..95d3ca9fe 100644 --- a/src/sm/agent_if/write/subscription_aperiod.h +++ b/src/sm/agent_if/write/subscription_aperiod.h @@ -5,10 +5,14 @@ #ifndef SUBSCRIPTION_APERIODIC_MIR_H #define SUBSCRIPTION_APERIODIC_MIR_H +#include "../../../lib/e2ap/ric_gen_id_wrapper.h" + #include typedef struct { - void (*free_aper_subs)(uint32_t ric_req_id); + // Takes the whole RIC ID: the Requestor ID alone does not identify the + // subscription being released. + void (*free_aper_subs)(ric_gen_id_t ric_id); // Optional indication data (e.g., rc_ind_data_t*) to send immediately once // the subscription is fully registered with the agent, for report styles diff --git a/src/sm/rc_sm/rc_sm_agent.c b/src/sm/rc_sm/rc_sm_agent.c index 79e9ff90a..65077f4ed 100644 --- a/src/sm/rc_sm/rc_sm_agent.c +++ b/src/sm/rc_sm/rc_sm_agent.c @@ -58,10 +58,10 @@ sm_ag_if_ans_subs_t on_subscription_rc_sm_ag(sm_agent_t const* sm_agent, const s //sm_ag_if_wr_t wr = {.type = SUBSCRIPTION_SM_AG_IF_WR}; //wr.subs.type = RAN_CTRL_SUBS_V1_03; - //wr.subs.wr_rc.ric_req_id = data->ric_req_id; + //wr.subs.wr_rc.ric_id = data->ric_id; wr_rc_sub_data_t wr_rc = {0}; - wr_rc.ric_req_id = data->ric_req_id; + wr_rc.ric_id = data->ric_id; wr_rc.rc.et = rc_dec_event_trigger(&sm->enc, data->len_et, data->event_trigger); defer({ free_e2sm_rc_event_trigger(&wr_rc.rc.et); }); diff --git a/src/sm/sm_proc_data.h b/src/sm/sm_proc_data.h index 11a076486..f6cb67c52 100644 --- a/src/sm/sm_proc_data.h +++ b/src/sm/sm_proc_data.h @@ -6,6 +6,7 @@ #define SM_PROCEDURES_DATA_H #include "../lib/e2ap/e2ap_ran_function_wrapper.h" +#include "../lib/e2ap/ric_gen_id_wrapper.h" #include #include @@ -24,7 +25,10 @@ typedef struct{ - uint32_t ric_req_id; + // Identifies the subscription. The RIC Requestor ID alone does not: E2AP + // identifies a subscription by {RequestorID, InstanceID} and nothing stops an + // xApp from reusing one Requestor ID across RAN functions. + ric_gen_id_t ric_id; uint8_t* event_trigger; size_t len_et; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f9843facc..ff92bbcf6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(agent-ric-xapp) add_subdirectory(agent-ric) +add_subdirectory(aperiodic_routing) add_subdirectory(encode_decode) add_subdirectory(sm) enable_testing() diff --git a/test/agent-ric-xapp/test_ag_ric_xapp.c b/test/agent-ric-xapp/test_ag_ric_xapp.c index 6e09f3eec..481f7ed45 100644 --- a/test/agent-ric-xapp/test_ag_ric_xapp.c +++ b/test/agent-ric-xapp/test_ag_ric_xapp.c @@ -174,13 +174,13 @@ sm_ag_if_ans_t write_ctrl_rc(void const* ctrl) } static -uint32_t sta_ric_id; +ric_gen_id_t sta_ric_id; static -void free_aperiodic_subscription(uint32_t ric_req_id) +void free_aperiodic_subscription(ric_gen_id_t ric_id) { - assert(ric_req_id == sta_ric_id); - (void)ric_req_id; + assert(eq_ric_gen_id(&ric_id, &sta_ric_id)); + (void)ric_id; } static @@ -193,7 +193,8 @@ void* emulate_rrc_msg(void* ptr) assert(d != NULL && "Memory exhausted"); *d = fill_rnd_rc_ind_data(); async_event_agent_api(sta_ric_id, d); - printf("Event for RIC Req ID %u generated\n", sta_ric_id); + printf("Event for RAN_FUNC_ID %d RIC_REQ_ID %d INST_ID %d generated\n", + sta_ric_id.ran_func_id, sta_ric_id.ric_req_id, sta_ric_id.ric_inst_id); } return NULL; @@ -208,7 +209,7 @@ sm_ag_if_ans_t write_subs_rc(void const* data) assert(data != NULL); wr_rc_sub_data_t const* wr_rc = (wr_rc_sub_data_t const*)data; - sta_ric_id = wr_rc->ric_req_id; + sta_ric_id = wr_rc->ric_id; int rc = pthread_create(&t, NULL, emulate_rrc_msg, NULL); assert(rc == 0); diff --git a/test/agent-ric/test_near_ric.c b/test/agent-ric/test_near_ric.c index afcaadb4c..25c9a589c 100644 --- a/test/agent-ric/test_near_ric.c +++ b/test/agent-ric/test_near_ric.c @@ -147,13 +147,13 @@ sm_ag_if_ans_t write_ctrl_rc(void const* ctrl) } static -uint32_t sta_ric_id; +ric_gen_id_t sta_ric_id; static -void free_aperiodic_subscription(uint32_t ric_req_id) +void free_aperiodic_subscription(ric_gen_id_t ric_id) { - assert(ric_req_id == sta_ric_id); - (void)ric_req_id; + assert(eq_ric_gen_id(&ric_id, &sta_ric_id)); + (void)ric_id; } static @@ -166,7 +166,8 @@ void* emulate_rrc_msg(void* ptr) assert(d != NULL && "Memory exhausted"); *d = fill_rnd_rc_ind_data(); async_event_agent_api(sta_ric_id, d); - printf("Event for RIC Req ID %u generated\n", sta_ric_id); + printf("Event for RAN_FUNC_ID %d RIC_REQ_ID %d INST_ID %d generated\n", + sta_ric_id.ran_func_id, sta_ric_id.ric_req_id, sta_ric_id.ric_inst_id); } return NULL; @@ -178,7 +179,7 @@ sm_ag_if_ans_t write_subs_rc(void const* data) assert(data != NULL); wr_rc_sub_data_t const* wr_rc = (wr_rc_sub_data_t const*)data; - sta_ric_id = wr_rc->ric_req_id; + sta_ric_id = wr_rc->ric_id; int rc = pthread_create(&t, NULL, emulate_rrc_msg, NULL); assert(rc == 0); diff --git a/test/aperiodic_routing/CMakeLists.txt b/test/aperiodic_routing/CMakeLists.txt new file mode 100644 index 000000000..b9b5028b4 --- /dev/null +++ b/test/aperiodic_routing/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: LicenseRef-CSSL-1.0 + +##################################### +# Unit test the routing of aperiodic indications +##################################### + +add_executable(test_aperiodic_routing + test_aperiodic_routing.c + ) + +target_link_libraries(test_aperiodic_routing + PUBLIC + e2_agent + -pthread + -lsctp + -ldl) + +# e2_agent sets these PRIVATE, and e2_agent_t's layout depends on them. +target_compile_definitions(test_aperiodic_routing PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) + +enable_testing() +add_test(Unit_test_aperiodic_routing test_aperiodic_routing) diff --git a/test/aperiodic_routing/test_aperiodic_routing.c b/test/aperiodic_routing/test_aperiodic_routing.c new file mode 100644 index 000000000..59b93e9b1 --- /dev/null +++ b/test/aperiodic_routing/test_aperiodic_routing.c @@ -0,0 +1,104 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * An aperiodic indication was routed by its RIC Requestor ID alone. E2AP + * identifies a subscription by {RequestorID, InstanceID}, so the agent can hold + * several subscriptions under one Requestor ID, and the O-RAN SC near-RT RIC + * sends every subscription under the same one. The first entry in the tree won, + * the highest RAN function ID, and the service model behind it was handed the + * indication data of another one. + */ + +#undef NDEBUG + +#include "agent/e2_agent.h" +#include "lib/aind_event.h" +#include "lib/ind_event.h" +#include "util/compare.h" + +#include +#include +#include +#include +#include + +// As the agent frees the copies bi_map_insert() makes +static +void free_ind_event(void* key, void* value) +{ + (void)key; + free(value); +} + +static +void free_fd(void* key, void* value) +{ + (void)key; + free(value); +} + +static +void add_aperiodic_subscription(e2_agent_t* ag, int fd, ric_gen_id_t ric_id, sm_agent_t* sm) +{ + ind_event_t ev = {.ric_id = ric_id, .sm = sm, .action_id = 1, .type = APERIODIC_SUBSCRIPTION_FLRC}; + bi_map_insert(&ag->ind_event, &fd, sizeof(fd), &ev, sizeof(ev)); +} + +static +void expect_routed_to(e2_agent_t* ag, ric_gen_id_t ric_id, sm_agent_t* sm, void* ind_data) +{ + e2_async_event_agent(ag, ric_id, ind_data); + + assert(size_tsq(&ag->aind) == 1); + aind_event_t aind = {0}; + pop_tsq(&ag->aind, &aind, sizeof(aind)); + + assert(aind.sm == sm); + assert(aind.ind_data == ind_data); + assert(aind.ric_id.ran_func_id == ric_id.ran_func_id); + assert(aind.ric_id.ric_req_id == ric_id.ric_req_id); + assert(aind.ric_id.ric_inst_id == ric_id.ric_inst_id); +} + +int main() +{ + e2_agent_t* ag = calloc(1, sizeof(*ag)); + assert(ag != NULL); + + int rc = pthread_mutex_init(&ag->mtx_ind_event, NULL); + assert(rc == 0); + init_asio_agent(&ag->io); + init_tsq(&ag->aind, sizeof(aind_event_t)); + bi_map_init(&ag->ind_event, sizeof(int), sizeof(ind_event_t), cmp_fd, cmp_ind_event, free_ind_event, free_fd); + + // Same Requestor ID, three subscriptions: two RAN functions, and one of them twice + uint32_t const req_id = 123; + ric_gen_id_t const rc_id = {.ric_req_id = req_id, .ric_inst_id = 0, .ran_func_id = 3}; + ric_gen_id_t const mac_id = {.ric_req_id = req_id, .ric_inst_id = 0, .ran_func_id = 142}; + ric_gen_id_t const mac_id_inst1 = {.ric_req_id = req_id, .ric_inst_id = 1, .ran_func_id = 142}; + + sm_agent_t sm_rc = {0}; + sm_agent_t sm_mac = {0}; + sm_agent_t sm_mac_inst1 = {0}; + + // Distinct, negative fds as the agent hands them out + add_aperiodic_subscription(ag, -1, rc_id, &sm_rc); + add_aperiodic_subscription(ag, -2, mac_id, &sm_mac); + add_aperiodic_subscription(ag, -3, mac_id_inst1, &sm_mac_inst1); + + int data = 0; + expect_routed_to(ag, rc_id, &sm_rc, &data); + expect_routed_to(ag, mac_id, &sm_mac, &data); + expect_routed_to(ag, mac_id_inst1, &sm_mac_inst1, &data); + + bi_map_free(&ag->ind_event); + free_tsq(&ag->aind, NULL); + rc = pthread_mutex_destroy(&ag->mtx_ind_event); + assert(rc == 0); + free(ag); + + printf("[TEST]: aperiodic indications reach the subscription with the whole RIC ID\n"); + return 0; +} diff --git a/test/sm/rc_sm/main.c b/test/sm/rc_sm/main.c index 31dc8619b..db2dc464a 100644 --- a/test/sm/rc_sm/main.c +++ b/test/sm/rc_sm/main.c @@ -93,9 +93,9 @@ sm_ag_if_ans_t write_ctrl_rc(void const* data) } static -void free_aperiodic_subscription(uint32_t ric_req_id) +void free_aperiodic_subscription(ric_gen_id_t ric_id) { - (void)ric_req_id; + (void)ric_id; } static From f1fcfc184b412c3772d3198f0c6f26014ba5804f Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Tue, 15 Sep 2026 12:39:22 -0400 Subject: [PATCH 2/7] fix(agent): key a subscription on its fd, not only its RIC ID ag->ind_event is a multiset: assoc_rb_tree_insert does not reject equality and cmp_ind_event compared RIC IDs only, so two subscriptions can sit under one ric_gen_id_t. A near-RT RIC produces that by installing a subscription the agent still holds, or by reusing one Requestor ID, as the O-RAN SC one does. A delete then corrupted the registry. stop_ind_event() found its entry with a linear scan (find_if_rb_tree) but unlinked with a binary search on the same ambiguous key (bi_map_extract_right), and those are not the same node. One entry's act_def was freed while another was unlinked, so the survivor kept an armed timer and a freed act_def, which the next tick handed to the service model. The fd already identifies a subscription: a timerfd when periodic, a negative synthetic one when aperiodic. Record it in ind_event_t, break ties on it in cmp_ind_event so the tree is a total order, and extract by it with bi_map_extract_left. eq_ind_event still matches on the RIC ID alone, which is what lets e2_async_event_agent() find a subscription without knowing its fd. test/ind_event_key/ installs three subscriptions under one RIC ID and deletes them; on the previous keying one act_def is freed twice. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- src/agent/msg_handler_agent.c | 21 +++-- src/lib/ind_event.c | 14 ++-- src/lib/ind_event.h | 6 ++ test/CMakeLists.txt | 1 + test/ind_event_key/CMakeLists.txt | 22 ++++++ test/ind_event_key/test_ind_event_key.c | 100 ++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 test/ind_event_key/CMakeLists.txt create mode 100644 test/ind_event_key/test_ind_event_key.c diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 0762a96b7..ede1df651 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -64,11 +64,18 @@ bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) assert(0!=0 && "Unknown subscritpion type"); } - void (*free_ind_event)(void*) = NULL; - int* fd = bi_map_extract_right(&ag->ind_event, &tmp, sizeof(tmp), free_ind_event); - if (*fd > -1) - rm_fd_asio_agent(&ag->io, *fd); - free(fd); + // Extract by the fd, i.e. the left key: it is unique, whereas several entries + // can share one RIC ID. Extracting by the right key let assoc_extract's binary + // search unlink a different entry than the linear scan above had found, which + // freed one entry's act_def and left the other live with a freed pointer and an + // armed timer. + int const fd = ind_ev->fd; + ind_event_t* val = bi_map_extract_left(&ag->ind_event, (int*)&fd, sizeof(fd), NULL); + assert(val != NULL); + free(val); + + if (fd > -1) + rm_fd_asio_agent(&ag->io, fd); return true;; } @@ -178,6 +185,9 @@ e2ap_msg_t e2ap_handle_subscription_request_agent(e2_agent_t* ag, const e2ap_msg // Periodic indication message generated i.e., every 5 ms assert(t.ms < 10001 && "Subscription for granularity larger than 10 seconds requested? "); int fd_timer = create_timer_ms_asio_agent(&ag->io, t.ms, t.ms); + // Must precede the insert: cmp_ind_event tie-breaks on it, so the right + // tree's ordering is wrong for any entry inserted with fd unset. + ev.fd = fd_timer; lock_guard(&ag->mtx_ind_event); bi_map_insert(&ag->ind_event, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); } else if(ev.type == APERIODIC_SUBSCRIPTION_FLRC){ @@ -193,6 +203,7 @@ e2ap_msg_t e2ap_handle_subscription_request_agent(e2_agent_t* ag, const e2ap_msg // for Requestor ID 0, which reads back as a real fd. Count down instead. int fd = atomic_fetch_sub(&next_aperiodic_fd, 1); assert(fd < 0 && "Too many aperiodic subscriptions"); + ev.fd = fd; { lock_guard(&ag->mtx_ind_event); bi_map_insert(&ag->ind_event, &fd, sizeof(int), &ev, sizeof(ev)); diff --git a/src/lib/ind_event.c b/src/lib/ind_event.c index 2e8e3a99c..08cc40817 100644 --- a/src/lib/ind_event.c +++ b/src/lib/ind_event.c @@ -19,12 +19,14 @@ int cmp_ind_event(void const* m0_v, void const* m1_v) if(cmp != 0) return cmp; - // It is foreseen that not all the struct is - // scanned. This is basically a bad trick, - // but works as expected and "simplifies?" - // the code - - return cmp; + // A RIC ID alone is not a key: assoc_rb_tree_insert does not reject equality, + // so two subscriptions can share one. Break the tie on the fd, unique per + // subscription, to make this a total order and every extraction by key exact. + // Same inverted convention as cmp_ric_gen_id above: 1 means "less than". + if(m0->fd < m1->fd) return 1; + if(m0->fd > m1->fd) return -1; + + return 0; } bool eq_ind_event(const void* value, const void* key) diff --git a/src/lib/ind_event.h b/src/lib/ind_event.h index 755a5b453..3dd27ee2e 100644 --- a/src/lib/ind_event.h +++ b/src/lib/ind_event.h @@ -13,6 +13,12 @@ typedef struct{ ric_gen_id_t ric_id; + + // The asio fd this subscription is keyed by in e2_agent_t::ind_event: a timerfd + // when periodic, a negative synthetic one when aperiodic. Unique per + // subscription, it is the tie-breaker in cmp_ind_event. + int fd; + // Non-owning ptr sm_agent_t* sm; uint8_t action_id; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ff92bbcf6..b37f9a1f6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(agent-ric-xapp) add_subdirectory(agent-ric) +add_subdirectory(ind_event_key) add_subdirectory(aperiodic_routing) add_subdirectory(encode_decode) add_subdirectory(sm) diff --git a/test/ind_event_key/CMakeLists.txt b/test/ind_event_key/CMakeLists.txt new file mode 100644 index 000000000..766a20383 --- /dev/null +++ b/test/ind_event_key/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: LicenseRef-CSSL-1.0 + +##################################### +# Unit test the subscription registry of the E2 agent +##################################### + +add_executable(test_ind_event_key + test_ind_event_key.c + ) + +target_link_libraries(test_ind_event_key + PUBLIC + e2_agent + -pthread + -lsctp + -ldl) + +# e2_agent sets these PRIVATE, and e2_agent_t's layout depends on them. +target_compile_definitions(test_ind_event_key PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) + +enable_testing() +add_test(Unit_test_ind_event_key test_ind_event_key) diff --git a/test/ind_event_key/test_ind_event_key.c b/test/ind_event_key/test_ind_event_key.c new file mode 100644 index 000000000..acb2e9964 --- /dev/null +++ b/test/ind_event_key/test_ind_event_key.c @@ -0,0 +1,100 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * The agent's subscription registry is a multiset: assoc_rb_tree_insert does not + * reject equality and cmp_ind_event compared RIC IDs only, so a RIC that installs + * a subscription the agent already holds, or one that reuses a single RIC + * Requestor ID as the O-RAN SC near-RT RIC does, leaves several entries under one + * RIC ID. + * + * A delete found its entry with a linear scan but unlinked it with a binary + * search on the same ambiguous key, which is not the same node. One entry's + * action definition was freed while another was unlinked, leaving a live entry + * with an armed timer pointing at freed memory. + */ + +#undef NDEBUG + +#include "agent/e2_agent.h" +#include "agent/msg_handler_agent.h" +#include "lib/ind_event.h" +#include "util/compare.h" + +#include +#include +#include +#include + +#define NUM_SUBS 3 + +static int act_def[NUM_SUBS]; +static int freed[NUM_SUBS]; + +static +void free_act_def_stub(sm_agent_t* sm, void* ad) +{ + (void)sm; + for(int i = 0; i < NUM_SUBS; ++i){ + if(ad == &act_def[i]){ + // freeing one twice is the bug + assert(freed[i] == 0); + freed[i] += 1; + return; + } + } + assert(0 && "freeing an action definition that is not one of ours"); +} + +static +void free_nop(void* key, void* value) +{ + (void)key; + (void)value; +} + +int main() +{ + e2_agent_t* ag = calloc(1, sizeof(*ag)); + assert(ag != NULL); + + int rc = pthread_mutex_init(&ag->mtx_ind_event, NULL); + assert(rc == 0); + init_asio_agent(&ag->io); + bi_map_init(&ag->ind_event, sizeof(int), sizeof(ind_event_t), cmp_fd, cmp_ind_event, free_nop, free_nop); + + sm_agent_t sm = {0}; + sm.free_act_def = free_act_def_stub; + + // The same RIC ID, installed three times + ric_gen_id_t const ric_id = {.ric_req_id = 123, .ric_inst_id = 0, .ran_func_id = 3}; + + for(int i = 0; i < NUM_SUBS; ++i){ + int const fd = create_timer_ms_asio_agent(&ag->io, 1000, 1000); + ind_event_t ev = {.ric_id = ric_id, .sm = &sm, .action_id = 1, + .type = PERIODIC_SUBSCRIPTION_FLRC, .act_def = &act_def[i], .fd = fd}; + bi_map_insert(&ag->ind_event, &fd, sizeof(fd), &ev, sizeof(ev)); + } + assert(assoc_rb_tree_size(&ag->ind_event.left) == NUM_SUBS); + assert(assoc_rb_tree_size(&ag->ind_event.right) == NUM_SUBS); + + e2ap_msg_t msg = {.type = RIC_SUBSCRIPTION_DELETE_REQUEST}; + msg.u_msgs.ric_sub_del_req.ric_id = ric_id; + + for(int n = 1; n <= NUM_SUBS; ++n){ + e2ap_handle_subscription_delete_request_agent(ag, &msg); + + // One entry gone, in both trees, and its action definition freed once + assert(assoc_rb_tree_size(&ag->ind_event.left) == (size_t)(NUM_SUBS - n)); + assert(assoc_rb_tree_size(&ag->ind_event.right) == (size_t)(NUM_SUBS - n)); + + int num_freed = 0; + for(int i = 0; i < NUM_SUBS; ++i) + num_freed += freed[i]; + assert(num_freed == n); + } + + printf("[TEST]: each delete removes the entry whose action definition it frees\n"); + return 0; +} From 1efa3ede7e24bb114d8047d01671c7900e4ec833 Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Sun, 20 Sep 2026 15:14:44 -0400 Subject: [PATCH 3/7] fix(agent): ignore a duplicate or unsolicited E2 SETUP RESPONSE A second E2 SETUP RESPONSE aborts the RAN process: assoc_rb_tree.c:457: assoc_rb_tree_extract: Assertion `z_node != tree->dummy && "Trying to extract a key not found in the tree"' failed. stop_pending_event() calls bi_map_extract_right(), which asserts that the key is present. The first response removes the pending timer entry; a second one tries to remove it again and aborts. A second assert sits right behind it, since the first response zeroes trans_id_setup_req. Both assert on what a peer sent rather than on an internal invariant, so a RIC that answers a setup twice takes down the RAN. stop_pending_event() now probes before extracting and reports whether it found anything, and the handler ignores the message when no request is outstanding. The probe mirrors stop_ind_event(), which has always checked it_r != end_r. The transaction ids are still reported and not compared: not every RIC echoes the id it was sent, and rejecting the mismatch would drop the only response such a peer sends. test/agent/ reproduces the abort on the unpatched handler. Built and tested for E2AP_V1/KPM_V2_01, E2AP_V2/KPM_V2_03 and E2AP_V3/KPM_V3_00. Assisted-by: Claude:claude-opus-5 Signed-off-by: Andrea Lacava (cherry picked from commit eebef0e526824b08604744670dbd5f892053c06a) Signed-off-by: Andrea Lacava --- src/agent/msg_handler_agent.c | 41 +++++++- test/CMakeLists.txt | 1 + test/agent/CMakeLists.txt | 22 +++++ test/agent/test_setup_response.c | 156 +++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 test/agent/CMakeLists.txt create mode 100644 test/agent/test_setup_response.c diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index ede1df651..df71c3fa1 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -319,18 +319,41 @@ e2ap_msg_t e2ap_handle_error_indication_agent(e2_agent_t* ag, const e2ap_msg_t* return ans; } +static inline +bool eq_pending_event(const void* m0_v, const void* m1_v) +{ + assert(m0_v != NULL); + assert(m1_v != NULL); + + pending_event_t const* m0 = (pending_event_t const*)m0_v; + pending_event_t const* m1 = (pending_event_t const*)m1_v; + + return *m0 == *m1; +} + static -void stop_pending_event(e2_agent_t* ag, pending_event_t event) +bool stop_pending_event(e2_agent_t* ag, pending_event_t event) { assert(ag != NULL); + /* bi_map_extract_right() asserts that the key is in the tree, so probe first. + * A timer that is already stopped is a recoverable condition, not a bug. Same + * shape as stop_ind_event() above. */ + void* start_r = assoc_rb_tree_front(&ag->pending.right); + void* end_r = assoc_rb_tree_end(&ag->pending.right); + void* it_r = find_if_rb_tree(&ag->pending.right, start_r, end_r, &event, eq_pending_event); + if(it_r == end_r) + return false; + void (*free_pending_event)(void*)=NULL; int* fd = bi_map_extract_right(&ag->pending, &event, sizeof(event), free_pending_event); + assert(fd != NULL); assert(*fd > 0); //printf("[E2-AGENT]: stopping pending\n"); //event = %d \n", *fd); rm_fd_asio_agent(&ag->io, *fd); free(fd); + return true; } @@ -341,16 +364,26 @@ e2ap_msg_t e2ap_handle_setup_response_agent(e2_agent_t* ag, const e2ap_msg_t* ms assert(msg->type == E2_SETUP_RESPONSE); printf("[E2-AGENT]: E2 SETUP RESPONSE rx\n"); + e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; + // Stop the timer pending_event_t ev = SETUP_REQUEST_PENDING_EVENT; - stop_pending_event(ag,ev); + if(stop_pending_event(ag, ev) == false){ + printf("[E2-AGENT]: E2 SETUP RESPONSE rx with no E2 SETUP REQUEST outstanding. Ignoring it\n"); + return ans; + } #if defined(E2AP_V2) || defined(E2AP_V3) - assert(ag->trans_id_setup_req > 0 && "Receiving an E2 SETUP-RESPONSE, eventhough not E2 SETUP-REQUEST not sent from this E2 Node" ); + if(ag->trans_id_setup_req == 0){ + printf("[E2-AGENT]: E2 SETUP RESPONSE rx, eventhough no E2 SETUP-REQUEST was sent from this E2 Node. Ignoring it\n"); + return ans; + } + /* The two transaction ids are reported but not compared: a RIC is not + * guaranteed to echo the one it was sent, and rejecting the mismatch would + * drop the only response such a RIC ever sends. */ printf("[E2-AGENT]: Transaction ID E2 SETUP-REQUEST %u E2 SETUP-RESPONSE %u \n", --ag->trans_id_setup_req, msg->u_msgs.e2_stp_resp.trans_id); ag->trans_id_setup_req = 0; #endif - e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; return ans; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b37f9a1f6..c8161cd10 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,6 @@ # SPDX-License-Identifier: LicenseRef-CSSL-1.0 +add_subdirectory(agent) add_subdirectory(agent-ric-xapp) add_subdirectory(agent-ric) add_subdirectory(ind_event_key) diff --git a/test/agent/CMakeLists.txt b/test/agent/CMakeLists.txt new file mode 100644 index 000000000..4eba4d51f --- /dev/null +++ b/test/agent/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: LicenseRef-CSSL-1.0 + +##################################### +# Unit test E2 SETUP RESPONSE handling +##################################### + +add_executable(test_setup_response + test_setup_response.c + ) + +target_link_libraries(test_setup_response + PUBLIC + e2_agent + -pthread + -lsctp + -ldl) + +# e2_agent sets these PRIVATE, and e2_agent_t's layout depends on them. +target_compile_definitions(test_setup_response PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) + +enable_testing() +add_test(Unit_test_setup_response test_setup_response) diff --git a/test/agent/test_setup_response.c b/test/agent/test_setup_response.c new file mode 100644 index 000000000..3d8aada0a --- /dev/null +++ b/test/agent/test_setup_response.c @@ -0,0 +1,156 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * A duplicate or unsolicited E2 SETUP RESPONSE used to abort the RAN process. + * + * e2ap_handle_setup_response_agent() stopped the setup timer unconditionally, + * and bi_map_extract_right() asserts that the key is in the tree. The first + * response removed the entry, so a second one aborted in assoc_rb_tree_extract. + * A second assert on trans_id_setup_req sat right behind it on the same path. + */ + +#include "agent/e2_agent.h" +#include "agent/msg_handler_agent.h" +#include "agent/asio_agent.h" +#include "lib/pending_events.h" +#include "util/compare.h" + +#include +#include +#include +#include + +static +void free_fd_test(void* key, void* value) +{ + assert(key != NULL); + assert(value != NULL); + free(value); +} + +static +void free_pending_ev_test(void* key, void* value) +{ + assert(key != NULL); + assert(value != NULL); + free(value); +} + +// The slice of e2_init_agent() that the setup-response path actually touches: +// the epoll set and the pending-event map. +static +void init_agent_stub(e2_agent_t* ag) +{ + memset(ag, 0, sizeof(*ag)); + + init_asio_agent(&ag->io); + + size_t const fd_sz = sizeof(int); + size_t const event_sz = sizeof(pending_event_t); + bi_map_init(&ag->pending, fd_sz, event_sz, cmp_fd, cmp_pending_event, free_fd_test, free_pending_ev_test); +} + +static +void free_agent_stub(e2_agent_t* ag) +{ + bi_map_free(&ag->pending); +} + +// What e2_start_agent() does when it sends the E2 SETUP REQUEST: arm the 3000 ms +// retry timer and record it as the outstanding pending event. +static +void arm_setup_request(e2_agent_t* ag) +{ + int const fd_timer = create_timer_ms_asio_agent(&ag->io, 3000, 3000); + assert(fd_timer > 0); + + pending_event_t const ev = SETUP_REQUEST_PENDING_EVENT; + bi_map_insert(&ag->pending, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); + +#if defined(E2AP_V2) || defined(E2AP_V3) + ag->trans_id_setup_req = 1; +#endif +} + +static +e2ap_msg_t setup_response_msg(uint32_t trans_id) +{ + e2ap_msg_t msg = {.type = E2_SETUP_RESPONSE}; +#if defined(E2AP_V2) || defined(E2AP_V3) + msg.u_msgs.e2_stp_resp.trans_id = trans_id; +#else + (void)trans_id; +#endif + return msg; +} + +// The reported case: the RIC answers the same setup twice. +static +void test_duplicate_setup_response(void) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + arm_setup_request(&ag); + + e2ap_msg_t const msg = setup_response_msg(21); + + e2ap_msg_t const first = e2ap_handle_setup_response_agent(&ag, &msg); + assert(first.type == NONE_E2_MSG_TYPE); + assert(bi_map_size(&ag.pending) == 0 && "the first response consumes the pending timer"); + + // Used to abort: assoc_rb_tree_extract on a key that is no longer there. + e2ap_msg_t const second = e2ap_handle_setup_response_agent(&ag, &msg); + assert(second.type == NONE_E2_MSG_TYPE); + assert(bi_map_size(&ag.pending) == 0); + + free_agent_stub(&ag); + printf("[TEST]: duplicate E2 SETUP RESPONSE ignored\n"); +} + +// A response to a request this node never sent. +static +void test_unsolicited_setup_response(void) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + + e2ap_msg_t const msg = setup_response_msg(21); + + e2ap_msg_t const ans = e2ap_handle_setup_response_agent(&ag, &msg); + assert(ans.type == NONE_E2_MSG_TYPE); + assert(bi_map_size(&ag.pending) == 0); + + free_agent_stub(&ag); + printf("[TEST]: unsolicited E2 SETUP RESPONSE ignored\n"); +} + +// A RIC that does not echo the transaction id is still answered, since rejecting +// the mismatch would drop the only response such a RIC sends. +static +void test_mismatched_trans_id_is_accepted(void) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + arm_setup_request(&ag); + + e2ap_msg_t const msg = setup_response_msg(21); + + e2ap_msg_t const ans = e2ap_handle_setup_response_agent(&ag, &msg); + assert(ans.type == NONE_E2_MSG_TYPE); + assert(bi_map_size(&ag.pending) == 0 && "the response was accepted, not rejected"); + + free_agent_stub(&ag); + printf("[TEST]: E2 SETUP RESPONSE with a non-echoed transaction id accepted\n"); +} + +int main() +{ + test_duplicate_setup_response(); + test_unsolicited_setup_response(); + test_mismatched_trans_id_is_accepted(); + + printf("[TEST]: E2 SETUP RESPONSE handling OK\n"); + return EXIT_SUCCESS; +} From e8926ad8d3685c30e71aed31428940cc7ea36c66 Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Thu, 15 Oct 2026 14:44:39 -0400 Subject: [PATCH 4/7] refactor(agent): split stop_ind_event and take the subscription mutex stop_ind_event() unlinked a subscription from both trees without holding ag->mtx_ind_event, although insert takes it and e2_async_event_agent() traverses the right tree under it from the RAN threads. A delete could free a node while one of them was walking it. Split it into unlink_ind_event(), which holds the mutex while it unlinks, and release_ind_event(), which stops the producer and hands the payload back with the mutex released, since free_act_def and free_subs_aperiodic are service model and RAN code. The producer is now stopped before its payload is released, not after. No other change in behavior. This is groundwork to drop every subscription at once. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- src/agent/msg_handler_agent.c | 72 ++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index df71c3fa1..28ae4d9b6 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -37,47 +37,73 @@ bool check_valid_msg_type(e2_msg_type_t msg_type ) // stop_ind_event() can tell them apart from the timer fds of periodic ones. static _Atomic int next_aperiodic_fd = -1; +// Unlink the subscription under id from both trees and copy it out. Returns +// false when there is none. The caller must then release_ind_event() it, with +// ag->mtx_ind_event no longer held. static -bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) +bool unlink_ind_event(e2_agent_t* ag, ric_gen_id_t id, ind_event_t* out) { assert(ag != NULL); - ind_event_t tmp = {.ric_id = id, .sm = NULL, .action_id =0 }; + assert(out != NULL); + + // Insert takes the mutex, and e2_async_event_agent() traverses the right tree + // under it from the RAN threads, so the unlink must as well. + lock_guard(&ag->mtx_ind_event); - // Fix this! bi_map should liberate the memory itself - void* start_r = assoc_rb_tree_front(&ag->ind_event.right); - void* end_r = assoc_rb_tree_end(&ag->ind_event.right); - void* it_r = find_if_rb_tree(&ag->ind_event.right, start_r, end_r, &tmp, eq_ind_event); + assoc_rb_tree_t* tree = &ag->ind_event.right; + void* end_r = assoc_rb_tree_end(tree); + void* it_r = find_if_rb_tree(tree, assoc_rb_tree_front(tree), end_r, &id, eq_ind_event); if(it_r == end_r){ printf("[E2 AGENT]: RAN_FUNC_ID %d RIC_REQ_ID %d not found. Spuriously occurs when abruptly closing the xApp\n", id.ran_func_id, id.ric_req_id); - return false;; + return false; } - assert(it_r != end_r); - ind_event_t* ind_ev = assoc_rb_tree_key(&ag->ind_event.right, it_r); - - if (ind_ev->type == PERIODIC_SUBSCRIPTION_FLRC) { - if (ind_ev->sm->free_act_def != NULL) - ind_ev->sm->free_act_def(ind_ev->sm, ind_ev->act_def); - } else if(ind_ev->type == APERIODIC_SUBSCRIPTION_FLRC) { - ind_ev->free_subs_aperiodic(id); - } else { - assert(0!=0 && "Unknown subscritpion type"); - } + *out = *(ind_event_t*)assoc_rb_tree_key(tree, it_r); // Extract by the fd, i.e. the left key: it is unique, whereas several entries // can share one RIC ID. Extracting by the right key let assoc_extract's binary // search unlink a different entry than the linear scan above had found, which // freed one entry's act_def and left the other live with a freed pointer and an // armed timer. - int const fd = ind_ev->fd; - ind_event_t* val = bi_map_extract_left(&ag->ind_event, (int*)&fd, sizeof(fd), NULL); + int fd = out->fd; + ind_event_t* val = bi_map_extract_left(&ag->ind_event, &fd, sizeof(fd), NULL); assert(val != NULL); free(val); - if (fd > -1) - rm_fd_asio_agent(&ag->io, fd); + return true; +} + +// Stop the subscription's producer and hand its payload back to its owner. Must +// run with ag->mtx_ind_event released: free_act_def and free_subs_aperiodic are +// service model and RAN code that take their own locks. +static +void release_ind_event(e2_agent_t* ag, ind_event_t const* ev) +{ + assert(ag != NULL); + assert(ev != NULL); + + if(ev->fd > -1) + rm_fd_asio_agent(&ag->io, ev->fd); - return true;; + if(ev->type == PERIODIC_SUBSCRIPTION_FLRC){ + if(ev->sm->free_act_def != NULL) + ev->sm->free_act_def(ev->sm, ev->act_def); + } else if(ev->type == APERIODIC_SUBSCRIPTION_FLRC){ + ev->free_subs_aperiodic(ev->ric_id); + } else { + assert(0!=0 && "Unknown subscritpion type"); + } +} + +static +bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) +{ + ind_event_t ev = {0}; + if(unlink_ind_event(ag, id, &ev) == false) + return false; + + release_ind_event(ag, &ev); + return true; } void init_handle_msg_agent(size_t len, handle_msg_fp_agent (*handle_msg)[len]) From 358a6f751ef8e1ce8c5eb78746c414a90c6f8cf4 Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Thu, 15 Oct 2026 14:47:19 -0400 Subject: [PATCH 5/7] fix(agent): drop the RIC subscriptions when the association shuts down On SCTP_SHUTDOWN_EVENT the agent only armed the E2 SETUP REQUEST retry. Every installed subscription kept its timer and its action definition, so the agent went on producing indications for a RIC that had forgotten them, and the RAN's own registry kept the aperiodic ones, since free_subs_aperiodic() is only reachable from the delete path. E2AP keeps no subscription state across an association. stop_all_ind_events() now drops every subscription before the retry is armed. The teardown is gated on SCTP_SHUTDOWN_EVENT: every SCTP notification reaches notification_handle_ag(), and cp_sctp_notification() already decodes SCTP_COMM_UP, which must not tear anything down. The retry is also armed only when none is pending, since pend_event() asserts that the map holds exactly one entry and a second notification before the E2 SETUP RESPONSE would abort the RAN on the next timer tick. test_association_loss fails without the change. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- src/agent/msg_handler_agent.c | 33 ++++++ src/agent/msg_handler_agent.h | 4 + src/agent/not_handler_agent.c | 33 ++++-- test/agent/CMakeLists.txt | 19 ++++ test/agent/test_association_loss.c | 161 +++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 test/agent/test_association_loss.c diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 28ae4d9b6..1df074e2c 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -106,6 +106,39 @@ bool stop_ind_event(e2_agent_t* ag, ric_gen_id_t id) return true; } +// Drop every installed subscription: E2AP keeps none across an association. +// Agent thread only. +void stop_all_ind_events(e2_agent_t* ag) +{ + assert(ag != NULL); + + size_t dropped = 0; + for(;;){ + ind_event_t ev = {0}; + { + lock_guard(&ag->mtx_ind_event); + + assoc_rb_tree_t* left = &ag->ind_event.left; + void* it = assoc_rb_tree_front(left); + if(it == assoc_rb_tree_end(left)) + break; + + // Re-read front() after every extraction: assoc_rb_tree_extract frees the + // node, so an iterator held across one would dangle. + int fd = *(int*)assoc_rb_tree_key(left, it); + ind_event_t* val = bi_map_extract_left(&ag->ind_event, &fd, sizeof(fd), NULL); + assert(val != NULL); + ev = *val; + free(val); + } + release_ind_event(ag, &ev); + ++dropped; + } + + if(dropped > 0) + printf("[E2 AGENT]: dropped %zu RIC subscription(s) with the association\n", dropped); +} + void init_handle_msg_agent(size_t len, handle_msg_fp_agent (*handle_msg)[len]) { assert(len == NONE_E2_MSG_TYPE); diff --git a/src/agent/msg_handler_agent.h b/src/agent/msg_handler_agent.h index b885d4474..1c3c417c7 100644 --- a/src/agent/msg_handler_agent.h +++ b/src/agent/msg_handler_agent.h @@ -14,6 +14,10 @@ void init_handle_msg_agent(size_t len, handle_msg_fp_agent (*handle_msg)[len]); e2ap_msg_t e2ap_msg_handle_agent(e2_agent_t* agent, const e2ap_msg_t* msg); +// Drop every installed RIC subscription, stopping its timer and releasing its +// action definition. Agent thread only. +void stop_all_ind_events(e2_agent_t* ag); + /////////////////////////////////////////////////////////////////////////////////////////////////// // O-RAN E2APv01.01: Messages for Global Procedures /////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/agent/not_handler_agent.c b/src/agent/not_handler_agent.c index 4d8e06e93..1d00dda76 100644 --- a/src/agent/not_handler_agent.c +++ b/src/agent/not_handler_agent.c @@ -3,6 +3,7 @@ */ #include "not_handler_agent.h" +#include "msg_handler_agent.h" #include #include @@ -15,15 +16,31 @@ void notification_handle_ag(e2_agent_t* ag, sctp_msg_t const* msg) assert(ag != NULL); assert(msg != NULL && msg->type == SCTP_MSG_NOTIFICATION); - // A pending event is created along with a timer of 3000 ms, - // after which an event will be generated - pending_event_t ev = SETUP_REQUEST_PENDING_EVENT; - long const wait_ms = 3000; - int fd_timer = create_timer_ms_asio_agent(&ag->io, wait_ms, wait_ms); - - bi_map_insert(&ag->pending, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); + // next_async_event_agent routes every SCTP notification here, and + // e2ap_recv_sctp_msg does not filter on sn_type either. Only the shutdown + // ends the E2 session: cp_sctp_notification decodes SCTP_COMM_UP too, and + // dropping every subscription on an association coming up would cause an + // outage. + if(msg->notif->sn_header.sn_type != SCTP_SHUTDOWN_EVENT){ + printf("[E2 AGENT]: SCTP notification type %u ignored\n", msg->notif->sn_header.sn_type); + return; + } printf("[E2 AGENT]: Communication with the nearRT-RIC lost\n" ); -} + stop_all_ind_events(ag); + // A pending event is created along with a timer of 3000 ms, + // after which an event will be generated. + // + // Only ever one: pend_event() asserts the map holds exactly one entry, so a + // second notification arriving before the setup response completes would abort + // the RAN on the next timer tick. + if(bi_map_size(&ag->pending) == 0){ + pending_event_t ev = SETUP_REQUEST_PENDING_EVENT; + long const wait_ms = 3000; + int fd_timer = create_timer_ms_asio_agent(&ag->io, wait_ms, wait_ms); + + bi_map_insert(&ag->pending, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); + } +} diff --git a/test/agent/CMakeLists.txt b/test/agent/CMakeLists.txt index 4eba4d51f..d98293433 100644 --- a/test/agent/CMakeLists.txt +++ b/test/agent/CMakeLists.txt @@ -20,3 +20,22 @@ target_compile_definitions(test_setup_response PRIVATE ${E2AP_ENCODING} ${E2AP_V enable_testing() add_test(Unit_test_setup_response test_setup_response) + +##################################### +# Unit test: a SCTP shutdown ends the E2 session +##################################### + +add_executable(test_association_loss + test_association_loss.c + ) + +target_link_libraries(test_association_loss + PUBLIC + e2_agent + -pthread + -lsctp + -ldl) + +target_compile_definitions(test_association_loss PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) + +add_test(Unit_test_association_loss test_association_loss) diff --git a/test/agent/test_association_loss.c b/test/agent/test_association_loss.c new file mode 100644 index 000000000..cc8486d85 --- /dev/null +++ b/test/agent/test_association_loss.c @@ -0,0 +1,161 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * A SCTP shutdown from the nearRT-RIC must end the E2 session: drop every + * subscription and arm a single E2 SETUP REQUEST retry. + */ + +// The asserts are the test: keep them in builds that define NDEBUG. +#undef NDEBUG + +#include "agent/e2_agent.h" +#include "agent/asio_agent.h" +#include "agent/msg_handler_agent.h" +#include "agent/not_handler_agent.h" +#include "lib/ind_event.h" +#include "lib/pending_events.h" +#include "util/compare.h" + +#include +#include +#include +#include +#include +#include + +static +int released = 0; + +// What the RAN registers as free_aper_subs: un-registers the subscription. +static +void count_release(ric_gen_id_t ric_id) +{ + (void)ric_id; + ++released; +} + +static +void free_value_test(void* key, void* value) +{ + assert(key != NULL); + assert(value != NULL); + free(value); +} + +// The slice of e2_init_agent() the notification path touches: the epoll set, the +// pending-event map and the subscription map with its mutex. +static +void init_agent_stub(e2_agent_t* ag) +{ + memset(ag, 0, sizeof(*ag)); + + init_asio_agent(&ag->io); + + bi_map_init(&ag->pending, sizeof(int), sizeof(pending_event_t), cmp_fd, cmp_pending_event, free_value_test, free_value_test); + bi_map_init(&ag->ind_event, sizeof(int), sizeof(ind_event_t), cmp_fd, cmp_ind_event, free_value_test, free_value_test); + + int const rc = pthread_mutex_init(&ag->mtx_ind_event, NULL); + assert(rc == 0); + + released = 0; +} + +static +void free_agent_stub(e2_agent_t* ag) +{ + bi_map_free(&ag->pending); + bi_map_free(&ag->ind_event); + pthread_mutex_destroy(&ag->mtx_ind_event); +} + +// What e2ap_handle_subscription_request_agent() installs for an aperiodic +// subscription, keyed by a negative synthetic fd. +static +void install_aperiodic_subscription(e2_agent_t* ag) +{ + ind_event_t ev = {0}; + ev.ric_id = (ric_gen_id_t){.ric_req_id = 1, .ric_inst_id = 1, .ran_func_id = 3}; + ev.fd = -1; + ev.type = APERIODIC_SUBSCRIPTION_FLRC; + ev.free_subs_aperiodic = count_release; + + pthread_mutex_lock(&ag->mtx_ind_event); + bi_map_insert(&ag->ind_event, &ev.fd, sizeof(ev.fd), &ev, sizeof(ev)); + pthread_mutex_unlock(&ag->mtx_ind_event); +} + +// What e2_start_agent() leaves behind: an E2 SETUP REQUEST retry timer. +static +void arm_setup_request(e2_agent_t* ag) +{ + int const fd_timer = create_timer_ms_asio_agent(&ag->io, 3000, 3000); + assert(fd_timer > 0); + + pending_event_t const ev = SETUP_REQUEST_PENDING_EVENT; + bi_map_insert(&ag->pending, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); +} + +static +sctp_msg_t shutdown_event(union sctp_notification* n) +{ + memset(n, 0, sizeof(*n)); + n->sn_header.sn_type = SCTP_SHUTDOWN_EVENT; + return (sctp_msg_t){.type = SCTP_MSG_NOTIFICATION, .notif = n}; +} + +// The session ends: the subscription is released through the RAN's callback and +// an E2 SETUP REQUEST retry is armed to open the next one. +static +void expect_session_ended(sctp_msg_t const* msg, char const* name) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + install_aperiodic_subscription(&ag); + + notification_handle_ag(&ag, msg); + + assert(bi_map_size(&ag.ind_event) == 0 && "the subscription was dropped"); + assert(released == 1 && "the RAN was told to un-register it"); + assert(bi_map_size(&ag.pending) == 1 && "an E2 SETUP REQUEST retry is armed"); + + free_agent_stub(&ag); + printf("[TEST]: %s ends the E2 session\n", name); +} + +// pend_event() asserts the pending map holds exactly one entry, so a loss while a +// setup is already outstanding -- or two losses in a row -- must not arm a second +// timer. +static +void test_one_retry_timer_only(void) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + arm_setup_request(&ag); + + union sctp_notification n; + sctp_msg_t const lost = shutdown_event(&n); + + notification_handle_ag(&ag, &lost); + assert(bi_map_size(&ag.pending) == 1); + + notification_handle_ag(&ag, &lost); + assert(bi_map_size(&ag.pending) == 1); + + free_agent_stub(&ag); + printf("[TEST]: repeated shutdowns arm a single E2 SETUP REQUEST retry\n"); +} + +int main() +{ + union sctp_notification n; + + sctp_msg_t msg = shutdown_event(&n); + expect_session_ended(&msg, "SCTP_SHUTDOWN_EVENT"); + + test_one_retry_timer_only(); + + printf("[TEST]: association loss handling OK\n"); + return EXIT_SUCCESS; +} From 2d3a4a77fe783d31e0844939ea7f59a4aff9bd9b Mon Sep 17 00:00:00 2001 From: Angelo Feraudo Date: Thu, 15 Oct 2026 14:48:28 -0400 Subject: [PATCH 6/7] fix(agent): end the E2 session when the association is lost notification_handle_ag() ended the E2 session on SCTP_SHUTDOWN_EVENT alone, and the agent did not subscribe to association events, so a SHUTDOWN was the only notification that could arrive. A nearRT-RIC that goes away without one (its host or the network fails, or it sends an ABORT) or that comes back under the same addresses produces no SHUTDOWN: the kernel reports SCTP_COMM_LOST or SCTP_RESTART, and only to a socket that asked for association events. Neither was seen, so subscriptions stayed installed and no E2 SETUP REQUEST was sent again. The socket is one-to-many, so the next indication silently opened a new association to a restarted RIC with no E2 setup on it. Subscribe to association events, and end the session on SCTP_COMM_LOST and SCTP_RESTART as on SCTP_SHUTDOWN_EVENT. SCTP_COMM_UP, SCTP_CANT_STR_ASSOC and SCTP_SHUTDOWN_COMP are not an end and stay ignored. No other event type is subscribed, since cp_sctp_notification() asserts on the ones it does not decode. test_association_loss covers the six association outcomes and fails without the change. Co-authored-by: Andrea Lacava Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- src/agent/endpoint_agent.c | 7 +++- src/agent/not_handler_agent.c | 60 ++++++++++++++++++++++++------ test/agent/CMakeLists.txt | 2 +- test/agent/test_association_loss.c | 60 ++++++++++++++++++++++++++++-- 4 files changed, 111 insertions(+), 18 deletions(-) diff --git a/src/agent/endpoint_agent.c b/src/agent/endpoint_agent.c index f82ec73d2..78fda80cf 100644 --- a/src/agent/endpoint_agent.c +++ b/src/agent/endpoint_agent.c @@ -27,8 +27,13 @@ void init_sctp_conn_client(e2ap_ep_ag_t* ep, const char* addr, int port) int rc = inet_pton(AF_INET, addr, &servaddr.sin_addr); assert(rc == 1); + // Association events report a nearRT-RIC that went away without a SHUTDOWN + // (SCTP_COMM_LOST) or came back under the same addresses (SCTP_RESTART). + // Subscribe to nothing else: cp_sctp_notification asserts on the types it + // does not decode. struct sctp_event_subscribe evnts = { .sctp_data_io_event = 1, - .sctp_shutdown_event = 1}; + .sctp_association_event = 1, + .sctp_shutdown_event = 1}; rc = setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof (evnts)); assert(rc == 0); diff --git a/src/agent/not_handler_agent.c b/src/agent/not_handler_agent.c index 1d00dda76..cf9f33fd8 100644 --- a/src/agent/not_handler_agent.c +++ b/src/agent/not_handler_agent.c @@ -11,22 +11,42 @@ #include "../lib/pending_events.h" -void notification_handle_ag(e2_agent_t* ag, sctp_msg_t const* msg) +// Why a notification ends the E2 session with the nearRT-RIC, or NULL when it +// does not. The association can end three ways: +// - SCTP_SHUTDOWN_EVENT: the RIC closed it gracefully. +// - SCTP_COMM_LOST: it died (RIC host or network gone, or the peer sent an +// ABORT); no SHUTDOWN is sent. +// - SCTP_RESTART: the RIC came back under the same addresses and re-joined the +// association, having forgotten every subscription and the E2 setup. +// The other association states are not an end: SCTP_COMM_UP is one coming up, +// SCTP_CANT_STR_ASSOC a setup attempt the retry timer repeats, and +// SCTP_SHUTDOWN_COMP closes the shutdown already reported. +static +char const* e2_session_end_reason(union sctp_notification const* notif) { - assert(ag != NULL); - assert(msg != NULL && msg->type == SCTP_MSG_NOTIFICATION); + assert(notif != NULL); - // next_async_event_agent routes every SCTP notification here, and - // e2ap_recv_sctp_msg does not filter on sn_type either. Only the shutdown - // ends the E2 session: cp_sctp_notification decodes SCTP_COMM_UP too, and - // dropping every subscription on an association coming up would cause an - // outage. - if(msg->notif->sn_header.sn_type != SCTP_SHUTDOWN_EVENT){ - printf("[E2 AGENT]: SCTP notification type %u ignored\n", msg->notif->sn_header.sn_type); - return; + if(notif->sn_header.sn_type == SCTP_SHUTDOWN_EVENT) + return "the nearRT-RIC shut the association down"; + + if(notif->sn_header.sn_type != SCTP_ASSOC_CHANGE) + return NULL; + + switch(notif->sn_assoc_change.sac_state){ + case SCTP_COMM_LOST: + return "the association with the nearRT-RIC was lost"; + case SCTP_RESTART: + return "the nearRT-RIC restarted"; + default: + return NULL; } +} - printf("[E2 AGENT]: Communication with the nearRT-RIC lost\n" ); +// The E2 session is over: drop what it installed and start a new one. +static +void end_e2_session(e2_agent_t* ag) +{ + assert(ag != NULL); stop_all_ind_events(ag); @@ -44,3 +64,19 @@ void notification_handle_ag(e2_agent_t* ag, sctp_msg_t const* msg) bi_map_insert(&ag->pending, &fd_timer, sizeof(fd_timer), &ev, sizeof(ev)); } } + +void notification_handle_ag(e2_agent_t* ag, sctp_msg_t const* msg) +{ + assert(ag != NULL); + assert(msg != NULL && msg->type == SCTP_MSG_NOTIFICATION); + + char const* reason = e2_session_end_reason(msg->notif); + if(reason == NULL){ + printf("[E2 AGENT]: SCTP notification type %u ignored\n", msg->notif->sn_header.sn_type); + return; + } + + printf("[E2 AGENT]: E2 session with the nearRT-RIC ended: %s\n", reason); + + end_e2_session(ag); +} diff --git a/test/agent/CMakeLists.txt b/test/agent/CMakeLists.txt index d98293433..ba0969b4e 100644 --- a/test/agent/CMakeLists.txt +++ b/test/agent/CMakeLists.txt @@ -22,7 +22,7 @@ enable_testing() add_test(Unit_test_setup_response test_setup_response) ##################################### -# Unit test: a SCTP shutdown ends the E2 session +# Unit test: every way the SCTP association can end ends the E2 session ##################################### add_executable(test_association_loss diff --git a/test/agent/test_association_loss.c b/test/agent/test_association_loss.c index cc8486d85..02f6b2762 100644 --- a/test/agent/test_association_loss.c +++ b/test/agent/test_association_loss.c @@ -3,8 +3,15 @@ */ /* - * A SCTP shutdown from the nearRT-RIC must end the E2 session: drop every - * subscription and arm a single E2 SETUP REQUEST retry. + * Every way the SCTP association to the nearRT-RIC can end must end the E2 + * session, not only a graceful SHUTDOWN. + * + * notification_handle_ag() acted on SCTP_SHUTDOWN_EVENT alone, and the agent did + * not subscribe to association events at all. A RIC that went away without a + * SHUTDOWN (SCTP_COMM_LOST: host or network gone, or an ABORT) or that came back + * under the same addresses (SCTP_RESTART) left every subscription installed and + * no E2 SETUP REQUEST armed, so the RAN kept producing indications for a RIC that + * had forgotten them and never set the E2 session up again. */ // The asserts are the test: keep them in builds that define NDEBUG. @@ -105,6 +112,15 @@ sctp_msg_t shutdown_event(union sctp_notification* n) return (sctp_msg_t){.type = SCTP_MSG_NOTIFICATION, .notif = n}; } +static +sctp_msg_t assoc_change(union sctp_notification* n, uint16_t state) +{ + memset(n, 0, sizeof(*n)); + n->sn_header.sn_type = SCTP_ASSOC_CHANGE; + n->sn_assoc_change.sac_state = state; + return (sctp_msg_t){.type = SCTP_MSG_NOTIFICATION, .notif = n}; +} + // The session ends: the subscription is released through the RAN's callback and // an E2 SETUP REQUEST retry is armed to open the next one. static @@ -124,6 +140,27 @@ void expect_session_ended(sctp_msg_t const* msg, char const* name) printf("[TEST]: %s ends the E2 session\n", name); } +// Not an end: nothing is torn down and no retry is armed. +static +void expect_session_kept(sctp_msg_t const* msg, char const* name) +{ + e2_agent_t ag = {0}; + init_agent_stub(&ag); + install_aperiodic_subscription(&ag); + + notification_handle_ag(&ag, msg); + + assert(bi_map_size(&ag.ind_event) == 1 && "the subscription survives"); + assert(released == 0); + assert(bi_map_size(&ag.pending) == 0 && "no retry is armed"); + + stop_all_ind_events(&ag); + assert(released == 1); + + free_agent_stub(&ag); + printf("[TEST]: %s leaves the E2 session alone\n", name); +} + // pend_event() asserts the pending map holds exactly one entry, so a loss while a // setup is already outstanding -- or two losses in a row -- must not arm a second // timer. @@ -135,7 +172,7 @@ void test_one_retry_timer_only(void) arm_setup_request(&ag); union sctp_notification n; - sctp_msg_t const lost = shutdown_event(&n); + sctp_msg_t const lost = assoc_change(&n, SCTP_COMM_LOST); notification_handle_ag(&ag, &lost); assert(bi_map_size(&ag.pending) == 1); @@ -144,7 +181,7 @@ void test_one_retry_timer_only(void) assert(bi_map_size(&ag.pending) == 1); free_agent_stub(&ag); - printf("[TEST]: repeated shutdowns arm a single E2 SETUP REQUEST retry\n"); + printf("[TEST]: repeated losses arm a single E2 SETUP REQUEST retry\n"); } int main() @@ -154,6 +191,21 @@ int main() sctp_msg_t msg = shutdown_event(&n); expect_session_ended(&msg, "SCTP_SHUTDOWN_EVENT"); + msg = assoc_change(&n, SCTP_COMM_LOST); + expect_session_ended(&msg, "SCTP_COMM_LOST"); + + msg = assoc_change(&n, SCTP_RESTART); + expect_session_ended(&msg, "SCTP_RESTART"); + + msg = assoc_change(&n, SCTP_COMM_UP); + expect_session_kept(&msg, "SCTP_COMM_UP"); + + msg = assoc_change(&n, SCTP_CANT_STR_ASSOC); + expect_session_kept(&msg, "SCTP_CANT_STR_ASSOC"); + + msg = assoc_change(&n, SCTP_SHUTDOWN_COMP); + expect_session_kept(&msg, "SCTP_SHUTDOWN_COMP"); + test_one_retry_timer_only(); printf("[TEST]: association loss handling OK\n"); From da9a1963a6eae0633bf59f7d6392b2fe1a0581c6 Mon Sep 17 00:00:00 2001 From: Andrea Lacava Date: Thu, 15 Oct 2026 15:40:10 -0400 Subject: [PATCH 7/7] fix(agent): replace a subscription the RIC installs a second time assoc_rb_tree_insert does not reject equality, so a RIC_SUBSCRIPTION_REQUEST for a RIC ID the agent already holds leaves two entries under one key, each with its own timer and action definition. A near-RT RIC produces that when the delete of the first never arrived, or by reusing one Requestor ID, as the O-RAN SC one does. Release the existing subscription before installing the new one, so a re-install replaces it. It runs before on_subscription(): an aperiodic entry's free_subs_aperiodic() lets the RAN un-register the RIC ID, which on_subscription() may have just registered. Keying on the fd already makes such duplicates survivable. This stops them forming. test/agent/test_replace_subscription.c installs the same RIC ID twice and expects one entry and one release. It fails on the previous handler. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Andrea Lacava --- src/agent/msg_handler_agent.c | 10 ++ test/agent/CMakeLists.txt | 19 ++++ test/agent/test_replace_subscription.c | 133 +++++++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 test/agent/test_replace_subscription.c diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 1df074e2c..643424ade 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -222,6 +222,16 @@ e2ap_msg_t e2ap_handle_subscription_request_agent(e2_agent_t* ag, const e2ap_msg printf("[E2 AGENT]: RIC_SUBSCRIPTION_REQUEST rx RAN_FUNC_ID %d RIC_REQ_ID %d\n", sr->ric_id.ran_func_id, sr->ric_id.ric_req_id); + // Replace, do not duplicate: a RIC that installs a subscription the agent + // still holds (its delete never arrived, or its Requestor ID is a constant, as + // the O-RAN SC one is) would otherwise leave two timers and two action + // definitions under one RIC ID. Done before on_subscription(), as an aperiodic + // entry's free_subs_aperiodic() lets the RAN un-register the RIC ID, which + // on_subscription() may have just registered. + if(stop_ind_event(ag, sr->ric_id) == true) + printf("[E2 AGENT]: RIC_SUBSCRIPTION_REQUEST replaces an existing subscription RAN_FUNC_ID %d RIC_REQ_ID %d INST_ID %d\n", + sr->ric_id.ran_func_id, sr->ric_id.ric_req_id, sr->ric_id.ric_inst_id); + sm_subs_data_t data = generate_sm_subs_data(sr); uint16_t const ran_func_id = sr->ric_id.ran_func_id; sm_agent_t* sm = sm_plugin_ag(&ag->plugin, ran_func_id); diff --git a/test/agent/CMakeLists.txt b/test/agent/CMakeLists.txt index ba0969b4e..37f493319 100644 --- a/test/agent/CMakeLists.txt +++ b/test/agent/CMakeLists.txt @@ -39,3 +39,22 @@ target_link_libraries(test_association_loss target_compile_definitions(test_association_loss PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) add_test(Unit_test_association_loss test_association_loss) + +##################################### +# Unit test: a subscription for an installed RIC ID replaces it +##################################### + +add_executable(test_replace_subscription + test_replace_subscription.c + ) + +target_link_libraries(test_replace_subscription + PUBLIC + e2_agent + -pthread + -lsctp + -ldl) + +target_compile_definitions(test_replace_subscription PRIVATE ${E2AP_ENCODING} ${E2AP_VERSION} ${KPM_VERSION}) + +add_test(Unit_test_replace_subscription test_replace_subscription) diff --git a/test/agent/test_replace_subscription.c b/test/agent/test_replace_subscription.c new file mode 100644 index 000000000..97cba6cb4 --- /dev/null +++ b/test/agent/test_replace_subscription.c @@ -0,0 +1,133 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * A RIC_SUBSCRIPTION_REQUEST for a RIC ID the agent already holds must replace + * that subscription, not add a second one under the same key. + * + * The subscription registry is a multiset (assoc_rb_tree_insert does not reject + * equality), so a RIC that re-installs a subscription whose delete never arrived, + * or that reuses one Requestor ID as the O-RAN SC near-RT RIC does, left two + * entries, each with its own action definition and timer, behind one RIC ID. + */ + +// The asserts are the test: keep them in builds that define NDEBUG. +#undef NDEBUG + +#include "agent/e2_agent.h" +#include "agent/asio_agent.h" +#include "agent/msg_handler_agent.h" +#include "lib/ind_event.h" +#include "util/compare.h" + +#include +#include +#include +#include +#include + +#define RAN_FUNC_ID 3 + +static int subscribed = 0; +static int released = 0; + +// What the RAN registers as free_aper_subs: un-registers the subscription. +static +void count_release(ric_gen_id_t ric_id) +{ + (void)ric_id; + ++released; +} + +static +sm_ag_if_ans_subs_t on_subscription_stub(sm_agent_t const* sm, sm_subs_data_t const* data) +{ + (void)sm; + (void)data; + ++subscribed; + sm_ag_if_ans_subs_t ans = {.type = APERIODIC_SUBSCRIPTION_FLRC}; + ans.aper.free_aper_subs = count_release; + return ans; +} + +static +uint16_t id_stub(void) +{ + return RAN_FUNC_ID; +} + +static +int cmp_ran_func_id_test(const void* a, const void* b) +{ + uint16_t const x = *(uint16_t const*)a; + uint16_t const y = *(uint16_t const*)b; + return x < y ? 1 : (x > y ? -1 : 0); +} + +static +void free_nop(void* key, void* value) +{ + (void)key; + (void)value; +} + +static +e2ap_msg_t subscription_request(ric_gen_id_t ric_id) +{ + static ric_action_t action = {0}; + action.id = 1; + action.type = RIC_ACT_REPORT; + + e2ap_msg_t msg = {.type = RIC_SUBSCRIPTION_REQUEST}; + msg.u_msgs.ric_sub_req.ric_id = ric_id; + msg.u_msgs.ric_sub_req.action = &action; + msg.u_msgs.ric_sub_req.len_action = 1; + return msg; +} + +int main() +{ + e2_agent_t* ag = calloc(1, sizeof(*ag)); + assert(ag != NULL); + + init_asio_agent(&ag->io); + int rc = pthread_mutex_init(&ag->mtx_ind_event, NULL); + assert(rc == 0); + bi_map_init(&ag->ind_event, sizeof(int), sizeof(ind_event_t), cmp_fd, cmp_ind_event, free_nop, free_nop); + + // A service model registry holding a single stub, as the agent's plugin loader would + sm_agent_t sm = {0}; + sm.proc.on_subscription = on_subscription_stub; + sm.info.id = id_stub; + + rc = pthread_mutex_init(&ag->plugin.sm_ds_mtx, NULL); + assert(rc == 0); + assoc_init(&ag->plugin.sm_ds, sizeof(uint16_t), cmp_ran_func_id_test, free_nop); + uint16_t const key = RAN_FUNC_ID; + assoc_insert(&ag->plugin.sm_ds, &key, sizeof(key), &sm); + + ric_gen_id_t const ric_id = {.ric_req_id = 123, .ric_inst_id = 0, .ran_func_id = RAN_FUNC_ID}; + e2ap_msg_t const msg = subscription_request(ric_id); + + e2ap_handle_subscription_request_agent(ag, &msg); + assert(subscribed == 1); + assert(bi_map_size(&ag->ind_event) == 1); + assert(released == 0); + + // The same RIC ID again: the first subscription is released, not kept beside it + e2ap_handle_subscription_request_agent(ag, &msg); + assert(subscribed == 2); + assert(bi_map_size(&ag->ind_event) == 1 && "the subscription was replaced, not duplicated"); + assert(released == 1 && "the replaced subscription was released"); + + // A different RIC ID is a new subscription + ric_gen_id_t const other = {.ric_req_id = 124, .ric_inst_id = 0, .ran_func_id = RAN_FUNC_ID}; + e2ap_msg_t const msg_other = subscription_request(other); + e2ap_handle_subscription_request_agent(ag, &msg_other); + assert(bi_map_size(&ag->ind_event) == 2); + assert(released == 1); + + printf("[TEST]: a subscription for an installed RIC ID replaces it\n"); + return 0; +}