On Thu, May 15, 2025 at 01:36:40PM -0700, Nathan Chen via Devel wrote:
Add support for an "iommufd" virDomainIOMMUDef member that
will be
translated to a qemu "-object iommufd" command line argument. This
iommufd struct has an "id" member to specify the iommufd object id
that can be associated with a passthrough device, and an "fd"
member to specify the fd for externally opening the /dev/iommu and
VFIO cdev by a management layer.
@@ -28310,6 +28406,16 @@ virDomainIOMMUDefFormat(virBuffer *buf,
virXMLFormatElement(&childBuf, "driver", &driverAttrBuf, NULL);
+ if (iommu->iommufd) {
+ virBufferAddLit(&childBuf, "<iommufd>\n");
+ virBufferAdjustIndent(&childBuf, 2);
+ virBufferAsprintf(&childBuf, "<id>%s</id>\n",
iommu->iommufd->id);
+ if (iommu->iommufd->fd)
+ virBufferAsprintf(&childBuf, "<fd>%s</fd>\n",
iommu->iommufd->fd);
I'm not convinced we should be exposing the QEMU "id" in the XML.
It is an identifier libvirt uses internally for configuring
QEMU, but I don't see a reason why the end user needs to control
its name. I'm similarly not sure we should expose an FD to
users eitherm, just use that internally when talking to QEMU.
IOW, why aren't we just having "iommu='yes'" as an attribute
on the <iommu> device, avoiding all the logic below that
tries to force all devices to use the same configuration.
+ virBufferAdjustIndent(&childBuf, -2);
+ virBufferAddLit(&childBuf, "</iommufd>\n");
+ }
+
virDomainDeviceInfoFormat(&childBuf, &iommu->info, 0);
virBufferAsprintf(&attrBuf, " model='%s'",
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 75568ff50a..ce447e9823 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3010,6 +3010,11 @@ typedef enum {
VIR_DOMAIN_IOMMU_MODEL_LAST
} virDomainIOMMUModel;
+struct _virDomainIommufdDef {
+ char *id;
+ char *fd;
+};
+
struct _virDomainIOMMUDef {
virDomainIOMMUModel model;
virTristateSwitch intremap;
@@ -3017,6 +3022,8 @@ struct _virDomainIOMMUDef {
virTristateSwitch eim;
virTristateSwitch iotlb;
unsigned int aw_bits;
+ virDomainIommufdDef *iommufd;
+
virDomainDeviceInfo info;
virTristateSwitch dma_translation;
};
@@ -3747,6 +3754,7 @@ virDomainVideoDef *virDomainVideoDefNew(virDomainXMLOption
*xmlopt);
void virDomainVideoDefFree(virDomainVideoDef *def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainVideoDef, virDomainVideoDefFree);
void virDomainVideoDefClear(virDomainVideoDef *def);
+void virDomainIommufdDefFree(virDomainIommufdDef *def);
virDomainHostdevDef *virDomainHostdevDefNew(void);
void virDomainHostdevDefFree(virDomainHostdevDef *def);
void virDomainHubDefFree(virDomainHubDef *def);
diff --git a/src/conf/domain_validate.c b/src/conf/domain_validate.c
index 4861b1c002..88649ba0b9 100644
--- a/src/conf/domain_validate.c
+++ b/src/conf/domain_validate.c
@@ -1838,9 +1838,9 @@ virDomainDefCputuneValidate(const virDomainDef *def)
static int
virDomainDefIOMMUValidate(const virDomainDef *def)
{
- size_t i;
+ size_t i, j;
- if (!def->iommu)
+ if (!def->iommu || def->niommus == 0)
return 0;
for (i = 0; i < def->niommus; i++) {
@@ -1851,6 +1851,39 @@ virDomainDefIOMMUValidate(const virDomainDef *def)
_("IOMMU model smmuv3Dev must be specified for multiple
IOMMU definitions"));
}
+ for (j = i + 1; j < def->niommus; j++) {
+ if (virDomainIOMMUDefEquals(iommu,
+ def->iommu[j])) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("IOMMU already exists in the domain
configuration"));
+ return -1;
+ }
+
+ if (iommu->iommufd && def->iommu[j]->iommufd) {
+ if (iommu->iommufd->id &&
def->iommu[j]->iommufd->id) {
+ if (STRNEQ(iommu->iommufd->id,
def->iommu[j]->iommufd->id)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("iommufd ID must be the same for multiple
IOMMUs"));
+ return -1;
+ }
+ } else {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("iommufd ID must be specified"));
+ }
+ if (iommu->iommufd->fd &&
def->iommu[j]->iommufd->fd &&
+ STRNEQ(iommu->iommufd->fd,
def->iommu[j]->iommufd->fd)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("iommufd FD must be the same for multiple
IOMMUs"));
+ return -1;
+ }
+ } else if ((iommu->iommufd && !def->iommu[j]->iommufd) ||
+ (!iommu->iommufd && def->iommu[j]->iommufd)) {
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("The same iommufd configuration must be specified
for multiple IOMMUs"));
+ return -1;
+ }
+ }
+
if (iommu->intremap == VIR_TRISTATE_SWITCH_ON &&
def->features[VIR_DOMAIN_FEATURE_IOAPIC] != VIR_DOMAIN_IOAPIC_QEMU) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@@ -3115,6 +3148,13 @@ virDomainIOMMUDefValidate(const virDomainIOMMUDef *iommu)
break;
}
+ if (iommu->iommufd) {
+ if (!iommu->iommufd->id) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("iommufd must have an associated id"));
+ return -1;
+ }
+ }
return 0;
}
diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincommon.rng
index 8d1768b24f..41db338c71 100644
--- a/src/conf/schemas/domaincommon.rng
+++ b/src/conf/schemas/domaincommon.rng
@@ -6231,6 +6231,20 @@
<optional>
<ref name="acpi"/>
</optional>
+ <optional>
+ <element name="iommufd">
+ <interleave>
+ <element name="id">
+ <text/>
+ </element>
+ <optional>
+ <element name="fd">
+ <text/>
+ </element>
+ </optional>
+ </interleave>
+ </element>
+ </optional>
<optional>
<ref name="address"/>
</optional>
diff --git a/src/conf/virconftypes.h b/src/conf/virconftypes.h
index c70437bc05..47392154a4 100644
--- a/src/conf/virconftypes.h
+++ b/src/conf/virconftypes.h
@@ -142,6 +142,8 @@ typedef struct _virDomainHubDef virDomainHubDef;
typedef struct _virDomainHugePage virDomainHugePage;
+typedef struct _virDomainIommufdDef virDomainIommufdDef;
+
typedef struct _virDomainIOMMUDef virDomainIOMMUDef;
typedef struct _virDomainIOThreadIDDef virDomainIOThreadIDDef;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 9deafefaad..d683d0eef7 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6166,6 +6166,19 @@ qemuBuildIOMMUCommandLine(virCommand *cmd,
if (!def->iommu || def->niommus <= 0)
return 0;
+ if (def->iommu[0]->iommufd) {
+ if (qemuMonitorCreateObjectProps(&props, "iommufd",
+ def->iommu[0]->iommufd->id,
+ "S:fd",
def->iommu[0]->iommufd->fd,
+ NULL) < 0)
+ return -1;
+
+ if (qemuBuildObjectCommandlineFromJSON(cmd, props) < 0)
+ return -1;
+ }
+
+ props = NULL;
+
for (i = 0; i < def->niommus; i++) {
virDomainIOMMUDef *iommu = def->iommu[i];
switch (iommu->model) {
--
2.43.0
With regards,
Daniel
--
|:
https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|:
https://libvirt.org -o-
https://fstop138.berrange.com :|
|:
https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|