From 777daf12ef52d62d62d47a16220daea4a25b4317 Mon Sep 17 00:00:00 2001 From: Filippo Olimpieri Date: Tue, 1 Sep 2026 21:00:18 -0400 Subject: [PATCH 1/4] e2ap: fix the inverted comparison of modified RAN functions eq_ric_service_update() compared the entries of the modified list with if (eq_ran_function(&m0->modified[i], &m1->modified[i])) return false; so it reported two RIC Service Updates as different precisely when their modified lists matched, and as equal when they did not. The added and deleted lists next to it both test for == false; only this one is missing it. Present identically in v1_01, v2_03 and v3_01. It has gone unnoticed because the only caller is the encode/decode round trip test, and that test is not run: the RIC Service Update codec asserted "Untested code", so the test was commented out of main(). The next commit implements the codec and enables the test, which is how this surfaced. Co-authored-by: Andrea Lacava Assisted-by: Claude Opus 5 (1M context) Signed-off-by: Andrea Lacava --- src/lib/e2ap/v1_01/e2ap_types/ric_service_update.c | 2 +- src/lib/e2ap/v2_03/e2ap_types/ric_service_update.c | 2 +- src/lib/e2ap/v3_01/e2ap_types/ric_service_update.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.c b/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.c index 69e8c616e..234caa962 100644 --- a/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.c +++ b/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.c @@ -25,7 +25,7 @@ bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_upd return false; for(size_t i = 0; i < m0->len_modified; ++i){ - if(eq_ran_function(&m0->modified[i], &m1->modified[i])) + if(eq_ran_function(&m0->modified[i], &m1->modified[i]) == false) return false; } diff --git a/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.c b/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.c index 6c98532c4..6eccbe708 100644 --- a/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.c +++ b/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.c @@ -28,7 +28,7 @@ bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_upd return false; for(size_t i = 0; i < m0->len_modified; ++i){ - if(eq_ran_function(&m0->modified[i], &m1->modified[i])) + if(eq_ran_function(&m0->modified[i], &m1->modified[i]) == false) return false; } diff --git a/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.c b/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.c index 6c98532c4..6eccbe708 100644 --- a/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.c +++ b/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.c @@ -28,7 +28,7 @@ bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_upd return false; for(size_t i = 0; i < m0->len_modified; ++i){ - if(eq_ran_function(&m0->modified[i], &m1->modified[i])) + if(eq_ran_function(&m0->modified[i], &m1->modified[i]) == false) return false; } From c1c2a29b74aa32be5ae6ccf8d0bc9189217b6517 Mon Sep 17 00:00:00 2001 From: Filippo Olimpieri Date: Tue, 1 Sep 2026 21:00:38 -0400 Subject: [PATCH 2/4] e2ap: implement RIC Service Update encoding and decoding Both e2ap_enc_service_update_asn_pdu() and e2ap_dec_service_update() (and their Acknowledge counterparts) opened with assert(0 != 0 && "Untested code"); so the procedure could not be exercised at all. Beneath the assert the code did not match the ASN.1: - The mandatory TransactionID IE was never encoded, although the decoder read it from index 0 and E2AP marks it mandatory. - All three RAN-function lists were encoded unconditionally, so an update carrying only one of them still emitted two empty RANfunctions-Lists, violating SIZE(1..maxofRANfunctionID). - The decoder read the lists positionally, at fixed indices 1..3. Since every list is optional, any update that omitted one read the wrong IE, or an IE that was not there. - The deleted list built RANfunction-ItemIEs and appended them to ran_mod's list rather than ran_del's, using RANfunction-Item where the ASN.1 asks for RANfunctionID-Item. - RANfunctionsAdded and RANfunctionsModified are distinct CHOICE members of the same generated type (RANfunctions_List and RANfunctions_List_1). The encoder had them the other way round from the decoder. - In the Acknowledge, the accepted and rejected items were tagged ProtocolIE_ID_id_RANfunction_Item instead of ProtocolIE_ID_id_RANfunctionID_Item, and criticality was ignore where the ASN.1 says reject. The decoder now reads the TransactionID at index 0 and dispatches the remaining IEs by id rather than by position, which is what makes the optional lists work in any combination. test_service_update() and test_service_update_ack() already existed but were commented out of main(). They are enabled, given the transaction id to round trip, and the update test now fills all three lists at once -- filling only one, as before, would not have caught the added/modified member mix-up. test_service_update_empty() covers the other boundary: an update carrying nothing but the mandatory TransactionID. Co-authored-by: Andrea Lacava Assisted-by: Claude Opus 5 (1M context) Signed-off-by: Andrea Lacava --- src/lib/e2ap/v2_03/dec/e2ap_msg_dec_asn.c | 101 ++++++------- src/lib/e2ap/v2_03/enc/e2ap_msg_enc_asn.c | 136 ++++++++++-------- .../e2ap/v2_03/test_e2ap_enc_dec_asn.c | 75 +++++++--- 3 files changed, 177 insertions(+), 135 deletions(-) diff --git a/src/lib/e2ap/v2_03/dec/e2ap_msg_dec_asn.c b/src/lib/e2ap/v2_03/dec/e2ap_msg_dec_asn.c index f35d9086c..6cdad8328 100644 --- a/src/lib/e2ap/v2_03/dec/e2ap_msg_dec_asn.c +++ b/src/lib/e2ap/v2_03/dec/e2ap_msg_dec_asn.c @@ -1990,8 +1990,6 @@ e2ap_msg_t e2ap_dec_service_update(const E2AP_PDU_t* pdu) { assert(pdu != NULL); - assert(0!=0 && "Untested code"); - e2ap_msg_t ret = {.type = RIC_SERVICE_UPDATE}; ric_service_update_t* su = &ret.u_msgs.ric_serv_updt; @@ -2002,63 +2000,56 @@ e2ap_msg_t e2ap_dec_service_update(const E2AP_PDU_t* pdu) assert(pdu->choice.initiatingMessage->value.present == InitiatingMessage__value_PR_RICserviceUpdate); const RICserviceUpdate_t *out = &pdu->choice.initiatingMessage->value.choice.RICserviceUpdate; + assert(out->protocolIEs.list.count >= 1); - // TransactionID. Mandatory + // TransactionID. Mandatory (first IE) const RICserviceUpdate_IEs_t* trans_id = out->protocolIEs.list.array[0]; assert(trans_id->id == ProtocolIE_ID_id_TransactionID); assert(trans_id->criticality == Criticality_reject); assert(trans_id->value.present == RICserviceUpdate_IEs__value_PR_TransactionID); su->trans_id = trans_id->value.choice.TransactionID; - // List of RAN Functions Added - const RICserviceUpdate_IEs_t* ran_add = out->protocolIEs.list.array[1]; - assert(ran_add->id == ProtocolIE_ID_id_RANfunctionsAdded); - assert(ran_add->criticality == Criticality_reject); - assert(ran_add->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List); - const int sz_add = ran_add->value.choice.RANfunctions_List.list.count; - su->added = calloc(sz_add, sizeof(ran_function_t)); - su->len_added = sz_add; - for(int i = 0; i < sz_add; ++i){ - const RANfunction_ItemIEs_t* r = (const RANfunction_ItemIEs_t*)ran_add->value.choice.RANfunctions_List.list.array[i]; - assert(r->id == ProtocolIE_ID_id_RANfunction_Item); - assert(r->criticality == Criticality_reject); - assert(r->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item); - su->added[i] = copy_ran_function(&r->value.choice.RANfunction_Item); - } - - // List of RAN Functions Modified - const RICserviceUpdate_IEs_t* ran_mod = out->protocolIEs.list.array[2]; - assert(ran_mod->id == ProtocolIE_ID_id_RANfunctionsModified); - assert(ran_mod->criticality == Criticality_reject); - assert(ran_mod->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List); - - const int sz_mod = ran_mod->value.choice.RANfunctions_List.list.count; - su->modified = calloc(sz_mod, sizeof(ran_function_t)); - su->len_modified = sz_mod; - for(int i = 0; i < sz_mod; ++i){ - const RANfunction_ItemIEs_t* r = (const RANfunction_ItemIEs_t*)ran_mod->value.choice.RANfunctions_List.list.array[i]; - assert(r->id == ProtocolIE_ID_id_RANfunction_Item); - assert(r->criticality == Criticality_reject); - assert(r->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item); - su->modified[i] = copy_ran_function(&r->value.choice.RANfunction_Item); + // Remaining IEs are optional — iterate by ID + for (int elm = 1; elm < out->protocolIEs.list.count; ++elm) { + const RICserviceUpdate_IEs_t* ie = out->protocolIEs.list.array[elm]; + + if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) { + assert(ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List); + const int sz = ie->value.choice.RANfunctions_List.list.count; + su->added = calloc(sz, sizeof(ran_function_t)); + su->len_added = sz; + for (int i = 0; i < sz; ++i) { + const RANfunction_ItemIEs_t* r = (const RANfunction_ItemIEs_t*)ie->value.choice.RANfunctions_List.list.array[i]; + assert(r->id == ProtocolIE_ID_id_RANfunction_Item); + assert(r->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item); + su->added[i] = copy_ran_function(&r->value.choice.RANfunction_Item); + } + } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) { + assert(ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List_1); + const int sz = ie->value.choice.RANfunctions_List_1.list.count; + su->modified = calloc(sz, sizeof(ran_function_t)); + su->len_modified = sz; + for (int i = 0; i < sz; ++i) { + const RANfunction_ItemIEs_t* r = (const RANfunction_ItemIEs_t*)ie->value.choice.RANfunctions_List_1.list.array[i]; + assert(r->id == ProtocolIE_ID_id_RANfunction_Item); + assert(r->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item); + su->modified[i] = copy_ran_function(&r->value.choice.RANfunction_Item); + } + } else if (ie->id == ProtocolIE_ID_id_RANfunctionsDeleted) { + assert(ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List); + const int sz = ie->value.choice.RANfunctionsID_List.list.count; + su->deleted = calloc(sz, sizeof(e2ap_ran_function_id_rev_t)); + su->len_deleted = sz; + for (int i = 0; i < sz; ++i) { + const RANfunctionID_ItemIEs_t* r = (const RANfunctionID_ItemIEs_t*)ie->value.choice.RANfunctionsID_List.list.array[i]; + assert(r->id == ProtocolIE_ID_id_RANfunctionID_Item); + assert(r->value.present == RANfunctionID_ItemIEs__value_PR_RANfunctionID_Item); + su->deleted[i].id = r->value.choice.RANfunctionID_Item.ranFunctionID; + su->deleted[i].rev = r->value.choice.RANfunctionID_Item.ranFunctionRevision; + } + } } - // List of RAN Functions Deleted - const RICserviceUpdate_IEs_t* ran_del = out->protocolIEs.list.array[3]; - assert(ran_del->id == ProtocolIE_ID_id_RANfunctionsDeleted); - assert(ran_del->criticality == Criticality_reject); - assert(ran_del->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List); - const int sz_del = ran_del->value.choice.RANfunctions_List.list.count; - su->deleted = calloc(sz_del, sizeof(e2ap_ran_function_id_rev_t)); - su->len_deleted = sz_del; - for(int i = 0; i < sz_del; ++i){ - const RANfunction_ItemIEs_t* r = (const RANfunction_ItemIEs_t*)ran_del->value.choice.RANfunctions_List.list.array[i]; - assert(r->id == ProtocolIE_ID_id_RANfunction_Item); - assert(r->criticality == Criticality_reject); - assert(r->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item); - su->deleted[i].id = r->value.choice.RANfunction_Item.ranFunctionID; - su->deleted[i].rev = r->value.choice.RANfunction_Item.ranFunctionRevision; - } return ret; } @@ -2068,8 +2059,6 @@ e2ap_msg_t e2ap_dec_service_update_ack(const E2AP_PDU_t* pdu) { assert(pdu != NULL); - assert(0!=0 && "Untested code"); - e2ap_msg_t ret = {.type = RIC_SERVICE_UPDATE_ACKNOWLEDGE}; ric_service_update_ack_t* su = &ret.u_msgs.ric_serv_updt_ack; // Message Type. Mandatory @@ -2096,14 +2085,14 @@ e2ap_msg_t e2ap_dec_service_update_ack(const E2AP_PDU_t* pdu) if(up_ack->id == ProtocolIE_ID_id_RANfunctionsAccepted){ // List of RAN Functions Accepted RICserviceUpdateAcknowledge_IEs_t* update_ack = up_ack; - assert(update_ack->criticality == Criticality_ignore); + assert(update_ack->criticality == Criticality_reject); assert(update_ack->value.present == RICserviceUpdateAcknowledge_IEs__value_PR_RANfunctionsID_List); const int sz_acc = update_ack->value.choice.RANfunctionsID_List.list.count; su->accepted = calloc(sz_acc, sizeof(ran_function_id_t)); su->len_accepted = sz_acc; for(int i = 0; i < sz_acc; ++i){ const RANfunctionID_ItemIEs_t* r = (const RANfunctionID_ItemIEs_t*)update_ack->value.choice.RANfunctionsID_List.list.array[i]; - assert(r->id == ProtocolIE_ID_id_RANfunction_Item); + assert(r->id == ProtocolIE_ID_id_RANfunctionID_Item); assert(r->criticality == Criticality_reject); assert(r->value.present == RANfunctionID_ItemIEs__value_PR_RANfunctionID_Item); su->accepted[i].id = r->value.choice.RANfunctionID_Item.ranFunctionID; @@ -2113,13 +2102,13 @@ e2ap_msg_t e2ap_dec_service_update_ack(const E2AP_PDU_t* pdu) // List of RAN Functions Rejected RICserviceUpdateAcknowledge_IEs_t* func_reject_ie = up_ack; assert(func_reject_ie->id == ProtocolIE_ID_id_RANfunctionsRejected); - assert(func_reject_ie->criticality == Criticality_ignore); + assert(func_reject_ie->criticality == Criticality_reject); assert(func_reject_ie->value.present == RICserviceUpdateAcknowledge_IEs__value_PR_RANfunctionsID_List); const int sz_rej = func_reject_ie->value.choice.RANfunctionsIDcause_List.list.count; su->rejected = calloc(sz_rej, sizeof( rejected_ran_function_t)); for(int i = 0; i < sz_rej; ++i){ const RANfunctionIDcause_ItemIEs_t * r = (const RANfunctionIDcause_ItemIEs_t * )func_reject_ie->value.choice.RANfunctionsIDcause_List.list.array[i]; - assert(r->criticality == Criticality_ignore); + assert(r->criticality == Criticality_reject); assert(r->id == ProtocolIE_ID_id_RANfunctionIEcause_Item); assert(r->value.present == RANfunctionIDcause_ItemIEs__value_PR_RANfunctionIDcause_Item); diff --git a/src/lib/e2ap/v2_03/enc/e2ap_msg_enc_asn.c b/src/lib/e2ap/v2_03/enc/e2ap_msg_enc_asn.c index 19d74a1df..aa0c762a9 100644 --- a/src/lib/e2ap/v2_03/enc/e2ap_msg_enc_asn.c +++ b/src/lib/e2ap/v2_03/enc/e2ap_msg_enc_asn.c @@ -2054,13 +2054,10 @@ byte_array_t e2ap_enc_service_update_asn_msg(const e2ap_msg_t* msg) E2AP_PDU_t* e2ap_enc_service_update_asn_pdu(const ric_service_update_t* su) { assert(su->len_added <= (size_t)MAX_NUM_RAN_FUNC_ID); - assert(su->len_deleted <= (size_t)MAX_NUM_RAN_FUNC_ID ); + assert(su->len_deleted <= (size_t)MAX_NUM_RAN_FUNC_ID); assert(su->len_modified <= (size_t)MAX_NUM_RAN_FUNC_ID); - assert(0!=0 && "Untested code"); - - // Message Type - E2AP_PDU_t* pdu = calloc(1, sizeof( E2AP_PDU_t ) ); + E2AP_PDU_t* pdu = calloc(1, sizeof(E2AP_PDU_t)); pdu->present = E2AP_PDU_PR_initiatingMessage; pdu->choice.initiatingMessage = calloc(1, sizeof(InitiatingMessage_t)); pdu->choice.initiatingMessage->procedureCode = ProcedureCode_id_RICserviceUpdate; @@ -2069,62 +2066,76 @@ E2AP_PDU_t* e2ap_enc_service_update_asn_pdu(const ric_service_update_t* su) RICserviceUpdate_t *out = &pdu->choice.initiatingMessage->value.choice.RICserviceUpdate; - // List of RAN Functions Added - RICserviceUpdate_IEs_t* ran_add = calloc(1,sizeof(RICserviceUpdate_IEs_t)); - ran_add->id = ProtocolIE_ID_id_RANfunctionsAdded; - ran_add->criticality = Criticality_reject; - ran_add->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List; - - for(size_t i = 0; i < su->len_added; ++i){ - RANfunction_ItemIEs_t* r = calloc(1, sizeof(RANfunction_ItemIEs_t)); - r->id = ProtocolIE_ID_id_RANfunction_Item; - r->criticality = Criticality_reject; - r->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; - r->value.choice.RANfunction_Item = copy_ran_function(&su->added[i]); - int rc = ASN_SEQUENCE_ADD(&ran_add->value.choice.RANfunctions_List.list, r); + // Transaction ID. Mandatory + RICserviceUpdate_IEs_t* trans_id_ie = calloc(1, sizeof(RICserviceUpdate_IEs_t)); + trans_id_ie->id = ProtocolIE_ID_id_TransactionID; + trans_id_ie->criticality = Criticality_reject; + trans_id_ie->value.present = RICserviceUpdate_IEs__value_PR_TransactionID; + trans_id_ie->value.choice.TransactionID = su->trans_id; + int rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, trans_id_ie); + assert(rc == 0); + + // List of RAN Functions Added. Optional + if (su->len_added > 0) { + RICserviceUpdate_IEs_t* ran_add = calloc(1, sizeof(RICserviceUpdate_IEs_t)); + ran_add->id = ProtocolIE_ID_id_RANfunctionsAdded; + ran_add->criticality = Criticality_reject; + ran_add->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List; + + for (size_t i = 0; i < su->len_added; ++i) { + RANfunction_ItemIEs_t* r = calloc(1, sizeof(RANfunction_ItemIEs_t)); + r->id = ProtocolIE_ID_id_RANfunction_Item; + r->criticality = Criticality_reject; + r->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; + r->value.choice.RANfunction_Item = copy_ran_function(&su->added[i]); + int rc = ASN_SEQUENCE_ADD(&ran_add->value.choice.RANfunctions_List.list, r); + assert(rc == 0); + } + int rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, ran_add); assert(rc == 0); } - int rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list,ran_add); - assert(rc == 0); - // List of RAN Functions Modified - RICserviceUpdate_IEs_t* ran_mod = calloc(1,sizeof(RICserviceUpdate_IEs_t)); - ran_mod->id = ProtocolIE_ID_id_RANfunctionsModified; - ran_mod->criticality = Criticality_reject; - ran_mod->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List; - - for(size_t i = 0; i < su->len_modified; ++i){ - RANfunction_ItemIEs_t* r = calloc(1, sizeof(RANfunction_ItemIEs_t)); - r->id = ProtocolIE_ID_id_RANfunction_Item; - r->criticality = Criticality_reject; - r->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; - r->value.choice.RANfunction_Item = copy_ran_function(&su->modified[i]); - rc = ASN_SEQUENCE_ADD(&ran_mod->value.choice.RANfunctions_List.list, r); + // List of RAN Functions Modified. Optional + if (su->len_modified > 0) { + RICserviceUpdate_IEs_t* ran_mod = calloc(1, sizeof(RICserviceUpdate_IEs_t)); + ran_mod->id = ProtocolIE_ID_id_RANfunctionsModified; + ran_mod->criticality = Criticality_reject; + ran_mod->value.present = RICserviceUpdate_IEs__value_PR_RANfunctions_List_1; + + for (size_t i = 0; i < su->len_modified; ++i) { + RANfunction_ItemIEs_t* r = calloc(1, sizeof(RANfunction_ItemIEs_t)); + r->id = ProtocolIE_ID_id_RANfunction_Item; + r->criticality = Criticality_reject; + r->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; + r->value.choice.RANfunction_Item = copy_ran_function(&su->modified[i]); + rc = ASN_SEQUENCE_ADD(&ran_mod->value.choice.RANfunctions_List_1.list, r); + assert(rc == 0); + } + rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, ran_mod); assert(rc == 0); } - rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, ran_mod); - assert(rc == 0); - // List of RAN Functions Deleted - RICserviceUpdate_IEs_t* ran_del = calloc(1,sizeof(RICserviceUpdate_IEs_t)); - ran_del->id = ProtocolIE_ID_id_RANfunctionsDeleted; - ran_del->criticality = Criticality_reject; - ran_del->value.present = RICserviceUpdate_IEs__value_PR_RANfunctionsID_List; - - for(size_t i = 0; i < su->len_deleted; ++i){ - RANfunction_ItemIEs_t* r = calloc(1, sizeof(RANfunction_ItemIEs_t)); - r->id = ProtocolIE_ID_id_RANfunction_Item; - r->criticality = Criticality_reject; - r->value.present = RANfunction_ItemIEs__value_PR_RANfunction_Item; - RANfunction_Item_t* dst = &r->value.choice.RANfunction_Item; - const e2ap_ran_function_id_rev_t* src = &su->deleted[i]; - dst->ranFunctionID = src->id; - dst->ranFunctionRevision = src->rev; - rc = ASN_SEQUENCE_ADD(&ran_mod->value.choice.RANfunctions_List.list, r); + // List of RAN Functions Deleted. Optional + if (su->len_deleted > 0) { + RICserviceUpdate_IEs_t* ran_del = calloc(1, sizeof(RICserviceUpdate_IEs_t)); + ran_del->id = ProtocolIE_ID_id_RANfunctionsDeleted; + ran_del->criticality = Criticality_reject; + ran_del->value.present = RICserviceUpdate_IEs__value_PR_RANfunctionsID_List; + + for (size_t i = 0; i < su->len_deleted; ++i) { + RANfunctionID_ItemIEs_t* r = calloc(1, sizeof(RANfunctionID_ItemIEs_t)); + r->id = ProtocolIE_ID_id_RANfunctionID_Item; + r->criticality = Criticality_reject; + r->value.present = RANfunctionID_ItemIEs__value_PR_RANfunctionID_Item; + r->value.choice.RANfunctionID_Item.ranFunctionID = su->deleted[i].id; + r->value.choice.RANfunctionID_Item.ranFunctionRevision = su->deleted[i].rev; + rc = ASN_SEQUENCE_ADD(&ran_del->value.choice.RANfunctionsID_List.list, r); + assert(rc == 0); + } + rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, ran_del); assert(rc == 0); } - rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, ran_del); - assert(rc == 0); + return pdu; } @@ -2150,8 +2161,6 @@ E2AP_PDU_t* e2ap_enc_service_update_ack_asn_pdu(const ric_service_update_ack_t* assert(su->len_accepted <= (size_t)MAX_NUM_RAN_FUNC_ID); assert(su->len_rejected <= (size_t)MAX_NUM_RAN_FUNC_ID); - assert(0!=0 && "Untested code"); - // Message Type. Mandatory E2AP_PDU_t* pdu = calloc(1, sizeof(E2AP_PDU_t)); pdu->present = E2AP_PDU_PR_successfulOutcome; @@ -2162,15 +2171,24 @@ E2AP_PDU_t* e2ap_enc_service_update_ack_asn_pdu(const ric_service_update_ack_t* RICserviceUpdateAcknowledge_t* out = &pdu->choice.successfulOutcome->value.choice.RICserviceUpdateAcknowledge; + // TransactionID. Mandatory + RICserviceUpdateAcknowledge_IEs_t* trans_id_ie = calloc(1, sizeof(RICserviceUpdateAcknowledge_IEs_t)); + trans_id_ie->id = ProtocolIE_ID_id_TransactionID; + trans_id_ie->criticality = Criticality_reject; + trans_id_ie->value.present = RICserviceUpdateAcknowledge_IEs__value_PR_TransactionID; + trans_id_ie->value.choice.TransactionID = su->trans_id; + int rc = ASN_SEQUENCE_ADD(&out->protocolIEs.list, trans_id_ie); + assert(rc == 0); + if(su->len_accepted > 0){ // List of RAN Functions Accepted RICserviceUpdateAcknowledge_IEs_t* update_ack = calloc(1,sizeof(RICserviceUpdateAcknowledge_IEs_t)); update_ack->id = ProtocolIE_ID_id_RANfunctionsAccepted; - update_ack->criticality = Criticality_ignore; + update_ack->criticality = Criticality_reject; update_ack->value.present = RICserviceUpdateAcknowledge_IEs__value_PR_RANfunctionsID_List; for(size_t i = 0; i < su->len_accepted; ++i){ RANfunctionID_ItemIEs_t* r = calloc(1, sizeof( RANfunctionID_ItemIEs_t)); - r->id = ProtocolIE_ID_id_RANfunction_Item; + r->id = ProtocolIE_ID_id_RANfunctionID_Item; r->criticality = Criticality_reject; r->value.present = RANfunctionID_ItemIEs__value_PR_RANfunctionID_Item; r->value.choice.RANfunctionID_Item.ranFunctionID = su->accepted[i].id; @@ -2186,11 +2204,11 @@ E2AP_PDU_t* e2ap_enc_service_update_ack_asn_pdu(const ric_service_update_ack_t* // List of RAN Functions Rejected RICserviceUpdateAcknowledge_IEs_t* func_reject_ie = calloc(1,sizeof(RICserviceUpdateAcknowledge_IEs_t)); func_reject_ie->id = ProtocolIE_ID_id_RANfunctionsRejected; - func_reject_ie->criticality = Criticality_ignore; + func_reject_ie->criticality = Criticality_reject; func_reject_ie->value.present = RICserviceUpdateAcknowledge_IEs__value_PR_RANfunctionsID_List; for(size_t i =0; i < su->len_rejected; ++i){ RANfunctionIDcause_ItemIEs_t * r = calloc(1,sizeof(RANfunctionIDcause_ItemIEs_t)); - r->criticality = Criticality_ignore; + r->criticality = Criticality_reject; r->id = ProtocolIE_ID_id_RANfunctionIEcause_Item; r->value.present = RANfunctionIDcause_ItemIEs__value_PR_RANfunctionIDcause_Item; RANfunctionIDcause_Item_t* dst = &r->value.choice.RANfunctionIDcause_Item; diff --git a/test/encode_decode/e2ap/v2_03/test_e2ap_enc_dec_asn.c b/test/encode_decode/e2ap/v2_03/test_e2ap_enc_dec_asn.c index a47890799..39e51931b 100644 --- a/test/encode_decode/e2ap/v2_03/test_e2ap_enc_dec_asn.c +++ b/test/encode_decode/e2ap/v2_03/test_e2ap_enc_dec_asn.c @@ -666,39 +666,71 @@ void test_reset_response() void test_service_update() { - byte_array_t ba; - memset(&ba, 0, sizeof(byte_array_t)); + // Exercise all three lists at once: added and modified are distinct CHOICE + // members of the same ASN.1 type, and deleted is a RANfunctionsID-List, so a + // round trip that only fills one of them would not catch a member mix-up. const char* def = "This is a dummy definition"; - ba.buf = malloc(strlen(def)); - memcpy(ba.buf, def, strlen(def)); - ba.len = strlen(def); const size_t len_added = 1; - ran_function_t* added = calloc(len_added, sizeof(ran_function_t )); - added->id = 42; - added->rev = 0; - added->defn = ba; - - ran_function_t* modified = NULL; - const size_t len_modified = 0; - e2ap_ran_function_id_rev_t* deleted = NULL; - size_t len_deleted = 0; + ran_function_t* added = calloc(len_added, sizeof(ran_function_t)); + assert(added != NULL); + added[0].id = 42; + added[0].rev = 0; + added[0].defn = copy_str_to_ba(def); + added[0].oid = copy_str_to_ba("1.3.6.1.4.1.53148.1.1.2.2"); + + const size_t len_modified = 1; + ran_function_t* modified = calloc(len_modified, sizeof(ran_function_t)); + assert(modified != NULL); + modified[0].id = 143; + modified[0].rev = 7; + modified[0].defn = copy_str_to_ba(def); + modified[0].oid = copy_str_to_ba("1.3.6.1.4.1.53148.1.1.2.3"); + + const size_t len_deleted = 2; + e2ap_ran_function_id_rev_t* deleted = calloc(len_deleted, sizeof(e2ap_ran_function_id_rev_t)); + assert(deleted != NULL); + deleted[0].id = 144; + deleted[0].rev = 1; + deleted[1].id = 145; + deleted[1].rev = 2; ric_service_update_t su_begin = { + .trans_id = 5, .len_added = len_added, - .added = added, + .added = added, .modified = modified, - .len_modified = len_modified, + .len_modified = len_modified, .deleted = deleted, .len_deleted = len_deleted, }; E2AP_PDU_t* pdu = e2ap_enc_service_update_asn_pdu(&su_begin); e2ap_msg_t msg = e2ap_dec_service_update(pdu); - free_pdu(pdu); - assert(msg.type == RIC_SERVICE_UPDATE); + free_pdu(pdu); + assert(msg.type == RIC_SERVICE_UPDATE); + ric_service_update_t* su_end = &msg.u_msgs.ric_serv_updt; + + assert(su_end->trans_id == su_begin.trans_id); + assert(eq_ric_service_update(&su_begin, su_end) == true); + e2ap_free_service_update(&su_begin); + e2ap_free_service_update(su_end); +} + +// Only the mandatory TransactionID: every RAN-function list in a RIC Service +// Update is optional, and an update with none of them must still round trip. +void test_service_update_empty() +{ + ric_service_update_t su_begin = {.trans_id = 11}; + + E2AP_PDU_t* pdu = e2ap_enc_service_update_asn_pdu(&su_begin); + e2ap_msg_t msg = e2ap_dec_service_update(pdu); + free_pdu(pdu); + assert(msg.type == RIC_SERVICE_UPDATE); ric_service_update_t* su_end = &msg.u_msgs.ric_serv_updt; + assert(su_end->trans_id == su_begin.trans_id); + assert(su_end->len_added == 0 && su_end->len_modified == 0 && su_end->len_deleted == 0); assert(eq_ric_service_update(&su_begin, su_end) == true); e2ap_free_service_update(&su_begin); e2ap_free_service_update(su_end); @@ -715,6 +747,7 @@ void test_service_update_ack() size_t len_rejected = 0; ric_service_update_ack_t su_begin = { + .trans_id = 5, .accepted = accepted, .len_accepted = len_accepted, .rejected = rejected, @@ -727,6 +760,7 @@ void test_service_update_ack() assert(msg.type == RIC_SERVICE_UPDATE_ACKNOWLEDGE); ric_service_update_ack_t* su_end = &msg.u_msgs.ric_serv_updt_ack; + assert(su_end->trans_id == su_begin.trans_id); assert(eq_ric_service_update_ack(&su_begin, su_end) == true); e2ap_free_service_update_ack(&su_begin); e2ap_free_service_update_ack(su_end); @@ -1138,8 +1172,9 @@ int main(int argc, char* argv[]) //test_reset_request(); //test_reset_response(); - //test_service_update(); - //test_service_update_ack(); + test_service_update(); + test_service_update_empty(); + test_service_update_ack(); //test_service_update_failure(); //test_service_query(); From ac18084f70ca8a2ba676957c9503bc857330c681 Mon Sep 17 00:00:00 2001 From: Filippo Olimpieri Date: Tue, 1 Sep 2026 21:01:22 -0400 Subject: [PATCH 3/4] msg_hand: do not abort on a service model the build does not model mv_rd_e2_setup() ended in assert(0 != 0 && "Unknown types"), so an E2 SETUP answer whose service-model definition type is not one of the eight handled here takes the whole process down. That is reachable without anything being wrong: the plugin directory is scanned, not enumerated, so a service model left in libs_dir by another install is loaded and answers E2 SETUP with a type this build has no case for. It is also what a build configured with a different service-model set hits against an agent that has more. Leave dst zeroed instead. A service model is selected by RAN-function id when a subscription arrives, so one that is not modelled here is simply never chosen; there is no path that consumes the zeroed definition. Co-authored-by: Andrea Lacava Assisted-by: Claude Opus 5 (1M context) Signed-off-by: Andrea Lacava --- src/lib/msg_hand/reg_e2_nodes.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/msg_hand/reg_e2_nodes.c b/src/lib/msg_hand/reg_e2_nodes.c index 4a5084a4b..adec16913 100644 --- a/src/lib/msg_hand/reg_e2_nodes.c +++ b/src/lib/msg_hand/reg_e2_nodes.c @@ -290,7 +290,10 @@ sm_ran_function_def_t mv_rd_e2_setup(sm_ag_if_rd_e2setup_t const* src) dst.type = RC_RAN_FUNC_DEF_E; dst.rc = src->rc.ran_func_def; } else { - assert(0!=0 && "Unknown types"); + /* Tolerate service-model definition types this build does not model instead + of aborting the E2 node: subscriptions target a service model by + RAN-function id, so a type that is not modelled here is simply never + selected. dst stays zeroed. */ } return dst; @@ -501,4 +504,3 @@ void rm_reg_e2_node(reg_e2_nodes_t* n, global_e2_node_id_t const* id) assoc_rb_tree_free_it(&n->node_to_rf, it); } } - From 6167100643f876f58825068b4dd2104f2ca31d43 Mon Sep 17 00:00:00 2001 From: Filippo Olimpieri Date: Tue, 1 Sep 2026 21:01:50 -0400 Subject: [PATCH 4/4] e2ap: complete the RIC Service Update procedure With the codec in place the procedure still had no implementation on either side: e2ap_handle_service_update_ric() was assert(0 != 0 && "Not Implemented"), the agent had no way to send an update, and RIC_SERVICE_UPDATE reached neither the iApp dispatch table nor the E2 node registry. A service model whose RAN function definition depends on state the RAN only learns after E2 SETUP has no other way to tell the RIC: E2 SETUP happens once per connection. This closes that gap. Agent side: - e2_send_ric_service_update() encodes and sends the update. - trigger_ric_service_update_api(ran_func_id) re-advertises one RAN function with a monotonically increasing revision, which is what makes the RIC replace the stored definition instead of rejecting a duplicate. It takes the RAN function id rather than picking a service model itself, so it stays independent of which service models a deployment loads. - e2ap_handle_service_update_ack_agent() reports the outcome and names any RAN function the RIC rejected. RIC side: - e2ap_handle_service_update_ric() applies the added and deleted lists to the E2 node's accepted set and answers with a RIC Service Update Acknowledge listing what it took. - The update carries no node id, so near_ric resolves it from the socket the message arrived on, via the new reverse lookup find_map_sad_e2_node(). That lookup returns false rather than asserting when the socket has no node registered, and an update for an unknown node is logged and dropped: this path is driven by whatever arrives on the SCTP socket, so an assertion there would let a stale or malformed update from any peer take the RIC down. - The update is forwarded to the iApp, which applies it to the E2 node registry through the new update_reg_e2_node(), so an xApp connecting afterwards sees the current RAN function list. RIC Service Update Failure is still unimplemented; a rejected update is reported through the Acknowledge's rejected list. Co-authored-by: Andrea Lacava Assisted-by: Claude Opus 5 (1M context) Signed-off-by: Andrea Lacava --- src/agent/e2_agent.c | 12 +++ src/agent/e2_agent.h | 2 + src/agent/e2_agent_api.c | 48 ++++++++++ src/agent/e2_agent_api.h | 3 + src/agent/msg_handler_agent.c | 11 ++- .../v1_01/e2ap_types/ric_service_update.h | 3 + src/lib/e2ap/v1_01/free/e2ap_msg_free.c | 2 + .../v2_03/e2ap_types/ric_service_update.h | 3 + src/lib/e2ap/v2_03/free/e2ap_msg_free.c | 2 + .../v3_01/e2ap_types/ric_service_update.h | 3 + src/lib/e2ap/v3_01/free/e2ap_msg_free.c | 2 + src/lib/msg_hand/reg_e2_nodes.c | 59 +++++++++++++ src/lib/msg_hand/reg_e2_nodes.h | 15 ++++ src/ric/iApp/e42_iapp.c | 3 +- src/ric/iApp/msg_handler_iapp.c | 27 +++++- src/ric/iApp/msg_handler_iapp.h | 3 + src/ric/map_e2_node_sockaddr.c | 20 +++++ src/ric/map_e2_node_sockaddr.h | 5 ++ src/ric/msg_handler_ric.c | 88 ++++++++++++++++++- src/ric/near_ric.c | 11 ++- 20 files changed, 313 insertions(+), 9 deletions(-) diff --git a/src/agent/e2_agent.c b/src/agent/e2_agent.c index 59a4780b4..2f8ac5952 100644 --- a/src/agent/e2_agent.c +++ b/src/agent/e2_agent.c @@ -619,3 +619,15 @@ void e2_send_control_failure(e2_agent_t* ag, const ric_control_failure_t* cf) free_byte_array(ba); } +void e2_send_ric_service_update(e2_agent_t* ag, const ric_service_update_t* su) +{ + assert(ag != NULL); + assert(su != NULL); + + e2ap_msg_t msg = {.type = RIC_SERVICE_UPDATE}; + msg.u_msgs.ric_serv_updt = *su; + + byte_array_t ba = e2ap_msg_enc_ag(&ag->ap, &msg); + e2ap_send_bytes_agent(&ag->ep, ba); + free_byte_array(ba); +} diff --git a/src/agent/e2_agent.h b/src/agent/e2_agent.h index fed1209c9..0283f3a76 100644 --- a/src/agent/e2_agent.h +++ b/src/agent/e2_agent.h @@ -97,6 +97,8 @@ void e2_send_control_acknowledge(e2_agent_t* ag, const ric_control_acknowledge_t void e2_send_control_failure(e2_agent_t* ag, const ric_control_failure_t* cf); +void e2_send_ric_service_update(e2_agent_t* ag, const ric_service_update_t* su); + //////////////////////////////////////////////// #undef NUM_HANDLE_MSG diff --git a/src/agent/e2_agent_api.c b/src/agent/e2_agent_api.c index f5d746aff..b857a6d10 100644 --- a/src/agent/e2_agent_api.c +++ b/src/agent/e2_agent_api.c @@ -6,6 +6,8 @@ #include // for assert #include // for pthread_cr... +#include +#include // for strdup, strlen #include #include // for NULL #include "e2_agent.h" // for e2_free_agent @@ -112,3 +114,49 @@ void async_event_agent_api(uint32_t ric_req_id, void* ind_data) e2_async_event_agent(agent, ric_req_id, ind_data); } +/* Re-advertise one RAN function to the RIC with a bumped revision. + * + * A service model whose RAN function definition changes at runtime -- because + * what it exposes depends on state the RAN learns after E2 SETUP -- has no way + * to tell the RIC without this: E2 SETUP happens once. The revision is + * per-call and monotonic, which is what makes the RIC replace the stored + * definition rather than reject it as a duplicate. */ +void trigger_ric_service_update_api(uint16_t ran_func_id) +{ + if (agent == NULL) { + printf("[E2 AGENT]: not initialised yet, skipping RIC Service Update\n"); + return; + } + + sm_agent_t* sm = sm_plugin_ag(&agent->plugin, ran_func_id); + if (sm == NULL) { + printf("[E2 AGENT]: no service model for RAN function %u, cannot send RIC Service Update\n", ran_func_id); + return; + } + + static _Atomic uint16_t service_update_rev = 1; + + sm_e2_setup_data_t setup = sm->proc.on_e2_setup(sm); + + ric_service_update_t su = {0}; + su.trans_id = (uint8_t)(atomic_load(&service_update_rev) % 256); + + su.len_modified = 1; + su.modified = calloc(1, sizeof(ran_function_t)); + assert(su.modified != NULL && "Memory exhausted"); + + su.modified[0].id = sm->info.id(); + su.modified[0].rev = atomic_fetch_add(&service_update_rev, 1); + su.modified[0].defn.buf = setup.ran_fun_def; + su.modified[0].defn.len = setup.len_rfd; + + const char* oid = sm->info.oid(); + su.modified[0].oid.buf = (uint8_t*)strdup(oid); + su.modified[0].oid.len = strlen(oid); + + e2_send_ric_service_update(agent, &su); + + free(su.modified[0].defn.buf); + free(su.modified[0].oid.buf); + free(su.modified); +} diff --git a/src/agent/e2_agent_api.h b/src/agent/e2_agent_api.h index 32622248b..4494264ca 100644 --- a/src/agent/e2_agent_api.h +++ b/src/agent/e2_agent_api.h @@ -8,6 +8,7 @@ #include "../sm/sm_io.h" #include "../util/conf_file.h" #include "../util/ngran_types.h" +#include void init_agent_api(int mcc, int mnc, @@ -22,5 +23,7 @@ void stop_agent_api(void); void async_event_agent_api(uint32_t ric_req_id, void* ind_data); +void trigger_ric_service_update_api(uint16_t ran_func_id); + #endif diff --git a/src/agent/msg_handler_agent.c b/src/agent/msg_handler_agent.c index 52570e3f0..8e2b33fa7 100644 --- a/src/agent/msg_handler_agent.c +++ b/src/agent/msg_handler_agent.c @@ -426,7 +426,16 @@ e2ap_msg_t e2ap_handle_service_update_ack_agent(e2_agent_t* ag, const e2ap_msg_t { assert(ag != NULL); assert(msg != NULL); - assert(0!=0 && "Not implemented"); + assert(msg->type == RIC_SERVICE_UPDATE_ACKNOWLEDGE); + + const ric_service_update_ack_t* ack = &msg->u_msgs.ric_serv_updt_ack; + printf("[E2-AGENT]: RIC SERVICE UPDATE ACKNOWLEDGE rx (accepted=%zu, rejected=%zu)\n", + ack->len_accepted, ack->len_rejected); + + for (size_t i = 0; i < ack->len_rejected; ++i) { + printf("[E2-AGENT]: RAN function %d REJECTED by the RIC, cause group %d\n", + ack->rejected[i].id, ack->rejected[i].cause.present); + } e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; return ans; diff --git a/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.h b/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.h index eb0981971..4f72db957 100644 --- a/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.h +++ b/src/lib/e2ap/v1_01/e2ap_types/ric_service_update.h @@ -9,6 +9,7 @@ #include "common/e2ap_ran_function.h" #include "common/e2ap_ran_function_id_rev.h" +#include "common/e2ap_global_node_id.h" typedef struct { @@ -20,6 +21,8 @@ typedef struct { e2ap_ran_function_id_rev_t* deleted; size_t len_deleted; + + global_e2_node_id_t e2_node_id; } ric_service_update_t; bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_update_t* m1); diff --git a/src/lib/e2ap/v1_01/free/e2ap_msg_free.c b/src/lib/e2ap/v1_01/free/e2ap_msg_free.c index abfc82426..1b5daaab9 100644 --- a/src/lib/e2ap/v1_01/free/e2ap_msg_free.c +++ b/src/lib/e2ap/v1_01/free/e2ap_msg_free.c @@ -400,6 +400,8 @@ void e2ap_free_service_update(ric_service_update_t* su) free(su->modified); free(su->deleted); + + free_global_e2_node_id(&su->e2_node_id); } // RIC -> E2 diff --git a/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.h b/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.h index a0ca81bcf..fec4dbf59 100644 --- a/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.h +++ b/src/lib/e2ap/v2_03/e2ap_types/ric_service_update.h @@ -9,6 +9,7 @@ #include "common/e2ap_ran_function.h" #include "common/e2ap_ran_function_id_rev.h" +#include "common/e2ap_global_node_id.h" typedef struct { uint8_t trans_id; @@ -21,6 +22,8 @@ typedef struct { e2ap_ran_function_id_rev_t* deleted; size_t len_deleted; + + global_e2_node_id_t e2_node_id; } ric_service_update_t; bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_update_t* m1); diff --git a/src/lib/e2ap/v2_03/free/e2ap_msg_free.c b/src/lib/e2ap/v2_03/free/e2ap_msg_free.c index d58e79cf4..054c1b35d 100644 --- a/src/lib/e2ap/v2_03/free/e2ap_msg_free.c +++ b/src/lib/e2ap/v2_03/free/e2ap_msg_free.c @@ -408,6 +408,8 @@ void e2ap_free_service_update(ric_service_update_t* su) free(su->modified); free(su->deleted); + + free_global_e2_node_id(&su->e2_node_id); } // RIC -> E2 diff --git a/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.h b/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.h index a0ca81bcf..fec4dbf59 100644 --- a/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.h +++ b/src/lib/e2ap/v3_01/e2ap_types/ric_service_update.h @@ -9,6 +9,7 @@ #include "common/e2ap_ran_function.h" #include "common/e2ap_ran_function_id_rev.h" +#include "common/e2ap_global_node_id.h" typedef struct { uint8_t trans_id; @@ -21,6 +22,8 @@ typedef struct { e2ap_ran_function_id_rev_t* deleted; size_t len_deleted; + + global_e2_node_id_t e2_node_id; } ric_service_update_t; bool eq_ric_service_update(const ric_service_update_t* m0, const ric_service_update_t* m1); diff --git a/src/lib/e2ap/v3_01/free/e2ap_msg_free.c b/src/lib/e2ap/v3_01/free/e2ap_msg_free.c index f2a551b7a..cfad44ea8 100644 --- a/src/lib/e2ap/v3_01/free/e2ap_msg_free.c +++ b/src/lib/e2ap/v3_01/free/e2ap_msg_free.c @@ -406,6 +406,8 @@ void e2ap_free_service_update(ric_service_update_t* su) free(su->modified); free(su->deleted); + + free_global_e2_node_id(&su->e2_node_id); } // RIC -> E2 diff --git a/src/lib/msg_hand/reg_e2_nodes.c b/src/lib/msg_hand/reg_e2_nodes.c index adec16913..c444acdb9 100644 --- a/src/lib/msg_hand/reg_e2_nodes.c +++ b/src/lib/msg_hand/reg_e2_nodes.c @@ -504,3 +504,62 @@ void rm_reg_e2_node(reg_e2_nodes_t* n, global_e2_node_id_t const* id) assoc_rb_tree_free_it(&n->node_to_rf, it); } } + +void update_reg_e2_node(reg_e2_nodes_t* n, + global_e2_node_id_t const* id, + size_t len_added, ran_function_t const* added, + size_t len_modified, ran_function_t const* modified, + size_t len_deleted, e2ap_ran_function_id_rev_t const* deleted) +{ + assert(n != NULL); + assert(id != NULL); + + lock_guard(&n->mtx); + + void* it = assoc_front(&n->node_to_rf); + void* end = assoc_end(&n->node_to_rf); + it = find_if(&n->node_to_rf, it, end, id, eq_global_e2_node_id_wrapper); + assert(it != end && "E2 Node not found in reg_e2_nodes"); + + pair_rf_cca_t* rf_cca = assoc_value(&n->node_to_rf, it); + seq_arr_t* arr = &rf_cca->ran_func; + + for (size_t i = 0; i < len_deleted; ++i) { + void* rf_it = seq_front(arr); + void* rf_end = seq_end(arr); + while (rf_it != rf_end) { + ran_function_t* rf = (ran_function_t*)rf_it; + if (rf->id == deleted[i].id) { + void* rf_next = seq_next(arr, rf_it); + free_ran_function(rf); + seq_erase(arr, rf_it, rf_next); + break; + } + rf_it = seq_next(arr, rf_it); + } + } + + for (size_t i = 0; i < len_modified; ++i) { + void* rf_it = seq_front(arr); + void* rf_end = seq_end(arr); + while (rf_it != rf_end) { + ran_function_t* rf = (ran_function_t*)rf_it; + if (rf->id == modified[i].id) { + free_ran_function(rf); + *rf = cp_ran_function(&modified[i]); + break; + } + rf_it = seq_next(arr, rf_it); + } + } + + for (size_t i = 0; i < len_added; ++i) { + ran_function_t tmp = cp_ran_function(&added[i]); + seq_push_back(arr, &tmp, sizeof(ran_function_t)); + } + + printf("[REG-E2-NODES]: Updated node MCC %d MNC %d NB_ID %u " + "(added=%zu, modified=%zu, deleted=%zu)\n", + id->plmn.mcc, id->plmn.mnc, id->nb_id.nb_id, + len_added, len_modified, len_deleted); +} diff --git a/src/lib/msg_hand/reg_e2_nodes.h b/src/lib/msg_hand/reg_e2_nodes.h index 1f5092759..7dd5ca9cd 100644 --- a/src/lib/msg_hand/reg_e2_nodes.h +++ b/src/lib/msg_hand/reg_e2_nodes.h @@ -10,6 +10,15 @@ #include "../e2ap/e2_node_connected_wrapper.h" #include "e2_node_arr.h" #include "../../xApp/e2_node_arr_xapp.h" +#ifdef E2AP_V1 +#include "../e2ap/v1_01/e2ap_types/common/e2ap_ran_function_id_rev.h" +#elif defined(E2AP_V2) +#include "../e2ap/v2_03/e2ap_types/common/e2ap_ran_function_id_rev.h" +#elif defined(E2AP_V3) +#include "../e2ap/v3_01/e2ap_types/common/e2ap_ran_function_id_rev.h" +#else +static_assert(0!=0, "Unknown E2AP Version"); +#endif #include #include @@ -52,5 +61,11 @@ e2_node_arr_t generate_e2_node_arr(reg_e2_nodes_t* n); e2_node_arr_xapp_t generate_e2_node_arr_xapp(reg_e2_nodes_t* n, plugin_ric_t const* plg_ric); +void update_reg_e2_node(reg_e2_nodes_t* n, + global_e2_node_id_t const* id, + size_t len_added, ran_function_t const* added, + size_t len_modified, ran_function_t const* modified, + size_t len_deleted, e2ap_ran_function_id_rev_t const* deleted); + #endif diff --git a/src/ric/iApp/e42_iapp.c b/src/ric/iApp/e42_iapp.c index c20774172..97b11b74f 100644 --- a/src/ric/iApp/e42_iapp.c +++ b/src/ric/iApp/e42_iapp.c @@ -342,7 +342,8 @@ void notify_msg_iapp(e42_iapp_t* iapp, e2ap_msg_t const* msg) || msg->type == RIC_SUBSCRIPTION_RESPONSE || msg->type == RIC_SUBSCRIPTION_DELETE_RESPONSE || msg->type == RIC_CONTROL_ACKNOWLEDGE - || msg->type == RIC_CONTROL_FAILURE); + || msg->type == RIC_CONTROL_FAILURE + || msg->type == RIC_SERVICE_UPDATE); e2ap_msg_t ans = e2ap_msg_handle_iapp(iapp, msg); diff --git a/src/ric/iApp/msg_handler_iapp.c b/src/ric/iApp/msg_handler_iapp.c index 6ff451132..98396654b 100644 --- a/src/ric/iApp/msg_handler_iapp.c +++ b/src/ric/iApp/msg_handler_iapp.c @@ -12,6 +12,7 @@ #include "util/compare.h" #include "util/alg_ds/ds/lock_guard/lock_guard.h" #include "util/time_now_us.h" +#include "../../lib/msg_hand/reg_e2_nodes.h" #include "iapp_if_generic.h" #include "xapp_ric_id.h" @@ -33,7 +34,8 @@ bool check_valid_msg_type(e2_msg_type_t msg_type ) || msg_type == RIC_CONTROL_ACKNOWLEDGE || msg_type == RIC_INDICATION || msg_type == RIC_SUBSCRIPTION_DELETE_RESPONSE - || msg_type == RIC_CONTROL_FAILURE; + || msg_type == RIC_CONTROL_FAILURE + || msg_type == RIC_SERVICE_UPDATE; } void init_handle_msg_iapp(size_t len, handle_msg_fp_iapp (*handle_msg)[len]) @@ -51,6 +53,7 @@ void init_handle_msg_iapp(size_t len, handle_msg_fp_iapp (*handle_msg)[len]) (*handle_msg)[RIC_CONTROL_FAILURE] = e2ap_handle_e42_ric_control_failure_iapp; (*handle_msg)[RIC_INDICATION] = e2ap_handle_ric_indication_iapp; (*handle_msg)[RIC_SUBSCRIPTION_DELETE_RESPONSE] = e2ap_handle_subscription_delete_response_iapp; + (*handle_msg)[RIC_SERVICE_UPDATE] = e2ap_handle_ric_service_update_iapp; // (*handle_msg)[RIC_SUBSCRIPTION_REQUEST] = e2ap_handle_subscription_request_iapp; // (*handle_msg)[RIC_SUBSCRIPTION_DELETE_REQUEST] = e2ap_handle_subscription_delete_request_iapp; @@ -491,7 +494,27 @@ e2ap_msg_t e2ap_handle_reset_response_iapp(e42_iapp_t* iapp, const e2ap_msg_t* m e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; return ans; } - + +e2ap_msg_t e2ap_handle_ric_service_update_iapp(e42_iapp_t* iapp, const e2ap_msg_t* msg) +{ + assert(iapp != NULL); + assert(msg != NULL); + assert(msg->type == RIC_SERVICE_UPDATE); + + ric_service_update_t const* su = &msg->u_msgs.ric_serv_updt; + + printf("[iApp]: RIC SERVICE UPDATE rx (added=%zu, modified=%zu, deleted=%zu)\n", + su->len_added, su->len_modified, su->len_deleted); + + update_reg_e2_node(&iapp->e2_nodes, &su->e2_node_id, + su->len_added, su->added, + su->len_modified, su->modified, + su->len_deleted, su->deleted); + + e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; + return ans; +} + e2ap_msg_t e2ap_handle_service_update_ack_iapp(e42_iapp_t* iapp, const e2ap_msg_t* msg) { assert(iapp != NULL); diff --git a/src/ric/iApp/msg_handler_iapp.h b/src/ric/iApp/msg_handler_iapp.h index eca7ab11b..53b3059cc 100644 --- a/src/ric/iApp/msg_handler_iapp.h +++ b/src/ric/iApp/msg_handler_iapp.h @@ -104,5 +104,8 @@ e2ap_msg_t e2ap_handle_e42_ric_control_ack_iapp(e42_iapp_t* iapp, const e2ap_msg // iApp -> xApp e2ap_msg_t e2ap_handle_e42_ric_control_failure_iapp(e42_iapp_t* iapp, const e2ap_msg_t* msg); +// RIC -> iApp (forwarded from near_ric msg handler) +e2ap_msg_t e2ap_handle_ric_service_update_iapp(e42_iapp_t* iapp, const e2ap_msg_t* msg); + #endif diff --git a/src/ric/map_e2_node_sockaddr.c b/src/ric/map_e2_node_sockaddr.c index 50ef4e8dd..65e0e5252 100644 --- a/src/ric/map_e2_node_sockaddr.c +++ b/src/ric/map_e2_node_sockaddr.c @@ -143,3 +143,23 @@ sctp_info_t find_map_e2_node_sad(map_e2_node_sockaddr_t* m, global_e2_node_id_t return *s; } +bool find_map_sad_e2_node(map_e2_node_sockaddr_t* m, sctp_info_t const* s, global_e2_node_id_t* id) +{ + assert(m != NULL); + assert(s != NULL); + assert(id != NULL); + + lock_guard(&m->mtx); + + assoc_rb_tree_t* tree = &m->map.right; + + void* it = assoc_front(tree); + void* end = assoc_end(tree); + + it = find_if(tree, it, end, (sctp_info_t*)s, eq_sctp_info_wrapper); + if (it == end) + return false; + + *id = cp_global_e2_node_id(assoc_value(tree, it)); + return true; +} diff --git a/src/ric/map_e2_node_sockaddr.h b/src/ric/map_e2_node_sockaddr.h index 4e205055e..e96190bff 100644 --- a/src/ric/map_e2_node_sockaddr.h +++ b/src/ric/map_e2_node_sockaddr.h @@ -40,5 +40,10 @@ sctp_info_t find_map_e2_node_sad(map_e2_node_sockaddr_t* m, global_e2_node_id_t sctp_info_t find_map_e2_node_sad(map_e2_node_sockaddr_t * m, global_e2_node_id_t const* id); +/* Reverse lookup: the E2 node registered for a socket. Returns false when the + socket has no node registered, rather than asserting -- the caller is driven + by inbound SCTP traffic. */ +bool find_map_sad_e2_node(map_e2_node_sockaddr_t* m, sctp_info_t const* s, global_e2_node_id_t* id); + #endif diff --git a/src/ric/msg_handler_ric.c b/src/ric/msg_handler_ric.c index 18195a499..1f83c3101 100644 --- a/src/ric/msg_handler_ric.c +++ b/src/ric/msg_handler_ric.c @@ -366,14 +366,96 @@ void publish_ind_msg(near_ric_t* ric, uint16_t ran_func_id, sm_ag_if_rd_ind_t* } // E2 -> RIC - e2ap_msg_t e2ap_handle_service_update_ric(near_ric_t* ric, const e2ap_msg_t* msg) +e2ap_msg_t e2ap_handle_service_update_ric(near_ric_t* ric, const e2ap_msg_t* msg) { assert(ric != NULL); assert(msg != NULL); assert(msg->type == RIC_SERVICE_UPDATE); - assert(0 != 0 && "Not Implemented"); - e2ap_msg_t ans = {.type = NONE_E2_MSG_TYPE}; + const ric_service_update_t* su = &msg->u_msgs.ric_serv_updt; + + printf("[NEAR-RIC]: RIC SERVICE UPDATE rx (added=%zu, modified=%zu, deleted=%zu)\n", + su->len_added, su->len_modified, su->len_deleted); + { + lock_guard(&ric->conn_e2_nodes_mtx); + + e2_node_t* node = NULL; + void* it = seq_front(&ric->conn_e2_nodes); + void* end_it = seq_end(&ric->conn_e2_nodes); + while (it != end_it) { + e2_node_t* n = (e2_node_t*)it; + if (eq_global_e2_node_id(&n->id, &su->e2_node_id)) { + node = n; + break; + } + it = seq_next(&ric->conn_e2_nodes, it); + } + if (node == NULL) { + /* The update names a node the RIC has no E2 SETUP for. Dropping it is + better than asserting: this path is driven by whatever arrives on the + SCTP socket, so an assertion here lets a malformed or stale update from + any peer take the RIC down. */ + printf("[NEAR-RIC]: RIC SERVICE UPDATE for an unknown E2 node, dropped\n"); + e2ap_msg_t none = {.type = NONE_E2_MSG_TYPE}; + return none; + } + + for (size_t i = 0; i < su->len_deleted; ++i) { + for (size_t j = 0; j < node->len_acc; ++j) { + if (node->accepted[j] == su->deleted[i].id) { + memmove(&node->accepted[j], &node->accepted[j + 1], + (node->len_acc - j - 1) * sizeof(accepted_ran_function_t)); + node->len_acc--; + break; + } + } + } + + if (su->len_added > 0) { + size_t new_len = node->len_acc + su->len_added; + node->accepted = realloc(node->accepted, new_len * sizeof(accepted_ran_function_t)); + assert(node->accepted != NULL && "Memory exhausted"); + for (size_t i = 0; i < su->len_added; ++i) { + node->accepted[node->len_acc + i] = su->added[i].id; + } + node->len_acc = new_len; + } + + } + +#ifndef TEST_AGENT_RIC + notify_msg_iapp_api(msg); +#endif + + e2ap_msg_t ans = {.type = RIC_SERVICE_UPDATE_ACKNOWLEDGE}; + ric_service_update_ack_t* ack = &ans.u_msgs.ric_serv_updt_ack; + ack->trans_id = su->trans_id; + ack->len_accepted = 0; + ack->accepted = NULL; + ack->len_rejected = 0; + ack->rejected = NULL; + + if (su->len_modified > 0) { + ack->len_accepted = su->len_modified; + ack->accepted = calloc(su->len_modified, sizeof(ran_function_id_t)); + assert(ack->accepted != NULL && "Memory exhausted"); + for (size_t i = 0; i < su->len_modified; ++i) { + ack->accepted[i].id = su->modified[i].id; + ack->accepted[i].rev = su->modified[i].rev; + } + } + + if (su->len_added > 0) { + size_t prev = ack->len_accepted; + ack->len_accepted += su->len_added; + ack->accepted = realloc(ack->accepted, ack->len_accepted * sizeof(ran_function_id_t)); + assert(ack->accepted != NULL && "Memory exhausted"); + for (size_t i = 0; i < su->len_added; ++i) { + ack->accepted[prev + i].id = su->added[i].id; + ack->accepted[prev + i].rev = su->added[i].rev; + } + } + return ans; } diff --git a/src/ric/near_ric.c b/src/ric/near_ric.c index aae2a2c23..5634be38e 100644 --- a/src/ric/near_ric.c +++ b/src/ric/near_ric.c @@ -389,8 +389,8 @@ void sctp_msg_arrived_event(void* arg) sctp_msg_t const* sctp_msg = &ric_ev->msg; defer({free_sctp_msg((sctp_msg_t*)sctp_msg);}); - e2ap_msg_t const msg = e2ap_msg_dec_ric(&ric->ap, sctp_msg->ba); - defer({e2ap_msg_free_ric(&ric->ap, (e2ap_msg_t*)&msg); } ); + e2ap_msg_t msg = e2ap_msg_dec_ric(&ric->ap, sctp_msg->ba); + defer({e2ap_msg_free_ric(&ric->ap, &msg); } ); if(msg.type == E2_SETUP_REQUEST){ global_e2_node_id_t const* id = &msg.u_msgs.e2_stp_req.id; @@ -398,6 +398,13 @@ void sctp_msg_arrived_event(void* arg) e2ap_reg_sock_addr_ric(&ric->ep, id, &sctp_msg->info); } + if(msg.type == RIC_SERVICE_UPDATE){ + // The update itself does not carry the node id; it is implied by the socket + // it arrived on. An unregistered socket leaves the id zeroed and the handler + // drops the message. + find_map_sad_e2_node(&ric->ep.e2_nodes, &sctp_msg->info, &msg.u_msgs.ric_serv_updt.e2_node_id); + } + e2ap_msg_t ans = e2ap_msg_handle_ric(ric, &msg); defer({e2ap_msg_free_ric(&ric->ap, &ans);});