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

fix(topology): claim wired clients learned on fleet-switch access ports (#1417)

Switches polled as SNMP externals (Type external + snmpEnabled, the
#1137 setup) were invisible to every client-placement gate: inferLldpLinks
treats their LLDP neighbor as a router-router uplink and marks the port
handled without attaching anyone, and the #1325 leaf pass filters on
Type == managed-switch. Their wired clients kept no AttachTo and the map
anchored them to the gateway.

claimSwitchFdbClients lets each fleet switch (same disjunct as the
buildRouter Switch role, now shared via cfgIsSwitch) claim gateway-
attributed wired clients its own FDB learned on a non-root port; the
deepest switch wins, #1325 leaf nodes get filled instead of bypassed,
and device-hub or foreign-distnode attaches are preserved. Root-port
guard keeps the #1374 case intact: upstream clients learned on the
switch's root port (dumb-switch gear next to its uplink) stay put.
Nacho 1 месяц назад
Родитель
Сommit
153cd5b1c3

+ 6 - 1
server-go/internal/adapters/live.go

@@ -1449,7 +1449,7 @@ func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 		r.Role, r.RoleBadge = "Gateway principal", "Principal"
 	} else if p.cfg.Type == "routeros" {
 		r.Role, r.RoleBadge = "Router", "Router"
-	} else if p.cfg.AgentOnly || p.cfg.Type == "managed-switch" || (p.cfg.Type == "external" && p.cfg.SnmpEnabled) {
+	} else if cfgIsSwitch(p.cfg) {
 		// external sondeado por SNMP = switch en la práctica (#661); evita el
 		// fallback AP para los switches SNMP de crowedavid (#1137).
 		r.Role, r.RoleBadge = "Switch", "SW"
@@ -3540,6 +3540,11 @@ func (l *Live) buildOverview(ctx context.Context) (*Overview, error) {
 	unread := l.engine.UnreadCount()
 	usteerAvailable := l.usteerAvailableCached()
 	dawnDetected := dawnDeprecatedFromPolled(polled)
+	// #1417: los clientes cableados que la FDB de un switch de flota aprendió
+	// en un puerto no-raíz cuelgan físicamente de él (switches SNMP externos
+	// de #1137 incluidos). Antes de la semántica: los blobs del gateway que
+	// queden vacíos los absorbe después #1374 sin dejar huérfanos.
+	claimSwitchFdbClients(polled, devices, distNodes, gwID, fdbEvidence, lldpEvidence, overrideUplinks)
 	topo := BuildTopoSemantics(routerList, devices, wgStats, distNodes, wan.Gateway, fdbEvidence, lldpEvidence, overrideUplinks) // SPEC-65 D65-3 + #1042/#1051/#1279/#1321
 	// #1374: retirar de la lista que ve el frontend los distnodes absorbidos
 	// como sombra de un switch gestionado. Slice NUEVO: filtrar in situ

+ 161 - 0
server-go/internal/adapters/topology.go

@@ -244,6 +244,167 @@ func inferLldpLinks(polled map[string]*routerPolled, devices []Device, byMAC map
 	return dists, handled
 }
 
+// cfgIsSwitch: el disyuntivo de rol "Switch" de buildRouter (#661/#1137):
+// agent-only, managed-switch, o external sondeado por SNMP. La pasada de
+// claim (#1417) usa el mismo criterio para decidir qué unidades de flota
+// pueden reclamar clientes con su FDB.
+func cfgIsSwitch(c RouterConfig) bool {
+	return c.AgentOnly || c.Type == "managed-switch" || (c.Type == "external" && c.SnmpEnabled)
+}
+
+// claimSwitchFdbClients (#1417): los clientes cableados atribuidos al gateway
+// que la FDB de un switch de flota aprendió en un puerto que NO es su root
+// port están físicamente detrás de ese switch, aunque la única atribución
+// DHCP disponible sea la del gateway (los switches SNMP no dan DHCP ni LLDP
+// útil: para la atribución son clientes del gateway; ver leafSwitchClients).
+//
+// Reglas:
+//   - Root port del switch = el puerto donde su FDB aprende la bridge-MAC del
+//     gateway (#1373). Sin esa MAC en su FDB el switch no reclama nada: sin
+//     root port no se distingue arriba de abajo.
+//   - Si varios switches reclaman la misma MAC (el trunk de sw1 ve también a
+//     los clientes de sw2), gana el más profundo según la evidencia de uplink
+//     (override > lldp > fdb, la precedencia de BuildTopoSemantics); empate
+//     de profundidad → id menor (determinista).
+//   - Un puerto del switch con hoja inferida #1325 ya creada recibe los
+//     attach de sus clientes (la hoja se llena; no se rodea).
+//   - Solo se re-anclan devices atribuidos al gateway con banda cable/"—"
+//     cuyo attach esté vacío o apunte a un distnode del gateway (el blob
+//     sombra #1374): un attach a un device-hub (hipervisor) o a un distnode
+//     de otro router no se toca. Los blobs del gateway que queden vacíos los
+//     absorbe después la pasada #1374 sin dejar huérfanos.
+//
+// Mutates devices; dists solo se lee.
+func claimSwitchFdbClients(polled map[string]*routerPolled, devices []Device, dists []DistributionNode, gatewayID string, fdbEv, lldpEv, overrideEv map[string]topoParent) {
+	if gatewayID == "" {
+		return
+	}
+	gw := polled[gatewayID]
+	if gw == nil || gw.brMac == "" {
+		return
+	}
+	gwMac := strings.ToUpper(gw.brMac)
+	routerMACs := map[string]bool{}
+	ids := make([]string, 0, len(polled))
+	for id, p := range polled {
+		ids = append(ids, id)
+		if p.brMac != "" {
+			routerMACs[strings.ToUpper(p.brMac)] = true
+		}
+	}
+	sort.Strings(ids)
+
+	// Uplinks por precedencia (override > lldp > fdb) y profundidad desde el
+	// gateway para el desempate entre switches.
+	up := map[string]topoParent{}
+	for rid, ev := range fdbEv {
+		if rid != gatewayID {
+			up[rid] = ev
+		}
+	}
+	for rid, ev := range lldpEv {
+		if rid != gatewayID {
+			up[rid] = ev // el LLDP pisa la FDB (#1279)
+		}
+	}
+	for rid, ev := range overrideEv {
+		if rid != gatewayID && ev.parent != rid {
+			up[rid] = ev // el override manual lo pisa todo (#1321)
+		}
+	}
+	depth := map[string]int{gatewayID: 0}
+	for iter := 0; iter <= len(ids); iter++ {
+		changed := false
+		for _, rid := range ids {
+			if rid == gatewayID {
+				continue
+			}
+			ev, ok := up[rid]
+			if !ok {
+				continue
+			}
+			if d, seen := depth[ev.parent]; seen && depth[rid] < d+1 {
+				depth[rid] = d + 1
+				changed = true
+			}
+		}
+		if !changed {
+			break
+		}
+	}
+
+	// Reclamos por MAC: el switch más profundo gana.
+	type swClaim struct {
+		switchID string // switch que aprende la MAC (para portLabel)
+		target   string // id del switch o de su hoja #1325
+		port     string
+		depth    int
+	}
+	best := map[string]swClaim{}
+	for _, sid := range ids {
+		if sid == gatewayID {
+			continue
+		}
+		p := polled[sid]
+		if !cfgIsSwitch(p.cfg) || len(p.fdb) == 0 {
+			continue
+		}
+		root := p.fdb[gwMac]
+		if root == "" {
+			continue // sin root port conocido: no se distingue arriba de abajo
+		}
+		for mac, port := range p.fdb {
+			if port == root || mac == "" {
+				continue
+			}
+			if routerMACs[mac] {
+				continue
+			}
+			// Un puerto con hoja #1325 ya creada reclama para la hoja.
+			target := sid
+			for _, dn := range dists {
+				if dn.RouterID == sid && dn.Port == port && (dn.Kind == "inferred" || dn.Kind == "managed") {
+					target = dn.ID
+					break
+				}
+			}
+			c := swClaim{switchID: sid, target: target, port: port, depth: depth[sid]}
+			if prev, ok := best[mac]; ok {
+				if c.depth < prev.depth || (c.depth == prev.depth && target >= prev.target) {
+					continue
+				}
+			}
+			best[mac] = c
+		}
+	}
+	if len(best) == 0 {
+		return
+	}
+
+	// Aplicar: solo gateway-atribuidos cableados con attach vacío o a blob
+	// del gateway; device-hubs y distnodes ajenos intactos.
+	distRouter := map[string]string{}
+	for _, dn := range dists {
+		distRouter[dn.ID] = dn.RouterID
+	}
+	for i := range devices {
+		d := &devices[i]
+		if d.RouterID != gatewayID || (d.Band != "cable" && d.Band != "—") {
+			continue
+		}
+		if d.AttachTo != "" && distRouter[d.AttachTo] != gatewayID {
+			continue // device-hub, distnode de otro router o id desconocido
+		}
+		c, ok := best[strings.ToUpper(d.MAC)]
+		if !ok {
+			continue
+		}
+		d.AttachTo = c.target
+		d.Port = c.port
+		d.PortLabel = portLabel(polled[c.switchID], c.port)
+	}
+}
+
 // containsMAC: ¿está mac (case-sensitive, como el FDB) en la lista?
 func containsMAC(macs []string, mac string) bool {
 	for _, m := range macs {

+ 229 - 0
server-go/internal/adapters/topology_claim_test.go

@@ -0,0 +1,229 @@
+package adapters
+
+import "testing"
+
+// #1417: flota con switches gestionados sondeados por SNMP como "external"
+// (el montaje de #1137). El gateway los ve por LLDP en su puerto de uplink;
+// como su Type no es "managed-switch", inferLldpLinks trata el enlace como
+// router↔router y marca el puerto como resuelto SIN crear distnode ni anclar
+// a los clientes cableados del puerto. La inferencia de hojas #1325 tampoco
+// cubre estos switches (filtra Type=="managed-switch"). Resultado: los
+// clientes cableados del switch quedan sin AttachTo y el mapa los cuelga del
+// gateway. claimSwitchFdbClients reclama para cada switch de flota los
+// clientes cableados (atribuidos al gateway) que SU FDB aprendió en un
+// puerto que no es su root port.
+
+const (
+	claimGwMac   = "AA:BB:CC:00:00:01"
+	claimSw1Mac  = "22:22:22:22:22:01"
+	claimSw2Mac  = "22:22:22:22:22:02"
+	claimSw16Mac = "22:22:22:22:22:16"
+)
+
+func claimFlotaPolled() map[string]*routerPolled {
+	return map[string]*routerPolled{
+		"gateway": {
+			cfg:   RouterConfig{ID: "gateway", Type: "openwrt", IsGateway: true},
+			brMac: claimGwMac,
+			fdb: map[string]string{
+				"11:11:11:11:11:01": "lan1", // sonos1 (detrás de sw1)
+				"11:11:11:11:11:02": "lan1", // sonos2 (detrás de sw1)
+				"11:11:11:11:11:03": "lan1", // sonos3 (detrás de sw1)
+				"11:11:11:11:11:04": "lan1", // sonos4 (detrás de sw2)
+				"11:11:11:11:11:09": "lan4", // cableado directo del gateway
+				claimSw1Mac:         "lan1",
+				claimSw2Mac:         "lan1",
+			},
+			lldp: []LldpNeighbor{{Port: "lan1", ChassisMac: claimSw1Mac, Chassis: "sw1", Mgmt: "192.168.1.2"}},
+		},
+		"sw1": {
+			cfg:   RouterConfig{ID: "sw1", Type: "external", SnmpEnabled: true, Host: "192.168.1.2"},
+			brMac: claimSw1Mac,
+			fdb: map[string]string{
+				claimGwMac:          "Port 1", // root port (hacia el gateway)
+				"11:11:11:11:11:01": "Port 5",
+				"11:11:11:11:11:02": "Port 5",
+				"11:11:11:11:11:03": "Port 6",
+				"11:11:11:11:11:04": "Port 2", // trunk hacia sw2
+				claimSw2Mac:         "Port 2",
+			},
+		},
+		"sw2": {
+			cfg:   RouterConfig{ID: "sw2", Type: "external", SnmpEnabled: true, Host: "192.168.1.3"},
+			brMac: claimSw2Mac,
+			fdb: map[string]string{
+				claimGwMac:          "Port 1", // root port
+				"11:11:11:11:11:04": "Port 3", // acceso
+			},
+		},
+	}
+}
+
+// El caso de crowedavid: los Sonos cableados tras sw1/sw2 (externos SNMP)
+// deben quedar anclados al nodo del switch que los aprendió en puerto de
+// acceso, con el puerto físico del switch, y no colgar del gateway.
+func TestClaim1417SonosDetrasDeSwitchSNMP(t *testing.T) {
+	polled := claimFlotaPolled()
+	devices := []Device{
+		dev("11:11:11:11:11:01", "gateway", "cable"),
+		dev("11:11:11:11:11:02", "gateway", "cable"),
+		dev("11:11:11:11:11:03", "gateway", "cable"),
+		dev("11:11:11:11:11:04", "gateway", "cable"),
+		dev("11:11:11:11:11:09", "gateway", "cable"),
+	}
+	devices, dists := inferTopology(polled, devices)
+	for i, d := range devices[:4] {
+		if d.AttachTo != "" {
+			t.Fatalf("prepara: sonos %d ya anclado a %q (fixture sin el bug)", i, d.AttachTo)
+		}
+	}
+	if len(dists) != 0 {
+		t.Fatalf("prepara: distnodes inesperados: %+v", dists)
+	}
+	gwID := "gateway"
+	claimSwitchFdbClients(polled, devices, dists, gwID,
+		fleetFdbEvidence(polled, gwID), fleetLldpEvidence(polled, gwID), nil)
+
+	// sonos1/2: acceso de sw1 por "Port 5"; sonos3: "Port 6".
+	for i, wantPort := range []string{"Port 5", "Port 5", "Port 6"} {
+		if devices[i].AttachTo != "sw1" || devices[i].Port != wantPort {
+			t.Fatalf("sonos%d: got (%q, %q), want (sw1, %q)", i+1, devices[i].AttachTo, devices[i].Port, wantPort)
+		}
+	}
+	// sonos4 lo ven sw1 (trunk) y sw2 (acceso): gana el más profundo.
+	if devices[3].AttachTo != "sw2" || devices[3].Port != "Port 3" {
+		t.Fatalf("sonos4: got (%q, %q), want (sw2, Port 3)", devices[3].AttachTo, devices[3].Port)
+	}
+	// El cableado directo del gateway no se toca.
+	if devices[4].AttachTo != "" || devices[4].Port != "lan4" {
+		t.Fatalf("lan4: got (%q, %q), want (\"\", lan4)", devices[4].AttachTo, devices[4].Port)
+	}
+}
+
+// El caso de casa (#1374): el uplink de switch16 y PC/Marantz (switch tonto)
+// comparten el puerto lan1 del gateway. La FDB de switch16 ve a PC/Marantz en
+// lan7, su ROOT port (tráfico upstream): no debe reclamarlos. Un equipo en un
+// puerto de acceso (beelink, lan5) sí se reclama aunque hoy cuelgue del blob
+// inferido del gateway.
+func TestClaim1417RootPortNoReclama(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"flint2": {
+			cfg:   RouterConfig{ID: "flint2", Type: "glinet", IsGateway: true},
+			brMac: claimGwMac,
+			fdb: map[string]string{
+				"11:11:11:11:11:01": "lan1", // PC (switch tonto)
+				"11:11:11:11:11:02": "lan1", // marantz
+				"11:11:11:11:11:05": "lan1", // beelink (detrás de switch16)
+				claimSw16Mac:        "lan1",
+			},
+		},
+		"switch16": {
+			cfg:   RouterConfig{ID: "switch16", Type: "managed-switch", AgentOnly: true},
+			brMac: claimSw16Mac,
+			fdb: map[string]string{
+				claimGwMac:          "lan7", // root port de switch16
+				"11:11:11:11:11:01": "lan7", // PC: upstream, NO reclamar
+				"11:11:11:11:11:02": "lan7", // marantz: upstream, NO reclamar
+				"11:11:11:11:11:05": "lan5", // beelink: acceso, SÍ
+			},
+		},
+	}
+	devices := []Device{
+		dev("11:11:11:11:11:01", "flint2", "cable"),
+		dev("11:11:11:11:11:02", "flint2", "cable"),
+		dev("11:11:11:11:11:05", "flint2", "cable"),
+	}
+	devices, dists := inferTopology(polled, devices)
+	before := []string{devices[0].AttachTo, devices[1].AttachTo}
+	gwID := "flint2"
+	claimSwitchFdbClients(polled, devices, dists, gwID,
+		fleetFdbEvidence(polled, gwID), fleetLldpEvidence(polled, gwID), nil)
+
+	if devices[0].AttachTo != before[0] || devices[1].AttachTo != before[1] {
+		t.Fatalf("PC/Marantz (root port upstream) no deben moverse: antes (%q, %q), después (%q, %q)",
+			before[0], before[1], devices[0].AttachTo, devices[1].AttachTo)
+	}
+	if devices[2].AttachTo != "switch16" || devices[2].Port != "lan5" {
+		t.Fatalf("beelink: got (%q, %q), want (switch16, lan5)", devices[2].AttachTo, devices[2].Port)
+	}
+}
+
+// Guardas del reclamo: un cliente wifi (aunque su RouterID sea el gateway) no
+// se reclama; un device ya colgado de un device-hub (attachTo que no es
+// distnode) se conserva; un switch sin la MAC del gateway en su FDB (root
+// port desconocido) no reclama nada.
+func TestClaim1417Guardas(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"gw": {
+			cfg:   RouterConfig{ID: "gw", Type: "openwrt", IsGateway: true},
+			brMac: claimGwMac,
+		},
+		"sw1": {
+			cfg:   RouterConfig{ID: "sw1", Type: "external", SnmpEnabled: true},
+			brMac: claimSw1Mac,
+			fdb: map[string]string{
+				// SIN la MAC del gateway: root port desconocido → no reclama.
+				"11:11:11:11:11:01": "Port 5",
+				"11:11:11:11:11:02": "Port 5",
+				"11:11:11:11:11:03": "Port 5",
+			},
+		},
+	}
+	devices := []Device{
+		dev("11:11:11:11:11:01", "gw", "cable"),
+		dev("11:11:11:11:11:02", "gw", "5GHz"),
+		{ID: "d-hub", MAC: "11:11:11:11:11:03", RouterID: "gw", Band: "cable", Online: true, AttachTo: "d-citadel-01"},
+	}
+	dists := []DistributionNode{}
+	gwID := "gw"
+	claimSwitchFdbClients(polled, devices, dists, gwID,
+		fleetFdbEvidence(polled, gwID), fleetLldpEvidence(polled, gwID), nil)
+
+	if devices[0].AttachTo != "" || devices[1].AttachTo != "" {
+		t.Fatalf("sin root port no hay reclamo: got (%q, %q)", devices[0].AttachTo, devices[1].AttachTo)
+	}
+	if devices[2].AttachTo != "d-citadel-01" {
+		t.Fatalf("el attach al device-hub debe conservarse, got %q", devices[2].AttachTo)
+	}
+}
+
+// Un puerto de sw1 con hoja inferida #1325 ya creada (switch tonto con >=2
+// clientes bajo un switch Type "managed-switch"): el claim llena la hoja
+// (attach al distnode) en vez de rodearla colgando los clientes del switch.
+func TestClaim1417LlenaHoja1325(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"gw": {
+			cfg:   RouterConfig{ID: "gw", Type: "openwrt", IsGateway: true},
+			brMac: claimGwMac,
+			fdb: map[string]string{
+				"11:11:11:11:11:01": "lan1", // tras el switch tonto de sw1
+				"11:11:11:11:11:02": "lan1",
+				claimSw1Mac:         "lan1",
+			},
+		},
+		"sw1": {
+			cfg:   RouterConfig{ID: "sw1", Type: "managed-switch"},
+			brMac: claimSw1Mac,
+			fdb: map[string]string{
+				claimGwMac:          "Port 1",
+				"11:11:11:11:11:01": "Port 5", // switch tonto (hoja #1325)
+				"11:11:11:11:11:02": "Port 5",
+			},
+		},
+	}
+	devices := []Device{
+		dev("11:11:11:11:11:01", "gw", "cable"),
+		dev("11:11:11:11:11:02", "gw", "cable"),
+	}
+	hoja := DistributionNode{ID: "dist-sw1-Port 5", Kind: "inferred", RouterID: "sw1", Port: "Port 5", MacCount: 2}
+	dists := []DistributionNode{hoja}
+	gwID := "gw"
+	claimSwitchFdbClients(polled, devices, dists, gwID,
+		fleetFdbEvidence(polled, gwID), fleetLldpEvidence(polled, gwID), nil)
+
+	for i, d := range devices {
+		if d.AttachTo != "dist-sw1-Port 5" {
+			t.Fatalf("device %d: got %q, want la hoja dist-sw1-Port 5", i, d.AttachTo)
+		}
+	}
+}