diff --git a/pkg/sentry/devices/nvproxy/BUILD b/pkg/sentry/devices/nvproxy/BUILD index 1551f9444..853a2bb9a 100644 --- a/pkg/sentry/devices/nvproxy/BUILD +++ b/pkg/sentry/devices/nvproxy/BUILD @@ -115,6 +115,7 @@ go_test( deps = [ ":nvproxy", "//pkg/abi/nvgpu", + "//pkg/sentry/devices/nvproxy/nvconf", "//pkg/test/testutil", "//tools/nvidia_driver_differ/parser", ], diff --git a/pkg/sentry/devices/nvproxy/nvconf/BUILD b/pkg/sentry/devices/nvproxy/nvconf/BUILD index bfa081aa7..567fe0a15 100644 --- a/pkg/sentry/devices/nvproxy/nvconf/BUILD +++ b/pkg/sentry/devices/nvproxy/nvconf/BUILD @@ -9,8 +9,10 @@ go_library( srcs = [ "caps.go", "nvconf.go", + "version.go", ], visibility = [ "//pkg/sentry:internal", + "//tools:__subpackages__", ], ) diff --git a/pkg/sentry/devices/nvproxy/nvconf/version.go b/pkg/sentry/devices/nvproxy/nvconf/version.go new file mode 100644 index 000000000..874f5eddf --- /dev/null +++ b/pkg/sentry/devices/nvproxy/nvconf/version.go @@ -0,0 +1,96 @@ +// Copyright 2025 The gVisor 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. + +package nvconf + +import ( + "fmt" + "strconv" + "strings" +) + +// DriverVersion represents a NVIDIA driver version patch release. +// +// +stateify savable +type DriverVersion struct { + major int + minor int + patch int +} + +// NewDriverVersion returns a new driver version. +func NewDriverVersion(major, minor, patch int) DriverVersion { + return DriverVersion{major, minor, patch} +} + +// DriverVersionFrom returns a DriverVersion from a string. +func DriverVersionFrom(version string) (DriverVersion, error) { + parts := strings.Split(version, ".") + if len(parts) != 3 { + return DriverVersion{}, fmt.Errorf("invalid format of version string %q", version) + } + var ( + res DriverVersion + err error + ) + res.major, err = strconv.Atoi(parts[0]) + if err != nil { + return DriverVersion{}, fmt.Errorf("invalid format for major version %q: %v", version, err) + } + res.minor, err = strconv.Atoi(parts[1]) + if err != nil { + return DriverVersion{}, fmt.Errorf("invalid format for minor version %q: %v", version, err) + } + res.patch, err = strconv.Atoi(parts[2]) + if err != nil { + return DriverVersion{}, fmt.Errorf("invalid format for patch version %q: %v", version, err) + } + return res, nil +} + +func (v DriverVersion) String() string { + return fmt.Sprintf("%02d.%02d.%02d", v.major, v.minor, v.patch) +} + +// Equals returns true if the two driver versions are equal. +func (v DriverVersion) Equals(other DriverVersion) bool { + return v.major == other.major && v.minor == other.minor && v.patch == other.patch +} + +// IsGreaterThan returns true if v is greater than other. +// isGreaterThan returns true if v is more recent than other, assuming v and other are on the same +// dev branch. +func (v DriverVersion) IsGreaterThan(other DriverVersion) bool { + switch { + case v.major > other.major: + return true + case other.major > v.major: + return false + case v.minor > other.minor: + return true + case other.minor > v.minor: + return false + case v.patch > other.patch: + return true + case other.patch > v.patch: + return false + default: + return true + } +} + +// Major returns the major version number. +func (v DriverVersion) Major() int { + return v.major +} diff --git a/pkg/sentry/devices/nvproxy/nvproxy.go b/pkg/sentry/devices/nvproxy/nvproxy.go index d2e00f653..14c9de7b5 100644 --- a/pkg/sentry/devices/nvproxy/nvproxy.go +++ b/pkg/sentry/devices/nvproxy/nvproxy.go @@ -30,17 +30,13 @@ import ( ) // Register registers all devices implemented by this package in vfsObj. -func Register(vfsObj *vfs.VirtualFilesystem, versionStr string, driverCaps nvconf.DriverCaps, uvmDevMajor uint32) error { +func Register(vfsObj *vfs.VirtualFilesystem, version nvconf.DriverVersion, driverCaps nvconf.DriverCaps, uvmDevMajor uint32) error { // The kernel driver's interface is unstable, so only allow versions of the // driver that are known to be supported. - log.Infof("NVIDIA driver version: %s", versionStr) - version, err := DriverVersionFrom(versionStr) - if err != nil { - return fmt.Errorf("failed to parse Nvidia driver version %s: %w", versionStr, err) - } + log.Infof("NVIDIA driver version: %s", version) abiCons, ok := abis[version] if !ok { - return fmt.Errorf("unsupported Nvidia driver version: %s", versionStr) + return fmt.Errorf("unsupported Nvidia driver version: %s", version) } if driverCaps == 0 { log.Warningf("nvproxy: NVIDIA driver capability set is empty; all GPU operations will fail") @@ -76,7 +72,7 @@ func Register(vfsObj *vfs.VirtualFilesystem, versionStr string, driverCaps nvcon // +stateify savable type nvproxy struct { abi *driverABI `state:"nosave"` - version DriverVersion + version nvconf.DriverVersion capsEnabled nvconf.DriverCaps fdsMu fdsMutex `state:"nosave"` diff --git a/pkg/sentry/devices/nvproxy/nvproxy_driver_parity_test.go b/pkg/sentry/devices/nvproxy/nvproxy_driver_parity_test.go index d192df77a..9e96f8387 100644 --- a/pkg/sentry/devices/nvproxy/nvproxy_driver_parity_test.go +++ b/pkg/sentry/devices/nvproxy/nvproxy_driver_parity_test.go @@ -32,6 +32,7 @@ import ( "gvisor.dev/gvisor/pkg/test/testutil" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" "gvisor.dev/gvisor/tools/nvidia_driver_differ/parser" ) @@ -56,7 +57,7 @@ func createParserRunner(t *testing.T) (*os.File, *parser.Runner) { return parserFile, runner } -func getDriverDefs(t *testing.T, runner *parser.Runner, version nvproxy.DriverVersion) ([]nvproxy.DriverStructName, *parser.OutputJSON) { +func getDriverDefs(t *testing.T, runner *parser.Runner, version nvconf.DriverVersion) ([]nvproxy.DriverStructName, *parser.OutputJSON) { t.Helper() structNames, ok := nvproxy.SupportedStructNames(version) @@ -84,7 +85,7 @@ func TestSupportedStructNames(t *testing.T) { nvproxy.Init() // Run the parser on all supported driver versions - nvproxy.ForEachSupportDriver(func(version nvproxy.DriverVersion, checksum string) { + nvproxy.ForEachSupportDriver(func(version nvconf.DriverVersion, checksum string) { t.Run(version.String(), func(t *testing.T) { t.Parallel() f, runner := createParserRunner(t) @@ -109,7 +110,7 @@ func TestSupportedStructNames(t *testing.T) { func TestStructDefinitionParity(t *testing.T) { nvproxy.Init() - nvproxy.ForEachSupportDriver(func(version nvproxy.DriverVersion, checksum string) { + nvproxy.ForEachSupportDriver(func(version nvconf.DriverVersion, checksum string) { t.Run(version.String(), func(t *testing.T) { t.Parallel() f, runner := createParserRunner(t) diff --git a/pkg/sentry/devices/nvproxy/version.go b/pkg/sentry/devices/nvproxy/version.go index 60f0c9f25..67ce65bdf 100644 --- a/pkg/sentry/devices/nvproxy/version.go +++ b/pkg/sentry/devices/nvproxy/version.go @@ -18,84 +18,12 @@ import ( "fmt" "reflect" "sort" - "strconv" - "strings" "gvisor.dev/gvisor/pkg/abi/nvgpu" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" "gvisor.dev/gvisor/pkg/sync" ) -// DriverVersion represents a NVIDIA driver version patch release. -// -// +stateify savable -type DriverVersion struct { - major int - minor int - patch int -} - -// NewDriverVersion returns a new driver version. -func NewDriverVersion(major, minor, patch int) DriverVersion { - return DriverVersion{major, minor, patch} -} - -// DriverVersionFrom returns a DriverVersion from a string. -func DriverVersionFrom(version string) (DriverVersion, error) { - parts := strings.Split(version, ".") - if len(parts) != 3 { - return DriverVersion{}, fmt.Errorf("invalid format of version string %q", version) - } - var ( - res DriverVersion - err error - ) - res.major, err = strconv.Atoi(parts[0]) - if err != nil { - return DriverVersion{}, fmt.Errorf("invalid format for major version %q: %v", version, err) - } - res.minor, err = strconv.Atoi(parts[1]) - if err != nil { - return DriverVersion{}, fmt.Errorf("invalid format for minor version %q: %v", version, err) - } - res.patch, err = strconv.Atoi(parts[2]) - if err != nil { - return DriverVersion{}, fmt.Errorf("invalid format for patch version %q: %v", version, err) - } - return res, nil -} - -func (v DriverVersion) String() string { - return fmt.Sprintf("%02d.%02d.%02d", v.major, v.minor, v.patch) -} - -// Equals returns true if the two driver versions are equal. -func (v DriverVersion) Equals(other DriverVersion) bool { - return v.major == other.major && v.minor == other.minor && v.patch == other.patch -} - -// isGreaterThan returns true if v is greater than other. -// isGreaterThan returns true if v is more recent than other, assuming v and other are on the same -// dev branch. -func (v DriverVersion) isGreaterThan(other DriverVersion) bool { - switch { - case v.major > other.major: - return true - case other.major > v.major: - return false - case v.minor > other.minor: - return true - case other.minor > v.minor: - return false - case v.patch > other.patch: - return true - case other.patch > v.patch: - return false - default: - return true - } -} - // A driverABIFunc constructs and returns a driverABI. // This indirection exists to avoid memory usage from unused driver ABIs. type driverABIFunc func() *driverABI @@ -154,7 +82,7 @@ type DriverStruct struct { // abis is a global map containing all supported Nvidia driver ABIs. This is // initialized on Init() and is immutable henceforth. -var abis map[DriverVersion]abiConAndChecksum +var abis map[nvconf.DriverVersion]abiConAndChecksum var abisOnce sync.Once // Note: runfileChecksum is the checksum of the .run file of the driver installer for linux from @@ -163,9 +91,9 @@ var abisOnce sync.Once // Run `make sudo TARGETS=//tools/gpu:main ARGS="checksum --version={}"` to get checksum. func addDriverABI(major, minor, patch int, runfileChecksum string, cons driverABIFunc) driverABIFunc { if abis == nil { - abis = make(map[DriverVersion]abiConAndChecksum) + abis = make(map[nvconf.DriverVersion]abiConAndChecksum) } - version := NewDriverVersion(major, minor, patch) + version := nvconf.NewDriverVersion(major, minor, patch) abis[version] = abiConAndChecksum{cons: cons, checksum: runfileChecksum} return cons } @@ -908,7 +836,7 @@ func newDriverStruct(paramType reflect.Type, name string) DriverStruct { // ForEachSupportDriver calls f on all supported drivers. // Precondition: Init() must have been called. -func ForEachSupportDriver(f func(version DriverVersion, checksum string)) { +func ForEachSupportDriver(f func(version nvconf.DriverVersion, checksum string)) { for version, abi := range abis { f(version, abi.checksum) } @@ -916,10 +844,10 @@ func ForEachSupportDriver(f func(version DriverVersion, checksum string)) { // LatestDriver returns the latest supported driver. // Precondition: Init() must have been called. -func LatestDriver() DriverVersion { - var ret DriverVersion +func LatestDriver() nvconf.DriverVersion { + var ret nvconf.DriverVersion for version := range abis { - if version.isGreaterThan(ret) { + if version.IsGreaterThan(ret) { ret = version } } @@ -928,20 +856,20 @@ func LatestDriver() DriverVersion { // SupportedDrivers returns a list of all supported drivers. // Precondition: Init() must have been called. -func SupportedDrivers() []DriverVersion { - var ret []DriverVersion +func SupportedDrivers() []nvconf.DriverVersion { + var ret []nvconf.DriverVersion for version := range abis { ret = append(ret, version) } sort.Slice(ret, func(i, j int) bool { - return !ret[i].isGreaterThan(ret[j]) + return !ret[i].IsGreaterThan(ret[j]) }) return ret } // ExpectedDriverChecksum returns the expected checksum for a given version. // Precondition: Init() must have been called. -func ExpectedDriverChecksum(version DriverVersion) (string, bool) { +func ExpectedDriverChecksum(version nvconf.DriverVersion) (string, bool) { abi, ok := abis[version] if !ok { return "", false @@ -951,7 +879,7 @@ func ExpectedDriverChecksum(version DriverVersion) (string, bool) { // SupportedIoctls returns the ioctl numbers that are supported by nvproxy at // a given version. -func SupportedIoctls(version DriverVersion) (frontendIoctls map[uint32]struct{}, uvmIoctls map[uint32]struct{}, controlCmds map[uint32]struct{}, allocClasses map[uint32]struct{}, ok bool) { +func SupportedIoctls(version nvconf.DriverVersion) (frontendIoctls map[uint32]struct{}, uvmIoctls map[uint32]struct{}, controlCmds map[uint32]struct{}, allocClasses map[uint32]struct{}, ok bool) { abiCons, ok := abis[version] if !ok { return nil, nil, nil, nil, false @@ -978,7 +906,7 @@ func SupportedIoctls(version DriverVersion) (frontendIoctls map[uint32]struct{}, // SupportedStructNames returns the list of struct names supported by the given driver version. // It merges the frontend, uvm, control, and allocation names into one slice. -func SupportedStructNames(version DriverVersion) ([]DriverStructName, bool) { +func SupportedStructNames(version nvconf.DriverVersion) ([]DriverStructName, bool) { namesCons, ok := abis[version] if !ok { return nil, false @@ -1011,7 +939,7 @@ func SupportedStructNames(version DriverVersion) ([]DriverStructName, bool) { // SupportedStructTypes returns the list of struct types supported by the given driver version. // It merges the frontend, uvm, control, and allocation names into one slice. -func SupportedStructTypes(version DriverVersion) ([]DriverStruct, bool) { +func SupportedStructTypes(version nvconf.DriverVersion) ([]DriverStruct, bool) { abiCons, ok := abis[version] if !ok { return nil, false diff --git a/runsc/boot/loader.go b/runsc/boot/loader.go index 2f9729cd6..24104639e 100644 --- a/runsc/boot/loader.go +++ b/runsc/boot/loader.go @@ -153,7 +153,7 @@ type containerInfo struct { // nvidiaDriverVersion is the NVIDIA driver ABI version to use for // communicating with NVIDIA devices on the host. - nvidiaDriverVersion string + nvidiaDriverVersion nvconf.DriverVersion } type loaderState int @@ -351,7 +351,7 @@ type Args struct { ProfileOpts profile.Opts // NvidiaDriverVersion is the NVIDIA driver ABI version to use for // communicating with NVIDIA devices on the host. - NvidiaDriverVersion string + NvidiaDriverVersion nvconf.DriverVersion // HostTHP contains host transparent hugepage settings. HostTHP HostTHP diff --git a/runsc/cmd/BUILD b/runsc/cmd/BUILD index 0388b5c9e..7391fd44c 100644 --- a/runsc/cmd/BUILD +++ b/runsc/cmd/BUILD @@ -90,6 +90,7 @@ go_library( "//pkg/prometheus", "//pkg/ring0", "//pkg/sentry/control", + "//pkg/sentry/devices/nvproxy/nvconf", "//pkg/sentry/devices/tpuproxy", "//pkg/sentry/devices/tpuproxy/vfio", "//pkg/sentry/hostmm", diff --git a/runsc/cmd/boot.go b/runsc/cmd/boot.go index 2b78c6606..a590d16eb 100644 --- a/runsc/cmd/boot.go +++ b/runsc/cmd/boot.go @@ -37,6 +37,7 @@ import ( "gvisor.dev/gvisor/pkg/metric" "gvisor.dev/gvisor/pkg/prometheus" "gvisor.dev/gvisor/pkg/ring0" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" "gvisor.dev/gvisor/pkg/sentry/hostmm" "gvisor.dev/gvisor/pkg/sentry/platform" "gvisor.dev/gvisor/runsc/boot" @@ -493,6 +494,14 @@ func (b *Boot) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomma log.Infof("Core tag enabled (core tag=%d)", coreTags[0]) } + var nvidiaDriverVersion nvconf.DriverVersion + if b.nvidiaDriverVersion != "" { + nvidiaDriverVersion, err = nvconf.DriverVersionFrom(b.nvidiaDriverVersion) + if err != nil { + util.Fatalf("Failed to parse nvidia driver version: %v", err) + } + } + // Create the loader. bootArgs := boot.Args{ ID: f.Arg(0), @@ -515,7 +524,7 @@ func (b *Boot) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomma PodInitConfigFD: b.podInitConfigFD, SinkFDs: b.sinkFDs.GetArray(), ProfileOpts: b.profileFDs.ToOpts(), - NvidiaDriverVersion: b.nvidiaDriverVersion, + NvidiaDriverVersion: nvidiaDriverVersion, HostTHP: b.hostTHP, SaveFDs: b.saveFDs.GetFDs(), } diff --git a/runsc/sandbox/BUILD b/runsc/sandbox/BUILD index e926ce5cb..45e38bcea 100644 --- a/runsc/sandbox/BUILD +++ b/runsc/sandbox/BUILD @@ -33,6 +33,7 @@ go_library( "//pkg/prometheus", "//pkg/sentry/control", "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", "//pkg/sentry/fsimpl/erofs", "//pkg/sentry/pgalloc", "//pkg/sentry/platform", diff --git a/runsc/sandbox/sandbox.go b/runsc/sandbox/sandbox.go index 639a77006..a56c2d656 100644 --- a/runsc/sandbox/sandbox.go +++ b/runsc/sandbox/sandbox.go @@ -47,6 +47,7 @@ import ( "gvisor.dev/gvisor/pkg/prometheus" "gvisor.dev/gvisor/pkg/sentry/control" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" "gvisor.dev/gvisor/pkg/sentry/fsimpl/erofs" "gvisor.dev/gvisor/pkg/sentry/pgalloc" "gvisor.dev/gvisor/pkg/sentry/platform" @@ -1747,7 +1748,7 @@ func getNvproxyDriverVersion(conf *config.Config) (string, error) { nvproxy.Init() return nvproxy.LatestDriver().String(), nil default: - version, err := nvproxy.DriverVersionFrom(conf.NVProxyDriverVersion) + version, err := nvconf.DriverVersionFrom(conf.NVProxyDriverVersion) return version.String(), err } } diff --git a/tools/gpu/BUILD b/tools/gpu/BUILD index 8a9ed4e09..99e87224a 100644 --- a/tools/gpu/BUILD +++ b/tools/gpu/BUILD @@ -11,6 +11,7 @@ go_binary( deps = [ "//pkg/log", "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", "//runsc/flag", "//tools/gpu/drivers", ], diff --git a/tools/gpu/drivers/BUILD b/tools/gpu/drivers/BUILD index 11f75eb7e..057466dd7 100644 --- a/tools/gpu/drivers/BUILD +++ b/tools/gpu/drivers/BUILD @@ -12,6 +12,7 @@ go_library( deps = [ "//pkg/log", "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", ], ) @@ -19,5 +20,5 @@ go_test( name = "drivers_test", srcs = ["install_driver_test.go"], library = ":drivers", - deps = ["//pkg/sentry/devices/nvproxy"], + deps = ["//pkg/sentry/devices/nvproxy/nvconf"], ) diff --git a/tools/gpu/drivers/install_driver.go b/tools/gpu/drivers/install_driver.go index 091f29401..2576eeda8 100644 --- a/tools/gpu/drivers/install_driver.go +++ b/tools/gpu/drivers/install_driver.go @@ -28,6 +28,7 @@ import ( "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" ) const ( @@ -42,10 +43,10 @@ func init() { // Installer handles the logic to install drivers. type Installer struct { - requestedVersion nvproxy.DriverVersion + requestedVersion nvconf.DriverVersion // include functions so they can be mocked in tests. - expectedChecksumFunc func(nvproxy.DriverVersion) (string, bool) - getCurrentDriverFunc func() (nvproxy.DriverVersion, error) + expectedChecksumFunc func(nvconf.DriverVersion) (string, bool) + getCurrentDriverFunc func() (nvconf.DriverVersion, error) downloadFunc func(context.Context, string) (io.ReadCloser, error) installFunc func(string) error } @@ -62,7 +63,7 @@ func NewInstaller(requestedVersion string, latest bool) (*Installer, error) { case latest: ret.requestedVersion = nvproxy.LatestDriver() default: - d, err := nvproxy.DriverVersionFrom(requestedVersion) + d, err := nvconf.DriverVersionFrom(requestedVersion) if err != nil { return nil, fmt.Errorf("failed to parse requested driver version: %w", err) } @@ -89,7 +90,7 @@ func (i *Installer) MaybeInstall(ctx context.Context) error { return nil } - if !existingDriver.Equals(nvproxy.DriverVersion{}) { + if !existingDriver.Equals(nvconf.DriverVersion{}) { log.Infof("Uninstalling driver: %s", existingDriver) if err := i.uninstallDriver(ctx, existingDriver.String()); err != nil { return fmt.Errorf("failed to uninstall driver: %w", err) @@ -167,19 +168,19 @@ func (i *Installer) writeAndCheck(f *os.File, reader io.ReadCloser) error { return nil } -func getCurrentDriver() (nvproxy.DriverVersion, error) { +func getCurrentDriver() (nvconf.DriverVersion, error) { _, err := os.Stat(nvidiaSMIPath) // If the nvidia-smi executable does not exist, then we don't have a driver installed. if os.IsNotExist(err) { - return nvproxy.DriverVersion{}, fmt.Errorf("nvidia-smi does not exist at path: %q", nvidiaSMIPath) + return nvconf.DriverVersion{}, fmt.Errorf("nvidia-smi does not exist at path: %q", nvidiaSMIPath) } if err != nil { - return nvproxy.DriverVersion{}, fmt.Errorf("failed to stat nvidia-smi: %w", err) + return nvconf.DriverVersion{}, fmt.Errorf("failed to stat nvidia-smi: %w", err) } out, err := exec.Command(nvidiaSMIPath, []string{"--query-gpu", "driver_version", "--format=csv,noheader"}...).CombinedOutput() if err != nil { log.Warningf("failed to run nvidia-smi: %v", err) - return nvproxy.DriverVersion{}, fmt.Errorf("failed to run nvidia-smi: %w", err) + return nvconf.DriverVersion{}, fmt.Errorf("failed to run nvidia-smi: %w", err) } // If there are multiple GPUs, there will be one version per line. // Make sure they are all the same version. @@ -194,13 +195,13 @@ func getCurrentDriver() (nvproxy.DriverVersion, error) { continue } if line != sameVersion { - return nvproxy.DriverVersion{}, fmt.Errorf("multiple driver versions found: %q and %q", sameVersion, line) + return nvconf.DriverVersion{}, fmt.Errorf("multiple driver versions found: %q and %q", sameVersion, line) } } if sameVersion == "" { - return nvproxy.DriverVersion{}, fmt.Errorf("no driver version found") + return nvconf.DriverVersion{}, fmt.Errorf("no driver version found") } - return nvproxy.DriverVersionFrom(sameVersion) + return nvconf.DriverVersionFrom(sameVersion) } // ListSupportedDrivers prints the driver to stderr in a format that can be @@ -217,7 +218,7 @@ func ListSupportedDrivers(outfile string) error { } var list []string - nvproxy.ForEachSupportDriver(func(version nvproxy.DriverVersion, checksum string) { + nvproxy.ForEachSupportDriver(func(version nvconf.DriverVersion, checksum string) { list = append(list, version.String()) }) sort.Strings(list) diff --git a/tools/gpu/drivers/install_driver_test.go b/tools/gpu/drivers/install_driver_test.go index 6f2b6f065..03fed0227 100644 --- a/tools/gpu/drivers/install_driver_test.go +++ b/tools/gpu/drivers/install_driver_test.go @@ -22,7 +22,7 @@ import ( "strings" "testing" - "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" ) // TestVersionInstalled tests when the version is already installed. @@ -30,12 +30,12 @@ func TestVersionInstalled(t *testing.T) { ctx := context.Background() versionContent := []byte("some cool content") checksum := fmt.Sprintf("%x", sha256.Sum256(versionContent)) - version := nvproxy.NewDriverVersion(1, 2, 3) - getFunction := func() (nvproxy.DriverVersion, error) { return version, nil } + version := nvconf.NewDriverVersion(1, 2, 3) + getFunction := func() (nvconf.DriverVersion, error) { return version, nil } downloadFunction := func(context.Context, string) (io.ReadCloser, error) { return nil, fmt.Errorf("should not get here") } installer := &Installer{ requestedVersion: version, - expectedChecksumFunc: func(v nvproxy.DriverVersion) (string, bool) { + expectedChecksumFunc: func(v nvconf.DriverVersion) (string, bool) { if v == version { return checksum, true } @@ -52,10 +52,10 @@ func TestVersionInstalled(t *testing.T) { // TestVersionNotSupported tests when the version is not supported. func TestVersionNotSupported(t *testing.T) { ctx := context.Background() - unsupportedVersion := nvproxy.NewDriverVersion(1, 2, 3) + unsupportedVersion := nvconf.NewDriverVersion(1, 2, 3) installer := &Installer{ requestedVersion: unsupportedVersion, - expectedChecksumFunc: func(v nvproxy.DriverVersion) (string, bool) { + expectedChecksumFunc: func(v nvconf.DriverVersion) (string, bool) { return "", false }, } @@ -71,13 +71,13 @@ func TestVersionNotSupported(t *testing.T) { // TestShaMismatch tests when a checksum of a driver doesn't match what's in the map. func TestShaMismatch(t *testing.T) { ctx := context.Background() - version := nvproxy.NewDriverVersion(1, 2, 3) + version := nvconf.NewDriverVersion(1, 2, 3) installer := &Installer{ requestedVersion: version, - getCurrentDriverFunc: func() (nvproxy.DriverVersion, error) { - return nvproxy.DriverVersion{}, nil + getCurrentDriverFunc: func() (nvconf.DriverVersion, error) { + return nvconf.DriverVersion{}, nil }, - expectedChecksumFunc: func(v nvproxy.DriverVersion) (string, bool) { + expectedChecksumFunc: func(v nvconf.DriverVersion) (string, bool) { if v == version { return "mismatch", true } @@ -102,13 +102,13 @@ func TestDriverInstalls(t *testing.T) { ctx := context.Background() content := []byte("some content") checksum := fmt.Sprintf("%x", sha256.Sum256(content)) - version := nvproxy.NewDriverVersion(1, 2, 3) + version := nvconf.NewDriverVersion(1, 2, 3) installer := &Installer{ requestedVersion: version, - getCurrentDriverFunc: func() (nvproxy.DriverVersion, error) { - return nvproxy.DriverVersion{}, nil + getCurrentDriverFunc: func() (nvconf.DriverVersion, error) { + return nvconf.DriverVersion{}, nil }, - expectedChecksumFunc: func(v nvproxy.DriverVersion) (string, bool) { + expectedChecksumFunc: func(v nvconf.DriverVersion) (string, bool) { if v == version { return checksum, true } diff --git a/tools/gpu/main.go b/tools/gpu/main.go index 41a8a6bbf..67cdaa4b2 100644 --- a/tools/gpu/main.go +++ b/tools/gpu/main.go @@ -22,6 +22,7 @@ import ( "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" "gvisor.dev/gvisor/runsc/flag" "gvisor.dev/gvisor/tools/gpu/drivers" ) @@ -114,7 +115,7 @@ func main() { os.Exit(1) } - nvproxy.ForEachSupportDriver(func(version nvproxy.DriverVersion, checksum string) { + nvproxy.ForEachSupportDriver(func(version nvconf.DriverVersion, checksum string) { wantChecksum, err := drivers.ChecksumDriver(ctx, version.String()) if err != nil { log.Warningf("error on version %q: %v", version.String(), err) diff --git a/tools/ioctl_sniffer/sniffer/BUILD b/tools/ioctl_sniffer/sniffer/BUILD index 42f736df7..fddfcd84e 100644 --- a/tools/ioctl_sniffer/sniffer/BUILD +++ b/tools/ioctl_sniffer/sniffer/BUILD @@ -17,6 +17,7 @@ go_library( "//pkg/abi/nvgpu", "//pkg/log", "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", "//tools/ioctl_sniffer:ioctl_go_proto", "@org_golang_google_protobuf//proto:go_default_library", ], diff --git a/tools/ioctl_sniffer/sniffer/sniffer.go b/tools/ioctl_sniffer/sniffer/sniffer.go index 5f8811b01..818e7e429 100644 --- a/tools/ioctl_sniffer/sniffer/sniffer.go +++ b/tools/ioctl_sniffer/sniffer/sniffer.go @@ -28,6 +28,7 @@ import ( "gvisor.dev/gvisor/pkg/abi/nvgpu" "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" pb "gvisor.dev/gvisor/tools/ioctl_sniffer/ioctl_go_proto" ) @@ -183,7 +184,7 @@ func Init() error { if err != nil { return fmt.Errorf("failed to get host driver version: %w", err) } - driverVer, err := nvproxy.DriverVersionFrom(driverVerStr) + driverVer, err := nvconf.DriverVersionFrom(driverVerStr) if err != nil { return fmt.Errorf("failed to parse host driver version: %w", err) } diff --git a/tools/nvidia_driver_differ/BUILD b/tools/nvidia_driver_differ/BUILD index 64d2efc92..f6533b1dc 100644 --- a/tools/nvidia_driver_differ/BUILD +++ b/tools/nvidia_driver_differ/BUILD @@ -52,6 +52,7 @@ go_binary( deps = [ "//pkg/log", "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", "//tools/nvidia_driver_differ/parser", ], ) diff --git a/tools/nvidia_driver_differ/parser/BUILD b/tools/nvidia_driver_differ/parser/BUILD index 0abbe9a68..be1ca10cd 100644 --- a/tools/nvidia_driver_differ/parser/BUILD +++ b/tools/nvidia_driver_differ/parser/BUILD @@ -20,6 +20,7 @@ go_library( ], deps = [ "//pkg/sentry/devices/nvproxy", + "//pkg/sentry/devices/nvproxy/nvconf", "@com_github_google_go_cmp//cmp:go_default_library", ], ) diff --git a/tools/nvidia_driver_differ/parser/auxiliary_files.go b/tools/nvidia_driver_differ/parser/auxiliary_files.go index e90f814fa..39c16c46e 100644 --- a/tools/nvidia_driver_differ/parser/auxiliary_files.go +++ b/tools/nvidia_driver_differ/parser/auxiliary_files.go @@ -20,14 +20,14 @@ import ( "os/exec" "path" - "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" ) // GitRepoURL is the URL for the NVIDIA open-gpu-kernel-modules repo. const GitRepoURL = "https://github.com/NVIDIA/open-gpu-kernel-modules.git" // CloneDriverSource clones the given driver version into the given directory. -func CloneDriverSource(dir string, version nvproxy.DriverVersion) (*DriverSourceDir, error) { +func CloneDriverSource(dir string, version nvconf.DriverVersion) (*DriverSourceDir, error) { // git clone -b $VERSION --depth 1 https://github.com/NVIDIA/open-gpu-kernel-modules.git $PATH args := []string{ "clone", diff --git a/tools/nvidia_driver_differ/parser/runner.go b/tools/nvidia_driver_differ/parser/runner.go index fb1cac331..25f2f7f6e 100644 --- a/tools/nvidia_driver_differ/parser/runner.go +++ b/tools/nvidia_driver_differ/parser/runner.go @@ -21,6 +21,7 @@ import ( "os/exec" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" ) // ParserFile is a wrapper around the driver_ast_parser binary. @@ -120,7 +121,7 @@ func (r *Runner) runParserConfig(config []ClangASTConfig) (*OutputJSON, error) { // ParseDriver checks out the git repo for the given version, and runs the driver_ast_parser on the // source code. -func (r *Runner) ParseDriver(version nvproxy.DriverVersion) (*OutputJSON, error) { +func (r *Runner) ParseDriver(version nvconf.DriverVersion) (*OutputJSON, error) { // Create a temp directory to run the parser in. // This is needed to set up compile_commands.json, since it needs to be named that exactly. dir, err := os.MkdirTemp(r.dir, "run_differ_*") diff --git a/tools/nvidia_driver_differ/parser/sources.go b/tools/nvidia_driver_differ/parser/sources.go index e41f9e74a..134c6e942 100644 --- a/tools/nvidia_driver_differ/parser/sources.go +++ b/tools/nvidia_driver_differ/parser/sources.go @@ -20,13 +20,13 @@ import ( "io" "path/filepath" - "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" ) // DriverSourceDir represents a directory containing the source code for a given driver version. type DriverSourceDir struct { ParentDirectory string - Version nvproxy.DriverVersion + Version nvconf.DriverVersion } // Name returns the name of the driver source directory. diff --git a/tools/nvidia_driver_differ/run_differ.go b/tools/nvidia_driver_differ/run_differ.go index f28dcf92a..a6cf054ed 100644 --- a/tools/nvidia_driver_differ/run_differ.go +++ b/tools/nvidia_driver_differ/run_differ.go @@ -24,6 +24,7 @@ import ( "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy" + "gvisor.dev/gvisor/pkg/sentry/devices/nvproxy/nvconf" _ "embed" // Necessary to use go:embed. ) @@ -63,11 +64,11 @@ func createParserBinary() (*os.File, error) { // Main is the main function for the NVIDIA driver differ. func Main() error { // Read driver version from command line - baseVersion, err := nvproxy.DriverVersionFrom(*baseVersionString) + baseVersion, err := nvconf.DriverVersionFrom(*baseVersionString) if err != nil { return fmt.Errorf("failed to parse driver version %s: %w", *baseVersionString, err) } - nextVersion, err := nvproxy.DriverVersionFrom(*nextVersionString) + nextVersion, err := nvconf.DriverVersionFrom(*nextVersionString) if err != nil { return fmt.Errorf("failed to parse driver version %s: %w", *nextVersionString, err) }