@@ -280,27 +280,14 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
280
280
return nil , fmt .Errorf ("unable to convert device string to uint16: %v" , deviceStr )
281
281
}
282
282
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 )
290
286
}
291
287
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 )
304
291
}
305
292
306
293
numa , err := os .ReadFile (path .Join (devicePath , "numa_node" ))
@@ -359,7 +346,8 @@ func (p *nvpci) getGPUByPciBusID(address string, cache map[string]*NvidiaPCIDevi
359
346
var sriovInfo SriovInfo
360
347
// Device is a virtual function (VF) if "physfn" symlink exists.
361
348
physFnAddress , err := filepath .EvalSymlinks (path .Join (devicePath , "physfn" ))
362
- if err == nil {
349
+ switch {
350
+ case err == nil :
363
351
physFn , err := p .getGPUByPciBusID (filepath .Base (physFnAddress ), cache )
364
352
if err != nil {
365
353
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
369
357
PhysicalFunction : physFn ,
370
358
},
371
359
}
372
- } else if os .IsNotExist (err ) {
360
+ case os .IsNotExist (err ):
373
361
sriovInfo , err = p .getSriovInfoForPhysicalFunction (devicePath )
374
362
if err != nil {
375
363
return nil , fmt .Errorf ("unable to read SRIOV physical function details for %s: %v" , devicePath , err )
376
364
}
377
- } else {
365
+ default :
378
366
return nil , fmt .Errorf ("unable to read %s: %v" , path .Join (devicePath , "physfn" ), err )
379
367
}
380
368
@@ -521,3 +509,31 @@ func (p *nvpci) getSriovInfoForPhysicalFunction(devicePath string) (sriovInfo Sr
521
509
}
522
510
return sriovInfo , nil
523
511
}
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 err == nil :
529
+ iommuGroupStr := strings .TrimSpace (filepath .Base (iommu ))
530
+ iommuGroup , err = strconv .ParseInt (iommuGroupStr , 0 , 64 )
531
+ if err != nil {
532
+ return 0 , fmt .Errorf ("unable to convert iommu_group string to int64: %v" , iommuGroupStr )
533
+ }
534
+ return iommuGroup , nil
535
+ case os .IsNotExist (err ):
536
+ iommuGroup = - 1
537
+ }
538
+ return iommuGroup , err
539
+ }
0 commit comments