Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 56

Hub, Switch, and Router: Understanding Traffic Forwarding

Recommended resources

Hello! Welcome to the final lesson of this module.

In our last session, we took a bird's-eye view of network design, analyzing architectures like the three-tier and spine-leaf models. We discussed how these blueprints are designed for redundancy and specific traffic patterns. Now, it's time to zoom in and get acquainted with the fundamental building blocks that bring those blueprints to life.

Today's learning outcome is to differentiate between a hub, switch, and router by observing their traffic forwarding behavior in a simulated network. This is a cornerstone concept for any networking or data center role. Understanding not just what these devices do, but how and why they do it, is exactly the kind of knowledge that interviewers look for.

We'll stick to your preferred practical approach. First, we'll observe these devices in action to see their behavior. Then, we'll dissect the underlying mechanics to understand why they behave differently.

1. The Core Trio: Hub, Switch, and Router

At the heart of any local area network (LAN), you'll find three main devices responsible for moving data: hubs, switches, and routers. While they might look like simple boxes with ports, their internal logic is vastly different.

Hub, Switch, & Router Explained
This image provides a high-level visual summary of the roles of hubs, switches, and routers. It shows how they handle data, their corresponding OSI model layer, and their impact on the network.

Here's a quick rundown of their primary roles:

  • A Hub is a simple, non-intelligent device that connects multiple computers together into a single network segment. It operates at Layer 1 (the Physical Layer) of the OSI model.
  • A Switch is an intelligent device that connects devices within a single network. It learns which devices are connected to which of its ports. It operates at Layer 2 (the Data Link Layer).
  • A Router is an even more intelligent device that connects two or more different networks. It directs traffic between them. It operates at Layer 3 (the Network Layer).

Now, let's see what this means in practice.


2. Observing the Behavior: The "How"

The best way to understand the difference is to watch how they handle a simple data packet. We'll use a simulation to visualize this.

Use of Hub,Switch,Router in network using Cisco Packet Tracer

This video by Beena Ballal uses Cisco Packet Tracer to demonstrate the behavior of all three devices. As you watch, pay close attention to the envelope icon, which represents a data packet, and notice where it goes in each scenario.

Please watch the video from the beginning until 14:04. It is divided into three parts: Hub (00:00 - 03:00): When a packet is sent from one PC to another, notice how the hub sends a copy to every connected device. Switch (03:00 - 05:52): Observe how the switch forwards the packet only to the intended destination PC. Router (05:52 - 14:04): See how the router's job is to pass packets between two separate networks (represented by different colors and IP address ranges).

This simulation clearly illustrates the core functional differences:

  • The hub acts like a megaphone, shouting the message to everyone in the room. This is inefficient and creates unnecessary network traffic.
  • The switch acts like a postal worker, looking at the specific address (the MAC address) and delivering the letter only to the correct mailbox.
  • The router acts like an international mail sorting center, looking at the destination country (the IP network) and forwarding the package into a different postal system.

3. Deconstructing the "Why": Inside the Devices

You've seen how they behave. Now for the crucial part: why they behave that way. This is about moving from observation to understanding the internal logic.

Layer 2: The Intelligence of a Switch

Hubs are simple Layer 1 devices; they only deal with electrical signals. Switches, operating at Layer 2, are more sophisticated. Their intelligence comes from their ability to use MAC addresses.

For a concise summary of the differences, this table is very helpful.

Difference between HUB and SWITCH

The 'CN Lab Manual' from SOUNotes provides a clear and direct comparison between a hub and a switch. This table is excellent for quick review.

Review the comparison table under 'Exercise: 2.' focusing on the 'Difference between HUB and SWITCH?' section.

The key to a switch's efficiency is its MAC address table. A switch learns the unique MAC address of every device connected to each of its ports and builds a table, much like a phone's contact list.

Basics of Cisco Packet Tracer Tutorial | What is Switch | Hub vs Switch

This video from Cisco Config provides a practical look at the MAC address table on a switch.

Watch the segment from 06:44 to 07:51. The demonstrator uses the show mac address-table command to reveal how the switch has mapped MAC addresses to its physical ports. This table is the 'brain' that allows the switch to forward packets intelligently.

This process of building and using the table follows a simple logic known as Learn, Flood, and Forward.

Let's Build a CNI Plugin!

The article 'Let's Build a CNI Plugin!' provides an excellent explanation of this process. It details exactly how a switch goes from knowing nothing to efficiently directing traffic.

Read the section titled 'Switching (L2 Domain)'. It breaks down the three-step logic that governs a switch's operation.

In short:

  1. Learn: When a frame arrives, the switch inspects the source MAC address and logs which port it came from.
  2. Flood: If the switch doesn't know where the destination MAC address is, it forwards the frame out of all ports (except the one it came in on), just like a hub. This is a one-time action.
  3. Forward: Once the destination device replies, the switch learns its location. From then on, any frames sent to that device are forwarded only to the correct port.

Layer 3: The Role of the Router

Switches are fantastic for communication within a single network (like 192.168.1.0/24). But what if a device at 192.168.1.50 wants to talk to a server on the internet at 8.8.8.8? A switch can't do this because 8.8.8.8 is not on the local network. This is the router's job.

Routers use Layer 3 IP addresses to make decisions. They maintain a routing table—a map of different networks—to determine the best path to forward a packet.

Let's Build a CNI Plugin!

Let's return to the 'Let's Build a CNI Plugin!' article for a clear breakdown of how routers work.

Read two sections from this article: OSI (L2, L3 and L4): Focus on the subsections for 'Layer 2' and 'Layer 3'. The example diagram showing a packet moving between 'Network 1' and 'Network 2' is crucial. Notice how the L2 MAC addresses change at each hop, but the L3 IP addresses remain the same end-to-end. Routing (L3 Domain): This section explains the routing table, which is the mechanism a router uses to make forwarding decisions.

The key takeaway is that switching is for local delivery, while routing is for long-distance delivery between networks.


4. Practice Time: Building a Virtual Network in Linux

Watching simulations is useful, but true understanding comes from doing. We're now going to use your Linux command-line skills to build a virtual network from scratch, demonstrating the principles of switching and routing. This exercise directly applies concepts you'll use in data center and system administration roles.

The following steps are based on the excellent practical guide in the "Let's Build a CNI Plugin!" article you've been reading. I'll guide you through creating a virtual switch and a virtual router using standard Linux tools. Open your Linux terminal and follow along.

Part 1: The Virtual Switch (Linux Bridge)

A Linux bridge behaves exactly like a virtual Layer 2 switch.

  1. Create the bridge:

    
    
    
    # Create the bridge device named br0
    sudo ip link add br0 type bridge
    
    
    
    
    # Bring the bridge online
    sudo ip link set br0 up
    
  2. Create virtual "PCs" (Network Namespaces):

    
    
    
    # Create two isolated network environments
    sudo ip netns add container0
    sudo ip netns add container1
    
  3. Create virtual "cables" (veth pairs) and "plug them in":

    
    
    
    # Create a patch cable for container0
    sudo ip link add c0-veth type veth peer name c0-br
    
    
    
    # Create a patch cable for container1
    sudo ip link add c1-veth type veth peer name c1-br
    
    
    
    
    # Plug one end of each cable into its container
    sudo ip link set c0-veth netns container0
    sudo ip link set c1-veth netns container1
    
    
    
    
    # Plug the other end of each cable into the bridge
    sudo ip link set c0-br master br0
    
  4. Configure the virtual PCs and bring interfaces up:

    
    
    
    # Assign an IP to the interface inside container0
    sudo ip netns exec container0 ip addr add 10.0.1.50/24 dev c0-veth
    
    
    
    # Bring up the interface inside container0
    sudo ip netns exec container0 ip link set dev c0-veth up
    
    
    
    
    # Assign an IP to the interface inside container1
    sudo ip netns exec container1 ip addr add 10.0.1.59/24 dev c1-veth
    
    
    
    # Bring up the interface inside container1
    sudo ip netns exec container1 ip link set dev c1-veth up
    
    
    
    
    # Bring up the bridge-facing ends of the cables
    sudo ip link set c0-br up
    sudo ip link set c1-br up
    
  5. Test connectivity:

    
    
    
    # From container1, ping container0
    sudo ip netns exec container1 ping -c 3 10.0.1.50
    

    You should see a successful ping. You have just simulated a switch connecting two PCs on the same LAN! The br0 device performed the "Learn" and "Forward" operations automatically.

Part 2: The Virtual Router (L3 Gateway)

Now, let's give these containers a path to the internet. To do this, we must turn our L2 bridge into an L3 gateway (a router).

  1. Assign an IP address to the bridge: This makes it a destination on the network.

    
    
    
    # Assign the gateway IP to the bridge
    sudo ip addr add 10.0.1.1/24 dev br0
    
  2. Add a default route inside the containers: Tell them where to send traffic destined for other networks.

    
    
    
    # Set the default gateway for container0
    sudo ip netns exec container0 ip route add default via 10.0.1.1
    
    
    
    # Set the default gateway for container1
    sudo ip netns exec container1 ip route add default via 10.0.1.1
    
  3. Enable IP Forwarding on the host: Tell the Linux kernel it's allowed to act as a router.

    
    
    
    # Enable IPv4 forwarding
    sudo sysctl -w net.ipv4.ip_forward=1
    
  4. Configure Network Address Translation (NAT): Your host machine's main network interface is eth0 or enp0s3 or similar. Find it using ip a. Then, use that interface name in the command below to "masquerade" the traffic from your containers so the internet knows how to reply. Replace YOUR_INTERFACE with your actual interface name.

    
    
    
    # Masquerade traffic from the container subnet leaving your main interface
    sudo iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o YOUR_INTERFACE -j MASQUERADE
    
  5. Test internet connectivity:

    
    
    
    # From container0, ping a public DNS server
    sudo ip netns exec container0 ping -c 3 8.8.8.8
    

    Success! You've now configured your br0 device to act as a router, forwarding traffic from the private 10.0.1.0/24 network to the public internet.


Conclusion

Today, you've gone from observing network devices to building them virtually. This practical understanding is far more valuable than memorizing definitions.

Key Takeaways:

  • Hubs (Layer 1): Non-intelligent devices that broadcast all traffic, creating network congestion.
  • Switches (Layer 2): Intelligent devices that use a MAC address table to forward frames only to the specific destination port within a single network. They operate via a "Learn, Flood, Forward" logic.
  • Routers (Layer 3): Devices that connect different networks. They use a routing table and IP addresses to forward packets between networks.
  • The Linux kernel itself contains powerful networking tools (ip, iptables) that allow you to create virtual switches (bridges) and routers, providing a hands-on way to master these core concepts.

In our next module, we'll take this knowledge and apply it to physical hardware. The first lesson will cover how to physically install a rack-mount switch or router and establish initial console access, setting you up for hands-on configuration.