Skip to contentSkip to conversation

ovpn: encrypt GSO input in place without child skbs

PATCH New Ralf Lici 1 participant 6 messages Nov 13

Subsystems (3)
lore
Share & tools
lore

Ralf Lici

author reviewer
#0 lore raw

cover ovpn: encrypt GSO input in place without child skbs

Hi,

This series reworks how ovpn turns GSO input into encrypted UDP GSO aggregates. It builds on two series still under review, which are its prerequisites:

- ovpn: preserve GSO batching (v5)
https://lore.kernel.org/openvpn-devel/[email protected]/
- ovpn: construct and process GRO batches (v3)
https://lore.kernel.org/openvpn-devel/[email protected]/

So far, a GSO skb is segmented in software, which also computes every segment's L4 checksum, and each segment is then encrypted out of place into the aggregate. Patch 4 instead copies each segment into its final position in the aggregate, computes the checksum during that copy and encrypts the record in place. This replaces the out-of-place AEAD path, so in-place encryption becomes the only UDP GSO encryption path. Patch 5 then skips the child skbs altogether for TCP and UDP GSO input which tso_build_hdr supports: each record's payload is copied straight from the original skb, and its headers are synthesized in front of it. Patches 2 and 3 prepare for this without functional change, moving the batching code to gso.c and letting it work on a generic record source.

Patch 1 adds a small helper to include/net/tso.h and needs review from the net core maintainers when the series is forwarded to netdev.

Performance was measured with one TCP flow over UDP ovpn with AES-128-GCM, a 1408-byte tunnel MTU and a 1500-byte physical MTU between two directly connected mlx5 ports. Unlike the GSO series, iperf3 used plain send instead of sendfile (-Z), whose shared page-cache frags make the segmenter linearize every segment and would overstate the gain. The sender, the receiver and their interrupts were pinned to separate CPUs. Each 70-second run omitted the first 10 seconds, with a 15-second perf sample. Three runs per direction were interleaved, and the values below are averaged over both directions:

Before patch 4 After patch 4 After patch 5
Throughput (Gbit/s) 27.31 29.22 30.32

Sender cycles per byte        1.762            1.614           1.193
Sender instructions/byte      4.450            4.349           3.155

The sender saves about 32% of its cycles per byte over the whole series. From patch 4 on, the receiver is the bottleneck and hides most of that in the throughput. With tx-udp-segmentation disabled on the lower device, the sender is the bottleneck throughout, and throughput rises from 17.27 to 17.96 Gbit/s with patch 4 and to 21.72 Gbit/s with patch 5 (+26%).

Without any CPU or IRQ pinning, the series still saves 16% of all cycles per byte on the host for one flow, receiver included, and 12-13% with four parallel flows. The four flows share one outer UDP tuple, and thus one receive queue, so their throughput hardly changes.

Thanks,

Ralf Lici
Mandelbit Srl

Ralf Lici (5):
net: tso: advance header state independently of the data cursor
ovpn: move UDP GSO transmit batching to gso.c
ovpn: batch GSO records from a generic source
ovpn: encrypt GSO segments in place
ovpn: encrypt GSO input without child skbs

 drivers/net/ovpn/Makefile      |   1 +
 drivers/net/ovpn/crypto_aead.c | 117 ++----
 drivers/net/ovpn/crypto_aead.h |  15 +-
 drivers/net/ovpn/gso.c         | 666 +++++++++++++++++++++++++++++++++
 drivers/net/ovpn/gso.h         |  21 ++
 drivers/net/ovpn/io.c          | 289 +++-----------
 drivers/net/ovpn/io.h          |   6 +
 include/net/tso.h              |  15 +
 net/core/tso.c                 |   2 +-
 9 files changed, 791 insertions(+), 341 deletions(-)
 create mode 100644 drivers/net/ovpn/gso.c
 create mode 100644 drivers/net/ovpn/gso.h

base-commit: 7b6ab4dbc156e2f96419715130b3d5738a306c93
prerequisite-message-id: [email protected]
prerequisite-message-id: [email protected]
prerequisite-patch-id: 0cc7597aca91a5984596d971b1b066ad6616c329
prerequisite-patch-id: 7a0446fa01fe78fa5f15ddac7a2b9aedda5018a9
prerequisite-patch-id: 15f724fc646c95ebcd83d22d6a8a2e29237a2f1c
prerequisite-patch-id: 257a9b0fe2bdc44fdff6a91f23bfc6306dd86f23
prerequisite-patch-id: 606b9d70ab683614f73f54ed006b05895ac7f0a8
prerequisite-patch-id: 54225ba7c226f8c804b1158a1d9d9e6ae7eaacdb
prerequisite-patch-id: 083ec87dd028ff34a7e84c04ebd4131e366450b0
prerequisite-patch-id: 09240d50ebf50cbba22a692983d36d8d5bf36280
prerequisite-patch-id: 7142b7587fb766e67d27f6158b0cf8bc2e6067f2
prerequisite-patch-id: 234e55c4cfc35cf9a600b1562337b454877a2479
prerequisite-patch-id: 38a6d5a48ae513c991e6c58ccb9baddb6c3f0f88
prerequisite-patch-id: 422293876a77bf7979b0ba8c81b5c7ac3e00bd82
prerequisite-patch-id: 1129c8986ffbad2d485392c6775daecadd230bce
prerequisite-patch-id: 96ee1b0fe6cde46671ee16887ff39b7ed04a89a3
prerequisite-patch-id: d304d8c678e79b6ab9623a9d541839ec9f5e7959

Signature · 8 lines
2.56.0



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

PATCH 1/5 net: tso: advance header state independently of the data cursor

tso_build_data advances both the TCP sequence number used by the next tso_build_hdr call and a cursor over the payload's virtual addresses. Callers which walk the payload in some other way, such as bnxt_sw_udp_gso_xmit through tso_dma_map, can skip tso_build_data only because UDP headers carry no sequence number, so a TCP user of the same pattern has no way to advance the header state.

Add tso_advance_hdr for that and use it in tso_build_data.

---
 include/net/tso.h | 15 +++++++++++++++
 net/core/tso.c    |  2 +-
 2 files changed, 16 insertions(+), 1 deletion(-)
include/net/tso.h +15 -0
index da82aabd1d48..0a8020274882 100644
@@ -18,6 +18,21 @@ struct tso_t {
 	u32	tcp_seq;
 };
 
+/**
+ * tso_advance_hdr - advance the header state past a segment's payload
+ * @tso: TSO state initialized by tso_start
+ * @size: payload length of the segment just built
+ *
+ * tso_build_data does this while also moving its cursor over the payload's
+ * virtual addresses. Callers which walk the payload in some other way, for
+ * example through tso_dma_map, use this to keep the TCP sequence number of
+ * the following headers correct.
+ */
+static inline void tso_advance_hdr(struct tso_t *tso, int size)
+{
+	tso->tcp_seq += size; /* not worth avoiding this operation for UDP */
+}
+
 /* Calculate the worst case buffer count */
 static inline int tso_count_descs(const struct sk_buff *skb)
 {
net/core/tso.c +1 -1
index d2934bcfa795..c091bee795a7 100644
@@ -47,7 +47,7 @@ EXPORT_SYMBOL(tso_build_hdr);
 
 void tso_build_data(const struct sk_buff *skb, struct tso_t *tso, int size)
 {
-	tso->tcp_seq += size; /* not worth avoiding this operation for UDP */
+	tso_advance_hdr(tso, size);
 	tso->size -= size;
 	tso->data += size;
 
Signature · 8 lines
2.56.0



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

PATCH 2/5 ovpn: move UDP GSO transmit batching to gso.c

In reply to Ralf Lici

The UDP GSO transmit path makes up almost half of io.c and is about to grow further. Move it to its own file, giving its functions an ovpn_gso_ prefix, and expose the helpers it shares with the ordinary transmit path through io.h. The receive side stays in udp.c, as UDP GRO is set up through the socket's encapsulation callbacks.

No functional change.

---
 drivers/net/ovpn/Makefile |   1 +
 drivers/net/ovpn/gso.c    | 262 ++++++++++++++++++++++++++++++++++++++
 drivers/net/ovpn/gso.h    |  20 +++
 drivers/net/ovpn/io.c     | 238 ++--------------------------------
 drivers/net/ovpn/io.h     |   6 +
 5 files changed, 298 insertions(+), 229 deletions(-)
 create mode 100644 drivers/net/ovpn/gso.c
 create mode 100644 drivers/net/ovpn/gso.h
drivers/net/ovpn/Makefile +1 -0
index 229be66167e1..9f993a532d84 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_OVPN) := ovpn.o
 ovpn-y += bind.o
 ovpn-y += crypto.o
 ovpn-y += crypto_aead.o
+ovpn-y += gso.o
 ovpn-y += main.o
 ovpn-y += io.o
 ovpn-y += netlink.o
drivers/net/ovpn/gso.c new file +262 -0

262 lines not on this page · raw message

drivers/net/ovpn/gso.h new file +20 -0
new file mode 100644
index 000000000000..cc7b6b8ce314
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*  OpenVPN data channel offload
+ *
+ *  Copyright (C) 2026 OpenVPN, Inc.
+ *
+ *  Author:	Antonio Quartulli <[email protected]>
+ *		Ralf Lici <[email protected]>
+ */
+
+#ifndef _NET_OVPN_GSO_H_
+#define _NET_OVPN_GSO_H_
+
+#include <linux/skbuff.h>
+
+#include "peer.h"
+
+void ovpn_gso_send(struct sk_buff_head *skbs, struct ovpn_peer *peer);
+bool ovpn_gso_supported(struct ovpn_peer *peer);
+
+#endif /* _NET_OVPN_GSO_H_ */
drivers/net/ovpn/io.c +9 -229

286 lines not on this page · raw message

drivers/net/ovpn/io.h +6 -0
index 1a94f0fda1d1..87a2b5a9e176 100644
@@ -30,4 +30,10 @@ void ovpn_xmit_special(struct ovpn_peer *peer, const void *data,
 void ovpn_encrypt_post(void *data, int ret);
 void ovpn_decrypt_post(void *data, int ret);
 
+struct ovpn_crypto_key_slot *ovpn_encrypt_refs_get(struct ovpn_peer *peer);
+void ovpn_encrypt_key_exhausted(struct ovpn_peer *peer,
+				struct ovpn_crypto_key_slot *ks);
+void ovpn_encrypt_send(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,
+		       struct sk_buff *skb, unsigned int packets);
+
 #endif /* _NET_OVPN_OVPN_H_ */
Signature · 8 lines
2.56.0



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

PATCH 3/5 ovpn: batch GSO records from a generic source

In reply to Ralf Lici

The UDP GSO batching loops work directly on the queue of materialized segments. Let them work on a source which tracks the number of records, the next one and their lengths instead. The segment queue remains the only source, so there is no functional change.

This prepares for records which are ranges of one parent skb.

---
 drivers/net/ovpn/gso.c | 138 ++++++++++++++++++++++++-----------------
 1 file changed, 80 insertions(+), 58 deletions(-)
drivers/net/ovpn/gso.c +80 -58
index e72f835e2a60..432738812548 100644
@@ -32,11 +32,18 @@
 					 sizeof(struct ipv6hdr) -	\
 					 sizeof(struct udphdr) - 1)
 
-static bool ovpn_gso_encrypt_queue(struct sk_buff_head *skbs,
+struct ovpn_gso_source {
+	unsigned int segments;
+	unsigned int next;
+	unsigned int record_len;
+	unsigned int last_record_len;
+	struct sk_buff_head *skbs;
+};
+
+static bool ovpn_gso_encrypt_batch(struct ovpn_gso_source *src,
 				   struct ovpn_peer *peer,
 				   struct ovpn_crypto_key_slot *ks,
-				   struct sk_buff *batch,
-				   unsigned int segments)
+				   struct sk_buff *batch, unsigned int segments)
 {
 	unsigned int offset = 0, moved = 0, i, len;
 	struct ovpn_aead_gso_ctx ctx = { };
@@ -57,7 +64,8 @@ static bool ovpn_gso_encrypt_queue(struct sk_buff_head *skbs,
 	}
 
 	for (i = 0; i < segments; i++) {
-		skb = __skb_dequeue(skbs);
+		skb = __skb_dequeue(src->skbs);
+		src->next++;
 		len = skb->len + OVPN_DATA_V2_OVERHEAD;
 
 		ret = ovpn_aead_encrypt_gso(&ctx, peer, ks, skb, batch, offset,
@@ -162,6 +170,67 @@ static struct sk_buff *ovpn_gso_alloc(const struct sk_buff *first_segment,
 	return gso_skb;
 }
 
+static void ovpn_gso_send_batch(struct ovpn_gso_source *src,
+				struct ovpn_peer *peer)
+{
+	unsigned int max_segs, batch_len, batch_segs, batch_start;
+	struct ovpn_crypto_key_slot *ks;
+	struct sk_buff *batch;
+
+	max_segs = min_t(unsigned int, UDP_MAX_SEGMENTS,
+			 OVPN_GSO_MAX_PAYLOAD /
+				 (src->record_len + OVPN_DATA_V2_OVERHEAD));
+
+	/* if even two encrypted records cannot fit, leave the source for the
+	 * ordinary transmit path
+	 */
+	if (max_segs < 2)
+		return;
+
+	/* An input GSO skb might produce more than max_segs records, so
+	 * consume as many as can fit in each aggregate. A final materialized
+	 * one, or the remainder after a failure, stays for the ordinary path.
+	 */
+	while (src->segments - src->next > 1) {
+		batch_start = src->next;
+		batch_segs = min(src->segments - src->next, max_segs);
+
+		/* acquire shared state before allocating/consuming anything */
+		ks = ovpn_encrypt_refs_get(peer);
+		if (unlikely(!ks))
+			break;
+		/* recheck in case the key was swapped */
+		if (unlikely(ks->async)) {
+			ovpn_crypto_key_slot_put(ks);
+			ovpn_peer_put(peer);
+			break;
+		}
+
+		/* all but the final input record have the same length, so
+		 * start with the full-size calculation and adjust only the
+		 * final group below
+		 */
+		batch_len = batch_segs *
+			    (src->record_len + OVPN_DATA_V2_OVERHEAD);
+		if (batch_start + batch_segs == src->segments)
+			batch_len -= src->record_len - src->last_record_len;
+
+		batch = ovpn_gso_alloc(skb_peek(src->skbs), batch_len,
+				       batch_segs);
+		if (unlikely(!batch)) {
+			ovpn_crypto_key_slot_put(ks);
+			ovpn_peer_put(peer);
+			break;
+		}
+
+		if (unlikely(!ovpn_gso_encrypt_batch(src, peer, ks, batch,
+						     batch_segs))) {
+			kfree_skb(batch);
+			break;
+		}
+	}
+}
+
 /**
  * ovpn_gso_send - encrypt materialized segments into UDP GSO aggregates
  * @skbs: segments of one GSO skb, all but the last of the same length
@@ -174,61 +243,14 @@ static struct sk_buff *ovpn_gso_alloc(const struct sk_buff *first_segment,
  */
 void ovpn_gso_send(struct sk_buff_head *skbs, struct ovpn_peer *peer)
 {
-	unsigned int max_segs, seg_len, batch_len, segs;
-	struct ovpn_crypto_key_slot *ks;
-	struct sk_buff *batch;
+	struct ovpn_gso_source src = {
+		.segments = skb_queue_len(skbs),
+		.record_len = skb_peek(skbs)->len,
+		.last_record_len = skb_peek_tail(skbs)->len,
+		.skbs = skbs,
+	};
 
-	seg_len = skb_peek(skbs)->len + OVPN_DATA_V2_OVERHEAD;
-	max_segs = min_t(unsigned int, UDP_MAX_SEGMENTS,
-			 OVPN_GSO_MAX_PAYLOAD / seg_len);
-
-	/* if even two encrypted skbs cannot fit, leave the whole queue for the
-	 * ordinary transmit path
-	 */
-	if (max_segs < 2)
-		return;
-
-	/* An input GSO skb might produce more than max_segs segments, so we
-	 * consume as many as we can for each iteration. A final single skb, or
-	 * any remainder left after batching stops, stays queued and falls back
-	 * to the ordinary transmit path.
-	 */
-	while (skb_queue_len(skbs) > 1) {
-		segs = min_t(unsigned int, skb_queue_len(skbs), max_segs);
-		/* acquire shared state before allocating/consuming anything */
-		ks = ovpn_encrypt_refs_get(peer);
-		if (unlikely(!ks))
-			return;
-		/* recheck in case the key was swapped */
-		if (unlikely(ks->async)) {
-			ovpn_crypto_key_slot_put(ks);
-			ovpn_peer_put(peer);
-			return;
-		}
-
-		/* all but the final input skb have the same length, so start
-		 * with the full-size calculation and adjust only the final
-		 * group below
-		 */
-		batch_len = segs * (skb_peek(skbs)->len +
-				    OVPN_DATA_V2_OVERHEAD);
-		if (segs == skb_queue_len(skbs))
-			batch_len -= skb_peek(skbs)->len -
-				     skb_peek_tail(skbs)->len;
-
-		batch = ovpn_gso_alloc(skb_peek(skbs), batch_len, segs);
-		if (unlikely(!batch)) {
-			ovpn_crypto_key_slot_put(ks);
-			ovpn_peer_put(peer);
-			return;
-		}
-
-		if (unlikely(!ovpn_gso_encrypt_queue(skbs, peer, ks,
-						     batch, segs))) {
-			kfree_skb(batch);
-			return;
-		}
-	}
+	ovpn_gso_send_batch(&src, peer);
 }
 
 /**
Signature · 8 lines
2.56.0



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

PATCH 4/5 ovpn: encrypt GSO segments in place

In reply to Ralf Lici

The segments of a GSO skb are encrypted out of place, from each segment into its range of the UDP GSO aggregate. Their L4 checksums are computed in software by the segmenter beforehand, which sums each segment's payload in a separate pass and, if its frags are shared as with sendfile, first copies it into a linear buffer.

When the segments are going to be batched, leave their checksums partial instead. Copy each segment into its final position in the aggregate, summing it on the way, complete its checksum there as skb_checksum_help would and encrypt it in place. The checksums of packets left for the ordinary path are completed after batching. Segments with a frag_list are not batched, as the copy only walks the linear area and page frags.

With one TCP flow over AES-128-GCM between two mlx5 ports connected back to back, with the sender, the receiver and their interrupts pinned to separate CPUs, the sender needs about 7% fewer CPU cycles per byte, and throughput rises from 27.5 to 29.2 Gbit/s, where the receiver becomes the bottleneck. With the aggregates segmented in software, where the sender is the bottleneck, throughput rises by 5%, from 17.1 to 18.0 Gbit/s.

---
 drivers/net/ovpn/crypto_aead.c | 109 ++++++------------------
 drivers/net/ovpn/crypto_aead.h |  12 +--
 drivers/net/ovpn/gso.c         | 147 ++++++++++++++++++++++++++++++++-
 drivers/net/ovpn/io.c          |  53 +++++++-----
 4 files changed, 209 insertions(+), 112 deletions(-)
drivers/net/ovpn/crypto_aead.c +23 -86
index e27b8c47c0fb..0a74cdf09e86 100644
@@ -153,9 +153,9 @@ static struct aead_request *ovpn_aead_request_alloc(struct crypto_aead *aead,
 	return req;
 }
 
-static void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
-				     struct ovpn_crypto_key_slot *ks,
-				     u32 pktid, u8 *iv, u8 *data)
+void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
+			      struct ovpn_crypto_key_slot *ks, u32 pktid,
+			      u8 *iv, u8 *data)
 {
 	u32 op;
 
@@ -171,39 +171,14 @@ static void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
 	memcpy(data + OVPN_OPCODE_SIZE, iv, OVPN_NONCE_WIRE_SIZE);
 }
 
-static bool ovpn_aead_encrypt_header_direct(struct ovpn_peer *peer,
-					    struct ovpn_crypto_key_slot *ks,
-					    u32 pktid, u8 *iv,
-					    struct scatterlist *sg)
-{
-	unsigned int offset, page_offset;
-	struct page *page;
-	void *addr;
-
-	offset = sg->offset;
-	page_offset = offset_in_page(offset);
-	if (page_offset + OVPN_AAD_SIZE > PAGE_SIZE)
-		return false;
-
-	/* an SG offset can address a later base page in a compound page */
-	page = sg_page(sg) + (offset >> PAGE_SHIFT);
-	addr = kmap_local_page(page);
-	ovpn_aead_encrypt_header(peer, ks, pktid, iv, addr + page_offset);
-	kunmap_local(addr);
-
-	return true;
-}
-
 int ovpn_aead_gso_ctx_init(struct ovpn_aead_gso_ctx *ctx,
 			   struct ovpn_crypto_key_slot *ks,
 			   struct sk_buff *gso_skb)
 {
 	const unsigned int dst_nents = skb_shinfo(gso_skb)->nr_frags + 4;
-	const unsigned int src_nents = MAX_SKB_FRAGS + 2;
 	unsigned int tmp_size;
 
-	tmp_size = ovpn_aead_crypto_tmp_size(ks->encrypt,
-					     src_nents + dst_nents);
+	tmp_size = ovpn_aead_crypto_tmp_size(ks->encrypt, dst_nents);
 	tmp_size += OVPN_AAD_SIZE;
 	ctx->tmp = kmalloc(tmp_size, GFP_ATOMIC);
 	if (unlikely(!ctx->tmp))
@@ -212,9 +187,8 @@ int ovpn_aead_gso_ctx_init(struct ovpn_aead_gso_ctx *ctx,
 	ctx->dst_nents = dst_nents;
 	ctx->iv = ovpn_aead_crypto_tmp_iv(ks->encrypt, ctx->tmp);
 	ctx->req = ovpn_aead_crypto_tmp_req(ks->encrypt, ctx->iv);
-	ctx->src = ovpn_aead_crypto_req_sg(ks->encrypt, ctx->req);
-	ctx->dst = ctx->src + src_nents;
-	ctx->aad = (u8 *)(ctx->dst + dst_nents);
+	ctx->dst = ovpn_aead_crypto_req_sg(ks->encrypt, ctx->req);
+	ctx->head = (u8 *)(ctx->dst + dst_nents);
 	aead_request_set_tfm(ctx->req, ks->encrypt);
 	aead_request_set_callback(ctx->req, 0, NULL, NULL);
 	aead_request_set_ad(ctx->req, OVPN_AAD_SIZE);
@@ -227,72 +201,35 @@ void ovpn_aead_gso_ctx_free(struct ovpn_aead_gso_ctx *ctx)
 	kfree(ctx->tmp);
 }
 
-int ovpn_aead_encrypt_gso(struct ovpn_aead_gso_ctx *ctx, struct ovpn_peer *peer,
-			  struct ovpn_crypto_key_slot *ks, struct sk_buff *skb,
+int ovpn_aead_encrypt_gso(struct ovpn_aead_gso_ctx *ctx,
 			  struct sk_buff *gso_skb, unsigned int offset,
-			  u32 pktid)
+			  unsigned int crypt_len)
 {
-	unsigned int payload_off;
-	int dst_idx, mapped, ret;
-	bool direct_aad;
+	int nents, mapped;
 
-	/* encrypt out of place from the original segmented skb directly into
-	 * its final range in the UDP GSO skb
+	/* The record at offset already holds its OpenVPN header, which is
+	 * the AAD, room for the tag and the plaintext, and ctx->iv holds its
+	 * nonce. Source and destination share one scatterlist, in which the
+	 * tag follows the plaintext although it precedes it on the wire.
 	 */
-	sg_init_table(ctx->src, skb_shinfo(skb)->nr_frags + 2);
-	/* use the final output bytes as source AAD too when they can be safely
-	 * written through one base-page mapping
-	 */
-	mapped = skb_to_sgvec_nomark(gso_skb, ctx->src, offset,
-				     OVPN_AAD_SIZE);
-	if (unlikely(mapped < 0))
-		return mapped;
-	direct_aad = mapped == 1 &&
-		     ovpn_aead_encrypt_header_direct(peer, ks, pktid, ctx->iv,
-						     ctx->src);
-	if (unlikely(!direct_aad)) {
-		ovpn_aead_encrypt_header(peer, ks, pktid, ctx->iv, ctx->aad);
-		ret = skb_store_bits(gso_skb, offset, ctx->aad,
-				     OVPN_AAD_SIZE);
-		if (unlikely(ret < 0))
-			return ret;
-		sg_set_buf(ctx->src, ctx->aad, OVPN_AAD_SIZE);
-	}
-
-	mapped = skb_to_sgvec_nomark(skb, ctx->src + 1, 0, skb->len);
-	if (unlikely(mapped < 0))
-		return mapped;
-	sg_mark_end(&ctx->src[mapped]);
-
 	sg_init_table(ctx->dst, ctx->dst_nents);
-	if (likely(direct_aad)) {
-		/* destination AAD is not written and may alias source AAD */
-		sg_set_page(ctx->dst, sg_page(ctx->src), ctx->src->length,
-			    ctx->src->offset);
-		dst_idx = 1;
-	} else {
-		dst_idx = skb_to_sgvec_nomark(gso_skb, ctx->dst, offset,
-					      OVPN_AAD_SIZE);
-		if (unlikely(dst_idx < 0))
-			return dst_idx;
-	}
-
-	payload_off = offset + OVPN_AAD_SIZE + OVPN_AUTH_TAG_SIZE;
-	mapped = skb_to_sgvec_nomark(gso_skb, ctx->dst + dst_idx,
-				     payload_off, skb->len);
+	nents = skb_to_sgvec_nomark(gso_skb, ctx->dst, offset, OVPN_AAD_SIZE);
+	if (unlikely(nents < 0))
+		return nents;
+	mapped = skb_to_sgvec_nomark(gso_skb, ctx->dst + nents,
+				     offset + OVPN_DATA_V2_OVERHEAD, crypt_len);
 	if (unlikely(mapped < 0))
 		return mapped;
-	dst_idx += mapped;
-
-	mapped = skb_to_sgvec_nomark(gso_skb, ctx->dst + dst_idx,
+	nents += mapped;
+	mapped = skb_to_sgvec_nomark(gso_skb, ctx->dst + nents,
 				     offset + OVPN_AAD_SIZE,
 				     OVPN_AUTH_TAG_SIZE);
 	if (unlikely(mapped < 0))
 		return mapped;
-	dst_idx += mapped;
-	sg_mark_end(&ctx->dst[dst_idx - 1]);
+	nents += mapped;
+	sg_mark_end(&ctx->dst[nents - 1]);
 
-	aead_request_set_crypt(ctx->req, ctx->src, ctx->dst, skb->len,
+	aead_request_set_crypt(ctx->req, ctx->dst, ctx->dst, crypt_len,
 			       ctx->iv);
 
 	return crypto_aead_encrypt(ctx->req);
drivers/net/ovpn/crypto_aead.h +7 -5
index c0c777410661..ae09d304c971 100644
@@ -20,11 +20,11 @@ struct scatterlist;
 
 struct ovpn_aead_gso_ctx {
 	struct aead_request *req;
-	struct scatterlist *src;
 	struct scatterlist *dst;
 	void *tmp;
 	u8 *iv;
-	u8 *aad;
+	/* start of the record: its AAD */
+	u8 *head;
 	unsigned int dst_nents;
 };
 
@@ -34,10 +34,12 @@ int ovpn_aead_gso_ctx_init(struct ovpn_aead_gso_ctx *ctx,
 			   struct ovpn_crypto_key_slot *ks,
 			   struct sk_buff *gso_skb);
 void ovpn_aead_gso_ctx_free(struct ovpn_aead_gso_ctx *ctx);
-int ovpn_aead_encrypt_gso(struct ovpn_aead_gso_ctx *ctx, struct ovpn_peer *peer,
-			  struct ovpn_crypto_key_slot *ks, struct sk_buff *skb,
+void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
+			      struct ovpn_crypto_key_slot *ks, u32 pktid,
+			      u8 *iv, u8 *data);
+int ovpn_aead_encrypt_gso(struct ovpn_aead_gso_ctx *ctx,
 			  struct sk_buff *gso_skb, unsigned int offset,
-			  u32 pktid);
+			  unsigned int crypt_len);
 int ovpn_aead_decrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,
 		      struct sk_buff *skb);
 
drivers/net/ovpn/gso.c +145 -2

171 lines not on this page · raw message

drivers/net/ovpn/io.c +34 -19

86 lines not on this page · raw message

Signature · 8 lines
2.56.0



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

PATCH 5/5 ovpn: encrypt GSO input without child skbs

In reply to Ralf Lici

Software segmentation materializes one child skb per inner packet even though UDP GSO output immediately copies each encrypted record into a new aggregate. The child skbs provide header storage, but their payload remains shared with the original GSO skb.

For the IPv4 and IPv6 TCP and UDP GSO shapes which tso_build_hdr supports, add a record source which copies each record's payload from the original skb into its final position in the outer UDP GSO aggregate with the segment copy helpers, computing the inner L4 checksum on the way. Synthesize the record's network and transport headers behind its OpenVPN data header in the per-batch crypto buffer, complete their checksums, store them in front of the payload and encrypt the record in place.

As with NETIF_F_TSO_MANGLEID, fixed-ID TCP aggregates get incrementing IPv4 IDs; gso_features_check already segments those without DF before they reach the driver. AccECN aggregates are accepted, as tso_build_hdr keeps CWR on every record just as their segmentation does.

The aggregates take over the input's write ownership as for materialized segments, and the input's own charge is released without its destructor once the last aggregate is built, so TSQ is not woken before the aggregates are transmitted.

Classic ECN aggregates, whose CWR belongs on the first segment only, frag_list input, IPv6 extension headers, headers outside the linear area, shared and TX-timestamped skbs and inputs whose records cannot all be batched stay on the software segmentation path. If an aggregate fails after earlier ones were sent, the remaining records are dropped, as they cannot fall back without resending the others.

With one TCP flow over AES-128-GCM between two mlx5 ports connected back to back, with the sender, the receiver and their interrupts pinned to separate CPUs, the sender needs about 26% fewer CPU cycles per byte than with copied segments, while throughput only rises from 29.2 to 30.3 Gbit/s, as the receiver remains the bottleneck and hides most of the gain. With the aggregates segmented in software, where the sender is the bottleneck, throughput rises from 18.0 to 21.7 Gbit/s.

---
 drivers/net/ovpn/crypto_aead.c |   8 +-
 drivers/net/ovpn/crypto_aead.h |   5 +-
 drivers/net/ovpn/gso.c         | 275 ++++++++++++++++++++++++++++++---
 drivers/net/ovpn/gso.h         |   1 +
 drivers/net/ovpn/io.c          |   2 +
 5 files changed, 269 insertions(+), 22 deletions(-)
drivers/net/ovpn/crypto_aead.c +6 -2
index 0a74cdf09e86..427eecea6b04 100644
@@ -173,13 +173,17 @@ void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
 
 int ovpn_aead_gso_ctx_init(struct ovpn_aead_gso_ctx *ctx,
 			   struct ovpn_crypto_key_slot *ks,
-			   struct sk_buff *gso_skb)
+			   struct sk_buff *gso_skb,
+			   unsigned int header_len)
 {
 	const unsigned int dst_nents = skb_shinfo(gso_skb)->nr_frags + 4;
 	unsigned int tmp_size;
 
 	tmp_size = ovpn_aead_crypto_tmp_size(ks->encrypt, dst_nents);
-	tmp_size += OVPN_AAD_SIZE;
+	/* the head buffer stages the start of a record: its AAD and, for a
+	 * TSO record, room for the tag and its synthesized headers
+	 */
+	tmp_size += OVPN_DATA_V2_OVERHEAD + header_len;
 	ctx->tmp = kmalloc(tmp_size, GFP_ATOMIC);
 	if (unlikely(!ctx->tmp))
 		return -ENOMEM;
drivers/net/ovpn/crypto_aead.h +3 -2
index ae09d304c971..f675995d9c64 100644
@@ -23,7 +23,7 @@ struct ovpn_aead_gso_ctx {
 	struct scatterlist *dst;
 	void *tmp;
 	u8 *iv;
-	/* start of the record: its AAD */
+	/* start of the record: AAD, room for the tag, synthesized headers */
 	u8 *head;
 	unsigned int dst_nents;
 };
@@ -32,7 +32,8 @@ int ovpn_aead_encrypt(struct ovpn_peer *peer, struct ovpn_crypto_key_slot *ks,
 		      struct sk_buff *skb);
 int ovpn_aead_gso_ctx_init(struct ovpn_aead_gso_ctx *ctx,
 			   struct ovpn_crypto_key_slot *ks,
-			   struct sk_buff *gso_skb);
+			   struct sk_buff *gso_skb,
+			   unsigned int header_len);
 void ovpn_aead_gso_ctx_free(struct ovpn_aead_gso_ctx *ctx);
 void ovpn_aead_encrypt_header(struct ovpn_peer *peer,
 			      struct ovpn_crypto_key_slot *ks, u32 pktid,
drivers/net/ovpn/gso.c +257 -18

383 lines not on this page · raw message

drivers/net/ovpn/gso.h +1 -0
index cc7b6b8ce314..0b0cd83e7696 100644
@@ -15,6 +15,7 @@
 #include "peer.h"
 
 void ovpn_gso_send(struct sk_buff_head *skbs, struct ovpn_peer *peer);
+bool ovpn_gso_send_tso(struct sk_buff *skb, struct ovpn_peer *peer);
 bool ovpn_gso_supported(struct ovpn_peer *peer);
 
 #endif /* _NET_OVPN_GSO_H_ */
drivers/net/ovpn/io.c +2 -0
index 83cdd2acfce2..884e19b87054 100644
@@ -430,6 +430,8 @@ netdev_tx_t ovpn_net_xmit(struct sk_buff *skb, struct net_device *dev)
 	skb_dst_drop(skb);
 
 	gso_out = gso_out && ovpn_gso_supported(peer);
+	if (gso_out && ovpn_gso_send_tso(skb, peer))
+		return NETDEV_TX_OK;
 
 	if (gso_in) {
 		/* force software segmentation while allowing safe page-backed
Signature · 8 lines
2.56.0



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