Преглед изворни кода

fix(vlans): stop the port fallback from inventing a VLAN for Proxmox guests (#1757) (#1970)

Nacho пре 3 недеља
родитељ
комит
5a61152a44

+ 5 - 0
app/src/components/topology/model.ts

@@ -1107,6 +1107,11 @@ export function buildTopologyModel({ routers, devices, wan, wireguard, distribut
     // `tag=` que declara el hipervisor para el invitado. El puerto solo se usa
     // cuando no hay dato.
     if (d.vlan) return { id: d.vlan, name: vlanNameOf(d.vlan), pvid: false }
+    // #1757: si una fuente resolvió la VLAN y la respuesta fue "ninguna", el
+    // puerto no es un respaldo. El host de Proxmox y sus invitados sin tag=
+    // cuelgan de un trunk, y el PVID de ese puerto (o su primera VLAN) es la
+    // de otro cliente, no la suya.
+    if (d.vlanKnown) return undefined
     // #1757: `bridge vlan show` lista también las interfaces AP (wlan0,
     // wlan1) como puertos del bridge, así que la estación inalámbrica se
     // resuelve por su interfaz igual que un cliente cableado por su puerto.

+ 7 - 0
app/src/data/types.ts

@@ -313,6 +313,13 @@ export interface Device {
    * porque el invitado cuelga del bridge del host (#1757). Ausente = sin dato.
    */
   vlan?: number
+  /**
+   * Una fuente resolvió la VLAN de este dispositivo (el FDB del bridge o el
+   * tag= del hipervisor). Con `vlanKnown` y sin `vlan`, la respuesta es "no
+   * está en ninguna VLAN conocida" y el puerto no se usa como respaldo: en un
+   * trunk no dice nada (#1757).
+   */
+  vlanKnown?: boolean
   /** Segundos restantes del lease DHCP (issue #437). */
   leaseRemaining?: number | null
   /** Nombre del icono manual elegido por el usuario (issue #437). */

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

@@ -3861,6 +3861,7 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 		if p := polled[devices[i].RouterID]; p != nil {
 			if v := p.fdbVlans[strings.ToUpper(devices[i].MAC)]; v > 0 {
 				devices[i].Vlan = v
+				devices[i].VlanKnown = true
 			}
 		}
 	}

+ 2 - 0
server-go/internal/adapters/proxmox.go

@@ -364,6 +364,7 @@ func applyPVEInfra(devices []Device, dists []DistributionNode, inv *pveInventory
 		// router cree haber aprendido para esa MAC no es evidencia de dónde
 		// vive el invitado, y un 0 aquí la limpia en vez de conservarla.
 		devices[idx].Vlan = vm.Tag
+		devices[idx].VlanKnown = true
 		// El nombre del invitado es el que le puso el administrador en
 		// Proxmox, y para un CT suele ser el ÚNICO que hay: no pide DHCP
 		// con hostname, así que sin esto se queda con su MAC por nombre —
@@ -395,6 +396,7 @@ func applyPVEInfra(devices []Device, dists []DistributionNode, inv *pveInventory
 		// un trunk y su MAC puede aprenderse en cualquiera de las VLAN que
 		// transporta, así que ese dato no dice en cuál vive su gestión.
 		devices[idx].Vlan = 0
+		devices[idx].VlanKnown = true
 		if n, ok := nodeByKey[key]; ok && looksLikeMACName(devices[idx].Name) {
 			devices[idx].Name = n.Node
 		}

+ 17 - 0
server-go/internal/adapters/proxmox_test.go

@@ -79,6 +79,13 @@ func TestSealProxmoxInfraCarriesTheGuestVlan(t *testing.T) {
 	if devices[2].Vlan != 0 {
 		t.Errorf("lan vlan=%d (want 0: sin tag, VLAN nativa)", devices[2].Vlan)
 	}
+	// La config de Proxmox resolvio ambos: sin tag tambien es una respuesta
+	// (#1757), asi que la UI no debe caer al puerto del trunk.
+	for _, i := range []int{1, 2} {
+		if !devices[i].VlanKnown {
+			t.Errorf("%s: la VLAN vino de Proxmox y VlanKnown deberia ser true", devices[i].Name)
+		}
+	}
 }
 
 // TestSealProxmoxInfraUntaggedGuestClearsTheFdbVlan: el FDB del router se
@@ -114,6 +121,16 @@ func TestSealProxmoxInfraUntaggedGuestClearsTheFdbVlan(t *testing.T) {
 	if devices[3].Vlan != 888 {
 		t.Errorf("device ajeno a PVE tocado: vlan=%d (want 888)", devices[3].Vlan)
 	}
+	// Host e invitados quedan resueltos (VlanKnown true, aunque Vlan sea 0);
+	// el device ajeno a PVE conserva "sin dato" para que la UI use el puerto.
+	for _, i := range []int{0, 1, 2} {
+		if !devices[i].VlanKnown {
+			t.Errorf("%s: Proxmox resolvio la VLAN y VlanKnown deberia ser true", devices[i].Name)
+		}
+	}
+	if devices[3].VlanKnown {
+		t.Error("un device ajeno a PVE no debe marcarse como resuelto")
+	}
 }
 
 // TestSealProxmoxInfraSinInventario: sin cluster configurado o sin CTs

+ 11 - 3
server-go/internal/adapters/types.go

@@ -331,8 +331,16 @@ type Device struct {
 	Infra string `json:"infra,omitempty"` // "hypervisor"|"ct"|"vm"|"managed-switch"|"ap"
 	// Vlan: VLAN del dispositivo cuando la declara una fuente que la conoce y
 	// el router no puede verla. Hoy solo el inventario de Proxmox la aporta,
-	// leyendo tag=<vid> de la config del invitado (#1757). 0 = sin dato.
+	// leyendo tag=<vid> de la config del invitado (#1757). 0 con
+	// VlanKnown = false = sin dato.
 	Vlan int `json:"vlan,omitempty"`
+	// VlanKnown: una fuente resolvió la VLAN de este dispositivo (el FDB del
+	// bridge o el tag= del hipervisor) y el valor de Vlan es su respuesta.
+	// Vlan = 0 con VlanKnown = true significa "no está en ninguna VLAN que
+	// conozcamos", no "sin dato": el frontend no debe caer al puerto, que en
+	// un trunk no dice nada (#1757, el host y los invitados de Proxmox se
+	// pintaban con la VLAN de otro invitado).
+	VlanKnown bool `json:"vlanKnown,omitempty"`
 	// --- mDNS/SSDP fingerprinting (#338) ---
 	// MdnsServices: mDNS service types advertised by this device (from umdns).
 	// e.g. ["_airplay._tcp", "_raop._tcp"] for an Apple TV.
@@ -1185,6 +1193,6 @@ type NetworkInfo struct {
 	// ClientMacs (#1942): clientes atribuidos a esta red por puerto FDB, VLAN
 	// observada o wifi-iface declarada. Ausente = ninguno atribuible, no "0".
 	ClientMacs []string `json:"clientMacs,omitempty"`
-	RxBps  float64  `json:"rxBps,omitempty"`
-	TxBps  float64  `json:"txBps,omitempty"`
+	RxBps      float64  `json:"rxBps,omitempty"`
+	TxBps      float64  `json:"txBps,omitempty"`
 }

+ 12 - 3
server-go/internal/adapters/vlans_client_1757_test.go

@@ -32,6 +32,12 @@ func TestBuildDevicesVlanDesdeFdb(t *testing.T) {
 	if got["AA:BB:CC:DD:EE:02"] != 777 {
 		t.Fatalf("cliente de eth0.777 esperado en VLAN 777, got %d (%+v)", got["AA:BB:CC:DD:EE:02"], devs)
 	}
+	// El FDB resolvió la VLAN: el frontend no debe caer al puerto (#1757).
+	for _, d := range devs {
+		if !d.VlanKnown {
+			t.Fatalf("%s: la VLAN vino del FDB y VlanKnown deberia ser true", d.MAC)
+		}
+	}
 }
 
 // Un FDB sin campo vlan (bridge sin VLAN filtering) NO inventa VLAN: el
@@ -51,6 +57,9 @@ func TestBuildDevicesSinVlanEnFdbNoInventa(t *testing.T) {
 	if devs[0].Vlan != 0 {
 		t.Fatalf("sin dato de VLAN el dispositivo no debe llevar ninguna, got %d", devs[0].Vlan)
 	}
+	if devs[0].VlanKnown {
+		t.Fatal("sin dato de VLAN, VlanKnown debe quedar false para que la UI pueda usar el puerto")
+	}
 }
 
 // La VLAN del FDB es de SU router: una MAC que otro router declara en una VLAN
@@ -59,9 +68,9 @@ func TestBuildDevicesVlanNoCruzaRouters(t *testing.T) {
 	l := NewLive(nil, nil, nil, nil)
 	polled := map[string]*routerPolled{
 		"rutto": {
-			cfg:  RouterConfig{ID: "rutto", Name: "RUTTO"},
-			fdb:  map[string]string{"AA:BB:CC:DD:EE:04": "eth0"},
-			arp:  map[string]string{"AA:BB:CC:DD:EE:04": "192.168.1.40"},
+			cfg: RouterConfig{ID: "rutto", Name: "RUTTO"},
+			fdb: map[string]string{"AA:BB:CC:DD:EE:04": "eth0"},
+			arp: map[string]string{"AA:BB:CC:DD:EE:04": "192.168.1.40"},
 		},
 		"ap": {
 			cfg:      RouterConfig{ID: "ap", Name: "AP"},