Browse Source

fix(ports): decimal link speeds and hide DSA conduits (#847) (#857)

fix(ports): decimal link speeds and hide DSA conduits (#857)

Closes #847.
Nacho 2 months ago
parent
commit
dd8211bf5e
3 changed files with 87 additions and 13 deletions
  1. 38 11
      agent/probe/probe.go
  2. 48 1
      agent/probe/probe_test.go
  3. 1 1
      server-go/internal/adapters/adapters_test.go

+ 38 - 11
agent/probe/probe.go

@@ -87,9 +87,12 @@ const (
 	// CmdRadioSections (#500): secciones wifi-device de UCI con su banda, para
 	// resolver "2.4 GHz" → radio0 (la relación iface→sección no sale de iwinfo).
 	CmdRadioSections = `uci -q show wireless 2>/dev/null | grep -E "=wifi-device$|\.band=|\.hwmode="`
-	// CmdPortStates: "<name> <operstate> <speed>" por interfaz.
+	// CmdPortStates: "<name> <operstate> <speed> <dsa?>" por interfaz. El
+	// 4º campo marca "dsa" cuando la interfaz es un conduit DSA (tiene el
+	// directorio /sys/class/net/<i>/dsa): es el puerto CPU interno del
+	// switch, no una boca usable, y se excluye del panel (#847).
 	CmdPortStates = `for d in /sys/class/net/*; do i=$(basename "$d"); ` +
-		`echo "$i $(cat $d/operstate 2>/dev/null) $(cat $d/speed 2>/dev/null || echo -1)"; done`
+		`echo "$i $(cat $d/operstate 2>/dev/null) $(cat $d/speed 2>/dev/null || echo -1) $([ -d $d/dsa ] && echo dsa || echo -)"; done`
 	// CmdProcArp (#377): tabla ARP del kernel, "IP dev mac ..." por línea.
 	CmdProcArp     = "cat /proc/net/arp 2>/dev/null"
 	CmdBoardJSON   = "cat /etc/board.json 2>/dev/null"
@@ -201,7 +204,24 @@ type WirelessClient struct {
 type PortState struct {
 	Name  string `json:"name"`
 	Up    bool   `json:"up"`
-	Speed string `json:"speed"` // "1 Gbps" | "100 Mbps" | "—"
+	Speed string `json:"speed"` // "1 Gbps" | "2.5 Gbps" | "100 Mbps" | "—"
+	// DSA: la interfaz es un conduit DSA (puerto CPU interno del switch,
+	// /sys/class/net/<i>/dsa presente). No es una boca usable: se excluye
+	// de los puertos del panel (#847).
+	DSA bool `json:"dsa,omitempty"`
+}
+
+// fmtSpeedMbps (#847) formatea la velocidad de enlace: Gbps con decimales
+// cuando no es múltiplo de 1000 (2500 → "2.5 Gbps"; antes truncaba a
+// entero y mentía "2 Gbps") y Mbps en otro caso.
+func fmtSpeedMbps(mbps int) string {
+	if mbps >= 1000 {
+		if mbps%1000 == 0 {
+			return strconv.Itoa(mbps/1000) + " Gbps"
+		}
+		return strconv.FormatFloat(float64(mbps)/1000, 'f', 1, 64) + " Gbps"
+	}
+	return strconv.Itoa(mbps) + " Mbps"
 }
 
 // PortLayout es una boca del layout canónico (/etc/board.json).
@@ -647,7 +667,7 @@ func ParseWirelessUplink(raw []byte) (bool, error) {
 // Puertos
 // ---------------------------------------------------------------------------
 
-// ParsePortStates parsea líneas "<name> <operstate> <speed>".
+// ParsePortStates parsea líneas "<name> <operstate> <speed> [dsa]".
 func ParsePortStates(out string) []PortState {
 	ports := []PortState{}
 	for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
@@ -661,13 +681,13 @@ func ParsePortStates(out string) []PortState {
 		mbps, _ := strconv.Atoi(p[2])
 		speed := "—"
 		if mbps > 0 {
-			if mbps >= 1000 {
-				speed = strconv.Itoa(mbps/1000) + " Gbps"
-			} else {
-				speed = strconv.Itoa(mbps) + " Mbps"
-			}
+			speed = fmtSpeedMbps(mbps)
+		}
+		st := PortState{Name: p[0], Up: p[1] == "up", Speed: speed}
+		if len(p) > 3 && p[3] == "dsa" {
+			st.DSA = true
 		}
-		ports = append(ports, PortState{Name: p[0], Up: p[1] == "up", Speed: speed})
+		ports = append(ports, st)
 	}
 	return ports
 }
@@ -768,7 +788,8 @@ func BuildEthPorts(layout []PortLayout, states []PortState, brMembers map[string
 			wanName = "wan"
 		} else if _, ok := byName["pppoe-wan"]; ok {
 			wanName = "eth1"
-		} else if _, ok := byName["eth1"]; ok {
+		} else if st, ok := byName["eth1"]; ok && !st.DSA {
+			// #847: un conduit DSA no es la WAN física aunque se llame eth1.
 			wanName = "eth1"
 		}
 		if wanName != "" {
@@ -792,6 +813,12 @@ func BuildEthPorts(layout []PortLayout, states []PortState, brMembers map[string
 		if used[st.Name] {
 			continue
 		}
+		// #847: el conduit DSA (puerto CPU interno del switch, /sys .../dsa)
+		// no es una boca usable: su velocidad es interna y sin sentido para
+		// el usuario (p. ej. 150 Mbps en BPI-R4). Se excluye del panel.
+		if st.DSA {
+			continue
+		}
 		if skipRe.MatchString(st.Name) {
 			continue
 		}

+ 48 - 1
agent/probe/probe_test.go

@@ -138,6 +138,53 @@ func TestBuildEthPortsConIfaces(t *testing.T) {
 	}
 }
 
+// TestFmtSpeedMbps (#847): 2500 debe ser "2.5 Gbps", no truncar a "2 Gbps".
+func TestFmtSpeedMbps(t *testing.T) {
+	cases := map[int]string{
+		10:    "10 Mbps",
+		100:   "100 Mbps",
+		1000:  "1 Gbps",
+		2500:  "2.5 Gbps",
+		5000:  "5 Gbps",
+		10000: "10 Gbps",
+	}
+	for mbps, want := range cases {
+		if got := fmtSpeedMbps(mbps); got != want {
+			t.Errorf("fmtSpeedMbps(%d) = %q, want %q", mbps, got, want)
+		}
+	}
+}
+
+// TestParsePortStatesDSA (#847): el 4º campo marca el conduit DSA.
+func TestParsePortStatesDSA(t *testing.T) {
+	states := ParsePortStates("eth1 up 2500 dsa\nlan1 up 1000 -\nsfp-wan up 2500\n")
+	if len(states) != 3 {
+		t.Fatalf("states: %+v", states)
+	}
+	if !states[0].DSA || states[0].Speed != "2.5 Gbps" {
+		t.Fatalf("conduit eth1: %+v", states[0])
+	}
+	if states[1].DSA {
+		t.Fatalf("lan1 no es conduit: %+v", states[1])
+	}
+	if states[2].DSA {
+		t.Fatalf("línea sin 4º campo no es conduit: %+v", states[2])
+	}
+}
+
+// TestBuildEthPortsSkipsDsaConduit (#847): los conduits DSA se excluyen del
+// panel aunque figuren en /sys (su velocidad interna no es una boca usable).
+func TestBuildEthPortsSkipsDsaConduit(t *testing.T) {
+	states := []PortState{
+		{Name: "lan1", Up: true, Speed: "1 Gbps"},
+		{Name: "eth1", Up: true, Speed: "150 Mbps", DSA: true},
+	}
+	ports := BuildEthPorts(nil, states, nil, nil)
+	if len(ports) != 1 || ports[0].ID != "lan1" {
+		t.Fatalf("el conduit DSA debe excluirse: %+v", ports)
+	}
+}
+
 func TestParsePingSummary(t *testing.T) {
 	out := "3 packets transmitted, 3 received, 0% packet loss, time 2003ms\nrtt min/avg/max/mdev = 8.123/9.456/10.999/0.5 ms"
 	lat, loss := ParsePingSummary(out)
@@ -241,7 +288,7 @@ func TestParseWireless(t *testing.T) {
 
 func TestParsePortsYLayout(t *testing.T) {
 	states := ParsePortStates("eth0 up 2500\nlan1 up 1000\nlan2 down -1\nwlan0 up 0\n")
-	if len(states) != 4 || states[0].Speed != "2 Gbps" || states[2].Up || states[2].Speed != "—" {
+	if len(states) != 4 || states[0].Speed != "2.5 Gbps" || states[2].Up || states[2].Speed != "—" {
 		t.Fatalf("states: %+v", states)
 	}
 	board := `{"network":{"lan":{"ports":["lan1","lan2","lan3","lan4"],"device":"br-lan"},"wan":{"device":"wan","protocol":"dhcp"}}}`

+ 1 - 1
server-go/internal/adapters/adapters_test.go

@@ -310,7 +310,7 @@ func TestParsePortStates(t *testing.T) {
 	if len(ports) != 4 {
 		t.Fatalf("ports: %d", len(ports))
 	}
-	if ports[0].Speed != "2 Gbps" || !ports[0].Up {
+	if ports[0].Speed != "2.5 Gbps" || !ports[0].Up {
 		t.Fatalf("p0: %+v", ports[0])
 	}
 	if ports[1].Speed != "1 Gbps" {