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

Commit

Permalink
device: support vfio cold plug
Browse files Browse the repository at this point in the history
Depending on ColdPlug flag, cold or hot plug vfio devices. The VFIO device
won't be hot removed when such flag is false

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Jun 16, 2020
1 parent 6532eaa commit 970ef45
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
27 changes: 23 additions & 4 deletions virtcontainers/device/drivers/vfio.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ func (device *VFIODevice) Attach(devReceiver api.DeviceReceiver) (retErr error)
}
}

// hotplug a VFIO device is actually hotplugging a group of iommu devices
if err := devReceiver.HotplugAddDevice(device, config.DeviceVFIO); err != nil {
deviceLogger().WithError(err).Error("Failed to add device")
return err
coldPlug := device.DeviceInfo.ColdPlug
deviceLogger().WithField("cold-plug", coldPlug).Info("Attaching VFIO device")

if coldPlug {
if err := devReceiver.AppendDevice(device); err != nil {
deviceLogger().WithError(err).Error("Failed to append device")
return err
}
} else {
// hotplug a VFIO device is actually hotplugging a group of iommu devices
if err := devReceiver.HotplugAddDevice(device, config.DeviceVFIO); err != nil {
deviceLogger().WithError(err).Error("Failed to add device")
return err
}
}

deviceLogger().WithFields(logrus.Fields{
Expand All @@ -128,6 +138,15 @@ func (device *VFIODevice) Detach(devReceiver api.DeviceReceiver) (retErr error)
}
}()

if device.GenericDevice.DeviceInfo.ColdPlug {
// nothing to detach, device was cold plugged
deviceLogger().WithFields(logrus.Fields{
"device-group": device.DeviceInfo.HostPath,
"device-type": "vfio-passthrough",
}).Info("Nothing to detach. VFIO device was cold plugged")
return nil
}

// hotplug a VFIO device is actually hotplugging a group of iommu devices
if err := devReceiver.HotplugRemoveDevice(device, config.DeviceVFIO); err != nil {
deviceLogger().WithError(err).Error("Failed to remove device")
Expand Down
9 changes: 9 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,16 @@ func (s *Sandbox) AppendDevice(device api.Device) error {
switch device.DeviceType() {
case config.VhostUserSCSI, config.VhostUserNet, config.VhostUserBlk, config.VhostUserFS:
return s.hypervisor.addDevice(device.GetDeviceInfo().(*config.VhostUserDeviceAttrs), vhostuserDev)
case config.DeviceVFIO:
vfioDevs := device.GetDeviceInfo().([]*config.VFIODev)
for _, d := range vfioDevs {
return s.hypervisor.addDevice(*d, vfioDev)
}
default:
s.Logger().WithField("device-type", device.DeviceType()).
Warn("Could not append device: unsupported device type")
}

return fmt.Errorf("unsupported device type")
}

Expand Down

0 comments on commit 970ef45

Please sign in to comment.