From 718dbd2a71ba8b6ce44849ec2c21fdd18c0cb42e Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Thu, 26 Apr 2018 09:52:42 -0700 Subject: [PATCH] device: Assign pci address for block devices Introduce a new field in Drive to store the PCI address if the drive is attached using virtio-blk. Assign PCI address in the format bridge-addr/device-addr. Since we need to assign the address while hotplugging, pass Drive by address. Signed-off-by: Archana Shinde --- virtcontainers/container.go | 4 ++-- virtcontainers/device.go | 7 +++++-- virtcontainers/qemu.go | 7 +++++-- virtcontainers/sandbox.go | 3 +++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/virtcontainers/container.go b/virtcontainers/container.go index c861bf4c43..b7a8103300 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -788,7 +788,7 @@ func (c *Container) hotplugDrive() error { Index: driveIndex, } - if err := c.sandbox.hypervisor.hotplugAddDevice(drive, blockDev); err != nil { + if err := c.sandbox.hypervisor.hotplugAddDevice(&drive, blockDev); err != nil { return err } c.setStateHotpluggedDrive(true) @@ -813,7 +813,7 @@ func (c *Container) removeDrive() (err error) { c.Logger().Info("unplugging block device") devID := makeNameID("drive", c.id) - drive := Drive{ + drive := &Drive{ ID: devID, } diff --git a/virtcontainers/device.go b/virtcontainers/device.go index b47d59bcfa..77aeb64417 100644 --- a/virtcontainers/device.go +++ b/virtcontainers/device.go @@ -334,6 +334,9 @@ type BlockDevice struct { // Path at which the device appears inside the VM, outside of the container mount namespace VirtPath string + + // PCI Slot of the block device + PCIAddr string } func newBlockDevice(devInfo DeviceInfo) *BlockDevice { @@ -380,7 +383,7 @@ func (device *BlockDevice) attach(h hypervisor, c *Container) (err error) { deviceLogger().WithField("device", device.DeviceInfo.HostPath).Info("Attaching block device") - if err = h.hotplugAddDevice(drive, blockDev); err != nil { + if err = h.hotplugAddDevice(&drive, blockDev); err != nil { return err } @@ -404,7 +407,7 @@ func (device BlockDevice) detach(h hypervisor) error { if device.DeviceInfo.Hotplugged { deviceLogger().WithField("device", device.DeviceInfo.HostPath).Info("Unplugging block device") - drive := Drive{ + drive := &Drive{ ID: makeNameID("drive", device.DeviceInfo.ID), } diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index abe57f5b2c..fa1680d640 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -593,7 +593,7 @@ func (q *qemu) removeDeviceFromBridge(ID string) error { return err } -func (q *qemu) hotplugBlockDevice(drive Drive, op operation) error { +func (q *qemu) hotplugBlockDevice(drive *Drive, op operation) error { defer func(qemu *qemu) { if q.qmpMonitorCh.qmp != nil { q.qmpMonitorCh.qmp.Shutdown() @@ -621,6 +621,9 @@ func (q *qemu) hotplugBlockDevice(drive Drive, op operation) error { return err } + // PCI address is in the format bridge-addr/device-addr eg. "03/02" + drive.PCIAddr = fmt.Sprintf("%02x", bridge.Addr) + "/" + addr + if err = q.qmpMonitorCh.qmp.ExecutePCIDeviceAdd(q.qmpMonitorCh.ctx, drive.ID, devID, driver, addr, bridge.ID); err != nil { return err } @@ -700,7 +703,7 @@ func (q *qemu) hotplugVFIODevice(device VFIODevice, op operation) error { func (q *qemu) hotplugDevice(devInfo interface{}, devType deviceType, op operation) error { switch devType { case blockDev: - drive := devInfo.(Drive) + drive := devInfo.(*Drive) return q.hotplugBlockDevice(drive, op) case cpuDev: vcpus := devInfo.(uint32) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 8cc92dc435..daa7a7d13f 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -237,6 +237,9 @@ type Drive struct { // Index assigned to the drive. In case of virtio-scsi, this is used as SCSI LUN index Index int + + // PCIAddr is the PCI address used to identify the slot at which the drive is attached. + PCIAddr string } // EnvVar is a key/value structure representing a command