Path for Linux Device Driver engineers-basic principles and framework of network device drivers

Source: Internet
Author: User

Path for Linux Device Driver engineers-basic principles and framework of network device drivers

K-Style

Reprinted please indicate from Hengyang Normal College 08 electric 2 K-Style http://blog.csdn.net/ayangke,QQ:843308498 mailbox: yangkeemail@qq.com

 

1. Linux network subsystem

At the top of the Linux network subsystem is the system call interface layer. It provides a socket Method for applications provided by the user space to access the kernel network subsystem ). Under it is a protocol-independent layer, which provides a common method to use the transport layer protocol. Then the implementation of specific protocols, including kernel protocols TCP, UDP, and IP in Linux. Then there is the device-independent layer, which provides a common interface for communication between the Protocol and the device driver. The bottom is the device driver.

Device-independent interfaces connect protocols with various network drivers. This layer provides a set of common functions for the underlying network device drivers to operate on the high-level protocol stack. To generate data from the protocol layer to the device, you need to call the dev_queue_xmit function. This function queues the data and submits it to the hard_start_xmit method of the underlying driver for final transmission. The netif_rx is usually used for receiving. When the underlying device program receives a packet (which is interrupted), it will call netif_rx to upload the data to the device-independent layer.

Architecture from device independent layer to driver layer

2. network device description (structnet_device)

 

Each network device is described by struct net_device. The following kernel functions can be used for dynamic allocation.

Struct net_device * alloc_net (intsizeof_priv, const char * mask, void (* setup) (struct net_deive *))

Sizeof_priv indicates the size of the private data zone. mask indicates the device name and setup indicates the initialization function. When registering the device, this function is called. That is, the init member of net_deivce.

Struct net_device * alloc_etherdev (intsizeof_priv)

The difference between this function and the above function is that the kernel knows that it will treat the device as an Ethernet device and perform related initialization.

The net_device structure can be divided into five parts: Global members, hardware-related members, interface-related members, device method members, and public members.

Major global members

Char name [INFAMSIZ]

Device name, for example, eh % d

Unsigned long state

Device status

Unsigned long base_addr

I/O base address

Unsigned int irq

Interrupt number

 

Main device methods include:

Int (* init) (struct net_device * dev)

Initializes the net_device structure by calling register_netdev.

Int (* open) (struct net_device * dev)

Open the interface. When ifconfig is activated, the interface will be opened

Int (* stop) (struct net_deivce * dev)

Stop interface, called when ifconfig eth % down

Int (* hard_start_xmit) (struct sk_buf * skb, struct net_device * dev)

Data sending Function

INT (* do_ioctl) (struct net_deive * Dev, struct ifreq * IFR, int cmd)

Process the interface-specific IOCTL command (sock_ioctl) for calling.

INT (* set_mac_address) (struct net_device * Dev, void * ADDR)

To change the MAC address, hardware must support this function.

 

Network device registration

The registration method of a network device differs from that of a character driver because it does not have a primary/secondary device number and uses the following function to register a device.

Int register_netdev (struct net_deivce * Dev)

Logout of network devices

Void unregister_netdev (struct net_device * Dev)

 

3. Network Packet description (sk_buff)

 

Each network packet in the Linux kernel is described by a socket buffer structure structsk_buff. Each sk_buff structure is a packet, and the pointer to sk_buff is usually called SKB.

Important data member in sk_buff

Struct device * dev; process the device in the package

_ U32 sadd; r // ip address

_ U32 daddr; // ip address

_ U32 raddr; // IP router address

 

Unsigned char * head; // start of space allocation

Unsigned char * data; // start of valid data

Unsigned char * tail; // end of valid data

Unsigned char * end; // end of the allocated space

Unsigned long Len; // The length of valid data

Sk_buff operation

Struct sk_buff * alloc_skb (unsigned intlen, int priority)

Allocate a sk_buff structure for the protocol stack code.

Struct sk_buff * dev_alloc_skb (unsignedint Len)

Allocate a sk_buff structure. Used by driver code

Void kfree_skb (struct sk_buff * SKB)

Void dev_kfree_skb (struct sk_buff * SKB)

Release the sk_buff Structure

Unsigned char * skb_push (struct sk_buff * SKB, int Len)

Move the Data Pointer Forward To The LEN Length. And return the value after moving. Adds a data header to the front end of the valid data area of SKB ).

Unsigned char * skb_put (struct sk_buff * SKB, int Len)

Move the taill pointer backward to The LEN Length and return the value before tail moves. Adds data to the end of the valid data area of SKB.

4. Driver implementation

 

1). Initialization (init)

Device Detection is carried out in the init method. Generally, a function called probe method is called.

During initialization, the system checks the device, configures and initializes the hardware, and finally applies for these resources from the system. In addition, fill in the dev structure of the device. We call the ether_setup method provided by the kernel to set some default Ethernet settings.

 

2) Open)

Open this method is called when the network device is activated in the network device driver (that is, the device status changes from down to up)

In fact, a lot of initialization work can be done here. For example, resource application and hardware activation. If Dev-> open returns a value other than 0, the hardware status remains down.

 

3) Stop)

The stop method is opposite to the open method.

Some resources can be released to reduce the burden on the system.

The stop operation is called when the device status changes from up to down.

 

4) Send (hard_start_xmit)

When the system calls the driver's hard_start_xmit, the data sent is placed in a sk_buff structure. The general driver is sent to the hardware. Some special devices, such as loopback, make up the data to receive data and directly discard the data to the system or dummy device.

If the message is sent successfully, the hard_start_xmit method releases sk_buff. If the device cannot be processed temporarily, for example, if the hardware is busy, 1 is returned.

 

5) receive

The driver has an acceptance method. When data is received, the driver calls the netif_rx function to deliver the SKB to the device-independent layer.

Generally, an interruption occurs when the device receives the data. In the Interrupt Processing Program, the driver requests a piece of sk_buff (SKB) to read the data from the hardware to the buffer of the application number.

Then fill in some information in sk_buff.

The interruption may be due to the receipt of data or the completion of sending. The interrupt processing program should judge the interruption type. If the interruption is due to the receipt of data, the program will start to receive data. If the interruption is due to the completion of sending, then, the operation after sending is processed, such as restarting the sending queue.

 

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.