all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server v2 20/32] test: add tests for PCI reservations
Date: Wed, 18 Jun 2025 15:01:57 +0200	[thread overview]
Message-ID: <20250618130209.90649-21-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250618130209.90649-1-f.ebner@proxmox.com>

This is currently tested as part of the config-to-command tests, but
the plan is to make command generation for 'qm showcmd' not actually
reserve anything. The reserve_pci_usage() function also serves as
checking what already is reserved by other VMs at the same time.
Thus, the config-to-command test will not be able to test whether
reservations are actually respected after the above-mentioned change.
Add a dedicated test for this.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

New in v2.

 src/PVE/QemuServer/PCI.pm             |   2 +-
 src/test/Makefile                     |   5 +-
 src/test/run_pci_reservation_tests.pl | 175 ++++++++++++++++++++++++++
 3 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100755 src/test/run_pci_reservation_tests.pl

diff --git a/src/PVE/QemuServer/PCI.pm b/src/PVE/QemuServer/PCI.pm
index 751f8a84..9b343115 100644
--- a/src/PVE/QemuServer/PCI.pm
+++ b/src/PVE/QemuServer/PCI.pm
@@ -576,7 +576,7 @@ my sub create_nvidia_device {
 #
 # 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 alternative anyway
-my sub choose_hostpci_devices {
+sub choose_hostpci_devices {
     my ($devices, $vmid) = @_;
 
     # if the vm is running, we must be in 'showcmd', so don't actually reserve or create anything
diff --git a/src/test/Makefile b/src/test/Makefile
index f7372e2e..2ef9073a 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -1,6 +1,6 @@
 all: test
 
-test: test_snapshot test_cfg_to_cmd test_pci_addr_conflicts test_qemu_img_convert test_migration test_restore_config test_parse_config
+test: test_snapshot test_cfg_to_cmd test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config
 
 test_snapshot: run_snapshot_tests.pl
 	./run_snapshot_tests.pl
@@ -15,6 +15,9 @@ test_qemu_img_convert: run_qemu_img_convert_tests.pl
 test_pci_addr_conflicts: run_pci_addr_checks.pl
 	./run_pci_addr_checks.pl
 
+test_pci_reservation: run_pci_reservation_tests.pl
+	./run_pci_reservation_tests.pl
+
 MIGRATION_TEST_TARGETS := $(addprefix test_migration_,$(shell perl -ne 'print "$$1 " if /^\s*name\s*=>\s*["'\'']([^\s"'\'']+)["'\'']\s*,\s*$$/; END { print "\n" }' run_qemu_migrate_tests.pl))
 
 test_migration: run_qemu_migrate_tests.pl MigrationTest/*.pm $(MIGRATION_TEST_TARGETS)
diff --git a/src/test/run_pci_reservation_tests.pl b/src/test/run_pci_reservation_tests.pl
new file mode 100755
index 00000000..bd428a80
--- /dev/null
+++ b/src/test/run_pci_reservation_tests.pl
@@ -0,0 +1,175 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+my $vmid = 8006;
+
+use Test::MockModule;
+use Test::More;
+
+use PVE::Mapping::PCI;
+
+use PVE::QemuServer::PCI;
+
+my $pci_devs = [
+    "0000:00:43.1",
+    "0000:00:f4.0",
+    "0000:00:ff.1",
+    "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",
+    "0000:f0:43.1",
+    "1234:f0:43.1",
+    "0000:01:00.4",
+    "0000:01:00.5",
+    "0000:01:00.6",
+    "0000:07:10.0",
+    "0000:07:10.1",
+    "0000:07:10.4",
+];
+
+my $pci_map_config = {
+    ids => {
+        someGpu => {
+            type => 'pci',
+            mdev => 1,
+            map => [
+                'node=localhost,path=0000:01:00.4,id=10de:2231,iommugroup=1',
+                'node=localhost,path=0000:01:00.5,id=10de:2231,iommugroup=1',
+                'node=localhost,path=0000:01:00.6,id=10de:2231,iommugroup=1',
+            ],
+        },
+        someNic => {
+            type => 'pci',
+            map => [
+                'node=localhost,path=0000:07:10.0,id=8086:1520,iommugroup=2',
+                'node=localhost,path=0000:07:10.1,id=8086:1520,iommugroup=2',
+                'node=localhost,path=0000:07:10.4,id=8086:1520,iommugroup=2',
+            ],
+        },
+    },
+};
+
+my $tests = [
+    {
+        name => 'reservation-is-respected',
+        conf => {
+            hostpci0 => 'mapping=someNic',
+            hostpci1 => 'mapping=someGpu,mdev=some-model',
+            hostpci2 => 'mapping=someNic',
+        },
+        expected => {
+            hostpci0 => { ids => [{ id => '0000:07:10.0' }] },
+            hostpci1 => {
+                ids => [
+                    { id => '0000:01:00.4' }, { id => '0000:01:00.5' },
+                    { id => '0000:01:00.6' },
+                ],
+                mdev => 'some-model',
+            },
+            hostpci2 => { ids => [{ id => '0000:07:10.4' }] },
+        },
+    },
+];
+
+plan tests => scalar($tests->@*);
+
+my $pve_common_inotify;
+$pve_common_inotify = Test::MockModule->new('PVE::INotify');
+$pve_common_inotify->mock(
+    nodename => sub {
+        return 'localhost';
+    },
+);
+
+my $pve_common_sysfstools;
+$pve_common_sysfstools = Test::MockModule->new('PVE::SysFSTools');
+$pve_common_sysfstools->mock(
+    lspci => sub {
+        my ($filter, $verbose) = @_;
+
+        return [
+            map { { id => $_ } }
+            grep {
+                !defined($filter)
+                    || (!ref($filter) && $_ =~ m/^(0000:)?\Q$filter\E/)
+                    || (ref($filter) eq 'CODE' && $filter->({ id => $_ }))
+            } sort @$pci_devs
+        ];
+    },
+    pci_device_info => sub {
+        my ($path, $noerr) = @_;
+
+        if ($path =~ m/^0000:01:00/) {
+            return {
+                mdev => 1,
+                iommugroup => 1,
+                mdev => 1,
+                vendor => "0x10de",
+                device => "0x2231",
+            };
+        } elsif ($path =~ m/^0000:07:10/) {
+            return {
+                iommugroup => 2,
+                vendor => "0x8086",
+                device => "0x1520",
+            };
+        } else {
+            return {};
+        }
+    },
+);
+
+my $mapping_pci_module = Test::MockModule->new("PVE::Mapping::PCI");
+$mapping_pci_module->mock(
+    config => sub {
+        return $pci_map_config;
+    },
+);
+
+my $pci_module = Test::MockModule->new("PVE::QemuServer::PCI");
+$pci_module->mock(
+    reserve_pci_usage => sub {
+        my ($ids, $vmid, $timeout, $pid, $dryrun) = @_;
+
+        $ids = [$ids] if !ref($ids);
+
+        for my $id (@$ids) {
+            if ($id eq "0000:07:10.1") {
+                die "reserved";
+            }
+        }
+
+        return undef;
+    },
+    create_nvidia_device => sub {
+        return 1;
+    },
+);
+
+for my $test ($tests->@*) {
+    my ($name, $conf, $expected) = $test->@{qw(name conf expected)};
+    my $pci_devices;
+    eval {
+        my $devices = PVE::QemuServer::PCI::parse_hostpci_devices($conf);
+        use JSON;
+        $pci_devices = PVE::QemuServer::PCI::choose_hostpci_devices($devices, $vmid);
+    };
+    if (my $err = $@) {
+        is($err, $expected, $name);
+    } elsif ($pci_devices) {
+        is_deeply($pci_devices, $expected, $name);
+    } else {
+        fail($name);
+        note("no result");
+    }
+}
+
+done_testing();
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-06-18 13:04 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-18 13:01 [pve-devel] [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 01/32] schema: parse property string: support skipping keys Fiona Ebner
2025-06-20 11:00   ` Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 02/32] json schema: add helper to convert to JSON boolean Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 03/32] buildsys: order Perl source files in QemuServer/Makefile Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 04/32] drive: code cleanup: drop unused $vmid parameter from get_path_and_format() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 05/32] cfg2cmd: require at least QEMU binary version 6.0 Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 06/32] drive: parse: use hash argument for optional parameters Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 07/32] drive: parse: properly handle dropped properties for restore Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 08/32] drive: remove geometry options gone since QEMU 3.1 Fiona Ebner
2025-06-20 11:03   ` Fabian Grünbichler
2025-06-20 11:20     ` Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 09/32] clone disk: io uring check: fix call to determine cache direct Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 10/32] drive: move storage_allows_io_uring_default() and drive_uses_cache_direct() helpers to drive module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 11/32] drive: introduce aio_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 12/32] drive: introduce detect_zeroes_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 13/32] vm start: assert that migration type is set for 'tcp' migration Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 14/32] introduce StateFile module for state file related helpers Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 15/32] vm start: move state file handling to dedicated module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 16/32] vm start: move config_to_command() call further down Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 17/32] vm start/commandline: also clean up pci reservation when config_to_command() fails Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 18/32] vm start/commandline: activate volumes before config_to_command() Fiona Ebner
2025-06-20 11:33   ` Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 19/32] pci: add missing includes Fiona Ebner
2025-06-18 13:01 ` Fiona Ebner [this message]
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 21/32] cfg2cmd: print vga: fix call to print_pcie_addr() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 22/32] pci: code cleanup: remove superfluous machine type paramater from print_pci_addr Fiona Ebner
2025-06-18 15:19   ` Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 23/32] cfg2cmd: collect optional parameters as a hash Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 24/32] qm: showcmd: never reserve PCI devices Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 25/32] vm devices list: prepare querying block device names for -blockdev Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 26/32] print drive device: explicitly set write-cache starting with machine version 10.0 Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 27/32] print drive device: set {r, w}error front-end properties " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 28/32] print drive device: don't reference any drive for 'none' " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 29/32] drive: create a throttle group for each drive " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 30/32] blockdev: add helpers to generate blockdev commandline Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 31/32] blockdev: add support for NBD paths Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 32/32] command line: switch to blockdev starting with machine version 10.0 Fiona Ebner
2025-06-23  9:12   ` DERUMIER, Alexandre via pve-devel
2025-06-23  9:31     ` Fiona Ebner
2025-06-23 13:06       ` DERUMIER, Alexandre via pve-devel
2025-06-20 13:03 ` [pve-devel] partially-applied: [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fabian Grünbichler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250618130209.90649-21-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal