Skip to contentSkip to conversation

selftests: ovpn: enable TCP_NODELAY on TCP sockets

PATCH New Marco Baffo 1 participant 1 message Oct 8

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests: ovpn: enable TCP_NODELAY on TCP sockets

Since OpenVPN 2.7.6, openvpn unconditionally sets TCP_NODELAY on its TCP sockets. Do the same in ovpn-cli so the TCP selftests match that configuration.

Allow OVPN_TCP_NODELAY=0 to keep Nagle enabled for testing sockets used by older OpenVPN configurations. Add a TCP test variant with Nagle enabled so the standard selftest suite exercises both settings.

The TCP peer ID capture assumes that every TCP segment starts with an ovpn length prefix followed by a data header. TCP does not preserve record boundaries, and enabling TCP_NODELAY makes this check unreliable. Restrict the capture-based peer ID check to UDP.

---

Changes in v2:
- Allow ovpn-cli to use Nagle's algorithm through the OVPN_TCP_NODELAY=0 environment variable.
- Add test-tcp-nagle.sh to the selftests

 tools/testing/selftests/net/ovpn/Makefile     |  1 +
 tools/testing/selftests/net/ovpn/common.sh    | 22 ++++----
 tools/testing/selftests/net/ovpn/ovpn-cli.c   | 28 ++++++++++
 .../selftests/net/ovpn/test-tcp-nagle.sh      | 11 ++++
 tools/testing/selftests/net/ovpn/test.sh      | 54 +++++++++----------
 5 files changed, 76 insertions(+), 40 deletions(-)
 create mode 100755 tools/testing/selftests/net/ovpn/test-tcp-nagle.sh
tools/testing/selftests/net/ovpn/Makefile +1 -0
index 169f0464ac3a..35e4c9d4fc68 100644
@@ -42,6 +42,7 @@ TEST_PROGS := \
 	test-symmetric-id-float.sh \
 	test-symmetric-id-tcp.sh \
 	test-symmetric-id.sh \
+	test-tcp-nagle.sh \
 	test-tcp.sh \
 	test.sh \
 # end of TEST_PROGS
tools/testing/selftests/net/ovpn/common.sh +9 -13
index 2d844eb3aa6e..290d6589e027 100644
@@ -17,6 +17,8 @@ OVPN_FLOAT=${OVPN_FLOAT:-0}
 OVPN_SYMMETRIC_ID=${OVPN_SYMMETRIC_ID:-0}
 OVPN_VERBOSE=${OVPN_VERBOSE:-0}
 
+# Set to 0 to exercise TCP sockets with Nagle enabled.
+export OVPN_TCP_NODELAY=${OVPN_TCP_NODELAY:-1}
 export OVPN_ID_OFFSET=$(( 9 * (OVPN_SYMMETRIC_ID == 0) ))
 
 OVPN_JQ_FILTER='map(if type == "array" then .[] else . end) |
@@ -178,20 +180,14 @@ ovpn_setup_ns() {
 
 ovpn_build_capture_filter() {
 	# match the first four bytes of the openvpn data payload
-	if [ "${OVPN_PROTO}" == "UDP" ]; then
-		# For UDP, libpcap transport indexing only works for IPv4, so
-		# use an explicit IPv4 or IPv6 expression based on the peer
-		# address. The IPv6 branch assumes there are no extension
-		# headers in the outer packet.
-		if [[ "${2}" == *:* ]]; then
-			printf "ip6 and ip6[6] = 17 and ip6[48:4] = %s" "${1}"
-		else
-			printf "ip and udp[8:4] = %s" "${1}"
-		fi
+	# For UDP, libpcap transport indexing only works for IPv4, so
+	# use an explicit IPv4 or IPv6 expression based on the peer
+	# address. The IPv6 branch assumes there are no extension
+	# headers in the outer packet.
+	if [[ "${2}" == *:* ]]; then
+		printf "ip6 and ip6[6] = 17 and ip6[48:4] = %s" "${1}"
 	else
-		# openvpn over TCP prepends a 2-byte packet length ahead of the
-		# DATA_V2 opcode, so skip it before matching the payload header
-		printf "ip and tcp[(((tcp[12] & 0xf0) >> 2) + 2):4] = %s" "${1}"
+		printf "ip and udp[8:4] = %s" "${1}"
 	fi
 }
 
tools/testing/selftests/net/ovpn/ovpn-cli.c +28 -0
index f4effa7580c0..30e49d1a5783 100644
@@ -470,6 +470,22 @@ static int ovpn_parse_key_direction(const char *dir, struct ovpn_ctx *ctx)
 	return 0;
 }
 
+static int ovpn_tcp_nodelay(int socket)
+{
+	const char *nodelay = getenv("OVPN_TCP_NODELAY");
+	int opt = 1;
+	int ret;
+
+	if (nodelay && !strcmp(nodelay, "0"))
+		opt = 0;
+
+	ret = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
+	if (ret < 0)
+		perror("setsockopt for TCP_NODELAY");
+
+	return ret;
+}
+
 static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
 {
 	struct sockaddr_storage local_sock = { 0 };
@@ -606,6 +622,12 @@ static int ovpn_accept(struct ovpn_ctx *ctx)
 		goto err;
 	}
 
+	if (ovpn_tcp_nodelay(ret) < 0) {
+		close(ret);
+		ret = -1;
+		goto err;
+	}
+
 	return ret;
 err:
 	close(ctx->socket);
@@ -623,6 +645,10 @@ static int ovpn_connect(struct ovpn_ctx *ovpn)
 		return -1;
 	}
 
+	ret = ovpn_tcp_nodelay(s);
+	if (ret < 0)
+		goto err;
+
 	switch (ovpn->remote.in4.sin_family) {
 	case AF_INET:
 		socklen = sizeof(struct sockaddr_in);
@@ -1779,6 +1805,8 @@ static void usage(const char *cmd)
 
 	fprintf(stderr,
 		"* listen_mcast: listen to ovpn netlink multicast messages\n");
+	fprintf(stderr,
+		"\nEnvironment: OVPN_TCP_NODELAY=0 enables Nagle for listen/connect (default: 1)\n");
 }
 
 static int ovpn_parse_remote(struct ovpn_ctx *ovpn, const char *host,
tools/testing/selftests/net/ovpn/test-tcp-nagle.sh new file +11 -0
new file mode 100755
index 000000000000..e603181dc2ba
@@ -0,0 +1,11 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 OpenVPN, Inc.
+#
+#	Author: Marco Baffo <[email protected]>
+#
+
+OVPN_PROTO="TCP"
+OVPN_TCP_NODELAY=0
+
+source test.sh
tools/testing/selftests/net/ovpn/test.sh +27 -27

70 lines not on this page · raw message

Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel