8139TOO.C Network card driver Simple analysis

Source: Internet
Author: User

Since the development of Linux C, the work is mainly in the application layer, the Nginx and Unbound and other software have some knowledge, but also often on the 2 software to develop two times. have been more interested in this piece of network. It is also curious how the network card receives the message, and how the message is accepted by the application layer. Oneself on the net to study a bit, do a simple summary. to feast on posterity. Basically I think the following sections are divided:

First, the preparation of knowledge

1, PCI equipment is standard, that is, the PCI device must contain the company, equipment and other information in a fixed location, so that the kernel starts to read out, and saved in the struct Pci_dev. Finally, all PCI devices are organized into a linked list structure.

2, the PCI standard stipulates that the PCI device has a configuration area, these areas by the BIOS when the machine starts, write to the PCI device interrupt number, memory mapped address. Because the BIOS is distributed uniformly, all of these resources are not duplicated.

Second, the network card driver initial process

Basically understand the kernel module how to write the person, should understand that Rtl8139_init_module is called when the Insmod command module is loaded, it will call Pci_register_driver, the function is provided by the kernel, to register the PCI device driver. The kernel traverses the list of PCI devices, and, based on the information provided by RTL8139_PCI_TBL, compares the information and, if satisfied, indicates that the driver is used to drive the device. A device can have only one drive, and one driver can drive multiple devices.

The probe function of the pci_driver structure is called when the device is found to be driven. That is, the Rtl8139_init_one function. This function, is to initialize the device, the main need to refer to equipment manufacturers datasheet, operation of various registers. Details do not speak, mainly to let you understand the general, the details can look at the code. When initializing, Dev->netdev_ops = &rtl8139_netdev_ops, which is equivalent to registering a variety of drive handler functions. Where Rtl8139_open is called when ifconfig eth0 up, it will Request_irq (DEV->IRQ, Rtl8139_interrupt, irqf_shared, Dev->name, Dev) ; registration driver. DEV-IRQ is the BIOS assignment. Rtl8139_interrupt is the interrupt handler function.

Third, the network card sends the message process

Rtl8139_start_xmit is called when it is sent. by calling Skb_copy_and_csum_dev (SKB, Tp->tx_buf[entry]), the message is written to the ring buf, which is a ring buffer, typically 4. In other words, when sending, it is to write the message network ring BUF, write full, wait for the next time to send.

Four, the network card accepts the message process

In addition to the online send buffer, there are also accept buffers. When a message comes in, the message is copied to the buffer by the DMA, and then the interrupt is initiated. The final call to the interrupt handler is rtl8139_interrupt he eventually calls __napi_schedule (&TP->NAPI) and adds the current device pool function to the Napi linked list. It is also the Rtl8139_poll function. So far, the message hardware interrupt processing has ended. Then it must be curious that the message has not been processed yet, just put in the buffer. It is actually accepted by interrupting the lower half, which is the soft interrupt soft IRQ. Continue below:

A soft interrupt is a kernel process that hangs when it's not handled. It is capable of being awakened. When it is awakened, it executes the net_rx_action, which traverses the list of links hanging over it, that is, poll_list, which is the rtl8139_poll we registered above. The Rtl8139_poll will eventually call Rtl8139_rx to accept the message, and the function has a while loop that reads the data from the buffer, populates the data into SKB, and finally calls NETIF_RECEIVE_SKB. To send the message up, this function, the end will be based on the use of the Protocol calls the corresponding processing method, if it is an IP packet, will eventually call IP_RCV. After that, it is sent to the application layer. The specific is not a bit to finish. There is nothing to add back.

8139TOO.C Network card driver Simple analysis

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.