Nova/proposal about usb passthrough

Background
Currently, nova has supported feature of pci-passthrough, but hasn't supported usb-passthrough. There were a bp about it in https://blueprints.launchpad.net/nova/+spec/host-usb-passthrough but don't have progress by now.

As I know, some telecom and enterprise customers have requirement of usb-passthrough especially in private cloud. So I think maybe it's a good choice to provide this feature in openstack.

Use case
Our customers have the following requirement:

1. There are some ERP softwares deployed in virtual machines that need usb-key for authentication. Customers will first insert usb-key to host, then use the cloud management software to attach usb-key to vm, the last guest os will discovery usb-key and we can use it.

2. Popular usb device are supporting usb 2.0 or usb 3.0 standard, so our cloud management software should guarantee the transfer speed of usb device.

Test case
I have tested some cases of creating vm with different type and amount of usb controllers and usb devices. I got the following result: 1. Usb controllers have a maximum port restriction. Uhci controller supports a maxmum of 2 ports while ehci controller supports 6 ports. 2. If we create vm without usb controller, qemu will create a piix3-usb-uhci controller by default but not create the other type's. 3. If we create 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. Some articles said that we can use product+vendor or bus+device properties to indentify a usb device, but we found that if we reboot host or plug/unplug usb device to/from host the device number may change, so use bus+port to identify a usb device maybe a good choice.

Consideration about usb controller
I have three alternatives to implement this feature:

Option 1:

Nova supports function and API for creating usb controller with specified type and number. When admin/user want to attach usb devices to virtual machine, they should specify the type of usb controllers, then nova will select matched usb controllers/ports which are created by admin/user for these device.If usb controllers/ports are not enough, admin/user should create new ones.

Advantage: (1)Admin/user can choice what they want. (2)System is flexible for future extention.

Shortcoming: (1)Admin/user's operations are complex. (2)The implementation of this function may be complicated. System should make decision about which usb device to which usb controller and save the relationship.

Option 2:

Nova dosen't expose operation of usb controller to admin/user. When admin/user want to attach usb devices to virtual machine, they can specify the type of usb controllers optionaly, then nova will automatically create appropriate usb controllers/ports or choose exist ones for these devices.

Advantage: (1)Admin/user's operations are simple.

Shortcoming: (1)The implementation of this function may be complicated. System should make decision about when to create usb controller, which usb device to which usb controller and save the relationship.

Option 3:

Nova creates some default usb controller(like just create a ehci usb controller) when creating virtual machine, then the virtual machine will have a default uhci controller and a ehci controller. When admin/user want to attach usb devices to virtual machine, they can specify the type of usb controller optionaly(null or ehci), then nova will attach usb device to uhci or ehci controller.

Advantage: (1)Admin/user's operations are simple. (2)The implementation of this function is simple. (3)In real business scenes a vm commonly dosen't need very much usb devices. Uhci usb controller can support many usb devices by using cascaded hubs, ehci usb controller can support maxmum of 6 usb device, may be they are enough for use.

Shortcoming: (1)Only support some types of usb controller. (2)The number of supported usb devices are restricted.

Consideration about usb-passthrough and hot-plug
The core ideas are referring to the implementation of pci-passthrough. But there is an important difference that for pci device nova can automatically select a compute node to provide pci devices to vm but for usb device user should specify which use device to which vm.

Things should be considered for usb device:
1. Nova should support auto-discovering usb devices and saving them to DB. 2. Nova should support creating a flavor with usb device. 3. Nova should support creating vm with flavor containing usb devices. 4. Nova should support automatically attaching or detaching usb devices in some use cases like starting virtual machine, suspend/resume virtual machine, make snapshot of virtual machine, and so on. 5. Nova should hot-plug/cold-plug usb devices to an exist virtual machine.

Related API:
Nova should support the following interfaces. 1. We should add usb device API so that nova can support querying usb device information. List usb devices on the nodes request: GET v2/​{tenant_id}/​os-usb-device The response JSON may like below : {"usb_devices": [{"id": 1, "compute_node_id": 1, "vendor_id": "8086", "product_id": "1520", "address":"0000:01:00.0", "status":"available"}]} Request: GET v2/​{tenant_id}/​os-usb-device/detail The response JSON may like below : {"usb_devices": [{"id": 1, "compute_node_id": 1, "vendor_id": "8086", "product_id": "1520", "address":"0000:01:00.0", "dev_id": "usb_0000_04_10_0", "dev_type":"type-USB", "status":"available", "instance_uuid":"", "extra_info":"", "created_at": null, "deleted": false,  "deleted_at": null,}]} Show usb device infomation Request: GET v2/​{tenant_id}/​os-usb-device/{usb_device_id} The response JSON may like below : {"usb_device": {"id": 1, "compute_node_id": 1, "vendor_id": "8086", "product_id": "1520", "address":"0000:01:00.0", "dev_id": "usb_0000_04_10_0", "dev_type":"type-USB", "status":"available", "instance_uuid":"", "extra_info":"", "created_at": null, "deleted": false,  "deleted_at": null,}}

2. We should extend hypervisor API to add usb device stats information so that nova can support querying usb device stats of a hypervisor. List and show usb device stats on hypervisors request: GET v2/​{tenant_id}​/os-hypervisors/​{hypervisor_hostname} The response JSON contains the variable "usb_device_stats": {"hypervisor": {"usb_stats": [{"id": 1, "vendor_id": "8086", "product_id": "1520", "address":"0000:01:00.0", "status":"available", "instance_uuid":""}]}}

3.We should extend server API to add usb assignment information so that nova can support querying usb device stats of a server. List and show usb devices of a virtual machine: GET v2/​{tenant_id}​/servers/​{server_id} The response JSON will contain the variable "os-usb-device:usb_devices": { "server": {"os-usb-device:usb_devices": [{"id": 1}], "tenant_id": "openstack", "user_id": "admin"}}

4.We should extend server API to add usb device hot-plug and cold-plug function. Attach usb device: Post v2/​{tenant_id}​/servers/​{server_id}/action {      "attach_usb_devices": {"usb_devices_requests":[{"id": 1}]} } Detach usb device: Post v2/​{tenant_id}​/servers/​{server_id}/action {      "detach_usb_devices": {"usb_devices_requests":[{"id": 1}]} }