This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b0494782ef drivers/vhost: add /dev/vhost and VHOSTIOC_DUMP
8b0494782ef is described below

commit 8b0494782efc21c9e7d61ee82949a6e7262b251c
Author: wangyongrong <[email protected]>
AuthorDate: Mon May 18 17:10:37 2026 +0800

    drivers/vhost: add /dev/vhost and VHOSTIOC_DUMP
    
    Like /dev/virtio on the driver side, register /dev/vhost with a
    VHOSTIOC_DUMP ioctl that walks the vhost bus (bound and deferred
    devices) and prints each device, its driver and its virtqueues, then
    calls a new optional dump callback in struct vhost_driver. The argument
    is an optional device name as returned by virtio_dev_name(); NULL or
    "all" dumps every device.
    
    VHOSTIOC_DUMP has the same value as VIRTIOIOC_DUMP, so one command
    number drives both nodes and no new ioctl base is needed.
    
    Signed-off-by: wangyongrong <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/vhost/vhost.c       | 135 ++++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/vhost/vhost.h |   9 +++
 2 files changed, 144 insertions(+)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 2de63110e2a..b846caad7e7 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -26,8 +26,14 @@
 
 #include <nuttx/debug.h>
 
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+
+#include <nuttx/fs/fs.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/mutex.h>
+#include <nuttx/nuttx.h>
 #include <nuttx/queue.h>
 #include <nuttx/wqueue.h>
 #include <nuttx/vhost/vhost.h>
@@ -72,10 +78,27 @@ struct vhost_device_item_s
                                  */
 };
 
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int vhost_dev_ioctl(FAR struct file *filep, int cmd,
+                           unsigned long arg);
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
+static const struct file_operations g_vhost_dev_ops =
+{
+  NULL,             /* open */
+  NULL,             /* close */
+  NULL,             /* read */
+  NULL,             /* write */
+  NULL,             /* seek */
+  vhost_dev_ioctl,  /* ioctl */
+};
+
 static struct vhost_bus_s g_vhost_bus =
 {
   .lock = NXMUTEX_INITIALIZER,
@@ -85,6 +108,110 @@ static struct vhost_bus_s g_vhost_bus =
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: vhost_dump_device
+ ****************************************************************************/
+
+static int vhost_dump_device(FAR struct vhost_device_item_s *item,
+                             FAR const char *name)
+{
+  FAR struct vhost_device *hdev = item->device;
+  FAR const char *dev_name;
+  unsigned int i;
+
+  dev_name = virtio_dev_name(hdev->id.device);
+  dev_name = dev_name == NULL ? "unknown" : dev_name;
+
+  /* Apply type filter if specified, "all" means no filter */
+
+  if (strcasecmp(name, "all") != 0 && strcasecmp(dev_name, name) != 0)
+    {
+      return 0;
+    }
+
+  syslog(LOG_EMERG, "  [%s] id=%" PRIu32 " hdev=%p driver=%p priv=%p\n",
+         dev_name, hdev->id.device, hdev, item->driver, hdev->priv);
+
+  /* Call driver-specific dump if available */
+
+  if (item->driver && item->driver->dump)
+    {
+      item->driver->dump(hdev);
+    }
+
+  /* Dump virtqueue information */
+
+  for (i = 0; i < hdev->vrings_num; i++)
+    {
+      virtqueue_dump(hdev->vrings_info[i].vq);
+    }
+
+  return 1;
+}
+
+/****************************************************************************
+ * Name: vhost_dump
+ ****************************************************************************/
+
+static void vhost_dump(FAR const char *name)
+{
+  FAR struct vhost_device_item_s *item;
+  int count = 0;
+  int ret;
+
+  syslog(LOG_EMERG, "VHost devices:\n");
+
+  name = name == NULL ? "all" : name;
+  ret = nxmutex_lock(&g_vhost_bus.lock);
+  if (ret < 0)
+    {
+      syslog(LOG_EMERG, "  Failed to acquire lock\n");
+      return;
+    }
+
+  dq_for_every_entry(&g_vhost_bus.device, item,
+                     struct vhost_device_item_s, node)
+    {
+      count += vhost_dump_device(item, name);
+    }
+
+  dq_for_every_entry(&g_vhost_bus.defered_device, item,
+                     struct vhost_device_item_s, node)
+    {
+      count += vhost_dump_device(item, name);
+    }
+
+  if (count == 0)
+    {
+      syslog(LOG_EMERG, "  No devices of type '%s' found\n", name);
+    }
+
+  nxmutex_unlock(&g_vhost_bus.lock);
+}
+
+/****************************************************************************
+ * Name: vhost_dev_ioctl
+ ****************************************************************************/
+
+static int vhost_dev_ioctl(FAR struct file *filep, int cmd,
+                           unsigned long arg)
+{
+  int ret = -ENOTTY;
+
+  switch (cmd)
+    {
+      case VHOSTIOC_DUMP:
+        vhost_dump((FAR const char *)(uintptr_t)arg);
+        ret = OK;
+        break;
+
+      default:
+        break;
+    }
+
+  return ret;
+}
+
 /****************************************************************************
  * Name: vhost_status_driver_ok
  ****************************************************************************/
@@ -522,6 +649,14 @@ void vhost_register_drivers(void)
     }
 #endif
 
+  /* Register vhost character device */
+
+  ret = register_driver("/dev/vhost", &g_vhost_dev_ops, 0200, NULL);
+  if (ret < 0)
+    {
+      vhosterr("register_driver /dev/vhost failed, ret=%d\n", ret);
+    }
+
 #ifdef CONFIG_DRIVERS_VHOST_BLK
   ret = vhost_register_blk_driver();
   if (ret < 0)
diff --git a/include/nuttx/vhost/vhost.h b/include/nuttx/vhost/vhost.h
index 8a4515b9925..5211d86219f 100644
--- a/include/nuttx/vhost/vhost.h
+++ b/include/nuttx/vhost/vhost.h
@@ -34,12 +34,20 @@
 #include <nuttx/queue.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/virtio/virtio-config.h>
+#include <nuttx/virtio/virtio.h>
 #include <openamp/open_amp.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
+/* VHost ioctl command definitions.  They carry the same values as the
+ * VirtIO ones, so one command number drives both /dev/virtio and
+ * /dev/vhost.
+ */
+
+#define VHOSTIOC_DUMP         VIRTIOIOC_DUMP
+
 /****************************************************************************
  * Public Type Definitions
  ****************************************************************************/
@@ -70,6 +78,7 @@ struct vhost_driver
   uint32_t           device;   /* device id */
   CODE int         (*probe)(FAR struct vhost_device *hdev);
   CODE void        (*remove)(FAR struct vhost_device *hdev);
+  CODE void        (*dump)(FAR struct vhost_device *hdev);
   struct dq_entry_s  node;
 };
 

Reply via email to