pci_dev_physfn_id() is used to obtain the parent device of a virtual
function, i.e. the physical function.

Needed for retrieving information about Nvidia vGPU devices via
proxmox-ve-vfio, as libnvidia-ml functions only work with physical
functions.

Signed-off-by: Christoph Heiss <[email protected]>
---
 src/PVE/SysFSTools.pm | 45 ++++++++++++++++++++++++++++++-------------
 1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm
index a00fbcb..89e57b9 100644
--- a/src/PVE/SysFSTools.pm
+++ b/src/PVE/SysFSTools.pm
@@ -4,8 +4,10 @@ use strict;
 use warnings;
 
 use IO::File;
+use File::Basename;
 
 use PVE::Tools qw(file_read_firstline dir_glob_foreach);
+use PVE::RS::VFIO::Nvidia;
 
 my $pcisysfs = "/sys/bus/pci";
 my $domainregex = "[a-f0-9]{4,}";
@@ -157,6 +159,7 @@ sub lspci {
 #         type => 'FooType_1',
 #         description => "a longer description with custom format\nand 
newlines",
 #         available => 5,
+#         name => "human-readable name of mdev"
 #     },
 #     ...
 # ]
@@ -170,7 +173,7 @@ sub get_mdev_types {
 
     my $dev_path = "$pcisysfs/devices/$id";
     my $mdev_path = "$dev_path/mdev_supported_types";
-    my $nvidia_path = "$dev_path/nvidia/creatable_vgpu_types";
+    my $nvidia_path = "$dev_path/nvidia";
     if (-d $mdev_path) {
         dir_glob_foreach(
             $mdev_path,
@@ -195,20 +198,20 @@ sub get_mdev_types {
                 push @$types, $entry;
             },
         );
-    } 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;
+    } elsif (-d $nvidia_path) {
+        $id = pci_dev_physfn_id($id);
+        my $nvtypes = eval { 
PVE::RS::VFIO::Nvidia::creatable_vgpu_types_for_dev($id); };
 
-            push $types->@*, {
-                type => "nvidia-$id", # backwards compatibility
-                description => "", # TODO, read from xml/nvidia-smi ?
+        if (my $err = $@) {
+            die "failed to get creatable Nvidia vGPU types for $id: $err\n";
+        } else {
+            my @mapped = map { {
+                type => "nvidia-$_->{id}", # backwards compatibility
                 available => 1,
-                name => $name,
-            };
+                description => $_->{description},
+                name => $_->{name},
+            } } @$nvtypes;
+            $types = \@mapped;
         }
     }
 
@@ -409,6 +412,22 @@ sub pci_create_mdev_device {
     return undef;
 }
 
+# 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.
+sub pci_dev_physfn_id {
+    my ($id) = @_;
+
+    $id = normalize_pci_id($id);
+    my $devpath = "$pcisysfs/devices/$id";
+
+    if (-d "$devpath/physfn") {
+        return basename(readlink("$devpath/physfn"));
+    } else {
+        return $id;
+    }
+}
+
 # encode the hostpci index and vmid into the uuid
 sub generate_mdev_uuid {
     my ($vmid, $index) = @_;
-- 
2.52.0



_______________________________________________
pve-devel mailing list
[email protected]
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to