Re: [PATCHv5 07/13] net/lwip: implement ping cmd

Ilias Apalodimas
2023-08-03T09:32:54Z

On Wed, Aug 02, 2023 at 08:06:52PM +0600, Maxim Uvarov wrote:
> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
> ---
> lib/lwip/Makefile | 1 +
> lib/lwip/apps/ping/Makefile | 11 ++++++++++
> lib/lwip/apps/ping/lwip_ping.c | 37 ++++++++++++++++++++++++++++++++++
> lib/lwip/apps/ping/lwip_ping.h | 24 ++++++++++++++++++++++
> lib/lwip/apps/ping/ping.h | 35 ++++++++++++++++++++++++++++++++
> 5 files changed, 108 insertions(+)
> create mode 100644 lib/lwip/apps/ping/Makefile
> create mode 100644 lib/lwip/apps/ping/lwip_ping.c
> create mode 100644 lib/lwip/apps/ping/lwip_ping.h
> create mode 100644 lib/lwip/apps/ping/ping.h
>
> diff --git a/lib/lwip/Makefile b/lib/lwip/Makefile
> index ec6b728c8e..87ed99a230 100644
> --- a/lib/lwip/Makefile
> +++ b/lib/lwip/Makefile
> @@ -67,5 +67,6 @@ obj-$(CONFIG_NET) += port/sys-arch.o
>
> obj-$(CONFIG_CMD_DHCP) += apps/dhcp/lwip-dhcp.o
> obj-$(CONFIG_CMD_DNS) += apps/dns/lwip-dns.o
> +obj-$(CONFIG_CMD_PING) += apps/ping/
> obj-$(CONFIG_CMD_TFTPBOOT) += apps/tftp/
> obj-$(CONFIG_CMD_WGET) += apps/http/
> diff --git a/lib/lwip/apps/ping/Makefile b/lib/lwip/apps/ping/Makefile
> new file mode 100644
> index 0000000000..0d0a811336
> --- /dev/null
> +++ b/lib/lwip/apps/ping/Makefile
> @@ -0,0 +1,11 @@
> +ccflags-y += -I$(srctree)/lib/lwip/port/include
> +ccflags-y += -I$(srctree)/lib/lwip/lwip-external/src/include -I$(srctree)/lib/lwip
> +ccflags-y += -I$(obj)
> +
> +.PHONY: $(obj)/ping.c
> +$(obj)/ping.o: $(obj)/ping.c
> +$(obj)/ping.c:
> + cp $(srctree)/lib/lwip/lwip-external/contrib/apps/ping/ping.c $(obj)/ping.c
> +
> +obj-$(CONFIG_CMD_PING) += ping.o
> +obj-$(CONFIG_CMD_PING) += lwip_ping.o
> diff --git a/lib/lwip/apps/ping/lwip_ping.c b/lib/lwip/apps/ping/lwip_ping.c
> new file mode 100644
> index 0000000000..40658ab6fd
> --- /dev/null
> +++ b/lib/lwip/apps/ping/lwip_ping.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
> + */
> +
> +#include "lwip/opt.h"
> +#include "lwip/ip_addr.h"
> +#include "ping.h"
> +
> +#include <ulwip.h>
> +
> +static ip_addr_t ip_target;
> +
> +static int ulwip_ping_tmo(void)
> +{
> +
> + printf("ping failed; host %s is not alive\n", ipaddr_ntoa(&ip_target));
> + return 0;

Shouldnt this return != 0? Also why do we need this? Looking at
ping_raw_init() it ets timeout and print fail messages

> +}
> +
> +int lwip_ping_init(char *ping_addr)
> +{
> + int err;
> +
> + err = ipaddr_aton(ping_addr, &ip_target);
> + if (err == 0) {
> + printf("wrong ping addr string \"%s\" \n", ping_addr);
> + return -1;
> + }
> +
> + ulwip_set_tmo(ulwip_ping_tmo);
> +
> + ping_init(&ip_target);
> +
> + return 0;
> +}
> diff --git a/lib/lwip/apps/ping/lwip_ping.h b/lib/lwip/apps/ping/lwip_ping.h
> new file mode 100644
> index 0000000000..7f08095427
> --- /dev/null
> +++ b/lib/lwip/apps/ping/lwip_ping.h
> @@ -0,0 +1,24 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +
> +/*
> + * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
> + */
> +
> +#ifndef LWIP_PING_H
> +#define LWIP_PING_H
> +
> +#include <lwip/ip_addr.h>
> +
> +/**
> + * PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used
> + */

Is there any support for sockets in the current series? IOW if someome sets
this to 1 will it work? If not get rid of it completely

> +#ifndef PING_USE_SOCKETS
> +#define PING_USE_SOCKETS 0
> +#endif
> +
> +int lwip_ping_init(char *ping_addr);
> +
> +void ping_raw_init(void);
> +void ping_send_now(void);
> +
> +#endif /* LWIP_PING_H */
> diff --git a/lib/lwip/apps/ping/ping.h b/lib/lwip/apps/ping/ping.h
> new file mode 100644
> index 0000000000..0dd4bd78c7
> --- /dev/null
> +++ b/lib/lwip/apps/ping/ping.h
> @@ -0,0 +1,35 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +
> +#include "../../../lwip/ulwip.h"

Again, find a away to include this normally, without all the ../../
uglyness

> +
> +#include "lwip/prot/ip4.h"
> +
> +#define ip4_print_parts(a, b, c, d) \
> + printf("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d);
> +
> +#define ip4_print(ipaddr) \
> + ip4_print_parts(\
> + (u16_t)((ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0), \
> + (u16_t)((ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0), \
> + (u16_t)((ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0), \
> + (u16_t)((ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0))
> +
> +
> +#define LWIP_DEBUG 1 /* ping_time is under ifdef*/
> +#define PING_RESULT(cond) { \
> + if (cond == 1) { \
> + printf("host "); \
> + ip4_print(addr); \
> + printf(" is alive\n"); \
> + printf(" %"U32_F" ms\n", (sys_now() - ping_time)); \
> + ulwip_exit(0); \
> + } else { \
> + printf("ping failed; host "); \
> + ip4_print(addr); \
> + printf(" is not alive\n"); \
> + ulwip_exit(-1); \
> + } \
> + } while (0);
> +
> +#include "lwip/ip_addr.h"
> +void ping_init(const ip_addr_t *ping_addr);
> --
> 2.30.2
>

Thanks
/Ilias