kernel/linux/src: add GRO for WireGuard data messages, and decrypt merged packets whole

Our UDP sockets now have gro_receive/gro_complete callbacks, as UDP
tunnels do. They merge consecutive data messages of a flow that use the
same receiving key and are all the same size but perhaps the last, and
UDP hands the merged packet (SKB_GSO_UDP_TUNNEL) to encap_rcv whole. It
goes through the decrypt ring and the peer's queue as one entry: one
worker decrypts each message into a new packet (copying it out and
decrypting in place there, rather than linearizing in place), and the
peer's NAPI poll delivers them in order. So the IP/UDP input path, the
ring and queue handoffs and the worker kick are paid once per merged
packet rather than per packet. Upstream gets no outer GRO at all.

skb_gro_receive isn't exported to modules, so socket.c has a copy
(tswg_gro_merge). gro_receive_network_flush and udp_tunnel_update_gro_rcv
are newer than 6.8, so they're probed. The latter matters: 7.x may make
the socket's gro_receive a static call, which detaching a user socket
must undo, or it would outlive the module.

Bulk TCP in the lean harness, now with guest USO off (so the receiving
VM sees individual datagrams, as from a physical network): about 9.8
Gbps, against 6.5 before, and within a few percent of wireguard-go
(about 10.0), at about 18% less receiver CPU. Making workers poll the
ring briefly before going idle (to save wakeup IPIs) was tried and
didn't help, so isn't included.

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: Ic22482120c6ce75b9a4d896f16b6cf77aca28b4c
This commit is contained in:
Brad Fitzpatrick committed 2026-11-07 13:29:26 +00:00
1 parent 9dc696cc4d
commit c9f9c52c0c
6 files changed
+370 -15

No files matched your search

@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-2.0
// Probe for gro_receive_network_flush() (6.10). Before it, inet_gro_receive
// and ipv6_gro_receive set NAPI_GRO_CB(p)->flush for network header changes.
#include <net/gro.h>
static int __maybe_unused probe(const void *th, const void *th2,
struct sk_buff *p)
{
return gro_receive_network_flush(th, th2, p);
}
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
// Probe for udp_tunnel_update_gro_rcv() (6.15), with which
// setup_udp_tunnel_sock may make a socket's gro_receive a static call.
#include <net/udp_tunnel.h>
static void __maybe_unused probe(struct sock *sk)
{
udp_tunnel_update_gro_rcv(sk, false);
}
+6 -5
View File
@@ -149,12 +149,13 @@ static inline int wg_cpumask_next_online(int *last_cpu)
*/
/*
* TSWG_DECRYPT_BACKLOG and TSWG_ENCRYPT_BACKLOG are how many ring entries
* may wait before another worker is recruited. A decrypt entry is one
* packet; an encrypt entry is a whole GSO packet's worth.
* TSWG_PACKET_BACKLOG and TSWG_BATCH_BACKLOG are how many ring entries may
* wait before another worker is recruited, when the entries are single
* packets, and when they're batches (a GSO packet's worth: encrypt entries
* are lists of packets, and decrypt entries can be packets merged by GRO).
*/
#define TSWG_DECRYPT_BACKLOG 16
#define TSWG_ENCRYPT_BACKLOG 1
#define TSWG_PACKET_BACKLOG 16
#define TSWG_BATCH_BACKLOG 1
/*
* tswg_crypt_backlog returns about how many entries are in the ring. It
+117 -8
View File
@@ -241,13 +241,8 @@ static void keep_key_fresh(struct wg_peer *peer)
}
}
static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
static bool keypair_may_receive(struct noise_keypair *keypair)
{
struct scatterlist sg[MAX_SKB_FRAGS + 8];
struct sk_buff *trailer;
unsigned int offset;
int num_frags;
if (unlikely(!keypair))
return false;
@@ -257,6 +252,73 @@ static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
WRITE_ONCE(keypair->receiving.is_valid, false);
return false;
}
return true;
}
/*
* TSWG: tswg_decrypt_gso decrypts each data message of skb, a packet merged
* by tswg_gro_receive, into a new packet, listed in
* PACKET_CB(skb)->tswg_out. Messages that fail to decrypt are dropped,
* like upstream drops packets that do.
*/
static bool tswg_decrypt_gso(struct sk_buff *skb, struct noise_keypair *keypair)
{
struct sk_buff **tail = &PACKET_CB(skb)->tswg_out;
unsigned int mss = skb_shinfo(skb)->gso_size;
struct net_device *dev = keypair->entry.peer->device->dev;
unsigned int off, len;
if (unlikely(!mss))
return false;
for (off = 0; off < skb->len; off += len) {
struct message_data hdr_buf;
const struct message_data *hdr;
unsigned int enc_len;
struct sk_buff *nskb;
u64 nonce;
len = min(mss, skb->len - off);
if (unlikely(len < message_data_len(0)))
break;
hdr = skb_header_pointer(skb, off, sizeof(*hdr), &hdr_buf);
if (unlikely(!hdr))
break;
nonce = le64_to_cpu(hdr->counter);
enc_len = len - sizeof(*hdr);
nskb = netdev_alloc_skb(dev, enc_len);
if (unlikely(!nskb))
continue;
if (unlikely(skb_copy_bits(skb, off + sizeof(*hdr),
skb_put(nskb, enc_len), enc_len) ||
!chacha20poly1305_decrypt(nskb->data, nskb->data,
enc_len, NULL, 0, nonce,
keypair->receiving.key))) {
kfree_skb(nskb);
continue;
}
__skb_trim(nskb, enc_len - noise_encrypted_len(0));
skb_reset_mac_header(nskb);
skb_reset_network_header(nskb);
PACKET_CB(nskb)->nonce = nonce;
PACKET_CB(nskb)->keypair = keypair; /* skb holds the reference */
PACKET_CB(nskb)->ds = PACKET_CB(skb)->ds;
*tail = nskb;
tail = &nskb->next;
}
return true;
}
static bool decrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
{
struct scatterlist sg[MAX_SKB_FRAGS + 8];
struct sk_buff *trailer;
unsigned int offset;
int num_frags;
if (unlikely(!keypair_may_receive(keypair)))
return false;
if (skb_is_gso(skb)) /* TSWG */
return tswg_decrypt_gso(skb, keypair);
PACKET_CB(skb)->nonce =
le64_to_cpu(((struct message_data *)skb->data)->counter);
@@ -438,6 +500,35 @@ packet_processed:
dev_kfree_skb(skb);
}
/*
* TSWG: tswg_receive_gso delivers the packets decrypted from skb (see
* tswg_decrypt_gso) and returns how many there were.
*/
static int tswg_receive_gso(struct wg_peer *peer, struct sk_buff *skb)
{
struct noise_keypair *keypair = PACKET_CB(skb)->keypair;
struct sk_buff *nskb = PACKET_CB(skb)->tswg_out, *next;
struct endpoint endpoint;
bool have_endpoint;
int n = 0;
PACKET_CB(skb)->tswg_out = NULL;
have_endpoint = !wg_socket_endpoint_from_skb(&endpoint, skb);
for (; nskb; nskb = next) {
next = nskb->next;
skb_mark_not_on_list(nskb);
n++;
if (unlikely(!have_endpoint ||
!counter_validate(&keypair->receiving_counter,
PACKET_CB(nskb)->nonce))) {
dev_kfree_skb(nskb);
continue;
}
wg_packet_consume_data_done(peer, nskb, &endpoint);
}
return n;
}
int wg_packet_rx_poll(struct napi_struct *napi, int budget)
{
struct wg_peer *peer = container_of(napi, struct wg_peer, napi);
@@ -461,6 +552,11 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget)
if (unlikely(state != PACKET_STATE_CRYPTED))
goto next;
if (skb_is_gso(skb)) { /* TSWG */
work_done += max(tswg_receive_gso(peer, skb), 1) - 1;
goto next;
}
if (unlikely(!counter_validate(&keypair->receiving_counter,
PACKET_CB(skb)->nonce))) {
net_dbg_ratelimited("%s: Packet has invalid nonce %llu (max %llu)\n",
@@ -478,6 +574,15 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget)
free = false;
next:
/*
* TSWG: drop what tswg_decrypt_gso decrypted but we didn't
* deliver. Only while we still own skb: once delivered, its cb
* is GRO's.
*/
if (unlikely(free && PACKET_CB(skb)->tswg_out)) {
kfree_skb_list(PACKET_CB(skb)->tswg_out);
PACKET_CB(skb)->tswg_out = NULL;
}
wg_noise_keypair_put(keypair, false);
wg_peer_put(peer);
if (unlikely(free))
@@ -490,7 +595,8 @@ next:
if (work_done < budget)
napi_complete_done(napi, work_done);
return work_done;
/* TSWG: a GSO packet counts as all its segments, so may overshoot. */
return min(work_done, budget);
}
void wg_packet_decrypt_worker(struct work_struct *work)
@@ -531,9 +637,12 @@ static void wg_packet_consume_data(struct wg_device *wg, struct sk_buff *skb)
if (unlikely(READ_ONCE(peer->is_dead)))
goto err;
PACKET_CB(skb)->tswg_out = NULL; /* TSWG */
ret = wg_queue_enqueue_per_device_and_peer(&wg->decrypt_queue, &peer->rx_queue, skb,
wg->packet_crypt_wq,
TSWG_DECRYPT_BACKLOG);
skb_is_gso(skb) ?
TSWG_BATCH_BACKLOG :
TSWG_PACKET_BACKLOG);
if (unlikely(ret == -EPIPE)) {
/* TSWG: count drops, which upstream doesn't. */
DEV_STATS_INC(wg->dev, rx_fifo_errors);
+1 -1
View File
@@ -438,7 +438,7 @@ static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)
ret = wg_queue_enqueue_per_device_and_peer(&wg->encrypt_queue, &peer->tx_queue, first,
wg->packet_crypt_wq,
TSWG_ENCRYPT_BACKLOG);
TSWG_BATCH_BACKLOG);
if (unlikely(ret == -EPIPE))
wg_queue_enqueue_per_peer_tx(first, PACKET_STATE_DEAD);
err:
+227 -1
View File
@@ -18,6 +18,7 @@
#include <linux/if_ether.h>
#include <linux/inetdevice.h>
#include <net/udp_tunnel.h>
#include <net/gro.h>
#include <net/ipv6.h>
/*
@@ -356,6 +357,216 @@ static bool tswg_is_wg_message(struct sk_buff *skb)
!h[1] && !h[2] && !h[3];
}
/*
* TSWG: GRO for WireGuard data messages.
*
* As a UDP tunnel, our sockets get their packets' GRO callbacks. We merge
* consecutive data messages of a flow (same addresses and ports) that use
* the same receiving key, and are all the same size but perhaps the last,
* into one packet, which UDP then passes to encap_rcv whole (it's marked
* SKB_GSO_UDP_TUNNEL). We decrypt it as one unit; see tswg_decrypt_gso.
* Upstream gets no GRO for the outer packets at all, so it pays the IP and
* UDP receive path, and a decrypt worker handoff, per packet.
*
* Only data messages are merged, so on user sockets, everything that's
* left for the user (disco, STUN) passes through as is.
*/
#define TSWG_GRO_MAX_SEGS 64
/*
* tswg_gro_merge is skb_gro_receive (net/core/gro.c, as of 7.4), which isn't
* exported to modules: it appends skb's payload to p's, by moving skb's
* page frags (or stealing its head, if it's a page fragment) to the last
* packet of p, or else by chaining skb on p's frag_list.
*/
static int tswg_gro_merge(struct sk_buff *p, struct sk_buff *skb)
{
struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
unsigned int offset = skb_gro_offset(skb);
unsigned int headlen = skb_headlen(skb);
unsigned int len = skb_gro_len(skb);
unsigned int delta_truesize;
unsigned int new_truesize;
struct sk_buff *lp;
int segs;
if (p->pp_recycle != skb->pp_recycle)
return -ETOOMANYREFS;
if (skb_zcopy(p) || skb_zcopy(skb))
return -ETOOMANYREFS;
if (unlikely(p->len + len >= min_t(unsigned int, GRO_LEGACY_MAX_SIZE,
netif_get_gro_max_size(p->dev, p)) ||
NAPI_GRO_CB(skb)->flush))
return -E2BIG;
segs = NAPI_GRO_CB(skb)->count;
lp = NAPI_GRO_CB(p)->last;
pinfo = skb_shinfo(lp);
if (headlen <= offset) {
skb_frag_t *frag;
skb_frag_t *frag2;
int i = skbinfo->nr_frags;
int nr_frags = pinfo->nr_frags + i;
if (nr_frags > MAX_SKB_FRAGS)
goto merge;
offset -= headlen;
pinfo->nr_frags = nr_frags;
skbinfo->nr_frags = 0;
frag = pinfo->frags + nr_frags;
frag2 = skbinfo->frags + i;
do {
*--frag = *--frag2;
} while (--i);
skb_frag_off_add(frag, offset);
skb_frag_size_sub(frag, offset);
/* All fragments' truesize: remove (head size + sk_buff). */
new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
delta_truesize = skb->truesize - new_truesize;
skb->truesize = new_truesize;
skb->len -= skb->data_len;
skb->data_len = 0;
NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
goto done;
} else if (skb->head_frag) {
int nr_frags = pinfo->nr_frags;
skb_frag_t *frag = pinfo->frags + nr_frags;
struct page *page = virt_to_head_page(skb->head);
unsigned int first_size = headlen - offset;
unsigned int first_offset;
if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
goto merge;
first_offset = skb->data -
(unsigned char *)page_address(page) +
offset;
pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
skb_frag_fill_page_desc(frag, page, first_offset, first_size);
memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
delta_truesize = skb->truesize - new_truesize;
skb->truesize = new_truesize;
NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
goto done;
}
merge:
/* sk ownership, if any, completely transferred to the aggregate */
skb->destructor = NULL;
skb->sk = NULL;
delta_truesize = skb->truesize;
if (offset > headlen) {
unsigned int eat = offset - headlen;
skb_frag_off_add(&skbinfo->frags[0], eat);
skb_frag_size_sub(&skbinfo->frags[0], eat);
skb->data_len -= eat;
skb->len -= eat;
offset = headlen;
}
__skb_pull(skb, offset);
if (NAPI_GRO_CB(p)->last == p)
skb_shinfo(p)->frag_list = skb;
else
NAPI_GRO_CB(p)->last->next = skb;
NAPI_GRO_CB(p)->last = skb;
__skb_header_release(skb);
lp = p;
done:
NAPI_GRO_CB(p)->count += segs;
p->data_len += len;
p->truesize += delta_truesize;
p->len += len;
skb_shinfo(p)->flags |= skbinfo->flags & SKBFL_SHARED_FRAG;
if (lp != p) {
lp->data_len += len;
lp->truesize += delta_truesize;
lp->len += len;
skb_shinfo(lp)->flags |= skbinfo->flags & SKBFL_SHARED_FRAG;
}
NAPI_GRO_CB(skb)->same_flow = 1;
return 0;
}
static struct sk_buff *tswg_gro_receive(struct sock *sk,
struct list_head *head,
struct sk_buff *skb)
{
unsigned int off = skb_gro_offset(skb), len = skb_gro_len(skb);
const struct message_data *hdr;
const struct udphdr *uh;
struct sk_buff *p;
hdr = skb_gro_header(skb, off + offsetofend(struct message_data, key_idx),
off);
if (unlikely(!hdr) || hdr->header.type != cpu_to_le32(MESSAGE_DATA) ||
len < message_data_len(0)) {
NAPI_GRO_CB(skb)->flush = 1;
return NULL;
}
uh = (const struct udphdr *)hdr - 1; /* contiguous with hdr */
list_for_each_entry(p, head, list) {
const struct udphdr *uh2;
__le32 idx_buf;
const __le32 *idx;
unsigned int mss;
int flush;
if (!NAPI_GRO_CB(p)->same_flow)
continue;
uh2 = (const struct udphdr *)(p->data + off - sizeof(*uh2));
mss = udp_get_len_short(uh2) - sizeof(*uh2);
idx = skb_header_pointer(p, off + offsetof(struct message_data, key_idx),
sizeof(*idx), &idx_buf);
flush = NAPI_GRO_CB(p)->flush;
#ifdef HAVE_GRO_RECEIVE_NETWORK_FLUSH
flush |= gro_receive_network_flush(uh, uh2, p);
#endif
/*
* Complete p and start anew with skb if skb doesn't fit: it's
* longer than p's segments, or uses another key, or the
* network headers differ (ECN marks, say).
*/
if (flush || !idx || *idx != hdr->key_idx || len > mss ||
tswg_gro_merge(p, skb))
return p;
skb_shinfo(p)->gso_size = mss;
/* Only the last segment may be shorter. */
if (len < mss || NAPI_GRO_CB(p)->count >= TSWG_GRO_MAX_SEGS)
return p;
return NULL;
}
return NULL;
}
static int tswg_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
{
/*
* udp_gro_complete has made it SKB_GSO_UDP_TUNNEL(_CSUM), and UDP
* only lets us merge packets whose checksums were verified.
*/
skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
skb->ip_summed = CHECKSUM_UNNECESSARY;
skb->csum_level = 0;
return 0;
}
static int wg_receive(struct sock *sk, struct sk_buff *skb)
{
struct wg_device *wg;
@@ -409,7 +620,9 @@ int wg_socket_init(struct wg_device *wg, u16 port)
struct udp_tunnel_sock_cfg cfg = {
.sk_user_data = wg,
.encap_type = 1,
.encap_rcv = wg_receive
.encap_rcv = wg_receive,
.gro_receive = tswg_gro_receive, /* TSWG */
.gro_complete = tswg_gro_complete,
};
struct socket *new4 = NULL, *new6 = NULL;
struct udp_port_cfg port4 = {
@@ -555,6 +768,17 @@ static void tswg_detach_user_sock_locked(struct wg_device *wg, struct sock *sk)
* its old value.
*/
lock_sock(sk);
/*
* setup_udp_tunnel_sock may have made our gro_receive a static call,
* which must not outlive the module, so undo that while gro_receive
* still identifies it. Packets that GRO merged just before this may
* still reach the socket's queue as one datagram; that's harmless.
*/
#ifdef HAVE_UDP_TUNNEL_UPDATE_GRO_RCV
udp_tunnel_update_gro_rcv(sk, false);
#endif
WRITE_ONCE(up->gro_receive, NULL);
WRITE_ONCE(up->gro_complete, NULL);
WRITE_ONCE(up->encap_rcv, NULL);
WRITE_ONCE(up->encap_destroy, NULL);
WRITE_ONCE(up->encap_type, 0);
@@ -629,6 +853,8 @@ int tswg_socket_set_user(struct wg_device *wg, int family, int fd,
.encap_type = 1,
.encap_rcv = wg_receive,
.encap_destroy = tswg_user_sock_destroy,
.gro_receive = tswg_gro_receive,
.gro_complete = tswg_gro_complete,
};
struct sock **slot, *old, *sk = NULL;
struct socket *sock;