[PATCH 1/7] net: add net_alloc_packets helper

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Mar 13 04:06:58 PDT 2024


We have a number of drivers that call net_alloc_packet in a loop and
will gain some more in the quest to drop NetRxPackets.
Let's provide a helper that can be used for this and a function to free
the packets as well.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/net.h |  8 ++++++++
 net/net.c     | 27 ++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/include/net.h b/include/net.h
index ffc1093ae6e7..38fd6f8d3c41 100644
--- a/include/net.h
+++ b/include/net.h
@@ -551,6 +551,14 @@ static inline char *net_alloc_packet(void)
 	return dma_alloc(PKTSIZE);
 }
 
+static inline void net_free_packet(char *pkt)
+{
+	return dma_free(pkt);
+}
+
+int net_alloc_packets(void **packets, int count);
+void net_free_packets(void **packets, unsigned count);
+
 struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport,
 		rx_handler_f *handler, void *ctx);
 
diff --git a/net/net.c b/net/net.c
index d72ed81e10a0..6f418df0538b 100644
--- a/net/net.c
+++ b/net/net.c
@@ -789,12 +789,33 @@ int net_receive(struct eth_device *edev, unsigned char *pkt, int len)
 	return ret;
 }
 
-static int net_init(void)
+void net_free_packets(void **packets, unsigned count)
 {
+	while (count-- > 0)
+		net_free_packet(packets[count]);
+}
+
+int net_alloc_packets(void **packets, int count)
+{
+	void *packet;
 	int i;
 
-	for (i = 0; i < PKTBUFSRX; i++)
-		NetRxPackets[i] = net_alloc_packet();
+	for (i = 0; i < count; i++) {
+		packet = net_alloc_packet();
+		if (!packet)
+			goto free;
+		packets[i] = packet;
+	}
+
+	return 0;
+free:
+	net_free_packets(packets, i);
+	return -ENOMEM;
+}
+
+static int net_init(void)
+{
+	net_alloc_packets((void **)NetRxPackets, PKTBUFSRX);
 
 	globalvar_add_simple_ip("net.nameserver", &net_nameserver);
 	globalvar_add_simple_string("net.domainname", &net_domainname);
-- 
2.39.2




More information about the barebox mailing list