systemd-resolved: routes answers the uplink's names and the mesh's fallback (issue 960)

routes (bus and the machine socket) gains uplink_names {names, link,
refused}, the applied list's names resolved holds on that link now, never
the file's, and mesh_resolvers {silent, since} from the guard's fallback
state. The guard writes that state at each of ADR 0688's two turns, and
once more at its own first round, since it goes at every boot.

Builds: design 08 sign-in-page §2, §8, §9
This commit is contained in:
jochen
2026-11-24 21:26:30 +01:00
parent b694c5e2a5
commit 9f5614b35e
5 changed files with 165 additions and 7 deletions
@@ -46,6 +46,9 @@ const (
Socket = RunDir + "/verbs.sock"
History = RunDir + "/history.json"
Displaced = RunDir + "/displaced"
// Fallback is `routes`'s state for ADR 0688's fallback (design 08 sign-in-page §8, S-N5, R2-2):
// whether the mesh's resolvers are silent, and since when.
Fallback = RunDir + "/fallback.json"
)
// Timing.
@@ -94,6 +97,11 @@ type Guard struct {
// silent is whether none of the mesh's resolvers answered at the last look, so each turn is said once.
// Only the routes' loop reads and writes it.
silent bool
// silentSince is when silent last changed, for Fallback's `since` (design 08 sign-in-page §8, S-N5).
silentSince time.Time
// fallbackWritten is whether Fallback has been written since this guard started; its first round
// writes it once more regardless of a turn, since it goes at every boot (§8).
fallbackWritten bool
// applied is what the guard last gave the link carrying the default route (design 08 sign-in-page §8):
// read at start from UplinkNamesAppliedFile, so a restart takes the names off the link it names,
@@ -355,24 +363,69 @@ func (g *Guard) KeepRoutes(ctx context.Context) {
return
}
if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) {
if !g.silent {
if turn := !g.silent; turn || !g.fallbackWritten {
if turn {
g.silentSince = g.Now()
}
g.silent = true
g.Log("none of the mesh's resolvers answers; a network's own servers answer every name until one does")
g.writeFallback()
if turn {
g.Log("none of the mesh's resolvers answers; a network's own servers answer every name until one does")
}
}
if changed, err := g.Resolver.ADefaultRouteAgain(ctx, routes); err == nil && len(changed) > 0 {
g.Log("%s answers every name again, while the mesh's resolvers are silent", strings.Join(changed, ", "))
}
return
}
if g.silent {
if turn := g.silent; turn || !g.fallbackWritten {
if turn {
g.silentSince = g.Now()
}
g.silent = false
g.Log("one of the mesh's resolvers answers again")
g.writeFallback()
if turn {
g.Log("one of the mesh's resolvers answers again")
}
}
if changed, err := g.Resolver.OnlyTheMeshIsADefaultRoute(ctx); err == nil && len(changed) > 0 {
g.Log("%s had servers answering every name; now only their own domains", strings.Join(changed, ", "))
}
}
// Fallback is `routes`'s state for ADR 0688's fallback (design 08 sign-in-page §8).
type FallbackState struct {
Silent bool `json:"silent"`
Since time.Time `json:"since"`
}
// writeFallback writes Resolver.FallbackFile whole, at each of the fallback's two turns, and once more at
// the guard's first round, since it goes at every boot (design 08 sign-in-page §8).
func (g *Guard) writeFallback() {
g.fallbackWritten = true
raw, err := json.MarshalIndent(FallbackState{Silent: g.silent, Since: g.silentSince}, "", " ")
if err != nil {
return
}
tmp := g.Resolver.FallbackFile + ".tmp"
if os.WriteFile(tmp, raw, 0o644) == nil {
_ = os.Rename(tmp, g.Resolver.FallbackFile)
}
}
// ReadFallback reads what the guard last said of ADR 0688's fallback; the zero value before it has run.
func ReadFallback(path string) FallbackState {
raw, err := os.ReadFile(path)
if err != nil {
return FallbackState{}
}
var s FallbackState
if json.Unmarshal(raw, &s) != nil {
return FallbackState{}
}
return s
}
// sameNames is whether two name lists are the same, in the same order (the file holds them one a line, in
// the order given).
func sameNames(a, b []string) bool {
@@ -499,7 +552,8 @@ func (g *Guard) Answer(ctx context.Context, req Request) Reply {
case "routes":
var routes *Routes
if routes, err = g.Resolver.Routes(ctx); err == nil {
result = map[string]any{"mesh": routes.Mesh, "links": routes.Links}
result = map[string]any{"mesh": routes.Mesh, "links": routes.Links,
"uplink_names": g.Resolver.UplinkNamesAnswer(routes), "mesh_resolvers": g.Resolver.MeshResolversAnswer()}
}
case "route":
var routed *Routed