From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A1BD9A1BAF for ; Fri, 16 Jun 2023 09:50:38 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 869B62F2CE for ; Fri, 16 Jun 2023 09:50:08 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 16 Jun 2023 09:50:06 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1F2E745A75 for ; Fri, 16 Jun 2023 09:50:06 +0200 (CEST) Date: Fri, 16 Jun 2023 09:49:58 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox VE development discussion References: <20230614084622.1446211-1-d.csapak@proxmox.com> <20230614084622.1446211-3-d.csapak@proxmox.com> In-Reply-To: <20230614084622.1446211-3-d.csapak@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.16.0 (https://github.com/astroidmail/astroid) Message-Id: <1686901207.x1ncj3w9te.astroid@yuna.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.072 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: Re: [pve-devel] [PATCH qemu-server v6 2/6] enable cluster mapped PCI devices for guests X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jun 2023 07:50:38 -0000 On June 14, 2023 10:46 am, Dominik Csapak wrote: > this patch allows configuring pci devices that are mapped via cluster > resource mapping when the user has 'Resource.Use' on the ACL path > '/mapping/pci/{ID}' (in addition to the usual required vm config > privileges) >=20 > When given multiple mappings in the config, we use them as alternatives > for the passthrough, and will select the first free one on startup. > It is using our regular pci reservation mechanism for regular devices and > we introduce a selection mechanism for mediated devices. >=20 > A few changes to the inner workings were required to make this work well: > * parse_hostpci now returns a different structure where we have a list > of lists (first level is for the different alternatives and second > level is for the different devices that should be passed through > together) > * factor out the 'parse_hostpci_devices' which parses each device from > the config and does some precondition checks > * reserve_pci_usage now behaves slightly different when trying to > reserve an device with the same VMID that's already reserved for, > since for checking which alternative we can use, we already must > reserve one (this means that qm showcmd can actually reserve devices, > albeit only for up to 10 seconds) > * configuring a mediated device on a multifunction device is not > supported anymore, and results in failure to start (previously, it > just chose the first device to do it). This is a breaking change > * configuring a single pci device twice on different hostpci slots now > fails during commandline generation instead on qemu start, so we had > to adapt one test where this occurred (it could never have worked > anyway) >=20 > Add the permission checks also to clone/restore. This fails now for non > root users for raw devices, this is a breaking change! >=20 > Fixes #3574: Improve SR-IOV usability > Signed-off-by: Dominik Csapak > --- > changes from v5: > * move permission check for clone/restore to QemuServer.pm > PVE/API2/Qemu.pm | 54 +++++- > PVE/QemuServer.pm | 71 ++++--- > PVE/QemuServer/PCI.pm | 243 ++++++++++++++++++++---- > test/cfg2cmd/q35-linux-hostpci.conf | 2 +- > test/cfg2cmd/q35-linux-hostpci.conf.cmd | 2 +- > test/run_config2command_tests.pl | 1 + > 6 files changed, 299 insertions(+), 74 deletions(-) >=20 > diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm > index 865edf7f..4c3c53d9 100644 > --- a/PVE/API2/Qemu.pm > +++ b/PVE/API2/Qemu.pm > @@ -32,6 +32,7 @@ use PVE::QemuServer::Drive; > use PVE::QemuServer::ImportDisk; > use PVE::QemuServer::Monitor qw(mon_cmd); > use PVE::QemuServer::Machine; > +use PVE::QemuServer::PCI; > use PVE::QemuServer::USB qw(parse_usb_device); > use PVE::QemuMigrate; > use PVE::RPCEnvironment; > @@ -607,6 +608,26 @@ my $check_vm_create_usb_perm =3D sub { > return 1; > }; > =20 > +my $check_vm_create_hostpci_perm =3D sub { > + my ($rpcenv, $authuser, $vmid, $pool, $param) =3D @_; > + > + return 1 if $authuser eq 'root@pam'; > + > + foreach my $opt (keys %{$param}) { > + next if $opt !~ m/^hostpci\d+$/; > + > + my $device =3D PVE::JSONSchema::parse_property_string('pve-qm-hostpci',= $param->{$opt}); > + if ($device->{host} !~ m/:/) { > + $rpcenv->check_full($authuser, "/mapping/pci/$device->{host}", ['Ma= pping.Use']); > + $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType'= ]); > + } else { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } > + } > + > + return 1; > +}; > + > my $check_vm_modify_config_perm =3D sub { > my ($rpcenv, $authuser, $vmid, $pool, $key_list) =3D @_; > =20 > @@ -617,7 +638,7 @@ my $check_vm_modify_config_perm =3D sub { > # else, as there the permission can be value dependend > next if PVE::QemuServer::is_valid_drivename($opt); > next if $opt eq 'cdrom'; > - next if $opt =3D~ m/^(?:unused|serial|usb)\d+$/; > + next if $opt =3D~ m/^(?:unused|serial|usb|hostpci)\d+$/; > next if $opt eq 'tags'; > =20 > =20 > @@ -646,7 +667,7 @@ my $check_vm_modify_config_perm =3D sub { > # also needs privileges on the storage, that will be checked later > $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', = 'VM.PowerMgmt' ]); > } else { > - # catches hostpci\d+, args, lock, etc. > + # catches args, lock, etc. > # new options will be checked here > die "only root can set '$opt' config\n"; > } > @@ -885,6 +906,8 @@ __PACKAGE__->register_method({ > =20 > &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $pa= ram); > &$check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param= ); > + &$check_vm_create_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $p= aram); > + > PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param); > &$check_cpu_model_access($rpcenv, $authuser, $param); > =20 > @@ -1737,6 +1760,16 @@ my $update_vm_api =3D sub { > } > PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); > PVE::QemuConfig->write_config($vmid, $conf); > + } elsif ($opt =3D~ m/^hostpci\d+$/) { > + my $olddevice =3D PVE::JSONSchema::parse_property_string('pve-qm-h= ostpci', $val); > + if ($olddevice->{host} !~ m/:/) { > + $rpcenv->check_full($authuser, "/mapping/pci/$olddevice->{host}", ['M= apping.Use']); > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; > + } elsif ($authuser ne 'root@pam') { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } this part > + PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); > + PVE::QemuConfig->write_config($vmid, $conf); > } elsif ($opt eq 'tags') { > assert_tag_permissions($vmid, $val, '', $rpcenv, $authuser); > delete $conf->{$opt}; > @@ -1823,6 +1856,23 @@ my $update_vm_api =3D sub { > } elsif ($authuser ne 'root@pam') { > die "only root can modify '$opt' config for real devices\n"; > } > + > + $conf->{pending}->{$opt} =3D $param->{$opt}; > + } elsif ($opt =3D~ m/^hostpci\d+$/) { > + my $olddevice; > + if (defined($conf->{$opt})) { > + $olddevice =3D PVE::JSONSchema::parse_property_string('pve-qm-hostpci= ', $conf->{$opt}); > + } > + my $newdevice =3D PVE::JSONSchema::parse_property_string('pve-qm-h= ostpci', $param->{$opt}); > + if ((!defined($olddevice) || $olddevice->{host} !~ m/:/) && $newde= vice->{host} !~ m/:/) { > + if (defined($olddevice)) { > + $rpcenv->check_full($authuser, "/mapping/pci/$olddevice->{host}",= ['Mapping.Use']); > + } > + $rpcenv->check_full($authuser, "/mapping/pci/$newdevice->{host}", ['M= apping.Use']); > + $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType'])= ; and this part could be the same, with the latter being two calls to the same helper, which would be easier to read.. it checks: VM.Config.HWType access to the old value if there is one (root if raw, mapping if mapping) access to the new value (root if raw, mapping if mapping) doing 0. (optional defined guard for existing value) 1. parse old 2. call check helper 3. parse new 4. call check helper with the helper doing if raw require root else check mapping && VM.Config.HWType is way more readable and should have the same result, and only requires one place to change should we change the format/checks in the future. keep in mind that ACL results are cached, so doing the exact same check twice in a row means the second one is almost free. > + } elsif ($authuser ne 'root@pam') { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } > $conf->{pending}->{$opt} =3D $param->{$opt}; > } elsif ($opt eq 'tags') { > assert_tag_permissions($vmid, $conf->{$opt}, $param->{$opt}, $rpce= nv, $authuser); > diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm > index ab5458c3..4fb8d1c2 100644 > --- a/PVE/QemuServer.pm > +++ b/PVE/QemuServer.pm > @@ -3783,8 +3783,8 @@ sub config_to_command { > my $bootorder =3D device_bootorder($conf); > =20 > # host pci device passthrough > - my ($kvm_off, $gpu_passthrough, $legacy_igd) =3D PVE::QemuServer::PC= I::print_hostpci_devices( > - $vmid, $conf, $devices, $vga, $winversion, $q35, $bridges, $arch, $mach= ine_type, $bootorder); > + my ($kvm_off, $gpu_passthrough, $legacy_igd, $pci_devices) =3D PVE::= QemuServer::PCI::print_hostpci_devices( > + $vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $machine_ty= pe, $bootorder); > =20 > # usb devices > my $usb_dev_features =3D {}; > @@ -4203,7 +4203,7 @@ sub config_to_command { > push @$cmd, @$aa; > } > =20 > - return wantarray ? ($cmd, $vollist, $spice_port) : $cmd; > + return wantarray ? ($cmd, $vollist, $spice_port, $pci_devices) : $cm= d; > } > =20 > sub check_rng_source { > @@ -5762,7 +5762,7 @@ sub vm_start_nolock { > print "Resuming suspended VM\n"; > } > =20 > - my ($cmd, $vollist, $spice_port) =3D config_to_command($storecfg, $v= mid, > + my ($cmd, $vollist, $spice_port, $pci_devices) =3D config_to_command= ($storecfg, $vmid, > $conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'}); > =20 > my $migration_ip; > @@ -5847,38 +5847,44 @@ sub vm_start_nolock { > =20 > my $start_timeout =3D $params->{timeout} // config_aware_timeout($co= nf, $resume); > =20 > - my $pci_devices =3D {}; # host pci devices > - for (my $i =3D 0; $i < $PVE::QemuServer::PCI::MAX_HOSTPCI_DEVICES; $= i++) { > - my $dev =3D $conf->{"hostpci$i"} or next; > - $pci_devices->{$i} =3D parse_hostpci($dev); > + my $pci_reserve_list =3D []; > + for my $device (values $pci_devices->%*) { > + next if $device->{mdev}; # we don't reserve for mdev devices > + push $pci_reserve_list->@*, map { $_->{id} } $device->{ids}->@*; > } > =20 > - # do not reserve pciid for mediated devices, sysfs will error out fo= r duplicate assignment > - my $real_pci_devices =3D [ grep { !(defined($_->{mdev}) && scalar($_= ->{pciid}->@*) =3D=3D 1) } values $pci_devices->%* ]; > - > - # map to a flat list of pci ids > - my $pci_id_list =3D [ map { $_->{id} } map { $_->{pciid}->@* } $real= _pci_devices->@* ]; > - > # reserve all PCI IDs before actually doing anything with them > - PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, $start_= timeout); > + PVE::QemuServer::PCI::reserve_pci_usage($pci_reserve_list, $vmid, $s= tart_timeout); > =20 > eval { > my $uuid; > for my $id (sort keys %$pci_devices) { > my $d =3D $pci_devices->{$id}; > - for my $dev ($d->{pciid}->@*) { > - my $info =3D PVE::QemuServer::PCI::prepare_pci_device($vmid, $dev->{id= }, $id, $d->{mdev}); > - > - # nvidia grid needs the qemu parameter '-uuid' set > - # use smbios uuid or mdev uuid as fallback for that > - if ($d->{mdev} && !defined($uuid) && $info->{vendor} eq '10de') { > - if (defined($conf->{smbios1})) { > - my $smbios_conf =3D parse_smbios1($conf->{smbios1}); > - $uuid =3D $smbios_conf->{uuid} if defined($smbios_conf->{uuid}); > - } > - $uuid =3D PVE::QemuServer::PCI::generate_mdev_uuid($vmid, $id) if = !defined($uuid); > + my ($index) =3D ($id =3D~ m/^hostpci(\d+)$/); > + > + my $chosen_mdev; > + for my $dev ($d->{ids}->@*) { > + my $info =3D eval { PVE::QemuServer::PCI::prepare_pci_device($vmid, $d= ev->{id}, $index, $d->{mdev}) }; > + if ($d->{mdev}) { > + warn $@ if $@; > + $chosen_mdev =3D $info; > + last if $chosen_mdev; # if successful, we're done > + } else { > + die $@ if $@; > } > } > + > + next if !$d->{mdev}; > + die "could not create mediated device\n" if !defined($chosen_mdev); > + > + # nvidia grid needs the uuid of the mdev as qemu parameter > + if (!defined($uuid) && $chosen_mdev->{vendor} =3D~ m/^(0x)?10de$/) = { > + if (defined($conf->{smbios1})) { > + my $smbios_conf =3D parse_smbios1($conf->{smbios1}); > + $uuid =3D $smbios_conf->{uuid} if defined($smbios_conf->{uuid}); > + } > + $uuid =3D PVE::QemuServer::PCI::generate_mdev_uuid($vmid, $index) if != defined($uuid); > + } > } > push @$cmd, '-uuid', $uuid if defined($uuid); > }; > @@ -5992,7 +5998,7 @@ sub vm_start_nolock { > =20 > # re-reserve all PCI IDs now that we can know the actual VM PID > my $pid =3D PVE::QemuServer::Helpers::vm_running_locally($vmid); > - eval { PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, = undef, $pid) }; > + eval { PVE::QemuServer::PCI::reserve_pci_usage($pci_reserve_list, $v= mid, undef, $pid) }; > warn $@ if $@; > =20 > if (defined($res->{migrate})) { > @@ -6187,9 +6193,7 @@ sub cleanup_pci_devices { > =20 > # some nvidia vgpu driver versions want to clean the mdevs up thems= elves, and error > # out when we do it first. so wait for 10 seconds and then try it > - my $pciid =3D $d->{pciid}->[0]->{id}; > - my $info =3D PVE::SysFSTools::pci_device_info("$pciid"); > - if ($info->{vendor} eq '10de') { > + if ($d->{ids}->[0]->[0]->{vendor} =3D~ m/^(0x)?10de$/) { > sleep 10; > } > =20 > @@ -6548,6 +6552,13 @@ sub check_mapping_access { > } elsif (!$device->{spice}) { > die "only root can set '$opt' config for real devices\n"; > } > + } elsif ($opt =3D~ m/^hostpci\d+$/) { > + my $device =3D PVE::JSONSchema::parse_property_string('pve-qm-hostpc= i', $conf->{$opt}); > + if ($device->{host} !~ m/:/) { > + $rpcenv->check_full($user, "/mapping/pci/$device->{host}", ['Map= ping.Use']); > + } else { > + die "only root can set '$opt' config for non-mapped devices\n"; > + } > } > } > }; > diff --git a/PVE/QemuServer/PCI.pm b/PVE/QemuServer/PCI.pm > index a18b9747..5a5cbcca 100644 > --- a/PVE/QemuServer/PCI.pm > +++ b/PVE/QemuServer/PCI.pm > @@ -4,6 +4,7 @@ use warnings; > use strict; > =20 > use PVE::JSONSchema; > +use PVE::Mapping::PCI; > use PVE::SysFSTools; > use PVE::Tools; > =20 > @@ -23,8 +24,8 @@ my $hostpci_fmt =3D { > host =3D> { > default_key =3D> 1, > type =3D> 'string', > - pattern =3D> qr/$PCIRE(;$PCIRE)*/, > - format_description =3D> 'HOSTPCIID[;HOSTPCIID2...]', > + pattern =3D> qr/(:?$PCIRE(;$PCIRE)*)|(:?$PVE::JSONSchema::CONFIGID_RE)/= , > + format_description =3D> 'HOSTPCIID[;HOSTPCIID2...] or configured mappin= g id', > description =3D> < Host PCI device pass through. The PCI ID of a host's PCI device or a lis= t > of PCI virtual functions of the host. HOSTPCIID syntax is: > @@ -32,6 +33,8 @@ of PCI virtual functions of the host. HOSTPCIID syntax = is: > 'bus:dev.func' (hexadecimal numbers) > =20 > You can us the 'lspci' command to list existing PCI devices. > + > +Alternatively use the ID of a mapped pci device. > EODESCR > }, > rombar =3D> { > @@ -376,6 +379,32 @@ sub print_pcie_root_port { > return $res; > } > =20 > +# returns the parsed pci config but parses the 'host' part into > +# a list if lists into the 'id' property like this: > +# > +# { > +# mdev =3D> 1, > +# rombar =3D> ... > +# ... > +# ids =3D> [ > +# # this contains a list of alternative devices, > +# [ > +# # which are itself lists of ids for one multifunction device > +# { > +# id =3D> "0000:00:00.0", > +# vendor =3D> "...", > +# }, > +# { > +# id =3D> "0000:00:00.1", > +# vendor =3D> "...", > +# }, > +# ], > +# [ > +# ... > +# ], > +# ... > +# ], > +# } > sub parse_hostpci { > my ($value) =3D @_; > =20 > @@ -383,31 +412,167 @@ sub parse_hostpci { > =20 > my $res =3D PVE::JSONSchema::parse_property_string($hostpci_fmt, $va= lue); > =20 > - my @idlist =3D split(/;/, $res->{host}); > + my $alternatives =3D []; > + if ($res->{host} !~ m/:/) { > + # we have no ordinary pci id, must be a mapping > + my $devices =3D PVE::Mapping::PCI::find_on_current_node($res->{host}); > + die "PCI device mapping not found for '$res->{host}'\n" if !$devices ||= !scalar($devices->@*); > + > + for my $device ($devices->@*) { > + eval { PVE::Mapping::PCI::assert_valid($res->{host}, $device) }; > + die "PCI device mapping invalid (hardware probably changed): $@\n" = if $@; > + push $alternatives->@*, [split(/;/, $device->{path})]; > + } > + } else { > + push $alternatives->@*, [split(/;/, $res->{host})]; > + } > delete $res->{host}; > - foreach my $id (@idlist) { > - my $devs =3D PVE::SysFSTools::lspci($id); > - die "no PCI device found for '$id'\n" if !scalar(@$devs); > - push @{$res->{pciid}}, @$devs; > + > + $res->{ids} =3D []; > + for my $alternative ($alternatives->@*) { > + my $ids =3D []; > + foreach my $id ($alternative->@*) { > + my $devs =3D PVE::SysFSTools::lspci($id); > + die "no PCI device found for '$id'\n" if !scalar($devs->@*); > + push $ids->@*, @$devs; > + } > + if (scalar($ids->@*) > 1) { > + $res->{'has-multifunction'} =3D 1; > + die "cannot use mediated device with multifunction device\n" if $re= s->{mdev}; > + } > + push $res->{ids}->@*, $ids; > } > + > return $res; > } > =20 > +# parses all hostpci devices from a config and does some sanity checks > +# returns a hash like this: > +# { > +# hostpci0 =3D> { > +# # hash from parse_hostpci function > +# }, > +# hostpci1 =3D> { ... }, > +# ... > +# } > +sub parse_hostpci_devices { > + my ($conf) =3D @_; > + > + my $q35 =3D PVE::QemuServer::Machine::machine_type_is_q35($conf); > + my $legacy_igd =3D 0; > + > + my $parsed_devices =3D {}; > + for (my $i =3D 0; $i < $MAX_HOSTPCI_DEVICES; $i++) { > + my $id =3D "hostpci$i"; > + my $d =3D parse_hostpci($conf->{$id}); > + next if !$d; > + > + # check syntax > + die "q35 machine model is not enabled" if !$q35 && $d->{pcie}; > + > + if ($d->{'legacy-igd'}) { > + die "only one device can be assigned in legacy-igd mode\n" > + if $legacy_igd; > + $legacy_igd =3D 1; > + > + die "legacy IGD assignment requires VGA mode to be 'none'\n" > + if !defined($conf->{'vga'}) || $conf->{'vga'} ne 'none'; > + die "legacy IGD assignment requires rombar to be enabled\n" > + if defined($d->{rombar}) && !$d->{rombar}; > + die "legacy IGD assignment is not compatible with x-vga\n" > + if $d->{'x-vga'}; > + die "legacy IGD assignment is not compatible with mdev\n" > + if $d->{mdev}; > + die "legacy IGD assignment is not compatible with q35\n" > + if $q35; > + die "legacy IGD assignment is not compatible with multifunction dev= ices\n" > + if $d->{'has-multifunction'}; > + die "legacy IGD assignment is not compatible with alternate devices= \n" > + if scalar($d->{ids}->@*) > 1; > + # check first device for valid id > + die "legacy IGD assignment only works for devices on host bus 00:02= .0\n" > + if $d->{ids}->[0]->[0]->{id} !~ m/02\.0$/; > + } > + > + $parsed_devices->{$id} =3D $d; > + } > + > + return $parsed_devices; > +} > + > +# takes the hash returned by parse_hostpci_devices and for all non mdev = gpus, > +# selects one of the given alternatives by trying to reserve it > +# > +# mdev devices must be chosen later when we actually allocate it, but we > +# flatten the inner list since there can only be one device per alternat= ive anyway > +my sub choose_hostpci_devices { > + my ($devices, $vmid) =3D @_; > + > + my $used =3D {}; > + > + my $add_used_device =3D sub { > + my ($devices) =3D @_; > + for my $used_device ($devices->@*) { > + my $used_id =3D $used_device->{id}; > + die "device '$used_id' assigned more than once\n" if $used->{$used_= id}; > + $used->{$used_id} =3D 1; > + } > + }; > + > + for (my $i =3D 0; $i < $MAX_HOSTPCI_DEVICES; $i++) { > + my $device =3D $devices->{"hostpci$i"}; > + next if !$device; > + > + if ($device->{mdev}) { > + $device->{ids} =3D [ map { $_->[0] } $device->{ids}->@* ]; > + next; > + } > + > + if (scalar($device->{ids}->@* =3D=3D 1)) { > + # we only have one alternative, use that > + $device->{ids} =3D $device->{ids}->[0]; > + $add_used_device->($device->{ids}); > + next; > + } > + > + my $found =3D 0; > + for my $alternative ($device->{ids}->@*) { > + my $ids =3D [map { $_->{id} } @$alternative]; > + > + next if grep { defined($used->{$_}) } @$ids; # already used > + eval { reserve_pci_usage($ids, $vmid, 10, undef) }; > + next if $@; > + > + # found one that is not used or reserved > + $add_used_device->($alternative); > + $device->{ids} =3D $alternative; > + $found =3D 1; > + last; > + } > + die "could not find a free device for 'hostpci$i'\n" if !$found; > + } > + > + return $devices; > +} > + > sub print_hostpci_devices { > - my ($vmid, $conf, $devices, $vga, $winversion, $q35, $bridges, $arch= , $machine_type, $bootorder) =3D @_; > + my ($vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $mac= hine_type, $bootorder) =3D @_; > =20 > my $kvm_off =3D 0; > my $gpu_passthrough =3D 0; > my $legacy_igd =3D 0; > =20 > my $pciaddr; > + my $pci_devices =3D choose_hostpci_devices(parse_hostpci_devices($co= nf), $vmid); > + > for (my $i =3D 0; $i < $MAX_HOSTPCI_DEVICES; $i++) { > my $id =3D "hostpci$i"; > - my $d =3D parse_hostpci($conf->{$id}); > + my $d =3D $pci_devices->{$id}; > next if !$d; > =20 > + $legacy_igd =3D 1 if $d->{'legacy-igd'}; > + > if (my $pcie =3D $d->{pcie}) { > - die "q35 machine model is not enabled" if !$q35; > # win7 wants to have the pcie devices directly on the pcie bus > # instead of in the root port > if ($winversion =3D=3D 7) { > @@ -425,29 +590,8 @@ sub print_hostpci_devices { > $pciaddr =3D print_pci_addr($pci_name, $bridges, $arch, $machine_ty= pe); > } > =20 > - my $pcidevices =3D $d->{pciid}; > - my $multifunction =3D @$pcidevices > 1; > - > - if ($d->{'legacy-igd'}) { > - die "only one device can be assigned in legacy-igd mode\n" > - if $legacy_igd; > - $legacy_igd =3D 1; > - > - die "legacy IGD assignment requires VGA mode to be 'none'\n" > - if !defined($conf->{'vga'}) || $conf->{'vga'} ne 'none'; > - die "legacy IGD assignment requires rombar to be enabled\n" > - if defined($d->{rombar}) && !$d->{rombar}; > - die "legacy IGD assignment is not compatible with x-vga\n" > - if $d->{'x-vga'}; > - die "legacy IGD assignment is not compatible with mdev\n" > - if $d->{mdev}; > - die "legacy IGD assignment is not compatible with q35\n" > - if $q35; > - die "legacy IGD assignment is not compatible with multifunction dev= ices\n" > - if $multifunction; > - die "legacy IGD assignment only works for devices on host bus 00:02= .0\n" > - if $pcidevices->[0]->{id} !~ m/02\.0$/; > - } > + my $num_devices =3D scalar($d->{ids}->@*); > + my $multifunction =3D $num_devices > 1 && !$d->{mdev}; > =20 > my $xvga =3D ''; > if ($d->{'x-vga'}) { > @@ -458,15 +602,13 @@ sub print_hostpci_devices { > } > =20 > my $sysfspath; > - if ($d->{mdev} && scalar(@$pcidevices) =3D=3D 1) { > + if ($d->{mdev}) { > my $uuid =3D generate_mdev_uuid($vmid, $i); > $sysfspath =3D "/sys/bus/mdev/devices/$uuid"; > - } elsif ($d->{mdev}) { > - warn "ignoring mediated device '$id' with multifunction device\n"; > } > =20 > - my $j =3D 0; > - foreach my $pcidevice (@$pcidevices) { > + for (my $j =3D 0; $j < $num_devices; $j++) { > + my $pcidevice =3D $d->{ids}->[$j]; > my $devicestr =3D "vfio-pci"; > =20 > if ($sysfspath) { > @@ -489,12 +631,13 @@ sub print_hostpci_devices { > } > } > =20 > + > push @$devices, '-device', $devicestr; > - $j++; > + last if $d->{mdev}; > } > } > =20 > - return ($kvm_off, $gpu_passthrough, $legacy_igd); > + return ($kvm_off, $gpu_passthrough, $legacy_igd, $pci_devices); > } > =20 > sub prepare_pci_device { > @@ -596,6 +739,26 @@ sub reserve_pci_usage { > warn "leftover PCI reservation found for $id, lets take it...\n"; > } > } > + } elsif ($reservation) { > + # already reserved by the same vmid > + if (my $reserved_time =3D $reservation->{time}) { > + if (defined($timeout)) { > + # use the longer timeout > + my $old_timeout =3D $reservation->{time} - 5 - $ctime; > + $timeout =3D $old_timeout if $old_timeout > $timeout; > + } > + } elsif (my $reserved_pid =3D $reservation->{pid}) { > + my $running_pid =3D PVE::QemuServer::Helpers::vm_running_locally($= reservation->{vmid}); > + if (defined($running_pid) && $running_pid =3D=3D $reservation->{pi= d}) { > + if (defined($pid)) { > + die "PCI device '$id' already in use by running VMID '$reservatio= n->{vmid}'\n"; > + } elsif (defined($timeout)) { > + # ignore timeout reservation for running vms, can happen with e.g= . > + # qm showcmd > + return; > + } > + } > + } > } > =20 > $reservation_list->{$id} =3D { vmid =3D> $vmid }; > diff --git a/test/cfg2cmd/q35-linux-hostpci.conf b/test/cfg2cmd/q35-linux= -hostpci.conf > index 749f9839..7290120a 100644 > --- a/test/cfg2cmd/q35-linux-hostpci.conf > +++ b/test/cfg2cmd/q35-linux-hostpci.conf > @@ -8,7 +8,7 @@ hostpci1: d0:13.0,pcie=3D1 > hostpci2: 00:f4.0 > hostpci3: d0:15.1,pcie=3D1 > hostpci4: d0:17.0,pcie=3D1,rombar=3D0 > -hostpci7: d0:15.1,pcie=3D1 > +hostpci7: d0:15.2,pcie=3D1 > machine: q35 > memory: 512 > net0: virtio=3D2E:01:68:F9:9C:87,bridge=3Dvmbr0 > diff --git a/test/cfg2cmd/q35-linux-hostpci.conf.cmd b/test/cfg2cmd/q35-l= inux-hostpci.conf.cmd > index 0774047d..0fedbd2c 100644 > --- a/test/cfg2cmd/q35-linux-hostpci.conf.cmd > +++ b/test/cfg2cmd/q35-linux-hostpci.conf.cmd > @@ -32,7 +32,7 @@ > -device 'pcie-root-port,id=3Dich9-pcie-port-5,addr=3D10.0,x-speed=3D16= ,x-width=3D32,multifunction=3Don,bus=3Dpcie.0,port=3D5,chassis=3D5' \ > -device 'vfio-pci,host=3D0000:d0:17.0,id=3Dhostpci4,bus=3Dich9-pcie-po= rt-5,addr=3D0x0,rombar=3D0' \ > -device 'pcie-root-port,id=3Dich9-pcie-port-8,addr=3D10.3,x-speed=3D16= ,x-width=3D32,multifunction=3Don,bus=3Dpcie.0,port=3D8,chassis=3D8' \ > - -device 'vfio-pci,host=3D0000:d0:15.1,id=3Dhostpci7,bus=3Dich9-pcie-po= rt-8,addr=3D0x0' \ > + -device 'vfio-pci,host=3D0000:d0:15.2,id=3Dhostpci7,bus=3Dich9-pcie-po= rt-8,addr=3D0x0' \ > -device 'VGA,id=3Dvga,bus=3Dpcie.0,addr=3D0x1' \ > -device 'virtio-balloon-pci,id=3Dballoon0,bus=3Dpci.0,addr=3D0x3,free-= page-reporting=3Don' \ > -iscsi 'initiator-name=3Diqn.1993-08.org.debian:01:aabbccddeeff' \ > diff --git a/test/run_config2command_tests.pl b/test/run_config2command_t= ests.pl > index c4966902..dda44d99 100755 > --- a/test/run_config2command_tests.pl > +++ b/test/run_config2command_tests.pl > @@ -81,6 +81,7 @@ my $pci_devs =3D [ > "0000:0f:f2.0", > "0000:d0:13.0", > "0000:d0:15.1", > + "0000:d0:15.2", > "0000:d0:17.0", > "0000:f0:42.0", > "0000:f0:43.0", > --=20 > 2.30.2 >=20 >=20 >=20 > _______________________________________________ > pve-devel mailing list > pve-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel >=20 >=20 >=20