vsock

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 11 Imported by: 94

README

vsock Test Status Go Reference Go Report Card

Package vsock provides access to Linux VM sockets (AF_VSOCK) for communication between a hypervisor and its virtual machines. MIT Licensed.

For more information about VM sockets, see my blog about Linux VM sockets in Go or the QEMU wiki page on virtio-vsock.

Stability

See the CHANGELOG file for a description of changes between releases.

This package has a stable v1 API and any future breaking changes will prompt the release of a new major version. Features and bug fixes will continue to occur in the v1.x.x series.

This package only supports the two most recent major versions of Go, mirroring Go's own release policy. Older versions of Go may lack critical features and bug fixes which are necessary for this package to function correctly.

Documentation

Overview

Package vsock provides access to Linux VM sockets (AF_VSOCK) for communication between a hypervisor and its virtual machines.

The types in this package implement interfaces provided by package net and may be used in applications that expect a net.Listener or net.Conn.

  • *Addr implements net.Addr
  • *Conn implements net.Conn
  • *Listener implements net.Listener

Index

Constants

View Source
const (
	// Hypervisor specifies that a socket should communicate with the hypervisor
	// process. Note that this is _not_ the same as a socket owned by a process
	// running on the hypervisor. Most users should probably use Host instead.
	Hypervisor = 0x0

	// Local specifies that a socket should communicate with a matching socket
	// on the same machine. This provides an alternative to UNIX sockets or
	// similar and may be useful in testing VM sockets applications.
	Local = 0x1

	// Host specifies that a socket should communicate with processes other than
	// the hypervisor on the host machine. This is the correct choice to
	// communicate with a process running on a hypervisor using a socket dialed
	// from a guest.
	Host = 0x2
)

Variables

This section is empty.

Functions

func ContextID

func ContextID() (uint32, error)

ContextID retrieves the local VM sockets context ID for this system. ContextID can be used to directly determine if a system is capable of using VM sockets.

If the kernel module is unavailable, access to the kernel module is denied, or VM sockets are unsupported on this system, it returns an error.

Types

type Addr

type Addr struct {
	ContextID, Port uint32
}

An Addr is the address of a VM sockets endpoint.

func (*Addr) Network

func (a *Addr) Network() string

Network returns the address's network name, "vsock".

func (*Addr) String

func (a *Addr) String() string

String returns a human-readable representation of Addr, and indicates if ContextID is meant to be used for a hypervisor, host, VM, etc.

type Config

type Config struct{}

Config contains options for a Conn or Listener.

type Conn

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

A Conn is a VM sockets implementation of a net.Conn.

func Dial

func Dial(contextID, port uint32, cfg *Config) (*Conn, error)

Dial dials a connection-oriented net.Conn to a VM sockets listener. The context ID and port parameters specify the address of the listener. Config specifies optional configuration for the Conn. If config is nil, a default configuration will be used.

If dialing a connection from the hypervisor to a virtual machine, the VM's context ID should be specified.

If dialing from a VM to the hypervisor, Hypervisor should be used to communicate with the hypervisor process, or Host should be used to communicate with other processes on the host machine.

When the connection is no longer needed, Close must be called to free resources.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) CloseRead

func (c *Conn) CloseRead() error

CloseRead shuts down the reading side of the VM sockets connection. Most callers should just use Close.

func (*Conn) CloseWrite

func (c *Conn) CloseWrite() error

CloseWrite shuts down the writing side of the VM sockets connection. Most callers should just use Close.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

Read implements the net.Conn Read method.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline implements the net.Conn SetDeadline method.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline implements the net.Conn SetReadDeadline method.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements the net.Conn SetWriteDeadline method.

func (*Conn) SyscallConn

func (c *Conn) SyscallConn() (syscall.RawConn, error)

SyscallConn returns a raw network connection. This implements the syscall.Conn interface.

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

Write implements the net.Conn Write method.

type Listener

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

A Listener is a VM sockets implementation of a net.Listener.

func FileListener added in v1.1.0

func FileListener(f *os.File) (*Listener, error)

FileListener returns a copy of the network listener corresponding to an open os.File. It is the caller's responsibility to close the Listener when finished. Closing the Listener does not affect the os.File, and closing the os.File does not affect the Listener.

This function is intended for advanced use cases and most callers should use Listen instead.

func Listen

func Listen(port uint32, cfg *Config) (*Listener, error)

Listen opens a connection-oriented net.Listener for incoming VM sockets connections. The port parameter specifies the port for the Listener. Config specifies optional configuration for the Listener. If config is nil, a default configuration will be used.

To allow the server to assign a port automatically, specify 0 for port. The address of the server can be retrieved using the Addr method.

Listen automatically infers the appropriate context ID for this machine by calling ContextID and passing that value to ListenContextID. Callers with advanced use cases (such as using the Local context ID) may wish to use ListenContextID directly.

When the Listener is no longer needed, Close must be called to free resources.

func ListenContextID

func ListenContextID(contextID, port uint32, cfg *Config) (*Listener, error)

ListenContextID is the same as Listen, but also accepts an explicit context ID parameter. This function is intended for advanced use cases and most callers should use Listen instead.

See the documentation of Listen for more details.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept implements the Accept method in the net.Listener interface; it waits for the next call and returns a generic net.Conn. The returned net.Conn will always be of type *Conn.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address, a *Addr. The Addr returned is shared by all invocations of Addr, so do not modify it.

func (*Listener) Close

func (l *Listener) Close() error

Close stops listening on the VM sockets address. Already Accepted connections are not closed.

func (*Listener) SetDeadline

func (l *Listener) SetDeadline(t time.Time) error

SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.

Directories

Path Synopsis
cmd
vscp
Command vscp provides a scp-like utility for copying files over VM sockets.
Command vscp provides a scp-like utility for copying files over VM sockets.
internal
vsutil
Package vsutil provides added functionality for package vsock-internal use.
Package vsutil provides added functionality for package vsock-internal use.

Jump to

Keyboard shortcuts

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