diff mbox series

[2/3] net: lwip: save struct netif in struct udevice

Message ID 20250306142525.3079189-3-jerome.forissier@linaro.org
State New
Headers show
Series net: lwip: map struct udevice to struct netif | expand

Commit Message

Jerome Forissier March 6, 2025, 2:25 p.m. UTC
Save the struct netif pointer in the corresponding struct udevice when
the netif is created. This avoids needlessly re-creating devices each
time the lwIP stack is entered.

Replace functions net_lwip_new_netif() and net_lwip_new_netif_noip()
with net_lwip_netif() and net_lwip_netif_noip() respectively since they
return a new structure only the first time.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
---
 include/net-lwip.h  |  4 ++--
 net/lwip/dhcp.c     |  7 ++-----
 net/lwip/dns.c      |  5 +----
 net/lwip/net-lwip.c | 19 +++++++++++--------
 net/lwip/ping.c     |  8 ++------
 net/lwip/tftp.c     |  5 +----
 net/lwip/wget.c     |  9 ++-------
 7 files changed, 21 insertions(+), 36 deletions(-)
diff mbox series

Patch

diff --git a/include/net-lwip.h b/include/net-lwip.h
index 64e5c720560..58ff2bb2d43 100644
--- a/include/net-lwip.h
+++ b/include/net-lwip.h
@@ -11,8 +11,8 @@  enum proto_t {
 };
 
 void net_lwip_set_current(void);
-struct netif *net_lwip_new_netif(struct udevice *udev);
-struct netif *net_lwip_new_netif_noip(struct udevice *udev);
+struct netif *net_lwip_netif(struct udevice *udev);
+struct netif *net_lwip_netif_noip(struct udevice *udev);
 void net_lwip_remove_netif(struct netif *netif);
 struct netif *net_lwip_get_netif(void);
 int net_lwip_rx(struct udevice *udev, struct netif *netif);
diff --git a/net/lwip/dhcp.c b/net/lwip/dhcp.c
index 3b7e4700c6e..4c621e52337 100644
--- a/net/lwip/dhcp.c
+++ b/net/lwip/dhcp.c
@@ -43,7 +43,7 @@  static int dhcp_loop(struct udevice *udev)
 		return CMD_RET_FAILURE;
 	}
 
-	netif = net_lwip_new_netif_noip(udev);
+	netif = net_lwip_netif_noip(udev);
 	if (!netif)
 		return CMD_RET_FAILURE;
 
@@ -70,10 +70,8 @@  static int dhcp_loop(struct udevice *udev)
 
 	sys_untimeout(call_lwip_dhcp_fine_tmr, NULL);
 
-	if (!bound) {
-		net_lwip_remove_netif(netif);
+	if (!bound)
 		return CMD_RET_FAILURE;
-	}
 
 	dhcp = netif_dhcp_data(netif);
 
@@ -106,7 +104,6 @@  static int dhcp_loop(struct udevice *udev)
 	printf("DHCP client bound to address %pI4 (%lu ms)\n",
 	       &dhcp->offered_ip_addr, get_timer(start));
 
-	net_lwip_remove_netif(netif);
 	return CMD_RET_SUCCESS;
 }
 
diff --git a/net/lwip/dns.c b/net/lwip/dns.c
index 149bdb784dc..65474055cff 100644
--- a/net/lwip/dns.c
+++ b/net/lwip/dns.c
@@ -54,7 +54,7 @@  static int dns_loop(struct udevice *udev, const char *name, const char *var)
 
 	dns_cb_arg.var = var;
 
-	netif = net_lwip_new_netif(udev);
+	netif = net_lwip_netif(udev);
 	if (!netif)
 		return CMD_RET_FAILURE;
 
@@ -74,7 +74,6 @@  static int dns_loop(struct udevice *udev, const char *name, const char *var)
 
 	if (!has_server) {
 		log_err("No valid name server (dnsip/dnsip2)\n");
-		net_lwip_remove_netif(netif);
 		return CMD_RET_FAILURE;
 	}
 
@@ -100,8 +99,6 @@  static int dns_loop(struct udevice *udev, const char *name, const char *var)
 		sys_untimeout(do_dns_tmr, NULL);
 	}
 
-	net_lwip_remove_netif(netif);
-
 	if (dns_cb_arg.done && dns_cb_arg.host_ipaddr.addr != 0)
 		return CMD_RET_SUCCESS;
 
diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c
index cab1dd7d483..77f16f8af9c 100644
--- a/net/lwip/net-lwip.c
+++ b/net/lwip/net-lwip.c
@@ -141,7 +141,7 @@  void net_lwip_set_current(void)
 	eth_set_current();
 }
 
-static struct netif *new_netif(struct udevice *udev, bool with_ip)
+static struct netif *get_or_create_netif(struct udevice *udev, bool with_ip)
 {
 	unsigned char enetaddr[ARP_HLEN];
 	char hwstr[MAC_ADDR_STRLEN];
@@ -152,13 +152,14 @@  static struct netif *new_netif(struct udevice *udev, bool with_ip)
 	if (!udev)
 		return NULL;
 
+	if (udev->netif)
+		return udev->netif;
+
 	if (eth_start_udev(udev) < 0) {
 		log_err("Could not start %s\n", udev->name);
 		return NULL;
 	}
 
-	netif_remove(net_lwip_get_netif());
-
 	ip4_addr_set_zero(&ip);
 	ip4_addr_set_zero(&mask);
 	ip4_addr_set_zero(&gw);
@@ -198,17 +199,19 @@  static struct netif *new_netif(struct udevice *udev, bool with_ip)
 	/* Routing: use this interface to reach the default gateway */
 	netif_set_default(netif);
 
+	udev->netif = netif;
+
 	return netif;
 }
 
-struct netif *net_lwip_new_netif(struct udevice *udev)
+struct netif *net_lwip_netif(struct udevice *udev)
 {
-	return new_netif(udev, true);
+	return get_or_create_netif(udev, true);
 }
 
-struct netif *net_lwip_new_netif_noip(struct udevice *udev)
+struct netif *net_lwip_netif_noip(struct udevice *udev)
 {
-	return new_netif(udev, false);
+	return get_or_create_netif(udev, false);
 }
 
 void net_lwip_remove_netif(struct netif *netif)
@@ -221,7 +224,7 @@  int net_init(void)
 {
 	eth_set_current();
 
-	net_lwip_new_netif(eth_get_dev());
+	net_lwip_netif(eth_get_dev());
 
 	return 0;
 }
diff --git a/net/lwip/ping.c b/net/lwip/ping.c
index 200a702bbb5..6dc8f88782b 100644
--- a/net/lwip/ping.c
+++ b/net/lwip/ping.c
@@ -119,17 +119,15 @@  static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
 	struct netif *netif;
 	int ret;
 
-	netif = net_lwip_new_netif(udev);
+	netif = net_lwip_netif(udev);
 	if (!netif)
 		return CMD_RET_FAILURE;
 
 	printf("Using %s device\n", udev->name);
 
 	ret = ping_raw_init(&ctx);
-	if (ret < 0) {
-		net_lwip_remove_netif(netif);
+	if (ret < 0)
 		return ret;
-	}
 
 	ctx.target = *addr;
 
@@ -149,8 +147,6 @@  static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
 	sys_untimeout(ping_send, &ctx);
 	ping_raw_stop(&ctx);
 
-	net_lwip_remove_netif(netif);
-
 	if (ctx.alive)
 		return 0;
 
diff --git a/net/lwip/tftp.c b/net/lwip/tftp.c
index 123d66b5dba..aae1b3bbad3 100644
--- a/net/lwip/tftp.c
+++ b/net/lwip/tftp.c
@@ -119,7 +119,7 @@  static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
 	if (!srvport)
 		srvport = TFTP_PORT;
 
-	netif = net_lwip_new_netif(udev);
+	netif = net_lwip_netif(udev);
 	if (!netif)
 		return -1;
 
@@ -146,7 +146,6 @@  static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
 	/* might return different errors, like routing problems */
 	if (err != ERR_OK) {
 		printf("tftp_get() error %d\n", err);
-		net_lwip_remove_netif(netif);
 		return -1;
 	}
 
@@ -162,8 +161,6 @@  static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
 
 	tftp_cleanup();
 
-	net_lwip_remove_netif(netif);
-
 	if (ctx.done == SUCCESS) {
 		if (env_set_hex("fileaddr", addr)) {
 			log_err("fileaddr not updated\n");
diff --git a/net/lwip/wget.c b/net/lwip/wget.c
index 14f27d42998..b191c95e251 100644
--- a/net/lwip/wget.c
+++ b/net/lwip/wget.c
@@ -307,7 +307,7 @@  static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
 	if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
 		return CMD_RET_USAGE;
 
-	netif = net_lwip_new_netif(udev);
+	netif = net_lwip_netif(udev);
 	if (!netif)
 		return -1;
 
@@ -320,7 +320,6 @@  static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
 
 		if (!tls_allocator.arg) {
 			log_err("error: Cannot create a TLS connection\n");
-			net_lwip_remove_netif(netif);
 			return -1;
 		}
 
@@ -332,10 +331,8 @@  static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
 	conn.headers_done_fn = httpc_headers_done_cb;
 	ctx.path = path;
 	if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
-			       &ctx, &state)) {
-		net_lwip_remove_netif(netif);
+			       &ctx, &state))
 		return CMD_RET_FAILURE;
-	}
 
 	while (!ctx.done) {
 		net_lwip_rx(udev, netif);
@@ -344,8 +341,6 @@  static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
 			break;
 	}
 
-	net_lwip_remove_netif(netif);
-
 	if (ctx.done == SUCCESS)
 		return 0;