ソースを参照

fix(topology): resolve the fleet root port from the elected parent, not only the gateway MAC (#1683)

fleetFdbEvidence derived a candidate's root port exclusively from the
gateway's recorded bridge MAC (#1373). When the gateway does not present
that MAC on the wire (bond/macvlan, or a NIC whose MAC differs from the
detected bridge), no downstream FDB contains it, the root stays empty,
the guard silently no-ops and the depth relaxation hangs a switch off
whatever it learns on its own uplink.

Use the MAC of the candidate's elected parent as the primary source of
the root port (that unit's MAC is necessarily on the port facing it) and
keep the gateway MAC as fallback. Add a revocation pass: a parent
elected before its own root became known is dropped and re-resolved,
otherwise the wrong edge is locked in because re-election only accepts a
deeper candidate.

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Bann-DEV 1 ヶ月 前
親
コミット
e5eb36e96c

+ 46 - 5
server-go/internal/adapters/topo_semantics.go

@@ -118,6 +118,32 @@ func fleetFdbEvidence(polled map[string]*routerPolled, gatewayID string, registe
 		}
 	}
 	parent := map[string]topoParent{}
+	// rootPortOf: el puerto de sid que mira al núcleo. #1373 lo derivaba solo
+	// de la MAC bridge del gateway, y eso falla en flotas donde el gateway no
+	// presenta esa MAC en el cable (bond/macvlan/NIC con MAC distinta al bridge
+	// detectado): el FDB del switch nunca contiene esa MAC, el root queda vacío
+	// y la relajación por profundidad vuelve a cuelga un switch de lo que
+	// aprende EN su propio uplink (tránsito plano). La MAC del padre electo es
+	// la misma evidencia y SIEMPRE está disponible —sid aprende a su padre por
+	// ese puerto—, así que manda; la del gateway queda de fallback hasta que
+	// sid tiene padre, y después como segunda fuente.
+	rootPortOf := func(sid string) string {
+		lm := learned[sid]
+		if len(lm) == 0 {
+			return ""
+		}
+		if p, ok := parent[sid]; ok {
+			if m := idToMac[p.parent]; m != "" {
+				if port := lm[m]; port != "" {
+					return port
+				}
+			}
+		}
+		if gwMac := idToMac[gatewayID]; gwMac != "" {
+			return lm[gwMac]
+		}
+		return ""
+	}
 	isDescendant := func(rid, c string) bool {
 		cur := c
 		for i := 0; i <= len(ids); i++ {
@@ -134,6 +160,25 @@ func fleetFdbEvidence(polled map[string]*routerPolled, gatewayID string, registe
 	}
 	for iter := 0; iter <= len(ids); iter++ {
 		changed := false
+		// Revocación: el root de un candidato puede volverse conocido después
+		// de que ganó (su propio padre se elige en una pasada posterior). Sin
+		// esto el error queda clavado porque la reelección solo acepta algo
+		// MÁS profundo, y el padre falso ya dejó al hijo en la mayor
+		// profundidad del árbol.
+		for _, rid := range ids {
+			if rid == gatewayID {
+				continue
+			}
+			p, ok := parent[rid]
+			if !ok {
+				continue
+			}
+			if cRoot := rootPortOf(p.parent); cRoot != "" && cRoot == p.port {
+				delete(parent, rid)
+				depth[rid] = 0
+				changed = true
+			}
+		}
 		for _, rid := range ids {
 			if rid == gatewayID {
 				continue
@@ -155,11 +200,7 @@ func fleetFdbEvidence(polled map[string]*routerPolled, gatewayID string, registe
 				// la relajación por profundidad cuelga un switch gestionado de
 				// sus propios APs (p. ej. switch16 <- rt4 cuando la física es
 				// switch16 -> gateway por lan7).
-				cRoot := ""
-				if gwMac := idToMac[gatewayID]; gwMac != "" {
-					cRoot = learned[c.parent][gwMac]
-				}
-				if cRoot != "" && c.port == cRoot {
+				if cRoot := rootPortOf(c.parent); cRoot != "" && c.port == cRoot {
 					continue
 				}
 				if isDescendant(rid, c.parent) {

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

@@ -374,6 +374,51 @@ func TestFleetFdbEvidence(t *testing.T) {
 	}
 }
 
+// Cadena gw -> core -> mid -> edge con el NAS colgado de core. La MAC bridge
+// del gateway NO aparece en ningún FDB downstream (gateway con bond/macvlan:
+// la MAC del cable no es la que el bridge detectado reporta), así que el root
+// port de #1373 queda vacío y sin otra fuente la relajación por profundidad
+// cuelga el NAS de edge: edge lo aprende EN su propio uplink (tránsito plano
+// hacia core), no aguas abajo. El root derivado de la MAC del padre electo sí
+// lo detecta y descarta el candidato.
+func TestFleetFdbEvidenceGatewayMacInvisibleRootByParent(t *testing.T) {
+	const (
+		macGW   = "AA:BB:CC:00:00:01"
+		macCore = "AA:BB:CC:00:00:02"
+		macMid  = "AA:BB:CC:00:00:03"
+		macEdge = "AA:BB:CC:00:00:04"
+		macNAS  = "AA:BB:CC:00:00:05"
+	)
+	polled := map[string]*routerPolled{
+		"gw": {brMac: macGW, fdb: map[string]string{
+			macCore: "lan1", macMid: "lan1", macEdge: "lan1", macNAS: "lan1",
+		}},
+		"core": {brMac: macCore, fdb: map[string]string{
+			macMid: "xg2", macEdge: "xg2", macNAS: "xg4", // sin macGW: invisible
+		}},
+		"mid": {brMac: macMid, fdb: map[string]string{
+			macCore: "sfp1", macEdge: "sfp2", macNAS: "sfp1", // NAS por su uplink
+		}},
+		"edge": {brMac: macEdge, fdb: map[string]string{
+			// Su uplink Te7 ve TODO el núcleo (tránsito plano), incluido el
+			// propio core: eso es lo que hace derivable el root.
+			macCore: "Te7", macMid: "Te7", macNAS: "Te7",
+		}},
+		"nas": {brMac: macNAS, fdb: map[string]string{macCore: "wan"}},
+	}
+	ev := fleetFdbEvidence(polled, "gw", nil, nil)
+	if got := ev["mid"]; got.parent != "core" || got.port != "xg2" || got.childPort != "sfp1" {
+		t.Fatalf("mid: %+v, want core/xg2 con childPort sfp1", got)
+	}
+	if got := ev["edge"]; got.parent != "mid" || got.port != "sfp2" || got.childPort != "Te7" {
+		t.Fatalf("edge: %+v, want mid/sfp2 con childPort Te7", got)
+	}
+	got := ev["nas"]
+	if got.parent != "core" || got.port != "xg4" {
+		t.Fatalf("nas: %+v, want core/xg4 (edge lo aprende en su propio root port Te7)", got)
+	}
+}
+
 // #1186: el caso real del switch gestionado: el switch aprende la MAC del gw
 // (su uplink, puerto 7) y la de los routers colgados de él; el router aprende
 // la MAC del switch. El padre del router es el switch (NO el gateway, que es