SkillPilotAI

STP, link aggregation and bonding

Redundant cables between switches create loops, and a loop with no protection melts the whole LAN in seconds. STP breaks loops by blocking extra links, while link aggregation (LACP, Linux bonding) does the opposite: it combines several links into one logical link for more bandwidth and failover.

53.1 Why is it used?

  • Three switches in a triangle give an alternate path if one cable fails.
  • But a broadcast (e.g. an ARP from PC 1) is flooded by each switch and circles the triangle forever.
  • Ethernet frames have no TTL (IP packets do), so nothing stops the frame: a broadcast storm.
  • MAC flapping: the switch sees PC 1's MAC on one port, then another, so its MAC table keeps changing.
  • Within seconds links are full and even ping fails.

53.2 How STP works

  • Switches exchange small messages called BPDUs.
  • Bridge ID = priority + MAC address; the lowest bridge ID becomes the root bridge.
  • Every non-root switch picks a root port: its cheapest path to the root.
  • Every segment gets one designated port; remaining ports go into blocking.
  • In the lab Switch 1 has priority 4096 (lower than 28672 and 32768), so it becomes root; Switch 3's port toward Switch 2 blocks.
STP election and failover
  1. 1.Switches exchange BPDUs; lowest bridge ID (Switch 1, priority 4096) = root

  2. 2.Switch 2 and Switch 3 each choose a root port toward Switch 1

  3. 3.On the Switch 2 - Switch 3 link, Switch 2's bridge ID is lower, so Switch 3's port blocks

  4. 4.PC 1 -> Switch 2 -> root -> Switch 3 -> PC 2 (loop logically broken)

  5. 5.Cable Switch 1 - Switch 3 is cut: Switch 3's root port goes down

  6. 6.Blocked port goes listening -> learning -> forwarding; traffic now Switch 2 -> Switch 3 directly

The backup path becomes active automatically

53.3 STP port states

StateWhat happens
BlockingOnly listens to BPDUs, no data
ListeningConfirms the topology
LearningLearns MAC addresses
ForwardingNormal data flows
DisabledPort is off
  • Classic STP (802.1D) takes 30-50 seconds to fail over.
  • RSTP (802.1w) converges in 1-2 seconds, so it is used today.

53.4 Practical: a loop without STP

Three namespaces, each with a Linux bridge as a switch, wired in a triangle with STP off.

Terminal
# sw1, sw2, sw3 in a triangle, STP offip netns exec pc1 arping -c 1 \  -I eth0 192.168.73.99ip netns exec sw1 cat \  /sys/class/net/p2/statistics/rx_packetsip netns exec pc1 ping -c 3 -W 1 \  192.168.73.12
Output
before arping : 2$ arping -c 1 192.168.73.99after 1 sec   : 6763after 2 sec   : 20177$ ping -c 3 -W 1 192.168.73.123 packets transmitted, 0 received,100% packet loss
  • Only one ARP broadcast was sent.
  • Frames on sw1's p2 jump from 2 to 6763 in one second and 20177 in two seconds.
  • Ping fails 100% during the storm.

53.5 Practical: STP on, root bridge and blocked port

Terminal
# inside sw1 (sw2: 28672, sw3: 32768)ip link set br0 type bridge stp_state 1 \  priority 4096 hello_time 100 \  forward_delay 400 ageing_time 1000cat /sys/class/net/br0/bridge/root_idbridge link
Output
sw1: bridge_id 1000.3eb945d4051fsw1: root_id   1000.3eb945d4051fsw2: root_id   1000.3eb945d4051fsw3: root_id   1000.3eb945d4051fsw3# bridge link10: p1@p2: ... state forwarding11: p2: ...    state blocking12: pc@br0: ... state forwardingsw1 p2 frames received in 2 sec: 1$ ping -c 3 192.168.73.123 packets transmitted, 3 received,0% packet loss
  • stp_state 1 enables STP; Switch 1 gets priority 4096; forward_delay 400 (4 s) for the lab.
  • All three switches report the same root_id: Switch 1's bridge ID.
  • Switch 3's p2 is blocking, others forwarding; the same broadcast stays one frame and ping has 0% loss.

53.6 Practical: cable cut, STP failover

Terminal
ip netns exec pc1 ping -c 20 \  192.168.73.12 &ip -n sw1 link set p3 downip netns exec sw3 bridge link
Output
sw3# bridge link   (2 s after the cut)10: p1@p2: <NO-CARRIER,...> state disabled11: p2: ... state listening12: pc@br0: ... state forwarding64 bytes from 192.168.73.12: icmp_seq=364 bytes from 192.168.73.12: icmp_seq=1264 bytes from 192.168.73.12: icmp_seq=1320 packets transmitted, 12 received,40% packet losssw3 p2: state forwarding, root_port 2
  • 2 s after the cut, Switch 3's p1 is disabled and p2 is listening.
  • Ping jumps from icmp_seq 3 to 12: a gap of about 9 s, i.e. 2 x forward delay (listening + learning).
  • p2 ends up forwarding as the new root port.

53.7 Link aggregation: LACP

  • A server with two NICs cabled to a switch stack (works as one logical switch).
  • With normal links STP would block one of them.
  • LACP (Link Aggregation Control Protocol) bundles both into one logical link: port channel on Cisco, bond on Linux.
  • Both ends exchange LACPDUs to confirm they are in the same bundle.
  • Traffic is split per flow, not per packet: a hash of IP + port picks the link, so packets of one flow stay in order.
  • If one cable fails, its flows move to the remaining link.
LACP hashing per flow
  1. 1.Server bond0 = eth1 + eth2, LACP with the switch stack

  2. 2.Flow 1 (src :41000 -> 192.168.73.1:5201): hash picks eth1

  3. 3.Flow 2: hash picks eth2

  4. 4.All packets of one flow always use the same link (no reordering)

  5. 5.eth2 fails: its flows shift to eth1

53.8 Bonding and teaming modes

Output
Linux bonding modesmode 0  balance-rr     round robinmode 1  active-backup  one active, one standbymode 2  balance-xor    hash picks a fixed linkmode 3  broadcast      copy on every linkmode 4  802.3ad        LACP, switch toomode 5  balance-tlb    TX load balancemode 6  balance-alb    TX + RX load balance Windows NIC Teaming (LBFO)Switch Independent     no switch configStatic Teaming         fixed EtherChannelLACP                   like 802.3ad
Pointactive-backup (mode 1)802.3ad / LACP (mode 4)
Links in useOne active, one standbyAll active
Switch supportWorks with any switchSwitch must run LACP too
BandwidthOne linkSum of links (across many flows)
FailoverYesYes
  • Windows Server NIC Teaming offers Switch Independent, Static Teaming and LACP modes.

53.9 Practical: active-backup bond

Terminal
ip link add bond0 type bond \  mode active-backup miimon 100ip link set eth1 master bond0ip link set eth2 master bond0grep -E "Mode|Active|Slave Int|MII S" \  /proc/net/bonding/bond0ip link set eth1 down
Output
Bonding Mode: fault-tolerance (active-backup)Currently Active Slave: eth1Slave Interface: eth1MII Status: upSlave Interface: eth2MII Status: up$ ip link set eth1 down   (during ping)Currently Active Slave: eth2Slave Interface: eth1MII Status: down10 packets transmitted, 10 received,0% packet loss
  • Active slave is eth1 and MII status of both links is up.
  • eth1 down during a ping: active slave becomes eth2 at once; 10/10 replies, 0% loss.

53.10 Practical: 802.3ad (LACP) bond

Terminal
ip link add bond0 type bond \  mode 802.3ad lacp_rate fast \  xmit_hash_policy layer3+4cat /proc/net/bonding/bond0
Output
Bonding Mode: IEEE 802.3ad Dynamic link aggregationTransmit Hash Policy: layer3+4 (1)LACP rate: fastActive Aggregator Info:  Aggregator ID: 1  Number of ports: 2  Partner Mac Address: 66:95:a6:b6:48:c7
  • xmit_hash_policy layer3+4 = the hash uses both IP and port.
  • Aggregator ID 1 with 2 ports and a visible partner MAC: both sides confirmed the bundle.
Terminal
iperf3 -c 192.168.73.1 -P 8 -t 3cat /sys/class/net/eth1/statistics/tx_bytesip link set eth2 downping -c 6 -i 0.5 192.168.73.1
Output
$ iperf3 -c 192.168.73.1 -P 8 -t 3[SUM] 0.00-3.02 sec 10.2 GBytes sendereth1 tx MB: 4316eth2 tx MB: 6661$ ip link set eth2 down6 packets transmitted, 6 received,0% packet loss  Number of ports: 1Slave Interface: eth2MII Status: down
  • 8 parallel TCP flows: 4316 MB on eth1 and 6661 MB on eth2. Spread over both, but not half-half, because the hash divides flows, not bytes.
  • eth2 down: 0% loss and Number of ports becomes 1.

53.11 Common mistakes

  • Turning STP off for speed: one wrong cable can take the whole LAN down.
  • Keep RSTP on, make PC ports edge ports / PortFast, and enable BPDU Guard (unknown switch plugged in = port off).
  • Expecting one file copy over 2 x 10 Gbps LACP to run at 20 Gbps: one TCP flow always hashes to one link, so ~10 Gbps.
  • Using 802.3ad on the server without LACP configured on the switch.

53.12 Interview tips

  • Root bridge = lowest bridge ID (priority + MAC); in production fix it manually with a low priority on the core switch.
  • Non-root switches pick the root port (lowest path cost), each segment has one designated port, the rest block.
  • Ethernet has no TTL, which is why loops cause broadcast storms.
  • RSTP converges in 1-2 s vs 30-50 s for classic STP.
  • LACP balances per flow, not per packet; 20 Gbps total only with many parallel flows.