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

Commit

Permalink
devices: don't use drivers package directly.
Browse files Browse the repository at this point in the history
Instead of using drivers.XXXDevice directly, we should use exported
struct from device structure. package drivers should be internal struct
and other package should avoid read it's struct content directly.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Jul 31, 2018
1 parent 5db5f42 commit eec7fa3
Showing 1 changed file with 70 additions and 8 deletions.
78 changes: 70 additions & 8 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,12 +1446,43 @@ func togglePauseSandbox(sandboxID string, pause bool) (*Sandbox, error) {
func (s *Sandbox) HotplugAddDevice(device api.Device, devType config.DeviceType) error {
switch devType {
case config.DeviceVFIO:
vfioDevice, ok := device.(*drivers.VFIODevice)
vfioDevices, ok := device.GetDeviceDrive().([]*config.VFIODrive)
if !ok {
return fmt.Errorf("device type mismatch, expect device type to be %s", devType)
}
_, err := s.hypervisor.hotplugAddDevice(*vfioDevice, vfioDev)
return err
addedDev := []*config.VFIODrive{}
var err error
defer func() {
// if err happens,roll back and remove added device!
if err != nil {
for _, dev := range addedDev {
if _, rollbackErr := s.hypervisor.hotplugRemoveDevice(dev, vfioDev); rollbackErr != nil {
s.Logger().
WithFields(logrus.Fields{
"sandboxid": s.id,
"vfio device ID": dev.ID,
"vfio device BDF": dev.BDF,
}).WithError(rollbackErr).
Error("failed to remove vfio device for rolling back")
}
}
}
}()

// adding a group of VFIO devices
for _, dev := range vfioDevices {
if _, err = s.hypervisor.hotplugAddDevice(dev, vfioDev); err != nil {
s.Logger().
WithFields(logrus.Fields{
"sandboxid": s.id,
"vfio device ID": dev.ID,
"vfio device BDF": dev.BDF,
}).WithError(err).Error("failed to hotplug VFIO device")
return err
}
addedDev = append(addedDev, dev)
}
return nil
case config.DeviceBlock:
blockDevice, ok := device.(*drivers.BlockDevice)
if !ok {
Expand All @@ -1471,18 +1502,49 @@ func (s *Sandbox) HotplugAddDevice(device api.Device, devType config.DeviceType)
func (s *Sandbox) HotplugRemoveDevice(device api.Device, devType config.DeviceType) error {
switch devType {
case config.DeviceVFIO:
vfioDevice, ok := device.(*drivers.VFIODevice)
vfioDevices, ok := device.GetDeviceDrive().([]*config.VFIODrive)
if !ok {
return fmt.Errorf("device type mismatch, expect device type to be %s", devType)
}
_, err := s.hypervisor.hotplugRemoveDevice(*vfioDevice, vfioDev)
return err
removedDev := []*config.VFIODrive{}
var err error
defer func() {
// if err happens,roll back and add the removed devices back!
if err != nil {
for _, dev := range removedDev {
if _, rollbackErr := s.hypervisor.hotplugAddDevice(dev, vfioDev); rollbackErr != nil {

s.Logger().WithError(rollbackErr).
WithFields(logrus.Fields{
"sandboxid": s.id,
"vfio device ID": dev.ID,
"vfio device BDF": dev.BDF,
}).Error("failed to add vfio device for rolling back")
}
}
}
}()

// remove a group of VFIO devices
for _, dev := range vfioDevices {
if _, err = s.hypervisor.hotplugRemoveDevice(dev, vfioDev); err != nil {
s.Logger().WithError(err).
WithFields(logrus.Fields{
"sandboxid": s.id,
"vfio device ID": dev.ID,
"vfio device BDF": dev.BDF,
}).Error("failed to hot unplug VFIO device")
return err
}
removedDev = append(removedDev, dev)
}
return nil
case config.DeviceBlock:
blockDevice, ok := device.(*drivers.BlockDevice)
blockDrive, ok := device.GetDeviceDrive().(*config.BlockDrive)
if !ok {
return fmt.Errorf("device type mismatch, expect device type to be %s", devType)
}
_, err := s.hypervisor.hotplugRemoveDevice(blockDevice.BlockDrive, blockDev)
_, err := s.hypervisor.hotplugRemoveDevice(blockDrive, blockDev)
return err
case config.DeviceGeneric:
// TODO: what?
Expand Down

0 comments on commit eec7fa3

Please sign in to comment.