Virtual networking: bridge, veth, namespace, Open vSwitch
Linux can build a whole network inside one machine: network namespaces act as separate computers, veth pairs as cables, a Linux bridge as a switch, and Open vSwitch as a programmable switch. Docker, Kubernetes and clouds are built on exactly these pieces.
54.1 Why is it used?
- Test a three-computer network when you only have one Linux laptop.
- Every container (Docker, Kubernetes pods) gets its network this way.
- Clouds and data centers need programmable switching (VLAN tags, tunnels, flow rules): Open vSwitch and SDN.
54.2 The building blocks
| Piece | Acts like | Key point |
|---|---|---|
| Network namespace | A separate computer | Its own interfaces, IPs and routing table; starts with only lo (DOWN) |
| veth pair | A virtual cable | Two ends; a packet entering one end comes out of the other |
| Linux bridge | A virtual switch | Learns source MACs in its FDB and forwards only to the right port |
| Open vSwitch (OVS) | A programmable switch | VLAN tags, tunnels, OpenFlow rules |
- Step 1
1.red (10.74.0.10) sends a frame out of its eth0
- Step 2
2.The veth carries it to port v-red on br0
- Step 3
3.br0 learns red's source MAC in its FDB
- Step 4
4.br0 forwards the frame only on the v-green port
- Step 5
5.green (10.74.0.30) receives it and replies the same way
Exactly how a real learning switch behaves
54.3 Practical: two new computers (namespaces)
ip netns add redip netns add blueip netns listip netns exec red ip linkip netns exec red ping -c 1 -W 1 10.74.0.20$ ip netns listbluered$ ip netns exec red ip link1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN$ ip netns exec red ping -c 1 -W 1 10.74.0.20ping: connect: Network unreachable- A new namespace has only lo, and it is DOWN.
- Ping gives Network unreachable: there is no cable yet.
54.4 Practical: connect with a veth pair
ip link add veth-red type veth \ peer name veth-blueip -br link show type vethip link set veth-red netns redip link set veth-blue netns blueip -n red addr add 10.74.0.10/24 \ dev veth-redip -n red link set veth-red upip netns exec red ping -c 2 10.74.0.20ip -n red neigh$ ip -br link show type vethveth-blue@veth-red DOWN 0a:c7:34:88:a5:0aveth-red@veth-blue DOWN f6:dc:ee:cd:fd:87$ ip netns exec red ping -c 2 10.74.0.2064 bytes from 10.74.0.20: icmp_seq=1 ttl=64 time=0.082 ms64 bytes from 10.74.0.20: icmp_seq=2 ttl=64 time=0.057 ms2 packets transmitted, 2 received, 0% packet loss$ ip -n red neigh10.74.0.20 dev veth-red lladdr 0a:c7:34:88:a5:0a REACHABLE- Both veth ends are listed joined to each other (veth-blue@veth-red) and start DOWN.
- Each end is moved into its namespace, given an IP (/24) and brought up (blue gets 10.74.0.20 the same way).
- ip neigh shows blue's MAC as REACHABLE: ARP works normally over the veth.
54.5 Practical: three computers on a Linux bridge
ip -n red link del veth-redip netns add greenip link add br0 type bridgeip link set br0 upfor n in red blue green; do ip link add v-$n type veth \ peer name eth0 netns $n ip link set v-$n master br0 up ip -n $n link set eth0 updonebridge linkip netns exec red ping -c 2 10.74.0.30bridge fdb show br br0 \ | grep -v permanent$ bridge link12: v-red@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding13: v-blue@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding14: v-green@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding$ ip netns exec red ping -c 2 10.74.0.3064 bytes from 10.74.0.30: icmp_seq=1 ttl=64 time=0.117 ms2 packets transmitted, 2 received, 0% packet loss$ bridge fdb show br br0 | grep -v permanentfe:ad:c0:52:e5:4c dev v-red master br0de:c4:e8:aa:c9:4d dev v-blue master br02e:4d:fb:d8:d2:9d dev v-green master br0- One veth per namespace: one end is eth0 inside the namespace, the other plugs into br0.
- bridge link shows all three ports in forwarding state.
- After the ping, the FDB has learned all three MACs with their ports.
54.6 Docker network = namespace + veth + bridge
- Every container is a network namespace.
- Its eth0 is one end of a veth; the other end plugs into a Linux bridge on the host (br-<network id>).
- Container A to B: veth -> bridge -> veth -> B.
- For the internet, the bridge IP (192.168.74.1) is the gateway where iptables does NAT.
- Docker's internal DNS resolves container names to IPs.
- Step 1
1.docker network create builds bridge br-711b9cf6b6c9 (192.168.74.1)
- Step 2
2.docker run creates a namespace for n054-a
- Step 3
3.A veth pair: eth0@if901 inside, veth 901 on the bridge
- Step 4
4.n054-a pings n054-b by name; Docker DNS gives 192.168.74.20
- Step 5
5.Frame goes veth -> bridge -> veth into n054-b
- Step 6
6.Traffic to the internet leaves via the bridge gateway with NAT
docker network create \ --subnet 192.168.74.0/24 \ --gateway 192.168.74.1 n054-netdocker run -d --name n054-a \ --network n054-net \ --ip 192.168.74.10 alpine:3.20 sleep 600docker exec n054-a ip link show eth0docker run --rm --network host \ nicolaka/netshoot:v0.13 sh -c \ "bridge link | grep br-711b9cf6b6c9"docker exec n054-a ping -c 2 n054-b$ docker network ls --filter name=n054NETWORK ID NAME DRIVER SCOPE711b9cf6b6c9 n054-net bridge local$ docker exec n054-a ip link show eth09: eth0@if901: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP$ bridge link | grep br-711b9cf6b6c9901: veth4664658@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-711b9cf6b6c9 state forwarding902: vethfdde2ce@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br-711b9cf6b6c9 state forwarding$ docker exec n054-a ping -c 2 n054-bPING n054-b (192.168.74.20): 56 data bytes64 bytes from 192.168.74.20: seq=0 ttl=64 time=0.182 ms64 bytes from 192.168.74.20: seq=1 ttl=64 time=0.246 ms2 packets transmitted, 2 packets received, 0% packet loss- eth0@if901 inside the container = the other end of the cable is interface 901.
- bridge link in the engine shows the same veth 901 as a port of br-711b9cf6b6c9.
54.7 Docker network drivers
| Driver | What it does |
|---|---|
| bridge | Default: namespace + veth + bridge |
| host | No own namespace; uses the host's network stack |
| none | Only lo, no network |
| overlay | Connects containers on multiple hosts with a VXLAN tunnel |
| macvlan | Container gets its own MAC address on the LAN |
54.8 Open vSwitch and SDN
- OVS is a programmable virtual switch for big data centers and clouds: VLAN tags, tunnels and OpenFlow rules.
- In a normal network each switch decides forwarding on its own.
- SDN (Software Defined Networking): a central controller pushes flow rules to all switches.
- Network behaviour changes through software without touching a cable.
- Step 1
1.red and blue are on OVS ports with VLAN tag 10
- Step 2
2.Default flow priority=0 actions=NORMAL: OVS acts as a learning switch, ping works
- Step 3
3.Controller (here ovs-ofctl) pushes priority=100,icmp,actions=drop
- Step 4
4.Ping from red is dropped right at OVS (n_packets=2 on the rule)
- Step 5
5.Rule deleted: ping works again
- Step 6
6.blue's tag changed to 20: ping fails, VLANs differ
54.9 Practical: Open vSwitch
ovs-vsctl add-br ovsbr0ovs-vsctl add-port ovsbr0 o-red tag=10ovs-vsctl add-port ovsbr0 o-blue tag=10ovs-vsctl showip netns exec red ping -c 2 10.74.1.20ovs-ofctl dump-flows ovsbr0$ ovs-vsctl show Bridge ovsbr0 Port o-red tag: 10 Port o-blue tag: 10$ ip netns exec red ping -c 2 10.74.1.2064 bytes from 10.74.1.20: seq=0 ttl=64 time=2.859 ms64 bytes from 10.74.1.20: seq=1 ttl=64 time=0.106 ms2 packets transmitted, 2 packets received, 0% packet loss$ ovs-ofctl dump-flows ovsbr0NXST_FLOW reply (xid=0x4): cookie=0x0, duration=8.196s, table=0, n_packets=8, n_bytes=616, idle_age=0, priority=0 actions=NORMAL- ovs-vsctl manages bridges and ports; the tag=10 puts ports in VLAN 10.
- Only one default flow with actions=NORMAL: a normal learning switch.
54.10 Practical: an SDN style flow rule
ovs-ofctl add-flow ovsbr0 \ priority=100,icmp,actions=dropip netns exec red ping -c 2 -W 1 10.74.1.20ovs-ofctl dump-flows ovsbr0ovs-ofctl del-flows ovsbr0 icmpip netns exec red ping -c 1 10.74.1.20ovs-vsctl set port o-blue tag=20ip netns exec red ping -c 2 -W 1 10.74.1.20$ ip netns exec red ping -c 2 -W 1 10.74.1.20PING 10.74.1.20 (10.74.1.20): 56 data bytes2 packets transmitted, 0 packets received, 100% packet loss$ ovs-ofctl dump-flows ovsbr0 cookie=0x0, duration=4.310s, table=0, n_packets=2, n_bytes=196, idle_age=1, priority=100,icmp actions=drop cookie=0x0, duration=13.465s, table=0, n_packets=10, n_bytes=700, idle_age=1, priority=0 actions=NORMAL$ ovs-ofctl del-flows ovsbr0 icmp$ ip netns exec red ping -c 1 10.74.1.2064 bytes from 10.74.1.20: seq=0 ttl=64 time=0.780 ms1 packets transmitted, 1 packets received, 0% packet loss$ ovs-vsctl set port o-blue tag=20$ ip netns exec red ping -c 2 -W 1 10.74.1.20PING 10.74.1.20 (10.74.1.20): 56 data bytes2 packets transmitted, 0 packets received, 100% packet loss- The priority 100 drop rule beats the priority 0 NORMAL rule, so ICMP gets 100% loss.
- del-flows restores the ping; a different VLAN tag on blue breaks it again.
54.11 Common mistakes
- Forgetting ip link set up: a new veth is DOWN by default, so no packet travels.
- Leaving a veth end in the wrong namespace.
- Myth: a container is a small VM with its own network card. Reality: it is a namespace sharing the host kernel; its eth0 is a veth end.
- Because container traffic passes the host's bridge and iptables, tcpdump on the host can see every container's traffic.
54.12 Interview tips
- Container networking: Docker creates a namespace and a veth pair per container; one end is eth0 inside, the other plugs into a host bridge.
- The bridge switches between containers and is the gateway for outbound traffic where iptables does NAT.
- Namespace = separate network stack, veth = virtual cable, bridge = virtual switch.
- OVS = programmable switch; SDN = central controller pushing flow rules.
- Debug order for a failed namespace ping: both ends UP, right namespace, IPs in the same subnet.