Skip to contentSkip to conversation

selftests: fixes and improvements

PATCH New Florian Westphal 1 participant 7 messages Oct 14, 2019

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

PATCH v2 1/1 selftests: fixes and improvements

First a couple of resends, rebased on top of current export branch. All commits may be squashed at your discretion.

The last two are new and were not included before.

The commits build on one-another so its possible to take the resends and defer or drop the last ones.

1. increase tc delay variation to cover more scenarios.
3. check accept/getpeername provide expected sockaddr info.
3. add ipv6 support (disabled via ipv6=false in script)
4. randomly disable any combination of gro/tso/gso.
5. allow to run multiple instances of mptcp_connect.sh in parallel.
6. Fix a possible deadlock in the test program where both ends block in write(mptcp_sk, ..).

Comments welcome.

These patches are currently queued in
https://git.breakpoint.cc/cgit/fw/mptcp-next.git/log/?h=sendmsg_refactor_12

and can be fetched via

git://git.breakpoint.cc/fw/mptcp-next.git sendmsg_refactor_12

Beware, it contains further changes -- you will need to toss the extra baggage.

author
#1 lore raw

PATCH v2 1/6 selftests: mptcp: make tc delays random

test with less predictable setups:
tc qdisc delay is now random, same for reordering and loss. Main motivation is to cover more scenarious without a large increase in test-time.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.sh      | 28 +++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.sh +26 -2
index f0528df9ac31..21f884800ca8 100755
@@ -302,8 +302,32 @@ for sender in 1 2 3 4;do
 	do_ping ns4 ns$sender 10.0.3.1
 done
 
-tc -net ns2 qdisc add dev ns2eth3 root netem loss random 1
-tc -net ns3 qdisc add dev ns3eth4 root netem delay 10ms reorder 25% 50% gap 5
+loss=$((RANDOM%101))
+if [ $loss -eq 100 ] ;then
+	loss=1%
+	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+elif [ $loss -ge 10 ]; then
+	loss=0.$loss%
+	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+elif [ $loss -ge 1 ]; then
+	loss=0.0$loss%
+	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+fi
+
+delay=$((RANDOM%1200))
+reorder1=$((RANDOM%99))
+reorder2=$((RANDOM%50))
+gap=$((RANDOM%5))
+if [ $gap -gt 0 ]; then
+	gap="gap $gap"
+else
+	gap=""
+fi
+
+if [ $reorder1 -gt 0 ] && [ $reorder2 -gt 0 ]; then
+  tc -net ns3 qdisc add dev ns3eth4 root netem delay ${delay}ms reorder ${reorder1}% ${reorder2}% $gap
+fi
+echo "INFO: Using loss of $loss, delay $delay ms, reorder: $reorder1, $reorder2 $gap on ns3eth4"
 
 for sender in 1 2 3 4;do
 	run_tests ns1 ns$sender 10.0.1.1
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v2 2/6 selftests: mptcp: add accept/getpeer checks

In reply to Florian Westphal

Check that the result coming from accept matches that of getpeername. For initiator side, check getpeername matches address passed to connect().

At this time, kernel returns the address of the first subflow in the list. Right now, we do not yet implement fate-sharing, i.e. if the original subflow goes away, the result of getpeername would change.

There are different ways to fix that. One would be to copy the quadruple from the first subflow socket to the mptcp socket.

If we do that, this test should catch a possible bug (such as accidental reversal of saddr/daddr).

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.c       | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
tools/testing/selftests/net/mptcp/mptcp_connect.c +107 -0
index 987a8411756e..c9738a8dcc7f 100644
@@ -54,6 +54,21 @@ static const char *getxinfo_strerr(int err)
 	return gai_strerror(err);
 }
 
+static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
+			 char *host, socklen_t hostlen,
+			 char *serv, socklen_t servlen)
+{
+	int flags = NI_NUMERICHOST | NI_NUMERICSERV;
+	int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen, flags);
+
+	if (err) {
+		const char *errstr = getxinfo_strerr(err);
+
+		fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
+		exit(1);
+	}
+}
+
 static void xgetaddrinfo(const char *node, const char *service,
 			 const struct addrinfo *hints,
 			 struct addrinfo **res)
@@ -327,6 +342,94 @@ static int copyfd_io(int infd, int peerfd, int outfd)
 	return 0;
 }
 
+static void check_sockaddr(int pf, struct sockaddr_storage *ss,
+			   socklen_t salen)
+{
+	char addr[INET6_ADDRSTRLEN];
+	char serv[INET6_ADDRSTRLEN];
+	struct sockaddr_in6 *sin6;
+	struct sockaddr_in *sin;
+	int wanted_size = 0;
+
+	switch (pf) {
+	case AF_INET:
+		wanted_size = sizeof(*sin);
+		sin = (void *)ss;
+		if (!sin->sin_port)
+			fprintf(stderr, "accept: something wrong: ip connection from port 0");
+		break;
+	case AF_INET6:
+		wanted_size = sizeof(*sin6);
+		sin6 = (void *)ss;
+		if (!sin6->sin6_port)
+			fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
+		break;
+	default:
+		fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
+		return;
+	}
+
+	if (salen != wanted_size)
+		fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
+				(int)salen, wanted_size);
+
+	if (ss->ss_family != pf)
+		fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
+				(int)ss->ss_family, pf);
+}
+
+static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
+{
+	struct sockaddr_storage peerss;
+	socklen_t peersalen = sizeof(peerss);
+
+	if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
+		perror("getpeername");
+		return;
+	}
+
+	if (peersalen != salen) {
+		fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
+		return;
+	}
+
+	if (memcmp(ss, &peerss, peersalen)) {
+		char a[INET6_ADDRSTRLEN];
+		char b[INET6_ADDRSTRLEN];
+		char c[INET6_ADDRSTRLEN];
+		char d[INET6_ADDRSTRLEN];
+
+		xgetnameinfo((struct sockaddr *)ss, salen,
+			     a, sizeof(a), b, sizeof(b));
+
+		xgetnameinfo((struct sockaddr *)&peerss, peersalen,
+			     c, sizeof(c), d, sizeof(d));
+
+		fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
+			__func__, a, c, b, d, peersalen, salen);
+	}
+}
+
+static void check_getpeername_connect(int fd)
+{
+	struct sockaddr_storage ss;
+	socklen_t salen = sizeof(ss);
+	char a[INET6_ADDRSTRLEN];
+	char b[INET6_ADDRSTRLEN];
+
+	if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
+		perror("getpeername");
+		return;
+	}
+
+	xgetnameinfo((struct sockaddr *)&ss, salen,
+		     a, sizeof(a), b, sizeof(b));
+
+	if (strcmp(cfg_host, a) || strcmp(cfg_port, b))
+		fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
+			cfg_host, a, cfg_port, b);
+}
+
 int main_loop_s(int listensock)
 {
 	struct sockaddr_storage ss;
@@ -350,6 +453,9 @@ int main_loop_s(int listensock)
 	salen = sizeof(ss);
 	remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
 	if (remotesock >= 0) {
+		check_sockaddr(pf, &ss, salen);
+		check_getpeername(remotesock, &ss, salen);
+
 		copyfd_io(0, remotesock, 1);
 		return 0;
 	}
@@ -384,6 +490,7 @@ int main_loop(void)
 	if (fd < 0)
 		return 2;
 
+	check_getpeername_connect(fd);
 	return copyfd_io(0, fd, 1);
 }
 
Signature · 1 line
2.21.0
author
#3 lore raw

PATCH v2 3/6 selftests: mptcp: extend mptcp_connect tool for ipv6 family

In reply to Florian Westphal

At this time socket() will fail when requesting an ipv6 mptcp socket. This is ok for now, as the test script won't request ipv6 tests yet.

Once someone starts to implement mptcp v6 support, just set ipv6=true in the script and the selftest will attempt to connect via ipv6.

Tested with a tcp <-> tcp connection.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.c       | 16 ++--
 .../selftests/net/mptcp/mptcp_connect.sh      | 77 ++++++++++++++++---
 2 files changed, 77 insertions(+), 16 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +11 -5
index c9738a8dcc7f..d81c94448686 100644
@@ -37,11 +37,11 @@ static const char *cfg_host;
 static const char *cfg_port	= "12000";
 static int cfg_sock_proto	= IPPROTO_MPTCP;
 static bool tcpulp_audit;
-
+static int pf = AF_INET;
 
 static void die_usage(void)
 {
-	fprintf(stderr, "Usage: mptcp_connect [-u] [-s MPTCP|TCP] [-p port] "
+	fprintf(stderr, "Usage: mptcp_connect [-6] [-u] [-s MPTCP|TCP] [-p port] "
 		"[ -l ] [ -t timeout ] connect_address\n");
 	exit(1);
 }
@@ -94,12 +94,13 @@ static int sock_listen_mptcp(const char * const listenaddr,
 		.ai_flags = AI_PASSIVE | AI_NUMERICHOST
 	};
 
-	hints.ai_family = AF_INET;
+	hints.ai_family = pf;
 
 	struct addrinfo *a, *addr;
 	int one = 1;
 
 	xgetaddrinfo(listenaddr, port, &hints, &addr);
+	hints.ai_family = pf;
 
 	for (a = addr; a; a = a->ai_next) {
 		sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
@@ -182,7 +183,7 @@ static int sock_connect_mptcp(const char * const remoteaddr,
 	struct addrinfo *a, *addr;
 	int sock = -1;
 
-	hints.ai_family = AF_INET;
+	hints.ai_family = pf;
 
 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
 	for (a = addr; a; a = a->ai_next) {
@@ -512,7 +513,7 @@ static void parse_opts(int argc, char **argv)
 {
 	int c;
 
-	while ((c = getopt(argc, argv, "lp:s:hut:")) != -1) {
+	while ((c = getopt(argc, argv, "6lp:s:hut:")) != -1) {
 		switch (c) {
 		case 'l':
 			listen_mode = true;
@@ -528,6 +529,8 @@ static void parse_opts(int argc, char **argv)
 			break;
 		case 'u':
 			tcpulp_audit = true;
+		case '6':
+			pf = AF_INET6;
 			break;
 		case 't':
 			poll_timeout = atoi(optarg) * 1000;
@@ -540,6 +543,9 @@ static void parse_opts(int argc, char **argv)
 	if (optind + 1 != argc)
 		die_usage();
 	cfg_host = argv[optind];
+
+	if (strchr(cfg_host, ':'))
+		pf = AF_INET6;
 }
 
 int main(int argc, char *argv[])
tools/testing/selftests/net/mptcp/mptcp_connect.sh +66 -11

175 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#4 lore raw

PATCH v2 4/6 selftests: mptcp: random ethtool tweaking

In reply to Florian Westphal

Instead of unconditionally disabling TSO in ns3, turn off any of gso/tso/gro in ns3 and/or ns4.

This gets us various combinations of GRO/GSO/TSO without a large impact on test time.

The old approach is already not deterministic, and cannot possibly be made so. For protocol tests mptcp packetdrill is a better fit.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.sh      | 29 ++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/mptcp_connect.sh +28 -1
index dd7272d30b20..10655c6d0f62 100755
@@ -91,7 +91,6 @@ $ipv6 && ip -net ns3 addr add dead:beef:3::2/64 dev ns3eth4
 ip -net ns3 link set ns3eth4 up
 ip -net ns3 route add default via 10.0.2.1
 $ipv6 && ip -net ns3 route add default via dead:beef:2::1
-ip netns exec ns3 ethtool -K ns3eth2 tso off 2>/dev/null
 ip netns exec ns3 sysctl -q net.ipv4.ip_forward=1
 $ipv6 && ip netns exec ns3 sysctl -q net.ipv6.conf.all.forwarding=1
 
@@ -101,6 +100,34 @@ ip -net ns4 link set ns4eth3 up
 ip -net ns4 route add default via 10.0.3.2
 $ipv6 && ip -net ns4 route add default via dead:beef:3::2
 
+set_ethtool_flags() {
+	ns=$1
+	dev=$2
+
+	r=$RANDOM
+
+	pick1=$((r & 1))
+	r=$((r>>1))
+	pick2=$((r & 1))
+	r=$((r>>1))
+	pick3=$((r & 1))
+
+	comma=""
+	flags=""
+	[ $pick1 -ne 0 ] && flags="tso"
+	[ $pick1 -ne 0 ] && comma=","
+	[ $pick2 -ne 0 ] && flags=${flags}${comma}"gso"
+	[ $pick3 -ne 0 ] && flags=${flags}${comma}"gro"
+
+	[ -z $flags ] && return
+
+	ip netns exec $ns ethtool -K $dev $flags off 2>/dev/null
+	[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags off"
+}
+
+set_ethtool_flags ns3 ns3eth2
+set_ethtool_flags ns4 ns4eth3
+
 print_file_err()
 {
 	ls -l "$1" 1>&2
Signature · 1 line
2.21.0
author
#5 lore raw

PATCH 5/6 selftests: allow parallel execution

In reply to Florian Westphal

This allows to run multiple instances of the test script in parallel on the same host.

Should it make easier to spot e.g. resource leaks or race conditions.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.sh      | 163 +++++++++---------
 1 file changed, 85 insertions(+), 78 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.sh +85 -78
index 10655c6d0f62..3a1ddbdbbaf0 100755
@@ -11,6 +11,13 @@ capture=0
 timeout=30
 ipv6=false
 
+sec=$(date +%s)
+rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
+ns1="ns1-$rndh"
+ns2="ns2-$rndh"
+ns3="ns3-$rndh"
+ns4="ns4-$rndh"
+
 TEST_COUNT=0
 
 cleanup()
@@ -19,8 +26,8 @@ cleanup()
 	rm -f "$sin" "$sout"
 	rm -f "$capout"
 
-	for i in 1 2 3 4; do
-		ip netns del ns$i
+	for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
+		ip netns del $netns
 	done
 }
 
@@ -43,62 +50,62 @@ cout=$(mktemp)
 capout=$(mktemp)
 trap cleanup EXIT
 
-for i in 1 2 3 4;do
-	ip netns add ns$i || exit $ksft_skip
-	ip -net ns$i link set lo up
+for i in "$ns1" "$ns2" "$ns3" "$ns4";do
+	ip netns add $i || exit $ksft_skip
+	ip -net $i link set lo up
 done
 
-#  ns1              ns2                    ns3                     ns4
+#  "$ns1"              ns2                    ns3                     ns4
 # ns1eth2    ns2eth1   ns2eth3      ns3eth2   ns3eth4       ns4eth3
 #                           - drop 1% ->            reorder 25%
 #                           <- TSO off -
 
-ip link add ns1eth2 netns ns1 type veth peer name ns2eth1 netns ns2
-ip link add ns2eth3 netns ns2 type veth peer name ns3eth2 netns ns3
-ip link add ns3eth4 netns ns3 type veth peer name ns4eth3 netns ns4
+ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
+ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth2 netns "$ns3"
+ip link add ns3eth4 netns "$ns3" type veth peer name ns4eth3 netns "$ns4"
 
-ip -net ns1 addr add 10.0.1.1/24 dev ns1eth2
+ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
 if $ipv6 ; then
-	ip -net ns1 addr add dead:beef:1::1/64 dev ns1eth2
+	ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2
 	if [ $? -ne 0 ] ;then
 		echo "SKIP: Can't add ipv6 address, skip ipv6 tests" 1>&2
 		ipv6=false
 	fi
 fi
 
-ip -net ns1 link set ns1eth2 up
-ip -net ns1 route add default via 10.0.1.2
-$ipv6 && ip -net ns1 route add default via dead:beef:1::2
-
-ip -net ns2 addr add 10.0.1.2/24 dev ns2eth1
-$ipv6 && ip -net ns2 addr add dead:beef:1::2/64 dev ns2eth1
-ip -net ns2 link set ns2eth1 up
-
-ip -net ns2 addr add 10.0.2.1/24 dev ns2eth3
-$ipv6 && ip -net ns2 addr add dead:beef:2::1/64 dev ns2eth3
-ip -net ns2 link set ns2eth3 up
-ip -net ns2 route add default via 10.0.2.2
-$ipv6 && ip -net ns2 route add default via dead:beef:2::2
-ip netns exec ns2 sysctl -q net.ipv4.ip_forward=1
-$ipv6 && ip netns exec ns2 sysctl -q net.ipv6.conf.all.forwarding=1
-
-ip -net ns3 addr add 10.0.2.2/24 dev ns3eth2
-$ipv6 && ip -net ns3 addr add dead:beef:2::2/64 dev ns3eth2
-ip -net ns3 link set ns3eth2 up
-
-ip -net ns3 addr add 10.0.3.2/24 dev ns3eth4
-$ipv6 && ip -net ns3 addr add dead:beef:3::2/64 dev ns3eth4
-ip -net ns3 link set ns3eth4 up
-ip -net ns3 route add default via 10.0.2.1
-$ipv6 && ip -net ns3 route add default via dead:beef:2::1
-ip netns exec ns3 sysctl -q net.ipv4.ip_forward=1
-$ipv6 && ip netns exec ns3 sysctl -q net.ipv6.conf.all.forwarding=1
-
-ip -net ns4 addr add 10.0.3.1/24 dev ns4eth3
-$ipv6 && ip -net ns4 addr add dead:beef:3::1/64 dev ns4eth3
-ip -net ns4 link set ns4eth3 up
-ip -net ns4 route add default via 10.0.3.2
-$ipv6 && ip -net ns4 route add default via dead:beef:3::2
+ip -net "$ns1" link set ns1eth2 up
+ip -net "$ns1" route add default via 10.0.1.2
+$ipv6 && ip -net "$ns1" route add default via dead:beef:1::2
+
+ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
+$ipv6 && ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1
+ip -net "$ns2" link set ns2eth1 up
+
+ip -net "$ns2" addr add 10.0.2.1/24 dev ns2eth3
+$ipv6 && ip -net "$ns2" addr add dead:beef:2::1/64 dev ns2eth3
+ip -net "$ns2" link set ns2eth3 up
+ip -net "$ns2" route add default via 10.0.2.2
+$ipv6 && ip -net "$ns2" route add default via dead:beef:2::2
+ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
+$ipv6 && ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
+
+ip -net "$ns3" addr add 10.0.2.2/24 dev ns3eth2
+$ipv6 && ip -net "$ns3" addr add dead:beef:2::2/64 dev ns3eth2
+ip -net "$ns3" link set ns3eth2 up
+
+ip -net "$ns3" addr add 10.0.3.2/24 dev ns3eth4
+$ipv6 && ip -net "$ns3" addr add dead:beef:3::2/64 dev ns3eth4
+ip -net "$ns3" link set ns3eth4 up
+ip -net "$ns3" route add default via 10.0.2.1
+$ipv6 && ip -net "$ns3" route add default via dead:beef:2::1
+ip netns exec "$ns3" sysctl -q net.ipv4.ip_forward=1
+$ipv6 && ip netns exec "$ns3" sysctl -q net.ipv6.conf.all.forwarding=1
+
+ip -net "$ns4" addr add 10.0.3.1/24 dev ns4eth3
+$ipv6 && ip -net "$ns4" addr add dead:beef:3::1/64 dev ns4eth3
+ip -net "$ns4" link set ns4eth3 up
+ip -net "$ns4" route add default via 10.0.3.2
+$ipv6 && ip -net "$ns4" route add default via dead:beef:3::2
 
 set_ethtool_flags() {
 	ns=$1
@@ -125,8 +132,8 @@ set_ethtool_flags() {
 	[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags off"
 }
 
-set_ethtool_flags ns3 ns3eth2
-set_ethtool_flags ns4 ns4eth3
+set_ethtool_flags "$ns3" ns3eth2
+set_ethtool_flags "$ns4" ns4eth3
 
 print_file_err()
 {
@@ -155,7 +162,7 @@ check_transfer()
 
 check_mptcp_disabled()
 {
-	disabled_ns="ns_disabled"
+	disabled_ns="ns_disabled-$sech-$(mktemp -u XXXXXX)"
 	ip netns add ${disabled_ns} || exit $ksft_skip
 
 	# net.mptcp.enabled should be enabled by default
@@ -183,7 +190,7 @@ check_mptcp_disabled()
 
 check_mptcp_ulp_setsockopt()
 {
-	local t="ns_ulp" retval=
+	local t="ns_ulp-$sech-$(mktemp -u XXXXXX)" retval=
 
 	ip netns add ${t} || exit $ksft_skip
 	if ! ip netns exec ${t} ./mptcp_connect -u -p 10000 -s TCP 127.0.0.1 2>&1; then
@@ -348,40 +355,40 @@ check_mptcp_ulp_setsockopt
 # Allow DAD to finish
 $ipv6 && sleep 2
 
-for sender in 1 2 3 4;do
-	do_ping ns1 ns$sender 10.0.1.1
+for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
+	do_ping "$ns1" $sender 10.0.1.1
 	if $ipv6;then
-		do_ping ns1 ns$sender dead:beef:1::1
+		do_ping "$ns1" $sender dead:beef:1::1
 		if [ $? -ne 0 ]; then
 			echo "SKIP: IPv6 tests" 2>&1
 			ipv6=false
 		fi
 	fi
 
-	do_ping ns2 ns$sender 10.0.1.2
-	$ipv6 && do_ping ns2 ns$sender dead:beef:1::2
-	do_ping ns2 ns$sender 10.0.2.1
-	$ipv6 && do_ping ns2 ns$sender dead:beef:2::1
+	do_ping "$ns2" $sender 10.0.1.2
+	$ipv6 && do_ping "$ns2" $sender dead:beef:1::2
+	do_ping "$ns2" $sender 10.0.2.1
+	$ipv6 && do_ping "$ns2" $sender dead:beef:2::1
 
-	do_ping ns3 ns$sender 10.0.2.2
-	$ipv6 && do_ping ns3 ns$sender dead:beef:2::2
-	do_ping ns3 ns$sender 10.0.3.2
-	$ipv6 && do_ping ns3 ns$sender dead:beef:3::2
+	do_ping "$ns3" $sender 10.0.2.2
+	$ipv6 && do_ping "$ns3" $sender dead:beef:2::2
+	do_ping "$ns3" $sender 10.0.3.2
+	$ipv6 && do_ping "$ns3" $sender dead:beef:3::2
 
-	do_ping ns4 ns$sender 10.0.3.1
-	$ipv6 && do_ping ns4 ns$sender dead:beef:3::1
+	do_ping ns4 $sender 10.0.3.1
+	$ipv6 && do_ping "$ns4" $sender dead:beef:3::1
 done
 
 loss=$((RANDOM%101))
 if [ $loss -eq 100 ] ;then
 	loss=1%
-	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+	tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $loss
 elif [ $loss -ge 10 ]; then
 	loss=0.$loss%
-	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+	tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $loss
 elif [ $loss -ge 1 ]; then
 	loss=0.0$loss%
-	tc -net ns2 qdisc add dev ns2eth3 root netem loss random $loss
+	tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $loss
 fi
 
 delay=$((RANDOM%1200))
@@ -395,32 +402,32 @@ else
 fi
 
 if [ $reorder1 -gt 0 ] && [ $reorder2 -gt 0 ]; then
-  tc -net ns3 qdisc add dev ns3eth4 root netem delay ${delay}ms reorder ${reorder1}% ${reorder2}% $gap
+  tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${delay}ms reorder ${reorder1}% ${reorder2}% $gap
 fi
 echo "INFO: Using loss of $loss, delay $delay ms, reorder: $reorder1, $reorder2 $gap on ns3eth4"
 
-for sender in 1 2 3 4;do
-	run_tests ns1 ns$sender 10.0.1.1 0.0.0.0
+for sender in $ns1 $ns2 $ns3 $ns4;do
+	run_tests "$ns1" $sender 10.0.1.1 0.0.0.0
 	if $ipv6;then
-		run_tests ns1 ns$sender dead:beef:1::1 ::
+		run_tests "$ns1" $sender dead:beef:1::1 ::
 		if [ $? -ne 0 ] ;then
 			echo "SKIP: IPv6 tests" 2>&1
 			ipv6=false
 		fi
 	fi
 
-	run_tests ns2 ns$sender 10.0.1.2 0.0.0.0
-	$ipv6 && run_tests ns2 ns$sender dead:beef:1::2 ::
-	run_tests ns2 ns$sender 10.0.2.1 0.0.0.0
-	$ipv6 && run_tests ns2 ns$sender dead:beef:2::1 ::
+	run_tests "$ns2" $sender 10.0.1.2 0.0.0.0
+	$ipv6 && run_tests "$ns2" $sender dead:beef:1::2 ::
+	run_tests "$ns2" $sender 10.0.2.1 0.0.0.0
+	$ipv6 && run_tests "$ns2" $sender dead:beef:2::1 ::
 
-	run_tests ns3 ns$sender 10.0.2.2 0.0.0.0
-	$ipv6 && run_tests ns3 ns$sender dead:beef:2::2 ::
-	run_tests ns3 ns$sender 10.0.3.2 0.0.0.0
-	$ipv6 && run_tests ns3 ns$sender dead:beef:3::2 ::
+	run_tests "$ns3" $sender 10.0.2.2 0.0.0.0
+	$ipv6 && run_tests "$ns3" $sender dead:beef:2::2 ::
+	run_tests "$ns3" $sender 10.0.3.2 0.0.0.0
+	$ipv6 && run_tests "$ns3" $sender dead:beef:3::2 ::
 
-	run_tests ns4 ns$sender 10.0.3.1 0.0.0.0
-	$ipv6 && run_tests ns4 ns$sender dead:beef:3::1 ::
+	run_tests "$ns4" $sender 10.0.3.1 0.0.0.0
+	$ipv6 && run_tests "$ns4" $sender dead:beef:3::1 ::
 done
 
 exit $ret
Signature · 1 line
2.21.0
author
#6 lore raw

PATCH 6/6 selftests: fix spurious deadlock

In reply to Florian Westphal

do_rnd_write() may call write on the mptcp socket repeatedly. As the peerfd is blocking, this prevents the process from draining the receive buffer.

Its unlikely but possible that both sides are blocked in write() on the tcp socket, with both ends announcing a zero window, waiting for read() to drain the socket receive buffer.

An alternative fix would be to make the peerfds nonblocking. This change instead makes it so we call write at most once after poll revents has the POLLOUT bit set.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 .../selftests/net/mptcp/mptcp_connect.c       | 53 ++++++++++---------
 1 file changed, 28 insertions(+), 25 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +28 -25
index d81c94448686..65514bd4a203 100644
@@ -207,27 +207,18 @@ static int sock_connect_mptcp(const char * const remoteaddr,
 
 static size_t do_rnd_write(const int fd, char *buf, const size_t len)
 {
-	size_t offset = 0;
-
-	while (offset < len) {
-		unsigned int do_w;
-		size_t written;
-		ssize_t bw;
+	unsigned int do_w;
+	ssize_t bw;
 
-		do_w = rand() & 0xffff;
-		if (do_w == 0 || do_w > (len - offset))
-			do_w = len - offset;
+	do_w = rand() & 0xffff;
+	if (do_w == 0 || do_w > len)
+		do_w = len;
 
-		bw = write(fd, buf + offset, do_w);
-		if (bw < 0) {
-			perror("write");
-			return 0;
-		}
+	bw = write(fd, buf, do_w);
+	if (bw < 0)
+		perror("write");
 
-		written = (size_t)bw;
-		offset += written;
-	}
-	return offset;
+	return bw;
 }
 
 static size_t do_write(const int fd, char *buf, const size_t len)
@@ -271,9 +262,11 @@ static int copyfd_io(int infd, int peerfd, int outfd)
 		.fd = peerfd,
 		.events = POLLIN | POLLOUT,
 	};
+	unsigned int woff = 0, wlen = 0;
+	char wbuf[8192];
 
 	for (;;) {
-		char buf[8192];
+		char rbuf[8192];
 		ssize_t len;
 
 		if (fds.events == 0)
@@ -293,7 +286,7 @@ static int copyfd_io(int infd, int peerfd, int outfd)
 		}
 
 		if (fds.revents & POLLIN) {
-			len = do_rnd_read(peerfd, buf, sizeof(buf));
+			len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
 			if (len == 0) {
 				/* no more data to receive:
 				 * peer has closed its write side
@@ -310,15 +303,25 @@ static int copyfd_io(int infd, int peerfd, int outfd)
 				return 3;
 			}
 
-			do_write(outfd, buf, len);
+			do_write(outfd, rbuf, len);
 		}
 
 		if (fds.revents & POLLOUT) {
-			len = do_rnd_read(infd, buf, sizeof(buf));
-			if (len > 0) {
-				if (!do_rnd_write(peerfd, buf, len))
+			if (wlen == 0) {
+				woff = 0;
+				wlen = read(infd, wbuf, sizeof(wbuf));
+			}
+
+			if (wlen > 0) {
+				ssize_t bw;
+
+				bw = do_rnd_write(peerfd, wbuf + woff, wlen);
+				if (bw < 0)
 					return 111;
-			} else if (len == 0) {
+
+				woff += bw;
+				wlen -= bw;
+			} else if (wlen == 0) {
 				/* We have no more data to send. */
 				fds.events &= ~POLLOUT;
 
Signature · 1 line
2.21.0