[pve-devel] [PATCH common 1/2] SysFSTools.pm: add mediated devices subs
Dominik Csapak
d.csapak at proxmox.com
Fri Nov 16 16:29:47 CET 2018
adds helpers for listing/creating/removing mediated devices
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
src/PVE/SysFSTools.pm | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm
index 4b23668..a031713 100644
--- a/src/PVE/SysFSTools.pm
+++ b/src/PVE/SysFSTools.pm
@@ -32,6 +32,39 @@ sub lspci {
return $devices;
}
+sub get_mdev_types {
+ my ($id) = @_;
+
+ my $fullid = $id;
+ if ($id !~ m/^[0-9a-fA-f]{4}:/) {
+ $fullid = "0000:$id";
+ }
+
+ my $types = [];
+
+ my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
+ if (!-d $mdev_path) {
+ return $types;
+ }
+
+ dir_glob_foreach($mdev_path, '[^\.].*', sub {
+ my ($type) = @_;
+
+ my $type_path = "$mdev_path/$type";
+
+ my $available = int(file_read_firstline("$type_path/available_instances"));
+ my $description = PVE::Tools::file_get_contents("$type_path/description");
+
+ push @$types, {
+ type => $type,
+ description => $description,
+ available => $available,
+ };
+ });
+
+ return $types;
+}
+
sub check_iommu_support{
# iommu support if there is anything in /sys/class/iommu besides . or ..
return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*");
@@ -152,6 +185,58 @@ sub pci_dev_group_bind_to_vfio {
return 1;
}
+sub pci_create_mdev_device {
+ my ($pciid, $uuid, $type) = @_;
+
+ my $basedir = "$pcisysfs/devices/0000:$pciid";
+ my $mdev_dir = "$basedir/mdev_supported_types";
+
+ die "pci device '$pciid' does not support mediated devices \n"
+ if !-d $mdev_dir;
+
+ die "pci device '$pciid' has no type '$type'\n"
+ if !-d "$mdev_dir/$type";
+
+ if (-d "$basedir/$uuid") {
+ # it already exists, checking type
+ my $typelink = readlink("$basedir/$uuid/mdev_type");
+ my ($existingtype) = $typelink =~ m|/([^/]+)$|;
+ die "mdev instance '$uuid' already exits, but type is not '$type'\n"
+ if $type ne $existingtype;
+
+ # instance exists, so use it but warn the user
+ warn "mdev instance '$uuid' already existed, using it.\n";
+ return undef;
+ }
+
+ my $instances = file_read_firstline("$mdev_dir/$type/available_instances");
+ my ($avail) = $instances =~ m/^(\d+)$/;
+ die "pci device '$pciid' has no available instances of '$type'\n"
+ if $avail < 1;
+
+ die "could not create 'type' for pci devices '$pciid'\n"
+ if !file_write("$mdev_dir/$type/create", $uuid);
+
+ return undef;
+}
+
+sub pci_cleanup_mdev_device {
+ my ($pciid, $uuid) = @_;
+
+ my $basedir = "$pcisysfs/devices/0000:$pciid/$uuid";
+
+ return file_write("$basedir/remove", "1");
+}
+
+# encode the hostpci index and vmid into the uuid
+sub generate_mdev_uuid {
+ my ($vmid, $index) = @_;
+
+ my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
+
+ return $string;
+}
+
# idea is from usbutils package (/usr/bin/usb-devices) script
sub __scan_usb_device {
my ($res, $devpath, $parent, $level) = @_;
--
2.11.0
More information about the pve-devel
mailing list