Expand description
Native container networking — N1 (loopback) and N2 (veth + bridge).
§Architecture
-
N1 loopback:
bring_up_loopbackis called inside the container’spre_execclosure, afterunshare(CLONE_NEWNET), usingioctl(SIOCSIFFLAGS)to setIFF_UPonlo. The kernel then automatically activates 127.0.0.1. -
N2 bridge:
setup_bridge_networkis called by the parent beforefork(). It creates a named network namespace (ip netns add), fully configures it (veth pair, IP, routes, bridge attachment), then returns. The child’spre_execjoins the named netns viasetns().
§Why named netns (not /proc/{pid}/ns/net)?
The primary reason is debuggability: named netns are visible via
ip netns list and inspectable with ip netns exec pelagos-foo ip addr
from the host. Anonymous namespaces via /proc/{pid}/ns/net offer none
of that visibility.
There are also two practical problems with /proc/{pid}/ns/net given
our current use of std::process::Command, though neither is fundamental:
-
Race with fast exit: if the container runs e.g.
exit 0, the child can terminate before the parent opens/proc/{pid}/ns/net. This isn’t truly fatal — a dead container doesn’t need networking — but it does require the parent to handle “PID gone” gracefully rather than treating it as an error. -
CLOEXEC deadlock: adding a sync pipe so the child blocks in
pre_execwhile the parent configures networking deadlocks becausestd::process::Command::spawn()itself blocks on an internal CLOEXEC fail-pipe untilexec(). The child can’texec()while blocked on our pipe, and the parent can’t signal our pipe untilspawn()returns. This is a Rust stdlib limitation — a rawfork()/exec()implementation could synchronize freely.
Named netns sidestep both issues (created before fork, no coordination needed) and give us host-side observability for free.
Teardown removes the host-side veth (ip link del) and the named netns
(ip netns del).
Structs§
- Ipv4Net
- A compact IPv4 network (address + prefix length), e.g.
10.88.1.0/24. - Network
Config - Network configuration for a container.
- Network
Def - Persistent definition of a named network (stored in config dir).
- Network
Setup - Runtime state from setting up bridge networking; needed for teardown.
- Pasta
Setup - Runtime state for a pasta-backed container; holds the pasta process for teardown.
Enums§
- Network
Mode - Port
Proto - Container network mode. Protocol for a port-forward mapping.
Constants§
- BRIDGE_
GW - Gateway IP for the default network.
- BRIDGE_
NAME - Bridge name for the default network.
Functions§
- attach_
network_ to_ netns - Attach an additional bridge network to an existing named netns.
- bootstrap_
default_ network - Bootstrap or load the default
pelagos0network definition. - bring_
up_ loopback - Bring up the loopback interface (
lo) inside the current network namespace. - ensure_
network - Ensure a named network exists, creating it if necessary.
- generate_
ns_ name - Generate a unique name for a container network namespace.
- is_
pasta_ available - Returns true if
pastais on PATH and responds to--version. - load_
network_ def - Load a network definition by name.
- setup_
bridge_ network - Set up full bridge networking for a container using a named network namespace.
- setup_
pasta_ network - Spawn pasta attached to an already-running container’s network namespace.
- teardown_
network - Remove the container’s veth pair and named network namespace.
- teardown_
pasta_ network - Kill the pasta relay process and collect its stdout+stderr output for diagnostics.
- teardown_
secondary_ network - Remove a secondary network’s veth pair.