An Ethernet switch is a Layer 2 (data link layer) device, therefore switch uses MAC addresses to make forwarding decisions. It is completely unaware of the protocol being carried in the data portion of the frame, such as an IPv4 packet. The switch makes its forwarding decisions based only on the Layer 2 Ethernet MAC addresses.
Not like an Ethernet hub that repeats bits out all ports except the incoming port, an Ethernet switch consults a MAC address table to make a forwarding decision for each frame. The MAC address table is sometimes referred to as a content addressable memory (CAM) table.
Learning MAC Address Table
The switch automatically builds the MAC address table by examining the source MAC address of the frames received on any port. The switch forwards frames by searching for a match between the destination MAC address in the frame and an entry in the MAC address table. The following process is performed on each Ethernet frame that enters a switch.
Learning the Source and destination MAC Addresses
Every frame that enters a switch it any port is checked for new information to learn. It does this by examining the frame’s source MAC address and port number where the frame entered the switch.
If the source MAC address does not exist, it is added to the table along with the incoming port number. See table in the movie, PC-1 is sending an Ethernet frame to PC-6. The switch adds the MAC address for PC-1 to the table against port-1. If the destination MAC address is on the table, it will forward the frame out the specified port. As you can see in the table there is no mac address entry so the switch will flood the packet to all ports except incoming port. All other will discard the packet and PC-6 will reply. When PC-6 Reply switch will add mac address of PC 6 which is on port-6.By this method, the switch will add all mac addresses against each port. If the destination MAC address is a broadcast or a multicast, the frame is also flooded out all ports except the incoming port.
If the source MAC address does exist, the switch updates the refresh timer for that entry. By default, most Ethernet switches keep an entry in the table for 5 minutes. If the source MAC address does exist in the table but on a different port, the switch treats this as a new entry. The entry is replaced using the same MAC address but with the more current port number.