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

feat(host): NewPciDevice add executor #19999

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/baremetal/tasks/baseprepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func getIsolatedDevicesInfo(cli *ssh.Client, ip net.IP) ([]*isolated_device.PCID
devs := []*isolated_device.PCIDevice{}
for _, line := range lines {
if len(line) > 0 {
dev := isolated_device.NewPCIDevice2(line)
dev := isolated_device.NewPCIDevice2(line, cli)
if len(dev.Addr) > 0 && utils.IsInArray(dev.ClassCode, isolated_device.GpuClassCodes) && !isBootVga(cli, dev, bootVgaPath) {
devs = append(devs, dev)
}
Expand Down
56 changes: 44 additions & 12 deletions pkg/hostman/isolated_device/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func getGPUPCIStr() ([]string, error) {
return lines, err
}

type IExecutor interface {
RunCmd(cmd string) ([]string, error)
}

var defaultExecutor IExecutor = new(SDefaultExecutor)

func GetDefaultExecutor() IExecutor {
return defaultExecutor
}

type SDefaultExecutor struct{}

func (*SDefaultExecutor) RunCmd(cmd string) ([]string, error) {
return bashOutput(cmd)
}

type PCIDevice struct {
Addr string `json:"bus_id"`
ClassName string `json:"class_name"`
Expand All @@ -165,11 +181,23 @@ type PCIDevice struct {
PCIEInfo *api.IsolatedDevicePCIEInfo `json:"pcie_info"`
}

func NewPCIDevice(line string) (*PCIDevice, error) {
if len(line) == 0 {
func NewPCIDevice(addr string, executors ...IExecutor) (*PCIDevice, error) {
if len(addr) == 0 {
return nil, errors.Errorf("input line is empty")
}
dev := NewPCIDevice2(line)

var executor IExecutor
if len(executors) == 0 {
executor = GetDefaultExecutor()
} else {
executor = executors[0]
}
ret, err := executor.RunCmd(fmt.Sprintf("lspci -nnmm -s %s", addr))
if err != nil {
return nil, errors.Wrapf(err, "run lspci -nnmm -s %s", addr)
}

dev := NewPCIDevice2(strings.Join(ret, ""))
if err := dev.checkSameIOMMUGroupDevice(); err != nil {
return nil, err
}
Expand All @@ -179,9 +207,16 @@ func NewPCIDevice(line string) (*PCIDevice, error) {
return dev, nil
}

func NewPCIDevice2(line string) *PCIDevice {
func NewPCIDevice2(line string, executors ...IExecutor) *PCIDevice {
var executor IExecutor
if len(executors) == 0 {
executor = GetDefaultExecutor()
} else {
executor = executors[0]
}

dev := parseLspci(line)
if err := dev.fillPCIEInfo(); err != nil {
if err := dev.fillPCIEInfo(executor); err != nil {
log.Warningf("fillPCIEInfo for line: %q, device: %s, error: %v", line, dev.String(), err)
}
return dev
Expand Down Expand Up @@ -409,12 +444,13 @@ func (d *PCIDevice) bindDriver() error {
)
}

func (d *PCIDevice) fillPCIEInfo() error {
func (d *PCIDevice) fillPCIEInfo(executor IExecutor) error {
if d.Addr == "" {
return errors.Errorf("device address is empty: %s", d.String())
}

cmd := fmt.Sprintf("lspci -vvv -s %s", d.Addr)
lines, err := bashOutput(cmd)
lines, err := executor.RunCmd(cmd)
if err != nil {
return errors.Wrapf(err, "execute cmd: %s", cmd)
}
Expand Down Expand Up @@ -554,11 +590,7 @@ func (g *IOMMUGroup) String() string {
}

func detectPCIDevByAddr(addr string) (*PCIDevice, error) {
ret, err := bashOutput(fmt.Sprintf("lspci -nnmm -s %s", addr))
if err != nil {
return nil, err
}
return NewPCIDevice(strings.Join(ret, ""))
return NewPCIDevice(addr)
}

func detectPCIDevByAddrWithoutIOMMUGroup(addr string) (*PCIDevice, error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (s *Client) RawRun(cmds ...string) ([]string, error) {
return s.run(false, cmds, nil, false)
}

func (s *Client) RunCmd(cmd string) ([]string, error) {
return s.Run(cmd)
}

func (s *Client) Run(cmds ...string) ([]string, error) {
return s.run(true, cmds, nil, false)
}
Expand Down
Loading