mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-05 19:47:53 +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
|
// VhostUserDeviceAttrs represents data shared by most vhost-user devices
|
||||||
type VhostUserDeviceAttrs struct {
|
type VhostUserDeviceAttrs struct {
|
||||||
ID string
|
DevID string
|
||||||
SocketPath string
|
SocketPath string
|
||||||
Type DeviceType
|
Type DeviceType
|
||||||
|
|
||||||
|
@ -18,16 +18,17 @@ const maxDevIDSize = 31
|
|||||||
|
|
||||||
// BlockDevice refers to a block storage device implementation.
|
// BlockDevice refers to a block storage device implementation.
|
||||||
type BlockDevice struct {
|
type BlockDevice struct {
|
||||||
ID string
|
*GenericDevice
|
||||||
DeviceInfo *config.DeviceInfo
|
|
||||||
BlockDrive *config.BlockDrive
|
BlockDrive *config.BlockDrive
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBlockDevice creates a new block device based on DeviceInfo
|
// NewBlockDevice creates a new block device based on DeviceInfo
|
||||||
func NewBlockDevice(devInfo *config.DeviceInfo) *BlockDevice {
|
func NewBlockDevice(devInfo *config.DeviceInfo) *BlockDevice {
|
||||||
return &BlockDevice{
|
return &BlockDevice{
|
||||||
ID: devInfo.ID,
|
GenericDevice: &GenericDevice{
|
||||||
DeviceInfo: devInfo,
|
ID: devInfo.ID,
|
||||||
|
DeviceInfo: devInfo,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,22 +106,15 @@ func (device *BlockDevice) Detach(devReceiver api.DeviceReceiver) error {
|
|||||||
return nil
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *BlockDevice) DeviceType() config.DeviceType {
|
func (device *BlockDevice) DeviceType() config.DeviceType {
|
||||||
return config.DeviceBlock
|
return config.DeviceBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeviceID returns device ID
|
|
||||||
func (device *BlockDevice) DeviceID() string {
|
|
||||||
return device.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDeviceInfo returns device information used for creating
|
// GetDeviceInfo returns device information used for creating
|
||||||
func (device *BlockDevice) GetDeviceInfo() interface{} {
|
func (device *BlockDevice) GetDeviceInfo() interface{} {
|
||||||
return device.BlockDrive
|
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
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *GenericDevice) DeviceType() config.DeviceType {
|
func (device *GenericDevice) DeviceType() config.DeviceType {
|
||||||
return config.DeviceGeneric
|
return config.DeviceGeneric
|
||||||
@ -62,3 +52,13 @@ func (device *GenericDevice) DeviceType() config.DeviceType {
|
|||||||
func (device *GenericDevice) GetDeviceInfo() interface{} {
|
func (device *GenericDevice) GetDeviceInfo() interface{} {
|
||||||
return device.DeviceInfo
|
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
|
// VFIODevice is a vfio device meant to be passed to the hypervisor
|
||||||
// to be used by the Virtual Machine.
|
// to be used by the Virtual Machine.
|
||||||
type VFIODevice struct {
|
type VFIODevice struct {
|
||||||
ID string
|
*GenericDevice
|
||||||
DeviceInfo *config.DeviceInfo
|
vfioDevs []*config.VFIODev
|
||||||
vfioDevs []*config.VFIODev
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVFIODevice create a new VFIO device
|
// NewVFIODevice create a new VFIO device
|
||||||
func NewVFIODevice(devInfo *config.DeviceInfo) *VFIODevice {
|
func NewVFIODevice(devInfo *config.DeviceInfo) *VFIODevice {
|
||||||
return &VFIODevice{
|
return &VFIODevice{
|
||||||
ID: devInfo.ID,
|
GenericDevice: &GenericDevice{
|
||||||
DeviceInfo: devInfo,
|
ID: devInfo.ID,
|
||||||
|
DeviceInfo: devInfo,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,26 +108,19 @@ func (device *VFIODevice) Detach(devReceiver api.DeviceReceiver) error {
|
|||||||
return nil
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *VFIODevice) DeviceType() config.DeviceType {
|
func (device *VFIODevice) DeviceType() config.DeviceType {
|
||||||
return config.DeviceVFIO
|
return config.DeviceVFIO
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeviceID returns device ID
|
|
||||||
func (device *VFIODevice) DeviceID() string {
|
|
||||||
return device.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDeviceInfo returns device information used for creating
|
// GetDeviceInfo returns device information used for creating
|
||||||
func (device *VFIODevice) GetDeviceInfo() interface{} {
|
func (device *VFIODevice) GetDeviceInfo() interface{} {
|
||||||
return device.vfioDevs
|
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
|
// getBDF returns the BDF of pci device
|
||||||
// Expected input strng format is [<domain>]:[<bus>][<slot>].[<func>] eg. 0000:02:10.0
|
// Expected input strng format is [<domain>]:[<bus>][<slot>].[<func>] eg. 0000:02:10.0
|
||||||
func getBDF(deviceSysStr string) (string, error) {
|
func getBDF(deviceSysStr string) (string, error) {
|
||||||
|
@ -16,8 +16,8 @@ import (
|
|||||||
|
|
||||||
// VhostUserBlkDevice is a block vhost-user based device
|
// VhostUserBlkDevice is a block vhost-user based device
|
||||||
type VhostUserBlkDevice struct {
|
type VhostUserBlkDevice struct {
|
||||||
|
*GenericDevice
|
||||||
config.VhostUserDeviceAttrs
|
config.VhostUserDeviceAttrs
|
||||||
DeviceInfo *config.DeviceInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -38,7 +38,7 @@ func (device *VhostUserBlkDevice) Attach(devReceiver api.DeviceReceiver) (err er
|
|||||||
}
|
}
|
||||||
id := hex.EncodeToString(randBytes)
|
id := hex.EncodeToString(randBytes)
|
||||||
|
|
||||||
device.ID = id
|
device.DevID = id
|
||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -60,16 +60,6 @@ func (device *VhostUserBlkDevice) Detach(devReceiver api.DeviceReceiver) error {
|
|||||||
return nil
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *VhostUserBlkDevice) DeviceType() config.DeviceType {
|
func (device *VhostUserBlkDevice) DeviceType() config.DeviceType {
|
||||||
return config.VhostUserBlk
|
return config.VhostUserBlk
|
||||||
@ -80,3 +70,6 @@ func (device *VhostUserBlkDevice) GetDeviceInfo() interface{} {
|
|||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
return &device.VhostUserDeviceAttrs
|
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
|
// VhostUserNetDevice is a network vhost-user based device
|
||||||
type VhostUserNetDevice struct {
|
type VhostUserNetDevice struct {
|
||||||
|
*GenericDevice
|
||||||
config.VhostUserDeviceAttrs
|
config.VhostUserDeviceAttrs
|
||||||
DeviceInfo *config.DeviceInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -38,7 +38,7 @@ func (device *VhostUserNetDevice) Attach(devReceiver api.DeviceReceiver) (err er
|
|||||||
}
|
}
|
||||||
id := hex.EncodeToString(randBytes)
|
id := hex.EncodeToString(randBytes)
|
||||||
|
|
||||||
device.ID = id
|
device.DevID = id
|
||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -60,16 +60,6 @@ func (device *VhostUserNetDevice) Detach(devReceiver api.DeviceReceiver) error {
|
|||||||
return nil
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *VhostUserNetDevice) DeviceType() config.DeviceType {
|
func (device *VhostUserNetDevice) DeviceType() config.DeviceType {
|
||||||
return config.VhostUserNet
|
return config.VhostUserNet
|
||||||
@ -80,3 +70,6 @@ func (device *VhostUserNetDevice) GetDeviceInfo() interface{} {
|
|||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
return &device.VhostUserDeviceAttrs
|
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
|
// VhostUserSCSIDevice is a SCSI vhost-user based device
|
||||||
type VhostUserSCSIDevice struct {
|
type VhostUserSCSIDevice struct {
|
||||||
|
*GenericDevice
|
||||||
config.VhostUserDeviceAttrs
|
config.VhostUserDeviceAttrs
|
||||||
DeviceInfo *config.DeviceInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -38,7 +38,7 @@ func (device *VhostUserSCSIDevice) Attach(devReceiver api.DeviceReceiver) (err e
|
|||||||
}
|
}
|
||||||
id := hex.EncodeToString(randBytes)
|
id := hex.EncodeToString(randBytes)
|
||||||
|
|
||||||
device.ID = id
|
device.DevID = id
|
||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -60,16 +60,6 @@ func (device *VhostUserSCSIDevice) Detach(devReceiver api.DeviceReceiver) error
|
|||||||
return nil
|
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
|
// DeviceType is standard interface of api.Device, it returns device type
|
||||||
func (device *VhostUserSCSIDevice) DeviceType() config.DeviceType {
|
func (device *VhostUserSCSIDevice) DeviceType() config.DeviceType {
|
||||||
return config.VhostUserSCSI
|
return config.VhostUserSCSI
|
||||||
@ -80,3 +70,6 @@ func (device *VhostUserSCSIDevice) GetDeviceInfo() interface{} {
|
|||||||
device.Type = device.DeviceType()
|
device.Type = device.DeviceType()
|
||||||
return &device.VhostUserDeviceAttrs
|
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)
|
id := hex.EncodeToString(randBytes)
|
||||||
|
|
||||||
d := config.VhostUserDeviceAttrs{
|
d := config.VhostUserDeviceAttrs{
|
||||||
ID: id,
|
DevID: id,
|
||||||
SocketPath: endpoint.SocketPath,
|
SocketPath: endpoint.SocketPath,
|
||||||
MacAddress: endpoint.HardAddr,
|
MacAddress: endpoint.HardAddr,
|
||||||
Type: config.VhostUserNet,
|
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
|
// TODO: find a way to remove dependency of drivers package
|
||||||
switch attr.Type {
|
switch attr.Type {
|
||||||
case config.VhostUserNet:
|
case config.VhostUserNet:
|
||||||
qemuVhostUserDevice.TypeDevID = utils.MakeNameID("net", attr.ID, maxDevIDSize)
|
qemuVhostUserDevice.TypeDevID = utils.MakeNameID("net", attr.DevID, maxDevIDSize)
|
||||||
qemuVhostUserDevice.Address = attr.MacAddress
|
qemuVhostUserDevice.Address = attr.MacAddress
|
||||||
case config.VhostUserSCSI:
|
case config.VhostUserSCSI:
|
||||||
qemuVhostUserDevice.TypeDevID = utils.MakeNameID("scsi", attr.ID, maxDevIDSize)
|
qemuVhostUserDevice.TypeDevID = utils.MakeNameID("scsi", attr.DevID, maxDevIDSize)
|
||||||
case config.VhostUserBlk:
|
case config.VhostUserBlk:
|
||||||
}
|
}
|
||||||
|
|
||||||
qemuVhostUserDevice.VhostUserType = govmmQemu.VhostUserDeviceType(attr.Type)
|
qemuVhostUserDevice.VhostUserType = govmmQemu.VhostUserDeviceType(attr.Type)
|
||||||
qemuVhostUserDevice.SocketPath = attr.SocketPath
|
qemuVhostUserDevice.SocketPath = attr.SocketPath
|
||||||
qemuVhostUserDevice.CharDevID = utils.MakeNameID("char", attr.ID, maxDevIDSize)
|
qemuVhostUserDevice.CharDevID = utils.MakeNameID("char", attr.DevID, maxDevIDSize)
|
||||||
|
|
||||||
devices = append(devices, qemuVhostUserDevice)
|
devices = append(devices, qemuVhostUserDevice)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user