...

Source file src/github.com/Microsoft/hcsshim/internal/guest/storage/pci/pci.go

Documentation: github.com/Microsoft/hcsshim/internal/guest/storage/pci

     1  //go:build linux
     2  // +build linux
     3  
     4  package pci
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/Microsoft/hcsshim/internal/guest/storage"
    13  	"github.com/Microsoft/hcsshim/internal/guest/storage/vmbus"
    14  )
    15  
    16  var storageWaitForFileMatchingPattern = storage.WaitForFileMatchingPattern
    17  var vmbusWaitForDevicePath = vmbus.WaitForDevicePath
    18  
    19  // WaitForPCIDeviceFromVMBusGUID waits for bus location path of the device to be present
    20  func WaitForPCIDeviceFromVMBusGUID(ctx context.Context, vmBusGUID string) error {
    21  	_, err := FindDeviceBusLocationFromVMBusGUID(ctx, vmBusGUID)
    22  	return err
    23  }
    24  
    25  // FindDeviceBusLocationFromVMBusGUID finds device bus location by
    26  // reading /sys/bus/vmbus/devices/<vmBusGUID>/... for pci specific directories
    27  func FindDeviceBusLocationFromVMBusGUID(ctx context.Context, vmBusGUID string) (string, error) {
    28  	fullPath, err := FindDeviceFullPath(ctx, vmBusGUID)
    29  	if err != nil {
    30  		return "", err
    31  	}
    32  
    33  	_, busFile := filepath.Split(fullPath)
    34  	return busFile, nil
    35  }
    36  
    37  // FindDeviceFullPath finds the full PCI device path in the form of
    38  // /sys/bus/vmbus/devices/<vmBusGUID>/pciXXXX:XX/XXXX:XX*
    39  func FindDeviceFullPath(ctx context.Context, vmBusGUID string) (string, error) {
    40  	pciDir, err := findVMBusPCIDir(ctx, vmBusGUID)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  
    45  	return findVMBusPCIDevice(ctx, pciDir)
    46  }
    47  
    48  // findVMBusPCIDir waits for the pci bus directory matching pattern
    49  // /sys/bus/vmbus/devices/<vmBusGUID>/pci* to exist and returns
    50  // the full resulting path or an error
    51  func findVMBusPCIDir(ctx context.Context, vmBusGUID string) (string, error) {
    52  	vmBusPCIPathPattern := filepath.Join(vmBusGUID, "pci*")
    53  	return vmbusWaitForDevicePath(ctx, vmBusPCIPathPattern)
    54  }
    55  
    56  // findVMBusPCIDevice waits for the pci bus location directory under the path
    57  // returned from findVMBusPCIDir to exist and returns the pci bus location or an error
    58  func findVMBusPCIDevice(ctx context.Context, pciDirFullPath string) (string, error) {
    59  	// trim /sys/bus/vmbus/devices/<vmBusGUID>/pciXXXX:XX to XXXX:XX
    60  	_, pciDirName := filepath.Split(pciDirFullPath)
    61  	busPrefix := strings.TrimPrefix(pciDirName, "pci")
    62  	// under /sys/bus/vmbus/devices/<vmBusGUID>/pciXXXX:XX/ look for directory matching XXXX:XX* pattern
    63  	busPathPattern := filepath.Join(pciDirFullPath, fmt.Sprintf("%s*", busPrefix))
    64  	busFileFullPath, err := storageWaitForFileMatchingPattern(ctx, busPathPattern)
    65  	if err != nil {
    66  		return "", err
    67  	}
    68  
    69  	return busFileFullPath, nil
    70  }
    71  

View as plain text