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

Commit

Permalink
devices: share genericDevice logic among devices
Browse files Browse the repository at this point in the history
Fixes #635

Shares generic device logic among all device drivers to reduce
duplicated codes.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Aug 31, 2018
1 parent 7d14aea commit 7f4b221
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 80 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/device/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 8 additions & 14 deletions virtcontainers/device/drivers/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand Down Expand Up @@ -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
20 changes: 10 additions & 10 deletions virtcontainers/device/drivers/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
24 changes: 9 additions & 15 deletions virtcontainers/device/drivers/vfio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand Down Expand Up @@ -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 [<domain>]:[<bus>][<slot>].[<func>] eg. 0000:02:10.0
func getBDF(deviceSysStr string) (string, error) {
Expand Down
17 changes: 5 additions & 12 deletions virtcontainers/device/drivers/vhost_user_blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// VhostUserBlkDevice is a block vhost-user based device
type VhostUserBlkDevice struct {
*GenericDevice
config.VhostUserDeviceAttrs
DeviceInfo *config.DeviceInfo
}

//
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
17 changes: 5 additions & 12 deletions virtcontainers/device/drivers/vhost_user_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// VhostUserNetDevice is a network vhost-user based device
type VhostUserNetDevice struct {
*GenericDevice
config.VhostUserDeviceAttrs
DeviceInfo *config.DeviceInfo
}

//
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
17 changes: 5 additions & 12 deletions virtcontainers/device/drivers/vhost_user_scsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// VhostUserSCSIDevice is a SCSI vhost-user based device
type VhostUserSCSIDevice struct {
*GenericDevice
config.VhostUserDeviceAttrs
DeviceInfo *config.DeviceInfo
}

//
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion virtcontainers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions virtcontainers/qemu_arch_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 7f4b221

Please sign in to comment.