mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-04 11:06:21 +00:00
devices: share genericDevice logic among devices
Fixes #635 Shares generic device logic among all device drivers to reduce duplicated codes. Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
This commit is contained in:
parent
7d14aea067
commit
7f4b221bc3
@ -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
|
||||
|
||||
|
@ -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{
|
||||
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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
*GenericDevice
|
||||
vfioDevs []*config.VFIODev
|
||||
}
|
||||
|
||||
// NewVFIODevice create a new VFIO device
|
||||
func NewVFIODevice(devInfo *config.DeviceInfo) *VFIODevice {
|
||||
return &VFIODevice{
|
||||
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 [<domain>]:[<bus>][<slot>].[<func>] eg. 0000:02:10.0
|
||||
func getBDF(deviceSysStr string) (string, error) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user