ratelimit.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <math.h>
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../timer.h"
#include "../../ut.h"
#include "../../locking.h"
#include "../../mod_fix.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../statistics.h"
#include "../sl/sl_api.h"

Include dependency graph for ratelimit.c:

Go to the source code of this file.

Data Structures

struct  pipe
struct  pipe_params
struct  rl_queue
struct  rl_queue_params
struct  str_map

Defines

#define LOCK_GET   lock_get
#define LOCK_RELEASE   lock_release
#define MAX_PIPES   16
#define MAX_QUEUES   10
#define RL_TIMER_INTERVAL   10
#define RXL(m, str, i)   (m)[i].rm_eo - (m)[i].rm_so
#define RXLS(m, str, i)   (m)[i].rm_eo - (m)[i].rm_so, (str) + (m)[i].rm_so
#define RXS(m, str, i)   (str) + (m)[i].rm_so

Typedefs

typedef struct pipe_params pipe_params_t
typedef struct pipe pipe_t
typedef struct rl_queue_params rl_queue_params_t
typedef struct rl_queue rl_queue_t
typedef struct str_map str_map_t

Enumerations

enum  {
  PIPE_ALGO_NOP = 0, PIPE_ALGO_RED, PIPE_ALGO_TAILDROP, PIPE_ALGO_FEEDBACK,
  PIPE_ALGO_NETWORK
}
enum  { LOAD_SOURCE_CPU, LOAD_SOURCE_EXTERNAL }

Functions

static int add_pipe_params (modparam_t, void *)
static int add_queue_params (modparam_t, void *)
static int check_feedback_setpoints (int modparam)
void destroy (void)
static void do_update_load (void)
static int find_queue (struct sip_msg *msg, int *queue)
static int get_cpuload (double *load)
static int init_params (void)
struct mi_rootmi_get_pid (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_get_pipes (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_get_queues (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_push_load (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_set_dbg (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_set_pid (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_set_pipe (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_set_queue (struct mi_root *cmd_tree, void *param)
struct mi_rootmi_stats (struct mi_root *cmd_tree, void *param)
static int mod_init (void)
static int parse_pipe_params (char *line, pipe_params_t *params)
static int parse_queue_params (char *line, rl_queue_params_t *params)
static int pipe_push (struct sip_msg *msg, int id)
static int rl_check (struct sip_msg *msg, int forced_pipe)
static int rl_drop (struct sip_msg *msg, unsigned int low, unsigned int high)
static void rl_timer (unsigned int, void *)
static int str_cmp (const str *a, const str *b)
static int str_cpy (str *dest, str *src)
static int str_i_cmp (const str *a, const str *b)
static int str_map_int (const str_map_t *map, int key, str *ret)
static int str_map_str (const str_map_t *map, const str *key, int *ret)
static void update_cpu_load (void)
static int w_rl_check_default (struct sip_msg *, char *, char *)
static int w_rl_check_forced (struct sip_msg *, char *, char *)
static int w_rl_check_forced_pipe (struct sip_msg *, char *, char *)
static int w_rl_drop (struct sip_msg *, char *, char *)
static int w_rl_drop_default (struct sip_msg *, char *, char *)
static int w_rl_drop_forced (struct sip_msg *, char *, char *)

Variables

str_map_t algo_names []
static int cfg_setpoint
static cmd_export_t cmds []
static int * drop_rate
struct module_exports exports
int hash [100]
static double int_err = 0.0
static double last_err = 0.0
static int * load_source
static int load_source_mp = LOAD_SOURCE_CPU
static double * load_value
static mi_export_t mi_cmds []
static int * network_load_value
static int * nqueues
static int nqueues_mp = 0
static param_export_t params []
static int params_inited = 0
static double * pid_kd
static double * pid_ki
static double * pid_kp
static double * pid_setpoint
static regex_t pipe_params_regex
static pipe_t pipes [MAX_PIPES]
str queue_other = str_init("*")
static regex_t queue_params_regex
static rl_queue_t queues [MAX_QUEUES]
static strrl_dbg_str = NULL
static int rl_drop_code = 503
static str rl_drop_reason = str_init("Server Unavailable")
gen_lock_t * rl_lock
struct sl_binds slb
str_map_t source_names []
static int timer_interval = RL_TIMER_INTERVAL


Define Documentation

#define LOCK_GET   lock_get

#define LOCK_RELEASE   lock_release

#define MAX_PIPES   16

Definition at line 53 of file ratelimit.c.

Referenced by mod_init().

#define MAX_QUEUES   10

Definition at line 54 of file ratelimit.c.

Referenced by add_queue_params(), mi_get_queues(), and mi_set_queue().

#define RL_TIMER_INTERVAL   10

Definition at line 59 of file ratelimit.c.

#define RXL ( m,
str,
 )     (m)[i].rm_eo - (m)[i].rm_so

Definition at line 62 of file ratelimit.c.

Referenced by parse_pipe_params(), and parse_queue_params().

#define RXLS ( m,
str,
 )     (m)[i].rm_eo - (m)[i].rm_so, (str) + (m)[i].rm_so

Definition at line 61 of file ratelimit.c.

Referenced by parse_pipe_params(), and parse_queue_params().

#define RXS ( m,
str,
 )     (str) + (m)[i].rm_so

Definition at line 63 of file ratelimit.c.

Referenced by parse_pipe_params(), and parse_queue_params().


Typedef Documentation

typedef struct pipe_params pipe_params_t

typedef struct pipe pipe_t

typedef struct rl_queue rl_queue_t

typedef struct str_map str_map_t


Enumeration Type Documentation

anonymous enum

Enumerator:
PIPE_ALGO_NOP 
PIPE_ALGO_RED 
PIPE_ALGO_TAILDROP 
PIPE_ALGO_FEEDBACK 
PIPE_ALGO_NETWORK 

Definition at line 84 of file ratelimit.c.

anonymous enum

Enumerator:
LOAD_SOURCE_CPU 
LOAD_SOURCE_EXTERNAL 

Definition at line 107 of file ratelimit.c.


Function Documentation

static int add_pipe_params ( modparam_t  type,
void *  val 
) [static]

static int add_queue_params ( modparam_t  type,
void *  val 
) [static]

static int check_feedback_setpoints ( int  modparam  )  [static]

checks that all FEEDBACK pipes use the same setpoint cpu load. also sets (common) cfg_setpoint value

Parameters:
modparam 1 to check modparam (static) fields, 0 to use shm ones
Returns:
0 if ok, -1 on error

Definition at line 1042 of file ratelimit.c.

References cfg_setpoint, pipe::limit, pipe::limit_mp, LM_ERR, and PIPE_ALGO_FEEDBACK.

Referenced by add_pipe_params(), and mi_set_pipe().

void destroy ( void   ) 

static void do_update_load ( void   )  [static]

Definition at line 378 of file ratelimit.c.

References drop_rate, load_value, and pid_setpoint.

Referenced by mi_push_load(), and update_cpu_load().

static int find_queue ( struct sip_msg msg,
int *  queue 
) [static]

finds the queue associated with the message's method (expects rl_lock to be taken)

Returns:
0 if a nueue was found, -1 otherwise

Definition at line 780 of file ratelimit.c.

References sip_msg::first_line, LM_INFO, msg_start::request, str_i_cmp(), and msg_start::u.

Referenced by rl_check().

static int get_cpuload ( double *  load  )  [static]

Definition at line 324 of file ratelimit.c.

References LM_ERR.

Referenced by update_cpu_load().

static int init_params ( void   )  [static]

compiles regexes for parsing modparams and clears the pipes and queues

Returns:
0 on success

Definition at line 958 of file ratelimit.c.

References LM_ERR, params_inited, pipe_params_regex, and queue_params_regex.

Referenced by parse_pipe_params(), and parse_queue_params().

struct mi_root * mi_get_pid ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_get_pipes ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_get_queues ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_push_load ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_set_dbg ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_set_pid ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_set_pipe ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_set_queue ( struct mi_root cmd_tree,
void *  param 
) [read]

struct mi_root * mi_stats ( struct mi_root cmd_tree,
void *  param 
) [read]

static int mod_init ( void   )  [static]

static int parse_pipe_params ( char *  line,
pipe_params_t params 
) [static]

parses a "pipe_no:algorithm:bandwidth" line

Returns:
0 on success

Definition at line 978 of file ratelimit.c.

References pipe_params::algo, init_params(), _str::len, pipe_params::limit, LM_DBG, LM_ERR, pipe_params::no, params_inited, pipe_params_regex, RXL, RXLS, RXS, _str::s, and str_map_str().

Referenced by add_pipe_params().

static int parse_queue_params ( char *  line,
rl_queue_params_t params 
) [static]

parses a "pipe_no:method" line

Returns:
0 on success

Definition at line 1007 of file ratelimit.c.

References init_params(), _str::len, len, LM_DBG, LM_ERR, rl_queue_params::method, params_inited, rl_queue_params::pipe, pkg_malloc, queue_params_regex, RXL, RXLS, RXS, and _str::s.

Referenced by add_queue_params().

static int pipe_push ( struct sip_msg msg,
int  id 
) [static]

runs the pipe's algorithm (expects rl_lock to be taken), TODO revert to "return" instead of "ret ="

Returns:
-1 if drop needed, 1 if allowed

Definition at line 817 of file ratelimit.c.

References pipe::counter, drop_rate, pipe::limit, LM_ERR, pipe::load, PIPE_ALGO_FEEDBACK, PIPE_ALGO_NETWORK, PIPE_ALGO_NOP, PIPE_ALGO_RED, PIPE_ALGO_TAILDROP, and timer_interval.

Referenced by rl_check().

static int rl_check ( struct sip_msg msg,
int  forced_pipe 
) [static]

runs the current request through the queues

Parameters:
msg 
forced_pipe is >= 0 if a specific pipe should be used, < 0 otherwise
Returns:
-1 if drop needed, 1 if allowed

Definition at line 857 of file ratelimit.c.

References pipe::algo, pipe::counter, find_queue(), sip_msg::first_line, _str::len, pipe::limit, LM_DBG, pipe::load, load_value, LOCK_GET, LOCK_RELEASE, network_load_value, rl_queue::pipe, pipe_push(), msg_start::request, rl_lock, _str::s, and msg_start::u.

Referenced by w_rl_check_default(), w_rl_check_forced(), and w_rl_check_forced_pipe().

static int rl_drop ( struct sip_msg msg,
unsigned int  low,
unsigned int  high 
) [static]

static void rl_timer ( unsigned int  ticks,
void *  param 
) [static]

static int str_cmp ( const str a,
const str b 
) [inline, static]

Definition at line 763 of file ratelimit.c.

References _str::len, and _str::s.

Referenced by str_map_str().

static int str_cpy ( str dest,
str src 
) [static]

strcpy for str's (does not allocate the str structure but only the .s member)

Returns:
0 if succeeded, -1 otherwise

Definition at line 311 of file ratelimit.c.

References _str::len, LM_ERR, _str::s, and shm_malloc().

Referenced by mi_set_queue(), and mod_init().

static int str_i_cmp ( const str a,
const str b 
) [inline, static]

Definition at line 768 of file ratelimit.c.

References _str::len, and _str::s.

Referenced by find_queue().

static int str_map_int ( const str_map_t map,
int  key,
str ret 
) [static]

converts a mapped int to a str

Returns:
0 if found, -1 otherwise

Definition at line 296 of file ratelimit.c.

References str_map::id, LM_DBG, _str::s, and str_map::str.

Referenced by mi_get_pipes().

static int str_map_str ( const str_map_t map,
const str key,
int *  ret 
) [static]

converts a mapped str to an int

Returns:
0 if found, -1 otherwise

Definition at line 281 of file ratelimit.c.

References str_map::id, _str::len, LM_DBG, _str::s, str_map::str, and str_cmp().

Referenced by mi_set_pipe(), and parse_pipe_params().

static void update_cpu_load ( void   )  [static]

Definition at line 418 of file ratelimit.c.

References do_update_load(), get_cpuload(), and load_value.

Referenced by rl_timer().

static int w_rl_check_default ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

Definition at line 931 of file ratelimit.c.

References rl_check().

static int w_rl_check_forced ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

static int w_rl_check_forced_pipe ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

Definition at line 917 of file ratelimit.c.

References LM_DBG, and rl_check().

static int w_rl_drop ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

Definition at line 730 of file ratelimit.c.

References rl_drop().

static int w_rl_drop_default ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

Definition at line 758 of file ratelimit.c.

References rl_drop().

static int w_rl_drop_forced ( struct sip_msg msg,
char *  p1,
char *  p2 
) [static]

Definition at line 744 of file ratelimit.c.

References LM_DBG, and rl_drop().


Variable Documentation

Definition at line 92 of file ratelimit.c.

int cfg_setpoint [static]

Definition at line 175 of file ratelimit.c.

Referenced by check_feedback_setpoints(), mi_set_pipe(), and mod_init().

cmd_export_t cmds[] [static]

Definition at line 215 of file ratelimit.c.

int* drop_rate [static]

Definition at line 146 of file ratelimit.c.

Referenced by do_update_load(), mi_stats(), mod_init(), and pipe_push().

module exports

Definition at line 261 of file ratelimit.c.

int hash[100]

Definition at line 804 of file ratelimit.c.

double int_err = 0.0 [static]

Definition at line 372 of file ratelimit.c.

double last_err = 0.0 [static]

Definition at line 373 of file ratelimit.c.

int* load_source [static]

Definition at line 152 of file ratelimit.c.

Referenced by mod_init(), and rl_timer().

int load_source_mp = LOAD_SOURCE_CPU [static]

Definition at line 151 of file ratelimit.c.

Referenced by mod_init().

double* load_value [static]

Definition at line 144 of file ratelimit.c.

Referenced by do_update_load(), mi_push_load(), mod_init(), rl_check(), and update_cpu_load().

mi_export_t mi_cmds[] [static]

Definition at line 247 of file ratelimit.c.

int* network_load_value [static]

Definition at line 148 of file ratelimit.c.

Referenced by mod_init(), rl_check(), and rl_timer().

int* nqueues [static]

Definition at line 169 of file ratelimit.c.

Referenced by mod_init().

int nqueues_mp = 0 [static]

Definition at line 168 of file ratelimit.c.

Referenced by add_queue_params(), and mod_init().

param_export_t params[] [static]

Definition at line 225 of file ratelimit.c.

int params_inited = 0 [static]

Definition at line 195 of file ratelimit.c.

Referenced by init_params(), parse_pipe_params(), and parse_queue_params().

double * pid_kd [static]

Definition at line 145 of file ratelimit.c.

Referenced by mi_get_pid(), mi_set_pid(), and mod_init().

double * pid_ki [static]

Definition at line 145 of file ratelimit.c.

Referenced by mi_get_pid(), mi_set_pid(), and mod_init().

double* pid_kp [static]

Definition at line 145 of file ratelimit.c.

Referenced by mi_get_pid(), mi_set_pid(), and mod_init().

double * pid_setpoint [static]

Definition at line 145 of file ratelimit.c.

Referenced by do_update_load(), mi_set_pipe(), and mod_init().

regex_t pipe_params_regex [static]

Definition at line 196 of file ratelimit.c.

Referenced by init_params(), and parse_pipe_params().

pipe_t pipes[MAX_PIPES] [static]

Definition at line 165 of file ratelimit.c.

str queue_other = str_init("*")

Definition at line 773 of file ratelimit.c.

regex_t queue_params_regex [static]

Definition at line 197 of file ratelimit.c.

Referenced by init_params(), and parse_queue_params().

rl_queue_t queues[MAX_QUEUES] [static]

Definition at line 166 of file ratelimit.c.

str* rl_dbg_str = NULL [static]

Definition at line 171 of file ratelimit.c.

int rl_drop_code = 503 [static]

Definition at line 118 of file ratelimit.c.

Referenced by rl_drop().

str rl_drop_reason = str_init("Server Unavailable") [static]

Definition at line 119 of file ratelimit.c.

gen_lock_t* rl_lock

struct sl_binds slb

SL binds

Definition at line 66 of file ratelimit.c.

Initial value:

 {
   {str_init("cpu"), LOAD_SOURCE_CPU},
   {str_init("external"),  LOAD_SOURCE_EXTERNAL},
   {{0, 0},    0},
}

Definition at line 112 of file ratelimit.c.

int timer_interval = RL_TIMER_INTERVAL [static]

Definition at line 174 of file ratelimit.c.

Referenced by mod_init(), pipe_push(), and rl_timer().


Generated on Thu May 24 08:01:16 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6