Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Switch functions at layer 2
Next article icon

Switch functions at layer 2

Last Updated : 09 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite - Switch 
The switch is a layer 2 device that works on the basis of the MAC address (physical address) of a device. Switch mainly performs these functions: 

  1. Learning - The switch learns the MAC address of the device on the switch port on which it receives the frame.
  2. Forwarding - The switch does 2 types of message forwarding: 
    (a) Unicast: The switch unicasts the frame to the destination only when it has an entry for destination MAC address in its MAC address table. 
    (b) Unknown Unicast: When a switch receives a unicast frame for a destination for which the switch has no entry in its MAC table then the switch simply broadcasts the frame through all ports. This is known as flooding.
  3. Filtering - The frame will be forwarded through that switch port only for which the switch has already learned the MAC address in its MAC table.
  4. Loop avoidance - For redundancy, two switches are connected to each other through two links which can also result in layer 2 loops. These loops are avoided by switching by using the STP(Spanning tree protocol) protocol.

Note - An entry in the switch MAC table, also known as CAM (Content Addressable Memory), can remain up to 300 seconds. When a frame is received for a destination MAC address, the time limit of 300 seconds gets reset. MAC table has 4 entries: 

  • Port Number - The switch port is attached to the destination MAC.
  • MAC Address - MAC address of that host which is attached to that switch port.
  • Type - It tells us about how the switch has learned the MAC address of the host i.e static or dynamic. If the entry is added manually then it will be static otherwise it will be dynamic.
  • VLAN -It tells about to which Vlan the host, attached to that switch port, belongs.

How does switch learn the Mac address? 
The switch will update its MAC table only when it receives any frame from the host. If there is no entry for the destination host then switch will first learn the Mac address of the source host and then flood the frame through all its ports except the port on which the frame is received but if there is an entry for the destination host in Mac table of the switch then it will be unicast. 

Let's see this by an example. 
Example - Here is a small topology having host A (192.168.1.1/24), host B (192.168.1.2/24), host C (192.168.1.3/24), host D (192.168.1.4/24), and 2 switches in between. It will understand the switch MAC address learning process by ping from host A to Host C. Initially both switches MAC tables have an entry for another switch only.  

As soon as the user tries to ping host C, he sees that 2 packets are generated: one of ICMP and the other of ARP.  

1

First, ARP will be resolved i.e ARP request is broadcast in the network by host A.  

2


The switch0 receives the broadcast ARP request and will update its MAC table. 

Note - The switch0 already has an entry for Switch1. The switch0 broadcasts the frame in return to switch1 and the PC. Host B will discard the frame as it is not destined for it.  

666666

Now, switch1 will receive the frame and will first update its MAC table. 

Note - Here, switch1 has learned the different MAC addresses of switch0 and host A on the same port fa0/1 because host A is attached to switch0 on fa0/1, therefore, showing the same port fa0/1. Now, switch1 will broadcast the ARP frame to host C and host D as these are present in the same broadcast domain. 

4

Host C will generate an ARP reply which is unicast to the switch. The switch1 will update its MAC table putting an entry for host C. 

Both the switch(switch0 and switch1) will unicast this ARP reply to host A this time because this time switches have already learned the MAC address of host A.  

5

The frame is successfully delivered to host A resolving the ARP.  

111111

Now the ICMP echo request is unicast to host C.  

7


 

8


 

9

The ICMP echo reply is unicast to host A.  

10


 

11


 

12

Remember - 
When the switch will broadcast? If the host has broadcast a frame then the switch receiving it will broadcast it further through its ports anyway. 
When the switch will do flooding (unknown unicast)? If the host has unicast a frame and the switch doesn't have a destination Mac address in its Mac table then the switch will flood through all its ports except the port on which the frame is received. 
When the switch will unicast? If the switch has an entry for the destination host in its Mac table then the frame will be unicast.
 


Next Article
Switch functions at layer 2

S

saurabhsharma56
Improve
Article Tags :
  • Misc
  • Computer Networks
Practice Tags :
  • Misc

Similar Reads

    Layer 3 Switches in Cisco
    A layer 3 Switch is a special type of networking device which is able to perform/execute functions of 2 layers of the OSI Model i.e., the Data Link Layer (Layer 2) and the Network Layer (Layer 3). In simple words, a Layer 3 Switch is a networking device that can perform switching (functions of layer
    3 min read
    Difference between layer-2 and layer-3 switches
    A switch is a device that sends a data packet to a local network. What is the advantage of a hub? A hub floods the network with the packet and only the destination system receives that packet while others just drop due to which the traffic increases a lot. To solve this problem switch came into the
    5 min read
    Functions of Physical layer in OSI Model
    The physical layer is the lowest layer of the OSI model. The physical layer consists of various networking devices such as connectors, receivers, plugs, modems, repeaters, cables, Hubs, etc. The rules and procedures that are defined for interaction between the physical layers are known as physical l
    5 min read
    How States are Decided in a Switch?
    In the seven-layer OSI model, the hubs are broadcast devices that operate on layer-1 in half-duplex mode, so only one device can transfer data at a time. To overcome this issue, bridges were introduced. The bridge is used to segment a LAN connection into smaller sub-networks. The bridge stores the M
    7 min read
    Difference Between Router and Layer-3 Switch
    Routing traffic is a crucial part of network implementation and management, making it essential to choose the right device for the job. Routers and Layer-3 switches are both commonly used for this purpose. While they both handle routing, they differ in functionality, features, and ideal use cases. S
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences