virtio-go

module
v0.0.0-...-1028ecd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: MIT

README

virtio-go

This repository provides a collection of Go packages related to TUN/TAP, VirtIO, Virtqueues and the kernel-level vhost devices. All fully implemented in native Go.

These packages serve the purpose to make it possible to write Go code that can send and receive packets over the vhost-net device within the Linux kernel without having to actually start a real virtual machine.

This implementation is not designed to be used in production data paths and is not optimized for this use case. It is rather a useful tool to experiment with the vhost-net implementation, to test offloads, or to build automated integration tests.

Packages

tuntap

vhost-net is often used in conjunction with Linux TAP devices, therefore this package provides simple utilities to create such devices from Go and write and read packets into/from them.

You can find an example for this in tuntap/device_test.go.

virtio

This package is a collection of virtio-related definitions which are used by some of the other packages.

virtqueue

The communication between virtio devices and drivers is based on queues called "virtqueues". This package implements the driver-side of a split virtqueue which is the queue variant with the widest support. This queue implementation is designed to work with any virtio device, not just with the vhost implementation within the Linux kernel. But please note that it was only tested with the latter one.

vhost

While the kernel-level virtio server within the Linux kernel provides multiple accelerated devices for networking, storage and more, they all share some of the same ioctls. This package contains the device-type independent ones.

vhost-net

The Linux kernel can act as an accelerated virtio networking device using the vhost-net implementation. This package contains simple to use helper methods to configure an instance of it, register the virtqueues and transmit and receive packets.

Strictly speaking, this Go package implements neither a virtio driver, nor a device, at least not in the proper sense. It is also not a replacement for qemu or even uses virtualization in any way. It rather allows to communicate with a vhost device as if it was a virtio driver within a VM guest. The handy part about this is, that this allows direct access to the vhost-net device and especially the virtio_net_hdr struct to be able to experiment with offloads.

For a comprehensive example for initializing the vhost-net device, transmitting and receiving packets, as well as using the virtio_net_hdr to configure offloads, please check out vhostnet/device_test.go.

Testing

This project uses virtrun to run tests inside a virtual environment. This way the interaction with the kernel can be tested without requiring root privileges and messing with the local system.

The tests can be run like this:

go test -exec \
"go tool virtrun -verbose \
-kernel /path/to/kernel/6.16/vmlinuz \
-addModule /path/to/kernel/6.16/drivers/net/tap.ko.zst \
-addModule /path/to/kernel/6.16/drivers/vhost/vhost_iotlb.ko.zst \
-addModule /path/to/kernel/6.16/drivers/vhost/vhost.ko.zst \
-addModule /path/to/kernel/6.16/drivers/vhost/vhost_net.ko.zst \
-smp 2" \
./...

The quickest way to obtain the needed kernel image and modules is to extract them from packages in the Ubuntu mainline PPA.

Disclaimer

This library is provided as an open source repository in the hope that it is helpful. Hetzner does not provide official support for the contents of this repository.

Directories

Path Synopsis
internal
Package tuntap provides methods to create TUN/TAP devices and send and receive packets on them.
Package tuntap provides methods to create TUN/TAP devices and send and receive packets on them.
Package vhost implements the basic ioctl requests needed to interact with the kernel-level virtio server that provides accelerated virtio devices for networking and more.
Package vhost implements the basic ioctl requests needed to interact with the kernel-level virtio server that provides accelerated virtio devices for networking and more.
Package vhostnet implements methods to initialize vhost networking devices within the kernel-level virtio implementation and communicate with them.
Package vhostnet implements methods to initialize vhost networking devices within the kernel-level virtio implementation and communicate with them.
Package virtio contains some generic types and concepts related to the virtio protocol.
Package virtio contains some generic types and concepts related to the virtio protocol.
Package virtqueue implements the driver-side for a virtio queue as described in the specification: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-270006 This package does not make assumptions about the device that consumes the queue.
Package virtqueue implements the driver-side for a virtio queue as described in the specification: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-270006 This package does not make assumptions about the device that consumes the queue.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL