netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Leonard Crestez <cdleonard@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>, Shuah Khan <shuah@kernel.org>,
	David Ahern <dsahern@kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>,
	Ido Schimmel <idosch@nvidia.com>,
	Seth David Schoen <schoen@loyalty.org>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 09/11] selftests: nettest: Convert timeout to miliseconds
Date: Wed,  6 Oct 2021 14:47:25 +0300	[thread overview]
Message-ID: <5a320aefed743c2a0e64c3cb30b3e258db013d1b.1633520807.git.cdleonard@gmail.com> (raw)
In-Reply-To: <cover.1633520807.git.cdleonard@gmail.com>

This allows tests to be faster

Signed-off-by: Leonard Crestez <cdleonard@gmail.com>
---
 tools/testing/selftests/net/nettest.c | 52 +++++++++++++++++++++------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/net/nettest.c b/tools/testing/selftests/net/nettest.c
index 576d8bb4c94c..eb6c8cf69a74 100644
--- a/tools/testing/selftests/net/nettest.c
+++ b/tools/testing/selftests/net/nettest.c
@@ -125,18 +125,24 @@ struct sock_args {
 	/* ESP in UDP encap test */
 	int use_xfrm;
 };
 
 static int server_mode;
-static unsigned int prog_timeout = 5;
+static unsigned int prog_timeout_ms = 5000;
 static unsigned int interactive;
 static int iter = 1;
 static char *msg = "Hello world!";
 static int msglen;
 static int quiet;
 static int try_broadcast = 1;
 
+static void set_timeval_ms(struct timeval *tv, unsigned long ms)
+{
+	tv->tv_sec = ms / 1000;
+	tv->tv_usec = (ms % 1000) * 1000;
+}
+
 static char *timestamp(char *timebuf, int buflen)
 {
 	time_t now;
 
 	now = time(NULL);
@@ -566,10 +572,29 @@ static int str_to_uint(const char *str, int min, int max, unsigned int *value)
 	}
 
 	return -1;
 }
 
+/* parse seconds with a decimal point as miliseconds */
+static int str_to_msec(const char *str, unsigned int *value)
+{
+	float float_value;
+	char *end;
+
+	float_value = strtof(str, &end);
+
+	/* entire string should be consumed by conversion
+	 * and value should be between min and max
+	 */
+	if (((*end == '\0') || (*end == '\n')) && (end != str)) {
+		*value = float_value * 1000;
+		return 0;
+	}
+
+	return -1;
+}
+
 static int resolve_devices(struct sock_args *args)
 {
 	if (args->dev) {
 		args->ifindex = get_ifidx(args->dev);
 		if (args->ifindex < 0) {
@@ -1165,11 +1190,11 @@ static void set_recv_attr(int sd, int version)
 }
 
 static int msg_loop(int client, int sd, void *addr, socklen_t alen,
 		    struct sock_args *args)
 {
-	struct timeval timeout = { .tv_sec = prog_timeout }, *ptval = NULL;
+	struct timeval timeout, *ptval = NULL;
 	fd_set rfds;
 	int nfds;
 	int rc;
 
 	if (args->type != SOCK_STREAM)
@@ -1182,13 +1207,15 @@ static int msg_loop(int client, int sd, void *addr, socklen_t alen,
 		if (client) {
 			if (send_msg(sd, addr, alen, args))
 				return 1;
 		}
 		if (!interactive) {
+			if (!prog_timeout_ms)
+				set_timeval_ms(&timeout, 5000);
+			else
+				set_timeval_ms(&timeout, prog_timeout_ms);
 			ptval = &timeout;
-			if (!prog_timeout)
-				timeout.tv_sec = 5;
 		}
 	}
 
 	nfds = interactive ? MAX(fileno(stdin), sd)  + 1 : sd + 1;
 	while (1) {
@@ -1479,11 +1506,11 @@ static void ipc_write(int fd, int message)
 }
 
 static int do_server(struct sock_args *args, int ipc_fd)
 {
 	/* ipc_fd = -1 if no parent process to signal */
-	struct timeval timeout = { .tv_sec = prog_timeout }, *ptval = NULL;
+	struct timeval timeout, *ptval = NULL;
 	unsigned char addr[sizeof(struct sockaddr_in6)] = {};
 	socklen_t alen = sizeof(addr);
 	int lsd, csd = -1;
 
 	fd_set rfds;
@@ -1501,12 +1528,14 @@ static int do_server(struct sock_args *args, int ipc_fd)
 	args->dev = args->server_dev;
 	args->expected_dev = args->expected_server_dev;
 	if (resolve_devices(args) || validate_addresses(args))
 		goto err_exit;
 
-	if (prog_timeout)
+	if (prog_timeout_ms) {
+		set_timeval_ms(&timeout, prog_timeout_ms);
 		ptval = &timeout;
+	}
 
 	if (args->has_grp)
 		lsd = msock_server(args);
 	else
 		lsd = lsock_init(args);
@@ -1584,20 +1613,22 @@ static int do_server(struct sock_args *args, int ipc_fd)
 	return 1;
 }
 
 static int wait_for_connect(int sd)
 {
-	struct timeval _tv = { .tv_sec = prog_timeout }, *tv = NULL;
+	struct timeval _tv, *tv = NULL;
 	fd_set wfd;
 	int val = 0, sz = sizeof(val);
 	int rc;
 
 	FD_ZERO(&wfd);
 	FD_SET(sd, &wfd);
 
-	if (prog_timeout)
+	if (prog_timeout_ms) {
+		set_timeval_ms(&_tv, prog_timeout_ms);
 		tv = &_tv;
+	}
 
 	rc = select(FD_SETSIZE, NULL, &wfd, NULL, tv);
 	if (rc == 0) {
 		log_error("connect timed out\n");
 		return -2;
@@ -1945,12 +1976,11 @@ int main(int argc, char *argv[])
 				return 1;
 			}
 			args.port = (unsigned short) tmp;
 			break;
 		case 't':
-			if (str_to_uint(optarg, 0, INT_MAX,
-					&prog_timeout) != 0) {
+			if (str_to_msec(optarg, &prog_timeout_ms) != 0) {
 				fprintf(stderr, "Invalid timeout\n");
 				return 1;
 			}
 			break;
 		case 'D':
@@ -2091,11 +2121,11 @@ int main(int argc, char *argv[])
 			"Fork after listen only supported for server mode\n");
 		return 1;
 	}
 
 	if (interactive) {
-		prog_timeout = 0;
+		prog_timeout_ms = 0;
 		msg = NULL;
 	}
 
 	if (both_mode) {
 		if (pipe(fd) < 0) {
-- 
2.25.1


  parent reply	other threads:[~2021-10-06 11:48 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-06 11:47 [PATCH 00/11] selftests: Improve nettest and net/fcnal-test.sh Leonard Crestez
2021-10-06 11:47 ` [PATCH 01/11] selftests: net/fcnal: Fix {ipv4,ipv6}_bind not run by default Leonard Crestez
2021-10-06 14:37   ` David Ahern
2021-10-06 11:47 ` [PATCH 02/11] selftests: net/fcnal: Mark unknown -t or TESTS value as error Leonard Crestez
2021-10-06 14:37   ` David Ahern
2021-10-06 11:47 ` [PATCH 03/11] selftests: net/fcnal: Non-zero exit on failures Leonard Crestez
2021-10-06 14:37   ` David Ahern
2021-10-06 11:47 ` [PATCH 04/11] selftests: net/fcnal: Use accept_dad=0 to avoid setup sleep Leonard Crestez
2021-10-06 14:38   ` David Ahern
2021-10-06 11:47 ` [PATCH 05/11] selftests: net/fcnal: kill_procs via spin instead of sleep Leonard Crestez
2021-10-06 14:45   ` David Ahern
2021-10-06 21:16     ` Leonard Crestez
2021-10-06 11:47 ` [PATCH 06/11] selftests: net/fcnal: Do not capture do_run_cmd in verbose mode Leonard Crestez
2021-10-06 14:48   ` David Ahern
2021-10-06 11:47 ` [PATCH 07/11] selftests: nettest: Implement -k to fork after bind or listen Leonard Crestez
2021-10-06 11:47 ` [PATCH 08/11] selftests: net/fcnal: Replace sleep after server start with -k Leonard Crestez
2021-10-06 14:54   ` David Ahern
2021-10-06 21:35     ` Leonard Crestez
2021-10-07  1:22       ` David Ahern
2021-11-10 13:54         ` Leonard Crestez
2021-10-06 11:47 ` Leonard Crestez [this message]
2021-10-06 14:56   ` [PATCH 09/11] selftests: nettest: Convert timeout to miliseconds David Ahern
2021-10-06 11:47 ` [PATCH 10/11] selftests: nettest: Add NETTEST_CLIENT,SERVER}_TIMEOUT envvars Leonard Crestez
2021-10-06 14:59   ` David Ahern
2021-10-06 11:47 ` [PATCH 11/11] selftests: net/fcnal: Reduce client timeout Leonard Crestez
2021-10-06 15:01   ` David Ahern
2021-10-06 21:26     ` Leonard Crestez
2021-10-07  1:17       ` David Ahern
2021-10-07 20:52         ` Leonard Crestez
2021-10-08  3:01           ` David Ahern

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=5a320aefed743c2a0e64c3cb30b3e258db013d1b.1633520807.git.cdleonard@gmail.com \
    --to=cdleonard@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=schoen@loyalty.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

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

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