Application of NAPI technology in Linux network drive (2)
Source: Internet
Author: User
Article title: NAPI application in Linux Network Driver (2 ). 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.
How to use NAPI in 8139CP: The essence of the POLL method is to minimize the number of interruptions, especially when a large number of small-length data packets are sent, in order to prevent the entire operating system from spending too much time on the protection and recovery of the interrupted site, so as to use the time to process data transmission at my network layer, for example, in the 8139CP interrupt handling process described below, the purpose is to put the device that has the interrupt on the poll_list as soon as possible and disable the device that receives the interrupt, finally, the device's POLL method is called directly to process data packet reception until the received data packet is unreachable, or the scheduling is completed within a time slice.
The RTL8139C + data receiving annular buffer queue:
The RTL8139C + receiving method is a brand new buffer method that can significantly reduce the cost caused by CPU data reception. it is suitable for large servers and suitable for IP, TCP, UDP and other data downloading methods, as well as connecting networks such as 802.1p, 802.1Q, and VLAN. in the 8139CP, there are 64 consecutive receiving/sending descriptor units, for three different ring buffer queues, one is the queue of high-priority transmission descriptor, the other is the queue of common priority transport operator description, and the other is the queue of receiver operator description, each ring buffer queue is composed of 64 consecutive descriptors with 4 dual characters on the right. Each descriptor consists of 4 consecutive dual characters. the starting address of each descriptor is aligned at the position of 256 bytes, before receiving data, the software needs to allocate a DMA buffer in advance. generally, for transmission, the buffer size is up to 8 Kbytes and the physical address is linked to the DMA address description unit of the descriptor, in addition, there are two double-character units that indicate the receiving status of the corresponding DMA buffer.
In/driver/net/8139CP. C, the data units of the ring buffer queue descriptor are as follows:
Struct cp_desc {u32 opts1;/* buffer state controller, including the buffer size and buffer transmission start bit */u32 opts2;/* dedicated for VLAN */u64 addr; /* The DMA address of the buffer */};
NIC interruption of 8139CP: Static irqreturn_t
Cp_interrupt (int irq, void * dev_instance, struct pt_regs * regs)
{
Struct net_device * dev = dev_instance;
Struct cp_private * cp = dev-> priv;
2010status;
/* Check for interruption arrival in rx-ring */
Status = cpr16 (IntrStatus );
If (! Status | (status = 0 xFFFF ))
Return IRQ_NONE;
If (netif_msg_intr (cp ))
Printk (KERN_DEBUG "% s: intr, status % 04x cmd % 02x cpcmd % 04x \ n ",
Dev-> name, status, cpr8 (Cmd), cpr16 (CpCmd ));
/* Clear NIC interrupt controller content */
Cpw16 (IntrStatus, status &~ Cp_rx_intr_mask );
Spin_lock (& cp-> lock );
/* The receiving status register indicates that a packet has arrived */
If (status & (RxOK | RxErr | RxEmpty | rx1_oovr )){
/* Mount the current disconnected NIC device to the POLL queue in softnet_data and wait for applications on the upper network to process it */
If (netif_rx_schedule_prep (dev )){
/* Disable reception interruption enabling */
Cpw16_f (IntrMask, cp_norx_intr_mask );
_ Netif_rx_schedule (dev );
}
}
/* Handle the sending interrupt and handle the 8139C + special soft interrupt. here we don't care */
If (status & (TxOK | TxErr | TxEmpty | SWInt ))
Cp_tx (cp );
/* If the link changes, check that the carrier status of the media-independent interface (MII) also changes. Otherwise, you must restart the MII interface */
If (status & LinkChg)
Mii_check_media (& cp-> mii_if, netif_msg_link (cp), FALSE );
/* If an error occurs in the PCI bus, you need to reset the device 8139C + */
If (status & PciErr ){
B2pci_status;
Pci_read_config_word (cp-> pdev, PCI_STATUS, & pci_status );
Pci_write_config_word (cp-> pdev, PCI_STATUS, pci_status );
Printk (KERN_ERR "% s: PCI bus error, status = % 04x, PCI status = % 04x \ n ",
Dev-> name, status, pci_status );
/* TODO: reset hardware */
}
Spin_unlock (& cp-> lock );
Return IRQ_HANDLED;
}
Hanging NIC on POLL queue (poll_list) In the interrupt program of 8139CP, we can see the call method of _ netif_rx_schedule. it hangs the NIC device on the poll_list queue in the softnet_data structure to return the interrupt in time, let's take a look at the internal workflow of _ netif_rx_schedule for processing the special packet processing bottom-half.
Local_irq_save (flags );
Dev_hold (dev );
/* Put the current NIC device in the POLL (poll_list) queue and wait until the soft interrupt is awakened for polling */
List_add_tail (& dev-> poll_list, & __ get_cpu_var (softnet_data). poll_list );
/* Determine the size of the package to be received by the current device */
If (dev-> quota <0)
Dev-> quota + = dev-> weight;
Else
Dev-> quota = dev-> weight;
/* Start the soft interrupt. in the soft interrupt field _ softirq_pending in irq_cpustat_t, set the soft interrupt location 1 for network round robin, run the handle net_rx_action of the interrupt when the scheduling time is approaching. */
_ Raise_softirq_irqoff (NET_RX_SOFTIRQ );
Local_irq_restore (flags );
}
Analysis of the handling process of soft interruptions started by _ netif_rx_schedule Before the soft interrupt event is triggered, subsys_initcall (net_dev_init), called at the time of the device subsystem initialization, was activated on the soft interrupt console and hung on the task queue tasklet to run it at the time of schedule scheduling, in this example, the most important part is to call the POLL method (dev-> poll) of the 8139C + network device to obtain data from the rx-ring queue of the network device, it should have been executed in the network device interrupt service program, as we explained earlier, the POLL method places the space-for-time mechanism in the soft interrupt part to execute the round robin mechanism (using a mechanism similar to the old Bottom-half can also achieve the same effect, and it is easier to understand) during each process scheduling, the network device is interrupted, and the rx-ring round robin is used to receive NIC data.
Soft interrupt handling process: Static void net_rx_action (struct softirq_action * h)
{
Struct softnet_data * queue = & __ get_cpu_var (softnet_data );
Unsigned long start_time = jiffies;
Int budget = netdev_max_backlog;/* indicates the maximum length of the queue */
/* Lock the current thread. processing cannot be interrupted by other processors in the case of multiple processors */
Preempt_disable ();
Local_irq_disable ();
/* Check whether there are devices in the POLL queue (poll_list */
While (! List_empty (& queue-> poll_list )){
Struct net_device * dev;
/* Ensure that the time of executing the current POLL process does not exceed one time slice, so that it will not take too much time by soft interruption. in this way, the current POLL process is executed within one scheduling time, budget indicates the "number of parts" for the maximum data transmission in a time slice. the number of sk_buff completed by each POLL is determined by the number of sk_buff in the middle of each slice is dev-> quota, in the 8139CP driver, the budget is 300, and the quota is 16, which indicates that each time slice is given a maximum of 4.8 K sk_buff */
If (budget <= 0 | jiffies-start_time> 1)
Goto softnet_break;
Local_irq_enable ();
/* Obtain the device structure waiting for the round robin from the round robin queue in the public softnet_data data structure */
Dev = list_entry (queue-> poll_list.next,
Struct net_device, poll_list );
/* Call the device's POLL method to read data from the Ring Buffer on the NIC */
If (dev-> quota <= 0 | dev-> poll (dev, & budget )){
/* Finish receiving data during a POLL process, and redefine the "quota" of the device's received data (in fact, it is the number of sk_buff buffers, the number of sk_buff buffers that can be created and submitted to the upper layer at most each time the POLL method is called. this parameter is very important for careful optimization of this value during high-speed processing, when receiving a large amount of data, you need to increase the value )*/
Local_irq_disable ();
List_del (& dev-> poll_list );
List_add_tail (& dev-> poll_list, & queue-> poll_list );
If (dev-> quota <0)
Dev-> quota + = dev-> weight;
Else
Dev-> quota = dev-> weight;
} Else {
/* If an error occurs in receiving data, or the specified quota is not completed, and no new data is received, the transmission process may be completed, call _ netif_rx_complete to clear network devices from the POLL queue (detailed introduction to the POLL process )*/
Dev_put (dev );
Local_irq_disable ();
}
}
Out:
Local_irq_enable ();
Preempt_enable ();
Return;
Polling method in 8139CP driver Dev-> poll method:
This method is usually called by the network layer when it obtains new data packets from the received cyclic queue of the driver, the number of packages that can be delivered to the network layer in the driver's receiving loop queue is expressed in the dev-> quota field. let's look at the POLL prototype in 8139cp:
Static int cp_rx_poll (struct net_device * dev, int * budget)
The number of data packets that need to be passed at the underlying layer for the upper-layer task of the budget parameter. this value cannot exceed the value of netdev_max_backlog.
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