Virtio shared dma-buf
v9 cover Virtio shared dma-buf
v1 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-05/msg00598.html
v2 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-05/msg04530.html
v3 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-05/msg06126.html
v4 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-06/msg05174.html
v5 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-08/msg00255.html
v6 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg00987.html
v7 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg01190.html
v8 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg02284.html
v8 -> v9:
- Remove EOF whitespace in util/uuid.c
This patch covers the required steps to add support for virtio cross-device resource sharing[1], which support is already available in the kernel.
The main usecase will be sharing dma buffers from virtio-gpu devices (as the exporter -see VIRTIO_GPU_CMD_RESOURCE_ASSIGN_UUID in [2]), to virtio-video (under discussion) devices (as the buffer-user or importer). Therefore, even though virtio specs talk about resources or objects[3], this patch adds the infrastructure with dma-bufs in mind. Note that virtio specs let the devices themselves define what a vitio object is.
These are the main parts that are covered in the patch:
- Add hash function to uuid module
- Shared resources table, to hold all resources that can be shared in the host and their assigned UUID, or pointers to the backend holding the resource
- Internal shared table API for virtio devices to add, lookup and remove resources
- Unit test to verify the API
- New messages to the vhost-user protocol to allow backend to interact with the shared table API through the control socket
- New vhost-user feature bit to enable shared objects feature
Applies cleanly to 36e9aab3c569d4c9ad780473596e18479838d1aa
[1] - https://lwn.net/Articles/828988/
[2] - https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-3730006
[3] - https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-10500011
Albert Esteve (4):
util/uuid: add a hash function
hw/display: introduce virtio-dmabuf
vhost-user: add shared_object msg
libvhost-user: handle shared_object msg
MAINTAINERS | 7 + docs/interop/vhost-user.rst | 57 ++++++++ hw/display/meson.build | 1 + hw/display/virtio-dmabuf.c | 138 ++++++++++++++++++ hw/virtio/vhost-user.c | 168 ++++++++++++++++++++-- include/hw/virtio/vhost-backend.h | 3 + include/hw/virtio/virtio-dmabuf.h | 100 +++++++++++++ include/qemu/uuid.h | 2 + subprojects/libvhost-user/libvhost-user.c | 120 ++++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 55 ++++++- tests/unit/meson.build | 1 + tests/unit/test-uuid.c | 27 ++++ tests/unit/test-virtio-dmabuf.c | 137 ++++++++++++++++++ util/uuid.c | 14 ++ 14 files changed, 817 insertions(+), 13 deletions(-) create mode 100644 hw/display/virtio-dmabuf.c create mode 100644 include/hw/virtio/virtio-dmabuf.h create mode 100644 tests/unit/test-virtio-dmabuf.c
Signature · 1 line
2.41.0
PATCH v9 1/4 util/uuid: add a hash function
Add hash function to uuid module using the
djb2 hash algorithm.
Add a couple simple unit tests for the hash
function, checking collisions for similar UUIDs.
--- include/qemu/uuid.h | 2 ++ tests/unit/test-uuid.c | 27 +++++++++++++++++++++++++++ util/uuid.c | 14 ++++++++++++++ 3 files changed, 43 insertions(+)
include/qemu/uuid.h +2 -0
@@ -96,4 +96,6 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);QemuUUID qemu_uuid_bswap (QemuUUID uuid); +uint32_t qemu_uuid_hash (const void *uuid); +#endif
tests/unit/test-uuid.c +27 -0
@@ -171,6 +171,32 @@ static void test_uuid_unparse_strdup(void) } } +static void test_uuid_hash (void ) +{ +QemuUUID uuid; +int i; + +for (i =0 ; i <100 ; i++) { +qemu_uuid_generate (&uuid); +/* Obtain the UUID hash */ +uint32_t hash_a =qemu_uuid_hash (&uuid); +int data_idx =g_random_int_range (0 ,15 ); +/* Change a single random byte of the UUID */ +if (uuid.data[data_idx] <0xFF ) { + uuid.data[data_idx]++; + }else { + uuid.data[data_idx]--; + } +/* Obtain the UUID hash again */ +uint32_t hash_b =qemu_uuid_hash (&uuid); +/* +* Both hashes shall be different (avoid collision) +* for any change in the UUID fields +*/ + g_assert_cmpint(hash_a, !=, hash_b); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );
@@ -179,6 +205,7 @@ int main(int argc, char **argv)g_test_add_func ("/uuid/parse" , test_uuid_parse);g_test_add_func ("/uuid/unparse" , test_uuid_unparse);g_test_add_func ("/uuid/unparse_strdup" , test_uuid_unparse_strdup); +g_test_add_func ("/uuid/hash" , test_uuid_hash);return g_test_run (); }
util/uuid.c +14 -0
@@ -116,3 +116,17 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)bswap16s (&uuid.fields.time_high_and_version);return uuid; } + +/* djb2 hash algorithm */ +uint32_t qemu_uuid_hash (const void *uuid) +{ +QemuUUID *qid = (QemuUUID *) uuid; +uint32_t h =5381 ; +int i; + +for (i =0 ; i <ARRAY_SIZE (qid->data); i++) { + h = (h <<5 ) + h + qid->data[i]; + } + +return h; +}
Signature · 1 line
2.41.0
PATCH v9 2/4 hw/display: introduce virtio-dmabuf
In reply to Albert EsteveThis API manages objects (in this iteration,
dmabuf fds) that can be shared along different
virtio devices, associated to a UUID.
The API allows the different devices to add,
remove and/or retrieve the objects by simply
invoking the public functions that reside in the
virtio-dmabuf file.
For vhost backends, the API stores the pointer
to the backend holding the object.
--- MAINTAINERS | 7 ++ hw/display/meson.build | 1 + hw/display/virtio-dmabuf.c | 138 ++++++++++++++++++++++++++++++ include/hw/virtio/virtio-dmabuf.h | 100 ++++++++++++++++++++++ tests/unit/meson.build | 1 + tests/unit/test-virtio-dmabuf.c | 137 +++++++++++++++++++++++++++++ 6 files changed, 384 insertions(+) create mode 100644 hw/display/virtio-dmabuf.c create mode 100644 include/hw/virtio/virtio-dmabuf.h create mode 100644 tests/unit/test-virtio-dmabuf.c
MAINTAINERS +7 -0
@@ -2154,6 +2154,13 @@ T: git https://gitlab.com/cohuck/qemu.git s390-next T: git https://github.com/borntraeger/qemu.git s390-next L: [email protected] +virtio-dmabuf +M: Albert Esteve <[email protected]> +S: Supported +F: hw/display/virtio-dmabuf.c +F: include/hw/virtio/virtio-dmabuf.h +F: tests/unit/test-virtio-dmabuf.c + virtiofs M: Stefan Hajnoczi <[email protected]> S: Supported
hw/display/meson.build +1 -0
@@ -37,6 +37,7 @@ system_ss.add(when: 'CONFIG_MACFB', if_true: files('macfb.c')) system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) +system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') or config_all_devices.has_key('CONFIG_VGA_PCI') or
hw/display/virtio-dmabuf.c new file +138 -0
138 lines not on this page · raw message
include/hw/virtio/virtio-dmabuf.h new file +100 -0
100 lines not on this page · raw message
tests/unit/meson.build +1 -0
@@ -50,6 +50,7 @@ tests = { 'test-qapi-util': [], 'test-interval-tree': [], 'test-xs-node': [qom], + 'test-virtio-dmabuf': [meson.project_source_root() / 'hw/display/virtio-dmabuf.c'], } if have_system or have_tools
tests/unit/test-virtio-dmabuf.c new file +137 -0
137 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v9 3/4 vhost-user: add shared_object msg
In reply to Albert EsteveAdd three new vhost-user protocol VHOST_USER_BACKEND_SHARED_OBJECT_* messages. These new messages are sent from vhost-user back-ends to interact with the virtio-dmabuf table in order to add or remove themselves as virtio exporters, or lookup for virtio dma-buf shared objects.
The action taken in the front-end depends on the type stored in the virtio shared object hash table.
When the table holds a pointer to a vhost backend for a given UUID, the front-end sends a VHOST_USER_GET_SHARED_OBJECT to the backend holding the shared object.
The messages can only be sent after successfully negotiating a new VHOST_USER_PROTOCOL_F_SHARED_OBJECT vhost-user protocol feature bit.
Finally, refactor code to send response message so that all common parts both for the common REPLY_ACK case, and other data responses, can call it and avoid code repetition.
--- docs/interop/vhost-user.rst | 57 ++++++++++ hw/virtio/vhost-user.c | 168 +++++++++++++++++++++++++++--- include/hw/virtio/vhost-backend.h | 3 + 3 files changed, 216 insertions(+), 12 deletions(-)
docs/interop/vhost-user.rst +57 -0
@@ -1440,6 +1440,18 @@ Front-end message types query the back-end for its device status as defined in the Virtio specification. +``VHOST_USER_GET_SHARED_OBJECT`` + :id: 41 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: dmabuf fd + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, and the UUID is found + in the exporters cache, this message is submitted by the front-end + to retrieve a given dma-buf fd from a given back-end, determined by + the requested UUID. Back-end will reply passing the fd when the operation + is successful, or no fd otherwise. Back-end message types ----------------------
@@ -1528,6 +1540,51 @@ is sent by the front-end. The state.num field is currently reserved and must be set to 0. +``VHOST_USER_BACKEND_SHARED_OBJECT_ADD`` + :id: 6 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backends to add themselves as exporters to the virtio shared lookup + table. The back-end device gets associated with a UUID in the shared table. + The back-end is responsible of keeping its own table with exported dma-buf fds. + When another back-end tries to import the resource associated with the UUID, + it will send a message to the front-end, which will act as a proxy to the + exporter back-end. If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, and + the back-end sets the ``VHOST_USER_NEED_REPLY`` flag, the front-end must + respond with zero when operation is successfully completed, or non-zero + otherwise. + +``VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE`` + :id: 7 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backend to remove themselves from to the virtio-dmabuf shared + table API. The shared table will remove the back-end device associated with + the UUID. If ``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, and the + back-end sets the ``VHOST_USER_NEED_REPLY`` flag, the front-end must respond + with zero when operation is successfully completed, or non-zero otherwise. + +``VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP`` + :id: 8 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserShared`` + :reply payload: dmabuf fd and ``u64`` + + When the ``VHOST_USER_PROTOCOL_F_SHARED_OBJECT`` protocol + feature has been successfully negotiated, this message can be submitted + by the backends to retrieve a given dma-buf fd from the virtio-dmabuf + shared table given a UUID. Frontend will reply passing the fd and a zero + when the operation is successful, or non-zero otherwise. Note that if the + operation fails, no fd is sent to the backend. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACK
hw/virtio/vhost-user.c +156 -12
231 lines not on this page · raw message
include/hw/virtio/vhost-backend.h +3 -0
@@ -196,4 +196,7 @@ int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,int vhost_user_gpu_set_socket (struct vhost_dev *dev,int fd); +int vhost_user_get_shared_object (struct vhost_dev *dev,unsigned char *uuid, +int *dmabuf_fd); +#endif /* VHOST_BACKEND_H */
Signature · 1 line
2.41.0
PATCH v9 4/4 libvhost-user: handle shared_object msg
In reply to Albert EsteveIn the libvhost-user library we need to handle VHOST_USER_GET_SHARED_OBJECT requests, and add helper functions to allow sending messages to interact with the virtio shared objects hash table.
--- subprojects/libvhost-user/libvhost-user.c | 120 ++++++++++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 55 +++++++++- 2 files changed, 174 insertions(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.c +120 -0
@@ -161,6 +161,7 @@ vu_request_to_string(unsigned int req)REQ (VHOST_USER_GET_MAX_MEM_SLOTS ),REQ (VHOST_USER_ADD_MEM_REG ),REQ (VHOST_USER_REM_MEM_REG ), +REQ (VHOST_USER_GET_SHARED_OBJECT ), REQ(VHOST_USER_MAX), };#undef REQ
@@ -900,6 +901,24 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {return false; } +static bool +vu_get_shared_object (VuDev *dev,VhostUserMsg *vmsg) +{ +int fd_num =0 ; +int dmabuf_fd =-1 ; +if (dev->iface->get_shared_object) { + dmabuf_fd = dev->iface->get_shared_object ( + dev, &vmsg->payload.object.uuid[0 ]); + } +if (dmabuf_fd !=-1 ) { +DPRINT ("dmabuf_fd found for requested UUID\n" ); + vmsg->fds[fd_num++] = dmabuf_fd; + } + vmsg->fd_num = fd_num; + +return true; +} +static bool vu_set_mem_table_exec_postcopy (VuDev *dev,VhostUserMsg *vmsg) {
@@ -1403,6 +1422,105 @@ bool vu_set_queue_host_notifier(VuDev *dev, VuVirtq *vq, int fd,return vu_process_message_reply (dev, &vmsg); } +bool +vu_lookup_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ], +int *dmabuf_fd) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg msg = { + .request =VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP , + .size =sizeof (msg.payload.object), + .flags =VHOST_USER_VERSION |VHOST_USER_NEED_REPLY_MASK , + }; + +memcpy (msg.payload.object.uuid, uuid,sizeof (uuid[0 ]) *UUID_LEN ); + +if (!vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_SHARED_OBJECT )) { +return false; + } + +pthread_mutex_lock (&dev->backend_mutex); +if (!vu_message_write (dev, dev->backend_fd, &msg)) { + goto out; + } + +if (!vu_message_read_default (dev, dev->backend_fd, &msg_reply)) { + goto out; + } + +if (msg_reply.request != msg.request) { +DPRINT ("Received unexpected msg type. Expected %d, received %d" , + msg.request, msg_reply.request); + goto out; + } + +if (msg_reply.fd_num !=1 ) { +DPRINT ("Received unexpected number of fds. Expected 1, received %d" , + msg_reply.fd_num); + goto out; + } + + *dmabuf_fd = msg_reply.fds[0 ]; + result = *dmabuf_fd >0 && msg_reply.payload.u64 ==0 ; +out: +pthread_mutex_unlock (&dev->backend_mutex); + +return result; +} + +static bool +vu_send_message (VuDev *dev,VhostUserMsg *vmsg) +{ +bool result = false; +pthread_mutex_lock (&dev->backend_mutex); +if (!vu_message_write (dev, dev->backend_fd, vmsg)) { + goto out; + } + + result = true; +out: +pthread_mutex_unlock (&dev->backend_mutex); + +return result; +} + +bool +vu_add_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]) +{ +VhostUserMsg msg = { + .request =VHOST_USER_BACKEND_SHARED_OBJECT_ADD , + .size =sizeof (msg.payload.object), + .flags =VHOST_USER_VERSION , + }; + +memcpy (msg.payload.object.uuid, uuid,sizeof (uuid[0 ]) *UUID_LEN ); + +if (!vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_SHARED_OBJECT )) { +return false; + } + +return vu_send_message (dev, &msg); +} + +bool +vu_rm_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]) +{ +VhostUserMsg msg = { + .request =VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE , + .size =sizeof (msg.payload.object), + .flags =VHOST_USER_VERSION , + }; + +memcpy (msg.payload.object.uuid, uuid,sizeof (uuid[0 ]) *UUID_LEN ); + +if (!vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_SHARED_OBJECT )) { +return false; + } + +return vu_send_message (dev, &msg); +} +static bool vu_set_vring_call_exec (VuDev *dev,VhostUserMsg *vmsg) {
@@ -1943,6 +2061,8 @@ vu_process_message(VuDev *dev, VhostUserMsg *vmsg)return vu_add_mem_reg (dev, vmsg);case VHOST_USER_REM_MEM_REG :return vu_rem_mem_reg (dev, vmsg); +case VHOST_USER_GET_SHARED_OBJECT : +return vu_get_shared_object (dev, vmsg);default :vmsg_close_fds (vmsg);vu_panic (dev,"Unhandled request: %d" , vmsg->request);
subprojects/libvhost-user/libvhost-user.h +54 -1
103 lines not on this page · raw message
Signature · 1 line
2.41.0