Răsfoiți Sursa

fix(topology): keep a portless wired client on its switch/NAS, not the gateway (#1534)

Closes #1533.

hubOf no longer force-anchors a wired client with no FDB port to the gateway when its RouterID resolves to a managed switch or an agent-only NAS (RoleBadge SW): those units report their downstream clients first-party, so the attribution is honored and the chip placement stays coherent with the unit's own client count. The gateway fallback remains for empty or gateway attribution and for wireless APs (rule 2-Aug-2026), and the frontend hub fallback mirrors the same guard. Golden canon unchanged: mac-mini and hue-hub still anchor to the gateway.

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Bann-DEV 1 lună în urmă
părinte
comite
ec2cc1c1de

+ 13 - 1
app/src/components/topology/model.ts

@@ -837,7 +837,19 @@ export function buildTopologyModel({ routers, devices, wan, wireguard, distribut
     if (d.attachTo && (routerById.has(d.attachTo) || distById.has(d.attachTo) || deviceById.has(d.attachTo))) {
       return d.attachTo
     }
-    if (isWired(d) && !d.port && gatewayNode) return gatewayNode.id
+    // A wired client with no FDB port is force-anchored to the gateway unless
+    // its routerId resolves to a managed switch. Attribution to a wireless AP
+    // is not trustworthy without port evidence (rule 2-Ago-2026), but a
+    // switch/NAS reports its own downstream clients first-party (e.g. a Docker
+    // container behind the NAS, portless because its veth is in the port
+    // denylist); honoring that routerId keeps the chip coherent with the unit's
+    // client count. Mirrors the hubOf guard in server topo_semantics.go.
+    if (
+      isWired(d) && !d.port && gatewayNode &&
+      routerById.get(d.routerId)?.router.roleBadge !== 'SW'
+    ) {
+      return gatewayNode.id
+    }
     return d.routerId
   }
   // SPEC-65 D65-3/D65-9 B2: con semántica server-side, los anillos llegan

+ 22 - 1
server-go/internal/adapters/topo_semantics.go

@@ -429,6 +429,19 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 	for _, r := range routerNodes {
 		inRouter[r.ID] = true
 	}
+	// switchRouter: managed switch units (RoleBadge "SW"), e.g. a fleet switch
+	// or an agent-only NAS. A wired client with no FDB port attributed to an AP
+	// is not trustworthy (the AP bridge learns MACs that merely transit its
+	// uplink), so rule 2-Ago-2026 anchors it to the gateway. But a switch/NAS
+	// reports its own downstream clients first-party (e.g. a Docker container
+	// behind the NAS, portless because its veth is in the denylist), so its
+	// RouterID is honored to stay coherent with the unit's client count.
+	switchRouter := map[string]bool{}
+	for _, r := range routerNodes {
+		if r.RoleBadge == "SW" {
+			switchRouter[r.ID] = true
+		}
+	}
 
 	// D1: los Devices cuya MAC es la chassis-MAC de un distnode managed se
 	// representan SOLO como nodo managed (sin chip ni enlaces propios).
@@ -628,7 +641,15 @@ func BuildTopoSemantics(routers []Router, devices []Device, wg WireGuardStats, d
 				return d.AttachTo
 			}
 		}
-		if d.Band == "cable" && d.Port == "" {
+		// A wired client with no FDB port is force-anchored to the gateway unless
+		// its RouterID resolves to a managed switch. Attribution to a wireless AP
+		// is not trustworthy without port evidence (the AP bridge learns MACs
+		// that merely transit its uplink — rule 2-Ago-2026), but a switch/NAS
+		// reports its own downstream clients first-party (e.g. a Docker container
+		// behind the NAS, portless because its veth is in the port denylist);
+		// honoring that RouterID keeps the chip coherent with the unit's client
+		// count. Mirrors the model.ts hubOf guard.
+		if d.Band == "cable" && d.Port == "" && !switchRouter[d.RouterID] {
 			return gateway.ID
 		}
 		return d.RouterID

+ 35 - 0
server-go/internal/adapters/topo_semantics_test.go

@@ -229,6 +229,41 @@ func TestTopoSemanticsWanPeer(t *testing.T) {
 	}
 }
 
+// A wired client with no FDB port keeps its real (non-gateway) attribution:
+// a Docker container behind the NAS reports Band=cable with an empty Port
+// (its veth lands in the port denylist), but RouterID already resolved it to
+// the NAS. hubOf must honor that RouterID instead of force-anchoring it to
+// the gateway, so it lands in the NAS ring and matches the unit's client
+// count. Mirrors the model.ts hubOf guard.
+func TestTopoSemanticsWiredNoPortHonorsNonGatewayRouter(t *testing.T) {
+	routers := []Router{
+		{ID: "gw", Name: "gateway", RoleBadge: "Principal", MAC: "AA:BB:CC:00:00:01"},
+		{ID: "nas", Name: "nas", RoleBadge: "SW", MAC: "AA:BB:CC:00:00:02"},
+	}
+	devices := []Device{
+		// The NAS itself, seen on a gateway port (so it is a real switch node).
+		{ID: "dev-nas", MAC: "AA:BB:CC:00:00:02", RouterID: "gw", Band: "cable", Online: true, Port: "3"},
+		// Docker container behind the NAS: cable, no port, attributed to nas.
+		{ID: "docker", MAC: "02:42:AC:11:00:03", RouterID: "nas", Band: "cable", Online: true},
+	}
+	sem := BuildTopoSemantics(routers, devices, WireGuardStats{}, nil, "", nil, nil, nil)
+
+	for _, id := range sem.Rings["gw"] {
+		if id == "docker" {
+			t.Fatal("the container must not be anchored to the gateway ring")
+		}
+	}
+	found := false
+	for _, id := range sem.Rings["nas"] {
+		if id == "docker" {
+			found = true
+		}
+	}
+	if !found {
+		t.Fatalf("the container must be in the NAS ring: rings=%+v", sem.Rings)
+	}
+}
+
 // #1047: la MAC de una unidad de flota aprendida en el FDB de otro miembro
 // ancla su uplink al padre real (ap1 cuelga de sw1, no del gateway), aunque
 // no haya LLDP. La unidad deja de duplicarse como chip de cliente.