Skip to content

Commit

Permalink
Add LocID and MapID fields
Browse files Browse the repository at this point in the history
Summary:
To help debug DNS targeting issues, logging location ID and map ID would be useful.

This is a task from Better Engineering Week.

Reviewed By: mjdiffy

Differential Revision: D63011253

fbshipit-source-id: 4645b1a80ddab307cedfd8f98bc5429f9cb0fe08
  • Loading branch information
aryanpatel3 authored and facebook-github-bot committed Oct 2, 2024
1 parent 5512326 commit 82b4af4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
8 changes: 5 additions & 3 deletions dnsrocks/dnsdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ const (

func (m Lmap) String() string {
var builder strings.Builder
putlmaptext(&builder, m)
Putlmaptext(&builder, m)
return builder.String()
}

Expand Down Expand Up @@ -1639,7 +1639,8 @@ func putloc(w io.Writer, lo Loc) error {
return err
}

func putloctext(w io.Writer, lo Loc) {
// Putloctext escapes the location ID and writes it to the writer
func Putloctext(w io.Writer, lo Loc) {
if len(lo) == 2 {
for _, b := range lo {
fmt.Fprintf(w, "\\%03o", b)
Expand Down Expand Up @@ -1670,7 +1671,8 @@ func putlmap(w io.Writer, m Lmap) {
}
}

func putlmaptext(w io.Writer, m Lmap) {
// Putlmaptext escapes the map ID and writes it to the writer
func Putlmaptext(w io.Writer, m Lmap) {
if len(m) == 0 {
m = []byte{0, 0}
}
Expand Down
38 changes: 19 additions & 19 deletions dnsrocks/dnsdata/data_marshaltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Rsoa) MarshalText() (text []byte, err error) {
// skip one unused field
w.Write(NSEP)
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)

return w.Bytes(), nil
}
Expand All @@ -53,11 +53,11 @@ func (r *Rsoa) MarshalText() (text []byte, err error) {
func (r *Rnet) MarshalText() (text []byte, err error) {
w := new(bytes.Buffer)
w.WriteString(string(prefixNet))
putloctext(w, r.lo)
Putloctext(w, r.lo)
w.Write(NSEP)
w.Write([]byte(r.ipnet.String()))
w.Write(NSEP)
putlmaptext(w, r.lmap)
Putlmaptext(w, r.lmap)
return w.Bytes(), nil
}

Expand All @@ -79,7 +79,7 @@ func (r *Rns) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.Rns1.lo)
Putloctext(w, r.Rns1.lo)
return w.Bytes(), nil
}

Expand All @@ -97,7 +97,7 @@ func (r *Rns1) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -120,7 +120,7 @@ func (r *Raddr) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
w.Write(NSEP)
fmt.Fprintf(w, "%d", r.weight)
return w.Bytes(), nil
Expand All @@ -145,7 +145,7 @@ func (r *Rpaddr) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -169,7 +169,7 @@ func (r *Rmx) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.Rmx1.lo)
Putloctext(w, r.Rmx1.lo)
return w.Bytes(), nil
}

Expand All @@ -189,7 +189,7 @@ func (r *Rmx1) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand Down Expand Up @@ -217,7 +217,7 @@ func (r *Rsrv) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.Rsrv1.lo)
Putloctext(w, r.Rsrv1.lo)
return w.Bytes(), nil
}

Expand All @@ -241,7 +241,7 @@ func (r *Rsrv1) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -260,7 +260,7 @@ func (r *Rcname) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -276,7 +276,7 @@ func (r *Rptr) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -295,7 +295,7 @@ func (r *Rtxt) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -313,7 +313,7 @@ func (r *Raux) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.lo)
Putloctext(w, r.lo)
return w.Bytes(), nil
}

Expand All @@ -335,7 +335,7 @@ func (r *Rdot) MarshalText() (text []byte, err error) {
w.Write(NSEP)
// skip one unused field
w.Write(NSEP)
putloctext(w, r.Rns1.lo)
Putloctext(w, r.Rns1.lo)
return w.Bytes(), nil
}

Expand All @@ -345,7 +345,7 @@ func (r *Ripmap) MarshalText() (text []byte, err error) {
w.WriteString(string(prefixIPMap))
putdomtext(w, r.dom)
w.Write(NSEP)
putlmaptext(w, r.lmap)
Putlmaptext(w, r.lmap)
return w.Bytes(), nil
}

Expand All @@ -355,7 +355,7 @@ func (r *Rcsmap) MarshalText() (text []byte, err error) {
w.WriteString(string(prefixCSMap))
putdomtext(w, r.dom)
w.Write(NSEP)
putlmaptext(w, r.lmap)
Putlmaptext(w, r.lmap)
return w.Bytes(), nil
}

Expand All @@ -381,7 +381,7 @@ func (r *Rsvcb) MarshalText() (text []byte, err error) {
buf.Write(NSEP)
fmt.Fprintf(buf, "%d", r.ttl)
buf.Write(NSEP)
putloctext(buf, r.lo)
Putloctext(buf, r.lo)
buf.Write(NSEP)
fmt.Fprintf(buf, "%d", r.priority)
buf.Write(NSEP)
Expand Down
4 changes: 2 additions & 2 deletions dnsrocks/dnsdata/rearranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r RangePoints) String() string {
func (p *RangePoint) MarshalTextForLmap(lmap Lmap) (text []byte, err error) {
w := new(bytes.Buffer)
w.WriteString(string(prefixRangePoint))
putlmaptext(w, lmap)
Putlmaptext(w, lmap)
w.Write(NSEP)
ip := p.To16()
b, err := ip.MarshalText()
Expand All @@ -92,7 +92,7 @@ func (p *RangePoint) MarshalTextForLmap(lmap Lmap) (text []byte, err error) {
fmt.Fprint(w, mlen)
w.Write(NSEP)
lo := Loc(p.LocID())
putloctext(w, lo)
Putloctext(w, lo)
}
return w.Bytes(), nil
}
Expand Down
20 changes: 10 additions & 10 deletions dnsrocks/dnsserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func MakeOPTWithECS(s string) (*dns.OPT, error) {
}

// writeAndLog writes the response to the network as well as log and bump stats
func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDNS0_SUBNET) (int, error) {
func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) (int, error) {
rcode := resp.Rcode

state.SizeAndDo(resp)
Expand All @@ -123,7 +123,7 @@ func (h *FBDNSDB) writeAndLog(state request.Request, resp *dns.Msg, ecs *dns.EDN
if err != nil {
return dns.RcodeServerFailure, err
}
h.logger.Log(state, resp, ecs)
h.logger.Log(state, resp, ecs, loc)
if !resp.Authoritative {
h.stats.IncrementCounter("DNS_queries_notauthoritative")
}
Expand Down Expand Up @@ -181,15 +181,15 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r

// Check if this is a supported edns version
if a, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once.
return h.writeAndLog(state, a, ecs)
return h.writeAndLog(state, a, ecs, loc)
}

offset, err := dns.PackDomainName(state.Name(), packedQName, 0, nil, false)
if err != nil {
h.stats.IncrementCounter("DNS_error.pack_domain_fail")
glog.Errorf("could not pack domain %s", state.Name())
dns.HandleFailed(w, r)
h.logger.LogFailed(state, r, ecs)
h.logger.LogFailed(state, r, ecs, loc)
// nolint: nilerr
return dns.RcodeServerFailure, nil
}
Expand All @@ -199,15 +199,15 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r
if ecs, loc, err = reader.FindLocation(packedQName, r, state.IP()); err != nil {
glog.Errorf("%s: failed to find location: %v", state.Name(), err)
//dns.HandleFailed(w, r)
h.logger.LogFailed(state, r, ecs)
h.logger.LogFailed(state, r, ecs, loc)
return dns.RcodeServerFailure, nil
}

if loc == nil {
// We could not find a location, not even the default one... potentially a bogus DB.
// dns.HandleFailed(w, r)
glog.Errorf("%s: nil location", state.Name())
h.logger.LogFailed(state, r, ecs)
h.logger.LogFailed(state, r, ecs, loc)
return dns.RcodeServerFailure, nil
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r

resp.Extra = append([]dns.RR{o}, resp.Extra...)
}
return h.writeAndLog(state, resp, ecs)
return h.writeAndLog(state, resp, ecs, loc)
}
} else {
h.stats.IncrementCounter("DNS_cache.missed")
Expand Down Expand Up @@ -293,7 +293,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r
m.IsEdns0().Option = append(m.IsEdns0().Option, &ede)
}
// does not matter if this write fails
return h.writeAndLog(state, m, ecs)
return h.writeAndLog(state, m, ecs, loc)
}

// Not authoritative but we have NS (implicit or we would not have passed the
Expand Down Expand Up @@ -337,7 +337,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r
if err != nil {
glog.Errorf("Failed to unpack control domain name %s", err)
dns.HandleFailed(w, r)
h.logger.Log(state, r, ecs)
h.logger.Log(state, r, ecs, loc)
return dns.RcodeServerFailure, nil
}

Expand Down Expand Up @@ -387,7 +387,7 @@ func (h *FBDNSDB) ServeDNSWithRCODE(ctx context.Context, w dns.ResponseWriter, r
a.Extra = append([]dns.RR{o}, a.Extra...)
}

return h.writeAndLog(state, a, ecs)
return h.writeAndLog(state, a, ecs, loc)
}

// ServeDNS implements the plugin.Handler interface.
Expand Down
16 changes: 9 additions & 7 deletions dnsrocks/dnsserver/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
"strings"

"github.com/coredns/coredns/request"
"github.com/facebook/dns/dnsrocks/db"
"github.com/miekg/dns"
)

// Logger is an interface for logging messages
type Logger interface {
// LogFailed logs a message when we could not construct an answer
LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET)
LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location)
// Log logs a DNS response
Log(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET)
Log(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location)
}

// TextLogger logs to an io.Writer
Expand All @@ -36,24 +37,25 @@ type TextLogger struct {
}

// Log is used to log to an ioWriter.
func (l *TextLogger) Log(state request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) {
func (l *TextLogger) Log(state request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) {
fmt.Fprintf(l.IoWriter, "[%s] %s %s %s\n",
state.IP(), strings.ToUpper(state.Proto()),
state.Name(), state.Type())
}

// LogFailed is used to log failures
func (l *TextLogger) LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET) {
func (l *TextLogger) LogFailed(state request.Request, r *dns.Msg, ecs *dns.EDNS0_SUBNET, loc *db.Location) {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeServerFailure)
l.Log(state, m, ecs)
l.Log(state, m, ecs, loc)
}

// DummyLogger logs nothing
type DummyLogger struct{}

// Log is used to log to an ioWriter.
func (l *DummyLogger) Log(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) {}
func (l *DummyLogger) Log(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) {}

// LogFailed is used to log failures
func (l *DummyLogger) LogFailed(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET) {}
func (l *DummyLogger) LogFailed(_ request.Request, _ *dns.Msg, _ *dns.EDNS0_SUBNET, _ *db.Location) {
}

0 comments on commit 82b4af4

Please sign in to comment.