Skip to content

Commit

Permalink
virtcontainers: vfio: add support for hot plugging VFIO devices
Browse files Browse the repository at this point in the history
With this patch VFIO devices are hot plugged in the VM, that means
no more cold plug in kata containers.

fixes kata-containers#85

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Mar 26, 2018
1 parent 1846624 commit e0de2f3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion virtcontainers/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ func (device *VFIODevice) attach(h hypervisor, c *Container) error {

device.BDF = deviceBDF

if err := h.addDevice(*device, vfioDev); err != nil {
randBytes, err := generateRandomBytes(8)
if err != nil {
return err
}
device.DeviceInfo.ID = hex.EncodeToString(randBytes)

if err := h.hotplugAddDevice(*device, vfioDev); err != nil {
deviceLogger().WithError(err).Error("Failed to add device")
return err
}
Expand Down
41 changes: 41 additions & 0 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,44 @@ func (q *qemu) hotplugBlockDevice(drive Drive, op operation) error {
return nil
}

func (q *qemu) hotplugVFIODevice(device VFIODevice, op operation) error {
defer func(qemu *qemu) {
if q.qmpMonitorCh.qmp != nil {
q.qmpMonitorCh.qmp.Shutdown()
}
}(q)

qmp, err := q.qmpSetup()
if err != nil {
return err
}

q.qmpMonitorCh.qmp = qmp

devID := "vfio-" + device.DeviceInfo.ID

if op == addDevice {
addr, bus, err := q.addDeviceToBridge(devID)
if err != nil {
return err
}

if err := q.qmpMonitorCh.qmp.ExecutePCIVFIODeviceAdd(q.qmpMonitorCh.ctx, devID, device.BDF, addr, bus); err != nil {
return err
}
} else {
if err := q.removeDeviceFromBridge(devID); err != nil {
return err
}

if err := q.qmpMonitorCh.qmp.ExecuteDeviceDel(q.qmpMonitorCh.ctx, devID); err != nil {
return err
}
}

return nil
}

func (q *qemu) hotplugDevice(devInfo interface{}, devType deviceType, op operation) error {
switch devType {
case blockDev:
Expand All @@ -646,6 +684,9 @@ func (q *qemu) hotplugDevice(devInfo interface{}, devType deviceType, op operati
case cpuDev:
vcpus := devInfo.(uint32)
return q.hotplugCPUs(vcpus, op)
case vfioDev:
device := devInfo.(VFIODevice)
return q.hotplugVFIODevice(device, op)
default:
return fmt.Errorf("cannot hotplug device: unsupported device type '%v'", devType)
}
Expand Down

0 comments on commit e0de2f3

Please sign in to comment.