conf: support disable expired cache on specific domain.

This commit is contained in:
Nick Peng
2022-12-18 22:33:01 +08:00
committed by Nick Peng
parent 4b42e1ef85
commit 70df7938f3
13 changed files with 87 additions and 21 deletions

View File

@@ -245,7 +245,7 @@ struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len
return (struct dns_cache_data *)cache_packet;
}
static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, int inactive,
static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, int no_inactive, int inactive,
struct dns_cache_data *cache_data)
{
struct dns_cache *dns_cache = NULL;
@@ -258,7 +258,7 @@ static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int spee
/* lookup existing cache */
dns_cache = dns_cache_lookup(cache_key);
if (dns_cache == NULL) {
return dns_cache_insert(cache_key, ttl, speed, cache_data);
return dns_cache_insert(cache_key, ttl, speed, no_inactive, cache_data);
}
if (ttl < DNS_CACHE_TTL_MIN) {
@@ -273,6 +273,7 @@ static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int spee
dns_cache->info.query_flag = cache_key->query_flag;
dns_cache->info.ttl = ttl;
dns_cache->info.speed = speed;
dns_cache->info.no_inactive = no_inactive;
old_cache_data = dns_cache->cache_data;
dns_cache->cache_data = cache_data;
list_del_init(&dns_cache->list);
@@ -293,14 +294,14 @@ static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int spee
return 0;
}
int dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, struct dns_cache_data *cache_data)
int dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data)
{
return _dns_cache_replace(cache_key, ttl, speed, 0, cache_data);
return _dns_cache_replace(cache_key, ttl, speed, no_inactive, 0, cache_data);
}
int dns_cache_replace_inactive(struct dns_cache_key *cache_key, int ttl, int speed, struct dns_cache_data *cache_data)
int dns_cache_replace_inactive(struct dns_cache_key *cache_key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data)
{
return _dns_cache_replace(cache_key, ttl, speed, 1, cache_data);
return _dns_cache_replace(cache_key, ttl, speed, no_inactive, 1, cache_data);
}
static void _dns_cache_remove_by_domain(struct dns_cache_key *cache_key)
@@ -390,7 +391,7 @@ errout:
return -1;
}
int dns_cache_insert(struct dns_cache_key *cache_key, int ttl, int speed, struct dns_cache_data *cache_data)
int dns_cache_insert(struct dns_cache_key *cache_key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data)
{
struct dns_cache_info info;
@@ -415,6 +416,7 @@ int dns_cache_insert(struct dns_cache_key *cache_key, int ttl, int speed, struct
info.ttl = ttl;
info.hitnum_update_add = DNS_CACHE_HITNUM_STEP;
info.speed = speed;
info.no_inactive = no_inactive;
time(&info.insert_time);
time(&info.replace_time);
@@ -663,7 +665,7 @@ void dns_cache_invalidate(dns_cache_callback precallback, int ttl_pre, unsigned
}
if (ttl < 0) {
if (dns_cache_head.enable_inactive) {
if (dns_cache_head.enable_inactive && dns_cache->info.no_inactive == 0) {
_dns_cache_move_inactive(dns_cache);
} else {
_dns_cache_remove(dns_cache);

View File

@@ -87,6 +87,7 @@ struct dns_cache_info {
int ttl;
int hitnum;
int speed;
int no_inactive;
int hitnum_update_add;
time_t insert_time;
time_t replace_time;
@@ -135,11 +136,11 @@ struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len
int dns_cache_init(int size, int enable_inactive, int inactive_list_expired);
int dns_cache_replace(struct dns_cache_key *key, int ttl, int speed, struct dns_cache_data *cache_data);
int dns_cache_replace(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
int dns_cache_replace_inactive(struct dns_cache_key *key, int ttl, int speed, struct dns_cache_data *cache_data);
int dns_cache_replace_inactive(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
int dns_cache_insert(struct dns_cache_key *key, int ttl, int speed, struct dns_cache_data *cache_data);
int dns_cache_insert(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
struct dns_cache *dns_cache_lookup(struct dns_cache_key *key);

View File

@@ -1842,6 +1842,11 @@ errout:
return -1;
}
static int _conf_domain_rule_no_serve_expired(const char *domain)
{
return _config_domain_rule_flag_set(domain, DOMAIN_FLAG_NO_SERVE_EXPIRED, 0);
}
static int _conf_domain_rules(void *data, int argc, char *argv[])
{
int opt = 0;
@@ -1856,6 +1861,7 @@ static int _conf_domain_rules(void *data, int argc, char *argv[])
{"nftset", required_argument, NULL, 't'},
{"nameserver", required_argument, NULL, 'n'},
{"dualstack-ip-selection", required_argument, NULL, 'd'},
{"no-serve-expired", no_argument, NULL, 254},
{NULL, no_argument, NULL, 0}
};
/* clang-format on */
@@ -1952,6 +1958,14 @@ static int _conf_domain_rules(void *data, int argc, char *argv[])
break;
}
case 254: {
if (_conf_domain_rule_no_serve_expired(domain) != 0) {
tlog(TLOG_ERROR, "set no-serve-expired rule failed.");
goto errout;
}
break;
}
default:
break;
}

View File

@@ -97,6 +97,7 @@ typedef enum {
#define DOMAIN_FLAG_NFTSET_INET_IGN (1 << 12)
#define DOMAIN_FLAG_NFTSET_IP_IGN (1 << 13)
#define DOMAIN_FLAG_NFTSET_IP6_IGN (1 << 14)
#define DOMAIN_FLAG_NO_SERVE_EXPIRED (1 << 15)
#define SERVER_FLAG_EXCLUDE_DEFAULT (1 << 0)

View File

@@ -236,6 +236,7 @@ struct dns_request {
int dualstack_selection_ping_time;
int dualstack_selection_has_ip;
struct dns_request *dualstack_request;
int no_serve_expired;
pthread_mutex_t ip_map_lock;
@@ -1070,17 +1071,17 @@ static int _dns_server_request_update_cache(struct dns_request *request, dns_typ
if (request->prefetch) {
if (request->prefetch_expired_domain == 0) {
if (dns_cache_replace(&cache_key, ttl, speed, cache_data) != 0) {
if (dns_cache_replace(&cache_key, ttl, speed, request->no_serve_expired, cache_data) != 0) {
goto errout;
}
} else {
if (dns_cache_replace_inactive(&cache_key, ttl, speed, cache_data) != 0) {
if (dns_cache_replace_inactive(&cache_key, ttl, speed, request->no_serve_expired, cache_data) != 0) {
goto errout;
}
}
} else {
/* insert result to cache */
if (dns_cache_insert(&cache_key, ttl, speed, cache_data) != 0) {
if (dns_cache_insert(&cache_key, ttl, speed, request->no_serve_expired, cache_data) != 0) {
goto errout;
}
}
@@ -1218,17 +1219,17 @@ static int _dns_cache_cname_packet(struct dns_server_post_context *context)
if (request->prefetch) {
if (request->prefetch_expired_domain == 0) {
if (dns_cache_replace(&cache_key, ttl, speed, cache_packet) != 0) {
if (dns_cache_replace(&cache_key, ttl, speed, request->no_serve_expired, cache_packet) != 0) {
goto errout;
}
} else {
if (dns_cache_replace_inactive(&cache_key, ttl, speed, cache_packet) != 0) {
if (dns_cache_replace_inactive(&cache_key, ttl, speed, request->no_serve_expired, cache_packet) != 0) {
goto errout;
}
}
} else {
/* insert result to cache */
if (dns_cache_insert(&cache_key, ttl, speed, cache_packet) != 0) {
if (dns_cache_insert(&cache_key, ttl, speed, request->no_serve_expired, cache_packet) != 0) {
goto errout;
}
}
@@ -1260,12 +1261,12 @@ static int _dns_cache_packet(struct dns_server_post_context *context)
cache_key.query_flag = request->server_flags;
if (request->prefetch) {
if (dns_cache_replace(&cache_key, context->reply_ttl, -1, cache_packet) != 0) {
if (dns_cache_replace(&cache_key, context->reply_ttl, -1, request->no_serve_expired, cache_packet) != 0) {
goto errout;
}
} else {
/* insert result to cache */
if (dns_cache_insert(&cache_key, context->reply_ttl, -1, cache_packet) != 0) {
if (dns_cache_insert(&cache_key, context->reply_ttl, -1, request->no_serve_expired, cache_packet) != 0) {
goto errout;
}
}
@@ -3603,6 +3604,10 @@ static int _dns_server_pre_process_rule_flags(struct dns_request *request)
}
flags = rule_flag->flags;
if (flags & DOMAIN_FLAG_NO_SERVE_EXPIRED) {
request->no_serve_expired = 1;
}
if (flags & DOMAIN_FLAG_ADDR_IGN) {
/* ignore this domain */
goto out;
@@ -3937,6 +3942,10 @@ reply_cache:
goto out;
}
if (dns_cache_get_ttl(dns_cache) <= 0 && request->no_serve_expired == 1) {
goto out;
}
ret = _dns_server_process_cache_data(request, dns_cache);
if (ret != 0) {
goto out;