This is an automated email from the ASF dual-hosted git repository.

raiden00pl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new a573e4e3321 drivers/virtio/virtio-net: restore the RX and TX flow 
control.
a573e4e3321 is described below

commit a573e4e332115cbbd80dd8fe1f69e2d56751890a
Author: dengwenqi <[email protected]>
AuthorDate: Wed Nov 25 21:38:37 2026 +0800

    drivers/virtio/virtio-net: restore the RX and TX flow control.
    
    c177a4107dd removed the netpkt quota from the network device upper half.
    virtio-net relied on it in two places that the diff of its own file did
    not show, so the driver was not adapted and lost both directions of flow
    control:
    
      - virtio_net_rxfill() loops bufnum times and runs on every receive.
        The RX quota bounded it; without the quota it fills the whole RX
        ring, which drains the shared IOB pool, and the ARP, ICMP and TCP
        allocations that follow fail.  On rv-virt the first ping from the
        node gets no reply and forwarding between two virtio-net interfaces
        stops for the same reason.
      - virtio_net_send() reported a full TX ring with -ENOMEM.  The upper
        half treats everything but -EBUSY as permanent and drops the packet
        instead of offering it again.
    
    Count the buffers the RX virtqueue holds in priv->rxnum and refill only
    up to bufnum of them, which is the bound the quota used to provide.
    virtio_net_ifdown() frees the buffers the device has used and decrements
    the count for each of them.  The buffers still in the avail ring are not
    returned by virtqueue_get_buffer(), so they stay queued and stay
    counted, and the next ifup refills only what is missing.
    
    On a full TX ring, reclaim the completed buffers and retry once before
    touching the interrupt, so that a retry which succeeds does not arm the
    TX done callback needlessly.  If the ring is still full, ask for the
    callback and return -EBUSY.
    
    The RX cap and the -EBUSY report follow dev commit 1b33dd83044 by
    dengwenqi; the ifdown accounting is not in that commit.
    
    Assisted-by: Kiro:claude-opus-4
    Signed-off-by: dengwenqi <[email protected]>
---
 drivers/virtio/virtio-net.c | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/drivers/virtio/virtio-net.c b/drivers/virtio/virtio-net.c
index 02bafaace8c..26fbdde0deb 100644
--- a/drivers/virtio/virtio-net.c
+++ b/drivers/virtio/virtio-net.c
@@ -130,6 +130,7 @@ struct virtio_net_priv_s
 
   FAR struct virtio_device *vdev;      /* Virtio device pointer */
   int                       bufnum;    /* TX and RX Buffer number */
+  int                       rxnum;     /* RX buffers in the virtqueue */
 };
 
 /* Virtio Link Layer Header, follow shows the iob buffer layout:
@@ -307,7 +308,13 @@ static void virtio_net_rxfill(FAR struct 
netdev_lowerhalf_s *dev)
   FAR netpkt_t *pkt;
   int i;
 
-  for (i = 0; i < priv->bufnum; i++)
+  /* Keep at most priv->bufnum RX buffers outstanding in the virtqueue.
+   * The netpkt quota used to enforce this limit; now that it is gone from
+   * the upper half, bound the refill here so that we do not drain the
+   * shared IOB pool by filling the whole RX ring.
+   */
+
+  for (i = 0; i < priv->bufnum - priv->rxnum; i++)
     {
       /* IOB Offload, Alloc buffer from RX netpkt */
 
@@ -336,6 +343,8 @@ static void virtio_net_rxfill(FAR struct netdev_lowerhalf_s 
*dev)
           netpkt_free(dev, pkt, NETPKT_RX);
           break;
         }
+
+      priv->rxnum++;
     }
 
   if (i > 0)
@@ -491,7 +500,9 @@ static int virtio_net_ifdown(FAR struct netdev_lowerhalf_s 
*dev)
     }
 
   /* Drain RX virtqueue to free netpkt buffers that are still queued
-   * but not yet consumed.
+   * but not yet consumed.  Only the buffers the device has used come
+   * back here; the ones still in the avail ring stay queued, so the
+   * count is decremented per buffer instead of being reset.
    */
 
   while (1)
@@ -504,6 +515,7 @@ static int virtio_net_ifdown(FAR struct netdev_lowerhalf_s 
*dev)
         }
 
       netpkt_free(dev, hdr->pkt, NETPKT_RX);
+      priv->rxnum--;
     }
 
   /* Drain TX virtqueue as well */
@@ -549,14 +561,24 @@ static int virtio_net_send(FAR struct netdev_lowerhalf_s 
*dev,
     {
       /* The virtqueue has no room.  The packet was not queued, so it is
        * still owned (and will be recycled) by the upper-half.  Reclaim what
-       * the other side has finished and ask for a TX done callback to
-       * resume the transmission.
+       * the other side has finished and retry before touching the
+       * interrupt, so that a retry which succeeds does not needlessly arm
+       * the TX done callback.
        */
 
-      vrterr("net send failed: %d\n", ret);
       virtio_net_txfree(dev);
-      virtqueue_enable_cb_lock(vq, &priv->lock[VIRTIO_NET_TX]);
-      return -ENOMEM;
+      ret = virtio_net_addbuffer(dev, vq, pkt, VIRTIO_NET_TX);
+      if (ret < 0)
+        {
+          /* Still full.  Ask for a TX done callback and report a transient
+           * failure, so that the upper half queues the packet and offers
+           * it again instead of dropping it.
+           */
+
+          vrtinfo("net send busy: %d\n", ret);
+          virtqueue_enable_cb_lock(vq, &priv->lock[VIRTIO_NET_TX]);
+          return -EBUSY;
+        }
     }
 
   virtqueue_kick_lock(vq, &priv->lock[VIRTIO_NET_TX]);
@@ -609,6 +631,10 @@ static netpkt_t *virtio_net_recv(FAR struct 
netdev_lowerhalf_s *dev)
       return NULL;
     }
 
+  /* One RX buffer left the virtqueue; allow rxfill to replenish it. */
+
+  priv->rxnum--;
+
   /* Set the received pkt length */
 
   netpkt_setdatalen(dev, hdr->pkt, len - VIRTIO_NET_HDRSIZE);

Reply via email to