Nova/USB device&USB controller

Related Content
https://wiki.openstack.org/wiki/Nova/USB_controller_hot_cold_plug

https://wiki.openstack.org/wiki/Nova/USB_device_hot_cold_plug

https://wiki.openstack.org/wiki/USB-api-support

Background
Currently, nova has supported function of pci-passthrough, but hasn't supported usb-passthrough. I think usb-passthrough is also a important and necessary function especially in private cloud.

User scenarios
1. When a administrator wants to deploy a vm for running erp software which need usb-key for authentication, him may need to insert usb-key to a host(compute node) first and then run vm with usb-key in this host.

2. When a user wants to copy data from vm to a removable media for business trip him may need a usb data disk. ps: The usb redirection technology over spice or vnc connection maybe more appropriate in thest scenarios.

Technical verification
1. Test case Creating vm with different type and number usb controller and usb device.

2. Xml definition (1)Sample of usb controller:  

(2)Sample of pass-through usb devices  

(3)Sample of emulated usb disks   < source file='/home/vms/usb/sdb.qcow2'/>  

3. The test results (1)Usb controllers have a maximum port restriction. Uhci controller supports a maxmum of 2 ports while ehci controller supports 6. (2)If creating vm without usb controller, qemu will default create a piix3-usb-uhci controller but not creating the other type's.  (3)If creating vm with usb device but no usb controller, the usb device will default be attached to the default uhci controller, but speed may mismatch. (4)There are two ways to support more usb devices. 1. Adding cascaded hubs and attaching usb devices to them. 2.Creating more controllers and connect usb devices to different ones. (5)When creating vm with a pair of usb hub and ehci controller, the vm crashed. Qemu thrown a error(why?)

Proposed solution
1. Use cases Case 1: (1)Admin/User request to create a flavor with usb controllers(key arguments: type, count). (2)System creates a flavor containing usb controller information. (3)Admin/User request to create a vm with flavor created above. (4)System first validates usb arguments specified in flavor, if valid then create a vm with usb controllers.

Case 2: (1)Admin/User request to create a flavor with usb devices(key arguments: unique identifiers of devices). (2)System creates a flavor containing usb device information. (3)Admin/User request to create a vm with flavor created above. (4)System first validates if usb devices are exist, available, and in the same host, if valid then create a vm with usb devices in the host. The usb devices will be attached to the default uhci controller created by qemu.

Case 3: (1)Admin/User request to create a flavor with usb controllers(key argument: type, count) and usb devices(key arguments: identifiers and usb controller type). (2)System creates a flavor containing usb device information and usb controller information. (3)Admin/User request to create a vm with flavor created above. (4)Admin/User first validate if usb devices are exist, available,in the same host, and matching with controllers(type, have available port and so on), if valid then create a vm with usb devices and usb controllers in the host.

2. System requirements and things should be considered for usb controller(An optional Implementation): (1)System supports creating a flavor with usb controller. Adding a property for usb controller to extra_specs field of flavor. The scheme may like {"usb_controllers":[{"type":"ehci", "count": 1}]} Note: Because libvirt/qemu will create a default pii3-uhi controller, so system will only create usb controllers except pii3-uhci.

(2)System supports creating vm with usb controller. Driver(ibvirt) layper need to support constructing usb controller xml configuration.

(3)System supports managing relationship between usb controllers and usb devices and storing them in instance_system_metadata table, so that <1>every usb device will have unique port, <2>vm can create usb controller and attach usb devices correctly the next starting. The usb controller schema may like: {          "usb_controller_requests": [            {                "type": "ehci", "count": 2, "usb_controllers": [                 {                    "index": 1, "ports": [                     {                         "port": 1, "usb_device_id": 5 }                   ]                  }                 ]             }           ]         }

3. System requirements and things should be considered for usb device(An optional Implementation): (1)System supports discovering usb devices and saving them to DB. The main process can refer to pci-passthrough implements: <1>Using white-list function and lsusb like commands to retrieve available usb devices of a host. <2>Adding a table usb_devices in DB to store usb device information. <3>Adding auto-update function to synchronize information between host(compute node) and DB.

(2)System supports creating a flavor with usb device. Adding a property for usb device to extra_specs field of flavor. The scheme may like {"usb_devices":[{"usb_device_id": 5}]} Note: It is necessary to exposure usb device id(maybe some other information also are necessary) information to admin/user because the most common scenarios are attaching specified usb device to vm. The mechanism of automatically selecting compute node to provide pci devices is not appropriate in here.

(3)System supports creating vm with usb devices. <1>Scheduler layer needs to scheduler request to the appropriate compute node, so a UsbDeviceFilter.py filter may be needed. <2>Driver(ibvirt) layper supports constructing usb device xml configuration. <3>Compute layer supports allocating usb controller for usb device. If not specifying usb controllers in request, compute layer not specify usb controller for usb device, then libvirt/qemu will attach usb device to the default pii3-uhci controller. If specifying usb controllers in request, system will first create usb controller which processes have been described above. It then choose available controller(type and port) for usb device. <4>DB layper supports storing usb devices. The instance_system_metadata table is a good choice. The usb device schema may like {                "usb_devices_requests": [                  {                     "usb_device_id": 5 }               ]              }

(4)System supports detaching usb devices when suspending a vm with usb devices, and re-attaching usb devices when resuming the vm.

(5)System supports detaching usb devices before making snapshot of a vm and re-attaching usb devices after making snapshot. System also support the protection of usb device when using snapshot to resume a vm.

(6)System supports reporting error when migrating vm with usb device. If a vm has usb devices attached, it is not allowed to migrate.