@@ -280,27 +280,14 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
280280 return nil , fmt .Errorf ("unable to convert device string to uint16: %v" , deviceStr )
281281 }
282282
283- driver , err := filepath .EvalSymlinks (path .Join (devicePath , "driver" ))
284- if err == nil {
285- driver = filepath .Base (driver )
286- } else if os .IsNotExist (err ) {
287- driver = ""
288- } else {
289- return nil , fmt .Errorf ("unable to detect driver for %s: %v" , address , err )
283+ driver , err := getDriver (devicePath )
284+ if err != nil {
285+ return nil , fmt .Errorf ("unable to detect driver for %s: %w" , address , err )
290286 }
291287
292- var iommuGroup int64
293- iommu , err := filepath .EvalSymlinks (path .Join (devicePath , "iommu_group" ))
294- if err == nil {
295- iommuGroupStr := strings .TrimSpace (filepath .Base (iommu ))
296- iommuGroup , err = strconv .ParseInt (iommuGroupStr , 0 , 64 )
297- if err != nil {
298- return nil , fmt .Errorf ("unable to convert iommu_group string to int64: %v" , iommuGroupStr )
299- }
300- } else if os .IsNotExist (err ) {
301- iommuGroup = - 1
302- } else {
303- return nil , fmt .Errorf ("unable to detect iommu_group for %s: %v" , address , err )
288+ iommuGroup , err := getIOMMUGroup (devicePath )
289+ if err != nil {
290+ return nil , fmt .Errorf ("unable to detect IOMMU group for %s: %w" , address , err )
304291 }
305292
306293 numa , err := os .ReadFile (path .Join (devicePath , "numa_node" ))
@@ -359,7 +346,8 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
359346 var sriovInfo SriovInfo
360347 // Device is a virtual function (VF) if "physfn" symlink exists.
361348 physFnAddress , err := filepath .EvalSymlinks (path .Join (devicePath , "physfn" ))
362- if err == nil {
349+ switch {
350+ case err == nil :
363351 physFn , err := p .getGPUByPciBusID (filepath .Base (physFnAddress ), cache )
364352 if err != nil {
365353 return nil , fmt .Errorf ("unable to detect physfn for %s: %v" , address , err )
@@ -369,12 +357,12 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
369357 PhysicalFunction : physFn ,
370358 },
371359 }
372- } else if os .IsNotExist (err ) {
360+ case os .IsNotExist (err ):
373361 sriovInfo , err = p .getSriovInfoForPhysicalFunction (devicePath )
374362 if err != nil {
375363 return nil , fmt .Errorf ("unable to read SRIOV physical function details for %s: %v" , devicePath , err )
376364 }
377- } else {
365+ default :
378366 return nil , fmt .Errorf ("unable to read %s: %v" , path .Join (devicePath , "physfn" ), err )
379367 }
380368
@@ -521,3 +509,31 @@ func (p *nvpci) getSriovInfoForPhysicalFunction(devicePath string) (sriovInfo Sr
521509 }
522510 return sriovInfo , nil
523511}
512+
513+ func getDriver (devicePath string ) (string , error ) {
514+ driver , err := filepath .EvalSymlinks (path .Join (devicePath , "driver" ))
515+ switch {
516+ case os .IsNotExist (err ):
517+ return "" , nil
518+ case err == nil :
519+ return filepath .Base (driver ), nil
520+ }
521+ return "" , err
522+ }
523+
524+ func getIOMMUGroup (devicePath string ) (int64 , error ) {
525+ var iommuGroup int64
526+ iommu , err := filepath .EvalSymlinks (path .Join (devicePath , "iommu_group" ))
527+ switch {
528+ case os .IsNotExist (err ):
529+ return - 1 , nil
530+ case err == nil :
531+ iommuGroupStr := strings .TrimSpace (filepath .Base (iommu ))
532+ iommuGroup , err = strconv .ParseInt (iommuGroupStr , 0 , 64 )
533+ if err != nil {
534+ return 0 , fmt .Errorf ("unable to convert iommu_group string to int64: %v" , iommuGroupStr )
535+ }
536+ return iommuGroup , nil
537+ }
538+ return 0 , err
539+ }
0 commit comments