Network Switches

Vishnu MohanVishnu Mohan
6 min read

In this article we will deep dive into network switches, asking questions ranging from what are they? why and when to use them? And we will also look at where do they fall in the OSI layer.

What is a switch?

A network switch is a device that connects multiple devices (computers, servers, printers, etc.) inside a Local Area Network (LAN).

It is designed to forward packets to a specific port within the switch and this port if connected with a device will be eligible for receiving the packets.

Normally it forwards data only to the device (port) that needs it, instead of broadcasting to all (like a hub would). But there are nuances to this when it comes to the actual working of switches which we will discuss later.

Now we’ve seen the definition of a switch, lets look at where it is mostly used and why? In computer networks there are many diff types of networks like WAN, LAN, PAN etc, among these network switches are primarily used within Local Area Networks (LANs).

A LAN usually connects many devices (PCs, printers, servers etc) in a relatively small area (home, office etc). And before the switch, we used a device known as hub for connecting all these devices within the LAN, and hub is designed to broadcast the packets to all the connected devices no matter who asked for it. This meant lots of collisions, wasted bandwidth, and poor performance.

A switch solves this by using the MAC address to resolve the destination of a packet and forward it to only the correct destination. . Because each device has a dedicated point-to-point link to the switch, there’s no shared medium and therefore no competition. Each device and its switch port can transmit and receive simultaneously using separate wire pairs in the Ethernet cable, enabling full-duplex communication without collisions. This has many other benefits, like less congestion in network, scalability and performance.

So one question I had while learning about switches is “Why they are prominent in LAN and not WAN? Doesn’t both LAN and WAN no matter their size require some form of data packet forwarding similar to that of switches? Who does the packet forwarding in WAN?“.

This is because how networks are structured, LAN is concerned with connecting many end devices in a limited area with faster device-to-device communication and a switch is suffice for this job.

And a WAN is concerned with connecting different LANs across cities/countries involves long-distance transmission, routing, addressing, traffic control and the switch is simply not meant for such heavy lifting, there are routers and other ISP specific devices for this task.

So in theory both WAN and LAN need some form of packet forwarding but switch does not cover for all the rest of features needed for WAN.

MAC Learning

So we look at what are switches and why they are needed, now lets look at how they are implemented.

In a switch we learned that the packets are sent from a device which is connected to a port to another device which is connected to another port in the switch. Now how does the switch identify the destination and source devices in the network, for this we’ve MAC addresses associated with each devices.

MAC address is a hard corded address assigned to each devices NIC by manufactures, this is a unique address which can identify a device in a network, now how does the switch keep track of this address.

How a Switch Learns MAC Addresses

By default the switch is not expected to know the MAC addresses of all devices, it uses a process known as MAC Learning to identify MAC of each devices

  1. Initial Frame Transmission
    When a device A sends its first packet, the switch looks at the source MAC address in that frame.

    • The switch adds "Source MAC = A’s MAC, Port = X" in its MAC Address Table (aka CAM Table).

    • This is how the switch learns where a device is connected.

  2. Forwarding Decision

    • The switch now checks the destination MAC address in the frame.

    • If the destination MAC is already in the table, the switch forwards the frame only to the port where that MAC is connected (unicast forwarding).

    • If the destination MAC is not in the table, the switch doesn’t know where to send it, so it floods the frame out to all ports (except the one it came from).

  3. Response & Learning
    The NIC of all the receiving devices checks the destination MAC address inside the Ethernet frame and If it matches its own MAC the NIC accepts the frame and passes it up to the operating system / higher layers. If not matching the NIC drops the frame silently.

    When the destination device replies, the switch sees the source MAC of that reply and records it in the table along with the port.

    • Now both source and destination devices are in the MAC table.

    • From then on, frames between these two devices are sent directly, port-to-port, without flooding.

It is important to note that switch does not send a broadcast request to learn MAC addresses on its own.
Instead, it learns passively from the source MAC of any frame it receives. The flooding only happens when the destination MAC is unknown and the flooded packet is just the original packet no separate packets are send for learning MAC.

The MAC table is built dynamically as mentioned above and updated continuously. Entries may age out after a timeout (typically 300 seconds) if no frames are seen from that MAC.


Layer 2 and Layer 3 in OSI

When learning about any networking concept it is good to look at them with an OSI lens, it will help you understand the concept of OSI model and give you the ability to think about roles of diff devices or layer within in a network in a structured way which will come handy when working with these layers!!

In the OSI model the network switching appear to take layer 2 or data link layer which is the closest to layer 1 or physical layer as expected. Going into the details of OSI model is out of scope for this article but it is important to understand layer 2 to better understand switching!!

For two devices to communicate across a network, they need to have both an IP address, associated with Layer 3 (the IP layer), and a MAC address, associated with Layer 2 (the Ethernet layer). Here is a good read to understand the modern nuances of switches and other layer 2, layer 3 devices


With that we’ve looked at the basics of Network Switches and where they fall in the OSI model, I will be trying to further improve this article in the near future but before that I need to work on a separate article to look into the OSI model which will provide a better foundation to understanding switches and routers, nevertheless this article will still be good enough to understand the basics of Network Switches and I hope you like it!!

Please share your thought and feedbacks in the comments if any, I would love to hear the stories you had while learning these topics!!

0
Subscribe to my newsletter

Read articles from Vishnu Mohan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishnu Mohan
Vishnu Mohan

Software Engineer | DevOps & CloudNative