Skip to content

Commit

Permalink
qrouting/drouting: Simplify callbacks code [part 1]
Browse files Browse the repository at this point in the history
    * merge unnecessary DRCB_SET_PROFILE callback
    * avoid unnecessary pkg_malloc() bloat, just use stack structures
    * fix a ton of pkg memory leaks
  • Loading branch information
liviuchircu committed Feb 5, 2020
1 parent 4de3e3c commit c29be86
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 123 deletions.
1 change: 0 additions & 1 deletion modules/drouting/dr_cb.h
Expand Up @@ -37,7 +37,6 @@ enum drcb_types {
DRCB_REG_FREE_LIST,
DRCB_ACC_CALL,
DRCB_SORT_DST,
DRCB_SET_PROFILE,
DRCB_MAX /*keep this at the end*/
};

Expand Down
8 changes: 2 additions & 6 deletions modules/drouting/dr_cb_sorting.h
Expand Up @@ -51,9 +51,11 @@ struct dr_reg_param {

struct dr_reg_init_rule_params {
void *rule; /* created at qr, set to dr */

int n_dst; /* the number of destination for the new rule;
sent by dr */
int r_id; /* the rule id: sent by dr */
int qr_profile; /* sent by dr */
};

struct dr_acc_call_params {
Expand All @@ -71,12 +73,6 @@ struct dr_sort_params {
int rc; /* return code for the funciton */
};

struct dr_set_profile_params {
void *qr_rule; /* qr_rule_t * to which the profile will be added.
* provided by dr */
unsigned int profile; /* profile id, sent by dr to qr */
};

struct dr_add_rule_params {
int part_index; /* partition index */
str part_name;
Expand Down
6 changes: 3 additions & 3 deletions modules/drouting/dr_load.c
Expand Up @@ -283,7 +283,7 @@ void dr_update_head_cache(struct head_db *head)
#define INT_VALS_BLANK_1 1
#define INT_VALS_PRIORITY_DRR_COL 2
#define INT_VALS_SCRIPT_ROUTE_ID 3
#define INT_VALS_SORT_PROFILE_DRR_COL 4
#define INT_VALS_QR_PROFILE_DRR_COL 4
#define STR_VALS_GROUP_DRR_COL 0
#define STR_VALS_PREFIX_DRR_COL 1
#define STR_VALS_TIME_DRR_COL 2
Expand Down Expand Up @@ -687,7 +687,7 @@ rt_data_t* dr_load_routing_info(struct head_db *current_partition
}
/* SORT_PROFILE column */
check_val(sort_profile_drr_col, ROW_VALUES(row)+8, DB_INT, 1, 0);
int_vals[INT_VALS_SORT_PROFILE_DRR_COL] = VAL_INT(ROW_VALUES(row)+8);
int_vals[INT_VALS_QR_PROFILE_DRR_COL] = VAL_INT(ROW_VALUES(row)+8);
/* ATTRS column */
check_val( attrs_drr_col, ROW_VALUES(row)+9, DB_STRING, 0, 0);
str_vals[STR_VALS_ATTRS_DRR_COL] =
Expand Down Expand Up @@ -718,7 +718,7 @@ rt_data_t* dr_load_routing_info(struct head_db *current_partition
str_vals[STR_VALS_ROUTEID_DRR_COL],
str_vals[STR_VALS_DSTLIST_DRR_COL],
str_vals[STR_VALS_SORT_ALG_DRR_COL],
int_vals[INT_VALS_SORT_PROFILE_DRR_COL],
int_vals[INT_VALS_QR_PROFILE_DRR_COL],
str_vals[STR_VALS_ATTRS_DRR_COL], rdata,
qr_parts, part_index, part_name,
current_partition->malloc,
Expand Down
110 changes: 32 additions & 78 deletions modules/drouting/routing.c
Expand Up @@ -329,7 +329,7 @@ build_rt_info(
/* list of destinations indexes */
char* dstlst,
char* sort_alg,
int sort_profile,
int qr_profile,
char* attrs,
rt_data_t* rd,
void *qr_parts_data,
Expand All @@ -344,11 +344,10 @@ build_rt_info(
/* callback parameters for the QR module */
int i;
void * qr_rule = NULL;
struct dr_cb_params *cb_params;
struct dr_reg_init_rule_params *init_rule_params;
struct dr_reg_param *reg_dst_param;
struct dr_set_profile_params *profile_params;
struct dr_add_rule_params *add_rule_params = NULL;
struct dr_cb_params cbp;
struct dr_reg_param rdp;
struct dr_add_rule_params arp;
struct dr_reg_init_rule_params irp;
pgw_list_t *p = NULL;

unsigned char * sort_p, n_alg;
Expand Down Expand Up @@ -395,90 +394,45 @@ build_rt_info(
}
}

if(n_alg == 3) { /* if the sorting algorithm for this rule is qr sorting */
if (n_alg == 3) { /* qr sorting */
irp.n_dst = rt->pgwa_len;
irp.r_id = id;
irp.qr_profile = qr_profile;
cbp.param = (void **)&irp;

/* call the create rule callbacks */
init_rule_params = (struct dr_reg_init_rule_params *) pkg_malloc(
sizeof(struct dr_reg_init_rule_params));
cb_params = (struct dr_cb_params *) pkg_malloc(sizeof(struct dr_cb_params));
if(init_rule_params != NULL && cb_params != NULL) {
memset(init_rule_params, 0, sizeof(struct dr_reg_init_rule_params));
init_rule_params->n_dst = rt->pgwa_len;
init_rule_params->r_id = id; /* name of the rule */
cb_params->param = (void*)&init_rule_params;
run_dr_cbs(DRCB_REG_INIT_RULE, &cbp);

if (run_dr_cbs(DRCB_REG_INIT_RULE, cb_params) != 0)
LM_BUG("No callback for DRCB_REG_INIT_RULE\n");
qr_rule = irp.rule;
rt->qr_handler = qr_rule;

qr_rule = (void*)((struct dr_reg_init_rule_params*)*cb_params->param)->rule;
rt->qr_handler = qr_rule;
p = rt->pgwl;

p = rt->pgwl;
/* TODO: should check if qr loaded */

for (i = 0; i < rt->pgwa_len; i++) {
if (p[i].is_carrier) {
rdp.rule = qr_rule;
rdp.n_dst = i;
rdp.cr_or_gw = p[i].dst.carrier;

/* TODO: params should be pkg - and freed in the cbs */
/* TODO: should check if qr loaded */


profile_params = (struct dr_set_profile_params *) pkg_malloc(
sizeof(struct dr_set_profile_params));
if(profile_params == NULL) {
LM_ERR("no more pkg memory");
return NULL;
}

profile_params->qr_rule = qr_rule;
profile_params->profile = sort_profile;

run_dr_cbs(DRCB_SET_PROFILE, profile_params); /* save the threholds from qr
to the rule */
for(i = 0; i < rt->pgwa_len; i++) {
if(p[i].is_carrier) {
reg_dst_param = (struct dr_reg_param *) pkg_malloc(
sizeof(struct dr_reg_param));
if(reg_dst_param == NULL) {
LM_ERR("no more pkg memory\n");
} else {
reg_dst_param->rule = qr_rule;
reg_dst_param->n_dst = i;
reg_dst_param->cr_or_gw = p[i].dst.carrier;

run_dr_cbs(DRCB_REG_CR, reg_dst_param);
}

} else {
reg_dst_param = (struct dr_reg_param *) pkg_malloc(sizeof(struct
dr_reg_param));
if(reg_dst_param == NULL) {
LM_ERR("no more pkg memory\n");
/* TODO: should we crash all together? */
} else {
reg_dst_param->rule = qr_rule;
reg_dst_param->n_dst = i;
reg_dst_param->cr_or_gw = p[i].dst.gw;

run_dr_cbs(DRCB_REG_GW, reg_dst_param);
}
}
run_dr_cbs(DRCB_REG_CR, &rdp);
} else {
rdp.rule = qr_rule;
rdp.n_dst = i;
rdp.cr_or_gw = p[i].dst.gw;

run_dr_cbs(DRCB_REG_GW, &rdp);
}
}
/* add rule to the partition list */
add_rule_params = (struct dr_add_rule_params *) pkg_malloc(
sizeof(struct dr_add_rule_params));
if(add_rule_params == NULL) {
LM_ERR("no more pkg memory\n");
}

add_rule_params->qr_rule = qr_rule;
add_rule_params->qr_parts = qr_parts_data;
add_rule_params->part_name = *part_name;
add_rule_params->part_index = part_index;
run_dr_cbs(DRCB_REG_ADD_RULE, add_rule_params);
pkg_free(add_rule_params);
/* add rule to the partition list */
arp.qr_rule = qr_rule;
arp.qr_parts = qr_parts_data;
arp.part_name = *part_name;
arp.part_index = part_index;
run_dr_cbs(DRCB_REG_ADD_RULE, &arp);
}


return rt;

err_exit:
Expand Down
60 changes: 31 additions & 29 deletions modules/qrouting/qr_stats.c
Expand Up @@ -64,7 +64,7 @@ qr_gw_t *qr_create_gw(void *dst)
str *gw_name;
gw_name = drb.get_gw_name(dst);

LM_DBG("Creating gw '%.*s'\n", gw_name->len, gw_name->s);
LM_DBG("creating gw '%.*s'\n", gw_name->len, gw_name->s);

if (!(gw = shm_malloc(sizeof *gw))) {
LM_ERR("oom\n");
Expand Down Expand Up @@ -204,6 +204,31 @@ void free_qr_cb(void *param)
free_qr_list(old_list);
}

int qr_set_profile(qr_rule_t *rule, unsigned int qrp)
{
unsigned int current_id;
int m, left, right;

left = 0;
right = *n_qr_profiles - 1;
while (left<=right) {
m = left + (right-left)/2;
current_id = ((*qr_profiles)[m]).id;
if(current_id == qrp) {
rule->thresholds = &(*qr_profiles)[m];
return 0;
} else if(current_id > qrp) {
right = m-1;
} else {
left = m+1;
}
}

if (left > right)
LM_WARN("profile '%d' not found\n", qrp);

return -1;
}

/* TODO: thresholds must be freed separatley */

Expand Down Expand Up @@ -235,6 +260,11 @@ void qr_create_rule(void *param)
this rule, as rcvd from dr*/
new->r_id = r_id;
irp->rule = new; /* send the rule to the dr */

if (qr_set_profile(new, irp->qr_profile) != 0)
LM_ERR("failed to set profile %d for rule %d\n",
irp->qr_profile, r_id);

LM_DBG("rule %d created\n", r_id);
}

Expand Down Expand Up @@ -397,31 +427,3 @@ void qr_link_rule_list(void *param)
add_last(second_list, *first_list);
}

void qr_search_profile(void *param)
{
struct dr_cb_params *cbp = (struct dr_cb_params *)param;
qr_rule_t *rule = (qr_rule_t*)
((struct dr_set_profile_params *)*cbp->param)->qr_rule;
unsigned int profile = ((struct dr_set_profile_params*)*cbp->param)
->profile;
unsigned int current_id;
int m, left,right, found = 0;
left = 0;
right = *n_qr_profiles - 1;
while(left<=right && !found) {
m = left + (right-left)/2;
current_id = ((*qr_profiles)[m]).id;
if(current_id == profile) {
rule->thresholds = &(*qr_profiles)[m];
found = 1;
} else if(current_id > profile) {
right = m-1;
} else {
left = m+1;
}

}
if(left>right) {
LM_WARN("profile '%d' not found\n", profile);
}
}
1 change: 0 additions & 1 deletion modules/qrouting/qr_stats.h
Expand Up @@ -143,7 +143,6 @@ void qr_create_rule(void *param);
void qr_add_rule_to_list(void *param);
void test_callback(int types, struct dr_cb_params *param);
void qr_dst_is_gw(void *param);
void qr_search_profile(void *param);
void qr_mark_as_main_list(void *param);
void qr_link_rule_list(void *param);
void qr_create_partition_list(void *param);
Expand Down
5 changes: 0 additions & 5 deletions modules/qrouting/qrouting.c
Expand Up @@ -255,11 +255,6 @@ static int qr_init_dr_cb(void)
return -1;
}

if (drb.register_drcb(DRCB_SET_PROFILE, &qr_search_profile, NULL, NULL) < 0) {
LM_ERR("[QR] failed to register DRCB_SET_PROFILE callback to DR\n");
return -1;
}

if (drb.register_drcb(DRCB_REG_MARK_AS_RULE_LIST, &qr_mark_as_main_list, NULL, NULL) < 0) {
LM_ERR("[QR] failed to register DRCB_MARK_AS_QR_RULE_LIST callback to DR\n");
return -1;
Expand Down

0 comments on commit c29be86

Please sign in to comment.