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 ¶
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.
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 ¶
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 ¶
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.
Types ¶
type MeshLAN ¶
type MeshLAN struct {
Node, Peer, LANHost *Netns
}
MeshLAN holds the namespaces NewMeshLAN builds.
func NewMeshLAN ¶
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 ¶
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) Do ¶
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 ¶
Route routes dst through the gateway gw on link. It stops the test when the route cannot be added.
func (*Netns) Sysctl ¶
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.
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 ¶
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.