Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078: - linux-generic: recognizes utility output by content, merges records by stable key, low detect confidence (20-30) below all vendor parsers - pciids.LookupByDescription: resolve a textual lspci description back to vendor/device IDs and manufacturer/model - archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7, single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected), same per-file and total limits as tar/zip - UI: .rar in the upload accept list Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
376 lines
10 KiB
Go
376 lines
10 KiB
Go
package linux_generic
|
|
|
|
import (
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.mchus.pro/mchus/logpile/internal/models"
|
|
"git.mchus.pro/mchus/logpile/internal/parser/vendors/pciids"
|
|
)
|
|
|
|
// PCI vendor IDs of BMC-embedded VGA functions (ASPEED AST*, Matrox G200e*):
|
|
// a display-class function that is not a GPU.
|
|
const (
|
|
pciVendorASPEED = 0x1a03
|
|
pciVendorMatrox = 0x102b
|
|
)
|
|
|
|
var (
|
|
// "17:00.0 Infiniband controller [0207]: Mellanox ... [15b3:1023] (rev 01)"
|
|
pciHeaderRe = regexp.MustCompile(`^((?:[0-9a-f]{4}:)?[0-9a-f]{2}:[0-9a-f]{2}\.[0-7]) ([^:]+?)(?: \[([0-9a-f]{4})\])?: (.+)$`)
|
|
pciSuffixRe = regexp.MustCompile(`\s+\((?:rev|prog-if) [^)]*\)$`)
|
|
pciIDsRe = regexp.MustCompile(`\s+\[([0-9a-f]{4}):([0-9a-f]{4})\]$`)
|
|
pciSpeedRe = regexp.MustCompile(`Speed ([0-9.]+ ?GT/s)`)
|
|
pciWidthRe = regexp.MustCompile(`Width x(\d+)`)
|
|
pciVPDTagRe = regexp.MustCompile(`^\[([A-Z0-9]{2})\] [^:]+:\s*(.*)$`)
|
|
)
|
|
|
|
// pciDevice is one PCI function merged from every lspci excerpt that
|
|
// mentions its address.
|
|
type pciDevice struct {
|
|
bdf string
|
|
class string
|
|
classCode string
|
|
description string
|
|
vendorID int
|
|
deviceID int
|
|
slot string
|
|
numaNode *int
|
|
maxSpeed string
|
|
maxWidth int
|
|
speed string
|
|
width int
|
|
productName string
|
|
partNumber string
|
|
serial string
|
|
driver string
|
|
}
|
|
|
|
func parseLspci(file sourceFile, inv *inventory) {
|
|
var current *pciDevice
|
|
|
|
for _, line := range strings.Split(file.content, "\n") {
|
|
if m := pciHeaderRe.FindStringSubmatch(line); m != nil {
|
|
current = inv.pciDevice(m[1])
|
|
current.setHeader(m[2], m[3], m[4])
|
|
continue
|
|
}
|
|
if !isIndented(line) {
|
|
current = nil
|
|
continue
|
|
}
|
|
if current != nil {
|
|
current.setDetail(strings.TrimSpace(line))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (inv *inventory) pciDevice(address string) *pciDevice {
|
|
bdf := strings.ToLower(address)
|
|
if len(bdf) == len("00:00.0") {
|
|
bdf = "0000:" + bdf
|
|
}
|
|
if d, ok := inv.pci[bdf]; ok {
|
|
return d
|
|
}
|
|
d := &pciDevice{bdf: bdf}
|
|
inv.pci[bdf] = d
|
|
return d
|
|
}
|
|
|
|
func (d *pciDevice) setHeader(class, classCode, description string) {
|
|
for {
|
|
trimmed := pciSuffixRe.ReplaceAllString(description, "")
|
|
if trimmed == description {
|
|
break
|
|
}
|
|
description = trimmed
|
|
}
|
|
if m := pciIDsRe.FindStringSubmatch(description); m != nil {
|
|
description = pciIDsRe.ReplaceAllString(description, "")
|
|
if d.vendorID == 0 {
|
|
d.vendorID = parseHex(m[1])
|
|
d.deviceID = parseHex(m[2])
|
|
}
|
|
}
|
|
setIfEmpty(&d.class, strings.TrimSpace(class))
|
|
setIfEmpty(&d.classCode, classCode)
|
|
setIfEmpty(&d.description, strings.TrimSpace(description))
|
|
}
|
|
|
|
func (d *pciDevice) setDetail(line string) {
|
|
if m := pciVPDTagRe.FindStringSubmatch(line); m != nil {
|
|
value := strings.TrimSpace(m[2])
|
|
switch m[1] {
|
|
case "PN":
|
|
setIfEmpty(&d.partNumber, value)
|
|
case "SN":
|
|
setIfEmpty(&d.serial, value)
|
|
}
|
|
return
|
|
}
|
|
|
|
key, value, ok := splitKeyValue(line)
|
|
if !ok {
|
|
return
|
|
}
|
|
switch key {
|
|
case "Physical Slot":
|
|
setIfEmpty(&d.slot, value)
|
|
case "NUMA node":
|
|
if node, err := strconv.Atoi(value); err == nil && d.numaNode == nil {
|
|
d.numaNode = &node
|
|
}
|
|
case "LnkCap":
|
|
speed, width := parsePCILink(value)
|
|
setIfEmpty(&d.maxSpeed, speed)
|
|
setIntIfZero(&d.maxWidth, width)
|
|
case "LnkSta":
|
|
speed, width := parsePCILink(value)
|
|
setIfEmpty(&d.speed, speed)
|
|
setIntIfZero(&d.width, width)
|
|
case "Product Name":
|
|
setIfEmpty(&d.productName, value)
|
|
case "Kernel driver in use":
|
|
setIfEmpty(&d.driver, value)
|
|
}
|
|
}
|
|
|
|
func parsePCILink(value string) (speed string, width int) {
|
|
if m := pciSpeedRe.FindStringSubmatch(value); m != nil {
|
|
speed = strings.ReplaceAll(m[1], " ", "")
|
|
}
|
|
if m := pciWidthRe.FindStringSubmatch(value); m != nil {
|
|
width, _ = strconv.Atoi(m[1])
|
|
}
|
|
return speed, width
|
|
}
|
|
|
|
func parseHex(value string) int {
|
|
n, err := strconv.ParseInt(value, 16, 32)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return int(n)
|
|
}
|
|
|
|
type pciKind int
|
|
|
|
const (
|
|
pciKindOther pciKind = iota
|
|
pciKindNetwork
|
|
pciKindDisplay
|
|
pciKindController // storage, Fibre Channel and accelerator functions
|
|
)
|
|
|
|
// kind classifies the function by its PCI class code when lspci printed one
|
|
// (`-nn`), and by the class name otherwise. Bridges, system peripherals and
|
|
// other chipset functions are not inventory.
|
|
func (d *pciDevice) kind() pciKind {
|
|
if len(d.classCode) == 4 {
|
|
switch d.classCode[:2] {
|
|
case "02":
|
|
return pciKindNetwork
|
|
case "03":
|
|
return pciKindDisplay
|
|
case "01", "12":
|
|
return pciKindController
|
|
}
|
|
if d.classCode == "0c04" {
|
|
return pciKindController
|
|
}
|
|
return pciKindOther
|
|
}
|
|
|
|
class := strings.ToLower(d.class)
|
|
switch {
|
|
case strings.Contains(class, "ethernet controller"),
|
|
strings.Contains(class, "network controller"),
|
|
strings.Contains(class, "infiniband controller"):
|
|
return pciKindNetwork
|
|
case strings.Contains(class, "vga compatible controller"),
|
|
strings.Contains(class, "3d controller"),
|
|
strings.Contains(class, "display controller"):
|
|
return pciKindDisplay
|
|
case strings.Contains(class, "raid bus controller"),
|
|
strings.Contains(class, "serial attached scsi controller"),
|
|
strings.Contains(class, "scsi storage controller"),
|
|
strings.Contains(class, "sata controller"),
|
|
strings.Contains(class, "non-volatile memory controller"),
|
|
strings.Contains(class, "mass storage controller"),
|
|
strings.Contains(class, "fibre channel"),
|
|
strings.Contains(class, "processing accelerators"):
|
|
return pciKindController
|
|
}
|
|
return pciKindOther
|
|
}
|
|
|
|
// identity resolves IDs and names. lspci prints "<vendor> <device>" as one
|
|
// string; pci.ids splits it back and recovers the numeric IDs that plain
|
|
// `lspci` (without -nn) omits. A "Device 1a2b" placeholder, printed when the
|
|
// host's pci.ids did not know the device, is replaced by the bundled name.
|
|
func (d *pciDevice) identity() (vendorID, deviceID int, manufacturer, model string) {
|
|
vendorID, deviceID = d.vendorID, d.deviceID
|
|
model = d.description
|
|
|
|
lookupVendor, lookupDevice, vendorName, deviceName, ok := pciids.LookupByDescription(d.description)
|
|
if ok {
|
|
manufacturer, model = vendorName, deviceName
|
|
if vendorID == 0 {
|
|
vendorID, deviceID = lookupVendor, lookupDevice
|
|
}
|
|
} else if vendorID != 0 {
|
|
manufacturer = pciids.VendorName(vendorID)
|
|
}
|
|
|
|
if vendorID != 0 && deviceID != 0 && (model == "" || strings.HasPrefix(model, "Device ")) {
|
|
if name := pciids.DeviceName(vendorID, deviceID); name != "" {
|
|
model = name
|
|
}
|
|
}
|
|
return vendorID, deviceID, manufacturer, model
|
|
}
|
|
|
|
// appendPCIDevices emits network adapters, GPUs and controller functions in
|
|
// bus order.
|
|
func (inv *inventory) appendPCIDevices(hw *models.HardwareConfig) {
|
|
addresses := make([]string, 0, len(inv.pci))
|
|
for bdf := range inv.pci {
|
|
addresses = append(addresses, bdf)
|
|
}
|
|
sort.Strings(addresses)
|
|
inv.shareCardVPD(addresses)
|
|
inv.shareNumericIDs()
|
|
|
|
for _, bdf := range addresses {
|
|
d := inv.pci[bdf]
|
|
kind := d.kind()
|
|
if kind == pciKindOther {
|
|
continue
|
|
}
|
|
vendorID, deviceID, manufacturer, model := d.identity()
|
|
|
|
switch kind {
|
|
case pciKindNetwork:
|
|
hw.NetworkAdapters = append(hw.NetworkAdapters, models.NetworkAdapter{
|
|
Slot: bdf,
|
|
Location: d.slot,
|
|
Present: true,
|
|
BDF: bdf,
|
|
Model: model,
|
|
Description: d.productName,
|
|
Vendor: manufacturer,
|
|
VendorID: vendorID,
|
|
DeviceID: deviceID,
|
|
SerialNumber: d.serial,
|
|
PartNumber: d.partNumber,
|
|
LinkWidth: d.width,
|
|
LinkSpeed: d.speed,
|
|
MaxLinkWidth: d.maxWidth,
|
|
MaxLinkSpeed: d.maxSpeed,
|
|
NUMANode: d.numaNode,
|
|
Details: d.details(),
|
|
})
|
|
case pciKindDisplay:
|
|
if vendorID == pciVendorASPEED || vendorID == pciVendorMatrox {
|
|
continue
|
|
}
|
|
hw.GPUs = append(hw.GPUs, models.GPU{
|
|
Slot: bdf,
|
|
Location: d.slot,
|
|
Model: model,
|
|
Manufacturer: manufacturer,
|
|
VendorID: vendorID,
|
|
DeviceID: deviceID,
|
|
BDF: bdf,
|
|
SerialNumber: d.serial,
|
|
PartNumber: d.partNumber,
|
|
MaxLinkWidth: d.maxWidth,
|
|
MaxLinkSpeed: d.maxSpeed,
|
|
CurrentLinkWidth: d.width,
|
|
CurrentLinkSpeed: d.speed,
|
|
Details: d.details(),
|
|
})
|
|
case pciKindController:
|
|
hw.PCIeDevices = append(hw.PCIeDevices, models.PCIeDevice{
|
|
Slot: firstNonEmpty(d.slot, bdf),
|
|
Description: model,
|
|
VendorID: vendorID,
|
|
DeviceID: deviceID,
|
|
BDF: bdf,
|
|
DeviceClass: d.class,
|
|
Manufacturer: manufacturer,
|
|
Model: model,
|
|
LinkWidth: d.width,
|
|
LinkSpeed: d.speed,
|
|
MaxLinkWidth: d.maxWidth,
|
|
MaxLinkSpeed: d.maxSpeed,
|
|
PartNumber: d.partNumber,
|
|
SerialNumber: d.serial,
|
|
NUMANode: d.numaNode,
|
|
Details: d.details(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *pciDevice) details() map[string]any {
|
|
if d.driver == "" {
|
|
return nil
|
|
}
|
|
return map[string]any{"kernel_driver": d.driver}
|
|
}
|
|
|
|
// shareCardVPD copies VPD identity between the functions of one card. Scripts
|
|
// usually capture `lspci -vv` for function 0 only, while every function of a
|
|
// multi-port adapter is the same physical part.
|
|
func (inv *inventory) shareCardVPD(sortedAddresses []string) {
|
|
byCard := make(map[string]*pciDevice)
|
|
for _, bdf := range sortedAddresses {
|
|
d := inv.pci[bdf]
|
|
if d.partNumber == "" && d.serial == "" {
|
|
continue
|
|
}
|
|
card := bdf[:strings.LastIndexByte(bdf, '.')]
|
|
if _, seen := byCard[card]; !seen {
|
|
byCard[card] = d
|
|
}
|
|
}
|
|
for _, bdf := range sortedAddresses {
|
|
d := inv.pci[bdf]
|
|
source, ok := byCard[bdf[:strings.LastIndexByte(bdf, '.')]]
|
|
if !ok || source == d || d.class != source.class {
|
|
continue
|
|
}
|
|
setIfEmpty(&d.partNumber, source.partNumber)
|
|
setIfEmpty(&d.serial, source.serial)
|
|
setIfEmpty(&d.productName, source.productName)
|
|
}
|
|
}
|
|
|
|
// shareNumericIDs gives functions listed only by plain `lspci` the numeric
|
|
// IDs that an `lspci -nn` excerpt printed for an identically described
|
|
// function. A description that maps to several ID pairs is left alone.
|
|
func (inv *inventory) shareNumericIDs() {
|
|
type ids struct{ vendor, device int }
|
|
byDescription := make(map[string]ids)
|
|
for _, d := range inv.pci {
|
|
if d.vendorID == 0 {
|
|
continue
|
|
}
|
|
known, seen := byDescription[d.description]
|
|
if seen && known != (ids{d.vendorID, d.deviceID}) {
|
|
byDescription[d.description] = ids{}
|
|
continue
|
|
}
|
|
byDescription[d.description] = ids{d.vendorID, d.deviceID}
|
|
}
|
|
for _, d := range inv.pci {
|
|
if known := byDescription[d.description]; d.vendorID == 0 && known.vendor != 0 {
|
|
d.vendorID, d.deviceID = known.vendor, known.device
|
|
}
|
|
}
|
|
}
|