1 package PVE
::SysFSTools
;
8 use PVE
::Tools
qw(file_read_firstline dir_glob_foreach);
10 my $pcisysfs = "/sys/bus/pci";
11 my $domainregex = "[a-f0-9]{4,}";
12 my $pciregex = "($domainregex):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
14 my $parse_pci_ids = sub {
17 open(my $fh, '<', "/usr/share/misc/pci.ids")
22 while (my $line = <$fh>) {
23 if ($line =~ m/^([0-9a-fA-F]{4})\s+(.*)$/) {
24 $curvendor = ($ids->{"0x$1"} = {});
25 $curvendor->{name
} = $2;
26 } elsif ($line =~ m/^\t([0-9a-fA-F]{4})\s+(.*)$/) {
27 $curdevice = ($curvendor->{devices
}->{"0x$1"} = {});
28 $curdevice->{name
} = $2;
29 } elsif ($line =~ m/^\t\t([0-9a-fA-F]{4}) ([0-9a-fA-F]{4})\s+(.*)$/) {
30 $curdevice->{subs
}->{"0x$1"}->{"0x$2"} = $3;
37 my sub normalize_pci_id
{
39 $id = "0000:$id" if $id !~ m/^${domainregex}:/;
43 # returns a list of pci devices
45 # filter is either a string (then it tries to match to the id)
46 # or a sub ref (then it adds the device if the sub returns truthy)
48 # verbose also returns iommu groups, subvendor/device and the
49 # human readable names from /usr/share/misc/pci.ids
57 # class => '0x012345',
62 # vendor_name => 'Foo Inc.',
63 # device_name => 'Bar 9000AF',
64 # subsystem_vendor => '0xacac',
65 # subsystem_device => '0xfefe',
66 # subsystem_vendor_name => 'Foo Europe GmbH',
67 # subsystem_device_name => 'Bar 9001AF OC',
73 my ($filter, $verbose) = @_;
78 $ids = $parse_pci_ids->();
81 dir_glob_foreach
("$pcisysfs/devices", $pciregex, sub {
82 my ($fullid, $domain, $bus, $slot, $function) = @_;
83 my $id = "$domain:$bus:$slot.$function";
85 if (defined($filter) && !ref($filter) && $id !~ m/^(0000:)?\Q$filter\E/) {
86 return; # filter ids early
89 my $devdir = "$pcisysfs/devices/$fullid";
91 my $vendor = file_read_firstline
("$devdir/vendor");
92 my $device = file_read_firstline
("$devdir/device");
93 my $class = file_read_firstline
("$devdir/class");
102 if (defined($filter) && ref($filter) eq 'CODE' && !$filter->($res)) {
107 $res->{iommugroup
} = -1;
108 if (-e
"$devdir/iommu_group") {
109 my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m
/\
/(\d+)$/);
110 $res->{iommugroup
} = int($iommugroup);
113 if (-d
"$devdir/mdev_supported_types") {
117 my $device_hash = $ids->{$vendor}->{devices
}->{$device} // {};
119 my $sub_vendor = file_read_firstline
("$devdir/subsystem_vendor");
120 my $sub_device = file_read_firstline
("$devdir/subsystem_device");
122 my $vendor_name = $ids->{$vendor}->{name
};
123 my $device_name = $device_hash->{name
};
124 my $sub_vendor_name = $ids->{$sub_vendor}->{name
};
125 my $sub_device_name = $device_hash->{subs
}->{$sub_vendor}->{$sub_device};
127 $res->{vendor_name
} = $vendor_name if defined($vendor_name);
128 $res->{device_name
} = $device_name if defined($device_name);
129 $res->{subsystem_vendor
} = $sub_vendor if defined($sub_vendor);
130 $res->{subsystem_device
} = $sub_device if defined($sub_device);
131 $res->{subsystem_vendor_name
} = $sub_vendor_name if defined($sub_vendor_name);
132 $res->{subsystem_device_name
} = $sub_device_name if defined($sub_device_name);
135 push @$devices, $res;
138 # Entries should be sorted by ids
139 $devices = [ sort { $a->{id
} cmp $b->{id
} } @$devices ];
148 # type => 'FooType_1',
149 # description => "a longer description with custom format\nand newlines",
158 $id = normalize_pci_id
($id);
162 my $mdev_path = "$pcisysfs/devices/$id/mdev_supported_types";
163 if (!-d
$mdev_path) {
167 dir_glob_foreach
($mdev_path, '[^\.].*', sub {
170 my $type_path = "$mdev_path/$type";
172 my $available = int(file_read_firstline
("$type_path/available_instances"));
173 my $description = PVE
::Tools
::file_get_contents
("$type_path/description");
177 description
=> $description,
178 available
=> $available,
181 my $name = file_read_firstline
("$type_path/name");
182 $entry->{name
} = $name if defined($name);
184 push @$types, $entry;
190 sub check_iommu_support
{
191 # we have IOMMU support if /sys/class/iommu/ is populated
192 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
196 my ($filename, $buf) = @_;
198 my $fh = IO
::File-
>new($filename, "w");
199 return undef if !$fh;
201 my $res = print $fh $buf;
208 sub pci_device_info
{
209 my ($name, $verbose) = @_;
213 return undef if $name !~ m/^${pciregex}$/;
214 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
216 my $devdir = "$pcisysfs/devices/$name";
218 my $irq = file_read_firstline
("$devdir/irq");
219 return undef if !defined($irq) || $irq !~ m/^\d+$/;
221 my $vendor = file_read_firstline
("$devdir/vendor");
222 return undef if !defined($vendor) || $vendor !~ s/^0x//;
224 my $product = file_read_firstline
("$devdir/device");
225 return undef if !defined($product) || $product !~ s/^0x//;
236 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
240 my $sub_vendor = file_read_firstline
("$devdir/subsystem_vendor");
241 $sub_vendor =~ s/^0x// if defined($sub_vendor);
242 my $sub_device = file_read_firstline
("$devdir/subsystem_device");
243 $sub_device =~ s/^0x// if defined($sub_device);
245 $res->{subsystem_vendor
} = $sub_vendor if defined($sub_vendor);
246 $res->{subsystem_device
} = $sub_device if defined($sub_device);
248 if (-e
"$devdir/iommu_group") {
249 my ($iommugroup) = (readlink("$devdir/iommu_group") =~ m
/\
/(\d+)$/);
250 $res->{iommugroup
} = int($iommugroup);
253 if (-d
"$devdir/mdev_supported_types") {
264 my $name = $dev->{name
};
266 my $fn = "$pcisysfs/devices/$name/reset";
268 return file_write
($fn, "1");
271 sub pci_dev_bind_to_vfio
{
274 my $name = $dev->{name
};
276 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
278 if (!-d
$vfio_basedir) {
279 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
281 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
283 my $testdir = "$vfio_basedir/$name";
284 return 1 if -d
$testdir;
286 my $data = "$dev->{vendor} $dev->{device}";
287 return undef if !file_write
("$vfio_basedir/new_id", $data);
289 my $fn = "$pcisysfs/devices/$name/driver/unbind";
290 if (!file_write
($fn, $name)) {
291 return undef if -f
$fn;
294 $fn = "$vfio_basedir/bind";
296 return undef if !file_write
($fn, $name);
302 sub pci_dev_group_bind_to_vfio
{
305 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
307 if (!-d
$vfio_basedir) {
308 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
310 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
312 $pciid = normalize_pci_id
($pciid);
314 # get IOMMU group devices
315 opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
316 my @devs = grep /^${domainregex}:/, readdir($D);
319 foreach my $pciid (@devs) {
320 $pciid =~ m/^([:\.0-9a-f]+)$/ or die "PCI ID $pciid not valid!\n";
322 # PCI bridges, switches or root-ports aren't supported and all have a pci_bus dir we can test
323 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
325 my $info = pci_device_info
($1);
326 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";
332 sub pci_create_mdev_device
{
333 my ($pciid, $uuid, $type) = @_;
335 $pciid = normalize_pci_id
($pciid);
337 my $basedir = "$pcisysfs/devices/$pciid";
338 my $mdev_dir = "$basedir/mdev_supported_types";
340 die "pci device '$pciid' does not support mediated devices \n"
343 die "pci device '$pciid' has no type '$type'\n"
344 if !-d
"$mdev_dir/$type";
346 if (-d
"$basedir/$uuid") {
347 # it already exists, checking type
348 my $typelink = readlink("$basedir/$uuid/mdev_type");
349 my ($existingtype) = $typelink =~ m
|/([^/]+)$|;
350 die "mdev instance '$uuid' already exits, but type is not '$type'\n"
351 if $type ne $existingtype;
353 # instance exists, so use it but warn the user
354 warn "mdev instance '$uuid' already existed, using it.\n";
358 my $instances = file_read_firstline
("$mdev_dir/$type/available_instances");
359 my ($avail) = $instances =~ m/^(\d+)$/;
360 die "pci device '$pciid' has no available instances of '$type'\n"
363 die "could not create 'type' for pci devices '$pciid'\n"
364 if !file_write
("$mdev_dir/$type/create", $uuid);
369 # FIXME: move over to qemu-server, drop after PVE 8.0 and break older (7.x) qemu-server
370 sub pci_cleanup_mdev_device
{
371 my ($pciid, $uuid) = @_;
373 # NOTE: PCI ID is actually not a requirement, /sys/bus/mdev/devices/$uuid is always available
374 $pciid = normalize_pci_id
($pciid);
376 my $basedir = "$pcisysfs/devices/$pciid/$uuid";
379 return 1; # no cleanup necessary if it does not exist
382 return file_write
("$basedir/remove", "1");
385 # encode the hostpci index and vmid into the uuid
386 sub generate_mdev_uuid
{
387 my ($vmid, $index) = @_;
389 my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
394 # idea is from usbutils package (/usr/bin/usb-devices) script
395 sub __scan_usb_device
{
396 my ($res, $devpath, $parent, $level) = @_;
398 return if ! -d
$devpath;
399 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
400 my $port = $level ?
int($1 - 1) : 0;
402 my $busnum = int(file_read_firstline
("$devpath/busnum"));
403 my $devnum = int(file_read_firstline
("$devpath/devnum"));
410 speed
=> file_read_firstline
("$devpath/speed"),
411 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
412 vendid
=> file_read_firstline
("$devpath/idVendor"),
413 prodid
=> file_read_firstline
("$devpath/idProduct"),
417 my $usbpath = $devpath;
418 $usbpath =~ s
|^.*/\d
+\
-||;
419 $d->{usbpath
} = $usbpath;
422 my $product = file_read_firstline
("$devpath/product");
423 $d->{product
} = $product if $product;
425 my $manu = file_read_firstline
("$devpath/manufacturer");
426 $d->{manufacturer
} = $manu if $manu;
428 my $serial => file_read_firstline
("$devpath/serial");
429 $d->{serial
} = $serial if $serial;
433 foreach my $subdev (<$devpath/$busnum-*>) {
434 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
435 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
444 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
445 __scan_usb_device
($devlist, $device, 0, 0);