netutils/iperf: Support local socket test

Usage:
iperf -s --local <path>
iperf -c <path> --local || iperf -c <whatever> --local <path>

Can combine with other options, e.g. '-u' will result in local
UDP (DGRAM).

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-04-07 16:46:19 +08:00 committed by Xiang Xiao
parent 411c98acec
commit 213219f30a
3 changed files with 125 additions and 36 deletions

View File

@ -32,6 +32,7 @@
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include "iperf.h"
@ -224,6 +225,13 @@ static void iperf_print_addr(FAR const char *str, FAR struct sockaddr *addr)
return;
}
case AF_LOCAL:
{
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)addr;
printf("%s: path=%s\n", str, unaddr->sun_path);
return;
}
default:
assert(false); /* shouldn't happen */
}
@ -375,15 +383,29 @@ static int iperf_start_report(FAR struct iperf_ctrl_t *ctrl)
static int iperf_run_server(FAR struct iperf_ctrl_t *ctrl,
iperf_server_func_t server_func)
{
struct sockaddr_in addr;
struct sockaddr_in remote_addr;
if (ctrl->cfg.flag & IPERF_FLAG_LOCAL)
{
struct sockaddr_un addr;
struct sockaddr_un remote_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(ctrl->cfg.sport);
addr.sin_addr.s_addr = ctrl->cfg.sip;
addr.sun_family = AF_LOCAL;
strlcpy(addr.sun_path, ctrl->cfg.path, sizeof(addr.sun_path));
return server_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr),
(FAR struct sockaddr *)&remote_addr);
return server_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr),
(FAR struct sockaddr *)&remote_addr);
}
else
{
struct sockaddr_in addr;
struct sockaddr_in remote_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(ctrl->cfg.sport);
addr.sin_addr.s_addr = ctrl->cfg.sip;
return server_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr),
(FAR struct sockaddr *)&remote_addr);
}
}
/****************************************************************************
@ -397,13 +419,25 @@ static int iperf_run_server(FAR struct iperf_ctrl_t *ctrl,
static int iperf_run_client(FAR struct iperf_ctrl_t *ctrl,
iperf_client_func_t client_func)
{
struct sockaddr_in addr;
if (ctrl->cfg.flag & IPERF_FLAG_LOCAL)
{
struct sockaddr_un addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(ctrl->cfg.dport);
addr.sin_addr.s_addr = ctrl->cfg.dip;
addr.sun_family = AF_LOCAL;
strlcpy(addr.sun_path, ctrl->cfg.path, sizeof(addr.sun_path));
return client_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr));
return client_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr));
}
else
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(ctrl->cfg.dport);
addr.sin_addr.s_addr = ctrl->cfg.dip;
return client_func(ctrl, (FAR struct sockaddr *)&addr, sizeof(addr));
}
}
/****************************************************************************

View File

@ -44,10 +44,11 @@ extern "C"
* Pre-processor Definitions
****************************************************************************/
#define IPERF_FLAG_CLIENT (1)
#define IPERF_FLAG_CLIENT (1 << 0)
#define IPERF_FLAG_SERVER (1 << 1)
#define IPERF_FLAG_TCP (1 << 2)
#define IPERF_FLAG_UDP (1 << 3)
#define IPERF_FLAG_TCP (1 << 2)
#define IPERF_FLAG_UDP (1 << 3)
#define IPERF_FLAG_LOCAL (1 << 4)
/****************************************************************************
* Public Types
@ -62,6 +63,7 @@ struct iperf_cfg_t
uint16_t sport;
uint32_t interval;
uint32_t time;
FAR const char *path; /* local path or rpmsg name */
};
/****************************************************************************

View File

@ -56,6 +56,7 @@ struct wifi_iperf_t
FAR struct arg_str *ip;
FAR struct arg_lit *server;
FAR struct arg_lit *udp;
FAR struct arg_str *local;
FAR struct arg_int *port;
FAR struct arg_int *interval;
FAR struct arg_int *time;
@ -79,7 +80,7 @@ static void iperf_showusage(FAR const char *progname,
FAR struct wifi_iperf_t *args, int exitcode)
{
printf("USAGE: %s [-sua] [-c <ip>] [-p <port>] [-i <interval>] "
"[-t <time>]\n", progname);
"[-t <time>] [--local <path>]\n", progname);
printf("iperf command:\n");
arg_print_glossary(stdout, (FAR void **)args, NULL);
@ -87,6 +88,39 @@ static void iperf_showusage(FAR const char *progname,
exit(exitcode);
}
/****************************************************************************
* Name: iperf_printcfg
*
* Description:
* Print config line before start
*
****************************************************************************/
static void iperf_printcfg(FAR struct iperf_cfg_t *cfg)
{
printf("\n mode=%s%s-%s ",
cfg->flag & IPERF_FLAG_LOCAL ? "local-":"",
cfg->flag & IPERF_FLAG_TCP ? "tcp":"udp",
cfg->flag & IPERF_FLAG_SERVER ? "server":"client");
if (cfg->flag & IPERF_FLAG_LOCAL)
{
printf("path=%s, ", cfg->path);
}
else
{
printf("sip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d,"
"dip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d, ",
cfg->sip & 0xff, (cfg->sip >> 8) & 0xff,
(cfg->sip >> 16) & 0xff, (cfg->sip >> 24) & 0xff, cfg->sport,
cfg->dip & 0xff, (cfg->dip >> 8) & 0xff,
(cfg->dip >> 16) & 0xff, (cfg->dip >> 24) & 0xff, cfg->dport);
}
printf("interval=%" PRId32 ", time=%" PRId32 "\n",
cfg->interval, cfg->time);
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -106,6 +140,7 @@ int main(int argc, FAR char *argv[])
"run in client mode, connecting to <host>");
iperf_args.server = arg_lit0("s", "server", "run in server mode");
iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP");
iperf_args.local = arg_str0(NULL, "local", "<path>", "use local socket");
iperf_args.port = arg_int0("p", "port", "<port>",
"server port to listen on/connect to");
iperf_args.interval = arg_int0("i", "interval", "<interval>",
@ -115,6 +150,10 @@ int main(int argc, FAR char *argv[])
iperf_args.abort = arg_lit0("a", "abort", "abort running iperf");
iperf_args.end = arg_end(1);
/* Value of local is needed for server, optional for client. */
iperf_args.local->hdr.flag |= ARG_HASOPTVALUE;
nerrors = arg_parse(argc, argv, (FAR void**) &iperf_args);
if (nerrors != 0)
{
@ -146,17 +185,40 @@ int main(int argc, FAR char *argv[])
cfg.flag |= IPERF_FLAG_CLIENT;
}
addr.s_addr = 0;
netlib_get_ipv4addr(DEVNAME, &addr);
if (addr.s_addr == 0)
if (iperf_args.local->count > 0)
{
printf("ERROR: access IP is 0x00\n");
return -1;
cfg.flag |= IPERF_FLAG_LOCAL;
if (strlen(iperf_args.local->sval[0]) > 0)
{
/* iperf -s --local <path> or iperf -c <whatever> --local <path> */
cfg.path = iperf_args.local->sval[0];
}
else if (iperf_args.ip->count > 0)
{
/* iperf -c <path> --local */
cfg.path = iperf_args.ip->sval[0];
}
else
{
printf("ERROR: should specific local socket path\n");
iperf_showusage(argv[0], &iperf_args, 0);
}
}
else
{
netlib_get_ipv4addr(DEVNAME, &addr);
if (addr.s_addr == 0)
{
printf("ERROR: access IP is 0x00\n");
goto out;
}
printf(" IP: %s\n", inet_ntoa_r(addr, inetaddr, sizeof(inetaddr)));
printf(" IP: %s\n", inet_ntoa_r(addr, inetaddr, sizeof(inetaddr)));
cfg.sip = addr.s_addr;
cfg.sip = addr.s_addr;
}
if (iperf_args.udp->count == 0)
{
@ -221,21 +283,12 @@ int main(int argc, FAR char *argv[])
}
}
iperf_printcfg(&cfg);
iperf_start(&cfg);
out:
arg_freetable((FAR void **)&iperf_args,
sizeof(iperf_args) / sizeof(FAR void *));
printf("\n mode=%s-%s "
"sip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d,"
"dip=%" PRId32 ".%" PRId32 ".%" PRId32 ".%" PRId32 ":%d, "
"interval=%" PRId32 ", time=%" PRId32 "\n",
cfg.flag & IPERF_FLAG_TCP ?"tcp":"udp",
cfg.flag & IPERF_FLAG_SERVER ?"server":"client",
cfg.sip & 0xff, (cfg.sip >> 8) & 0xff, (cfg.sip >> 16) & 0xff,
(cfg.sip >> 24) & 0xff, cfg.sport,
cfg.dip & 0xff, (cfg.dip >> 8) & 0xff, (cfg.dip >> 16) & 0xff,
(cfg.dip >> 24) & 0xff, cfg.dport,
cfg.interval, cfg.time);
iperf_start(&cfg);
return 0;
}