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

commit 7eeb9e32f9b6e613bb3d2cd6cb6175ab43c17152
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 25 19:23:32 2026 +0100

    net/pkt: Report no EPOLLOUT edge for a poll of the device alone.
    
    An epoll set keeps its poll set up across its waits (POLLALWAYS), and
    each notification of the socket can be an event for it.
    pkt_poll_eventhandler() set POLLOUT for each PKT_POLL callback.  The
    device polls every packet connection of the device when one packet
    socket has a frame to send, and this does not change the state of the
    other sockets.  So an EPOLLOUT | EPOLLET waiter on an idle packet socket
    got an event each time another packet socket sent a frame.
    
    Without CONFIG_NET_PKT_WRITE_BUFFERS a send does not return EAGAIN, and
    the socket cannot send only when no IOB is free.  Count the send() calls
    that wait for a poll of the device in the connection, and keep the
    result of the last check in the poll information.  Report POLLOUT to an
    epoll set only while a send of this socket waits (Linux reports EPOLLOUT
    when a frame that the socket sent leaves it), or when the last check
    found no free IOB.  poll() and select() do not change.  With write
    buffers the callback does not change: a send can return EAGAIN there,
    and the waiter must get the edge when room comes back.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/pkt/pkt.h                    |  4 ++++
 net/pkt/pkt_netpoll.c            | 23 ++++++++++++++++++++---
 net/pkt/pkt_sendmsg_unbuffered.c |  6 +++++-
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/net/pkt/pkt.h b/net/pkt/pkt.h
index 274bae393ea..676e1f96a0a 100644
--- a/net/pkt/pkt.h
+++ b/net/pkt/pkt.h
@@ -62,6 +62,7 @@ struct pkt_poll_s
   FAR struct pkt_conn_s       *conn; /* Needed to handle loss of connection */
   FAR struct pollfd_s         *fds;  /* Needed to handle poll events */
   FAR struct devif_callback_s *cb;   /* Needed to teardown the poll */
+  bool                         full; /* The last check found no free IOB */
 };
 
 struct pkt_conn_s
@@ -75,6 +76,9 @@ struct pkt_conn_s
   uint8_t    ifindex;
   uint8_t    crefs;    /* Reference counts on this instance */
   uint16_t   type;     /* The Ethernet type of the packet */
+#ifndef CONFIG_NET_PKT_WRITE_BUFFERS
+  uint16_t   sending;  /* send() calls that wait for a poll of the device */
+#endif
 
 #if CONFIG_NET_RECV_BUFSIZE > 0
   int32_t    rcvbufs;  /* Maximum amount of bytes queued in recv */
diff --git a/net/pkt/pkt_netpoll.c b/net/pkt/pkt_netpoll.c
index 5f18aca9331..b2c15f99e45 100644
--- a/net/pkt/pkt_netpoll.c
+++ b/net/pkt/pkt_netpoll.c
@@ -128,11 +128,27 @@ static uint32_t pkt_poll_eventhandler(FAR struct 
net_driver_s *dev,
           eventset |= POLLHUP | POLLERR;
         }
 
-      /* A poll is a sign that we are free to send data. */
+      /* A poll is a sign that we are free to send data.  Without write
+       * buffers the device polls every packet connection when one of them
+       * sends: an epoll set (POLLALWAYS) gets POLLOUT only from its own,
+       * or when the last check found no free IOB.
+       */
 
       else if (psock_pkt_cansend(info->conn) >= 0)
         {
-          eventset |= POLLOUT;
+#ifndef CONFIG_NET_PKT_WRITE_BUFFERS
+          if ((info->fds->events & POLLALWAYS) == 0 || info->full ||
+              info->conn->sending > 0)
+#endif
+            {
+              eventset |= POLLOUT;
+            }
+
+          info->full = false;
+        }
+      else
+        {
+          info->full = true;
         }
 
 #ifdef CONFIG_NET_TIMESTAMP
@@ -271,7 +287,8 @@ int pkt_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
 
   /* Check for write data availability now */
 
-  if (psock_pkt_cansend(conn) >= 0)
+  info->full = psock_pkt_cansend(conn) < 0;
+  if (!info->full)
     {
       /* Normal data may be sent without blocking (at least one byte). */
 
diff --git a/net/pkt/pkt_sendmsg_unbuffered.c b/net/pkt/pkt_sendmsg_unbuffered.c
index 464e1d128a1..06d9e365b6a 100644
--- a/net/pkt/pkt_sendmsg_unbuffered.c
+++ b/net/pkt/pkt_sendmsg_unbuffered.c
@@ -249,8 +249,11 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
           state.snd_cb->priv  = (FAR void *)&state;
           state.snd_cb->event = psock_send_eventhandler;
 
-          /* Notify the device driver that new TX data is available. */
+          /* Notify the device driver that new TX data is available.  The
+           * poll waiters report POLLOUT while conn->sending is not zero.
+           */
 
+          conn->sending++;
           netdev_txnotify_dev(dev, PKT_POLL);
 
           /* Wait for the send to complete or an error to occur.
@@ -260,6 +263,7 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
 
           ret = conn_dev_sem_timedwait(&state.snd_sem, true, UINT_MAX,
                                        &conn->sconn, dev);
+          conn->sending--;
 
           /* Make sure that no further events are processed */
 

Reply via email to