Add support for getting basic device information.
Originally-by: John Johnson <john.g.johnson@oracle.com>
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Signed-off-by: John Levon <john.levon@nutanix.com>
---
hw/vfio-user/device.h | 20 ++++++++++++++++
hw/vfio-user/protocol.h | 12 ++++++++++
hw/vfio-user/proxy.h | 7 ++++++
hw/vfio-user/container.c | 10 +++++++-
hw/vfio-user/device.c | 48 +++++++++++++++++++++++++++++++++++++++
hw/vfio-user/proxy.c | 12 ++++------
hw/vfio-user/meson.build | 1 +
hw/vfio-user/trace-events | 1 +
8 files changed, 102 insertions(+), 9 deletions(-)
create mode 100644 hw/vfio-user/device.h
create mode 100644 hw/vfio-user/device.c
diff --git a/hw/vfio-user/device.h b/hw/vfio-user/device.h
new file mode 100644
index 0000000000..d6f2b56066
--- /dev/null
+++ b/hw/vfio-user/device.h
@@ -0,0 +1,20 @@
+#ifndef VFIO_USER_DEVICE_H
+#define VFIO_USER_DEVICE_H
+
+/*
+ * vfio protocol over a UNIX socket device handling.
+ *
+ * Copyright © 2018, 2021 Oracle and/or its affiliates.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "linux/vfio.h"
+
+#include "hw/vfio-user/proxy.h"
+
+int vfio_user_get_device_info(VFIOUserProxy *proxy,
+ struct vfio_device_info *info);
+
+#endif /* VFIO_USER_DEVICE_H */
diff --git a/hw/vfio-user/protocol.h b/hw/vfio-user/protocol.h
index 2d52d0fb10..e0bba68739 100644
--- a/hw/vfio-user/protocol.h
+++ b/hw/vfio-user/protocol.h
@@ -112,4 +112,16 @@ typedef struct {
*/
#define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
+/*
+ * VFIO_USER_DEVICE_GET_INFO
+ * imported from struct vfio_device_info
+ */
+typedef struct {
+ VFIOUserHdr hdr;
+ uint32_t argsz;
+ uint32_t flags;
+ uint32_t num_regions;
+ uint32_t num_irqs;
+} VFIOUserDeviceInfo;
+
#endif /* VFIO_USER_PROTOCOL_H */
diff --git a/hw/vfio-user/proxy.h b/hw/vfio-user/proxy.h
index 5bc890a0f5..bd5860e9b8 100644
--- a/hw/vfio-user/proxy.h
+++ b/hw/vfio-user/proxy.h
@@ -12,7 +12,9 @@
#include "io/channel.h"
#include "io/channel-socket.h"
+#include "qemu/queue.h"
#include "qemu/sockets.h"
+#include "qemu/thread.h"
#include "hw/vfio-user/protocol.h"
typedef struct {
@@ -96,4 +98,9 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
void *reqarg);
bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
+void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
+ uint32_t size, uint32_t flags);
+void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+ VFIOUserFDs *fds, int rsize);
+
#endif /* VFIO_USER_PROXY_H */
diff --git a/hw/vfio-user/container.c b/hw/vfio-user/container.c
index 2367332177..664cdf044f 100644
--- a/hw/vfio-user/container.c
+++ b/hw/vfio-user/container.c
@@ -11,6 +11,7 @@
#include "qemu/osdep.h"
#include "hw/vfio-user/container.h"
+#include "hw/vfio-user/device.h"
#include "hw/vfio/vfio-cpr.h"
#include "hw/vfio/vfio-device.h"
#include "hw/vfio/vfio-listener.h"
@@ -140,7 +141,14 @@ static void vfio_user_container_disconnect(VFIOUserContainer *container)
static bool vfio_user_device_get(VFIOUserContainer *container,
VFIODevice *vbasedev, Error **errp)
{
- struct vfio_device_info info = { 0 };
+ struct vfio_device_info info = { .argsz = sizeof(info) };
+ int ret;
+
+ ret = vfio_user_get_device_info(vbasedev->proxy, &info);
+ if (ret) {
+ error_setg_errno(errp, -ret, "get info failure");
+ return ret;
+ }
vbasedev->fd = -1;
diff --git a/hw/vfio-user/device.c b/hw/vfio-user/device.c
new file mode 100644
index 0000000000..4998019b30
--- /dev/null
+++ b/hw/vfio-user/device.c
@@ -0,0 +1,48 @@
+/*
+ * vfio protocol over a UNIX socket device handling.
+ *
+ * Copyright © 2018, 2021 Oracle and/or its affiliates.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+
+#include "hw/vfio-user/device.h"
+#include "hw/vfio-user/trace.h"
+
+/*
+ * These are to defend against a malign server trying
+ * to force us to run out of memory.
+ */
+#define VFIO_USER_MAX_REGIONS 100
+#define VFIO_USER_MAX_IRQS 50
+
+int vfio_user_get_device_info(VFIOUserProxy *proxy,
+ struct vfio_device_info *info)
+{
+ VFIOUserDeviceInfo msg;
+ uint32_t argsz = sizeof(msg) - sizeof(msg.hdr);
+
+ memset(&msg, 0, sizeof(msg));
+ vfio_user_request_msg(&msg.hdr, VFIO_USER_DEVICE_GET_INFO, sizeof(msg), 0);
+ msg.argsz = argsz;
+
+ vfio_user_send_wait(proxy, &msg.hdr, NULL, 0);
+ if (msg.hdr.flags & VFIO_USER_ERROR) {
+ return -msg.hdr.error_reply;
+ }
+ trace_vfio_user_get_info(msg.num_regions, msg.num_irqs);
+
+ memcpy(info, &msg.argsz, argsz);
+
+ /* defend against a malicious server */
+ if (info->num_regions > VFIO_USER_MAX_REGIONS ||
+ info->num_irqs > VFIO_USER_MAX_IRQS) {
+ error_printf("%s: invalid reply\n", __func__);
+ return -EINVAL;
+ }
+
+ return 0;
+}
diff --git a/hw/vfio-user/proxy.c b/hw/vfio-user/proxy.c
index 0887d0aa1a..c6b6628505 100644
--- a/hw/vfio-user/proxy.c
+++ b/hw/vfio-user/proxy.c
@@ -39,10 +39,6 @@ static void vfio_user_cb(void *opaque);
static void vfio_user_request(void *opaque);
static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg);
-static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
- VFIOUserFDs *fds, int rsize);
-static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
- uint32_t size, uint32_t flags);
static inline void vfio_user_set_error(VFIOUserHdr *hdr, uint32_t err)
{
@@ -610,8 +606,8 @@ static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg)
return 0;
}
-static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
- VFIOUserFDs *fds, int rsize)
+void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
+ VFIOUserFDs *fds, int rsize)
{
VFIOUserMsg *msg;
int ret;
@@ -782,8 +778,8 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
g_free(proxy);
}
-static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
- uint32_t size, uint32_t flags)
+void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
+ uint32_t size, uint32_t flags)
{
static uint16_t next_id;
diff --git a/hw/vfio-user/meson.build b/hw/vfio-user/meson.build
index cb958d0aa3..54af0da585 100644
--- a/hw/vfio-user/meson.build
+++ b/hw/vfio-user/meson.build
@@ -1,6 +1,7 @@
vfio_user_ss = ss.source_set()
vfio_user_ss.add(files(
'container.c',
+ 'device.c',
'pci.c',
'proxy.c',
))
diff --git a/hw/vfio-user/trace-events b/hw/vfio-user/trace-events
index 7a3645024f..6b06a3ed82 100644
--- a/hw/vfio-user/trace-events
+++ b/hw/vfio-user/trace-events
@@ -6,3 +6,4 @@ vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x"
vfio_user_recv_request(uint16_t cmd) " command 0x%x"
vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
+vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
--
2.43.0
On 6/19/25 15:31, John Levon wrote:
> Add support for getting basic device information.
>
> Originally-by: John Johnson <john.g.johnson@oracle.com>
> Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
> Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
> Signed-off-by: John Levon <john.levon@nutanix.com>
> ---
> hw/vfio-user/device.h | 20 ++++++++++++++++
> hw/vfio-user/protocol.h | 12 ++++++++++
> hw/vfio-user/proxy.h | 7 ++++++
> hw/vfio-user/container.c | 10 +++++++-
> hw/vfio-user/device.c | 48 +++++++++++++++++++++++++++++++++++++++
> hw/vfio-user/proxy.c | 12 ++++------
> hw/vfio-user/meson.build | 1 +
> hw/vfio-user/trace-events | 1 +
> 8 files changed, 102 insertions(+), 9 deletions(-)
> create mode 100644 hw/vfio-user/device.h
> create mode 100644 hw/vfio-user/device.c
>
> diff --git a/hw/vfio-user/device.h b/hw/vfio-user/device.h
> new file mode 100644
> index 0000000000..d6f2b56066
> --- /dev/null
> +++ b/hw/vfio-user/device.h
> @@ -0,0 +1,20 @@
> +#ifndef VFIO_USER_DEVICE_H
> +#define VFIO_USER_DEVICE_H
> +
> +/*
> + * vfio protocol over a UNIX socket device handling.
> + *
> + * Copyright © 2018, 2021 Oracle and/or its affiliates.
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#include "qemu/osdep.h"
> +#include "linux/vfio.h"
> +
> +#include "hw/vfio-user/proxy.h"
> +
> +int vfio_user_get_device_info(VFIOUserProxy *proxy,
> + struct vfio_device_info *info);
> +
> +#endif /* VFIO_USER_DEVICE_H */
> diff --git a/hw/vfio-user/protocol.h b/hw/vfio-user/protocol.h
> index 2d52d0fb10..e0bba68739 100644
> --- a/hw/vfio-user/protocol.h
> +++ b/hw/vfio-user/protocol.h
> @@ -112,4 +112,16 @@ typedef struct {
> */
> #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
>
> +/*
> + * VFIO_USER_DEVICE_GET_INFO
> + * imported from struct vfio_device_info
> + */
> +typedef struct {
> + VFIOUserHdr hdr;
> + uint32_t argsz;
> + uint32_t flags;
> + uint32_t num_regions;
> + uint32_t num_irqs;
> +} VFIOUserDeviceInfo;
> +
> #endif /* VFIO_USER_PROTOCOL_H */
> diff --git a/hw/vfio-user/proxy.h b/hw/vfio-user/proxy.h
> index 5bc890a0f5..bd5860e9b8 100644
> --- a/hw/vfio-user/proxy.h
> +++ b/hw/vfio-user/proxy.h
> @@ -12,7 +12,9 @@
> #include "io/channel.h"
> #include "io/channel-socket.h"
>
> +#include "qemu/queue.h"
> #include "qemu/sockets.h"
> +#include "qemu/thread.h"
> #include "hw/vfio-user/protocol.h"
>
> typedef struct {
> @@ -96,4 +98,9 @@ void vfio_user_set_handler(VFIODevice *vbasedev,
> void *reqarg);
> bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);
>
> +void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
> + uint32_t size, uint32_t flags);
> +void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
> + VFIOUserFDs *fds, int rsize);
> +
> #endif /* VFIO_USER_PROXY_H */
> diff --git a/hw/vfio-user/container.c b/hw/vfio-user/container.c
> index 2367332177..664cdf044f 100644
> --- a/hw/vfio-user/container.c
> +++ b/hw/vfio-user/container.c
> @@ -11,6 +11,7 @@
> #include "qemu/osdep.h"
>
> #include "hw/vfio-user/container.h"
> +#include "hw/vfio-user/device.h"
> #include "hw/vfio/vfio-cpr.h"
> #include "hw/vfio/vfio-device.h"
> #include "hw/vfio/vfio-listener.h"
> @@ -140,7 +141,14 @@ static void vfio_user_container_disconnect(VFIOUserContainer *container)
> static bool vfio_user_device_get(VFIOUserContainer *container,
> VFIODevice *vbasedev, Error **errp)
> {
> - struct vfio_device_info info = { 0 };
> + struct vfio_device_info info = { .argsz = sizeof(info) };
> + int ret;
> +
> + ret = vfio_user_get_device_info(vbasedev->proxy, &info);
> + if (ret) {
> + error_setg_errno(errp, -ret, "get info failure");
> + return ret;
> + }
>
> vbasedev->fd = -1;
>
> diff --git a/hw/vfio-user/device.c b/hw/vfio-user/device.c
> new file mode 100644
> index 0000000000..4998019b30
> --- /dev/null
> +++ b/hw/vfio-user/device.c
> @@ -0,0 +1,48 @@
> +/*
> + * vfio protocol over a UNIX socket device handling.
> + *
> + * Copyright © 2018, 2021 Oracle and/or its affiliates.
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qemu/error-report.h"
> +
> +#include "hw/vfio-user/device.h"
> +#include "hw/vfio-user/trace.h"
> +
> +/*
> + * These are to defend against a malign server trying
> + * to force us to run out of memory.
> + */
> +#define VFIO_USER_MAX_REGIONS 100
> +#define VFIO_USER_MAX_IRQS 50
> +
> +int vfio_user_get_device_info(VFIOUserProxy *proxy,
> + struct vfio_device_info *info)
> +{
> + VFIOUserDeviceInfo msg;
> + uint32_t argsz = sizeof(msg) - sizeof(msg.hdr);
> +
> + memset(&msg, 0, sizeof(msg));
> + vfio_user_request_msg(&msg.hdr, VFIO_USER_DEVICE_GET_INFO, sizeof(msg), 0);
> + msg.argsz = argsz;
> +
> + vfio_user_send_wait(proxy, &msg.hdr, NULL, 0);
> + if (msg.hdr.flags & VFIO_USER_ERROR) {
> + return -msg.hdr.error_reply;
> + }
> + trace_vfio_user_get_info(msg.num_regions, msg.num_irqs);
> +
> + memcpy(info, &msg.argsz, argsz);
> +
> + /* defend against a malicious server */
> + if (info->num_regions > VFIO_USER_MAX_REGIONS ||
> + info->num_irqs > VFIO_USER_MAX_IRQS) {
> + error_printf("%s: invalid reply\n", __func__);
The error could be reported to the caller.
Thanks,
C.
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> diff --git a/hw/vfio-user/proxy.c b/hw/vfio-user/proxy.c
> index 0887d0aa1a..c6b6628505 100644
> --- a/hw/vfio-user/proxy.c
> +++ b/hw/vfio-user/proxy.c
> @@ -39,10 +39,6 @@ static void vfio_user_cb(void *opaque);
>
> static void vfio_user_request(void *opaque);
> static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg);
> -static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
> - VFIOUserFDs *fds, int rsize);
> -static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
> - uint32_t size, uint32_t flags);
>
> static inline void vfio_user_set_error(VFIOUserHdr *hdr, uint32_t err)
> {
> @@ -610,8 +606,8 @@ static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg)
> return 0;
> }
>
> -static void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
> - VFIOUserFDs *fds, int rsize)
> +void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,
> + VFIOUserFDs *fds, int rsize)
> {
> VFIOUserMsg *msg;
> int ret;
> @@ -782,8 +778,8 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
> g_free(proxy);
> }
>
> -static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
> - uint32_t size, uint32_t flags)
> +void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,
> + uint32_t size, uint32_t flags)
> {
> static uint16_t next_id;
>
> diff --git a/hw/vfio-user/meson.build b/hw/vfio-user/meson.build
> index cb958d0aa3..54af0da585 100644
> --- a/hw/vfio-user/meson.build
> +++ b/hw/vfio-user/meson.build
> @@ -1,6 +1,7 @@
> vfio_user_ss = ss.source_set()
> vfio_user_ss.add(files(
> 'container.c',
> + 'device.c',
> 'pci.c',
> 'proxy.c',
> ))
> diff --git a/hw/vfio-user/trace-events b/hw/vfio-user/trace-events
> index 7a3645024f..6b06a3ed82 100644
> --- a/hw/vfio-user/trace-events
> +++ b/hw/vfio-user/trace-events
> @@ -6,3 +6,4 @@ vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x"
> vfio_user_recv_request(uint16_t cmd) " command 0x%x"
> vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x"
> vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
> +vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
© 2016 - 2025 Red Hat, Inc.