From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id ED8711FF0C0 for ; Wed, 18 Nov 2026 16:28:20 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id F1D9921783; Wed, 18 Nov 2026 16:28:18 +0100 (CET) From: Dominik Csapak To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v5 04/13] pve-qemu-rs: build perlmod library with bindings for pve-qemu-pci Date: Wed, 18 Nov 2026 16:27:46 +0100 Message-ID: <20261118152803.3843758-5-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118152803.3843758-1-d.csapak@proxmox.com> References: <20261118152803.3843758-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.009 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SCC_5_SHORT_WORD_LINES 0.25 5 lines with many short words SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: QP662HFQRLFWA3OHFT53UD65XHXUT3QJ X-Message-ID-Hash: QP662HFQRLFWA3OHFT53UD65XHXUT3QJ X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: These provide bindings for using the Rust PCI code in the Perl code of qemu-server. This exposes 'PVE::QemuServer::RS::PCI::HardwareConfig', which provides an object that can be used to query the PCI addresses of QEMU VMs. The library is built from the new 'pve-qemu-perl' crate and shipped with the qemu-server package, so the Rust code and its Perl users can change together, without going through libpve-rs-perl. It uses its own library tag, since libraries with the same tag share their loading state, which Proxmox::Lib::Common uses to find the library with the common bindings of libpve-rs-perl. The generated modules and the library are placed in pve-qemu-rs/build/perl, laid out like in @INC, so that the tests and the documentation generation can use them without installing them. Signed-off-by: Dominik Csapak --- Makefile | 3 + debian/control | 13 ++ debian/rules | 17 +++ pve-qemu-rs/.cargo/config.toml | 2 + pve-qemu-rs/.gitignore | 1 + pve-qemu-rs/Cargo.toml | 6 + pve-qemu-rs/Makefile | 57 ++++++++- pve-qemu-rs/pve-qemu-perl/Cargo.toml | 19 +++ pve-qemu-rs/pve-qemu-perl/src/lib.rs | 14 +++ .../pve-qemu-perl/src/pci/hardware_config.rs | 117 ++++++++++++++++++ pve-qemu-rs/pve-qemu-perl/src/pci/mod.rs | 4 + src/Makefile | 11 +- src/bin/Makefile | 15 ++- src/test/Makefile | 3 + 14 files changed, 273 insertions(+), 9 deletions(-) create mode 100644 pve-qemu-rs/pve-qemu-perl/Cargo.toml create mode 100644 pve-qemu-rs/pve-qemu-perl/src/lib.rs create mode 100644 pve-qemu-rs/pve-qemu-perl/src/pci/hardware_config.rs create mode 100644 pve-qemu-rs/pve-qemu-perl/src/pci/mod.rs diff --git a/Makefile b/Makefile index a659523e..25d0f0a9 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,9 @@ $(BUILDDIR): rm -rf $@ $@.tmp cp -a src $@.tmp cp -a debian $@.tmp/ + mkdir $@.tmp/pve-qemu-rs + cp -a -t $@.tmp/pve-qemu-rs pve-qemu-rs/.cargo pve-qemu-rs/Cargo.toml pve-qemu-rs/Makefile \ + pve-qemu-rs/rustfmt.toml pve-qemu-rs/pve-qemu-*/ echo "git clone git://git.proxmox.com/git/qemu-server.git\\ngit checkout $(GITVERSION)" > $@.tmp/debian/SOURCE mv $@.tmp $@ diff --git a/debian/control b/debian/control index 2b816e5e..eddc8b47 100644 --- a/debian/control +++ b/debian/control @@ -3,6 +3,8 @@ Section: admin Priority: optional Maintainer: Proxmox Support Team Build-Depends: debhelper-compat (= 13), + cargo:native, + dh-cargo (>= 25), libclass-methodmaker-perl, libglib2.0-dev, libio-multiplex-perl, @@ -15,11 +17,21 @@ Build-Depends: debhelper-compat (= 13), libpve-network-perl, libpve-rs-perl (>= 0.14.0), libpve-storage-perl (>= 9.1.17), + librust-anyhow-1+default-dev, + librust-perlmod-0.14+default-dev, + librust-perlmod-0.14+exporter-dev, + librust-pve-api-types-8+default-dev (>= 8.1-~~), + librust-serde-1+default-dev, + librust-serde-1+derive-dev, + librust-strum-0.26+default-dev, + librust-strum-0.26+derive-dev, + libstd-rust-dev, libtest-mockmodule-perl, liburi-perl, libuuid-perl, lintian, perl, + perlmod-bin, pkgconf, pve-cluster, pve-doc-generator (>= 6.2-5), @@ -27,6 +39,7 @@ Build-Depends: debhelper-compat (= 13), pve-firewall, pve-ha-manager , pve-qemu-kvm (>= 11.1~), + rustc:native (>= 1.94), Standards-Version: 4.5.1 Homepage: https://www.proxmox.com diff --git a/debian/rules b/debian/rules index 955dd78b..32f86397 100755 --- a/debian/rules +++ b/debian/rules @@ -9,5 +9,22 @@ # Uncomment this to turn on verbose mode. #export DH_VERBOSE=1 +include /usr/share/dpkg/pkg-info.mk +include /usr/share/rustc/architecture.mk + +export BUILD_MODE=release + +export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS +export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE +export CARGO_HOME = $(CURDIR)/debian/cargo_home +export DEB_CARGO_CRATE=pve-qemu-perl_$(DEB_VERSION_UPSTREAM) + +# the build directory has the contents of src/ at its root, see the top-level Makefile +export PVE_QEMU_RS = $(CURDIR)/pve-qemu-rs + %: dh $@ + +override_dh_auto_configure: + /usr/share/cargo/bin/cargo prepare-debian $(CURDIR)/debian/cargo_registry --link-from-system + dh_auto_configure diff --git a/pve-qemu-rs/.cargo/config.toml b/pve-qemu-rs/.cargo/config.toml index 3b5b6e48..1eebffbd 100644 --- a/pve-qemu-rs/.cargo/config.toml +++ b/pve-qemu-rs/.cargo/config.toml @@ -3,3 +3,5 @@ directory = "/usr/share/cargo/registry" [source.crates-io] replace-with = "debian-packages" +[profile.release] +debug = true diff --git a/pve-qemu-rs/.gitignore b/pve-qemu-rs/.gitignore index a18277be..d74c6b6d 100644 --- a/pve-qemu-rs/.gitignore +++ b/pve-qemu-rs/.gitignore @@ -1,3 +1,4 @@ /target Cargo.lock **/*.rs.bk +/build diff --git a/pve-qemu-rs/Cargo.toml b/pve-qemu-rs/Cargo.toml index b64d5c48..326fa505 100644 --- a/pve-qemu-rs/Cargo.toml +++ b/pve-qemu-rs/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "pve-qemu-pci", + "pve-qemu-perl", ] resolver = "3" @@ -16,8 +17,13 @@ rust-version = "1.94" # any features enabled here are enabled on all members using 'workspace = true'! # external dependencies +anyhow = "1.0" +perlmod = "0.14" serde = "1.0" strum = { version = "0.26", features = ["derive"] } # proxmox dependencies pve-api-types = "8.1" + +# local crates +pve-qemu-pci = { path = "pve-qemu-pci" } diff --git a/pve-qemu-rs/Makefile b/pve-qemu-rs/Makefile index 49055573..b4205723 100644 --- a/pve-qemu-rs/Makefile +++ b/pve-qemu-rs/Makefile @@ -1,10 +1,62 @@ CARGO ?= cargo -all: +PERL_INSTALLVENDORARCH != perl -MConfig -e 'print $$Config{installvendorarch};' +PERL_INSTALLVENDORLIB != perl -MConfig -e 'print $$Config{installvendorlib};' + +ifeq ($(BUILD_MODE), release) +CARGO_BUILD_ARGS += --release +TARGET_DIR=release +else +TARGET_DIR=debug +endif + +LIBNAME := pve_qemu_perl +LIB := target/$(TARGET_DIR)/lib$(LIBNAME).so + +# The generated modules and the library, laid out like in @INC, so that the code in src/ can be +# used without installing them. +PERL_BUILDDIR := build/perl + +PERLMOD_GENPACKAGE ?= /usr/lib/perlmod/genpackage.pl + +# Libraries with the same tag share their loading state, so this must not use the 'proxmox' tag +# of libpve-rs-perl. +PERLMOD_GENPACKAGE_ARGS := \ + --lib=$(LIBNAME) \ + --lib-tag=pve-qemu \ + --lib-package=PVE::QemuServer::RS::Lib \ + --lib-prefix=PVE::QemuServer + +PERLMOD_PACKAGES := \ + PVE::QemuServer::RS::PCI::HardwareConfig + +PERLMOD_PACKAGE_FILES := \ + PVE/QemuServer/RS/Lib.pm \ + $(addsuffix .pm,$(subst ::,/,$(PERLMOD_PACKAGES))) + +.PHONY: all +all: $(addprefix $(PERL_BUILDDIR)/,$(PERLMOD_PACKAGE_FILES)) cargo-build + install -D -m644 $(LIB) $(PERL_BUILDDIR)/auto/lib$(LIBNAME).so + +.PHONY: cargo-build +cargo-build: + $(CARGO) build $(CARGO_BUILD_ARGS) + +$(addprefix $(PERL_BUILDDIR)/,$(PERLMOD_PACKAGE_FILES)) &: Makefile + rm -rf $(PERL_BUILDDIR)/PVE + mkdir -p $(PERL_BUILDDIR) + cd $(PERL_BUILDDIR) && $(PERLMOD_GENPACKAGE) $(PERLMOD_GENPACKAGE_ARGS) $(PERLMOD_PACKAGES) + +.PHONY: install +install: all + install -D -m644 $(LIB) $(DESTDIR)$(PERL_INSTALLVENDORARCH)/auto/lib$(LIBNAME).so + for f in $(PERLMOD_PACKAGE_FILES); do \ + install -D -m644 $(PERL_BUILDDIR)/$$f $(DESTDIR)$(PERL_INSTALLVENDORLIB)/$$f; \ + done .PHONY: test test: - $(CARGO) test + $(CARGO) test $(CARGO_BUILD_ARGS) # Prints a diff between the current code and the one rustfmt would produce .PHONY: fmt @@ -14,3 +66,4 @@ fmt: .PHONY: clean clean: $(CARGO) clean + rm -rf build diff --git a/pve-qemu-rs/pve-qemu-perl/Cargo.toml b/pve-qemu-rs/pve-qemu-perl/Cargo.toml new file mode 100644 index 00000000..48dc8e93 --- /dev/null +++ b/pve-qemu-rs/pve-qemu-perl/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "pve-qemu-perl" +version = "0.1.0" +description = "Perl bindings for the Rust parts of qemu-server" +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +rust-version.workspace = true + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyhow.workspace = true +perlmod = { workspace = true, features = ["exporter"] } + +pve-qemu-pci.workspace = true diff --git a/pve-qemu-rs/pve-qemu-perl/src/lib.rs b/pve-qemu-rs/pve-qemu-perl/src/lib.rs new file mode 100644 index 00000000..be545ef8 --- /dev/null +++ b/pve-qemu-rs/pve-qemu-perl/src/lib.rs @@ -0,0 +1,14 @@ +//! Perl bindings for the Rust parts of qemu-server. +//! +//! The packages are available below `PVE::QemuServer::RS`. + +#![deny(missing_docs)] + +pub mod pci; + +#[perlmod::package(name = "PVE::QemuServer::RS::Lib", lib = "pve_qemu_perl")] +pub mod pve_qemu_rs_lib { + //! The `PVE::QemuServer::RS::Lib` package. + //! + //! The base package of all other packages, which loads the library. +} diff --git a/pve-qemu-rs/pve-qemu-perl/src/pci/hardware_config.rs b/pve-qemu-rs/pve-qemu-perl/src/pci/hardware_config.rs new file mode 100644 index 00000000..53bb41bb --- /dev/null +++ b/pve-qemu-rs/pve-qemu-perl/src/pci/hardware_config.rs @@ -0,0 +1,117 @@ +#[perlmod::package( + name = "PVE::QemuServer::RS::PCI::HardwareConfig", + lib = "pve_qemu_perl" +)] +pub mod pve_qemu_rs_pci_hardware_config { + //! The `PVE::QemuServer::RS::PCI::HardwareConfig` package. + //! + //! Provides bindings for the `HardwareConfig` struct of the `pve-qemu-pci` crate. + + use std::ops::Deref; + use std::rc::Rc; + + use anyhow::Error; + + use perlmod::Value; + + use pve_qemu_pci::{HardwareConfigParams, LayoutError, address}; + + perlmod::declare_magic!(Box: &HardwareConfig as "PVE::QemuServer::RS::PCI::HardwareConfig"); + + /// Wrapper for [`pve_qemu_pci::HardwareConfig`]. + pub struct HardwareConfig { + inner: Rc, + } + + impl Deref for HardwareConfig { + type Target = pve_qemu_pci::HardwareConfig; + + fn deref(&self) -> &Self::Target { + &self.inner + } + } + + #[export(raw_return)] + /// Class method: Create an instance of the `HardwareConfig` struct. + pub fn new(#[raw] class: Value, params: HardwareConfigParams) -> Result { + let hw_config = pve_qemu_pci::HardwareConfig::new(params); + Ok( + perlmod::instantiate_magic!(&class, MAGIC => Box::new(HardwareConfig { inner: Rc::new(hw_config) })), + ) + } + + #[export] + /// Method: Returns the PCI or PCIe address of the chosen device on the given machine if the + /// device exists on the layout. + pub fn addr( + #[try_from_ref] this: &HardwareConfig, + id: &str, + ) -> Result, LayoutError> { + match address(this, id) { + Ok(addr) => Ok(Some(addr.to_qemu_addr())), + Err(LayoutError::NoAddressFound(_)) => Ok(None), + Err(other) => Err(other), + } + } + + #[export] + /// Method: Get the `-device` arguments of all bridges, root ports and chipset devices forming + /// the buses of the guest, which have to come before any device on them. + pub fn pci_bus_devices( + #[try_from_ref] this: &HardwareConfig, + ) -> Result, LayoutError> { + pve_qemu_pci::pci_bus_devices(this) + } + + #[export] + /// Method: Whether the q35 chipset devices still have to be loaded from `pve-q35-4.0.cfg`. + pub fn uses_q35_config_file(#[try_from_ref] this: &HardwareConfig) -> bool { + this.uses_q35_config_file() + } + + #[export] + /// Method: Get the architecture of the machine + pub fn arch(#[try_from_ref] this: &HardwareConfig) -> String { + this.arch().to_string() + } + + #[export] + /// Method: Get the machines mainboard model + pub fn machine_type(#[try_from_ref] this: &HardwareConfig) -> String { + this.machine.mainboard.to_string() + } + + #[export] + /// Method: Checks if the given machine is at least the given version + /// Note: records the necessary PVE revision. + pub fn version_at_least( + #[try_from_ref] this: &HardwareConfig, + major: u16, + minor: u16, + pve: Option, + ) -> bool { + this.version_at_least(major, minor, pve) + } + + #[export] + /// Method: Returns the windows version (if any) + pub fn windows_version(#[try_from_ref] this: &HardwareConfig) -> u8 { + this.windows_version() + } + + #[export] + /// Method: Returns the bus the given device sits on, if it is a known device. + pub fn pci_bridge_for_device( + #[try_from_ref] _this: &HardwareConfig, + device: &str, + ) -> Option { + let bus = pve_qemu_pci::pci_bridge_for_device(device)?; + Some(bus.to_string()) + } + + #[export] + /// Method: Returns the required '+pveX' version of the machine. + pub fn required_pve_version(#[try_from_ref] this: &HardwareConfig) -> u16 { + this.required_pve_version() + } +} diff --git a/pve-qemu-rs/pve-qemu-perl/src/pci/mod.rs b/pve-qemu-rs/pve-qemu-perl/src/pci/mod.rs new file mode 100644 index 00000000..1d2214a6 --- /dev/null +++ b/pve-qemu-rs/pve-qemu-perl/src/pci/mod.rs @@ -0,0 +1,4 @@ +//! Bindings for the `pve-qemu-pci` crate. + +mod hardware_config; +pub use hardware_config::pve_qemu_rs_pci_hardware_config; diff --git a/src/Makefile b/src/Makefile index f8e9a2d9..b0441252 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,14 @@ +# The Rust workspace is next to src/ in git, the package build copies it into the build directory. +PVE_QEMU_RS ?= $(abspath ../pve-qemu-rs) +export PVE_QEMU_RS + +.PHONY: all all: + $(MAKE) -C $(PVE_QEMU_RS) .PHONY: install install: + $(MAKE) -C $(PVE_QEMU_RS) install $(MAKE) -C PVE install $(MAKE) -C bin install $(MAKE) -C qmeventd install @@ -9,12 +16,14 @@ install: $(MAKE) -C usr install .PHONY: test -test: +test: all + $(MAKE) -C $(PVE_QEMU_RS) test $(MAKE) -C test $(MAKE) -C bin $@ .PHONY: clean clean: + $(MAKE) -C $(PVE_QEMU_RS) clean $(MAKE) -C test $@ $(MAKE) -C bin $@ diff --git a/src/bin/Makefile b/src/bin/Makefile index 72763873..8e7aa309 100644 --- a/src/bin/Makefile +++ b/src/bin/Makefile @@ -10,26 +10,29 @@ MAN5DIR=$(MANDIR)/man5/ BASHCOMPLDIR=$(PREFIX)/share/bash-completion/completions/ ZSHCOMPLDIR=$(PREFIX)/share/zsh/vendor-completions/ -PERL_DOC_INC_DIRS=.. +PVE_QEMU_RS ?= $(abspath ../../pve-qemu-rs) +PVE_QEMU_RS_PERL = $(PVE_QEMU_RS)/build/perl + +PERL_DOC_INC_DIRS=.. $(PVE_QEMU_RS_PERL) -include /usr/share/pve-doc-generator/pve-doc-generator.mk all: qm.bash-completion: - PVE_GENERATING_DOCS=1 perl -I.. -T -e "use PVE::CLI::qm; PVE::CLI::qm->generate_bash_completions();" >$@.tmp + PVE_GENERATING_DOCS=1 perl -I.. -I$(PVE_QEMU_RS_PERL) -T -e "use PVE::CLI::qm; PVE::CLI::qm->generate_bash_completions();" >$@.tmp mv $@.tmp $@ qmrestore.bash-completion: - PVE_GENERATING_DOCS=1 perl -I.. -T -e "use PVE::CLI::qmrestore; PVE::CLI::qmrestore->generate_bash_completions();" >$@.tmp + PVE_GENERATING_DOCS=1 perl -I.. -I$(PVE_QEMU_RS_PERL) -T -e "use PVE::CLI::qmrestore; PVE::CLI::qmrestore->generate_bash_completions();" >$@.tmp mv $@.tmp $@ qm.zsh-completion: - PVE_GENERATING_DOCS=1 perl -I.. -T -e "use PVE::CLI::qm; PVE::CLI::qm->generate_zsh_completions();" >$@.tmp + PVE_GENERATING_DOCS=1 perl -I.. -I$(PVE_QEMU_RS_PERL) -T -e "use PVE::CLI::qm; PVE::CLI::qm->generate_zsh_completions();" >$@.tmp mv $@.tmp $@ qmrestore.zsh-completion: - PVE_GENERATING_DOCS=1 perl -I.. -T -e "use PVE::CLI::qmrestore; PVE::CLI::qmrestore->generate_zsh_completions();" >$@.tmp + PVE_GENERATING_DOCS=1 perl -I.. -I$(PVE_QEMU_RS_PERL) -T -e "use PVE::CLI::qmrestore; PVE::CLI::qmrestore->generate_zsh_completions();" >$@.tmp mv $@.tmp $@ PKGSOURCES=qm qm.1 qmrestore qmrestore.1 qmextract qm.conf.5 qm.bash-completion qmrestore.bash-completion \ @@ -57,7 +60,7 @@ install: $(PKGSOURCES) .PHONY: test test: - PVE_GENERATING_DOCS=1 perl -I.. ./qm verifyapi + PVE_GENERATING_DOCS=1 perl -I.. -I$(PVE_QEMU_RS_PERL) ./qm verifyapi .PHONY: clean clean: diff --git a/src/test/Makefile b/src/test/Makefile index fa14908e..825b3afa 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,3 +1,6 @@ +PVE_QEMU_RS ?= $(abspath ../../pve-qemu-rs) +export PERL5LIB := $(PVE_QEMU_RS)/build/perl$(if $(PERL5LIB),:$(PERL5LIB)) + all: test test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump -- 2.47.3