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 732df98a2f9cdb6b424624b816304a7e704764af Author: gaohedong <[email protected]> AuthorDate: Wed Oct 15 14:39:33 2025 +0800 net/pkt: config pkt socket read back mechanism config pkt socket read back mechanism Signed-off-by: gaohedong <[email protected]> --- drivers/net/netdev_upperhalf.c | 6 ++-- include/nuttx/net/pkt.h | 24 +++++++++++++ net/pkt/pkt.h | 9 ++++- net/pkt/pkt_conn.c | 17 ++++++++-- net/pkt/pkt_input.c | 77 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 124 insertions(+), 9 deletions(-) diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index d28e562630f..a7ef7eb1e6d 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -307,9 +307,11 @@ static int netdev_upper_txpoll(FAR struct net_driver_s *dev) NETDEV_TXPACKETS(dev); #ifdef CONFIG_NET_PKT - /* When packet sockets are enabled, feed the tx frame into it */ + /* When packet sockets are enabled, loopback the tx frame to pkt socket + * who has protocol type of ETH_P_ALL + */ - pkt_input(dev); + pkt_loopback(dev); #endif pkt = netpkt_get(dev, NETPKT_TX); diff --git a/include/nuttx/net/pkt.h b/include/nuttx/net/pkt.h index db943696d53..1fed6e11fa7 100644 --- a/include/nuttx/net/pkt.h +++ b/include/nuttx/net/pkt.h @@ -70,4 +70,28 @@ struct net_driver_s; /* Forward reference */ int pkt_input(FAR struct net_driver_s *dev); +/**************************************************************************** + * Name: pkt_loopback + * + * Description: + * Handle loopback packet input. + * This function is quite similar to pkt_inpt, and we made a distinction + * to ensure compatibility with old code + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the + * packet yet. Useful when a packet arrives before a recv call + * is in place. + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +int pkt_loopback(FAR struct net_driver_s *dev); + #endif /* __INCLUDE_NUTTX_NET_PKT_H */ diff --git a/net/pkt/pkt.h b/net/pkt/pkt.h index 46170ca8407..c09670dd01a 100644 --- a/net/pkt/pkt.h +++ b/net/pkt/pkt.h @@ -169,13 +169,20 @@ void pkt_free(FAR struct pkt_conn_s *conn); * Find a connection structure that is the appropriate connection to be * used with the provided network device * + * Input Parameters: + * dev - The device driver structure containing the received packet + * conn - A pointer to the PKT connection structure + * loopback - Indicate whether it is a loopback packet + * + * * Assumptions: * This function is called from network logic at with the network locked. * ****************************************************************************/ FAR struct pkt_conn_s *pkt_active(FAR struct net_driver_s *dev, - FAR struct pkt_conn_s *conn); + FAR struct pkt_conn_s *conn, + bool loopback); /**************************************************************************** * Name: pkt_nextconn() diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c index 6dc75bd3c0b..ab44c012668 100644 --- a/net/pkt/pkt_conn.c +++ b/net/pkt/pkt_conn.c @@ -172,13 +172,19 @@ FAR struct pkt_conn_s *pkt_nextconn(FAR struct pkt_conn_s *conn) * Find a connection structure that is the appropriate connection to be * used with the provided network device * + * Input Parameters: + * dev - The device driver structure containing the received packet + * conn - A pointer to the PKT connection structure + * loopback - Indicate whether it is a loopback packet + * * Assumptions: * This function is called from network logic at with the network locked. * ****************************************************************************/ FAR struct pkt_conn_s *pkt_active(FAR struct net_driver_s *dev, - FAR struct pkt_conn_s *conn) + FAR struct pkt_conn_s *conn, + bool loopback) { uint16_t ethertype = 0; @@ -190,8 +196,13 @@ FAR struct pkt_conn_s *pkt_active(FAR struct net_driver_s *dev, while ((conn = pkt_nextconn(conn)) != NULL) { - if (dev->d_ifindex == conn->ifindex && - (conn->type == HTONS(ETH_P_ALL) || conn->type == ethertype)) + if (dev->d_ifindex != conn->ifindex) + { + continue; + } + + if (conn->type == HTONS(ETH_P_ALL) || + (!loopback && conn->type == ethertype)) { /* Matching connection found.. return a reference to it */ diff --git a/net/pkt/pkt_input.c b/net/pkt/pkt_input.c index f7ded2d8c06..8cd0a4a1c7e 100644 --- a/net/pkt/pkt_input.c +++ b/net/pkt/pkt_input.c @@ -110,7 +110,7 @@ errout: } /**************************************************************************** - * Name: pkt_in + * Name: pkt_in_ * * Description: * Handle incoming packet input @@ -125,6 +125,7 @@ errout: * * Input Parameters: * dev - The device driver structure containing the received packet + * loopback - Indicate whether it is a loopback packet * * Returned Value: * OK The packet has been processed and can be deleted @@ -137,7 +138,7 @@ errout: * ****************************************************************************/ -static int pkt_in(FAR struct net_driver_s *dev) +static int pkt_in_(FAR struct net_driver_s *dev, bool loopback) { FAR struct pkt_conn_s *conn = NULL; uint16_t flags; @@ -150,7 +151,7 @@ static int pkt_in(FAR struct net_driver_s *dev) pkt_conn_list_lock(); - while ((conn = pkt_active(dev, conn)) != NULL) + while ((conn = pkt_active(dev, conn, loopback)) != NULL) { if (conn->pendiob == dev->d_iob) { @@ -240,6 +241,32 @@ static int pkt_in(FAR struct net_driver_s *dev) return ret; } +/**************************************************************************** + * Name: pkt_in + * + * Description: + * To be compatible with the old framework, keep this function. + * For more detailed information, please refer to __pkt_input + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the packet + * yet. Useful when a packet arrives before a recv call is in + * place. + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +static int pkt_in(FAR struct net_driver_s *dev) +{ + return pkt_in_(dev, false); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -291,4 +318,48 @@ int pkt_input(FAR struct net_driver_s *dev) return ret; } +/**************************************************************************** + * Name: pkt_loopback + * + * Description: + * Handle loopback packet input. + * This function is quite similar to pkt_inpt, and we made a distinction + * to ensure compatibility with old code + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the + * packet yet. Useful when a packet arrives before a recv call + * is in place. + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +int pkt_loopback(FAR struct net_driver_s *dev) +{ + FAR uint8_t *buf; + int ret; + + netdev_lock(dev); + + buf = dev->d_buf; + + /* Set the device buffer to l2 */ + + dev->d_buf = NETLLBUF; + + ret = pkt_in_(dev, true); + + dev->d_buf = buf; + + netdev_unlock(dev); + + return ret; +} + #endif /* CONFIG_NET && CONFIG_NET_PKT */
