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 7e744595a5065d69c050c607f390715b75fb4791
Author: wangjinjing1 <[email protected]>
AuthorDate: Wed Nov 18 10:32:14 2026 +0800

    net/netforward: limit the forward callbacks cached on a device
    
    When a device cannot send forwarded packets out as fast as they arrive,
    the forward callbacks pile up on its callback list and may exhaust the
    callback pool, which is shared by the whole stack.
    
    Add CONFIG_NET_FORWARD_NCALLBACKS to cap the number of IPFWD_POLL
    callbacks cached on one outgoing device.  netfwd_forward_notify() counts
    them with the new devif_count_callbacks() helper and drops the packet
    once the limit is reached.  Zero (the default) keeps the old unlimited
    behaviour.
    
    Signed-off-by: wangjinjing <[email protected]>
---
 net/devif/devif.h               | 22 ++++++++++++++++++++++
 net/devif/devif_callback.c      | 36 ++++++++++++++++++++++++++++++++++++
 net/netforward/Kconfig          | 10 ++++++++++
 net/netforward/netfwd_forward.c | 16 ++++++++++++++++
 4 files changed, 84 insertions(+)

diff --git a/net/devif/devif.h b/net/devif/devif.h
index 4151e63e378..dd6bdf1d833 100644
--- a/net/devif/devif.h
+++ b/net/devif/devif.h
@@ -379,6 +379,28 @@ void devif_conn_callback_free(FAR struct net_driver_s *dev,
 void devif_dev_callback_free(FAR struct net_driver_s *dev,
                              FAR struct devif_callback_s *cb);
 
+/****************************************************************************
+ * Name: devif_count_callbacks
+ *
+ * Description:
+ *   Count the callbacks in a connection callback list whose flags match any
+ *   of the given flags.
+ *
+ * Input Parameters:
+ *   list  - The head of the connection callback list.
+ *   flags - The callback flags to match.
+ *
+ * Returned Value:
+ *   The number of matching callbacks.
+ *
+ * Assumptions:
+ *   This function must be called with the lock of the device that owns the
+ *   list held.
+ *
+ ****************************************************************************/
+
+int devif_count_callbacks(FAR struct devif_callback_s *list, uint32_t flags);
+
 /****************************************************************************
  * Name: devif_conn_event
  *
diff --git a/net/devif/devif_callback.c b/net/devif/devif_callback.c
index d59ff7b9b89..1495cd9967e 100644
--- a/net/devif/devif_callback.c
+++ b/net/devif/devif_callback.c
@@ -223,6 +223,42 @@ static inline bool devif_event_trigger(uint32_t events, 
uint32_t triggers)
   return (events & triggers) != 0;
 }
 
+/****************************************************************************
+ * Name: devif_count_callbacks
+ *
+ * Description:
+ *   Count the callbacks in a connection callback list whose flags match any
+ *   of the given flags.
+ *
+ * Input Parameters:
+ *   list  - The head of the connection callback list.
+ *   flags - The callback flags to match.
+ *
+ * Returned Value:
+ *   The number of matching callbacks.
+ *
+ * Assumptions:
+ *   This function must be called with the lock of the device that owns the
+ *   list held.
+ *
+ ****************************************************************************/
+
+int devif_count_callbacks(FAR struct devif_callback_s *list, uint32_t flags)
+{
+  FAR struct devif_callback_s *cb;
+  int count = 0;
+
+  for (cb = list; cb != NULL; cb = cb->nxtconn)
+    {
+      if ((cb->flags & flags) != 0)
+        {
+          count++;
+        }
+    }
+
+  return count;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
diff --git a/net/netforward/Kconfig b/net/netforward/Kconfig
index 745d88a9bb7..cf952293dab 100644
--- a/net/netforward/Kconfig
+++ b/net/netforward/Kconfig
@@ -56,6 +56,16 @@ config NET_IPFORWARD_BROADCAST
                If selected, broadcast packets received on one network device 
will
                be forwarded though other network devices.
 
+config NET_FORWARD_NCALLBACKS
+       int "Temporary cached forward callbacks max number"
+       default 0
+       depends on NET_FORWARD
+       ---help---
+               Maximum number of forward callbacks that may be cached on the
+               callback list of one outgoing device while waiting to be sent, 
to
+               avoid over-consumption of the callback pool.  A packet that 
would
+               exceed the limit is dropped.  Zero means no limit.
+
 config NET_FORWARD_NSTRUCT
        int "Number of pre-allocated forwarding structures"
        default 4
diff --git a/net/netforward/netfwd_forward.c b/net/netforward/netfwd_forward.c
index fdf380161bc..0cb0a957ba7 100644
--- a/net/netforward/netfwd_forward.c
+++ b/net/netforward/netfwd_forward.c
@@ -227,6 +227,22 @@ static uint32_t netfwd_eventhandler(FAR struct 
net_driver_s *dev,
 
 static inline_function int netfwd_forward_notify(FAR struct forward_s *fwd)
 {
+#if CONFIG_NET_FORWARD_NCALLBACKS > 0
+  int n_conncb;
+
+  /* Do not let the callbacks of forwarded packets pile up on a device that
+   * cannot send them out fast enough.
+   */
+
+  n_conncb = devif_count_callbacks(fwd->f_dev->d_conncb, IPFWD_POLL);
+  if (n_conncb >= CONFIG_NET_FORWARD_NCALLBACKS)
+    {
+      nwarn("WARNING: Too many forwardings (%d >= %d)\n",
+            n_conncb, CONFIG_NET_FORWARD_NCALLBACKS);
+      return -ENOMEM;
+    }
+#endif
+
   /* Set up the callback in the connection */
 
   fwd->f_cb = netfwd_callback_alloc(fwd->f_dev);

Reply via email to