diff --git a/examples/emulator/agent/sm_rc.c b/examples/emulator/agent/sm_rc.c index 776a6a4d..8d9d4e8d 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 59a4780b..2ff3a0d0 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 fed1209c..d26e6f48 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 f5d746af..3bd169d4 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 32622248..9e706fc1 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/endpoint_agent.c b/src/agent/endpoint_agent.c index f82ec73d..78fda80c 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/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 52570e3f..643424ad 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,40 +33,110 @@ 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; + +// 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); + *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 fd = out->fd; + ind_event_t* val = bi_map_extract_left(&ag->ind_event, &fd, sizeof(fd), NULL); + assert(val != NULL); + free(val); + + 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); - 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.ric_req_id); + 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"); } +} - 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); +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; +} + +// 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); - return true;; + 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]) @@ -114,7 +185,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; @@ -151,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); @@ -173,25 +254,34 @@ 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){ 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"); + ev.fd = fd; { 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"); } @@ -298,18 +388,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; } @@ -320,16 +433,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/src/agent/msg_handler_agent.h b/src/agent/msg_handler_agent.h index b885d447..1c3c417c 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 4d8e06e9..cf9f33fd 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 @@ -10,20 +11,72 @@ #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(notif != NULL); + + 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; + } +} + +// 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); - assert(msg != NULL && msg->type == SCTP_MSG_NOTIFICATION); + + stop_all_ind_events(ag); // 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); + // 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)); + 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); - printf("[E2 AGENT]: Communication with the nearRT-RIC lost\n" ); + 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/src/lib/ind_event.c b/src/lib/ind_event.c index c1d9a008..08cc4081 100644 --- a/src/lib/ind_event.c +++ b/src/lib/ind_event.c @@ -19,26 +19,16 @@ 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; -} - -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; + // 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) { assert(value != NULL); diff --git a/src/lib/ind_event.h b/src/lib/ind_event.h index 40c88b6f..3dd27ee2 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; @@ -26,15 +32,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 91fd174e..b84ef6b3 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 04a23241..95d3ca9f 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 79e9ff90..65077f4e 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 11a07648..f6cb67c5 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 f9843fac..c8161cd1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,10 @@ # SPDX-License-Identifier: LicenseRef-CSSL-1.0 +add_subdirectory(agent) 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) 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 6e09f3ee..481f7ed4 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 afcaadb4..25c9a589 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/agent/CMakeLists.txt b/test/agent/CMakeLists.txt new file mode 100644 index 00000000..37f49331 --- /dev/null +++ b/test/agent/CMakeLists.txt @@ -0,0 +1,60 @@ +# 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) + +##################################### +# Unit test: every way the SCTP association can end 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) + +##################################### +# 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_association_loss.c b/test/agent/test_association_loss.c new file mode 100644 index 00000000..02f6b276 --- /dev/null +++ b/test/agent/test_association_loss.c @@ -0,0 +1,213 @@ +/* + * SPDX-License-Identifier: LicenseRef-CSSL-1.0 + */ + +/* + * 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. +#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}; +} + +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 +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); +} + +// 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. +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 = assoc_change(&n, SCTP_COMM_LOST); + + 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 losses 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"); + + 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"); + return EXIT_SUCCESS; +} diff --git a/test/agent/test_replace_subscription.c b/test/agent/test_replace_subscription.c new file mode 100644 index 00000000..97cba6cb --- /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; +} diff --git a/test/agent/test_setup_response.c b/test/agent/test_setup_response.c new file mode 100644 index 00000000..3d8aada0 --- /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; +} diff --git a/test/aperiodic_routing/CMakeLists.txt b/test/aperiodic_routing/CMakeLists.txt new file mode 100644 index 00000000..b9b5028b --- /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 00000000..59b93e9b --- /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/ind_event_key/CMakeLists.txt b/test/ind_event_key/CMakeLists.txt new file mode 100644 index 00000000..766a2038 --- /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 00000000..acb2e996 --- /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; +} diff --git a/test/sm/rc_sm/main.c b/test/sm/rc_sm/main.c index 31dc8619..db2dc464 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