gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/device/api/interface.go (about) 1 // Copyright (c) 2017-2018 Intel Corporation 2 // Copyright (c) 2018 Huawei Corporation 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 // 6 7 package api 8 9 import ( 10 "github.com/kata-containers/runtime/virtcontainers/device/config" 11 persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" 12 "github.com/sirupsen/logrus" 13 ) 14 15 var devLogger = logrus.WithField("subsystem", "device") 16 17 // SetLogger sets the logger for device api package. 18 func SetLogger(logger *logrus.Entry) { 19 fields := devLogger.Data 20 devLogger = logger.WithFields(fields) 21 } 22 23 // DeviceLogger returns logger for device management 24 func DeviceLogger() *logrus.Entry { 25 return devLogger 26 } 27 28 // DeviceReceiver is an interface used for accepting devices 29 // a device should be attached/added/plugged to a DeviceReceiver 30 type DeviceReceiver interface { 31 // these are for hotplug/hot-unplug devices to/from hypervisor 32 HotplugAddDevice(Device, config.DeviceType) error 33 HotplugRemoveDevice(Device, config.DeviceType) error 34 35 // this is only for virtio-blk and virtio-scsi support 36 GetAndSetSandboxBlockIndex() (int, error) 37 UnsetSandboxBlockIndex(int) error 38 GetHypervisorType() string 39 40 // this is for appending device to hypervisor boot params 41 AppendDevice(Device) error 42 } 43 44 // Device is the virtcontainers device interface. 45 type Device interface { 46 Attach(DeviceReceiver) error 47 Detach(DeviceReceiver) error 48 49 // ID returns device identifier 50 DeviceID() string 51 52 // DeviceType indicates which kind of device it is 53 // e.g. block, vfio or vhost user 54 DeviceType() config.DeviceType 55 56 // GetMajorMinor returns major and minor numbers 57 GetMajorMinor() (int64, int64) 58 59 // GetHostPath return the device path in the host 60 GetHostPath() string 61 62 // GetDeviceInfo returns device specific data used for hotplugging by hypervisor 63 // Caller could cast the return value to device specific struct 64 // e.g. Block device returns *config.BlockDrive, 65 // vfio device returns []*config.VFIODev, 66 // VhostUser device returns []*config.VhostUserDeviceAttrs 67 GetDeviceInfo() interface{} 68 69 // GetAttachCount returns how many times the device has been attached 70 GetAttachCount() uint 71 72 // Reference adds one reference to device then returns final ref count 73 Reference() uint 74 75 // Dereference removes one reference to device then returns final ref count 76 Dereference() uint 77 78 // Save converts Device to DeviceState 79 Save() persistapi.DeviceState 80 81 // Load loads DeviceState and converts it to specific device 82 Load(persistapi.DeviceState) 83 } 84 85 // DeviceManager can be used to create a new device, this can be used as single 86 // device management object. 87 type DeviceManager interface { 88 NewDevice(config.DeviceInfo) (Device, error) 89 RemoveDevice(string) error 90 AttachDevice(string, DeviceReceiver) error 91 DetachDevice(string, DeviceReceiver) error 92 IsDeviceAttached(string) bool 93 GetDeviceByID(string) Device 94 GetAllDevices() []Device 95 LoadDevices([]persistapi.DeviceState) 96 }