fix #3271: USB: allow usb hotplugging for modern guests

same as with the extended support for more usb devices, allow
hotplugging for guests that can use the qemu-xhci controller which
require a machine type >= 7.1 and a ostype l26 or windows > 7

if no usb device was passed through on startup, dynamically add
the xhci controller (and remove if the last usb device is unplugged)
so that live migration is still possible

much of the usb hotplug code was already there, but it still needed
a few adaptions, for example we have to add a chardev when adding
a spice redir port (that gets automatically removed when the
usb-redir device gets removed)

since the spice devices use the id 'usbredirdevX' instead of 'usbX', we
have to manually map that a bit around

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2022-11-10 15:35:58 +01:00 committed by Thomas Lamprecht
parent 0c3d18ef13
commit c60cad61a0

View File

@ -299,7 +299,9 @@ my $confdesc = {
type => 'string', format => 'pve-hotplug-features', type => 'string', format => 'pve-hotplug-features',
description => "Selectively enable hotplug features. This is a comma separated list of" description => "Selectively enable hotplug features. This is a comma separated list of"
." hotplug features: 'network', 'disk', 'cpu', 'memory', 'usb' and 'cloudinit'. Use '0' to disable" ." hotplug features: 'network', 'disk', 'cpu', 'memory', 'usb' and 'cloudinit'. Use '0' to disable"
." hotplug completely. Using '1' as value is an alias for the default `network,disk,usb`.", ." hotplug completely. Using '1' as value is an alias for the default `network,disk,usb`."
." USB hotplugging is possible for guests with machine version >= 7.1 and ostype l26 or"
." windows > 7.",
default => 'network,disk,usb', default => 'network,disk,usb',
}, },
reboot => { reboot => {
@ -4202,7 +4204,7 @@ sub vm_devices_list {
# qom-list path=/machine/peripheral # qom-list path=/machine/peripheral
my $resperipheral = mon_cmd($vmid, 'qom-list', path => '/machine/peripheral'); my $resperipheral = mon_cmd($vmid, 'qom-list', path => '/machine/peripheral');
foreach my $per (@$resperipheral) { foreach my $per (@$resperipheral) {
if ($per->{name} =~ m/^usb\d+$/) { if ($per->{name} =~ m/^usb(?:redirdev)?\d+$/) {
$devices->{$per->{name}} = 1; $devices->{$per->{name}} = 1;
} }
} }
@ -4225,11 +4227,12 @@ sub vm_deviceplug {
qemu_deviceadd($vmid, print_tabletdevice_full($conf, $arch)); qemu_deviceadd($vmid, print_tabletdevice_full($conf, $arch));
} elsif ($deviceid eq 'keyboard') { } elsif ($deviceid eq 'keyboard') {
qemu_deviceadd($vmid, print_keyboarddevice_full($conf, $arch)); qemu_deviceadd($vmid, print_keyboarddevice_full($conf, $arch));
} elsif ($deviceid =~ m/^usbredirdev(\d+)$/) {
my $id = $1;
qemu_spice_usbredir_chardev_add($vmid, "usbredirchardev$id");
qemu_deviceadd($vmid, PVE::QemuServer::USB::print_spice_usbdevice($id, "xhci", $id + 1));
} elsif ($deviceid =~ m/^usb(\d+)$/) { } elsif ($deviceid =~ m/^usb(\d+)$/) {
die "usb hotplug currently not reliable\n"; qemu_deviceadd($vmid, PVE::QemuServer::USB::print_usbdevice_full($conf, $deviceid, $device, {}, $1 + 1));
# since we can't reliably hot unplug all added usb devices and usb
# passthrough breaks live migration we disable usb hotplugging for now
#qemu_deviceadd($vmid, PVE::QemuServer::USB::print_usbdevice_full($conf, $deviceid, $device));
} elsif ($deviceid =~ m/^(virtio)(\d+)$/) { } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
qemu_iothread_add($vmid, $deviceid, $device); qemu_iothread_add($vmid, $deviceid, $device);
@ -4315,14 +4318,14 @@ sub vm_deviceunplug {
my $bootdisks = PVE::QemuServer::Drive::get_bootdisks($conf); my $bootdisks = PVE::QemuServer::Drive::get_bootdisks($conf);
die "can't unplug bootdisk '$deviceid'\n" if grep {$_ eq $deviceid} @$bootdisks; die "can't unplug bootdisk '$deviceid'\n" if grep {$_ eq $deviceid} @$bootdisks;
if ($deviceid eq 'tablet' || $deviceid eq 'keyboard') { if ($deviceid eq 'tablet' || $deviceid eq 'keyboard' || $deviceid eq 'xhci') {
qemu_devicedel($vmid, $deviceid); qemu_devicedel($vmid, $deviceid);
} elsif ($deviceid =~ m/^usbredirdev\d+$/) {
qemu_devicedel($vmid, $deviceid);
qemu_devicedelverify($vmid, $deviceid);
} elsif ($deviceid =~ m/^usb\d+$/) { } elsif ($deviceid =~ m/^usb\d+$/) {
die "usb hotplug currently not reliable\n"; qemu_devicedel($vmid, $deviceid);
# when unplugging usb devices this way, there may be remaining usb qemu_devicedelverify($vmid, $deviceid);
# controllers/hubs so we disable it for now
#qemu_devicedel($vmid, $deviceid);
#qemu_devicedelverify($vmid, $deviceid);
} elsif ($deviceid =~ m/^(virtio)(\d+)$/) { } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
my $device = parse_drive($deviceid, $conf->{$deviceid}); my $device = parse_drive($deviceid, $conf->{$deviceid});
@ -4354,6 +4357,20 @@ sub vm_deviceunplug {
return 1; return 1;
} }
sub qemu_spice_usbredir_chardev_add {
my ($vmid, $id) = @_;
mon_cmd($vmid, "chardev-add" , (
id => $id,
backend => {
type => 'spicevmc',
data => {
type => "usbredir",
},
},
));
}
sub qemu_deviceadd { sub qemu_deviceadd {
my ($vmid, $devicefull) = @_; my ($vmid, $devicefull) = @_;
@ -4568,15 +4585,14 @@ sub qemu_usb_hotplug {
vm_deviceunplug($vmid, $conf, $deviceid); vm_deviceunplug($vmid, $conf, $deviceid);
# check if xhci controller is necessary and available # check if xhci controller is necessary and available
if ($device->{usb3}) {
my $devicelist = vm_devices_list($vmid); my $devicelist = vm_devices_list($vmid);
if (!$devicelist->{xhci}) { if (!$devicelist->{xhci}) {
my $pciaddr = print_pci_addr("xhci", undef, $arch, $machine_type); my $pciaddr = print_pci_addr("xhci", undef, $arch, $machine_type);
qemu_deviceadd($vmid, "nec-usb-xhci,id=xhci$pciaddr"); qemu_deviceadd($vmid, PVE::QemuServer::USB::print_qemu_xhci_controller($pciaddr));
}
} }
# print_usbdevice_full expects the parsed device
my $d = parse_usb_device($device->{host}); my $d = parse_usb_device($device->{host});
$d->{usb3} = $device->{usb3}; $d->{usb3} = $device->{usb3};
@ -4885,7 +4901,12 @@ sub vmconfig_hotplug_pending {
PVE::QemuConfig->write_config($vmid, $conf); PVE::QemuConfig->write_config($vmid, $conf);
} }
my $ostype = $conf->{ostype};
my $version = extract_version($machine_type, get_running_qemu_version($vmid));
my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1'); my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
my $usb_hotplug = $hotplug_features->{usb}
&& min_version($version, 7, 1)
&& defined($ostype) && ($ostype eq 'l26' || windows_version($ostype) > 7);
my $cgroup = PVE::QemuServer::CGroup->new($vmid); my $cgroup = PVE::QemuServer::CGroup->new($vmid);
my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete}); my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
@ -4905,11 +4926,11 @@ sub vmconfig_hotplug_pending {
vm_deviceunplug($vmid, $conf, 'tablet'); vm_deviceunplug($vmid, $conf, 'tablet');
vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64'; vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64';
} }
} elsif ($opt =~ m/^usb\d+/) { } elsif ($opt =~ m/^usb(\d+)$/) {
die "skip\n"; my $index = $1;
# since we cannot reliably hot unplug usb devices we are disabling it die "skip\n" if !$usb_hotplug;
#die "skip\n" if !$hotplug_features->{usb} || $conf->{$opt} =~ m/spice/i; vm_deviceunplug($vmid, $conf, "usbredirdev$index"); # if it's a spice port
#vm_deviceunplug($vmid, $conf, $opt); vm_deviceunplug($vmid, $conf, $opt);
} elsif ($opt eq 'vcpus') { } elsif ($opt eq 'vcpus') {
die "skip\n" if !$hotplug_features->{cpu}; die "skip\n" if !$hotplug_features->{cpu};
qemu_cpu_hotplug($vmid, $conf, undef); qemu_cpu_hotplug($vmid, $conf, undef);
@ -4963,13 +4984,15 @@ sub vmconfig_hotplug_pending {
vm_deviceunplug($vmid, $conf, 'tablet'); vm_deviceunplug($vmid, $conf, 'tablet');
vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64'; vm_deviceunplug($vmid, $conf, 'keyboard') if $arch eq 'aarch64';
} }
} elsif ($opt =~ m/^usb\d+$/) { } elsif ($opt =~ m/^usb(\d+)$/) {
die "skip\n"; my $index = $1;
# since we cannot reliably hot unplug usb devices we disable it for now die "skip\n" if !$usb_hotplug;
#die "skip\n" if !$hotplug_features->{usb} || $value =~ m/spice/i; my $d = eval { parse_property_string($usbdesc->{format}, $value) };
#my $d = eval { parse_property_string($usbdesc->{format}, $value) }; my $id = $opt;
#die "skip\n" if !$d; if ($d->{host} eq 'spice') {
#qemu_usb_hotplug($storecfg, $conf, $vmid, $opt, $d, $arch, $machine_type); $id = "usbredirdev$index";
}
qemu_usb_hotplug($storecfg, $conf, $vmid, $id, $d, $arch, $machine_type);
} elsif ($opt eq 'vcpus') { } elsif ($opt eq 'vcpus') {
die "skip\n" if !$hotplug_features->{cpu}; die "skip\n" if !$hotplug_features->{cpu};
qemu_cpu_hotplug($vmid, $conf, $value); qemu_cpu_hotplug($vmid, $conf, $value);
@ -5019,6 +5042,20 @@ sub vmconfig_hotplug_pending {
delete $conf->{pending}->{$opt}; delete $conf->{pending}->{$opt};
} }
} }
# unplug xhci controller if no usb device is left
if ($usb_hotplug) {
my $has_usb = 0;
for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
next if !defined($conf->{"usb$i"});
$has_usb = 1;
last;
}
if (!$has_usb) {
vm_deviceunplug($vmid, $conf, 'xhci');
}
}
PVE::QemuConfig->write_config($vmid, $conf); PVE::QemuConfig->write_config($vmid, $conf);
if($hotplug_features->{cloudinit}) { if($hotplug_features->{cloudinit}) {