Switch Learning and Forwarding
A switch is a layer 2 device, which means that it processes frames and forwards them based on MAC addresses. But how does a switch actually think and take forwarding decisions?
A switch learns MAC addresses from the frames it receives and then stores this information in an internal table. Every time a switch receives a new frame, it stores the source MAC address of that frame along with the port from where it received it in its table. That means that next time the switch receives a frame with this MAC address as a destination, it will know –by looking in its address table- to which port to forward that frame.
What happens though when the switch receives a frame with a destination MAC address that is not included in the table? In that case the switch will just broadcast/flood the frame with the unknown destination address to all of its ports (apart from the port where the frame came from). This process is called unknown unicast flooding. The same will happen if the switch receives a frame with a broadcast/multicast destination address with the difference that this time the destination of the frames is the well-known broadcast/multicast address. A basic example is described below of how a switch, connected to three servers, builds its MAC address table and forwards or floods frames.
In the initial stage, the switch does not know any MAC addresses at all. Let’s assume that the switch receives for first time a frame from A destined to B. When the switch receives this frame from A, it will store the source MAC address (aaaa.aaaa.aaaa) and the port where the frame came from (fa0/1) in its table . So the table will now contain one entry with the information of server A. However, since the switch does not find the address of B in its table, it will have to flood the frame to both ports fa0/2 and fa0/3, so both B and C will receive it:
Let’s now assume that B wants to reply back to A, so the switch now receives a frame from B destined to A. First of all, the switch will store the source MAC address of that frame (bbbb.bbbb.bbbb) and the port the frame came from (fa0/2) in its table as a new entry. In order to forward the frame, the switch will check if the destination MAC address exists in its address table. Since the address of A does exist in the table, the switch will forward the frame only to port fa0/1 (where A is located):
The switch will learn and register in its table the MAC address of C, only when it receives a frame from C. After learning the addresses of all servers, the switch’s address table will look like the picture below:
These MAC addresses are learned dynamically and stored in the table for some certain period. Static MAC addresses can be also configured in the table. This posts describes a basic example of how a switch works. In a switching environment, this learning/forwarding process can cause bridging loops, and therefore the Spanning Tree Protocol (STP) is used to avoid this. Later posts will describe in detail these cases and the protocols used.