What are the differences between a Layer 3 switch and a router?
Nov 16, 2024
Leave a message
What are the differences between a Layer 3 switch and a router?
Many friends ask, what is the difference between a router and a Layer 3 switch? That's a great question, and today COBTEL's engineers are here to provide the most professional and detailed explanation.
Many friends ask, what is the difference between a router and a Layer 3 switch? That's a great question, and today COBTEL's engineers are here to provide the most professional and detailed explanation.
1. Working Principle of Ethernet Switches
When a switch receives data, it checks the destination MAC address and then forwards the data through the interface connected to the destination host. The switch can do this because it has a built-in MAC address table, which records the correspondence between all MAC addresses on the network and the switch's ports. When a data frame needs to be forwarded, the switch looks up the destination MAC address in the MAC address table to find the corresponding port, i.e., knowing which port on the switch the device with that MAC address is connected to, and then the switch forwards the data frame out of that port.

1. The switch establishes a mapping between the source MAC address in the received data frame and the switch's port, and writes it into the MAC address table.
2. The switch compares the destination MAC address in the data frame with the established MAC address table to decide which port to forward the data frame through.
3. If the destination MAC address in the data frame is not in the MAC address table, the data frame is forwarded to all ports. This process is called flooding (where the data frame is sent to all ports).
4. Broadcast and multicast frames are forwarded to all ports.
Example: A network is shown in Figure 1.

Figure 1 Switch Address Table
Table 1: Port/MAC Address Mapping Table

For example, if host pc1 sends a data frame to host pc7. After the data frame is sent to the switch, the switch first checks the MAC address table and finds that host pc7 is connected to the E0/24 interface, so it forwards the data frame out of the E0/24 interface.
1.1 Three Basic Functions of Ethernet Switches
Learning
The Ethernet switch learns the MAC address of each connected device and stores this address along with the corresponding port in the switch's buffer, creating a MAC address table.
Forwarding/Filtering
When the destination address of a data frame is in the MAC address table, it is forwarded to the port connected to the destination node rather than all ports (if the data frame is a broadcast/multicast frame, it is forwarded to all ports).
Eliminating Loops
When a switch includes a redundant loop, the Ethernet switch uses the Spanning Tree Protocol to avoid loop generation while allowing the existence of backup paths.
2. Comparison of Layer 2 and Layer 3 Switches
2.1 Layer 2 Switching Technology
Layer 2 switching technology is relatively mature. Layer 2 switches are data link layer devices that can recognize MAC address information in data packets, forward based on MAC addresses, and record these MAC addresses and corresponding ports in an internal address table.

The specific workflow is as follows:
(1) When the switch receives a data packet from a certain port, it first reads the source MAC address in the packet header to know which port the machine with the source MAC address is connected to.
(2) It then reads the destination MAC address in the packet header and looks up the corresponding port in the address table.
(3) If there is a corresponding port in the table for this destination MAC address, the data packet is directly copied to that port.
(4) If the corresponding port is not found in the table, the data packet is broadcast to all ports. When the destination machine responds to the source machine, the switch can learn which port corresponds to the destination MAC address, so the next time data is transmitted, it no longer needs to broadcast to all ports.
(2) It then reads the destination MAC address in the packet header and looks up the corresponding port in the address table.
(3) If there is a corresponding port in the table for this destination MAC address, the data packet is directly copied to that port.
(4) If the corresponding port is not found in the table, the data packet is broadcast to all ports. When the destination machine responds to the source machine, the switch can learn which port corresponds to the destination MAC address, so the next time data is transmitted, it no longer needs to broadcast to all ports.
By continuously cycling this process, the Layer 2 switch can learn all MAC address information on the network and thus establish and maintain its own address table.
2.2 From the working principle of Layer 2 switches, we can infer the following three points:
(1) Since the switch simultaneously exchanges data on most ports, it requires a very wide switching bus bandwidth. If a Layer 2 switch has N ports, each with a bandwidth of M, the switch's bus bandwidth must exceed N×M for the switch to achieve line-speed switching.
(2) Learning the MAC address of the machine connected to each port and writing it into the address table. The size of the address table (usually represented in two ways: one is BUFFER RAM, the other is the number of MAC table entries) affects the switch's access capacity.
(3) Most Layer 2 switches contain ASIC chips specifically designed for processing data packet forwarding, so the forwarding speed can be very fast. Since different manufacturers use different ASICs, this directly affects product performance.
2.3 Layer 3 Ethernet Switches
Layer 3 switching operates at the network layer of the OSI model. It uses the header information of IP packets in the third-layer protocol to mark subsequent data service flows. Subsequent packets of the same marked service flow are switched to the second-layer data link layer, thus opening a path between the source IP address and the destination IP address. This path passes through the second-layer link layer. With this path, Layer 3 switches do not need to unpack each received data packet to determine the route but can directly forward the data packet and switch the data flow.

For example
Suppose A wants to send data to B, knowing the destination IP. Then A uses the subnet mask to obtain the network address and determines whether the destination IP is in the same network segment as itself.
Suppose A wants to send data to B, knowing the destination IP. Then A uses the subnet mask to obtain the network address and determines whether the destination IP is in the same network segment as itself.
Device A using IP ------------ Layer 3 Switch ---------------- Device B using IP
If they are in the same network segment but A does not know the MAC address required for forwarding data, A sends an ARP request, and B returns its MAC address. A uses this MAC address to encapsulate the data packet and sends it to the switch. The switch activates the Layer 2 switching module, looks up the MAC address table, and forwards the data packet to the corresponding port.
If the destination IP address indicates that it is not in the same network segment, then A needs to communicate with B. If there is no corresponding MAC address entry in the flow cache, the first normal data packet is sent to a default gateway. This default gateway (the network's default route) is usually set in the operating system and corresponds to the third-layer routing module. Therefore, for data not in the same subnet, the first MAC address placed in the MAC table is the default gateway's MAC address. Then the third-layer routing module receives this data packet, queries the routing table to determine the route to B, constructs a new frame header with the default gateway's MAC address as the source MAC address and B's MAC address as the destination MAC address. Through a certain recognition trigger mechanism, the correspondence between A's and B's MAC addresses and the forwarding port is established and recorded in the flow cache table. Subsequent data from A to B is directly handled by the Layer 2 switching module. This is commonly referred to as "one-time routing, multiple exchanges."
2.4 It can be seen that the characteristics of Layer 3 switching are:
a. High-speed data forwarding is achieved through hardware integration.
b. This is not a simple superposition of a Layer 2 switch and a router. The Layer 3 routing module is directly superimposed on the high-speed backplane bus of the Layer 2 switch, breaking through the traditional router's interface rate limit, with a rate of up to tens of Gbit/s. Including backplane bandwidth, these are two important parameters of Layer 3 switch performance.
c. Simplified routing software makes the routing process simpler.
d. Most data forwarding, except for the necessary routing selection handled by the routing software, is handled by the Layer 2 module at high speed. The routing software is mostly highly efficient and optimized software processed, not a simple copy of the software in routers.
Therefore:
Layer 2 Switch: Based on MAC address
Layer 3 Switch: With VLAN functionality (virtual LANs), switching and routing, based on IP, i.e., network.
Layer 2 Switch: Based on MAC address
Layer 3 Switch: With VLAN functionality (virtual LANs), switching and routing, based on IP, i.e., network.
3. Differences Between Routers and Layer 3 Ethernet Switches
A router is a Layer 3 device, but a Layer 3 switch can work simultaneously at both Layer 3 and Layer 2.

In fact, there are significant differences between Layer 3 switches and routers:
3.1 Main functions are different.
Although both Layer 3 switches and routers have routing functions, they cannot be equated. Routers not only have routing functions but also provide switch ports and additional hardware firewall functions, aiming to make the device more versatile and practical.
Like Layer 2 switches, Layer 3 switches primarily handle data switching, but they also have some basic routing functions. Layer 3 switches combine data switching with routing capabilities, but their main function is still data switching; while routers only have the main function of routing forwarding.
3.2 The main applicable environments are different.
The routing function of Layer 3 switches is usually simple because they mainly face simple LAN connections. Their features are far less complex than routers. Their primary use in LANs is to provide fast data switching, which is essential for frequent data exchanges.
Routers, on the other hand, are also suitable for connections between LANs, but their routing function is more reflected in the interconnection between different types of networks, such as connections between LANs and WANs, connections between networks with different protocols, etc. Their advantage lies in selecting the best route, load distribution, link backup, and exchanging routing information with other networks. Additionally, routers have a very rich variety of interface types to connect with various types of networks, while Layer 3 switches generally only have the same type of LAN interfaces, which is very simple.
3.3 The technical implementation is different.
There is a significant difference in data packet switching operations between routers and Layer 3 switches.
Routers generally perform data packet switching based on network processors or multi-core routing engines.
Layer 3 switches perform data packet switching using specialized hardware. After the first data packet is sent to the control plane for routing lookup, the Layer 3 switch will generate a mapping table of MAC addresses and IP addresses for data plane lookup. When the same data flow passes again, it will look up this table to pass through instead of sending it to the control plane for routing lookup again (i.e., "one-time routing, multiple exchanges").
Routers generally perform data packet switching based on network processors or multi-core routing engines.
Layer 3 switches perform data packet switching using specialized hardware. After the first data packet is sent to the control plane for routing lookup, the Layer 3 switch will generate a mapping table of MAC addresses and IP addresses for data plane lookup. When the same data flow passes again, it will look up this table to pass through instead of sending it to the control plane for routing lookup again (i.e., "one-time routing, multiple exchanges").
This improves the efficiency of data packet forwarding. The routing lookup of Layer 3 switches is for data flows, and it easily uses caching technology and ASIC technology to implement, thus greatly saving costs and achieving fast forwarding.
Routers, however, use a more complex longest match method for forwarding, which is implemented using expensive network processors or multi-core processors, and the number of routing tables is huge, resulting in considerable costs.
Routers, however, use a more complex longest match method for forwarding, which is implemented using expensive network processors or multi-core processors, and the number of routing tables is huge, resulting in considerable costs.
4. Summary
Layer 2 switches are ideal for small LANs. In small LANs, broadcast packets have little impact, and the fast switching function, multiple access ports, and low price of Layer 2 switches provide a very complete solution for small network users.
The most important function of Layer 3 switches is to speed up the rapid forwarding of data within large LANs, and adding routing functions also serves this purpose. If a large network is divided into small LANs according to factors such as departments and regions, this will lead to a large amount of inter-network access. Simply using Layer 2 switches cannot achieve inter-network access;
If only routers are used, due to the limited number of interfaces and slow routing forwarding speed, it will limit the speed and scale of the network. Using Layer 3 switches with routing functions and fast forwarding becomes the preferred choice.






