Bug 1375410

Summary: Hot-plugged USB redirect device cannot generate address
Product: Red Hat Enterprise Linux 7 Reporter: Pei Zhang <pzhang>
Component: libvirtAssignee: Ján Tomko <jtomko>
Status: CLOSED ERRATA QA Contact: yisun
Severity: medium Docs Contact:
Priority: medium    
Version: 7.3CC: dyuan, jtomko, rbalakri, xuzhang
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Unspecified   
Whiteboard:
Fixed In Version: libvirt-3.1.0-1.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-08-01 17:14:13 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Pei Zhang 2016-09-13 03:31:16 UTC
Description of problem:
Now libvirt support hotplug a redirect USB device to guest, but it 
cannot generated USB address correctly. 

Version-Release number of selected component (if applicable):
libvirt-2.0.0-8.el7.x86_64

How reproducible:
100%

Steps to Reproduce:
1. define and start a guest with USB controller

2. Prepare an USB hostdev, xml as following 
# cat hostdev1.xml 
<hostdev mode='subsystem' type='usb'>
  <source startupPolicy='optional'>
    <vendor id='0x1005'/>
    <product id='0xb113'/>
  </source>
</hostdev>

# virsh attach-device vm2 hostdev1.xml 
Device attached successfully

3. Prepare an USB redirect device, xml like following 
# cat redir-usb.xml 
<redirdev bus='usb' type='spicevmc'/>

# virsh attach-device vm2 redir-usb.xml 
Device attached successfully


# virsh dumpxml vm2 | grep usb 
  <hostdev mode='subsystem' type='usb' managed='no'>
      <source startupPolicy='optional'>
        <vendor id='0x1005'/>
        <product id='0xb113'/>
        <address bus='2' device='4'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='1'/>
    </hostdev>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
    </redirdev>
......

4. save and restore, it will fail to restore.
# virsh save vm2 vm2.save 

Domain vm2 saved to vm2.save

# virsh restore vm2.save 
error: Failed to restore domain from vm2.save
error: internal error: qemu unexpectedly closed the monitor: 2016-09-13T02:55:05.263225Z qemu-kvm: -device usb-host,hostbus=2,hostaddr=4,id=hostdev0,bus=usb.0,port=1: usb port 1 (bus usb.0) not found (in use?)

5. re-define and start a guest with usb controller 

6. attach redirect dev first. 

# virsh attach-device vm2 redir-usb.xml 
Device attached successfully

# virsh dumpxml vm2 
......
  <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
    </redirdev>
......

7. then try to hotplug hostdev, it will fail to attach 

# virsh attach-device vm2 hostdev1.xml 
error: Failed to attach device from hostdev1.xml
error: internal error: unable to execute QEMU command 'device_add': usb port 1 (bus usb.0) not found (in use?)

Actual results:
As above, hot-plugged USB redirect device cannot generated address. 

Expected results:
It can generate USB address. 

Additional info:

Comment 1 Ján Tomko 2017-01-27 16:44:50 UTC
Upstream patch:
https://www.redhat.com/archives/libvir-list/2017-January/msg01107.html

Comment 2 Ján Tomko 2017-01-30 15:59:28 UTC
Pushed upstream:
commit de325472cc27f57c56d0a4049fc3c58f59e8c8b4
Author:     Ján Tomko <jtomko>
CommitDate: 2017-01-30 16:17:35 +0100

    qemu: assign USB addresses on redirdev hotplug too
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1375410

git describe: v3.0.0-83-gde32547

Comment 4 yisun 2017-03-22 10:43:23 UTC
Verified on: 
libvirt-3.1.0-2.el7.x86_64

Scenario 1: attach hostdev and then redirdev
1. prepare a vm without usb devices
## virsh dumpxml vm | grep usb -2
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <controller type='usb' index='0' model='piix3-uhci'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>


2. prepare a usb device on host
## lsusb | grep SanDisk
Bus 001 Device 005: ID 0781:5567 SanDisk Corp. Cruzer Blade


3. prepare one hostdev and one redir usb device xmls
## cat hostdev.usb 
<hostdev mode='subsystem' type='usb'>
  <source startupPolicy='optional'>
    <vendor id='0x0781'/>
    <product id='0x5567'/>
  </source>
</hostdev>


## cat redir.usb 
<redirdev bus='usb' type='spicevmc'/>


4. attach the hostdev usb device and check the usb addr
## virsh attach-device vm hostdev.usb 
Device attached successfully


## virsh dumpxml vm | grep usb -A6
...
    <hostdev mode='subsystem' type='usb' managed='no'>
      <source startupPolicy='optional'>
        <vendor id='0x0781'/>
        <product id='0x5567'/>
        <address bus='1' device='5'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='1'/>  <==== usb addr assigned
    </hostdev>
...


5. attach the redir usb device and check the usb addr
## virsh attach-device vm redir.usb 
Device attached successfully
## virsh dumpxml vm | grep usb -A6

--
    <hostdev mode='subsystem' type='usb' managed='no'>
      <source startupPolicy='optional'>
        <vendor id='0x0781'/>
        <product id='0x5567'/>
        <address bus='1' device='5'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='1'/>
    </hostdev>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
      <address type='usb' bus='0' port='2'/> <===== redirdev also has usb addr assigned
    </redirdev>


## virsh save vm vm.save 
Domain vm saved to vm.save <=== no more error

Scenario 2: attach redirdev and then hostdev
======================================
1. clean the vm's hotplugged usb devices
## virsh destroy vm; virsh start vm
Domain vm destroyed

Domain vm started

2. attach the redirdev first and check the usb addr
## virsh attach-device vm redir.usb 
Device attached successfully

## virsh dumpxml vm | grep usb -A6
--
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
      <address type='usb' bus='0' port='1'/> <==== usb addr assigned
    </redirdev>


3. attach the hostdev and check the usb addr
## virsh attach-device vm hostdev.usb 
Device attached successfully


## virsh dumpxml vm | grep usb -A6
...
    <hostdev mode='subsystem' type='usb' managed='no'>
      <source startupPolicy='optional'>
        <vendor id='0x0781'/>
        <product id='0x5567'/>
        <address bus='1' device='5'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='2'/> <===== usb addr assigned without conflicts
    </hostdev>
    <redirdev bus='usb' type='spicevmc'>
      <alias name='redir0'/>
      <address type='usb' bus='0' port='1'/>
    </redirdev>

Comment 5 errata-xmlrpc 2017-08-01 17:14:13 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHEA-2017:1846

Comment 6 errata-xmlrpc 2017-08-01 23:55:08 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHEA-2017:1846

Comment 7 errata-xmlrpc 2017-08-02 01:27:35 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHEA-2017:1846