19.3. Using VLANs and Bridges

Two hosts on the same Ethernet behave as if the cable is private; add a switch and every host hears every frame. Two kernel features scale that world without more cables: VLANs split one physical link into several isolated broadcast domains, and bridges turn a Debian host into the switch itself. Both are layer-2 concepts - no IP forwarding involved - and both are configured with the same systemd-networkd file scheme the chapter has used since Section 19.1.

IEEE 802.1Q divides one Ethernet into virtual LANs by inserting a 4-byte tag between the source address and the payload; the tag carries a VLAN ID (VID) from 1 to 4094. A tagged frame carries its VID explicitly. An untagged frame has no VID at all, and the switch port that receives it assigns one - the port's PVID, the port VLAN ID. Every switch port has both a role and a tagging rule:

Switch port style Frames it sends Frames it accepts Use
Access port Untagged only; tagged frames from the host are usually dropped Untagged frames, classified into the PVID One ordinary host, one VLAN
Trunk port Tagged frames for each carried VID; the PVID's VID untagged Tagged frames with a carried VID; untagged frames into the PVID Links between switches, hypervisors, routers

The classic trap is the trunk's native VLAN: a trunk port sends its PVID untagged, so a VLAN that "works everywhere" may actually be traveling without a tag, and misconfigured native VLANs silently merge two VLANs into one. The safe rule is to tag every VID a trunk carries and to keep the native VLAN unused for data.

Linux hosts sit on the access side or the trunk side, and the lab uses both. rtclient and rtserver share one physical link (eth2 on the 192.168.222.0/24 trunk network) that carries both the untagged base network and, from the next subsection, tagged VLAN 219 with its own address range 10.19.219.0/24. In a datacenter this link would be a switch trunk port configured to pass VID 219; between two directly connected hosts the tags simply ride the wire. Look at the parent interface first, on rtclient, as john:

$ ip -d link show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:d0:95:7e brd ff:ff:ff:ff:ff:ff promiscuity 0 allmulti 0 minmtu 68 maxmtu 65535 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 gso_ipv4_max_size 65536 gro_ipv4_max_size 65536 parentbus virtio parentdev virtio4
    altname enp0s7
    altname ens7
    altname enx525400d0957e

An ordinary Ethernet device: virtio-backed in this lab (the parentbus/parentdev fields), with the alternative predictable names from Section 17.1.3 visible as altname entries. Nothing about it is VLAN-specific yet - VLAN interfaces are separate devices stacked on this one.

19.3.2. Creating Persistent VLAN Interfaces

A VLAN interface is three pieces: a .netdev file defining the device, a VLAN= entry in the parent link's .network file that makes systemd-networkd create it, and a .network file of its own carrying the address. The .netdev kinds this chapter creates:

[NetDev] Kind Creates Key settings
vlan Tagging sub-interface on a parent Id (the VID)
bridge Software Ethernet bridge STP, ForwardDelaySec
bond Link aggregation device Mode, MIIMonitorSec
dummy Address-only virtual device none

The naming convention eth2.219 - parent dot VID - is just convention, but a good one: fifteen characters is the kernel's limit for interface names, and the name tells you both the parent and the tag. Three files define the client's side; create them in the workspace:

# /etc/systemd/network/20-ch19-vlan219.netdev
[NetDev]
Name=eth2.219
Kind=vlan

[VLAN]
Id=219
# /etc/systemd/network/10-ch19-eth2.network
[Match]
Name=eth2

[Network]
VLAN=eth2.219
Address=192.168.222.10/24
ConfigureWithoutCarrier=yes
# /etc/systemd/network/20-ch19-vlan219.network
[Match]
Name=eth2.219

[Network]
Address=10.19.219.10/24

The .netdev is shared content - identical on both hosts. The parent .network and the VLAN .network are per-node: VLAN=eth2.219 in the parent's [Network] section is what makes systemd-networkd create the device (the .netdev alone defines it), and the VLAN carries the untagged base network's address alongside, so one cable hosts two address ranges. The second [Route]-style extra from rtclient's eth1 file is not needed here: both networks are on-link. Install all three on rtclient, as john:

$ sudo install -m 0644 rtclient-eth2.network /etc/systemd/network/10-ch19-eth2.network
$ sudo install -m 0644 vlan219.netdev /etc/systemd/network/20-ch19-vlan219.netdev
$ sudo install -m 0644 rtclient-vlan219.network /etc/systemd/network/20-ch19-vlan219.network
$ sudo networkctl reload
$ sudo networkctl reconfigure eth2

All silent. The same three files, with Address=192.168.222.20/24 and Address=10.19.219.20/24, go to rtserver - install them there and apply with the same reload and reconfigure eth2. The new device exists and tags:

$ ip -d link show eth2.219
10: eth2.219@eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:d0:95:7e brd ff:ff:ff:ff:ff:ff promiscuity 0 allmulti 0 minmtu 0 maxmtu 65535
    vlan protocol 802.1Q id 219 <REORDER_HDR> addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 gso_ipv4_max_size 65536 gro_ipv4_max_size 65536

The name shows the stacking (eth2.219@eth2), the detail line shows vlan protocol 802.1Q id 219, and the MAC is inherited from the parent - a VLAN device is the parent's wire, plus a tag. The address came from the .network file:

$ ip -br addr show eth2.219
eth2.219@eth2    UP             10.19.219.10/24 fe80::5054:ff:fed0:957e/64

With both ends configured, tagged traffic crosses, on rtclient as john:

$ ping -c 2 10.19.219.20
PING 10.19.219.20 (10.19.219.20) 56(84) bytes of data.
64 bytes from 10.19.219.20: icmp_seq=1 ttl=64 time=0.761 ms
64 bytes from 10.19.219.20: icmp_seq=2 ttl=64 time=0.426 ms

--- 10.19.219.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1023ms
rtt min/avg/max/mdev = 0.426/0.593/0.761/0.167 ms

ttl=64 again: layer 2, no router in the path. Frames for 10.19.219.20 left eth2.219 carrying the 802.1Q tag; the peer's eth2.219 stripped it.

Now the failure that makes VLANs worth understanding. On rtserver, as john, create a VLAN with the wrong ID, move the address onto it, and bring it up:

$ sudo ip link add link eth2 name eth2.220 type vlan id 220
$ sudo ip addr del 10.19.219.20/24 dev eth2.219
$ sudo ip addr add 10.19.219.20/24 dev eth2.220
$ sudo ip link set eth2.220 up

All four are silent - the state looks perfect on both hosts, but on rtclient, as john:

$ ping -c 2 -W 1 10.19.219.20
PING 10.19.219.20 (10.19.219.20) 56(84) bytes of data.

--- 10.19.219.20 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1002ms

The address is present, the links are up, and nothing answers: the server now tags its VLAN 219 traffic with VID 220, and the frames arrive at a device that classifies them into a different VLAN than the one carrying the conversation. A tag mismatch is invisible to ip address and to ping diagnostics - the tool that shows it is a capture on the parent interface (tcpdump -i eth2 -e shows the arriving VID; see Section 17.7.4 for the capture mechanics). Recover on rtserver, as john:

$ sudo ip link del eth2.220
$ sudo networkctl reconfigure eth2.219

Both silent: the runtime VLAN 220 disappears, and reapplying the .network file restores the address the experiment moved. On rtclient, as john, the ping answers again:

$ ping -c 2 10.19.219.20
PING 10.19.219.20 (10.19.219.20) 56(84) bytes of data.
64 bytes from 10.19.219.20: icmp_seq=1 ttl=64 time=0.513 ms
64 bytes from 10.19.219.20: icmp_seq=2 ttl=64 time=0.290 ms

--- 10.19.219.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1029ms
rtt min/avg/max/mdev = 0.290/0.401/0.513/0.111 ms

19.3.3. Building a Bridge for Guests and Services

A bridge is the kernel's implementation of a switch: a virtual device that learns which MAC addresses live behind which port and forwards frames between them. Debian hosts use bridges for virtual machines (Chapter 24 attaches guests to exactly this kind of device), for containers, and for making a service host present on several physical networks with one address per network. The lab builds br0 between rtclient's and rtserver's fifth interfaces.

Three files again - the bridge device, the port that joins it, and the address on top:

# /etc/systemd/network/30-ch19-br0.netdev
[NetDev]
Name=br0
Kind=bridge

[Bridge]
STP=yes
ForwardDelaySec=2
# /etc/systemd/network/30-ch19-br0-port.network
[Match]
Name=eth5

[Network]
Bridge=br0
# /etc/systemd/network/30-ch19-br0.network
[Match]
Name=br0

[Network]
Address=192.168.224.10/24

The .netdev and the port .network are identical on both hosts; the br0 address differs (192.168.224.20 on rtserver). Note what is absent: the port file has no address. An enslaved interface stops being an endpoint - its addresses belong on the bridge, and an address left on the port as well as on the bridge is the classic half-working bridge configuration. STP=yes enables the spanning tree protocol, explained in the next subsection; ForwardDelaySec=2 shortens the listening/learning delay to fit the exercise. Install on rtclient, as john:

$ sudo install -m 0644 br0.netdev /etc/systemd/network/30-ch19-br0.netdev
$ sudo install -m 0644 br0-port.network /etc/systemd/network/30-ch19-br0-port.network
$ sudo install -m 0644 rtclient-br0.network /etc/systemd/network/30-ch19-br0.network
$ sudo networkctl reload
$ sudo networkctl reconfigure eth5 br0

All silent; reconfigure takes both the port and the bridge because both files are new. Mirror the sequence on rtserver with its own address file, then check the result, on rtclient as john:

$ ip -br addr show br0
br0              UP             192.168.224.10/24 fe80::d0a8:88ff:fed8:949b/64

The port's membership and state come from the bridge command:

bridge subcommand Description
link show List ports and their bridge, state, priority, and cost.
fdb show [dev] Dump the forwarding database - learned and permanent MAC entries.
vlan show List per-port VLAN membership once filtering is on.
vlan add / vlan del Change a port's or the bridge's VLAN membership at runtime.
$ sudo bridge link show
7: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100

master br0 names the bridge and state forwarding says the port is fully active - past STP's blocking and learning phases, which is also the place to look when a bridged guest has no connectivity for the first tens of seconds after boot. STP itself is running:

$ cat /sys/class/net/br0/bridge/stp_state
1

One is enabled, zero disabled - a sysfs knob worth knowing because it is the bridge's own state rather than a derived view. Traffic crosses, on rtclient as john:

$ ping -c 2 192.168.224.20
PING 192.168.224.20 (192.168.224.20) 56(84) bytes of data.
64 bytes from 192.168.224.20: icmp_seq=1 ttl=64 time=0.642 ms
64 bytes from 192.168.224.20: icmp_seq=2 ttl=64 time=0.462 ms

--- 192.168.224.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1029ms
rtt min/avg/max/mdev = 0.462/0.552/0.642/0.090 ms

And the switch behavior itself - MAC learning - is visible in the forwarding database, on rtclient as john. Selected output - the entries learned on and owned by eth5, the bridge's one port; the full command lists the permanent multicast entries of every device too:

$ sudo bridge fdb show br0
e6:00:9b:a8:7d:08 dev eth5 master br0
fe:54:00:2c:01:04 dev eth5 master br0
52:54:00:2c:01:04 dev eth5 vlan 1 master br0 permanent
52:54:00:2c:01:04 dev eth5 master br0 permanent

52:54:00:2c:01:04 is eth5's own MAC, permanent by construction; the e6:00:9b:a8:7d:08 entry has no permanent marker - it was learned from a frame that arrived on eth5, which is rtserver's bridge MAC. The bridge now knows which port leads to that address and will send unicast only there, exactly as a switch's CAM table does. Entries age out after gc_timer-driven inactivity; a vanished neighbor reverts to flooding until it speaks again.

19.3.4. Bridge Loops, Spanning Tree, and VLAN Filtering

Two ports of one bridge connected to the same downstream network make a loop, and a loop is the fastest way to stop an Ethernet working: a broadcast frame - an ARP request, an IPv6 router solicitation - arrives on port A, is flooded to port B, comes back on port B, floods to port A, and so consumes the link forever, multiplied by every broadcast source. Spanning tree prevents it: bridges exchange BPDUs, elect a root bridge, and put redundant ports into blocking state so the active topology is a tree. When an active port fails, STP re-elects and unblocks a backup after the forward delay.

That is why br0 was born with STP=yes. A bridge with one port cannot loop, and its port sits in forwarding state as observed above - but the moment a second port joins, or the bridge connects to a real switch also running STP, the protocol is what keeps the network alive. Leaving STP off is a deliberate choice with a known price; some hypervisor hosts do it to avoid the forward delay on single-port bridges, accepting that any future second port must be planned carefully. ForwardDelaySec=2 here shortens the time a port spends in each of STP's listening and learning states - the interval during which a newly active port forwards nothing - so the exercise converges in seconds; a production bridge usually keeps the STP defaults and accepts the same tens of seconds on every port activation.

VLAN filtering turns the bridge into a VLAN-aware switch: with filtering on, each port carries a set of VIDs - one of them the PVID, sent and received untagged; the rest tagged. This is the bridge equivalent of the trunk configuration from Section 19.3.1, and it composes the chapter's two layer-2 devices: VLAN 219 rides through br0 between the two hosts.

One bridge, two broadcast domains

Figure 19.4. Conceptual view of the filtered bridge: untagged traffic belongs to PVID 1 and carries the 192.168.224.0/24 addresses; VID 219 is tagged on the port and present on the bridge itself, so the br0.219 and eth5.219 sub-interfaces share a second broadcast domain over the same wire.

Enable filtering and add VID 219 to the port - on rtclient, as john:

$ sudo ip link set br0 type bridge vlan_filtering 1
$ sudo bridge vlan add vid 219 dev eth5
$ sudo bridge vlan add vid 219 dev br0 self

All three are silent. The self form adds the VID to the bridge device itself, not just a port - without it, the host's own br0 stack would not be a member of VLAN 219, and its sub-interface would have no one to talk to. Existing untagged traffic is undisturbed: a filtered bridge starts with PVID 1 on every port and classifies untagged frames into it, so the 192.168.224.0/24 conversation from the previous subsection keeps working unchanged. Give the host an address inside the new VLAN, as a VLAN device on top of the bridge:

$ sudo ip link add link br0 name br0.219 type vlan id 219
$ sudo ip addr add 10.19.221.10/24 dev br0.219
$ sudo ip link set br0.219 up

And on the far side, rtserver tags its eth5 directly - on rtserver, as john:

$ sudo ip link add link eth5 name eth5.219 type vlan id 219
$ sudo ip addr add 10.19.221.20/24 dev eth5.219
$ sudo ip link set eth5.219 up

Every one of these commands is silent. The tagged path now carries, on rtclient as john:

$ ping -c 2 10.19.221.20
PING 10.19.221.20 (10.19.221.20) 56(84) bytes of data.
64 bytes from 10.19.221.20: icmp_seq=1 ttl=64 time=0.726 ms
64 bytes from 10.19.221.20: icmp_seq=2 ttl=64 time=0.476 ms

--- 10.19.221.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1032ms
rtt min/avg/max/mdev = 0.476/0.601/0.726/0.125 ms

The membership table shows the design, on rtclient as john:

$ sudo bridge vlan show
port              vlan-id
eth5              1 PVID Egress Untagged
                  219
br0               1 PVID Egress Untagged
                  219

eth5 carries VID 1 as PVID, untagged on egress, plus VID 219 tagged; br0 (the bridge itself) matches. Now prove the filter is really filtering - remove VID 219 from the port, on rtclient as john:

$ sudo bridge vlan del vid 219 dev eth5
$ ping -c 2 -W 1 10.19.221.20
PING 10.19.221.20 (10.19.221.20) 56(84) bytes of data.

--- 10.19.221.20 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1019ms

The port drops what it is not a member of - same silence as the tag mismatch in Section 19.3.2, and the same diagnosis: bridge vlan show first, capture second. Restore it, on rtclient as john:

$ sudo bridge vlan add vid 219 dev eth5
$ ping -c 2 10.19.221.20
PING 10.19.221.20 (10.19.221.20) 56(84) bytes of data.
64 bytes from 10.19.221.20: icmp_seq=1 ttl=64 time=0.496 ms
64 bytes from 10.19.221.20: icmp_seq=2 ttl=64 time=0.318 ms

--- 10.19.221.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1015ms
rtt min/avg/max/mdev = 0.318/0.407/0.496/0.089 ms

Clean up the exercise on both hosts - rtclient, as john:

$ sudo ip link del br0.219
$ sudo ip link set br0 type bridge vlan_filtering 0

And rtserver, as john:

$ sudo ip link del eth5.219

All silent; br0 returns to plain switching with its one address. A note on persistence: bridge vlan and ip link ... vlan_filtering are runtime state, like the raw ip route commands of Section 19.1.2 - they vanish at reboot, and the chapter deliberately leaves them that way, because the persistent form changes shape. systemd-networkd accepts the same membership in [BridgeVLAN] sections of the port's .network file and a VLANFiltering= in the bridge's [Bridge] stanza; systemd.network(5) documents both. The boot-time verification in Section 19.7.1 therefore checks the bridge's persisted parts - the device, its port, STP, and the address - which is what the drop-in files own.