Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 79 ยท Data Link Layer

Switches, Bridges and MAC Learning

See how switches learn MAC addresses, flood unknown frames, split collision domains and use Spanning Tree to stop loops, with live Python simulations.

A hub repeats every bit it receives out of every other port, so all attached computers share one collision domain and see everyone's traffic. A switch is the device that fixed that. It reads the destination MAC address of each Ethernet frame and sends the frame only to the port where that address lives, so many conversations can happen at the same time.

What is remarkable is that nobody has to configure this. A switch works out which station is where by watching the traffic go by. In this lesson you will see how that MAC learning works, what the switch does with frames it cannot place, how switches divide a network into collision and broadcast domains, and why redundant links create loops that the Spanning Tree Protocol has to break. Two Python simulations let you watch both mechanisms.

Bridges and switches

A bridge is a two-port (or few-port) device that connects two LAN segments at the data link layer. It was invented to split a busy shared Ethernet in two, so that traffic between neighbors on one side did not occupy the other side. A switch is a multi-port bridge, usually with the forwarding logic in dedicated hardware, so it can handle every port at full line rate. The learning and forwarding rules are identical, and in networking conversation the two words are often used interchangeably.

A switch works at layer 2, so it only looks at MAC addresses and knows nothing about IP addresses or ports. (A "layer 3 switch" also does routing, but that is a second function in the same box.) Most switches are store-and-forward: they receive the whole frame, check the FCS and drop damaged frames, and only then forward it. Some designs use cut-through forwarding, which starts sending as soon as the destination address has arrived to cut latency, at the cost of passing on damaged frames.

The forwarding table

Inside every switch is a MAC address table (also called the CAM table, the forwarding table or the filtering database). Each entry says: this MAC address was last seen on this port. It is built by a simple rule that works on every incoming frame.

Learn, then forward

For each frame that arrives on port P:

  1. Learn. Look at the source MAC address. Store or refresh the entry source -> P, with a fresh timestamp.
  2. Decide. Look at the destination MAC address.
    • If it is in the table and points to a port other than P, forward the frame out of that port only.
    • If it points to P itself, the destination is on the same segment as the sender. Filter (drop) the frame, because the destination has already received it.
    • If it is not in the table (an unknown unicast), flood the frame out of every port except P.
    • If it is a broadcast or multicast address, flood it the same way.

Entries are aged out, typically after five minutes of silence, so the table forgets machines that were unplugged or moved. Because the learning step uses the source address, a station that moves to another port is found again with its first frame.

sequenceDiagram
    participant A as Host A (port 1)
    participant S as Switch
    participant B as Host B (port 2)
    participant C as Host C (port 3)
    A->>S: frame A to B
    Note over S: learn A on port 1, B unknown, flood
    S->>B: frame A to B
    S->>C: frame A to B (C ignores it)
    B->>S: frame B to A
    Note over S: learn B on port 2, A known, forward
    S->>A: frame B to A

After the first frame is flooded, the switch knows both hosts and no longer bothers host C.

Python A learning switch in 40 lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the first two frames: the first frame from aa to bb is flooded because bb is not yet known, while bb's reply is forwarded to port 1 only. The switch learned aa from the first frame.

Collision domains and broadcast domains

Two terms describe what a switch changes and what it does not.

  • A collision domain is the set of devices whose transmissions can collide. A hub extends one collision domain across all its ports. Every switch port is its own collision domain, and with full duplex links there are no collisions at all.
  • A broadcast domain is the set of devices that receive a broadcast frame sent by any one of them. A switch floods broadcasts, so all its ports (and every switch connected to them) form one broadcast domain. Only a router (or a VLAN) divides a broadcast domain.
Device Collision domains Broadcast domains
Hub / repeater 1 for the whole device 1
Bridge / switch 1 per port 1 for the whole device
Router 1 per port 1 per port

Why does the broadcast domain matter? Every host has to process every broadcast (ARP requests, DHCP discovery). In a very large flat network those broadcasts eat bandwidth and CPU, which is one reason networks are divided with VLANs and routers.

Loops and the Spanning Tree Protocol

Network designers want redundancy: if one link or switch fails, traffic should find another way. The obvious design is to connect switches in a ring or a mesh. But a layer 2 network with a loop is dangerous, for a reason that comes straight from the rules above.

Imagine three switches in a triangle. Host A sends a broadcast to switch 1. Switch 1 floods it to switches 2 and 3. Each of those floods it onward, so switch 2 sends a copy to switch 3 and switch 3 sends a copy to switch 2, and each of those floods again. Ethernet frames have no TTL, so the copies circle forever and multiply at every switch. This is a broadcast storm, and it can bring a network down in seconds. A loop also confuses the MAC table: the same source address appears on two different ports as copies arrive over both paths, so the entry flaps back and forth.

The fix is the Spanning Tree Protocol (STP, IEEE 802.1D). Switches exchange small messages called BPDUs (bridge protocol data units) and together choose a loop-free subset of the links, a spanning tree, while leaving the extra links blocked but ready. If an active link fails, a blocked one is brought into service.

The election works in steps:

  1. Choose a root bridge. Every switch has a bridge ID: a priority (default 32,768, configurable) followed by its MAC address. The lowest bridge ID wins and becomes the root of the tree.
  2. Choose a root port on every other switch. Each switch finds the cheapest path to the root, where link cost grows as speed drops (for example 19 for 100 Mbit/s and 4 for 1 Gbit/s in the classic scheme). The port on that path is the root port.
  3. Choose a designated port on every segment. On each link, the end whose switch has the lowest path cost to the root (ties broken by bridge ID) forwards traffic and is the designated port.
  4. Block everything else. A port that is neither a root port nor a designated port goes to the blocking state and does not forward data frames.
flowchart TB
    S1["S1 (root)<br/>priority 4096"]
    S2["S2<br/>priority 32768"]
    S3["S3<br/>priority 32768"]
    S1 ---|"forwarding"| S2
    S1 ---|"forwarding"| S3
    S2 -.-|"S3 side blocked"| S3

In a triangle, the link between the two non-root switches is blocked at one end, which breaks the loop.

The original STP took 30 to 50 seconds to react after a change because ports passed through listening and learning states. Rapid STP (RSTP, 802.1w) reaches the same result in about a second or less, and is what most modern switches use by default. Vendors also provide a per-VLAN variant, and MSTP (802.1s) groups VLANs into a few trees. Edge ports that connect to end hosts are normally set to a portfast or edge mode so that they come up immediately and do not take part in the election. Many networks also add protections such as BPDU guard, which shuts down an edge port that receives a BPDU, so that a rogue switch cannot change the tree.

Python Elect a root and block the loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical points

  • Switches do not isolate security. A frame to an unknown destination is flooded, and an attacker can fill the table with fake source addresses (MAC flooding) so that the switch behaves like a hub. Port security limits how many MAC addresses are learned on a port.
  • The table has a limit. Cheap switches may hold only a few thousand addresses, and a data-center switch many hundreds of thousands.
  • Managed vs unmanaged. Unmanaged switches learn and forward with no configuration. Managed switches add VLANs, STP tuning, port mirroring and monitoring.
  • Mirroring ("SPAN" port) copies traffic from one port to another so that you can capture packets on a switched network, where you would otherwise see only your own traffic.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A switch receives a frame whose destination MAC address is not in its table. What does it do?

    Unknown unicast frames are flooded. If the destination answers, its reply teaches the switch where it is.
  2. 2. Which part of a received frame does a switch use to learn the table?

    The source address says which host is reachable via the port the frame arrived on. The destination address is used to decide where to forward.
  3. 3. A switch has 24 ports and no VLANs. How many collision domains and broadcast domains does it create?

    Each port is its own collision domain, but switches flood broadcasts, so all ports share a single broadcast domain.
  4. 4. Why does a layer 2 loop cause a broadcast storm?

    Nothing removes a looping frame from the network, and every switch floods each copy to more ports, so the number of frames grows rapidly.
  5. 5. How does STP choose the root bridge?

    Bridge ID is the priority followed by the MAC address, and the lowest value wins. Lowering the priority on the switch you want is the standard way to pick the root.

Summary

  • A switch learns the source MAC of every frame, then forwards by destination: known destinations go out of one port, unknown ones and broadcasts are flooded, and same-segment frames are filtered.
  • Table entries age out after a few minutes, so moved or removed hosts are forgotten.
  • Each switch port is a collision domain; the whole switched network is still one broadcast domain until a router or VLAN splits it.
  • Redundant layer 2 links create loops and broadcast storms because Ethernet frames carry no TTL.
  • STP (and faster RSTP) elects a root bridge, picks root and designated ports, and blocks the rest, keeping the redundant links ready for failures.

Next, learn how to split one switch into several virtual networks and how hosts find MAC addresses: VLANs and ARP.