A switch can learn MAC addresses in two ways; statically or dynamically. In the static option, we manually add MAC addresses to the CAM table. In the dynamic option, the switch automatically learns and adds MAC addresses to the CAM table.
Adding MAC addresses to the CAM table is a tedious task. For example, if you have 1000 devices connected through the five switches, you have to make 5000 (1000 * 5) entries. Furthermore, if you relocate any device, you have to update all related entries.
In the dynamic option, a switch automatically updates the CAM table. When it receives a frame, it reads the source MAC address and destination MAC address of the frame. It uses the source address to learn about the connected device. It uses the destination address to make forwarding decision devices.
It saves source addresses in the CAM table. The CAM table has three fields: MAC address, Port, and Aging. In the MAC address field, it saves the MAC address the frame has in the source field. In the port field, it saves the port's information on which the switch received the frame. In the aging field, it saves a timer.
It assigns a separate timer to each entry of the CAM table. This timer is used to age out old entries from the CAM table, allowing room to store new entries. This feature is known as the Aging. Once the CAM table is full, the switch has no place to store any new addresses. Aging resolves this issue by automatically removing the old entries from the CAM table. It keeps the MAC addresses of only those devices that are constantly sending the frames.
If any device is not sending the frames, once the timer is expired, it removes the MAC address of that device from the CAM table. In this way, only the devices that are constantly sending frames remain in the CAM table and the devices that are not sending any frames will eventually be removed from the table.
How switches learn MAC addresses
When a switch receives a frame, it finds the frame's source address in the CAM table. If it finds an entry for the source address, it resets the timer stored in the aging field. If it does not find an entry, it adds a new entry for the source address.
Let us take an example.
The following image shows a network. This network has three PCs connected through a switch. When we turn on this switch, the CAM table of this switch is empty.
Now suppose PC-A sends a frame to PC-B. Upon receiving this frame on the F0 port, the switch takes the following actions.
The switch reads the source and destination MAC addresses of the frame and finds them in the CAM table.
If the switch finds an entry for the destination MAC address, it forwards the frame from the port on which the destination address is available. If it does not find an entry for the destination MAC, it floods the frame from all ports except the port on which the frame arrived. Since the CAM table is empty, the switch sends this frame out of every port except port F0.
If the switch does not find an entry for the source MAC address, it creates a new entry for this MAC address. Since the CAM table is empty, the switch adds the source MAC address in the CAM table and sends out the frame from all the ports except port F0.
If PC-B replies, the switch takes the source MAC address from that frame and places it in the CAM table along with the interface's information that received the frame. Since the switch already knows PC-A's location, it doesn't flood the frame, as it did the first time. This time it sends the frame only from port F0.
The following image shows how the switch processes this frame.
The CAM table is not shareable
A switch does not share its CAM table with others. If multiple switches are used in the network, each of them will use its own CAM table to take the forwarding decision. The following image shows a network that uses two switches.
Viewing entries of the CAM table
To view the entries of the CAM table, you can use the "show mac-address-table" command in privileged-exec mode. This command displays the CAM table's entries. If you use this command just after starting the switch, it displays a blank CAM table. A switch adds a device MAC address in the CAM table when it receives a frame from that device on any of its ports.
Relocation of devices
If you change a device's location, the switch receives frames from that device on another port. If a switch finds an entry for the source MAC address with different port information, it assumes that the device has been relocated. In this situation, the switch updates the port information along with the timer. This feature makes the relocation of devices completely hassle-free.