vnet

package module
v0.0.0-...-733a429 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: BSD-3-Clause Imports: 23 Imported by: 1

README

VirtIO Network driver

This Go package implements TCP/IP connectivity through VirtIO Networking to be used with GOOS=tamago as supported by the TamaGo framework for bare metal Go on AMD64/ARM/RISC-V processors.

The package supports TCP/IP networking through gVisor (go branch) tcpip stack pure Go implementation.

The interface TCP/IP stack can be attached to the Go runtime by setting net.SocketFunc to the interface Socket function:

// VirtIO over MMIO
dev := &vnet.Net{
	Transport: &virtio.MMIO{
		Base: vm.VIRTIO_NET0_BASE,
	}
}

// VirtIO over PCI
dev := &vnet.Net{
	Transport: &virtio.PCI{
		Device: pci.Probe(
			0,
			vm.VIRTIO_NET_PCI_VENDOR,
			vm.VIRTIO_NET_PCI_DEVICE,
		),
	}
}

// VirtIO interface
iface := vnet.Interface{}

// initialize IP, Netmask, Gateway
_ = iface.Init(dev, "10.0.0.1", "255.255.255.0", "10.0.0.2")

// Go runtime hook
net.SocketFunc = iface.Socket

See tamago-example for a full integration example.

Authors

Andrea Barisani
andrea@inversepath.com

Andrej Rosano
andrej@inversepath.com

Documentation

The package API documentation can be found on pkg.go.dev.

For more information about TamaGo see its repository and project wiki.

License

tamago | https://github.com/usbarmory/virtio-net
Copyright (c) The virtio-net authors. All Rights Reserved.

These source files are distributed under the BSD-style license found in the LICENSE file.

Documentation

Overview

Package vnet implements TCP/IP connectivity through a VirtIO (version 1.2) network device.

The TCP/IP stack is implemented using gVisor pure Go implementation.

This package is only meant to be used with `GOOS=tamago` as supported by the TamaGo framework for bare metal Go, see https://github.com/usbarmory/tamago.

Index

Constants

View Source
const (
	DeviceID   = 0x01
	ConfigSize = 12
)

Device parameters

View Source
const (
	// receiveq1
	ReceiveQueue = 0
	// transmitq1
	TransmitQueue = 1
)

virtual queue pairs

View Source
const (
	STATUS_DOWN = 0
	STATUS_UP   = 1

	SPEED_MIN = 0x00000000
	SPEED_MAX = 0x7fffffff

	DUPLEX_HALF = 0x00
	DUPLEX_FULL = 0x01

	HASH_TYPE_IPV4   = 1 << 0
	HASH_TYPE_TCPV4  = 1 << 1
	HASH_TYPE_UDPV4  = 1 << 2
	HASH_TYPE_IPV6   = 1 << 3
	HASH_TYPE_TCPV6  = 1 << 4
	HASH_TYPE_UDPV6  = 1 << 5
	HASH_TYPE_IP_EX  = 1 << 6
	HASH_TYPE_TCP_EX = 1 << 7
	HASH_TYPE_UDP_EX = 1 << 8
)

Configuration constants

View Source
const (
	FeatureChecksum    = (1 << 0)
	FeatureMTU         = (1 << 3)
	FeatureMAC         = (1 << 5)
	FeatureStatus      = (1 << 16)
	FeatureSpeedDuplex = (1 << 63)

	DriverFeatures = FeatureChecksum | FeatureMTU | FeatureMAC | FeatureStatus | FeatureSpeedDuplex
)

Supported Features

View Source
const (
	NeedsChecksum = 0
)

Header flags

Variables

View Source
var (
	// MTU represents the Maximum Transmission Unit
	MTU = 1518

	// NICID represents the default gVisor NIC identifier
	NICID = tcpip.NICID(1)

	// DefaultStackOptions represents the default gVisor Stack configuration
	DefaultStackOptions = stack.Options{
		NetworkProtocols: []stack.NetworkProtocolFactory{
			ipv4.NewProtocol,
			arp.NewProtocol},
		TransportProtocols: []stack.TransportProtocolFactory{
			tcp.NewProtocol,
			icmp.NewProtocol4,
			udp.NewProtocol},
	}
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// MAC represents the interface physical address.
	MAC [6]byte
	// Status represents the driver status.
	Status uint16
	// MaxVirtualQueuePairs is the Maximum number of tx/rx queues.
	MaxVirtualQueuePairs uint16
	// MTU represents the Ethernet Maximum Transmission Unit.
	MTU uint16
}

Config represents a VirtIO network device configuration

type Header struct {
	Flags      uint8
	GSOType    uint8
	HdrLen     uint16
	GSOSize    uint16
	CSumStart  uint16
	CSumOffset uint16
	NumBuffers uint16 // not used in legacy drivers
}

Header represents a VirtIO network device header (virtio_net_hdr)

func (*Header) Bytes

func (d *Header) Bytes() []byte

Bytes converts the descriptor structure to byte array format.

type Interface

type Interface struct {
	NICID tcpip.NICID
	NIC   *NIC

	Stack *stack.Stack
	Link  *channel.Endpoint
}

Interface represents an Ethernet interface instance.

func (*Interface) DialContextTCP4

func (iface *Interface) DialContextTCP4(ctx context.Context, address string) (net.Conn, error)

DialContextTCP4 connects to an IPv4 TCP address with support for timeout supplied by ctx.

func (*Interface) DialTCP4

func (iface *Interface) DialTCP4(address string) (net.Conn, error)

DialTCP4 connects to an IPv4 TCP address.

func (*Interface) DialUDP4

func (iface *Interface) DialUDP4(lAddr, rAddr string) (net.Conn, error)

DialUDP4 creates a UDP connection to the ip:port specified by rAddr, optionally setting the local ip:port to lAddr.

func (*Interface) EnableICMP

func (iface *Interface) EnableICMP() error

EnableICMP adds an ICMP endpoint to the interface, it is useful to enable ping requests.

func (*Interface) Init

func (iface *Interface) Init(nic *Net, ip string, netmask string, gateway string) (err error)

Init initializes a VirtIO Network interface associating it to a gVisor link, a default NICID and TCP/IP gVisor Stack are set if not previously assigned.

func (*Interface) ListenerTCP4

func (iface *Interface) ListenerTCP4(port uint16) (net.Listener, error)

ListenerTCP4 returns a net.Listener capable of accepting IPv4 TCP connections for the argument port.

func (*Interface) Socket

func (iface *Interface) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)

Socket can be used as net.SocketFunc under GOOS=tamago to allow its use internal use within the Go runtime.

type NIC

type NIC struct {
	// Link is a gVisor channel endpoint
	Link *channel.Endpoint

	// Device is the physical interface associated to the virtual one.
	Device *Net
	// contains filtered or unexported fields
}

NIC represents an virtual Ethernet instance.

func (*NIC) Init

func (nic *NIC) Init() (err error)

Init initializes a virtual Ethernet instance bound to a physical Ethernet device.

func (*NIC) Rx

func (nic *NIC) Rx(buf []byte)

Rx receives a single Ethernet frame from the virtual Ethernet instance.

func (*NIC) Tx

func (nic *NIC) Tx() (buf []byte)

Tx transmits a single Ethernet frame to the virtual Ethernet instance.

type Net

type Net struct {
	sync.Mutex

	// Controller index
	Index int
	// VirtIO Transport instance
	Transport virtio.VirtIO
	// Interrupt ID
	IRQ int

	// Incoming packet handler
	RxHandler func([]byte)

	// HeaderLength allows to override the VirtIO network device header
	// length as some implementations, such as QEMU, omit certain fields.
	HeaderLength int

	// Maximum Transmission Unit
	MTU uint16
	// contains filtered or unexported fields
}

Net represents a VirtIO network device instance.

func (*Net) Config

func (hw *Net) Config() (config Config)

Config returns the network device configuration.

func (*Net) Init

func (hw *Net) Init() (err error)

Init initializes the VirtIO network device.

func (*Net) Rx

func (hw *Net) Rx() []byte

Rx receives a single network frame, excluding the checksum, from the MAC controller ring buffer.

func (*Net) Start

func (hw *Net) Start(rx bool)

Start begins processing of incoming packets. When the argument is true the function waits and handles received packets (see Rx()) through RxHandler() (when set), it should never return.

func (*Net) Tx

func (hw *Net) Tx(buf []byte)

Tx transmits a single network frame, the checksum is appended automatically and must not be included.

Jump to

Keyboard shortcuts

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