nos_
ENRU

10 Gbit/s on an old Mellanox ConnectX-3: the driver, the bugs, and what's next

How we wrote a Rust ConnectX-3 driver for nos, got UDP close to line rate, dealt with firmware, queues and MSI-X, and why TCP and NBS still have room to improve.

7 min read
HardwareNetworkingDriversPerformanceTestingRustx86-64
Читать по-русски

We had three identical Supermicro X9 servers and a job to do: connect them with a fast private network for NBS and virtual networks. Each server had a Mellanox ConnectX-3. These cards are old, but they can still push 10 Gbit/s. We just had to teach nos to talk to them.

We kept the problem to Ethernet. Our stack needs neither InfiniBand nor RDMA, so this was always going to be a driver for IP packets. All three hosts got addresses on the card's second port, in a separate VLAN 3515. The first port stayed on the regular network for SSH.

Why not reuse a driver

nos has no Linux drivers that you can simply drop in. Its network drivers are written in Rust and live inside the kernel. They take frames from the shared network stack, hand received frames back to it, and work with device memory through DMA.

That meant understanding the card itself. ConnectX-3 does much more than consume descriptors from shared memory. It has firmware with its own command interface, context pages, queues and initialization rules. We used hardware documentation and the behavior of the Linux and FreeBSD drivers, but wrote our implementation from scratch. It is separate MIT-licensed code, not a port of somebody else's source.

The first working driver landed on November 28, with PCI IDs 15b3:1003 and 15b3:1007, covering ConnectX-3 and ConnectX-3 Pro.

How the card talks to the kernel

The packet path looks like this:

nos IP stack -> TX QP -> card -> network
network -> card -> RX CQ -> polling/softirq -> nos IP stack
                       |
                       +-> EQ: errors and link changes

QP, Queue Pair, is the send and receive work queue. CQ, Completion Queue, says what happened to that work. EQ, Event Queue, carries errors and events such as link changes.

There is one pair per port. RX holds 256 descriptors, TX 128. A frame buffer is 2 KiB, and MTU is the usual 1500 bytes. To transmit, the driver builds a small WQE containing the buffer address, publishes it and writes to a doorbell register. The card fetches the work with DMA. Reception comes back through the RX CQ.

No giant packets are involved. The speed comes from treating queues, memory and interrupts carefully.

Firmware comes first

Before the first frame, the driver has to claim the device, reset it, start its firmware, allocate memory for the firmware's context and only then create Ethernet queues. Every object has to be destroyed in reverse order on shutdown, or the next boot can find the card in an unexpected state.

Memory was a problem of its own. Firmware needs physically contiguous regions, and our allocator used to be limited to smaller pieces. We extended contiguous allocation to 4096 pages, or 16 MiB, and moved zeroing of large blocks out from under the lock. That helped more than mlx4: a large allocation no longer stalls every other allocation for a long time.

Firmware 2.42.5000 had another surprise. It advertised PORT_REMAP but rejected the FPP bit while moving the TX QP into INIT2RTR. The driver now retries that transition without the bit. Both ports then came up at 10G.

We added mlxdump for diagnostics. It shows queue state and mailbox contents without writing anything to the card. One small detail mattered a lot: at first we read QP flags at offset zero and always got zero. QUERY_QP returns its parameter mask first, and the actual context starts at 0x08.

The error that looked like an unstable card

Short runs reached 9.9 Gbit/s almost immediately. But longer runs sometimes ended with CQE 0x02 and vendor code 0x6d, after which the TX QP entered its error state. It looked like a queue overrun or a hardware fault.

In fact, we were publishing descriptors in pieces. owner_opcode and doorbell fields were initially written one byte at a time. The card could read half of a new value and half of an old one. Now every 16- and 32-bit field goes out in a single aligned volatile store. That fix closed the torn publication.

The four doorbell records also shared one 64-byte cache line. RX and TX CPUs were slowing each other down even without a logical race. We moved them to separate lines.

We added a guard against bad completions too. A completion can no longer advance the TX consumer past the work the producer had actually sent. It marks the QP broken instead of silently wrapping the counters.

With these changes, all three directions passed 30-second full-speed runs without TX errors. They sent 23.5-24.0 million frames and delivered 97-99%.

What we mean by 10 Gbit/s

Send rate and useful delivery are not the same thing. We measured both on three physical servers, not in QEMU.

Check Result
Short single-source transmit 9.9 Gbit/s
Delivery over 30 seconds 9.4-9.5 Gbit/s, 97-99% of frames
TX errors on those runs 0
Ping, two concurrent runs 0.026-0.129 ms
Raw TCP 307 MiB/s
TLS 1.3 between hosts 259-267 MiB/s
NBS writes to three replicas 204.2 MiB/s
NBS reads 424.1 MiB/s

So UDP really did get close to 10G line rate. TCP and TLS are substantially slower, and replicated storage has its own costs. These numbers do not justify saying "10G for everything".

Interrupts and polling work together

The first version polled the card every 10 ms. Under load that meant only 6,400 packets per second, and ping sometimes waited 9-19 ms. Replacing everything with permanent polling would have been wasteful too: an idle server would burn CPU for no reason.

The result is a hybrid:

  • with little traffic, the EQ and RX CQ raise MSI-X;
  • the handler masks the interrupt and wakes the receive softirq;
  • one pass handles at most 64 frames;
  • if the queue is still full, the next pass runs immediately and the interrupt stays masked;
  • when the queue drains, MSI-X is enabled again;
  • if a polling task cannot be created, a 1 ms timer remains as a fallback.

That change brought ping back to 0.026-0.129 ms. A dedicated task later began checking RX and TX every millisecond so completions would not wait for the next frame. On the same hardware, we got fast replies when idle and nearly line rate under load.

What about TCP, TLS and NBS

UDP was already fast when TCP/TLS moved only a few megabytes per second. Fixing the card's queues was not enough there. We removed an extra copy of decrypted data by returning it straight into the caller's buffer, and accelerated polling. Raw TCP reached 307 MiB/s, and pinned TLS reached 259-267 MiB/s. That stage is described in this commit.

NBS adds replication, request windows, metadata writes and disk I/O. For hypervisor volumes, sending each write to every replica directly turned out to be better than waiting for a chain of acknowledgements. On the three hosts, that produced 204.2 MiB/s of writes and 424.1 MiB/s of reads.

That does not mean the network no longer matters. It means it is no longer the only bottleneck on this stand.

The surprising copy experiment

Byte-wise copying looked like the obvious thing to optimize. We replaced it with ptr::copy and got no speedup at all. Throughput fell to 1.1 million frames in 30 seconds. Restoring scalar volatile copies brought back 23.9 million.

So the experiment stayed out of the main branch. There is no pretty universal rule here that bulk copies are always faster. On this kernel, compiler and packet path, the measured result was different, and that is the result we kept.

How we tested it

The host test covers BAR formats, multicast tables and SFP/QSFP decoding. But nothing emulates ConnectX-3, so most of the work happened on the three real Supermicro X9 machines.

We brought up both ports first, with static IPv4, ARP, ICMP and SSH. Then the second port formed a three-way ipcnet network inside the VLAN. After that came short and long UDP runs in every direction, four senders to one host, a TCP/TLS ring and NBS. Linux-to-Linux traffic on the same VLAN acted as a control: no TX errors in 20 seconds.

One useful check sounds boring: ping still answered after 30 seconds of load. A network that is fast only while you leave it alone is not what we need.

What is still missing

  • One RX/TX queue pair per port. No RSS and no spreading receive work across CPUs.
  • MTU 1500. No jumbo frames.
  • No InfiniBand and no RDMA. That is a deliberate scope limit, not an unfinished mode.
  • Hardware segmentation and checksum offloads are not used yet.
  • Multiple senders and paced traffic receive unevenly. One paced run delivered 1.27 million of 3.0 million frames, another 9.92 of 20.79 million.
  • Link loss and recovery still need a dedicated hardware test.
  • A longer storage soak is needed. A few good minutes under load are not proof of production reliability.

Those last two points matter most. A fast driver and a driver you can trust with storage for months are different stages of the work.

What comes next

First we need to explain uneven receive and show exactly where frames disappear: in the card, the softirq queue, the buffer pool or CPU load. Then more queues and spreading them across CPUs make sense.

After that come jumbo frames, segmentation offload, fewer copies and more flexible interrupt moderation. NBS still has work in request windows, TLS and the disk side. We now also have a full JSONL trace that can be saved straight from nos and followed from the VM to the replicas.

And of course we need long runs with link flaps, VM shutdowns, host loss and cluster recovery. The speed is there. The next goal is to make it the system's ordinary behavior.

The code lives in drivers/net/mlx4, with measurements and hardware notes in docs/testing.md. The first driver and the follow-up merges are here: initial driver, queue hardening, MSI-X + polling.