Skip to content

Commit

Permalink
Merge pull request #65 from numtide/pci
Browse files Browse the repository at this point in the history
remove fields from pci, which we won't need
  • Loading branch information
brianmcgee authored Aug 9, 2024
2 parents 0dc91e4 + cc2d877 commit 8a39965
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 56 deletions.
35 changes: 22 additions & 13 deletions pkg/hwinfo/detail_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package hwinfo
#include <hd.h>
*/
import "C"
import "regexp"

//go:generate enumer -type=CpuArch -json -transform=snake -trimprefix CpuArch -output=./detail_enum_cpu_arch.go
type CpuArch uint
Expand Down Expand Up @@ -36,13 +37,21 @@ type DetailCpu struct {
Model uint `json:"model"`
Stepping uint `json:"stepping"`
Cache uint `json:"cache"`
Clock uint `json:"clock"`
Units uint `json:"units"`
VendorName string `json:"vendor_name"`
ModelName string `json:"model_name"`
Platform string `json:"platform"`
Features []string `json:"features"`
Bogo float64 `json:"bogo"`
// This field changes as the CPU up/down scales, so we do not export it
Clock uint `json:"-"`
Units uint `json:"units"`
VendorName string `json:"vendor_name"`
ModelName string `json:"model_name"`
Platform string `json:"platform"`
Features []string `json:"features"`
Bogo float64 `json:"bogo"`
}

var matchCPUFreq = regexp.MustCompile(`, %d MHz$`)

func stripCpuFreq(s string) string {
// strip frequency of the model name as it is not stable.
return matchCPUFreq.ReplaceAllString(s, "")
}

func NewDetailCpu(cpu C.hd_detail_cpu_t) (Detail, error) {
Expand All @@ -56,12 +65,12 @@ func NewDetailCpu(cpu C.hd_detail_cpu_t) (Detail, error) {
Stepping: uint(data.stepping),
Cache: uint(data.cache),
Clock: uint(data.clock),
Units: uint(data.units),
VendorName: C.GoString(data.vend_name),
ModelName: C.GoString(data.model_name),
Platform: C.GoString(data.platform),
Features: ReadStringList(data.features),
Bogo: float64(data.bogo),
Units: uint(data.units),
VendorName: C.GoString(data.vend_name),
ModelName: stripCpuFreq(C.GoString(data.model_name)),
Platform: C.GoString(data.platform),
Features: ReadStringList(data.features),
Bogo: float64(data.bogo),
}, nil
}

Expand Down
81 changes: 40 additions & 41 deletions pkg/hwinfo/detail_pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ package hwinfo
#include <hd.h>
*/
import "C"

import (
"encoding/hex"
"unsafe"
)
import "encoding/hex"
import "unsafe"

//go:generate enumer -type=PciFlag -json -transform=snake -trimprefix PciFlag -output=./detail_enum_pci_flag.go
type PciFlag uint
Expand All @@ -30,10 +27,11 @@ func ParsePciFlags(flags uint) (result []PciFlag) {
}

type DetailPci struct {
Type DetailType `json:"type"`
Data string `json:"data"` // the PCI data, hex encoded
DataLength uint `json:"data_length"` // holds the actual length of Data
DataExtLength uint `json:"data_ext_length"` // max. accessed config byte
Type DetailType `json:"type"`
// Not needed
Data string `json:"-"` // the PCI data, hex encoded
DataLength uint `json:"-"` // holds the actual length of Data
DataExtLength uint `json:"-"` // max. accessed config byte

Log string `json:""` // log messages

Expand Down Expand Up @@ -62,15 +60,16 @@ type DetailPci struct {
// used irq if any
Irq uint `json:"irq"`

BaseAddress [7]uint64 `json:"base_address"` // I/O or memory base
BaseLength [7]uint64 `json:"base_length"` // I/O or memory ranges
AddressFlags [7]uint `json:"address_flags"` // I/O or memory address flags
BaseAddress [7]uint64 `json:"-"` // I/O or memory base
BaseLength [7]uint64 `json:"-"` // I/O or memory ranges
AddressFlags [7]uint `json:"-"` // I/O or memory address flags

RomBaseAddress uint64 `json:"rom_base_address"` // memory base for card ROM
RomBaseLength uint64 `json:"rom_base_length"` // memory range for card ROM
RomBaseAddress uint64 `json:"-"` // memory base for card ROM
RomBaseLength uint64 `json:"-"` // memory range for card ROM

SysfsId string `json:"sysfs_id,omitempty"` // sysfs path
SysfsBusId string `json:"sysfs_bus_id,omitempty"` // sysfs bus id
// already included in the normal model
SysfsId string `json:"-"` // sysfs path
SysfsBusId string `json:"-"` // sysfs bus id
ModuleAlias string `json:"module_alias,omitempty"` // module alias
Label string `json:"label,omitempty"` // Consistent Device Name (CDN), pci firmware 3.1, chapter 4.6.7

Expand All @@ -87,36 +86,36 @@ func NewDetailPci(pci C.hd_detail_pci_t) (Detail, error) {
data := pci.data

return DetailPci{
Type: DetailTypePci,
Data: hex.EncodeToString(C.GoBytes(unsafe.Pointer(&data.data), 256)),
DataLength: uint(data.data_len),
DataExtLength: uint(data.data_ext_len),
Log: C.GoString(data.log),
Flags: ParsePciFlags(uint(data.flags)),
Command: uint(data.cmd),
HeaderType: uint(data.hdr_type),
SecondaryBus: uint(data.secondary_bus),
Bus: uint(data.bus),
Slot: uint(data.slot),
Function: uint(data._func),
BaseClass: uint(data.base_class),
SubClass: uint(data.sub_class),
ProgIf: uint(data.prog_if),
Device: uint(data.dev),
Vendor: uint(data.vend),
SubDevice: uint(data.sub_dev),
SubVendor: uint(data.sub_vend),
Revision: uint(data.rev),
Irq: uint(data.irq),
Type: DetailTypePci,
Data: hex.EncodeToString(C.GoBytes(unsafe.Pointer(&data.data), 256)),
DataLength: uint(data.data_len),
DataExtLength: uint(data.data_ext_len),
Log: C.GoString(data.log),
Flags: ParsePciFlags(uint(data.flags)),
Command: uint(data.cmd),
HeaderType: uint(data.hdr_type),
SecondaryBus: uint(data.secondary_bus),
Bus: uint(data.bus),
Slot: uint(data.slot),
Function: uint(data._func),
BaseClass: uint(data.base_class),
SubClass: uint(data.sub_class),
ProgIf: uint(data.prog_if),
Device: uint(data.dev),
Vendor: uint(data.vend),
SubDevice: uint(data.sub_dev),
SubVendor: uint(data.sub_vend),
Revision: uint(data.rev),
Irq: uint(data.irq),
BaseAddress: [7]uint64(ReadUint64Array(unsafe.Pointer(&data.base_addr), 7)),
BaseLength: [7]uint64(ReadUint64Array(unsafe.Pointer(&data.base_len), 7)),
AddressFlags: [7]uint(ReadUintArray(unsafe.Pointer(&data.addr_flags), 7)),
RomBaseAddress: uint64(data.rom_base_addr),
RomBaseLength: uint64(data.rom_base_len),
SysfsId: C.GoString(data.sysfs_id),
SysfsBusId: C.GoString(data.sysfs_bus_id),
ModuleAlias: C.GoString(data.modalias),
Label: C.GoString(data.label),
SysfsId: C.GoString(data.sysfs_id),
SysfsBusId: C.GoString(data.sysfs_bus_id),
ModuleAlias: C.GoString(data.modalias),
Label: C.GoString(data.label),
// todo edid data
}, nil
}
15 changes: 15 additions & 0 deletions pkg/hwinfo/hwinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import (
"unsafe"
)

func excludeDevice(item *HardwareItem) bool {
if item.HardwareClass == HardwareClassNetwork {
for _, driver := range item.Drivers {
// devices that are not mapped to hardware should be not included in the hardware report
if virtualNetworkDevices[driver] {
return true
}
}
}
return false
}

func Scan(probes []ProbeFeature) ([]Smbios, []*HardwareItem, error) {
// initialise the struct to hold scan data
data := (*C.hd_data_t)(unsafe.Pointer(C.calloc(1, C.size_t(unsafe.Sizeof(C.hd_data_t{})))))
Expand Down Expand Up @@ -42,6 +54,9 @@ func Scan(probes []ProbeFeature) ([]Smbios, []*HardwareItem, error) {
if item, err := NewHardwareItem(hd); err != nil {
return nil, nil, err
} else {
if excludeDevice(item) {
continue
}
hardwareItems = append(hardwareItems, item)
}
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/hwinfo/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ func NewHardwareItem(hd *C.hd_t) (*HardwareItem, error) {
if err != nil {
return nil, fmt.Errorf("failed to read driver info: %w", err)
}
model := C.GoString(hd.model)
hwClass := HardwareClass(hd.hw_class)
if hwClass == HardwareClassCpu {
model = stripCpuFreq(model)
}

return &HardwareItem{
Index: uint(hd.idx),
Expand All @@ -332,8 +337,8 @@ func NewHardwareItem(hd *C.hd_t) (*HardwareItem, error) {
Serial: C.GoString(hd.serial),
CompatVendor: NewId(hd.compat_vendor),
CompatDevice: NewId(hd.compat_device),
HardwareClass: HardwareClass(hd.hw_class),
Model: C.GoString(hd.model),
HardwareClass: hwClass,
Model: model,
AttachedTo: uint(hd.attached_to),
SysfsId: C.GoString(hd.sysfs_id),
SysfsBusId: C.GoString(hd.sysfs_bus_id),
Expand Down
38 changes: 38 additions & 0 deletions pkg/hwinfo/virtual_network_devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hwinfo

// original list taken from systemd.network
var virtualNetworkDevices = map[string]bool {
"bonding": true,
"bridge": true,
"dummy": true,
"ip_gre": true,
"ip6_gre": true,
"ipip": true,
"ipvlan": true,
"macvlan": true,
"sit": true,
"tun": true,
"veth": true,
"8021q": true,
"ip_vti": true,
"ip6_vti": true,
"vxlan": true,
"geneve": true,
"macsec": true,
"vrf": true,
"vcan": true,
"vxcan": true,
"wireguard": true,
"nlmon": true,
"fou": true,
"ifb": true,
"bareudp": true,
"batman-adv": true,
"ib_ipoib": true,
// FIXME: could not confirm the following netdev drivers:
// "l2tp": true,
// "xfrm": true,
// "erspan": true,
// "ip6tnl": true,
}

0 comments on commit 8a39965

Please sign in to comment.