Skip to content

Commit

Permalink
[qrouting] QR aware of DR partitions; qr_status modified to recognize…
Browse files Browse the repository at this point in the history
… partitions
  • Loading branch information
tallicamike authored and razvancrainea committed Feb 11, 2020
1 parent 79ba404 commit 9ccc1fb
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 119 deletions.
2 changes: 1 addition & 1 deletion modules/drouting/dr_cb.c
Expand Up @@ -176,7 +176,7 @@ int register_dr_cb(enum drcb_types type, dr_cb f, void *param,
} else if(types & (DRCB_REG_INIT_RULE | DRCB_REG_GW |
DRCB_REG_CR | DRCB_REG_ADD_RULE
| DRCB_REG_MARK_AS_RULE_LIST | DRCB_REG_LINK_LISTS
| DRCB_REG_FREE_LIST)) {
| DRCB_REG_FREE_LIST | DRCB_REG_CREATE_PARTS_LIST)) {
if(insert_drcb(&dr_reg_cbs, cb, types) == -1)
goto error;
} else if(types & (DRCB_SET_PROFILE)) {
Expand Down
4 changes: 2 additions & 2 deletions modules/drouting/dr_load.c
Expand Up @@ -305,7 +305,7 @@ void dr_update_head_cache(struct head_db *head)
*/

rt_data_t* dr_load_routing_info(struct head_db *current_partition
, int persistent_state, void **part_rule_list)
, int persistent_state, void *qr_parts, int part_index, str part_name)
{
int int_vals[5];
char * str_vals[7];
Expand Down Expand Up @@ -728,7 +728,7 @@ rt_data_t* dr_load_routing_info(struct head_db *current_partition
str_vals[STR_VALS_SORT_ALG_DRR_COL],
int_vals[INT_VALS_SORT_PROFILE_DRR_COL],
str_vals[STR_VALS_ATTRS_DRR_COL], rdata,
part_rule_list,
qr_parts, part_index, part_name,
current_partition->malloc,
current_partition->free))== 0 ) {
LM_ERR("failed to add routing info for rule id %d -> "
Expand Down
3 changes: 2 additions & 1 deletion modules/drouting/dr_load.h
Expand Up @@ -37,6 +37,7 @@
#include "routing.h"

void dr_update_head_cache(struct head_db *head);
rt_data_t* dr_load_routing_info(struct head_db * ,int persistent_state, void** part_rule_list);
rt_data_t* dr_load_routing_info(struct head_db * ,int persistent_state,
void* qr_parts, int part_index, str part_name);

#endif
14 changes: 11 additions & 3 deletions modules/drouting/dr_sorting_cbs.h
Expand Up @@ -43,6 +43,7 @@
#define DRCB_REG_MARK_AS_RULE_LIST (1<<7)
#define DRCB_REG_LINK_LISTS (1<<8)
#define DRCB_REG_FREE_LIST (1<<9)
#define DRCB_REG_CREATE_PARTS_LIST (1<<10) /* create a qr partitions list */

struct dr_head_cbl *dr_reg_cbs, *dr_acc_cbs, *dr_set_profile_cbs;

Expand Down Expand Up @@ -96,8 +97,10 @@ struct dr_set_profile_params {
};

struct dr_add_rule_params {
int part_index; /* partition index */
str part_name;
void *qr_rule; /* rule to be added to list */
void **rule_list; /* the list to which the rule will be added */
void *qr_parts; /* the partitions list to which the rule will be added */
};

struct dr_link_rule_list_params {
Expand All @@ -106,14 +109,19 @@ struct dr_link_rule_list_params {
};

struct dr_mark_as_main_list_params{
void *new_list; /* list to become qr rules list */
void **old_list; /* old list: to be freed */
void *qr_parts_new_list; /* list to become qr rules list */
void **qr_parts_old_list; /* old list: to be freed */
};

struct dr_free_qr_list_params {
void *old_list;
};

struct dr_create_partition_list_params {
void **part_list; /* the list of partitions created at QR returned for DR */
int n_parts; /* the number of partitions:provided by dr */
};

#endif


52 changes: 30 additions & 22 deletions modules/drouting/drouting.c
Expand Up @@ -176,7 +176,10 @@ static struct head_config {
str rule_attrs_avp_spec; /* extracted from database - has default value */
str carrier_attrs_avp_spec; /* extracted from database - has default value */
struct head_config *next;
}* head_start = NULL;
}* head_start = NULL,* head_end = NULL;
int *n_partitions; /* the number of partitions accepted by modules init */

struct head_db * head_db_start = NULL,* head_db_end = NULL;

struct head_db * head_db_start = NULL;

Expand Down Expand Up @@ -960,7 +963,7 @@ static void dr_state_timer(unsigned int ticks, void* param)
*/

static inline int dr_reload_data_head( struct head_db *hd,
void **part_rule_list )
void *qr_parts, int part_index, str part_name)
{
rt_data_t *new_data;
rt_data_t *old_data;
Expand Down Expand Up @@ -990,7 +993,8 @@ static inline int dr_reload_data_head( struct head_db *hd,
}

LM_INFO("loading drouting data!\n");
new_data = dr_load_routing_info(hd, dr_persistent_state, part_rule_list);
new_data = dr_load_routing_info(hd, dr_persistent_state, qr_parts,
part_index, part_name);
if ( new_data==0 ) {
LM_CRIT("failed to load routing info\n");
goto error;
Expand Down Expand Up @@ -1069,36 +1073,37 @@ static inline int dr_reload_data_head( struct head_db *hd,
static inline int dr_reload_data(int initial) {
struct head_db * it_head_db;
int ret_val = 0;
void *qr_main_list = NULL;
struct dr_link_rule_list_params *link_lists_param;
void *part_rule_list = NULL;
void *old_list = NULL;
void *old_list = NULL; /* list to be freed */
void *qr_parts_data = NULL; /* all the partitions */
int part_index;
struct dr_mark_as_main_list_params * mark_as_main_list;
struct dr_free_qr_list_params *free_list_params;
struct dr_create_partition_list_params *create_parts_list_params;

link_lists_param = (struct dr_link_rule_list_params*)pkg_malloc(
sizeof(struct dr_link_rule_list_params));
if(link_lists_param == NULL) {
LM_ERR("No more pkg memory");

create_parts_list_params = (struct dr_create_partition_list_params*)
pkg_malloc(sizeof(struct dr_create_partition_list_params));

if(create_parts_list_params == NULL) {
LM_ERR("no more pkg memory");
ret_val = -1;
}
link_lists_param->first_list = &qr_main_list;
create_parts_list_params->part_list = &qr_parts_data;
create_parts_list_params->n_parts = *n_partitions;
run_callbacks(dr_reg_cbs, DRCB_REG_CREATE_PARTS_LIST,
create_parts_list_params ); /* create the QR list for
all the partitions */

/* TODO will atomize operations under lock (rt_info_t vector) */
lock_start_write(reload_lock);
for( it_head_db=head_db_start; it_head_db!=NULL;
it_head_db=it_head_db->next ) {
if( dr_reload_data_head(it_head_db, initial, &part_rule_list)!=0 )
if( dr_reload_data_head(it_head_db, initial, qr_parts_data, part_index,
it_head_db->partition)!=0 )
ret_val = -1;

link_lists_param->second_list = part_rule_list;

run_callbacks(dr_reg_cbs, DRCB_REG_LINK_LISTS,
(void*)link_lists_param); /* link the lists for the partitions */
}

pkg_free(link_lists_param);

mark_as_main_list = (struct dr_mark_as_main_list_params*)
pkg_malloc(sizeof(struct dr_mark_as_main_list_params));

Expand All @@ -1107,8 +1112,8 @@ static inline int dr_reload_data(int initial) {
ret_val = -1; /* TODO */
}
/* TODO: only this should be under lock */
mark_as_main_list->new_list = qr_main_list;
mark_as_main_list->old_list = &old_list;
mark_as_main_list->qr_parts_new_list = qr_parts_data;
mark_as_main_list->qr_parts_old_list = &old_list;
/* make the new list the main list used by the QR */
run_callbacks(dr_reg_cbs, DRCB_REG_MARK_AS_RULE_LIST, mark_as_main_list);
lock_stop_write(reload_lock);
Expand Down Expand Up @@ -1442,6 +1447,8 @@ static int dr_init(void)
if(reload_lock == NULL) {
LM_ERR("failed to init rw lock for dr_reload\n");
}
n_partitions = (int*)shm_malloc(sizeof(int));
*n_partitions = 0;

drd_table.len = strlen(drd_table.s);
drg_table.len = strlen(drg_table.s);
Expand Down Expand Up @@ -2092,6 +2099,7 @@ mi_response_t *dr_reload_cmd_1(const mi_params_t *params,
{
struct head_db * part;
mi_response_t *resp;
str x;
void *rule_list;

LM_INFO("dr_reload MI command received!\n");
Expand All @@ -2100,7 +2108,7 @@ mi_response_t *dr_reload_cmd_1(const mi_params_t *params,
if (resp)
return resp;

if( dr_reload_data_head(part, 0, &rule_list)<0 ) {
if( dr_reload_data_head(part, 0, &rule_list, 0, x)<0 ) {
LM_CRIT("Failed to load data head\n");
return init_mi_error(500, MI_SSTR("Failed to reload"));
}
Expand Down
8 changes: 6 additions & 2 deletions modules/drouting/routing.c
Expand Up @@ -341,7 +341,9 @@ build_rt_info(
int sort_profile,
char* attrs,
rt_data_t* rd,
void **qr_rule_list,
void *qr_parts_data,
int part_index,
str part_name,
osips_malloc_f mf,
osips_free_f ff
)
Expand Down Expand Up @@ -488,7 +490,9 @@ build_rt_info(
}

add_rule_params->qr_rule = qr_rule;
add_rule_params->rule_list = qr_rule_list;
add_rule_params->qr_parts = qr_parts_data;
add_rule_params->part_name = part_name;
add_rule_params->part_index = part_index;
run_callbacks(dr_reg_cbs, DRCB_REG_ADD_RULE, add_rule_params);
pkg_free(add_rule_params);
}
Expand Down
4 changes: 3 additions & 1 deletion modules/drouting/routing.h
Expand Up @@ -142,7 +142,9 @@ build_rt_info(
int sort_profile,
char* attr,
rt_data_t* rd,
void **qr_rule_list,
void *qr_parts,
int part_index,
str part_name,
osips_malloc_f mf,
osips_free_f ff
);
Expand Down
97 changes: 81 additions & 16 deletions modules/qrouting/qr_stats.c
Expand Up @@ -178,18 +178,27 @@ void qr_free_rule(qr_rule_t *rule) {

}

void free_qr_list(qr_rule_t *list) {
void free_qr_list(qr_partitions_t *qr_parts) {
qr_rule_t * rule_it, *next;
int i;

/* free the rules from the given list */
rule_it = list;

while(rule_it != NULL) {
next = rule_it->next;
qr_free_rule(rule_it);
rule_it->next = NULL;
rule_it = next;
if(qr_parts == NULL) {
return ;
}
for(i = 0; i < qr_parts->n_parts; i++) {
rule_it = qr_parts->qr_rules_start[i];
/* free the rules from the given list */
while(rule_it != NULL) {
next = rule_it->next;
qr_free_rule(rule_it);
rule_it->next = NULL;
rule_it = next;
}
}
if(qr_parts->rw_lock != NULL)
lock_destroy_rw(qr_parts->rw_lock);
shm_free(qr_parts->qr_rules_start);
shm_free(qr_parts);

}

Expand All @@ -198,7 +207,7 @@ void free_qr_cb(int type, struct dr_cb_params *param) {
*param->param;
LM_DBG("freeing the old rules...\n");

qr_rule_t * old_list = free_params->old_list;
qr_partitions_t * old_list = free_params->old_list;
free_qr_list(old_list);
}

Expand Down Expand Up @@ -283,21 +292,74 @@ void qr_dst_is_grp(int type, struct dr_cb_params *params) {
shm_free(rule->dest[n_dst].dst.grp.gw);
}

void qr_create_partition_list(int type, struct dr_cb_params *param) {
struct dr_create_partition_list_params *partition_list_params;
partition_list_params = (struct dr_create_partition_list_params*)*param->param;
qr_partitions_t **part_list =
(qr_partitions_t**)partition_list_params->part_list;
int n_partitions = partition_list_params->n_parts;

*part_list = (qr_partitions_t*)shm_malloc(sizeof(qr_partitions_t));
memset(*part_list, 0, sizeof(qr_partitions_t));
if (part_list == NULL) {
LM_ERR("no more shm memory");
return;
}
if(((*part_list)->rw_lock = lock_init_rw()) == NULL) {
LM_ERR("failed to init rw lock");
goto error;
}
(*part_list)->qr_rules_start = (qr_rule_t**)shm_malloc(
n_partitions * sizeof(qr_rule_t*));
if((*part_list)->qr_rules_start == NULL) {
LM_ERR("no more shm memory");
goto error;
}

(*part_list)->part_name = (str*)shm_malloc(n_partitions*sizeof(str));
(*part_list)->n_parts = n_partitions;

memset((*part_list)->part_name, 0, n_partitions*sizeof(str));
memset((*part_list)->qr_rules_start, 0,n_partitions*sizeof(qr_rule_t*));

return ;
error:
if((*part_list)->rw_lock != NULL) {
lock_destroy_rw((*part_list)->rw_lock);

}

if((*part_list)->qr_rules_start) {
shm_free((*part_list)->qr_rules_start);
}

if(part_list != NULL) {
shm_free(part_list);
part_list = NULL;
}

}

/* add rule to list. if the list is NULL a new list is created */
void qr_add_rule_to_list(int type, struct dr_cb_params * param) {
struct dr_add_rule_params *add_rule_params =
(struct dr_add_rule_params*)*param->param;
qr_rule_t **rule_list = (qr_rule_t**)add_rule_params->rule_list;
qr_partitions_t *qr_parts = (qr_partitions_t*)add_rule_params->qr_parts;
qr_rule_t *new = add_rule_params->qr_rule;
int part_index = add_rule_params->part_index;
qr_rule_t **rule_list = &qr_parts->qr_rules_start[part_index];
str part_name = add_rule_params->part_name;

if(new != NULL) {
if(*rule_list == NULL) {
*rule_list = new;
qr_parts->part_name[part_index] = part_name;
} else {
new->next = *rule_list;
*rule_list = new;
}
LM_DBG("rule '%d' added to qr rule list \n", new->r_id);
LM_DBG("rule '%d' added to qr rule list for partition index '%d' \n",
new->r_id, part_index);
}
}

Expand All @@ -307,11 +369,14 @@ void qr_add_rule_to_list(int type, struct dr_cb_params * param) {
void qr_mark_as_main_list(int type, struct dr_cb_params * param) {
struct dr_mark_as_main_list_params * mark_as_main_list =
(struct dr_mark_as_main_list_params*) *param->param;
qr_rule_t *rule_list = (qr_rule_t*)mark_as_main_list->new_list;
qr_partitions_t *qr_parts_new = (qr_partitions_t*)mark_as_main_list
->qr_parts_new_list;

LM_DBG("Mark main QR rule list\n");
*mark_as_main_list->old_list = *qr_rules_start; /* save old list so it can be freed */
*qr_rules_start = rule_list; /* the new list that the QR will work with */
*mark_as_main_list->qr_parts_old_list = *qr_main_list; /* save old list so it can be freed */
lock_start_write(*rw_lock_qr);
*qr_main_list = qr_parts_new; /* the new list that the QR will work with */
lock_stop_write(*rw_lock_qr);
}

/* copy link two rule lists together => used for dr_reload and partitions
Expand All @@ -328,7 +393,7 @@ void qr_link_rule_list(int type, struct dr_cb_params *param) {
} else {
for(rule_it = *first_list; rule_it->next != NULL;
rule_it = rule_it->next) { /* go to the last rule from the first
list */
list */
}
rule_it->next = second_list; /* link it to the second list */
}
Expand Down

0 comments on commit 9ccc1fb

Please sign in to comment.