@@ -28,7 +28,7 @@ func NewVFIOManager(nvlibInstance nvlib.Interface) *VFIOManager {
2828// ParentDevice represents an NVIDIA parent PCI device.
2929type ParentDevice struct {
3030 * nvpci.NvidiaPCIDevice
31- VirtualFunctionPaths map [ string ] string
31+ VirtualFunctionPath string
3232}
3333
3434// Device represents an NVIDIA (vGPU) device.
@@ -46,19 +46,17 @@ func (m *VFIOManager) GetAllParentDevices() ([]*ParentDevice, error) {
4646 for _ , device := range nvdevices {
4747 vfnum := 0
4848 numVF := int (device .SriovInfo .PhysicalFunction .NumVFs )
49- virtualFunctionPaths := make (map [string ]string )
5049 for vfnum < numVF {
5150 vfAddr := filepath .Join (HostPCIDevicesRoot , device .Address , "virtfn" + strconv .Itoa (vfnum ), "nvidia" )
5251 if _ , err := os .Stat (vfAddr ); err != nil {
5352 return nil , fmt .Errorf ("virtual function %d at address %s does not exist" , vfnum , vfAddr )
5453 }
55- virtualFunctionPaths [strconv .Itoa (vfnum )] = vfAddr
54+ parentDevices = append (parentDevices , & ParentDevice {
55+ NvidiaPCIDevice : device ,
56+ VirtualFunctionPath : vfAddr ,
57+ })
5658 vfnum ++
5759 }
58- parentDevices = append (parentDevices , & ParentDevice {
59- NvidiaPCIDevice : device ,
60- VirtualFunctionPaths : virtualFunctionPaths ,
61- })
6260 }
6361 return parentDevices , nil
6462}
@@ -70,8 +68,7 @@ func (m *VFIOManager) GetAllDevices() ([]*Device, error) {
7068 }
7169 devices := []* Device {}
7270 for _ , parentDevice := range parentDevices {
73- for _ , vfAddr := range parentDevice .VirtualFunctionPaths {
74- vgpuTypeNumberBytes , err := os .ReadFile (filepath .Join (vfAddr , "current_vgpu_type" ))
71+ vgpuTypeNumberBytes , err := os .ReadFile (filepath .Join (parentDevice .VirtualFunctionPath , "current_vgpu_type" ))
7572 if err != nil {
7673 return nil , fmt .Errorf ("unable to read current vGPU type: %v" , err )
7774 }
@@ -81,19 +78,18 @@ func (m *VFIOManager) GetAllDevices() ([]*Device, error) {
8178 }
8279 if vgpuTypeNumber != 0 {
8380 devices = append (devices , & Device {
84- Path : vfAddr ,
81+ Path : parentDevice . VirtualFunctionPath ,
8582 Parent : parentDevice ,
8683 })
8784 }
88- }
8985 }
9086 return devices , nil
9187}
9288
9389// GetPhysicalFunction gets the physical PCI device backing a 'parent' device.
9490func (p * ParentDevice ) GetPhysicalFunction () * nvpci.NvidiaPCIDevice {
95- if p .SriovInfo .IsVF () {
96- return p .SriovInfo .VirtualFunction .PhysicalFunction
91+ if p .NvidiaPCIDevice . SriovInfo .IsVF () {
92+ return p .NvidiaPCIDevice . SriovInfo .VirtualFunction .PhysicalFunction
9793 }
9894 // Either it is an SRIOV physical function or a non-SRIOV device, so return the device itself
9995 return p .NvidiaPCIDevice
@@ -149,24 +145,22 @@ func (m *VFIOManager) IsVFIOEnabled(gpu int) (bool, error) {
149145
150146// IsVGPUTypeSupported checks if the vfioType is supported by this parent GPU
151147func (p * ParentDevice ) IsVGPUTypeAvailable (vfioType string ) (bool , error ) {
152- for _ , vfPath := range p .VirtualFunctionPaths {
153- creatableTypesPath := filepath .Join (vfPath , "creatable_vgpu_types" )
154- file , err := os .Open (creatableTypesPath )
155- if err != nil {
156- return false , fmt .Errorf ("unable to open file %s: %v" , creatableTypesPath , err )
148+ creatableTypesPath := filepath .Join (p .VirtualFunctionPath , "creatable_vgpu_types" )
149+ file , err := os .Open (creatableTypesPath )
150+ if err != nil {
151+ return false , fmt .Errorf ("unable to open file %s: %v" , creatableTypesPath , err )
152+ }
153+ defer file .Close ()
154+ scanner := bufio .NewScanner (file )
155+ for scanner .Scan () {
156+ line := scanner .Text ()
157+ fields := strings .Fields (line )
158+ if len (fields ) < 2 {
159+ continue
157160 }
158- defer file .Close ()
159- scanner := bufio .NewScanner (file )
160- for scanner .Scan () {
161- line := scanner .Text ()
162- fields := strings .Fields (line )
163- if len (fields ) < 2 {
164- continue
165- }
166- name := fields [len (fields )- 1 ]
167- if name == vfioType {
168- return true , nil
169- }
161+ name := fields [len (fields )- 1 ]
162+ if name == vfioType {
163+ return true , nil
170164 }
171165 }
172166 return false , nil
@@ -183,7 +177,7 @@ func (m *Device) Delete() error {
183177}
184178
185179func (p * ParentDevice ) CreateVGPUDevice (vfioType string , vfnum string ) error {
186- vfPath := p .VirtualFunctionPaths [ vfnum ]
180+ vfPath := p .VirtualFunctionPath
187181 currentVGPUTypePath := filepath .Join (vfPath , "current_vgpu_type" )
188182 number , err := p .GetIdForVGPUTypeName (filepath .Join (vfPath , "creatable_vgpu_types" ), vfioType )
189183 if err != nil {
@@ -202,7 +196,7 @@ func (p *ParentDevice) GetAvailableVGPUInstances(vfioType string) (int, error) {
202196 return 0 , fmt .Errorf ("unable to check if vGPU type is available: %v" , err )
203197 }
204198 if available {
205- return int ( p . NvidiaPCIDevice . SriovInfo . PhysicalFunction . NumVFs ) , nil
199+ return 1 , nil
206200 }
207201 return 0 , nil
208202}
0 commit comments