Skip to contentSkip to conversation

Virtio shared dma-buf

PATCH Superseded Albert Esteve 2 participants 9 messages May 18, 2023 – May 22, 2023 Arm/Amlogic Meson SoC support

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 cover Virtio shared dma-buf

v1 link -> https://lists.gnu.org/archive/html/qemu-devel/2023-05/msg00598.html
v1 -> v2:
- Add new files to MAINTAINERS
- Add hash_func and key_equal_func to uuid
- Expose functions to send vhost-user shared_object messages back instead of exposing write_msg method

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_func and key_equal_func to uuid
- Shared resources table, to hold all resources that can be shared in the host and their assigned UUID
- Internal shared table API for virtio devices to add, lookup and remove resources
- Unit test to verify the API.
- New message to the vhost-user protocol to allow backend to interact with the shared table API through the control socket

Applies cleanly to 4ebc33f

[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):
uuid: add hash_func and equal_func
virtio-dmabuf: introduce virtio-dmabuf
vhost-user: add shared_object msg
vhost-user: refactor send_resp code

 MAINTAINERS                               |   7 ++
 docs/interop/vhost-user.rst               |  15 +++
 hw/display/meson.build                    |   1 +
 hw/display/virtio-dmabuf.c                |  82 ++++++++++++++++
 hw/virtio/vhost-user.c                    |  90 ++++++++++++++---
 include/hw/virtio/virtio-dmabuf.h         |  59 ++++++++++++
 include/qemu/uuid.h                       |   4 +
 subprojects/libvhost-user/libvhost-user.c |  88 +++++++++++++++++
 subprojects/libvhost-user/libvhost-user.h |  56 +++++++++++
 tests/unit/meson.build                    |   1 +
 tests/unit/test-uuid.c                    |  46 +++++++++
 tests/unit/test-virtio-dmabuf.c           | 112 ++++++++++++++++++++++
 util/uuid.c                               |  38 ++++++++
 13 files changed, 586 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.40.0
#1 lore raw

PATCH v2 1/4 uuid: add hash_func and equal_func

Add hash and an equal function to uuid module.

Add a couple simple unit tests for new functions, checking collisions for similar UUIDs in the case of the hash function, and comparing generated UUIDs for the equal function.

---
 include/qemu/uuid.h    |  4 ++++
 tests/unit/test-uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 util/uuid.c            | 38 ++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
include/qemu/uuid.h +4 -0
index dc40ee1fc9..136df682c9 100644
@@ -96,4 +96,8 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
 
 QemuUUID qemu_uuid_bswap(QemuUUID uuid);
 
+uint32_t qemu_uuid_hash(const void *uuid);
+
+int qemu_uuid_equal(const void *lhv, const void *rhv);
+
 #endif
tests/unit/test-uuid.c +46 -0

58 lines not on this page · raw message

util/uuid.c +38 -0
index b1108dde78..efa9b0a0e4 100644
@@ -16,6 +16,7 @@
 #include "qemu/osdep.h"
 #include "qemu/uuid.h"
 #include "qemu/bswap.h"
+#include "qemu/xxhash.h"
 
 void qemu_uuid_generate(QemuUUID *uuid)
 {
@@ -116,3 +117,40 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *id = (QemuUUID *) uuid;
+    uint64_t ab = (id->fields.time_low) |
+                  (((uint64_t) id->fields.time_mid) << 32) |
+                  (((uint64_t) id->fields.time_high_and_version) << 48);
+    uint64_t cd = (id->fields.clock_seq_and_reserved) |
+                  (id->fields.clock_seq_low << 8);
+    int i = 0, shift = 8;
+
+    for (; i < 6; i++) {
+        shift += 8;
+        cd |= ((uint64_t) id->fields.node[i]) << shift;
+    }
+
+    return qemu_xxhash4(ab, cd);
+}
+
+int qemu_uuid_equal(const void *lhv, const void *rhv)
+{
+    int i;
+    QemuUUID *lid = (QemuUUID *) lhv;
+    QemuUUID *rid = (QemuUUID *) rhv;
+    if (lid == NULL || rid == NULL) {
+        return 0;
+    }
+    if (lid == rid) {
+        return 1;
+    }
+    for (i = 0; i < 16; i++) {
+        if (lid->data[i] != rid->data[i]) {
+            return 0;
+        }
+    }
+    return 1;
+}
Signature · 1 line
2.40.0
#2 lore raw

Re: [PATCH v2 1/4] uuid: add hash_func and equal_func

Hi

Quoted 19 lines · Albert Esteve

Add hash and an equal function to uuid module.

Add a couple simple unit tests for new functions, checking collisions for similar UUIDs in the case of the hash function, and comparing generated UUIDs for the equal function.

---
 include/qemu/uuid.h    |  4 ++++
 tests/unit/test-uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 util/uuid.c            | 38 ++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
include/qemu/uuid.h +0 -0
index dc40ee1fc9..136df682c9 100644

include/qemu/uuid.h int qemu_uuid_parse(const char *str, QemuUUID *uuid); · line 102

Quoted 7 lines
include/qemu/uuid.h +3 -0
@@ -96,4 +96,8 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
 
 QemuUUID qemu_uuid_bswap(QemuUUID uuid);
 
+uint32_t qemu_uuid_hash(const void *uuid);
+
+int qemu_uuid_equal(const void *lhv, const void *rhv);
 

There is already qemu_uuid_is_equal()

Quoted 91 lines
include/qemu/uuid.h +1 -0
+

#endif

tests/unit/test-uuid.c +46 -0
index c111de5fc1..8c865869d5 100644
@@ -171,6 +171,50 @@ 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);
+    }
+}
+
+static void test_uuid_equal(void)
+{
+    QemuUUID uuid_a, uuid_b, uuid_c;
+    int i;
+
+    for (i = 0; i < 100; i++) {
+        qemu_uuid_generate(&uuid_a);
+        qemu_uuid_generate(&uuid_b);
+        memcpy(&uuid_c, &uuid_a, sizeof(uuid_a));
+
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_a));
+        g_assert(qemu_uuid_equal(&uuid_b, &uuid_b));
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_c));
+        g_assert_false(qemu_uuid_equal(&uuid_a, &uuid_b));
+        g_assert_false(qemu_uuid_equal(NULL, NULL));
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -179,6 +223,8 @@ 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);
+    g_test_add_func("/uuid/equal", test_uuid_equal);
 
     return g_test_run();
 }
util/uuid.c +9 -0
index b1108dde78..efa9b0a0e4 100644
@@ -16,6 +16,7 @@
 #include "qemu/osdep.h"
 #include "qemu/uuid.h"
 #include "qemu/bswap.h"
+#include "qemu/xxhash.h"
 
 void qemu_uuid_generate(QemuUUID *uuid)
 {
@@ -116,3 +117,40 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *id = (QemuUUID *) uuid;
+    uint64_t ab = (id->fields.time_low) |
+                  (((uint64_t) id->fields.time_mid) << 32) |
+                  (((uint64_t) id->fields.time_high_and_version) << 48);
+    uint64_t cd = (id->fields.clock_seq_and_reserved) |

util/uuid.c QemuUUID qemu_uuid_bswap(QemuUUID uuid) · line 137

Quoted 10 lines
util/uuid.c +10 -0
+                  (id->fields.clock_seq_low << 8);
+    int i = 0, shift = 8;
+
+    for (; i < 6; i++) {
+        shift += 8;
+        cd |= ((uint64_t) id->fields.node[i]) << shift;
+    }
+
+    return qemu_xxhash4(ab, cd);
+

That looks quite complex, and I have no idea if this is a good hash or not.

Instead I would implement the traditional "djb" hash over the char[16] data (see g_str_hash implementation for \0-terminated implementation)

Quoted 24 lines

}

util/uuid.c +19 -1
+
+int qemu_uuid_equal(const void *lhv, const void *rhv)
+{
+    int i;
+    QemuUUID *lid = (QemuUUID *) lhv;
+    QemuUUID *rid = (QemuUUID *) rhv;
+    if (lid == NULL || rid == NULL) {
+        return 0;
+    }
+    if (lid == rid) {
+        return 1;
+    }
+    for (i = 0; i < 16; i++) {
+        if (lid->data[i] != rid->data[i]) {
+            return 0;
+        }
+    }
+    return 1;
+}
--

2.40.0

Signature · 1 line
Marc-André Lureau
#3 lore raw

Re: [PATCH v2 1/4] uuid: add hash_func and equal_func

Quoted 24 lines · Marc-André Lureau

Hi

Quoted 20 lines · Albert Esteve

Add hash and an equal function to uuid module.

Add a couple simple unit tests for new functions, checking collisions for similar UUIDs in the case of the hash function, and comparing generated UUIDs for the equal function.

---
 include/qemu/uuid.h    |  4 ++++
 tests/unit/test-uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 util/uuid.c            | 38 ++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
include/qemu/uuid.h +0 -0
index dc40ee1fc9..136df682c9 100644
@@ -96,4 +96,8 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
 
Quoted 10 lines

include/qemu/uuid.h int qemu_uuid_parse(const char *str, QemuUUID *uuid);

Quoted 6 lines
include/qemu/uuid.h +3 -0
 QemuUUID qemu_uuid_bswap(QemuUUID uuid);
 
+uint32_t qemu_uuid_hash(const void *uuid);
+
+int qemu_uuid_equal(const void *lhv, const void *rhv);
 

There is already qemu_uuid_is_equal()

Agh, true. I'll remove it. Not sure why my brain ignored it as I was reading the code...

Quoted 104 lines

+

Quoted 90 lines
include/qemu/uuid.h +0 -0
 #endif
tests/unit/test-uuid.c +46 -0
index c111de5fc1..8c865869d5 100644
@@ -171,6 +171,50 @@ 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);
+    }
+}
+
+static void test_uuid_equal(void)
+{
+    QemuUUID uuid_a, uuid_b, uuid_c;
+    int i;
+
+    for (i = 0; i < 100; i++) {
+        qemu_uuid_generate(&uuid_a);
+        qemu_uuid_generate(&uuid_b);
+        memcpy(&uuid_c, &uuid_a, sizeof(uuid_a));
+
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_a));
+        g_assert(qemu_uuid_equal(&uuid_b, &uuid_b));
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_c));
+        g_assert_false(qemu_uuid_equal(&uuid_a, &uuid_b));
+        g_assert_false(qemu_uuid_equal(NULL, NULL));
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -179,6 +223,8 @@ 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);
+    g_test_add_func("/uuid/equal", test_uuid_equal);
 
     return g_test_run();
 }
util/uuid.c +9 -0
index b1108dde78..efa9b0a0e4 100644
@@ -16,6 +16,7 @@
 #include "qemu/osdep.h"
 #include "qemu/uuid.h"
 #include "qemu/bswap.h"
+#include "qemu/xxhash.h"
 
 void qemu_uuid_generate(QemuUUID *uuid)
 {
@@ -116,3 +117,40 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *id = (QemuUUID *) uuid;
+    uint64_t ab = (id->fields.time_low) |
+                  (((uint64_t) id->fields.time_mid) << 32) |
+                  (((uint64_t) id->fields.time_high_and_version) << 48);
+    uint64_t cd = (id->fields.clock_seq_and_reserved) |

util/uuid.c QemuUUID qemu_uuid_bswap(QemuUUID uuid) · line 137

Quoted 10 lines
util/uuid.c +10 -0
+                  (id->fields.clock_seq_low << 8);
+    int i = 0, shift = 8;
+
+    for (; i < 6; i++) {
+        shift += 8;
+        cd |= ((uint64_t) id->fields.node[i]) << shift;
+    }
+
+    return qemu_xxhash4(ab, cd);
+

That looks quite complex, and I have no idea if this is a good hash or not.

Quoted 3 lines

Instead I would implement the traditional "djb" hash over the char[16] data (see g_str_hash implementation for \0-terminated implementation)

ok, I'll try to do something like that. Thanks for the suggestion.

I looked for any hash library within qemu code and xxhash was one of the options that seemed easier to use.

Quoted 30 lines
Quoted 23 lines

}

util/uuid.c +19 -1
+
+int qemu_uuid_equal(const void *lhv, const void *rhv)
+{
+    int i;
+    QemuUUID *lid = (QemuUUID *) lhv;
+    QemuUUID *rid = (QemuUUID *) rhv;
+    if (lid == NULL || rid == NULL) {
+        return 0;
+    }
+    if (lid == rid) {
+        return 1;
+    }
+    for (i = 0; i < 16; i++) {
+        if (lid->data[i] != rid->data[i]) {
+            return 0;
+        }
+    }
+    return 1;
+}
--
Quoted 3 lines

2.40.0

--
Marc-André Lureau

#4 lore raw

Re: [PATCH v2 1/4] uuid: add hash_func and equal_func

Quoted 41 lines · Albert Esteve
Quoted 24 lines · Marc-André Lureau

Hi

Quoted 20 lines · Albert Esteve

Add hash and an equal function to uuid module.

Add a couple simple unit tests for new functions, checking collisions for similar UUIDs in the case of the hash function, and comparing generated UUIDs for the equal function.

---
 include/qemu/uuid.h    |  4 ++++
 tests/unit/test-uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 util/uuid.c            | 38 ++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
include/qemu/uuid.h +0 -0
index dc40ee1fc9..136df682c9 100644
@@ -96,4 +96,8 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
 
Quoted 10 lines

include/qemu/uuid.h int qemu_uuid_parse(const char *str, QemuUUID *uuid);

Quoted 6 lines
include/qemu/uuid.h +3 -0
 QemuUUID qemu_uuid_bswap(QemuUUID uuid);
 
+uint32_t qemu_uuid_hash(const void *uuid);
+
+int qemu_uuid_equal(const void *lhv, const void *rhv);
 

There is already qemu_uuid_is_equal()

Quoted 3 lines

Agh, true. I'll remove it. Not sure why my brain ignored it as I was reading the code...

One thing to consider here is that the function signature, if we want to pass it as a parameter for g_hash_table_new,
expects void pointers whereas qemu_uuid_is_equal() takes QemuUUID pointers.

How would you suggest proceeding here? Would be better to "overload" (or wrap) a call to qemu_uuid_equal() from
another function that matches the expected signature? (int qemu_uuid_is_equal(void*, void*) is not a good name in that case, it should be something that highlights the difference between the two in the name) Or should I change the current existing
function signature?

Quoted 147 lines
Quoted 106 lines

+

Quoted 90 lines
include/qemu/uuid.h +0 -0
 #endif
tests/unit/test-uuid.c +46 -0
index c111de5fc1..8c865869d5 100644
@@ -171,6 +171,50 @@ 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);
+    }
+}
+
+static void test_uuid_equal(void)
+{
+    QemuUUID uuid_a, uuid_b, uuid_c;
+    int i;
+
+    for (i = 0; i < 100; i++) {
+        qemu_uuid_generate(&uuid_a);
+        qemu_uuid_generate(&uuid_b);
+        memcpy(&uuid_c, &uuid_a, sizeof(uuid_a));
+
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_a));
+        g_assert(qemu_uuid_equal(&uuid_b, &uuid_b));
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_c));
+        g_assert_false(qemu_uuid_equal(&uuid_a, &uuid_b));
+        g_assert_false(qemu_uuid_equal(NULL, NULL));
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -179,6 +223,8 @@ 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);
+    g_test_add_func("/uuid/equal", test_uuid_equal);
 
     return g_test_run();
 }
util/uuid.c +9 -0
index b1108dde78..efa9b0a0e4 100644
@@ -16,6 +16,7 @@
 #include "qemu/osdep.h"
 #include "qemu/uuid.h"
 #include "qemu/bswap.h"
+#include "qemu/xxhash.h"
 
 void qemu_uuid_generate(QemuUUID *uuid)
 {
@@ -116,3 +117,40 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *id = (QemuUUID *) uuid;
+    uint64_t ab = (id->fields.time_low) |
+                  (((uint64_t) id->fields.time_mid) << 32) |
+                  (((uint64_t) id->fields.time_high_and_version) << 48);
+    uint64_t cd = (id->fields.clock_seq_and_reserved) |

util/uuid.c QemuUUID qemu_uuid_bswap(QemuUUID uuid) · line 137

Quoted 10 lines
util/uuid.c +10 -0
+                  (id->fields.clock_seq_low << 8);
+    int i = 0, shift = 8;
+
+    for (; i < 6; i++) {
+        shift += 8;
+        cd |= ((uint64_t) id->fields.node[i]) << shift;
+    }
+
+    return qemu_xxhash4(ab, cd);
+

That looks quite complex, and I have no idea if this is a good hash or not.

Quoted 3 lines

Instead I would implement the traditional "djb" hash over the char[16] data (see g_str_hash implementation for \0-terminated implementation)

ok, I'll try to do something like that. Thanks for the suggestion.

I looked for any hash library within qemu code and xxhash was one of the options that seemed easier to use.

Quoted 30 lines
Quoted 23 lines

}

util/uuid.c +19 -1
+
+int qemu_uuid_equal(const void *lhv, const void *rhv)
+{
+    int i;
+    QemuUUID *lid = (QemuUUID *) lhv;
+    QemuUUID *rid = (QemuUUID *) rhv;
+    if (lid == NULL || rid == NULL) {
+        return 0;
+    }
+    if (lid == rid) {
+        return 1;
+    }
+    for (i = 0; i < 16; i++) {
+        if (lid->data[i] != rid->data[i]) {
+            return 0;
+        }
+    }
+    return 1;
+}
--
Quoted 3 lines

2.40.0

--
Marc-André Lureau

#5 lore raw

Re: [PATCH v2 1/4] uuid: add hash_func and equal_func

Hi

Quoted 55 lines · Albert Esteve
Quoted 42 lines · Albert Esteve
Quoted 25 lines · Marc-André Lureau

Hi

Quoted 20 lines · Albert Esteve

Add hash and an equal function to uuid module.

Add a couple simple unit tests for new functions, checking collisions for similar UUIDs in the case of the hash function, and comparing generated UUIDs for the equal function.

---
 include/qemu/uuid.h    |  4 ++++
 tests/unit/test-uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++
 util/uuid.c            | 38 ++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
include/qemu/uuid.h +0 -0
index dc40ee1fc9..136df682c9 100644
@@ -96,4 +96,8 @@ int qemu_uuid_parse(const char *str, QemuUUID *uuid);
 
Quoted 10 lines

include/qemu/uuid.h int qemu_uuid_parse(const char *str, QemuUUID *uuid);

Quoted 6 lines
include/qemu/uuid.h +3 -0
 QemuUUID qemu_uuid_bswap(QemuUUID uuid);
 
+uint32_t qemu_uuid_hash(const void *uuid);
+
+int qemu_uuid_equal(const void *lhv, const void *rhv);
 

There is already qemu_uuid_is_equal()

Quoted 3 lines

Agh, true. I'll remove it. Not sure why my brain ignored it as I was reading the code...

One thing to consider here is that the function signature, if we want to pass it as a parameter for g_hash_table_new,
expects void pointers whereas qemu_uuid_is_equal() takes QemuUUID pointers.

Quoted 8 lines

How would you suggest proceeding here? Would be better to "overload" (or wrap) a call to qemu_uuid_equal() from
another function that matches the expected signature? (int qemu_uuid_is_equal(void*, void*) is not a good name in that case, it should be something that highlights the difference between the two in the name) Or should I change the current existing
function signature?

I would wrap the call, next to g_hash_table_new(). Alternatively, just cast the function.

Quoted 150 lines
Quoted 150 lines
Quoted 109 lines

+

Quoted 91 lines
include/qemu/uuid.h +0 -0
 #endif
tests/unit/test-uuid.c +46 -0
index c111de5fc1..8c865869d5 100644
@@ -171,6 +171,50 @@ 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);
+    }
+}
+
+static void test_uuid_equal(void)
+{
+    QemuUUID uuid_a, uuid_b, uuid_c;
+    int i;
+
+    for (i = 0; i < 100; i++) {
+        qemu_uuid_generate(&uuid_a);
+        qemu_uuid_generate(&uuid_b);
+        memcpy(&uuid_c, &uuid_a, sizeof(uuid_a));
+
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_a));
+        g_assert(qemu_uuid_equal(&uuid_b, &uuid_b));
+        g_assert(qemu_uuid_equal(&uuid_a, &uuid_c));
+        g_assert_false(qemu_uuid_equal(&uuid_a, &uuid_b));
+        g_assert_false(qemu_uuid_equal(NULL, NULL));
+    }
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -179,6 +223,8 @@ 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);
+    g_test_add_func("/uuid/equal", test_uuid_equal);
 
     return g_test_run();
 }
util/uuid.c +8 -0
index b1108dde78..efa9b0a0e4 100644
@@ -16,6 +16,7 @@
 #include "qemu/osdep.h"
 #include "qemu/uuid.h"
 #include "qemu/bswap.h"
+#include "qemu/xxhash.h"
 
 void qemu_uuid_generate(QemuUUID *uuid)
 {
@@ -116,3 +117,40 @@ QemuUUID qemu_uuid_bswap(QemuUUID uuid)
     bswap16s(&uuid.fields.time_high_and_version);
     return uuid;
 }
+
+uint32_t qemu_uuid_hash(const void *uuid)
+{
+    QemuUUID *id = (QemuUUID *) uuid;
+    uint64_t ab = (id->fields.time_low) |
+                  (((uint64_t) id->fields.time_mid) << 32) |
+                  (((uint64_t) id->fields.time_high_and_version) <<

48);

util/uuid.c +1 -0
+    uint64_t cd = (id->fields.clock_seq_and_reserved) |

util/uuid.c QemuUUID qemu_uuid_bswap(QemuUUID uuid) · line 138

Quoted 10 lines
util/uuid.c +10 -0
+                  (id->fields.clock_seq_low << 8);
+    int i = 0, shift = 8;
+
+    for (; i < 6; i++) {
+        shift += 8;
+        cd |= ((uint64_t) id->fields.node[i]) << shift;
+    }
+
+    return qemu_xxhash4(ab, cd);
+

That looks quite complex, and I have no idea if this is a good hash or not.

Quoted 3 lines

Instead I would implement the traditional "djb" hash over the char[16] data (see g_str_hash implementation for \0-terminated implementation)

ok, I'll try to do something like that. Thanks for the suggestion.

I looked for any hash library within qemu code and xxhash was one of the options that seemed easier to use.

Quoted 30 lines
Quoted 23 lines

}

util/uuid.c +19 -1
+
+int qemu_uuid_equal(const void *lhv, const void *rhv)
+{
+    int i;
+    QemuUUID *lid = (QemuUUID *) lhv;
+    QemuUUID *rid = (QemuUUID *) rhv;
+    if (lid == NULL || rid == NULL) {
+        return 0;
+    }
+    if (lid == rid) {
+        return 1;
+    }
+    for (i = 0; i < 16; i++) {
+        if (lid->data[i] != rid->data[i]) {
+            return 0;
+        }
+    }
+    return 1;
+}
--
Quoted 3 lines

2.40.0

--
Marc-André Lureau

Signature · 1 line
Marc-André Lureau
#6 lore raw

PATCH v2 2/4 virtio-dmabuf: introduce virtio-dmabuf

In reply to Albert Esteve

This API manages objects (in this iteration,
dmabuf fds) that can be shared along different
virtio devices.

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.

---
 MAINTAINERS                       |   7 ++
 hw/display/meson.build            |   1 +
 hw/display/virtio-dmabuf.c        |  82 ++++++++++++++++++++++
 include/hw/virtio/virtio-dmabuf.h |  59 ++++++++++++++++
 tests/unit/meson.build            |   1 +
 tests/unit/test-virtio-dmabuf.c   | 112 ++++++++++++++++++++++++++++++
 6 files changed, 262 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
index 50585117a0..c4313fd657 100644
@@ -2140,6 +2140,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
index 17165bd536..62a27395c0 100644
@@ -37,6 +37,7 @@ softmmu_ss.add(when: 'CONFIG_MACFB', if_true: files('macfb.c'))
 softmmu_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c'))
 
 softmmu_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c'))
+softmmu_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 +82 -0

82 lines not on this page · raw message

include/hw/virtio/virtio-dmabuf.h new file +59 -0

59 lines not on this page · raw message

tests/unit/meson.build +1 -0
index 3bc78d8660..eb2a1a8872 100644
@@ -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 +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.40.0
#7 lore raw

PATCH v2 3/4 vhost-user: add shared_object msg

In reply to Albert Esteve

Add new vhost-user protocol message VHOST_USER_BACKEND_SHARED_OBJECT. This new message is sent from vhost-user back-ends to interact with the virtio-dmabuf table in order to add, remove, or lookup for virtio dma-buf shared objects.

The action taken in the front-end depends on the type stored in the payload struct.

In the libvhost-user library add helper functions to allow sending messages to interact with the virtio shared objects hash table.

---
 docs/interop/vhost-user.rst               | 15 ++++
 hw/virtio/vhost-user.c                    | 68 ++++++++++++++++++
 subprojects/libvhost-user/libvhost-user.c | 88 +++++++++++++++++++++++
 subprojects/libvhost-user/libvhost-user.h | 56 +++++++++++++++
 4 files changed, 227 insertions(+)
docs/interop/vhost-user.rst +15 -0
index 5a070adbc1..d3d8db41e5 100644
@@ -1528,6 +1528,21 @@ is sent by the front-end.
 
   The state.num field is currently reserved and must be set to 0.
 
+``VHOST_USER_BACKEND_SHARED_OBJECT``
+  :id: 6
+  :equivalent ioctl: N/A
+  :request payload: ``struct VhostUserShared``
+  :reply payload: ``struct VhostUserShared`` (only for ``LOOKUP`` requests)
+
+  Backends that need to interact with the virtio-dmabuf shared table API
+  can send this message. The operation is determined by the ``type`` member
+  of the payload struct. The valid values for the operation type are
+  ``VHOST_SHARED_OBJECT_*`` members, i.e., ``ADD``, ``LOOKUP``, and ``REMOVE``.
+  ``LOOKUP`` operations require the ``VHOST_USER_NEED_REPLY_MASK`` flag to be
+  set by the back-end, and the front-end will then send the dma-buf fd as
+  a response if the UUID matches an object in the table, or a negative value
+  otherwise.
+
 .. _reply_ack:
 
 VHOST_USER_PROTOCOL_F_REPLY_ACK
hw/virtio/vhost-user.c +68 -0

110 lines not on this page · raw message

subprojects/libvhost-user/libvhost-user.c +88 -0

94 lines not on this page · raw message

subprojects/libvhost-user/libvhost-user.h +56 -0

80 lines not on this page · raw message

Signature · 1 line
2.40.0
#8 lore raw

PATCH v2 4/4 vhost-user: refactor send_resp code

In reply to Albert Esteve

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.

---
 hw/virtio/vhost-user.c | 52 +++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 28 deletions(-)
hw/virtio/vhost-user.c +24 -28
index d9bcaafebf..c82ba9e59f 100644
@@ -1628,28 +1628,36 @@ static int vhost_user_backend_handle_shared_object(VhostUserShared *object)
     return 0;
 }
 
-static bool
-vhost_user_backend_send_dmabuf_fd(QIOChannel *ioc, VhostUserHeader *hdr,
-                                  VhostUserPayload *payload)
+static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
+                                 VhostUserPayload *payload)
 {
     Error *local_err = NULL;
     struct iovec iov[2];
-    if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
-        hdr->flags &= ~VHOST_USER_NEED_REPLY_MASK;
-        hdr->flags |= VHOST_USER_REPLY_MASK;
+    hdr->flags &= ~VHOST_USER_NEED_REPLY_MASK;
+    hdr->flags |= VHOST_USER_REPLY_MASK;
 
-        hdr->size = sizeof(payload->object);
+    iov[0].iov_base = hdr;
+    iov[0].iov_len = VHOST_USER_HDR_SIZE;
+    iov[1].iov_base = payload;
+    iov[1].iov_len = hdr->size;
+
+    if (qio_channel_writev_all(ioc, iov, ARRAY_SIZE(iov), &local_err)) {
+        error_report_err(local_err);
+        return false;
+    }
 
-        iov[0].iov_base = hdr;
-        iov[0].iov_len = VHOST_USER_HDR_SIZE;
-        iov[1].iov_base = payload;
-        iov[1].iov_len = hdr->size;
+    return true;
+}
 
-        if (qio_channel_writev_all(ioc, iov, ARRAY_SIZE(iov), &local_err)) {
-            error_report_err(local_err);
-            return false;
-        }
+static bool
+vhost_user_backend_send_dmabuf_fd(QIOChannel *ioc, VhostUserHeader *hdr,
+                                  VhostUserPayload *payload)
+{
+    if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
+        hdr->size = sizeof(payload->object);
+        return vhost_user_send_resp(ioc, hdr, payload);
     }
+
     return true;
 }
 
@@ -1726,22 +1734,10 @@ static gboolean slave_read(QIOChannel *ioc, GIOCondition condition,
      * directly in their request handlers.
      */
     if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
-        struct iovec iovec[2];
-
-
-        hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
-        hdr.flags |= VHOST_USER_REPLY_MASK;
-
         payload.u64 = !!ret;
         hdr.size = sizeof(payload.u64);
 
-        iovec[0].iov_base = &hdr;
-        iovec[0].iov_len = VHOST_USER_HDR_SIZE;
-        iovec[1].iov_base = &payload;
-        iovec[1].iov_len = hdr.size;
-
-        if (qio_channel_writev_all(ioc, iovec, ARRAY_SIZE(iovec), &local_err)) {
-            error_report_err(local_err);
+        if (!vhost_user_send_resp(ioc, &hdr, &payload)) {
             goto err;
         }
     }
Signature · 1 line
2.40.0