pl_ht.h

00001 /*
00002  * $Id$
00003  *
00004  * pipelimit module
00005  *
00006  * Copyright (C) 2006 Hendrik Scholz <hscholz@raisdorf.net>
00007  * Copyright (C) 2008 Ovidiu Sas <osas@voipembedded.com>
00008  * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
00009  *
00010  * This file is part of Kamailio, a free SIP server.
00011  *
00012  * Kamailio is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version
00016  *
00017  * Kamailio is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License 
00023  * along with this program; if not, write to the Free Software 
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  */
00026 
00027 #ifndef _RL_HT_H_
00028 #define _RL_HT_H_
00029 
00030 #include "../../str.h"
00031 
00032 typedef struct _pl_pipe
00033 {
00034     unsigned int cellid;
00035         str name;
00036 
00037         /* stuff that gets read as a modparam or set via fifo */
00038         int algo;
00039         int limit;
00040 
00041         /* updated values */
00042         int counter;
00043         int last_counter;
00044         int load;
00045 
00046     struct _pl_pipe *prev;
00047     struct _pl_pipe *next;
00048 } pl_pipe_t;
00049 
00050 typedef struct _rlp_slot
00051 {
00052         unsigned int ssize;
00053         pl_pipe_t *first;
00054         gen_lock_t lock;        
00055 } rlp_slot_t;
00056 
00057 typedef struct _rlp_htable
00058 {
00059         unsigned int htsize;
00060         rlp_slot_t *slots;
00061 } rlp_htable_t;
00062 
00063 int pl_init_htable(unsigned int hsize);
00064 int pl_destroy_htable(void);
00065 void pl_pipe_release(str *pipeid);
00066 pl_pipe_t* pl_pipe_get(str *pipeid, int mode);
00067 int pl_pipe_add(str *pipeid, str *algorithm, int limit);
00068 int pl_print_pipes(void);
00069 int pl_pipe_check_feedback_setpoints(int *cfgsp);
00070 void pl_pipe_timer_update(int interval, int netload);
00071 
00072 void rpl_pipe_lock(int slot);
00073 void rpl_pipe_release(int slot);
00074 
00075 /* PIPE_ALGO_FEEDBACK holds cpu usage to a fixed value using 
00076  * negative feedback according to the PID controller model
00077  *
00078  * <http://en.wikipedia.org/wiki/PID_controller>
00079  */
00080 enum {
00081         PIPE_ALGO_NOP = 0,
00082         PIPE_ALGO_RED,
00083         PIPE_ALGO_TAILDROP,
00084         PIPE_ALGO_FEEDBACK,
00085         PIPE_ALGO_NETWORK
00086 };
00087 
00088 typedef struct str_map {
00089         str     str;
00090         int     id;
00091 } str_map_t;
00092 
00093 extern str_map_t algo_names[];
00094 
00095 static inline int str_cmp(const str * a , const str * b)
00096 {
00097         return ! (a->len == b->len && ! strncmp(a->s, b->s, a->len));
00098 }
00099 
00100 static inline int str_i_cmp(const str * a, const str * b)
00101 {
00102         return ! (a->len == b->len && ! strncasecmp(a->s, b->s, a->len));
00103 }
00104 
00109 static inline int str_map_str(const str_map_t * map, const str * key, int * ret)
00110 {
00111         for (; map->str.s; map++) 
00112                 if (! str_cmp(&map->str, key)) {
00113                         *ret = map->id;
00114                         return 0;
00115                 }
00116         LM_DBG("str_map_str() failed map=%p key=%.*s\n", map, key->len, key->s);
00117         return -1;
00118 }
00119 
00124 static inline int str_map_int(const str_map_t * map, int key, str * ret)
00125 {
00126         for (; map->str.s; map++) 
00127                 if (map->id == key) {
00128                         *ret = map->str;
00129                         return 0;
00130                 }
00131         LM_DBG("str_map_str() failed map=%p key=%d\n", map, key);
00132         return -1;
00133 }
00134 
00139 static inline int str_cpy(str * dest, str * src)
00140 {
00141         dest->len = src->len;
00142         dest->s = shm_malloc(src->len);
00143         if (! dest->s) {
00144                 LM_ERR("oom: '%.*s'\n", src->len, src->s);
00145                 return -1;
00146         }
00147         memcpy(dest->s, src->s, src->len);
00148         return 0;
00149 }
00150 
00151 
00152 #endif