| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	"math"
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metadata"
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 	rc := r.Copy() // We potentially modify r, to prevent other plugins from seeing this (r is a pointer), copy r into rc.
 | 
					
						
							|  |  |  | 	state := request.Request{W: w, Req: rc}
 | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 	do := state.Do()
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:47:35 -04:00
										 |  |  | 	ad := r.AuthenticatedData
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	zone := plugin.Zones(c.Zones).Matches(state.Name())
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if zone == "" {
 | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 		return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, rc)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	now := c.now().UTC()
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	server := metrics.WithServer(ctx)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 	// On cache miss, if the request has the OPT record and the DO bit set we leave the message as-is. If there isn't a DO bit
 | 
					
						
							|  |  |  | 	// set we will modify the request to _add_ one. This means we will always do DNSSEC lookups on cache misses.
 | 
					
						
							|  |  |  | 	// When writing to cache, any DNSSEC RRs in the response are written to cache with the response.
 | 
					
						
							|  |  |  | 	// When sending a response to a non-DNSSEC client, we remove DNSSEC RRs from the response. We use a 2048 buffer size, which is
 | 
					
						
							|  |  |  | 	// less than 4096 (and older default) and more than 1024 which may be too small. We might need to tweaks this
 | 
					
						
							|  |  |  | 	// value to be smaller still to prevent UDP fragmentation?
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 	ttl := 0
 | 
					
						
							|  |  |  | 	i := c.getIgnoreTTL(now, state, server)
 | 
					
						
							| 
									
										
										
										
											2020-03-20 05:05:09 -04:00
										 |  |  | 	if i == nil {
 | 
					
						
							| 
									
										
										
										
											2022-07-28 10:51:08 -04:00
										 |  |  | 		crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server, do: do, ad: ad,
 | 
					
						
							|  |  |  | 			nexcept: c.nexcept, pexcept: c.pexcept, wildcardFunc: wildcardFunc(ctx)}
 | 
					
						
							| 
									
										
										
										
											2021-01-15 09:32:49 -03:00
										 |  |  | 		return c.doRefresh(ctx, state, crr)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | 	ttl = i.ttl(now)
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 	if ttl < 0 {
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | 		// serve stale behavior
 | 
					
						
							|  |  |  | 		if c.verifyStale {
 | 
					
						
							|  |  |  | 			crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server, do: do}
 | 
					
						
							|  |  |  | 			cw := newVerifyStaleResponseWriter(crr)
 | 
					
						
							|  |  |  | 			ret, err := c.doRefresh(ctx, state, cw)
 | 
					
						
							|  |  |  | 			if cw.refreshed {
 | 
					
						
							|  |  |  | 				return ret, err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 		// Adjust the time to get a 0 TTL in the reply built from a stale item.
 | 
					
						
							|  |  |  | 		now = now.Add(time.Duration(ttl) * time.Second)
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | 		if !c.verifyStale {
 | 
					
						
							|  |  |  | 			cw := newPrefetchResponseWriter(server, state, c)
 | 
					
						
							|  |  |  | 			go c.doPrefetch(ctx, state, cw, i, now)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		servedStale.WithLabelValues(server, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2021-01-15 09:32:49 -03:00
										 |  |  | 	} else if c.shouldPrefetch(i, now) {
 | 
					
						
							|  |  |  | 		cw := newPrefetchResponseWriter(server, state, c)
 | 
					
						
							|  |  |  | 		go c.doPrefetch(ctx, state, cw, i, now)
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if i.wildcard != "" {
 | 
					
						
							|  |  |  | 		// Set wildcard source record name to metadata
 | 
					
						
							|  |  |  | 		metadata.SetValueFunc(ctx, "zone/wildcard", func() string {
 | 
					
						
							|  |  |  | 			return i.wildcard
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:47:35 -04:00
										 |  |  | 	resp := i.toMsg(r, now, do, ad)
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 	w.WriteMsg(resp)
 | 
					
						
							|  |  |  | 	return dns.RcodeSuccess, nil
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | func wildcardFunc(ctx context.Context) func() string {
 | 
					
						
							|  |  |  | 	return func() string {
 | 
					
						
							|  |  |  | 		// Get wildcard source record name from metadata
 | 
					
						
							|  |  |  | 		if f := metadata.ValueFunc(ctx, "zone/wildcard"); f != nil {
 | 
					
						
							|  |  |  | 			return f()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 09:32:49 -03:00
										 |  |  | func (c *Cache) doPrefetch(ctx context.Context, state request.Request, cw *ResponseWriter, i *item, now time.Time) {
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 	cachePrefetches.WithLabelValues(cw.server, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2021-01-15 09:32:49 -03:00
										 |  |  | 	c.doRefresh(ctx, state, cw)
 | 
					
						
							| 
									
										
										
										
											2019-10-03 15:05:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// When prefetching we loose the item i, and with it the frequency
 | 
					
						
							|  |  |  | 	// that we've gathered sofar. See we copy the frequencies info back
 | 
					
						
							|  |  |  | 	// into the new item that was stored in the cache.
 | 
					
						
							|  |  |  | 	if i1 := c.exists(state); i1 != nil {
 | 
					
						
							|  |  |  | 		i1.Freq.Reset(now, i.Freq.Hits())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | func (c *Cache) doRefresh(ctx context.Context, state request.Request, cw dns.ResponseWriter) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2021-01-15 09:32:49 -03:00
										 |  |  | 	if !state.Do() {
 | 
					
						
							|  |  |  | 		setDo(state.Req)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return plugin.NextOrFailure(c.Name(), c.Next, ctx, cw, state.Req)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 15:05:44 +01:00
										 |  |  | func (c *Cache) shouldPrefetch(i *item, now time.Time) bool {
 | 
					
						
							|  |  |  | 	if c.prefetch <= 0 {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	i.Freq.Update(c.duration, now)
 | 
					
						
							|  |  |  | 	threshold := int(math.Ceil(float64(c.percentage) / 100 * float64(i.origTTL)))
 | 
					
						
							|  |  |  | 	return i.Freq.Hits() >= c.prefetch && i.ttl(now) <= threshold
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (c *Cache) Name() string { return "cache" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | // getIgnoreTTL unconditionally returns an item if it exists in the cache.
 | 
					
						
							|  |  |  | func (c *Cache) getIgnoreTTL(now time.Time, state request.Request, server string) *item {
 | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 	k := hash(state.Name(), state.QType())
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 	cacheRequests.WithLabelValues(server, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if i, ok := c.ncache.Get(k); ok {
 | 
					
						
							| 
									
										
										
										
											2022-05-01 08:57:03 -04:00
										 |  |  | 		itm := i.(*item)
 | 
					
						
							|  |  |  | 		ttl := itm.ttl(now)
 | 
					
						
							|  |  |  | 		if itm.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 			cacheHits.WithLabelValues(server, Denial, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2020-03-20 05:05:09 -04:00
										 |  |  | 			return i.(*item)
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if i, ok := c.pcache.Get(k); ok {
 | 
					
						
							| 
									
										
										
										
											2022-05-01 08:57:03 -04:00
										 |  |  | 		itm := i.(*item)
 | 
					
						
							|  |  |  | 		ttl := itm.ttl(now)
 | 
					
						
							|  |  |  | 		if itm.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 			cacheHits.WithLabelValues(server, Success, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2020-03-20 05:05:09 -04:00
										 |  |  | 			return i.(*item)
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 	cacheMisses.WithLabelValues(server, c.zonesMetricLabel).Inc()
 | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | func (c *Cache) exists(state request.Request) *item {
 | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 	k := hash(state.Name(), state.QType())
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	if i, ok := c.ncache.Get(k); ok {
 | 
					
						
							|  |  |  | 		return i.(*item)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if i, ok := c.pcache.Get(k); ok {
 | 
					
						
							|  |  |  | 		return i.(*item)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // setDo sets the DO bit and UDP buffer size in the message m.
 | 
					
						
							|  |  |  | func setDo(m *dns.Msg) {
 | 
					
						
							|  |  |  | 	o := m.IsEdns0()
 | 
					
						
							|  |  |  | 	if o != nil {
 | 
					
						
							|  |  |  | 		o.SetDo()
 | 
					
						
							|  |  |  | 		o.SetUDPSize(defaultUDPBufSize)
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	o = &dns.OPT{Hdr: dns.RR_Header{Name: ".", Rrtype: dns.TypeOPT}}
 | 
					
						
							|  |  |  | 	o.SetDo()
 | 
					
						
							|  |  |  | 	o.SetUDPSize(defaultUDPBufSize)
 | 
					
						
							|  |  |  | 	m.Extra = append(m.Extra, o)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // defaultUDPBufsize is the bufsize the cache plugin uses on outgoing requests that don't
 | 
					
						
							|  |  |  | // have an OPT RR.
 | 
					
						
							|  |  |  | const defaultUDPBufSize = 2048
 |