Skip to content

Commit

Permalink
- use_dns_cache can be enabled/disabled runtime
Browse files Browse the repository at this point in the history
- dns_cache_init is introduced: if set to 0, the dns cache is not
initialized, and cannot be enabled later (default is 1)
- dst_blacklist_init is introduced: if set to 0, the dst blacklist is
not initialized (similar to dns_cache_init) (default is 1)
  • Loading branch information
Miklos Tirpak committed Feb 11, 2008
1 parent 4731698 commit 7905e2d
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 25 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ new config variables:
tcp_source_ipv6 = IPv6 address
Set the given source IP for all outbound TCP connections.
If setting the IP fails the TCP connection will use the default.
dns_cache_init = on | off (default on) - if off, the dns cache is not
initialized at startup and cannot be enabled runtime, that saves some
memory.
dst_blacklist_init = on | off (default on) - if off, the blacklist
is not initialized at startup and cannot be enabled runtime,
that saves some memory.



2.0.0 changes
Expand Down
6 changes: 6 additions & 0 deletions cfg.lex
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ DNS_SERVERS_NO dns_servers_no
DNS_USE_SEARCH dns_use_search_list
DNS_SEARCH_FMATCH dns_search_full_match
/* dns cache */
DNS_CACHE_INIT dns_cache_init
DNS_USE_CACHE use_dns_cache
DNS_USE_FAILOVER use_dns_failover
DNS_CACHE_FLAGS dns_cache_flags
Expand All @@ -260,6 +261,7 @@ DNS_CACHE_MEM dns_cache_mem
DNS_CACHE_GC_INT dns_cache_gc_interval
DNS_CACHE_DEL_NONEXP dns_cache_del_nonexp|dns_cache_delete_nonexpired
/* blacklist */
DST_BLST_INIT dst_blacklist_init
USE_DST_BLST use_dst_blacklist
DST_BLST_MEM dst_blacklist_mem
DST_BLST_TTL dst_blacklist_expire|dst_blacklist_ttl
Expand Down Expand Up @@ -506,6 +508,8 @@ EAT_ABLE [\ \t\b\r]
return DNS_USE_SEARCH; }
<INITIAL>{DNS_SEARCH_FMATCH} { count(); yylval.strval=yytext;
return DNS_SEARCH_FMATCH; }
<INITIAL>{DNS_CACHE_INIT} { count(); yylval.strval=yytext;
return DNS_CACHE_INIT; }
<INITIAL>{DNS_USE_CACHE} { count(); yylval.strval=yytext;
return DNS_USE_CACHE; }
<INITIAL>{DNS_USE_FAILOVER} { count(); yylval.strval=yytext;
Expand All @@ -524,6 +528,8 @@ EAT_ABLE [\ \t\b\r]
return DNS_CACHE_GC_INT; }
<INITIAL>{DNS_CACHE_DEL_NONEXP} { count(); yylval.strval=yytext;
return DNS_CACHE_DEL_NONEXP; }
<INITIAL>{DST_BLST_INIT} { count(); yylval.strval=yytext;
return DST_BLST_INIT; }
<INITIAL>{USE_DST_BLST} { count(); yylval.strval=yytext;
return USE_DST_BLST; }
<INITIAL>{DST_BLST_MEM} { count(); yylval.strval=yytext;
Expand Down
8 changes: 7 additions & 1 deletion cfg.y
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ static struct socket_id* mk_listen_id(char*, int, int);
%token DNS_SERVERS_NO
%token DNS_USE_SEARCH
%token DNS_SEARCH_FMATCH
%token DNS_CACHE_INIT
%token DNS_USE_CACHE
%token DNS_USE_FAILOVER
%token DNS_CACHE_FLAGS
Expand All @@ -302,6 +303,7 @@ static struct socket_id* mk_listen_id(char*, int, int);
%token DNS_CACHE_GC_INT
%token DNS_CACHE_DEL_NONEXP
/*blacklist*/
%token DST_BLST_INIT
%token USE_DST_BLST
%token DST_BLST_MEM
%token DST_BLST_TTL
Expand Down Expand Up @@ -630,7 +632,9 @@ assign_stm:
| DNS_USE_SEARCH error { yyerror("boolean value expected"); }
| DNS_SEARCH_FMATCH EQUAL NUMBER { default_core_cfg.dns_search_fmatch=$3; }
| DNS_SEARCH_FMATCH error { yyerror("boolean value expected"); }
| DNS_USE_CACHE EQUAL NUMBER { IF_DNS_CACHE(use_dns_cache=$3); }
| DNS_CACHE_INIT EQUAL NUMBER { IF_DNS_CACHE(dns_cache_init=$3); }
| DNS_CACHE_INIT error { yyerror("boolean value expected"); }
| DNS_USE_CACHE EQUAL NUMBER { IF_DNS_CACHE(default_core_cfg.use_dns_cache=$3); }
| DNS_USE_CACHE error { yyerror("boolean value expected"); }
| DNS_USE_FAILOVER EQUAL NUMBER { IF_DNS_FAILOVER(default_core_cfg.use_dns_failover=$3);}
| DNS_USE_FAILOVER error { yyerror("boolean value expected"); }
Expand All @@ -648,6 +652,8 @@ assign_stm:
| DNS_CACHE_GC_INT error { yyerror("boolean value expected"); }
| DNS_CACHE_DEL_NONEXP EQUAL NUMBER { IF_DNS_CACHE(default_core_cfg.dns_cache_del_nonexp=$3); }
| DNS_CACHE_DEL_NONEXP error { yyerror("boolean value expected"); }
| DST_BLST_INIT EQUAL NUMBER { IF_DST_BLACKLIST(dst_blacklist_init=$3); }
| DST_BLST_INIT error { yyerror("boolean value expected"); }
| USE_DST_BLST EQUAL NUMBER { IF_DST_BLACKLIST(default_core_cfg.use_dst_blacklist=$3); }
| USE_DST_BLST error { yyerror("boolean value expected"); }
| DST_BLST_MEM EQUAL NUMBER { IF_DST_BLACKLIST(default_core_cfg.blst_max_mem=$3); }
Expand Down
5 changes: 4 additions & 1 deletion cfg_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct cfg_group_core default_core_cfg = {
0, /* dns_reinit */
/* DNS cache */
#ifdef USE_DNS_CACHE
1, /* use_dns_cache -- on by default */
0, /* dns_cache_flags */
0, /* use_dns_failover -- off by default */
0, /* dns_srv_lb -- off by default */
Expand All @@ -84,7 +85,7 @@ cfg_def_t core_cfg_def[] = {
{"debug", CFG_VAR_INT, 0, 0, 0, 0, "debug level"},
#ifdef USE_DST_BLACKLIST
/* blacklist */
{"use_dst_blacklist", CFG_VAR_INT, 0, 0, 0, 0,
{"use_dst_blacklist", CFG_VAR_INT, 0, 1, use_dst_blacklist_fixup, 0,
"enable/disable destination blacklisting"},
{"dst_blacklist_expire", CFG_VAR_INT, 0, 0, 0, 0,
"how much time (in s) a blacklisted destination is kept in the list"},
Expand Down Expand Up @@ -126,6 +127,8 @@ cfg_def_t core_cfg_def[] = {
"set to 1 in order to reinitialize the DNS resolver"},
/* DNS cache */
#ifdef USE_DNS_CACHE
{"use_dns_cache", CFG_VAR_INT, 0, 1, use_dns_cache_fixup, 0,
"enable/disable the dns cache"},
{"dns_cache_flags", CFG_VAR_INT, 0, 4, 0, fix_dns_flags,
"dns cache specific resolver flags "
"(1=ipv4 only, 2=ipv6 only, 4=prefer ipv6"},
Expand Down
1 change: 1 addition & 0 deletions cfg_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct cfg_group_core {
int dns_reinit;
/* DNS cache */
#ifdef USE_DNS_CACHE
int use_dns_cache;
int dns_cache_flags;
int use_dns_failover;
int dns_srv_lb;
Expand Down
66 changes: 50 additions & 16 deletions dns_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* 2007-08-17 dns_cache_del_nonexp config option is introduced (Miklos)
* 2008-02-04 DNS cache options are adapted for the configuration
* framework (Miklos)
* 2008-02-11 dns_cache_init cfg parameter is introduced (Miklos)
*/

#ifdef USE_DNS_CACHE
Expand Down Expand Up @@ -87,7 +88,7 @@
#define MAX_CNAME_CHAIN 10
#define SPACE_FORMAT " " /* format of view output */


int dns_cache_init=1; /* if 0, the DNS cache is not initialized at startup */
static gen_lock_t* dns_hash_lock=0;
static volatile unsigned int *dns_cache_mem_used=0; /* current mem. use */
unsigned int dns_timer_interval=DEFAULT_DNS_TIMER_INTERVAL; /* in s */
Expand Down Expand Up @@ -299,7 +300,7 @@ void fix_dns_flags(str *name)
*/
int use_dns_failover_fixup(void *handle, str *name, void **val)
{
if ((int)(long)(*val) && !use_dns_cache) {
if ((int)(long)(*val) && !cfg_get(core, handle, use_dns_cache)) {
LOG(L_ERR, "ERROR: use_dns_failover_fixup(): "
"DNS cache is turned off, failover cannot be enabled. "
"(set use_dns_cache to 1)\n");
Expand All @@ -308,6 +309,26 @@ int use_dns_failover_fixup(void *handle, str *name, void **val)
return 0;
}

/* fixup function for use_dns_cache
* verifies that dns_cache_init is set to 1
*/
int use_dns_cache_fixup(void *handle, str *name, void **val)
{
if ((int)(long)(*val) && !dns_cache_init) {
LOG(L_ERR, "ERROR: use_dns_cache_fixup(): "
"DNS cache is turned off by dns_cache_init=0, "
"it cannot be enabled runtime.\n");
return -1;
}
if (((int)(long)(*val)==0) && cfg_get(core, handle, use_dns_failover)) {
LOG(L_ERR, "ERROR: use_dns_failover_fixup(): "
"DNS failover depends on use_dns_cache, set use_dns_failover "
"to 0 before disabling the DNS cache\n");
return -1;
}
return 0;
}

/* KByte to Byte conversion */
int dns_cache_max_mem_fixup(void *handle, str *name, void **val)
{
Expand All @@ -323,6 +344,13 @@ int init_dns_cache()
int r;
int ret;

if (dns_cache_init==0) {
/* the DNS cache is turned off */
default_core_cfg.use_dns_cache=0;
default_core_cfg.use_dns_failover=0;
return 0;
}

ret=0;
/* sanity check */
if (E_DNS_CRITICAL>=sizeof(dns_str_errors)/sizeof(char*)){
Expand Down Expand Up @@ -374,6 +402,8 @@ int init_dns_cache()

/* fix options */
default_core_cfg.dns_cache_max_mem<<=10; /* Kb */ /* TODO: test with 0 */
if (default_core_cfg.use_dns_cache==0)
default_core_cfg.use_dns_failover=0; /* cannot work w/o dns_cache support */
/* fix flags */
fix_dns_flags(NULL);

Expand All @@ -399,7 +429,11 @@ int init_dns_cache()
}

#ifdef USE_DNS_CACHE_STATS
int init_dns_cache_stats(int iproc_num) {
int init_dns_cache_stats(int iproc_num)
{
/* do not initialize the stats array if the DNS cache will not be used */
if (dns_cache_init==0) return 0;

/* if it is already initialized */
if (dns_cache_stats)
shm_free(dns_cache_stats);
Expand Down Expand Up @@ -2225,7 +2259,7 @@ struct hostent* dns_resolvehost(char* name)
{
str host;

if ((use_dns_cache==0) || (dns_hash==0)){ /* not init yet */
if ((cfg_get(core, core_cfg, use_dns_cache)==0) || (dns_hash==0)){ /* not init yet */
return _resolvehost(name);
}
host.s=name;
Expand All @@ -2250,7 +2284,7 @@ struct hostent* dns_sip_resolvehost(str* name, unsigned short* port,
struct ip_addr ip;
int ret;

if ((use_dns_cache==0) || (dns_hash==0)){
if ((cfg_get(core, core_cfg, use_dns_cache==0)) || (dns_hash==0)){
/* not init or off => use normal, non-cached version */
return _sip_resolvehost(name, port, proto);
}
Expand Down Expand Up @@ -2282,7 +2316,7 @@ struct hostent* dns_srv_sip_resolvehost(str* name, unsigned short* port,
str srv_name;
char srv_proto;

if ((use_dns_cache==0) || (dns_hash==0)){
if ((cfg_get(core, core_cfg, use_dns_cache)==0) || (dns_hash==0)){
/* not init or off => use normal, non-cached version */
return _sip_resolvehost(name, port, proto);
}
Expand Down Expand Up @@ -3172,7 +3206,7 @@ int dns_get_server_state(void)
/* rpc functions */
void dns_cache_mem_info(rpc_t* rpc, void* ctx)
{
if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand All @@ -3186,7 +3220,7 @@ void dns_cache_debug(rpc_t* rpc, void* ctx)
struct dns_hash_entry* e;
ticks_t now;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3255,7 +3289,7 @@ void dns_cache_stats_get(rpc_t* rpc, void* c)
};


if (!use_dns_cache) {
if (!cfg_get(core, core_cfg, use_dns_cache)) {
rpc->fault(c, 500, "dns cache support disabled");
return;
}
Expand Down Expand Up @@ -3300,7 +3334,7 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
int i;
ticks_t now;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3380,7 +3414,7 @@ void dns_cache_view(rpc_t* rpc, void* ctx)
ticks_t now;
str s;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3488,7 +3522,7 @@ void dns_cache_flush(void)
/* deletes all the entries from the cache */
void dns_cache_delete_all(rpc_t* rpc, void* ctx)
{
if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3637,7 +3671,7 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
ip_addr = 0;
size = 0;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3844,7 +3878,7 @@ static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
str name;
int err, h, found=0;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down Expand Up @@ -3911,7 +3945,7 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
{
int state;

if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand All @@ -3923,7 +3957,7 @@ void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
/* prints the DNS server state */
void dns_get_server_state_rpc(rpc_t* rpc, void* ctx)
{
if (!use_dns_cache){
if (!cfg_get(core, core_cfg, use_dns_cache)){
rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
return;
}
Expand Down
1 change: 1 addition & 0 deletions dns_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const char* dns_strerror(int err);

void fix_dns_flags(str *name);
int use_dns_failover_fixup(void *handle, str *name, void **val);
int use_dns_cache_fixup(void *handle, str *name, void **val);
int dns_cache_max_mem_fixup(void *handle, str *name, void **val);
int init_dns_cache();
#ifdef USE_DNS_CACHE_STATS
Expand Down
3 changes: 3 additions & 0 deletions doc/dns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ DNS Cache and Failover Config Variables
ones. The last-recently used entries are deleted first.
Default: no

dns_cache_init = on | off - if off, the dns cache is not initialized
at startup and cannot be enabled runtime, that saves some memory.
Default: on

DNS Cache Compile Options

Expand Down
3 changes: 3 additions & 0 deletions doc/dst_blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Config Variables
dst_blacklist_gc_interval = time in s (default 60 s) - how often the
garbage collection will run (eliminating old, expired entries).

dst_blacklist_init = on | off (default on) - if off, the blacklist
is not initialized at startup and cannot be enabled runtime,
that saves some memory.

Compile Options

Expand Down
Loading

0 comments on commit 7905e2d

Please sign in to comment.