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

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

commit 7f04498f157399639d5eb9e97f3b6fa08524caaf
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Dec 24 14:08:05 2025 +0800

    drivers/net/vlan: strip vlan devices from their reliance on real device lock
    
    Previously a received 802.1Q VLAN-tagged frame was handled
    recursively inside eth_input() while holding the real device's
    netdev_lock(), so all VLAN receive processing serialized on the real
    device lock.
    
    Dispatch a tagged frame into the target VLAN device's own RX queue
    (an iob queue protected by a per-VLAN spinlock) and let the VLAN
    device run its own RX poll via netdev_lower_rxready(), instead of
    processing it under the real device lock.  This keeps VLAN receive off
    the real device lock and improves real-time behavior.
    
    netdev_upper_vlan_dispatch() performs the lookup and enqueue in the
    upper-half RX path; vlan_dispatch()/vlan_receive() implement the
    per-VLAN RX queue.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 drivers/net/netdev_upperhalf.c | 118 +++++++++++++++++++++++++----------------
 drivers/net/vlan.c             |  62 +++++++++++++++++++++-
 include/nuttx/net/vlan.h       |  17 ++++++
 3 files changed, 148 insertions(+), 49 deletions(-)

diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c
index bb782997621..240833ee3d6 100644
--- a/drivers/net/netdev_upperhalf.c
+++ b/drivers/net/netdev_upperhalf.c
@@ -517,72 +517,86 @@ netdev_upper_vlan_foreach(FAR struct netdev_upperhalf_s 
*upper,
 
   netdev_unlock(&upper->lower->netdev);
 }
-#endif
 
 /****************************************************************************
- * Name: eth_input
+ * Name: netdev_upper_vlan_dispatch
  *
  * Description:
- *   Handle L2 packet input.
+ *   Dispatch the received packet to corresponding VLAN device if this is a
+ *   VLAN tagged packet.
  *
  * Input Parameters:
- *   dev - Reference to the NuttX network driver state structure
+ *   upper - Reference to the upper half driver structure
+ *   pkt   - The received packet
  *
- * Assumptions:
- *   Called with the network locked.
+ * Returned Value:
+ *   0:Success; negated errno on failure
  *
  ****************************************************************************/
 
-#if defined(CONFIG_NET_LOOPBACK) || defined(CONFIG_NET_ETHERNET) || \
-    defined(CONFIG_DRIVERS_IEEE80211)
-static void eth_input(FAR struct net_driver_s *dev)
+static int netdev_upper_vlan_dispatch(FAR struct netdev_upperhalf_s *upper,
+                                      FAR netpkt_t *pkt)
 {
-  FAR struct eth_hdr_s *eth_hdr = (FAR struct eth_hdr_s *)NETLLBUF;
-
-  /* Check if this is an 802.1Q VLAN tagged packet */
+  FAR struct netdev_lowerhalf_s *vlan;
+  FAR struct netdev_lowerhalf_s *lower = upper->lower;
+  FAR struct eth_8021qhdr_s *vlan_hdr;
+  FAR struct eth_hdr_s *eth_hdr;
+  uint16_t vid;
 
-#ifdef CONFIG_NET_VLAN
-  if (eth_hdr->type == HTONS(ETHERTYPE_VLAN))
+  if (lower->netdev.d_lltype != NET_LL_ETHERNET)
     {
-      FAR struct netdev_upperhalf_s *upper = dev->d_private;
-      FAR struct netdev_lowerhalf_s *vlan;
-      FAR struct eth_8021qhdr_s *vlan_hdr = NETLLBUF;
-      uint16_t vid = NTOHS(vlan_hdr->tci) & VLAN_VID_MASK;
-
-      vlan = netdev_upper_vlan_dev(upper, vid);
-      if (vlan)
-        {
-          /* Need to remove the 4 octet VLAN Tag, by moving src and dest
-           * addresses 4 octets to the right, and then read the actual
-           * ethertype.
-           */
+      return -ENOENT;
+    }
 
-          netdev_lock(&vlan->netdev);
-          memmove((FAR uint8_t *)eth_hdr + 4, eth_hdr,
-                  offsetof(struct eth_hdr_s, type));
-          netdev_iob_release(&vlan->netdev);
-          vlan->netdev.d_iob = iob_trimhead(dev->d_iob, 4);
-          vlan->netdev.d_len = dev->d_len - 4;
-          netdev_iob_clear(dev);
+  /* Check if this is an 802.1Q VLAN tagged packet */
 
-          /* Then call eth_input again with the new dev */
+  eth_hdr = (FAR struct eth_hdr_s *)netpkt_getdata(lower, pkt);
+  if (eth_hdr->type != HTONS(ETHERTYPE_VLAN))
+    {
+      return -ENOENT;
+    }
 
-#ifdef CONFIG_NET_PKT
-          pkt_input(&vlan->netdev);
-#endif
-          eth_input(&vlan->netdev);
-          netdev_unlock(&vlan->netdev);
-        }
-      else
+  vlan_hdr = (FAR struct eth_8021qhdr_s *)netpkt_getdata(lower, pkt);
+  vid = NTOHS(vlan_hdr->tci) & VLAN_VID_MASK;
+  vlan = netdev_upper_vlan_dev(upper, vid);
+  if (vlan)
+    {
+      if (vlan_dispatch(vlan, pkt) == 0)
         {
-          ninfo("INFO: Dropped, unknown vlan id: %d\n", vid);
-          NETDEV_RXDROPPED(dev);
-          dev->d_len = 0;
+          netdev_lower_rxready(vlan);
         }
     }
   else
+    {
+      nerr("ERROR: Dropped, unknown vlan id: %d\n", vid);
+      netpkt_free(lower, pkt, NETPKT_RX);
+      NETDEV_RXDROPPED(&lower->netdev);
+    }
+
+  return 0;
+}
 #endif
 
+/****************************************************************************
+ * Name: eth_input
+ *
+ * Description:
+ *   Handle L2 packet input.
+ *
+ * Input Parameters:
+ *   dev - Reference to the NuttX network driver state structure
+ *
+ * Assumptions:
+ *   Called with the network locked.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_LOOPBACK) || defined(CONFIG_NET_ETHERNET) || \
+    defined(CONFIG_DRIVERS_IEEE80211)
+static void eth_input(FAR struct net_driver_s *dev)
+{
+  FAR struct eth_hdr_s *eth_hdr = (FAR struct eth_hdr_s *)NETLLBUF;
+
   /* We only accept IP packets of the configured type and ARP packets */
 
 #ifdef CONFIG_NET_IPv4
@@ -819,14 +833,24 @@ static void netdev_upper_rxpoll_work(FAR struct 
netdev_upperhalf_s *upper)
 
   /* Loop while receive() successfully retrieves valid Ethernet frames. */
 
-  netdev_lock(dev);
   while ((pkt = lower->ops->receive(lower)) != NULL)
     {
+#if defined(CONFIG_NET_VLAN) && defined(CONFIG_NET_ETHERNET)
+      /* If this is a VLAN tagged frame, dispatch it to the VLAN device's
+       * own RX queue without taking the real device lock, then let the
+       * VLAN device run its own RX poll.  This keeps VLAN RX off the real
+       * device lock for better real-time behavior.
+       */
+
+      if (netdev_upper_vlan_dispatch(upper, pkt) == 0)
+        {
+          continue;
+        }
+#endif
+
       netpkt_put(dev, pkt, NETPKT_RX);
       netdev_upper_input(dev);
     }
-
-  netdev_unlock(dev);
 }
 
 /****************************************************************************
diff --git a/drivers/net/vlan.c b/drivers/net/vlan.c
index d2290224291..eeaf17d6254 100644
--- a/drivers/net/vlan.c
+++ b/drivers/net/vlan.c
@@ -33,6 +33,7 @@
 #include <stdio.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/mm/iob.h>
 #include <nuttx/net/netdev_lowerhalf.h>
 #include <nuttx/net/vlan.h>
 
@@ -52,6 +53,8 @@ struct vlan_device_s
 
   FAR struct netdev_lowerhalf_s *real;
   uint16_t tci;
+  struct iob_queue_s rxq;
+  spinlock_t rxlock;
 };
 
 /****************************************************************************
@@ -197,9 +200,15 @@ static int vlan_transmit(FAR struct netdev_lowerhalf_s 
*dev,
 
 static FAR netpkt_t *vlan_receive(FAR struct netdev_lowerhalf_s *dev)
 {
-  /* VLAN device doesn't receive packets, the real device does. */
+  FAR struct vlan_device_s *vlan = (FAR struct vlan_device_s *)dev;
+  FAR netpkt_t *pkt;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave(&vlan->rxlock);
+  pkt = iob_remove_queue(&vlan->rxq);
+  spin_unlock_irqrestore(&vlan->rxlock, flags);
 
-  return NULL;
+  return pkt;
 }
 
 /****************************************************************************
@@ -345,6 +354,7 @@ int vlan_register(FAR struct netdev_lowerhalf_s *real, 
uint16_t vid,
 
   /* Init the VLAN device */
 
+  spin_lock_init(&vlan->rxlock);
   vlan->tci           = (vid & VLAN_VID_MASK) |
                         ((prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
   vlan->real          = real;
@@ -421,8 +431,56 @@ void vlan_unregister(FAR struct netdev_lowerhalf_s *dev)
     }
 
   netdev_lower_vlan_del(vlan->real, vlan->tci & VLAN_VID_MASK);
+  iob_free_queue(&vlan->rxq);
   netdev_lower_unregister(dev);
   kmm_free(dev);
 }
 
+/****************************************************************************
+ * Name: vlan_dispatch
+ *
+ * Description:
+ *   Dispatch the received packet to the VLAN device's RX queue.
+ *
+ * Input Parameters:
+ *   dev - The VLAN device
+ *   pkt - The received packet
+ *
+ * Returned Value:
+ *   0:Success; negated errno on failure
+ *
+ ****************************************************************************/
+
+int vlan_dispatch(FAR struct netdev_lowerhalf_s *dev, FAR netpkt_t *pkt)
+{
+  FAR struct vlan_device_s *vlan = (FAR struct vlan_device_s *)dev;
+  irqstate_t flags;
+  int ret;
+
+  /* Need to remove the 4 octet VLAN Tag, by moving src and dest addresses
+   * 4 octets to the right, and then read the actual ethertype.
+   */
+
+  memmove(netpkt_getdata(dev, pkt) + 4, netpkt_getdata(dev, pkt),
+          offsetof(struct eth_hdr_s, type));
+  pkt = iob_trimhead(pkt, 4);
+
+  /* Enqueue the packet to the VLAN device's RX queue */
+
+  flags = spin_lock_irqsave(&vlan->rxlock);
+  ret = iob_tryadd_queue(pkt, &vlan->rxq);
+  spin_unlock_irqrestore(&vlan->rxlock, flags);
+
+  if (ret < 0)
+    {
+      /* Failed to enqueue, drop the packet */
+
+      nerr("ERROR: VLAN RX queue full, dropping packet\n");
+      netpkt_free(dev, pkt, NETPKT_RX);
+      NETDEV_RXDROPPED(&dev->netdev);
+    }
+
+  return ret;
+}
+
 #endif /* CONFIG_NET_VLAN */
diff --git a/include/nuttx/net/vlan.h b/include/nuttx/net/vlan.h
index ba8a93b6098..5373b5dddf6 100644
--- a/include/nuttx/net/vlan.h
+++ b/include/nuttx/net/vlan.h
@@ -131,6 +131,23 @@ int vlan_register(FAR struct netdev_lowerhalf_s *real, 
uint16_t vid,
 
 void vlan_unregister(FAR struct netdev_lowerhalf_s *dev);
 
+/****************************************************************************
+ * Name: vlan_dispatch
+ *
+ * Description:
+ *   Dispatch the received packet to the VLAN device's RX queue.
+ *
+ * Input Parameters:
+ *   dev - The VLAN device
+ *   pkt - The received packet
+ *
+ * Returned Value:
+ *   0:Success; negated errno on failure
+ *
+ ****************************************************************************/
+
+int vlan_dispatch(FAR struct netdev_lowerhalf_s *vlan, FAR netpkt_t *pkt);
+
 #ifdef __cplusplus
 }
 #endif

Reply via email to