-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from cloudflare/ivan/pci
Add `pci_vendor`, `pci_device`, `pci_class`, `pci_subclass` decoders
- Loading branch information
Showing
16 changed files
with
635 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package decoder | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/jaypipes/pcidb" | ||
) | ||
|
||
const pciIdsPath = "/usr/share/misc/pci.ids" | ||
const missingPciIdsText = "missing pci.ids db" | ||
|
||
var pci *pcidb.PCIDB | ||
|
||
func init() { | ||
if _, err := os.Stat(pciIdsPath); err != nil { | ||
log.Printf("PCI DB path %q is not accessible: %v", pciIdsPath, err) | ||
return | ||
} | ||
|
||
db, err := pcidb.New() | ||
if err != nil { | ||
log.Fatalf("Error initializing PCI DB: %v", err) | ||
} | ||
|
||
pci = db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package decoder | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cloudflare/ebpf_exporter/v2/config" | ||
) | ||
|
||
// PCIClass is a decoder that transforms PCI class id into a name | ||
type PCIClass struct{} | ||
|
||
// Decode transforms PCI class id into a name | ||
func (d *PCIClass) Decode(in []byte, conf config.Decoder) ([]byte, error) { | ||
if pci == nil { | ||
return []byte(missingPciIdsText), nil | ||
} | ||
|
||
num, err := strconv.Atoi(string(in)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := fmt.Sprintf("%02x", num) | ||
|
||
if device, ok := pci.Classes[key]; ok { | ||
return []byte(device.Name), nil | ||
} else { | ||
return []byte(fmt.Sprintf("unknown pci class: 0x%s", key)), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package decoder | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/cloudflare/ebpf_exporter/v2/config" | ||
) | ||
|
||
func TestPCIClassDecoderMissing(t *testing.T) { | ||
if pci != nil { | ||
t.Skip("PCI DB is available") | ||
} | ||
|
||
cases := [][]byte{ | ||
[]byte("1"), | ||
[]byte("2"), | ||
[]byte("6"), | ||
} | ||
|
||
for _, c := range cases { | ||
d := &PCIClass{} | ||
|
||
out, err := d.Decode(c, config.Decoder{}) | ||
if err != nil { | ||
t.Errorf("Error decoding %#v: %v", c, err) | ||
} | ||
|
||
if !bytes.Equal(out, []byte(missingPciIdsText)) { | ||
t.Errorf("Expected %q, got %s", missingPciIdsText, out) | ||
} | ||
} | ||
} | ||
|
||
func TestPCIClassDecoderPresent(t *testing.T) { | ||
if pci == nil { | ||
t.Skip("PCI DB is not available") | ||
} | ||
|
||
cases := []struct { | ||
in []byte | ||
out []byte | ||
}{ | ||
{ | ||
in: []byte("1"), | ||
out: []byte("Mass storage controller"), | ||
}, | ||
{ | ||
in: []byte("2"), | ||
out: []byte("Network controller"), | ||
}, | ||
{ | ||
in: []byte("3"), | ||
out: []byte("Display controller"), | ||
}, | ||
{ | ||
in: []byte("6"), | ||
out: []byte("Bridge"), | ||
}, | ||
{ | ||
in: []byte("12"), | ||
out: []byte("Serial bus controller"), | ||
}, | ||
{ | ||
in: []byte("253"), | ||
out: []byte("unknown pci class: 0xfd"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
d := &PCIClass{} | ||
|
||
out, err := d.Decode(c.in, config.Decoder{}) | ||
if err != nil { | ||
t.Errorf("Error decoding %#v: %v", c.in, err) | ||
} | ||
|
||
if !bytes.Equal(out, c.out) { | ||
t.Errorf("Expected %q, got %q", c.out, out) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package decoder | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cloudflare/ebpf_exporter/v2/config" | ||
) | ||
|
||
// PCIDevice2 is a decoder that transforms PCI device id into a name | ||
type PCIDevice struct{} | ||
|
||
// Decode transforms PCI device id into a name | ||
func (d *PCIDevice) Decode(in []byte, conf config.Decoder) ([]byte, error) { | ||
if pci == nil { | ||
return []byte(missingPciIdsText), nil | ||
} | ||
|
||
num, err := strconv.Atoi(string(in)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := fmt.Sprintf("%04x", num) | ||
|
||
if device, ok := pci.Products[key]; ok { | ||
return []byte(device.Name), nil | ||
} else { | ||
return []byte(fmt.Sprintf("unknown pci device: 0x%s", key)), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package decoder | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/cloudflare/ebpf_exporter/v2/config" | ||
) | ||
|
||
func TestPCIDeviceDecoderMissing(t *testing.T) { | ||
if pci != nil { | ||
t.Skip("PCI DB is available") | ||
} | ||
|
||
cases := [][]byte{ | ||
[]byte("2156269568"), // 0x80861000 | ||
[]byte("268596191"), // 0x100273df | ||
[]byte("282994436"), // 0x10de2704 | ||
} | ||
|
||
for _, c := range cases { | ||
d := &PCIDevice{} | ||
|
||
out, err := d.Decode(c, config.Decoder{}) | ||
if err != nil { | ||
t.Errorf("Error decoding %#v: %v", c, err) | ||
} | ||
|
||
if !bytes.Equal(out, []byte(missingPciIdsText)) { | ||
t.Errorf("Expected %q, got %s", missingPciIdsText, out) | ||
} | ||
} | ||
} | ||
|
||
func TestPCIDeviceDecoderPresent(t *testing.T) { | ||
if pci == nil { | ||
t.Skip("PCI DB is not available") | ||
} | ||
|
||
cases := []struct { | ||
in []byte | ||
out []byte | ||
}{ | ||
{ | ||
in: []byte("2156269568"), // 0x80861000 | ||
out: []byte("82542 Gigabit Ethernet Controller (Fiber)"), | ||
}, | ||
{ | ||
in: []byte("268596191"), // 0x100273df | ||
out: []byte("Navi 22 [Radeon RX 6700/6700 XT/6750 XT / 6800M/6850M XT]"), | ||
}, | ||
{ | ||
in: []byte("282994436"), // 0x10de2704 | ||
out: []byte("AD103 [GeForce RTX 4080]"), | ||
}, | ||
{ | ||
in: []byte("364056607"), // 0x15b3101f | ||
out: []byte("MT2894 Family [ConnectX-6 Lx]"), | ||
}, | ||
{ | ||
in: []byte("340633610"), // 0x144da80a | ||
out: []byte("NVMe SSD Controller PM9A1/PM9A3/980PRO"), | ||
}, | ||
{ | ||
in: []byte("350492180"), // 0x14e41614 | ||
out: []byte("BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet"), | ||
}, | ||
{ | ||
in: []byte("3735928559"), // 0xdeadbeef | ||
out: []byte("unknown pci device: 0xdeadbeef"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
d := &PCIDevice{} | ||
|
||
out, err := d.Decode(c.in, config.Decoder{}) | ||
if err != nil { | ||
t.Errorf("Error decoding %#v: %v", c.in, err) | ||
} | ||
|
||
if !bytes.Equal(out, c.out) { | ||
t.Errorf("Expected %q, got %q", c.out, out) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package decoder | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/cloudflare/ebpf_exporter/v2/config" | ||
) | ||
|
||
// PCISubClass is a decoder that transforms PCI class id into a name | ||
type PCISubClass struct{} | ||
|
||
// Decode transforms PCI class id into a name | ||
func (d *PCISubClass) Decode(in []byte, conf config.Decoder) ([]byte, error) { | ||
if pci == nil { | ||
return []byte(missingPciIdsText), nil | ||
} | ||
|
||
num, err := strconv.Atoi(string(in)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
classID := fmt.Sprintf("%02x", num>>8) | ||
|
||
if class, ok := pci.Classes[classID]; ok { | ||
subclassID := fmt.Sprintf("%02x", num&0xff) | ||
for _, subclass := range class.Subclasses { | ||
if subclass.ID == subclassID { | ||
return []byte(subclass.Name), nil | ||
} | ||
} | ||
|
||
return []byte(fmt.Sprintf("unknown pci subclass: 0x%s (class 0x%s)", subclassID, classID)), nil | ||
} else { | ||
return []byte(fmt.Sprintf("unknown pci class: 0x%s", classID)), nil | ||
} | ||
} |
Oops, something went wrong.