Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable IOMMUFD for VM device passthrough #4004

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions virttest/qemu_devices/qcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4031,9 +4031,25 @@ def hostdev_define_by_params(self, name, params, bus=None):
"failover_pair_id": params.get("vm_hostdev_failover_pair_id"),
}
)
# Use hard code 'iommufd0' here, multiple devices use the same iommufd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned here, multiple devices can use the same one. How about checking the iommufd as an ID? If the iommufd device exists with the same ID, reuse it; if not, create a new one, cause we should also support using different iommufd devices, what do you think?

if params.get("iommufd") == "yes":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code block, all parameters use vm_hostdev prefix, I would suggest align with this.

dev_params["iommufd"] = "iommufd0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the iommufd is only used for VFIO devices, so I have an idea here,

devs = []
...
...
fd_id = params.get("vm_hostdev_iommufd")
if fd_id and not self.get_by_qid(fd_id):
    iommufd = iommufd_object_define_by_params()
    dev_params["iommufd"] = fd_id
    devs.append(iommufd)
...
...
dev = qdevices.QDevice(driver, dev_params, parent_bus=dev_bus)
devs.append(dev)
return devs

If you have any concern, please don't hesitate to raise them.

# TODO: Support vfio-ap and vfio-ccw, currently only for pci devices
dev_bus = bus or {"aobject": params.get("pci_bus", "pci.0")}
dev = qdevices.QDevice(driver, dev_params, parent_bus=dev_bus)
for ext_k, ext_v in params.get_dict("vm_hostdev_extra_params").items():
dev.set_param(ext_k, ext_v)
return dev

def iommufd_object_define_by_params(self, obj_id):
"""
Create iommufd object device by params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before creating an iommufd device, it is better to check that /dev/iommu exists.


The iommufd objects cmdlines:
-object iommufd,id=iommufd0

:param obj_id: The id of the QObject device, e.g. iommufd0
:return: the iommufd QObject device
"""
backend, properties = "iommufd", {"id": obj_id}
return qdevices.QObject(backend, properties)
13 changes: 13 additions & 0 deletions virttest/qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,16 @@ def __iothread_conflict_check(params):
"'image_iothread_vq_mapping' can NOT be set!"
)

def add_iommufd_descriptor():
"""
Add iommufd into device tree

Use hard code 'iommufd0' here for iommufd object, because
multiple devices can use the same iommufd
"""
obj = devices.iommufd_object_define_by_params('iommufd0')
devices.insert(obj)

# End of command line option wrappers

# If nothing changed and devices exists, return immediately
Expand Down Expand Up @@ -1885,6 +1895,9 @@ def __iothread_conflict_check(params):
if params.get("vm_secure_guest_type"):
add_secure_guest_descriptor(params)

if params.get("iommufd"):
add_iommufd_descriptor()

# no automagic devices please
defaults = params.get("defaults", "no")
if devices.has_option("nodefaults") and defaults != "yes":
Expand Down