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 7701b4935 nshlib: add a ping subcommand to the virtio and vhost 
commands
7701b4935 is described below

commit 7701b49351a8d30db3959215b2426b18a671be3b
Author: wangyongrong <[email protected]>
AuthorDate: Tue Aug 18 14:49:17 2026 +0800

    nshlib: add a ping subcommand to the virtio and vhost commands
    
    Add "virtio ping <name> <times> <len> <period(ms)>" (and the same for
    vhost), which runs a ping measurement through VIRTIOIOC_PING on
    /dev/virtio, or VHOSTIOC_PING on /dev/vhost, and leaves the result in
    syslog. Raise the maximum argument count of both commands to 6, so the
    framework passes the extra arguments to the handler.
    
    Signed-off-by: wangyongrong <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 nshlib/nsh_command.c |  6 +++--
 nshlib/nsh_syscmds.c | 64 ++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 54 insertions(+), 16 deletions(-)

diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index ef3c0abaa..47453e02a 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -672,11 +672,13 @@ static const struct cmdmap_s g_cmdmap[] =
 #endif
 
 #if defined(CONFIG_DRIVERS_VIRTIO) && !defined(CONFIG_NSH_DISABLE_VIRTIO)
-  CMD_MAP("virtio",   cmd_virtio,   2, 3, "dump [type]"),
+  CMD_MAP("virtio",   cmd_virtio,   2, 6,
+          "dump [type] | ping <name> <times> <len> <period(ms)>"),
 #endif
 
 #if defined(CONFIG_DRIVERS_VHOST) && !defined(CONFIG_NSH_DISABLE_VHOST)
-  CMD_MAP("vhost",    cmd_vhost,    2, 3, "dump [type]"),
+  CMD_MAP("vhost",    cmd_vhost,    2, 6,
+          "dump [type] | ping <name> <times> <len> <period(ms)>"),
 #endif
 
 #if !defined(CONFIG_DISABLE_MOUNTPOINT)
diff --git a/nshlib/nsh_syscmds.c b/nshlib/nsh_syscmds.c
index 2aab0b874..b62c0e177 100644
--- a/nshlib/nsh_syscmds.c
+++ b/nshlib/nsh_syscmds.c
@@ -1139,14 +1139,28 @@ static int cmd_virtio_common_usage(FAR struct 
nsh_vtbl_s *vtbl,
   nsh_output(vtbl, "Description:\n");
   nsh_output(vtbl, "  Display information about registered %s\n", argv[0]);
   nsh_output(vtbl, "  devices. Output is sent to syslog.\n");
+  nsh_output(vtbl, "  %s ping <name> <times> <len> "
+                   "<period(ms)>\n\n", argv[0]);
+  nsh_output(vtbl, "<name>       Instance name, as printed by "
+                   "'%s dump', or 'all'.\n", argv[0]);
+  nsh_output(vtbl, "<times>      Number of ping operations.\n");
+  nsh_output(vtbl, "<len>        Request length in bytes, header "
+                   "included.\n");
+  nsh_output(vtbl, "<period(ms)> Sleep interval between two "
+                   "operations.\n\n");
+  nsh_output(vtbl, "Results are printed to syslog. Use '%s dump all' to "
+                   "list devices\n", argv[0]);
+  nsh_output(vtbl, "(ping shows up as 'unknown' with id=63, it is not in "
+                   "the openamp name table).\n");
   return OK;
 }
 
 static int cmd_virtio_common(FAR struct nsh_vtbl_s *vtbl, int argc,
-                             FAR char **argv, FAR const char *devpath,
-                             int ioc_cmd)
+                             FAR char **argv, FAR const char *devpath)
 {
-  FAR const char *dev_name = NULL;
+  struct virtio_ping_s ping;
+  unsigned long arg = 0;
+  int cmd;
   int fd;
   int ret;
 
@@ -1159,17 +1173,41 @@ static int cmd_virtio_common(FAR struct nsh_vtbl_s 
*vtbl, int argc,
     {
       return cmd_virtio_common_usage(vtbl, argv);
     }
-  else if (strcmp(argv[1], "dump") != 0)
+  else if (strcmp(argv[1], "dump") == 0)
     {
-      nsh_output(vtbl, g_fmtarginvalid, argv[1]);
-      return ERROR;
+      /* Get optional type argument, pass directly to kernel, which will
+       * parse it.
+       */
+
+      cmd = VIRTIOIOC_DUMP;
+      if (argc >= 3)
+        {
+          arg = (unsigned long)argv[2];
+        }
     }
+  else if (strcmp(argv[1], "ping") == 0)
+    {
+      /* <cmd> ping <name> <times> <len> <period> */
+
+      if (argc < 6)
+        {
+          nsh_error(vtbl, g_fmtargrequired, argv[0]);
+          return ERROR;
+        }
 
-  /* Get optional type argument, pass directly to kernel */
+      memset(&ping, 0, sizeof(ping));
+      strlcpy(ping.name, argv[2], VIRTIO_PING_NAMELEN);
+      ping.times = atoi(argv[3]);
+      ping.len   = atoi(argv[4]);
+      ping.sleep = atoi(argv[5]);
 
-  if (argc >= 3)
+      cmd = VIRTIOIOC_PING;
+      arg = (unsigned long)(uintptr_t)&ping;
+    }
+  else
     {
-      dev_name = argv[2];
+      nsh_output(vtbl, g_fmtarginvalid, argv[1]);
+      return ERROR;
     }
 
   fd = open(devpath, O_CLOEXEC);
@@ -1179,9 +1217,7 @@ static int cmd_virtio_common(FAR struct nsh_vtbl_s *vtbl, 
int argc,
       return ERROR;
     }
 
-  /* Pass type name string to kernel, kernel will parse it */
-
-  ret = ioctl(fd, ioc_cmd, (unsigned long)dev_name);
+  ret = ioctl(fd, cmd, arg);
   close(fd);
 
   if (ret < 0)
@@ -1201,7 +1237,7 @@ static int cmd_virtio_common(FAR struct nsh_vtbl_s *vtbl, 
int argc,
 #if defined(CONFIG_DRIVERS_VIRTIO) && !defined(CONFIG_NSH_DISABLE_VIRTIO)
 int cmd_virtio(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
-  return cmd_virtio_common(vtbl, argc, argv, "/dev/virtio", VIRTIOIOC_DUMP);
+  return cmd_virtio_common(vtbl, argc, argv, "/dev/virtio");
 }
 #endif
 
@@ -1212,6 +1248,6 @@ int cmd_virtio(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #if defined(CONFIG_DRIVERS_VHOST) && !defined(CONFIG_NSH_DISABLE_VHOST)
 int cmd_vhost(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
-  return cmd_virtio_common(vtbl, argc, argv, "/dev/vhost", VHOSTIOC_DUMP);
+  return cmd_virtio_common(vtbl, argc, argv, "/dev/vhost");
 }
 #endif

Reply via email to