Canonical: https://nitride.xyz/blog/kernel-bypass-worth-it

# Kernel bypass for a shred receiver: when it is worth it

Kernel bypass can help a shred receiver, but only after drop counters and timestamps show the kernel path is the limit, and each step up costs a core, a driver or a team.

Kernel bypass is worth it for a shred receiver only when measurements show the kernel receive path is the bottleneck: drops that track CPU time on the receive core, or a wait between kernel arrival and your read that matters to your strategy. Batched reads and busy polling often get you there, well before AF_XDP or DPDK.

## A receiver usually stops two rungs in
Kernel bypass has a glamour that a shred receiver rarely needs. The tools were built for line-rate packet processing: routers, load balancers, firewalls. A shred feed is one UDP flow into one port ([where Solana latency comes from](https://nitride.xyz/learn/solana-latency) shows where it sits in the path). It is busy at peaks, and every datagram counts, but it is small next to what AF_XDP and DPDK were designed for.

So we think of the options as a ladder, from least effort to most. Batched reads. Busy polling. AF_XDP. DPDK. Each rung removes some per-packet or per-wakeup cost, and each costs more to run than the last: a spare core, a driver that supports the feature, code that replaces the socket API, and finally a network card the kernel no longer owns. Climb only as far as your measurements push you.

There is a fair counterpoint. Agave's own source tree now has an XDP crate, with a UMEM and a transmit loop, and its retransmit stage can hand shreds to an XDP sender instead of a UDP socket. Validators send to many peers per shred, so the per-packet cost on their transmit side multiplies. A receiver reading one feed is a different load.

## Measure first: drops and timestamps
Before you change anything, get two numbers from the machine that will run the strategy.

**Where packets are lost.** The socket counters, the per-CPU backlog in `/proc/net/softnet_stat`, and the card's own counters in `ethtool -S` each cover one layer. [What causes UDP packet loss](https://nitride.xyz/learn/packet-loss-and-jitter) lists them, and [Linux sockets and buffers](https://nitride.xyz/docs/receiver/linux-sockets) shows the socket side in detail. Kernel bypass only helps with loss that the kernel path causes. If drops are on the network or in the card's ring, bypass changes nothing.

**How long a datagram waits for you.** Ask the kernel for a receive timestamp with `SO_TIMESTAMPNS`, then read the clock when your code gets the datagram. The difference is the time spent between the kernel stack and your read: softirq work, the socket queue, and your thread waking up. [Measuring arrival time](https://nitride.xyz/docs/receiver/arrival-time) covers timestamps properly. This small program does one thing with them: it collects that wait for a number of datagrams and prints its distribution, optionally with busy polling switched on, so you can compare runs.

```c file="wakeup.c"
// wakeup.c: gcc -O2 -o wakeup wakeup.c && ./wakeup 9000 20000 0
// args: port, datagrams to sample, SO_BUSY_POLL in microseconds (0 = off)
// prints how long datagrams waited between the kernel stamp and the read
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>

static int cmp(const void *a, const void *b) {
    long x = *(const long *)a, y = *(const long *)b;
    return (x > y) - (x < y);
}

int main(int argc, char **argv) {
    int port = argc > 1 ? atoi(argv[1]) : 9000;
    int n = argc > 2 ? atoi(argv[2]) : 20000;
    int busy = argc > 3 ? atoi(argv[3]) : 0;

    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    int on = 1;
    setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on, sizeof on);
    if (busy > 0 && setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, &busy, sizeof busy) < 0)
        perror("SO_BUSY_POLL (raising it needs CAP_NET_ADMIN)");

    struct sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) { perror("bind"); return 1; }

    long *wait_ns = malloc(sizeof(long) * n);
    char buf[1228];
    union { char raw[CMSG_SPACE(sizeof(struct timespec))]; struct cmsghdr align; } ctl;

    for (int i = 0; i < n; i++) {
        struct iovec iov = { buf, sizeof buf };
        struct msghdr msg = {0};
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
        msg.msg_control = ctl.raw;
        msg.msg_controllen = sizeof ctl.raw;
        if (recvmsg(fd, &msg, 0) < 0) { perror("recvmsg"); return 1; }

        struct timespec now, *k = NULL;
        clock_gettime(CLOCK_REALTIME, &now);
        for (struct cmsghdr *c = CMSG_FIRSTHDR(&msg); c; c = CMSG_NXTHDR(&msg, c))
            if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_TIMESTAMPNS)
                k = (struct timespec *)CMSG_DATA(c);
        wait_ns[i] = k ? (now.tv_sec - k->tv_sec) * 1000000000L + (now.tv_nsec - k->tv_nsec) : 0;
    }

    qsort(wait_ns, n, sizeof(long), cmp);
    printf("busy_poll=%dus n=%d  p50=%ldns  p99=%ldns  max=%ldns\n", busy, n,
           wait_ns[n / 2], wait_ns[n * 99 / 100], wait_ns[n - 1]);
    return 0;
}
```

Run it on the real interface during a busy period, once with `0` and once with `50`, and keep the tail figures, not just the median. Both stamps come from the same clock on the same host, so clock sync does not matter here. A software receive stamp is taken when the driver hands the packet to the stack, so it misses time spent in the card's ring; [hardware timestamps](https://nitride.xyz/docs/receiver/arrival-time#hardware-timestamps) cover that part. Loopback tests prove the code works and nothing else: there is no device queue to poll, so both runs look alike.

## Rung one: batch the reads
`recvmmsg` (Linux 2.6.33 and later) fills an array of datagrams in one system call, and recvmmsg(2) is modest about it: it has performance benefits for some applications. A shred receiver is one of them, because bursts leave many datagrams queued at once. Our [sockets page](https://nitride.xyz/docs/receiver/linux-sockets#recvmmsg) has a tested C loop, including the `MSG_WAITFORONE` flag and the warning to pass no timeout. Do this before anything else on the list. It is cheap and keeps the normal socket API.

## Rung two: busy polling
Normally a datagram arrives, the card raises an interrupt, NAPI processes the queue in softirq context, and your sleeping thread is woken. Busy polling lets a process check for incoming packets before the device interrupt fires, in the words of the kernel's NAPI docs, which are equally plain about the price: it trades CPU cycles for lower latency.

There are two switches:

- **Per socket:** `SO_BUSY_POLL`, the approximate time in microseconds to busy poll on a blocking receive when no data is waiting. socket(7) says raising it needs `CAP_NET_ADMIN`, and that it applies only when the socket's last data came from a device that supports it.
- **Global:** `net.core.busy_read` sets that default for socket reads and `net.core.busy_poll` covers `poll` and `select`. The sysctl docs suggest 50 for `busy_read` if you enable it globally, and warn that it will increase power usage. Both need a kernel built with `CONFIG_NET_RX_BUSY_POLL`; on some container hosts the files are missing.

Newer kernels add `SO_PREFER_BUSY_POLL` and `SO_BUSY_POLL_BUDGET`, which let a busy-polling application keep interrupts deferred and control how much work each poll does. The NAPI docs describe them along with `napi_defer_hard_irqs` and `gro_flush_timeout`. They are worth reading once the simple switch has shown a gain.

Busy polling only pays when the thread that polls owns a core and the feed lands on the queue it polls. A single shred flow hashes to one receive queue, and `SO_INCOMING_NAPI_ID` tells you which. Pin the receive thread, keep the decoder elsewhere, and rerun the program above. If the tail of the wait distribution does not move, turn it off again and spend the core on something else.

## Rung three: AF_XDP
AF_XDP is the first rung that leaves the socket API. The kernel docs describe it as an address family optimized for high performance packet processing. An XDP program attached to the interface redirects chosen frames into a socket through an `XSKMAP`. The frames land in a UMEM, a region of user memory divided into equal-sized frames, and four rings (fill, completion, RX and TX) pass ownership of those frames between the kernel and your program.

What that means in practice for a receiver:

- **You parse Ethernet, IP and UDP yourself.** Redirected frames skip the kernel's UDP layer, so the socket counters you used to watch no longer see them. Count drops in your own code.
- **A socket is bound to one queue.** The docs are explicit that only the XDP program running for that device and queue id can pass data to it, and suggest using ethtool to steer your traffic to a single queue. Traffic you do not redirect continues into the normal stack, so SSH and everything else keep working.
- **Zero copy depends on the driver.** Binding with `XDP_ZEROCOPY` uses it or fails. Without zero-copy support the socket uses copy mode (`XDP_COPY`), and without native XDP the program runs in generic `XDP_SKB` mode; both give up much of the point.
- **The need_wakeup flag matters.** With `XDP_USE_NEED_WAKEUP`, the kernel tells you when a system call is required to keep packets moving, which lets a polling loop avoid needless calls.

The kernel docs recommend libbpf unless you have become a power user. Take that advice. AF_XDP fits a team that already has a dedicated core, a card with native XDP support, and a measured problem that rung two did not solve.

## Rung four: DPDK
DPDK takes the card away from the kernel. Its poll mode drivers access receive and transmit descriptors directly in a polling mode without relying on interrupts, on cores assigned to DPDK. Before an application runs, most devices must be bound to `vfio-pci`; ports left under kernel control are ignored. The DPDK Linux drivers guide asks for IOMMU support in the kernel and BIOS, and calls the no-IOMMU fallback inherently unsafe. Some cards use a bifurcated driver that stays bound to the kernel while DPDK handles the data path, which softens that.

For a receiver this means a core that spins at all times, a card or port dedicated to the feed, hugepage memory, and your own UDP parsing, gap tracking and monitoring, because none of the kernel's tools see that port any more. It is the right tool for someone building network appliances. For one shred stream, the case is strongest for teams that already run DPDK for other traffic and can add one more queue.

## So when is it worth it?
Walk the ladder in order and stop at the first rung where the numbers go quiet.

1. **Socket counters climb.** Fix buffers and the loop first, then add `recvmmsg`. Many drop problems end here.
2. **No drops, but the wait before your read has a long tail that matters to you.** Try busy polling on a pinned core and compare runs of the program above.
3. **The receive core is still saturated by kernel work at peak rate**, with buffers, batching and busy polling in place. Now AF_XDP is a reasonable project.
4. **You already operate DPDK.** Then adding the feed there may be simpler than adding a second stack.

On a cloud VPS, steal time and a virtual card usually decide the result before any of this does; [whether a VPS can keep up](https://nitride.xyz/blog/can-a-vps-keep-up) is the better first read there. And none of these rungs changes what UDP is: loss is still final and order still yours to rebuild, which [why serious feeds use plain UDP](https://nitride.xyz/blog/why-plain-udp) argues is the right trade.

The same logic applies to plan choice. If you count every step of the path, note that Ultra Low Latency Shreds travels directly from our server to yours with no relay hop, while Standard Shreds is delivered through our relay; the [plan comparison](https://nitride.xyz/compare/ultra-vs-standard-shreds) and the [MEV searching use case](https://nitride.xyz/use-cases/mev-searching) explain where that matters. Measure it on your machine, with your receiver, before you tune anything.

Each plan has a free 2-hour trial, once per person. Email support@nitride.xyz with the IPv4 address and UDP port you want to test. We verify you receive on that address, then your 2 hours start.
