fix(inventory): preserve distinct PCI devices sharing a serial

This commit is contained in:
Mikhail Chusavitin committed 2026-10-22 21:24:19 +03:00
1 parent 78e1a473cc
commit 488f730c9d
3 files changed
+74 -5

No files matched your search

+18
View File
@@ -462,6 +462,13 @@ func shouldMergeDevices(a, b models.HardwareDevice) bool {
}
}
// Distinct PCI devices never merge, even on an equal serial: onboard chips
// (B300 NVL8 ConnectX-8) carry their baseboard serial in VPD. Functions of
// one device (50:00.0 and 50:00.1) still merge.
if aBDF != "" && bBDF != "" && pciDeviceOf(aBDF) != pciDeviceOf(bBDF) {
return false
}
// Hard conflicts.
if aSN != "" && bSN != "" && aSN == bSN {
if a.Kind == models.DeviceKindMemory && b.Kind == models.DeviceKindMemory {
@@ -598,6 +605,17 @@ func mergeDevices(primary, secondary models.HardwareDevice) models.HardwareDevic
return primary
}
// pciDeviceOf drops the function from a BDF: "0000:50:00.1" -> "0000:50:00".
func pciDeviceOf(bdf string) string {
if i := strings.LastIndexByte(bdf, '.'); i >= 0 {
bdf = bdf[:i]
}
if strings.Count(bdf, ":") == 1 {
bdf = "0000:" + bdf
}
return bdf
}
func samePCIID(a, b models.HardwareDevice) bool {
if (a.VendorID == 0 && a.DeviceID == 0) || (b.VendorID == 0 && b.DeviceID == 0) {
return false
+31 -5
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.mchus.pro/reanimator/parser/v31/models"
@@ -24,17 +25,17 @@ func TestBuildHardwareDevices_DedupSerialThenBDF(t *testing.T) {
}
devices := BuildHardwareDevices(hw)
// 1 board + (SER-1 dedup -> 1) + (BDF 03 dedup -> 1) + (C1,C2 keep both) = 5
if len(devices) != 5 {
t.Fatalf("expected 5 devices after dedupe, got %d", len(devices))
// 1 board + (SER-1 on distinct BDFs keeps both) + (BDF 03 dedup -> 1) + (C1,C2 keep both) = 6
if len(devices) != 6 {
t.Fatalf("expected 6 devices after dedupe, got %d", len(devices))
}
bySlot := map[string]bool{}
for _, d := range devices {
bySlot[d.Slot] = true
}
if !bySlot["A1"] && !bySlot["A2"] {
t.Fatalf("expected one serial-deduped A* device")
if !bySlot["A1"] || !bySlot["A2"] {
t.Fatalf("expected A1 and A2 to stay separate: equal serial, distinct BDFs")
}
if bySlot["B1"] && bySlot["B2"] {
t.Fatalf("expected B1/B2 to dedupe by bdf")
@@ -332,3 +333,28 @@ func TestBuildHardwareDevices_CPUUsesPPINAsSerialFallback(t *testing.T) {
}
t.Fatal("CPU device was not built")
}
func TestBuildHardwareDevices_SharedSerialOnDistinctPCIDevicesIsNotMerged(t *testing.T) {
// B300 NVL8: every onboard ConnectX-8 carries the baseboard serial in its
// VPD. Functions of one device (50:00.0/.1) still merge; chips on
// different buses stay separate and get DUP annotations.
hw := &models.HardwareConfig{
PCIeDevices: []models.PCIeDevice{
{Slot: "0000:0d:00.0", BDF: "0000:0d:00.0", VendorID: 0x15b3, DeviceID: 0x1023, DeviceClass: "NetworkController", PartNumber: "MLX000647", SerialNumber: "1951126571627"},
{Slot: "0000:35:00.0", BDF: "0000:35:00.0", VendorID: 0x15b3, DeviceID: 0x1023, DeviceClass: "NetworkController", PartNumber: "MLX000647", SerialNumber: "1951126571627"},
{Slot: "0000:50:00.0", BDF: "0000:50:00.0", VendorID: 0x15b3, DeviceID: 0xa2dc, DeviceClass: "EthernetController", SerialNumber: "MT2621606QBJ"},
{Slot: "0000:50:00.1", BDF: "0000:50:00.1", VendorID: 0x15b3, DeviceID: 0xa2dc, DeviceClass: "EthernetController", SerialNumber: "MT2621606QBJ"},
},
}
var serials []string
for _, d := range BuildHardwareDevices(hw) {
if d.Kind == models.DeviceKindPCIe || d.Kind == models.DeviceKindNetwork {
serials = append(serials, d.BDF+" "+d.SerialNumber)
}
}
want := []string{"0000:0d:00.0 1951126571627 (DUP#1)", "0000:35:00.0 1951126571627 (DUP#2)", "0000:50:00.0 MT2621606QBJ"}
if strings.Join(serials, "|") != strings.Join(want, "|") {
t.Fatalf("devices = %q, want %q", serials, want)
}
}