Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
356 lines (302 loc) · 12.4 KB
/
Copy pathcache.go
File metadata and controls
356 lines (302 loc) · 12.4 KB
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
/*
File: cache.go
Version: 2.54.0 (Split)
Last Updated: 04-Sep-2026 12:32 CEST
Description:
High-performance, sharded, non-blocking DNS cache core for sdproxy.
Manages memory allocation, cryptographic sharding (HashDoS protection),
and asynchronous garbage collection (Sweeper).
Hot-path Read/Write operations have been moved to cache_rw.go.
Disk persistence has been moved to cache_persistence.go.
UI introspection has been moved to cache_ui.go.
Changes:
2.54.0 - [SECURITY/FIX] Added RecursionDesired to cache partitioning and shard hashing.
Recursive and non-recursive client queries must never share cached responses.
2.53.0 - [PERF] Eliminated significant Garbage Collection (GC) thrashing
within the `runSweeper` background routine. Hoisted the `toDelete`
array allocation completely out of the iteration loop, leveraging
`[:0]` capacity-reuse natively to eradicate 32 redundant slice
allocations per sweeping cycle organically across the lifetime
of the process.
2.52.0 - [CLEANUP] Simplified `storeItem` mutex orchestration organically
via `defer` declarations. Eliminated redundant condition bounds
during oldest-victim sample evictions natively.
2.51.0 - [PERF/FIX] Eliminated the cache-hot-path repack caused by
`answer_sort: round-robin`.
*/
package main
import (
"hash/maphash"
"log"
"sync"
"sync/atomic"
"time"
)
// DNSCacheKey is the map key for all cache lookups.
//
// Name must always be normalised (lowercase, no trailing dot) as produced by
// lowerTrimDot in policy.go — ensures "GOOGLE.COM." and "google.com" share
// the same entry. RouteIdx is a compact uint16 so the key stays small and
// struct-comparable without a string route name on every lookup.
// DoBit and CdBit ensure cryptographically signed/unsigned, and validated/unvalidated
// requests are isolated securely, preventing cross-contamination.
// BypassGlobal explicitly partitions the cache payload natively when the routing
// group bypasses globally defined Spoofed Records (rrs) and Domain Policies natively.
// ClientName explicitly partitions the cache payload natively when the routing
// group relies on tailored upstream endpoints (e.g., NextDNS/ControlD).
// ECS explicitly partitions the cache payload natively when the routing group
// injects localized EDNS0 Client Subnet architectures, neutralizing cross-contamination.
type DNSCacheKey struct {
Name string
ClientName string
ECS string
Qtype uint16
Qclass uint16
RouteIdx uint16
DoBit bool
CdBit bool
BypassGlobal bool
RecursionDesired bool
}
// cacheItem is a single cached DNS response in wire format.
type cacheItem struct {
packed atomic.Pointer[[]byte] // immutable packed DNS wire bytes, rotatable atomically
expireNano int64 // expiry deadline as unix nanoseconds
staleNano int64 // end of stale-serving window; == expireNano for synth entries
cachedAtNano int64 // timestamp of exact creation time for introspection
routeName string // upstream group name used by backgroundRevalidate
hits atomic.Uint32 // hit counter for prefetch popularity gate
prefetched atomic.Bool // CAS flag: exactly one prefetch fires per entry lifetime
// rotation drives answer_sort: round-robin without ever rewriting the
// packed bytes. Advanced once per cache hit; CacheGet rotates the unpacked
// answer set by this offset. Deliberately allowed to wrap — the consumer
// takes it modulo the group length, so uint32 overflow is a no-op.
rotation atomic.Uint32
}
// cacheShard is an independently locked segment of the cache.
// 32 shards reduce write-lock contention by ~32× compared to a single mutex.
type cacheShard struct {
sync.RWMutex
items map[DNSCacheKey]*cacheItem
}
const shardCount = 32
var shards [shardCount]*cacheShard
// cacheMaxPerShard is the per-shard entry ceiling, pre-computed from the
// configured total size at InitCache time.
var cacheMaxPerShard int
// cacheHashSeed provides cryptographic randomization for the shard hashing algorithm.
// It is initialized exactly once at startup to ensure consistent bucket resolution.
var cacheHashSeed maphash.Seed
// ---------------------------------------------------------------------------
// Startup feature flags — set once in InitCache, read on every hot-path call.
// ---------------------------------------------------------------------------
// hasPrefetch is true when both prefetch knobs are > 0.
// Determines whether cache hits evaluate background revalidation logic natively.
var hasPrefetch bool
// staleEnabled is true when cfg.Cache.StaleTTL > 0.
// Guards the stale-window logic so the common disabled case pays nothing
// beyond a single bool load.
var staleEnabled bool
// cacheUpstreamNeg controls whether upstream NXDOMAIN / NODATA responses are
// stored. true = cache them (RFC 2308 compliant, default).
// false = always forward negative queries upstream — useful when upstream
// blocklists change frequently and you don't want negatives to linger.
var cacheUpstreamNeg bool
// cacheRotateAnswers is true when answer_sort is "round-robin".
var cacheRotateAnswers bool
// serveStaleInfinite controls whether expired cache entries are retained
// indefinitely and served as an absolute last resort during upstream outages.
var serveStaleInfinite bool
// cacheSynthFlag controls whether synthesised policy responses (domain_policy,
// rtype_policy, AAAA filter, strict_ptr, obsolete qtypes) are stored via
// CacheSetSynth. When true, repeat policy-blocked queries hit the cache at
// step 3 in process.go and skip domain walks + policy lookups entirely.
var cacheSynthFlag bool
// cacheLocalIdentity controls whether local A/AAAA/PTR responses from
// hosts/leases files are stored via CacheSetSynth. Only safe when
// syntheticTTL ≤ identity.poll_interval — otherwise stale local addresses
// may be served in the gap between a file change and the next poll.
var cacheLocalIdentity bool
// ---------------------------------------------------------------------------
// Initialisation
// ---------------------------------------------------------------------------
// InitCache initialises all shards and starts the background sweeper.
// Called once from main() after cfg is populated.
func InitCache(maxSize int, _ int) {
if !cfg.Cache.Enabled {
return
}
// Initialize the randomized seed for HashDoS protection natively at boot
cacheHashSeed = maphash.MakeSeed()
for i := range shards {
shards[i] = &cacheShard{items: make(map[DNSCacheKey]*cacheItem)}
}
cacheMaxPerShard = maxSize / shardCount
if cacheMaxPerShard < 1 {
cacheMaxPerShard = 1
}
// Set hot-path feature flags once so CacheGet/CacheSet branches are pure
// bool loads — no config struct field accesses on the critical path.
hasPrefetch = cfg.Cache.PrefetchBefore > 0 && cfg.Cache.PrefetchMinHits > 0
staleEnabled = cfg.Cache.StaleTTL > 0
cacheUpstreamNeg = cfg.Cache.CacheUpstreamNegative
cacheSynthFlag = cfg.Cache.CacheSynthetic
cacheLocalIdentity = cfg.Cache.CacheLocalIdentity
serveStaleInfinite = cfg.Cache.ServeStaleInfinite
cacheRotateAnswers = cfg.Cache.AnswerSort == "round-robin"
sweepInterval := 60 * time.Second
if cfg.Cache.SweepIntervalS > 0 {
sweepInterval = time.Duration(cfg.Cache.SweepIntervalS) * time.Second
}
if logCaching {
log.Printf("[CACHE] Initialised: size=%d shards=%d sweep=%s stale=%ds "+
"prefetch=%ds/%dhits synth=%v localid=%v upneg=%v sort=%s inf_stale=%v persist=%v",
maxSize, shardCount, sweepInterval,
cfg.Cache.StaleTTL, cfg.Cache.PrefetchBefore, cfg.Cache.PrefetchMinHits,
cacheSynthFlag, cacheLocalIdentity, cacheUpstreamNeg, cfg.Cache.AnswerSort, serveStaleInfinite, cfg.Cache.Persist)
}
go runSweeper(sweepInterval)
if cfg.Cache.Persist {
LoadCache()
if cfg.Cache.PersistSaveInterval != "" && cfg.Cache.PersistSaveInterval != "0" && cfg.Cache.PersistSaveInterval != "0s" {
if interval, err := time.ParseDuration(cfg.Cache.PersistSaveInterval); err == nil && interval > 0 {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
SaveCache()
case <-shutdownCh:
return
}
}
}()
} else {
if logCaching {
log.Printf("[CACHE] WARNING: Invalid persist_save_interval %q: %v", cfg.Cache.PersistSaveInterval, err)
}
}
}
}
}
// runSweeper periodically reclaims cache entries whose stale window has passed.
func runSweeper(interval time.Duration) {
if serveStaleInfinite {
// Disable garbage collection of expired records so they remain
// available indefinitely for upstream outage fallbacks.
return
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
// [PERF/FIX] Allocate the eviction array ONCE outside the sweeping loop.
// By leveraging capacity-reuse (toDelete[:0]) natively, we completely eradicate
// 32 redundant slice heap-allocations per sweep cycle, drastically reducing
// Garbage Collection (GC) thrashing and locking contention organically over
// the lifetime of the process.
toDelete := make([]DNSCacheKey, 0, max(cacheMaxPerShard/4, 16))
for {
select {
case <-ticker.C:
now := time.Now().UnixNano()
for i := range shards {
shard := shards[i]
// Re-slice to zero length to reuse the underlying capacity organically
toDelete = toDelete[:0]
shard.RLock()
for k, v := range shard.items {
if now >= v.staleNano {
toDelete = append(toDelete, k)
}
}
shard.RUnlock()
if len(toDelete) == 0 {
continue
}
shard.Lock()
for _, k := range toDelete {
// Re-check: a concurrent CacheSet may have refreshed this key.
if v, ok := shard.items[k]; ok && now >= v.staleNano {
delete(shard.items, k)
}
}
shard.Unlock()
}
case <-shutdownCh:
return
}
}
}
// ---------------------------------------------------------------------------
// Shard selector
// ---------------------------------------------------------------------------
// getShard maps a cache key to a shard using Go's hardened maphash.
func getShard(key DNSCacheKey) *cacheShard {
// Hash the domain name utilizing the cryptographically seeded maphash
h := maphash.String(cacheHashSeed, key.Name)
// Incorporate dynamic ClientName boundaries to guarantee total cache isolation
// when upstream protocols specify individualized targeting natively.
if key.ClientName != "" {
h ^= maphash.String(cacheHashSeed, key.ClientName) * 0x5bd1e9955bd1e995
}
// Incorporate ECS boundaries dynamically to prevent subset-target contamination
// natively across independent origin IP mappings.
if key.ECS != "" {
h ^= maphash.String(cacheHashSeed, key.ECS) * 0x9e3779b97f4a7c15
}
// Pack the remaining structured, trusted deterministic fields.
mix := uint64(key.Qtype)<<32 | uint64(key.Qclass)<<16 | uint64(key.RouteIdx)
if key.DoBit {
mix |= 1 << 48
}
if key.CdBit {
mix |= 1 << 49
}
if key.BypassGlobal {
mix |= 1 << 50
}
if key.RecursionDesired {
mix |= 1 << 51
}
// Fold the scalar fields into the primary hash
h ^= mix
// Avalanche the upper bits downwards to guarantee absolute uniformity across the lowest 5 bits
h ^= h >> 32
h ^= h >> 16
h ^= h >> 8
h ^= h >> 4
return shards[h&(shardCount-1)]
}
// ---------------------------------------------------------------------------
// Internal store helper
// ---------------------------------------------------------------------------
// evictionSampleSize is how many entries storeItem inspects when choosing a
// victim under capacity pressure.
const evictionSampleSize = 8
// storeItem acquires the shard write-lock, evicts the oldest of a small random
// sample when the shard is at capacity, then stores item under key.
//
// [CLEANUP 2.52.0] Refactored to utilize localized `defer` operations, eliminating
// redundant condition constraints natively during active eviction matrices.
func storeItem(key DNSCacheKey, item *cacheItem) {
shard := getShard(key)
shard.Lock()
defer shard.Unlock()
if _, exists := shard.items[key]; !exists && len(shard.items) >= cacheMaxPerShard {
var victim DNSCacheKey
var victimStale int64
var sampled int
for k, v := range shard.items {
if sampled == 0 || v.staleNano < victimStale {
victim = k
victimStale = v.staleNano
}
sampled++
if sampled >= evictionSampleSize {
break
}
}
delete(shard.items, victim)
}
shard.items[key] = item
}