Skip to content

Commit

Permalink
[qrouting] registering carriers, debug info and qr_create_gw
Browse files Browse the repository at this point in the history
  • Loading branch information
tallicamike authored and razvancrainea committed Feb 11, 2020
1 parent d69747c commit 665c921
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 66 deletions.
21 changes: 14 additions & 7 deletions modules/drouting/dr_api.h
Expand Up @@ -48,14 +48,21 @@ typedef int (*add_rule_f)(dr_head_p partition, unsigned int rid,
str *prefix, unsigned int gr_id, unsigned int priority,
tmrec_t *time_rec, void *attr);
typedef str * (*get_gw_name_f) (pgw_t *gw);

typedef int (*get_cr_n_gw_f) (pcr_t *cr);
typedef str * (*get_cr_name_f) (pcr_t *cr);
typedef pgw_t * (*get_gw_from_cr_f) (pcr_t *cr, int n); /* gets the n-th
gateway from the
carrier */
struct dr_binds {
create_head_f create_head;
free_head_f free_head;
match_number_f match_number;
add_rule_f add_rule;
register_drcb_f register_drcb;
get_gw_name_f get_gw_name;
create_head_f create_head;
free_head_f free_head;
match_number_f match_number;
add_rule_f add_rule;
register_drcb_f register_drcb;
get_gw_name_f get_gw_name;
get_cr_name_f get_cr_name;
get_cr_n_gw_f get_cr_n_gw;
get_gw_from_cr_f get_gw_from_cr;
};

typedef int (*load_dr_api_f)(struct dr_binds *drb);
Expand Down
20 changes: 20 additions & 0 deletions modules/drouting/dr_api_internal.c
Expand Up @@ -41,12 +41,29 @@ static int add_rule_api(dr_head_p partition, unsigned int rid,


static str * get_gw_name(pgw_t * gw);
static str * get_cr_name(pcr_t * cr);
static int get_cr_n_gw(pcr_t * cr); /* gets the number of gateways from a carrier */
static pgw_t * get_gw_from_cr (pcr_t *cr, int n);


static str * get_gw_name(pgw_t *gw) {
return &gw->id;
}

static int get_cr_n_gw(pcr_t * cr) {
return cr->pgwa_len;
}

static pgw_t * get_gw_from_cr(pcr_t *cr, int n) {
if(cr->pgwa_len > n)
return cr->pgwl[n].dst.gw; /* a carrier cannot contain another carrier */
return NULL; /* provided index was bigger than the vector */
}

static str *get_cr_name(pcr_t * cr) {
return &cr->id;
}

/* Warning this function assumes the lock is already taken */
rt_info_t* find_rule_by_prefix_unsafe(ptree_t *pt, ptree_node_t *noprefix,
str prefix, unsigned int grp_id, unsigned int *matched_len)
Expand Down Expand Up @@ -77,6 +94,9 @@ int load_dr (struct dr_binds *drb)
drb->add_rule = add_rule_api;
drb->register_drcb = register_dr_cb;
drb->get_gw_name = get_gw_name;
drb->get_cr_n_gw = get_cr_n_gw;
drb->get_cr_name = get_cr_name;
drb->get_gw_from_cr = get_gw_from_cr;
return 0;
}

Expand Down
7 changes: 7 additions & 0 deletions modules/drouting/dr_cb.h
Expand Up @@ -30,6 +30,13 @@
#ifndef _DR_CB_H_
#define _DR_CB_H_

/* parameters needed for the registration of a gw */
struct dr_reg_param {
void *rule;
int n_dst; /* the index of the destination within the rule */
void *cr_or_gw;
};

/* callback types used on top of DRouting */
enum drcb_types {
DRCB_REG_CREATE_PARTS_LIST /* create a partitions list */,
Expand Down
29 changes: 20 additions & 9 deletions modules/drouting/routing.c
Expand Up @@ -330,11 +330,11 @@ build_rt_info(

/* callback parameters for the QR module */
int i;
void * qr_rule;
void * qr_rule = NULL;
struct dr_callback *dr_cb_it;
struct dr_cb_params *cb_params;
struct dr_reg_gw_params *reg_gw_params;
struct dr_reg_init_rule_params *init_rule_params;
struct dr_reg_param *reg_dst_param;
pgw_list_t *p = NULL;

rt = (rt_info_t*)func_malloc(mf, sizeof(rt_info_t) +
Expand Down Expand Up @@ -394,19 +394,30 @@ build_rt_info(
/* TODO: should check if qr loaded */
for(i = 0; i < rt->pgwa_len; i++) {
if(p[i].is_carrier) {
reg_dst_param = (struct dr_reg_param *) shm_malloc(
sizeof(struct dr_reg_param));
if(reg_dst_param == NULL) {
LM_ERR("no more shm 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_callbacks(dr_reg_cbs, DRCB_REG_CR, (void*)reg_dst_param);
}

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

run_callbacks(dr_reg_cbs, DRCB_REG_GW, (void*)reg_gw_params);
run_callbacks(dr_reg_cbs, DRCB_REG_GW, (void*)reg_dst_param);
}
}
}
Expand Down
108 changes: 64 additions & 44 deletions modules/qrouting/qr_stats.c
Expand Up @@ -63,28 +63,12 @@ qr_sample_t * create_history(void) {
return history;
}

/* create a gateway */
void qr_dst_is_gw(int type, struct dr_cb_params *param){
qr_gw_t * qr_create_gw(void *dst) {
qr_gw_t *gw = NULL; /* internal gw for qr */
void *dst; /* pgw_t received from dr */
qr_rule_t *rule; /* qr_rule that was initialised with the qr callback from dr */
int n_dst; /* the number of the destination within the dr rule */
str *gw_name;

/* extract the parameters from dr */
rule = (qr_rule_t*)((struct dr_reg_gw_params *)(*param->param))->rule;
dst = ((struct dr_reg_gw_params *)*param->param)->gw;
n_dst = ((struct dr_reg_gw_params *)*param->param)->n_dst;

gw_name = drb.get_gw_name(dst);
LM_DBG("Adding gw '%.*s' to rule id: %d\n", gw_name->len, gw_name->s,
rule->r_id);


if(rule == 0) {
LM_ERR("no rule to add gw to\n");
goto error;
}
LM_DBG("Creating gw '%.*s'\n", gw_name->len, gw_name->s);

if ((gw = (qr_gw_t*)shm_malloc(sizeof(qr_gw_t))) == NULL) {
LM_ERR("no more shm memory\n");
Expand All @@ -109,12 +93,34 @@ void qr_dst_is_gw(int type, struct dr_cb_params *param){
gw->dr_gw = dst; /* save the pointer to the dr gateway */

/* save gateway to rule */
rule->dest[n_dst].type = QR_DST_GW;
rule->dest[n_dst].dst.gw = gw;
return ;
//rule->dest[n_dst].type = QR_DST_GW;
//rule->dest[n_dst].dst.gw = gw;
return gw;
error:
if(gw)
qr_free_gw(gw);
return NULL;

}

/* make gateway a given destination - to be registered as callback */
void qr_dst_is_gw(int type, struct dr_cb_params *param){
void *dst; /* pgw_t received from dr */
qr_rule_t *rule; /* qr_rule that was initialised with the qr callback from dr */
int n_dst; /* the number of the destination within the dr rule */

/* extract the parameters from dr */
rule = (qr_rule_t*)((struct dr_reg_param *)(*param->param))->rule;
dst = ((struct dr_reg_param *)*param->param)->cr_or_gw;
n_dst = ((struct dr_reg_param *)*param->param)->n_dst;
LM_DBG("Adding gw to rule %d\n", rule->r_id);

if(rule != NULL) {
rule->dest[n_dst].type = QR_DST_GW;
rule->dest[n_dst].dst.gw = qr_create_gw(dst);
} else {
LM_ERR("no rule to add the gateway to\n");
}
}

/* free all the samples in a gateway's history */
Expand Down Expand Up @@ -143,7 +149,7 @@ void qr_free_gw(qr_gw_t * gw) {
/* creates a rule n_dest destinations (by default marked as gws) */
void qr_create_rule(int type, struct dr_cb_params * param) {
qr_rule_t *new = NULL;
int i, r_id;
int r_id;
struct dr_reg_init_rule_params *init_rule_params = NULL;
init_rule_params = (struct dr_reg_init_rule_params *)*param->param;

Expand All @@ -164,33 +170,47 @@ void qr_create_rule(int type, struct dr_cb_params * param) {
this rule, as rcvd from dr*/
new->r_id = r_id;
init_rule_params->rule = new; /* send the rule to the dr */
LM_DBG("Rule %d created\n", r_id);
}

/* marks index_grp destination from the rule as group and creates the gw array */
int qr_dst_is_grp(void *rule_v, int index_grp, int n_gw) {
qr_rule_t *rule = (qr_rule_t*)rule_v;

if(rule == NULL) {
void qr_dst_is_grp(int type, struct dr_cb_params *params) {
qr_rule_t *rule = (qr_rule_t*)((struct dr_reg_param *)*params->param)
->rule;
void * dr_gw;
str *cr_name, *gw_name;
int i;
int n_dst = ((struct dr_reg_param*)*params->param)->n_dst;
int n_gws ;
void *grp = ((struct dr_reg_param*)*params->param)->cr_or_gw;
n_gws = drb.get_cr_n_gw(grp);
cr_name = drb.get_cr_name(grp);
LM_DBG("Carrier '%.*s' with %d gateways added to rule %d\n", cr_name->len,
cr_name->s, n_gws, rule->r_id);


if(rule != NULL) {
rule->dest[n_dst].type = QR_DST_GRP;
rule->dest[n_dst].dst.grp.gw = (qr_gw_t**)shm_malloc(n_gws *
sizeof(qr_gw_t*));
if(rule->dest[n_dst].dst.grp.gw != NULL) {
rule->dest[n_dst].dst.grp.n = n_gws;
rule->dest[n_dst].dst.grp.id = cr_name;
for(i = 0; i < n_gws; i++) {
dr_gw = (void*)drb.get_gw_from_cr(grp, i); /* get the gateway
as pgw_t from dr */
rule->dest[n_dst].dst.grp.gw[i] = qr_create_gw(dr_gw);
gw_name = drb.get_gw_name(rule->dest[n_dst].dst.grp.gw[i]->dr_gw);
LM_DBG("Gw '%.*s' added to carrier '%.*s' from rule %d\n",
gw_name->len, gw_name->s, cr_name->len, cr_name->s,
rule->r_id);
}
} else {
LM_ERR("no more shm memory\n");
}
} else {
LM_ERR("bad rule\n");
return -1;
}
rule->dest[index_grp].type = 0;
rule->dest[index_grp].type |= QR_DST_GRP;

rule->dest[index_grp].dst.grp.gw = (qr_gw_t**)shm_malloc(n_gw *
sizeof(qr_gw_t*));
if(rule->dest[index_grp].dst.grp.gw == NULL) {
LM_ERR("no more shm memory\n");
goto error;
}
rule->dest[index_grp].dst.grp.n = n_gw;

return 0;
error:
if(rule->dest[index_grp].dst.grp.gw != NULL)
shm_free(rule->dest[index_grp].dst.grp.gw);
return -1;

}

/* add rule to internal rule list */
Expand Down
7 changes: 4 additions & 3 deletions modules/qrouting/qr_stats.h
Expand Up @@ -98,9 +98,10 @@ typedef struct qr_gw {
typedef struct qr_grp {
qr_gw_t **gw;
char sort_method; /* sorting for the group */
str name;
str *id;
int n;
} qr_grp_t;
/* TODO: add weights */


/* two types of destination */
Expand All @@ -124,9 +125,9 @@ typedef struct qr_rule {

extern qr_rule_t ** qr_rules_start; /* used when updating statistics */

qr_gw_t * qr_create_gw(void);
qr_gw_t * qr_create_gw(void *);
void qr_free_gw(qr_gw_t *);
int qr_dst_is_grp(void *, int, int);
void qr_dst_is_grp(int, struct dr_cb_params*);
void qr_create_rule(int, struct dr_cb_params*);
void qr_add_rule(int , struct dr_cb_params*);
void test_callback(int types, struct dr_cb_params *param);
Expand Down
8 changes: 5 additions & 3 deletions modules/qrouting/qrouting.c
Expand Up @@ -98,8 +98,6 @@ struct module_exports exports = {
};

static int qr_init(void){
int i;
qr_rule_t *my_rule; /* FIXME: testing purpose */
LM_INFO("QR module\n");
LM_DBG("history = %d, sampling_interval = %d\n", history,
sampling_interval);
Expand Down Expand Up @@ -137,6 +135,10 @@ static int qr_init(void){
LM_ERR("[QR] failed to register DRCB_REG_REG_GW callback to DR\n");
return -1;
}
if(drb.register_drcb(DRCB_REG_CR, &qr_dst_is_grp, NULL, NULL) < 0) {
LM_ERR("[QR] failed to register DRCB_REG_REG_GW callback to DR\n");
return -1;
}
if(drb.register_drcb(DRCB_REG_ADD_RULE, &qr_add_rule, NULL, NULL) < 0) {
LM_ERR("[QR] failed to register DRCB_REG_ADD_RULE callback to DR\n");
return -1;
Expand Down Expand Up @@ -187,7 +189,7 @@ static str * qr_get_dst_name(qr_dst_t * dst) {
if(dst->type == QR_DST_GW) {
return drb.get_gw_name(dst->dst.gw->dr_gw);
} else {
return &dst->dst.grp.name;
return dst->dst.grp.id;
}
}

Expand Down

0 comments on commit 665c921

Please sign in to comment.