Skip to content

Commit

Permalink
ratelimit: port script functions to new param interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu committed Apr 2, 2019
1 parent a55a5d5 commit 3c6f151
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 91 deletions.
51 changes: 15 additions & 36 deletions modules/ratelimit/ratelimit.c
Expand Up @@ -101,9 +101,6 @@ static unsigned int rl_repl_timer_interval = RL_TIMER_INTERVAL;
static int mod_init(void);
static int mod_child(int);

/* fixup prototype */
static int fixup_rl_check(void **param, int param_no);

mi_response_t *mi_stats(const mi_params_t *params,
struct mi_handler *async_hdl);
mi_response_t *mi_stats_1(const mi_params_t *params,
Expand All @@ -120,19 +117,21 @@ static int pv_get_rl_count(struct sip_msg *msg, pv_param_t *param,
static int pv_parse_rl_count(pv_spec_p sp, str *in);

static cmd_export_t cmds[] = {
{"rl_check", (cmd_function)w_rl_check_2, 2,
fixup_rl_check, 0, REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{"rl_check", (cmd_function)w_rl_check_3, 3,
fixup_rl_check, 0, REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{"rl_dec_count", (cmd_function)w_rl_dec, 1,
fixup_spve_null, 0, REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{"rl_reset_count", (cmd_function)w_rl_reset, 1,
fixup_spve_null, 0, REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{0,0,0,0,0,0}
{"rl_check", (cmd_function)w_rl_check, {
{CMD_PARAM_STR,0,0},
{CMD_PARAM_INT,0,0},
{CMD_PARAM_STR,0,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{"rl_dec_count", (cmd_function)w_rl_dec, {
{CMD_PARAM_STR,0,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{"rl_reset_count", (cmd_function)w_rl_reset, {
{CMD_PARAM_STR,0,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|
BRANCH_ROUTE|ERROR_ROUTE|LOCAL_ROUTE|TIMER_ROUTE|EVENT_ROUTE},
{0,0,{{0,0,0}},0}
};

static param_export_t params[] = {
Expand Down Expand Up @@ -789,26 +788,6 @@ mi_response_t *mi_reset_pipe(const mi_params_t *params,
return init_mi_result_ok();
}

/* fixup functions */
static int fixup_rl_check(void **param, int param_no)
{
switch (param_no) {
/* pipe name */
case 1:
return fixup_spve(param);
/* limit */
case 2:
return fixup_igp(param);
/* algorithm */
case 3:
return fixup_sgp(param);
/* error */
default:
LM_ERR("[BUG] too many params (%d)\n", param_no);
}
return E_UNSPEC;
}

/* pseudo-variable functions */
static int pv_get_rl_count(struct sip_msg *msg, pv_param_t *param,
pv_value_t *res)
Expand Down
7 changes: 3 additions & 4 deletions modules/ratelimit/ratelimit.h
Expand Up @@ -114,10 +114,9 @@ void mod_destroy(void);
int init_rl_table(unsigned int size);

/* exported functions */
int w_rl_check_2(struct sip_msg*, char *, char *);
int w_rl_check_3(struct sip_msg*, char *, char *, char *);
int w_rl_dec(struct sip_msg*, char *);
int w_rl_reset(struct sip_msg*, char *);
int w_rl_check(struct sip_msg*, str *, int *, str *);
int w_rl_dec(struct sip_msg*, str *);
int w_rl_reset(struct sip_msg*, str *);
int w_rl_set_count(str, int);
int rl_stats(mi_item_t *, str *);
int rl_pipe_check(rl_pipe_t *);
Expand Down
74 changes: 23 additions & 51 deletions modules/ratelimit/ratelimit_helper.c
Expand Up @@ -309,11 +309,6 @@ static str * get_rl_algo_name(rl_algo_t algo)
return NULL;
}

int w_rl_check_2(struct sip_msg *_m, char *_n, char *_l)
{
return w_rl_check_3(_m, _n, _l, NULL);
}

rl_pipe_t *rl_create_pipe(int limit, rl_algo_t algo)
{
rl_pipe_t *pipe;
Expand Down Expand Up @@ -343,93 +338,76 @@ rl_pipe_t *rl_create_pipe(int limit, rl_algo_t algo)
return pipe;
}

int w_rl_check_3(struct sip_msg *_m, char *_n, char *_l, char *_a)
int w_rl_check(struct sip_msg *_m, str *name, int *limit, str *algorithm)
{
str name;
int limit = 0, ret = 1, should_update = 0;
str algorithm;
int ret = 1, should_update = 0;
unsigned int hash_idx;
rl_pipe_t **pipe;

rl_algo_t algo = -1;

/* retrieve and check parameters */
if (!_n || !_l) {
LM_ERR("invalid parameters\n");
goto end;
}
if (fixup_get_svalue(_m, (gparam_p) _n, &name) < 0) {
LM_ERR("cannot retrieve identifier\n");
goto end;
}
if (fixup_get_ivalue(_m, (gparam_p) _l, &limit) < 0) {
LM_ERR("cannot retrieve limit\n");
goto end;
}
algorithm.s = 0;
if (!_a || fixup_get_svalue(_m, (gparam_p) _a, &algorithm) < 0 ||
(algo = get_rl_algo(algorithm)) == PIPE_ALGO_NOP) {
if (!algorithm || (algo = get_rl_algo(*algorithm)) == PIPE_ALGO_NOP) {
algo = PIPE_ALGO_NOP;
}

/* get limit for FEEDBACK algorithm */
if (algo == PIPE_ALGO_FEEDBACK) {
lock_get(rl_lock);
if (*rl_feedback_limit) {
if (*rl_feedback_limit != limit) {
if (*rl_feedback_limit != *limit) {
LM_WARN("FEEDBACK limit should be the same for all pipes, but"
" new limit %d differs - setting to %d\n",
limit, *rl_feedback_limit);
limit = *rl_feedback_limit;
*limit, *rl_feedback_limit);
*limit = *rl_feedback_limit;
}
} else {
if (limit <= 0 || limit >= 100) {
if (*limit <= 0 || *limit >= 100) {
LM_ERR("invalid limit for FEEDBACK algorithm "
"(must be between 0 and 100)\n");
lock_release(rl_lock);
goto end;
}
*rl_feedback_limit = limit;
pid_setpoint_limit(limit);
*rl_feedback_limit = *limit;
pid_setpoint_limit(*limit);
}
lock_release(rl_lock);
}

hash_idx = RL_GET_INDEX(name);
hash_idx = RL_GET_INDEX(*name);
RL_GET_LOCK(hash_idx);

/* try to get the value */
pipe = RL_GET_PIPE(hash_idx, name);
pipe = RL_GET_PIPE(hash_idx, *name);
if (!pipe) {
LM_ERR("cannot get the index\n");
goto release;
}

if (!*pipe) {
/* allocate new pipe */
if (!(*pipe = rl_create_pipe(limit, algo)))
if (!(*pipe = rl_create_pipe(*limit, algo)))
goto release;

LM_DBG("Pipe %.*s doesn't exist, but was created %p\n",
name.len, name.s, *pipe);
name->len, name->s, *pipe);
if ((*pipe)->algo == PIPE_ALGO_NETWORK)
should_update = 1;
} else {
LM_DBG("Pipe %.*s found: %p - last used %lu\n",
name.len, name.s, *pipe, (*pipe)->last_used);
name->len, name->s, *pipe, (*pipe)->last_used);
if (algo != PIPE_ALGO_NOP && (*pipe)->algo != algo) {
LM_WARN("algorithm %d different from the initial one %d for pipe "
"%.*s", algo, (*pipe)->algo, name.len, name.s);
"%.*s", algo, (*pipe)->algo, name->len, name->s);
}
/* update the limit */
(*pipe)->limit = limit;
(*pipe)->limit = *limit;
}

/* set the last used time */
(*pipe)->last_used = time(0);
if (RL_USE_CDB(*pipe)) {
/* release the counter for a while */
if (rl_change_counter(&name, *pipe, 1) < 0) {
if (rl_change_counter(name, *pipe, 1) < 0) {
LM_ERR("cannot increase counter\n");
goto release;
}
Expand All @@ -439,7 +417,7 @@ int w_rl_check_3(struct sip_msg *_m, char *_n, char *_l, char *_a)

ret = rl_pipe_check(*pipe);
LM_DBG("Pipe %.*s counter:%d load:%d limit:%d should %sbe blocked (%p)\n",
name.len, name.s, (*pipe)->counter, (*pipe)->load,
name->len, name->s, (*pipe)->counter, (*pipe)->load,
(*pipe)->limit, ret == 1 ? "NOT " : "", *pipe);


Expand Down Expand Up @@ -691,27 +669,21 @@ int w_rl_set_count(str key, int val)
return ret;
}

static inline int w_rl_change_counter(struct sip_msg *_m, char *_n, int dec)
static inline int w_rl_change_counter(struct sip_msg *_m, str *name, int dec)
{
str name;

if (!_n || fixup_get_svalue(_m, (gparam_p) _n, &name) < 0) {
LM_ERR("cannot retrieve identifier\n");
return -1;
}
if (w_rl_set_count(name, dec)) {
LM_ERR("cannot find any pipe named %.*s\n", name.len, name.s);
if (w_rl_set_count(*name, dec)) {
LM_ERR("cannot find any pipe named %.*s\n", name->len, name->s);
return -1;
}
return 1;
}

int w_rl_dec(struct sip_msg *_m, char *_n)
int w_rl_dec(struct sip_msg *_m, str *_n)
{
return w_rl_change_counter(_m, _n, -1);
}

int w_rl_reset(struct sip_msg *_m, char *_n)
int w_rl_reset(struct sip_msg *_m, str *_n)
{
return w_rl_change_counter(_m, _n, 0);
}
Expand Down

0 comments on commit 3c6f151

Please sign in to comment.