Skip to content

Commit

Permalink
added logic to extract subsystem information from lspci command (#57)
Browse files Browse the repository at this point in the history
* address review comments and include the lib-exports changes
Signed-off-by: Nishant Parekh <nparekh@redhat.com>
  • Loading branch information
nishant-parekh authored Aug 14, 2024
1 parent 361da7d commit 282b95f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ install-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GO_PATH}/bin ${GOLANGCI_VERSION}
vet:
go vet ${GO_PACKAGES}
test:
go test ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.21.4
require github.com/sirupsen/logrus v1.9.3

require (
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.4
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.5
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.4 h1:66qE0Vj/8vWvt8yaVq1yJK5+OOELGZdlCwzHkXRxFr4=
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.4/go.mod h1:NO9mpaj5neVKarRGcsL5SHVFb+uYUoCC8uVbuT419xs=
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.5 h1:0X6pIv4tMsSquGS4m+xgmzFGIm16Snj2ErmnpoYqLog=
github.com/redhat-best-practices-for-k8s/l2discovery-exports v0.0.5/go.mod h1:NO9mpaj5neVKarRGcsL5SHVFb+uYUoCC8uVbuT419xs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
33 changes: 26 additions & 7 deletions l2discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
exports "github.com/redhat-best-practices-for-k8s/l2discovery-exports"
"github.com/sirupsen/logrus"
)

/*
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -348,9 +347,10 @@ func getIfs() (macs map[string]*exports.Iface, macsExist map[string]bool, err er
func getPci(ifaceName string) (aPciAddress exports.PCIAddress, err error) {
const (
ethtoolBaseCommand = "ethtool -i"
lscpiCommand = "lspci -s"
lscpiCommand = "lspci -vv -s"
newLineCharacter = "\n"
emptySpaceSeparator = " "
subsystemString = "Subsystem: "
)
aCommand := fmt.Sprintf("%s %s", ethtoolBaseCommand, ifaceName)
stdout, stderr, err := RunLocalCommand(aCommand)
Expand All @@ -360,6 +360,7 @@ func getPci(ifaceName string) (aPciAddress exports.PCIAddress, err error) {

r := regexp.MustCompile(`(?m)bus-info: (.*)\.(\d+)$`)
for _, submatches := range r.FindAllStringSubmatchIndex(stdout, -1) {

aPciAddress.Device = string(r.ExpandString([]byte{}, "$1", stdout, submatches))
aPciAddress.Function = string(r.ExpandString([]byte{}, "$2", stdout, submatches))
}
Expand All @@ -369,16 +370,34 @@ func getPci(ifaceName string) (aPciAddress exports.PCIAddress, err error) {
if err != nil || stderr != "" {
return aPciAddress, fmt.Errorf("could not execute lspci command, err=%s stderr=%s", err, stderr)
}
stdout = strings.TrimSuffix(stdout, newLineCharacter)
spaceIndex := strings.Index(stdout, emptySpaceSeparator)
if spaceIndex != -1 {
description := stdout[spaceIndex+1:]
aPciAddress.Description = description

description, subsystem, err := parseLspci(stdout)
if err != nil {
return aPciAddress, fmt.Errorf("could not parse lspci output, err=%s", err)
}
aPciAddress.Description = description
aPciAddress.Subsystem = subsystem

return aPciAddress, nil
}

func parseLspci(output string) (string, string, error) {
const regex = `(?m)[^\s]*\s*(.*)$(?m)\s+Subsystem:\s*(.*)$`

// Compile the regular expression
re := regexp.MustCompile(regex)

// Find all matches
matches := re.FindAllStringSubmatch(output, -1)
var description, subsystem string
if len(matches) < 1 {
return description, subsystem, fmt.Errorf("could not parse lspci output")
}
description=matches[0][1]
subsystem=matches[0][2]
return description, subsystem, nil
}

func getPtpCaps(ifaceName string, runCmd func(command string) (outStr, errStr string, err error)) (aPTPCaps exports.PTPCaps, err error) {
const (
ethtoolBaseCommand = "ethtool -T "
Expand Down
41 changes: 41 additions & 0 deletions l2discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import "testing"

func Test_parseLspci(t *testing.T) {
type args struct {
output string
}
tests := []struct {
name string
args args
exDesc string
exSubsystem string
parseErr bool
}{
{
name: "ok",
args: args{
output: "ca:00.0 Ethernet controller: Intel Corporation Ethernet Controller E810-C for SFP (rev 02)\nSubsystem: Intel Corporation Ethernet Network Adapter E810-XXV-4T\nControl: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx+\n",
},
exDesc: "Ethernet controller: Intel Corporation Ethernet Controller E810-C for SFP (rev 02)",
exSubsystem: "Intel Corporation Ethernet Network Adapter E810-XXV-4T",
parseErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
desc, subsystem, err := parseLspci(tt.args.output)
if (err != nil) != tt.parseErr {
t.Errorf("parseLspci() error = %v, wantErr %v", err, tt.parseErr)
return
}
if desc != tt.exDesc {
t.Errorf("parseLspci() description does not match got = %v, want %v", desc, tt.exDesc)
}
if subsystem != tt.exSubsystem {
t.Errorf("parseLspci() subsystem does not match got = %v, want %v", subsystem, tt.exSubsystem)
}
})
}
}
2 changes: 1 addition & 1 deletion scripts/image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ set -x
VERSION=latest
IMAGE_TAG=l2discovery
REPO=quay.io/redhat-best-practices-for-k8s
make test
go build l2discovery.go
podman build -t ${IMAGE_TAG} --rm -f Dockerfile .

podman tag ${IMAGE_TAG} ${REPO}/${IMAGE_TAG}:${VERSION}
podman push ${REPO}/${IMAGE_TAG}:${VERSION}

0 comments on commit 282b95f

Please sign in to comment.