vmnet

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT, MIT Imports: 17 Imported by: 0

Documentation

Rendered for darwin/amd64

Index

Constants

View Source
const EnableVirtioHeaderKeyUnavailableError = "vmnet_enable_virtio_header_key requires macOS 15.4 or newer SDK"
View Source
const HeaderSizeForStream = int(unsafe.Sizeof(C.uint32_t(0)))
View Source
const VirtioNetHdrSize = 12 // Size of virtio_net_hdr_v1

Variables

View Source
var (
	// AllocateMacAddressKey represents [vmnet_allocate_mac_address_key].
	//    - Allocate a MAC address for the VM to use (bool). Default value is true.
	//    - If set to false, no MAC address will be generated.
	//    - Can be used in the interface describing dictionary passed to [StartInterfaceWithNetwork] to request automatic MAC address allocation.
	//    - See <vmnet/vmnet.h> for details.
	//
	// [vmnet_allocate_mac_address_key]: https://developer.apple.com/documentation/vmnet/vmnet_allocate_mac_address_key?language=objc
	AllocateMacAddressKey = C.GoString(C.vmnet_allocate_mac_address_key)

	// EnableChecksumOffloadKey represents [vmnet_enable_checksum_offload_key].
	//    - Can be used in the interface describing dictionary passed to [StartInterfaceWithNetwork] to enable checksum offloading.
	//    - See <vmnet/vmnet.h> for details.
	//
	// [vmnet_enable_checksum_offload_key]: https://developer.apple.com/documentation/vmnet/vmnet_enable_checksum_offload_key?language=objc
	EnableChecksumOffloadKey = C.GoString(C.vmnet_enable_checksum_offload_key)

	// EnableIsolationKey represents [vmnet_enable_isolation_key].
	//    - Can be used in the interface describing dictionary passed to [StartInterfaceWithNetwork] to enable isolation.
	//    - See <vmnet/vmnet.h> for details.
	//
	// [vmnet_enable_isolation_key]: https://developer.apple.com/documentation/vmnet/vmnet_enable_isolation_key?language=objc
	EnableIsolationKey = C.GoString(C.vmnet_enable_isolation_key)

	// EnableTSOKey represents [vmnet_enable_tso_key].
	//    - Can be used in the interface describing dictionary passed to [StartInterfaceWithNetwork] to enable TCP Segmentation Offloading (TSO).
	//    - See <vmnet/vmnet.h> for details.
	//
	// [vmnet_enable_tso_key]: https://developer.apple.com/documentation/vmnet/vmnet_enable_tso_key?language=objc
	EnableTSOKey = C.GoString(C.vmnet_enable_tso_key)

	// EnableVirtioHeaderKey represents [vmnet_enable_virtio_header_key].
	//    - Can be used in the interface describing dictionary passed to [StartInterfaceWithNetwork] to enable Virtio header support.
	//    - See <vmnet/vmnet.h> for details.
	//    - Requires macOS 15.4 or newer SDK. On older SDKs, [StartInterfaceWithNetwork] will return an error if this key is used.
	//
	// [vmnet_enable_virtio_header_key]: https://developer.apple.com/documentation/vmnet/vmnet_enable_virtio_header_key-swift.var?language=objc
	EnableVirtioHeaderKey = func() string {

		if cs := C.wrap_vmnet_enable_virtio_header_key(); cs != nil {
			return C.GoString(cs)
		}
		return EnableVirtioHeaderKeyUnavailableError
	}()
)

Keys for interface describing parameters dictionary.

Functions

func ReleaseOnCleanup

func ReleaseOnCleanup[O interface{ releaseOnCleanup() }](o O) O

ReleaseOnCleanup calls releaseOnCleanup method on the given object and returns it.

func Retain

func Retain[O interface{ retain() }](o O) O

Retain calls retain method on the given object and returns it.

Types

type Interface

type Interface struct {
	Param               *xpc.Dictionary
	MaxPacketSize       uint64
	MaxReadPacketCount  int
	MaxWritePacketCount int

	// Interface Describing Parameters on starting the interface.
	AllocateMacAddress    bool
	EnableChecksumOffload bool
	EnableIsolation       bool
	EnableTSO             bool
	EnableVirtioHeader    bool
	// contains filtered or unexported fields
}

Interface represents a interface_ref in vmnet.

func StartInterfaceWithNetwork

func StartInterfaceWithNetwork(network *Network, interfaceDesc *xpc.Dictionary) (*Interface, error)

StartInterfaceWithNetwork starts an Interface with the given Network and interface describing parameter.

func (*Interface) ReadPackets

func (i *Interface) ReadPackets(v *VMPktDesc, packetCount int) (int, error)

ReadPackets reads packets from the Interface into VMPktDesc array. It returns the number of packets read.

func (*Interface) WritePackets

func (i *Interface) WritePackets(v *VMPktDesc, packetCount int) error

WritePackets writes packets to the Interface from VMPktDesc array.

type Network

type Network struct {
	// contains filtered or unexported fields
}

Network represents a vmnet_network_ref.

func NewNetwork

func NewNetwork(config *NetworkConfiguration) (*Network, error)

NewNetwork creates a new Network with NetworkConfiguration. This is only supported on macOS 26 and newer, error will be returned on older versions.

func NewNetworkFromPointer

func NewNetworkFromPointer(p *objc.Pointer) *Network

NewNetworkFromPointer creates a new Network from an existing objc.Pointer.

func NewNetworkWithSerialization

func NewNetworkWithSerialization(serialization xpc.Object) (*Network, error)

NewNetworkWithSerialization creates a new Network from a serialized representation. This is only supported on macOS 26 and newer, error will be returned on older versions.

func (*Network) CopySerialization

func (n *Network) CopySerialization() (xpc.Object, error)

CopySerialization returns a serialized copy of Network in xpc_object_t as xpc.Object.

func (*Network) IPv4Subnet

func (n *Network) IPv4Subnet() (subnet netip.Prefix, err error)

IPv4Subnet returns the IPv4 subnet of the Network.

func (*Network) IPv6Prefix

func (n *Network) IPv6Prefix() (netip.Prefix, error)

IPv6Prefix returns the IPv6 prefix of the Network.

type NetworkConfiguration

type NetworkConfiguration struct {
	// contains filtered or unexported fields
}

NetworkConfiguration represents a vmnet_network_configuration_ref.

func NewNetworkConfiguration

func NewNetworkConfiguration(mode Mode) (*NetworkConfiguration, error)

NewNetworkConfiguration creates a new NetworkConfiguration with Mode. This is only supported on macOS 26 and newer, error will be returned on older versions. [BridgedMode] is not supported by this function.

func (*NetworkConfiguration) AddDhcpReservation

func (c *NetworkConfiguration) AddDhcpReservation(client net.HardwareAddr, reservation netip.Addr) error

AddDhcpReservation configures a new DHCP reservation for the Network. client is the MAC address for which the DHCP address is reserved. reservation is the DHCP IPv4 address to be reserved.

func (*NetworkConfiguration) AddPortForwardingRule

func (c *NetworkConfiguration) AddPortForwardingRule(protocol uint8, addressFamily uint8, internalPort uint16, externalPort uint16, internalAddress netip.Addr) error

AddPortForwardingRule configures a port forwarding rule for the Network. These rules will not be able to be removed or queried until network has been started. To do that, use `vmnet_interface_remove_ip_forwarding_rule` or `vmnet_interface_get_ip_port_forwarding_rules` C API directly. (`vmnet_interface` related functionality not implemented in this package yet)

protocol must be either IPPROTO_TCP or IPPROTO_UDP addressFamily must be either AF_INET or AF_INET6 internalPort is the TCP or UDP port that forwarded traffic should be redirected to. externalPort is the TCP or UDP port on the outside network that should be redirected from. internalAddress is the IPv4 or IPv6 address of the machine on the internal network that should receive the forwarded traffic.

func (*NetworkConfiguration) DisableDhcp

func (c *NetworkConfiguration) DisableDhcp()

DisableDhcp disables DHCP server on the Network.

func (*NetworkConfiguration) DisableDnsProxy

func (c *NetworkConfiguration) DisableDnsProxy()

DisableDnsProxy disables DNS proxy on the Network.

func (*NetworkConfiguration) DisableRouterAdvertisement

func (c *NetworkConfiguration) DisableRouterAdvertisement()

DisableRouterAdvertisement disables router advertisement on the Network.

func (*NetworkConfiguration) SetExternalInterface

func (c *NetworkConfiguration) SetExternalInterface(ifname string) error

SetExternalInterface sets the external interface of the Network. This is only available to networks of SharedMode.

func (*NetworkConfiguration) SetIPv4Subnet

func (c *NetworkConfiguration) SetIPv4Subnet(subnet netip.Prefix) error

SetIPv4Subnet configures the IPv4 address for the Network. Note that the first, second, and last addresses of the range are reserved. The second address is reserved for the host, the first and last are not assignable to any node.

func (*NetworkConfiguration) SetIPv6Prefix

func (c *NetworkConfiguration) SetIPv6Prefix(prefix netip.Prefix) error

SetIPv6Prefix configures the IPv6 prefix for the Network.

func (*NetworkConfiguration) SetMtu

func (c *NetworkConfiguration) SetMtu(mtu uint32) error

SetMtu configures the maximum transmission unit (MTU) for the Network.

type PacketsAvailableEventCallback

type PacketsAvailableEventCallback func(estimatedCount int)

PacketsAvailableEventCallback is a callback function type for packets available event.

type PktDescsManager

type PktDescsManager struct {
	PktDescs *VMPktDesc
	// contains filtered or unexported fields
}

PktDescsManager manages pktDescs and their backing buffers.

func NewPktDescsManager

func NewPktDescsManager(count int, maxPacketSize uint64) *PktDescsManager

NewPktDescsManager allocates pktDesc array and backing buffers. pktDesc's iov_base points to the buffer after 4-byte header. The 4-byte header is reserved for packet size to the connection.

func (*PktDescsManager) At

func (v *PktDescsManager) At(index int) *VMPktDesc

at returns the pointer to the pktDesc at the given index.

func (*PktDescsManager) MaxPacketCount

func (v *PktDescsManager) MaxPacketCount() int

maxPacketCount returns the maximum number of pktDescs managed.

func (*PktDescsManager) ReadPacketsFromConn

func (v *PktDescsManager) ReadPacketsFromConn(conn net.Conn) (int, error)

ReadPacketsFromConn reads packets from the net.Conn into [VMPktDesc]s.

  • It returns the number of packets read.
  • The packets are expected to come one by one with 4-byte big-endian header indicating the packet size.
  • It reads all available packets until no more packets are available, packetCount reaches maxPacketCount, or an error occurs.
  • It waits for the connection to be ready for initial read of 4-byte header.

func (*PktDescsManager) ReadPacketsFromPacketConn

func (v *PktDescsManager) ReadPacketsFromPacketConn(conn net.PacketConn) (int, error)

ReadPacketsFromPacketConn reads packets from the net.PacketConn into [VMPktDesc]s.

  • It returns the number of packets read.
  • The packets are expected to come one by one.
  • It receives all available packets until no more packets are available, packetCount reaches maxPacketCount, or an error occurs.
  • It waits for the connection to be ready for initial packet.

func (*PktDescsManager) Reset

func (v *PktDescsManager) Reset()

Reset resets pktDescs to initial state.

func (*PktDescsManager) WritePacketsToConn

func (v *PktDescsManager) WritePacketsToConn(conn net.Conn, packetCount, receiveBufferSize int) error

WritePacketsToConn writes packets from [VMPktDesc]s to the net.Conn.

  • It returns the number of bytes written.

func (*PktDescsManager) WritePacketsToPacketConn

func (v *PktDescsManager) WritePacketsToPacketConn(conn net.PacketConn, packetCount int) error

WritePacketsToPacketConn writes packets from [VMPktDesc]s to the net.PacketConn.

  • It returns an error if any occurs during sending packets.

type Return

type Return C.uint32_t

The status code returning the result of vmnet operations.

const (
	ErrSuccess            Return = C.VMNET_SUCCESS              // VMNET_SUCCESS Successfully completed.
	ErrFailure            Return = C.VMNET_FAILURE              // VMNET_FAILURE General failure.
	ErrMemFailure         Return = C.VMNET_MEM_FAILURE          // VMNET_MEM_FAILURE Memory allocation failure.
	ErrInvalidArgument    Return = C.VMNET_INVALID_ARGUMENT     // VMNET_INVALID_ARGUMENT Invalid argument specified.
	ErrSetupIncomplete    Return = C.VMNET_SETUP_INCOMPLETE     // VMNET_SETUP_INCOMPLETE Interface setup is not complete.
	ErrInvalidAccess      Return = C.VMNET_INVALID_ACCESS       // VMNET_INVALID_ACCESS Permission denied.
	ErrPacketTooBig       Return = C.VMNET_PACKET_TOO_BIG       // VMNET_PACKET_TOO_BIG Packet size larger than MTU.
	ErrBufferExhausted    Return = C.VMNET_BUFFER_EXHAUSTED     // VMNET_BUFFER_EXHAUSTED Buffers exhausted in kernel.
	ErrTooManyPackets     Return = C.VMNET_TOO_MANY_PACKETS     // VMNET_TOO_MANY_PACKETS Packet count exceeds limit.
	ErrSharingServiceBusy Return = C.VMNET_SHARING_SERVICE_BUSY // VMNET_SHARING_SERVICE_BUSY Vmnet Interface cannot be started as conflicting sharing service is in use.
	ErrNotAuthorized      Return = C.VMNET_NOT_AUTHORIZED       // VMNET_NOT_AUTHORIZED The operation could not be completed due to missing authorization.
)

func (Return) Error

func (e Return) Error() string

type VMPktDesc

type VMPktDesc C.struct_vmpktdesc

VMPktDesc is a Go representation of C.struct_vmpktdesc.

func (*VMPktDesc) GetPacketSize

func (v *VMPktDesc) GetPacketSize() int

GetPacketSize gets the packet size from VMPktDesc.

func (*VMPktDesc) GetVmPktIov

func (v *VMPktDesc) GetVmPktIov() unsafe.Pointer

GetVmPktIov returns the vm_pkt_iov pointer in VMPktDesc.

func (*VMPktDesc) SetPacketSize

func (v *VMPktDesc) SetPacketSize(size int) *VMPktDesc

SetPacketSize sets the packet size in VMPktDesc.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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