Linux network drivers

Source: Internet
Author: User

Linux the network system is mainly based on BSD UNIX socket mechanism. Data structure Sk_buff is defined between the system and the driver for data transmission. The system supports sending and receiving data caches, providing a flow control mechanism and providing support for multiple protocols.

1. Linux Architecture of the network driver


Linux the architecture of the network driver is divided from top to bottom 4 layers, the functions of each layer are as follows:

(1) The network Protocol interface layer provides a unified packet transceiver interface to the network layer protocol, regardless of the upper IP or is ARP , all through Dev_queue_xmit function to send data and pass Net_rx function to receive data. The presence of this layer allows the upper level protocol to be independent of the specific device.

(2) The Network Device Interface layer provides a unified structure for describing the properties and operations of specific network devices to the Protocol interface layer Net_dev , Net_dev is the container for each function in the device-driven function layer. The network protocol interface layer plans the structure of the device driving function layer of the specific operation hardware from the macroscopic.

(3) the function layer of the device drive function is the network device interface layer Net_dev the specific member of the data structure is the program that drives the network device hardware to complete the corresponding action. initiates a send operation through the HARD_START_XMIT function and triggers the receive operation through interrupts on the network device.

because the reception of network packets is caused by interrupts, another body part in the device driver layer is the interrupt handler function, the interrupt handler is responsible for reading the packets received on the hardware and passing it to the upper layer protocol, which may contain Xxx_interrupt and the Xxx_rx function. xxx_interrupt function completes the basic work such as interrupt type judgment, xxx_rx function completes the data packet generation and submits the upper layer and other complex work.

(4) The network device and the media layer are the physical entities that complete the packet sending and receiving, including the network adapter hungry for the specific transmission medium, which is physically driven by the function in the device drive function layer. In linux , network devices and media can be virtual.

in the Linux all network systems in the system are abstracted as an interface, and the structure struct Net_device to indicate the operation of the network device in the kernel, that is, the network device interface. The network device interface includes both the Pure Software network device interface and the hardware network device interface. All network devices are managed through a list of devices linked with dev_base as the head pointer. Each element in the device's linked list represents a network interface. The structure net_device contains a number of device methods for system access and protocol layer invocation, including the init function (device initialization and system registration),theOpen function (open Network device),stop function (turn off network device),hard_start_xmit Functions (handling packet sending) and interrupt handling functions.

2. Initialization of network devices

the initialization of network devices is mainly struct Net_device in the Init the function pointer points to the initialization function to complete. The init function is called when the kernel boots or loads the network driver module . the init function determines whether a network physical device exists by detecting the hardware characteristics of the physical device before deciding whether to enable the driver. The device is then configured to allocate resources, such as interrupts and I/O space, to the system after it is configured . Finally , the corresponding member variables of Net_device are initialized so that a network device can be used by the system.

for a newly added device, you need to construct the device's Net_device data structure, and then to Linux The kernel registers the device and requests memory space.

3. Sending and receiving of data packets

when the network device is activated, call the Net_device in the Open function pointer, open the network device, and use the Net_device in the Hard_header function pointers to establish hardware frame header information. the Hard_start_xmit in net_device is then invoked through the dev_queue_xmit Protocol interface layer function . The function pointer completes the sending of the packet. the hard_start_xmit function is responsible for sending data stored in a socket buffer (sk_buff) to a physical device.

in general, the device is subjected to data that generates an interrupt and the driver requests a block when the interrupt is processed. Sk_buff , read the data from the hardware and put it in the requested buffer, then populate Sk_buff some of the information. Finally , the netif_rx function is called to pass the received packet to the upper layer of the network protocol for processing.

Net_device open Net_device Hard_header Dev_queue_xmit style= color:black " protocol interface Layer function Style= "Color:black" > Net_device Hard_start_ XMit The function pointer completes the sending of the packet. hard_start_xmit sk_buff

in general, the device receives data and generates an interrupt when the driver requests a piece of Read the data from the hardware and put it in the requested buffer, then populate the sk_buff Netif_rx

The sk_buff struct, or socket buffer, is used to pass data between layers in a Linux network subsystem. When sendinga packet, the network processing module of theLinux kernel must establish a Sk_buffcontaining the packet to be transmitted, and then the sk_buff submitted to the lower level, each layer in Sk_buff add different protocol headers to the network device to send. When the network device receives the packet from the network medium, the received data must be loaded into the sk_buff structure and passed to the upper layer, and the layers are removed from the corresponding protocol headers until they are delivered to the user.

4. module loading for network devices

Network device driver loading mode has kernel load and module loading two ways. The system kernel has a process for loading network devices, or you can Add new network devices to the system via the Module_init macro. Here we mainly discuss the module load of network equipment, the basic process is:

(1) through Module_init macro-Modified functions are called when the module is loaded (or system-initiated);

(2) after the network device is detected, call the The Register_netdev function adds the device to the end of the system's network device chain list dev_base in a Linux system ;

(3) registration is successful and will be called Net_device in the Init function to initialize the device.

through Register_netdev The function Registration network device is a dynamic loading process, Linux 2.6 in the kernel, all network devices are loaded with this function. Module loading does not exist to reserve space for network devices, thus saving system memory resources.

5. Net_device Structural Details

netdevice.h files are saved in Include/linux in the catalog

(1) Global Information

Char                   Name[ifnamsiz];

The device name. The assigned number starts from zero.

unsigned long state          ;

Device status. Contains several identities.

struct Net_device      *next;

A pointer to the next device in the global list.

(2) Hardware Information

Unsignedlong           mem_end;unsigned long          mem_start;
Device memory information. The start and end addresses of shared memory values used by the device are saved

unsigned long          base_addr;

The I/O base address of the network interface.

unsigned int           IRQ;

The interrupt number that was given.

unsigned char          if_port;

Specifies which port to use on the multiport device.

unsigned char          DMA;

The DMA channel assigned to the device. Used to display information (ifconfig command).

(3) Device Method

Only the methods required to use the interface are listed below.

int                     (*open) (struct net_device*dev);

Open the interface. When the ifconfig Activates the interface, the interface is opened. the open function should register all system resources, turn on the hardware, and perform other required settings on the device.

int                     (*stop) (struct net_device*dev);

Stop the interface. The function performs the opposite of the open function.

int                     (*hard_header) (struct Sk_buff *skb,                         structnet_device *dev,                         unsigned short type,                         void *daddr,                         void *saddr,                         unsigned len);
The function establishes a hardware header based on the source and destination hardware addresses that were previously retrieved. The function task is to organize the information that is passed as a parameter into the appropriate hardware header that is unique to the device.Eth_headeris the default function of the Ethernet type interface,Ether_setupassign the member a value ofEth_header.

int                     (*rebuild_header) (Structsk_buff *skb);

This function is used to complete the data packet before it is transmitted. ARP after parsing, re-establish the hardware header.

  void                    (*tx_timeout) (Structnet_device *dev);

If the transmission of the packet fails for a reasonable period of time, the method is called if the loss of the interrupt or the interface is locked. Tx_timeout is responsible for resolving the problem and restarting the transmission of the packet.

int                     (*set_config) (Structnet_device *dev,                                      struct ifmap *map);
Change the interface configuration. This function is the entry point for configuring the driver. UseSet_configThe device can be changed during operation.I/Oaddress and interrupt number.

struct net_device_stats* (*get_stats) (struct net_device*dev);

This function is called when the application needs to obtain statistics on the interface.

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

Linux network drivers

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.