diff --git a/virtcontainers/device/config/config.go b/virtcontainers/device/config/config.go index d9c3dfe934..3f77540d20 100644 --- a/virtcontainers/device/config/config.go +++ b/virtcontainers/device/config/config.go @@ -123,7 +123,7 @@ type VFIODev struct { // VhostUserDeviceAttrs represents data shared by most vhost-user devices type VhostUserDeviceAttrs struct { - ID string + DevID string SocketPath string Type DeviceType diff --git a/virtcontainers/device/drivers/block.go b/virtcontainers/device/drivers/block.go index 38e5a4b64b..41d7087c17 100644 --- a/virtcontainers/device/drivers/block.go +++ b/virtcontainers/device/drivers/block.go @@ -18,16 +18,17 @@ const maxDevIDSize = 31 // BlockDevice refers to a block storage device implementation. type BlockDevice struct { - ID string - DeviceInfo *config.DeviceInfo + *GenericDevice BlockDrive *config.BlockDrive } // NewBlockDevice creates a new block device based on DeviceInfo func NewBlockDevice(devInfo *config.DeviceInfo) *BlockDevice { return &BlockDevice{ - ID: devInfo.ID, - DeviceInfo: devInfo, + GenericDevice: &GenericDevice{ + ID: devInfo.ID, + DeviceInfo: devInfo, + }, } } @@ -105,22 +106,15 @@ func (device *BlockDevice) Detach(devReceiver api.DeviceReceiver) error { return nil } -// IsAttached checks if the device is attached -func (device *BlockDevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - // DeviceType is standard interface of api.Device, it returns device type func (device *BlockDevice) DeviceType() config.DeviceType { return config.DeviceBlock } -// DeviceID returns device ID -func (device *BlockDevice) DeviceID() string { - return device.ID -} - // GetDeviceInfo returns device information used for creating func (device *BlockDevice) GetDeviceInfo() interface{} { return device.BlockDrive } + +// It should implement IsAttached() and DeviceID() as api.Device implementation +// here it shares function from *GenericDevice so we don't need duplicate codes diff --git a/virtcontainers/device/drivers/generic.go b/virtcontainers/device/drivers/generic.go index 431d178439..8b03b9c75a 100644 --- a/virtcontainers/device/drivers/generic.go +++ b/virtcontainers/device/drivers/generic.go @@ -43,16 +43,6 @@ func (device *GenericDevice) Detach(devReceiver api.DeviceReceiver) error { return nil } -// IsAttached checks if the device is attached -func (device *GenericDevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - -// DeviceID returns device ID -func (device *GenericDevice) DeviceID() string { - return device.ID -} - // DeviceType is standard interface of api.Device, it returns device type func (device *GenericDevice) DeviceType() config.DeviceType { return config.DeviceGeneric @@ -62,3 +52,13 @@ func (device *GenericDevice) DeviceType() config.DeviceType { func (device *GenericDevice) GetDeviceInfo() interface{} { return device.DeviceInfo } + +// IsAttached checks if the device is attached +func (device *GenericDevice) IsAttached() bool { + return device.DeviceInfo.Hotplugged +} + +// DeviceID returns device ID +func (device *GenericDevice) DeviceID() string { + return device.ID +} diff --git a/virtcontainers/device/drivers/vfio.go b/virtcontainers/device/drivers/vfio.go index c5d7037452..351b867353 100644 --- a/virtcontainers/device/drivers/vfio.go +++ b/virtcontainers/device/drivers/vfio.go @@ -30,16 +30,17 @@ const ( // VFIODevice is a vfio device meant to be passed to the hypervisor // to be used by the Virtual Machine. type VFIODevice struct { - ID string - DeviceInfo *config.DeviceInfo - vfioDevs []*config.VFIODev + *GenericDevice + vfioDevs []*config.VFIODev } // NewVFIODevice create a new VFIO device func NewVFIODevice(devInfo *config.DeviceInfo) *VFIODevice { return &VFIODevice{ - ID: devInfo.ID, - DeviceInfo: devInfo, + GenericDevice: &GenericDevice{ + ID: devInfo.ID, + DeviceInfo: devInfo, + }, } } @@ -107,26 +108,19 @@ func (device *VFIODevice) Detach(devReceiver api.DeviceReceiver) error { return nil } -// IsAttached checks if the device is attached -func (device *VFIODevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - // DeviceType is standard interface of api.Device, it returns device type func (device *VFIODevice) DeviceType() config.DeviceType { return config.DeviceVFIO } -// DeviceID returns device ID -func (device *VFIODevice) DeviceID() string { - return device.ID -} - // GetDeviceInfo returns device information used for creating func (device *VFIODevice) GetDeviceInfo() interface{} { return device.vfioDevs } +// It should implement IsAttached() and DeviceID() as api.Device implementation +// here it shares function from *GenericDevice so we don't need duplicate codes + // getBDF returns the BDF of pci device // Expected input strng format is []:[][].[] eg. 0000:02:10.0 func getBDF(deviceSysStr string) (string, error) { diff --git a/virtcontainers/device/drivers/vhost_user_blk.go b/virtcontainers/device/drivers/vhost_user_blk.go index 6bb629461f..80e4c598fe 100644 --- a/virtcontainers/device/drivers/vhost_user_blk.go +++ b/virtcontainers/device/drivers/vhost_user_blk.go @@ -16,8 +16,8 @@ import ( // VhostUserBlkDevice is a block vhost-user based device type VhostUserBlkDevice struct { + *GenericDevice config.VhostUserDeviceAttrs - DeviceInfo *config.DeviceInfo } // @@ -38,7 +38,7 @@ func (device *VhostUserBlkDevice) Attach(devReceiver api.DeviceReceiver) (err er } id := hex.EncodeToString(randBytes) - device.ID = id + device.DevID = id device.Type = device.DeviceType() defer func() { @@ -60,16 +60,6 @@ func (device *VhostUserBlkDevice) Detach(devReceiver api.DeviceReceiver) error { return nil } -// IsAttached checks if the device is attached -func (device *VhostUserBlkDevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - -// DeviceID returns device ID -func (device *VhostUserBlkDevice) DeviceID() string { - return device.ID -} - // DeviceType is standard interface of api.Device, it returns device type func (device *VhostUserBlkDevice) DeviceType() config.DeviceType { return config.VhostUserBlk @@ -80,3 +70,6 @@ func (device *VhostUserBlkDevice) GetDeviceInfo() interface{} { device.Type = device.DeviceType() return &device.VhostUserDeviceAttrs } + +// It should implement IsAttached() and DeviceID() as api.Device implementation +// here it shares function from *GenericDevice so we don't need duplicate codes diff --git a/virtcontainers/device/drivers/vhost_user_net.go b/virtcontainers/device/drivers/vhost_user_net.go index 6a731542ce..b5961ea8d6 100644 --- a/virtcontainers/device/drivers/vhost_user_net.go +++ b/virtcontainers/device/drivers/vhost_user_net.go @@ -16,8 +16,8 @@ import ( // VhostUserNetDevice is a network vhost-user based device type VhostUserNetDevice struct { + *GenericDevice config.VhostUserDeviceAttrs - DeviceInfo *config.DeviceInfo } // @@ -38,7 +38,7 @@ func (device *VhostUserNetDevice) Attach(devReceiver api.DeviceReceiver) (err er } id := hex.EncodeToString(randBytes) - device.ID = id + device.DevID = id device.Type = device.DeviceType() defer func() { @@ -60,16 +60,6 @@ func (device *VhostUserNetDevice) Detach(devReceiver api.DeviceReceiver) error { return nil } -// IsAttached checks if the device is attached -func (device *VhostUserNetDevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - -// DeviceID returns device ID -func (device *VhostUserNetDevice) DeviceID() string { - return device.ID -} - // DeviceType is standard interface of api.Device, it returns device type func (device *VhostUserNetDevice) DeviceType() config.DeviceType { return config.VhostUserNet @@ -80,3 +70,6 @@ func (device *VhostUserNetDevice) GetDeviceInfo() interface{} { device.Type = device.DeviceType() return &device.VhostUserDeviceAttrs } + +// It should implement IsAttached() and DeviceID() as api.Device implementation +// here it shares function from *GenericDevice so we don't need duplicate codes diff --git a/virtcontainers/device/drivers/vhost_user_scsi.go b/virtcontainers/device/drivers/vhost_user_scsi.go index ce5f846cd5..dcd79f47c3 100644 --- a/virtcontainers/device/drivers/vhost_user_scsi.go +++ b/virtcontainers/device/drivers/vhost_user_scsi.go @@ -16,8 +16,8 @@ import ( // VhostUserSCSIDevice is a SCSI vhost-user based device type VhostUserSCSIDevice struct { + *GenericDevice config.VhostUserDeviceAttrs - DeviceInfo *config.DeviceInfo } // @@ -38,7 +38,7 @@ func (device *VhostUserSCSIDevice) Attach(devReceiver api.DeviceReceiver) (err e } id := hex.EncodeToString(randBytes) - device.ID = id + device.DevID = id device.Type = device.DeviceType() defer func() { @@ -60,16 +60,6 @@ func (device *VhostUserSCSIDevice) Detach(devReceiver api.DeviceReceiver) error return nil } -// IsAttached checks if the device is attached -func (device *VhostUserSCSIDevice) IsAttached() bool { - return device.DeviceInfo.Hotplugged -} - -// DeviceID returns device ID -func (device *VhostUserSCSIDevice) DeviceID() string { - return device.ID -} - // DeviceType is standard interface of api.Device, it returns device type func (device *VhostUserSCSIDevice) DeviceType() config.DeviceType { return config.VhostUserSCSI @@ -80,3 +70,6 @@ func (device *VhostUserSCSIDevice) GetDeviceInfo() interface{} { device.Type = device.DeviceType() return &device.VhostUserDeviceAttrs } + +// It should implement IsAttached() and DeviceID() as api.Device implementation +// here it shares function from *GenericDevice so we don't need duplicate codes diff --git a/virtcontainers/network.go b/virtcontainers/network.go index 8768ffa377..ec986f9969 100644 --- a/virtcontainers/network.go +++ b/virtcontainers/network.go @@ -323,7 +323,7 @@ func (endpoint *VhostUserEndpoint) Attach(h hypervisor) error { id := hex.EncodeToString(randBytes) d := config.VhostUserDeviceAttrs{ - ID: id, + DevID: id, SocketPath: endpoint.SocketPath, MacAddress: endpoint.HardAddr, Type: config.VhostUserNet, diff --git a/virtcontainers/qemu_arch_base.go b/virtcontainers/qemu_arch_base.go index d65bc9cb72..0925bd9ce4 100644 --- a/virtcontainers/qemu_arch_base.go +++ b/virtcontainers/qemu_arch_base.go @@ -476,16 +476,16 @@ func (q *qemuArchBase) appendVhostUserDevice(devices []govmmQemu.Device, attr co // TODO: find a way to remove dependency of drivers package switch attr.Type { case config.VhostUserNet: - qemuVhostUserDevice.TypeDevID = utils.MakeNameID("net", attr.ID, maxDevIDSize) + qemuVhostUserDevice.TypeDevID = utils.MakeNameID("net", attr.DevID, maxDevIDSize) qemuVhostUserDevice.Address = attr.MacAddress case config.VhostUserSCSI: - qemuVhostUserDevice.TypeDevID = utils.MakeNameID("scsi", attr.ID, maxDevIDSize) + qemuVhostUserDevice.TypeDevID = utils.MakeNameID("scsi", attr.DevID, maxDevIDSize) case config.VhostUserBlk: } qemuVhostUserDevice.VhostUserType = govmmQemu.VhostUserDeviceType(attr.Type) qemuVhostUserDevice.SocketPath = attr.SocketPath - qemuVhostUserDevice.CharDevID = utils.MakeNameID("char", attr.ID, maxDevIDSize) + qemuVhostUserDevice.CharDevID = utils.MakeNameID("char", attr.DevID, maxDevIDSize) devices = append(devices, qemuVhostUserDevice)