Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

fix up remaining issues with pci hotplug and uevent listening #380

Merged
merged 3 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const (
driverEphemeralType = "ephemeral"
)

const rootBusPath = "/devices/pci0000:00"
const (
rootBusPath = "/devices/pci0000:00"
pciBusRescanFile = "/sys/bus/pci/rescan"
pciBusMode = 0220
)

var (
sysBusPrefix = "/sys/bus/pci/devices"
Expand Down Expand Up @@ -58,6 +62,10 @@ var deviceHandlerList = map[string]deviceHandler{
driverSCSIType: virtioSCSIDeviceHandler,
}

func rescanPciBus() error {
return ioutil.WriteFile(pciBusRescanFile, []byte{'1'}, pciBusMode)
}

// getDevicePCIAddress fetches the complete PCI address in sysfs, based on the PCI
// identifier provided. This should be in the format: "bridgeAddr/deviceAddr".
// Here, bridgeAddr is the address at which the brige is attached on the root bus,
Expand Down Expand Up @@ -112,8 +120,8 @@ func getPCIDeviceName(s *sandbox, pciID string) (string, error) {

fieldLogger := agentLog.WithField("pciID", pciID)

s.Lock()
// Check if the PCI identifier is in PCI device map.
s.Lock()
for key, value := range s.pciDeviceMap {
if strings.Contains(key, pciAddr) {
devName = value
Expand All @@ -122,13 +130,11 @@ func getPCIDeviceName(s *sandbox, pciID string) (string, error) {
}
}

// Check if the PCI path is present before we setup the pci device map.
_, err = os.Stat(filepath.Join(rootBusPath, pciAddr))
if err == nil {
// Found pci path. It's there before we wait for the device map.
// We cannot find the name for it.
fieldLogger.Info("Device found on pci device path")
return "", nil
// Rescan pci bus if we need to wait for a new pci device
if err = rescanPciBus(); err != nil {
fieldLogger.WithError(err).Error("Failed to scan pci bus")
s.Unlock()
return "", err
}

// If device is not found in the device map, hotplug event has not
Expand All @@ -137,7 +143,7 @@ func getPCIDeviceName(s *sandbox, pciID string) (string, error) {
// Note this is done inside the lock, not to miss any events from the
// global udev listener.
if devName == "" {
notifyChan := make(chan string, 1)
notifyChan = make(chan string, 1)
s.deviceWatchers[pciAddr] = notifyChan
}
s.Unlock()
Expand Down Expand Up @@ -169,9 +175,6 @@ func virtioBlkDeviceHandler(device pb.Device, spec *pb.Spec, s *sandbox) error {
if err != nil {
return err
}
if devPath == "" {
return fmt.Errorf("cannot find device name for virtio block device")
}
device.VmPath = devPath

return updateSpecDeviceList(device, spec)
Expand Down
9 changes: 9 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,12 @@ func TestUpdateSpecDeviceList(t *testing.T) {
err = updateSpecDeviceList(device, spec)
assert.NoError(err)
}

func TestRescanPciBus(t *testing.T) {
skipUnlessRoot(t)

assert := assert.New(t)

err := rescanPciBus()
assert.Nil(err)
}
8 changes: 1 addition & 7 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ type agentGRPC struct {
version string
}

// PCI scanning
const (
pciBusRescanFile = "/sys/bus/pci/rescan"
pciBusMode = 0220
)

// CPU and Memory hotplug
const (
cpuRegexpPattern = "cpu[0-9]*"
Expand Down Expand Up @@ -530,7 +524,7 @@ func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainer

// re-scan PCI bus
// looking for hidden devices
if err = ioutil.WriteFile(pciBusRescanFile, []byte("1"), pciBusMode); err != nil {
if err = rescanPciBus(); err != nil {
agentLog.WithError(err).Warn("Could not rescan PCI bus")
}

Expand Down