Switches

A switch is a network device with multiple interfaces or ports. The ports can connect computers and other devices, and any devices that are connected to the switch can communicate with each other. Switches aimed at the home network market segment often have 5-16 ports, but there are a lot of varieties available with different amounts of ports.

Network Switch

In fact, most people already have a switch at home, often without even knowing it. The LAN ports of your home router where you can connect your internal computers, printers etcetera are in fact built-in switch ports that act exactly like the ports in a standalone switch.

Whereas a hub just copies electrical signals between ports, a switch works intelligently with MAC addresses which have been mentioned previously to make sure that traffic that is sent between devices end up at the right place.

What a switch does is that it constantly monitors the traffic which is entering the switch from connected devices. It then learns about where the different MAC addresses of those devices are connected. It does this by looking at the traffic that arrives from computers to read the source MAC address of the traffic.

In network communication, data is packaged into different types of “envelopes” at different stages of the communication.

  • When data from an application is going to be transmitted by a computer it is put inside a TCP or UDP Segment with port information.
  • The Segment is put inside an IP Packet that contains the IP addresses.
  • The IP packet is wrapped inside a Frame with MAC addresses.

Since a switch is working with MAC addresses it is looking at the information in the Frame, which contains the MAC address information. This is also explained a bit further in one of the advanced sections. The frame carries an IP packet inside, which in turn carries a segment, which in turn carries the actual data that is transmitted.

If a frame arrives on port 1 of the switch, and the frame is coming from Source MAC address 00:11:22:33:44:55 then the switch will automatically learn that a device with MAC address 00:11:22:33:44:55 is connected on port 1. The switch will store this information in a MAC address table that it keeps in memory.

MAC address learning in a Switch

When the switch has seen at least one frame from each connected device it will know exactly which MAC addresses that are connected to what ports, and it will then also be able to forward traffic only to the correct destination ports.

The way a switch forwards traffic is based on always trying to send traffic only to the correct destination port. Whenever traffic enters the switch the switch will read what destination MAC address the traffic is being sent to, and then it will compare the destination MAC address to its own table of known destinations to find out if it knows where the destination is located.

If it can match the destination to a port in the table then it will forward the traffic only to that port.

MAC address forwarding in a Switch

The most common exception is broadcast traffic. A broadcast is sent by computers when they want to send a message to all other devices on the same LAN. As mentioned earlier a computer could be looking for a DHCP server and uses a broadcast DHCP request to find out if there are any connected DHCP servers.

For broadcasts to work as intended the switch must handle broadcasts as a special case, and must send broadcasts to all other connected ports. It doesn’t act quite like a hub, however, because the hub is Half Duplex. The switch, which is Full Duplex, can still handle other traffic at the same time as it is sending a broadcast, so there is no need for all other computers to be silent while the broadcast is being sent.

Broadcast messages are sent to the destination MAC address FF:FF:FF:FF:FF:FF. If a switch receives a message that is sent to that destination MAC address then the switch knows it is a broadcast and will forward the message to every other connected port.

Broadcast Forwarding in a Switch

There is also another common scenario where switches send out traffic to all other ports, and that is when the switch simply does not yet know where the destination address is connected.

Let’s say a computer sends a message to another computer via a switch, but the switch hasn’t learned where the destination MAC address is located. This means that the switch cannot know where the second computer is connected. Then the switch simply treats the traffic as broadcast traffic and sends it out all other ports.

Switches forward unicasts as broadcasts for unknown MAC addresses

But the switch is also smart. Once the second computer replies back, then the switch can read the source MAC address in the reply and will learn which port the MAC address is connected to.

MAC address learning in a Switch

So as the computers continue to communicate with each other the switch will remember where the MAC addresses of those computers are connected. The rest of the communication will be forwarded by the switch only to the correct switch ports where the intended recipients are connected, without bothering any other computers on the LAN.

Previous part:
Broadcasts

Next part:
ARP - Associating IP with MAC addresses