rdns-server 1.17.20

A high-performance, security-focused DNS server written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use super::entry::{CacheEntry, CacheKey};
use crate::fasthash::{fx_hash, FxBuildHasher};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

/// Number of cache shards. Power of 2 for fast modulo.
const NUM_SHARDS: usize = 256;

/// High-performance sharded DNS cache using parking_lot RwLock.
/// Optimized for read-heavy workloads (cache hits are ~99% of queries).
#[derive(Clone)]
pub struct FastCacheStore {
    inner: Arc<FastCacheInner>,
}

struct FastCacheInner {
    shards: Vec<RwLock<HashMap<CacheKey, Arc<CacheEntry>, FxBuildHasher>>>,
    max_entries: usize,
    min_ttl: u32,
    max_ttl: u32,
    negative_ttl: u32,
    /// Seconds past expiry that expired entries remain eligible for
    /// serve-stale (RFC 8767). Zero ⇒ feature disabled; entries evicted
    /// at expiry like a non-serve-stale cache.
    stale_window_secs: AtomicU32,
    hits: AtomicU64,
    misses: AtomicU64,
    insertions: AtomicU64,
    evictions: AtomicU64,
}

impl FastCacheStore {
    pub fn new(max_entries: usize, min_ttl: u32, max_ttl: u32, negative_ttl: u32) -> Self {
        let per_shard = max_entries / NUM_SHARDS + 1;
        let shards = (0..NUM_SHARDS)
            .map(|_| {
                RwLock::new(HashMap::with_capacity_and_hasher(
                    per_shard / 4,
                    FxBuildHasher::default(),
                ))
            })
            .collect();

        Self {
            inner: Arc::new(FastCacheInner {
                shards,
                max_entries,
                min_ttl,
                max_ttl,
                negative_ttl,
                stale_window_secs: AtomicU32::new(0),
                hits: AtomicU64::new(0),
                misses: AtomicU64::new(0),
                insertions: AtomicU64::new(0),
                evictions: AtomicU64::new(0),
            }),
        }
    }

    /// Configure serve-stale (RFC 8767) grace window. Expired entries
    /// remain in the cache and are returned by [`lookup_stale`] for up to
    /// this many seconds past their original TTL. `0` disables the
    /// feature; eviction then runs exactly at expiry.
    pub fn set_stale_window(&self, secs: u32) {
        self.inner.stale_window_secs.store(secs, Ordering::Relaxed);
    }

    #[inline]
    fn stale_window(&self) -> u32 {
        self.inner.stale_window_secs.load(Ordering::Relaxed)
    }

    #[inline]
    fn shard_idx(key: &CacheKey) -> usize {
        // FxHash (not SipHash) — the key space is bounded by the cache size and
        // eviction, so DoS-resistance is unnecessary here and SipHash dominated
        // the cached hot path. The shard HashMap uses the same fast hasher.
        fx_hash(key) as usize & (NUM_SHARDS - 1)
    }

    /// Look up a fresh cache entry. Uses read lock for maximum concurrency.
    /// Returns `None` if not found or expired. Expired entries are not
    /// returned here even if they're still inside the serve-stale window —
    /// serve-stale is deliberately a resolver-level fallback, never a
    /// fast-path shortcut that skips a fresh resolution attempt.
    ///
    /// When serve-stale is disabled (`stale_window_secs == 0`) an expired
    /// entry found here is opportunistically removed to keep the cache
    /// clean. When enabled, we leave the entry in place so `lookup_stale`
    /// can find it after the fresh attempt fails.
    pub fn lookup(&self, key: &CacheKey) -> Option<Arc<CacheEntry>> {
        let idx = Self::shard_idx(key);
        let stale_window = self.stale_window();

        // Fast path: read lock only
        {
            let shard = self.inner.shards[idx].read();
            if let Some(entry) = shard.get(key) {
                if !entry.is_expired() {
                    self.inner.hits.fetch_add(1, Ordering::Relaxed);
                    return Some(entry.clone());
                }
                // Expired. If stale is enabled, keep the entry in place for
                // lookup_stale; otherwise fall through to opportunistic GC.
                if stale_window > 0 {
                    self.inner.misses.fetch_add(1, Ordering::Relaxed);
                    return None;
                }
            } else {
                self.inner.misses.fetch_add(1, Ordering::Relaxed);
                return None;
            }
        }

        // Expired and serve-stale is off — remove.
        {
            let mut shard = self.inner.shards[idx].write();
            shard.remove(key);
        }
        self.inner.misses.fetch_add(1, Ordering::Relaxed);
        None
    }

    /// Serve-stale lookup (RFC 8767). Returns an entry that is expired but
    /// still within the configured `stale_window_secs`. The resolver calls
    /// this only after fresh resolution fails, so a successful return here
    /// means "we tried upstream and it didn't answer; here's the last
    /// answer we cached."
    ///
    /// Returns `None` when serve-stale is disabled, when the entry is
    /// absent, when the entry is fresh (caller should have used `lookup`),
    /// or when the entry is past the grace window.
    pub fn lookup_stale(&self, key: &CacheKey) -> Option<Arc<CacheEntry>> {
        let stale_window = self.stale_window();
        if stale_window == 0 {
            return None;
        }
        let idx = Self::shard_idx(key);
        let shard = self.inner.shards[idx].read();
        let entry = shard.get(key)?;
        if entry.is_stale_usable(stale_window) {
            Some(entry.clone())
        } else {
            None
        }
    }

    /// Insert a cache entry with TTL clamping and size enforcement.
    pub fn insert(&self, key: CacheKey, mut entry: CacheEntry) {
        let ttl = if entry.negative {
            entry.original_ttl.min(self.inner.negative_ttl)
        } else {
            entry
                .original_ttl
                .max(self.inner.min_ttl)
                .min(self.inner.max_ttl)
        };
        entry.original_ttl = ttl;

        let idx = Self::shard_idx(&key);
        let max_per_shard = self.inner.max_entries / NUM_SHARDS + 1;
        {
            let mut shard = self.inner.shards[idx].write();

            // Enforce max size: drop entries past the stale-serving
            // window first, then oldest-by-insertion if still over.
            // Under memory pressure we favor keeping recently-inserted
            // entries over serve-stale candidates — a stale answer is
            // still better to lose than a fresh one.
            if shard.len() >= max_per_shard {
                let stale_window = self.stale_window();
                let before = shard.len();
                shard.retain(|_, e| !e.is_past_stale_window(stale_window));
                let evicted = before - shard.len();
                if evicted > 0 {
                    self.inner
                        .evictions
                        .fetch_add(evicted as u64, Ordering::Relaxed);
                }
            }
            if shard.len() >= max_per_shard {
                // Still over capacity — evict ~10% of oldest entries by insertion time
                let to_evict = max_per_shard / 10 + 1;
                let mut entries: Vec<(CacheKey, std::time::Instant)> = shard
                    .iter()
                    .map(|(k, v)| (k.clone(), v.inserted_at))
                    .collect();
                entries.sort_by_key(|(_, ts)| *ts); // oldest first
                let mut evicted = 0;
                for (k, _) in entries.into_iter().take(to_evict) {
                    if shard.remove(&k).is_some() {
                        evicted += 1;
                    }
                }
                self.inner
                    .evictions
                    .fetch_add(evicted as u64, Ordering::Relaxed);
            }

            shard.insert(key, Arc::new(entry));
        }
        self.inner.insertions.fetch_add(1, Ordering::Relaxed);
    }

    pub fn flush(&self) {
        let mut total = 0usize;
        for shard in &self.inner.shards {
            let mut s = shard.write();
            total += s.len();
            s.clear();
        }
        self.inner
            .evictions
            .fetch_add(total as u64, Ordering::Relaxed);
    }

    pub fn flush_name(&self, name: &crate::protocol::name::DnsName) {
        for shard in &self.inner.shards {
            let mut s = shard.write();
            s.retain(|k, _| &k.name != name);
        }
    }

    pub fn stats(&self) -> super::store::CacheStats {
        let entries: usize = self.inner.shards.iter().map(|s| s.read().len()).sum();
        super::store::CacheStats {
            entries,
            max_entries: self.inner.max_entries,
            hits: self.inner.hits.load(Ordering::Relaxed),
            misses: self.inner.misses.load(Ordering::Relaxed),
            insertions: self.inner.insertions.load(Ordering::Relaxed),
            evictions: self.inner.evictions.load(Ordering::Relaxed),
        }
    }

    /// Evict entries that are past their stale-serving window. When
    /// serve-stale is disabled this reduces to "evict anything expired".
    pub fn evict_expired(&self) {
        let stale_window = self.stale_window();
        let mut evicted = 0usize;
        for shard in &self.inner.shards {
            let mut s = shard.write();
            let before = s.len();
            s.retain(|_, entry| !entry.is_past_stale_window(stale_window));
            evicted += before - s.len();
        }
        if evicted > 0 {
            self.inner
                .evictions
                .fetch_add(evicted as u64, Ordering::Relaxed);
        }
    }

    pub fn spawn_expiry_task(self, interval: Duration) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            let mut ticker = tokio::time::interval(interval);
            loop {
                ticker.tick().await;
                self.evict_expired();
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::name::DnsName;
    use crate::protocol::rdata::RData;
    use crate::protocol::record::{RecordClass, RecordType, ResourceRecord};
    use std::net::Ipv4Addr;

    fn make_key(name: &str) -> CacheKey {
        CacheKey::new(
            DnsName::from_str(name).unwrap(),
            RecordType::A,
            RecordClass::IN,
        )
    }

    fn make_entry(name: &str, ip: Ipv4Addr, ttl: u32) -> CacheEntry {
        let rr = ResourceRecord {
            name: DnsName::from_str(name).unwrap(),
            rtype: RecordType::A,
            rclass: RecordClass::IN,
            ttl,
            rdata: RData::A(ip),
        };
        CacheEntry::new(vec![rr], vec![], vec![], ttl, false, crate::protocol::rcode::Rcode::NoError)
    }

    #[test]
    fn test_fast_cache_insert_lookup() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        let key = make_key("example.com");
        let entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 300);

        store.insert(key.clone(), entry);

        let result = store.lookup(&key);
        assert!(result.is_some());
        assert_eq!(result.unwrap().answers.len(), 1);
    }

    #[test]
    fn test_fast_cache_miss() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        assert!(store.lookup(&make_key("nonexistent.com")).is_none());
    }

    #[test]
    fn test_fast_cache_stats() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        let key = make_key("example.com");
        let entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 300);

        store.insert(key.clone(), entry);
        store.lookup(&key); // hit
        store.lookup(&make_key("miss.com")); // miss

        let stats = store.stats();
        assert_eq!(stats.entries, 1);
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[test]
    fn test_fast_cache_flush() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        for i in 0..100 {
            let name = format!("test{}.com", i);
            store.insert(
                make_key(&name),
                make_entry(&name, Ipv4Addr::new(1, 2, 3, i as u8), 300),
            );
        }
        assert_eq!(store.stats().entries, 100);
        store.flush();
        assert_eq!(store.stats().entries, 0);
    }

    #[test]
    fn test_stale_lookup_disabled_returns_none() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        let key = make_key("example.com");
        let mut entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 60);
        entry.inserted_at = std::time::Instant::now() - std::time::Duration::from_secs(120);
        store.insert(key.clone(), entry);
        // stale disabled by default
        assert!(store.lookup_stale(&key).is_none());
    }

    #[test]
    fn test_stale_lookup_returns_expired_within_window() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        store.set_stale_window(86400);
        let key = make_key("example.com");
        // 10 s past the clamped 60 s TTL.
        let mut entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 60);
        entry.inserted_at = std::time::Instant::now() - std::time::Duration::from_secs(70);
        store.insert(key.clone(), entry);
        // Fresh lookup: none.
        assert!(store.lookup(&key).is_none());
        // Stale lookup: entry returned.
        let stale = store.lookup_stale(&key).expect("entry within stale window");
        assert_eq!(stale.answers.len(), 1);
    }

    #[test]
    fn test_stale_lookup_none_past_window() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        store.set_stale_window(120); // 2 minute window
        let key = make_key("example.com");
        let mut entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 60);
        // 5 minutes past insertion → 4 minutes past 60 s TTL → beyond 2 min window.
        entry.inserted_at = std::time::Instant::now() - std::time::Duration::from_secs(300);
        store.insert(key.clone(), entry);
        assert!(store.lookup(&key).is_none());
        assert!(store.lookup_stale(&key).is_none());
    }

    #[test]
    fn test_lookup_does_not_evict_within_stale_window() {
        // Regression: without the "leave-in-place when stale enabled" branch,
        // a miss-that-found-expired would immediately delete the entry and
        // lookup_stale would never see it.
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        store.set_stale_window(86400);
        let key = make_key("example.com");
        let mut entry = make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 60);
        entry.inserted_at = std::time::Instant::now() - std::time::Duration::from_secs(120);
        store.insert(key.clone(), entry);
        let _ = store.lookup(&key); // fresh path — expired, must NOT delete
        let stale = store.lookup_stale(&key);
        assert!(stale.is_some(), "serve-stale entry must survive prior fresh lookup");
    }

    #[test]
    fn test_fast_cache_ttl_clamping() {
        let store = FastCacheStore::new(1000, 60, 86400, 300);
        let key = make_key("example.com");
        store.insert(key.clone(), make_entry("example.com", Ipv4Addr::new(1, 2, 3, 4), 10));
        let cached = store.lookup(&key).unwrap();
        assert_eq!(cached.original_ttl, 60); // clamped to min
    }
}