Просмотр исходного кода

fix(routerdetail): pick the fastest router-link as a switch/AP backhaul (#1555)

The detail backhaul chose the uplink by matching Detail == "enlace entre
routers" - a string that is never set server-side (it is a frontend i18n
label) - so it always fell through to the first UP port. On a managed switch
that is an arbitrary access port, so a switch whose real uplink (1/xg27 ->
XS708T) negotiates 10 Gbps reported a camera port's 100 Mbps.

Choose the uplink among ports flagged as router links (PeerKind constants, not
loose literals), preferring the highest link speed: a switch has several
router-links (parent uplink plus AP downlinks) and the core uplink is the fast
one. The first-UP fallback is kept only when there is no router-link peer (a
single-cable AP without LLDP). The picker lives in its own file backhaul.go
with tests beside it; routerDetail keeps a single wiring call.

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Bann-DEV 1 месяц назад
Родитель
Сommit
d8234347d7

+ 37 - 0
server-go/internal/adapters/backhaul.go

@@ -0,0 +1,37 @@
+// backhaul.go — choosing the uplink port that carries a non-gateway unit's
+// backhaul toward the rest of the network (issue #1544).
+package adapters
+
+// pickBackhaulUplink returns the port that carries a non-gateway unit's uplink,
+// or nil when it has no up port. The uplink is a port the enrichment flagged as
+// a router link (PeerKind router-link / router-link-lldp). A managed switch has
+// several router-links - its parent uplink plus downlinks to mesh APs - so the
+// fastest one is the core uplink (e.g. 1/xg27 10G beats 1G AP downlinks). The
+// previous matcher looked for Detail == "enlace entre routers", a string that is
+// never set server-side (it is a frontend i18n label), so it always fell through
+// to the first UP port - which on a switch is an arbitrary access port, making a
+// 10G-uplinked switch report a camera port's 100 Mbps as its backhaul. The
+// first-UP fallback is kept only when there is no router-link peer at all (a
+// single-cable AP without LLDP).
+func pickBackhaulUplink(enriched []EthPort) *EthPort {
+	var uplink *EthPort
+	bestMbps := -1
+	for i := range enriched {
+		ep := &enriched[i]
+		if !ep.Up || (ep.PeerKind != PeerKindRouterLink && ep.PeerKind != PeerKindRouterLinkLLDP) {
+			continue
+		}
+		if mb := parseSpeedMbps(ep.Speed); mb > bestMbps {
+			bestMbps, uplink = mb, ep
+		}
+	}
+	if uplink != nil {
+		return uplink
+	}
+	for i := range enriched {
+		if enriched[i].Up {
+			return &enriched[i]
+		}
+	}
+	return nil
+}

+ 40 - 0
server-go/internal/adapters/backhaul_test.go

@@ -0,0 +1,40 @@
+package adapters
+
+import "testing"
+
+func TestPickBackhaulUplink(t *testing.T) {
+	// A managed switch with a 10G parent uplink (xg27) plus 1G AP downlinks and
+	// a 100M access port that happens to be first: the core uplink wins.
+	sw := []EthPort{
+		{ID: "snmp-1", Label: "1/g1", Up: true, Speed: "100 Mbps", PeerKind: PeerKindMAC},
+		{ID: "snmp-9", Label: "1/g9", Up: true, Speed: "1 Gbps", PeerKind: PeerKindRouterLinkLLDP},
+		{ID: "snmp-27", Label: "1/xg27", Up: true, Speed: "10 Gbps", PeerKind: PeerKindRouterLinkLLDP},
+	}
+	if got := pickBackhaulUplink(sw); got == nil || got.ID != "snmp-27" {
+		t.Fatalf("fastest router-link: %+v", got)
+	}
+
+	// Single router-link (an AP whose only wired peer is its parent): picked
+	// even though a plain access port is up and listed first.
+	ap := []EthPort{
+		{ID: "eth1", Label: "ETH 1", Up: true, Speed: "1 Gbps", PeerKind: PeerKindMAC},
+		{ID: "wan", Label: "WAN", Up: true, Speed: "2.5 Gbps", PeerKind: PeerKindRouterLink},
+	}
+	if got := pickBackhaulUplink(ap); got == nil || got.ID != "wan" {
+		t.Fatalf("router-link over access port: %+v", got)
+	}
+
+	// No router-link peer (single-cable AP without LLDP): fall back to first up.
+	noll := []EthPort{
+		{ID: "eth1", Label: "ETH 1", Up: false},
+		{ID: "eth2", Label: "ETH 2", Up: true, Speed: "1 Gbps", PeerKind: PeerKindMAC},
+	}
+	if got := pickBackhaulUplink(noll); got == nil || got.ID != "eth2" {
+		t.Fatalf("first-up fallback: %+v", got)
+	}
+
+	// Nothing up: nil.
+	if got := pickBackhaulUplink([]EthPort{{ID: "eth1", Up: false}}); got != nil {
+		t.Fatalf("no up port should be nil, got %+v", got)
+	}
+}

+ 2 - 15
server-go/internal/adapters/live.go

@@ -4407,21 +4407,8 @@ func (l *Live) GetRouterDetail(ctx context.Context, id string) (*RouterDetail, e
 		}
 	} else {
 		// Backhaul real del AP: boca que enlaza con otro router + latencia
-		var uplink *EthPort
-		for i := range enriched {
-			if enriched[i].Detail == "enlace entre routers" {
-				uplink = &enriched[i]
-				break
-			}
-		}
-		if uplink == nil {
-			for i := range enriched {
-				if enriched[i].Up {
-					uplink = &enriched[i]
-					break
-				}
-			}
-		}
+		// (la elección de boca vive en backhaul.go).
+		uplink := pickBackhaulUplink(enriched)
 		speed := "—"
 		if uplink != nil && uplink.Speed != "" {
 			speed = uplink.Speed