Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for several /sys/class/sas_* classes #453

Merged
merged 8 commits into from
Dec 17, 2022
5,910 changes: 5,910 additions & 0 deletions fixtures.ttar

Large diffs are not rendered by default.

174 changes: 174 additions & 0 deletions sysfs/class_sas_device.go
@@ -0,0 +1,174 @@
// Copyright 2022 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
// +build linux

package sysfs

import (
"github.com/prometheus/procfs/internal/util"
"io/ioutil"
"path/filepath"
"regexp"
)

const sasDeviceClassPath = "class/sas_device"
scottlaird marked this conversation as resolved.
Show resolved Hide resolved
const sasEndDeviceClassPath = "class/sas_end_device"
const sasExpanderClassPath = "class/sas_expander"

type SASDevice struct {
Name string // /sys/class/sas_device/<Name>
SASAddress string // /sys/class/sas_device/<Name>/sas_address
SASPhys []string // /sys/class/sas_device/<Name>/device/phy-*
SASPorts []string // /sys/class/sas_device/<Name>/device/ports-*
BlockDevices []string // /sys/class/sas_device/<Name>/device/target*/*/block/*
}

type SASDeviceClass map[string]SASDevice

// sasDeviceClasses reads all of the SAS devices from a specific set
// of /sys/class/sas*/ entries. The sas_device, sas_end_device, and
// sas_expander classes are all nearly identical and can be handled by the same basic code.

func (fs FS) parseSASDeviceClass(dir string) (SASDeviceClass, error) {
path := fs.sys.Path(dir)

dirs, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}

sdc := make(SASDeviceClass, len(dirs))

for _, d := range dirs {
device, err := fs.parseSASDevice(d.Name())
if err != nil {
return nil, err
}

sdc[device.Name] = *device
}

return sdc, nil
}

// SASDeviceClass parses devices in /sys/class/sas_device.
func (fs FS) SASDeviceClass() (SASDeviceClass, error) {
return fs.parseSASDeviceClass(sasDeviceClassPath)
}

// SASEndDeviceClass parses devices in /sys/class/sas_end_device.
// This is a subset of sas_device, and excludes expanders.
func (fs FS) SASEndDeviceClass() (SASDeviceClass, error) {
return fs.parseSASDeviceClass(sasEndDeviceClassPath)
}

// SASExpanderClass parses devices in /sys/class/sas_expander.
// This is a subset of sas_device, but only includes expanders.
func (fs FS) SASExpanderClass() (SASDeviceClass, error) {
return fs.parseSASDeviceClass(sasExpanderClassPath)
}

// Parse a single sas_device. This uses /sys/class/sas_device, as
// it's a superset of the other two directories so there's no reason
// to plumb the path through to here.
func (fs FS) parseSASDevice(name string) (*SASDevice, error) {
device := SASDevice{Name: name}

devicepath := fs.sys.Path(filepath.Join(sasDeviceClassPath, name, "device"))

dirs, err := ioutil.ReadDir(devicepath)
if err != nil {
return nil, err
}

phyDevice := regexp.MustCompile(`^phy-[0-9:]+$`)
scottlaird marked this conversation as resolved.
Show resolved Hide resolved
portDevice := regexp.MustCompile(`^port-[0-9:]+$`)

for _, d := range dirs {
if phyDevice.MatchString(d.Name()) {
device.SASPhys = append(device.SASPhys, d.Name())
}
if portDevice.MatchString(d.Name()) {
device.SASPorts = append(device.SASPorts, d.Name())
}
}

address := fs.sys.Path(sasDeviceClassPath, name, "sas_address")
value, err := util.SysReadFile(address)
if err != nil {
return nil, err
}
device.SASAddress = value

device.BlockDevices, err = fs.blockSASDeviceBlockDevices(name)
if err != nil {
return nil, err
}

return &device, nil
}

// Identify block devices that map to a specific SAS Device
// This info comes from (for example)
// /sys/class/sas_device/end_device-11:2/device/target11:0:0/11:0:0:0/block/sdp
//
// To find that, we have to look in the device directory for target$X
// subdirs, then specific subdirs of $X, then read from directory
// names in the 'block/' subdirectory under that. This really
// shouldn't be this hard.
func (fs FS) blockSASDeviceBlockDevices(name string) ([]string, error) {
var devices []string

devicepath := fs.sys.Path(filepath.Join(sasDeviceClassPath, name, "device"))

dirs, err := ioutil.ReadDir(devicepath)
if err != nil {
return nil, err
}

targetDevice := regexp.MustCompile(`^target[0-9:]+$`)
targetSubDevice := regexp.MustCompile(`[0-9]+:.*`)

for _, d := range dirs {
if targetDevice.MatchString(d.Name()) {
targetdir := d.Name()

subtargets, err := ioutil.ReadDir(filepath.Join(devicepath, targetdir))
if err != nil {
return nil, err
}

for _, targetsubdir := range subtargets {

if !targetSubDevice.MatchString(targetsubdir.Name()) {
// need to skip 'power', 'subsys', etc.
continue
}

blocks, err := ioutil.ReadDir(filepath.Join(devicepath, targetdir, targetsubdir.Name(), "block"))

if err != nil {
return nil, err
}

for _, blockdevice := range blocks {
devices = append(devices, blockdevice.Name())
}
}
}
}

return devices, nil
}
155 changes: 155 additions & 0 deletions sysfs/class_sas_device_test.go
@@ -0,0 +1,155 @@
// Copyright 2022 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
// +build linux

package sysfs

import (
"github.com/google/go-cmp/cmp"
"testing"
)

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

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

want := SASDeviceClass{
"end_device-11:0:0": {
Name: "end_device-11:0:0",
SASAddress: "0x5000ccab02009402",
BlockDevices: []string{"sdv"},
},
"end_device-11:0:1": {
Name: "end_device-11:0:1",
SASAddress: "0x5000cca26128b1f5",
BlockDevices: []string{"sdw"},
},
"end_device-11:0:2": {
Name: "end_device-11:0:2",
SASAddress: "0x5000ccab02009406",
BlockDevices: []string{"sdx"},
},
"end_device-11:2": {
Name: "end_device-11:2",
SASAddress: "0x5000cca0506b5f1d",
BlockDevices: []string{"sdp"},
},
"expander-11:0": {
Name: "expander-11:0",
SASAddress: "0x5000ccab0200947e",
SASPhys: []string{
"phy-11:0:10", "phy-11:0:11", "phy-11:0:12",
"phy-11:0:13", "phy-11:0:14", "phy-11:0:15",
"phy-11:0:2", "phy-11:0:4", "phy-11:0:6",
"phy-11:0:7", "phy-11:0:8", "phy-11:0:9",
},
SASPorts: []string{
"port-11:0:0", "port-11:0:1",
"port-11:0:2",
},
},
"expander-11:1": {
Name: "expander-11:1",
SASAddress: "0x5003048001e8967f",
},
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected SASDevice class (-want +got):\n%s", diff)
}
}

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

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

want := SASDeviceClass{
"end_device-11:0:0": {
Name: "end_device-11:0:0",
SASAddress: "0x5000ccab02009402",
BlockDevices: []string{"sdv"},
},
"end_device-11:0:1": {
Name: "end_device-11:0:1",
SASAddress: "0x5000cca26128b1f5",
BlockDevices: []string{"sdw"},
},
"end_device-11:0:2": {
Name: "end_device-11:0:2",
SASAddress: "0x5000ccab02009406",
BlockDevices: []string{"sdx"},
},
"end_device-11:2": {
Name: "end_device-11:2",
SASAddress: "0x5000cca0506b5f1d",
BlockDevices: []string{"sdp"},
},
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected SASDevice class (-want +got):\n%s", diff)
}
}

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

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

want := SASDeviceClass{
"expander-11:0": {
Name: "expander-11:0",
SASAddress: "0x5000ccab0200947e",
SASPhys: []string{
"phy-11:0:10", "phy-11:0:11", "phy-11:0:12",
"phy-11:0:13", "phy-11:0:14", "phy-11:0:15",
"phy-11:0:2", "phy-11:0:4", "phy-11:0:6",
"phy-11:0:7", "phy-11:0:8", "phy-11:0:9",
},
SASPorts: []string{
"port-11:0:0", "port-11:0:1",
"port-11:0:2",
},
},
"expander-11:1": {
Name: "expander-11:1",
SASAddress: "0x5003048001e8967f",
},
}

if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected SASDevice class (-want +got):\n%s", diff)
}
}