Alexander Shursha wrote:
> It is used to pass a PCI device from the host to a bhyve guest
>
> Signed-off-by: Alexander Shursha <[email protected]>
> ---
> src/bhyve/bhyve_command.c | 24 ++++++++++++++++
> .../bhyvexml2argv-passthru.args | 9 ++++++
> .../bhyvexml2argv-passthru.ldargs | 4 +++
> .../bhyvexml2argv-passthru.xml | 28 +++++++++++++++++++
> tests/bhyvexml2argvtest.c | 1 +
> 5 files changed, 66 insertions(+)
> create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args
> create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs
> create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml
>
> diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
> index bc287307c8..23bb2cdcf4 100644
> --- a/src/bhyve/bhyve_command.c
> +++ b/src/bhyve/bhyve_command.c
> @@ -2,6 +2,7 @@
> * bhyve_command.c: bhyve command generation
> *
> * Copyright (C) 2014 Roman Bogorodskiy
> + * Copyright (C) 2024-2025 Future Crew, LLC
> *
> * This library is free software; you can redistribute it and/or
> * modify it under the terms of the GNU Lesser General Public
> @@ -152,6 +153,26 @@ bhyveBuildConsoleArgStr(const virDomainDef *def,
> virCommand *cmd)
> return 0;
> }
>
> +static int
> +bhyveBuildHostdevArgStr(const virDomainDef *def, virCommand *cmd)
> +{
> + size_t i;
> +
> + for (i = 0; i < def->nhostdevs; i++) {
> + virDomainHostdevDef *hostdev = def->hostdevs[i];
> + virDomainHostdevSubsys *subsys = &hostdev->source.subsys;
> +
> + if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
> + subsys->type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
> + continue;
> + virCommandAddArg(cmd, "-s");
> + virCommandAddArgFormat(cmd, "%d,passthru,%d/%d/%d",
^^
Two things about that:
I think it's better to also specify function here (even if it's :0) for
consistency with the other options; also, we don't seem to win anything
by omitting it here.
Another thing: I think it misses the PCI address auto assignment, so
users don't have to explicitly set the address. I mean something like
that:
--- a/src/bhyve/bhyve_device.c
+++ b/src/bhyve/bhyve_device.c
@@ -184,6 +184,16 @@ bhyveAssignDevicePCISlots(virDomainDef *def,
return -1;
}
+ for (i = 0; i < def->nhostdevs; i++) {
+ if (!virDeviceInfoPCIAddressIsWanted(def->hostdevs[i]->info))
+ continue;
+ if (virDomainPCIAddressReserveNextAddr(addrs,
+ def->hostdevs[i]->info,
+ VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
+ -1) < 0)
+ return -1;
+ }
+
return 0;
}
Other than that it looks good to me.
> + hostdev->info->addr.pci.slot, subsys->u.pci.addr.bus,
> + subsys->u.pci.addr.slot, subsys->u.pci.addr.function);
> + }
> + return 0;
> +}
> +
> static int
> bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
> virDomainControllerDef *controller,
> @@ -819,6 +840,9 @@ virBhyveProcessBuildBhyveCmd(struct _bhyveConn *driver,
> virDomainDef *def,
> virCommandAddArg(cmd, bhyvecmd->args[i]);
> }
>
> + if (bhyveBuildHostdevArgStr(def, cmd) < 0)
> + return NULL;
> +
> virCommandAddArg(cmd, def->name);
>
> return g_steal_pointer(&cmd);
> diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args
> b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args
> new file mode 100644
> index 0000000000..ec17756d36
> --- /dev/null
> +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.args
> @@ -0,0 +1,9 @@
> +bhyve \
> +-c 1 \
> +-m 214 \
> +-H \
> +-P \
> +-s 0:0,hostbridge \
> +-s 2:0,ahci,hd:/tmp/freebsd.img \
> +-s 7,passthru,3/0/0 \
> +bhyve
> diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs
> b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs
> new file mode 100644
> index 0000000000..5905f4b3e6
> --- /dev/null
> +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.ldargs
> @@ -0,0 +1,4 @@
> +bhyveload \
> +-m 214 \
> +-d /tmp/freebsd.img \
> +bhyve
> diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml
> b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml
> new file mode 100644
> index 0000000000..9845587f1e
> --- /dev/null
> +++ b/tests/bhyvexml2argvdata/bhyvexml2argv-passthru.xml
> @@ -0,0 +1,28 @@
> +<domain type='bhyve'>
> + <name>bhyve</name>
> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
> + <memory unit='KiB'>219136</memory>
> + <currentMemory unit='KiB'>219136</currentMemory>
> + <vcpu placement='static'>1</vcpu>
> + <os>
> + <type>hvm</type>
> + </os>
> + <clock offset='localtime'/>
> + <on_poweroff>destroy</on_poweroff>
> + <on_reboot>destroy</on_reboot>
> + <on_crash>destroy</on_crash>
> + <devices>
> + <disk type='file'>
> + <driver name='file' type='raw'/>
> + <source file='/tmp/freebsd.img'/>
> + <target dev='hda' bus='sata'/>
> + <address type='drive' controller='0' bus='0' target='2' unit='0'/>
> + </disk>
> + <hostdev mode='subsystem' type='pci' managed='no'>
> + <source>
> + <address domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
> + </source>
> + <address type='pci' domain='0x0000' bus='0x00' slot='0x07'
> function='0x0'/>
> + </hostdev>
> + </devices>
> +</domain>
> diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c
> index cdaa32f65c..c1da8a1a17 100644
> --- a/tests/bhyvexml2argvtest.c
> +++ b/tests/bhyvexml2argvtest.c
> @@ -217,6 +217,7 @@ mymain(void)
> DO_TEST("serial-grub");
> DO_TEST("localtime");
> DO_TEST("net-e1000");
> + DO_TEST("passthru");
> DO_TEST("uefi");
> DO_TEST("vnc");
> DO_TEST("vnc-vgaconf-on");
> --
> 2.46.1