Beginner · Ethernet, MAC addresses and the Cisco CLI · topic 8 of 57 · 25 min
How a switch learns and forwards
Explanation
Why learning matters
A hub sends every frame everywhere. A switch is useful because it sends frames only where they need to go. To do that it needs to know where each device is, and it learns this by itself, with no configuration.
The four actions: learn, forward, flood, filter
- Learn: when a frame arrives, the switch reads its source MAC address and writes "this MAC is on this port" in its MAC address table (also called the CAM table). If the entry already exists, it refreshes the timer, or updates the port if the device has moved.
- Forward: it looks up the destination MAC. If the table has it on another port, the frame goes out of that one port only.
- Flood: if the destination is broadcast (ffff.ffff.ffff), a multicast the switch has no special handling for, or a unicast address not in the table (an unknown unicast), the switch sends the frame out of every port except the one it came in on.
- Filter: if the destination MAC is on the same port the frame came in on, the switch drops it, because the destination already got it.
A worked example
A switch has PCs A on port 1, B on port 2 and C on port 3. The table starts empty.
| Frame | Learned | Action |
|---|---|---|
| A to B | A is on port 1 | B unknown: flood out of ports 2 and 3 |
| B to A | B is on port 2 | A known on port 1: forward out of port 1 only |
| C to broadcast | C is on port 3 | Broadcast: flood out of ports 1 and 2 |
| A to C | A refreshed | C known on port 3: forward out of port 3 only |
Notice that the switch learns only from source addresses. It never learns a location from a destination address.
Aging
Devices move and get unplugged, so entries cannot last forever. On Cisco switches, a dynamic entry that has not been seen as a source for 300 seconds (5 minutes) is removed. This is the aging time. If that device talks again, the switch learns it again.
Looking at the table on a Cisco switch
Switch# show mac address-table
Mac Address Table
-------------------------------------------
Vlan Mac Address Type Ports
---- ----------- -------- -----
1 0011.2233.4401 DYNAMIC Gi0/1
1 0011.2233.4402 DYNAMIC Gi0/2
1 0011.2233.4403 DYNAMIC Gi0/3
10 00aa.bbcc.dd01 DYNAMIC Gi0/24
Total Mac Addresses for this criterion: 4Switch# show mac address-table dynamic interface g0/24
Switch# show mac address-table aging-time
Switch# clear mac address-table dynamicIf one port shows many MAC addresses, there is probably another switch, or a virtual machine host, connected to that port. That is normal on links between switches.
Per-VLAN tables and limits
A switch keeps the table per VLAN (VLANs are covered in a later module), and floods only within the VLAN the frame belongs to. The table has a size limit. An attacker can flood a switch with thousands of fake source MAC addresses to fill the table, after which it floods everything like a hub. Port security, covered in the security module, stops this.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
How a switch learns and forwards · 1/7
A switch sends frames only where they need to go. To do that, it learns where every device is.
Prefer a video by a teacher? Find free videos on “how a switch learns MAC addresses forwarding flooding filtering CCNA” on YouTube ↗
In real life: Tracing a printer at an Infosys-style office campus
A help desk engineer needs to find which desk socket a misbehaving printer is plugged into. She knows its MAC address from the printer status page, so she runs show mac address-table address 0011.2233.44aa on the floor switch. It shows port Gi0/37. The patch panel label for port 37 says desk 4B-12. She walks straight there instead of checking 48 desks.
Switch# show mac address-table address 0011.2233.44aa
Vlan Mac Address Type Ports
---- ----------- -------- -----
20 0011.2233.44aa DYNAMIC Gi0/37Practice (0/3 done)
A switch receives a frame whose destination MAC is not in its table. What does it do?
Simulate a switch. Write run_switch(ports, frames) where ports is a list of port numbers and frames is a list of (in_port, src_mac, dst_mac). Start with an empty MAC table. For each frame: learn src_mac on in_port; then if dst_mac is "ffff.ffff.ffff" or unknown, flood (all other ports, in the order of ports); if dst_mac is known on in_port, filter (empty list); otherwise forward (a list with the one known port). Return a list with the result for each frame.
In Cisco Packet Tracer, connect three PCs to one 2960 switch. Give them IP addresses in the same network (for example 192.168.1.1 to 192.168.1.3 with mask 255.255.255.0). Run show mac address-table on the switch, then ping between the PCs and run it again.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- ARP, the default gateway and pingEthernet, MAC addresses and the Cisco CLI · same module
- Cables, fibre and Ethernet standardsEthernet, MAC addresses and the Cisco CLI · same module
- VLANs: what they are and why we use themSwitching and VLANs
- Why layer 2 loops are deadlySpanning Tree and EtherChannel
- Layer 2 security: port security, DHCP snooping, DAINetwork security
- Inter-VLAN routingSwitching and VLANs
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which address does a switch use to LEARN where a device is?
Answer: The source MAC address
Switches learn from the source MAC address of incoming frames.
What is the default MAC address table aging time on a Cisco switch?
Answer: 300 seconds
Dynamic entries are removed after 300 seconds without traffic. 4 hours is the Cisco ARP timeout.
A frame to ffff.ffff.ffff arrives on port 5 of a 24-port switch (all in one VLAN). Where does it go?
Answer: All ports except port 5
Broadcasts are flooded out of every port except the incoming one.
Which command shows the MAC address table on a Cisco switch?
Answer: show mac address-table
show mac address-table lists VLAN, MAC, type and port.
An IP phone and a PC share one switch port. Which command puts the phone traffic in VLAN 50?Related
Answer: switchport voice vlan 50
switchport voice vlan 50 tells the phone to tag voice frames with VLAN 50; the PC stays in the access VLAN.
A switch log keeps saying a host is "flapping between port Gi0/1 and port Gi0/2". What does this suggest?Related
Answer: A layer 2 loop
MAC flapping means the same source MAC keeps arriving on different ports, a typical sign of a loop.
Which configuration helps prevent VLAN hopping by switch spoofing?Related
Answer: switchport mode access on user ports
An access port does not negotiate a trunk, so a host cannot use DTP to turn it into one.
What is the Ethernet broadcast MAC address written in Cisco format?Related
Answer: ffff.ffff.ffff
Broadcast is all ones. 255.255.255.255 is the IPv4 broadcast, not a MAC address.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Ethernet, MAC addresses and the Cisco CLI” before its exam: ARP, the default gateway and ping, The Cisco IOS command line.
- Module 2 exam: Ethernet, MAC addresses and the Cisco CLI · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, IPv4 addressing. 57 topics left in the course before the final exam.