How Switch learns the MAC addresses Explained

This tutorial explains how the switch learns the MAC addresses. Learn what the CAM table is and how the switch dynamically stores the MAC addresses of the connected devices in it.

The CAM table

To make accurate forwarding and filtering decisions, the switch learns and stores the MAC addresses of all connected devices. The switch stores the learned MAC addresses in a table that is known as the CAM table.

In old days of switching, when the bridges were used to be in the networking, a special form of high-speed memory, known as the content addressable memory or CAM memory, was used to facilitate the switching function.

To refer the table that stores in the CAM memory, the term CAM table was used. Gradually, the bridges were replaced by the switches. The switches use RAM to store this table, but the trend to use the term CAM table remains to continue.

Nowadays, the CAM table is also known as the MAC forward table, or MAC filter table, or MAC address table, or switching table, or bridging table. No matter what name you use to refer this table, it is used to store the MAC addresses of the connected devices.

In the below section, we will understand how the switch learns and stores MAC addresses in the CAM table.

Static V/s Dynamic configuration

A switch can learn MAC address in two ways; statically or dynamically. In the static option, we have to add the MAC addresses in the CAM table manually. In the dynamic option, the switch learns and adds the MAC addresses in the CAM table automatically. The switch stores the CAM table in the RAM. The RAM is a temporary memory. All contents stored in the RAM are wiped out automatically when we turn off the switch.

To preserve the changes which we make in the running-configuration, the switch allows us to copy the running-configuration into the NVRAM. The NVRAM is a permanent memory. Any contents stored in the NVRAM remains available after the restart. At startup, the switch copies the running-configuration saved in the NVRAM to the RAM.

When we copy the running-configuration into the NVRAM, any MAC addresses which we add statically are also saved in the NVRAM. These MAC addresses are automatically copied back into the CAM table at the startup when we restart the switch.

Adding MAC addresses in the CAM table is a very tedious and boring task. For example, if you have 1000 devices connected through the 5 switches, then you have to make 5000 (1000 * 5) entries. Furthermore, if you relocate any device, then you have to update all related entries manually.

Thanks to the dynamic learning feature, you do not need to type any MAC address. All five switches not only learn all MAC addresses automatically but also make their correct entries in the CAM table.

Learning MAC Addresses dynamically

The MAC address learning process is relatively simple. When processing an incoming frame, the switch checks the source MAC address of the frame in the CAM table. If an entry for the source address is not available in the CAM table, it creates a new entry for that source. If an entry is already available, the switch updates the existing entry.

Let’s understand this process in detail with examples.

The following image shows a simple network. In this network, three PCs are connected through a switch. When we turn on this switch, the CAM table of this switch is empty.

cam table without entry

Now suppose, the PC-A sends a frame to the PC-B. Upon receiving this frame on the F0 interface, the switch takes the following actions.

The switch reads both the source and destination MAC addresses of the frame and finds them in the CAM table.

Processing frame based on the destination

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 the source port (F0).

Adding or updating the source MAC address in the CAM table

If the switch does not find an entry for the source MAC address, it creates a new entry for this MAC address. An entry contains three pieces of information; the source MAC address, the port or interface on which the frame arrived, and the time when the frame arrived.

If the switch finds an entry for the source MAC address, it updates that entry and resets the timer of that specific entry. The switch 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 manner, 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.

There is one more possible situation that you need to understand. If the switch finds an entry for the source MAC address with the different port information, it assumes that the device has been relocated. In this situation, the switch also updates the port information along with the timer. This feature makes the relocation of devices completely hassle-free.

Let’s back with our example. 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 the F0 port.

The following image shows how the switch processes the frame sent by the PC-A.

cam table after first entry

If the PC-B responses back to PC-A and sends a frame back, the switch takes the source MAC address from that frame and place that MAC address in the CAM table as well, associating this address with the interface that received the frame. Since the switch already knows where the PC-A is located, so it doesn’t flood the frame as it did the first time. This time it only sends the frame out from the F0 interface.

The following image shows how the switch processes this frame.

switch address learning process

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.

switch address learning example with 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 from the privileged exec mode. This command displays the real-time entries of the CAM table. If you use this command just after turning on the switch, it displays a blank CAM table. The switch adds a device MAC address in the CAM table only when it receives a frame from that device on any of its port.

The following image shows an example of the "show mac-address-table" command.

example of how switch learn mac address

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social platform.

Advertisements

ComputerNetworkingNotes CCNA Study Guide How Switch learns the MAC addresses Explained