Linux Performance Tuning combat: Linux on the network, you have to know (the) (say 33)

First, the section reviews

Previous sections, we work together to learn the principles of the file system and disk I / O, and the corresponding performance analysis and optimization methods. Next, we will enter the next important module - Linux network subsystems.

As the flow of network processing of the most complex, with the process of scheduling we talked about earlier, interrupt handling, memory management and I / O are all inextricably linked, so I put a resource network module as the last module to explain.

With CPU, memory as well as I / O, the core function of the network is the Linux system. Network is a network of different computer or device connected to the technology together, is an interprocess communication on its nature, particularly cross-system process
between the communication must be carried out through the network to. With the popularity of high concurrency, distributed, cloud computing, micro-technology services, network performance becomes increasingly important.

Then:

1, Linux is how networks work?

2, and what indicators to measure the performance of the network do?

The next two articles, I will take you to learn the working principle and performance of Linux network together.

Second, the network model

When it comes to network, I'm sure you often mention seven load balancing, load balancing four, or three devices, two-story equipment. Well, here to say two, three, four, seven and both are what does that mean?

1, OSI network model

In fact, these layers from the Open Systems Interconnection Reference Model Communication International Organization for Standardization (OpenSystem Interconnection Reference Model), referred to as the OSI network model .

In order to solve the compatibility problem of heterogeneous networking devices, and complex decoupling network packet processing flow, OSI model divides networking framework divided into an application layer, presentation layer, session layer, transport layer, network layer, data link layer and the physical layer
and other seven, each layer is responsible for different functions. among them,

The application layer is responsible for providing a unified interface for applications.
The presentation layer is responsible for converting data into a format compatible with the receiving system.
The session layer is responsible for maintaining a communication connection between the computer.
The transport layer is responsible for the transmission of data plus header, the packet is formed.
The network layer is responsible for data routing and forwarding.
The data link layer is responsible for MAC addressing, error detection and error correction.

2, TCP / IP model

But the OSI model is still too complicated, did not provide a method that can be achieved. So, in Linux, we actually use is another, more practical four-layer model, namely TCP / IP network model.

TCP / IP model, the frame is divided into four interconnected network application layer, transport layer, network layer, network interface layer, etc., wherein,

The physical layer is responsible for transmitting the data frame in the physical network.
The application layer is responsible for providing a set of applications to users, such as HTTP, FTP, DNS and the like.
The transport layer is responsible for end to end communication, such as TCP, UDP and the like.
Network layer for network packet encapsulation, addressing and routing, such as IP, ICMP, etc.

Network interface layer, is responsible for packet transmission network in a physical network, such as MAC address, error detection, and the like through a card transport network frame.

To help you better understand the relationship between the image of TCP / IP and the OSI model, I drew a picture, as shown below:

Of course, although in accordance with the actual Linux TCP / IP model implements the network protocol stack, but in the usual exchange of learning, we are still customarily used to describe the OSI seven-layer model. For example, when it comes to seven and four load balancing, the corresponding points
respectively in the OSI model is the application layer and the transport layer (which corresponds to the TCP / IP model, in fact, four and three).

TCP / IP model includes a large number of network protocols, principles of these agreements, as well as the core foundation of knowledge that each of us must master. If you are not skilled, it is recommended that you go to school "TCP / IP Detailed" Volume I and Volume II of, or learning geeks time
produced "Something about network protocol" column.

Three, Linux networking stack

With TCP / IP model, during the network transmission, the packet according to the protocol stack, the upper layer of the incoming data layer by layer; then the protocol header of the encapsulation layer, and then sent to the next layer .

Of course, network packet processing logic in each layer, depending on the network protocol layers are used. For example, in the application layer, the application provides a REST API, the HTTP protocol may be used, it needs to transmit the encapsulated data to a JSON HTTP
protocol, and then passed down to the TCP layer.

1, the package

The package is very simple to do, just before and after the original load, increase the fixed format of metadata, the original load data will not be modified.

For example, the network packet to the TCP protocol to communicate, for example, by the image below, we can see that the application data in each encapsulation layer

among them:

Transporting layer in front of the application data adds TCP header; 
network layer packet before TCP adds IP header; 
the network interface layer, and before and after, respectively, the IP packet header and trailer increases.

The new head and tail, are filled according to a specific protocol format, want to know the specific format, you can view the document protocol. For example, you can see here, understand TCP header format

2. What is MTU? Linux  MTU The default value is how much

The new head and tail, increase the size of the network packets, but we all know that physical link and can not transmit packets of any size . Maximum transmission unit configured network interfaces (MTU), to a predetermined maximum IP packet size.
In our most commonly used in Ethernet, MTU default value is 1500 (which is the default for Linux) .

Once the packet exceeds the MTU size of the network, it will be fragmented on the network layer to ensure that the IP packet fragmentation is not larger than the MTU value. Obviously, MTU greater need of subcontracting the less, naturally, the better network throughput.

After understanding the principles encapsulated TCP / IP network model and network packets, you can easily imagine, Linux kernel network stack, in fact, similar to the TCP / IP four-layer structure. Shown below, it is the Linux IP network stack general schematic

diagram:

 

 

 


(Reference image "Performance top of" generic IP network stack FIG drawn 10.7)

3, LAN equipment

We look at the network stack from top to bottom, you can find,

1, the top of the application, you need to call through the system to interact with the sockets interface; below the socket, that is the transport layer, network layer and network interface layer we mentioned earlier;

2, the bottom, is the physical NIC driver and network adapter devices.

Here, I briefly about the card:

1, the basic device is a NIC to send and receive network packets.

2, during system startup, the registration card to the system through the network card driver in the kernel.

3, and in the network transceiver process, the kernel interacts with the card via an interrupt.

Linux network stack combined with the aforementioned, it can be seen, very complicated processing network packets. So, the NIC hardware interrupt only deal with the core of the network card to read or send data , while the protocol stack most of the logic, will put the soft interrupt handling .

Four, Linux network transceiver Process

Understanding of the Linux networking stack, we look at, in the end is how Linux transceiver network packets.

Note that the following are physical card, for example. In fact, Linux also supports a large number of virtual network
devices and their network transceiver process there will be some differences.

1, the network packet receiving process

We first look at the process receives network packets.

1, when a frame arrives the network card, the card will pass the DMA mode, the network packet into the receive packet queue; followed by hardware interrupt, to tell the interrupt handler has received network packet.

2, then the interrupt handler for the card network frame allocated kernel data structures (sk_buff), and copies it into the buffer sk_buff; and then through the soft interrupt, notify the kernel receives a new network frame.

3, next, the kernel protocol stack is removed from the network frame buffer, and the network protocol stack, layer by layer from the bottom to the network frame. such as,

1, the legitimacy of the link layer checks the message to find out the type of the upper layer protocols (such as IPv4 or the IPv6), then remove the header, frame end, then to the network layer.

2, the network layer IP header removed, the next step to determine whether the network packet, such as a processing or forwarding to the upper layer. When the network layer is to confirm that the packet sent to the machine, it will remove the upper layer protocol type (such as TCP or the UDP), went
      off the IP header, and then to the transport layer processing.

3, the transport layer TCP header or a UDP header extraction, according to <source the IP, source port, destination IP, destination port> as a four-tuple identifier, find the corresponding Socket, and copy the data to the receiving buffer in the Socket.

4, finally, the application can use the Socket interface to read the newly received data. In order to more clearly express this process, I drew a picture, the left part of this figure represents the receiving process, while the pink arrows in the figure indicates processing path of network packets.

 

 

2, the network packet transmission process

After the network packet receiving process understanding, it is readily understood that the network packet transmission process. Process packet transmission network is the right half of the figure, it is easy to find the direction of transmission of network packets received with opposite directions.

1, First, the application calls the Socket API (such as the sendmsg) packet transmission network.

Since this is a system call, it will fall into the socket layer in kernel mode. Socket Socket Layer will send packets into the buffer.

2, then, transmitted from a network protocol stack Socket buffer,

1, remove the packet; then in accordance with TCP / IP stack, layer by layer from top to bottom.

2, for example, the transport layer and network layer, respectively, increases its TCP and IP header, IP route lookup to confirm the next hop, and fragmented according to the MTU size.

3, after the fragmented packet network, then sent to the network interface layer, a physical address is the address to find the next hop MAC address. Then add header and trailer, contracting into a queue.

After all this is completed, there will be soft interrupt signals the driver: contracting queue new network frame needs to be sent

3, finally, the driver through the DMA, the network frame is read out from the bidding queue, and sends it out through physical NIC.

 V. Summary

In today's article, I'll take you sort out the Linux network works together.

Multiple servers connected together by cards, switches, routers and other network devices, interconnected network configuration. Due to the heterogeneity of network protocols and complexities of network equipment, International Organization for Standardization defines one of the seven OSI network
model, but the model is too complicated, the fact that the standard practice is more practical TCP / IP model.

TCP / IP model, the networking framework, divided into four application layer, transport layer, network layer, network interface layer or the like, which is Linux network stack core component.

1, the application, first layer by layer from top to bottom in the network protocol stack through a socket interface to send packets, and eventually sent to the card.

2, while receiving the same through the first network stack layer by layer from the bottom to the final applications will be sent.

Understand the basic principles and processes Linux network transceiver after, you certainly can not wait to know how to observe the performance of the network. So, specifically, what indicators can measure network performance Linux it? Do not worry, I will next
explain in detail the section for you.

Guess you like

Origin www.cnblogs.com/luoahong/p/11464755.html