Skip to content

Commit 0de9e38

Browse files
anchaoxiaoxiang781216
authored andcommitted
net/loopback: reuse devif_loopback() logic for device lo(loopback)
TX poll callback in device lo(loopback) can be replaced by devif_loopback() from devif_poll() hook, remove duplicate code to reuse this logic Signed-off-by: chao an <[email protected]>
1 parent 8232522 commit 0de9e38

File tree

2 files changed

+12
-89
lines changed

2 files changed

+12
-89
lines changed

drivers/net/loopback.c

Lines changed: 5 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
struct lo_driver_s
7171
{
7272
bool lo_bifup; /* true:ifup false:ifdown */
73-
bool lo_txdone; /* One RX packet was looped back */
7473
struct work_s lo_work; /* For deferring poll work to the work queue */
7574

7675
/* This holds the information visible to the NuttX network */
@@ -89,10 +88,6 @@ static uint8_t g_iobuffer[NET_LO_PKTSIZE + CONFIG_NET_GUARDSIZE];
8988
* Private Function Prototypes
9089
****************************************************************************/
9190

92-
/* Polling logic */
93-
94-
static int lo_txpoll(FAR struct net_driver_s *dev);
95-
9691
/* NuttX callback functions */
9792

9893
static int lo_ifup(FAR struct net_driver_s *dev);
@@ -108,80 +103,6 @@ static int lo_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
108103
* Private Functions
109104
****************************************************************************/
110105

111-
/****************************************************************************
112-
* Name: lo_txpoll
113-
*
114-
* Description:
115-
* Check if the network has any outgoing packets ready to send. This is
116-
* a callback from devif_poll(). devif_poll() will be called only during
117-
* normal TX polling.
118-
*
119-
* Input Parameters:
120-
* dev - Reference to the NuttX driver state structure
121-
*
122-
* Returned Value:
123-
* OK on success; a negated errno on failure
124-
*
125-
* Assumptions:
126-
* May or may not be called from an interrupt handler. In either case,
127-
* the network is locked.
128-
*
129-
****************************************************************************/
130-
131-
static int lo_txpoll(FAR struct net_driver_s *dev)
132-
{
133-
FAR struct lo_driver_s *priv = (FAR struct lo_driver_s *)dev->d_private;
134-
135-
/* Loop while there is data "sent", i.e., while d_len > 0. That should be
136-
* the case upon entry here and while the processing of the IPv4/6 packet
137-
* generates a new packet to be sent. Sending, of course, just means
138-
* relaying back through the network for this driver.
139-
*/
140-
141-
while (priv->lo_dev.d_len > 0)
142-
{
143-
NETDEV_TXPACKETS(&priv->lo_dev);
144-
NETDEV_RXPACKETS(&priv->lo_dev);
145-
146-
#ifdef CONFIG_NET_PKT
147-
/* When packet sockets are enabled, feed the frame into the tap */
148-
149-
pkt_input(&priv->lo_dev);
150-
#endif
151-
152-
/* We only accept IP packets of the configured type and ARP packets */
153-
154-
#ifdef CONFIG_NET_IPv4
155-
if ((IPv4BUF->vhl & IP_VERSION_MASK) == IPv4_VERSION)
156-
{
157-
ninfo("IPv4 frame\n");
158-
NETDEV_RXIPV4(&priv->lo_dev);
159-
ipv4_input(&priv->lo_dev);
160-
}
161-
else
162-
#endif
163-
#ifdef CONFIG_NET_IPv6
164-
if ((IPv6BUF->vtc & IP_VERSION_MASK) == IPv6_VERSION)
165-
{
166-
ninfo("IPv6 frame\n");
167-
NETDEV_RXIPV6(&priv->lo_dev);
168-
ipv6_input(&priv->lo_dev);
169-
}
170-
else
171-
#endif
172-
{
173-
nwarn("WARNING: Unrecognized IP version\n");
174-
NETDEV_RXDROPPED(&priv->lo_dev);
175-
priv->lo_dev.d_len = 0;
176-
}
177-
178-
priv->lo_txdone = true;
179-
NETDEV_TXDONE(&priv->lo_dev);
180-
}
181-
182-
return 0;
183-
}
184-
185106
/****************************************************************************
186107
* Name: lo_ifup
187108
*
@@ -275,14 +196,11 @@ static void lo_txavail_work(FAR void *arg)
275196
net_lock();
276197
if (priv->lo_bifup)
277198
{
278-
do
279-
{
280-
/* If so, then poll the network for new XMIT data */
281-
282-
priv->lo_txdone = false;
283-
devif_poll(&priv->lo_dev, lo_txpoll);
284-
}
285-
while (priv->lo_txdone);
199+
/* Reuse the devif_loopback() logic, Polling all pending events until
200+
* return stop
201+
*/
202+
203+
while (devif_poll(&priv->lo_dev, NULL));
286204
}
287205

288206
net_unlock();

net/devif/devif_poll.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,14 @@ int devif_poll_out(FAR struct net_driver_s *dev,
848848
return bstop;
849849
}
850850

851-
devif_out(dev);
851+
if (callback)
852+
{
853+
devif_out(dev);
854+
855+
return callback(dev);
856+
}
852857

853-
return callback(dev);
858+
return 0;
854859
}
855860

856861
#endif /* CONFIG_NET */

0 commit comments

Comments
 (0)