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)05/31 Report--
This article shows you how the receiving process of data packets in the Linux network is, the content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The following describes how data packets are transferred from the network card to the process step by step in the Linux system.
If there is no problem with English, it is strongly recommended to read the two articles in the following reference, which are introduced in more detail.
The editor only discusses the physical network card of Ethernet and does not involve virtual devices, and takes the receiving process of an UDP packet as an example.
The function call relationship listed in the example comes from kernel 3.13.0. if your kernel is not in this version, the function name and related path may be different, but the principle behind it should be the same (or slightly different).
Network card to memory
The network card needs a driver to work. The driver is the module loaded into the kernel, which is responsible for connecting the network card and the network module of the kernel. The driver registers itself into the network module when loading. When the corresponding network card receives the data packet, the network module will call the corresponding driver to process the data.
The following figure shows how the packet (packet) enters memory and is processed by the kernel's network module:
+-+ | | Memroy +-+ 1 | | 2 DMA +-+ | Packet |-> | NIC |-> | Packet | Packet | Packet |. | | +-+ | | +-+ | | NIC Driver | 4 | | +-| -- + + | 6 | Raise soft IRQ | ↓
1: the packet enters the physical network card from the outside network. If the destination address is not the network card, and the network card does not turn on promiscuous mode, the packet will be discarded by the network card.
2: the network card writes the data packet to the specified memory address by DMA, which is assigned and initialized by the network card driver. Note: the old network card may not support DMA, but the new network card generally supports it.
3: the network card notifies CPU through hardware interrupt (IRQ) that there is data coming.
4: CPU calls the registered interrupt function according to the interrupt table, which is called to the corresponding function in the driver (NIC Driver)
5: the driver first disables the interruption of the Nic, indicating that the driver already knows that there is data in memory. Tell the Nic to write directly to memory next time it receives a packet, and do not notify CPU. This can improve efficiency and prevent CPU from being interrupted constantly.
6: start the soft interrupt. At the end of this step, the hardware interrupt handler ends and returns. Because the hard interrupt handler cannot be interrupted in the process of execution, if it takes too long to execute, it will cause CPU to be unable to respond to interrupts from other hardware, so the kernel introduces soft interrupts, which can move the time-consuming part of the hard interrupt handler to the soft interrupt handler to deal with it slowly.
Network module of the kernel
The soft interrupt triggers the soft interrupt handling function in the kernel network module, and the subsequent process is as follows
+-+ 14 | | +-> | NIC | Enable IRQ +-+ | | +-+ Memroy | | Read +-| -+-+-> | NIC Driver | | drop this packet | +-+-+ | | ↓ +- -+ | NF_INET_PRE_ROUTING | +-+ | | ↓ +-+ | | enabled ip forword +-+- -- + | routing |-> | ip_forward |-> | NF_INET_FOWARD | +-+ | destination IP is local ↓ ↓ +-| -+ + | dst_output_sk | | ip_local_deliver | +-+- + | | ↓ +-+ | NF_INET_LOCAL_IN | +-+ | | ↓ +-+ | UDP layer | +-+
Ip_rcv: the ip_rcv function is the entry function of the IP module. In this function, the * thing is to throw away the junk packet directly (the destination mac address is not the current Nic, but is received because the Nic has set the promiscuous mode), and then call the function registered on the NF_INET_PRE_ROUTING.
NF_INET_PRE_ROUTING: a hook placed in the protocol stack by netfilter. Some packet handling functions can be injected through iptables to modify or discard the packet. If the packet is not dropped, it will continue to go down.
Routing: routing. If the destination IP is not a local IP, and the ip forward function is not enabled, the packet will be discarded. If the ip forward function is enabled, it will enter the ip_forward function.
Ip_forward: ip_forward will first call the NF_INET_FORWARD-related function registered by netfilter. If the packet is not discarded, it will continue to call the dst_output_sk function later.
Dst_output_sk: this function calls the corresponding function in the IP layer to send the packet out, just like the second half of the packet sending process described in the next article.
Ip_local_deliver: if the destination IP is found to be a local IP when routing above, this function will be called. In this function, the hook program related to NF_INET_LOCAL_IN will be called first. If passed, the packet will be sent down to the UDP layer.
UDP layer
| | ↓ +-+ +-- + | udp_rcv |-> | _ _ udp4_lib_lookup_skb | +-+- -+ | | ↓ +-+-+ | sock_queue_rcv_skb |-> | sk_filter | +-+-+ | | ↓ +-+ | _ _ skb_queue_tail | +-+ | | ↓ +-+ | sk_data_ready | +-+ |
Udp_rcv: the udp_rcv function is the entry function of the UDP module. It will call other functions, mainly to do some necessary checks. One of the important calls is _ _ udp4_lib_lookup_skb. This function will find the corresponding socket according to the destination IP and port. If the corresponding socket is not found, the packet will be discarded, otherwise continue.
Sock_queue_rcv_skb: the first is to check whether the receive buffer of the socket is full, if so, discard the packet, and then call sk_filter to see if the packet meets the conditions. If filter is set on the current socket and the packet does not meet the conditions, the packet will also be discarded (in Linux, filter can be defined on each socket as in tcpdump Packets that do not meet the conditions will be discarded)
_ _ skb_queue_tail: put the packet at the end of the socket receive queue
Sk_data_ready: notifies socket that the packet is ready
After calling sk_data_ready, a packet is processed, waiting for the application layer program to read, and all the above functions are executed in the context of a soft interrupt.
Socket
There are generally two ways for the application layer to receive data. One is that the recvfrom function blocks waiting for the data to come. In this case, when socket receives the notification, recvfrom will be awakened, and then read the data of the receiving queue; the other is to listen to the corresponding socket through epoll or select, and then call the recvfrom function to read the data of the receiving queue after receiving the notification. In both cases, the corresponding data packets can be received normally.
The above is what the process of receiving packets in Linux network is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.