device: deduplicate state structures

Before, we maintained almost identical structures between our persist
API and what we keep for our devices, with the persist API being a
slight subset of device structures.

Let's deduplicate this, now that persist is importing device package.
Json unmarshal of prior persist structure will work fine, since it was
an exact subset of fields.

Fixes: #4468

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
Eric Ernst 2022-06-16 03:57:20 -07:00
parent f97d9b45c8
commit e32bf53318
7 changed files with 59 additions and 224 deletions

View File

@ -441,3 +441,45 @@ func getVhostUserDevName(dirname string, majorNum, minorNum uint32) (string, err
return "", fmt.Errorf("Required device node (%d:%d) doesn't exist under directory %s",
majorNum, minorNum, dirname)
}
// DeviceState is a structure which represents host devices
// plugged to a hypervisor, one Device can be shared among containers in POD
// Refs: virtcontainers/device/drivers/generic.go:GenericDevice
type DeviceState struct {
// DriverOptions is specific options for each device driver
// for example, for BlockDevice, we can set DriverOptions["block-driver"]="virtio-blk"
DriverOptions map[string]string
// VhostUserDeviceAttrs is specific for vhost-user device driver
VhostUserDev *VhostUserDeviceAttrs `json:",omitempty"`
// BlockDrive is specific for block device driver
BlockDrive *BlockDrive `json:",omitempty"`
ID string
// Type is used to specify driver type
// Refs: virtcontainers/device/config/config.go:DeviceType
Type string
// Type of device: c, b, u or p
// c , u - character(unbuffered)
// p - FIFO
// b - block(buffered) special file
// More info in mknod(1).
DevType string
// VFIODev is specific VFIO device driver
VFIODevs []*VFIODev `json:",omitempty"`
RefCount uint
AttachCount uint
// Major, minor numbers for device.
Major int64
Minor int64
// ColdPlug specifies whether the device must be cold plugged (true)
// or hot plugged (false).
ColdPlug bool
}

View File

@ -1,124 +0,0 @@
// Copyright (c) 2016 Intel Corporation
// Copyright (c) 2019 Huawei Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package config
import vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
// ============= sandbox level resources =============
// BlockDrive represents a block storage drive which may be used in case the storage
// driver has an underlying block storage device.
type BlockDriveState struct {
// File is the path to the disk-image/device which will be used with this drive
File string
// Format of the drive
Format string
// ID is used to identify this drive in the hypervisor options.
ID string
// MmioAddr is used to identify the slot at which the drive is attached (order?).
MmioAddr string
// SCSI Address of the block device, in case the device is attached using SCSI driver
// SCSI address is in the format SCSI-Id:LUN
SCSIAddr string
// NvdimmID is the nvdimm id inside the VM
NvdimmID string
// VirtPath at which the device appears inside the VM, outside of the container mount namespace
VirtPath string
// DevNo
DevNo string
// PCIPath is the PCI path used to identify the slot at which the drive is attached.
PCIPath vcTypes.PciPath
// Index assigned to the drive. In case of virtio-scsi, this is used as SCSI LUN index
Index int
// Pmem enabled persistent memory. Use File as backing file
// for a nvdimm device in the guest.
Pmem bool
}
// VFIODev represents a VFIO drive used for hotplugging
type VFIODevState struct {
// ID is used to identify this drive in the hypervisor options.
ID string
// BDF (Bus:Device.Function) of the PCI address
BDF string
// Sysfsdev of VFIO mediated device
SysfsDev string
// Type of VFIO device
Type uint32
}
// VhostUserDeviceAttrs represents data shared by most vhost-user devices
type VhostUserDeviceAttrsState struct {
DevID string
SocketPath string
Type string
// MacAddress is only meaningful for vhost user net device
MacAddress string
// PCIPath is the PCI path used to identify the slot at which the drive is attached.
// It is only meaningful for vhost user block devices
PCIPath vcTypes.PciPath
// Block index of the device if assigned
Index int
}
// DeviceState is sandbox level resource which represents host devices
// plugged to hypervisor, one Device can be shared among containers in POD
// Refs: virtcontainers/device/drivers/generic.go:GenericDevice
type DeviceState struct {
// DriverOptions is specific options for each device driver
// for example, for BlockDevice, we can set DriverOptions["block-driver"]="virtio-blk"
DriverOptions map[string]string
// VhostUserDeviceAttrsState is specific for vhost-user device driver
VhostUserDev *VhostUserDeviceAttrsState `json:",omitempty"`
// BlockDrive is specific for block device driver
BlockDrive *BlockDriveState `json:",omitempty"`
ID string
// Type is used to specify driver type
// Refs: virtcontainers/device/config/config.go:DeviceType
Type string
// Type of device: c, b, u or p
// c , u - character(unbuffered)
// p - FIFO
// b - block(buffered) special file
// More info in mknod(1).
DevType string
// VFIODevState is specific VFIO device driver
VFIODevs []*VFIODevState `json:",omitempty"`
RefCount uint
AttachCount uint
// Major, minor numbers for device.
Major int64
Minor int64
// ColdPlug specifies whether the device must be cold plugged (true)
// or hot plugged (false).
ColdPlug bool
}

View File

@ -162,22 +162,8 @@ func (device *BlockDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
drive := device.BlockDrive
if drive != nil {
ds.BlockDrive = &config.BlockDriveState{
File: drive.File,
Format: drive.Format,
ID: drive.ID,
Index: drive.Index,
MmioAddr: drive.MmioAddr,
PCIPath: drive.PCIPath,
SCSIAddr: drive.SCSIAddr,
NvdimmID: drive.NvdimmID,
VirtPath: drive.VirtPath,
DevNo: drive.DevNo,
Pmem: drive.Pmem,
}
}
ds.BlockDrive = device.BlockDrive
return ds
}
@ -186,23 +172,7 @@ func (device *BlockDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)
bd := ds.BlockDrive
if bd == nil {
return
}
device.BlockDrive = &config.BlockDrive{
File: bd.File,
Format: bd.Format,
ID: bd.ID,
Index: bd.Index,
MmioAddr: bd.MmioAddr,
PCIPath: bd.PCIPath,
SCSIAddr: bd.SCSIAddr,
NvdimmID: bd.NvdimmID,
VirtPath: bd.VirtPath,
DevNo: bd.DevNo,
Pmem: bd.Pmem,
}
device.BlockDrive = ds.BlockDrive
}
// It should implement GetAttachCount() and DeviceID() as api.Device implementation

View File

@ -180,12 +180,7 @@ func (device *VFIODevice) Save() config.DeviceState {
devs := device.VfioDevs
for _, dev := range devs {
if dev != nil {
ds.VFIODevs = append(ds.VFIODevs, &config.VFIODevState{
ID: dev.ID,
Type: uint32(dev.Type),
BDF: dev.BDF,
SysfsDev: dev.SysfsDev,
})
ds.VFIODevs = append(ds.VFIODevs, dev)
}
}
return ds

View File

@ -158,17 +158,8 @@ func (device *VhostUserBlkDevice) GetDeviceInfo() interface{} {
func (device *VhostUserBlkDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
ds.VhostUserDev = device.VhostUserDeviceAttrs
vAttr := device.VhostUserDeviceAttrs
if vAttr != nil {
ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: vAttr.DevID,
SocketPath: vAttr.SocketPath,
Type: string(vAttr.Type),
PCIPath: vAttr.PCIPath,
Index: vAttr.Index,
}
}
return ds
}
@ -176,19 +167,7 @@ func (device *VhostUserBlkDevice) Save() config.DeviceState {
func (device *VhostUserBlkDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)
dev := ds.VhostUserDev
if dev == nil {
return
}
device.VhostUserDeviceAttrs = &config.VhostUserDeviceAttrs{
DevID: dev.DevID,
SocketPath: dev.SocketPath,
Type: config.DeviceType(dev.Type),
PCIPath: dev.PCIPath,
Index: dev.Index,
}
device.VhostUserDeviceAttrs = ds.VhostUserDev
}
// It should implement GetAttachCount() and DeviceID() as api.Device implementation

View File

@ -18,7 +18,7 @@ import (
// VhostUserNetDevice is a network vhost-user based device
type VhostUserNetDevice struct {
*GenericDevice
config.VhostUserDeviceAttrs
*config.VhostUserDeviceAttrs
}
//
@ -70,19 +70,16 @@ func (device *VhostUserNetDevice) DeviceType() config.DeviceType {
// GetDeviceInfo returns device information used for creating
func (device *VhostUserNetDevice) GetDeviceInfo() interface{} {
device.Type = device.DeviceType()
return &device.VhostUserDeviceAttrs
return device.VhostUserDeviceAttrs
}
// Save converts Device to DeviceState
func (device *VhostUserNetDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: device.DevID,
SocketPath: device.SocketPath,
Type: string(device.Type),
MacAddress: device.MacAddress,
}
ds.VhostUserDev = device.VhostUserDeviceAttrs
return ds
}
@ -91,17 +88,7 @@ func (device *VhostUserNetDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)
dev := ds.VhostUserDev
if dev == nil {
return
}
device.VhostUserDeviceAttrs = config.VhostUserDeviceAttrs{
DevID: dev.DevID,
SocketPath: dev.SocketPath,
Type: config.DeviceType(dev.Type),
MacAddress: dev.MacAddress,
}
device.VhostUserDeviceAttrs = ds.VhostUserDev
}
// It should implement GetAttachCount() and DeviceID() as api.Device implementation

View File

@ -18,7 +18,7 @@ import (
// VhostUserSCSIDevice is a SCSI vhost-user based device
type VhostUserSCSIDevice struct {
*GenericDevice
config.VhostUserDeviceAttrs
*config.VhostUserDeviceAttrs
}
//
@ -70,19 +70,15 @@ func (device *VhostUserSCSIDevice) DeviceType() config.DeviceType {
// GetDeviceInfo returns device information used for creating
func (device *VhostUserSCSIDevice) GetDeviceInfo() interface{} {
device.Type = device.DeviceType()
return &device.VhostUserDeviceAttrs
return device.VhostUserDeviceAttrs
}
// Save converts Device to DeviceState
func (device *VhostUserSCSIDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType())
ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: device.DevID,
SocketPath: device.SocketPath,
Type: string(device.Type),
MacAddress: device.MacAddress,
}
ds.VhostUserDev = device.VhostUserDeviceAttrs
return ds
}
@ -91,17 +87,7 @@ func (device *VhostUserSCSIDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds)
dev := ds.VhostUserDev
if dev == nil {
return
}
device.VhostUserDeviceAttrs = config.VhostUserDeviceAttrs{
DevID: dev.DevID,
SocketPath: dev.SocketPath,
Type: config.DeviceType(dev.Type),
MacAddress: dev.MacAddress,
}
device.VhostUserDeviceAttrs = ds.VhostUserDev
}
// It should implement GetAttachCount() and DeviceID() as api.Device implementation