Skip to content
Open
117 changes: 117 additions & 0 deletions sysfs/class_mei.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package sysfs
Comment thread
micgor32 marked this conversation as resolved.

import (
"fmt"
"os"
"path/filepath"

"github.com/prometheus/procfs/internal/util"
)

const meiClassPath = "class/mei"

type MEIDev struct {
Dev *string
DevState *string
FWStatus *string
FWVersion *string
HBMVersion *string
HBMVersionDrv *string
Kind *string
Trc *string
TxQueueLimit *string
}

type MEIClass map[string]MEIDev

// MEIClass returns Management Engine Interface (MEI) information read from /sys/class/mei/.
func (fs FS) MEIClass() (*MEIClass, error) {
path := fs.sys.Path(meiClassPath)

subdirs, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("failed to list MEI devices at %q: %w", path, err)
}

mei := make(MEIClass, len(subdirs))
for _, d := range subdirs {
dev, err := fs.parseMEI(d.Name())
if err != nil {
return nil, err
}

mei[d.Name()] = dev
}

return &mei, nil
}

func (fs FS) parseMEI(meiDev string) (MEIDev, error) {
path := fs.sys.Path(meiClassPath, meiDev)

var mei MEIDev

files, err := os.ReadDir(path)
if err != nil {
return mei, fmt.Errorf("failed to read directory %q: %w", path, err)
}

for _, f := range files {
if !f.Type().IsRegular() {
continue
}

name := f.Name()
if name == "uevent" {
continue
}

filename := filepath.Join(path, name)
value, err := util.SysReadFile(filename)
if err != nil {
if os.IsPermission(err) {
continue
}

return mei, fmt.Errorf("failed to read file %q: %w", filename, err)
}

switch name {
case "dev":
mei.Dev = &value
case "dev_state":
mei.DevState = &value
case "fw_status":
mei.FWStatus = &value
case "fw_ver":
mei.FWVersion = &value
case "hbm_ver":
mei.HBMVersion = &value
case "hbm_ver_drv":
mei.HBMVersionDrv = &value
case "kind":
mei.Kind = &value
case "trc":
mei.Trc = &value
case "tx_queue_limit":
mei.TxQueueLimit = &value
}
}

return mei, nil
}
77 changes: 77 additions & 0 deletions sysfs/class_mei_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package sysfs

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMEIClass(t *testing.T) {
fs, err := NewFS(sysTestFixtures)
if err != nil {
t.Fatal(err)
}

got, err := fs.MEIClass()
if err != nil {
t.Fatal(err)
}

dev0 := "244:0"
dev1 := "243:0"
devState := "ENABLED"
fwStatus0 := "90000245\n00110500\n00000020\n00000000\n02F41F03\n40000000"
fwStatus1 := "90000245\n00110500\n00000020\n00000004\n02F61F03\n40000000"
fwVer0 := "0:18.0.5.2098\n0:18.0.5.2098\n0:18.0.5.2098"
fwVer1 := "0:18.1.18.2595\n0:18.1.18.2595\n0:18.1.18.2599"
hbmVer := "2.2"
hbmVerDrv := "2.2"
kind := "mei"
trc0 := "00000889"
trc1 := "00000879"
txQueueLimit := "50"

want := &MEIClass{
"mei0": MEIDev{
Dev: &dev0,
DevState: &devState,
FWStatus: &fwStatus0,
FWVersion: &fwVer0,
HBMVersion: &hbmVer,
HBMVersionDrv: &hbmVerDrv,
Kind: &kind,
Trc: &trc0,
TxQueueLimit: &txQueueLimit,
},
"mei1": MEIDev{
Dev: &dev1,
DevState: &devState,
FWStatus: &fwStatus1,
FWVersion: &fwVer1,
HBMVersion: &hbmVer,
HBMVersionDrv: &hbmVerDrv,
Kind: &kind,
Trc: &trc1,
TxQueueLimit: &txQueueLimit,
},
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected MEI class (-want +got):\n%s", diff)
}
}
127 changes: 127 additions & 0 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -6486,6 +6486,133 @@ Lines: 1
4: ACTIVE
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: fixtures/sys/class/mei
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: fixtures/sys/class/mei/mei0
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/dev
Lines: 1
244:0
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/dev_state
Lines: 1
ENABLED
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/fw_status
Lines: 6
90000245
00110500
00000020
00000000
02F41F03
40000000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/fw_ver
Lines: 3
0:18.0.5.2098
0:18.0.5.2098
0:18.0.5.2098
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/hbm_ver
Lines: 1
2.2
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/hbm_ver_drv
Lines: 1
2.2
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/kind
Lines: 1
mei
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/trc
Lines: 1
00000889
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/tx_queue_limit
Lines: 1
50
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei0/uevent
Lines: 3
MAJOR=244
MINOR=0
DEVNAME=mei0
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: fixtures/sys/class/mei/mei1
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/dev
Lines: 1
243:0
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/dev_state
Lines: 1
ENABLED
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/fw_status
Lines: 6
90000245
00110500
00000020
00000004
02F61F03
40000000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/fw_ver
Lines: 3
0:18.1.18.2595
0:18.1.18.2595
0:18.1.18.2599
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/hbm_ver
Lines: 1
2.2
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/hbm_ver_drv
Lines: 1
2.2
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/kind
Lines: 1
mei
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/trc
Lines: 1
00000879
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/tx_queue_limit
Lines: 1
50
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/sys/class/mei/mei1/uevent
Lines: 3
MAJOR=243
MINOR=0
DEVNAME=mei1
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: fixtures/sys/class/net
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down