diff --git a/kernel/linux/DESIGN.md b/kernel/linux/DESIGN.md index eb546845c..c8879c53e 100644 --- a/kernel/linux/DESIGN.md +++ b/kernel/linux/DESIGN.md @@ -63,7 +63,12 @@ which the router forwards to, masquerading. Updated 2026-11-07. for the page allocator's zone lock, 14-20% of those CPUs, plus zeroing (`init_on_alloc`). Tried there, no better: RPS, bigger per-CPU page lists (`vm.percpu_pagelist_high_fraction=8`), capping wg0's GRO size - (for hardware TSO on forwarding). More clients helped until about 40 + (for hardware TSO on forwarding), and zero-copy receive (module + parameter `rx_zerocopy`, off by default), which took tswg's own page + allocations out (page zeroing 4.7% to 1.1%, zone lock time 9-11% to + 7%) but cost more in scatterlist decryption: with 24 clients, 62.5-63.3 + Gbps and 6.05 CPU-s/GB against 63.4-64.7 and 5.47 copying. So tswg's + allocations weren't what binds; ENA's page per received packet is. More clients helped until about 40 (20: 56 Gbps, 40: 72, 60: 74). * On the big routers, tswg isn't CPU-bound (45% of 128 CPUs at 74 Gbps), so its CPU per byte is about even with wireguard-go's; where CPU is the @@ -1033,8 +1038,9 @@ Performance (the primary motivation; see "Performance"): when nothing is busy (encrypt or decrypt inline, send from the encrypt worker), without bringing back softirq starvation of workers on the NIC's CPU under load. -- [x] Zero-copy receive: tried, 7% worse for the subnet router (see - "Subnet router"); kept out of tree. +- [x] Zero-copy receive: tried, 7% worse on a CPU-bound subnet router, + and no better on a 200 Gbps one where page allocation is the limit + (see "Results at a glance"); kept behind `rx_zerocopy`, off. - [ ] TSO from the inner GSO packet: encrypt straight from it, building each segment's headers, instead of `skb_gso_segment` in `wg_xmit` (per-segment skb allocation). @@ -1054,9 +1060,10 @@ Performance (the primary motivation; see "Performance"): - [x] Subnet router beyond 40 Gbps at MTU 1280: c6in.32xlarge, 74 Gbps with 60 clients (12x wireguard-go). - [ ] Subnet router past 75 Gbps at MTU 1280: the ENA interrupt CPUs' - page allocator contention. Ideas: fewer pages per packet on our side - (decrypted packets from slab or a recycling pool), ENA with a page - pool (upstream driver work), other NICs (mlx5 on bare metal). + page allocator contention, from ENA allocating a page per received + packet (zero-copy receive, taking tswg's own allocations out, didn't + help). Ideas: ENA with a page pool (upstream driver work); NICs + outside EC2 (mlx5 has a page pool); EC2 metal uses ENA too. - [ ] EC2: c7i.large (2 vCPUs, CPU-bound), and the QEMU lab on a c7i.metal-24xl (to see what nesting cost the lab's numbers). - [ ] vmtest: shared socket with `UDP_GRO` enabled still works. diff --git a/kernel/linux/src/compat/compat.h b/kernel/linux/src/compat/compat.h index ad7b4618c..e02ff62db 100644 --- a/kernel/linux/src/compat/compat.h +++ b/kernel/linux/src/compat/compat.h @@ -19,6 +19,19 @@ #include #include +#ifdef HAVE_SKBUFF_REF_H +#include /* skb_frag_ref, which was in skbuff.h */ +#endif + +#ifndef HAVE_SKB_FRAGS_READABLE +#include +/* Before devmem TCP, every skb's frags were readable. */ +static inline bool skb_frags_readable(const struct sk_buff *skb) +{ + return true; +} +#endif + #ifndef HAVE_UDP_GET_LEN_SHORT #include static inline unsigned int udp_get_len_short(const struct udphdr *uh) diff --git a/kernel/linux/src/compat/probes/HAVE_SKBUFF_REF_H.c b/kernel/linux/src/compat/probes/HAVE_SKBUFF_REF_H.c new file mode 100644 index 000000000..023d2e044 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_SKBUFF_REF_H.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for (6.11), where skb_frag_ref moved. +#include + +static void __maybe_unused probe(struct sk_buff *skb) +{ + skb_frag_ref(skb, 0); +} diff --git a/kernel/linux/src/compat/probes/HAVE_SKB_FRAGS_READABLE.c b/kernel/linux/src/compat/probes/HAVE_SKB_FRAGS_READABLE.c new file mode 100644 index 000000000..8e0c45fa5 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_SKB_FRAGS_READABLE.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for skb_frags_readable() (6.12, for devmem TCP). +#include + +static bool __maybe_unused probe(const struct sk_buff *skb) +{ + return skb_frags_readable(skb); +} diff --git a/kernel/linux/src/receive.c b/kernel/linux/src/receive.c index 42eb14fe5..a609e5634 100644 --- a/kernel/linux/src/receive.c +++ b/kernel/linux/src/receive.c @@ -256,6 +256,153 @@ static bool keypair_may_receive(struct noise_keypair *keypair) return true; } +/* + * TSWG: TSWG_RX_COPYBREAK is the plaintext size below which + * tswg_decrypt_gso copies a message out rather than decrypting it in place + * and referencing its pages: for small ones (TCP ACKs, say), copying is + * cheap, and holding on to a page for them wastes memory. + * TSWG_RX_HEADROOM is the headroom we give packets that reference pages, + * for the link layer header if they're forwarded. + */ +#define TSWG_RX_COPYBREAK 256 + +/* + * TSWG: rx_zerocopy turns on decrypting in place (tswg_decrypt_message_zc). + * It's off by default: on a CPU-bound router it was 7% slower than + * copying, since scatterlist decryption costs more than linear decryption + * of a cache-hot copy; it may win where page allocation is the limit. + */ +static bool tswg_rx_zerocopy; +module_param_named(rx_zerocopy, tswg_rx_zerocopy, bool, 0644); +MODULE_PARM_DESC(rx_zerocopy, "decrypt received data messages in place, without copying them out"); +#define TSWG_RX_HEADROOM (NET_SKB_PAD + LL_MAX_HEADER) + +/* + * tswg_writable reports whether we may change skb's data in place: no one + * else can see it (as a clone, or a tap's copy, would), and it's all in + * memory we can read (not devmem). + */ +static bool tswg_writable(struct sk_buff *skb) +{ + return !skb_cloned(skb) && !skb_has_shared_frag(skb) && + skb_frags_readable(skb); +} + +/* + * tswg_ref_range appends to nskb's frags references to the len bytes of + * skb's data at off, as skb_segment does. They must be in skb's head (if + * that's a page fragment), its page frags, or (recursively) its frag_list + * members, all writable. It reports whether it could. + */ +static bool tswg_ref_range(struct sk_buff *nskb, struct sk_buff *skb, + unsigned int off, unsigned int len) +{ + struct skb_shared_info *si = skb_shinfo(skb); + unsigned int headlen = skb_headlen(skb); + struct sk_buff *member; + int i; + + if (!tswg_writable(skb) || + (skb_shinfo(nskb)->nr_frags && nskb->pp_recycle != skb->pp_recycle)) + return false; + nskb->pp_recycle = skb->pp_recycle; + if (off < headlen) { + unsigned int n = min(len, headlen - off); + struct page *page; + skb_frag_t *f; + + if (!skb->head_frag || skb_shinfo(nskb)->nr_frags >= MAX_SKB_FRAGS) + return false; + page = virt_to_head_page(skb->head); + f = &skb_shinfo(nskb)->frags[skb_shinfo(nskb)->nr_frags]; + skb_frag_fill_page_desc(f, page, + skb->data + off - (unsigned char *)page_address(page), n); + skb_frag_ref(nskb, skb_shinfo(nskb)->nr_frags++); + off += n; + len -= n; + } + off -= min(off, headlen); + for (i = 0; len && i < si->nr_frags; i++) { + unsigned int size = skb_frag_size(&si->frags[i]), n; + skb_frag_t *f; + + if (off >= size) { + off -= size; + continue; + } + if (skb_shinfo(nskb)->nr_frags >= MAX_SKB_FRAGS) + return false; + n = min(len, size - off); + f = &skb_shinfo(nskb)->frags[skb_shinfo(nskb)->nr_frags]; + *f = si->frags[i]; + skb_frag_off_add(f, off); + skb_frag_size_set(f, n); + skb_frag_ref(nskb, skb_shinfo(nskb)->nr_frags++); + off = 0; + len -= n; + } + /* + * Past the frags (the loop has taken their sizes off off), off is + * relative to the frag_list's data. Its members are whole datagrams + * (see tswg_gro_merge), so a message doesn't usually straddle them. + */ + skb_walk_frags(skb, member) { + unsigned int n; + + if (!len) + break; + if (off >= member->len) { + off -= member->len; + continue; + } + n = min(len, member->len - off); + if (!tswg_ref_range(nskb, member, off, n)) + return false; + off = 0; + len -= n; + } + return !len; +} + +/* + * tswg_decrypt_message_zc decrypts the message at off in skb, of enc_len + * bytes after its header, in place, and returns a new packet that + * references its plaintext. It returns NULL if it can't, leaving skb + * untouched, or ERR_PTR(-EBADMSG) if the message doesn't authenticate. + */ +static noinline_for_stack struct sk_buff * +tswg_decrypt_message_zc(struct sk_buff *skb, unsigned int off, + unsigned int enc_len, u64 nonce, + struct noise_keypair *keypair) +{ + unsigned int plain_len = enc_len - noise_encrypted_len(0); + struct scatterlist sg[MAX_SKB_FRAGS + 2]; + struct sk_buff *nskb; + int n; + + nskb = alloc_skb(TSWG_RX_HEADROOM, GFP_ATOMIC); + if (unlikely(!nskb)) + return NULL; + skb_reserve(nskb, TSWG_RX_HEADROOM); + if (unlikely(!tswg_ref_range(nskb, skb, off, plain_len))) + goto fail; + sg_init_table(sg, ARRAY_SIZE(sg)); + n = skb_to_sgvec(skb, sg, off, enc_len); + if (unlikely(n <= 0)) + goto fail; + if (!chacha20poly1305_decrypt_sg_inplace(sg, enc_len, NULL, 0, nonce, + keypair->receiving.key)) { + kfree_skb(nskb); + return ERR_PTR(-EBADMSG); + } + nskb->len = nskb->data_len = plain_len; + nskb->truesize += plain_len; + return nskb; +fail: + kfree_skb(nskb); + return NULL; +} + /* * TSWG: tswg_contig returns a pointer to the len bytes of skb's data at off * if they're contiguous in memory: in its head, or in one page frag (of @@ -303,6 +450,49 @@ static const u8 *tswg_contig(const struct sk_buff *skb, unsigned int off, return NULL; } +/* + * TSWG: tswg_decrypt_message_copy decrypts the message at off in skb, of + * enc_len bytes after its header, into a new packet, which it returns, or + * NULL if it can't or the message doesn't authenticate. + */ +static noinline_for_stack struct sk_buff * +tswg_decrypt_message_copy(struct sk_buff *skb, unsigned int off, + unsigned int enc_len, u64 nonce, + struct noise_keypair *keypair, struct net_device *dev) +{ + struct sk_buff *nskb; + const u8 *src; + bool ok, ahead; + + nskb = netdev_alloc_skb(dev, enc_len); + if (unlikely(!nskb)) + return NULL; + /* + * Decrypt straight from skb when the message is contiguous there, as + * it usually is, rather than copy it out and decrypt the copy in + * place: one pass over it fewer. + */ + src = tswg_contig(skb, off, enc_len, &ahead); + if (likely(src && ahead && skb_headroom(nskb) >= CHACHA_BLOCK_SIZE)) { + ok = tswg_aead_decrypt_ahead(skb_put(nskb, enc_len - noise_encrypted_len(0)), + src, enc_len, nonce, + keypair->receiving.key); + } else if (src) { + ok = tswg_aead_decrypt(skb_put(nskb, enc_len - noise_encrypted_len(0)), + src, enc_len, nonce, keypair->receiving.key); + } else { + ok = !skb_copy_bits(skb, off, skb_put(nskb, enc_len), enc_len) && + tswg_aead_decrypt(nskb->data, nskb->data, enc_len, nonce, + keypair->receiving.key); + __skb_trim(nskb, enc_len - noise_encrypted_len(0)); + } + if (unlikely(!ok)) { + kfree_skb(nskb); + return NULL; + } + return nskb; +} + /* * TSWG: tswg_decrypt_gso decrypts each data message of skb, a packet merged * by tswg_gro_receive, into a new packet, listed in @@ -323,8 +513,6 @@ static bool tswg_decrypt_gso(struct sk_buff *skb, struct noise_keypair *keypair) const struct message_data *hdr; unsigned int enc_len; struct sk_buff *nskb; - const u8 *src; - bool ok, ahead; u64 nonce; len = min(mss, skb->len - off); @@ -335,34 +523,19 @@ static bool tswg_decrypt_gso(struct sk_buff *skb, struct noise_keypair *keypair) break; nonce = le64_to_cpu(hdr->counter); enc_len = len - sizeof(*hdr); - nskb = netdev_alloc_skb(dev, enc_len); + nskb = NULL; + if (READ_ONCE(tswg_rx_zerocopy) && + enc_len - noise_encrypted_len(0) >= TSWG_RX_COPYBREAK) { + nskb = tswg_decrypt_message_zc(skb, off + sizeof(*hdr), + enc_len, nonce, keypair); + if (IS_ERR(nskb)) + continue; + } + if (!nskb) + nskb = tswg_decrypt_message_copy(skb, off + sizeof(*hdr), + enc_len, nonce, keypair, dev); if (unlikely(!nskb)) continue; - /* - * Decrypt straight from skb when the message is contiguous - * there, as it usually is, rather than copy it out and - * decrypt the copy in place: one pass over it fewer. - */ - src = tswg_contig(skb, off + sizeof(*hdr), enc_len, &ahead); - if (likely(src && ahead && skb_headroom(nskb) >= CHACHA_BLOCK_SIZE)) { - ok = tswg_aead_decrypt_ahead(skb_put(nskb, enc_len - noise_encrypted_len(0)), - src, enc_len, nonce, - keypair->receiving.key); - } else if (src) { - ok = tswg_aead_decrypt(skb_put(nskb, enc_len - noise_encrypted_len(0)), - src, enc_len, nonce, - keypair->receiving.key); - } else { - ok = !skb_copy_bits(skb, off + sizeof(*hdr), - skb_put(nskb, enc_len), enc_len) && - tswg_aead_decrypt(nskb->data, nskb->data, enc_len, - nonce, keypair->receiving.key); - __skb_trim(nskb, enc_len - noise_encrypted_len(0)); - } - if (unlikely(!ok)) { - kfree_skb(nskb); - continue; - } skb_reset_mac_header(nskb); skb_reset_network_header(nskb); PACKET_CB(nskb)->nonce = nonce;