From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.schauer@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id C35FA9C785
 for <pve-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:56:34 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A485D83A8
 for <pve-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:56:04 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:56:04 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id EBD5D44B70
 for <pve-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:56:03 +0200 (CEST)
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 24 Oct 2023 14:55:54 +0200
Message-Id: <20231024125554.131800-3-f.schauer@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20231024125554.131800-1-f.schauer@proxmox.com>
References: <20231024125554.131800-1-f.schauer@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.199 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH v2 guest-common 1/1] Add
 foreach_passthrough_device
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 24 Oct 2023 12:56:34 -0000

Add a function to iterate over passthrough devices of a provided
container config.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/PVE/AbstractConfig.pm | 44 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm
index a286b13..ed26e91 100644
--- a/src/PVE/AbstractConfig.pm
+++ b/src/PVE/AbstractConfig.pm
@@ -484,6 +484,50 @@ sub foreach_volume {
     return $class->foreach_volume_full($conf, undef, $func, @param);
 }
 
+sub foreach_passthrough_device {
+    my ($class, $conf, $func, @param) = @_;
+
+    foreach my $key (keys %$conf) {
+	next if $key !~ m/^dev(\d+)$/;
+
+	my $device = $class->parse_device($conf->{$key});
+
+	if (defined($device->{path})) {
+	    die "Device path $device->{path} does not start with /dev/\n"
+		if $device->{path} !~ m!^/dev/!;
+
+	    my $sanitized_path = substr($conf->{$key}, 1);
+	    die "Device /$sanitized_path does not exist\n" unless (-e "/$sanitized_path");
+
+	    $func->($key, $sanitized_path, @param);
+	} elsif (defined($device->{usbmapping})) {
+	    my $mapping = $device->{usbmapping};
+	    my $map_devices = PVE::Mapping::USB::find_on_current_node($mapping);
+
+	    die "USB device mapping not found for '$mapping'\n"
+		if !$map_devices || !scalar($map_devices->@*);
+
+	    die "More than one USB mapping per host not supported\n"
+		if scalar($map_devices->@*) > 1;
+
+	    eval {
+		PVE::Mapping::USB::assert_valid($mapping, $map_devices->[0]);
+	    };
+	    if (my $err = $@) {
+		die "USB Mapping invalid (hardware probably changed): $err\n";
+	    }
+
+	    my $map_device = $map_devices->[0];
+	    my $lsusb_output = `/usr/bin/lsusb -d $map_device->{id}`;
+	    my ($bus_id, $device_id) = $lsusb_output =~ /Bus (\d+) Device (\d+)/;
+
+	    $func->($key, "dev/bus/usb/$bus_id/$device_id", @param);
+	} else {
+	    die "Either path or usbmapping has to be defined for $key";
+	}
+    }
+}
+
 # $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
 # This method replaces 'old_volid' by 'new_volid' throughout the config including snapshots, pending
 # changes, unused volumes and vmstate volumes.
-- 
2.39.2