virtual network environment#
Understanding container networking requires stepping below high-level abstractions like Docker or Kubernetes and examining the Linux kernel primitives that make them possible. At its core, container connectivity is built by chaining together five fundamental kernel features:
- Network Namespaces: Provide network stack isolation (routing tables, sockets, interfaces).
- Veth Pairs: Act as virtual Ethernet cables bridging isolated namespaces.
- Linux Bridges: Function as virtual L2 switches connecting multiple namespaces within a host.
- Routing Tables: Control packet paths between internal subnets and host interfaces.
- Iptables & Netfilter: Implement NAT to bridge private container IPs with external physical networks.
By combining these primitives, the kernel creates completely isolated, configurable virtual network environments. Here is a deep dive into how these mechanisms operate at the system and driver level.
1. Veth Pairs (Virtual Ethernet Interfaces)#
A veth pair acts as a virtual bidirectional Ethernet cable. Packets transmitted on one end.
graph LR
subgraph Host ["Linux Host"]
direction LR
subgraph Veth0_Dev ["veth0 Interface"]
A["veth0
192.168.1.1/24"]
end
subgraph Veth1_Dev ["veth1 Interface"]
B["veth1
192.168.1.2/24"]
end
A <===> |"Virtual Ethernet Cable (Peer)"| B
end
Basic Commands#
# Create a veth pair named veth0 and veth1
ip link add veth0 type veth peer name veth1
# Assign an IP address to veth0
ip addr add 192.168.1.1/24 dev veth0
# Bring the interfaces up
ip link set veth0 up
ip link set veth1 up
# Verify created devices
ip link showCrucial Kernel Parameters#
When testing local veth packet routing on the same host, the kernel’s Reverse Path Filtering (rp_filter) or local interface route checks may silently drop packets.
sysctl -w net.ipv4.conf.veth0.rp_filter=0: Disables strict reverse path filtering (RPF) checks.
sysctl -w net.ipv4.conf.veth0.accept_local=1: Allows accepting packets with a local source IP.
Inspecting Traffic#
# Capture packets passing through the virtual interface
tcpdump -nn -i veth0Veth Kernel Internals & Packet Path#
Unlike physical network cards that use DMA to push frames to hardware, veth relies on veth_xmit() inside the driver:
- Application calls send() –> allocates an sk_buff payload.
- veth_xmit() grabs the peer device handle (priv->peer).
- Switches the packet context to the peer interface via netif_rx() or netif_receive_skb().
- Triggering a softirq (NET_RX_SOFTIRQ) processes the frame as an incoming packet on the peer device.
2. Network Namespaces (netns)#
Network namespaces isolate network stacks across processes—providing independent routing tables, firewall rules (iptables/nftables), socket lists, ARP tables, and network interfaces (lo, eth0).
Working with Network Namespaces#
# Create a network namespace named net1
ip netns add net1
# Create a veth pair
ip link add veth-host type veth peer name veth-net1
# Move veth-net1 inside the net1 namespace
ip link set veth-net1 netns net1
# Configure IP inside net1
ip netns exec net1 ip addr add 192.168.1.2/24 dev veth-net1
ip netns exec net1 ip link set veth-net1 up
ip netns exec net1 ip link set lo upKernel Representations#
- Process to Namespace (task_struct –> nsproxy –> net): A process references its namespace via current->nsproxy->net_ns.
- The struct net Structure: Contains loopback pointers (loopback_dev), IPv4 protocols (netns_ipv4), sysctl knobs, and active socket tracking tables.
Lifecycle & Creation#
- clone() Flag: Passing CLONE_NEWNET to clone() or unshare() provisions a new struct net instance.
- Subsystem Initialization: Each network subsystem (TCP, UDP, Netfilter, Routing) executes its init_net constructor function inside the new namespace.
- Sockets and Namespaces: Every socket (struct sock) stores a pointer sk_net referencing its parent struct net. Sockets cannot cross namespace boundaries once bound.
3. Linux Bridge (net_bridge)#
A Linux Bridge operates as a virtual Layer-2 Software Switch, connecting multiple veth endpoints or physical interfaces into a shared broadcast domain.
Primary Kernel Structures#
struct net_device: Represents the virtual bridge interface (br0).
struct net_bridge: Represents the internal bridge state, including port lists, MAC forwarding tables (fdb), and spanning tree parameters.
Packet Traversal in a Bridge#
- An incoming frame arrives at a bridged port via netif_receive_skb().
- Execution diverts to br_handle_frame().
- The bridge checks its MAC forwarding table (FDB):
- Unicast Hit: Forwards directly to the target port (br_forward()).
- Unicast Miss / Broadcast / Multicast: Floods the frame out to all connected ports (br_flood()).
4. Routing, NAT, and External Connectivity#
To forward container traffic to an external network (or route host traffic to a container), Linux uses Routing Tables and Netfilter NAT.
Route Tables#
Linux uses multiple route tables evaluated by policy routing rules (ip rule):
local: Reserved for local IP addresses and loopback traffic.
main: Default table for standard route entries.
Enable kernel IP forwarding:#
sysctl -w net.ipv4.ip_forward=1Netfilter Architecture & Hooks#
Table PrioritiesEvaluated in order: raw –> mangle –> nat –> filter.
flowchart TD
Prerouting["PREROUTING
(DNAT / Port Forwarding)"]
Route1{"Routing Decision"}
Input["INPUT
(Local Filter)"]
Process["Local Process"]
Output["OUTPUT
(Outbound Filter)"]
Forward["FORWARD
(Packet Routing)"]
Route2{"Routing Decision"}
Postrouting["POSTROUTING
(SNAT / Masquerade)"]
%% Flow Path
Prerouting --> Route1
%% Local Inbound & Outbound
Route1 -- Local IP --> Input --> Process --> Output --> Route2 --> Postrouting
%% Forwarding Path
Route1 -- Other IP --> Forward --> Postrouting
Port Forwarding (Docker -p 80:80 equivalent)#
- Source NAT (SNAT / MASQUERADE): Rewrites private container IPs to host IPs for outbound internet access.
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE- Destination NAT (DNAT): Redirects inbound traffic on host port 80 to container port 80.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:805. Architectural Summary & Container Networking#
To summarize how these primitives fit together, container networking operates by layering virtual devices over standard kernel subsystems:
flowchart TD
subgraph Host ["Host OS"]
direction TB
subgraph Container ["net1 (Container Namespace)"]
direction TB
VethContainer["eth0 (veth interface)"]
RouteTable["Route Table (Container)"]
VethContainer --- RouteTable
end
subgraph HostNet ["init_net (Host Namespace)"]
direction TB
VethHost["veth-host (Host Interface)"]
Bridge["Linux Bridge (br0)"]
Netfilter["Netfilter / NAT (iptables)"]
PhysicalEth["Physical eth0 (NIC)"]
VethHost --> Bridge
Bridge --> Netfilter
Netfilter --> PhysicalEth
end
VethContainer <== "Virtual Cable (Peer)" ==> VethHost
end
External["External Network"]
PhysicalEth --> External
Virtual Interfaces: Physical eth0 vs. Container eth0#
On a standard Linux host, eth0 is backed by physical network hardware and a hardware driver. Inside a container, eth0 is simply one end of a veth pair renamed to present a familiar interface name to the container applications. It operates purely in software, moving packets via veth_xmit() directly in kernel memory without hardware DMA operations.
How the Kernel Enforces Isolation#
By default, the host operates in a single global network namespace called init_net. When a new namespace is provisioned via clone(CLONE_NEWNET) or unshare(), the kernel allocates a new struct net instance.
Every process (task_struct) holds a pointer (nsproxy->net_ns) to its assigned namespace. When a process issues socket calls or transmits network frames, the kernel inspects this pointer first to evaluate routes, socket bindings, firewall rules, and interface indexes within that specific namespace’s scope.
Inter-Container Communication (Layer 2)#
Much like physical servers connect to a physical Ethernet switch, containers on the same host communicate through a Linux Bridge (net_bridge). The bridge acts as a software L2 switch:
- Each container’s host-side veth device attaches as a virtual port on the bridge.
- The bridge learns MAC addresses and populates its Forwarding Database (FDB).
- Frame delivery between containers occurs via Layer 2 switching (
br_handle_frame()), bypassing higher-layer routing entirely.
Reaching External Networks (Layer 3 & NAT)#
Because containers live in private subnets (e.g., 192.168.1.0/24), their IPs are non-routable on the external physical network. To communicate outside the host:
- Routing: The container sends outbound packets to its default gateway (the bridge IP on the host).
- IP Forwarding: Kernel forwarding (
net.ipv4.ip_forward=1) transfers the packet from the virtual bridge to the host’s physical network card (eth0). - Source NAT (MASQUERADE): Netfilter rewrites the packet’s private source IP to the host’s physical IP address before it exits onto the wire.
- Destination NAT (DNAT): For inbound traffic, Netfilter intercepts host ports and rewrites the destination IP back to the specific container’s virtual IP.