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

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

commit 750fae4cca677775f8de8bd6200eb85217dfb1a5
Author: Zhe Weng <[email protected]>
AuthorDate: Tue Jan 7 11:35:37 2025 +0800

    netdev/lower: Support share quota between devices
    
    To prepare for VLAN device support, a VLAN device will share quota with
    its real device.
    
    Signed-off-by: Zhe Weng <[email protected]>
---
 drivers/net/netdev_upperhalf.c       | 24 +++++++++++++++++-------
 include/nuttx/net/netdev_lowerhalf.h |  1 +
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c
index c0ae479441e..e102c0b9c6f 100644
--- a/drivers/net/netdev_upperhalf.c
+++ b/drivers/net/netdev_upperhalf.c
@@ -155,7 +155,7 @@ static FAR netpkt_t *netpkt_get(FAR struct net_driver_s 
*dev,
    * cases will be limited by netdev_upper_can_tx and seldom reaches here.
    */
 
-  if (atomic_fetch_sub(&upper->lower->quota[type], 1) <= 0)
+  if (atomic_fetch_sub(&upper->lower->quota_ptr[type], 1) <= 0)
     {
       nwarn("WARNING: Allowing temporarily exceeding quota of %s.\n",
             dev->d_ifname);
@@ -182,7 +182,7 @@ static void netpkt_put(FAR struct net_driver_s *dev, FAR 
netpkt_t *pkt,
 
   DEBUGASSERT(dev && pkt);
 
-  atomic_fetch_add(&upper->lower->quota[type], 1);
+  atomic_fetch_add(&upper->lower->quota_ptr[type], 1);
   netdev_iob_replace_l2(dev, pkt);
 }
 
@@ -1188,12 +1188,22 @@ int netdev_lower_register(FAR struct netdev_lowerhalf_s 
*dev,
   int i;
 #endif
 
-  if (dev == NULL || quota_is_valid(dev) == false || dev->ops == NULL ||
+  if (dev == NULL || dev->ops == NULL ||
       dev->ops->transmit == NULL || dev->ops->receive == NULL)
     {
       return -EINVAL;
     }
 
+  if (dev->quota_ptr == NULL)
+    {
+      dev->quota_ptr = dev->quota;
+    }
+
+  if (quota_is_valid(dev) == false)
+    {
+      return -EINVAL;
+    }
+
   if ((upper = netdev_upper_alloc(dev)) == NULL)
     {
       return -ENOMEM;
@@ -1384,16 +1394,16 @@ FAR netpkt_t *netpkt_alloc(FAR struct 
netdev_lowerhalf_s *dev,
 {
   FAR netpkt_t *pkt;
 
-  if (atomic_fetch_sub(&dev->quota[type], 1) <= 0)
+  if (atomic_fetch_sub(&dev->quota_ptr[type], 1) <= 0)
     {
-      atomic_fetch_add(&dev->quota[type], 1);
+      atomic_fetch_add(&dev->quota_ptr[type], 1);
       return NULL;
     }
 
   pkt = iob_tryalloc(false);
   if (pkt == NULL)
     {
-      atomic_fetch_add(&dev->quota[type], 1);
+      atomic_fetch_add(&dev->quota_ptr[type], 1);
       return NULL;
     }
 
@@ -1417,7 +1427,7 @@ FAR netpkt_t *netpkt_alloc(FAR struct netdev_lowerhalf_s 
*dev,
 void netpkt_free(FAR struct netdev_lowerhalf_s *dev, FAR netpkt_t *pkt,
                  enum netpkt_type_e type)
 {
-  atomic_fetch_add(&dev->quota[type], 1);
+  atomic_fetch_add(&dev->quota_ptr[type], 1);
   iob_free_chain(pkt);
 }
 
diff --git a/include/nuttx/net/netdev_lowerhalf.h 
b/include/nuttx/net/netdev_lowerhalf.h
index b9421afaa38..3f7785e23c3 100644
--- a/include/nuttx/net/netdev_lowerhalf.h
+++ b/include/nuttx/net/netdev_lowerhalf.h
@@ -117,6 +117,7 @@ struct netdev_lowerhalf_s
 
   /* Max # of buffer held by driver */
 
+  FAR atomic_t *quota_ptr; /* Shared quota, ignore `quota` if ptr is set */
   atomic_t quota[NETPKT_TYPENUM];
 
   /* The structure used by net stack.

Reply via email to