From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v5 02/13] pve-qemu-pci: legacy: generate pci bridges
Date: Wed, 18 Nov 2026 16:27:44 +0100 [thread overview]
Message-ID: <20261118152803.3843758-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261118152803.3843758-1-d.csapak@proxmox.com>
Besides the address of a single device, we also need the list of
bridges a guest has to be started with. Return them from
`pci_bus_devices`, which is intended to return all devices that form a
bus, since those must come before the devices using them on the
commandline.
The list itself follows the perl implementation:
* q35 machines already have the bridges up to 3 in their machine
config
* pci.3 is only needed for virtio-scsi-single
* pci.4 is only needed for scsi controllers with an index > 1, or
starting with machine version 11.1
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Reviewed-by: Fiona Ebner <f.ebner@proxmox.com>
---
pve-qemu-rs/pve-qemu-pci/src/layout/legacy.rs | 131 +++++++++++++++++-
pve-qemu-rs/pve-qemu-pci/src/lib.rs | 9 ++
2 files changed, 138 insertions(+), 2 deletions(-)
diff --git a/pve-qemu-rs/pve-qemu-pci/src/layout/legacy.rs b/pve-qemu-rs/pve-qemu-pci/src/layout/legacy.rs
index 5614173e..eef9bd1f 100644
--- a/pve-qemu-rs/pve-qemu-pci/src/layout/legacy.rs
+++ b/pve-qemu-rs/pve-qemu-pci/src/layout/legacy.rs
@@ -325,6 +325,43 @@ pub(crate) fn pci_bridge_for_device(id: &str) -> Option<Bus> {
Some(addr.bus)
}
+/// Renders the bridge devices a guest needs, see
+/// [`crate::pci_bus_devices`].
+pub(crate) fn pci_bus_devices(hw: &HardwareConfig) -> Result<Vec<String>, LayoutError> {
+ let mut res = Vec::new();
+
+ // some SCSI controllers can only have 7 disks each, so scsi14 and upwards
+ // need scsihw2 and above, which live on pci.4
+ let include_pci4 = hw.version_at_least(11, 1, None) || hw.scsi.max_scsihw > 1;
+
+ for i in 1..=4 {
+ // q35.cfg already includes the bridges up to three
+ if i < 4 && hw.machine.mainboard == Mainboard::Q35 {
+ continue;
+ }
+
+ if i == 3 && !hw.virtio_scsi_single() {
+ continue;
+ }
+
+ if i == 4 && !include_pci4 {
+ continue;
+ }
+
+ let id = if i == 2 && hw.legacy_igd {
+ format!("pci.{i}-igd")
+ } else {
+ format!("pci.{i}")
+ };
+
+ let addr = address(hw, &id)?.to_qemu_addr();
+ res.push("-device".to_string());
+ res.push(format!("pci-bridge,id=pci.{i},chassis_nr={i}{addr}"));
+ }
+
+ Ok(res)
+}
+
/// Returns if the caller wants a PCIE address instead of a PCI one based on the id.
fn is_pcie_id(id: &str) -> bool {
id.starts_with("hostpcie") || id == "vga" || id == "ivshmem"
@@ -357,10 +394,16 @@ pub(crate) fn address(hw: &HardwareConfig, id: &str) -> Result<PciAddress, Layou
pub(crate) mod test {
use std::collections::HashSet;
- use super::{LEGACY_PCIE_LAYOUT, get_legacy_pci_layout, pci_bridge_for_device};
+ use pve_api_types::QemuConfigScsihw;
+
+ use super::{
+ LEGACY_PCIE_LAYOUT, get_legacy_pci_layout, pci_bridge_for_device, pci_bus_devices,
+ };
use crate::Mainboard;
+ use crate::hardware_config::ScsiOptions;
use crate::layout::legacy::is_pcie_id;
- use crate::{Bus, Device, HardwareConfig, Layout, PciAddress};
+ use crate::machine::MachineVersion;
+ use crate::{Bus, Device, HardwareConfig, HardwareConfigParams, Layout, PciAddress};
pub const LEGACY_ADDRS: [(&str, u8, u8); 121] = [
("ahci0", 0, 7),
@@ -628,4 +671,88 @@ pub(crate) mod test {
assert_eq!(pci_bridge_for_device("no-such-device"), None);
}
+
+ fn hw_config(
+ q35: bool,
+ scsihw: QemuConfigScsihw,
+ max_scsihw: u8,
+ legacy_igd: bool,
+ ) -> HardwareConfig {
+ let params = HardwareConfigParams {
+ arch: crate::hardware_config::Arch::X8664,
+ machine: crate::Machine {
+ mainboard: if q35 {
+ Mainboard::Q35
+ } else {
+ Mainboard::I440fx
+ },
+ version: MachineVersion::new(9, 2, None),
+ },
+ scsi: ScsiOptions {
+ hw: scsihw,
+ max_scsihw,
+ },
+ legacy_igd,
+ ..Default::default()
+ };
+ HardwareConfig::new(params)
+ }
+
+ fn bridge(nr: u8, addr: &str) -> [String; 2] {
+ [
+ "-device".to_string(),
+ format!("pci-bridge,id=pci.{nr},chassis_nr={nr}{addr}"),
+ ]
+ }
+
+ #[test]
+ fn test_pci_bus_devices() {
+ let pci1 = bridge(1, ",bus=pci.0,addr=0x1e");
+ let pci2 = bridge(2, ",bus=pci.0,addr=0x1f");
+ let pci3 = bridge(3, ",bus=pci.0,addr=0x5");
+ let pci4 = bridge(4, ",bus=pci.1,addr=0x1c");
+
+ let test = |hw: HardwareConfig, expected: &[&[String; 2]]| {
+ let expected: Vec<String> = expected.iter().flat_map(|b| b.iter().cloned()).collect();
+ assert_eq!(pci_bus_devices(&hw), Ok(expected));
+ };
+
+ test(
+ hw_config(false, QemuConfigScsihw::Lsi, 0, false),
+ &[&pci1, &pci2],
+ );
+
+ // q35 already brings the bridges up to three along
+ test(hw_config(true, QemuConfigScsihw::Lsi, 0, false), &[]);
+
+ // every SCSI disk gets its own controller on pci.3
+ test(
+ hw_config(false, QemuConfigScsihw::VirtioScsiSingle, 0, false),
+ &[&pci1, &pci2, &pci3],
+ );
+
+ // scsihw2 and above live on pci.4
+ test(
+ hw_config(false, QemuConfigScsihw::Lsi, 1, false),
+ &[&pci1, &pci2],
+ );
+ test(
+ hw_config(false, QemuConfigScsihw::Lsi, 2, false),
+ &[&pci1, &pci2, &pci4],
+ );
+ test(hw_config(true, QemuConfigScsihw::Lsi, 2, false), &[&pci4]);
+
+ // pci.4 is always added from machine version 11.1 on
+ let mut new_machine = hw_config(false, QemuConfigScsihw::Lsi, 0, false);
+ new_machine.machine.version = MachineVersion::new(11, 1, None);
+ test(new_machine, &[&pci1, &pci2, &pci4]);
+
+ // with legacy IGD passthrough pci.2 moves behind pci.1
+ let mut expected_igd = pci2.clone();
+ expected_igd[1] = "pci-bridge,id=pci.2,chassis_nr=2,bus=pci.1,addr=0x1e".to_string();
+ test(
+ hw_config(false, QemuConfigScsihw::Lsi, 0, true),
+ &[&pci1, &expected_igd],
+ );
+ }
}
diff --git a/pve-qemu-rs/pve-qemu-pci/src/lib.rs b/pve-qemu-rs/pve-qemu-pci/src/lib.rs
index 77a17c11..b2a39c96 100644
--- a/pve-qemu-rs/pve-qemu-pci/src/lib.rs
+++ b/pve-qemu-rs/pve-qemu-pci/src/lib.rs
@@ -27,6 +27,15 @@ pub fn pci_bridge_for_device(id: &str) -> Option<Bus> {
layout::legacy::pci_bridge_for_device(id)
}
+/// Returns the QEMU arguments for all devices that form the buses `hw`
+/// needs, as alternating `-device` flags and their values.
+///
+/// They are ordered so that every bus is created before the devices on it,
+/// and have to be passed before any other device that sits on these buses.
+pub fn pci_bus_devices(hw: &HardwareConfig) -> Result<Vec<String>, LayoutError> {
+ layout::legacy::pci_bus_devices(hw)
+}
+
/// Returns the address of the device with the configuration id `id`.
pub fn address(hw: &HardwareConfig, id: &str) -> Result<PciAddress, LayoutError> {
layout::legacy::address(hw, id)
--
2.47.3
next prev parent reply other threads:[~2026-11-18 15:28 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 15:27 [PATCH manager/qemu-server v5 00/13] pci-handling rewrite Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 01/13] pve-qemu-rs: add pve-qemu-pci crate for guest PCI address generation Dominik Csapak
2026-11-18 15:27 ` Dominik Csapak [this message]
2026-11-18 15:27 ` [PATCH qemu-server v5 03/13] pve-qemu-pci: legacy: generate q35 chipset devices and hostpcie root ports Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 04/13] pve-qemu-rs: build perlmod library with bindings for pve-qemu-pci Dominik Csapak
2026-11-19 12:47 ` Elias Huhsovitz
2026-11-19 14:51 ` Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 05/13] pci: use PVE::QemuServer::RS::PCI bindings for addresses and bus devices Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 06/13] tree-wide: use HardwareConfig for the machine version checks Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 07/13] cfg2cmd: q35: generate chipset devices with machine version 11.1+pve1 Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 08/13] pve-qemu-pci: layout: add grouped PCI and PCIe layouts Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 09/13] machine: add 'pci-layout' option Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 10/13] pve-qemu-pci: bus: add buses for numa expander bridges and pcie switches Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 11/13] pve-qemu-pci: topology: place hostpcie devices like on the host Dominik Csapak
2026-11-19 13:43 ` Elias Huhsovitz
2026-11-19 15:10 ` Dominik Csapak
2026-11-18 15:27 ` [PATCH qemu-server v5 12/13] machine: add 'pci-topology' option Dominik Csapak
2026-11-19 12:42 ` Elias Huhsovitz
2026-11-19 15:12 ` Dominik Csapak
2026-11-18 15:27 ` [PATCH pve-manager v5 13/13] ui: qemu machine: don't lose pci options on edit cycle Dominik Csapak
2026-11-19 12:18 ` [PATCH manager/qemu-server v5 00/13] pci-handling rewrite Elias Huhsovitz
2026-11-19 12:33 ` Dominik Csapak
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=20261118152803.3843758-3-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.