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 a9abd2f00 testing/nettest: Add a close test for packet sockets.
a9abd2f00 is described below

commit a9abd2f0000f6448c67660b45b24e92a59dbd6b9
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 21:20:23 2026 +0100

    testing/nettest: Add a close test for packet sockets.
    
    test_pkt_lo_close sends IPv4 packets to 127.0.0.1 on a packet socket
    bound to lo, then closes the socket.  The free IOB count of
    /proc/iobinfo must be the same before and after four such rounds.
    
    In the second set of rounds the test runs above the work queue that
    sends the packets.  With CONFIG_NET_PKT_WRITE_BUFFERS, the packets then
    come back to the socket while close() waits for its write buffers to
    drain.  close() must free these frames too.
    
    send() uses MSG_DONTWAIT: when the lost frames hold all the IOB queue
    entries, a send fails and the test reports it; it does not wait for ever.
    The test skips when /proc/iobinfo is not there.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/pkt/test_pkt.c    |   1 +
 testing/nettest/pkt/test_pkt.h    |   5 +-
 testing/nettest/pkt/test_pkt_lo.c | 119 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 2 deletions(-)

diff --git a/testing/nettest/pkt/test_pkt.c b/testing/nettest/pkt/test_pkt.c
index 3f15a4cae..7e858bc1e 100644
--- a/testing/nettest/pkt/test_pkt.c
+++ b/testing/nettest/pkt/test_pkt.c
@@ -42,6 +42,7 @@ int main(int argc, FAR char *argv[])
     {
       cmocka_unit_test(test_pkt_lo_send),
       cmocka_unit_test(test_pkt_lo_frag),
+      cmocka_unit_test(test_pkt_lo_close),
     };
 
   return cmocka_run_group_tests(pkt_tests, NULL, NULL);
diff --git a/testing/nettest/pkt/test_pkt.h b/testing/nettest/pkt/test_pkt.h
index 958bd54d1..036dafd05 100644
--- a/testing/nettest/pkt/test_pkt.h
+++ b/testing/nettest/pkt/test_pkt.h
@@ -38,12 +38,13 @@
  *
  * Description:
  *   Packet sockets bound to the loopback device: a frame sent on lo comes
- *   back, and the reassembly timeout of an IPv4 fragment sent on lo does
- *   not stop the kernel.
+ *   back, the reassembly timeout of an IPv4 fragment sent on lo does not
+ *   stop the kernel, and close() frees the frames queued on a socket.
  *
  ****************************************************************************/
 
 void test_pkt_lo_send(FAR void **state);
 void test_pkt_lo_frag(FAR void **state);
+void test_pkt_lo_close(FAR void **state);
 
 #endif /* __APPS_TESTING_NETTEST_PKT_TEST_PKT_H */
diff --git a/testing/nettest/pkt/test_pkt_lo.c 
b/testing/nettest/pkt/test_pkt_lo.c
index bd9ae23e2..32b995572 100644
--- a/testing/nettest/pkt/test_pkt_lo.c
+++ b/testing/nettest/pkt/test_pkt_lo.c
@@ -28,11 +28,13 @@
 
 #include <errno.h>
 #include <poll.h>
+#include <sched.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <cmocka.h>
@@ -59,6 +61,8 @@
 #define TEST_PKT_TIMXCEED 11      /* ICMP Time Exceeded */
 #define TEST_PKT_FRAGTIME 1       /* Its code: reassembly time exceeded */
 #define TEST_PKT_WAIT_MS  2000    /* Time to wait for a frame */
+#define TEST_PKT_ROUNDS   4       /* Open, send and close rounds */
+#define TEST_PKT_NFRAMES  2       /* Packets sent in one round */
 
 /****************************************************************************
  * Private Functions
@@ -151,6 +155,91 @@ static ssize_t test_pkt_read(int sd, FAR uint8_t *buf, 
size_t len, int ms)
   return recv(sd, buf, len, MSG_DONTWAIT);
 }
 
+/* The number of free IOBs, from /proc/iobinfo; -1 if it is not there */
+
+static int test_pkt_iob_nfree(void)
+{
+  FAR FILE *f;
+  char line[64];
+  int ntotal;
+  int nfree = -1;
+
+  f = fopen("/proc/iobinfo", "re");
+  if (f == NULL)
+    {
+      return -1;
+    }
+
+  /* The first line is the header, the second line has the counts */
+
+  if (fgets(line, sizeof(line), f) == NULL ||
+      fgets(line, sizeof(line), f) == NULL ||
+      sscanf(line, "%d %d", &ntotal, &nfree) != 2)
+    {
+      nfree = -1;
+    }
+
+  fclose(f);
+  return nfree;
+}
+
+/* Open a socket on lo, send IPv4 packets to 127.0.0.1 and close it, in
+ * TEST_PKT_ROUNDS rounds.  With 'high', send() and close() run above the
+ * work queue that sends the packets.  send() does not wait: a lost IOB
+ * gives a failed send, not a hang.  Returns the number of IOBs lost.
+ */
+
+static int test_pkt_close_rounds(bool high, FAR int *nfail)
+{
+  struct sched_param oparam;
+  struct sched_param param;
+  uint8_t frame[64];
+  size_t len;
+  int before;
+  int after;
+  int sd;
+  int i;
+  int j;
+
+  usleep(100 * 1000);
+  before = test_pkt_iob_nfree();
+  assert_true(before > 0);
+
+  assert_return_code(sched_getparam(0, &oparam), errno);
+  param.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
+  *nfail = 0;
+
+  for (i = 0; i < TEST_PKT_ROUNDS; i++)
+    {
+      sd = test_pkt_open(ETH_P_ALL);
+      if (high)
+        {
+          assert_return_code(sched_setparam(0, &param), errno);
+        }
+
+      for (j = 0; j < TEST_PKT_NFRAMES; j++)
+        {
+          len = test_pkt_ipv4(frame, 8, (i << 8) | j, 0);
+          if (send(sd, frame, len, MSG_DONTWAIT) < 0)
+            {
+              (*nfail)++;
+            }
+        }
+
+      close(sd);
+      if (high)
+        {
+          assert_return_code(sched_setparam(0, &oparam), errno);
+        }
+    }
+
+  /* Let the work queue finish */
+
+  usleep(200 * 1000);
+  after = test_pkt_iob_nfree();
+  return before - after;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -244,3 +333,33 @@ void test_pkt_lo_frag(FAR void **state)
   skip();
 #endif
 }
+
+/****************************************************************************
+ * Name: test_pkt_lo_close
+ *
+ * Description:
+ *   close() of a packet socket on lo frees every frame queued on it.  This
+ *   also covers a frame that comes back while close() waits for the write
+ *   buffers of the socket to drain (CONFIG_NET_PKT_WRITE_BUFFERS).
+ *
+ ****************************************************************************/
+
+void test_pkt_lo_close(FAR void **state)
+{
+  int nfail;
+
+  if (test_pkt_iob_nfree() < 0)
+    {
+      skip();
+    }
+
+  /* The frames come back before close() */
+
+  assert_int_equal(test_pkt_close_rounds(false, &nfail), 0);
+  assert_int_equal(nfail, 0);
+
+  /* The frames come back during close() */
+
+  assert_int_equal(test_pkt_close_rounds(true, &nfail), 0);
+  assert_int_equal(nfail, 0);
+}

Reply via email to