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 368ec7457d1ee3b18642fe0343db63d1a6aa7819
Author: wangjinjing1 <[email protected]>
AuthorDate: Mon Mar 30 20:49:58 2026 +0800

    net/netdev: add TX retry mechanism for transient transmit failures.
    
    Add a retry mechanism in netdev upperhalf to handle transient transmit
    failures instead of dropping packets immediately.
    
    - Distinguish transient errors from permanent errors in txpoll
    - Re-queue transient-failed packets to txq head to preserve ordering
    - Add iob_add_queue_head() for head-insertion into IOB queues
    - Release residual dev->d_iob in netdev_upper_tx after txpoll
    - Drain TX queue in ifdown to avoid stale packets
    
    Signed-off-by: wangjinjing1 <[email protected]>
---
 drivers/net/netdev_upperhalf.c | 59 ++++++++++++++++++++++++++++++++++--------
 include/nuttx/mm/iob.h         | 11 ++++++++
 mm/iob/iob_add_queue.c         | 35 +++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 11 deletions(-)

diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c
index d92e68e6e90..a5a12b4b457 100644
--- a/drivers/net/netdev_upperhalf.c
+++ b/drivers/net/netdev_upperhalf.c
@@ -85,7 +85,11 @@ struct netdev_upperhalf_s
 {
   FAR struct netdev_lowerhalf_s *lower;
 
-  /* TX queue for re-queueing replies */
+  /* TX queue for pending packets.  Packets are queued here when:
+   *   - eth_input / ip_input generates a reply (e.g. ARP, ICMP echo)
+   *   - A transient transmit error causes a packet to be re-queued
+   *     for later retry (head-inserted to preserve ordering).
+   */
 
   struct iob_queue_s txq;
 
@@ -331,13 +335,32 @@ static int netdev_upper_txpoll(FAR struct net_driver_s 
*dev)
 
   if (ret != OK)
     {
-      /* Stop polling on any error
-       * REVISIT: maybe store the pkt in upper half and retry later?
-       */
-
-      NETDEV_TXERRORS(dev);
-      nerr("ERROR: Transmit failed: %d\n", ret);
-      netpkt_put(dev, pkt, NETPKT_TX);
+      if (ret == NETDEV_TX_CONTINUE)
+        {
+          /* Permanent error -- drop the packet to avoid infinite retry.
+           * netpkt_put() restores quota and places pkt back into
+           * dev->d_iob so the caller can release it.
+           */
+
+          NETDEV_TXERRORS(dev);
+          nerr("ERROR: Packet too large, dropping\n");
+          netpkt_put(dev, pkt, NETPKT_TX);
+        }
+      else
+        {
+          /* Transient error -- re-queue to txq head for retry.
+           * Head-insert preserves packet ordering.  The retry will be
+           * driven by txdone callback from the lower half driver.
+           *
+           * Note: We manually restore quota instead of using netpkt_put()
+           * because the pkt is moved to txq, not back to dev->d_iob.
+           * After netpkt_get(), dev->d_iob is already NULL.
+           */
+
+          nwarn("WARNING: Transmit failed: %d, re-queue pkt\n", ret);
+          iob_add_queue_head(pkt, &upper->txq);
+          atomic_add(&upper->lower->quota_ptr[NETPKT_TX], 1);
+        }
 
       /* When the quota is not 0, lower half may also return a failure,
        * in which case the txdone process is required to trigger subsequent
@@ -373,13 +396,23 @@ static int netdev_upper_txpoll(FAR struct net_driver_s 
*dev)
 static int netdev_upper_tx(FAR struct net_driver_s *dev)
 {
   FAR struct netdev_upperhalf_s *upper = dev->d_private;
+  int ret;
 
   if (!IOB_QEMPTY(&upper->txq))
     {
-      /* Put the packet back to the device */
-
       netdev_iob_replace_l2(dev, iob_remove_queue(&upper->txq));
-      return netdev_upper_txpoll(dev);
+      ret = netdev_upper_txpoll(dev);
+
+      /* Release dev->d_iob if txpoll left it non-NULL.
+       * This happens when txpoll drops an oversize packet via
+       * netpkt_put() -- the txq path does not go through devif_poll
+       * which would otherwise manage the iob lifecycle.
+       * For success / transient-error, dev->d_iob is already NULL,
+       * so this is a safe no-op.
+       */
+
+      netdev_iob_release(dev);
+      return ret;
     }
 
   /* No more TX packets in queue, poll the net stack to get more packets */
@@ -1461,6 +1494,10 @@ static int netdev_upper_ifdown(FAR struct net_driver_s 
*dev)
 {
   FAR struct netdev_upperhalf_s *upper = dev->d_private;
 
+  /* Drain the TX queue before tearing down the interface. */
+
+  iob_free_queue(&upper->txq);
+
   switch (upper->lower->rxtype)
     {
       case NETDEV_RX_WORK:
diff --git a/include/nuttx/mm/iob.h b/include/nuttx/mm/iob.h
index 5e395887751..66d98c43f62 100644
--- a/include/nuttx/mm/iob.h
+++ b/include/nuttx/mm/iob.h
@@ -426,6 +426,17 @@ int iob_add_queue(FAR struct iob_s *iob, FAR struct 
iob_queue_s *iobq);
 
 int iob_tryadd_queue(FAR struct iob_s *iob, FAR struct iob_queue_s *iobq);
 
+/****************************************************************************
+ * Name: iob_add_queue_head
+ *
+ * Description:
+ *   Add one I/O buffer chain to the head of a queue.
+ *
+ ****************************************************************************/
+
+void iob_add_queue_head(FAR struct iob_s *iob,
+                        FAR struct iob_queue_s *iobq);
+
 /****************************************************************************
  * Name: iob_concat_queue
  *
diff --git a/mm/iob/iob_add_queue.c b/mm/iob/iob_add_queue.c
index 44c8cdda160..c27a5dea140 100644
--- a/mm/iob/iob_add_queue.c
+++ b/mm/iob/iob_add_queue.c
@@ -167,3 +167,38 @@ int iob_concat_queue(FAR struct iob_queue_s *dest,
 
   return 0;
 }
+
+/****************************************************************************
+ * Name: iob_add_queue_head
+ *
+ * Description:
+ *   Add one I/O buffer chain to the head of a queue.  This is the
+ *   counterpart of iob_add_queue() which appends to the tail.
+ *
+ ****************************************************************************/
+
+void iob_add_queue_head(FAR struct iob_s *iob,
+                        FAR struct iob_queue_s *iobq)
+{
+  FAR struct iob_s *last;
+
+  DEBUGASSERT(iob != NULL && iobq != NULL);
+
+  /* Find the last IOB in this chain */
+
+  last = iob;
+  while (IOB_LINK_GET(last) != NULL)
+    {
+      last = IOB_LINK_GET(last);
+    }
+
+  /* Link this chain's tail to the current head (NULL if empty) */
+
+  IOB_FLAG_SET(last, iobq->qh_head, IOB_CHAIN_END);
+  iobq->qh_head = iob;
+
+  if (iobq->qh_tail == NULL)
+    {
+      iobq->qh_tail = last;
+    }
+}

Reply via email to