diff --git a/src/runtime/pkg/device/config/config.go b/src/runtime/pkg/device/config/config.go index 48280092d6..748c5b4e51 100644 --- a/src/runtime/pkg/device/config/config.go +++ b/src/runtime/pkg/device/config/config.go @@ -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 +} diff --git a/src/runtime/pkg/device/config/device_state.go b/src/runtime/pkg/device/config/device_state.go deleted file mode 100644 index 36d513fa65..0000000000 --- a/src/runtime/pkg/device/config/device_state.go +++ /dev/null @@ -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 -} diff --git a/src/runtime/pkg/device/drivers/block.go b/src/runtime/pkg/device/drivers/block.go index 6088a5c9f5..d2e9644fde 100644 --- a/src/runtime/pkg/device/drivers/block.go +++ b/src/runtime/pkg/device/drivers/block.go @@ -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 diff --git a/src/runtime/pkg/device/drivers/vfio.go b/src/runtime/pkg/device/drivers/vfio.go index ed791f8416..58658b0b88 100644 --- a/src/runtime/pkg/device/drivers/vfio.go +++ b/src/runtime/pkg/device/drivers/vfio.go @@ -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 diff --git a/src/runtime/pkg/device/drivers/vhost_user_blk.go b/src/runtime/pkg/device/drivers/vhost_user_blk.go index 75d65d0a5a..49c66e7117 100644 --- a/src/runtime/pkg/device/drivers/vhost_user_blk.go +++ b/src/runtime/pkg/device/drivers/vhost_user_blk.go @@ -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 diff --git a/src/runtime/pkg/device/drivers/vhost_user_net.go b/src/runtime/pkg/device/drivers/vhost_user_net.go index 7441c2d892..39c38751e2 100644 --- a/src/runtime/pkg/device/drivers/vhost_user_net.go +++ b/src/runtime/pkg/device/drivers/vhost_user_net.go @@ -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 diff --git a/src/runtime/pkg/device/drivers/vhost_user_scsi.go b/src/runtime/pkg/device/drivers/vhost_user_scsi.go index 7fbcc382c4..bb9fe97227 100644 --- a/src/runtime/pkg/device/drivers/vhost_user_scsi.go +++ b/src/runtime/pkg/device/drivers/vhost_user_scsi.go @@ -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