1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "resolved-dns-cache.h"
24 /* Never cache more than 1K entries */
25 #define CACHE_MAX 1024
27 /* We never keep any item longer than 10min in our cache */
28 #define CACHE_TTL_MAX_USEC (10 * USEC_PER_MINUTE)
30 static void dns_cache_item_free(DnsCacheItem *i) {
34 dns_resource_record_unref(i->rr);
38 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem*, dns_cache_item_free);
40 static void dns_cache_item_remove_and_free(DnsCache *c, DnsCacheItem *i) {
48 first = hashmap_get(c->rrsets, i->rr->key);
49 LIST_REMOVE(rrsets, first, i);
52 assert_se(hashmap_replace(c->rrsets, first->rr->key, first) >= 0);
54 hashmap_remove(c->rrsets, i->rr->key);
56 prioq_remove(c->expire, i, &i->expire_prioq_idx);
58 dns_cache_item_free(i);
61 void dns_cache_flush(DnsCache *c) {
66 while ((i = hashmap_first(c->rrsets)))
67 dns_cache_item_remove_and_free(c, i);
69 assert(hashmap_size(c->rrsets) == 0);
70 assert(prioq_size(c->expire) == 0);
72 hashmap_free(c->rrsets);
75 prioq_free(c->expire);
79 void dns_cache_remove(DnsCache *c, DnsResourceKey *key) {
85 while ((i = hashmap_get(c->rrsets, key)))
86 dns_cache_item_remove_and_free(c, i);
89 static void dns_cache_make_space(DnsCache *c, unsigned add) {
95 /* Makes space for n new entries. Note that we actually allow
96 * the cache to grow beyond CACHE_MAX, but only when we shall
97 * add more RRs to the cache than CACHE_MAX at once. In that
98 * case the cache will be emptied completely otherwise. */
101 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
104 if (prioq_size(c->expire) <= 0)
107 if (prioq_size(c->expire) + add < CACHE_MAX)
110 i = prioq_peek(c->expire);
113 /* Take an extra reference to the key so that it
114 * doesn't go away in the middle of the remove call */
115 key = dns_resource_key_ref(i->rr->key);
116 dns_cache_remove(c, key);
120 void dns_cache_prune(DnsCache *c) {
125 /* Remove all entries that are past their TTL */
128 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
132 i = prioq_peek(c->expire);
136 ttl = i->rr->ttl * USEC_PER_SEC;
137 if (ttl > CACHE_TTL_MAX_USEC)
138 ttl = CACHE_TTL_MAX_USEC;
141 t = now(CLOCK_MONOTONIC);
143 if (i->timestamp + ttl > t)
146 /* Take an extra reference to the key so that it
147 * doesn't go away in the middle of the remove call */
148 key = dns_resource_key_ref(i->rr->key);
149 dns_cache_remove(c, key);
153 static int dns_cache_item_prioq_compare_func(const void *a, const void *b) {
155 const DnsCacheItem *x = a, *y = b;
157 t = x->timestamp + x->rr->ttl * USEC_PER_SEC;
158 z = y->timestamp + y->rr->ttl * USEC_PER_SEC;
167 static void dns_cache_item_update(DnsCache *c, DnsCacheItem *i, DnsResourceRecord *rr, usec_t timestamp) {
172 if (!i->rrsets_prev) {
173 /* We are the first item in the list, we need to
174 * update the key used in the hashmap */
176 assert_se(hashmap_replace(c->rrsets, rr->key, i) >= 0);
179 dns_resource_record_ref(rr);
180 dns_resource_record_unref(i->rr);
183 i->timestamp = timestamp;
184 prioq_reshuffle(c->expire, i, &i->expire_prioq_idx);
187 static DnsCacheItem* dns_cache_get(DnsCache *c, DnsResourceRecord *rr) {
193 LIST_FOREACH(rrsets, i, hashmap_get(c->rrsets, rr->key))
194 if (dns_resource_record_equal(i->rr, rr))
200 int dns_cache_put(DnsCache *c, DnsResourceRecord *rr, usec_t timestamp) {
201 _cleanup_(dns_cache_item_freep) DnsCacheItem *i = NULL;
202 DnsCacheItem *first = NULL, *existing;
208 /* New TTL is 0? Delete the entry... */
210 dns_cache_remove(c, rr->key);
214 /* Entry exists already? Update TTL and timestamp */
215 existing = dns_cache_get(c, rr);
217 dns_cache_item_update(c, existing, rr, timestamp);
221 /* Otherwise, add the new RR */
222 r = prioq_ensure_allocated(&c->expire, dns_cache_item_prioq_compare_func);
226 r = hashmap_ensure_allocated(&c->rrsets, dns_resource_key_hash_func, dns_resource_key_compare_func);
230 dns_cache_make_space(c, 1);
232 i = new0(DnsCacheItem, 1);
236 i->rr = dns_resource_record_ref(rr);
237 i->timestamp = timestamp;
238 i->expire_prioq_idx = PRIOQ_IDX_NULL;
240 r = prioq_put(c->expire, i, &i->expire_prioq_idx);
244 first = hashmap_get(c->rrsets, i->rr->key);
246 LIST_PREPEND(rrsets, first, i);
247 assert_se(hashmap_replace(c->rrsets, first->rr->key, first) >= 0);
249 r = hashmap_put(c->rrsets, i->rr->key, i);
251 prioq_remove(c->expire, i, &i->expire_prioq_idx);
261 int dns_cache_put_answer(DnsCache *c, DnsAnswer *answer, usec_t timestamp) {
262 unsigned i, added = 0;
268 /* First iteration, delete all matching old RRs, so that we
269 * only keep complete rrsets in place. */
270 for (i = 0; i < answer->n_rrs; i++)
271 dns_cache_remove(c, answer->rrs[i]->key);
273 dns_cache_make_space(c, answer->n_rrs);
275 /* Second iteration, add in new RRs */
276 for (added = 0; added < answer->n_rrs; added++) {
278 timestamp = now(CLOCK_MONOTONIC);
280 r = dns_cache_put(c, answer->rrs[added], timestamp);
288 /* Adding all RRs failed. Let's clean up what we already
289 * added, just in case */
291 for (i = 0; i < added; i++)
292 dns_cache_remove(c, answer->rrs[i]->key);
297 int dns_cache_lookup(DnsCache *c, DnsQuestion *q, DnsAnswer **ret) {
298 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
306 if (q->n_keys <= 0) {
311 for (i = 0; i < q->n_keys; i++) {
314 j = hashmap_get(c->rrsets, q->keys[i]);
316 /* If one question cannot be answered we need to refresh */
321 LIST_FOREACH(rrsets, j, j)
327 answer = dns_answer_new(n);
331 for (i = 0; i < q->n_keys; i++) {
334 j = hashmap_get(c->rrsets, q->keys[i]);
335 LIST_FOREACH(rrsets, j, j) {
336 r = dns_answer_add(answer, j->rr);
342 assert(n >= answer->n_rrs);