Skip to content

Commit

Permalink
[SBI] Do not raise ASSERT when not enough CLIENT pool (open5gs#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
acetcom committed Oct 25, 2023
1 parent 7a98baf commit e3c2fd0
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 23 deletions.
17 changes: 13 additions & 4 deletions lib/sbi/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ ogs_sbi_client_t *ogs_sbi_client_add(
{
ogs_sbi_client_t *client = NULL;
CURLM *multi = NULL;
char buf[OGS_ADDRSTRLEN];

ogs_assert(scheme);
ogs_assert(addr);

ogs_pool_alloc(&client_pool, &client);
ogs_assert(client);
if (!client) {
ogs_error("No memory in client_pool");
return NULL;
}
memset(client, 0, sizeof(ogs_sbi_client_t));

client->scheme = scheme;

ogs_debug("ogs_sbi_client_add[%s]", OpenAPI_uri_scheme_ToString(scheme));
OGS_OBJECT_REF(client);

ogs_assert(OGS_OK == ogs_copyaddrinfo(&client->node.addr, addr));
Expand Down Expand Up @@ -141,6 +144,9 @@ ogs_sbi_client_t *ogs_sbi_client_add(

ogs_list_add(&ogs_sbi_self()->client_list, client);

ogs_debug("[%d] CLEINT added with Ref [%s:%d]",
client->reference_count, OGS_ADDR(addr, buf), OGS_PORT(addr));

return client;
}

Expand All @@ -153,15 +159,18 @@ void ogs_sbi_client_remove(ogs_sbi_client_t *client)

addr = client->node.addr;
ogs_assert(addr);
ogs_debug("ogs_sbi_client_remove() [%s:%d]",
OGS_ADDR(addr, buf), OGS_PORT(addr));
ogs_debug("[%d] CLEINT UnRef [%s:%d]",
client->reference_count, OGS_ADDR(addr, buf), OGS_PORT(addr));

/* ogs_sbi_client_t is always created with reference context */
if (OGS_OBJECT_IS_REF(client)) {
OGS_OBJECT_UNREF(client);
return;
}

ogs_debug("[%d] CLEINT removed [%s:%d]",
client->reference_count, OGS_ADDR(addr, buf), OGS_PORT(addr));

ogs_list_remove(&ogs_sbi_self()->client_list, client);

connection_remove_all(client);
Expand Down
6 changes: 5 additions & 1 deletion lib/sbi/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ extern "C" {

#define OGS_SBI_SETUP_CLIENT(__cTX, __pClient) \
do { \
char buf[OGS_ADDRSTRLEN]; \
ogs_assert((__cTX)); \
ogs_assert((__pClient)); \
\
if ((__cTX)->client) { \
ogs_sbi_client_t *client = NULL; \
ogs_sockaddr_t *addr = NULL; \
char buf[OGS_ADDRSTRLEN]; \
\
client = ((__cTX)->client); \
ogs_assert(client); \
Expand All @@ -49,6 +49,10 @@ extern "C" {
\
OGS_OBJECT_REF(__pClient); \
((__cTX)->client) = (__pClient); \
ogs_debug("[%d] CLIENT Ref [%s:%d]", \
(__pClient)->reference_count, \
OGS_ADDR((__pClient)->node.addr, buf), \
OGS_PORT((__pClient)->node.addr)); \
} while(0)

typedef int (*ogs_sbi_client_cb_f)(
Expand Down
39 changes: 33 additions & 6 deletions lib/sbi/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ int ogs_sbi_context_parse_config(

if (addr == NULL) continue;

ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(
ogs_sbi_client_default_scheme(), addr);
ogs_assert(client);
Expand Down Expand Up @@ -683,6 +684,7 @@ int ogs_sbi_context_parse_config(

if (addr == NULL) continue;

ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(
ogs_sbi_client_default_scheme(), addr);
ogs_assert(client);
Expand Down Expand Up @@ -805,8 +807,6 @@ ogs_sbi_nf_instance_t *ogs_sbi_nf_instance_add(void)
ogs_assert(nf_instance);
memset(nf_instance, 0, sizeof(ogs_sbi_nf_instance_t));

ogs_debug("ogs_sbi_nf_instance_add()");

OGS_OBJECT_REF(nf_instance);

nf_instance->time.heartbeat_interval =
Expand All @@ -818,6 +818,9 @@ ogs_sbi_nf_instance_t *ogs_sbi_nf_instance_add(void)

ogs_list_add(&ogs_sbi_self()->nf_instance_list, nf_instance);

ogs_debug("[%s:%d] NFInstance added with Ref",
nf_instance->id, nf_instance->reference_count);

return nf_instance;
}

Expand Down Expand Up @@ -913,13 +916,17 @@ void ogs_sbi_nf_instance_remove(ogs_sbi_nf_instance_t *nf_instance)
{
ogs_assert(nf_instance);

ogs_debug("ogs_sbi_nf_instance_remove()");
ogs_debug("[%s:%d] NFInstance UnRef",
nf_instance->id, nf_instance->reference_count);

if (OGS_OBJECT_IS_REF(nf_instance)) {
OGS_OBJECT_UNREF(nf_instance);
return;
}

ogs_debug("[%s:%d] NFInstance removed",
nf_instance->id, nf_instance->reference_count);

ogs_list_remove(&ogs_sbi_self()->nf_instance_list, nf_instance);

ogs_sbi_nf_info_remove_all(&nf_instance->nf_info_list);
Expand Down Expand Up @@ -1519,8 +1526,15 @@ static ogs_sbi_client_t *find_client_by_fqdn(

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
ogs_freeaddrinfo(addr);
return NULL;
}
}

ogs_freeaddrinfo(addr);
Expand Down Expand Up @@ -1548,8 +1562,14 @@ static ogs_sbi_client_t *nf_instance_find_client(
if (addr) {
client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
return NULL;
}
}
}
}
Expand Down Expand Up @@ -1578,12 +1598,19 @@ static void nf_service_associate_client(ogs_sbi_nf_service_t *nf_service)
if (addr) {
client = ogs_sbi_client_find(nf_service->scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(nf_service->scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
}
}
}
}

ogs_debug("[%s] NFService associated [%s]",
nf_service->name, nf_service->id);
if (client)
OGS_SBI_SETUP_CLIENT(nf_service, client);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/sbi/nnrf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ bool ogs_nnrf_nfm_handle_nf_status_notify(
ogs_sbi_nf_fsm_tran(
nf_instance, ogs_sbi_nf_state_de_registered);
} else {
ogs_info("[%s:%d] NF removed",
ogs_info("[%s:%d] (NRF-notify) NF_DEREGISTERED event",
nf_instance->id, nf_instance->reference_count);
ogs_sbi_nf_fsm_fini((nf_instance));
ogs_sbi_nf_instance_remove(nf_instance);
Expand Down
9 changes: 8 additions & 1 deletion src/amf/namf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,15 @@ int amf_namf_comm_handle_n1_n2_message_transfer(

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
ogs_freeaddrinfo(addr);
return OGS_ERROR;
}
}
OGS_SBI_SETUP_CLIENT(&sess->paging, client);
ogs_freeaddrinfo(addr);
Expand Down
14 changes: 12 additions & 2 deletions src/amf/nnssf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,23 @@ int amf_nnssf_nsselection_handle_get(

ogs_sbi_discovery_option_free(discovery_option);

return OGS_ERROR;;
return OGS_ERROR;
}

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));

ogs_sbi_discovery_option_free(discovery_option);
ogs_freeaddrinfo(addr);

return OGS_ERROR;
}
}

OGS_SBI_SETUP_CLIENT(&sess->nssf.nrf, client);
Expand Down
1 change: 1 addition & 0 deletions src/nrf/nnrf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ bool nrf_nnrf_handle_nf_status_subscribe(

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
}
Expand Down
3 changes: 3 additions & 0 deletions src/nrf/nrf-sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void nrf_state_operational(ogs_fsm_t *s, nrf_event_t *e)
ogs_fsm_dispatch(&nf_instance->sm, e);
if (OGS_FSM_CHECK(&nf_instance->sm,
nrf_nf_state_de_registered)) {
ogs_info("[%s:%d] NF de-registered",
nf_instance->id,
nf_instance->reference_count);
nrf_nf_fsm_fini(nf_instance);
ogs_sbi_nf_instance_remove(nf_instance);
} else if (OGS_FSM_CHECK(&nf_instance->sm,
Expand Down
29 changes: 26 additions & 3 deletions src/pcf/npcf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@ bool pcf_npcf_am_policy_contrtol_handle_create(pcf_ue_t *pcf_ue,

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
ogs_freeaddrinfo(addr);
return false;
}
}
OGS_SBI_SETUP_CLIENT(&pcf_ue->namf, client);
ogs_freeaddrinfo(addr);
Expand Down Expand Up @@ -276,8 +283,16 @@ bool pcf_npcf_smpolicycontrol_handle_create(pcf_sess_t *sess,

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
strerror = ogs_msprintf("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
status = OGS_SBI_HTTP_STATUS_INTERNAL_SERVER_ERROR;
ogs_freeaddrinfo(addr);
goto cleanup;
}
}
OGS_SBI_SETUP_CLIENT(&sess->nsmf, client);
ogs_freeaddrinfo(addr);
Expand Down Expand Up @@ -599,8 +614,16 @@ bool pcf_npcf_policyauthorization_handle_create(pcf_sess_t *sess,

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
strerror = ogs_msprintf("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
status = OGS_SBI_HTTP_STATUS_INTERNAL_SERVER_ERROR;
ogs_freeaddrinfo(addr);
goto cleanup;
}
}
OGS_SBI_SETUP_CLIENT(&app_session->naf, client);
ogs_freeaddrinfo(addr);
Expand Down
26 changes: 24 additions & 2 deletions src/scp/sbi-path.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,19 @@ static int request_handler(ogs_sbi_request_t *request, void *data)

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("ogs_sbi_client_add() failed [%s]:%d",
OGS_ADDR(addr, buf), OGS_PORT(addr));

ogs_freeaddrinfo(addr);
ogs_sbi_discovery_option_free(discovery_option);
scp_assoc_remove(assoc);

return OGS_ERROR;
}
}
OGS_SBI_SETUP_CLIENT(assoc, client);
ogs_freeaddrinfo(addr);
Expand Down Expand Up @@ -443,8 +454,19 @@ static int request_handler(ogs_sbi_request_t *request, void *data)

nrf_client = ogs_sbi_client_find(scheme, addr);
if (!nrf_client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
nrf_client = ogs_sbi_client_add(scheme, addr);
ogs_assert(nrf_client);
if (!nrf_client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("ogs_sbi_client_add() failed [%s]:%d",
OGS_ADDR(addr, buf), OGS_PORT(addr));

ogs_freeaddrinfo(addr);
ogs_sbi_discovery_option_free(discovery_option);
scp_assoc_remove(assoc);

return OGS_ERROR;
}
}
OGS_SBI_SETUP_CLIENT(assoc, nrf_client);
ogs_freeaddrinfo(addr);
Expand Down
9 changes: 8 additions & 1 deletion src/smf/nsmf-handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,15 @@ bool smf_nsmf_handle_create_sm_context(

client = ogs_sbi_client_find(scheme, addr);
if (!client) {
ogs_debug("%s: ogs_sbi_client_add()", OGS_FUNC);
client = ogs_sbi_client_add(scheme, addr);
ogs_assert(client);
if (!client) {
char buf[OGS_ADDRSTRLEN];
ogs_error("%s: ogs_sbi_client_add() failed [%s]:%d",
OGS_FUNC, OGS_ADDR(addr, buf), OGS_PORT(addr));
ogs_freeaddrinfo(addr);
return false;
}
}
OGS_SBI_SETUP_CLIENT(&sess->namf, client);
ogs_freeaddrinfo(addr);
Expand Down
Loading

0 comments on commit e3c2fd0

Please sign in to comment.