Skip to content
Visual explainer 2h 30m to learn 4 steps

Switches & MAC learning

Explain how a switch learns, floods and forwards frames.

By Invicly Tech, , in Computer Networking. About 10 minutes to read.

Topic 6 of 51

In short

A switch forwards frames by MAC address. It learns which port each source MAC lives on, stores that in its MAC address table and sends later frames only out of the right port. Frames to unknown or broadcast destinations are flooded to every other port in the VLAN.

Explained simply

A switch is a box with many Ethernet ports that connects devices in a LAN. Its job is simple: when a frame arrives, send it out of the port where the destination device lives, and nowhere else. That keeps traffic private between the two devices and leaves other ports free.

A switch is not told where devices are. It learns. Every time a frame arrives, the switch looks at the sender's MAC address and notes which port it came in on. Over a few seconds it builds a table of which MAC lives on which port.

If a frame arrives for a MAC the switch has not learned yet, it does the safe thing: it sends a copy out of every other port, which is called flooding. When the destination replies, the switch learns its port, and from then on it sends frames only there.

Before switches, networks used hubs, which copied every bit to every port. Only one device could talk at a time, and collisions were common. A switch gives each port its own lane, so many pairs of devices can talk at full speed at the same moment.

Press Play on a diagram to walk through it one step at a time.

FlowWhat a switch does with each frame
  1. Step 1
    Frame arrives

    On port 3 from MAC A.

  2. Step 2
    Learn

    Record MAC A on port 3.

  3. Step 3
    Look up destination

    Search the MAC table for MAC B.

  4. Step 4
    Forward or flood

    Known: one port. Unknown or broadcast: all others.

FlowDecision for one incoming frame
  1. 1
    Learn source

    Save source MAC, port, VLAN; reset age

  2. 2
    Broadcast or multicast?

    Yes: flood to all other ports in the VLAN

  3. 3
    Destination in table?

    No: flood as unknown unicast

  4. 4
    Same port it came from?

    Yes: filter (drop) it

  5. 5
    Forward

    Send out of the one learned port

In depth

Learn, flood, forward, filter

For every frame, a switch does two independent things. First it learns: it records the source MAC, the incoming port and the VLAN in its MAC address table, resetting that entry's age timer. Then it decides where the frame goes, based on the destination MAC. If the destination is known and on a different port, it forwards the frame out of that port only. If the destination is on the same port the frame came in on, it filters (drops) it, since the receiver already saw it. If the destination is unknown, broadcast or (without snooping) multicast, it floods to all other ports in the same VLAN.

Entries that are not refreshed age out, by default after 300 seconds on Cisco switches and Linux bridges. If a MAC shows up on a new port, the entry simply moves.

SequenceA switch learns two hosts
Host A (port 1)
Switch
Host B (port 2)
Host C (port 3)
  1. 1Frame A to B; learn A = port 1
    Host A (port 1) to Switch: Frame A to B; learn A = port 1
  2. 2B unknown: flood copy
    Switch to Host B (port 2): B unknown: flood copy
  3. 3Flood copy (C ignores it)
    Switch to Host C (port 3): Flood copy (C ignores it)
  4. 4Reply B to A; learn B = port 2
    Host B (port 2) to Switch: Reply B to A; learn B = port 2
  5. 5A known: forward to port 1 only
    Switch to Host A (port 1): A known: forward to port 1 only
  6. 6Next frame A to B: port 2 only
    Host A (port 1) to Switch: Next frame A to B: port 2 only

Hubs, collision domains and broadcast domains

A collision domain is a group of devices where two transmissions at the same time destroy each other. A hub puts every port in one collision domain, so devices must use CSMA/CD: listen, send, detect a collision, wait a random time and retry. Throughput drops sharply as more devices talk.

A switch makes each port its own collision domain, and with full duplex there are no collisions at all. But a switch does not split broadcast domains. A broadcast entering any port goes out of every other port in the same VLAN, so a flat network of 2,000 hosts on one VLAN means every host hears every ARP and DHCP broadcast. To split broadcast domains you need VLANs or routers.

  • Hub: 1 collision domain, 1 broadcast domain
  • Switch: 1 collision domain per port, 1 broadcast domain per VLAN
  • Router: each interface is its own broadcast domain

Store-and-forward versus cut-through

A store-and-forward switch receives the whole frame, checks the FCS and only then sends it on. Damaged frames are dropped at the first switch, and the switch can connect ports of different speeds. The cost is latency: the full frame must arrive first, about 12 microseconds for a 1500-byte frame at 1 Gbps.

A cut-through switch starts sending as soon as it has read the destination MAC, saving that wait. It is popular in data centres and trading networks where microseconds matter, but it can pass on a corrupted frame, which is only dropped further along. Most campus switches use store-and-forward; many data-centre switches use cut-through when both ports run at the same speed.

When the MAC table goes wrong

The MAC table is finite, often 8,000 to 128,000 entries. In a MAC flooding attack, a tool like macof sends frames from thousands of fake source MACs until the table is full. New, real hosts can no longer be learned, so their traffic is flooded to every port, and an attacker on any port can capture it. Port security, which limits how many MACs each port may learn, stops this.

Another warning sign is MAC flapping: the same MAC is learned on two ports in turn, many times a second. It usually means a layer 2 loop, or two devices using the same MAC. A quieter issue is unknown unicast flooding: if a router's ARP entry lives longer than the switch's MAC entry (Cisco defaults are 4 hours and 5 minutes), the switch forgets a silent host and floods traffic to it.

  • Port security: switchport port-security maximum 2
  • Flapping MAC: look for a loop first
  • MAC aging 300 s vs Cisco ARP timeout 4 h can cause flooding

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Explain MAC address table learning~40m
  2. 2
    Describe flooding of unknown destinations~40m
  3. 3
    Compare hubs, switches and collision domains~40m
  4. 4
    Inspect a MAC table in a simulator~40m

Key points

  • Each switch port is its own collision domain.
  • All ports in one VLAN share a broadcast domain.
  • MAC table entries age out, typically after 300 seconds.
  • Hubs repeat everything to every port; switches replaced them.

Key terms

Tap a card to flip it. 0/4 known

Examples

text
Switch# show mac address-table
          Mac Address Table
Vlan    Mac Address       Type        Ports
----    -----------       --------    -----
  10    0050.7966.6800    DYNAMIC     Fa0/1
  10    0050.7966.6801    DYNAMIC     Fa0/2

Cisco IOS output in Packet Tracer.

Watching the MAC table fill in Packet Tracer

Build one switch with three PCs on Fa0/1, Fa0/2 and Fa0/3. Step 1: run show mac address-table; it shows no dynamic entries yet. Step 2: ping from PC1 to PC2. The ARP broadcast teaches the switch PC1's MAC on Fa0/1, and the ARP reply teaches PC2's MAC on Fa0/2. Step 3: show the table again and see two DYNAMIC entries. PC3 is still missing because it has not sent anything. Step 4: in simulation mode, watch the first ARP flood to PC3, while the later ICMP packets go only to PC2.

text
Switch# show mac address-table dynamic
Switch# show mac address-table address 0050.7966.6800
Switch# show mac address-table aging-time
Switch# clear mac address-table dynamic

A Linux bridge is a software switch

Linux can act as a switch, which is how VMs and containers share a host's network. Step 1: create a bridge and attach two interfaces. Step 2: bring them up. Step 3: after some traffic, bridge fdb show lists the learned MACs per port (the forwarding database, the same idea as a MAC table). Step 4: the ageing time is shown in centiseconds, so 30000 means 300 seconds, the same default as hardware switches.

bash
sudo ip link add br0 type bridge
sudo ip link set eth1 master br0
sudo ip link set eth2 master br0
sudo ip link set br0 up; sudo ip link set eth1 up; sudo ip link set eth2 up
bridge fdb show br br0 | grep -v permanent
ip -d link show br0 | grep -o 'ageing_time [0-9]*'

Quick reference

  • Switches learn from the source MAC and forward on the destination MAC
  • Unknown unicast, broadcast and unsnooped multicast are flooded within the VLAN
  • Default MAC aging: 300 seconds (Cisco and Linux bridge)
  • Each switch port = its own collision domain; each VLAN = one broadcast domain
  • Cisco: show mac address-table dynamic; Linux: bridge fdb show
  • MAC flapping between two ports usually means a layer 2 loop
  • Port security limits MACs per port and blocks MAC flooding attacks

Common mistakes

  • Assuming a switch separates broadcast domains; only VLANs or routers do.
  • Forgetting that a full MAC table makes a switch flood like a hub.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A switch receives a frame. Which address does it use to update its MAC table?

Question 2

2How many collision domains and broadcast domains does a 24-port switch with one VLAN have?

Question 3

3A frame arrives on port 5 for a MAC the table says is on port 5. What does the switch do?

Question 4

4An attacker fills the MAC table with fake addresses. What is the effect?

Question 5

5Which switching mode starts sending a frame before it has fully arrived?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    In Packet Tracer, connect three PCs to one switch, ping between two of them, and show the MAC table before and after.

  2. Exercise 2medium

    Configure a Cisco access port to allow at most 2 MAC addresses, learn them automatically, and drop (not shut down) frames from extra MACs.

  3. Exercise 3hard

    A server on one VLAN receives heavy traffic meant for another host every few minutes. The router's ARP timeout is 4 hours and the switch's MAC aging is 5 minutes. Explain the cause and two fixes.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need to configure a switch to make it work?
No. An unmanaged switch learns MACs by itself as soon as it is powered on. Managed switches add VLANs, security and monitoring, which you configure.
What is the difference between a MAC table and an ARP table?
A switch's MAC table maps MAC addresses to switch ports (layer 2). A host or router's ARP table maps IP addresses to MAC addresses (layer 2 to 3).
Why is it called a CAM table?
Switches store the MAC table in content-addressable memory (CAM), a chip that finds a matching entry in one step, so lookups keep up with line rate.
ShareXLinkedInWhatsApp