mirror of
https://git.proxmox.com/git/pve-common
synced 2025-04-30 18:27:46 +00:00
SysFSTools: factor out the domain fixup and domain regex
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
969c4ffd9e
commit
853fcdef31
@ -8,7 +8,8 @@ use IO::File;
|
|||||||
use PVE::Tools qw(file_read_firstline dir_glob_foreach);
|
use PVE::Tools qw(file_read_firstline dir_glob_foreach);
|
||||||
|
|
||||||
my $pcisysfs = "/sys/bus/pci";
|
my $pcisysfs = "/sys/bus/pci";
|
||||||
my $pciregex = "([a-f0-9]{4,}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
|
my $domainregex = "[a-f0-9]{4,}";
|
||||||
|
my $pciregex = "($domainregex):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
|
||||||
|
|
||||||
my $parse_pci_ids = sub {
|
my $parse_pci_ids = sub {
|
||||||
my $ids = {};
|
my $ids = {};
|
||||||
@ -33,6 +34,14 @@ my $parse_pci_ids = sub {
|
|||||||
return $ids;
|
return $ids;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
my $fixup_missing_domain = sub {
|
||||||
|
my ($id) = @_;
|
||||||
|
|
||||||
|
$id = "0000:$id" if $id !~ m/^${domainregex}:/;
|
||||||
|
|
||||||
|
return $id;
|
||||||
|
};
|
||||||
|
|
||||||
# returns a list of pci devices
|
# returns a list of pci devices
|
||||||
#
|
#
|
||||||
# filter is either a string (then it tries to match to the id)
|
# filter is either a string (then it tries to match to the id)
|
||||||
@ -148,14 +157,11 @@ sub lspci {
|
|||||||
sub get_mdev_types {
|
sub get_mdev_types {
|
||||||
my ($id) = @_;
|
my ($id) = @_;
|
||||||
|
|
||||||
my $fullid = $id;
|
$id = $fixup_missing_domain->($id);
|
||||||
if ($id !~ m/^[0-9a-fA-f]{4,}:/) {
|
|
||||||
$fullid = "0000:$id";
|
|
||||||
}
|
|
||||||
|
|
||||||
my $types = [];
|
my $types = [];
|
||||||
|
|
||||||
my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
|
my $mdev_path = "$pcisysfs/devices/$id/mdev_supported_types";
|
||||||
if (!-d $mdev_path) {
|
if (!-d $mdev_path) {
|
||||||
return $types;
|
return $types;
|
||||||
}
|
}
|
||||||
@ -300,11 +306,11 @@ sub pci_dev_group_bind_to_vfio {
|
|||||||
}
|
}
|
||||||
die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
|
die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
|
||||||
|
|
||||||
$pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4,}:/;
|
$pciid = $fixup_missing_domain->($pciid);
|
||||||
|
|
||||||
# get IOMMU group devices
|
# get IOMMU group devices
|
||||||
opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
|
opendir(my $D, "$pcisysfs/devices/$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
|
||||||
my @devs = grep /^[0-9a-f]{4,}:/, readdir($D);
|
my @devs = grep /^${domainregex}:/, readdir($D);
|
||||||
closedir($D);
|
closedir($D);
|
||||||
|
|
||||||
foreach my $pciid (@devs) {
|
foreach my $pciid (@devs) {
|
||||||
@ -323,7 +329,7 @@ sub pci_dev_group_bind_to_vfio {
|
|||||||
sub pci_create_mdev_device {
|
sub pci_create_mdev_device {
|
||||||
my ($pciid, $uuid, $type) = @_;
|
my ($pciid, $uuid, $type) = @_;
|
||||||
|
|
||||||
$pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4,}:/;
|
$pciid = $fixup_missing_domain->($pciid);
|
||||||
|
|
||||||
my $basedir = "$pcisysfs/devices/$pciid";
|
my $basedir = "$pcisysfs/devices/$pciid";
|
||||||
my $mdev_dir = "$basedir/mdev_supported_types";
|
my $mdev_dir = "$basedir/mdev_supported_types";
|
||||||
@ -360,7 +366,7 @@ sub pci_create_mdev_device {
|
|||||||
sub pci_cleanup_mdev_device {
|
sub pci_cleanup_mdev_device {
|
||||||
my ($pciid, $uuid) = @_;
|
my ($pciid, $uuid) = @_;
|
||||||
|
|
||||||
$pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4,}:/;
|
$pciid = $fixup_missing_domain->($pciid);
|
||||||
|
|
||||||
my $basedir = "$pcisysfs/devices/$pciid/$uuid";
|
my $basedir = "$pcisysfs/devices/$pciid/$uuid";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user