Skip to content
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
30 changes: 17 additions & 13 deletions cmd/vfio-manage/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ package main
import (
"fmt"

"github.com/NVIDIA/go-nvlib/pkg/nvpci"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/NVIDIA/k8s-driver-manager/internal/nvpci"
"github.com/NVIDIA/k8s-driver-manager/internal/nvpassthrough"
)

type bindCommand struct {
logger *logrus.Logger
nvpciLib nvpci.Interface
options bindOptions
logger *logrus.Logger
nvpci nvpci.Interface
nvpassthrough nvpassthrough.Interface
options bindOptions
}

type bindOptions struct {
Expand Down Expand Up @@ -104,9 +106,13 @@ func (m bindCommand) validateFlags() error {
}

func (m bindCommand) run() error {
m.nvpciLib = nvpci.New(
m.nvpci = nvpci.New(
nvpci.WithLogger(m.logger),
nvpci.WithHostRoot(m.options.hostRoot),
)

m.nvpassthrough = nvpassthrough.New(
nvpassthrough.WithLogger(m.logger),
nvpassthrough.WithHostRoot(m.options.hostRoot),
)

if m.options.deviceID != "" {
Expand All @@ -117,13 +123,13 @@ func (m bindCommand) run() error {
}

func (m bindCommand) bindAll() error {
devices, err := m.nvpciLib.GetGPUs()
devices, err := m.nvpci.GetGPUs()
if err != nil {
return fmt.Errorf("failed to get NVIDIA GPUs: %w", err)
}

if m.options.bindNVSwitches {
nvswitches, err := m.nvpciLib.GetNVSwitches()
nvswitches, err := m.nvpci.GetNVSwitches()
if err != nil {
return fmt.Errorf("failed to get NVIDIA NVSwitches: %w", err)
}
Expand All @@ -132,8 +138,7 @@ func (m bindCommand) bindAll() error {

for _, dev := range devices {
m.logger.Infof("Binding device %s", dev.Address)
// (cdesiniotis) ideally this should be replaced by a call to nvdev.BindToVFIODriver()
if err := m.nvpciLib.BindToVFIODriver(dev); err != nil {
if err := m.nvpassthrough.BindToVFIODriver(dev); err != nil {
m.logger.Warnf("Failed to bind device %s: %v", dev.Address, err)
}
}
Expand All @@ -145,7 +150,7 @@ func (m bindCommand) bindDevice() error {
device := m.options.deviceID
// Note: Despite its name, GetGPUByPciBusID returns any NVIDIA PCI device
// (GPU, NVSwitch, etc.) at the specified address, not just GPUs.
nvdev, err := m.nvpciLib.GetGPUByPciBusID(device)
nvdev, err := m.nvpci.GetGPUByPciBusID(device)
if err != nil {
return fmt.Errorf("failed to get NVIDIA device: %w", err)
}
Expand All @@ -164,8 +169,7 @@ func (m bindCommand) bindDevice() error {

m.logger.Infof("Binding device %s", device)

// (cdesiniotis) ideally this should be replaced by a call to nvdev.BindToVFIODriver()
if err := m.nvpciLib.BindToVFIODriver(nvdev); err != nil {
if err := m.nvpassthrough.BindToVFIODriver(nvdev); err != nil {
return fmt.Errorf("failed to bind device %s to vfio driver: %w", device, err)
}

Expand Down
27 changes: 15 additions & 12 deletions cmd/vfio-manage/unbind.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ package main
import (
"fmt"

"github.com/NVIDIA/go-nvlib/pkg/nvpci"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/NVIDIA/k8s-driver-manager/internal/nvpci"
"github.com/NVIDIA/k8s-driver-manager/internal/nvpassthrough"
)

type unbindCommand struct {
logger *logrus.Logger
nvpciLib nvpci.Interface
options unbindOptions
logger *logrus.Logger
nvpci nvpci.Interface
nvpassthrough nvpassthrough.Interface
options unbindOptions
}

type unbindOptions struct {
Expand All @@ -43,9 +45,12 @@ type unbindOptions struct {
func newUnbindCommand(logger *logrus.Logger) *cli.Command {
c := unbindCommand{
logger: logger,
nvpciLib: nvpci.New(
nvpci: nvpci.New(
nvpci.WithLogger(logger),
),
nvpassthrough: nvpassthrough.New(
nvpassthrough.WithLogger(logger),
),
}
return c.build()
}
Expand Down Expand Up @@ -107,13 +112,13 @@ func (m unbindCommand) run() error {
}

func (m unbindCommand) unbindAll() error {
devices, err := m.nvpciLib.GetGPUs()
devices, err := m.nvpci.GetGPUs()
if err != nil {
return fmt.Errorf("failed to get NVIDIA GPUs: %w", err)
}

if m.options.unbindNVSwitches {
nvswitches, err := m.nvpciLib.GetNVSwitches()
nvswitches, err := m.nvpci.GetNVSwitches()
if err != nil {
return fmt.Errorf("failed to get NVIDIA NVSwitches: %w", err)
}
Expand All @@ -122,8 +127,7 @@ func (m unbindCommand) unbindAll() error {

for _, dev := range devices {
m.logger.Infof("Unbinding device %s", dev.Address)
// (cdesiniotis) ideally this should be replaced by a call to nvdev.UnbindFromDriver()
if err := m.nvpciLib.UnbindFromDriver(dev); err != nil {
if err := m.nvpassthrough.UnbindFromDriver(dev); err != nil {
m.logger.Warnf("Failed to unbind device %s: %v", dev.Address, err)
}
}
Expand All @@ -134,7 +138,7 @@ func (m unbindCommand) unbindDevice() error {
device := m.options.deviceID
// Note: Despite its name, GetGPUByPciBusID returns any NVIDIA PCI device
// (GPU, NVSwitch, etc.) at the specified address, not just GPUs.
nvdev, err := m.nvpciLib.GetGPUByPciBusID(device)
nvdev, err := m.nvpci.GetGPUByPciBusID(device)
if err != nil {
return fmt.Errorf("failed to get NVIDIA device: %w", err)
}
Expand All @@ -153,8 +157,7 @@ func (m unbindCommand) unbindDevice() error {

m.logger.Infof("Unbinding device %s", device)

// (cdesiniotis) ideally this should be replaced by a call to nvdev.UnbindFromDriver()
if err := m.nvpciLib.UnbindFromDriver(nvdev); err != nil {
if err := m.nvpassthrough.UnbindFromDriver(nvdev); err != nil {
return fmt.Errorf("failed to unbind device %s from driver: %w", device, err)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package nvpci
package nvpassthrough

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
package nvpci
/*
* Copyright (c) NVIDIA CORPORATION. All rights reserved.
*
* 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.
*/

package nvpassthrough

import (
"testing"
Expand Down
Loading