diff --git a/samples/net/zperf/CMakeLists.txt b/samples/net/zperf/CMakeLists.txt index fc05a8322c79..5de5694c61cd 100644 --- a/samples/net/zperf/CMakeLists.txt +++ b/samples/net/zperf/CMakeLists.txt @@ -1,48 +1,30 @@ cmake_minimum_required(VERSION 3.8.2) + macro(set_conf_file) - if(PROFILER) - set(CONF_FILE prj_${BOARD}_prof.conf) + if(EXISTS ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf) + set(CONF_FILE "${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf") + elseif(EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf) + set(CONF_FILE + "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf") else() - set(CONF_FILE prj_${BOARD}.conf) + set(CONF_FILE "prj.conf") endif() endmacro() include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) project(NONE) -target_sources( - app PRIVATE +target_sources(app PRIVATE src/shell_utils.c src/zperf_session.c src/zperf_shell.c - ) -target_sources_ifdef( - CONFIG_NET_UDP - app PRIVATE src/zperf_udp_receiver.c src/zperf_udp_uploader.c - ) -target_sources_ifdef( - CONFIG_NET_TCP - app PRIVATE src/zperf_tcp_receiver.c src/zperf_tcp_uploader.c ) -target_compile_definitions_ifdef( - PROFILER - app PRIVATE - PROFILER - ) - target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/subsys/net/ip $ENV{ZEPHYR_BASE}/samples/task_profiler/profiler/src ) - -if(PROFILER) - assert(0 "PROFILER is not supported yet") - # KBuild did this, but this did not work, not sure why. - # export PROFILER_NO_SHELL_REGISTER=1 - # obj-y += ../../../task_profiler/profiler/ -endif() diff --git a/samples/net/zperf/README.rst b/samples/net/zperf/README.rst index 7e4d0ee3e4d1..6deec51ac16d 100644 --- a/samples/net/zperf/README.rst +++ b/samples/net/zperf/README.rst @@ -14,7 +14,6 @@ Features - Compatible with iPerf_2.0.5. - Client or server mode allowed without need to modify the source code. -- Working with task profiler (PROFILER=1 to be set when building zperf) Supported Boards **************** @@ -58,14 +57,14 @@ In the Zephyr console, zperf can be executed as follows: .. code-block:: console - zperf> udp.upload 2001:db8::2 5001 10 1K 1M + zperf> udp upload 2001:db8::2 5001 10 1K 1M For TCP the zperf command would look like this: .. code-block:: console - zperf> tcp.upload 2001:db8::2 5001 10 1K 1M + zperf> tcp upload 2001:db8::2 5001 10 1K 1M If the IP addresses of Zephyr and the host machine are specified in the @@ -73,28 +72,28 @@ config file, zperf can be started as follows: .. code-block:: console - zperf> udp.upload2 v6 10 1K 1M + zperf> udp upload2 v6 10 1K 1M or like this if you want to test TCP: .. code-block:: console - zperf> tcp.upload2 v6 10 1K 1M + zperf> tcp upload2 v6 10 1K 1M If Zephyr is acting as a server, set the download mode as follows for UDP: .. code-block:: console - zperf> udp.download 5001 + zperf> udp download 5001 or like this for TCP: .. code-block:: console - zperf> tcp.download 5001 + zperf> tcp download 5001 and in the host side, iPerf must be executed with the following diff --git a/samples/net/zperf/boards/quark_se_c1000_devboard.conf b/samples/net/zperf/boards/quark_se_c1000_devboard.conf new file mode 100644 index 000000000000..8b98dd3da22a --- /dev/null +++ b/samples/net/zperf/boards/quark_se_c1000_devboard.conf @@ -0,0 +1,9 @@ +CONFIG_NET_L2_IEEE802154=y +CONFIG_IEEE802154_CC2520=y +CONFIG_NET_6LO_CONTEXT=y + +CONFIG_NET_PKT_RX_COUNT=24 +CONFIG_NET_PKT_TX_COUNT=24 +CONFIG_NET_BUF_RX_COUNT=48 +CONFIG_NET_BUF_TX_COUNT=48 +CONFIG_NET_BUF_DATA_SIZE=128 diff --git a/samples/net/zperf/overlay-bt.conf b/samples/net/zperf/overlay-bt.conf new file mode 100644 index 000000000000..b11d715e18a7 --- /dev/null +++ b/samples/net/zperf/overlay-bt.conf @@ -0,0 +1,2 @@ +CONFIG_NET_L2_BT=y +CONFIG_NET_L2_BT_SHELL=y diff --git a/samples/net/zperf/overlay-netusb.conf b/samples/net/zperf/overlay-netusb.conf new file mode 100644 index 000000000000..e893d64f0b06 --- /dev/null +++ b/samples/net/zperf/overlay-netusb.conf @@ -0,0 +1,6 @@ +# USB Device Settings +CONFIG_USB=y +CONFIG_USB_DEVICE_STACK=y + +# Select USB Configurations +CONFIG_USB_DEVICE_NETWORK_ECM=y diff --git a/samples/net/zperf/prj_qemu_x86.conf b/samples/net/zperf/prj.conf similarity index 82% rename from samples/net/zperf/prj_qemu_x86.conf rename to samples/net/zperf/prj.conf index 2d2afa1e6f4e..47e98cb06c58 100644 --- a/samples/net/zperf/prj_qemu_x86.conf +++ b/samples/net/zperf/prj.conf @@ -4,15 +4,14 @@ CONFIG_NET_IPV6=y CONFIG_NET_IPV4=y CONFIG_NET_DHCPV4=n CONFIG_NET_UDP=y -CONFIG_NET_TCP=n +CONFIG_NET_TCP=y CONFIG_NET_STATISTICS=y CONFIG_NET_PKT_RX_COUNT=14 CONFIG_NET_PKT_TX_COUNT=14 CONFIG_NET_BUF_RX_COUNT=28 CONFIG_NET_BUF_TX_COUNT=28 -CONFIG_NET_BUF_DATA_SIZE=512 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=5 +CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=4 CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=1 CONFIG_NET_MAX_CONTEXTS=3 @@ -24,12 +23,10 @@ CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_L2_ETHERNET=y CONFIG_NET_SHELL=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_CONSOLE_SHELL=y -CONFIG_PRINTK=y - CONFIG_NET_CONFIG_SETTINGS=y CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" + +CONFIG_LOG=y diff --git a/samples/net/zperf/prj_bt.conf b/samples/net/zperf/prj_bt.conf deleted file mode 100644 index 0304b55be5e2..000000000000 --- a/samples/net/zperf/prj_bt.conf +++ /dev/null @@ -1,32 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_LOG=y -CONFIG_NET_IPV6=y -CONFIG_NET_UDP=y -CONFIG_NET_TCP=n -CONFIG_NET_STATISTICS=y - -CONFIG_NET_PKT_RX_COUNT=14 -CONFIG_NET_PKT_TX_COUNT=14 -CONFIG_NET_BUF_RX_COUNT=28 -CONFIG_NET_BUF_TX_COUNT=28 -CONFIG_NET_BUF_DATA_SIZE=512 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=5 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 -CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=1 -CONFIG_NET_MAX_CONTEXTS=3 -CONFIG_NET_CONTEXT_SYNC_RECV=y - -CONFIG_INIT_STACKS=y -CONFIG_TEST_RANDOM_GENERATOR=y - -CONFIG_NET_L2_BT=y -CONFIG_NET_SHELL=y -CONFIG_NET_L2_BT_SHELL=y - -CONFIG_CONSOLE_HANDLER=y -CONFIG_CONSOLE_SHELL=y -CONFIG_PRINTK=y - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" diff --git a/samples/net/zperf/prj_frdm_k64f.conf b/samples/net/zperf/prj_frdm_k64f.conf deleted file mode 100644 index 5af2d9feeeb6..000000000000 --- a/samples/net/zperf/prj_frdm_k64f.conf +++ /dev/null @@ -1,36 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_LOG=y -CONFIG_NET_IPV6=y -CONFIG_NET_IPV4=y -CONFIG_NET_DHCPV4=n -CONFIG_NET_UDP=y -CONFIG_NET_TCP=y -CONFIG_NET_STATISTICS=y - -CONFIG_NET_PKT_RX_COUNT=100 -CONFIG_NET_PKT_TX_COUNT=100 -CONFIG_NET_BUF_RX_COUNT=100 -CONFIG_NET_BUF_TX_COUNT=100 -CONFIG_NET_BUF_DATA_SIZE=128 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 -CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=2 -CONFIG_NET_MAX_CONTEXTS=10 -CONFIG_NET_CONTEXT_SYNC_RECV=y - -CONFIG_INIT_STACKS=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_PRINTK=y - -CONFIG_NET_L2_ETHERNET=y - -CONFIG_NET_SHELL=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_CONSOLE_SHELL=y -CONFIG_PRINTK=y - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" -CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" -CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" diff --git a/samples/net/zperf/prj_netusb.conf b/samples/net/zperf/prj_netusb.conf deleted file mode 100644 index 5b83232fbedf..000000000000 --- a/samples/net/zperf/prj_netusb.conf +++ /dev/null @@ -1,39 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_LOG=y -CONFIG_NET_IPV6=y -CONFIG_NET_IPV4=n -CONFIG_NET_UDP=y -CONFIG_NET_TCP=y -CONFIG_NET_STATISTICS=y - -CONFIG_NET_PKT_RX_COUNT=50 -CONFIG_NET_PKT_TX_COUNT=50 -CONFIG_NET_BUF_RX_COUNT=50 -CONFIG_NET_BUF_TX_COUNT=50 - -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 -CONFIG_NET_MAX_CONTEXTS=8 -CONFIG_NET_CONTEXT_SYNC_RECV=y - -CONFIG_INIT_STACKS=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_PRINTK=y - -CONFIG_NET_6LO_CONTEXT=y - -CONFIG_NET_SHELL=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_CONSOLE_SHELL=y -CONFIG_PRINTK=y - -# USB Device Settings -CONFIG_USB=y -CONFIG_USB_DEVICE_STACK=y - -# Select USB Configurations -CONFIG_USB_DEVICE_NETWORK_ECM=y - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" diff --git a/samples/net/zperf/prj_quark_se_c1000_devboard.conf b/samples/net/zperf/prj_quark_se_c1000_devboard.conf deleted file mode 100644 index 37e450d5c3d5..000000000000 --- a/samples/net/zperf/prj_quark_se_c1000_devboard.conf +++ /dev/null @@ -1,35 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_LOG=y -CONFIG_NET_IPV6=y -CONFIG_NET_IPV4=n -CONFIG_NET_UDP=y -CONFIG_NET_TCP=n -CONFIG_NET_STATISTICS=y - -CONFIG_NET_PKT_RX_COUNT=96 -CONFIG_NET_PKT_TX_COUNT=96 -CONFIG_NET_BUF_RX_COUNT=48 -CONFIG_NET_BUF_TX_COUNT=48 -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 -CONFIG_NET_MAX_CONTEXTS=10 -CONFIG_NET_CONTEXT_SYNC_RECV=y - -CONFIG_INIT_STACKS=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_PRINTK=y - -CONFIG_NET_L2_IEEE802154=y - -CONFIG_IEEE802154_CC2520=y - -CONFIG_NET_6LO_CONTEXT=y - -CONFIG_NET_SHELL=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_CONSOLE_SHELL=y -CONFIG_PRINTK=y - -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" diff --git a/samples/net/zperf/sample.yaml b/samples/net/zperf/sample.yaml index 3c719a5664c3..d6a5ba23ec7c 100644 --- a/samples/net/zperf/sample.yaml +++ b/samples/net/zperf/sample.yaml @@ -1,36 +1,35 @@ +common: + harness: net + tags: net zperf samples + # TODO: Does not work properly with native_posix, need more TLC + platform_exclude: native_posix sample: - description: TBD - name: TBD + description: Network performance measurement tool for Zephyr + name: zperf tests: test: - harness: net platform_whitelist: qemu_x86 - tags: samples net test_netusb_ecm: - harness: net - extra_args: CONF_FILE="prj_netusb.conf" + extra_args: OVERLAY_CONFIG="overlay-netusb.conf" extra_configs: - - CONFIG_NET_PKT_RX_COUNT=10 - - CONFIG_NET_PKT_TX_COUNT=10 + - CONFIG_NET_TCP=n platform_whitelist: quark_se_c1000_devboard 96b_carbon - tags: samples usb net + tags: samples usb net zperf test_netusb_eem: - harness: net - extra_args: CONF_FILE="prj_netusb.conf" + extra_args: OVERLAY_CONFIG="overlay-netusb.conf" extra_configs: - CONFIG_USB_DEVICE_NETWORK_ECM=n - CONFIG_USB_DEVICE_NETWORK_EEM=y - - CONFIG_NET_PKT_RX_COUNT=10 - - CONFIG_NET_PKT_TX_COUNT=10 + - CONFIG_NET_TCP=n platform_whitelist: quark_se_c1000_devboard 96b_carbon - tags: samples usb net + tags: samples usb net zperf test_netusb_rndis: - harness: net - extra_args: CONF_FILE="prj_netusb.conf" + extra_args: OVERLAY_CONFIG="overlay-netusb.conf" extra_configs: - CONFIG_USB_DEVICE_NETWORK_ECM=n - CONFIG_USB_DEVICE_NETWORK_RNDIS=y - - CONFIG_NET_PKT_RX_COUNT=10 - - CONFIG_NET_PKT_TX_COUNT=10 + - CONFIG_NET_PKT_RX_COUNT=32 + - CONFIG_NET_PKT_TX_COUNT=32 + - CONFIG_NET_TCP=n platform_whitelist: quark_se_c1000_devboard 96b_carbon - tags: samples usb net + tags: samples usb net zperf diff --git a/samples/net/zperf/src/shell_utils.c b/samples/net/zperf/src/shell_utils.c index 57c633878dc9..1121155cb5cc 100644 --- a/samples/net/zperf/src/shell_utils.c +++ b/samples/net/zperf/src/shell_utils.c @@ -24,8 +24,8 @@ const char *KBPS_UNIT[] = { "Mbps", "Kbps" }; const u32_t K[] = { 1024 * 1024, 1024, 0 }; const char *K_UNIT[] = { "M", "K", "" }; -void print_number(u32_t value, const u32_t *divisor, - const char **units) +void print_number(const struct shell *shell, u32_t value, + const u32_t *divisor, const char **units) { const char **unit; const u32_t *div; @@ -42,9 +42,10 @@ void print_number(u32_t value, const u32_t *divisor, if (*div != 0) { radix = value / *div; dec = (value % *div) * 100 / *div; - printk("%u.%s%u %s", radix, (dec < 10) ? "0" : "", dec, *unit); + shell_fprintf(shell, SHELL_NORMAL, "%u.%s%u %s", radix, + (dec < 10) ? "0" : "", dec, *unit); } else { - printk("%u %s", value, *unit); + shell_fprintf(shell, SHELL_NORMAL, "%u %s", value, *unit); } } diff --git a/samples/net/zperf/src/shell_utils.h b/samples/net/zperf/src/shell_utils.h index ae283fd73c0d..467c0653e6d8 100644 --- a/samples/net/zperf/src/shell_utils.h +++ b/samples/net/zperf/src/shell_utils.h @@ -6,6 +6,8 @@ #ifndef __SHELL_UTILS_H #define __SHELL_UTILS_H +#include + #define IPV4_STR_LEN_MAX 15 #define IPV4_STR_LEN_MIN 7 @@ -16,8 +18,8 @@ extern const char *KBPS_UNIT[]; extern const u32_t K[]; extern const char *K_UNIT[]; -extern void print_number(u32_t value, const u32_t *divisor, - const char **units); +extern void print_number(const struct shell *shell, u32_t value, + const u32_t *divisor, const char **units); extern long parse_number(const char *string, const u32_t *divisor, - const char **units); + const char **units); #endif /* __SHELL_UTILS_H */ diff --git a/samples/net/zperf/src/zperf.h b/samples/net/zperf/src/zperf.h index dc7e203191b8..503f32222468 100644 --- a/samples/net/zperf/src/zperf.h +++ b/samples/net/zperf/src/zperf.h @@ -6,18 +6,7 @@ #ifndef __ZPERF_H #define __ZPERF_H -#define VERSION "1.0" - -/* commands strings */ -#define CMD_STR_SETIP "setip" -#define CMD_STR_CONNECTAP "connectap" -#define CMD_STR_VERSION "version" -#define CMD_STR_UDP_UPLOAD "udp.upload" -#define CMD_STR_UDP_UPLOAD2 "udp.upload2" -#define CMD_STR_UDP_DOWNLOAD "udp.download" -#define CMD_STR_TCP_UPLOAD "tcp.upload" -#define CMD_STR_TCP_UPLOAD2 "tcp.upload2" -#define CMD_STR_TCP_DOWNLOAD "tcp.download" +#define VERSION "1.1" struct zperf_results { u32_t nb_packets_sent; diff --git a/samples/net/zperf/src/zperf_internal.h b/samples/net/zperf/src/zperf_internal.h index 93d34e842849..160929f31c62 100644 --- a/samples/net/zperf/src/zperf_internal.h +++ b/samples/net/zperf/src/zperf_internal.h @@ -8,6 +8,7 @@ #include #include +#include #define IP6PREFIX_STR2(s) #s #define IP6PREFIX_STR(p) IP6PREFIX_STR2(p) @@ -19,11 +20,19 @@ #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_CONFIG_SETTINGS) #define MY_IP6ADDR CONFIG_NET_CONFIG_MY_IPV6_ADDR #define DST_IP6ADDR CONFIG_NET_CONFIG_PEER_IPV6_ADDR +#define MY_IP6ADDR_SET +#else +#define MY_IP6ADDR NULL +#define DST_IP6ADDR NULL #endif #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_CONFIG_SETTINGS) #define MY_IP4ADDR CONFIG_NET_CONFIG_MY_IPV4_ADDR #define DST_IP4ADDR CONFIG_NET_CONFIG_PEER_IPV4_ADDR +#define MY_IP4ADDR_SET +#else +#define MY_IP4ADDR NULL +#define DST_IP4ADDR NULL #endif #define PACKET_SIZE_MAX 1024 @@ -78,33 +87,30 @@ static inline u32_t time_delta(u32_t ts, u32_t t) return (t >= ts) ? (t - ts) : (ULONG_MAX - ts + t); } -#if defined(CONFIG_NET_IPV6) -int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr, - const char *str); +int zperf_get_ipv6_addr(const struct shell *shell, char *host, + char *prefix_str, struct in6_addr *addr); struct sockaddr_in6 *zperf_get_sin6(void); -#endif -#if defined(CONFIG_NET_IPV4) -int zperf_get_ipv4_addr(char *host, struct in_addr *addr, const char *str); +int zperf_get_ipv4_addr(const struct shell *shell, char *host, + struct in_addr *addr); struct sockaddr_in *zperf_get_sin(void); -#endif -extern void zperf_udp_upload(struct net_context *net_context, +extern void zperf_udp_upload(const struct shell *shell, + struct net_context *context, unsigned int duration_in_ms, unsigned int packet_size, unsigned int rate_in_kbps, struct zperf_results *results); -extern void zperf_receiver_init(int port); +extern void zperf_receiver_init(const struct shell *shell, int port); -#if defined(CONFIG_NET_TCP) -extern void zperf_tcp_receiver_init(int port); +extern void zperf_tcp_receiver_init(const struct shell *shell, int port); extern void zperf_tcp_uploader_init(struct k_fifo *tx_queue); -extern void zperf_tcp_upload(struct net_context *net_context, +extern void zperf_tcp_upload(const struct shell *shell, + struct net_context *net_context, unsigned int duration_in_ms, unsigned int packet_size, struct zperf_results *results); -#endif extern void connect_ap(char *ssid); diff --git a/samples/net/zperf/src/zperf_shell.c b/samples/net/zperf/src/zperf_shell.c index 148eb6fdcc31..7adec898dc08 100644 --- a/samples/net/zperf/src/zperf_shell.c +++ b/samples/net/zperf/src/zperf_shell.c @@ -13,7 +13,8 @@ #include #include -#include +#include +#include #include #include @@ -49,17 +50,12 @@ static const char *CONFIG = #endif ""; -#if defined(PROFILER) -#include "profiler.h" -#endif - #define MY_SRC_PORT 50000 #define DEF_PORT 5001 -#define WAIT_CONNECT (2 * 1000) /* in ms */ +#define DEF_PORT_STR STRINGIFY(DEF_PORT) +#define WAIT_CONNECT K_SECONDS(2) /* in ms */ -#if defined(CONFIG_NET_IPV6) static struct in6_addr ipv6; -#endif static struct sockaddr_in6 in6_addr_my = { .sin6_family = AF_INET6, @@ -76,9 +72,7 @@ struct sockaddr_in6 *zperf_get_sin6(void) return &in6_addr_my; } -#if defined(CONFIG_NET_IPV4) static struct in_addr ipv4; -#endif static struct sockaddr_in in4_addr_my = { .sin_family = AF_INET, @@ -95,10 +89,20 @@ struct sockaddr_in *zperf_get_sin(void) return &in4_addr_my; } -#if defined(CONFIG_NET_IPV6) -static int parse_ipv6_addr(char *host, char *port, - struct sockaddr_in6 *addr, - const char *str) +static void zperf_init(const struct shell *shell); + +static void do_init(const struct shell *shell) +{ + static bool init_ok; + + if (!init_ok) { + zperf_init(shell); + init_ok = true; + } +} + +static int parse_ipv6_addr(const struct shell *shell, char *host, char *port, + struct sockaddr_in6 *addr) { int ret; @@ -108,21 +112,23 @@ static int parse_ipv6_addr(char *host, char *port, ret = net_addr_pton(AF_INET6, host, &addr->sin6_addr); if (ret < 0) { - printk("[%s] Error! Invalid IPv6 address %s\n", str, host); + shell_fprintf(shell, SHELL_WARNING, + "Invalid IPv6 address %s\n", host); return -EINVAL; } addr->sin6_port = htons(strtoul(port, NULL, 10)); if (!addr->sin6_port) { - printk("[%s] Error! Invalid port %s\n", str, port); + shell_fprintf(shell, SHELL_WARNING, + "Invalid port %s\n", port); return -EINVAL; } return 0; } -int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr, - const char *str) +int zperf_get_ipv6_addr(const struct shell *shell, char *host, + char *prefix_str, struct in6_addr *addr) { struct net_if_ipv6_prefix *prefix; struct net_if_addr *ifaddr; @@ -143,7 +149,8 @@ int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr, ifaddr = net_if_ipv6_addr_add(net_if_get_default(), addr, NET_ADDR_MANUAL, 0); if (!ifaddr) { - printk("[%s] Error! Cannot set IPv6 address\n", str); + shell_fprintf(shell, SHELL_WARNING, + "Cannot set IPv6 address %s\n", host); return -EINVAL; } @@ -151,18 +158,16 @@ int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr, addr, prefix_len, NET_IPV6_ND_INFINITE_LIFETIME); if (!prefix) { - printk("[%s] Error! Cannot set IPv6 prefix\n", str); + shell_fprintf(shell, SHELL_WARNING, + "Cannot set IPv6 prefix %s\n", prefix_str); return -EINVAL; } return 0; } -#endif -#if defined(CONFIG_NET_IPV4) -static int parse_ipv4_addr(char *host, char *port, - struct sockaddr_in *addr, - const char *str) +static int parse_ipv4_addr(const struct shell *shell, char *host, char *port, + struct sockaddr_in *addr) { int ret; @@ -172,20 +177,23 @@ static int parse_ipv4_addr(char *host, char *port, ret = net_addr_pton(AF_INET, host, &addr->sin_addr); if (ret < 0) { - printk("[%s] Error! Invalid IPv4 address %s\n", str, host); + shell_fprintf(shell, SHELL_WARNING, + "Invalid IPv4 address %s\n", host); return -EINVAL; } addr->sin_port = htons(strtoul(port, NULL, 10)); if (!addr->sin_port) { - printk("[%s] Error! Invalid port %s\n", str, port); + shell_fprintf(shell, SHELL_WARNING, + "Invalid port %s\n", port); return -EINVAL; } return 0; } -int zperf_get_ipv4_addr(char *host, struct in_addr *addr, const char *str) +int zperf_get_ipv4_addr(const struct shell *shell, char *host, + struct in_addr *addr) { struct net_if_addr *ifaddr; int ret; @@ -202,320 +210,235 @@ int zperf_get_ipv4_addr(char *host, struct in_addr *addr, const char *str) ifaddr = net_if_ipv4_addr_add(net_if_get_default(), addr, NET_ADDR_MANUAL, 0); if (!ifaddr) { - printk("[%s] Error! Cannot set IPv4 address\n", str); + shell_fprintf(shell, SHELL_WARNING, + "Cannot set IPv4 address %s\n", host); return -EINVAL; } return 0; } -#endif -static int shell_cmd_setip(int argc, char *argv[]) +static int cmd_setip(const struct shell *shell, size_t argc, char *argv[]) { int start = 0; - if (!strcmp(argv[0], "zperf")) { - start++; - argc--; - } + do_init(shell); -#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4) - if (argc != 3) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_SETIP); - printk("Usage:\t%s \n", CMD_STR_SETIP); - printk("\nExample %s 2001:db8::2 64\n", CMD_STR_SETIP); - return -1; - } + if (IS_ENABLED(CONFIG_NET_IPV6) && !IS_ENABLED(CONFIG_NET_IPV4)) { + if (argc != 3 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } - if (zperf_get_ipv6_addr(argv[start + 1], argv[start + 2], &ipv6, - CMD_STR_SETIP) < 0) { - printk("[%s] ERROR! Unable to set IP\n", CMD_STR_SETIP); - return 0; + if (zperf_get_ipv6_addr(shell, argv[start + 1], argv[start + 2], + &ipv6) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + return 0; + } + + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv6_addr(&ipv6)); } - printk("[%s] Setting IP address %s\n", CMD_STR_SETIP, - net_sprint_ipv6_addr(&ipv6)); -#endif + if (IS_ENABLED(CONFIG_NET_IPV4) && !IS_ENABLED(CONFIG_NET_IPV6)) { + if (argc != 2 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } -#if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6) - if (argc != 2) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_SETIP); - printk("Usage:\t%s \n", CMD_STR_SETIP); - printk("\nExample %s 10.237.164.178\n", CMD_STR_SETIP); - return -1; - } + if (zperf_get_ipv4_addr(shell, argv[start + 1], &ipv4) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + return -ENOEXEC; + } - if (zperf_get_ipv4_addr(argv[start + 1], &ipv4, CMD_STR_SETIP) < 0) { - printk("[%s] ERROR! Unable to set IP\n", CMD_STR_SETIP); - return 0; + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv4_addr(&ipv4)); } - printk("[%s] Setting IP address %s\n", CMD_STR_SETIP, - net_sprint_ipv4_addr(&ipv4)); -#endif + if (IS_ENABLED(CONFIG_NET_IPV6) && IS_ENABLED(CONFIG_NET_IPV4)) { + if (net_addr_pton(AF_INET6, argv[start + 1], &ipv6) < 0) { + if (argc != 2 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } -#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4) - if (net_addr_pton(AF_INET6, argv[start + 1], - &ipv6) < 0) { - if (argc != 2) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_SETIP); - printk("Usage:\t%s \n", CMD_STR_SETIP); - printk("\nExample %s 10.237.164.178\n", CMD_STR_SETIP); - printk("Example %s 2001:db8::1 64\n", CMD_STR_SETIP); - return -1; - } + if (zperf_get_ipv4_addr(shell, argv[start + 1], + &ipv4) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + return -ENOEXEC; + } - if (zperf_get_ipv4_addr(argv[start + 1], &ipv4, - CMD_STR_SETIP) < 0) { - printk("[%s] ERROR! Unable to set IP\n", CMD_STR_SETIP); - return 0; - } + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv4_addr(&ipv4)); + } else { + if (argc != 3 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } - printk("[%s] Setting IP address %s\n", CMD_STR_SETIP, - net_sprint_ipv4_addr(&ipv4)); - } else { - if (argc != 3) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_SETIP); - printk("Usage:\t%s \n", - CMD_STR_SETIP); - printk("\nExample %s 2001:db8::2 64\n", CMD_STR_SETIP); - printk("Example %s 10.237.164.178\n", CMD_STR_SETIP); - return -1; - } + if (zperf_get_ipv6_addr(shell, argv[start + 1], + argv[start + 2], &ipv6) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + return -ENOEXEC; + } - if (zperf_get_ipv6_addr(argv[start + 1], argv[start + 2], &ipv6, - CMD_STR_SETIP) < 0) { - printk("[%s] ERROR! Unable to set IP\n", CMD_STR_SETIP); - return 0; + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv6_addr(&ipv6)); } - - printk("[%s] Setting IP address %s\n", CMD_STR_SETIP, - net_sprint_ipv6_addr(&ipv6)); } -#endif return 0; } -#if defined(CONFIG_NET_UDP) -static int shell_cmd_udp_download(int argc, char *argv[]) +static int cmd_udp_download(const struct shell *shell, size_t argc, + char *argv[]) { - static bool udp_stopped = true; - int port, start = 0; + if (IS_ENABLED(CONFIG_NET_UDP)) { + static bool udp_stopped = true; + int port, start = 0; - if (!strcmp(argv[0], "zperf")) { - start++; - argc--; - } - - if (argc == 1) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_UDP_DOWNLOAD); - printk("Usage:\t%s \n", CMD_STR_UDP_DOWNLOAD); - printk("\nExample %s 5001\n", CMD_STR_UDP_DOWNLOAD); - return -1; - } + do_init(shell); - if (argc > 1) { - port = strtoul(argv[start + 1], NULL, 10); - } else { - port = DEF_PORT; - } - - if (!udp_stopped) { - printk("[%s] ERROR! UDP server already started!\n", - CMD_STR_UDP_DOWNLOAD); - return -1; - } + if (argc < 2 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } - zperf_receiver_init(port); + if (argc > 1) { + port = strtoul(argv[start + 1], NULL, 10); + } else { + port = DEF_PORT; + } - k_yield(); + if (!udp_stopped) { + shell_fprintf(shell, SHELL_WARNING, + "UDP server already started!\n"); + return -ENOEXEC; + } - udp_stopped = false; + zperf_receiver_init(shell, port); - printk("[%s] UDP server started on port %u\n", CMD_STR_UDP_DOWNLOAD, - port); + k_yield(); - return 0; -} -#endif + udp_stopped = false; -#if defined(CONFIG_NET_UDP) -static void shell_udp_upload_usage(void) -{ - /* Print usage */ - printk("\n%s:\n", CMD_STR_UDP_UPLOAD); - printk("Usage:\t%s [K] [K|M]\n", CMD_STR_UDP_UPLOAD); - printk("\t:\tIP destination\n"); - printk("\t:\tUDP destination port\n"); - printk("\t:\tDuration of the test in seconds\n"); - printk("\t:\tSize of the packet in byte or kilobyte " - "(with suffix K)\n"); - printk("\t:\tBaudrate in kilobyte or megabyte\n"); - printk("\nExample %s 10.237.164.178 1111 1 1K 1M\n", - CMD_STR_UDP_UPLOAD); -} + shell_fprintf(shell, SHELL_NORMAL, + "UDP server started on port %u\n", port); -static void shell_udp_upload2_usage(void) -{ - /* Print usage */ - printk("\n%s:\n", CMD_STR_UDP_UPLOAD2); - printk("Usage:\t%s v6|v4 [K] [K|M]\n", CMD_STR_UDP_UPLOAD2); - printk("\t:\tUse either IPv6 or IPv4\n"); - printk("\t:\tDuration of the test in seconds\n"); - printk("\t:\tSize of the packet in byte or kilobyte " - "(with suffix K)\n"); - printk("\t:\tBaudrate in kilobyte or megabyte\n"); - printk("\nExample %s v6 1 1K 1M\n", - CMD_STR_UDP_UPLOAD2); -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) - printk("\nDefault IPv6 address is %s, destination [%s]:%d\n", - MY_IP6ADDR, DST_IP6ADDR, DEF_PORT); -#endif -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) - printk("\nDefault IPv4 address is %s, destination %s:%d\n", - MY_IP4ADDR, DST_IP4ADDR, DEF_PORT); -#endif -} -#endif - -#if defined(CONFIG_NET_TCP) -static void shell_tcp_upload_usage(void) -{ - /* Print usage */ - printk("\n%s:\n", CMD_STR_TCP_UPLOAD); - printk("Usage:\t%s [K]\n", CMD_STR_TCP_UPLOAD); - printk("\t:\tIP destination\n"); - printk("\t:\tport destination\n"); - printk("\t:\t of the test in seconds\n"); - printk("\t:\tSize of the packet in byte or kilobyte " - "(with suffix K)\n"); - printk("\nExample %s 10.237.164.178 1111 1 1K 1M\n", - CMD_STR_TCP_UPLOAD); + return 0; + } else { + return -ENOTSUP; + } } -static void shell_tcp_upload2_usage(void) +static void shell_udp_upload_print_stats(const struct shell *shell, + struct zperf_results *results) { - /* Print usage */ - printk("\n%s:\n", CMD_STR_TCP_UPLOAD2); - printk("Usage:\t%s v6|v4 [K] [K|M]\n", CMD_STR_TCP_UPLOAD2); - printk("\t:\tUse either IPv6 or IPv4\n"); - printk("\t:\tDuration of the test in seconds\n"); - printk("\t:\tSize of the packet in byte or kilobyte " - "(with suffix K)\n"); - printk("\t:\tBaudrate in kilobyte or megabyte\n"); - printk("\nExample %s v6 1 1K 1M\n", - CMD_STR_TCP_UPLOAD2); -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) - printk("\nDefault IPv6 address is %s, destination [%s]:%d\n", - MY_IP6ADDR, DST_IP6ADDR, DEF_PORT); -#endif -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) - printk("\nDefault IPv4 address is %s, destination %s:%d\n", - MY_IP4ADDR, DST_IP4ADDR, DEF_PORT); -#endif -} -#endif + if (IS_ENABLED(CONFIG_NET_UDP)) { + unsigned int rate_in_kbps, client_rate_in_kbps; -#if defined(CONFIG_NET_UDP) -static void shell_udp_upload_print_stats(struct zperf_results *results) -{ - unsigned int rate_in_kbps, client_rate_in_kbps; + shell_fprintf(shell, SHELL_NORMAL, "-\nUpload completed!\n"); - printk("[%s] Upload completed!\n", CMD_STR_UDP_UPLOAD); + if (results->time_in_us != 0) { + rate_in_kbps = (u32_t) + (((u64_t)results->nb_bytes_sent * + (u64_t)8 * (u64_t)USEC_PER_SEC) / + ((u64_t)results->time_in_us * 1024)); + } else { + rate_in_kbps = 0; + } - if (results->time_in_us != 0) { - rate_in_kbps = (u32_t) - (((u64_t)results->nb_bytes_sent * - (u64_t)8 * (u64_t)USEC_PER_SEC) / - ((u64_t)results->time_in_us * 1024)); - } else { - rate_in_kbps = 0; - } + if (results->client_time_in_us != 0) { + client_rate_in_kbps = (u32_t) + (((u64_t)results->nb_packets_sent * + (u64_t)results->packet_size * (u64_t)8 * + (u64_t)USEC_PER_SEC) / + ((u64_t)results->client_time_in_us * 1024)); + } else { + client_rate_in_kbps = 0; + } - if (results->client_time_in_us != 0) { - client_rate_in_kbps = (u32_t) - (((u64_t)results->nb_packets_sent * - (u64_t)results->packet_size * (u64_t)8 * - (u64_t)USEC_PER_SEC) / - ((u64_t)results->client_time_in_us * 1024)); - } else { - client_rate_in_kbps = 0; - } + if (!rate_in_kbps) { + shell_fprintf(shell, SHELL_ERROR, + "LAST PACKET NOT RECEIVED!!!\n"); + } - if (!rate_in_kbps) { - printk("[%s] LAST PACKET NOT RECEIVED!!!\n", - CMD_STR_UDP_UPLOAD); + shell_fprintf(shell, SHELL_NORMAL, + "Statistics:\t\tserver\t(client)\n"); + shell_fprintf(shell, SHELL_NORMAL, "Duration:\t\t"); + print_number(shell, results->time_in_us, TIME_US, + TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\t("); + print_number(shell, results->client_time_in_us, TIME_US, + TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, ")\n"); + + shell_fprintf(shell, SHELL_NORMAL, "Num packets:\t\t%u\t(%u)\n", + results->nb_packets_rcvd, + results->nb_packets_sent); + + shell_fprintf(shell, SHELL_NORMAL, + "Num packets out order:\t%u\n", + results->nb_packets_outorder); + shell_fprintf(shell, SHELL_NORMAL, "Num packets lost:\t%u\n", + results->nb_packets_lost); + + shell_fprintf(shell, SHELL_NORMAL, "Jitter:\t\t\t"); + print_number(shell, results->jitter_in_us, TIME_US, + TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + + shell_fprintf(shell, SHELL_NORMAL, "Rate:\t\t\t"); + print_number(shell, rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\t("); + print_number(shell, client_rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, ")\n"); } - - printk("[%s] statistics:\t\tserver\t(client)\n", CMD_STR_UDP_UPLOAD); - printk("[%s] duration:\t\t\t", CMD_STR_UDP_UPLOAD); - print_number(results->time_in_us, TIME_US, TIME_US_UNIT); - printk("\t("); - print_number(results->client_time_in_us, TIME_US, TIME_US_UNIT); - printk(")\n"); - - printk("[%s] nb packets:\t\t%u\t(%u)\n", CMD_STR_UDP_UPLOAD, - results->nb_packets_rcvd, - results->nb_packets_sent); - - printk("[%s] nb packets outorder:\t%u\n", CMD_STR_UDP_UPLOAD, - results->nb_packets_outorder); - printk("[%s] nb packets lost:\t\t%u\n", CMD_STR_UDP_UPLOAD, - results->nb_packets_lost); - - printk("[%s] jitter:\t\t\t", CMD_STR_UDP_UPLOAD); - print_number(results->jitter_in_us, TIME_US, TIME_US_UNIT); - printk("\n"); - - printk("[%s] rate:\t\t\t", CMD_STR_UDP_UPLOAD); - print_number(rate_in_kbps, KBPS, KBPS_UNIT); - printk("\t("); - print_number(client_rate_in_kbps, KBPS, KBPS_UNIT); - printk(")\n"); } -#endif -#if defined(CONFIG_NET_TCP) -static void shell_tcp_upload_print_stats(struct zperf_results *results) +static void shell_tcp_upload_print_stats(const struct shell *shell, + struct zperf_results *results) { - unsigned int client_rate_in_kbps; + if (IS_ENABLED(CONFIG_NET_TCP)) { + unsigned int client_rate_in_kbps; - printk("[%s] Upload completed!\n", CMD_STR_TCP_UPLOAD); + shell_fprintf(shell, SHELL_NORMAL, "-\nUpload completed!\n"); - if (results->client_time_in_us != 0) { - client_rate_in_kbps = (u32_t) - (((u64_t)results->nb_packets_sent * - (u64_t)results->packet_size * (u64_t)8 * - (u64_t)USEC_PER_SEC) / - ((u64_t)results->client_time_in_us * 1024)); - } else { - client_rate_in_kbps = 0; - } + if (results->client_time_in_us != 0) { + client_rate_in_kbps = (u32_t) + (((u64_t)results->nb_packets_sent * + (u64_t)results->packet_size * (u64_t)8 * + (u64_t)USEC_PER_SEC) / + ((u64_t)results->client_time_in_us * 1024)); + } else { + client_rate_in_kbps = 0; + } - printk("[%s] duration:\t", CMD_STR_TCP_UPLOAD); - print_number(results->client_time_in_us, TIME_US, TIME_US_UNIT); - printk("\n"); - printk("[%s] nb packets:\t%u\n", CMD_STR_TCP_UPLOAD, - results->nb_packets_sent); - printk("[%s] nb sending errors (retry or fail):\t%u\n", - CMD_STR_TCP_UPLOAD, results->nb_packets_errors); - printk("[%s] rate:\t", CMD_STR_TCP_UPLOAD); - print_number(client_rate_in_kbps, KBPS, KBPS_UNIT); - printk("\n"); + shell_fprintf(shell, SHELL_NORMAL, "Duration:\t"); + print_number(shell, results->client_time_in_us, + TIME_US, TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + shell_fprintf(shell, SHELL_NORMAL, "Num packets:\t%u\n", + results->nb_packets_sent); + shell_fprintf(shell, SHELL_NORMAL, + "Num errors:\t%u (retry or fail)\n", + results->nb_packets_errors); + shell_fprintf(shell, SHELL_NORMAL, "Rate:\t\t"); + print_number(shell, client_rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + } } -#endif -static int setup_contexts(struct net_context **context6, +static int setup_contexts(const struct shell *shell, + struct net_context **context6, struct net_context **context4, sa_family_t family, struct sockaddr_in6 *ipv6, @@ -526,44 +449,47 @@ static int setup_contexts(struct net_context **context6, { int ret; -#if defined(CONFIG_NET_IPV6) - ret = net_context_get(AF_INET6, - is_udp ? SOCK_DGRAM : SOCK_STREAM, - is_udp ? IPPROTO_UDP : IPPROTO_TCP, - context6); - if (ret < 0) { - printk("[%s] Cannot get IPv6 network context (%d)\n", - argv0, ret); - return -1; + if (IS_ENABLED(CONFIG_NET_IPV6)) { + ret = net_context_get(AF_INET6, + is_udp ? SOCK_DGRAM : SOCK_STREAM, + is_udp ? IPPROTO_UDP : IPPROTO_TCP, + context6); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv6 network context (%d)\n", + ret); + return -ENOEXEC; + } + + ipv6->sin6_port = htons(port); + ipv6->sin6_family = AF_INET6; } - ipv6->sin6_port = htons(port); - ipv6->sin6_family = AF_INET6; -#endif + if (IS_ENABLED(CONFIG_NET_IPV4)) { + ret = net_context_get(AF_INET, + is_udp ? SOCK_DGRAM : SOCK_STREAM, + is_udp ? IPPROTO_UDP : IPPROTO_TCP, + context4); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv4 network context (%d)\n", + ret); + return -ENOEXEC; + } -#if defined(CONFIG_NET_IPV4) - ret = net_context_get(AF_INET, - is_udp ? SOCK_DGRAM : SOCK_STREAM, - is_udp ? IPPROTO_UDP : IPPROTO_TCP, - context4); - if (ret < 0) { - printk("[%s] Cannot get IPv4 network context (%d)\n", - argv0, ret); - return -1; + ipv4->sin_port = htons(port); + ipv4->sin_family = AF_INET; } - ipv4->sin_port = htons(port); - ipv4->sin_family = AF_INET; -#endif - if (family == AF_INET6 && *context6) { ret = net_context_bind(*context6, (struct sockaddr *)ipv6, sizeof(struct sockaddr_in6)); if (ret < 0) { - printk("[%s] Cannot bind IPv6 port %d (%d)", - argv0, ntohs(ipv6->sin6_port), ret); - return -1; + shell_fprintf(shell, SHELL_NORMAL, + "Cannot bind IPv6 port %d (%d)", + ntohs(ipv6->sin6_port), ret); + return -ENOEXEC; } } @@ -572,22 +498,24 @@ static int setup_contexts(struct net_context **context6, (struct sockaddr *)ipv4, sizeof(struct sockaddr_in)); if (ret < 0) { - printk("[%s] Cannot bind IPv4 port %d (%d)", - argv0, ntohs(ipv4->sin_port), ret); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Cannot bind IPv4 port %d (%d)", + ntohs(ipv4->sin_port), ret); + return -ENOEXEC; } } if (!(*context6) && !(*context4)) { - printk("[%s] ERROR! Fail to retrieve network context(s)\n", - argv0); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Fail to retrieve network context(s)\n"); + return -ENOEXEC; } return 0; } -static int execute_upload(struct net_context *context6, +static int execute_upload(const struct shell *shell, + struct net_context *context6, struct net_context *context4, sa_family_t family, struct sockaddr_in6 *ipv6, @@ -601,14 +529,17 @@ static int execute_upload(struct net_context *context6, struct zperf_results results = { }; int ret; - printk("[%s] duration:\t\t", argv0); - print_number(duration_in_ms * USEC_PER_MSEC, TIME_US, TIME_US_UNIT); - printk("\n"); - printk("[%s] packet size:\t%u bytes\n", argv0, packet_size); - printk("[%s] start...\n", argv0); - -#if defined(CONFIG_NET_IPV6) - if (family == AF_INET6 && context6) { + shell_fprintf(shell, SHELL_NORMAL, "Duration:\t"); + print_number(shell, duration_in_ms * USEC_PER_MSEC, TIME_US, + TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + shell_fprintf(shell, SHELL_NORMAL, "Packet size:\t%u bytes\n", + packet_size); + shell_fprintf(shell, SHELL_NORMAL, "Rate:\t\t%u kbps\n", + rate_in_kbps); + shell_fprintf(shell, SHELL_NORMAL, "Starting...\n"); + + if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6 && context6) { /* For IPv6, we should make sure that neighbor discovery * has been done for the peer. So send ping here, wait * some time and start the test after that. @@ -616,15 +547,13 @@ static int execute_upload(struct net_context *context6, net_icmpv6_send_echo_request(net_if_get_default(), &ipv6->sin6_addr, 0, 0); - k_sleep(1 * MSEC_PER_SEC); + k_sleep(K_SECONDS(1)); } -#endif - if (is_udp) { -#if defined(CONFIG_NET_UDP) - printk("[%s] rate:\t\t", argv0); - print_number(rate_in_kbps, KBPS, KBPS_UNIT); - printk("\n"); + if (is_udp && IS_ENABLED(CONFIG_NET_UDP)) { + shell_fprintf(shell, SHELL_NORMAL, "Rate:\t\t"); + print_number(shell, rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); if (family == AF_INET6 && context6) { ret = net_context_connect(context6, @@ -634,14 +563,15 @@ static int execute_upload(struct net_context *context6, K_NO_WAIT, NULL); if (ret < 0) { - printk("[%s] IPv6 connect failed (%d)\n", - argv0, ret); + shell_fprintf(shell, SHELL_WARNING, + "IPv6 connect failed (%d)\n", + ret); goto out; } - zperf_udp_upload(context6, duration_in_ms, packet_size, - rate_in_kbps, &results); - shell_udp_upload_print_stats(&results); + zperf_udp_upload(shell, context6, duration_in_ms, + packet_size, rate_in_kbps, &results); + shell_udp_upload_print_stats(shell, &results); } if (family == AF_INET && context4) { @@ -652,20 +582,24 @@ static int execute_upload(struct net_context *context6, K_NO_WAIT, NULL); if (ret < 0) { - printk("[%s] IPv4 connect failed (%d)\n", - argv0, ret); + shell_fprintf(shell, SHELL_NORMAL, + "IPv4 connect failed (%d)\n", + ret); goto out; } - zperf_udp_upload(context4, duration_in_ms, packet_size, - rate_in_kbps, &results); - shell_udp_upload_print_stats(&results); + zperf_udp_upload(shell, context4, duration_in_ms, + packet_size, rate_in_kbps, &results); + shell_udp_upload_print_stats(shell, &results); } -#else - printk("[%s] UDP not supported\n", argv0); -#endif } else { -#if defined(CONFIG_NET_TCP) + if (!IS_ENABLED(CONFIG_NET_UDP)) { + shell_fprintf(shell, SHELL_INFO, + "UDP not supported\n"); + } + } + + if (!is_udp && IS_ENABLED(CONFIG_NET_TCP)) { if (family == AF_INET6 && context6) { ret = net_context_connect(context6, (struct sockaddr *)ipv6, @@ -674,8 +608,9 @@ static int execute_upload(struct net_context *context6, WAIT_CONNECT, NULL); if (ret < 0) { - printk("[%s] IPv6 connect failed (%d)\n", - argv0, ret); + shell_fprintf(shell, SHELL_WARNING, + "IPv6 connect failed (%d)\n", + ret); goto out; } @@ -684,10 +619,10 @@ static int execute_upload(struct net_context *context6, */ net_context_put(context4); - zperf_tcp_upload(context6, duration_in_ms, + zperf_tcp_upload(shell, context6, duration_in_ms, packet_size, &results); - shell_tcp_upload_print_stats(&results); + shell_tcp_upload_print_stats(shell, &results); return 0; } @@ -700,23 +635,26 @@ static int execute_upload(struct net_context *context6, WAIT_CONNECT, NULL); if (ret < 0) { - printk("[%s] IPv4 connect failed (%d)\n", - argv0, ret); + shell_fprintf(shell, SHELL_WARNING, + "IPv4 connect failed (%d)\n", + ret); goto out; } net_context_put(context6); - zperf_tcp_upload(context4, duration_in_ms, + zperf_tcp_upload(shell, context4, duration_in_ms, packet_size, &results); - shell_tcp_upload_print_stats(&results); + shell_tcp_upload_print_stats(shell, &results); return 0; } -#else - printk("[%s] TCP not supported\n", argv0); -#endif + } else { + if (!IS_ENABLED(CONFIG_NET_TCP)) { + shell_fprintf(shell, SHELL_INFO, + "TCP not supported\n"); + } } out: @@ -726,105 +664,114 @@ static int execute_upload(struct net_context *context6, return 0; } -static int shell_cmd_upload(int argc, char *argv[]) +static int shell_cmd_upload(const struct shell *shell, size_t argc, + char *argv[], enum net_ip_protocol proto) { struct sockaddr_in6 ipv6 = { .sin6_family = AF_INET6 }; struct sockaddr_in ipv4 = { .sin_family = AF_INET }; struct net_context *context6 = NULL, *context4 = NULL; sa_family_t family = AF_UNSPEC; unsigned int duration_in_ms, packet_size, rate_in_kbps; + char *port_str; u16_t port; bool is_udp; int start = 0; - if (!strcmp(argv[0], "zperf")) { - start++; - argc--; - } + is_udp = proto == IPPROTO_UDP; - is_udp = !strcmp(argv[start], CMD_STR_UDP_UPLOAD) ? 1 : 0; + if (argc < 2) { + shell_fprintf(shell, SHELL_WARNING, + "Not enough parameters.\n"); - if (argc == 1) { if (is_udp) { -#if defined(CONFIG_NET_UDP) - shell_udp_upload_usage(); -#endif + if (IS_ENABLED(CONFIG_NET_UDP)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } } else { -#if defined(CONFIG_NET_TCP) - shell_tcp_upload_usage(); -#endif + if (IS_ENABLED(CONFIG_NET_TCP)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } } - return -1; + return -ENOEXEC; } -#if defined(CONFIG_NET_IPV6) && !defined(CONFIG_NET_IPV4) - if (parse_ipv6_addr(argv[start + 1], argv[start + 2], - &ipv6, argv[start]) < 0) { - printk("[%s] ERROR! Please specify the IP address of the " - "remote server\n", argv[start]); - return -1; + if (argc > 2) { + port = strtoul(argv[start + 2], NULL, 10); + shell_fprintf(shell, SHELL_NORMAL, + "Remote port is %u\n", port); + port_str = argv[start + 2]; + } else { + port = DEF_PORT; + port_str = DEF_PORT_STR; } - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv6_addr(&ipv6.sin6_addr)); + if (IS_ENABLED(CONFIG_NET_IPV6) && !IS_ENABLED(CONFIG_NET_IPV4)) { + if (parse_ipv6_addr(shell, argv[start + 1], port_str, + &ipv6) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Please specify the IP address of the " + "remote server.\n"); + return -ENOEXEC; + } - family = AF_INET6; -#endif + shell_fprintf(shell, SHELL_WARNING, "Connecting to %s\n", + net_sprint_ipv6_addr(&ipv6.sin6_addr)); -#if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_IPV6) - if (parse_ipv4_addr(argv[start + 1], argv[start + 2], - &ipv4, argv[start]) < 0) { - printk("[%s] ERROR! Please specify the IP address of the " - "remote server\n", argv[start]); - return -1; + family = AF_INET6; } - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv4_addr(&ipv4.sin_addr)); - - family = AF_INET; -#endif - -#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_IPV4) - if (parse_ipv6_addr(argv[start + 1], argv[start + 2], - &ipv6, argv[start]) < 0) { - if (parse_ipv4_addr(argv[start + 1], argv[start + 2], - &ipv4, argv[start]) < 0) { - printk("[%s] ERROR! Please specify the IP address " - "of the remote server\n", argv[start]); - return -1; + if (IS_ENABLED(CONFIG_NET_IPV4) && !IS_ENABLED(CONFIG_NET_IPV6)) { + if (parse_ipv4_addr(shell, argv[start + 1], port_str, + &ipv4) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Please specify the IP address of the " + "remote server.\n"); + return -ENOEXEC; } - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv4_addr(&ipv4.sin_addr)); + shell_fprintf(shell, SHELL_NORMAL, "Connecting to %s\n", + net_sprint_ipv4_addr(&ipv4.sin_addr)); family = AF_INET; - } else { - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv6_addr(&ipv6.sin6_addr)); - - family = AF_INET6; } -#endif - if (argc > 2) { - port = strtoul(argv[start + 2], NULL, 10); - printk("[%s] Remote port is %u\n", argv[start], port); - } else { - port = DEF_PORT; + if (IS_ENABLED(CONFIG_NET_IPV6) && IS_ENABLED(CONFIG_NET_IPV4)) { + if (parse_ipv6_addr(shell, argv[start + 1], port_str, + &ipv6) < 0) { + if (parse_ipv4_addr(shell, argv[start + 1], port_str, + &ipv4) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Please specify the IP address " + "of the remote server.\n"); + return -ENOEXEC; + } + + shell_fprintf(shell, SHELL_NORMAL, + "Connecting to %s\n", + net_sprint_ipv4_addr(&ipv4.sin_addr)); + + family = AF_INET; + } else { + shell_fprintf(shell, SHELL_NORMAL, + "Connecting to %s\n", + net_sprint_ipv6_addr(&ipv6.sin6_addr)); + + family = AF_INET6; + } } - if (setup_contexts(&context6, &context4, family, &in6_addr_my, + if (setup_contexts(shell, &context6, &context4, family, &in6_addr_my, &in4_addr_my, port, is_udp, argv[start]) < 0) { - return -1; + return -ENOEXEC; } if (argc > 3) { - duration_in_ms = strtoul(argv[start + 3], NULL, 10) * - MSEC_PER_SEC; + duration_in_ms = K_SECONDS(strtoul(argv[start + 3], NULL, 10)); } else { - duration_in_ms = 1000; + duration_in_ms = K_SECONDS(1); } if (argc > 4) { @@ -841,12 +788,27 @@ static int shell_cmd_upload(int argc, char *argv[]) rate_in_kbps = 10; } - return execute_upload(context6, context4, family, &ipv6, &ipv4, + return execute_upload(shell, context6, context4, family, &ipv6, &ipv4, is_udp, argv[start], duration_in_ms, packet_size, rate_in_kbps); } -static int shell_cmd_upload2(int argc, char *argv[]) +static int cmd_tcp_upload(const struct shell *shell, size_t argc, char *argv[]) +{ + do_init(shell); + + return shell_cmd_upload(shell, argc, argv, IPPROTO_TCP); +} + +static int cmd_udp_upload(const struct shell *shell, size_t argc, char *argv[]) +{ + do_init(shell); + + return shell_cmd_upload(shell, argc, argv, IPPROTO_UDP); +} + +static int shell_cmd_upload2(const struct shell *shell, size_t argc, + char *argv[], enum net_ip_protocol proto) { struct net_context *context6 = NULL, *context4 = NULL; u16_t port = DEF_PORT; @@ -855,88 +817,89 @@ static int shell_cmd_upload2(int argc, char *argv[]) u8_t is_udp; int start = 0; - if (!strcmp(argv[0], "zperf")) { - start++; - argc--; - } + is_udp = proto == IPPROTO_UDP; - is_udp = !strcmp(argv[start], CMD_STR_UDP_UPLOAD2) ? 1 : 0; + if (argc < 2) { + shell_fprintf(shell, SHELL_WARNING, + "Not enough parameters.\n"); - if (argc == 1) { if (is_udp) { -#if defined(CONFIG_NET_UDP) - shell_udp_upload2_usage(); -#endif + if (IS_ENABLED(CONFIG_NET_UDP)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } } else { -#if defined(CONFIG_NET_TCP) - shell_tcp_upload2_usage(); -#endif + if (IS_ENABLED(CONFIG_NET_TCP)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } } - return -1; + return -ENOEXEC; } family = !strcmp(argv[start + 1], "v4") ? AF_INET : AF_INET6; -#if defined(CONFIG_NET_IPV6) - in6_addr_my.sin6_port = htons(port); -#endif + if (IS_ENABLED(CONFIG_NET_IPV6)) { + in6_addr_my.sin6_port = htons(port); + } -#if defined(CONFIG_NET_IPV4) - in4_addr_my.sin_port = htons(port); -#endif + if (IS_ENABLED(CONFIG_NET_IPV4)) { + in4_addr_my.sin_port = htons(port); + } if (family == AF_INET6) { if (net_is_ipv6_addr_unspecified(&in6_addr_my.sin6_addr)) { - printk("[%s] Invalid local IPv6 address\n", - argv[start]); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Invalid local IPv6 address.\n"); + return -ENOEXEC; } if (net_is_ipv6_addr_unspecified(&in6_addr_dst.sin6_addr)) { - printk("[%s] Invalid destination IPv6 address\n", - argv[start]); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Invalid destination IPv6 address.\n"); + return -ENOEXEC; } - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv6_addr(&in6_addr_dst.sin6_addr)); + shell_fprintf(shell, SHELL_NORMAL, + "Connecting to %s\n", + net_sprint_ipv6_addr(&in6_addr_dst.sin6_addr)); } else { if (net_is_ipv4_addr_unspecified(&in4_addr_my.sin_addr)) { - printk("[%s] Invalid local IPv4 address\n", - argv[start]); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Invalid local IPv4 address.\n"); + return -ENOEXEC; } if (net_is_ipv4_addr_unspecified(&in4_addr_dst.sin_addr)) { - printk("[%s] Invalid destination IPv4 address\n", - argv[start]); - return -1; + shell_fprintf(shell, SHELL_WARNING, + "Invalid destination IPv4 address.\n"); + return -ENOEXEC; } - printk("[%s] Connecting to %s\n", argv[start], - net_sprint_ipv4_addr(&in4_addr_dst.sin_addr)); + shell_fprintf(shell, SHELL_NORMAL, + "Connecting to %s\n", + net_sprint_ipv4_addr(&in4_addr_dst.sin_addr)); } - if (setup_contexts(&context6, &context4, family, &in6_addr_my, + if (setup_contexts(shell, &context6, &context4, family, &in6_addr_my, &in4_addr_my, port, is_udp, argv[start]) < 0) { - return -1; + return -ENOEXEC; } - if (argc > 1) { - duration_in_ms = strtoul(argv[start + 2], NULL, 10) * - MSEC_PER_SEC; + if (argc > 2) { + duration_in_ms = K_SECONDS(strtoul(argv[start + 2], NULL, 10)); } else { - duration_in_ms = 1000; + duration_in_ms = K_SECONDS(1); } - if (argc > 2) { + if (argc > 3) { packet_size = parse_number(argv[start + 3], K, K_UNIT); } else { packet_size = 256; } - if (argc > 3) { + if (argc > 4) { rate_in_kbps = (parse_number(argv[start + 4], K, K_UNIT) + 1023) / 1024; @@ -944,154 +907,278 @@ static int shell_cmd_upload2(int argc, char *argv[]) rate_in_kbps = 10; } - return execute_upload(context6, context4, family, &in6_addr_dst, + return execute_upload(shell, context6, context4, family, &in6_addr_dst, &in4_addr_dst, is_udp, argv[start], duration_in_ms, packet_size, rate_in_kbps); } -static int shell_cmd_connectap(int argc, char *argv[]) +static int cmd_tcp_upload2(const struct shell *shell, size_t argc, + char *argv[]) { - printk("[%s] Zephyr has not been built with Wi-Fi support.\n", - CMD_STR_CONNECTAP); + do_init(shell); - return 0; + return shell_cmd_upload2(shell, argc, argv, IPPROTO_TCP); } -#if defined(CONFIG_NET_TCP) -static int shell_cmd_tcp_download(int argc, char *argv[]) +static int cmd_udp_upload2(const struct shell *shell, size_t argc, + char *argv[]) { - static bool tcp_stopped = true; - int port; - - if (argc == 1) { - /* Print usage */ - printk("\n[%s]:\n", CMD_STR_TCP_DOWNLOAD); - printk("Usage:\t%s \n", CMD_STR_TCP_DOWNLOAD); - printk("\nExample %s 5001\n", CMD_STR_TCP_DOWNLOAD); - return -1; - } + do_init(shell); - if (argc > 1) { - port = strtoul(argv[1], NULL, 10); - } else { - port = DEF_PORT; + return shell_cmd_upload2(shell, argc, argv, IPPROTO_UDP); +} + +static int cmd_tcp(const struct shell *shell, size_t argc, char *argv[]) +{ + if (IS_ENABLED(CONFIG_NET_TCP)) { + do_init(shell); + + shell_help_print(shell, NULL, 0); + return -ENOEXEC; } - if (!tcp_stopped) { - printk("[%s] ERROR! TCP server already started!\n", - CMD_STR_TCP_DOWNLOAD); - return -1; + shell_fprintf(shell, SHELL_INFO, "TCP support is not enabled. " + "Set CONFIG_NET_TCP=y in your config file.\n"); + + return -ENOTSUP; +} + +static int cmd_udp(const struct shell *shell, size_t argc, char *argv[]) +{ + if (IS_ENABLED(CONFIG_NET_UDP)) { + do_init(shell); + + shell_help_print(shell, NULL, 0); + return -ENOEXEC; } - zperf_tcp_receiver_init(port); + shell_fprintf(shell, SHELL_INFO, "UDP support is not enabled. " + "Set CONFIG_NET_UDP=y in your config file.\n"); - tcp_stopped = false; + return -ENOTSUP; +} - printk("[%s] TCP server started on port %u\n", CMD_STR_TCP_DOWNLOAD, - port); +static int cmd_connectap(const struct shell *shell, size_t argc, char *argv[]) +{ + shell_fprintf(shell, SHELL_INFO, + "Zephyr has not been built with Wi-Fi support.\n"); return 0; } -#endif -static int shell_cmd_version(int argc, char *argv[]) +static int cmd_tcp_download(const struct shell *shell, size_t argc, + char *argv[]) { - printk("\nzperf [%s]: %s config: %s\n", CMD_STR_VERSION, VERSION, - CONFIG); + if (IS_ENABLED(CONFIG_NET_TCP)) { + static bool tcp_stopped = true; + int port; + + do_init(shell); + + if (argc < 2 || shell_help_requested(shell)) { + shell_help_print(shell, NULL, 0); + return -ENOEXEC; + } + + if (argc > 1) { + port = strtoul(argv[1], NULL, 10); + } else { + port = DEF_PORT; + } + + if (!tcp_stopped) { + shell_fprintf(shell, SHELL_WARNING, + "TCP server already started!\n"); + return -ENOEXEC; + } + + zperf_tcp_receiver_init(shell, port); + + tcp_stopped = false; + + shell_fprintf(shell, SHELL_NORMAL, + "TCP server started on port %u\n", port); + + return 0; + } else { + return -ENOTSUP; + } +} + +static int cmd_version(const struct shell *shell, size_t argc, char *argv[]) +{ + shell_fprintf(shell, SHELL_NORMAL, "Version: %s\nConfig: %s\n", + VERSION, CONFIG); return 0; } -static void zperf_init(void) +static void zperf_init(const struct shell *shell) { -#if defined(MY_IP6ADDR) || defined(MY_IP4ADDR) int ret; - printk("\n"); -#endif + shell_fprintf(shell, SHELL_NORMAL, "\n"); -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) - if (zperf_get_ipv6_addr(MY_IP6ADDR, MY_PREFIX_LEN_STR, - &ipv6, __func__) < 0) { - printk("[%s] ERROR! Unable to set IP\n", __func__); - } else { - printk("[%s] Setting IP address %s\n", __func__, - net_sprint_ipv6_addr(&ipv6)); + if (IS_ENABLED(CONFIG_NET_IPV6) && MY_IP6ADDR) { + if (zperf_get_ipv6_addr(shell, MY_IP6ADDR, MY_PREFIX_LEN_STR, + &ipv6) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + } else { + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv6_addr(&ipv6)); - net_ipaddr_copy(&in6_addr_my.sin6_addr, &ipv6); - } + net_ipaddr_copy(&in6_addr_my.sin6_addr, &ipv6); + } - ret = net_addr_pton(AF_INET6, DST_IP6ADDR, - &in6_addr_dst.sin6_addr); - if (ret < 0) { - printk("[%s] ERROR! Unable to set IP %s\n", __func__, - DST_IP6ADDR); - } else { - printk("[%s] Setting destination IP address %s\n", __func__, - net_sprint_ipv6_addr(&in6_addr_dst.sin6_addr)); + ret = net_addr_pton(AF_INET6, DST_IP6ADDR, + &in6_addr_dst.sin6_addr); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP %s\n", + DST_IP6ADDR); + } else { + shell_fprintf(shell, SHELL_NORMAL, + "Setting destination IP address %s\n", + net_sprint_ipv6_addr( + &in6_addr_dst.sin6_addr)); + } } -#endif -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) - if (zperf_get_ipv4_addr(MY_IP4ADDR, &ipv4, __func__) < 0) { - printk("[%s] ERROR! Unable to set IP\n", __func__); - } else { - printk("[%s] Setting IP address %s\n", __func__, - net_sprint_ipv4_addr(&ipv4)); + if (IS_ENABLED(CONFIG_NET_IPV4) && MY_IP4ADDR) { + if (zperf_get_ipv4_addr(shell, MY_IP4ADDR, &ipv4) < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP\n"); + } else { + shell_fprintf(shell, SHELL_NORMAL, + "Setting IP address %s\n", + net_sprint_ipv4_addr(&ipv4)); - net_ipaddr_copy(&in4_addr_my.sin_addr, &ipv4); - } + net_ipaddr_copy(&in4_addr_my.sin_addr, &ipv4); + } - ret = net_addr_pton(AF_INET, DST_IP4ADDR, - &in4_addr_dst.sin_addr); - if (ret < 0) { - printk("[%s] ERROR! Unable to set IP %s\n", __func__, - DST_IP4ADDR); - } else { - printk("[%s] Setting destination IP address %s\n", __func__, - net_sprint_ipv4_addr(&in4_addr_dst.sin_addr)); + ret = net_addr_pton(AF_INET, DST_IP4ADDR, + &in4_addr_dst.sin_addr); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IP %s\n", + DST_IP4ADDR); + } else { + shell_fprintf(shell, SHELL_NORMAL, + "Setting destination IP address %s\n", + net_sprint_ipv4_addr( + &in4_addr_dst.sin_addr)); + } } -#endif + + shell_fprintf(shell, SHELL_NORMAL, "\n"); zperf_session_init(); } -#define MY_SHELL_MODULE "zperf" -struct shell_cmd commands[] = { - { CMD_STR_SETIP, shell_cmd_setip }, - { CMD_STR_CONNECTAP, shell_cmd_connectap }, - { CMD_STR_VERSION, shell_cmd_version }, -#if defined(CONFIG_NET_UDP) - { CMD_STR_UDP_UPLOAD, shell_cmd_upload }, - /* Same as upload command but no need to specify the addresses */ - { CMD_STR_UDP_UPLOAD2, shell_cmd_upload2 }, - { CMD_STR_UDP_DOWNLOAD, shell_cmd_udp_download }, +SHELL_CREATE_STATIC_SUBCMD_SET(zperf_cmd_tcp) +{ + SHELL_CMD(upload, NULL, + " [K]\n" + " IP destination\n" + " port destination\n" + " of the test in seconds\n" + " Size of the packet in byte or kilobyte " + "(with suffix K)\n" + "Example: tcp upload 192.0.2.2 1111 1 1K\n" + "Example: tcp upload 2001:db8::2\n", + cmd_tcp_upload), + SHELL_CMD(upload2, NULL, + "v6|v4 [K] [K|M]\n" + ": Use either IPv6 or IPv4\n" + " Duration of the test in seconds\n" + " Size of the packet in byte or kilobyte " + "(with suffix K)\n" + "Example: tcp upload2 v6 1 1K\n" + "Example: tcp upload2 v4\n" +#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR_SET) + "Default IPv6 address is " MY_IP6ADDR + ", destination [" DST_IP6ADDR "]:" DEF_PORT_STR "\n" #endif -#if defined(CONFIG_NET_TCP) - { CMD_STR_TCP_UPLOAD, shell_cmd_upload }, - { CMD_STR_TCP_UPLOAD2, shell_cmd_upload2 }, - { CMD_STR_TCP_DOWNLOAD, shell_cmd_tcp_download }, +#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR_SET) + "Default IPv4 address is " MY_IP4ADDR + ", destination " DST_IP4ADDR ":" DEF_PORT_STR "\n" +#endif + , + cmd_tcp_upload2), + SHELL_CMD(download, NULL, + "\n" + "Example: tcp download 5001\n", + cmd_tcp_download), + SHELL_SUBCMD_SET_END +}; + +SHELL_CREATE_STATIC_SUBCMD_SET(zperf_cmd_udp) +{ + SHELL_CMD(upload, NULL, + " [ [K] " + "[K|M]]\n" + " IP destination\n" + " port destination\n" + " of the test in seconds\n" + " Size of the packet in byte or kilobyte " + "(with suffix K)\n" + " Baudrate in kilobyte or megabyte\n" + "Example: udp upload 192.0.2.2 1111 1 1K 1M\n" + "Example: udp upload 2001:db8::2\n", + cmd_udp_upload), + SHELL_CMD(upload2, NULL, + "v6|v4 [ [K] [K|M]]\n" + ": Use either IPv6 or IPv4\n" + " Duration of the test in seconds\n" + " Size of the packet in byte or kilobyte " + "(with suffix K)\n" + " Baudrate in kilobyte or megabyte\n" + "Example: udp upload2 v4 1 1K 1M\n" + "Example: udp upload2 v6\n" +#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR_SET) + "Default IPv6 address is " MY_IP6ADDR + ", destination [" DST_IP6ADDR "]:" DEF_PORT_STR "\n" #endif -#if defined(PROFILER) - PROF_CMD, +#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR_SET) + "Default IPv4 address is " MY_IP4ADDR + ", destination " DST_IP4ADDR ":" DEF_PORT_STR "\n" #endif - { NULL, NULL } + , + cmd_udp_upload2), + SHELL_CMD(download, NULL, + "\n" + "Example: udp download 5001\n", + cmd_udp_download), + SHELL_SUBCMD_SET_END }; -void main(void) +SHELL_CREATE_STATIC_SUBCMD_SET(zperf_commands) { - shell_cmd_version(0, NULL); - SHELL_REGISTER(MY_SHELL_MODULE, commands); - shell_register_default_module(MY_SHELL_MODULE); + SHELL_CMD(connectap, NULL, + "Connect to AP", + cmd_connectap), + SHELL_CMD(setip, NULL, + "Set IP address\n" + " \n" + "Example setip 2001:db8::2 64\n" + "Example setip 192.0.2.2\n", + cmd_setip), + SHELL_CMD(tcp, &zperf_cmd_tcp, + "Upload/Download TCP data", + cmd_tcp), + SHELL_CMD(udp, &zperf_cmd_udp, + "Upload/Download UDP data", + cmd_udp), + SHELL_CMD(version, NULL, + "Zperf version", + cmd_version), + SHELL_SUBCMD_SET_END +}; - zperf_init(); +SHELL_CMD_REGISTER(zperf, &zperf_commands, "Zperf commands", NULL); -#if PROFILER - while (1) { - k_sleep(5 * MSEC_PER_SEC); - prof_flush(); - } -#else - k_sleep(K_FOREVER); -#endif +void main(void) +{ } diff --git a/samples/net/zperf/src/zperf_tcp_receiver.c b/samples/net/zperf/src/zperf_tcp_receiver.c index b00f20bc8269..58560ff33af0 100644 --- a/samples/net/zperf/src/zperf_tcp_receiver.c +++ b/samples/net/zperf/src/zperf_tcp_receiver.c @@ -27,8 +27,6 @@ #define NET_LOG_ENABLED 1 #include "net_private.h" -#define TAG CMD_STR_TCP_DOWNLOAD" " - #define TCP_RX_FIBER_STACK_SIZE 1024 static K_THREAD_STACK_DEFINE(zperf_tcp_rx_stack, TCP_RX_FIBER_STACK_SIZE); @@ -46,6 +44,7 @@ static void tcp_received(struct net_context *context, int status, void *user_data) { + const struct shell *shell = user_data; struct session *session; u32_t time; @@ -57,14 +56,14 @@ static void tcp_received(struct net_context *context, session = get_session(pkt, SESSION_TCP); if (!session) { - printk(TAG "ERROR! cannot get a session!\n"); + shell_fprintf(shell, SHELL_WARNING, "Cannot get a session!\n"); return; } switch (session->state) { case STATE_NULL: case STATE_COMPLETED: - printk(TAG "New session started\n"); + shell_fprintf(shell, SHELL_NORMAL, "New session started\n"); zperf_reset_session_stats(session); session->start_time = k_cycle_get_32(); session->state = STATE_ONGOING; @@ -91,21 +90,23 @@ static void tcp_received(struct net_context *context, rate_in_kbps = 0; } - printk(TAG "TCP session ended\n"); + shell_fprintf(shell, SHELL_NORMAL, + "TCP session ended\n"); - printk(TAG " duration:\t\t"); - print_number(duration, TIME_US, TIME_US_UNIT); - printk("\n"); + shell_fprintf(shell, SHELL_NORMAL, + " Duration:\t\t"); + print_number(shell, duration, TIME_US, TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); - printk(TAG " rate:\t\t\t"); - print_number(rate_in_kbps, KBPS, KBPS_UNIT); - printk("\n"); + shell_fprintf(shell, SHELL_NORMAL, " rate:\t\t\t"); + print_number(shell, rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); } break; case STATE_LAST_PACKET_RECEIVED: break; default: - printk(TAG "Error! Unsupported case\n"); + shell_fprintf(shell, SHELL_WARNING, "Unsupported case\n"); } net_pkt_unref(pkt); @@ -117,113 +118,124 @@ static void tcp_accepted(struct net_context *context, int error, void *user_data) { + const struct shell *shell = user_data; int ret; ret = net_context_recv(context, tcp_received, K_NO_WAIT, user_data); if (ret < 0) { - printk(TAG "Cannot receive TCP packet (family %d)", - net_context_get_family(context)); + shell_fprintf(shell, SHELL_WARNING, + "Cannot receive TCP packet (family %d)", + net_context_get_family(context)); } } -static void zperf_tcp_rx_thread(int port) +static void zperf_tcp_rx_thread(const struct shell *shell, int port) { -#if defined(CONFIG_NET_IPV4) struct net_context *context4 = NULL; -#endif -#if defined(CONFIG_NET_IPV6) struct net_context *context6 = NULL; -#endif int ret, fail = 0; -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) - ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &context4); - if (ret < 0) { - printk(TAG "ERROR! Cannot get IPv4 TCP network context.\n"); - return; - } - - ret = zperf_get_ipv4_addr(MY_IP4ADDR, &in4_addr_my->sin_addr, TAG); - if (ret < 0) { - printk(TAG "ERROR! Unable to set IPv4\n"); - return; - } + if (IS_ENABLED(CONFIG_NET_IPV4) && MY_IP4ADDR) { + ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, + &context4); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv4 TCP network context.\n"); + return; + } - printk(TAG "Binding to %s\n", - net_sprint_ipv4_addr(&in4_addr_my->sin_addr)); + ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR, + &in4_addr_my->sin_addr); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IPv4\n"); + return; + } - in4_addr_my->sin_port = htons(port); -#endif + shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n", + net_sprint_ipv4_addr(&in4_addr_my->sin_addr)); -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) - ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &context6); - if (ret < 0) { - printk(TAG "ERROR! Cannot get IPv6 TCP network context.\n"); - return; + in4_addr_my->sin_port = htons(port); } - ret = zperf_get_ipv6_addr(MY_IP6ADDR, MY_PREFIX_LEN_STR, - &in6_addr_my->sin6_addr, TAG); - if (ret < 0) { - printk(TAG "ERROR! Unable to set IPv6\n"); - return; - } + if (IS_ENABLED(CONFIG_NET_IPV6) && MY_IP6ADDR) { + ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, + &context6); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv6 TCP network context.\n"); + return; + } - printk(TAG "Binding to %s\n", - net_sprint_ipv6_addr(&in6_addr_my->sin6_addr)); + ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR, MY_PREFIX_LEN_STR, + &in6_addr_my->sin6_addr); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IPv6\n"); + return; + } - in6_addr_my->sin6_port = htons(port); -#endif + shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n", + net_sprint_ipv6_addr(&in6_addr_my->sin6_addr)); -#if defined(CONFIG_NET_IPV6) - if (context6) { + in6_addr_my->sin6_port = htons(port); + } + + if (IS_ENABLED(CONFIG_NET_IPV6) && context6) { ret = net_context_bind(context6, (struct sockaddr *)in6_addr_my, sizeof(struct sockaddr_in6)); if (ret < 0) { - printk(TAG "Cannot bind IPv6 TCP port %d (%d)\n", - ntohs(in6_addr_my->sin6_port), ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot bind IPv6 TCP port %d (%d)\n", + ntohs(in6_addr_my->sin6_port), ret); fail++; } ret = net_context_listen(context6, 0); if (ret < 0) { - printk(TAG "Cannot listen IPv6 TCP (%d)", ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot listen IPv6 TCP (%d)", ret); return; } - ret = net_context_accept(context6, tcp_accepted, K_NO_WAIT, NULL); + ret = net_context_accept(context6, tcp_accepted, K_NO_WAIT, + NULL); if (ret < 0) { - printk(TAG "Cannot receive IPv6 TCP packets (%d)", ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot receive IPv6 TCP packets (%d)", + ret); return; } } -#endif -#if defined(CONFIG_NET_IPV4) - if (context4) { + if (IS_ENABLED(CONFIG_NET_IPV4) && context4) { ret = net_context_bind(context4, (struct sockaddr *)in4_addr_my, sizeof(struct sockaddr_in)); if (ret < 0) { - printk(TAG "Cannot bind IPv4 TCP port %d (%d)\n", - ntohs(in4_addr_my->sin_port), ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot bind IPv4 TCP port %d (%d)\n", + ntohs(in4_addr_my->sin_port), ret); fail++; } ret = net_context_listen(context4, 0); if (ret < 0) { - printk(TAG "Cannot listen IPv4 TCP (%d)", ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot listen IPv4 TCP (%d)", ret); return; } - ret = net_context_accept(context4, tcp_accepted, K_NO_WAIT, NULL); + ret = net_context_accept(context4, tcp_accepted, K_NO_WAIT, + (void *)shell); if (ret < 0) { - printk(TAG "Cannot receive IPv4 TCP packets (%d)", ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot receive IPv4 TCP packets (%d)", + ret); return; } } -#endif if (fail > 1) { return; @@ -232,18 +244,19 @@ static void zperf_tcp_rx_thread(int port) k_sleep(K_FOREVER); } -void zperf_tcp_receiver_init(int port) +void zperf_tcp_receiver_init(const struct shell *shell, int port) { -#if defined(CONFIG_NET_IPV6) - in6_addr_my = zperf_get_sin6(); -#endif -#if defined(CONFIG_NET_IPV4) - in4_addr_my = zperf_get_sin(); -#endif + if (IS_ENABLED(CONFIG_NET_IPV6)) { + in6_addr_my = zperf_get_sin6(); + } + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + in4_addr_my = zperf_get_sin(); + } k_thread_create(&zperf_tcp_rx_thread_data, zperf_tcp_rx_stack, K_THREAD_STACK_SIZEOF(zperf_tcp_rx_stack), (k_thread_entry_t)zperf_tcp_rx_thread, - INT_TO_POINTER(port), 0, 0, + (void *)shell, INT_TO_POINTER(port), 0, K_PRIO_COOP(7), 0, K_NO_WAIT); } diff --git a/samples/net/zperf/src/zperf_tcp_uploader.c b/samples/net/zperf/src/zperf_tcp_uploader.c index 18d3662db774..d8e88e5f8cc2 100644 --- a/samples/net/zperf/src/zperf_tcp_uploader.c +++ b/samples/net/zperf/src/zperf_tcp_uploader.c @@ -19,11 +19,10 @@ #include "zperf.h" #include "zperf_internal.h" -#define TAG CMD_STR_TCP_UPLOAD" " - static char sample_packet[PACKET_SIZE_MAX]; -void zperf_tcp_upload(struct net_context *ctx, +void zperf_tcp_upload(const struct shell *shell, + struct net_context *ctx, unsigned int duration_in_ms, unsigned int packet_size, struct zperf_results *results) @@ -34,8 +33,9 @@ void zperf_tcp_upload(struct net_context *ctx, u8_t time_elapsed = 0, finished = 0; if (packet_size > PACKET_SIZE_MAX) { - printk(TAG "WARNING! packet size too large! max size: %u\n", - PACKET_SIZE_MAX); + shell_fprintf(shell, SHELL_WARNING, + "Packet size too large! max size: %u\n", + PACKET_SIZE_MAX); packet_size = PACKET_SIZE_MAX; } @@ -43,7 +43,9 @@ void zperf_tcp_upload(struct net_context *ctx, start_time = k_cycle_get_32(); last_print_time = start_time; last_loop_time = start_time; - printk(TAG "New session started\n"); + + shell_fprintf(shell, SHELL_NORMAL, + "New session started\n"); (void)memset(sample_packet, 'z', sizeof(sample_packet)); @@ -59,14 +61,16 @@ void zperf_tcp_upload(struct net_context *ctx, pkt = net_pkt_get_tx(ctx, K_FOREVER); if (!pkt) { - printk(TAG "ERROR! Failed to retrieve a packet\n"); + shell_fprintf(shell, SHELL_ERROR, + "Failed to retrieve a packet\n"); break; } frag = net_pkt_get_data(ctx, K_FOREVER); if (!frag) { net_pkt_unref(pkt); - printk(TAG "ERROR! Failed to retrieve a fragment\n"); + shell_fprintf(shell, SHELL_ERROR, + "Failed to retrieve a fragment\n"); break; } @@ -79,8 +83,9 @@ void zperf_tcp_upload(struct net_context *ctx, /* Send the packet */ ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL); if (ret < 0) { - printk(TAG "ERROR! Failed to send the packet (%d)\n", - ret); + shell_fprintf(shell, SHELL_WARNING, + "Failed to send the packet (%d)\n", + ret); net_pkt_unref(pkt); nb_errors++; diff --git a/samples/net/zperf/src/zperf_udp_receiver.c b/samples/net/zperf/src/zperf_udp_receiver.c index f09d75d6a91b..6b8a7370fd8f 100644 --- a/samples/net/zperf/src/zperf_udp_receiver.c +++ b/samples/net/zperf/src/zperf_udp_receiver.c @@ -26,8 +26,6 @@ #define NET_LOG_ENABLED 1 #include "net_private.h" -#define TAG CMD_STR_UDP_DOWNLOAD" " - #define RX_THREAD_STACK_SIZE 1024 #define MY_SRC_PORT 50001 @@ -35,16 +33,13 @@ static K_THREAD_STACK_DEFINE(zperf_rx_stack, RX_THREAD_STACK_SIZE); static struct k_thread zperf_rx_thread_data; -#if defined(CONFIG_NET_IPV6) static struct sockaddr_in6 *in6_addr_my; -#endif -#if defined(CONFIG_NET_IPV4) static struct sockaddr_in *in4_addr_my; -#endif #define MAX_DBG_PRINT 64 -static inline void set_dst_addr(sa_family_t family, +static inline void set_dst_addr(const struct shell *shell, + sa_family_t family, struct net_pkt *pkt, struct sockaddr *dst_addr) { @@ -52,30 +47,28 @@ static inline void set_dst_addr(sa_family_t family, udp_hdr = net_udp_get_hdr(pkt, &hdr); if (!udp_hdr) { - printk(TAG "Invalid UDP data\n"); + shell_fprintf(shell, SHELL_WARNING, + "Invalid UDP data\n"); return; } -#if defined(CONFIG_NET_IPV6) - if (family == AF_INET6) { + if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) { net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr, &NET_IPV6_HDR(pkt)->src); net_sin6(dst_addr)->sin6_family = AF_INET6; net_sin6(dst_addr)->sin6_port = udp_hdr->src_port; } -#endif /* CONFIG_NET_IPV6 */ -#if defined(CONFIG_NET_IPV4) - if (family == AF_INET) { + if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) { net_ipaddr_copy(&net_sin(dst_addr)->sin_addr, &NET_IPV4_HDR(pkt)->src); net_sin(dst_addr)->sin_family = AF_INET; net_sin(dst_addr)->sin_port = udp_hdr->src_port; } -#endif /* CONFIG_NET_IPV4 */ } -static inline struct net_pkt *build_reply_pkt(struct net_context *context, +static inline struct net_pkt *build_reply_pkt(const struct shell *shell, + struct net_context *context, struct net_pkt *pkt, struct zperf_udp_datagram *hdr, struct zperf_server_hdr *stat) @@ -83,7 +76,8 @@ static inline struct net_pkt *build_reply_pkt(struct net_context *context, struct net_pkt *reply_pkt; struct net_buf *frag; - printk(TAG "received %d bytes\n", net_pkt_appdatalen(pkt)); + shell_fprintf(shell, SHELL_NORMAL, + "Received %d bytes\n", net_pkt_appdatalen(pkt)); reply_pkt = net_pkt_get_tx(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER); @@ -109,7 +103,8 @@ static inline struct net_pkt *build_reply_pkt(struct net_context *context, } /* Send statistics to the remote client */ -static int zperf_receiver_send_stat(struct net_context *context, +static int zperf_receiver_send_stat(const struct shell *shell, + struct net_context *context, struct net_pkt *pkt, struct zperf_udp_datagram *hdr, struct zperf_server_hdr *stat) @@ -118,9 +113,9 @@ static int zperf_receiver_send_stat(struct net_context *context, struct sockaddr dst_addr; int ret; - set_dst_addr(net_pkt_family(pkt), pkt, &dst_addr); + set_dst_addr(shell, net_pkt_family(pkt), pkt, &dst_addr); - reply_pkt = build_reply_pkt(context, pkt, hdr, stat); + reply_pkt = build_reply_pkt(shell, context, pkt, hdr, stat); net_pkt_unref(pkt); @@ -130,7 +125,8 @@ static int zperf_receiver_send_stat(struct net_context *context, sizeof(struct sockaddr_in), NULL, 0, NULL, NULL); if (ret < 0) { - printk(TAG " Cannot send data to peer (%d)", ret); + shell_fprintf(shell, SHELL_WARNING, + " Cannot send data to peer (%d)", ret); net_pkt_unref(reply_pkt); } @@ -142,6 +138,7 @@ static void udp_received(struct net_context *context, int status, void *user_data) { + const struct shell *shell = user_data; struct zperf_udp_datagram hdr; struct session *session; struct net_buf *frag; @@ -157,7 +154,8 @@ static void udp_received(struct net_context *context, frag = pkt->frags; if (net_pkt_appdatalen(pkt) < sizeof(struct zperf_udp_datagram)) { - printk(TAG "ERROR! short iperf packet!\n"); + shell_fprintf(shell, SHELL_WARNING, + "Short iperf packet!\n"); net_pkt_unref(pkt); return; } @@ -166,7 +164,8 @@ static void udp_received(struct net_context *context, session = get_session(pkt, SESSION_UDP); if (!session) { - printk(TAG "ERROR! cannot get a session!\n"); + shell_fprintf(shell, SHELL_WARNING, + "Cannot get a session!\n"); return; } @@ -180,16 +179,16 @@ static void udp_received(struct net_context *context, /* Check last packet flags */ if (id < 0) { - printk(TAG "End of session!\n"); + shell_fprintf(shell, SHELL_NORMAL, "End of session!\n"); if (session->state == STATE_COMPLETED) { /* Session is already completed: Resend the stat packet * and continue */ - if (zperf_receiver_send_stat(context, pkt, &hdr, + if (zperf_receiver_send_stat(shell, context, pkt, &hdr, &session->stat) < 0) { - printk(TAG "ERROR! Failed to send the " - "packet\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to send the packet\n"); net_pkt_unref(pkt); } @@ -200,7 +199,8 @@ static void udp_received(struct net_context *context, } else if (session->state != STATE_ONGOING) { /* Start a new session! */ - printk(TAG "New session started.\n"); + shell_fprintf(shell, SHELL_NORMAL, + "New session started.\n"); zperf_reset_session_stats(session); session->state = STATE_ONGOING; @@ -270,32 +270,39 @@ static void udp_received(struct net_context *context, session->stat.jitter1 = 0; session->stat.jitter2 = session->jitter; - if (zperf_receiver_send_stat(context, pkt, &hdr, + if (zperf_receiver_send_stat(shell, context, pkt, &hdr, &session->stat) < 0) { - printk(TAG "ERROR! Failed to send the " - "packet\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to send the packet\n"); net_pkt_unref(pkt); } - printk(TAG " duration:\t\t"); - print_number(duration, TIME_US, TIME_US_UNIT); - printk("\n"); - - printk(TAG " received packets:\t%u\n", - session->counter); - printk(TAG " nb packets lost:\t%u\n", - session->outorder); - printk(TAG " nb packets outorder:\t%u\n", - session->error); - - printk(TAG " jitter:\t\t\t"); - print_number(session->jitter, TIME_US, TIME_US_UNIT); - printk("\n"); - - printk(TAG " rate:\t\t\t"); - print_number(rate_in_kbps, KBPS, KBPS_UNIT); - printk("\n"); + shell_fprintf(shell, SHELL_NORMAL, + " duration:\t\t"); + print_number(shell, duration, TIME_US, TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + + shell_fprintf(shell, SHELL_NORMAL, + " received packets:\t%u\n", + session->counter); + shell_fprintf(shell, SHELL_NORMAL, + " nb packets lost:\t%u\n", + session->outorder); + shell_fprintf(shell, SHELL_NORMAL, + " nb packets outorder:\t%u\n", + session->error); + + shell_fprintf(shell, SHELL_NORMAL, + " jitter:\t\t\t"); + print_number(shell, session->jitter, TIME_US, + TIME_US_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); + + shell_fprintf(shell, SHELL_NORMAL, + " rate:\t\t\t"); + print_number(shell, rate_in_kbps, KBPS, KBPS_UNIT); + shell_fprintf(shell, SHELL_NORMAL, "\n"); } } else { net_pkt_unref(pkt); @@ -303,108 +310,123 @@ static void udp_received(struct net_context *context, } /* RX thread entry point */ -static void zperf_rx_thread(int port) +static void zperf_rx_thread(const struct shell *shell, int port) { -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) struct net_context *context4 = NULL; -#endif -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) struct net_context *context6 = NULL; -#endif int ret; -#if defined(CONFIG_NET_IPV4) && defined(MY_IP4ADDR) - ret = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &context4); - if (ret < 0) { - printk(TAG "ERROR! Cannot get IPv4 network context.\n"); - return; - } - - ret = zperf_get_ipv4_addr(MY_IP4ADDR, &in4_addr_my->sin_addr, TAG); - if (ret < 0) { - printk(TAG "ERROR! Unable to set IPv4\n"); - return; - } - - printk(TAG "Binding to %s\n", - net_sprint_ipv4_addr(&in4_addr_my->sin_addr)); - - in4_addr_my->sin_port = htons(port); - - if (context4) { - ret = net_context_bind(context4, - (struct sockaddr *)in4_addr_my, - sizeof(struct sockaddr_in)); + if (IS_ENABLED(CONFIG_NET_IPV4) && MY_IP4ADDR) { + ret = net_context_get(AF_INET, SOCK_DGRAM, IPPROTO_UDP, + &context4); if (ret < 0) { - printk(TAG "Cannot bind IPv4 UDP port %d (%d)\n", - ntohs(in4_addr_my->sin_port), ret); + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv4 network context.\n"); return; } - } -#endif -#if defined(CONFIG_NET_IPV6) && defined(MY_IP6ADDR) - ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &context6); - if (ret < 0) { - printk(TAG "ERROR! Cannot get IPv6 network context.\n"); - return; - } + ret = zperf_get_ipv4_addr(shell, MY_IP4ADDR, + &in4_addr_my->sin_addr); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IPv4\n"); + return; + } - ret = zperf_get_ipv6_addr(MY_IP6ADDR, MY_PREFIX_LEN_STR, - &in6_addr_my->sin6_addr, TAG); - if (ret < 0) { - printk(TAG "ERROR! Unable to set IPv6\n"); - return; + shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n", + net_sprint_ipv4_addr(&in4_addr_my->sin_addr)); + + in4_addr_my->sin_port = htons(port); + + if (context4) { + ret = net_context_bind(context4, + (struct sockaddr *)in4_addr_my, + sizeof(struct sockaddr_in)); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot bind IPv4 UDP port %d (%d)\n", + ntohs(in4_addr_my->sin_port), + ret); + return; + } + } } - printk(TAG "Binding to %s\n", - net_sprint_ipv6_addr(&in6_addr_my->sin6_addr)); - - in6_addr_my->sin6_port = htons(port); + if (IS_ENABLED(CONFIG_NET_IPV6) && MY_IP6ADDR) { + ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, + &context6); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot get IPv6 network context.\n"); + return; + } - if (context6) { - ret = net_context_bind(context6, - (struct sockaddr *)in6_addr_my, - sizeof(struct sockaddr_in6)); + ret = zperf_get_ipv6_addr(shell, MY_IP6ADDR, MY_PREFIX_LEN_STR, + &in6_addr_my->sin6_addr); if (ret < 0) { - printk(TAG "Cannot bind IPv6 UDP port %d (%d)\n", - ntohs(in6_addr_my->sin6_port), ret); + shell_fprintf(shell, SHELL_WARNING, + "Unable to set IPv6\n"); return; } + + shell_fprintf(shell, SHELL_NORMAL, "Binding to %s\n", + net_sprint_ipv6_addr(&in6_addr_my->sin6_addr)); + + in6_addr_my->sin6_port = htons(port); + + if (context6) { + ret = net_context_bind(context6, + (struct sockaddr *)in6_addr_my, + sizeof(struct sockaddr_in6)); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot bind IPv6 UDP port %d (%d)\n", + ntohs(in6_addr_my->sin6_port), + ret); + return; + } + } } -#endif -#if defined(CONFIG_NET_IPV6) - ret = net_context_recv(context6, udp_received, K_NO_WAIT, NULL); - if (ret < 0) { - printk(TAG "Cannot receive IPv6 UDP packets\n"); + if (IS_ENABLED(CONFIG_NET_IPV6)) { + ret = net_context_recv(context6, udp_received, K_NO_WAIT, + (void *)shell); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot receive IPv6 UDP packets\n"); + return; + } } -#endif /* CONFIG_NET_IPV6 */ -#if defined(CONFIG_NET_IPV4) - ret = net_context_recv(context4, udp_received, K_NO_WAIT, NULL); - if (ret < 0) { - printk(TAG "Cannot receive IPv4 UDP packets\n"); + if (IS_ENABLED(CONFIG_NET_IPV4)) { + ret = net_context_recv(context4, udp_received, K_NO_WAIT, + (void *)shell); + if (ret < 0) { + shell_fprintf(shell, SHELL_WARNING, + "Cannot receive IPv4 UDP packets\n"); + return; + } } -#endif /* CONFIG_NET_IPV4 */ - printk(TAG "Listening on port %d\n", port); + shell_fprintf(shell, SHELL_NORMAL, + "Listening on port %d\n", port); k_sleep(K_FOREVER); } -void zperf_receiver_init(int port) +void zperf_receiver_init(const struct shell *shell, int port) { -#if defined(CONFIG_NET_IPV6) - in6_addr_my = zperf_get_sin6(); -#endif -#if defined(CONFIG_NET_IPV4) - in4_addr_my = zperf_get_sin(); -#endif + if (IS_ENABLED(CONFIG_NET_IPV6)) { + in6_addr_my = zperf_get_sin6(); + } + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + in4_addr_my = zperf_get_sin(); + } k_thread_create(&zperf_rx_thread_data, zperf_rx_stack, K_THREAD_STACK_SIZEOF(zperf_rx_stack), (k_thread_entry_t)zperf_rx_thread, - INT_TO_POINTER(port), 0, 0, + (void *)shell, INT_TO_POINTER(port), 0, K_PRIO_COOP(7), 0, K_NO_WAIT); } diff --git a/samples/net/zperf/src/zperf_udp_uploader.c b/samples/net/zperf/src/zperf_udp_uploader.c index 00fc02b6abbc..10e4c59be19c 100644 --- a/samples/net/zperf/src/zperf_udp_uploader.c +++ b/samples/net/zperf/src/zperf_udp_uploader.c @@ -18,11 +18,10 @@ #include "zperf.h" #include "zperf_internal.h" -#define TAG CMD_STR_UDP_UPLOAD" " - static u8_t sample_packet[PACKET_SIZE_MAX]; -static inline void zperf_upload_decode_stat(struct net_pkt *pkt, +static inline void zperf_upload_decode_stat(const struct shell *shell, + struct net_pkt *pkt, struct zperf_results *results) { struct net_buf *frag; @@ -31,7 +30,8 @@ static inline void zperf_upload_decode_stat(struct net_pkt *pkt, u16_t pos; if (!pkt) { - printk(TAG "ERROR! Failed to receive statistic\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to receive statistics\n"); return; } @@ -42,7 +42,8 @@ static inline void zperf_upload_decode_stat(struct net_pkt *pkt, sizeof(struct zperf_udp_datagram), &offset); if (!frag) { - printk(TAG "ERROR! Network packet too short\n"); + shell_fprintf(shell, SHELL_WARNING, + "Network packet too short\n"); return; } @@ -50,7 +51,8 @@ static inline void zperf_upload_decode_stat(struct net_pkt *pkt, if (net_pkt_appdatalen(pkt) < (sizeof(struct zperf_server_hdr) + sizeof(struct zperf_udp_datagram))) { - printk(TAG "ERROR! Statistics too small\n"); + shell_fprintf(shell, SHELL_WARNING, + "Statistics too small\n"); return; } @@ -84,7 +86,8 @@ static void stat_received(struct net_context *context, *stat = pkt; } -static inline void zperf_upload_fin(struct net_context *context, +static inline void zperf_upload_fin(const struct shell *shell, + struct net_context *context, u32_t nb_packets, u32_t end_time, u32_t packet_size, @@ -102,13 +105,15 @@ static inline void zperf_upload_fin(struct net_context *context, pkt = net_pkt_get_tx(context, K_FOREVER); if (!pkt) { - printk(TAG "ERROR! Failed to retrieve a packet\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to retrieve a packet\n"); continue; } frag = net_pkt_get_data(context, K_FOREVER); if (!frag) { - printk(TAG "ERROR! Failed to retrieve a fragment\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to retrieve a fragment\n"); continue; } @@ -118,12 +123,13 @@ static inline void zperf_upload_fin(struct net_context *context, datagram.id = htonl(-nb_packets); datagram.tv_sec = htonl(HW_CYCLES_TO_SEC(end_time)); datagram.tv_usec = htonl(HW_CYCLES_TO_USEC(end_time) % - USEC_PER_SEC); + USEC_PER_SEC); status = net_pkt_append_all(pkt, sizeof(datagram), - (u8_t *)&datagram, K_FOREVER); + (u8_t *)&datagram, K_FOREVER); if (!status) { - printk(TAG "ERROR! Cannot append datagram data\n"); + shell_fprintf(shell, SHELL_WARNING, + "Cannot append datagram data\n"); break; } @@ -143,8 +149,9 @@ static inline void zperf_upload_fin(struct net_context *context, /* Send the packet */ ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL); if (ret < 0) { - printk(TAG "ERROR! Failed to send the packet (%d)\n", - ret); + shell_fprintf(shell, SHELL_WARNING, + "Failed to send the packet (%d)\n", + ret); net_pkt_unref(pkt); continue; } @@ -153,7 +160,7 @@ static inline void zperf_upload_fin(struct net_context *context, stat = NULL; ret = net_context_recv(context, stat_received, - 2 * MSEC_PER_SEC, &stat); + K_SECONDS(2), &stat); if (ret == -ETIMEDOUT) { break; } @@ -161,7 +168,7 @@ static inline void zperf_upload_fin(struct net_context *context, /* Decode statistics */ if (stat) { - zperf_upload_decode_stat(stat, results); + zperf_upload_decode_stat(shell, stat, results); net_pkt_unref(stat); } @@ -177,14 +184,16 @@ static inline void zperf_upload_fin(struct net_context *context, } if (stat) { - printk(TAG "Drain one spurious stat packet!\n"); + shell_fprintf(shell, SHELL_WARNING, + "Drain one spurious stat packet!\n"); net_pkt_unref(stat); } } } -void zperf_udp_upload(struct net_context *context, +void zperf_udp_upload(const struct shell *shell, + struct net_context *context, unsigned int duration_in_ms, unsigned int packet_size, unsigned int rate_in_kbps, @@ -200,12 +209,14 @@ void zperf_udp_upload(struct net_context *context, u32_t start_time, last_print_time, last_loop_time, end_time; if (packet_size > PACKET_SIZE_MAX) { - printk(TAG "WARNING! packet size too large! max size: %u\n", - PACKET_SIZE_MAX); + shell_fprintf(shell, SHELL_WARNING, + "Packet size too large! max size: %u\n", + PACKET_SIZE_MAX); packet_size = PACKET_SIZE_MAX; } else if (packet_size < sizeof(struct zperf_udp_datagram)) { - printk(TAG "WARNING! packet size set to the min size: %zu\n", - sizeof(struct zperf_udp_datagram)); + shell_fprintf(shell, SHELL_WARNING, + "Packet size set to the min size: %zu\n", + sizeof(struct zperf_udp_datagram)); packet_size = sizeof(struct zperf_udp_datagram); } @@ -248,13 +259,15 @@ void zperf_udp_upload(struct net_context *context, pkt = net_pkt_get_tx(context, K_FOREVER); if (!pkt) { - printk(TAG "ERROR! Failed to retrieve a packet\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to retrieve a packet\n"); continue; } frag = net_pkt_get_data(context, K_FOREVER); if (!frag) { - printk(TAG "ERROR! Failed to retrieve a frag\n"); + shell_fprintf(shell, SHELL_WARNING, + "Failed to retrieve a frag\n"); continue; } @@ -269,7 +282,8 @@ void zperf_udp_upload(struct net_context *context, status = net_pkt_append_all(pkt, sizeof(datagram), (u8_t *)&datagram, K_FOREVER); if (!status) { - printk(TAG "ERROR! Cannot append datagram data\n"); + shell_fprintf(shell, SHELL_WARNING, + "Cannot append datagram data\n"); break; } @@ -288,8 +302,9 @@ void zperf_udp_upload(struct net_context *context, /* Send the packet */ ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL); if (ret < 0) { - printk(TAG "ERROR! Failed to send the packet (%d)\n", - ret); + shell_fprintf(shell, SHELL_WARNING, + "Failed to send the packet (%d)\n", + ret); net_pkt_unref(pkt); break; @@ -299,8 +314,9 @@ void zperf_udp_upload(struct net_context *context, /* Print log every seconds */ if (time_delta(last_print_time, loop_time) > print_interval) { - printk(TAG "nb_packets=%u\tdelay=%u\tadjust=%d\n", - nb_packets, delay, adjust); + shell_fprintf(shell, SHELL_WARNING, + "nb_packets=%u\tdelay=%u\tadjust=%d\n", + nb_packets, delay, adjust); last_print_time = loop_time; } @@ -313,7 +329,8 @@ void zperf_udp_upload(struct net_context *context, end_time = k_cycle_get_32(); - zperf_upload_fin(context, nb_packets, end_time, packet_size, results); + zperf_upload_fin(shell, context, nb_packets, end_time, packet_size, + results); /* Add result coming from the client */ results->nb_packets_sent = nb_packets;