Linux Network Driver programming (4)

Source: Internet
Author: User
Article title: Linux Network Driver programming (4 ). 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.
3. notes for compiling Linux network drivers
  
3.1 interrupt sharing
  
In Linux, several devices run to share the same interrupt. If you need to share, specify the sharing method when applying. Definition of system-provided request_irq () call:
  
Int request_irq (unsigned int irq,
Void (* handler) (int irq, void * dev_id, struct pt_regs * regs ),
Unsigned long irqflags,
Const char * devname,
Void * dev_id );
  
If the sharing is interrupted, irqflags sets the SA_SHIRQ attribute so that other devices can apply for the same interruption. Note that this attribute must be set for all devices that use this interrupt when calling request_irq. When the system calls back each interrupt handler, you can use the dev_id parameter to find the corresponding device. When the system calls back each interrupt handler, you can use the dev_id parameter to find the corresponding device. Generally, dev_id is set to the device structure. The system uses the dev_id parameter to call each interrupt handler in sequence.
  
3.2 Handling of busy hardware sending
  
The processing capability of the master CPU is generally faster than that of the network. Therefore, the system usually needs to send data, but the last packet of data network devices has not been sent. In Linux, network device drivers generally do not cache data. data that cannot be sent is a notification that the system fails to send the data, therefore, there must be a mechanism to notify the system in time when the hardware is not busy and then send the following data.
  
Generally, the process of sending busy messages has been described in the sending method (hard_start_xmit) of the previous device. that is, if sending is busy, set tbusy to 1. After processing the sent data, clear tbusy in the sending end interrupt, and use mark_bh () to call the notification system to continue sending.
  
However, when implementing my driver, I found that such a processing system does not seem to be able to know that the hardware is idle in time, that is, after mark_bh, the system will wait for a while before sending. The sending efficiency is very low. The usage of 2 m lines is less than 10%. The kernel version is 2.0.35.
  
My final implementation is not to set tbusy to 1, so that the system always considers the hardware idle, but the report is not sent successfully. The system will always try again. In this way, the processing will run normally. However, the network driver in the kernel source code does not seem to be processed in this way. I don't know where the problem is.
  
3.3 flow control)
  
Traffic control is required for sending and receiving network data. These controls are implemented in the system and do not need to be implemented by the driver. Each device data structure has a parameter dev-> tx_queue_len, which indicates the maximum number of cached data packets sent. In Linux, the Ethernet device (10/100 Mbps) indicates the maximum number of cached data packets sent. In Linux, the Ethernet device (10/100 Mbps) tx_queue_len is generally set to 100, and the serial line (asynchronous serial port) is 10. In fact, if you can see the source code, setting dev-> tx_queue_len does not apply for space to cache the data. This parameter is only used to determine whether the data in the sending queue reaches the limit of tx_queue_len when the packets at the protocol layer are received, so as to determine whether the data in the packet is added to the sending queue. Another aspect of throttling during sending is the sending window of the higher-level protocol (there is a sending window in the TCP protocol ). When the window size is reached, the high-level protocol will not send data again.
  
The receiving traffic control is also divided into two levels. There are limits on the cached data packets of netif_rx. In addition, the high-level protocol also has a maximum amount of data to be processed.
  
The sending and receiving traffic control processes are in do_dev_queue_xmit () and netif_rx () of net/core/dev. c.
3.4 debugging
  
Many Linux drivers are compiled into the kernel to form a large kernel file. However, this is quite troublesome for debugging. You can use the module method to debug the driver. The driver that supports the module method must provide two functions: int init_module (void) and void cleanup_module (void ). Init_module () is called when this module is loaded. in this function, register_netdev () can be used to register the device. If init_module () is returned, 0 indicates success, and negative indicates failure. Cleanup_module () is called when the driver is uninstalled. it clears the occupied resources and calls unregister_netdev ().
  
The module can be dynamically loaded and detached. In version 2.0.xx, the kerneld automatic loading module is also available, but kerneld has been canceled in version 2.2.xx. Manually load and use the insmod command, uninstall and use the rmmod command, and check that the modules in the kernel use the lsmod command.
  
Compile the driver using gcc, the main command line parameter-DKERNEL-DMODULE. And as the driver loaded by the module, it is only compiled into the obj form (with The-c parameter added ). Put the compiled target file in/lib/modules/2.x. xx/misc and load it with insmod in the startup file.
  
4. Further reading
  
Linux program design materials can be obtained from the Internet. This is the benefit of open source code. There is no "undisclosed secret ". The main materials I read when writing the driver include:
Linux kernel source code
The Linux Kernel Hackers Guide by Michael K. Johnson
Linux Kernel Module Programming Guide by Ori Pomerantz
Device drivers in Linux by olly in BBS
You can select a template as the start. the kernel source code contains a network driver template, drivers/net/skeleton. c. Contains the basic content of the driver. However, this template is intended for Ethernet devices. Ethernet processing has special "benefits" in Linux systems. if it is not an Ethernet device, pay attention to some details, mainly in the initialization program.
  
Finally, it is probably the most effective help to listen to the experiences of other developers by referring to programs written by others.
  
  
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.