nettest

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nettest builds the network namespaces, packets and utun devices of plexd's real-kernel tests. On Linux a test runs its listeners and clients in network namespaces joined by veth pairs. On macOS it writes packets into a utun device it owns and reads what the host sends out of it. The packet builders and the parser serve both.

The package is imported only from tests.

Index

Constants

View Source
const (
	// MeshLink is N's end of the veth to P. It stands for the mesh
	// interface.
	MeshLink = "nt-mesh"
	// PeerLink is P's end of the veth to N.
	PeerLink = "nt-peer"
	// LANLink is N's end of the veth to L, the other network.
	LANLink = "nt-lan"
	// HostLink is L's end of the veth to N.
	HostLink = "nt-host"
	// NodeMeshIP is N's mesh IP, on MeshLink.
	NodeMeshIP = "10.98.0.1"
	// PeerMeshIP is P's mesh IP, on PeerLink.
	PeerMeshIP = "10.98.0.2"
	// NodeLANIP is N's address on LANLink.
	NodeLANIP = "192.0.2.1"
	// LANHostIP is L's own address, on HostLink.
	LANHostIP = "192.0.2.2"
	// LANMeshSource is a mesh address L holds on its loopback, so L can
	// send from a mesh source.
	LANMeshSource = "10.98.0.3"
	// MeshPrefix is the prefix of the mesh that N and P share.
	MeshPrefix = "10.98.0.0/24"
)

The links and addresses of the topology NewMeshLAN builds. N is the node, P its mesh peer and L a host on another network N is attached to.

View Source
const (
	ProtoTCP = 6
	ProtoUDP = 17
	FlagSYN  = 0x02
	FlagACK  = 0x10
)

Protocol numbers and TCP flags of the packets the builders write and ParseIPv4 reads.

Variables

This section is empty.

Functions

func TCPSegment

func TCPSegment(src, dst netip.AddrPort, seq, ack uint32, flags uint8) []byte

TCPSegment returns an IPv4 packet from src to dst that carries a TCP header of 20 bytes without options and no payload: seq, ack, flags and a window of 65535. The IPv4 header sets the don't-fragment bit and a TTL of 64, and both checksums are valid. src and dst must hold IPv4 addresses.

func UDPDatagram

func UDPDatagram(src, dst netip.AddrPort, payload []byte) []byte

UDPDatagram returns an IPv4 packet from src to dst that carries a UDP datagram with payload. The IPv4 header sets the don't-fragment bit and a TTL of 64, and both checksums are valid. src and dst must hold IPv4 addresses.

func Veth

func Veth(t testing.TB, a *Netns, aName string, b *Netns, bName string)

Veth creates a veth pair with aName in a and bName in b. Both ends stay down until Addr or Up sets them up.

Types

type MeshLAN

type MeshLAN struct {
	Node, Peer, LANHost *Netns
}

MeshLAN holds the namespaces NewMeshLAN builds.

func NewMeshLAN

func NewMeshLAN(t testing.TB) MeshLAN

NewMeshLAN builds the namespaces of a node N that is attached to the mesh and to another network, a mesh peer P and a host L on the other network. The real tests of plexd's mesh listeners run a listener in N and show which of P's and L's packets reach it.

N holds the mesh IP 10.98.0.1/24 on nt-mesh, a veth to P, which holds 10.98.0.2/24 on nt-peer and stands for the mesh. N also holds 192.0.2.1/24 on nt-lan, a veth to L, which holds 192.0.2.2/24 on nt-host. L holds the mesh address 10.98.0.3 on its loopback and routes the mesh IP through N, and N routes 10.98.0.3 back to L. So what L sends to the mesh IP arrives on nt-lan, from L's own address and from the mesh source alike, and an answer to it returns to L. N's loopback is up: without it no client inside N reaches N's own addresses, the mesh IP included.

The namespaces, links and addresses go away with the test.

type Netns

type Netns struct {
	// Name names the namespace in failure messages.
	Name string
	// contains filtered or unexported fields
}

Netns is a network namespace a test created. It goes away with its last handle, link end and socket.

func NewNetns

func NewNetns(t testing.TB, name string) *Netns

NewNetns creates a network namespace and a netlink handle inside it, and stops the test when either fails. The test's cleanup closes both. name appears in failure messages only.

func (*Netns) Addr

func (n *Netns) Addr(t testing.TB, link, cidr string)

Addr adds cidr to link and sets link up. It stops the test when either fails.

func (*Netns) Do

func (n *Netns) Do(fn func() error) error

Do runs fn on a new thread inside the namespace and returns fn's error, so the sockets fn opens belong to the namespace.

func (*Netns) Route

func (n *Netns) Route(t testing.TB, link, dst, gw string)

Route routes dst through the gateway gw on link. It stops the test when the route cannot be added.

func (*Netns) Sysctl

func (n *Netns) Sysctl(t testing.TB, key, value string)

Sysctl writes value to the IPv4 sysctl key inside the namespace, and stops the test when the write fails. key is the name below net.ipv4, such as conf.nt-lan.rp_filter, so it cannot name an interface whose name holds a dot. The write happens on a thread inside the namespace, which sees the namespace's own /proc/sys/net.

func (*Netns) Up

func (n *Netns) Up(t testing.TB, link string)

Up sets link up. It stops the test when that fails.

type Packet

type Packet struct {
	Proto            uint8
	Src, Dst         netip.Addr
	SrcPort, DstPort uint16
	Seq, Ack         uint32 // TCP only
	Flags            uint8  // TCP only
	Payload          []byte // the TCP payload or the UDP payload
}

Packet is an IPv4 TCP segment or UDP datagram reduced to the fields the real tests look at.

func ParseIPv4

func ParseIPv4(b []byte) (Packet, bool)

ParseIPv4 parses an IPv4 packet that carries a whole TCP segment or UDP datagram. It reports false for anything else: a buffer shorter than the headers, another IP version, a header, total, data offset or UDP length that does not fit the buffer, a fragment, or another protocol. It does not check the checksums. The payload shares b's memory.

func (Packet) String

func (p Packet) String() string

String formats p for a failure message.

Jump to

Keyboard shortcuts

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