Skip to content

Commit af9d459

Browse files
randomizedcoderastro
authored andcommitted
feat(qemu): add tap.vhost option for vhost-net acceleration
1 parent 4e8cb26 commit af9d459

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ imperatively with the provided `microvm` command.
4242
- Zero, one, or more virtual tap ethernet network interfaces can be
4343
attached to a MicroVM. `qemu`, `kvmtool`, and `vfkit` also support *user*
4444
networking which requires no additional setup on the host.
45+
- For high-throughput TAP networking with `qemu`, enable `tap.vhost = true`
46+
to use vhost-net kernel acceleration (~10 Gbps vs ~1.5 Gbps without).
4547

4648
## Hypervisors
4749

doc/src/interfaces.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ with more than one CPU core.
4848
When running MicroVMs through the `host` module, the tap network
4949
interfaces are created through a systemd service dependency.
5050

51+
### vhost-net acceleration
52+
53+
For high-throughput workloads, enable vhost-net to offload packet
54+
processing to the kernel instead of QEMU userspace:
55+
56+
```nix
57+
{
58+
microvm.interfaces = [ {
59+
type = "tap";
60+
id = "vm-a1";
61+
mac = "02:00:00:00:00:01";
62+
tap.vhost = true; # Enable vhost-net (~10 Gbps vs ~1.5 Gbps)
63+
} ];
64+
}
65+
```
66+
67+
This requires the `vhost_net` kernel module on the host. The performance
68+
improvement is significant for workloads with many concurrent connections
69+
or high bandwidth requirements.
70+
71+
**Note:** Currently only supported with the `qemu` hypervisor.
72+
5173
Extend the generated script in the guest configuration like this:
5274

5375
```nix

lib/runners/qemu.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ lib.warnIf (mem == 2048) ''
296296
forwardPorts != [] &&
297297
! builtins.any ({ type, ... }: type == "user") interfaces
298298
) "${hostName}: forwardPortsOptions only running with user network" (
299-
builtins.concatMap ({ type, id, mac, bridge, ... }: [
299+
builtins.concatMap ({ type, id, mac, bridge, tap ? {}, ... }: [
300300
"-netdev" (
301301
lib.concatStringsSep "," (
302302
[
@@ -313,6 +313,9 @@ lib.warnIf (mem == 2048) ''
313313
"ifname=${id}"
314314
"script=no" "downscript=no"
315315
]
316+
++ lib.optionals (type == "tap" && tap.vhost or false) [
317+
"vhost=on"
318+
]
316319
++ lib.optionals (type == "macvtap") [ (
317320
let
318321
fds = macvtapFds.${id};

nixos-modules/microvm/options.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,19 @@ in
342342
MAC address of the guest's network interface
343343
'';
344344
};
345+
tap.vhost = mkOption {
346+
type = types.bool;
347+
default = false;
348+
description = ''
349+
Enable vhost-net for TAP interfaces.
350+
351+
When enabled, packet processing is offloaded to the kernel's
352+
vhost-net module instead of QEMU userspace, significantly
353+
improving network throughput (~10 Gbps vs ~1.5 Gbps).
354+
355+
Requires the vhost_net kernel module on the host.
356+
'';
357+
};
345358
};
346359
});
347360
};

0 commit comments

Comments
 (0)