Skip to contentSkip to conversation

net: lwip: halt ethernet after network commands

PATCH Superseded David Lechner 3 participants 9 messages May 12 – May 13 Networking

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: lwip: halt ethernet after network commands

Background:

While testing [1], I found that it would take down my local network if I left the command prompt open for more than 30 seconds after running any network command. The network activity light on the board running U-Boot would go crazy and other network adapters on the network would start getting watchdog timeouts. So apparently, it is doing something quite low-level and I don't have any hardware to sniff that (Wireshark on another computer was no help since all network adapters on the network stopped working.) So I took a logical approach of looking at the code to see if I could find anything that would cause this. This seemed to only happen after I switched from the legacy network implementation to the lwIP implementation, so I looked for differences between the two implementations.

After some investigation, I found that the lwIP implementation was different from the legacy implementation in that it does not call eth_halt() after a network command. Since the problem happened only when at the command prompt with no command running, this seemed like a likely culprit. And indeed, it does seem to fix the issue for me.

For reference, the legacy implementation does something like this as documented in doc/develop/driver-model/ethernet.rst:

	(some net operation (ping / tftp / whatever...))
	eth_init()
		ops->start()
	eth_send()
		ops->send()
	eth_rx()
		ops->recv()
		(process packet)
		if (ops->free_pkt)
			ops->free_pkt()
	eth_halt()
		ops->stop()

After this patch, the lwIP implementation will do the same thing for all ethernet commands.

[1]: https://lore.kernel.org/u-boot/20260429-add-ethernet-support-for-genio-520-720-v4-0-be54e17239b7@baylibre.com/

During review of v1, we noticed some existing bugs in wget_handle_request() so two new patches were added to fix those first before attempting the primary objective.

---
Changes in v2:
- Added two new patches to address existing bugs in the code.
- Don't remove eth_halt() before eth_init().
- Link to v1: https://patch.msgid.link/20260509-net-lwip-add-stop-function-v1-1-62d854bfa66a@baylibre.com

To: Jerome Forissier <[email protected]>
To: Tom Rini <[email protected]>
To: Ilias Apalodimas <[email protected]>
Cc: Julien Stephan <[email protected]>
Cc: [email protected]

---
David Lechner (3):
net: lwip: wget: fix error handling in wget_do_request()
net: lwip: wget: return errno codes from wget_do_request()
net: lwip: introduce net_lwip_eth_stop() function

 cmd/lwip/ping.c     |  10 ++++-
 cmd/lwip/sntp.c     |  10 ++++-
 include/net-lwip.h  |   1 +
 net/lwip/dhcp.c     |  15 ++++++--
 net/lwip/dns.c      |   7 +++-
 net/lwip/net-lwip.c |   5 +++
 net/lwip/nfs.c      |   4 ++
 net/lwip/tftp.c     |   4 ++
 net/lwip/wget.c     | 107 ++++++++++++++++++++++++++++++----------------------
 9 files changed, 108 insertions(+), 55 deletions(-)

---
base-commit: 36d4c653580824b16574560b21d4401614d8b68e
change-id: 20260509-net-lwip-add-stop-function-7f7794074844

Best regards,

Signature · 1 line
David Lechner <[email protected]>
#1 lore raw

PATCH v2 1/3 net: lwip: wget: fix error handling in wget_do_request()

Split wget_do_request() into two functions to make error handling less error-prone.

After a successful call to net_lwip_new_netif(), net_lwip_remove_netif() must always be called to leaks. This was missed in the HTTPS section of the code where we returned on error without cleaning up.

Instead of adding more calls to net_lwip_remove_netif(), refactor the code into two functions. The outer function handles managing the netif lifecycle. The inner function no longer has to worry about cleaning up before returning on error.

To keep things simple, the path local variable is removed during the refactoring. Instead, ctx.path is used directly everywhere.

Fixes 3c656c928bd7 ("net: lwip: add wget command")
---
 net/lwip/wget.c | 88 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 47 insertions(+), 41 deletions(-)
net/lwip/wget.c +47 -41
index 502c0faebb2..f89a6580b41 100644
@@ -292,45 +292,17 @@ static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct
 #if CONFIG_IS_ENABLED(WGET_CACERT)
 #endif
 
-int wget_do_request(ulong dst_addr, char *uri)
+static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
+			       struct udevice *udev, struct netif *netif)
 {
 #if CONFIG_IS_ENABLED(WGET_HTTPS)
 	altcp_allocator_t tls_allocator;
 #endif
 	httpc_connection_t conn;
 	httpc_state_t *state;
-	struct udevice *udev;
-	struct netif *netif;
-	struct wget_ctx ctx;
-	char *path;
-	bool is_https;
-
-	ctx.daddr = dst_addr;
-	ctx.saved_daddr = dst_addr;
-	ctx.done = NOT_DONE;
-	ctx.size = 0;
-	ctx.prevsize = 0;
-	ctx.start_time = 0;
-	ctx.content_len = 0;
-	ctx.hash_count = 0;
-
-	if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
-		return CMD_RET_USAGE;
-
-	if (net_lwip_eth_start() < 0)
-		return CMD_RET_FAILURE;
-
-	if (!wget_info)
-		wget_info = &default_wget_info;
-
-	udev = eth_get_dev();
-
-	netif = net_lwip_new_netif(udev);
-	if (!netif)
-		return -1;
 
 	/* if URL with hostname init dns */
-	if (!ipaddr_aton(ctx.server_name, NULL) && net_lwip_dns_init())
+	if (!ipaddr_aton(ctx->server_name, NULL) && net_lwip_dns_init())
 		return CMD_RET_FAILURE;
 
 	memset(&conn, 0, sizeof(conn));
@@ -374,11 +346,10 @@ int wget_do_request(ulong dst_addr, char *uri)
 		tls_allocator.alloc = &altcp_tls_alloc;
 		tls_allocator.arg =
 			altcp_tls_create_config_client(ca, ca_sz,
-						       ctx.server_name);
+						       ctx->server_name);
 
 		if (!tls_allocator.arg) {
 			log_err("error: Cannot create a TLS connection\n");
-			net_lwip_remove_netif(netif);
 			return -1;
 		}
 
@@ -388,24 +359,20 @@ int wget_do_request(ulong dst_addr, char *uri)
 
 	conn.result_fn = httpc_result_cb;
 	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);
+	if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn,
+			       httpc_recv_cb, ctx, &state)) {
 		return CMD_RET_FAILURE;
 	}
 
 	errno = 0;
 
-	while (!ctx.done) {
+	while (!ctx->done) {
 		net_lwip_rx(udev, netif);
 		if (ctrlc())
 			break;
 	}
 
-	net_lwip_remove_netif(netif);
-
-	if (ctx.done == SUCCESS)
+	if (ctx->done == SUCCESS)
 		return 0;
 
 	if (errno == EPERM && !wget_info->silent)
@@ -414,6 +381,45 @@ int wget_do_request(ulong dst_addr, char *uri)
 	return -1;
 }
 
+int wget_do_request(ulong dst_addr, char *uri)
+{
+	struct udevice *udev;
+	struct wget_ctx ctx;
+	struct netif *netif;
+	bool is_https;
+	int ret;
+
+	ctx.daddr = dst_addr;
+	ctx.saved_daddr = dst_addr;
+	ctx.done = NOT_DONE;
+	ctx.size = 0;
+	ctx.prevsize = 0;
+	ctx.start_time = 0;
+	ctx.content_len = 0;
+	ctx.hash_count = 0;
+
+	if (parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https))
+		return CMD_RET_USAGE;
+
+	if (net_lwip_eth_start() < 0)
+		return CMD_RET_FAILURE;
+
+	if (!wget_info)
+		wget_info = &default_wget_info;
+
+	udev = eth_get_dev();
+
+	netif = net_lwip_new_netif(udev);
+	if (!netif)
+		return -1;
+
+	ret = wget_handle_request(&ctx, is_https, udev, netif);
+
+	net_lwip_remove_netif(netif);
+
+	return ret;
+}
+
 /**
  * wget_validate_uri() - validate the uri for wget
  *
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] net: lwip: wget: fix error handling in wget_do_request()

Quoted 153 lines · David Lechner

Split wget_do_request() into two functions to make error handling less error-prone.

After a successful call to net_lwip_new_netif(), net_lwip_remove_netif() must always be called to leaks. This was missed in the HTTPS section of the code where we returned on error without cleaning up.

Instead of adding more calls to net_lwip_remove_netif(), refactor the code into two functions. The outer function handles managing the netif lifecycle. The inner function no longer has to worry about cleaning up before returning on error.

To keep things simple, the path local variable is removed during the refactoring. Instead, ctx.path is used directly everywhere.

Fixes 3c656c928bd7 ("net: lwip: add wget command")
---
 net/lwip/wget.c | 88 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 47 insertions(+), 41 deletions(-)
net/lwip/wget.c +40 -41
index 502c0faebb2..f89a6580b41 100644
@@ -292,45 +292,17 @@ static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct
 #if CONFIG_IS_ENABLED(WGET_CACERT)
 #endif
 
-int wget_do_request(ulong dst_addr, char *uri)
+static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
+                            struct udevice *udev, struct netif *netif)
 {
 #if CONFIG_IS_ENABLED(WGET_HTTPS)
      altcp_allocator_t tls_allocator;
 #endif
      httpc_connection_t conn;
      httpc_state_t *state;
-     struct udevice *udev;
-     struct netif *netif;
-     struct wget_ctx ctx;
-     char *path;
-     bool is_https;
-
-     ctx.daddr = dst_addr;
-     ctx.saved_daddr = dst_addr;
-     ctx.done = NOT_DONE;
-     ctx.size = 0;
-     ctx.prevsize = 0;
-     ctx.start_time = 0;
-     ctx.content_len = 0;
-     ctx.hash_count = 0;
-
-     if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
-             return CMD_RET_USAGE;
-
-     if (net_lwip_eth_start() < 0)
-             return CMD_RET_FAILURE;
-
-     if (!wget_info)
-             wget_info = &default_wget_info;
-
-     udev = eth_get_dev();
-
-     netif = net_lwip_new_netif(udev);
-     if (!netif)
-             return -1;
 
      /* if URL with hostname init dns */
-     if (!ipaddr_aton(ctx.server_name, NULL) && net_lwip_dns_init())
+     if (!ipaddr_aton(ctx->server_name, NULL) && net_lwip_dns_init())
              return CMD_RET_FAILURE;
 
      memset(&conn, 0, sizeof(conn));
@@ -374,11 +346,10 @@ int wget_do_request(ulong dst_addr, char *uri)
              tls_allocator.alloc = &altcp_tls_alloc;
              tls_allocator.arg =
                      altcp_tls_create_config_client(ca, ca_sz,
-                                                    ctx.server_name);
+                                                    ctx->server_name);
 
              if (!tls_allocator.arg) {
                      log_err("error: Cannot create a TLS connection\n");
-                     net_lwip_remove_netif(netif);
                      return -1;
              }
 
@@ -388,24 +359,20 @@ int wget_do_request(ulong dst_addr, char *uri)
 
      conn.result_fn = httpc_result_cb;
      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);
+     if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn,
+                            httpc_recv_cb, ctx, &state)) {
              return CMD_RET_FAILURE;
      }
 
      errno = 0;
 
-     while (!ctx.done) {
+     while (!ctx->done) {
              net_lwip_rx(udev, netif);
              if (ctrlc())
                      break;
      }
 
-     net_lwip_remove_netif(netif);
-
-     if (ctx.done == SUCCESS)
+     if (ctx->done == SUCCESS)
              return 0;
 
      if (errno == EPERM && !wget_info->silent)
@@ -414,6 +381,45 @@ int wget_do_request(ulong dst_addr, char *uri)
      return -1;
 }
 
+int wget_do_request(ulong dst_addr, char *uri)
+{
+     struct udevice *udev;
+     struct wget_ctx ctx;
+     struct netif *netif;
+     bool is_https;
+     int ret;
+
+     ctx.daddr = dst_addr;
+     ctx.saved_daddr = dst_addr;
+     ctx.done = NOT_DONE;
+     ctx.size = 0;
+     ctx.prevsize = 0;
+     ctx.start_time = 0;
+     ctx.content_len = 0;
+     ctx.hash_count = 0;
+
+     if (parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https))
+             return CMD_RET_USAGE;
+
+     if (net_lwip_eth_start() < 0)
+             return CMD_RET_FAILURE;
+
+     if (!wget_info)
+             wget_info = &default_wget_info;
+
+     udev = eth_get_dev();
+
+     netif = net_lwip_new_netif(udev);
+     if (!netif)
+             return -1;
+

net/lwip/wget.c int wget_do_request(ulong dst_addr, char *uri) · line 425

Quoted 10 lines
net/lwip/wget.c +7 -0
+     ret = wget_handle_request(&ctx, is_https, udev, netif);
+
+     net_lwip_remove_netif(netif);
+
+     return ret;
+}
+
 /**
  * wget_validate_uri() - validate the uri for wget
  *

Thanks,
--
Jerome
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Acked-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] net: lwip: wget: fix error handling in wget_do_request()

In reply to David Lechner

Hi David,

Quoted 6 lines · David Lechner

Split wget_do_request() into two functions to make error handling less error-prone.

After a successful call to net_lwip_new_netif(), net_lwip_remove_netif() must always be called to leaks.

prevent leaks*

Quoted 2 lines

This was missed in the HTTPS section of
the code where we returned on error without cleaning up.

I'd prefer making this a bit more explicit. net_lwip_remove_netif() was called in case of a failure afaict. The only place that wasn's called is when checking the cacert_auth_mode, right?

Anyway, I think Jerome can fix them up during the PR

Thanks
/Ilias

Quoted 160 lines

Instead of adding more calls to net_lwip_remove_netif(), refactor the code into two functions. The outer function handles managing the netif lifecycle. The inner function no longer has to worry about cleaning up before returning on error.

To keep things simple, the path local variable is removed during the refactoring. Instead, ctx.path is used directly everywhere.

Fixes 3c656c928bd7 ("net: lwip: add wget command")
---
 net/lwip/wget.c | 88 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 47 insertions(+), 41 deletions(-)
net/lwip/wget.c +47 -42
index 502c0faebb2..f89a6580b41 100644
@@ -292,45 +292,17 @@ static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct
 #if CONFIG_IS_ENABLED(WGET_CACERT)
 #endif
 
-int wget_do_request(ulong dst_addr, char *uri)
+static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
+                              struct udevice *udev, struct netif *netif)
 {
 #if CONFIG_IS_ENABLED(WGET_HTTPS)
        altcp_allocator_t tls_allocator;
 #endif
        httpc_connection_t conn;
        httpc_state_t *state;
-       struct udevice *udev;
-       struct netif *netif;
-       struct wget_ctx ctx;
-       char *path;
-       bool is_https;
-
-       ctx.daddr = dst_addr;
-       ctx.saved_daddr = dst_addr;
-       ctx.done = NOT_DONE;
-       ctx.size = 0;
-       ctx.prevsize = 0;
-       ctx.start_time = 0;
-       ctx.content_len = 0;
-       ctx.hash_count = 0;
-
-       if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
-               return CMD_RET_USAGE;
-
-       if (net_lwip_eth_start() < 0)
-               return CMD_RET_FAILURE;
-
-       if (!wget_info)
-               wget_info = &default_wget_info;
-
-       udev = eth_get_dev();
-
-       netif = net_lwip_new_netif(udev);
-       if (!netif)
-               return -1;
 
        /* if URL with hostname init dns */
-       if (!ipaddr_aton(ctx.server_name, NULL) && net_lwip_dns_init())
+       if (!ipaddr_aton(ctx->server_name, NULL) && net_lwip_dns_init())
                return CMD_RET_FAILURE;
 
        memset(&conn, 0, sizeof(conn));
@@ -374,11 +346,10 @@ int wget_do_request(ulong dst_addr, char *uri)
                tls_allocator.alloc = &altcp_tls_alloc;
                tls_allocator.arg =
                        altcp_tls_create_config_client(ca, ca_sz,
-                                                      ctx.server_name);
+                                                      ctx->server_name);
 
                if (!tls_allocator.arg) {
                        log_err("error: Cannot create a TLS connection\n");
-                       net_lwip_remove_netif(netif);
                        return -1;
                }
 
@@ -388,24 +359,20 @@ int wget_do_request(ulong dst_addr, char *uri)
 
        conn.result_fn = httpc_result_cb;
        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);
+       if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn,
+                              httpc_recv_cb, ctx, &state)) {
                return CMD_RET_FAILURE;
        }
 
        errno = 0;
 
-       while (!ctx.done) {
+       while (!ctx->done) {
                net_lwip_rx(udev, netif);
                if (ctrlc())
                        break;
        }
 
-       net_lwip_remove_netif(netif);
-
-       if (ctx.done == SUCCESS)
+       if (ctx->done == SUCCESS)
                return 0;
 
        if (errno == EPERM && !wget_info->silent)
@@ -414,6 +381,45 @@ int wget_do_request(ulong dst_addr, char *uri)
        return -1;
 }
 
+int wget_do_request(ulong dst_addr, char *uri)
+{
+       struct udevice *udev;
+       struct wget_ctx ctx;
+       struct netif *netif;
+       bool is_https;
+       int ret;
+
+       ctx.daddr = dst_addr;
+       ctx.saved_daddr = dst_addr;
+       ctx.done = NOT_DONE;
+       ctx.size = 0;
+       ctx.prevsize = 0;
+       ctx.start_time = 0;
+       ctx.content_len = 0;
+       ctx.hash_count = 0;
+
+       if (parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https))
+               return CMD_RET_USAGE;
+
+       if (net_lwip_eth_start() < 0)
+               return CMD_RET_FAILURE;
+
+       if (!wget_info)
+               wget_info = &default_wget_info;
+
+       udev = eth_get_dev();
+
+       netif = net_lwip_new_netif(udev);
+       if (!netif)
+               return -1;
+
+       ret = wget_handle_request(&ctx, is_https, udev, netif);
+
+       net_lwip_remove_netif(netif);
+
+       return ret;
+}
+
 /**
  * wget_validate_uri() - validate the uri for wget
  *
 
--

2.43.0

#4 lore raw

PATCH v2 2/3 net: lwip: wget: return errno codes from wget_do_request()

In reply to David Lechner

Change the return values of the lwip implementation of wget_do_request() to be errno codes instead of command return codes.

wget_do_request() is not a command, so it does not make sense to return command return codes from it. Also, the legacy network implementation of wget_do_request() already returns errno codes so it is logical for the lwip implementation to do the same.

This fixes a bug in try_load_from_uri_path() in efi_manager.c where it checks that the return value of wget_do_request() is < 0. Before this change, CMD_RET_FAILURE would not be considered an error since it has a value of 1.

The value of ENODEV is used in places where there could actually be a number of different causes of failure and it isn't possible to discriminate (i.e. failing function returns NULL for all errors). Since all callers of wget_do_request() don't propagate the error code, it doesn't matter so much that this is not ideal, at least at this point in time.

Fixes 3c656c928bd7 ("net: lwip: add wget command")
---
 net/lwip/wget.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
net/lwip/wget.c +17 -11
index f89a6580b41..3a0b0dca145 100644
@@ -300,10 +300,14 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 #endif
 	httpc_connection_t conn;
 	httpc_state_t *state;
+	int ret;
 
 	/* if URL with hostname init dns */
-	if (!ipaddr_aton(ctx->server_name, NULL) && net_lwip_dns_init())
-		return CMD_RET_FAILURE;
+	if (!ipaddr_aton(ctx->server_name, NULL)) {
+		ret = net_lwip_dns_init();
+		if (ret)
+			return ret;
+	}
 
 	memset(&conn, 0, sizeof(conn));
 #if CONFIG_IS_ENABLED(WGET_HTTPS)
@@ -325,7 +329,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 					printf("Error: cacert authentication "
 					       "mode is 'required' but no CA "
 					       "certificates given\n");
-				return CMD_RET_FAILURE;
+				return -EINVAL;
 		       }
 		} else if (cacert_auth_mode == AUTH_NONE) {
 			ca = NULL;
@@ -350,7 +354,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 
 		if (!tls_allocator.arg) {
 			log_err("error: Cannot create a TLS connection\n");
-			return -1;
+			return -ENODEV;
 		}
 
 		conn.altcp_allocator = &tls_allocator;
@@ -361,7 +365,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 	conn.headers_done_fn = httpc_headers_done_cb;
 	if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn,
 			       httpc_recv_cb, ctx, &state)) {
-		return CMD_RET_FAILURE;
+		return -ENODEV;
 	}
 
 	errno = 0;
@@ -378,7 +382,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 	if (errno == EPERM && !wget_info->silent)
 		printf("Certificate verification failed\n");
 
-	return -1;
+	return -errno ?: -EIO;
 }
 
 int wget_do_request(ulong dst_addr, char *uri)
@@ -398,11 +402,13 @@ int wget_do_request(ulong dst_addr, char *uri)
 	ctx.content_len = 0;
 	ctx.hash_count = 0;
 
-	if (parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https))
-		return CMD_RET_USAGE;
+	ret = parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https);
+	if (ret)
+		return ret;
 
-	if (net_lwip_eth_start() < 0)
-		return CMD_RET_FAILURE;
+	ret = net_lwip_eth_start();
+	if (ret)
+		return ret;
 
 	if (!wget_info)
 		wget_info = &default_wget_info;
@@ -411,7 +417,7 @@ int wget_do_request(ulong dst_addr, char *uri)
 
 	netif = net_lwip_new_netif(udev);
 	if (!netif)
-		return -1;
+		return -ENODEV;
 
 	ret = wget_handle_request(&ctx, is_https, udev, netif);
 
Signature · 1 line
2.43.0
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] net: lwip: wget: return errno codes from wget_do_request()

Quoted 103 lines · David Lechner

Change the return values of the lwip implementation of wget_do_request() to be errno codes instead of command return codes.

wget_do_request() is not a command, so it does not make sense to return command return codes from it. Also, the legacy network implementation of wget_do_request() already returns errno codes so it is logical for the lwip implementation to do the same.

This fixes a bug in try_load_from_uri_path() in efi_manager.c where it checks that the return value of wget_do_request() is < 0. Before this change, CMD_RET_FAILURE would not be considered an error since it has a value of 1.

The value of ENODEV is used in places where there could actually be a number of different causes of failure and it isn't possible to discriminate (i.e. failing function returns NULL for all errors). Since all callers of wget_do_request() don't propagate the error code, it doesn't matter so much that this is not ideal, at least at this point in time.

Fixes 3c656c928bd7 ("net: lwip: add wget command")
---
 net/lwip/wget.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
net/lwip/wget.c +16 -10
index f89a6580b41..3a0b0dca145 100644
@@ -300,10 +300,14 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 #endif
 	httpc_connection_t conn;
 	httpc_state_t *state;
+	int ret;
 
 	/* if URL with hostname init dns */
-	if (!ipaddr_aton(ctx->server_name, NULL) && net_lwip_dns_init())
-		return CMD_RET_FAILURE;
+	if (!ipaddr_aton(ctx->server_name, NULL)) {
+		ret = net_lwip_dns_init();
+		if (ret)
+			return ret;
+	}
 
 	memset(&conn, 0, sizeof(conn));
 #if CONFIG_IS_ENABLED(WGET_HTTPS)
@@ -325,7 +329,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 					printf("Error: cacert authentication "
 					       "mode is 'required' but no CA "
 					       "certificates given\n");
-				return CMD_RET_FAILURE;
+				return -EINVAL;
 		       }
 		} else if (cacert_auth_mode == AUTH_NONE) {
 			ca = NULL;
@@ -350,7 +354,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 
 		if (!tls_allocator.arg) {
 			log_err("error: Cannot create a TLS connection\n");
-			return -1;
+			return -ENODEV;
 		}
 
 		conn.altcp_allocator = &tls_allocator;
@@ -361,7 +365,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 	conn.headers_done_fn = httpc_headers_done_cb;
 	if (httpc_get_file_dns(ctx->server_name, ctx->port, ctx->path, &conn,
 			       httpc_recv_cb, ctx, &state)) {
-		return CMD_RET_FAILURE;
+		return -ENODEV;
 	}
 
 	errno = 0;
@@ -378,7 +382,7 @@ static int wget_handle_request(struct wget_ctx *ctx, bool is_https,
 	if (errno == EPERM && !wget_info->silent)
 		printf("Certificate verification failed\n");
 
-	return -1;
+	return -errno ?: -EIO;
 }
 
 int wget_do_request(ulong dst_addr, char *uri)
@@ -398,11 +402,13 @@ int wget_do_request(ulong dst_addr, char *uri)
 	ctx.content_len = 0;
 	ctx.hash_count = 0;
 
-	if (parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https))
-		return CMD_RET_USAGE;
+	ret = parse_url(uri, ctx.server_name, &ctx.port, &ctx.path, &is_https);
+	if (ret)
+		return ret;
 
-	if (net_lwip_eth_start() < 0)
-		return CMD_RET_FAILURE;
+	ret = net_lwip_eth_start();
+	if (ret)
+		return ret;
 
 	if (!wget_info)
 		wget_info = &default_wget_info;

net/lwip/wget.c int wget_do_request(ulong dst_addr, char *uri) · line 423

Quoted 8 lines
net/lwip/wget.c +1 -1
@@ -411,7 +417,7 @@ int wget_do_request(ulong dst_addr, char *uri)
 
 	netif = net_lwip_new_netif(udev);
 	if (!netif)
-		return -1;
+		return -ENODEV;
 
 	ret = wget_handle_request(&ctx, is_https, udev, netif);
 

Thanks,

Signature · 1 line
Jerome
#6 lore raw

PATCH v2 3/3 net: lwip: introduce net_lwip_eth_stop() function

In reply to David Lechner

Add a introduce net_lwip_eth_stop() function and use that to stop the network interface after each command that uses the network.

This makes the behavior the same as the legacy net code and avoids potential issues with the network interface being left in an active state after a command finishes.

---
 cmd/lwip/ping.c     | 10 ++++++++--
 cmd/lwip/sntp.c     | 10 ++++++++--
 include/net-lwip.h  |  1 +
 net/lwip/dhcp.c     | 15 +++++++++++----
 net/lwip/dns.c      |  7 ++++++-
 net/lwip/net-lwip.c |  5 +++++
 net/lwip/nfs.c      |  4 ++++
 net/lwip/tftp.c     |  4 ++++
 net/lwip/wget.c     |  5 ++++-
 9 files changed, 51 insertions(+), 10 deletions(-)
cmd/lwip/ping.c +8 -2
index fc4cf7bde5f..836d1d8287f 100644
@@ -163,6 +163,7 @@ static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
 int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	ip_addr_t addr;
+	int ret = CMD_RET_FAILURE;
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
@@ -176,8 +177,13 @@ restart:
 		if (net_start_again() == 0)
 			goto restart;
 		else
-			return CMD_RET_FAILURE;
+			goto out;
 	}
 
-	return CMD_RET_SUCCESS;
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
+
+	return ret;
 }
cmd/lwip/sntp.c +8 -2
index 608345c873b..5fa400b104a 100644
@@ -101,6 +101,7 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	ip_addr_t *srvip;
 	char *server;
 	ip_addr_t ipaddr;
+	int ret = CMD_RET_FAILURE;
 
 	switch (argc) {
 	case 1:
@@ -127,7 +128,12 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		return CMD_RET_FAILURE;
 
 	if (sntp_loop(eth_get_dev(), srvip) < 0)
-		return CMD_RET_FAILURE;
+		goto out;
+
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
 
-	return CMD_RET_SUCCESS;
+	return ret;
 }
include/net-lwip.h +1 -0
index 20cb0992cce..5d0627eb271 100644
@@ -35,6 +35,7 @@ int eth_init_state_only(void); /* Set active state */
 
 int net_lwip_dns_init(void);
 int net_lwip_eth_start(void);
+void net_lwip_eth_stop(void);
 struct netif *net_lwip_new_netif(struct udevice *udev);
 struct netif *net_lwip_new_netif_noip(struct udevice *udev);
 void net_lwip_remove_netif(struct netif *netif);
net/lwip/dhcp.c +11 -4
index acdf601d7eb..18dc36ae7ca 100644
@@ -138,18 +138,25 @@ int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	dev = eth_get_dev();
 	if (!dev) {
 		log_err("No network device\n");
-		return CMD_RET_FAILURE;
+		ret = CMD_RET_FAILURE;
+		goto out;
 	}
 
 	ret = dhcp_loop(dev);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (argc > 1) {
 		struct cmd_tbl cmdtp = {};
 
-		return do_tftpb(&cmdtp, 0, argc, argv);
+		ret = do_tftpb(&cmdtp, 0, argc, argv);
+		goto out;
 	}
 
-	return CMD_RET_SUCCESS;
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
+
+	return ret;
 }
net/lwip/dns.c +6 -1
index 8b7b3b7f970..b620b0611d6 100644
@@ -91,6 +91,7 @@ int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	char *name;
 	char *var = NULL;
+	int ret;
 
 	if (argc == 1 || argc > 3)
 		return CMD_RET_USAGE;
@@ -103,5 +104,9 @@ int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	if (net_lwip_eth_start() < 0)
 		return CMD_RET_FAILURE;
 
-	return dns_loop(eth_get_dev(), name, var);
+	ret = dns_loop(eth_get_dev(), name, var);
+
+	net_lwip_eth_stop();
+
+	return ret;
 }
net/lwip/net-lwip.c +5 -0
index 0c83c004cab..be63531c2a2 100644
@@ -192,6 +192,11 @@ int net_lwip_eth_start(void)
 	return 0;
 }
 
+void net_lwip_eth_stop(void)
+{
+	eth_halt();
+}
+
 static struct netif *new_netif(struct udevice *udev, bool with_ip)
 {
 	unsigned char enetaddr[ARP_HLEN];
net/lwip/nfs.c +4 -0
index 9e6b801e465..4cc36373fdd 100644
@@ -187,6 +187,7 @@ static int nfs_loop(struct udevice *udev, ulong addr, char *fname,
 int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	int ret = CMD_RET_SUCCESS;
+	bool started = false;
 	char *arg = NULL;
 	char *words[2] = { };
 	char *fname = NULL;
@@ -281,10 +282,13 @@ int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		ret = CMD_RET_FAILURE;
 		goto out;
 	}
+	started = true;
 
 	if (nfs_loop(eth_get_dev(), laddr, fname, srvip) < 0)
 		ret = CMD_RET_FAILURE;
 out:
+	if (started)
+		net_lwip_eth_stop();
 	if (arg != net_boot_file_name)
 		free(arg);
 	return ret;
net/lwip/tftp.c +4 -0
index 7f3b28b8507..571c38172f9 100644
@@ -261,6 +261,7 @@ static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	int ret = CMD_RET_SUCCESS;
+	bool started = false;
 	char *arg = NULL;
 	char *words[3] = { };
 	char *fname = NULL;
@@ -365,12 +366,15 @@ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		ret = CMD_RET_FAILURE;
 		goto out;
 	}
+	started = true;
 
 	if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0)
 		ret = CMD_RET_FAILURE;
 	else
 		image_load_addr = laddr;
 out:
+	if (started)
+		net_lwip_eth_stop();
 	if (arg != net_boot_file_name)
 		free(arg);
 	return ret;
net/lwip/wget.c +4 -1
index 3a0b0dca145..247ece18e2b 100644
@@ -416,12 +416,15 @@ int wget_do_request(ulong dst_addr, char *uri)
 	udev = eth_get_dev();
 
 	netif = net_lwip_new_netif(udev);
-	if (!netif)
+	if (!netif) {
+		net_lwip_eth_stop();
 		return -ENODEV;
+	}
 
 	ret = wget_handle_request(&ctx, is_https, udev, netif);
 
 	net_lwip_remove_netif(netif);
+	net_lwip_eth_stop();
 
 	return ret;
 }
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2 3/3] net: lwip: introduce net_lwip_eth_stop() function

Quoted 101 lines · David Lechner

Add a introduce net_lwip_eth_stop() function and use that to stop the network interface after each command that uses the network.

This makes the behavior the same as the legacy net code and avoids potential issues with the network interface being left in an active state after a command finishes.

---
 cmd/lwip/ping.c     | 10 ++++++++--
 cmd/lwip/sntp.c     | 10 ++++++++--
 include/net-lwip.h  |  1 +
 net/lwip/dhcp.c     | 15 +++++++++++----
 net/lwip/dns.c      |  7 ++++++-
 net/lwip/net-lwip.c |  5 +++++
 net/lwip/nfs.c      |  4 ++++
 net/lwip/tftp.c     |  4 ++++
 net/lwip/wget.c     |  5 ++++-
 9 files changed, 51 insertions(+), 10 deletions(-)
cmd/lwip/ping.c +8 -2
index fc4cf7bde5f..836d1d8287f 100644
@@ -163,6 +163,7 @@ static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
 int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	ip_addr_t addr;
+	int ret = CMD_RET_FAILURE;
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
@@ -176,8 +177,13 @@ restart:
 		if (net_start_again() == 0)
 			goto restart;
 		else
-			return CMD_RET_FAILURE;
+			goto out;
 	}
 
-	return CMD_RET_SUCCESS;
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
+
+	return ret;
 }
cmd/lwip/sntp.c +8 -2
index 608345c873b..5fa400b104a 100644
@@ -101,6 +101,7 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	ip_addr_t *srvip;
 	char *server;
 	ip_addr_t ipaddr;
+	int ret = CMD_RET_FAILURE;
 
 	switch (argc) {
 	case 1:
@@ -127,7 +128,12 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		return CMD_RET_FAILURE;
 
 	if (sntp_loop(eth_get_dev(), srvip) < 0)
-		return CMD_RET_FAILURE;
+		goto out;
+
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
 
-	return CMD_RET_SUCCESS;
+	return ret;
 }
include/net-lwip.h +1 -0
index 20cb0992cce..5d0627eb271 100644
@@ -35,6 +35,7 @@ int eth_init_state_only(void); /* Set active state */
 
 int net_lwip_dns_init(void);
 int net_lwip_eth_start(void);
+void net_lwip_eth_stop(void);
 struct netif *net_lwip_new_netif(struct udevice *udev);
 struct netif *net_lwip_new_netif_noip(struct udevice *udev);
 void net_lwip_remove_netif(struct netif *netif);
net/lwip/dhcp.c +2 -1
index acdf601d7eb..18dc36ae7ca 100644
@@ -138,18 +138,25 @@ int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	dev = eth_get_dev();
 	if (!dev) {
 		log_err("No network device\n");
-		return CMD_RET_FAILURE;
+		ret = CMD_RET_FAILURE;
+		goto out;
 	}
 
 	ret = dhcp_loop(dev);

net/lwip/dhcp.c int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 153

Quoted 10 lines
net/lwip/dhcp.c +3 -2
 	if (ret)
-		return ret;
+		goto out;
 
 	if (argc > 1) {
 		struct cmd_tbl cmdtp = {};
 
-		return do_tftpb(&cmdtp, 0, argc, argv);
+		ret = do_tftpb(&cmdtp, 0, argc, argv);
+		goto out;

do_tftpb() already takes care of net_lwip_eth_stop() so this should probably be:

+ return do_tftpb(&cmdtp, 0, argc, argv);

But this also raises the qestion of the double call to net_lwip_eth_start():

do_dhcp()
net_lwip_eth_start()
do_tftpb()
net_lwip_eth_start()

How about adding a static counter to better deal with the nested case?

static int net_lwip_eth_started;

int net_lwip_eth_start(void)

{
	int ret;

	if (net_lwip_eth_started++ > 0)
		return 0;

	ret = eth_init();
	if (ret) {
		net_lwip_eth_started--;
		return ret;
	}

	return 0;
}

void net_lwip_eth_stop(void)

{
	if (!net_lwip_eth_started)
		return;

	if (--net_lwip_eth_started)
		return;

	eth_halt();
}

Thanks,

Signature · 1 line
Jerome
Quoted 125 lines
net/lwip/dhcp.c +6 -1
 	}
 
-	return CMD_RET_SUCCESS;
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
+
+	return ret;
 }
net/lwip/dns.c +6 -1
index 8b7b3b7f970..b620b0611d6 100644
@@ -91,6 +91,7 @@ int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	char *name;
 	char *var = NULL;
+	int ret;
 
 	if (argc == 1 || argc > 3)
 		return CMD_RET_USAGE;
@@ -103,5 +104,9 @@ int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	if (net_lwip_eth_start() < 0)
 		return CMD_RET_FAILURE;
 
-	return dns_loop(eth_get_dev(), name, var);
+	ret = dns_loop(eth_get_dev(), name, var);
+
+	net_lwip_eth_stop();
+
+	return ret;
 }
net/lwip/net-lwip.c +5 -0
index 0c83c004cab..be63531c2a2 100644
@@ -192,6 +192,11 @@ int net_lwip_eth_start(void)
 	return 0;
 }
 
+void net_lwip_eth_stop(void)
+{
+	eth_halt();
+}
+
 static struct netif *new_netif(struct udevice *udev, bool with_ip)
 {
 	unsigned char enetaddr[ARP_HLEN];
net/lwip/nfs.c +4 -0
index 9e6b801e465..4cc36373fdd 100644
@@ -187,6 +187,7 @@ static int nfs_loop(struct udevice *udev, ulong addr, char *fname,
 int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	int ret = CMD_RET_SUCCESS;
+	bool started = false;
 	char *arg = NULL;
 	char *words[2] = { };
 	char *fname = NULL;
@@ -281,10 +282,13 @@ int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		ret = CMD_RET_FAILURE;
 		goto out;
 	}
+	started = true;
 
 	if (nfs_loop(eth_get_dev(), laddr, fname, srvip) < 0)
 		ret = CMD_RET_FAILURE;
 out:
+	if (started)
+		net_lwip_eth_stop();
 	if (arg != net_boot_file_name)
 		free(arg);
 	return ret;
net/lwip/tftp.c +4 -0
index 7f3b28b8507..571c38172f9 100644
@@ -261,6 +261,7 @@ static int tftp_loop(struct udevice *udev, ulong addr, char *fname,
 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	int ret = CMD_RET_SUCCESS;
+	bool started = false;
 	char *arg = NULL;
 	char *words[3] = { };
 	char *fname = NULL;
@@ -365,12 +366,15 @@ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		ret = CMD_RET_FAILURE;
 		goto out;
 	}
+	started = true;
 
 	if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0)
 		ret = CMD_RET_FAILURE;
 	else
 		image_load_addr = laddr;
 out:
+	if (started)
+		net_lwip_eth_stop();
 	if (arg != net_boot_file_name)
 		free(arg);
 	return ret;
net/lwip/wget.c +4 -1
index 3a0b0dca145..247ece18e2b 100644
@@ -416,12 +416,15 @@ int wget_do_request(ulong dst_addr, char *uri)
 	udev = eth_get_dev();
 
 	netif = net_lwip_new_netif(udev);
-	if (!netif)
+	if (!netif) {
+		net_lwip_eth_stop();
 		return -ENODEV;
+	}
 
 	ret = wget_handle_request(&ctx, is_https, udev, netif);
 
 	net_lwip_remove_netif(netif);
+	net_lwip_eth_stop();
 
 	return ret;
 }
#8 lore raw

Re: [PATCH v2 3/3] net: lwip: introduce net_lwip_eth_stop() function

Quoted 159 lines · Jerome Forissier
Quoted 101 lines · David Lechner

Add a introduce net_lwip_eth_stop() function and use that to stop the network interface after each command that uses the network.

This makes the behavior the same as the legacy net code and avoids potential issues with the network interface being left in an active state after a command finishes.

---
 cmd/lwip/ping.c     | 10 ++++++++--
 cmd/lwip/sntp.c     | 10 ++++++++--
 include/net-lwip.h  |  1 +
 net/lwip/dhcp.c     | 15 +++++++++++----
 net/lwip/dns.c      |  7 ++++++-
 net/lwip/net-lwip.c |  5 +++++
 net/lwip/nfs.c      |  4 ++++
 net/lwip/tftp.c     |  4 ++++
 net/lwip/wget.c     |  5 ++++-
 9 files changed, 51 insertions(+), 10 deletions(-)
cmd/lwip/ping.c +8 -2
index fc4cf7bde5f..836d1d8287f 100644
@@ -163,6 +163,7 @@ static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
 int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	ip_addr_t addr;
+	int ret = CMD_RET_FAILURE;
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
@@ -176,8 +177,13 @@ restart:
 		if (net_start_again() == 0)
 			goto restart;
 		else
-			return CMD_RET_FAILURE;
+			goto out;
 	}
 
-	return CMD_RET_SUCCESS;
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
+
+	return ret;
 }
cmd/lwip/sntp.c +8 -2
index 608345c873b..5fa400b104a 100644
@@ -101,6 +101,7 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	ip_addr_t *srvip;
 	char *server;
 	ip_addr_t ipaddr;
+	int ret = CMD_RET_FAILURE;
 
 	switch (argc) {
 	case 1:
@@ -127,7 +128,12 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		return CMD_RET_FAILURE;
 
 	if (sntp_loop(eth_get_dev(), srvip) < 0)
-		return CMD_RET_FAILURE;
+		goto out;
+
+	ret = CMD_RET_SUCCESS;
+
+out:
+	net_lwip_eth_stop();
 
-	return CMD_RET_SUCCESS;
+	return ret;
 }
include/net-lwip.h +1 -0
index 20cb0992cce..5d0627eb271 100644
@@ -35,6 +35,7 @@ int eth_init_state_only(void); /* Set active state */
 
 int net_lwip_dns_init(void);
 int net_lwip_eth_start(void);
+void net_lwip_eth_stop(void);
 struct netif *net_lwip_new_netif(struct udevice *udev);
 struct netif *net_lwip_new_netif_noip(struct udevice *udev);
 void net_lwip_remove_netif(struct netif *netif);
net/lwip/dhcp.c +2 -1
index acdf601d7eb..18dc36ae7ca 100644
@@ -138,18 +138,25 @@ int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	dev = eth_get_dev();
 	if (!dev) {
 		log_err("No network device\n");
-		return CMD_RET_FAILURE;
+		ret = CMD_RET_FAILURE;
+		goto out;
 	}
 
 	ret = dhcp_loop(dev);

net/lwip/dhcp.c int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 153

Quoted 10 lines
net/lwip/dhcp.c +3 -2
 	if (ret)
-		return ret;
+		goto out;
 
 	if (argc > 1) {
 		struct cmd_tbl cmdtp = {};
 
-		return do_tftpb(&cmdtp, 0, argc, argv);
+		ret = do_tftpb(&cmdtp, 0, argc, argv);
+		goto out;

do_tftpb() already takes care of net_lwip_eth_stop() so this should probably be:

(quoted diff) +1 -0
+		return do_tftpb(&cmdtp, 0, argc, argv);

But this also raises the qestion of the double call to net_lwip_eth_start():

do_dhcp()
net_lwip_eth_start()
do_tftpb()
net_lwip_eth_start()

How about adding a static counter to better deal with the nested case?

static int net_lwip_eth_started;

int net_lwip_eth_start(void)

{
	int ret;

	if (net_lwip_eth_started++ > 0)
		return 0;

	ret = eth_init();
	if (ret) {
		net_lwip_eth_started--;
		return ret;
	}

	return 0;
}

void net_lwip_eth_stop(void)

{
	if (!net_lwip_eth_started)
		return;

	if (--net_lwip_eth_started)
		return;

	eth_halt();
}
Quoted 1 line

Thanks,

Yes, I like this idea. There is also an odd case in do_ping() that we will need to look at though. It currently would not work with reference counting since net_lwip_eth_start() can be called multiple times on retries.