network

package
v0.0.0-...-0123ebd Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package network provides user-space port forwarding and network utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPortAvailable

func CheckPortAvailable(port int) error

CheckPortAvailable attempts to listen on the given TCP port to verify it is not already in use. Returns an error if the port is occupied.

func CheckPortsAvailable

func CheckPortsAvailable(ports []int) error

CheckPortsAvailable verifies a list of ports are all free.

Types

type Conditioner

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

Conditioner manages network chaos rules between nodes.

func NewConditioner

func NewConditioner() *Conditioner

NewConditioner creates a new network conditioner.

func (*Conditioner) AllRules

func (c *Conditioner) AllRules() []LinkRule

AllRules returns a snapshot of all active rules.

func (*Conditioner) Degrade

func (c *Conditioner) Degrade(a, b string, latency, jitter time.Duration, loss float64) error

Degrade sets latency, jitter, and loss on a link.

func (*Conditioner) Delay

func (c *Conditioner) Delay(a, b string) time.Duration

Delay returns how long a packet should be delayed on this link.

func (*Conditioner) GetRule

func (c *Conditioner) GetRule(a, b string) *LinkRule

GetRule returns the rule for a node pair, or nil if none.

func (*Conditioner) Heal

func (c *Conditioner) Heal(a, b string)

Heal removes a partition (and all conditions) between two nodes.

func (*Conditioner) Partition

func (c *Conditioner) Partition(a, b string)

Partition creates a hard partition between two nodes.

func (*Conditioner) RemoveRule

func (c *Conditioner) RemoveRule(a, b string)

RemoveRule removes any rule between two nodes.

func (*Conditioner) Reset

func (c *Conditioner) Reset()

Reset removes all rules.

func (*Conditioner) SetRule

func (c *Conditioner) SetRule(rule LinkRule)

SetRule applies a network condition rule between two nodes.

func (*Conditioner) ShouldDrop

func (c *Conditioner) ShouldDrop(a, b string) bool

ShouldDrop returns true if a packet on this link should be dropped.

type Forwarder

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

Forwarder manages a set of user-space port forwards.

func NewForwarder

func NewForwarder(rules []PortForwardRule) *Forwarder

NewForwarder creates a Forwarder for the given rules.

func (*Forwarder) Start

func (f *Forwarder) Start(ctx context.Context) error

Start begins listening on all configured host ports and proxying to the guest.

func (*Forwarder) Stop

func (f *Forwarder) Stop()

Stop closes all listeners and terminates forwarding.

type L2Switch

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

L2Switch is a userspace virtual Ethernet switch. Each VM connects via a socketpair; the host connects via a TAP device.

func NewL2Switch

func NewL2Switch(cond *Conditioner, tapName, cidr, gateway string) (*L2Switch, error)

NewL2Switch creates a new L2Switch backed by a TAP device named tapName with the given gateway address (e.g. "10.0.0.1") in cidr (e.g. "10.0.0.0/24"). Returns an error if the TAP device cannot be opened (e.g. missing CAP_NET_ADMIN).

func NewL2SwitchForCluster

func NewL2SwitchForCluster(cond *Conditioner, tapName, cidr, gateway string) (*L2Switch, error)

NewL2SwitchForCluster creates an L2Switch for multi-node clusters. On Linux this is identical to NewL2Switch (TAP-backed).

func (*L2Switch) Close

func (sw *L2Switch) Close() error

Close shuts down the switch (cancels reader goroutines, closes TAP, removes NAT rules).

func (*L2Switch) Gateway

func (sw *L2Switch) Gateway() string

Gateway returns the host-side TAP address (e.g. "10.0.0.1").

func (*L2Switch) NewPort

func (sw *L2Switch) NewPort(nodeName string) (*os.File, error)

NewPort allocates a socketpair for a new VM. The QEMU-side *os.File is returned; the caller must add it to cmd.ExtraFiles and then close it in the parent after cmd.Start().

func (*L2Switch) RemovePort

func (sw *L2Switch) RemovePort(nodeName string)

RemovePort closes the daemon side of a port and removes it from the switch.

func (*L2Switch) Subnet

func (sw *L2Switch) Subnet() string

Subnet returns the CIDR assigned to this switch (e.g. "10.0.0.0/24").

type LinkRule

type LinkRule struct {
	NodeA   string        `json:"node_a"`
	NodeB   string        `json:"node_b"`
	Latency time.Duration `json:"latency"` // One-way added latency.
	Jitter  time.Duration `json:"jitter"`  // Random +/- range around latency.
	Loss    float64       `json:"loss"`    // Packet loss probability (0.0–1.0).
	Down    bool          `json:"down"`    // Hard partition — all packets dropped.
}

LinkRule describes network conditions between two nodes.

type PortForwardRule

type PortForwardRule struct {
	HostPort  int
	GuestIP   string
	GuestPort int
	Protocol  string // "tcp" or "udp"
}

PortForwardRule describes a single host->guest port mapping.

Jump to

Keyboard shortcuts

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