From 4e01d270eba360ca7ce3a5d0e2e98bef6b573dee Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Mon, 27 Apr 2020 13:08:24 -0400 Subject: [PATCH] Use ogs_pool_alloc in diameter state #420 --- lib/core/ogs-macros.h | 5 ++--- src/pcrf/pcrf-gx-path.c | 38 +++++++++++++++++++++++++++++++++----- src/pcrf/pcrf-rx-path.c | 22 ++++++++++++++++++++-- src/pgw/pgw-context.c | 8 ++++++-- src/pgw/pgw-fd-path.c | 23 ++++++++++++++++++++--- src/smf/context.c | 12 +++++++++--- src/smf/fd-path.c | 34 +++++++++++++++++++++++++--------- 7 files changed, 115 insertions(+), 27 deletions(-) diff --git a/lib/core/ogs-macros.h b/lib/core/ogs-macros.h index 6617e802b..fa4f27fd3 100644 --- a/lib/core/ogs-macros.h +++ b/lib/core/ogs-macros.h @@ -161,9 +161,8 @@ extern "C" { #define OGS_IS_DIR_SEPARATOR(c) ((c) == OGS_DIR_SEPARATOR) #endif -#define ogs_container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type, member) );}) +#define ogs_container_of(ptr, type, member) \ + (type *)((u_char *)ptr - offsetof(type, member)) #ifdef __cplusplus } diff --git a/src/pcrf/pcrf-gx-path.c b/src/pcrf/pcrf-gx-path.c index fa83b59a8..cd1c3ce26 100644 --- a/src/pcrf/pcrf-gx-path.c +++ b/src/pcrf/pcrf-gx-path.c @@ -50,6 +50,10 @@ ED3(uint8_t ipv4:1;, struct timespec ts; /* Time of sending the message */ }; +static OGS_POOL(sess_state_pool, struct sess_state); +static OGS_POOL(rx_sess_state_pool, struct rx_sess_state); +static ogs_thread_mutex_t sess_state_mutex; + static struct session_handler *pcrf_gx_reg = NULL; static struct disp_hdl *hdl_gx_fb = NULL; static struct disp_hdl *hdl_gx_ccr = NULL; @@ -71,7 +75,12 @@ static __inline__ struct sess_state *new_state(os0_t sid) ogs_assert(sid); - new = ogs_calloc(1, sizeof(*new)); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_alloc(&sess_state_pool, &new); + ogs_assert(new); + memset(new, 0, sizeof(*new)); + ogs_thread_mutex_unlock(&sess_state_mutex); + new->sid = (os0_t)ogs_strdup((char *)sid); ogs_assert(new->sid); @@ -87,7 +96,12 @@ static struct rx_sess_state *add_rx_state(struct sess_state *gx, os0_t sid) ogs_assert(gx); ogs_assert(sid); - new = ogs_calloc(1, sizeof(*new)); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_alloc(&rx_sess_state_pool, &new); + ogs_assert(new); + memset(new, 0, sizeof(*new)); + ogs_thread_mutex_unlock(&sess_state_mutex); + new->sid = (os0_t)ogs_strdup((char *)sid); ogs_assert(new->sid); @@ -105,6 +119,9 @@ static int remove_rx_state(struct rx_sess_state *rx_sess_data) ogs_assert(rx_sess_data); gx = rx_sess_data->gx; + ogs_assert(gx); + + ogs_list_remove(&gx->rx_list, rx_sess_data); for (i = 0; i < rx_sess_data->num_of_pcc_rule; i++) { OGS_PCC_RULE_FREE(&rx_sess_data->pcc_rule[i]); @@ -113,8 +130,9 @@ static int remove_rx_state(struct rx_sess_state *rx_sess_data) if (rx_sess_data->sid) ogs_free(rx_sess_data->sid); - ogs_list_remove(&gx->rx_list, rx_sess_data); - ogs_free(rx_sess_data); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_free(&rx_sess_state_pool, rx_sess_data); + ogs_thread_mutex_unlock(&sess_state_mutex); return OGS_OK; } @@ -168,7 +186,9 @@ static void state_cleanup(struct sess_state *sess_data, os0_t sid, void *opaque) remove_rx_state_all(sess_data); - ogs_free(sess_data); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_free(&sess_state_pool, sess_data); + ogs_thread_mutex_unlock(&sess_state_mutex); } static int pcrf_gx_fb_cb(struct msg **msg, struct avp *avp, @@ -1110,6 +1130,10 @@ int pcrf_gx_init(void) int ret; struct disp_when data; + ogs_thread_mutex_init(&sess_state_mutex); + ogs_pool_init(&sess_state_pool, ogs_config()->pool.sess); + ogs_pool_init(&rx_sess_state_pool, ogs_config()->pool.sess); + /* Install objects definitions for this application */ ret = ogs_diam_gx_init(); ogs_assert(ret == 0); @@ -1148,6 +1172,10 @@ void pcrf_gx_final(void) (void) fd_disp_unregister(&hdl_gx_fb, NULL); if (hdl_gx_ccr) (void) fd_disp_unregister(&hdl_gx_ccr, NULL); + + ogs_pool_final(&sess_state_pool); + ogs_pool_final(&rx_sess_state_pool); + ogs_thread_mutex_destroy(&sess_state_mutex); } static int encode_pcc_rule_definition( diff --git a/src/pcrf/pcrf-rx-path.c b/src/pcrf/pcrf-rx-path.c index 2efe3ba63..49e69e341 100644 --- a/src/pcrf/pcrf-rx-path.c +++ b/src/pcrf/pcrf-rx-path.c @@ -35,6 +35,9 @@ struct sess_state { struct timespec ts; /* Time of sending the message */ }; +static OGS_POOL(sess_state_pool, struct sess_state); +static ogs_thread_mutex_t sess_state_mutex; + static struct session_handler *pcrf_rx_reg = NULL; static struct disp_hdl *hdl_rx_fb = NULL; static struct disp_hdl *hdl_rx_aar = NULL; @@ -44,7 +47,14 @@ static void pcrf_rx_asa_cb(void *data, struct msg **msg); static __inline__ struct sess_state *new_state(os0_t sid) { - struct sess_state *new = ogs_calloc(1, sizeof(*new));; + struct sess_state *new = NULL; + + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_alloc(&sess_state_pool, &new); + ogs_assert(new); + memset(new, 0, sizeof(*new)); + ogs_thread_mutex_unlock(&sess_state_mutex); + new->rx_sid = (os0_t)ogs_strdup((char *)sid); ogs_assert(new->rx_sid); @@ -63,7 +73,9 @@ static void state_cleanup(struct sess_state *sess_data, os0_t sid, void *opaque) if (sess_data->peer_host) ogs_free(sess_data->peer_host); - ogs_free(sess_data); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_free(&sess_state_pool, sess_data); + ogs_thread_mutex_unlock(&sess_state_mutex); } static int pcrf_rx_fb_cb(struct msg **msg, struct avp *avp, @@ -716,6 +728,9 @@ int pcrf_rx_init(void) int ret; struct disp_when data; + ogs_thread_mutex_init(&sess_state_mutex); + ogs_pool_init(&sess_state_pool, ogs_config()->pool.sess); + /* Install objects definitions for this application */ ret = ogs_diam_rx_init(); ogs_assert(ret == 0); @@ -764,4 +779,7 @@ void pcrf_rx_final(void) (void) fd_disp_unregister(&hdl_rx_aar, NULL); if (hdl_rx_str) (void) fd_disp_unregister(&hdl_rx_str, NULL); + + ogs_pool_final(&sess_state_pool); + ogs_thread_mutex_destroy(&sess_state_mutex); } diff --git a/src/pgw/pgw-context.c b/src/pgw/pgw-context.c index 120ac1546..1dba00026 100644 --- a/src/pgw/pgw-context.c +++ b/src/pgw/pgw-context.c @@ -786,7 +786,12 @@ pgw_sess_t *pgw_sess_add( ogs_assert(paa); ogs_pool_alloc(&pgw_sess_pool, &sess); - ogs_assert(sess); + if (!sess) { + ogs_error("Maximum number of session[%d] reached", + ogs_config()->pool.sess); + return NULL; + } + memset(sess, 0, sizeof *sess); sess->index = ogs_pool_index(&pgw_sess_pool, sess); @@ -1008,7 +1013,6 @@ pgw_sess_t *pgw_sess_add_by_message(ogs_gtp_message_t *message) sess = pgw_sess_add(req->imsi.data, req->imsi.len, apn, req->pdn_type.u8, req->bearer_contexts_to_be_created.eps_bearer_id.u8, paa); - ogs_assert(sess); return sess; } diff --git a/src/pgw/pgw-fd-path.c b/src/pgw/pgw-fd-path.c index 80d7e1e83..dbd9b123d 100644 --- a/src/pgw/pgw-fd-path.c +++ b/src/pgw/pgw-fd-path.c @@ -37,6 +37,9 @@ struct sess_state { struct timespec ts; /* Time of sending the message */ }; +static OGS_POOL(sess_state_pool, struct sess_state); +static ogs_thread_mutex_t sess_state_mutex; + static int decode_pcc_rule_definition( ogs_pcc_rule_t *pcc_rule, struct avp *avpch1, int *perror); static void pgw_gx_cca_cb(void *data, struct msg **msg); @@ -45,7 +48,12 @@ static __inline__ struct sess_state *new_state(os0_t sid) { struct sess_state *new = NULL; - new = ogs_calloc(1, sizeof(*new)); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_alloc(&sess_state_pool, &new); + ogs_assert(new); + memset(new, 0, sizeof(*new)); + ogs_thread_mutex_unlock(&sess_state_mutex); + new->gx_sid = (os0_t)ogs_strdup((char *)sid); ogs_assert(new->gx_sid); @@ -57,7 +65,9 @@ static void state_cleanup(struct sess_state *sess_data, os0_t sid, void *opaque) if (sess_data->gx_sid) ogs_free(sess_data->gx_sid); - ogs_free(sess_data); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_free(&sess_state_pool, sess_data); + ogs_thread_mutex_unlock(&sess_state_mutex); } void pgw_gx_send_ccr(pgw_sess_t *sess, ogs_gtp_xact_t *xact, @@ -1028,6 +1038,9 @@ int pgw_fd_init(void) int ret; struct disp_when data; + ogs_thread_mutex_init(&sess_state_mutex); + ogs_pool_init(&sess_state_pool, ogs_config()->pool.sess); + ret = ogs_diam_init(FD_MODE_CLIENT|FD_MODE_SERVER, pgw_self()->diam_conf_path, pgw_self()->diam_config); ogs_assert(ret == 0); @@ -1071,6 +1084,9 @@ void pgw_fd_final(void) (void) fd_disp_unregister(&hdl_gx_rar, NULL); ogs_diam_final(); + + ogs_pool_final(&sess_state_pool); + ogs_thread_mutex_destroy(&sess_state_mutex); } static int decode_pcc_rule_definition( @@ -1102,7 +1118,8 @@ static int decode_pcc_rule_definition( ogs_flow_t *flow = &pcc_rule->flow[pcc_rule->num_of_flow]; - ret = fd_avp_search_avp(avpch2, ogs_diam_gx_flow_direction, &avpch3); + ret = fd_avp_search_avp( + avpch2, ogs_diam_gx_flow_direction, &avpch3); ogs_assert(ret == 0); if (avpch3) { ret = fd_msg_avp_hdr( avpch3, &hdr); diff --git a/src/smf/context.c b/src/smf/context.c index 8e398b553..5753afed3 100644 --- a/src/smf/context.c +++ b/src/smf/context.c @@ -35,12 +35,14 @@ static int context_initiaized = 0; int num_sessions = 0; void stats_add_session(void) { num_sessions = num_sessions + 1; - ogs_info("Added a session. Number of active sessions is now %d", num_sessions); + ogs_info("Added a session. Number of active sessions is now %d", + num_sessions); } void stats_remove_session(void) { num_sessions = num_sessions - 1; - ogs_info("Removed a session. Number of active sessions is now %d", num_sessions); + ogs_info("Removed a session. Number of active sessions is now %d", + num_sessions); } void smf_context_init(void) @@ -542,7 +544,11 @@ smf_sess_t *smf_sess_add( ogs_assert(paa); ogs_pool_alloc(&smf_sess_pool, &sess); - ogs_assert(sess); + if (!sess) { + ogs_error("Maximum number of session[%d] reached", + ogs_config()->pool.sess); + return NULL; + } memset(sess, 0, sizeof *sess); sess->index = ogs_pool_index(&smf_sess_pool, sess); diff --git a/src/smf/fd-path.c b/src/smf/fd-path.c index 6574c1bf2..155a0ceaa 100644 --- a/src/smf/fd-path.c +++ b/src/smf/fd-path.c @@ -37,6 +37,9 @@ struct sess_state { struct timespec ts; /* Time of sending the message */ }; +static OGS_POOL(sess_state_pool, struct sess_state); +static ogs_thread_mutex_t sess_state_mutex; + static int decode_pcc_rule_definition( ogs_pcc_rule_t *pcc_rule, struct avp *avpch1, int *perror); static void smf_gx_cca_cb(void *data, struct msg **msg); @@ -45,7 +48,11 @@ static __inline__ struct sess_state *new_state(os0_t sid) { struct sess_state *new = NULL; - new = ogs_calloc(1, sizeof(*new)); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_alloc(&sess_state_pool, &new); + ogs_assert(new); + ogs_thread_mutex_unlock(&sess_state_mutex); + new->gx_sid = (os0_t)ogs_strdup((char *)sid); ogs_assert(new->gx_sid); @@ -57,7 +64,9 @@ static void state_cleanup(struct sess_state *sess_data, os0_t sid, void *opaque) if (sess_data->gx_sid) ogs_free(sess_data->gx_sid); - ogs_free(sess_data); + ogs_thread_mutex_lock(&sess_state_mutex); + ogs_pool_free(&sess_state_pool, sess_data); + ogs_thread_mutex_unlock(&sess_state_mutex); } void smf_gx_send_ccr(smf_sess_t *sess, ogs_gtp_xact_t *xact, @@ -1022,6 +1031,9 @@ int smf_fd_init(void) int ret; struct disp_when data; + ogs_thread_mutex_init(&sess_state_mutex); + ogs_pool_init(&sess_state_pool, ogs_config()->pool.sess); + ret = ogs_diam_init(FD_MODE_CLIENT|FD_MODE_SERVER, smf_self()->diam_conf_path, smf_self()->diam_config); ogs_assert(ret == 0); @@ -1065,6 +1077,9 @@ void smf_fd_final(void) (void) fd_disp_unregister(&hdl_gx_rar, NULL); ogs_diam_final(); + + ogs_pool_final(&sess_state_pool); + ogs_thread_mutex_destroy(&sess_state_mutex); } static int decode_pcc_rule_definition( @@ -1084,8 +1099,7 @@ static int decode_pcc_rule_definition( ogs_assert(ret == 0); switch (hdr->avp_code) { case OGS_DIAM_GX_AVP_CODE_CHARGING_RULE_NAME: - if (pcc_rule->name) - { + if (pcc_rule->name) { ogs_error("PCC Rule Name has already been defined"); ogs_free(pcc_rule->name); } @@ -1097,16 +1111,17 @@ static int decode_pcc_rule_definition( ogs_flow_t *flow = &pcc_rule->flow[pcc_rule->num_of_flow]; - ret = fd_avp_search_avp(avpch2, ogs_diam_gx_flow_direction, &avpch3); + ret = fd_avp_search_avp( + avpch2, ogs_diam_gx_flow_direction, &avpch3); ogs_assert(ret == 0); - if (avpch3) - { + if (avpch3) { ret = fd_msg_avp_hdr( avpch3, &hdr); ogs_assert(ret == 0); flow->direction = hdr->avp_value->i32; } - ret = fd_avp_search_avp(avpch2, ogs_diam_gx_flow_description, &avpch3); + ret = fd_avp_search_avp( + avpch2, ogs_diam_gx_flow_description, &avpch3); ogs_assert(ret == 0); if (avpch3) { @@ -1141,7 +1156,8 @@ static int decode_pcc_rule_definition( ogs_diam_gx_allocation_retention_priority, &avpch3); ogs_assert(ret == 0); if (avpch3) { - ret = fd_avp_search_avp(avpch3, ogs_diam_gx_priority_level, &avpch4); + ret = fd_avp_search_avp( + avpch3, ogs_diam_gx_priority_level, &avpch4); ogs_assert(ret == 0); if (avpch4) { ret = fd_msg_avp_hdr(avpch4, &hdr);