diff mbox series

[v2,05/13] lwip: move net/lwip/ping.c to cmd/lwip

Message ID 20250606064211.3091237-6-jerome.forissier@linaro.org
State New
Headers show
Series sntp for NET_LWIP | expand

Commit Message

Jerome Forissier June 6, 2025, 6:41 a.m. UTC
Move the implementation of the ping command under cmd/.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
---

Changes in v2:
- New patch

 cmd/lwip/ping.c   | 175 ++++++++++++++++++++++++++++++++++++++++++++
 net/lwip/Makefile |   1 -
 net/lwip/ping.c   | 180 ----------------------------------------------
 3 files changed, 175 insertions(+), 181 deletions(-)
 delete mode 100644 net/lwip/ping.c
diff mbox series

Patch

diff --git a/cmd/lwip/ping.c b/cmd/lwip/ping.c
index af5c022f55f..21931821521 100644
--- a/cmd/lwip/ping.c
+++ b/cmd/lwip/ping.c
@@ -2,7 +2,182 @@ 
 /* Copyright (C) 2024-2025 Linaro Ltd. */
 
 #include <command.h>
+#include <console.h>
+#include <dm/device.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <lwip/icmp.h>
+#include <lwip/inet_chksum.h>
+#include <lwip/raw.h>
+#include <lwip/timeouts.h>
 #include <net.h>
+#include <time.h>
 
 U_BOOT_CMD(ping, 2, 1, do_ping, "send ICMP ECHO_REQUEST to network host",
 	   "pingAddress");
+
+#define PING_DELAY_MS 1000
+#define PING_COUNT 5
+/* Ping identifier - must fit on a u16_t */
+#define PING_ID 0xAFAF
+
+struct ping_ctx {
+	ip_addr_t target;
+	struct raw_pcb *pcb;
+	struct icmp_echo_hdr *iecho;
+	u16 seq_num;
+	bool alive;
+};
+
+static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p,
+		      const ip_addr_t *addr)
+{
+	struct ping_ctx *ctx = arg;
+	struct icmp_echo_hdr *iecho = ctx->iecho;
+
+	if (addr->addr != ctx->target.addr)
+		return 0;
+
+	if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
+	    pbuf_remove_header(p, IP_HLEN) == 0) {
+		iecho = (struct icmp_echo_hdr *)p->payload;
+
+		if (iecho->id == PING_ID &&
+		    iecho->seqno == lwip_htons(ctx->seq_num)) {
+			ctx->alive = true;
+			printf("host %s is alive\n", ipaddr_ntoa(addr));
+			pbuf_free(p);
+			return 1; /* eat the packet */
+		}
+		/* not eaten, restore original packet */
+		pbuf_add_header(p, IP_HLEN);
+	}
+
+	return 0; /* don't eat the packet */
+}
+
+static int ping_raw_init(struct ping_ctx *ctx)
+{
+	ctx->pcb = raw_new(IP_PROTO_ICMP);
+	if (!ctx->pcb)
+		return -ENOMEM;
+
+	raw_recv(ctx->pcb, ping_recv, ctx);
+	raw_bind(ctx->pcb, IP_ADDR_ANY);
+
+	return 0;
+}
+
+static void ping_raw_stop(struct ping_ctx *ctx)
+{
+	if (ctx->pcb)
+		raw_remove(ctx->pcb);
+}
+
+static void ping_prepare_echo(struct ping_ctx *ctx)
+{
+	struct icmp_echo_hdr *iecho = ctx->iecho;
+
+	ICMPH_TYPE_SET(iecho, ICMP_ECHO);
+	ICMPH_CODE_SET(iecho, 0);
+	iecho->chksum = 0;
+	iecho->id = PING_ID;
+	iecho->seqno = lwip_htons(ctx->seq_num);
+
+	iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
+}
+
+static void ping_send_icmp(struct ping_ctx *ctx)
+{
+	struct pbuf *p;
+	size_t ping_size = sizeof(struct icmp_echo_hdr);
+
+	p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
+	if (!p)
+		return;
+
+	if (p->len == p->tot_len && !p->next) {
+		ctx->iecho = (struct icmp_echo_hdr *)p->payload;
+		ping_prepare_echo(ctx);
+		raw_sendto(ctx->pcb, p, &ctx->target);
+	}
+
+	pbuf_free(p);
+}
+
+static void ping_send(void *arg)
+{
+	struct ping_ctx *ctx = arg;
+
+	ctx->seq_num++;
+	if (ctx->seq_num <= PING_COUNT) {
+		ping_send_icmp(ctx);
+		sys_timeout(PING_DELAY_MS, ping_send, ctx);
+	}
+}
+
+static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
+{
+	struct ping_ctx ctx = {};
+	struct netif *netif;
+	int ret;
+
+	netif = net_lwip_new_netif(udev);
+	if (!netif)
+		return -ENODEV;
+
+	printf("Using %s device\n", udev->name);
+
+	ret = ping_raw_init(&ctx);
+	if (ret < 0) {
+		net_lwip_remove_netif(netif);
+		return ret;
+	}
+
+	ctx.target = *addr;
+
+	ping_send(&ctx);
+
+	do {
+		sys_check_timeouts();
+		net_lwip_rx(udev, netif);
+		if (ctx.alive)
+			break;
+		if (ctrlc()) {
+			printf("\nAbort\n");
+			break;
+		}
+	} while (ctx.seq_num <= PING_COUNT);
+
+	sys_untimeout(ping_send, &ctx);
+	ping_raw_stop(&ctx);
+
+	net_lwip_remove_netif(netif);
+
+	if (ctx.alive)
+		return 0;
+
+	printf("ping failed; host %s is not alive\n", ipaddr_ntoa(addr));
+	return -1;
+}
+
+int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	ip_addr_t addr;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	if (!ipaddr_aton(argv[1], &addr))
+		return CMD_RET_USAGE;
+
+restart:
+	if (net_lwip_eth_start() < 0 || ping_loop(eth_get_dev(), &addr) < 0) {
+		if (net_start_again() == 0)
+			goto restart;
+		else
+			return CMD_RET_FAILURE;
+	}
+
+	return CMD_RET_SUCCESS;
+}
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 255c7d018b0..97299d9b542 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -2,7 +2,6 @@  ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot
 
 obj-$(CONFIG_$(PHASE_)DM_ETH) += net-lwip.o
 obj-$(CONFIG_CMD_DHCP) += dhcp.o
-obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
 obj-$(CONFIG_WGET) += wget.o
 
diff --git a/net/lwip/ping.c b/net/lwip/ping.c
deleted file mode 100644
index d8042ceecf9..00000000000
--- a/net/lwip/ping.c
+++ /dev/null
@@ -1,180 +0,0 @@ 
-// SPDX-License-Identifier: GPL-2.0+
-/* Copyright (C) 2024 Linaro Ltd. */
-
-#include <command.h>
-#include <console.h>
-#include <dm/device.h>
-#include <linux/delay.h>
-#include <linux/errno.h>
-#include <lwip/icmp.h>
-#include <lwip/inet_chksum.h>
-#include <lwip/raw.h>
-#include <lwip/timeouts.h>
-#include <net.h>
-#include <time.h>
-
-#define PING_DELAY_MS 1000
-#define PING_COUNT 5
-/* Ping identifier - must fit on a u16_t */
-#define PING_ID 0xAFAF
-
-struct ping_ctx {
-	ip_addr_t target;
-	struct raw_pcb *pcb;
-	struct icmp_echo_hdr *iecho;
-	uint16_t seq_num;
-	bool alive;
-};
-
-static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p,
-		      const ip_addr_t *addr)
-{
-	struct ping_ctx *ctx = arg;
-	struct icmp_echo_hdr *iecho = ctx->iecho;
-
-	if (addr->addr != ctx->target.addr)
-		return 0;
-
-	if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
-	    pbuf_remove_header(p, IP_HLEN) == 0) {
-		iecho = (struct icmp_echo_hdr *)p->payload;
-
-		if (iecho->id == PING_ID &&
-		    iecho->seqno == lwip_htons(ctx->seq_num)) {
-			ctx->alive = true;
-			printf("host %s is alive\n", ipaddr_ntoa(addr));
-			pbuf_free(p);
-			return 1; /* eat the packet */
-		}
-		/* not eaten, restore original packet */
-		pbuf_add_header(p, IP_HLEN);
-	}
-
-	return 0; /* don't eat the packet */
-}
-
-static int ping_raw_init(struct ping_ctx *ctx)
-{
-	ctx->pcb = raw_new(IP_PROTO_ICMP);
-	if (!ctx->pcb)
-		return -ENOMEM;
-
-	raw_recv(ctx->pcb, ping_recv, ctx);
-	raw_bind(ctx->pcb, IP_ADDR_ANY);
-
-	return 0;
-}
-
-static void ping_raw_stop(struct ping_ctx *ctx)
-{
-	if (ctx->pcb)
-		raw_remove(ctx->pcb);
-}
-
-static void ping_prepare_echo(struct ping_ctx *ctx)
-{
-	struct icmp_echo_hdr *iecho = ctx->iecho;
-
-	ICMPH_TYPE_SET(iecho, ICMP_ECHO);
-	ICMPH_CODE_SET(iecho, 0);
-	iecho->chksum = 0;
-	iecho->id = PING_ID;
-	iecho->seqno = lwip_htons(ctx->seq_num);
-
-	iecho->chksum = inet_chksum(iecho, sizeof(*iecho));
-}
-
-static void ping_send_icmp(struct ping_ctx *ctx)
-{
-	struct pbuf *p;
-	size_t ping_size = sizeof(struct icmp_echo_hdr);
-
-	p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
-	if (!p)
-		return;
-
-	if (p->len == p->tot_len && !p->next) {
-		ctx->iecho = (struct icmp_echo_hdr *)p->payload;
-		ping_prepare_echo(ctx);
-		raw_sendto(ctx->pcb, p, &ctx->target);
-	}
-
-	pbuf_free(p);
-}
-
-static void ping_send(void *arg)
-{
-	struct ping_ctx *ctx = arg;
-
-	ctx->seq_num++;
-	if (ctx->seq_num <= PING_COUNT) {
-		ping_send_icmp(ctx);
-		sys_timeout(PING_DELAY_MS, ping_send, ctx);
-	}
-}
-
-static int ping_loop(struct udevice *udev, const ip_addr_t *addr)
-{
-	struct ping_ctx ctx = {};
-	struct netif *netif;
-	int ret;
-
-	netif = net_lwip_new_netif(udev);
-	if (!netif)
-		return -ENODEV;
-
-	printf("Using %s device\n", udev->name);
-
-	ret = ping_raw_init(&ctx);
-	if (ret < 0) {
-		net_lwip_remove_netif(netif);
-		return ret;
-	}
-
-	ctx.target = *addr;
-
-	ping_send(&ctx);
-
-	do {
-		sys_check_timeouts();
-		net_lwip_rx(udev, netif);
-		if (ctx.alive)
-			break;
-		if (ctrlc()) {
-			printf("\nAbort\n");
-			break;
-		}
-	} while (ctx.seq_num <= PING_COUNT);
-
-	sys_untimeout(ping_send, &ctx);
-	ping_raw_stop(&ctx);
-
-	net_lwip_remove_netif(netif);
-
-	if (ctx.alive)
-		return 0;
-
-	printf("ping failed; host %s is not alive\n", ipaddr_ntoa(addr));
-	return -1;
-}
-
-int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
-{
-	ip_addr_t addr;
-
-	if (argc < 2)
-		return CMD_RET_USAGE;
-
-	if (!ipaddr_aton(argv[1], &addr))
-		return CMD_RET_USAGE;
-
-restart:
-	if (net_lwip_eth_start() < 0 || ping_loop(eth_get_dev(), &addr) < 0) {
-		if (net_start_again() == 0)
-			goto restart;
-		else
-			return CMD_RET_FAILURE;
-	}
-
-	return CMD_RET_SUCCESS;
-}