This gets us the missing description that used to be in sysfs.

In case this is an SR-IOV virtual function, we have to get the physical
device first since only that is a valid device for querying with NVML.

'pci_dev_physfn_id' is only used here currently so it's a local sub, but
if we need it in more places, a good place could be 'PVE::SysFSTools' or
'PVE::QemuServer::PCI'.

Signed-off-by: Dominik Csapak <[email protected]>
---
 src/PVE/QemuServer/PCI/Mdev.pm | 45 ++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/src/PVE/QemuServer/PCI/Mdev.pm b/src/PVE/QemuServer/PCI/Mdev.pm
index 3b42ce2d..51dca474 100644
--- a/src/PVE/QemuServer/PCI/Mdev.pm
+++ b/src/PVE/QemuServer/PCI/Mdev.pm
@@ -2,11 +2,28 @@ package PVE::QemuServer::PCI::Mdev;
 
 use v5.36;
 
+use File::Basename;
+
+use PVE::RS::NVML;
 use PVE::SysFSTools;
 use PVE::File qw(file_read_first_line dir_glob_foreach file_get_contents);
 
 my $pcisysfs = "/sys/bus/pci";
 
+# Returns the PCI bus id of the physical function (IOW, parent device) of the
+# given device. If the device does not have a parent physical function, returns
+# the given ID unchanged.
+my sub pci_dev_physfn_id($id) {
+    $id = PVE::SysFSTools::normalize_pci_id($id);
+    my $devpath = "$pcisysfs/devices/$id";
+
+    if (-d "$devpath/physfn") {
+        return basename(readlink("$devpath/physfn"));
+    } else {
+        return $id;
+    }
+}
+
 sub generate_mdev_uuid($vmid, $index) {
     return sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
 }
@@ -18,6 +35,7 @@ sub generate_mdev_uuid($vmid, $index) {
 #         type => 'FooType_1',
 #         description => "a longer description with custom format\nand 
newlines",
 #         available => 5,
+#         name => "human readable name for the type",
 #     },
 #     ...
 # ]
@@ -55,19 +73,20 @@ sub get_mdev_types($id) {
             },
         );
     } elsif (-f $nvidia_path) {
-        my $creatable = PVE::Tools::file_get_contents($nvidia_path);
-        for my $line (split("\n", $creatable)) {
-            next if $line =~ m/^ID/; # header
-            next if $line !~ m/^(.*?)\s*:\s*(.*)$/;
-            my $id = $1;
-            my $name = $2;
-
-            push $types->@*, {
-                type => "nvidia-$id", # backwards compatibility
-                description => "", # TODO, read from xml/nvidia-smi ?
-                available => 1,
-                name => $name,
-            };
+        my $physfn = pci_dev_physfn_id($id);
+        my $creatable = eval { 
PVE::RS::NVML::creatable_vgpu_types_for_dev($physfn) };
+        die "failed to query NVIDIA vGPU types for $id - $@\n" if $@;
+
+        for my $type ($creatable->@*) {
+            my $nvidia_id = $type->{id};
+            my $name = $type->{name};
+            push $types->@*,
+                {
+                    type => "nvidia-$nvidia_id",
+                    description => $type->{description},
+                    available => 1,
+                    name => $name,
+                };
         }
     }
 
-- 
2.47.3




Reply via email to