U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ilias Apalodimas <ilias.apalodimas@linaro.org>
To: Jerome Forissier <jerome.forissier@linaro.org>
Cc: u-boot@lists.denx.de, Javier Tia <javier.tia@linaro.org>,
	Maxim Uvarov <muvarov@gmail.com>, Tom Rini <trini@konsulko.com>,
	Simon Glass <sjg@chromium.org>,
	Eddie James <eajames@linux.ibm.com>,
	Mattijs Korpershoek <mkorpershoek@baylibre.com>,
	AKASHI Takahiro <akashi.tkhro@gmail.com>,
	Michal Simek <michal.simek@amd.com>,
	Francis Laniel <francis.laniel@amarulasolutions.com>,
	Peter Robinson <pbrobinson@gmail.com>
Subject: Re: [PATCH v2 06/14] net-lwip: add ping command
Date: Thu, 6 Jun 2024 12:10:32 +0300	[thread overview]
Message-ID: <ZmF9CBbXSg4kDJLv@hera> (raw)
In-Reply-To: <284076d79cd331cf1a4923e0cea32fefc4c14fba.1716566960.git.jerome.forissier@linaro.org>

> +#include <net-lwip.h>
> +#include <time.h>
> +
> +#define PING_DELAY_MS 1000
> +#define PING_TIMEOUT_MS 10000
> +/* Additional data size to include in the packet */
> +#define PING_DATA_SIZE 32
> +/* Ping identifier - must fit on a u16_t */
> +#define PING_ID 0xAFAF
> +
> +static const ip_addr_t *ping_target;
> +static struct raw_pcb *ping_pcb;
> +static u16_t ping_seq_num;

As a general note, u8_t u16_t etc are lwip constructs.
Can we not use them and instead use the original definition?
uint8_t etc. I am not sure introducing another define is what we want. Tom?

> +static bool ping_target_alive;
> +
> +static u8_t ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p,
> +		      const ip_addr_t *addr)
> +{
> +	struct icmp_echo_hdr *iecho;
> +
> +	if (addr->addr != ping_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(ping_seq_num))) {
> +			ping_target_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(void)
> +{
> +	ping_pcb = raw_new(IP_PROTO_ICMP);
> +	if (!ping_pcb)
> +		return -ENOMEM;
> +
> +	raw_recv(ping_pcb, ping_recv, NULL);

Instead of defining a global variable ping_target_alive can we instead pass
the ptr of void *recv_arg? We can then fill in that private ptr with the
status

> +	raw_bind(ping_pcb, IP_ADDR_ANY);
> +
> +	return 0;
> +}
> +
> +static void ping_raw_stop(void)
> +{
> +	if (ping_pcb != NULL) {
> +		raw_remove(ping_pcb);
> +		ping_pcb = NULL;
> +	}
> +}
> +
> +static void ping_prepare_echo(struct icmp_echo_hdr *iecho, u16_t len)
> +{
> +	size_t i;
> +	size_t data_len = len - sizeof(struct icmp_echo_hdr);
> +
> +	ICMPH_TYPE_SET(iecho, ICMP_ECHO);
> +	ICMPH_CODE_SET(iecho, 0);
> +	iecho->chksum = 0;
> +	iecho->id = PING_ID;
> +	iecho->seqno = lwip_htons(++ping_seq_num);
> +
> +	/* Fill the additional data buffer with some data */
> +	for(i = 0; i < data_len; i++) {
> +		((char *)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
> +	}

is this additional data used? And if they are shouldn't we care about
endianess?

> +
> +	iecho->chksum = inet_chksum(iecho, len);
> +}
> +
> +static void ping_send_icmp(struct raw_pcb *raw, const ip_addr_t *addr)
> +{
> +	struct pbuf *p;
> +	struct icmp_echo_hdr *iecho;
> +	size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
> +
> +	p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
> +	if (!p)
> +		return;
> +
> +	if ((p->len == p->tot_len) && (p->next == NULL)) {
> +		iecho = (struct icmp_echo_hdr *)p->payload;
> +		ping_prepare_echo(iecho, (u16_t)ping_size);
> +		raw_sendto(raw, p, addr);
> +	}
> +
> +	pbuf_free(p);
> +}
> +
> +static void ping_send(void *arg)
> +{
> +	struct raw_pcb *pcb = (struct raw_pcb *)arg;
> +
> +	ping_send_icmp(pcb, ping_target);
> +	sys_timeout(PING_DELAY_MS, ping_send, ping_pcb);
> +}
> +
> +static int ping_loop(const ip_addr_t* addr)
> +{
> +	ulong start;
> +	int ret;
> +
> +	printf("Using %s device\n", eth_get_name());
> +
> +	ret = ping_raw_init();
> +	if (ret < 0)
> +		return ret;
> +	ping_target = addr;
> +
> +	start = get_timer(0);
> +	ping_send(ping_pcb);
> +
> +	do {
> +		eth_rx();

eth_rx() has a ret value. Don't we have to check it here?

> +		if (ping_target_alive)
> +			break;
> +		sys_check_timeouts();
> +		if (ctrlc()) {
> +			printf("\nAbort\n");
> +			break;
> +		}
> +	} while (get_timer(start) < PING_TIMEOUT_MS);
> +
> +	sys_untimeout(ping_send, ping_pcb);
> +	ping_raw_stop();
> +	ping_target = NULL;
> +
> +	if (ping_target_alive) {
> +		ping_target_alive = false;
> +		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_loop(&addr) < 0)
> +		return CMD_RET_FAILURE;
> +
> +	return CMD_RET_SUCCESS;
> +}
> --
> 2.40.1
>

Regards
/Ilias

  reply	other threads:[~2024-06-06  9:10 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-24 16:19 [PATCH v2 00/14] Introduce the lwIP network stack Jerome Forissier
2024-05-24 16:19 ` [PATCH v2 01/14] net: introduce alternative implementation as net-lwip/ Jerome Forissier
2024-05-27 15:34   ` Tom Rini
2024-05-28 11:53     ` Ilias Apalodimas
2024-06-05 17:48     ` Jerome Forissier
2024-05-24 16:19 ` [PATCH v2 02/14] Squashed 'lib/lwip/lwip/' content from commit 0a0452b2c39 Jerome Forissier
2024-05-24 16:19 ` [PATCH v2 03/14] net-lwip: build lwIP Jerome Forissier
2024-05-24 16:19 ` [PATCH v2 04/14] net-lwip: add DHCP support and dhcp commmand Jerome Forissier
2024-05-24 16:19 ` [PATCH v2 05/14] net-lwip: add TFTP support and tftpboot command Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 06/14] net-lwip: add ping command Jerome Forissier
2024-06-06  9:10   ` Ilias Apalodimas [this message]
2024-06-06 12:04     ` Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 07/14] net-lwip: add dns command Jerome Forissier
2024-06-06  6:29   ` Ilias Apalodimas
2024-06-06  8:51     ` Maxim Uvarov
2024-06-06 12:19     ` Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 08/14] net-lwip: add wget command Jerome Forissier
2024-05-28 13:39   ` Maxim Uvarov
2024-06-06  9:56     ` Jerome Forissier
2024-06-06 10:16       ` Maxim Uvarov
2024-06-06 12:14         ` Jerome Forissier
2024-06-06  9:38   ` Ilias Apalodimas
2024-05-24 16:20 ` [PATCH v2 09/14] test: dm: dsa, eth: disable tests when CONFIG_NET_LWIP=y Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 10/14] cmd: bdinfo: enable -e when CONFIG_CMD_NET_LWIP=y Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 11/14] configs: add qemu_arm64_lwip_defconfig Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 12/14] test/py: net: add _lwip variants of dhcp, ping and tftpboot tests Jerome Forissier
2024-05-28  9:41   ` Love Kumar
2024-05-28  9:53   ` Maxim Uvarov
2024-05-30 14:11     ` Jerome Forissier
2024-05-30 14:22       ` Maxim Uvarov
2024-06-06  9:18         ` Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 13/14] MAINTAINERS: net-lwip: add myself as a maintainer Jerome Forissier
2024-05-24 16:20 ` [PATCH v2 14/14] CI: add qemu_arm64_lwip to the test matrix Jerome Forissier
2024-05-27  9:23 ` [PATCH v2 00/14] Introduce the lwIP network stack Francesco Dolcini
2024-05-27  9:36   ` Jerome Forissier
2024-05-27  9:45     ` Martin Husemann
2024-05-27 12:45       ` Jerome Forissier
2024-05-27 12:47         ` Martin Husemann
2024-05-28 11:50           ` Ilias Apalodimas
2024-06-04 23:13   ` Peter Robinson
2024-05-27 15:34 ` Tom Rini
2024-06-06  9:15   ` Jerome Forissier
2024-06-06 14:25     ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZmF9CBbXSg4kDJLv@hera \
    --to=ilias.apalodimas@linaro.org \
    --cc=akashi.tkhro@gmail.com \
    --cc=eajames@linux.ibm.com \
    --cc=francis.laniel@amarulasolutions.com \
    --cc=javier.tia@linaro.org \
    --cc=jerome.forissier@linaro.org \
    --cc=michal.simek@amd.com \
    --cc=mkorpershoek@baylibre.com \
    --cc=muvarov@gmail.com \
    --cc=pbrobinson@gmail.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox