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-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 249ef79f4 nshlib: add the virtio command
249ef79f4 is described below

commit 249ef79f4c2cf8f3e708461120f1689d83044bea
Author: Yongrong Wang <[email protected]>
AuthorDate: Thu Nov 12 18:40:45 2026 +0800

    nshlib: add the virtio command
    
    Add "virtio dump [type]", which opens /dev/virtio and issues
    VIRTIOIOC_DUMP so the kernel prints the registered VirtIO devices and
    their virtqueues to syslog. The optional type is the device name as
    printed by the dump (e.g. Block, Network); "all" or no argument dumps
    every device. Disable it with CONFIG_NSH_DISABLE_VIRTIO.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 nshlib/Kconfig       |  5 +++
 nshlib/nsh.h         |  4 +++
 nshlib/nsh_command.c |  4 +++
 nshlib/nsh_syscmds.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+)

diff --git a/nshlib/Kconfig b/nshlib/Kconfig
index 026d9acbc..9d69636e2 100644
--- a/nshlib/Kconfig
+++ b/nshlib/Kconfig
@@ -676,6 +676,11 @@ config NSH_DISABLE_RPTUN
        default DEFAULT_SMALL
        depends on RPTUN && !NSH_DISABLE_RPMSG
 
+config NSH_DISABLE_VIRTIO
+       bool "Disable virtio"
+       default DEFAULT_SMALL
+       depends on DRIVERS_VIRTIO
+
 config NSH_DISABLE_SET
        bool "Disable set"
        default DEFAULT_SMALL
diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index b493a01c9..00cdcadab 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -1196,6 +1196,10 @@ int cmd_switchboot(FAR struct nsh_vtbl_s *vtbl, int 
argc, FAR char **argv);
   int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
 #endif
 
+#if defined(CONFIG_DRIVERS_VIRTIO) && !defined(CONFIG_NSH_DISABLE_VIRTIO)
+  int cmd_virtio(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
+#endif
+
 #ifndef CONFIG_NSH_DISABLE_SET
   int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
 #endif
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index 59fcc2559..42e2fbf0c 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -670,6 +670,10 @@ static const struct cmdmap_s g_cmdmap[] =
 #  endif
 #endif
 
+#if defined(CONFIG_DRIVERS_VIRTIO) && !defined(CONFIG_NSH_DISABLE_VIRTIO)
+  CMD_MAP("virtio",   cmd_virtio,   2, 3, "dump [type]"),
+#endif
+
 #if !defined(CONFIG_DISABLE_MOUNTPOINT)
 #  ifndef CONFIG_NSH_DISABLE_UMOUNT
   CMD_MAP("umount",   cmd_umount,   2, 3, "[-f] <dir-path>"),
diff --git a/nshlib/nsh_syscmds.c b/nshlib/nsh_syscmds.c
index 459e01370..4b200f158 100644
--- a/nshlib/nsh_syscmds.c
+++ b/nshlib/nsh_syscmds.c
@@ -30,6 +30,7 @@
 #include <nuttx/rpmsg/rpmsg.h>
 #include <nuttx/rptun/rptun.h>
 #include <nuttx/streams.h>
+#include <nuttx/virtio/virtio.h>
 #include <sys/boardctl.h>
 #include <sys/ioctl.h>
 #include <sys/param.h>
@@ -1104,3 +1105,90 @@ int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_virtio_usage
+ ****************************************************************************/
+
+#if defined(CONFIG_DRIVERS_VIRTIO) && !defined(CONFIG_NSH_DISABLE_VIRTIO)
+static int cmd_virtio_usage(FAR struct nsh_vtbl_s *vtbl,
+                            FAR char **argv)
+{
+  nsh_output(vtbl, "Usage:\n");
+  nsh_output(vtbl, "  %s dump [type]  - Dump VirtIO device info\n",
+             argv[0]);
+  nsh_output(vtbl, "  %s -h           - Show this help message\n\n",
+             argv[0]);
+  nsh_output(vtbl, "Supported types:\n");
+  nsh_output(vtbl, "  all              - Dump all devices (default)\n");
+  nsh_output(vtbl, "  Network          - Network devices\n");
+  nsh_output(vtbl, "  Block            - Block devices\n");
+  nsh_output(vtbl, "  Console          - Console devices\n");
+  nsh_output(vtbl, "  Entropy          - Entropy/RNG devices\n");
+  nsh_output(vtbl, "  RPMSG            - RPMSG devices\n");
+  nsh_output(vtbl, "  Serial           - Serial devices\n");
+  nsh_output(vtbl, "  GPU              - GPU devices\n");
+  nsh_output(vtbl, "  Input            - Input devices\n");
+  nsh_output(vtbl, "  Vsock            - Vsock devices\n");
+  nsh_output(vtbl, "  Crypto           - Crypto devices\n");
+  nsh_output(vtbl, "  Sound            - Sound devices\n");
+  nsh_output(vtbl, "  RPMB             - RPMB devices\n\n");
+  nsh_output(vtbl, "Description:\n");
+  nsh_output(vtbl, "  Display information about registered VirtIO\n");
+  nsh_output(vtbl, "  devices. Output is sent to syslog.\n");
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cmd_virtio
+ ****************************************************************************/
+
+int cmd_virtio(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  FAR const char *dev_name = NULL;
+  int fd;
+  int ret;
+
+  if (argc < 2)
+    {
+      nsh_output(vtbl, g_fmtargrequired, argv[0]);
+      return ERROR;
+    }
+  else if (strcmp(argv[1], "-h") == 0)
+    {
+      return cmd_virtio_usage(vtbl, argv);
+    }
+  else if (strcmp(argv[1], "dump") != 0)
+    {
+      nsh_output(vtbl, g_fmtarginvalid, argv[1]);
+      return ERROR;
+    }
+
+  /* Get optional type argument, pass directly to kernel */
+
+  if (argc >= 3)
+    {
+      dev_name = argv[2];
+    }
+
+  fd = open("/dev/virtio", O_CLOEXEC);
+  if (fd < 0)
+    {
+      nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
+      return ERROR;
+    }
+
+  /* Pass type name string to kernel, kernel will parse it */
+
+  ret = ioctl(fd, VIRTIOIOC_DUMP, (unsigned long)dev_name);
+  close(fd);
+
+  if (ret < 0)
+    {
+      nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
+      return ERROR;
+    }
+
+  return OK;
+}
+#endif

Reply via email to