Skip to contentSkip to conversation

xl: Allow PCI devices to be passthrough'd via Qemu command line

RFC PATCH Superseded Thierry Escande 2 participants 6 messages Apr 9 – Apr 10

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover xl: Allow PCI devices to be passthrough'd via Qemu command line

This series adds support for a new 'hotplug' option for PCI devices to be passthrough'd to guests.

The current mechanism for device passthrough uses QMP device_add command to hot-plug PCI devices to the guest Qemu instance. This is an issue for guests running on Q35 chipset (preliminary support posted at [1]) since the Q35 PCI root bus does not support hotplug. Devices could be hotplugged to a secondary PCI bus but Xen only support 1 PCI bus for now.

The 'hotplug' option allows to control how devices are attached to the guest, either by using the legacy QMP mechanism (this is the default) or by passing it directly to the Qemu command line using xen-pci-passthrough device.

Example usage in cfg file:
pci = [ "00:03.0,seize=1,hotplug=0" ]

Since Qemu -device option accepts parameters in json format, this patchset adds a new internal function libxl__device_pci_get_qmp_json() that generates the device json object used for both QMP hotplug and command line passing.
Also, the function libxl_pci_assignable() is made available from libxl.h for access in libxl_dm.c. I don't know if it's ok to make it public or if it should stay private. Maybe it should be renamed as libxl_device_pci_assignable() to match the other PCI device APIs.

This has been successfully tested on Xen 4.22-dev and Qemu 10.0 running Debian VMs in both Bios and UEFI mode with a passthrough'd nvme disk. Disabling the hotplug mechanism might be made mandatory for Q35 machines later, once Q35 support is merged upstream.

[1] https://lore.kernel.org/xen-devel/[email protected]/

Changes in v2:
- Add support for YAJL json parser

Thierry Escande (3):
xl: Add pci device hotplug option
libxl: Allow PCI device passthrough using -device Qemu command line
docs: provide description for pci hotplug option

 docs/man/xl-pci-configuration.5.pod | 17 ++++++
 docs/man/xl.cfg.5.pod.in            |  6 +++
 tools/include/libxl.h               |  1 +
 tools/libs/light/libxl_dm.c         | 80 +++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h   |  2 +
 tools/libs/light/libxl_pci.c        | 41 +++++++++++----
 tools/libs/light/libxl_types.idl    |  1 +
 tools/libs/util/libxlu_pci.c        |  2 +
 tools/xl/xl_parse.c                 |  5 ++
 9 files changed, 145 insertions(+), 10 deletions(-)
Signature · 10 lines
2.53.0



--
Thierry Escande | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

RFC PATCH v2 1/3 xl: Add pci device hotplug option

This option is used to passthrough PCI devices using the Qemu command line (-device driver,...) instead of the QMP hot-plug mechanism.

This is needed for Q35 support since its PCI root bus doesn't support hotplugging.

If not specified, the default behavior is to use hotplug via QMP.

---
v2: no change

---
 tools/libs/light/libxl_types.idl | 1 +
 tools/libs/util/libxlu_pci.c     | 2 ++
 tools/xl/xl_parse.c              | 5 +++++
 3 files changed, 8 insertions(+)
tools/libs/light/libxl_types.idl +1 -0
index a7893460f0..aca0e93793 100644
@@ -922,6 +922,7 @@ libxl_device_pci = Struct("device_pci", [
     ("seize", bool),
     ("rdm_policy", libxl_rdm_reserve_policy),
     ("name", string),
+    ("hotplug", bool),
     ])
 
 libxl_device_rdm = Struct("device_rdm", [
tools/libs/util/libxlu_pci.c +2 -0
index 294482c6d7..f6440c878b 100644
@@ -192,6 +192,8 @@ int xlu_pci_parse_spec_string(XLU_Config *cfg, libxl_device_pci *pci,
             name_present = true;
             pci->name = strdup(val);
             if (!pci->name) ret = ERROR_NOMEM;
+        } else if (!strcmp(key, "hotplug")) {
+            pci->hotplug = atoi(val);
         } else {
             XLU__PCI_ERR(cfg, "Unknown PCI_SPEC_STRING option: %s", key);
             ret = ERROR_INVAL;
tools/xl/xl_parse.c +5 -0
index 48c72dce9c..7ea2a76662 100644
@@ -1361,6 +1361,7 @@ void parse_config_data(const char *config_source,
     int pci_msitranslate = 0;
     int pci_permissive = 0;
     int pci_seize = 0;
+    int pci_hotplug = 1;
     int i, e;
     int num_llc_colors;
     int num_xs_quota;
@@ -1699,6 +1700,9 @@ void parse_config_data(const char *config_source,
     if (!xlu_cfg_get_long (config, "pci_seize", &l, 0))
         pci_seize = l;
 
+    if (!xlu_cfg_get_long (config, "pci_hotplug", &l, 0))
+        pci_hotplug = l;
+
     if (!xlu_cfg_get_string(config, "rdm", &buf, 0)) {
         libxl_rdm_reserve rdm;
         if (!xlu_rdm_parse(config, &rdm, buf)) {
@@ -1720,6 +1724,7 @@ void parse_config_data(const char *config_source,
             pci->power_mgmt = pci_power_mgmt;
             pci->permissive = pci_permissive;
             pci->seize = pci_seize;
+            pci->hotplug = pci_hotplug;
             /*
              * Like other pci option, the per-device policy always follows
              * the global policy by default.
Signature · 10 lines
2.53.0



--
Thierry Escande | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#2 lore raw

RFC PATCH v2 2/3 libxl: Allow PCI device passthrough using -device Qemu command line

In reply to Thierry Escande

This change makes use of the new option 'hotplug' for host PCI devices passthrough'd to the guest. If hotplug=0 is used in the pci device configuration table, the device will be attached to the guest using the Qemu command line as '-device xen-pci-passthrough,hostaddr=...'

The host device configuration is passed to the -device option as a json array, just like it's done for hotplug using QMP. The json array is created by a new internal function libxl__device_pci_get_qmp_json() that is also used by pci_add_qmp_device_add().

Then, instead of sending the 'device_add' command, the device_add callback is called to perform the 'query-pci' check to make sure the passthrough'd device is present.

In the same way at shutdown, the device is not removed using QMP and only the pci_remove_done() function is called.

As with QMP, the use of the 'hotplug=0' option honors the 'seize' option by adding the PCI device to the assignable list if needed.

Example use:
pci = [ "00:03.0,seize=1,hotplug=0" ]

---
v2:
- Add support for YAJL json parser

---
 tools/include/libxl.h             |  1 +
 tools/libs/light/libxl_dm.c       | 80 +++++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h |  2 +
 tools/libs/light/libxl_pci.c      | 41 ++++++++++++----
 4 files changed, 114 insertions(+), 10 deletions(-)
tools/include/libxl.h +1 -0
index 7c098edab6..66fb07ad67 100644
@@ -2666,6 +2666,7 @@ int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_device_pci *pci, int r
 int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_device_pci *pci, int rebind);
 libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num);
 void libxl_device_pci_assignable_list_free(libxl_device_pci *list, int num);
+bool libxl_pci_assignable(libxl_ctx *ctx, libxl_device_pci *pci);
 
 /* CPUID handling */
 int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str);
tools/libs/light/libxl_dm.c +80 -0

86 lines not on this page · raw message

tools/libs/light/libxl_internal.h +2 -0
index b65e0064b9..06b5a14409 100644
@@ -1729,6 +1729,8 @@ _hidden int libxl__device_pci_setdefault(libxl__gc *gc, uint32_t domid,
                                          libxl_device_pci *pci, bool hotplug);
 _hidden bool libxl__is_igd_vga_passthru(libxl__gc *gc,
                                         const libxl_domain_config *d_config);
+_hidden libxl__json_object *libxl__device_pci_get_qmp_json(libxl__gc *gc,
+                                                         libxl_device_pci *pci);
 
 /* from libxl_dtdev */
 
tools/libs/light/libxl_pci.c +31 -10

71 lines not on this page · raw message

Signature · 10 lines
2.53.0



--
Thierry Escande | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#3 lore raw

RFC PATCH v2 3/3 docs: provide description for pci hotplug option

In reply to Thierry Escande

This patch adds description for the 'hotplug' PCI option, allowing devices to be attached to the guest using the legacy QMP hotplug mechanism or by passing them to the Qemu command line using '-device'.

---
v2: no change

---
 docs/man/xl-pci-configuration.5.pod | 17 +++++++++++++++++
 docs/man/xl.cfg.5.pod.in            |  6 ++++++
 2 files changed, 23 insertions(+)
docs/man/xl-pci-configuration.5.pod +17 -0
index 0691f06ad3..8b664173cb 100644
@@ -166,6 +166,23 @@ dom0 without confirmation.  Please use with care.
 
 =back
 
+=item B<hotplug>=I<BOOLEAN>
+
+=over 4
+
+=item Description
+
+Tells L<xl(1)> to use QMP hotplug mechanism to attach assignable device to
+the guest, or to pass it via Qemu command line using
+'B<-device xen-pci-passthrough,...>'. The default is to use the legacy QMP
+mechanism.
+
+=item Default Value
+
+1
+
+=back
+
 =item B<power_mgmt>=I<BOOLEAN>
 
 =over 4
docs/man/xl.cfg.5.pod.in +6 -0
index 3aac0bc4fb..181aaffa0f 100644
@@ -1230,6 +1230,12 @@ B<(HVM only)> Changes the default value of B<power_mgmt> for all PCI
 devices passed through to this VM. See B<power_mgmt>
 above.
 
+=item B<pci_hotplug=BOOLEAN>
+
+B<(HVM only)> Changes the default value of B<hotplug> for all PCI
+devices passed through to this VM. See L<xl-pci-configuration(5)> for
+more details.
+
 =item B<gfx_passthru=BOOLEAN|"STRING">
 
 Enable graphics device PCI passthrough. This option makes an assigned
Signature · 10 lines
2.53.0



--
Thierry Escande | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#4 lore raw
In reply to Thierry Escande
Quoted 26 lines · Thierry Escande

This series adds support for a new 'hotplug' option for PCI devices to be passthrough'd to guests.

The current mechanism for device passthrough uses QMP device_add command to hot-plug PCI devices to the guest Qemu instance. This is an issue for guests running on Q35 chipset (preliminary support posted at [1]) since the Q35 PCI root bus does not support hotplug. Devices could be hotplugged to a secondary PCI bus but Xen only support 1 PCI bus for now.

The 'hotplug' option allows to control how devices are attached to the guest, either by using the legacy QMP mechanism (this is the default) or by passing it directly to the Qemu command line using xen-pci-passthrough device.

Example usage in cfg file:
pci = [ "00:03.0,seize=1,hotplug=0" ]

Since Qemu -device option accepts parameters in json format, this patchset adds a new internal function libxl__device_pci_get_qmp_json() that generates the device json object used for both QMP hotplug and command line passing.
Also, the function libxl_pci_assignable() is made available from libxl.h for access in libxl_dm.c. I don't know if it's ok to make it public or if it should stay private. Maybe it should be renamed as libxl_device_pci_assignable() to match the other PCI device APIs.

Quoted 6 lines

This has been successfully tested on Xen 4.22-dev and Qemu 10.0 running Debian VMs in both Bios and UEFI mode with a passthrough'd nvme disk. Disabling the hotplug mechanism might be made mandatory for Q35 machines later, once Q35 support is merged upstream.

[1] https://lore.kernel.org/xen-devel/[email protected]/

I don't see relevant device option added to the QEMU cmdline:
https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/13860278916

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#5 lore raw
Quoted 32 lines · Marek Marczykowski-Górecki
Quoted 31 lines · Thierry Escande

This series adds support for a new 'hotplug' option for PCI devices to be passthrough'd to guests.

The current mechanism for device passthrough uses QMP device_add command to hot-plug PCI devices to the guest Qemu instance. This is an issue for guests running on Q35 chipset (preliminary support posted at [1]) since the Q35 PCI root bus does not support hotplug. Devices could be hotplugged to a secondary PCI bus but Xen only support 1 PCI bus for now.

The 'hotplug' option allows to control how devices are attached to the guest, either by using the legacy QMP mechanism (this is the default) or by passing it directly to the Qemu command line using xen-pci-passthrough device.

Example usage in cfg file:
pci = [ "00:03.0,seize=1,hotplug=0" ]

Since Qemu -device option accepts parameters in json format, this patchset adds a new internal function libxl__device_pci_get_qmp_json() that generates the device json object used for both QMP hotplug and command line passing.
Also, the function libxl_pci_assignable() is made available from libxl.h for access in libxl_dm.c. I don't know if it's ok to make it public or if it should stay private. Maybe it should be renamed as libxl_device_pci_assignable() to match the other PCI device APIs.

This has been successfully tested on Xen 4.22-dev and Qemu 10.0 running Debian VMs in both Bios and UEFI mode with a passthrough'd nvme disk. Disabling the hotplug mechanism might be made mandatory for Q35 machines later, once Q35 support is merged upstream.

Quoted 5 lines
Quoted 1 line

[1] https://lore.kernel.org/xen-devel/[email protected]/

I don't see relevant device option added to the QEMU cmdline:
https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/13860278916

My bad. The code that adds the -device option is not in the correct code block. (bad copy/paste at some point I guess...). v3 on its way.

Regards,
Thierry

--
Thierry Escande | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech