From patchwork Mon Jun 17 15:32:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 804882 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp611474wrr; Mon, 17 Jun 2024 08:34:42 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUQ3QFsAcGCiIOUlvUMIHSqxTCH6bGfsYG+jos12RZCrW8IC1fPthdNY21Oah+g6tosrX9EGiBMW14ObZ2XPEuN X-Google-Smtp-Source: AGHT+IEjondPnmA+ubeGL3QaS11x1MTFLHU7z1uwL2eLAJebrpF2IIEO3yC0buyDObBK72vgDsO8 X-Received: by 2002:a19:e014:0:b0:52c:98b1:36d9 with SMTP id 2adb3069b0e04-52ca6e9da28mr7252112e87.62.1718638482034; Mon, 17 Jun 2024 08:34:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718638482; cv=none; d=google.com; s=arc-20160816; b=oeuRfgVt62irf9mvkAeRfiJIj3SbthS6bbHoJqbsl0qogfRdwJvfI/p1WAsWlFTXkL qPNLC6R3Zlrnif01ybI/RBgVargQDCs04dCQRy3F3qMcY2Cxd++W6U9/dA45QMT/gIVR zSn9fxZdLaOZQmLf1iKsQpKlYYBnR9rlNFkXUbirbALL7izgdfUyLbxRsePunoVFxBZV hxDa0BJCwZc4C4+UNP08tCcHsYolqoC2DHQ9C6spaIeN+HbRaX9HIpP/fwoeMLW7zQTf Q3WZ9zyjVXCwUDnaOMRntvS1cMM+GNg78cDOy4cTfz0FmaaLgogEGuyTpyhi4RGzcCVm Zf4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ba6V2jwrSohqfhLWHSsiGktUyB3OyaTdQBVDttl3Kfg=; fh=7uPs/cguLYnM12fXj44p32RH3Z+fhJ+3gQe5lH8zA1w=; b=QWd6EsdVfUrMfhLtThwE1Hn0wK/xkRl+VE0R1RmXKM70qdqUq9WS7PXSSS5uMrwjn9 k/43p/6xYXD2LLCB+pCmQ1u9igTClngcB0Bi9umLLdUl9Vl6e/woew7i0BCDutkPtPK/ S0zyyKgxJw0gZzrSBJFF9XFq727d/QqJao+goRUEuPqF9YVSulB3Wax1BiH5D4B6Y4Yv ijOqqpAJ1EccTmXNjm8U4SQNkGI5V4g28SwkSEmX/4ihfy045G6iCUTLe9oVkCwYgz4M dBVrhiwVA7vZUBQflS9Qhmfa112+m+QeY6rD9LEZiK0CwD1DOXmleTsmkXYZynt9h1Mr Sl4Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GA+DKmOz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56d4bbc8si439108566b.225.2024.06.17.08.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 08:34:42 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GA+DKmOz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DDA108839A; Mon, 17 Jun 2024 17:33:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="GA+DKmOz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 053D8883B3; Mon, 17 Jun 2024 17:33:46 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D398988398 for ; Mon, 17 Jun 2024 17:33:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-4230366ad7bso39004225e9.1 for ; Mon, 17 Jun 2024 08:33:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718638423; x=1719243223; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ba6V2jwrSohqfhLWHSsiGktUyB3OyaTdQBVDttl3Kfg=; b=GA+DKmOzhq5DBvvQ1oZ8HmlYfFTf9Wt1zrxjhsKiKNNCSPFbXrxCSXvC431LOMv29b LFW45XvN3S8mzQEvl7GQK7pCdVfuFG4oqDU5UzFO3AwUDkz7ajghTT15Xi3ei6uXmuGU dDX9mI5g9+/LxQYciahzE2q1znZzd7BpE6ja7ZUFc8eATcBaOE6f/yiKlHsEO4DZiG76 zO7LqEdy+9cW901OYrz7HKHVbjx/ykv/Jxp8af72kmh9NSlROR8coV1EkB18ByM2F5C6 47sQEGfV9YFQAOi6qHa7mWlQ7xWxgz4dOokAIjCHjT+e02IRrALjXb5gPnYv+2Q6wZAQ udBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718638423; x=1719243223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ba6V2jwrSohqfhLWHSsiGktUyB3OyaTdQBVDttl3Kfg=; b=uL1AgrlWFpVd3bahcmpQejKzBTfUG0hdijxxbBXFgi3z6tgjANAy+yeW857enFEpm4 /tY95078X/jlF3Rg+fDW+WAjWmQgG4l8ptpcmbWvesRO1W1IYkMCU9ZOxT9Qh8cbuuDK HqZUR+jFO/GcEL/nwJLZtu9FILucRXM6s6xWroBpLZfa6nuCoBwXAQnYdpLPgFT/8IOo K0XjwYpe+5F51klnDuVreQzIcYikNnpuTU1S8MZYVsvFnkkd6G2w7R7LwtP241Lkf9KH HA+8ikUQH/V34OFqqFSzxyV3GxkMnw9xcBjjx09wKf2d6hqccnvoyZmhPN3ZdbJnS1gX cT9w== X-Gm-Message-State: AOJu0YzNXmXkhb1eRVVjh9gWVMTv9SH/UlsP9VoQVlFGDHpkVpTJrNW2 8ESM+sdI563QmK/eHx6G2KmYMlwoLi8ujSw1GfmM5cmSoDNWG3J5H05QamTIt1iwPxFkBKv46rM QDHc= X-Received: by 2002:a05:600c:4fd6:b0:422:fb9a:90b3 with SMTP id 5b1f17b1804b1-42304844aaemr121686085e9.32.1718638423168; Mon, 17 Jun 2024 08:33:43 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:6e75:be8f:56ae:2b5e]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-422f602f620sm161350895e9.19.2024.06.17.08.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 08:33:42 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Javier Tia , Maxim Uvarov , Jerome Forissier , Tom Rini , Simon Glass , Eddie James , Mattijs Korpershoek , AKASHI Takahiro , Francis Laniel , Peter Robinson Subject: [PATCH v4 07/14] net-lwip: add ping command Date: Mon, 17 Jun 2024 17:32:59 +0200 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add support for the the ping command with NET_LWIP. The implementation is derived from lwIP's contrib/apps/ping/ping.c. Signed-off-by: Jerome Forissier --- boot/Kconfig | 2 +- cmd/Kconfig | 6 ++ cmd/net-lwip.c | 8 +++ include/net-lwip.h | 1 + net-lwip/Makefile | 1 + net-lwip/ping.c | 174 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 net-lwip/ping.c diff --git a/boot/Kconfig b/boot/Kconfig index 004e69dd92a..983a284ec52 100644 --- a/boot/Kconfig +++ b/boot/Kconfig @@ -379,7 +379,7 @@ config BOOT_DEFAULTS_CMDS select CMD_FS_GENERIC select CMD_PART if PARTITIONS select CMD_DHCP if CMD_NET || CMD_NET_LWIP - select CMD_PING if CMD_NET + select CMD_PING if CMD_NET || CMD_NET_LWIP select CMD_PXE if CMD_NET select CMD_BOOTI if ARM64 select CMD_BOOTZ if ARM && !ARM64 diff --git a/cmd/Kconfig b/cmd/Kconfig index 94a8de266f6..07cfe824e3f 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -2098,6 +2098,12 @@ config CMD_DHCP help Boot image via network using DHCP/TFTP protocol +config CMD_PING + bool "ping" + select PROT_RAW_LWIP + help + Send ICMP ECHO_REQUEST to network host + config CMD_TFTPBOOT bool "tftp" select PROT_UDP_LWIP diff --git a/cmd/net-lwip.c b/cmd/net-lwip.c index 80f0872bb8f..feed719beef 100644 --- a/cmd/net-lwip.c +++ b/cmd/net-lwip.c @@ -12,6 +12,14 @@ U_BOOT_CMD( ); #endif +#if defined(CONFIG_CMD_PING) +U_BOOT_CMD( + ping, 2, 1, do_ping, + "send ICMP ECHO_REQUEST to network host", + "pingAddress" +); +#endif + #if defined(CONFIG_CMD_TFTPBOOT) U_BOOT_CMD( tftpboot, 3, 0, do_tftpb, diff --git a/include/net-lwip.h b/include/net-lwip.h index 46cf6875f7e..b34f53b4891 100644 --- a/include/net-lwip.h +++ b/include/net-lwip.h @@ -12,5 +12,6 @@ void net_lwip_remove_netif(struct netif *netif); struct netif *net_lwip_get_netif(void); int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); #endif /* __NET_LWIP_H__ */ diff --git a/net-lwip/Makefile b/net-lwip/Makefile index a56c32bfa74..e68d4e24197 100644 --- a/net-lwip/Makefile +++ b/net-lwip/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_$(SPL_)DM_ETH) += ../net/eth_common.o obj-$(CONFIG_$(SPL_)DM_ETH) += ../net/eth-uclass.o obj-$(CONFIG_$(SPL_)DM_ETH) += net-lwip.o obj-$(CONFIG_CMD_DHCP) += dhcp.o +obj-$(CONFIG_CMD_PING) += ping.o obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o # Disable this warning as it is triggered by: diff --git a/net-lwip/ping.c b/net-lwip/ping.c new file mode 100644 index 00000000000..de0b633dc32 --- /dev/null +++ b/net-lwip/ping.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2024 Linaro Ltd. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); + } +} + +int ping_send(const ip_addr_t* addr) +{ + struct ping_ctx ctx = {}; + struct netif *netif; + int ret; + + netif = net_lwip_new_netif(); + if (!netif) + return CMD_RET_FAILURE; + + printf("Using %s device\n", eth_get_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(); + eth_rx(); + 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; + + if (ping_send(&addr) < 0) + return CMD_RET_FAILURE; + + return CMD_RET_SUCCESS; +}