In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you Linux how to send and receive network packets, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Text
Network model
In order to enable a variety of devices to communicate with each other through the network, and to solve the compatibility of different devices in the network interconnection, the International Standards Organization has developed an open system interconnection communication reference model (open System Interconnection Reference Model), that is, the OSI network model, which mainly has seven layers, namely, application layer, presentation layer, session layer, transport layer, network layer, data link layer and physical layer.
The functions of each layer of responsibility are different, as follows:
The application layer, which is responsible for providing a unified interface for applications
The presentation layer, which is responsible for converting the data into a format that can be recognized by another system
Session layer, which is responsible for establishing, managing, and terminating communication sessions between presentation layer entities
Transport layer, responsible for end-to-end data transmission
Network layer, responsible for data routing, forwarding and fragmentation
Data link layer, responsible for data sealing and error detection, as well as MAC addressing
The physical layer, which is responsible for transmitting data frames in the physical network
Because the OSI model is too complex, the proposed concept is only a theoretical layering, and does not provide a specific implementation scheme. In fact, what we are more common and practical is the four-layer model, that is, the TCP/IP network model. Linux system implements the network protocol stack according to this network model.
The TCP/IP network model has four layers, namely, the application layer, the transport layer, the network layer, and the network interface layer. Each layer is responsible for the following functions:
Application layer, which is responsible for providing users with a set of applications, such as HTTP, DNS, FTP, etc.
Transport layer, responsible for end-to-end communication, such as TCP, UDP, etc.
Network layer, responsible for encapsulation, fragmentation, routing and forwarding of network packets, such as IP, ICMP, etc.
The network interface layer is responsible for the transmission of network packets in the physical network, such as frame sealing, MAC addressing, error detection, and transmission of network frames through network cards.
The TCP/IP network model is much simpler and easier to remember than the OSI network model. The relationship between them is shown in the following figure:
However, we often talk about layer 7 and layer 4 load balancing, which is described by the OSI network model. Layer 7 corresponds to the application layer and layer four corresponds to the transport layer.
Linux network protocol stack
We can compare our body to the data in the application layer, the bottom clothing to the TCP header in the transport layer, the coat to the IP header in the network layer, and the hat and shoes to the header and trailer of the network interface layer, respectively.
In the winter season, when we want to go out from home to play, we naturally have to put on a bottom coat, then put on a warm coat, and finally put on a hat and shoes before going out. This process is like when we send out the network packet of TCP protocol communications, we will encapsulate and process the data of the application layer according to the network protocol stack.
You can see from the following figure the encapsulation format of the application layer data at each layer.
Where:
Transport layer, adding TCP headers to the front of the application data
Network layer, adding IP headers to the front of TCP packets
Network interface layer, adding a frame header and a frame trailer respectively before and after the IP packet
These additions and headers and tails have their own functions, and they are all filled according to a specific protocol format. Each layer adds its own protocol header, so the size of the natural network packet increases, but the physical link cannot transmit packets of any size, so in Ethernet, the maximum transmission unit (MTU) is 1500 bytes, that is, the maximum IP packet size for a single transmission.
When the network packet exceeds the size of the MTU, it will be shredded at the network layer to ensure that the sharded IP packet will not exceed the MTU size. If the MTU is smaller, more packets are needed, and the network throughput capacity is worse. On the contrary, the larger the MTU, the smaller the subpackets required, so the network throughput capacity is better.
After knowing the TCP/IP network model and the encapsulation principle of network packets, you must have guessed what the Linux network protocol stack looks like. It is actually similar to the four-layer structure of TCP/IP:
From the network protocol stack shown above, you can see:
The application needs to exchange data with the Socket layer through system calls
Below the Socket layer are the transport layer, network layer, and network interface layer.
The bottom layer is the network card driver and the hardware network card device.
The process of receiving network packets by Linux
The network card is a hardware in the computer, which is specially responsible for receiving and sending network packets. When the network card receives a network packet, it will put the network packet into Ring Buffer through DMA technology. This is a ring buffer, and the buffer is in the network card driver in the kernel memory.
After receiving the network packet, how should I tell the operating system that the network packet has arrived?
One of the simplest ways is to trigger an interrupt, that is, every time the network card receives a network packet, it triggers an interrupt to tell the operating system.
However, there is a problem. In a high-performance network scenario, the number of network packets will be very large, so a lot of interrupts will be triggered. You know, when CPU receives an interrupt, it will stop what it is doing, and deal with these network packets. After processing these network packets, it will go back to other things. Triggering interrupts so frequently will lead to processing interruptions in which CPU has never been played. As a result, other tasks may not be able to move forward, thus affecting the overall efficiency of the system.
Therefore, in order to solve the performance overhead caused by frequent interrupts, the Linux kernel introduces the NAPI mechanism in version 2.6. it is a mixture of "interrupt and polling" to receive network packets. Its core concept is to read data without interruption, but first to use the service program that interrupts to wake up data reception, and then poll method to poll data.
For example, when a network packet arrives, the network card initiates a hardware interrupt, and then executes the network card hardware interrupt handling function, which needs to "temporarily shield the interrupt" after processing, and then wakes up the "soft interrupt" to poll the processing data. interrupts are not resumed until there is no new data, so that multiple network packets can be processed at one time, thus reducing the performance overhead caused by network card interrupts.
How do soft interrupts deal with network packets? It copies data from Ring Buffer into the kernel struct sk_buff buffer so that it can be processed layer by layer as a network packet to the network protocol stack.
First of all, it will enter the network interface layer, where it will check the validity of the message. If it is illegal, it will discard it. If it is legal, it will find out the type of upper layer protocol of the network packet, such as IPv4 or IPv6, then remove the header and trailer, and then give it to the network layer.
At the network layer, the IP packet is taken out to determine the next direction of the network packet, such as whether it is handed over to the upper layer for processing or forwarded out. When it is confirmed that the network packet is to be sent to the local machine, it will look in the IP header to see if the upper layer protocol type is TCP or UDP, then remove the IP header and give it to the transport layer.
The transport layer takes out the TCP header or UDP header, finds the corresponding Socket according to the quadruple "source IP, source port, destination IP, destination port", and copies the data to the receiving buffer of Socket.
Finally, the application layer program calls the Socket interface to read the new data from the kernel's Socket receive buffer to the application layer.
At this point, the receiving process of a network packet is over. You can also see the process of receiving the network packet from the left part of the following figure, and the right part is just the opposite, which is the process of sending the network packet.
The process of sending network packets by Linux
As shown in the half of the figure above, the process of sending a network packet is exactly the opposite of the receiving process.
First of all, the application will call the interface where the Socket sends the packet. Because this is a system call, it will fall into the Socket layer in the kernel state from the user mode, and the Socket layer will copy the application layer data into the Socket send buffer.
Next, the network protocol stack takes the packet from the Socket send buffer and processes it layer by layer according to the TCP/IP protocol stack.
If the TCP transport protocol is used to send data, the TCP header is added at the transport layer and then handed over to the network layer, which adds IP packets to the packet, and then confirms the next hop IP by querying the routing table and slicing according to the MTU size.
The sliced network packet is sent to the network interface layer, where the MAC address of the next hop is obtained through the ARP protocol, and then the header and trailer are added to the packet queue.
When these are ready, a soft interrupt will be triggered to tell the network card driver that there is a new network packet to be sent. Finally, the driver reads the network packet from the sending queue through DMA, puts it into the queue of the hardware network card, and then the physical network card sends it out.
The above is all the contents of the article "how to send and receive Network packets in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.