Four Ways a Packet Moves: DMA, Express and CTPIO with ef_vi
In low latency trading, the native network stack is often the slowest part of the path between a market data packet arriving and an order going out. With a normal BSD socket, each packet raises an interrupt, passes through the kernel’s protocol stack, wakes your thread, and is finally copied out of kernel buffers by a recv() system call. Together these cost several microseconds, often more than your application spends on the packet.
Solarflare NICs (now AMD) are built for this. Besides acting as a normal Ethernet card, they let user-space programs interact with the NIC hardware directly, with no kernel involved. ef_vi is the Layer 2 user-space API that gives raw, direct access to the network adapter. It provides a virtual interface (VI): its own slice of the NIC with a receive queue, transmit queue and event queue. You send and receive raw Ethernet frames and poll for completion events, with no sockets, no TCP/IP stack and no kernel on the data path. At the cost of building the packet headers, managing buffers and keeping up with the hardware, you get to control every step between the wire and your code.
The catch is that what “every step” looks like depends on the card. On the older x2 series, the model is descriptor rings and DMA. The newer x4 has two datapaths on the same card: an enterprise path that behaves like x2, and an express path built around a different idea of who owns the packet buffers and how bytes reach the wire.
We are going to use small code snippets to illustrate the four paths:
- DMA receive: the classic ring-based model
- DMA send: the NIC reads your frame from memory
- Express receive: the NIC writes into shared buffers and lends you packets by reference
- CTPIO send: the CPU writes the frame straight into the NIC
This blog will skip most boilerplate setup code and focus on two questions for each path:
- Where do the packet bytes live?
- How do the CPU and NIC tell each other what’s happening?
Once the answers to these questions are clear, the API calls follow, and so does the reason the express path is faster.
Setup in one minute
Firstly, a quick introduction to the data structure terminology in the world of ef_vi:
| Data structure | What it is | Which question it answers |
|---|---|---|
ef_driver_handle |
Open handle to the driver | (plumbing) |
ef_pd (protection domain) |
Your memory “namespace” on the NIC and data path choice | Which data path and memory the NIC may touch |
ef_memreg |
Your buffer, pinned and registered so the NIC can DMA in and out of it | Where the packet bytes live (on the DMA path) |
ef_vi |
Your RX queue, TX queue and event queue | How descriptors go in and events come out |
ef_filter_spec |
Rules that steer matching packets to your VI | Which packets reach you at all |
struct efvi_ctx {
ef_driver_handle dh;
ef_pd pd;
ef_memreg mr;
ef_vi vi;
ef_filter_spec filter_spec;
char *mem;
int multicast_join_fd;
};
You’d typically wrap these into a small struct like above for managing the ef_vi. Choosing the datapath is a single flag when allocating the ef_pd: EF_PD_DEFAULT for DMA, EF_PD_EXPRESS for express. In a DMA path you’d usually need to register memory with the NIC first as follows. Registration pins the pages, so the kernel can’t swap out or move the physical memory behind the virtual addresses.
#define BUF_SIZE 2048 /* one buffer per packet, 2 KB slots */
#define NUM_BUFS 256
static inline char *buf_ptr(struct efvi_ctx *ctx, int i) {
return ctx->mem + (size_t)i * BUF_SIZE; // CPU's name (virtual)
}
static inline ef_addr buf_dma(struct efvi_ctx *ctx, int i) {
return ef_memreg_dma_addr(&ctx->mr, (size_t)i * BUF_SIZE); // NIC's name (DMA address)
}
posix_memalign((void **)&ctx->mem, 4096, BUF_SIZE * NUM_BUFS);
ef_memreg_alloc(&ctx->mr, ctx->dh, ctx->pd, ctx->mem,
BUF_SIZE * NUM_BUFS);
One extra point about receive UDP multicast packet. A switch won’t forward multicast to your port unless the host has joined the group with IGMP, but ef_vi has no IGMP. The common trick is to open a UDP socket on the side, join the multicast group with IP_ADD_MEMBERSHIP, and keep it alive along the lifetime of this ef_vi.
With a VI allocated and packets steered to it, let’s look at the first path: DMA receive.
DMA Receive
Firstly we need to initialize and submit RX descriptors to the NIC so that NIC knows it can write the incoming packets into this descriptor’s DMA buffer space.
for (int i = 0; i < NUM_BUFS; i++) {
ef_vi_receive_init(&ctx.vi, buf_dma(&ctx, i), i);
}
ef_vi_receive_push(&ctx.vi);
Once this step is done, we need to actively poll the event queue to process the incoming packets and return back the RX descriptors to the NIC before it runs out of posted buffer and overflows. Typically it’s arranged as such a loop below. It polls from the event queue and finds the RX event and its associated dma_id tag, through the dma_id we could find the bytes of the packet and process it.
Each RX event carries the dma_id and the number of bytes received. The NIC writes a small prefix (metadata such as the hardware timestamp) in front of the frame, and the byte count includes it, so the Ethernet frame starts at buf_ptr(&ctx, dma_id) + ef_vi_receive_prefix_len(&ctx.vi).
ef_event evs[16];
int r = ef_eventq_poll(&ctx.vi, evs, 16);
for (int i = 0; i < r; i++) {
switch (EF_EVENT_TYPE(evs[i])) {
case EF_EVENT_TYPE_RX: {
int dma_id = EF_EVENT_RX_RQ_ID(evs[i]);
handle_packet(buf_ptr(&ctx, dma_id),
EF_EVENT_RX_BYTES(evs[i]));
ef_vi_receive_init(&ctx.vi, buf_dma(&ctx, dma_id), dma_id);
break;
}
default:
fprintf(stderr, "unexpected event type %d\n",
EF_EVENT_TYPE(evs[i]));
break;
}
}
// Ideally stage a few more before ringing the doorbell.
if (r) {
ef_vi_receive_push(&ctx.vi);
}
DMA Send
As with DMA receive, you first register memory with the NIC. Afterwards, since ef_vi works at raw Layer 2, you have to build the full Ethernet frame yourself, including the Ethernet, IP and UDP headers. Checksums are still offloaded to the NIC by default. (With a BSD socket you only provide the payload and the kernel builds the rest for you.) After the tedious work of building the full packet, the send is actually pretty straightforward:
ef_vi_transmit(&ctx.vi, buf_dma(&ctx, dma_id), len, dma_id);
ef_event evs[16];
ef_request_id ids[EF_VI_TRANSMIT_BATCH];
int r = ef_eventq_poll(&ctx.vi, evs, 16);
for (int i = 0; i < r; i++) {
switch (EF_EVENT_TYPE(evs[i])) {
case EF_EVENT_TYPE_TX: {
int n = ef_vi_transmit_unbundle(&ctx.vi, &evs[i], ids);
// ids[0..n) are free to reuse again.
break;
}
default:
fprintf(stderr, "unexpected event type %d\n",
EF_EVENT_TYPE(evs[i]));
break;
}
}
ef_vi_transmit writes a TX descriptor and rings the doorbell. After the doorbell, the NIC DMA-reads the descriptor and then the frame across PCIe, sends it and writes a TX event. That’s a round trip over PCIe (doorbell out, descriptor and frame read back). This is what the CTPIO capability aims to improve.
Notice one EF_EVENT_TYPE_TX can carry transmission completion information for multiple packets, hence the array of ef_request_id to be unbundled.
Express Receive
Recall that the express datapath is selected by passing EF_PD_EXPRESS when allocating the PD:
ef_pd_alloc_by_name(&ctx.pd, ctx.dh, iface, EF_PD_EXPRESS);
ef_memreg_alloc is no longer needed for receive, because on the express path the driver and NIC allocate and manage the buffers and only lend you a read-only view of them.
ef_event evs[16];
int r = ef_eventq_poll(&ctx.vi, evs, 16);
for (int i = 0; i < r; i++) {
switch (EF_EVENT_TYPE(evs[i])) {
case EF_EVENT_TYPE_RX_REF: {
unsigned int pkt_id = evs[i].rx_ref.pkt_id;
size_t len = evs[i].rx_ref.len;
const char *pkt = efct_vi_rxpkt_get(&ctx.vi, pkt_id);
handle_packet(pkt, len);
efct_vi_rxpkt_release(&ctx.vi, pkt_id);
break;
}
default:
fprintf(stderr, "unexpected event type %d\n",
EF_EVENT_TYPE(evs[i]));
break;
}
}
The main difference from DMA receive is that you no longer manage buffers. The driver allocates a few large buffers called superbufs (typically 1 MB each, split into fixed-size packet slots), and the NIC fills the slots one after another. efct_vi_rxpkt_get lends you a read-only pointer to your packet’s slot, and efct_vi_rxpkt_release gives it back. You need to release promptly: a superbuf only returns to the NIC once every packet in it has been released, so holding one packet pins the whole buffer and can starve the NIC.
The notification mechanism changes too. On the DMA path, the NIC writes to two places for each packet: the frame into your buffer, then an RX event into the event queue. The CPU polls the event queue and follows the event to the buffer.
On the express path there is no separate event write. Each slot starts with a small metadata header (length, timestamp, status) containing a sentinel bit. ef_eventq_poll watches the next slot directly, and when its sentinel flips, ef_vi creates the RX_REF event in software. The code looks the same as before, but the NIC never wrote an event. That saves one PCIe write per packet.
CTPIO Send
With CTPIO the NIC never reads from host memory: the CPU pushes the frame bytes over MMIO, so the PCIe round trip from DMA send disappears. Note that with CTPIO you have to compute the IP checksum yourself before sending (the UDP checksum is optional on IPv4).
On X2 card, a CTPIO send can fail, which is why ef_vi_transmit_ctpio is immediately followed by ef_vi_transmit_ctpio_fallback, which provides a DMA copy as a backup. On x4 express, once you’ve checked there is enough space in the CTPIO TX FIFO, the send won’t fail, and _fallback only tags your dma_id on the completion.
The snippet below uses full store-and-forward mode, which keeps the code simple: the NIC buffers the whole frame before sending, so a write that stalls halfway can’t put a broken frame on the wire. The true low-latency path is cut-through, where the NIC starts sending once the first CTPIO-threshold bytes of the packet ready while the CPU is still writing the rest.
static int send_one_ctpio(struct efvi_ctx *ctx, int buf_id,
size_t len, ef_request_id dma_id) {
if (ef_vi_transmit_space_bytes(&ctx->vi) < (int)len) {
return -EAGAIN;
}
ef_vi_transmit_ctpio(&ctx->vi, buf_ptr(ctx, buf_id), len, EF_VI_CTPIO_CT_THRESHOLD_SNF); // uses store-and-forward here
// x2: DMA backup; x4 express: just tags dma_id on completion.
ef_vi_transmit_ctpio_fallback(&ctx->vi, buf_dma(ctx, buf_id),
len, dma_id);
return 0;
}
The TX completion loop is essentially the same as for DMA send. The only addition is that you can use EF_EVENT_TX_CTPIO(evs[i]) to check whether the packet actually went out via CTPIO or fell back to DMA (on x2).
Summary
Going back to the two questions we asked at the beginning, here is a quick comparison of the four paths.
| DMA receive | Express receive | DMA send | CTPIO send | |
|---|---|---|---|---|
| Where do the bytes live? | Your registered buffer | Superbuf owned by the driver; you only get a read-only pointer | Your registered buffer; NIC reads it from host memory | CPU writes it directly into the NIC’s FIFO |
| CPU tells NIC | Post RX descriptors and ring the doorbell | Release the pkt_id |
Write TX descriptor and ring the doorbell | MMIO write of the frame itself |
| NIC tells CPU | Writes the frame, then an RX event | Writes the frame and metadata together; CPU polls the sentinel bit | TX completion event | TX completion event |
| PCIe on critical path | One-way writes | One-way writes, one less write per packet | Round trip (NIC reads descriptor and frame) | One-way writes |
| When can buffer be reused? | After you recycle it | After you release it | After TX completion | Right after the CTPIO write returns (store-and-forward, x4) |
| Flow control | RX descriptors | Superbufs | TX descriptors | Bytes of FIFO space |
| Cards | x2, x4 enterprise | x3, x4 express | x2, x4 enterprise | x2 (with DMA fallback), x3, x4 express |
In short, the DMA paths are all about descriptors: you and the NIC pass buffer addresses back and forth and the NIC moves the bytes. The express paths take that bookkeeping out of the critical path. On receive, the NIC writes into a shared buffer and you watch it directly. On send, the CPU pushes the frame straight into the NIC. Fewer PCIe trips and less per-packet work is basically where the latency saving comes from.