mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/7] net: add net_alloc_packets helper
Date: Wed, 13 Mar 2024 12:06:58 +0100	[thread overview]
Message-ID: <20240313110704.1095554-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240313110704.1095554-1-a.fatoum@pengutronix.de>

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@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




  reply	other threads:[~2024-03-13 11:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 11:06 [PATCH 0/7] net: replace global NetRxPackets arrays with per-interface allocation Ahmad Fatoum
2024-03-13 11:06 ` Ahmad Fatoum [this message]
2024-03-13 11:06 ` [PATCH 2/7] net: ep93xx: replace global NetRxPackets " Ahmad Fatoum
2024-03-13 11:07 ` [PATCH 3/7] net: enc28j60: " Ahmad Fatoum
2024-03-13 11:07 ` [PATCH 4/7] net: gianfar: " Ahmad Fatoum
2024-03-13 11:07 ` [PATCH 5/7] net: ethoc: " Ahmad Fatoum
2024-03-13 11:07 ` [PATCH 6/7] net: cpsw: " Ahmad Fatoum
2024-03-13 11:07 ` [PATCH 7/7] net: retire global NetRxPackets arrays Ahmad Fatoum
2024-03-13 11:38 ` [PATCH 0/7] net: replace global NetRxPackets arrays with per-interface allocation Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313110704.1095554-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox