Illustration | This is the Internet

You are a computer and your name is A

Once upon a time, you were not connected to any other computers, and you were all alone.

Until one day, you want to establish communication with another computer B, so you each open a network port and connect them with a network cable .

How can we "communicate" by connecting them with a network cable? I can tell you about IO, interrupts, and buffers, but this is not what you should be concerned about when studying the network.

If you are confused, either study how the operating system handles network IO, or study how the packet is converted into an electrical signal by the network card and sent out, or just treat it as a villain in the computer shooting . ~

Anyway, you are connected and can communicate.

level one

One day, a new partner C joined, but you smart people quickly discovered that each person can open two network ports and use a total of three network cables to connect to each other.

As more and more people join, you will find that there are too many network ports on your body, and the network cables are densely packed and confusing. (In fact, a computer cannot open so many network ports, so this kind of connection is only theoretically possible, so I use a red dotted line to indicate the connection that cannot be connected. It is so rigorous haha~)

So you invented an intermediate device. You plugged all the network cables into this device, and this device does the forwarding, and you can communicate with each other. It is essentially the same as before, except that the number of network ports and network cables has been reduced. , no longer so confusing.

You name it a hub , and it just forwards electrical signals to all outlets (broadcasts) without any brain , without doing any processing. You think it has no IQ, so you attribute it to the physical layer .

 

Since it is forwarded to all exits, how do the four BCDE machines know whether the data packet is sent to them?

First, you need to give names to all the devices connected to the switch. It used to be called ABCD, but now you need a more professional, globally unique name as an identifier. You call this higher-end name  the MAC address .

Your MAC address is aa-aa-aa-aa-aa-aa, your partner b's MAC address is bb-bb-bb-bb-bb-bb, and so on, just don't repeat it.

In this way, when A sends a data packet to B, it only needs to splice data with such a structure in the header.

After receiving the data packet, B determines based on the target MAC address information in the header that the data packet is indeed sent to itself, so it accepts it .

After receiving the data packet, other CDEs judge that the data packet is not sent to them based on the target MAC address information in the header, so they discard it .

Although the hub makes the entire layout much cleaner, the messages I originally only wanted to send to computer B now have to be sent to all computers connected to the hub, which is neither safe nor saves network resources.

Second floor

It would be nice if the hub was made smarter and only sent to the computer pointed to by the target MAC address .

Although it is only slightly different from a hub, it seems to be intelligent. You call this thing a switch . It is precisely because of this little bit of intelligence that you put it on another level, the data link layer .

 

As shown in the picture above, this is how you design it.

The switch maintains a  MAC address table internally , recording which port each device with a MAC address is connected to.

MAC address port
bb-bb-bb-bb-bb-bb 1
cc-cc-cc-cc-cc-cc 3
oh-oh-oh-oh-oh-oh 4
dd-dd-dd-dd-dd-dd 5

If you still want to send a data packet to B, construct the following data structure and send it out from the network port.

When arriving at the switch, the switch internally uses the MAC address table maintained by itself and finds that the MAC address bb-bb-bb-bb-bb-bb of target machine B is mapped to port 1 , so the data is sent to port B from port 1. Done~

You give this small-scale network formed by this transmission method called Ethernet .

Of course, at the beginning, the MAC address table was empty. How was it gradually established?

If the MAC address table is empty, you send the following data to B

Since this packet enters the switch from port 4, the switch can record the first piece of data in the MAC address table:

MAC:aa-aa-aa-aa-aa-aa-aa
PORT:4

The switch sees that the target MAC address (bb-bb-bb-bb-bb-bb) has no mapping relationship in the address table, so it sends this packet to all ports , that is, to all machines.

After that, only machine B received the packet that was indeed sent to itself, so it responded . The response data entered the switch from port 1, so the switch updated the second piece of data in the address table:

MAC: bb-bb-bb-bb-bb-bb
Port: 1

The process is as follows

After the machines in the network continue to communicate, the switch finally completes the establishment of the MAC address table~

As the number of machines increases, the number of switch ports is not enough, but if you are smart, you will find that as long as you connect multiple switches, this problem can be easily solved~

You don't need to design anything extra at all, you just need to follow the previous design and rules, and follow the above-mentioned wiring method to complete the interconnection of all computers, so this rule of switch design is really clever. Think about why (for example, A wants to send data to F).

But you should note that the red line above is not a record in the MAC address table, but all the mappings between the four EFGH machines and the port (port 6) are recorded in the table.

Eventually, the two switches will record the mapping records of all machines A ~ H respectively .

switch on the left

MAC address port
bb-bb-bb-bb-bb-bb 1
cc-cc-cc-cc-cc-cc 3
oh-oh-oh-oh-oh-oh 4
dd-dd-dd-dd-dd-dd 5
so-so-so-so-so-so 6
ff-ff-ff-ff-ff-ff 6
gg-gg-gg-gg-gg-gg 6
hh-hh-hh-hh-hh-hh 6

switch on the right

MAC address port
bb-bb-bb-bb-bb-bb 1
cc-cc-cc-cc-cc-cc 1
oh-oh-oh-oh-oh-oh 1
dd-dd-dd-dd-dd-dd 1
so-so-so-so-so-so 2
ff-ff-ff-ff-ff-ff 3
gg-gg-gg-gg-gg-gg 4
hh-hh-hh-hh-hh-hh 6

This is fine when there are only 8 computers, or even when there are only a few hundred computers, so this switch design has been sufficient for a while.

But unfortunately, people are greedy animals. Soon, the number of computers increased to thousands, tens of thousands, and hundreds of thousands.

the third floor

The switch can no longer record such a large mapping relationship.

At this time, you used your brain and discovered that the root of the problem is that there are countless devices connected to the red network cable that are connected out, making the address table larger and larger.

Then can I connect that red network cable to a new device , which has its own independent MAC address just like a computer, and can also help me forward the data packet ?

This device is a router. Its function is to be an independent device with a MAC address, and it can help me forward the data packet once . You set it at the network layer.

Note that each port of the router has an independent MAC address

Okay, now in the MAC address table of the switch, you only need to add one more mapping relationship between the MAC address ABAB and its port, and you can successfully transfer the data packet to the router. This is done.

So how to do it, send all the data packets sent to C and D, even the data packets sent to DEFGH.... to the router first?

It is not difficult to think of an idea. If the MAC addresses of computers C and D have a common prefix, such as

C's MAC address: FFFF-FFFF-CCCC
D's MAC address: FFFF-FFFF-DDDD

Then we can say, the target MAC address will be  FFFF-FFFF-? The ones at the beginning are all sent to the router first.

Is this feasible? the answer is negative.

Let’s start with the structure of the MAC address in reality. The MAC address is also called a physical address and a hardware address. It is 48 bits in length. It is generally expressed like this

00-16-EA-AE-3C-40

It is an EPROM (a flash memory chip that can usually be erased by a program) that is burned into the network card when it is produced by the network equipment manufacturer. The first 24 digits (00-16-EA) represent the network hardware manufacturer's number , and the last 24 digits (AE-3C-40) are allocated by the manufacturer itself, generally indicating the serial number. As long as you do not change your MAC address, the MAC address is unique in the world. Figuratively speaking, the MAC address is like the ID number on the ID card, which is unique.

What if you want to express the target MAC address as FFFF-FFFF- as above  ? At the beginning , it is sent out from the router to a certain group of devices (I will mention this later is actually the concept of subnet), then you need to ask all the devices in a certain subnet to buy equipment manufactured by one manufacturer, or you need to ask the manufacturer When burning MAC addresses on production network equipment, determine the MAC address in advance according to your planned subnet structure, and the structure of this network cannot be easily changed in the future.

This is obviously unrealistic.

So you invented a new address and gave each machine a 32-bit number, such as:

11000000101010000000000000000001

You feel it's a little unclear, so you divide it into four parts and connect them with dots in the middle.

11000000.10101000.00000000.00000001

You still think it's not clear, so convert it to base 10.

192.168.0.1

Finally you gave the address a catchy name, the IP address . Now every computer has its own MAC address and its own IP address. However, the IP address is at the software level and can be modified at any time. The MAC address generally cannot be modified.

Such an IP address that can be modified at any time can be adjusted according to your planned network topology.

As shown in the picture above, if I want to send a data packet to one of the ABCD devices, no matter which one, I can describe it like this, "Send all IP addresses starting with 192.168.0 to the router, and then forward them. , leave it to it!" , be clever.

After it is handed over to the router, how does the router accurately forward the data packet to the designated device?

Don't worry, let's take our time.

We first give each device in the above networking method its own IP address.

Now when transmitting between two devices, in addition to adding the header of the data link layer, another header of the network layer must be added.

If A sends data to B, since they are directly connected to the switch, A can directly send the following data packets. In fact, the network layer does not play a role.

But if A sends data to C, A needs to transfer it to the router first, and then the router transfers it to C. Since the lowest layer of transmission still relies on Ethernet, the data packet is divided into two segments.

A ~ The package of the router section is as follows:

The packets from the router to C are as follows:

Okay, I believe careful readers will have many questions about the two situations mentioned above (A->B, A->C). Let’s expand on them one by one.

When A sends a data packet to C, how does it know whether it needs to be forwarded through the router?

Answer: subnet

If the source IP and destination IP are in the same subnet, send the packet directly through the switch.

If the source IP and destination IP are not in the same subnet, it is left to the router to handle.

Okay, now we just need to figure out what it means to be in a subnet.

  • 192.168.0.1 and 192.168.0.2 are in the same subnet

  • 192.168.0.1 and 192.168.1.1 are in different subnets

These two are artificially specified by us, that is, we want to express that for 192.168.0.1:

If it starts with 192.168.0.xxx, it is considered to be in the same subnet, otherwise it is in a different subnet.

So for a computer, how to express this meaning? So people invented the concept of subnet mask

If the subnet mask of a certain machine is set to 255.255.255.0

This means that the source IP and destination IP are ANDed with this subnet mask respectively . If they are equal, they are in the same subnet. If they are not equal, they are in different subnets . It's that simple.

for example

  • Computer A : 192.168.0.1 & 255.255.255.0 = 192.168.0.0

  • Computer B : 192.168.0.2 & 255.255.255.0 = 192.168.0.0

  • C computer : 192.168.1.1 & 255.255.255.0 = 192.168.1.0

  • D computer : 192.168.1.2 & 255.255.255.0 = 192.168.1.0

Then A and B are in the same subnet, C and D are in the same subnet, but A and C are not in the same subnet, and D is not in the same subnet, and so on.

So if A sends a message to C, and the IP addresses of A and C are not equal to the subnet mask configured on A's machine, then A thinks that C is not in the same subnet as itself, so it sends the packet to the router and ignores it. , how to forward it later, A doesn’t care .

A How to know, which device is the router?

Answer: Set the default gateway on A

In the previous step, A judged whether it should send the packet to the router by checking whether it was in the same subnet as C. What is the IP of the router?

In fact, it is not accurate to say that it is sent to the router. It should be said that A will send the packet to the default gateway .

For A, A can only directly send the packet to an IP in the same subnet, so it does not matter whether it is sent to the router or to a certain computer, as long as the device has an IP Just the address.

Therefore , the default gateway is an IP address that A configures in its own computer so that it can send to this IP address when sending to machines in different subnets.

That's all!

How does the router know where C is?

Answer: routing table

Now A wants to send a data packet to C, and it can be successfully sent to the router. The last question is, how does the router know which port the received data packet should go out of , so that it can directly (or indirectly) finally Arrive at destination C.

The data packets received by the router have a destination IP, which is the IP address of C, and need to be converted into the port from which it will go out. It is easy to think that there should be a table, just like the MAC address table.

This table is called a routing table .

As for how this routing table came out, there are many routing algorithms. I won’t expand on this article because I don’t know how to haha~

Unlike the MAC address table, the routing table does not have a clear one-to-one relationship. Let's look at the structure of a routing table.

Destination address subnet mask Next hop port
192.168.0.0 255.255.255.0 0
192.168.0.254 255.255.255.255 0
192.168.1.0 255.255.255.0 1
192.168.1.254 255.255.255.255 1

We learn a new representation method. Since the subnet mask actually indicates the first few digits indicating the network segment of the subnet, so 192.168.0.0 (255.255.255.0) can also be abbreviated as 192.168.0.0/24

Destination address Next hop port
192.168.0.0/24 0
192.168.0.254/32 0
192.168.1.0/24 1
192.168.1.254/32 1

This is easy to understand. The routing table indicates that everything under the subnet 192.168.0.xxx is forwarded to port 0, and everything under the subnet 192.168.1.xxx is forwarded to port 1 . The next hop column has no value yet, so we don’t care about it for now.

Look at it with the structure diagram (the subnet mask and default gateway are completed here)

What I just mentioned is the IP layer, but the data link layer that sends data packets needs to know the MAC address, but what should I do if I only know the IP address?

Answer: arp

If you (A) don’t know the MAC address of your partner B at this time (you don’t know it in reality, we just assumed it was known), and you only know its IP address, how should you accurately pass the data packet to B? ?

The answer is simple. At the network layer, I need to find the MAC address corresponding to the IP address , that is, in some way, find  the MAC address BBBB  corresponding to  192.168.0.2 .

This method is  the arp protocol . At the same time, computers A and B will also have an  arp cache table , which records the corresponding relationship between  IP and MAC addresses .

IP address MAC address
192.168.0.2 BBBB

一开始的时候这个表是空的,电脑 A 为了知道电脑 B(192.168.0.2)的 MAC 地址,将会广播一条 arp 请求,B 收到请求后,带上自己的 MAC 地址给 A 一个响应。此时 A 便更新了自己的 arp 表。

这样通过大家不断广播 arp 请求,最终所有电脑里面都将 arp 缓存表更新完整。

总结一下

好了,总结一下,到目前为止就几条规则

从各个节点的视角来看

电脑视角

  • 首先我要知道我的 IP 以及对方的 IP

  • 通过子网掩码判断我们是否在同一个子网

  • 在同一个子网就通过 arp 获取对方 mac 地址直接扔出去

  • 不在同一个子网就通过 arp 获取默认网关的 mac 地址直接扔出去

交换机视角:

  • 我收到的数据包必须有目标 MAC 地址

  • 通过 MAC 地址表查映射关系

  • 查到了就按照映射关系从我的指定端口发出去

  • 查不到就所有端口都发出去

路由器视角:

  • 我收到的数据包必须有目标 IP 地址

  • 通过路由表查映射关系

  • 查到了就按照映射关系从我的指定端口发出去(不在任何一个子网范围,走其路由器的默认网关也是查到了)

  • 查不到则返回一个路由不可达的数据包

如果你嗅觉足够敏锐,你应该可以感受到下面这句话:

网络层(IP协议)本身没有传输包的功能,包的实际传输是委托给数据链路层(以太网中的交换机)来实现的。

涉及到的三张表分别是

  • 交换机中有 MAC 地址表用于映射 MAC 地址和它的端口

  • 路由器中有路由表用于映射 IP 地址(段)和它的端口

  • 电脑和路由器中都有 arp 缓存表用于缓存 IP 和 MAC 地址的映射关系

这三张表是怎么来的

  • MAC 地址表是通过以太网内各节点之间不断通过交换机通信,不断完善起来的。

  • 路由表是各种路由算法 + 人工配置逐步完善起来的。

  • arp 缓存表是不断通过 arp 协议的请求逐步完善起来的。

知道了以上这些,目前网络上两个节点是如何发送数据包的这个过程,就完全可以解释通了!

那接下来我们就放上本章 最后一个 网络拓扑图吧,请做好 战斗 准备!

这时路由器 1 连接了路由器 2,所以其路由表有了下一条地址这一个概念,所以它的路由表就变成了这个样子。如果匹配到了有下一跳地址的一项,则需要再次匹配,找到其端口,并找到下一跳 IP 的 MAC 地址。

也就是说找来找去,最终必须能映射到一个端口号,然后从这个端口号把数据包发出去。

目的地址 下一跳 端口
192.168.0.0/24 0
192.168.0.254/32 0
192.168.1.0/24 1
192.168.1.254/32 1
192.168.2.0/24 192.168.100.5
192.168.100.0/24 2
192.168.100.4/32 2

这时如果 A 给 F 发送一个数据包,能不能通呢?如果通的话整个过程是怎样的呢?

思考一分钟...

详细过程动画描述:

详细过程文字描述:

1. 首先 A(192.168.0.1)通过子网掩码(255.255.255.0)计算出自己与 F(192.168.2.2)并不在同一个子网内,于是决定发送给默认网关(192.168.0.254)

2. A 通过 ARP 找到 默认网关 192.168.0.254 的 MAC 地址。

3. A 将源 MAC 地址(AAAA)与网关 MAC 地址(ABAB)封装在数据链路层头部,又将源 IP 地址(192.168.0.1)和目的 IP 地址(192.168.2.2)(注意这里千万不要以为填写的是默认网关的 IP 地址,从始至终这个数据包的两个 IP 地址都是不变的,只有 MAC 地址在不断变化)封装在网络层头部,然后发包

4. 交换机 1 收到数据包后,发现目标 MAC 地址是 ABAB,转发给路由器1

5. 数据包来到了路由器 1,发现其目标 IP 地址是 192.168.2.2,查看其路由表,发现了下一跳的地址是 192.168.100.5

6. 所以此时路由器 1 需要做两件事,第一件是再次匹配路由表,发现匹配到了端口为 2,于是将其封装到数据链路层,最后把包从 2 号口发出去。

7. 此时路由器 2 收到了数据包,看到其目的地址是 192.168.2.2,查询其路由表,匹配到端口号为 1,准备从 1 号口把数据包送出去。

8. 但此时路由器 2 需要知道 192.168.2.2 的 MAC 地址了,于是查看其 arp 缓存,找到其 MAC 地址为 FFFF,将其封装在数据链路层头部,并从 1 号端口把包发出去。

9. 交换机 3 收到了数据包,发现目的 MAC 地址为 FFFF,查询其 MAC 地址表,发现应该从其 6 号端口出去,于是从 6 号端口把数据包发出去。

10. F 最终收到了数据包!并且发现目的 MAC 地址就是自己,于是收下了这个包

更详细且精准的过程:

读到这相信大家已经很累了,理解上述过程基本上网络层以下的部分主流程就基本疏通了,如果你想要本过程更为专业的过程描述,可以在公众号 低并发编程 后台回复 网络,获得我模拟这个过程的 Cisco Packet Tracer 源文件。

每一步包的传输都会有各层的原始数据,以及专业的过程描述

同时在此基础之上你也可以设计自己的网络拓扑结构,进行各种实验,来加深网络传输过程的理解。

后记

至此,经过物理层、数据链路层、网络层这前三层的协议,以及根据这些协议设计的各种网络设备(网线、集线器、交换机、路由器),理论上只要拥有对方的 IP 地址,就已经将地球上任意位置的两个节点连通了。

Guess you like

Origin blog.csdn.net/weixin_45925028/article/details/133068924