Skip to contentSkip to conversation

ovpn: use bound device in UDP when available

PATCH In review Antonio Quartulli 2 participants 6 messages May 12 – May 18

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

PATCH 1/5 ovpn: use bound device in UDP when available

From: Ralf Lici <[email protected]>

Use the socket’s bound network interface if it’s explicitly specified via the --bind-dev option in openvpn.

---
 drivers/net/ovpn/udp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ovpn/udp.c +3 -1
index 493a5a0744af..78b9aea06de9 100644
@@ -153,6 +153,7 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,
 		.fl4_dport = bind->remote.in4.sin_port,
 		.flowi4_proto = sk->sk_protocol,
 		.flowi4_mark = sk->sk_mark,
+		.flowi4_oif = sk->sk_bound_dev_if,
 	};
 	int ret;
 
@@ -230,7 +231,8 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,
 		.fl6_dport = bind->remote.in6.sin6_port,
 		.flowi6_proto = sk->sk_protocol,
 		.flowi6_mark = sk->sk_mark,
-		.flowi6_oif = bind->remote.in6.sin6_scope_id,
+		.flowi6_oif = sk->sk_bound_dev_if ?:
+				      bind->remote.in6.sin6_scope_id,
 	};
 
 	local_bh_disable();
Signature · 8 lines
2.53.0



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

PATCH 2/5 selftests: ovpn: add test for bound device

From: Ralf Lici <[email protected]>

Add a selftest to verify that when a UDP socket is bound to a device, ovpn traffic is transmitted and received only through the bound interface.

The test sets up a P2P session between two peers in separate network namespaces connected by two veth pairs. It binds each side to both veth interfaces in turn and uses tcpdump to verify the selected underlay path.

It also checks negative cases where the route-selected egress device or the ingress device does not match the configured bind device, ensuring that mismatched traffic does not complete the tunnel exchange.

---
 tools/testing/selftests/net/ovpn/Makefile     |   1 +
 tools/testing/selftests/net/ovpn/common.sh    |  13 +-
 tools/testing/selftests/net/ovpn/ovpn-cli.c   |  49 ++-
 tools/testing/selftests/net/ovpn/test-bind.sh | 281 ++++++++++++++++++
 tools/testing/selftests/net/ovpn/test-mark.sh |   4 +-
 5 files changed, 324 insertions(+), 24 deletions(-)
 create mode 100755 tools/testing/selftests/net/ovpn/test-bind.sh
tools/testing/selftests/net/ovpn/Makefile +1 -0
index 169f0464ac3a..5c70cac0a95b 100644
@@ -33,6 +33,7 @@ TEST_FILES = \
 # end of TEST_FILES
 
 TEST_PROGS := \
+	test-bind.sh \
 	test-chachapoly.sh \
 	test-close-socket-tcp.sh \
 	test-close-socket.sh \
tools/testing/selftests/net/ovpn/common.sh +7 -6
index 2d844eb3aa6e..06ce298b6e0e 100644
@@ -213,12 +213,13 @@ ovpn_add_peer() {
 	local peer_ns
 	local server_ns="ovpn_peer0"
 	M_ID=${labels[OVPN_SYMMETRIC_ID]}
+	local dev=${2:-"any"}
 
 	if [ "${OVPN_PROTO}" == "UDP" ]; then
 		if [ ${1} -eq 0 ]; then
-			ip netns exec "${server_ns}" ${OVPN_CLI} \
-				new_multi_peer tun0 1 ${M_ID} \
-				${OVPN_UDP_PEERS_FILE}
+			ip netns exec "${server_ns}" "${OVPN_CLI}" \
+				new_multi_peer tun0 "${dev}" 1 "${M_ID}" \
+				"${OVPN_UDP_PEERS_FILE}"
 
 			for p in $(seq 1 ${OVPN_NUM_PEERS}); do
 				ip netns exec "${server_ns}" ${OVPN_CLI} \
@@ -241,9 +242,9 @@ ovpn_add_peer() {
 				${OVPN_UDP_PEERS_FILE})
 			LPORT=$(awk "NR == ${1} {print \$6}" \
 				${OVPN_UDP_PEERS_FILE})
-			ip netns exec "${peer_ns}" ${OVPN_CLI} new_peer \
-				tun${1} ${PEER_ID} ${TX_ID} ${LPORT} ${RADDR} \
-				${RPORT}
+			ip netns exec "${peer_ns}" "${OVPN_CLI}" new_peer \
+				tun"${1}" "${dev}" "${PEER_ID}" "${TX_ID}" \
+				"${LPORT}" "${RADDR}" "${RPORT}"
 			ip netns exec "${peer_ns}" ${OVPN_CLI} new_key tun${1} \
 				${PEER_ID} 1 0 ${OVPN_ALG} 1 data64.key
 		fi
tools/testing/selftests/net/ovpn/ovpn-cli.c +33 -16

109 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-bind.sh new file +281 -0

281 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-mark.sh +2 -2
index 5a8f47554286..010f5b44dbf4 100755
@@ -38,8 +38,8 @@ ovpn_mark_prepare_network() {
 	done
 
 	ovpn_cmd_ok "create server-side multi-peer with fwmark" \
-		ip netns exec ovpn_peer0 "${OVPN_CLI}" new_multi_peer tun0 1 \
-			ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
+		ip netns exec ovpn_peer0 "${OVPN_CLI}" new_multi_peer tun0 \
+			any 1 ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
 	for p in $(seq 1 3); do
 		ovpn_cmd_ok "install server key for peer ${p}" \
 			ip netns exec ovpn_peer0 "${OVPN_CLI}" new_key tun0 \
Signature · 8 lines
2.53.0



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

PATCH 3/5 ovpn: use bound address in UDP when available

In reply to Antonio Quartulli

From: Ralf Lici <[email protected]>

Use the socket's locally bound address if it's explicitly specified via the --local option in openvpn.

---
 drivers/net/ovpn/udp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ovpn/udp.c +4 -2
index 78b9aea06de9..b5d1ca014732 100644
@@ -147,7 +147,7 @@ static int ovpn_udp4_output(struct ovpn_peer *peer, struct ovpn_bind *bind,
 {
 	struct rtable *rt;
 	struct flowi4 fl = {
-		.saddr = bind->local.ipv4.s_addr,
+		.saddr = inet_sk(sk)->inet_rcv_saddr ?: bind->local.ipv4.s_addr,
 		.daddr = bind->remote.in4.sin_addr.s_addr,
 		.fl4_sport = inet_sk(sk)->inet_sport,
 		.fl4_dport = bind->remote.in4.sin_port,
@@ -225,7 +225,9 @@ static int ovpn_udp6_output(struct ovpn_peer *peer, struct ovpn_bind *bind,
 	int ret;
 
 	struct flowi6 fl = {
-		.saddr = bind->local.ipv6,
+		.saddr = ipv6_addr_any(&sk->sk_v6_rcv_saddr) ?
+				 bind->local.ipv6 :
+				 sk->sk_v6_rcv_saddr,
 		.daddr = bind->remote.in6.sin6_addr,
 		.fl6_sport = inet_sk(sk)->inet_sport,
 		.fl6_dport = bind->remote.in6.sin6_port,
Signature · 8 lines
2.53.0



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

PATCH 4/5 selftests: ovpn: add test for bound address

In reply to Antonio Quartulli

From: Ralf Lici <[email protected]>

Extend the bound socket selftest to cover UDP sockets bound to a local address.

The address variant verifies that peer1 transmits ovpn data with the configured local address as the outer source address, even when routing selects the other underlay device. It also checks peer2 receive-side binding by accepting packets sent to the configured local address on both underlay paths.

---
 tools/testing/selftests/net/ovpn/Makefile     |   1 +
 tools/testing/selftests/net/ovpn/common.sh    |   7 +-
 tools/testing/selftests/net/ovpn/ovpn-cli.c   | 212 +++++++++---------
 .../selftests/net/ovpn/test-bind-addr.sh      |  10 +
 tools/testing/selftests/net/ovpn/test-bind.sh | 185 +++++++++++----
 tools/testing/selftests/net/ovpn/test-mark.sh |   2 +-
 6 files changed, 270 insertions(+), 147 deletions(-)
 create mode 100755 tools/testing/selftests/net/ovpn/test-bind-addr.sh
tools/testing/selftests/net/ovpn/Makefile +1 -0
index 5c70cac0a95b..453bee34ea08 100644
@@ -33,6 +33,7 @@ TEST_FILES = \
 # end of TEST_FILES
 
 TEST_PROGS := \
+	test-bind-addr.sh \
 	test-bind.sh \
 	test-chachapoly.sh \
 	test-close-socket-tcp.sh \
tools/testing/selftests/net/ovpn/common.sh +4 -3
index 06ce298b6e0e..a51186cbb1dd 100644
@@ -214,12 +214,13 @@ ovpn_add_peer() {
 	local server_ns="ovpn_peer0"
 	M_ID=${labels[OVPN_SYMMETRIC_ID]}
 	local dev=${2:-"any"}
+	local laddr=${3:-"any"}
 
 	if [ "${OVPN_PROTO}" == "UDP" ]; then
 		if [ ${1} -eq 0 ]; then
 			ip netns exec "${server_ns}" "${OVPN_CLI}" \
-				new_multi_peer tun0 "${dev}" 1 "${M_ID}" \
-				"${OVPN_UDP_PEERS_FILE}"
+				new_multi_peer tun0 "${dev}" "${laddr}" 1 \
+				"${M_ID}" "${OVPN_UDP_PEERS_FILE}"
 
 			for p in $(seq 1 ${OVPN_NUM_PEERS}); do
 				ip netns exec "${server_ns}" ${OVPN_CLI} \
@@ -244,7 +245,7 @@ ovpn_add_peer() {
 				${OVPN_UDP_PEERS_FILE})
 			ip netns exec "${peer_ns}" "${OVPN_CLI}" new_peer \
 				tun"${1}" "${dev}" "${PEER_ID}" "${TX_ID}" \
-				"${LPORT}" "${RADDR}" "${RPORT}"
+				"${laddr}" "${LPORT}" "${RADDR}" "${RPORT}"
 			ip netns exec "${peer_ns}" ${OVPN_CLI} new_key tun${1} \
 				${PEER_ID} 1 0 ${OVPN_ALG} 1 data64.key
 		fi
tools/testing/selftests/net/ovpn/ovpn-cli.c +110 -102

367 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-bind-addr.sh new file +10 -0
new file mode 100755
index 000000000000..e33a433ceb4b
@@ -0,0 +1,10 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2020-2025 OpenVPN, Inc.
+#
+#	Author:	Ralf Lici <[email protected]>
+#		Antonio Quartulli <[email protected]>
+
+BIND_TYPE="ADDR"
+
+source test-bind.sh
tools/testing/selftests/net/ovpn/test-bind.sh +144 -41

264 lines not on this page · raw message

tools/testing/selftests/net/ovpn/test-mark.sh +1 -1
index 010f5b44dbf4..581d208c18ac 100755
@@ -39,7 +39,7 @@ ovpn_mark_prepare_network() {
 
 	ovpn_cmd_ok "create server-side multi-peer with fwmark" \
 		ip netns exec ovpn_peer0 "${OVPN_CLI}" new_multi_peer tun0 \
-			any 1 ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
+			any any 1 ASYMM "${OVPN_UDP_PEERS_FILE}" "${MARK}"
 	for p in $(seq 1 3); do
 		ovpn_cmd_ok "install server key for peer ${p}" \
 			ip netns exec ovpn_peer0 "${OVPN_CLI}" new_key tun0 \
Signature · 8 lines
2.53.0



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

PATCH 5/5 ovpn: add per-peer NAPI for RX decap and reinject

In reply to Antonio Quartulli

From: Ralf Lici <[email protected]>

Currently ovpn processes received packets in the same softirq context that delivers the skb. If the packet is valid and decapsulated, it is reinjected via a single per-interface gro_cells instance.

To improve parallelism and scaling, add a per-peer NAPI and skb queue for reception and reinjection. On receive, the skb is enqueued to the peer’s queue and the peer NAPI is scheduled. The poll function drains the queue, processes the packet, and reinjects the decapsulated skb into the stack through that peer NAPI.

This is the first patch introducing a more elaborated packet processing pipeline aimed at improving parallelism and scalability.

---
 drivers/net/ovpn/io.c       | 20 +++++--------
 drivers/net/ovpn/main.c     | 20 +------------
 drivers/net/ovpn/ovpnpriv.h |  2 --
 drivers/net/ovpn/peer.c     | 60 +++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/peer.h     |  6 ++++
 drivers/net/ovpn/tcp.c      |  5 +++-
 drivers/net/ovpn/udp.c      |  5 +++-
 7 files changed, 82 insertions(+), 36 deletions(-)
drivers/net/ovpn/io.c +7 -13
index 22c555dd962e..3e6c06b9a5ae 100644
@@ -59,9 +59,6 @@ static bool ovpn_is_keepalive(struct sk_buff *skb)
  */
 static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
 {
-	unsigned int pkt_len;
-	int ret;
-
 	/*
 	 * GSO state from the transport layer is not valid for the tunnel/data
 	 * path. Reset all GSO fields to prevent any further GSO processing
@@ -89,20 +86,17 @@ static void ovpn_netdev_write(struct ovpn_peer *peer, struct sk_buff *skb)
 	skb_reset_transport_header(skb);
 	skb_reset_inner_headers(skb);
 
-	/* cause packet to be "received" by the interface */
-	pkt_len = skb->len;
+	/* update RX stats with the size of decrypted packet */
+	ovpn_peer_stats_increment_rx(&peer->vpn_stats, skb->len);
 	/* we may get here in process context in case of TCP connections,
-	 * therefore we have to disable BHs to ensure gro_cells_receive()
-	 * and dev_dstats_rx_add() do not get corrupted or enter deadlock
+	 * therefore we have to disable BHs to ensure dev_dstats_rx_add()
+	 * does not get corrupted
 	 */
 	local_bh_disable();
-	ret = gro_cells_receive(&peer->ovpn->gro_cells, skb);
-	if (likely(ret == NET_RX_SUCCESS)) {
-		/* update RX stats with the size of decrypted packet */
-		ovpn_peer_stats_increment_rx(&peer->vpn_stats, pkt_len);
-		dev_dstats_rx_add(peer->ovpn->dev, pkt_len);
-	}
+	dev_dstats_rx_add(peer->ovpn->dev, skb->len);
 	local_bh_enable();
+
+	napi_gro_receive(&peer->napi, skb);
 }
 
 void ovpn_decrypt_post(void *data, int ret)
drivers/net/ovpn/main.c +1 -19
index 2e0420febda0..57b743cebc79 100644
@@ -74,30 +74,12 @@ static int ovpn_mp_alloc(struct ovpn_priv *ovpn)
 static int ovpn_net_init(struct net_device *dev)
 {
 	struct ovpn_priv *ovpn = netdev_priv(dev);
-	int err = gro_cells_init(&ovpn->gro_cells, dev);
 
-	if (err < 0)
-		return err;
-
-	err = ovpn_mp_alloc(ovpn);
-	if (err < 0) {
-		gro_cells_destroy(&ovpn->gro_cells);
-		return err;
-	}
-
-	return 0;
-}
-
-static void ovpn_net_uninit(struct net_device *dev)
-{
-	struct ovpn_priv *ovpn = netdev_priv(dev);
-
-	gro_cells_destroy(&ovpn->gro_cells);
+	return ovpn_mp_alloc(ovpn);
 }
 
 static const struct net_device_ops ovpn_netdev_ops = {
 	.ndo_init		= ovpn_net_init,
-	.ndo_uninit		= ovpn_net_uninit,
 	.ndo_start_xmit		= ovpn_net_xmit,
 };
 
drivers/net/ovpn/ovpnpriv.h +0 -2
index 5898f6adada7..703e90d1dafc 100644
@@ -39,7 +39,6 @@ struct ovpn_peer_collection {
  * @lock: protect this object
  * @peers: data structures holding multi-peer references
  * @peer: in P2P mode, this is the only remote peer
- * @gro_cells: pointer to the Generic Receive Offload cell
  * @keepalive_work: struct used to schedule keepalive periodic job
  */
 struct ovpn_priv {
@@ -48,7 +47,6 @@ struct ovpn_priv {
 	spinlock_t lock; /* protect writing to the ovpn_priv object */
 	struct ovpn_peer_collection *peers;
 	struct ovpn_peer __rcu *peer;
-	struct gro_cells gro_cells;
 	struct delayed_work keepalive_work;
 };
 
drivers/net/ovpn/peer.c +60 -0

84 lines not on this page · raw message

drivers/net/ovpn/peer.h +6 -0
index 328401570cba..eb0ec9605ced 100644
@@ -46,6 +46,8 @@
  * @crypto: the crypto configuration (ciphers, keys, etc..)
  * @dst_cache: cache for dst_entry used to send to peer
  * @bind: remote peer binding
+ * @encap_q: queue of encapsulated packets awaiting processing/decapsulation
+ * @napi: NAPI context for handling packet reception
  * @keepalive_interval: seconds after which a new keepalive should be sent
  * @keepalive_xmit_exp: future timestamp when next keepalive should be sent
  * @last_sent: timestamp of the last successfully sent packet
@@ -100,6 +102,8 @@ struct ovpn_peer {
 	struct ovpn_crypto_state crypto;
 	struct dst_cache dst_cache;
 	struct ovpn_bind __rcu *bind;
+	struct sk_buff_head encap_q;
+	struct napi_struct napi;
 	unsigned long keepalive_interval;
 	unsigned long keepalive_xmit_exp;
 	time64_t last_sent;
@@ -154,6 +158,8 @@ void ovpn_peer_hash_vpn_ip(struct ovpn_peer *peer);
 bool ovpn_peer_check_by_src(struct ovpn_priv *ovpn, struct sk_buff *skb,
 			    struct ovpn_peer *peer);
 
+bool ovpn_enqueue_encap(struct ovpn_peer *peer, struct sk_buff *skb);
+
 void ovpn_peer_keepalive_set(struct ovpn_peer *peer, u32 interval, u32 timeout);
 void ovpn_peer_keepalive_work(struct work_struct *work);
 
drivers/net/ovpn/tcp.c +4 -1
index 65054cc84be5..87f9d785f44c 100644
@@ -145,13 +145,16 @@ static void ovpn_tcp_rcv(struct strparser *strp, struct sk_buff *skb)
 	if (WARN_ON(!ovpn_peer_hold(peer)))
 		goto err_nopeer;
 
-	ovpn_recv(peer, skb);
+	if (unlikely(!ovpn_enqueue_encap(peer, skb)))
+		goto drop;
+
 	return;
 err:
 	/* take reference for deferred peer deletion. should never fail */
 	if (WARN_ON(!ovpn_peer_hold(peer)))
 		goto err_nopeer;
 	schedule_work(&peer->tcp.defer_del_work);
+drop:
 	dev_dstats_rx_dropped(peer->ovpn->dev);
 err_nopeer:
 	kfree_skb(skb);
drivers/net/ovpn/udp.c +4 -1
index b5d1ca014732..8adff0b90430 100644
@@ -121,7 +121,10 @@ static int ovpn_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
 
 	/* pop off outer UDP header */
 	__skb_pull(skb, sizeof(struct udphdr));
-	ovpn_recv(peer, skb);
+
+	if (unlikely(!ovpn_enqueue_encap(peer, skb)))
+		goto drop;
+
 	return 0;
 
 drop:
Signature · 8 lines
2.53.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
reviewer
#5 lore raw

Re: [Openvpn-devel] [PATCH ovpn net-next 5/5] ovpn: add per-peer NAPI for RX decap and reinject

Quoted 6 lines · Antonio Quartulli

From: Ralf Lici <[email protected]>

Currently ovpn processes received packets in the same softirq context that delivers the skb. If the packet is valid and decapsulated, it is reinjected via a single per-interface gro_cells instance.

Quoted 8 lines

To improve parallelism and scaling, add a per-peer NAPI and skb queue for reception and reinjection. On receive, the skb is enqueued to the peer's queue and the peer NAPI is scheduled. The poll function drains the queue, processes the packet, and reinjects the decapsulated skb into the stack through that peer NAPI.

This is the first patch introducing a more elaborated packet processing pipeline aimed at improving parallelism and scalability.

[...]

Hi,

we decided to drop this patch from the next respin.

We are working on a broader datapath rework that will cover this optimization more cleanly, so we'll revisit the NAPI part separately once that work is ready.

Thanks,

Signature · 8 lines
Ralf Lici
Mandelbit Srl


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