Quellcode durchsuchen

revert(devices): drop SNMP switch-port traffic attribution (reverts #1540)

Back to per-MAC rates from the gateway/agent only. The attribution
returns restructured (own file, uplink handling decided) once PRs get
CI (see the restructuring issue).
gnacho vor 1 Monat
Ursprung
Commit
c39b18d337

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

@@ -3480,9 +3480,6 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 		}
 		}
 		devices = append(devices, d)
 		devices = append(devices, d)
 	}
 	}
-	// SNMP switches have no per-MAC accounting; give their wired clients a
-	// figure from the switch port byte counters (see helper).
-	l.applySnmpSwitchPortTraffic(polled, devices)
 	// Orden estable por MAC (issue #656): buildDevices itera un mapa de Go
 	// Orden estable por MAC (issue #656): buildDevices itera un mapa de Go
 	// (allMacs), cuyo orden es aleatorio entre ticks. Ese orden llega al
 	// (allMacs), cuyo orden es aleatorio entre ticks. Ese orden llega al
 	// frontend y alimenta la asignación de ranuras de los anillos de la
 	// frontend y alimenta la asignación de ranuras de los anillos de la
@@ -3493,82 +3490,6 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 	return normalizeDevices(mergeLinkedDevices(devices, l.deviceLinks()))
 	return normalizeDevices(mergeLinkedDevices(devices, l.deviceLinks()))
 }
 }
 
 
-// applySnmpSwitchPortTraffic gives wired clients learned on an SNMP switch a
-// traffic figure from that switch's own port byte counters. A managed switch
-// has no per-MAC accounting (nlbwmon is an OpenWrt/agent feature), and switch-
-// local flows (e.g. a camera to an NVR on the same switch) never traverse the
-// gateway, so the gateway's per-MAC rate reports only the tiny gateway-bound
-// slice - the client otherwise looks idle. The switch port does see the whole
-// flow, so we attribute the port's rx+tx rate to the clients learned on it,
-// split evenly when several MACs share a port so a trunk is not duplicated
-// onto each. Only SNMP-enabled routers are consulted, and we only RAISE a
-// client's rate (never lower an agent-reported figure), so gateway/AP clients
-// are untouched.
-func (l *Live) applySnmpSwitchPortTraffic(polled map[string]*routerPolled, devices []Device) {
-	type portKey struct{ router, port string }
-
-	// Mbps per (router, port name). Indexed by every alias a port can carry so
-	// the FDB's name (its DisplayName = Label/Iface) resolves.
-	rate := map[portKey]float64{}
-	for rID, p := range polled {
-		if p == nil || !p.cfg.SnmpEnabled {
-			continue
-		}
-		for _, ep := range p.ports {
-			bps := ep.RxBps + ep.TxBps
-			if bps <= 0 {
-				continue
-			}
-			mp := bps / 1e6
-			for _, name := range []string{ep.ID, ep.Label, ep.Iface} {
-				if name != "" {
-					rate[portKey{rID, name}] = mp
-				}
-			}
-		}
-	}
-	if len(rate) == 0 {
-		return
-	}
-
-	// A client's port comes from the switch's own FDB (mac -> port), the same
-	// uppercased-MAC key the topology step uses. Device.Port is not filled yet
-	// at this point (inferTopology runs later), so we resolve from FDB directly.
-	resolve := func(d *Device) (portKey, bool) {
-		if !d.Online || d.RouterID == "" {
-			return portKey{}, false
-		}
-		p := polled[d.RouterID]
-		if p == nil || !p.cfg.SnmpEnabled || len(p.fdb) == 0 {
-			return portKey{}, false
-		}
-		port, ok := p.fdb[strings.ToUpper(d.MAC)]
-		if !ok {
-			return portKey{}, false
-		}
-		k := portKey{d.RouterID, port}
-		return k, rate[k] > 0
-	}
-
-	keys := make([]portKey, len(devices))
-	matched := make([]bool, len(devices))
-	share := map[portKey]int{}
-	for i := range devices {
-		if k, ok := resolve(&devices[i]); ok {
-			keys[i], matched[i] = k, true
-			share[k]++
-		}
-	}
-	for i := range devices {
-		if !matched[i] || share[keys[i]] <= 0 {
-			continue
-		}
-		if mp := rate[keys[i]] / float64(share[keys[i]]); mp > devices[i].TrafficMbps {
-			devices[i].TrafficMbps = mp
-		}
-	}
-}
-
 // normalizeDevices: MACs a formato canónico (mayúsculas, ':') y dedup por
 // normalizeDevices: MACs a formato canónico (mayúsculas, ':') y dedup por
 // MAC. La caché gl-clients de GL.iNet emite MACs con guiones y entradas
 // MAC. La caché gl-clients de GL.iNet emite MACs con guiones y entradas
 // caducadas: sin normalizar el mismo cliente puede aparecer duplicado
 // caducadas: sin normalizar el mismo cliente puede aparecer duplicado

+ 0 - 73
server-go/internal/adapters/switch_port_traffic_test.go

@@ -1,73 +0,0 @@
-package adapters
-
-import (
-	"math"
-	"testing"
-)
-
-// applySnmpSwitchPortTraffic raises a switch client's traffic to its FDB port's
-// byte-counter rate (switch-local flows the gateway's per-MAC nlbwmon misses),
-// splits a port across its clients, and never lowers an existing higher figure.
-func TestApplySnmpSwitchPortTraffic(t *testing.T) {
-	polled := map[string]*routerPolled{
-		"sw": {
-			cfg: RouterConfig{ID: "sw", SnmpEnabled: true},
-			ports: []EthPort{
-				{ID: "snmp-3", Label: "1/g3", RxBps: 14e6, TxBps: 0.14e6},
-				{ID: "snmp-4", Label: "1/g4", RxBps: 5e6, TxBps: 0},
-			},
-			fdb: map[string]string{
-				"AA:01": "1/g3",
-				"AA:02": "1/g3",
-				"AA:03": "1/g4",
-				"AA:04": "1/g3",
-			},
-		},
-	}
-	devices := []Device{
-		// Two cameras sharing port 1/g3 (14.14 Mbps total → 7.07 each). Lowercase
-		// MACs also prove the FDB uppercased-key lookup.
-		{MAC: "aa:01", Online: true, RouterID: "sw", TrafficMbps: 0.0008},
-		{MAC: "aa:02", Online: true, RouterID: "sw", TrafficMbps: 0},
-		// One camera on 1/g4 (5 Mbps); its existing 20 must not be lowered.
-		{MAC: "aa:03", Online: true, RouterID: "sw", TrafficMbps: 20},
-		// Offline client is ignored (not counted toward the port split).
-		{MAC: "aa:04", Online: false, RouterID: "sw", TrafficMbps: 0},
-	}
-
-	l := &Live{}
-	l.applySnmpSwitchPortTraffic(polled, devices)
-
-	if d := devices[0]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
-		t.Fatalf("AA:01 = %.4f, want ~7.07 (port 14.14 split over 2 online)", d.TrafficMbps)
-	}
-	if d := devices[1]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
-		t.Fatalf("AA:02 = %.4f, want ~7.07", d.TrafficMbps)
-	}
-	if d := devices[2]; d.TrafficMbps != 20 {
-		t.Fatalf("AA:03 = %.4f, want 20 (never lowered from a higher existing figure)", d.TrafficMbps)
-	}
-	if d := devices[3]; d.TrafficMbps != 0 {
-		t.Fatalf("offline AA:04 = %.4f, want 0", d.TrafficMbps)
-	}
-}
-
-// Non-SNMP routers are untouched: a client on an OpenWrt/agent router keeps
-// whatever per-MAC rate it had (no switch port to attribute).
-func TestApplySnmpSwitchPortTrafficSkipsAgentRouters(t *testing.T) {
-	polled := map[string]*routerPolled{
-		"gw": {
-			cfg:   RouterConfig{ID: "gw"}, // SnmpEnabled false
-			ports: []EthPort{{ID: "lan1", Label: "LAN 1", RxBps: 100e6}},
-			fdb:   map[string]string{"BB:01": "lan1"},
-		},
-	}
-	devices := []Device{{MAC: "BB:01", Online: true, RouterID: "gw", TrafficMbps: 1}}
-
-	l := &Live{}
-	l.applySnmpSwitchPortTraffic(polled, devices)
-
-	if devices[0].TrafficMbps != 1 {
-		t.Fatalf("agent-router client changed to %.4f, want 1 (helper is SNMP-only)", devices[0].TrafficMbps)
-	}
-}