From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 0AF2A71C7E for ; Tue, 5 Oct 2021 15:12:37 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CFF5528534 for ; Tue, 5 Oct 2021 15:12:06 +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 id B02032851F for ; Tue, 5 Oct 2021 15:12:01 +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 7D7E3456E6 for ; Tue, 5 Oct 2021 15:12:01 +0200 (CEST) From: Dominik Csapak To: pve-devel@lists.proxmox.com Date: Tue, 5 Oct 2021 15:11:59 +0200 Message-Id: <20211005131200.791836-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211005131200.791836-1-d.csapak@proxmox.com> References: <20211005131200.791836-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.309 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [pci.pm] Subject: [pve-devel] [PATCH qemu-server 2/3] pci: add helpers to (un)reserve pciids for a vm X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Oct 2021 13:12:37 -0000 saves a list of pciid <-> vmid mappings in /var/run that we can check when we start a vm Signed-off-by: Dominik Csapak --- PVE/QemuServer/PCI.pm | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/PVE/QemuServer/PCI.pm b/PVE/QemuServer/PCI.pm index 5608207..0626b76 100644 --- a/PVE/QemuServer/PCI.pm +++ b/PVE/QemuServer/PCI.pm @@ -5,6 +5,7 @@ use strict; use PVE::JSONSchema; use PVE::SysFSTools; +use PVE::Tools; use base 'Exporter'; @@ -461,4 +462,92 @@ sub print_hostpci_devices { return ($kvm_off, $gpu_passthrough, $legacy_igd); } +my $PCIID_RESERVATION_FILE = "/var/run/pve-reserved-pciids"; +my $PCIID_RESERVATION_LCK = "/var/lock/pve-reserved-pciids.lck"; + +my $parse_pci_reservation = sub { + my $pciids = {}; + + if (my $fh = IO::File->new ($PCIID_RESERVATION_FILE, "r")) { + while (my $line = <$fh>) { + if ($line =~ m/^($PCIRE)\s(\d+)\s(\d+)$/) { + $pciids->{$1} = { + timestamp => $2, + vmid => $3, + }; + } + } + } + + return $pciids; +}; + +my $write_pci_reservation = sub { + my ($pciids) = @_; + + my $data = ""; + foreach my $p (keys %$pciids) { + $data .= "$p $pciids->{$p}->{timestamp} $pciids->{$p}->{vmid}\n"; + } + + PVE::Tools::file_set_contents($PCIID_RESERVATION_FILE, $data); +}; + +sub remove_pci_reservation { + my ($id) = @_; + + my $code = sub { + my $pciids = $parse_pci_reservation->(); + + delete $pciids->{$id}; + + $write_pci_reservation->($pciids); + }; + + PVE::Tools::lock_file($PCIID_RESERVATION_LCK, 10, $code); + die $@ if $@; + + return; +} + +sub reserve_pci_usage { + my ($id, $vmid) = @_; + + my $code = sub { + + # have a 60 second grace period, since we reserve before + # we actually start the vm + my $graceperiod = 60; + my $ctime = time(); + + my $pciids = $parse_pci_reservation->(); + + if (my $pciid = $pciids->{$id}) { + if ($pciid->{vmid} == $vmid) { + return; # already reserved + } + + if (($pciid->{timestamp} + $graceperiod > $ctime) || + PVE::QemuServer::Helpers::vm_running_locally($vmid)) + { + die "PCI device '$id' already in use\n"; + } + } + + $pciids->{$id} = { + timestamp => $ctime, + vmid => $vmid, + }; + + $write_pci_reservation->($pciids); + + return; + }; + + PVE::Tools::lock_file($PCIID_RESERVATION_LCK, 10, $code); + die $@ if $@; + + return; +} + 1; -- 2.30.2