A typical LAN(Local Area Network) in any company consists of the various devices connected to each other as shown in the above fig:
The devices like computers, printers, IP phones etc which the staff of the company uses are connected to other hardware through Layer 2 switches. These devices are also connected to the server through layer 2 switches only. These layer 2 switches are also connected to each other. Normally the servers and switches are placed in the server room of the company. The layer 2 switches will help the data from the hardware devices like computers, printers, IP phones, etc to pass to the servers and vice versa.
Functions of a Switch
The job of a Layer 2 switch is to forward Layer 2 packets which are called frames. The main task of the switch is to deliver that frame to the right destination(MAC) address. For that purpose, the switch uses the source and destination MAC address as parameters that are available in the ethernet header of the frame.
Decision making of Ethernet Switch
Whenever the switch receives the ethernet frame on any of the port it has to decide whether to forward it through other ports or to drop it. In order to decide this, it performs three functions
1)It reads the destination MAC address contained in the ethernet frame header and decides whether to forward or drop the frame.
2)It learns the MAC addresses by checking the source MAC address of each frame which is received by the switch.
3)It uses Spanning Tree Protocol(STP) to avoid loops when more than one switch is used.
Frame forwarding:
The switch contains a table called MAC address table which lists the MAC addresses and the ports through which those MAC address can be reached. The switch reads the destination MAC address available in the frame and cross checks it with the MAC address table and decides whether to forward or drop the packet.
In the above figure PC-1 wants to send data frame to PC-2.The MAC address of PC-2 is 0400.1111.2222. So PC-1 builds a layer 2 frame with the destination MAC address as 0400.1111.2222 and forwards that frame to the F0/1 port of the switch.Now the switch receives the frame on the F0/1 interface. The switch reads the destination MAC address of the received frame as 0400.1111.2222 and it cross checks this address with the MAC address table . The MAC address table is shown in the figure. The MAC address table shows that the MAC address 0400.1111.2222 is mapped to F0/4 interface of the switch. So the switch forwards that frame out through F0/4 interface and thus to the PC-2 which is the destination MAC address.
Learning MAC Addresses:
The switches learn the MAC address from the source MAC address of the frame to build the MAC address table. Whenever a frame is received through any interface the switch reads the source MAC address of that frame and checks if there an entry in the table with this MAC address. If its not there then it creates an entry in the MAC address table with the source MAC address of the frame and the interface through which it is received (entered the switch).
The below example illustrates the process of Learning MAC address by the switch to build the MAC address table.
Lets assume the below switch is a new one and its MAC address table is empty as shown in the below figure.
Lets say PC-1 wants to send a Frame1 to PC-2. So the PC-1 builds the Frame1 and forwards it to the switch.PC-1 is connected to the switch on the F0/1 interface. The switch receives the Frame1 on the F0/1 interface.The switch reads the frame source MAC address and creates a entry in the MAC address table noting down this source MAC address and the interface through which it received this frame.The below figure shows the first entry in the MAC address table.
After the first MAC entry is made in the MAC address table, the switch reads the destination MAC address of Frame1 and cross checks it with the MAC address table.In this case, since the PC-1 wants to send the frame to PC-2 the destination MAC address would be the MAC address of PC-2 which is 0400.1111.2222. But this destination MAC address is not available in the MAC address table. So the switch makes multiple copies of the Frame1 and sends each copy out of each interface except the interface through which it received this frame. In this case since the frame is received on F0/1 interface, it will not send the copy of Frame1 out of this interface.So the switch will send the each copy out of interfaces F0/2,F0/3 and F0/4.This process of sending copies of the frame through all the interfaces except the interface on which it is received is called flooding.The flooding process is shown in the below figure.
Lets say now PC-2 wants to send a Frame2 to PC-4. So the PC-2 constructs the Frame2 and forwards it to the switch.PC-3 is connected to the switch on the F0/2 interface.So the switch receives the Frame2 on the F0/2 interface. The switch reads the frame's source MAC address and creates an entry in the MAC address table, noting down this source MAC address and the interface through which it is received.The below figure shows the second entry in the MAC address table.
So all the PC's(PC-1,PC-3 and PC-4) will receive the Frame1. Since the Frame1's destination MAC is the MAC address of PC-2, the PC-2 will accept the Frame1 and sends the reply to PC-1.This reply will have the frame whose source MAC address will be the MAC address of PC-2.When the switch receives the frame, the switch will read the source MAC address and will adds this MAC address (0400.1111.2222) as the second entry in the MAC address table with the F0/4 interface in the port column as shown below.
The remaining PC's(PC-3 and PC-4) will ignore this frame as it does not belong to them.
Similarly, the remaining two entries are added as shown in the below two figures.
Avoiding Loops using STP
The third function of a switch is to avoid loops using STP. In real-time scenerio’s we use more than one switch for redundancy purpose. In such cases whenever flooding of frame occurs, the frame looping happens. To avoid looping of frames, STP is used. STP blocks some ports so that only one path is available at any particular point of time for the frame to reach from source to the destination.