all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH common] SysFSTools: factor out the domain fixup and domain regex
Date: Thu, 17 Mar 2022 09:12:26 +0100	[thread overview]
Message-ID: <20220317081226.740703-1-d.csapak@proxmox.com> (raw)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/PVE/SysFSTools.pm | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/PVE/SysFSTools.pm b/src/PVE/SysFSTools.pm
index aabe833..4038eed 100644
--- a/src/PVE/SysFSTools.pm
+++ b/src/PVE/SysFSTools.pm
@@ -8,7 +8,8 @@ use IO::File;
 use PVE::Tools qw(file_read_firstline dir_glob_foreach);
 
 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 $ids = {};
@@ -33,6 +34,14 @@ my $parse_pci_ids = sub {
     return $ids;
 };
 
+my $fixup_missing_domain = sub {
+    my ($id) = @_;
+
+    $id = "0000:$id" if $id !~ m/^${domainregex}:/;
+
+    return $id;
+};
+
 # returns a list of pci devices
 #
 # filter is either a string (then it tries to match to the id)
@@ -148,14 +157,11 @@ sub lspci {
 sub get_mdev_types {
     my ($id) = @_;
 
-    my $fullid = $id;
-    if ($id !~ m/^[0-9a-fA-f]{4,}:/) {
-	$fullid = "0000:$id";
-    }
+    $id = $fixup_missing_domain->($id);
 
     my $types = [];
 
-    my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
+    my $mdev_path = "$pcisysfs/devices/$id/mdev_supported_types";
     if (!-d $mdev_path) {
 	return $types;
     }
@@ -300,11 +306,11 @@ sub pci_dev_group_bind_to_vfio {
     }
     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
     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);
 
     foreach my $pciid (@devs) {
@@ -323,7 +329,7 @@ sub pci_dev_group_bind_to_vfio {
 sub pci_create_mdev_device {
     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 $mdev_dir = "$basedir/mdev_supported_types";
@@ -360,7 +366,7 @@ sub pci_create_mdev_device {
 sub pci_cleanup_mdev_device {
     my ($pciid, $uuid) = @_;
 
-    $pciid = "0000:$pciid" if $pciid !~ m/^[0-9a-f]{4,}:/;
+    $pciid = $fixup_missing_domain->($pciid);
 
     my $basedir = "$pcisysfs/devices/$pciid/$uuid";
 
-- 
2.30.2





             reply	other threads:[~2022-03-17  8:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17  8:12 Dominik Csapak [this message]
2022-04-25 14:31 ` Dominik Csapak
2022-04-25 16:16 ` [pve-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220317081226.740703-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal