From 2091b3482021ac8549c976437c42036ac61c08d4 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Wed, 13 Jul 2022 09:42:11 +0200 Subject: [PATCH] net: zperf: Extract zperf into library Make a library out of the zperf shell sample. This makes to enable the module in any application, not only the dedicated sample. Signed-off-by: Robert Lubos --- doc/connectivity/networking/api/index.rst | 1 + doc/connectivity/networking/api/zperf.rst | 103 ++++++++++++++++++ samples/net/zperf/CMakeLists.txt | 11 -- samples/net/zperf/README.rst | 80 +------------- samples/net/zperf/prj.conf | 1 + subsys/net/lib/CMakeLists.txt | 1 + subsys/net/lib/Kconfig | 2 + subsys/net/lib/zperf/CMakeLists.txt | 17 +++ subsys/net/lib/zperf/Kconfig | 19 ++++ .../net/lib/zperf}/shell_utils.c | 1 - .../net/lib/zperf}/shell_utils.h | 0 .../src => subsys/net/lib/zperf}/zperf.h | 0 .../net/lib/zperf}/zperf_internal.h | 0 .../net/lib/zperf}/zperf_session.c | 6 +- .../net/lib/zperf}/zperf_session.h | 0 .../net/lib/zperf}/zperf_shell.c | 3 +- .../net/lib/zperf}/zperf_tcp_receiver.c | 4 +- .../net/lib/zperf}/zperf_tcp_uploader.c | 3 +- .../net/lib/zperf}/zperf_udp_receiver.c | 3 +- .../net/lib/zperf}/zperf_udp_uploader.c | 4 +- 20 files changed, 156 insertions(+), 103 deletions(-) create mode 100644 doc/connectivity/networking/api/zperf.rst create mode 100644 subsys/net/lib/zperf/CMakeLists.txt create mode 100644 subsys/net/lib/zperf/Kconfig rename {samples/net/zperf/src => subsys/net/lib/zperf}/shell_utils.c (97%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/shell_utils.h (100%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf.h (100%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_internal.h (100%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_session.c (95%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_session.h (100%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_shell.c (99%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_tcp_receiver.c (99%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_tcp_uploader.c (96%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_udp_receiver.c (99%) rename {samples/net/zperf/src => subsys/net/lib/zperf}/zperf_udp_uploader.c (98%) diff --git a/doc/connectivity/networking/api/index.rst b/doc/connectivity/networking/api/index.rst index 59410af5ef..2386a07546 100644 --- a/doc/connectivity/networking/api/index.rst +++ b/doc/connectivity/networking/api/index.rst @@ -13,3 +13,4 @@ Networking APIs system_mgmt.rst tsn.rst gsm_modem.rst + zperf.rst diff --git a/doc/connectivity/networking/api/zperf.rst b/doc/connectivity/networking/api/zperf.rst new file mode 100644 index 0000000000..4f6674679d --- /dev/null +++ b/doc/connectivity/networking/api/zperf.rst @@ -0,0 +1,103 @@ +.. _zperf: + +zperf: Network Traffic Generator +################################ + +.. contents:: + :local: + :depth: 2 + +Overview +******** + +zperf is a shell utility which allows to generate network traffic in Zephyr. The +tool may be used to evaluate network bandwidth. + +zperf is compatible with iPerf_2.0.5. Note that in newer iPerf versions, +an error message like this is printed and the server reported statistics +are missing. + +.. code-block:: console + + LAST PACKET NOT RECEIVED!!! + +zperf can be enabled in any application, a dedicated sample is also present +in Zephyr. See :ref:`zperf sample application ` for details. + +Sample Usage +************ + +If Zephyr acts as a client, iPerf must be executed in server mode. +For example, the following command line must be used for UDP testing: + +.. code-block:: console + + $ iperf -s -l 1K -u -V -B 2001:db8::2 + +For TCP testing, the command line would look like this: + +.. code-block:: console + + $ iperf -s -l 1K -V -B 2001:db8::2 + + +In the Zephyr console, zperf can be executed as follows: + +.. code-block:: console + + 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 + + +If the IP addresses of Zephyr and the host machine are specified in the +config file, zperf can be started as follows: + +.. code-block:: console + + 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 + + +If Zephyr is acting as a server, set the download mode as follows for UDP: + +.. code-block:: console + + zperf udp download 5001 + + +or like this for TCP: + +.. code-block:: console + + zperf tcp download 5001 + + +and in the host side, iPerf must be executed with the following +command line if you are testing UDP: + +.. code-block:: console + + $ iperf -l 1K -u -V -c 2001:db8::1 -p 5001 + + +and this if you are testing TCP: + +.. code-block:: console + + $ iperf -l 1K -V -c 2001:db8::1 -p 5001 + + +iPerf output can be limited by using the -b option if Zephyr is not +able to receive all the packets in orderly manner. diff --git a/samples/net/zperf/CMakeLists.txt b/samples/net/zperf/CMakeLists.txt index c9b1235038..ae9b46dadc 100644 --- a/samples/net/zperf/CMakeLists.txt +++ b/samples/net/zperf/CMakeLists.txt @@ -7,15 +7,4 @@ project(zperf) target_sources(app PRIVATE src/main.c - src/shell_utils.c - src/zperf_session.c - src/zperf_shell.c - src/zperf_udp_receiver.c - src/zperf_udp_uploader.c - src/zperf_tcp_receiver.c - src/zperf_tcp_uploader.c - ) - -target_include_directories(app PRIVATE - ${ZEPHYR_BASE}/subsys/net/ip ) diff --git a/samples/net/zperf/README.rst b/samples/net/zperf/README.rst index d98bd66f04..dd968b3423 100644 --- a/samples/net/zperf/README.rst +++ b/samples/net/zperf/README.rst @@ -6,8 +6,8 @@ zperf: Network Traffic Generator Description *********** -zperf is a network traffic generator for Zephyr that may be used to -evaluate network bandwidth. +The zperf sample demonstrates the :ref:`zperf shell utility `, which +allows to evaluate network bandwidth. Features ********* @@ -46,77 +46,5 @@ to setup the network environment. Usage ***** -If Zephyr acts as a client, iPerf must be executed in server mode. -For example, the following command line must be used for UDP testing: - -.. code-block:: console - - $ iperf -s -l 1K -u -V -B 2001:db8::2 - -For TCP testing, the command line would look like this: - -.. code-block:: console - - $ iperf -s -l 1K -V -B 2001:db8::2 - - -In the Zephyr console, zperf can be executed as follows: - -.. code-block:: console - - 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 - - -If the IP addresses of Zephyr and the host machine are specified in the -config file, zperf can be started as follows: - -.. code-block:: console - - 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 - - -If Zephyr is acting as a server, set the download mode as follows for UDP: - -.. code-block:: console - - zperf udp download 5001 - - -or like this for TCP: - -.. code-block:: console - - zperf tcp download 5001 - - -and in the host side, iPerf must be executed with the following -command line if you are testing UDP: - -.. code-block:: console - - $ iperf -l 1K -u -V -c 2001:db8::1 -p 5001 - - -and this if you are testing TCP: - -.. code-block:: console - - $ iperf -l 1K -V -c 2001:db8::1 -p 5001 - - -iPerf output can be limited by using the -b option if Zephyr is not -able to receive all the packets in orderly manner. +See :ref:`zperf library documentation ` for more information about +the library usage. diff --git a/samples/net/zperf/prj.conf b/samples/net/zperf/prj.conf index a2cd41ad84..f5149803b6 100644 --- a/samples/net/zperf/prj.conf +++ b/samples/net/zperf/prj.conf @@ -1,4 +1,5 @@ CONFIG_NETWORKING=y +CONFIG_NET_ZPERF=y CONFIG_NET_LOG=y CONFIG_NET_IPV6=y CONFIG_NET_IPV4=y diff --git a/subsys/net/lib/CMakeLists.txt b/subsys/net/lib/CMakeLists.txt index 189c918e17..ca787566c2 100644 --- a/subsys/net/lib/CMakeLists.txt +++ b/subsys/net/lib/CMakeLists.txt @@ -12,6 +12,7 @@ add_subdirectory_ifdef(CONFIG_NET_SOCKETS sockets) add_subdirectory_ifdef(CONFIG_TLS_CREDENTIALS tls_credentials) add_subdirectory_ifdef(CONFIG_NET_CONNECTION_MANAGER conn_mgr) add_subdirectory_ifdef(CONFIG_NET_CAPTURE capture) +add_subdirectory_ifdef(CONFIG_NET_ZPERF zperf) if (CONFIG_DNS_RESOLVER OR CONFIG_MDNS_RESPONDER diff --git a/subsys/net/lib/Kconfig b/subsys/net/lib/Kconfig index 1e8e95fb05..3cdf56f6c7 100644 --- a/subsys/net/lib/Kconfig +++ b/subsys/net/lib/Kconfig @@ -39,4 +39,6 @@ source "subsys/net/lib/conn_mgr/Kconfig" source "subsys/net/lib/capture/Kconfig" +source "subsys/net/lib/zperf/Kconfig" + endmenu diff --git a/subsys/net/lib/zperf/CMakeLists.txt b/subsys/net/lib/zperf/CMakeLists.txt new file mode 100644 index 0000000000..9994c617b2 --- /dev/null +++ b/subsys/net/lib/zperf/CMakeLists.txt @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library_named(zperf) + +zephyr_library_sources( + shell_utils.c + zperf_session.c + zperf_shell.c + zperf_udp_receiver.c + zperf_udp_uploader.c + zperf_tcp_receiver.c + zperf_tcp_uploader.c +) + +zephyr_library_include_directories( + ${ZEPHYR_BASE}/subsys/net/ip +) diff --git a/subsys/net/lib/zperf/Kconfig b/subsys/net/lib/zperf/Kconfig new file mode 100644 index 0000000000..5ac5c5c6f5 --- /dev/null +++ b/subsys/net/lib/zperf/Kconfig @@ -0,0 +1,19 @@ +# Copyright (c) 2022 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +menuconfig NET_ZPERF + bool "zperf shell utility" + depends on SHELL + help + This option enables zperf shell utility, which allows to generate + network traffic and evaluate network bandwidth. + +if NET_ZPERF + +module = NET_ZPERF +module-dep = NET_LOG +module-str = Log level for zperf +module-help = Enable debug message of zperf libray. +source "subsys/net/Kconfig.template.log_config.net" + +endif diff --git a/samples/net/zperf/src/shell_utils.c b/subsys/net/lib/zperf/shell_utils.c similarity index 97% rename from samples/net/zperf/src/shell_utils.c rename to subsys/net/lib/zperf/shell_utils.c index a9d11c2299..988bd5dc44 100644 --- a/samples/net/zperf/src/shell_utils.c +++ b/subsys/net/lib/zperf/shell_utils.c @@ -5,7 +5,6 @@ */ #include -#include #include #include #include diff --git a/samples/net/zperf/src/shell_utils.h b/subsys/net/lib/zperf/shell_utils.h similarity index 100% rename from samples/net/zperf/src/shell_utils.h rename to subsys/net/lib/zperf/shell_utils.h diff --git a/samples/net/zperf/src/zperf.h b/subsys/net/lib/zperf/zperf.h similarity index 100% rename from samples/net/zperf/src/zperf.h rename to subsys/net/lib/zperf/zperf.h diff --git a/samples/net/zperf/src/zperf_internal.h b/subsys/net/lib/zperf/zperf_internal.h similarity index 100% rename from samples/net/zperf/src/zperf_internal.h rename to subsys/net/lib/zperf/zperf_internal.h diff --git a/samples/net/zperf/src/zperf_session.c b/subsys/net/lib/zperf/zperf_session.c similarity index 95% rename from samples/net/zperf/src/zperf_session.c rename to subsys/net/lib/zperf/zperf_session.c index bc643ec3c9..68e2a66baf 100644 --- a/samples/net/zperf/src/zperf_session.c +++ b/subsys/net/lib/zperf/zperf_session.c @@ -5,7 +5,7 @@ */ #include -LOG_MODULE_DECLARE(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include @@ -29,7 +29,7 @@ struct session *get_session(const struct sockaddr *addr, const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; if (proto != SESSION_TCP && proto != SESSION_UDP) { - printk("Error! unsupported proto.\n"); + NET_ERR("Error! unsupported proto.\n"); return NULL; } @@ -92,7 +92,7 @@ struct session *get_tcp_session(int sock) int i = 0; if (sock < 0) { - printk("Error! Invalid socket.\n"); + NET_ERR("Error! Invalid socket.\n"); return NULL; } diff --git a/samples/net/zperf/src/zperf_session.h b/subsys/net/lib/zperf/zperf_session.h similarity index 100% rename from samples/net/zperf/src/zperf_session.h rename to subsys/net/lib/zperf/zperf_session.h diff --git a/samples/net/zperf/src/zperf_shell.c b/subsys/net/lib/zperf/zperf_shell.c similarity index 99% rename from samples/net/zperf/src/zperf_shell.c rename to subsys/net/lib/zperf/zperf_shell.c index 9ce46c1cdd..df79e95658 100644 --- a/samples/net/zperf/src/zperf_shell.c +++ b/subsys/net/lib/zperf/zperf_shell.c @@ -5,14 +5,13 @@ */ #include -LOG_MODULE_REGISTER(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_REGISTER(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include #include #include #include -#include #include #include diff --git a/samples/net/zperf/src/zperf_tcp_receiver.c b/subsys/net/lib/zperf/zperf_tcp_receiver.c similarity index 99% rename from samples/net/zperf/src/zperf_tcp_receiver.c rename to subsys/net/lib/zperf/zperf_tcp_receiver.c index ad540a9a8f..fea4f66a7c 100644 --- a/samples/net/zperf/src/zperf_tcp_receiver.c +++ b/subsys/net/lib/zperf/zperf_tcp_receiver.c @@ -5,15 +5,13 @@ */ #include -LOG_MODULE_DECLARE(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include #include #include -#include - #include #include "zperf.h" diff --git a/samples/net/zperf/src/zperf_tcp_uploader.c b/subsys/net/lib/zperf/zperf_tcp_uploader.c similarity index 96% rename from samples/net/zperf/src/zperf_tcp_uploader.c rename to subsys/net/lib/zperf/zperf_tcp_uploader.c index 4b3c3a0ca8..888b1ce06b 100644 --- a/samples/net/zperf/src/zperf_tcp_uploader.c +++ b/subsys/net/lib/zperf/zperf_tcp_uploader.c @@ -5,12 +5,11 @@ */ #include -LOG_MODULE_DECLARE(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include #include -#include #include diff --git a/samples/net/zperf/src/zperf_udp_receiver.c b/subsys/net/lib/zperf/zperf_udp_receiver.c similarity index 99% rename from samples/net/zperf/src/zperf_udp_receiver.c rename to subsys/net/lib/zperf/zperf_udp_receiver.c index 4e05e31a13..623120e5de 100644 --- a/samples/net/zperf/src/zperf_udp_receiver.c +++ b/subsys/net/lib/zperf/zperf_udp_receiver.c @@ -5,13 +5,12 @@ */ #include -LOG_MODULE_DECLARE(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include #include #include -#include #include diff --git a/samples/net/zperf/src/zperf_udp_uploader.c b/subsys/net/lib/zperf/zperf_udp_uploader.c similarity index 98% rename from samples/net/zperf/src/zperf_udp_uploader.c rename to subsys/net/lib/zperf/zperf_udp_uploader.c index d2f6943878..3abd22d8a2 100644 --- a/samples/net/zperf/src/zperf_udp_uploader.c +++ b/subsys/net/lib/zperf/zperf_udp_uploader.c @@ -5,12 +5,10 @@ */ #include -LOG_MODULE_DECLARE(net_zperf_sample, LOG_LEVEL_DBG); +LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL); #include -#include - #include #include "zperf.h"