Fun talk about network protocols-Lecture 2 | What is the true meaning of network layering?

This series of related blog, reference geeks time - Something about network protocol

Fun talk about network protocols-Lecture 2 | What is the true meaning of network layering?

I have been engaged in computer network related work for a long time. I found that computer network has a remarkable feature, that is, it is a discipline that not only needs to be memorized, but also needs to be familiar with the principle. Many questions seem to be understood, but I am afraid to ask them in detail, and I will find that you do not understand them so thoroughly.

In the previous section, we listed the network protocols to be discussed later. These agreements are not unusual at all, every textbook will speak, and you are required to memorize them. Because the exam will take the test, the interview will ask. It can be said that after graduating to find a job and still unable to answer such questions, then your written test will basically hang up.

When you hear about Layer 2 devices, Layer 3 devices, Layer 4 LB, and Layer 7 LB middle layers, are you a bit confused? I do n’t know these so-called layers, what specific “work” do the various protocols correspond to?

Do you really understand these four questions?

Because textbooks or teachers often make a very inappropriate analogy: Why should the network be layered? Because there are different ways of communication between different levels, this is called an agreement. For example, a company is also divided into "levels", including general managers, managers, team leaders, and employees. The general manager has their way of communication, and there is also the way of communication between the manager and the manager, as well as the team leader and employees. Have you ever heard a similar analogy?

Then the first question comes. May I ask what the employees are doing when the manager shakes hands? Many people have heard TCP's three-way handshake protocol to establish a connection, and they will recite it as a point of knowledge. Similarly, I ask you, when TCP performs three handshake, what operations do the IP layer and MAC layer correspond to?

In addition to the inappropriate metaphor above, the textbook will list the protocols included in each level, and then start to talk about these protocols layer by layer.

But what about the relationship between these agreements? But few textbooks can speak.

When learning the third layer, it will be mentioned that the IP protocol contains the target address and source address. The third layer often learns routing protocols. The route is like a relay station. We go from the original address A to the destination address D, passing through two relay stations A-> B-> C-> D in the middle, and it is forwarded through the route.

Then the second question comes. A knows that his next transfer station is B. Where should the packet sent from A put B's IP address? B knows that his next transfer station is C. Where should the IP address of C be placed in the packet sent from B? If the destination address is placed in the IP protocol, and the packet arrives at the relay station, how do you know that the final destination address is D?

The textbook will not tell the life cycle of the network package through scene-based examples, so you will be confused and do not know what the actual application scenarios of these protocols are.

I will ask you another question. You must often hear about second-tier equipment and third-tier equipment. Layer 2 devices usually deal with the MAC layer. Then I send an HTTP packet, which works on the seventh layer, does that need not go through the second layer device? Or even if it passes, the second-tier equipment will not deal with it? Or to put it another way, is there any HTTP layer content in the packets processed by the layer 2 device?

Finally, I want to ask you a comprehensive question. From your computer, log in to the public cloud host via SSH, what process do you need to go through? Or, what process do you have to go through to open an e-commerce website? The more detailed the better.

The actual situation may be that many people will not answer. Although they are familiar with each layer, knowledge points cannot be connected.

Some of the above questions will have an explanation in this section, and some will run through our entire course. Fortunately, in a later section, I will give a through example. After explaining the details of many layers, you can easily string these knowledge points together.

Why should the network be layered?

Why should the network be layered?

Here we first discuss the first question, why should the network be layered? Because, it is a complex program to be layered.

To understand the concepts in computer networks, a good perspective is to imagine that a network packet is a buffer, or a block of memory, in a format. At the same time, imagine that you are a program that processes network packets, and this program can run on a computer, a server, a switch, or a router. You imagine you have a lot of network ports. Take a network packet from a certain port, process it with your own program, and send it out from another network port.

Of course, the format of the network packet is very complicated, and this program is also very complicated. Complex procedures must be layered, which is the requirement of program design. For example, complex e-commerce will also be divided into database layer, cache layer, Compose layer, Controller layer and access layer, each layer focuses on doing things at this layer.

How does the program work?

How does the program work?

We can simply imagine the working process of the program "you".
Insert picture description here
When a network packet passes through a network port, you see it. First, see if you want to come in and handle it. Some network ports are configured with promiscuous mode, and all passing through are taken in.

After taking it in, it will be handed over to a program to deal with. So, you call process_layer2 (buffer). Of course, this is a fake function. But you understand the meaning and know that there must be such a function. What does this function do? Remove the header of the second layer from the Buffer and take a look at what should be done according to the contents of the header.

Assuming that you find that the MAC address of this packet matches yours, the instructions are sent to you, so you need to call process_layer3 (buffer). At this time, there is often no layer 2 header in the Buffer, because it has been removed during the processing of the previous function, or the starting offset has been moved a bit. In this function, remove the header of the third layer to see if it is sent to yourself, or if you want to forward it yourself.

How to judge? If the IP address is not yours, it should be forwarded; if the IP address is yours, it should be sent to you. According to the label in the IP header, remove the three-layer header and proceed to the next layer of processing. Is it to call process_tcp (buffer) or process_udp (buffer)?

Assuming this address is TCP, process_tcp (buffer) will be called. At this time, there is no three-layer header in the Buffer. You need to check the four-layer header to see if this is an initiation, a response, or a normal data packet, and then each is processed by different logic. If it is an initiation or response, then a reply packet may be sent; if it is a normal data packet, it needs to be handed over to the upper layer. To whom? Is there a process_http (buffer) function?

No, if you are a network packet handler, you do not need to have process_http (buffer), but should be left to the application to handle. Which application is it for? There is a port number in the header of the fourth layer, and different applications listen to different port numbers. If you find that the browser application is listening on this port, then you just send it to the browser. As for how the browser handles it, it has nothing to do with you.

The browser naturally parses the HTML and displays the page. The computer owner was very happy to see the page and clicked the mouse. The mouse click is captured by the browser. The browser knows that another HTTP request is about to be initiated, so the port number is used to send the request to you.

You should call send_tcp (buffer). Needless to say, the Buffer contains the content of the HTTP request. Add a TCP header to this function and record the source port number. The browser will give you the destination port number, which is generally port 80.

Then call send_layer3 (buffer). Buffer already has HTTP header and content, and TCP header. Add an IP header to this function, and record the source IP address and destination IP address.

Then call send_layer2 (buffer). Buffer already has HTTP header and content, TCP header, and IP header. In this function, add the MAC header, record the source MAC address, and get the MAC address of the machine and the target MAC address. However, this depends on whether you know it or not, and add it if you know it; if you do n’t, you will have to go through a certain protocol processing process to find the MAC address. Anyway, one must be filled in, and it cannot be left blank.

Everything is ready, as long as the contents of the Buffer are complete, you can send it out through the network port, and your task as a program is complete.

Demystifying the relationship between layers

Demystifying the relationship between layers

After knowing this process, let's look at the original puzzle again.

The first is the layered metaphor. All the metaphors that cannot express the meaning of layers of packaging are inappropriate. The general manager shakes hands and does not need employees to be there. There is nothing to be discussed between general managers and employees are not required to participate, but this is not the case in the online world. It should be correct that when the general manager communicates, the manager puts the general manager in his pocket, then the team leader puts the manager in his pocket, and the employee puts the team leader in his pocket, like a doll. It was inappropriate for the employee to communicate directly without the general manager.

In real life, employees often say one sentence, the team leader adds two sentences, then the manager adds two sentences, and finally the general manager adds two sentences. But in the online world, the general manager should speak, the manager adds two sentences, the team leader adds two sentences, and the employee adds two sentences.

What are the IP and MAC layers doing during the three-way handshake? Of course, TCP sends every message with IP layer and MAC layer. Because, every time TCP sends a message, all the mechanisms of the IP layer and the MAC layer must be run again. And you only see TCP three-way handshake, in fact, the IP layer and MAC layer have been busy working for this for a long time.

One thing to remember here: As long as the package is running on the network, it is complete. There may be no lower layer but no upper layer.

Therefore, for the TCP protocol, a three-way handshake or a retry is required. As long as you want to send out packets, there must be an IP layer and a MAC layer, otherwise it cannot be sent out.

People often ask such a question. I know the IP address of that machine. I sent him a message directly. What do I need the MAC address for? The key here is that messages cannot be sent without a MAC address.

So if a packet of HTTP protocol runs on the network, it must be complete. No matter what equipment this package passes through, it is complete.

The so-called two-tier equipment and three-tier equipment are different in the running procedures of these devices. An HTTP protocol packet passes through a Layer 2 device, and the Layer 2 device receives the entire network packet. There are HTTP, TCP, IP and MAC. What is a Layer 2 device? It just removes the MAC header to see if it is discarded, forwarded, or left alone. What is a three-tier device? After removing the MAC header, the IP header is removed to see if it is discarded, forwarded, or left alone.

summary

To sum up today's content, to understand the working mode of the network protocol, there are two tips:

  • Always imagine that you are a program that handles network packets: how to get network packets, how to process them according to rules, and how to send them out;
  • Always keep in mind a principle: as long as the package is running on the network, it is complete. There may be no lower layer but no upper layer.

Finally, leave you two thinking questions.

  1. If you also think that the analogy between the general manager and employees is inappropriate, do you have a more appropriate analogy?
  2. To learn the network protocol, the concept of IP is the most basic. Do you know how to check the IP address?
Published 40 original articles · won praise 1 · views 4976

Guess you like

Origin blog.csdn.net/aha_jasper/article/details/105525982