From patchwork Wed Oct 9 14:49:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 833956 Delivered-To: patch@linaro.org Received: by 2002:adf:a1de:0:b0:37d:45d0:187 with SMTP id v30csp150045wrv; Wed, 9 Oct 2024 07:51:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUS07KfUcTKW6Vgh6r7EEOrsshUZGq9Gja0js8y5/GRWOBFcrFY34+MamgJraqdaUDJYeI37w==@linaro.org X-Google-Smtp-Source: AGHT+IEeA7gG4n0CiQKLuHYgwK6vwLY4LZbDHna8Je6dPDeiXK63cGJqzo4EvJO4XEXwHuFTQWES X-Received: by 2002:adf:f6ca:0:b0:37c:cfeb:e612 with SMTP id ffacd0b85a97d-37d3a9b73a8mr1861270f8f.1.1728485473950; Wed, 09 Oct 2024 07:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728485473; cv=none; d=google.com; s=arc-20240605; b=J8IvuW6h4pp5Gy0esba0qwkeCjTY33KVTHyHZ7MU57XkDCQyETLuAkamftY85Ots+n oD7/VEFgL7HEvRZFpx+pygEIvdPgRCDA5Ga7sWpkG0e2mpFwUo7P6KUwO/wwi8Yilq6q tfi+QiV+xzugzSg0vj40OZPZ7Vry/DvDPLvyr2PbhBt7UrQRtCEKKee08pUCmWX/g6Fs 0IVXD2iPwM/fDZvtO+zMnw5AR5AVInP8c+ZE1cTL4PxOt2dd4ZLH/7h44zoghfj/6fUb 3qBsjXlJyKCMVodsjLsEZgRzh/kg25g7iOiK7iz6xSw+s/QNT4yaz+BCMJ1Hux+U0rZX OveQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=ga32euvPkGSQVXpBzMEZunKOSLFRAK5yLOjQLFqx7O4=; fh=M1S23L4OuXqF/0xVUVC1y8b6k04LnWuSUlGL53OIcXM=; b=Eswv7P4eYt+maZvDMPwVGdoB3/q1cy/059Prb7Ap1MnAWrDYrW7lfAqCqJ5HtVwOW0 SoCeWQud7CmjYZWHwzJ8KaAjat20lH4OptJAuHMkWHyeq3AjONsuDXAdcJiiuD1pXqfn yD1WpxLoYO8Iyw0TA562eL8Neksa88Liw+oQoz5oPlZumaeVdNkZ6DaN4HWPJRFKCl3t mqPFDeMGbisgBB0n4Nz4FFkcfEDbd3VBgr4kotkGPrF7iWPQpuzMawb99bFcMhECHn0O 3dWCsupOukN3/J+GR6Jw5HmhUDr8WyJduUgVOxxV/atpD/moDw83fYq/G6Hrg4Evyznl /uHg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m54tnx9R; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ffacd0b85a97d-37d16911717si4224288f8f.86.2024.10.09.07.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 07:51:13 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m54tnx9R; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6613A88FED; Wed, 9 Oct 2024 16:50:34 +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="m54tnx9R"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AAA3F88EE2; Wed, 9 Oct 2024 16:50:33 +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,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) (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 D80F888EDD for ; Wed, 9 Oct 2024 16:50:30 +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-x333.google.com with SMTP id 5b1f17b1804b1-42cb806623eso65556905e9.2 for ; Wed, 09 Oct 2024 07:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1728485430; x=1729090230; 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=ga32euvPkGSQVXpBzMEZunKOSLFRAK5yLOjQLFqx7O4=; b=m54tnx9R0l0qrRpRbMMxkTa177wgJWU8X+ZFPcf5XOfDxdTe2Fu1m1Kr4gUAwk9P+k 7iW716vETKm6szaveoBk1p8PY5DBNNqCwJGZsADNy1X9mheQZDAccqgEppz6IJvpJ/d5 MFA7C4zPIZmovUUOva3T3ZjEyplc6sR9eeJfw5DWHZ4kvNR3YxDu2ysydarC1oXqYjVZ DkwSHn/Kg/YBFMD6u69+XRQhcOy9j2V54Q9aXKBcL65982Xspp4lZjr6+ncRNGXIBCPq jdaayuQw7yHAVu24244ebLEi1Mippfst8WMx4vyDPzGMweatfBrhHFOTLaQhvB89ePeM dkBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728485430; x=1729090230; 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=ga32euvPkGSQVXpBzMEZunKOSLFRAK5yLOjQLFqx7O4=; b=SYexbabM3dJgo0LrPtTPUyqZwt/fX7CNRXjb0wIYWecPo8ovQB+xFGp6ylVR7EHWGQ oJybUGPJGWRT2mh1HB692r9oscz/vLqpgROppTf7jyVExOp4xIATeivUL4z1cEfwhudt cUlWEY8UBrdIG42SSI/R7kToIx8PTRonULjfuyQcmkmwjLVq9Ov0JUWUJiRz1CMSdmx9 zpcHKZ2u0XnJf39Oay2u1YDqc0mVZ7W3A3kEBdZaCODlXG5RRYuMzUse899flkoqdnUM F68vCDKuftFZjpfI3+844YOVfWwapBY1oE1k9C91cI1j0K/UI6ttYgI7eKobj9plplol +Tig== X-Gm-Message-State: AOJu0YzHlQgzAM4aJOxaw6d3EbBv/gXTZks74dv/YgcT5Ovsn+o3iAWx r6c6z3vN81swDnho/Z56Fl2MXEKRKqlwsq9NrG3tt4QPNrhnTEkO/deArqX4UbNKRgccPhojv8x zYC9kGMLe X-Received: by 2002:adf:f04a:0:b0:37d:2e67:222a with SMTP id ffacd0b85a97d-37d3a9d90e0mr1870377f8f.18.1728485430064; Wed, 09 Oct 2024 07:50:30 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:9b46:2ad8:5153:1163]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d1691b5ddsm10700957f8f.47.2024.10.09.07.50.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 07:50:29 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Javier Tia , Raymond Mao , Maxim Uvarov , Tim Harvey , Anton Antonov , Jerome Forissier , Tom Rini , Joe Hershberger , Ramon Fried , Simon Glass , Mattijs Korpershoek , Heinrich Schuchardt , AKASHI Takahiro Subject: [PATCH v12 12/21] net: lwip: add ping command Date: Wed, 9 Oct 2024 16:49:53 +0200 Message-Id: <253a8999568410e540ecb029894417d2908d1d8c.1728482473.git.jerome.forissier@linaro.org> 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 Acked-by: Ilias Apalodimas --- cmd/Kconfig | 11 +-- cmd/net-lwip.c | 8 ++ include/net-lwip.h | 1 + net/lwip/Makefile | 1 + net/lwip/ping.c | 177 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+), 5 deletions(-) create mode 100644 net/lwip/ping.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 01b884f6f59..132835e43f8 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -2024,11 +2024,6 @@ config CMD_WGET wget is a simple command to download kernel, or other files, from a http server over TCP. -config CMD_PING - bool "ping" - help - Send ICMP ECHO_REQUEST to network host - config CMD_PING6 bool "ping6" depends on IPV6 @@ -2122,6 +2117,12 @@ config CMD_MDIO The MDIO interface is orthogonal to the MII interface and extends it by adding access to more registers through indirect addressing. +config CMD_PING + bool "ping" + select PROT_RAW_LWIP if NET_LWIP + help + Send ICMP ECHO_REQUEST to network host + config CMD_TFTPBOOT bool "tftp" select PROT_UDP_LWIP if NET_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 37744b0e2cd..9aa371e962c 100644 --- a/include/net-lwip.h +++ b/include/net-lwip.h @@ -17,5 +17,6 @@ struct netif *net_lwip_get_netif(void); int net_lwip_rx(struct udevice *udev, struct netif *netif); 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 4e92a101ddb..b5af37a9b18 100644 --- a/net/lwip/Makefile +++ b/net/lwip/Makefile @@ -2,4 +2,5 @@ ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot 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 diff --git a/net/lwip/ping.c b/net/lwip/ping.c new file mode 100644 index 00000000000..8dafa25959f --- /dev/null +++ b/net/lwip/ping.c @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2024 Linaro Ltd. */ + +#include +#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); + } +} + +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 CMD_RET_FAILURE; + + 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; + + eth_set_current(); + + if (ping_loop(eth_get_dev(), &addr) < 0) + return CMD_RET_FAILURE; + + return CMD_RET_SUCCESS; +}