Switches
Switch
Joins devices into one network, forwarding each frame toward the port its destination MAC was seen on.
The problem
Alice, bob and carol need one network, but each frame should reach only the card it is for.
- One cable from each of them ends in a port of officeSwitch; on Linux the switch is a bridge,
br0. - Alice pings bob: the switch reads the destination MAC in her frame,
06:c7:51:36:4e:b8, bob's. - It has seen that MAC arrive on the port
bob, so it sends the frame out of that port only. - Carol's port gets nothing, and the frame leaves unchanged: same MACs, same TTL. A switch is not a hop.
What you'd see
$ bridge link show
14: alice@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
16: bob@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
18: carol@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 2
- One line per port, named after who is at the other end of its cable:
alice,bob,carol. master br0: each port belongs to the bridgebr0, the switch.state forwarding: the port passes frames. Spanning tree can set a port toblockinginstead.
Guess first
Alice runs tracepath -n 10.0.1.11 to bob, through officeSwitch. What is hop 1?
Show answer
Bob himself: 1: 10.0.1.11 … reached. The switch does not lower the TTL, so it never shows up as a hop.
In short
A switch joins devices into one network and learns, from the frames it sees, which MAC address is behind which
port. It sends each frame only to that port; broadcasts and frames for unlearned addresses go out of every port. A switch is not a hop: it does not lower the TTL or appear in traceroute. On Linux a switch is a Linux
bridge (ip link add br0 type bridge).
How a switch works, step by step
A switch joins devices into one network and forwards each Ethernet frame toward the port its destination MAC
address was last seen on. It reads MAC addresses, not IP addresses, so it works one layer below routing. On Linux a
switch is a bridge. In the learning switches lab frontDesk 10.0.1.10, xrayRoom
10.0.1.20 and billing 10.0.1.30 each have one cable into clinicSwitch, a bridge br0 whose only
address, 10.0.1.2, is for management. The switch follows four rules (IEEE 802.1D):
- Learn. A frame arriving on a port teaches the switch that its source MAC sits behind that port. A machine that has sent nothing is in no table yet.
- Forward. When the destination MAC is known, the frame leaves by that one port only.
- Flood. An unknown destination, or a broadcast (
ff:ff:ff:ff:ff:ff), goes out every port but the one it came in on. This is why ARP and early frames reach everyone. - Age. An entry not refreshed for the aging time (300 seconds by default) is dropped, so a device that moves or goes quiet is relearned when it next sends.
Everything on one switch, or one VLAN of it, is a single broadcast domain. A switch is not a hop: it does not
lower the TTL, never appears in traceroute, and devices store each other's MAC, never the switch's. Keeping
several networks apart on one switch is the job of a VLAN.
What it looks like on a Linux machine
You build a switch with the bridge link type and enslave cards to it:
$ ip link add br0 type bridge
$ ip link set eth0 master br0
$ bridge link show
3: eth0: ... master br0 state forwarding priority 32 cost 2
The MAC address table is the forwarding database, bridge fdb. After frontDesk pings xrayRoom, the switch has
learned both from the frames' source addresses:
$ bridge fdb show br br0 dynamic
02:00:00:00:00:10 dev frontDesk master br0
02:00:00:00:00:20 dev xrayRoom master br0
bridge -s fdb adds timers; aging counts from updated, seconds since a frame last came from that address.
ip -d link show br0 prints the aging time (in hundredths of a second) and the learned-entry counters:
$ ip -d link show br0
... bridge ... ageing_time 30000 ... fdb_n_learned 2 fdb_max_learned 0
A VLAN-aware bridge filters by tag, and bridge vlan show lists each port's VLANs. Flush the whole table with
ip link set br0 type bridge fdb_flush.
What goes wrong, and how you see it
A port is not forwarding. bridge link show shows state disabled or blocking instead of
forwarding, and nothing crosses it. A down member card or spanning tree blocking a loop is the usual cause.
One device sees another's traffic. Flooding is normal for unknown or broadcast frames, so a capture on
billing catching a frame addressed to xrayRoom is expected until the switch has learned xrayRoom. If it never stops, the table is not
learning.
The table is full and the switch floods everything. With a fixed table size, an attacker sends
frames from thousands of made-up MACs until fdb_n_learned hits fdb_max_learned; real addresses can no longer
be learned, so every frame is flooded and the switch behaves like a hub. This is
MAC flooding, and port security is the defence.
No connectivity through the switch, but pings to its address work. The management address on br0 answers
while forwarding is broken; do not confuse the switch's own address with its forwarding. The
learning switches lab watches a table fill, empty and flood, and the
MAC address table page covers each case.
Questions people ask
Is a Linux bridge a real switch? Yes. ip link add type bridge creates a software switch that learns,
floods, ages and (with VLAN filtering) tags exactly as a hardware switch does; it is what the labs run.
Switch vs router? A switch forwards by MAC within one network and is invisible at layer 3; a router forwards by IP between networks and is a hop you send frames to. Traffic between VLANs goes through a router.
Switch vs hub? A hub repeats every frame out every port with no table, so everyone hears everything; a switch learns and sends each frame only where it belongs. See network devices.
Read next
- MAC address table A switch's record of which port each MAC address was last seen on, learned from source addresses.
- Broadcast domain The set of devices a broadcast reaches: one network at the link layer.
- VLAN Several separate networks on one switch, kept apart by a tag in each frame.
- MAC address The 48-bit address of a network card, used to deliver frames within one network.
- Spanning tree The protocol that lets switches be cabled in loops by electing a root and blocking the ports that close a loop.
- veth pair Two virtual cards joined like the ends of a cable: what goes in one comes out the other, even across namespaces.
- Hub The switch's ancestor: it repeats every frame out of every port, so everyone hears everything.
- Port mirroring (SPAN) A switch copying every frame of some ports out of another port, where a sensor or capture box listens.
- LLDP Devices announcing their name, port and addresses to whoever is at the other end of each cable.
- CDP Cisco's own discovery protocol, on by default on every Cisco device.
- Edge port A spanning-tree port that faces a single host, so it forwards the instant the link is up — plus the guards that protect it.
- Link aggregation Several cables between the same two devices joined into one logical link, with one MAC and one address.
- Trunk One link that carries several VLANs, each frame tagged with its VLAN, usually between switches.
- Access point and controller The bridge between the air and the wired network, and the box that manages many of them.
- IGMP snooping A switch reading its devices' IGMP to send each multicast group only out of the ports that joined it.
- Spine-leaf A data center design where every leaf (rack) switch connects to every spine, so any two racks are two hops apart.
- Network namespace A separate copy of the kernel's network: its own cards, addresses, routes and firewall, which is what a container's network is.
- OpenFlow The protocol an SDN controller uses to write match-and-action rules (flows) into a switch's flow table.
- 802.1X and EAP Port login: a switch port (or Wi-Fi association) passes only login frames until the device proves who is using it.
- MAC flooding Filling a switch's MAC table with made-up addresses so that it floods everyone's frames to every port.