github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/devices/tpuproxy/device.go (about) 1 // Copyright 2023 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tpuproxy 16 17 import ( 18 "fmt" 19 "path/filepath" 20 21 "golang.org/x/sys/unix" 22 "github.com/metacubex/gvisor/pkg/abi/linux" 23 "github.com/metacubex/gvisor/pkg/context" 24 "github.com/metacubex/gvisor/pkg/devutil" 25 "github.com/metacubex/gvisor/pkg/errors/linuxerr" 26 "github.com/metacubex/gvisor/pkg/fdnotifier" 27 "github.com/metacubex/gvisor/pkg/log" 28 "github.com/metacubex/gvisor/pkg/sentry/vfs" 29 "github.com/metacubex/gvisor/pkg/sync" 30 ) 31 32 const ( 33 // VFIO_MINOR is the VFIO minor number from include/linux/miscdevice.h. 34 VFIO_MINOR = 196 35 // VFIOPath is the path to a VFIO device, it is ususally used to 36 // construct a VFIO container. 37 VFIOPath = "/dev/vfio/vfio" 38 39 tpuDeviceGroupName = "vfio" 40 vfioDeviceGroupName = "vfio" 41 ) 42 43 // device implements TPU's vfs.Device for /dev/vfio/[0-9]+ 44 // 45 // +stateify savable 46 type tpuDevice struct { 47 mu sync.Mutex 48 49 minor uint32 50 } 51 52 // Open implememnts vfs.Device.Open. 53 func (dev *tpuDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { 54 devClient := devutil.GoferClientFromContext(ctx) 55 if devClient == nil { 56 log.Warningf("devutil.CtxDevGoferClient is not set") 57 return nil, linuxerr.ENOENT 58 } 59 dev.mu.Lock() 60 defer dev.mu.Unlock() 61 devName := fmt.Sprintf("vfio/%d", dev.minor) 62 hostFD, err := devClient.OpenAt(ctx, devName, opts.Flags) 63 if err != nil { 64 ctx.Warningf("vfioDevice: failed to open host %s: %v", devName, err) 65 return nil, err 66 } 67 fd := &tpuFD{ 68 hostFD: int32(hostFD), 69 device: dev, 70 } 71 if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{ 72 UseDentryMetadata: true, 73 }); err != nil { 74 unix.Close(hostFD) 75 return nil, err 76 } 77 if err := fdnotifier.AddFD(int32(hostFD), &fd.queue); err != nil { 78 unix.Close(hostFD) 79 return nil, err 80 } 81 fd.memmapFile.fd = fd 82 return &fd.vfsfd, nil 83 } 84 85 // device implements vfs.Device for /dev/vfio/vfio. 86 type vfioDevice struct { 87 mu sync.Mutex 88 } 89 90 // Open implements vfs.Device.Open. 91 func (dev *vfioDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { 92 client := devutil.GoferClientFromContext(ctx) 93 if client == nil { 94 log.Warningf("devutil.CtxDevGoferClient is not set") 95 return nil, linuxerr.ENOENT 96 } 97 98 dev.mu.Lock() 99 defer dev.mu.Unlock() 100 name := fmt.Sprintf("vfio/%s", filepath.Base(VFIOPath)) 101 hostFd, err := client.OpenAt(ctx, name, opts.Flags) 102 if err != nil { 103 ctx.Warningf("failed to open host file %s: %v", name, err) 104 return nil, err 105 } 106 fd := &vfioFd{ 107 hostFd: int32(hostFd), 108 device: dev, 109 } 110 if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{ 111 UseDentryMetadata: true, 112 }); err != nil { 113 unix.Close(hostFd) 114 return nil, err 115 } 116 if err := fdnotifier.AddFD(int32(hostFd), &fd.queue); err != nil { 117 unix.Close(hostFd) 118 return nil, err 119 } 120 fd.memmapFile.fd = fd 121 return &fd.vfsfd, nil 122 } 123 124 // RegisterTPUDevice registers devices implemented by this package in vfsObj. 125 func RegisterTPUDevice(vfsObj *vfs.VirtualFilesystem, minor uint32) error { 126 return vfsObj.RegisterDevice(vfs.CharDevice, linux.VFIO_MAJOR, minor, &tpuDevice{ 127 minor: minor, 128 }, &vfs.RegisterDeviceOptions{ 129 GroupName: tpuDeviceGroupName, 130 }) 131 } 132 133 // RegisterVfioDevice registers VFIO devices that are implemented by this pacakge in vfsObj. 134 func RegisterVfioDevice(vfsObj *vfs.VirtualFilesystem) error { 135 return vfsObj.RegisterDevice(vfs.CharDevice, linux.MISC_MAJOR, VFIO_MINOR, &vfioDevice{}, &vfs.RegisterDeviceOptions{ 136 GroupName: vfioDeviceGroupName, 137 }) 138 }