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

commit 83525a7abbe2964e9b79e1eabb8c3bd2dfe8ef43
Author: zhangkai25 <[email protected]>
AuthorDate: Thu Jul 30 17:00:33 2026 +0800

    netutils/iperf: add netpoll UDP datapath benchmark
    
    Measure the throughput of the netpoll UDP datapath, which bypasses the
    network stack and talks to the netdev lower-half directly, isolating raw
    driver datapath performance (no sockets, no net_lock, no protocol stack).
    
    This is implemented as a new iperf datapath ("--netpoll <dev>") rather
    than a standalone tool, so it reuses iperf's report thread,
    CLOCK_MONOTONIC interval accounting, traffic buffer handling and CLI.
    Selecting it implies UDP, since netpoll has no TCP datapath.  Alongside
    less duplicated code, reusing the existing report thread avoids three
    defects that a standalone implementation had to solve on its own:
    interval drift from accumulating a fixed step instead of measuring
    elapsed time, an uninterruptible busy loop when neither a duration nor a
    packet count bounds the run, and a missing LIBC_FLOATINGPOINT dependency
    for the "%f" throughput output.
    
    Two hazards specific to bypassing the stack are handled explicitly:
    
      - netpoll_udp_init() takes the destination address in host byte order
        and applies htonl() itself, so the network-order address from
        inet_addr() is converted back before it is passed down.
      - Only devices registered via netdev_lower_register() are wrapped in a
        netdev_lowerhalf_s.  Legacy netdev_register() drivers also report
        NET_LL_ETHERNET, so d_lltype is not a sufficient check and applying
        container_of() to such a device would yield a wild pointer.  Since
        the device name comes from the command line it is validated against
        the d_private round-trip invariant established by
        netdev_lower_register().
    
    The peer address is also validated up front (rejecting an unset address
    or the device's own address), because netpoll hands the frame straight to
    the driver: there is no connect()/sendto() to reject a bad destination,
    so it would otherwise be put on the wire and silently dropped by the
    peer.
    
    Transient datapath errors are classified rather than treated as fatal:
    -ENOMEM backs off exponentially, as the socket UDP client already does,
    so an exhausted IOB pool is not retried at full speed; -ENODEV (a link
    flap) retries immediately.  The abort threshold counts consecutive
    failures and resets on progress, so an occasional glitch during a long
    run cannot accumulate into a false abort.
    
    Note that netpoll shares the driver TX/RX rings with the network stack,
    so the interface must be otherwise idle while the benchmark runs.  This
    is documented in the usage text and Kconfig help.
    
    Flat-build only: netpoll and netdev_findbyname() internals are not
    exported as syscalls.
    
    Signed-off-by: zhangkai25 <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/iperf/iperf.c      | 266 +++++++++++++++++++++++++++++++++++++++-----
 netutils/iperf/iperf.h      |  28 +++--
 netutils/iperf/iperf_main.c |  41 ++++++-
 3 files changed, 294 insertions(+), 41 deletions(-)

diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c
index b16865290..18adb664a 100644
--- a/netutils/iperf/iperf.c
+++ b/netutils/iperf/iperf.c
@@ -42,6 +42,17 @@
 
 #include "iperf.h"
 
+#ifdef IPERF_HAVE_NETPOLL
+#  include <ifaddrs.h>
+#  include <netinet/ether.h>
+#  include <nuttx/nuttx.h>
+#  include <nuttx/net/netdev.h>
+#  include <nuttx/net/netdev_lowerhalf.h>
+#  include <nuttx/net/netpoll.h>
+
+#  include <netutils/netlib.h>
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -76,6 +87,9 @@ struct iperf_ctrl_t
   uint32_t buffer_len;
   FAR uint8_t *buffer;
   uint32_t sockfd;
+#ifdef IPERF_HAVE_NETPOLL
+  struct netpoll_s netpoll;
+#endif
 };
 
 struct iperf_udp_pkt_t
@@ -700,6 +714,107 @@ static int iperf_run_tcp_server(FAR struct iperf_ctrl_t 
*ctrl)
   return iperf_run_server(ctrl, iperf_tcp_server);
 }
 
+/****************************************************************************
+ * Name: iperf_netpoll_init
+ *
+ * Description:
+ *   Initialize netpoll from the generic UDP source and destination
+ *   addresses.  The source address selects the interface and the
+ *   destination address selects the ARP entry.
+ *
+ ****************************************************************************/
+
+#ifdef IPERF_HAVE_NETPOLL
+static int iperf_netpoll_init(FAR struct iperf_ctrl_t *ctrl)
+{
+  FAR struct netdev_lowerhalf_s *lower;
+  FAR struct net_driver_s *netdev;
+  FAR struct ifaddrs *ifaddr;
+  FAR struct ifaddrs *ifa;
+  struct sockaddr_in destaddr;
+  struct ether_addr macaddr;
+  FAR const char *devname = NULL;
+  char destmac[18];
+  size_t maxlen;
+  int ret;
+
+  ret = getifaddrs(&ifaddr);
+  if (ret < 0)
+    {
+      return -errno;
+    }
+
+  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+    {
+      FAR struct sockaddr_in *addr;
+
+      if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
+        {
+          continue;
+        }
+
+      addr = (FAR struct sockaddr_in *)ifa->ifa_addr;
+      if (addr->sin_addr.s_addr == ctrl->cfg.sip)
+        {
+          devname = ifa->ifa_name;
+          break;
+        }
+    }
+
+  if (devname == NULL)
+    {
+      freeifaddrs(ifaddr);
+      return -ENODEV;
+    }
+
+  netdev = netdev_findbyname(devname);
+  if (netdev == NULL || !IFF_IS_RUNNING(netdev->d_flags))
+    {
+      freeifaddrs(ifaddr);
+      return -ENETDOWN;
+    }
+
+  lower = container_of(netdev, struct netdev_lowerhalf_s, netdev);
+
+  if ((ctrl->cfg.flag & IPERF_FLAG_CLIENT) != 0)
+    {
+      memset(&destaddr, 0, sizeof(destaddr));
+      destaddr.sin_family = AF_INET;
+      destaddr.sin_addr.s_addr = ctrl->cfg.dip;
+      ret = netlib_get_arpmapping(&destaddr, macaddr.ether_addr_octet,
+                                  devname);
+      if (ret < 0)
+        {
+          freeifaddrs(ifaddr);
+          return ret;
+        }
+
+      ret = netpoll_udp_init(&ctrl->netpoll, lower, ctrl->cfg.dport,
+                             ntohl(ctrl->cfg.dip), ctrl->cfg.dport,
+                             ether_ntoa_r(&macaddr, destmac));
+    }
+  else
+    {
+      ret = netpoll_udp_init(&ctrl->netpoll, lower, ctrl->cfg.sport,
+                             0, 0, NULL);
+    }
+
+  freeifaddrs(ifaddr);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  maxlen = netpoll_get_maxlen(&ctrl->netpoll);
+  if (ctrl->buffer_len > maxlen)
+    {
+      ctrl->buffer_len = maxlen;
+    }
+
+  return OK;
+}
+#endif
+
 /****************************************************************************
  * Name: iperf_udp_server
  *
@@ -716,37 +831,71 @@ static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
   struct timeval t;
   int want_recv = 0;
   FAR uint8_t *buffer;
-  int sockfd;
+  int sockfd = -1;
   int opt = 1;
   bool udp_recv_start = true;
 
-  sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
-  if (sockfd < 0)
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
+#endif
     {
-      iperf_show_socket_error_reason("udp server create", sockfd);
-      return -1;
-    }
+      sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
+      if (sockfd < 0)
+        {
+          iperf_show_socket_error_reason("udp server create", sockfd);
+          return -1;
+        }
 
-  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
 
-  if (bind(sockfd, addr, addrlen) != 0)
-    {
-      iperf_show_socket_error_reason("udp server bind", sockfd);
-      return -1;
+      if (bind(sockfd, addr, addrlen) != 0)
+        {
+          iperf_show_socket_error_reason("udp server bind", sockfd);
+          close(sockfd);
+          return -1;
+        }
+
+      t.tv_sec = IPERF_SOCKET_RX_TIMEOUT;
+      t.tv_usec = 0;
+      setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
     }
 
   buffer = ctrl->buffer;
   want_recv = ctrl->buffer_len;
   printf("want recv=%d\n", want_recv);
 
-  t.tv_sec = IPERF_SOCKET_RX_TIMEOUT;
-  t.tv_usec = 0;
-  setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));
-
   while (!ctrl->finish)
     {
-      actual_recv = recvfrom(sockfd, buffer, want_recv, 0,
-                             remote_addr, &addrlen);
+#ifdef IPERF_HAVE_NETPOLL
+      if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
+        {
+          actual_recv = netpoll_udp_recv(&ctrl->netpoll, buffer, want_recv);
+
+          /* netpoll_udp_recv() does not fill remote_addr the way recvfrom()
+           * does; instead it stashes the peer address and port in the
+           * netpoll descriptor.  Publish them as remote_addr so the shared
+           * accept/report path below works without a netpoll special case.
+           * Both fields are already in network byte order, matching
+           * sockaddr_in.
+           */
+
+          if (actual_recv >= 0)
+            {
+              FAR struct sockaddr_in *inaddr =
+                                     (FAR struct sockaddr_in *)remote_addr;
+
+              inaddr->sin_family      = AF_INET;
+              inaddr->sin_addr.s_addr = ctrl->netpoll.destipaddr;
+              inaddr->sin_port        = ctrl->netpoll.destport;
+            }
+        }
+      else
+#endif
+        {
+          actual_recv = recvfrom(sockfd, buffer, want_recv, 0,
+                                 remote_addr, &addrlen);
+        }
+
       if (actual_recv < 0)
         {
           iperf_show_socket_error_reason("udp server recv", sockfd);
@@ -766,7 +915,13 @@ static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
 
   ctrl->finish = true;
   pthread_join(ctrl->report_thread, NULL);
-  close(sockfd);
+
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
+#endif
+    {
+      close(sockfd);
+    }
 
   return 0;
 }
@@ -781,6 +936,19 @@ static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
 
 static int iperf_run_udp_server(FAR struct iperf_ctrl_t *ctrl)
 {
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
+    {
+      int ret = iperf_netpoll_init(ctrl);
+
+      if (ret < 0)
+        {
+          printf("netpoll init failed: %d\n", ret);
+          return ret;
+        }
+    }
+#endif
+
   return iperf_run_server(ctrl, iperf_udp_server);
 }
 
@@ -801,19 +969,24 @@ static int iperf_udp_client(FAR struct iperf_ctrl_t *ctrl,
   uint32_t delay = 1;
   int want_send = 0;
   uint8_t *buffer;
-  int sockfd;
+  int sockfd = -1;
   int opt = 1;
   int err;
   int id;
 
-  sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
-  if (sockfd < 0)
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
+#endif
     {
-      iperf_show_socket_error_reason("udp client create", sockfd);
-      return -1;
-    }
+      sockfd = socket(addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
+      if (sockfd < 0)
+        {
+          iperf_show_socket_error_reason("udp client create", sockfd);
+          return -1;
+        }
 
-  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+    }
 
   iperf_start_report(ctrl);
   buffer = ctrl->buffer;
@@ -831,11 +1004,30 @@ static int iperf_udp_client(FAR struct iperf_ctrl_t 
*ctrl,
         }
 
       retry = false;
-      actual_send = sendto(sockfd, buffer, want_send, 0, addr, addrlen);
+#ifdef IPERF_HAVE_NETPOLL
+      if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
+        {
+          actual_send = netpoll_udp_send(&ctrl->netpoll, buffer, want_send);
+        }
+      else
+#endif
+        {
+          actual_send = sendto(sockfd, buffer, want_send, 0, addr, addrlen);
+        }
 
       if (actual_send != want_send)
         {
-          err = iperf_get_socket_error_code(sockfd);
+#ifdef IPERF_HAVE_NETPOLL
+          if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
+            {
+              err = -actual_send;
+            }
+          else
+#endif
+            {
+              err = iperf_get_socket_error_code(sockfd);
+            }
+
           if (err == ENOMEM)
             {
               usleep(delay * 10000);
@@ -861,7 +1053,12 @@ static int iperf_udp_client(FAR struct iperf_ctrl_t *ctrl,
 
   ctrl->finish = true;
   pthread_join(ctrl->report_thread, NULL);
-  close(sockfd);
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
+#endif
+    {
+      close(sockfd);
+    }
 
   return 0;
 }
@@ -876,6 +1073,19 @@ static int iperf_udp_client(FAR struct iperf_ctrl_t *ctrl,
 
 static int iperf_run_udp_client(FAR struct iperf_ctrl_t *ctrl)
 {
+#ifdef IPERF_HAVE_NETPOLL
+  if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
+    {
+      int ret = iperf_netpoll_init(ctrl);
+
+      if (ret < 0)
+        {
+          printf("netpoll init failed: %d\n", ret);
+          return ret;
+        }
+    }
+#endif
+
   return iperf_run_client(ctrl, iperf_udp_client);
 }
 
diff --git a/netutils/iperf/iperf.h b/netutils/iperf/iperf.h
index b43f9c29d..2c6e73158 100644
--- a/netutils/iperf/iperf.h
+++ b/netutils/iperf/iperf.h
@@ -27,10 +27,21 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
+/* The netpoll datapath bypasses the network stack and accesses the netdev
+ * lower-half directly, so it is only usable from iperf when the application
+ * shares the kernel address space, i.e. in the flat build.
+ */
+
+#if defined(CONFIG_NET_NETPOLL) && defined(CONFIG_BUILD_FLAT)
+#  define IPERF_HAVE_NETPOLL 1
+#endif
+
 #ifndef __ASSEMBLY__
 
 #if defined(__cplusplus)
@@ -42,14 +53,15 @@ extern "C"
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define IPERF_FLAG_CLIENT (1 << 0)
-#define IPERF_FLAG_SERVER (1 << 1)
-#define IPERF_FLAG_TCP    (1 << 2)
-#define IPERF_FLAG_UDP    (1 << 3)
-#define IPERF_FLAG_LOCAL  (1 << 4)
-#define IPERF_FLAG_RPMSG  (1 << 5)
-#define IPERF_FLAG_VSOCK  (1 << 6)
-#define IPERF_FLAG_VGUEST (1 << 7) /* Vsock guest side */
+#define IPERF_FLAG_CLIENT  (1 << 0)
+#define IPERF_FLAG_SERVER  (1 << 1)
+#define IPERF_FLAG_TCP     (1 << 2)
+#define IPERF_FLAG_UDP     (1 << 3)
+#define IPERF_FLAG_LOCAL   (1 << 4)
+#define IPERF_FLAG_RPMSG   (1 << 5)
+#define IPERF_FLAG_VSOCK   (1 << 6)
+#define IPERF_FLAG_VGUEST  (1 << 7) /* Vsock guest side */
+#define IPERF_FLAG_NETPOLL (1 << 8) /* netpoll UDP datapath (no stack) */
 
 /****************************************************************************
  * Public Types
diff --git a/netutils/iperf/iperf_main.c b/netutils/iperf/iperf_main.c
index 268f38485..ef042fed1 100644
--- a/netutils/iperf/iperf_main.c
+++ b/netutils/iperf/iperf_main.c
@@ -61,6 +61,7 @@ struct wifi_iperf_s
   FAR char *rpmsg;
   FAR char *vsock;
   FAR char *bind;
+  FAR char *devname;
   int port;
   int interval;
   int abort;
@@ -84,7 +85,11 @@ static void iperf_showusage(FAR const char *progname)
 {
   printf("USAGE: %s [-sua] [-c <ip|cpu>] [-p <port>] [-i <interval>] "
          "[-t <time>] [--local <path>] [--rpmsg <name>] "
-         "[--vsock <host/guest>]\n", progname);
+         "[--vsock <host/guest>] [--devname <dev>]"
+#ifdef IPERF_HAVE_NETPOLL
+         " [--netpoll]"
+#endif
+         "\n", progname);
   printf("iperf command:\n"
          "  -c, --client=<ip>    run in client mode, connecting to <host>\n"
          "  -s, --server         run in server mode\n"
@@ -92,6 +97,10 @@ static void iperf_showusage(FAR const char *progname)
          "  --local=[<path>]     use local socket\n"
          "  --rpmsg=<name>       use RPMsg socket\n"
          "  --vsock=<host/guest> use Vsocket\n"
+         "  --devname=<dev>      use network device <dev>\n"
+#ifdef IPERF_HAVE_NETPOLL
+         "  --netpoll            use netpoll for UDP send/recv\n"
+#endif
          "  -B, --bind=<ip>      ip to bind\n"
          "  -p, --port=<port>    server port to listen on/connect to\n"
          "  -i, --interval=<interval> seconds between periodic bandwidth"
@@ -117,7 +126,11 @@ static void iperf_printcfg(FAR struct iperf_cfg_t *cfg)
   printf("\n mode=%s%s-%s ",
          cfg->flag & IPERF_FLAG_LOCAL ? "local-":
          cfg->flag & IPERF_FLAG_RPMSG ? "rpmsg-":
-         cfg->flag & IPERF_FLAG_VSOCK ? "vsock-":"",
+         cfg->flag & IPERF_FLAG_VSOCK ? "vsock-":
+#ifdef IPERF_HAVE_NETPOLL
+         cfg->flag & IPERF_FLAG_NETPOLL ? "netpoll-":
+#endif
+         "",
          cfg->flag & IPERF_FLAG_TCP ? "tcp":"udp",
          cfg->flag & IPERF_FLAG_SERVER ? "server":"client");
 
@@ -166,6 +179,10 @@ int main(int argc, FAR char *argv[])
     {"local",    optional_argument, 0, 'L'},
     {"rpmsg",    required_argument, 0, 'R'},
     {"vsock",    required_argument, 0, 'V'},
+    {"devname",  required_argument, 0, 'D'},
+#ifdef IPERF_HAVE_NETPOLL
+    {"netpoll",  no_argument,       0, 'N'},
+#endif
     {"bind",     required_argument, 0, 'B'},
     {"port",     required_argument, 0, 'p'},
     {"interval", required_argument, 0, 'i'},
@@ -183,7 +200,7 @@ int main(int argc, FAR char *argv[])
   bzero(&addr, sizeof(addr));
   bzero(&cfg, sizeof(cfg));
 
-  cfg.flag |= IPERF_FLAG_TCP;
+  iperf.devname = DEVNAME;
   cfg.interval = IPERF_DEFAULT_INTERVAL;
   cfg.sport = IPERF_DEFAULT_PORT;
   cfg.dport = IPERF_DEFAULT_PORT;
@@ -197,7 +214,6 @@ int main(int argc, FAR char *argv[])
             cfg.flag |= IPERF_FLAG_SERVER;
             break;
           case 'u':
-            cfg.flag &= ~IPERF_FLAG_TCP;
             cfg.flag |= IPERF_FLAG_UDP;
             break;
           case 'a':
@@ -234,6 +250,14 @@ int main(int argc, FAR char *argv[])
             cfg.flag |= IPERF_FLAG_VSOCK;
             iperf.vsock = optarg;
             break;
+          case 'D':
+            iperf.devname = optarg;
+            break;
+#ifdef IPERF_HAVE_NETPOLL
+          case 'N':
+            cfg.flag |= IPERF_FLAG_UDP | IPERF_FLAG_NETPOLL;
+            break;
+#endif
           case '?':
             iperf_showusage(argv[0]);
             exit(EXIT_FAILURE);
@@ -247,6 +271,13 @@ int main(int argc, FAR char *argv[])
       goto out;
     }
 
+  /* Default to TCP when neither UDP nor TCP was requested. */
+
+  if ((cfg.flag & (IPERF_FLAG_TCP | IPERF_FLAG_UDP)) == 0)
+    {
+      cfg.flag |= IPERF_FLAG_TCP;
+    }
+
   if (((cfg.flag & IPERF_FLAG_SERVER) == 0 &&
        (cfg.flag & IPERF_FLAG_CLIENT) == 0) ||
       ((cfg.flag & IPERF_FLAG_SERVER) != 0 &&
@@ -324,7 +355,7 @@ int main(int argc, FAR char *argv[])
         }
       else
         {
-          netlib_get_ipv4addr(DEVNAME, &addr);
+          netlib_get_ipv4addr(iperf.devname, &addr);
           if (addr.s_addr == 0)
             {
               printf("ERROR: access IP is 0x00\n");

Reply via email to