From 786a23638bd44bfdb14230f90d744b2c2ebe03e0 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 24 Nov 2026 21:23:56 +0100 Subject: [PATCH] M6 networkmanager: the bundle gives the resolver the check's name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bundle calls this node's node-resolver uplink-names with the check address's host at its start, at a changed check-address, at its bus connection's return and when this node's holder of node-resolver runs, reading the holder from the assignments; no holder is recorded and no failure, a failed call stands failed in names.json until the next moment. Builds: design 08 sign-in-page §12, §13, §22 --- .../networkmanager/cmd/uplink-tools/bundle.go | 50 ++++-- .../networkmanager/cmd/uplink-tools/dbus.go | 20 ++- .../networkmanager/cmd/uplink-tools/names.go | 150 ++++++++++++++++++ .../cmd/uplink-tools/names_test.go | 139 ++++++++++++++++ .../cmd/uplink-tools/publish_test.go | 9 +- modules/networkmanager/go.mod | 1 + 6 files changed, 356 insertions(+), 13 deletions(-) create mode 100644 modules/networkmanager/cmd/uplink-tools/names.go create mode 100644 modules/networkmanager/cmd/uplink-tools/names_test.go diff --git a/modules/networkmanager/cmd/uplink-tools/bundle.go b/modules/networkmanager/cmd/uplink-tools/bundle.go index e24ff6bb4..3afb7f9b7 100644 --- a/modules/networkmanager/cmd/uplink-tools/bundle.go +++ b/modules/networkmanager/cmd/uplink-tools/bundle.go @@ -17,14 +17,25 @@ import ( type bundle struct { folder Folder pub *publisher + names *namer mu sync.Mutex missed bool lastAt time.Time } +// bundleRuns is what the bundle hears through: file events of the kernel, its consumer's heartbeat and its +// subscription. +type bundleRuns struct { + files func(dirs []string, stop <-chan struct{}) <-chan struct{} + fileMade func(dir, name string, stop <-chan struct{}) <-chan struct{} + onHeartbeat func(func(stdio.Heartbeat)) error + subscribe func(pattern string, handle func(stdio.Envelope) error) error +} + func newBundle(f Folder) *bundle { - return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish}} + return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish}, + names: &namer{folder: f, ask: stdio.Ask, now: time.Now}} } // seatPublish says a kept event through the runtime, which awaits the stream's acknowledgement. @@ -39,23 +50,43 @@ func startBundle() { journal("the module's folder: %v", err) return } - b := newBundle(f) - b.start(fileEvents, stdio.OnHeartbeat) + newBundle(f).start(bundleRuns{files: fileEvents, fileMade: fileMade, onHeartbeat: stdio.OnHeartbeat, + subscribe: stdio.Subscribe}) } -// start publishes what is kept, then again at each file event on pending/ and at each return of the bus connection. -func (b *bundle) start(files func([]string, <-chan struct{}) <-chan struct{}, onHeartbeat func(func(stdio.Heartbeat)) error) { +// start publishes what is kept and gives the resolver the check's host name, then does each again at its moments: a +// file event on pending/ publishes, a changed check-address gives the names, node-resolver's holder running gives the +// names, and the bus connection's return does both (§13, §20). +func (b *bundle) start(runs bundleRuns) { pending := filepath.Join(b.folder.Dir, pendingName) if err := os.MkdirAll(pending, 0o700); err != nil { journal("making %s failed: %v", pending, err) } - kept := files([]string{pending}, nil) + kept := runs.files([]string{pending}, nil) + check := runs.fileMade(b.folder.Dir, checkName, nil) b.pub.publishKept("the bundle's start") - if err := onHeartbeat(b.heartbeat); err != nil { + b.names.give("the bundle's start") + if err := runs.subscribe(runningPattern, b.names.running); err != nil { + journal("subscribing to %s failed: %v", runningPattern, err) + } + if err := runs.onHeartbeat(b.heartbeat); err != nil { journal("hearing the bus connection's heartbeat failed: %v", err) } - for range kept { - b.pub.publishKept("a kept event") + for kept != nil || check != nil { + select { + case _, ok := <-kept: + if !ok { + kept = nil + continue + } + b.pub.publishKept("a kept event") + case _, ok := <-check: + if !ok { + check = nil + continue + } + b.names.give("a changed check address") + } } } @@ -84,4 +115,5 @@ func (b *bundle) heartbeat(hb stdio.Heartbeat) { // reconnected is the work at the bus connection's return. func (b *bundle) reconnected() { b.pub.publishKept("the bus connection's return") + b.names.give("the bus connection's return") } diff --git a/modules/networkmanager/cmd/uplink-tools/dbus.go b/modules/networkmanager/cmd/uplink-tools/dbus.go index 7a0783c7d..88fbe5562 100644 --- a/modules/networkmanager/cmd/uplink-tools/dbus.go +++ b/modules/networkmanager/cmd/uplink-tools/dbus.go @@ -290,6 +290,16 @@ func userEnvironment(bus string) ([]string, error) { // fileEvents fires at each file event of the kernel that makes a file in one of dirs (a burst is one), until stop // closes. A folder that is not there is not watched, and said. func fileEvents(dirs []string, stop <-chan struct{}) <-chan struct{} { + return watchFiles(dirs, nil, stop) +} + +// fileMade fires at each file event of the kernel that makes name in dir, until stop closes. +func fileMade(dir, name string, stop <-chan struct{}) <-chan struct{} { + return watchFiles([]string{dir}, func(made string) bool { return made == name }, stop) +} + +// watchFiles fires at each file event that makes a file in dirs whose name match takes (any, when nil). +func watchFiles(dirs []string, match func(string) bool, stop <-chan struct{}) <-chan struct{} { out := make(chan struct{}, 1) fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK) if err != nil { @@ -309,9 +319,17 @@ func fileEvents(dirs []string, stop <-chan struct{}) <-chan struct{} { go func() { defer f.Close() for { - if _, err := readInotify(f); err != nil { + names, err := readInotify(f) + if err != nil { return } + matched := match == nil + for _, name := range names { + matched = matched || match(name) + } + if !matched { + continue + } select { case out <- struct{}{}: default: diff --git a/modules/networkmanager/cmd/uplink-tools/names.go b/modules/networkmanager/cmd/uplink-tools/names.go new file mode 100644 index 000000000..01fd76953 --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/names.go @@ -0,0 +1,150 @@ +package main + +// The bundle gives the resolver the check's host name (design 08 sign-in-page §13): it calls +// `/node-resolver.uplink-names` with the check address's host name, or an empty list when there is no address, +// and writes the call to names.json. Which module holds node-resolver on its node it reads from the seat +// mesh-module-assigner's assignments; none is `no holder`, which is no failure (DQ-8). A failed call is written to the +// journal, stands failed in names.json and is asked again at the next moment, never on a timer. + +import ( + "encoding/json" + "fmt" + "net/url" + "strings" + "sync" + "time" + + "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +const ( + // runningPattern is node-engine's running event, as the manifest consumes it: node-engine.running... + runningPattern = "node-engine.running.*.*" + assignmentsKey = "seat:mesh-module-assigner.assignments" +) + +// namer calls uplink-names, one call at a time. +type namer struct { + folder Folder + // ask calls a verb on the mesh as the module, through the runtime. + ask func(key string, body any) (json.RawMessage, error) + now func() time.Time + + mu sync.Mutex +} + +// hostOf is the check address's host name, lower-cased, as uplink-names takes it: none for an empty address. +func hostOf(address string) ([]string, error) { + if address == "" { + return []string{}, nil + } + u, err := url.Parse(address) + if err != nil || u.Hostname() == "" { + return nil, fmt.Errorf("the check address %q names no host", address) + } + return []string{strings.ToLower(u.Hostname())}, nil +} + +// resolverHolder is the module that holds node-resolver on this node, "" when none does (DQ-8). +func (n *namer) resolverHolder() (string, error) { + raw, err := n.ask(assignmentsKey, map[string]string{"node": n.folder.Node, "seats": "true"}) + if err != nil { + return "", fmt.Errorf("reading the assignments: %w", err) + } + var answer struct { + Seats []struct { + Seat string `json:"seat"` + Holders []struct { + Module string `json:"module"` + Node string `json:"node"` + } `json:"holders"` + } `json:"seats"` + } + if err := json.Unmarshal(raw, &answer); err != nil { + return "", fmt.Errorf("the assignments answered in no form this reads: %w", err) + } + for _, s := range answer.Seats { + if s.Seat != noderesolver.Seat { + continue + } + for _, h := range s.Holders { + if h.Node == n.folder.Node { + return h.Module, nil + } + } + } + return "", nil +} + +// give calls uplink-names now with the check address's host name and writes the call to names.json. +func (n *namer) give(moment string) NamesCall { + n.mu.Lock() + defer n.mu.Unlock() + call := NamesCall{Names: []string{}, At: n.now()} + address, err := n.folder.checkAddress() + if err == nil { + call.Names, err = hostOf(address) + } + if err == nil { + call.Result, err = n.call(call.Names) + } + if err != nil { + call.Result = err.Error() + } + if call.Names == nil { + call.Names = []string{} + } + if err := n.folder.writeNames(call); err != nil { + journal("writing names.json failed: %v", err) + } + journal("uplink-names %v at %s: %s", call.Names, moment, call.Result) + return call +} + +func (n *namer) call(names []string) (string, error) { + holder, err := n.resolverHolder() + if err != nil { + return "", err + } + if holder == "" { + return resultNoHolder, nil + } + raw, err := n.ask(uplinkNamesKey(n.folder.Node), noderesolver.UplinkNamesInput{Names: names}) + if err != nil { + return "", fmt.Errorf("%s/%s.%s: %w", n.folder.Node, noderesolver.Seat, noderesolver.VerbUplinkNames, err) + } + if _, _, err := noderesolver.DecodeUplinkNamesAnswer(raw); err != nil { + return "", fmt.Errorf("uplink-names answered in no form this reads: %w", err) + } + return resultOK, nil +} + +func uplinkNamesKey(node string) string { + return "seat:" + noderesolver.Seat + "." + noderesolver.VerbUplinkNames + "@" + node +} + +// running hears node-engine's running: one for this node's holder of node-resolver, running, is a moment to call +// uplink-names; every other is dropped, another node's first (§13). +func (n *namer) running(e stdio.Envelope) error { + parts := strings.Split(e.Key, ".") + if len(parts) != 4 || parts[2] != n.folder.Node { + return nil + } + var body struct { + Module string `json:"module"` + Stopped bool `json:"stopped"` + } + if err := json.Unmarshal(e.Body, &body); err != nil || body.Stopped { + return nil + } + holder, err := n.resolverHolder() + if err != nil { + journal("hearing %s: %v", e.Key, err) + return nil + } + if holder != "" && holder == parts[3] { + n.give(holder + " running") + } + return nil +} diff --git a/modules/networkmanager/cmd/uplink-tools/names_test.go b/modules/networkmanager/cmd/uplink-tools/names_test.go new file mode 100644 index 000000000..db58b8952 --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/names_test.go @@ -0,0 +1,139 @@ +package main + +import ( + "encoding/json" + "errors" + "strings" + "sync" + "testing" + "time" + + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// The mesh stood in for the bundle's asks: the assignments naming this node's holder of node-resolver (none when +// holder is empty), and node-resolver's uplink-names, failing while fail is set. +type fakeMesh struct { + mu sync.Mutex + holder string + fail error + calls []string + routes string +} + +func (m *fakeMesh) ask(key string, body any) (json.RawMessage, error) { + m.mu.Lock() + defer m.mu.Unlock() + switch key { + case assignmentsKey: + in := body.(map[string]string) + if in["node"] != "laptop" || in["seats"] != "true" { + return nil, errors.New("asked the assignments of another node") + } + seats := `{"seats":[{"seat":"node-uplink","holders":[{"module":"networkmanager","node":"laptop"}]}` + if m.holder != "" { + seats += `,{"seat":"node-resolver","holders":[{"module":"` + m.holder + `","node":"laptop"}]}` + } + return json.RawMessage(seats + `]}`), nil + case "seat:node-resolver.uplink-names@laptop": + raw, _ := json.Marshal(body) + m.calls = append(m.calls, string(raw)) + if m.fail != nil { + return nil, m.fail + } + return json.RawMessage(`{"names":["check.example.net"],"link":"wlan0","set_at":"2026-11-24T19:00:00Z"}`), nil + case "seat:node-resolver.routes@laptop": + if m.routes == "" { + return nil, errors.New("no routes") + } + return json.RawMessage(m.routes), nil + } + return nil, errors.New("no such verb " + key) +} + +func (m *fakeMesh) called() []string { + m.mu.Lock() + defer m.mu.Unlock() + return append([]string(nil), m.calls...) +} + +// uplink-names is called at the bundle's start, on a changed check-address, when the bus connection comes back and on +// a running holder of node-resolver on its own node; every other running is dropped; no holder is recorded and is +// healthy, a failed call stands failed (design 08 sign-in-page §13, §25). +func TestUplinkNamesIsCalledAtEachMoment(t *testing.T) { + lines := journalLines(t) + f := aFolder(t) + if err := f.writeCheckAddress("http://Check.Example.net/connecttest"); err != nil { + t.Fatal(err) + } + mesh := &fakeMesh{holder: "systemd-resolved"} + b := &bundle{folder: f, pub: &publisher{folder: f, publish: (&fakeStream{}).publish}, + names: &namer{folder: f, ask: mesh.ask, now: time.Now}} + check := make(chan struct{}) + var running func(stdio.Envelope) error + var hook func(stdio.Heartbeat) + done := make(chan struct{}) + go func() { + b.start(bundleRuns{files: func([]string, <-chan struct{}) <-chan struct{} { return nil }, + fileMade: func(dir, name string, _ <-chan struct{}) <-chan struct{} { + if dir != f.Dir || name != "check-address" { + t.Errorf("watched %s in %s", name, dir) + } + return check + }, + onHeartbeat: func(h func(stdio.Heartbeat)) error { hook = h; return nil }, + subscribe: func(pattern string, h func(stdio.Envelope) error) error { + if pattern != "node-engine.running.*.*" { + t.Errorf("subscribed to %s", pattern) + } + running = h + return nil + }}) + close(done) + }() + if err := f.writeCheckAddress(""); err != nil { + t.Fatal(err) + } + check <- struct{}{} + f.writeCheckAddress("https://other.example.net/") + close(check) + <-done + want := []string{`{"names":["check.example.net"]}`, `{"names":[]}`} + if got := mesh.called(); strings.Join(got, " ") != strings.Join(want, " ") { + t.Fatalf("at the start and a changed check address: called %q", got) + } + hook(stdio.Heartbeat{Missed: true}) + hook(stdio.Heartbeat{}) + for _, e := range []stdio.Envelope{ + {Key: "node-engine.running.other.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved"}`)}, + {Key: "node-engine.running.laptop.i3", Body: json.RawMessage(`{"module":"i3"}`)}, + {Key: "node-engine.running.laptop.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved","stopped":true}`)}, + {Key: "node-engine.running.laptop.systemd-resolved", Body: json.RawMessage(`{"module":"systemd-resolved","healthy":true}`)}, + } { + if err := running(e); err != nil { + t.Fatal(err) + } + } + want = append(want, `{"names":["other.example.net"]}`, `{"names":["other.example.net"]}`) + if got := mesh.called(); strings.Join(got, " ") != strings.Join(want, " ") { + t.Fatalf("at the connection's return and a running holder: called %q", got) + } + if c, _ := f.names(); c == nil || c.Result != resultOK || strings.Join(c.Names, " ") != "other.example.net" { + t.Fatalf("names.json holds %+v", c) + } + + mesh.holder = "" + if c := b.names.give("a test"); c.Result != resultNoHolder || c.Failed() || len(mesh.called()) != 4 { + t.Fatalf("no holder gave %+v after %d calls", c, len(mesh.called())) + } + mesh.holder, mesh.fail = "systemd-resolved", errors.New("no answer within its bound") + if c := b.names.give("a test"); !c.Failed() || !strings.Contains(c.Result, "no answer within its bound") { + t.Fatalf("a failed call gave %+v", c) + } + if c, _ := f.names(); c == nil || !c.Failed() { + t.Fatalf("names.json holds %+v", c) + } + if !strings.Contains(strings.Join(lines(), "\n"), "uplink-names [other.example.net] at a test: laptop/node-resolver.uplink-names: no answer") { + t.Fatalf("the failed call is not in the journal: %q", lines()) + } +} diff --git a/modules/networkmanager/cmd/uplink-tools/publish_test.go b/modules/networkmanager/cmd/uplink-tools/publish_test.go index d27cf8b28..0ee310c49 100644 --- a/modules/networkmanager/cmd/uplink-tools/publish_test.go +++ b/modules/networkmanager/cmd/uplink-tools/publish_test.go @@ -84,17 +84,20 @@ func TestTheBundlePublishesAtEachMoment(t *testing.T) { f := aFolder(t) keepThree(t, f) stream := &fakeStream{refuse: map[string]bool{"laptop.sign-in-ended.1": true}} - b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish}} + b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish}, names: &namer{folder: f, now: time.Now, + ask: func(string, any) (json.RawMessage, error) { return json.RawMessage(`{"seats":[]}`), nil }}} files := make(chan struct{}) var hook func(stdio.Heartbeat) done := make(chan struct{}) go func() { - b.start(func(dirs []string, _ <-chan struct{}) <-chan struct{} { + b.start(bundleRuns{files: func(dirs []string, _ <-chan struct{}) <-chan struct{} { if len(dirs) != 1 || !strings.HasSuffix(dirs[0], "/pending") { t.Errorf("watched %v", dirs) } return files - }, func(h func(stdio.Heartbeat)) error { hook = h; return nil }) + }, fileMade: func(string, string, <-chan struct{}) <-chan struct{} { return nil }, + onHeartbeat: func(h func(stdio.Heartbeat)) error { hook = h; return nil }, + subscribe: func(string, func(stdio.Envelope) error) error { return nil }}) close(done) }() files <- struct{}{} // the start's round ran and was refused; this one publishes what waits diff --git a/modules/networkmanager/go.mod b/modules/networkmanager/go.mod index ff59b309b..bdcbd251e 100644 --- a/modules/networkmanager/go.mod +++ b/modules/networkmanager/go.mod @@ -3,6 +3,7 @@ module networkmanager go 1.26.0 require ( + git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 github.com/godbus/dbus/v5 v5.1.0