Linux Network card Driver Learning (five) (delivery process)

Source: Internet
Author: User

Function interface

Device initialization function

Network device drivers exist as kernel modules in the Linux kernel, and corresponding to the initialization of modules, an initialization function is required to initialize the hardware registers of the network devices, configure the DMA, and initialize the relevant kernel variables. The device initialization function is called when the kernel module is loaded, and its function is as follows:

static int __init xx_init (void) {    ...}  Module_init (xx_init);   This statement indicates that the Xx_init function is called automatically when the module is loaded

The device initialization function mainly accomplishes the following functions:

1. Hardware initialization

Because the network device is mainly divided into PHY, MAC and DMA three hardware modules, the developer needs to initialize these three modules separately.

    1. Initializes the PHY module, which includes setting the duplex/half duplex operation mode, device operating rate, and self-negotiation mode.

2. Kernel variable Initialization

Initialize and register the kernel device. The kernel device is a variable that has a property of Net_device, and the developer needs to apply for that variable's space (via the Alloc_netdev function), set variable parameters, hook up interface functions, and register the device (via the Register_netdev function).

The common hook interface functions are as follows:

Net_device *dev_p;  Dev_p->open              = Xx_open;   Device Open function Dev_p->stop              = xx_stop;   Device Stop function dev_p->hard_start_xmit = XX_TX;     Data send function Dev_p->do_ioctl          = xx_ioctl;//other control functions ...

Data transceiver Functions

Data reception and transmission is the most important part of the network device drivers, for users, they do not need to know what network equipment used by the current system, how to send and receive network equipment, all these details for the user is blocked. Linux uses sockets as a bridge for connecting users and network devices. The user can operate the socket via the Read/write function, and then interact with the specific network device through the socket to perform the actual data sending and receiving work.

Linux provides a type of data interface called Sk_buff, the data that the user passes to the socket is first saved in the sk_buff corresponding buffer, and the SK_BUFF structure is defined in the Include/linux/skbuff.h file. The structure in which it holds the packet is shown below.

Figure 4. Sk_buff Data structure diagram

1. Data transmission Process

When the user calls the socket to start sending data, the data is stored in the Sk_buff type of cache, and the sending function of the network device (the Hard_start_xmit registered in the device initialization function) is also called, as shown in the flowchart.

Figure 5. Data transmission flowchart
  1. The user first creates a socket and then calls write functions such as write to access the network device through the socket, while saving the data in a buffer of type Sk_buff.
  2. The socket interface calls the network device send function (hard_start_xmit), Hard_start_xmit has been hooked up to a specific send function similar to XX_TX during initialization, XX_TX mainly implements the following steps.
    1. Remove an idle BD from the Send BD table.
    2. Modify the BD properties according to the data saved in Sk_buff, one is the data length and the other is the packet cache pointer. It is important to note that the packet cache pointer must correspond to a physical address, because DMA can only identify the physical address that stores the data cache when it obtains the corresponding data in the BD.
      Bd_p->length = skb_p->len;  Bd_p->bufptr = Virt_to_phys (Skb_p->data);
    3. Modify the status of the BD to be ready, and the DMA module will automatically send the corresponding data in the ready-to-state Bd.
    4. Move the pointer that sends the BD table toward the next BD.
  3. The DMA module starts sending data in the ready-to-use BD cache to the network and automatically resumes the BD when the send is complete.

2. Data reception Process

When the network device receives the data, the DMA module automatically saves the data and notifies the processor to fetch it, and when the processor detects that the data is received by means of interruption or polling, the data is saved to the Sk_buff buffer and read through the socket interface. The flowchart is shown below.

Figure 6. Data reception flowchart
  1. After the network device receives the data, the DMA module searches for the BD table, takes out the idle BD, and automatically saves the data to the BD's cache, modifies the BD as the ready state, and simultaneously triggers the interrupt (this step is optional).
  2. The processor can check the status of the receiving BD table by interrupting or polling, either way, and they need to implement the following steps.
      1. Remove an idle BD from the receiving BD table.
      2. If the current BD is ready, check the current BD data status and update the data receive statistics.
      3. The data from the BD is saved in the Sk_buff buffer.
      4. The status of the update BD is idle.
      5. Move the pointer to the receiving BD table pointing to the next Bd.
  3. The user invokes read functions such as read, reads the data from the Sk_buff buffer, and releases the buffer.
Interrupts and polling

The Linux kernel has two options for receiving data, one is interrupt mode and the other is polling mode.

Interrupt mode

If you choose the interrupt mode, you must first register the interrupt class model and interrupt handler that corresponds to the interrupt before using the driver. The network device driver will attach the specific Xx_open function to the drive's open interface at initialization, the steps of the Xx_open function hook interrupt are as follows.

REQUEST_IRQ (RX_IRQ, Xx_isr_rx, ...);  REQUEST_IRQ (TX_IRQ, Xx_isr_tx, ...);

The interruption of network equipment is generally divided into two kinds, one is send interrupt, the other is receive interrupt. The kernel needs to register both of these interrupt class models separately.

    1. The job of sending interrupt handler (XX_ISR_TX) is to monitor data sending status, update data sending statistics and so on.
    2. The receiving interrupt handler (XX_ISR_RX) works mainly to receive data and pass it to the protocol layer, monitor the data receiving status, update the data receiving statistics, etc.

In the case of interrupt mode, because each packet receives an interrupt, and the processor quickly jumps into the interrupt service program to process the packet, the interrupt receive mode is high-real-time, but if the packet traffic is large, excessive interruptions can increase the load on the system.

Polling method

If you use polling, you do not need to enable the interrupt state of the network device, and you do not need to register an interrupt handler. The operating system will specifically open a task to check the BD table periodically, if the current pointer pointed to the BD is not idle, then the BD corresponding data is taken out, and restore the BD idle state.

Because of the principle of task timing check, the real-time polling method is poor, but it does not interrupt the overhead of that system context switch, so polling is more efficient when processing large traffic packets.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Network card Driver Learning (five) (delivery process)

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.