Application of NAPI technology in Linux network drive (I)

Source: Internet
Author: User
Article title: NAPI technology application on Linux network drivers (I ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
NAPI is a technology used in Linux to improve network processing efficiency. its core concept is to read data without interruption, instead of POLL, similar to the half-end method (bottom-half processing mode), but currently NAPI in Linux is less efficient. This article analyzes NAPI while, an efficient improvement method is provided for your reference.
  
   Preface:
NAPI is a technology used in Linux to improve network processing efficiency. its core concept is to read data without interruption, instead, we first use the service program to interrupt and wake up data reception, and then POLL the data (similar to the bottom half (bottom-half) processing mode ); according to the data we obtained in the experiment, NIC-triggered interruptions can be continuously reduced as the network receives more quickly, at present, NAPI technology has been widely used in the NIC driver layer and network layer. at the driver level, E1000 series NICs and RTL8139 series NICs are available, mainstream network adapters such as the 3c50X series use this technology. at the network level, NAPI technology has been fully applied to the famous netif_rx function, the dedicated POLL method process_backlog is provided to process polling. according to the experimental data, NAPI technology can greatly improve the efficiency of receiving short-length data packets and reduce the interruption trigger time; because RTL8139CP is an application ratio Therefore, this article uses NAPI as an example to illustrate the application and basic principles of NAPI in network adapters.
  
However, NAPI has some serious defects: for upper-layer applications, the system cannot process each data packet as soon as it receives it, and as the transmission speed increases, the accumulated data packets will consume a lot of memory. experiments show that the problem on the Linux platform is more serious than on FreeBSD; another problem caused by NAPI is that it is difficult to process large data packets, the reason is that it takes much longer to send large data packets to the network layer than short data packets (even using DMA), so as mentioned above, NAPI technology is suitable for processing high-speed short-length data packets. at the end of this article, we propose NAPI improvement methods and experimental data.
  
   NAPI prerequisites:
The driver can continue to use the network driver interface of the old 2.4 Kernel. the addition of NAPI will not lead to the loss of forward compatibility, but the use of NAPI should at least be guaranteed as follows:
A. the round-robin input queue (ring_dma, which is detailed in the Ethernet section of the 2.4 driver) to use DMA, or there is enough memory space to cache the packets obtained by the driver.
B. the ability to process NIC interruption events when sending/receiving packets are interrupted. after NIC disconnection, the circular buffer (rx-ring) of the network device is not affected) processing queue.
  
NAPI uses the round robin method to process the events when data packets arrive. when data packets arrive, NAPI enforces the dev-> poll method. Unlike the previous drivers, the interrupt method is usually used to reduce the processing delay of packet arrival time.
  
It should be noted that, after testing, if the DEC Tulip series (DE21x4x chip) and some Nic chips of National Semi are tested, if you change the previously interrupted parts to the device's POLL method for execution, it will cause a slight delay. Therefore, the MII (media-independent) for more information, see the mii_check_media function processing process.
  
The example shown below shows how to put the processing process in the poll method of dev in 8139, and put all the original interrupt processing processes in the POLL method. for the sake of length, we will only introduce the method of receiving POLL.
  
In the following 8139CP driver introduction, it indicates that everything that can be done in the interrupt program can be done in the POLL method, of course, the statuses and events to be handled by different NICs during the interruption are different.
  
   For all NIC devices, two types of NIC receive event register response mechanisms are available:
COR mechanism: when the user program reads the status/event Register, the status queue indicated in the register and NIC rx-ring will be cleared when the read is complete, NIC of natsemi and sunbmac will do this. in this case, all the previously interrupted responses of the NIC must be moved to the POLL method.
  
COW mechanism: When a user program writes a status register, it must first write 1 clear 0 to the bit to be written. the 8139CP to be introduced below is of this type, most NICs belong to this type, and this type has the best response to NAPI. it only needs to place the received data packet processing part in the POLL method, the status processing of received events is in the original interrupt control program. we will wait for the 8139CP type Nic to be introduced.
  
C. prevents packet conflicts in the NIC queue.
When the sending/receiving shutdown event is interrupted, NAPI will be called in POLL for processing. due to the POLL method, NIC interruption will no longer be able to notify packets to arrive, in this case, if the polling is completed and the NIC is disconnected immediately, a POLL event is triggered, this kind of packet arrives at the time of interruption and shutdown is called "rotting". This will lead to a competition between the POLL mechanism and NIC interruption. the solution is to use the received status bit of the NIC, continue to receive data in the rx-ring buffer in the ring queue until no data is received.
  
   Lock and conflict prevention mechanisms:
-1.SMP guarantee mechanism: ensure that only one processor calls the POLL method of the network device at the same time, as we will see below, there is only one processor that can call the POLL method for NIC devices that call netif_rx_schedule to be mounted to the POLL queue.
-2. the network core layer (net core) calls the device driver to send data packets cyclically. the device driver layer receives data packets without a lock, the network core layer must also ensure that only one processor can use soft interrupt processing to receive queues at a time.
-3. when multiple processors access NIC rx-ring, they can only occur when the call to the cyclic queue is closed or suspended) method (at this time, it will try to clear the receiving cyclic queue)
-4. for the data synchronization problem (for the receive loop queue), the driver does not need to consider the network layer programs that have completed these tasks.
-5. if all the parts are not handed over to the POLL method for processing, NIC interruption still needs to be enabled. the status of the receiving link changes and the sending completion interruption is still the same as the previous processing steps, this process assumes that the reception interruption is the largest load on the device, and of course it cannot be said that this is correct.
The following sections describe how to call the POLL method of a device in receiving events.
  
   Important functions and data structures and functions provided by NAPI:
Core data structure:
  
The field in the struct softnet_data structure is the processing queue between the NIC and the network layer. this structure is global and it transmits data information between the NIC interrupt and the POLL method. The fields include:
  
Struct softnet_data
{
Intthrottle;/* indicates that the data packet in the current queue is disabled */
Intcng_level;/* indicates the degree of data packet congestion of the current processor */
Intavg_blog;/* average congestion of a certain processor */
Struct sk_buff_headinput_pkt_queue;/* receives the sk_buff queue of the buffer zone */
Struct list_head poll_list;/* POLL device queue header */
Struct net_device output_queue;/* queue header of the network device sending queue */
Struct sk_buffcompletion_queue;/* queue in which sent packets are waiting for release */
Struct net_devicebacklog_dev;/* indicates the network device currently involved in POLL processing */
};
  
Core API:
  
1. netif_rx_schedule (dev)
This function is called by the interrupt service program. It adds the POLL method of the device to the POLL processing queue at the network level, queues and prepares to receive data packets. before using this function, you need to call netif_rx_reschedule_prep, the number of returned packets is 1, and a NET_RX_SOFTIRQ soft interrupt notification is triggered to receive packets at the network layer.
  
2. netif_rx_schedule_prep (dev)
Make sure the device is running and the device has not been added to the POLL processing queue at the network layer. This function is called before netif_rx_schedule is called.
  
3. netif_rx_complete (dev)
Clears the specified device from the POLL queue. it is usually called by the POLL method of the device. Note that if the specified device cannot be cleared when the POLL queue is in the working status, otherwise, an error occurs.
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.