This article briefly introduces the difference between layer 2 and layer 3.
There are two and three levels of communication. So what is the difference between the second and third floors? Generally speaking, the second layer realizes forwarding by checking the MAC table, and the third layer realizes forwarding by checking the routing table. Today, I will discuss in detail the difference between the second and third floors.
Let me talk about the second layer of communication. Layer 2 switches are devices at the data link layer. When the Layer 2 switch receives a data packet, it will forward it through the following steps.
1. It first needs to know where the data packet comes from, so it first reads the source MAC address of the packet header and knows which port the data packet comes from.
2. Then it needs to know where the data packet is going, so it needs to read the destination MAC address of the packet header.
3. Then look up the MAC table. If the MAC address and the corresponding port are in the table, the Layer 2 switch will copy the data packet to the corresponding port.
4. If the corresponding port cannot be found, the data packet will be broadcast to all ports.
5. When the destination device responds to the source device, it will copy the data packet to the corresponding port. At the same time, a learned correspondence will be added to the MAC table.

In this network topology, the communication between PC1 and PC2 belongs to Layer 2 communication. Let's capture the G0/0/3 of the switch LSW3 and take a look at the data packet.

The source and destination MAC addresses are 54:89:98:09:42:11, which is the MAC address of PC1, and the destination MAC address is 54:89:98:09:42:22, which is the MAC address of PC2. The switch searches the MAC address table to find the peer end and forwards the data packet.
So, what about Layer 3 communication? Assume that device A sends a data packet to device B. Perform an AND operation on the subnet masks of the two subnet masks to determine whether the two subnet masks are in the same network segment. If the destination IP addresses are in the same network segment, Layer 2 communication is used. If not, the interworking between A and B must be implemented. It sends the packet to the gateway and sends an ARP request to find the destination IP address. The Layer 3 switch searches the routing table. If the routing table contains the routing information, Layer 3 switch forwards the data according to the routing information. If not, it broadcasts an ARP request to find the route to the destination IP address.

The figure shown is a three-layer communication model. We capture the G0/0/1 of LSW8 and analyze its data packets.

It can be seen that the source MAC address and destination MAC address are no longer the MAC addresses of PC1 and PC2. It can be seen that the routing table is checked in the third layer, and the second layer switch sends ARP to the gateway. Please "where is the destination IP address", search the routing table and find it The corresponding routing information can be forwarded; if not, broadcast ARP to find the corresponding routing information.
Layer 2 communication is mostly used in small local area networks and has the advantages of mature technology, multi-port access, and low price. The third layer is suitable for routing between large-scale networks, with powerful functions and powerful routing functions. The best route can be selected to achieve load sharing.


