Add an initial implementation for vfioDevice Open, which implements vfs.Device interface.

PiperOrigin-RevId: 586047954
This commit is contained in:
Jing Chen
2023-11-28 11:23:48 -08:00
committed by gVisor bot
parent 60d6f0b291
commit 77eff46290
3 changed files with 75 additions and 1 deletions
+8 -1
View File
@@ -6,13 +6,20 @@ licenses(["notice"])
go_library(
name = "tpuproxy",
srcs = ["tpu.go"],
srcs = [
"device.go",
"tpu.go",
],
deps = [
"//pkg/context",
"//pkg/devutil",
"//pkg/errors/linuxerr",
"//pkg/log",
"//pkg/sentry/arch",
"//pkg/sentry/vfs",
"//pkg/sync",
"//pkg/usermem",
"//pkg/waiter",
"@org_golang_x_sys//unix:go_default_library",
],
)
+64
View File
@@ -0,0 +1,64 @@
// Copyright 2023 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 tpuproxy
import (
"fmt"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/devutil"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sync"
)
// vfioDevice implements vfs.Device for /dev/vfio/[0-9]+
//
// +stateify savable
type vfioDevice struct {
mu sync.Mutex
minor uint32
}
// Open implememnts vfs.Device.Open.
func (dev *vfioDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) {
devClient := devutil.GoferClientFromContext(ctx)
if devClient == nil {
log.Warningf("devutil.CtxDevGoferClient is not set")
return nil, linuxerr.ENOENT
}
dev.mu.Lock()
defer dev.mu.Unlock()
devName := fmt.Sprintf("/dev/vfio/%d", dev.minor)
hostFD, err := devClient.OpenAt(ctx, devName, opts.Flags)
if err != nil {
ctx.Warningf("vfioDevice: failed to open host %s: %v", devName, err)
return nil, err
}
fd := &vfioFD{
hostFD: int32(hostFD),
device: dev,
}
if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{
UseDentryMetadata: true,
}); err != nil {
unix.Close(hostFD)
return nil, err
}
return &fd.vfsfd, nil
}
+3
View File
@@ -32,6 +32,9 @@ type vfioFD struct {
vfs.FileDescriptionDefaultImpl
vfs.DentryMetadataFileDescriptionImpl
vfs.NoLockFD
hostFD int32
device *vfioDevice
}
// Release implements vfs.FileDescriptionImpl.Release.