[PATCH mptcp-next 1/4] selftests: mptcp: add cfg_timeo for mptcp_connect

Geliang Tang posted 4 patches 3 months ago
[PATCH mptcp-next 1/4] selftests: mptcp: add cfg_timeo for mptcp_connect
Posted by Geliang Tang 3 months ago
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch adds a new helper settimeo(), which comes from BPF selftests
network_helpers.c, into MPTCP selftests tool mptcp_connect, to set
SO_RCVTIMEO and SO_SNDTIMEO options of the given socket.

Add a new mptcp_connect option "-O" to pass a timeout value and a new
cfg cfg_timeo to store this value.

Invoke this new helper in sock_connect_mptcp() when cfg_timeo is set.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../selftests/net/mptcp/mptcp_connect.c       | 33 ++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/mptcp/mptcp_connect.c b/tools/testing/selftests/net/mptcp/mptcp_connect.c
index 4209b9569039..ce261a4bb324 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_connect.c
+++ b/tools/testing/selftests/net/mptcp/mptcp_connect.c
@@ -75,6 +75,7 @@ static char *cfg_input;
 static int cfg_repeat = 1;
 static int cfg_truncate;
 static int cfg_rcv_trunc;
+static int cfg_timeo;
 
 struct cfg_cmsg_types {
 	unsigned int cmsg_enabled:1;
@@ -249,6 +250,30 @@ static void set_mptfo(int fd, int pf)
 		perror("TCP_FASTOPEN");
 }
 
+static int settimeo(int fd, int timeout_ms)
+{
+	struct timeval timeout = { .tv_sec = 3 };
+
+	if (timeout_ms > 0) {
+		timeout.tv_sec = timeout_ms / 1000;
+		timeout.tv_usec = (timeout_ms % 1000) * 1000;
+	}
+
+	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+		       sizeof(timeout))) {
+		perror("set SO_RCVTIMEO");
+		return -1;
+	}
+
+	if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout,
+		       sizeof(timeout))) {
+		perror("set SO_SNDTIMEO");
+		return -1;
+	}
+
+	return 0;
+}
+
 static int do_ulp_so(int sock, const char *name)
 {
 	return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name));
@@ -376,6 +401,9 @@ static int sock_connect_mptcp(const char * const remoteaddr,
 		if (cfg_mark)
 			set_mark(sock, cfg_mark);
 
+		if (cfg_timeo)
+			settimeo(sock, cfg_timeo);
+
 		if (cfg_sockopt_types.mptfo) {
 			if (!winfo->total_len)
 				winfo->total_len = winfo->len = read(infd, winfo->buf,
@@ -1384,7 +1412,7 @@ static void parse_opts(int argc, char **argv)
 {
 	int c;
 
-	while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
+	while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:O:")) != -1) {
 		switch (c) {
 		case 'f':
 			cfg_truncate = atoi(optarg);
@@ -1462,6 +1490,9 @@ static void parse_opts(int argc, char **argv)
 		case 'o':
 			parse_setsock_options(optarg);
 			break;
+		case 'O':
+			cfg_timeo = strtol(optarg, NULL, 0);
+			break;
 		}
 	}
 
-- 
2.43.0
Re: [PATCH mptcp-next 1/4] selftests: mptcp: add cfg_timeo for mptcp_connect
Posted by Matthieu Baerts 3 months ago
Hi Geliang,

On 01/08/2024 11:21, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> This patch adds a new helper settimeo(), which comes from BPF selftests
> network_helpers.c, into MPTCP selftests tool mptcp_connect, to set
> SO_RCVTIMEO and SO_SNDTIMEO options of the given socket.

Can you add a (very) brief description explaining *why* it is
interesting to set these options in our tests?

(e.g. mention why the 'timeout' command we use when launching
'mptcp_connect' is not enough)

> Add a new mptcp_connect option "-O" to pass a timeout value and a new
> cfg cfg_timeo to store this value.

Please also update the 'usage()' helper with this new option.

> Invoke this new helper in sock_connect_mptcp() when cfg_timeo is set.

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.