Bkjia.com exclusive Article] preface introduces the concepts involved in this article:
1) snull:Is a virtual network model that can be used to understand the operation of the driver of a real physical network interface device.
2)The code in this article isStandard C LanguageFormat.
3) * skb:The important pointer volume points to a memory area to buffer incoming and outgoing network packets to be processed.
Linux Network Interface Device Overview
In Linux, network interfaces are one of the three standard devices. The other two types are character devices and Block devices. The following describes how the driver of the network interface device interacts with the kernel module. The Network Device Driver asynchronously receives network data packets from the external world and pushes the incoming data packets to the kernel through the push operation. The block Device Driver sends a piece of data buffer to the kernel, drivers all need to register the feature information of the corresponding device to a specific data structure in the kernel. The block device driver can be described in the file format, however, network drivers cannot use normal file read/write operations. network device drivers have their own memory namespace and use push and other operations to complete packet conversion and delivery.
In the Unix world, "Everything is a file" is not applicable to network device interfaces. A block device can find a specific file entry mark under the/dev directory of the System File tree, while a network device does not have such a file operation entry, hundreds of sockets on a physical network interface for network data exchange can be reused by applications.
For example, there is a packet operation interface specifically designed for the network device driver in the kernel. The Network Interface sn0 reaches the network snullnet0, which is the meaning of the route. Use the route add-net snullnet0 dev sn0 command to complete the process. If more than 2.2 of the Linux kernel does not need to do so, the kernel will automatically be added. If the network is not a class C address, use the mask to specify 255.255.255.0 to specify a class C network, because the address mechanism is modified in the snull experimental network model, therefore, packets sent to 192.168.0.33 will be delivered to machines 192.168.1.33 in snullnet1 through the sn0 interface, and packets sent to other network addresses will be discarded by the sn1 interface. when it comes to physical data transmission, the snull network model is in the Ethernet category.
Due to the widespread use of Ethernet, even the plip interface of the print protocol also claims that it belongs to the Ethernet device category. You can also use tcpdump to view the network data trends in the snull experimental network model. When the tcpdump tool is used to load the snull device driver in the 2.0 kernel, the specified Eth = 1 item must be displayed. Note that the snull network model can only be used to test packets of the IP network protocol. The source code of the snull module must be modified to transmit non-IP network data packets using the snull network model. Otherwise, other non-IP network data packets will be damaged.
Core operations of the Snull Model
There are more than 11 core snull operations. They are:
Ether_setup, open, stop, set_config, hard_start_xmit, do_ioctl, get_stats, rebuild_header, tx_timeout, watchdog_timeo, flags, hard_header_cache, SET_MODULE_OWNER, etc. |
Note: The snull network does not process arp packets because the IFF_NOARP flag in core operations. ARP is an underlying Ethernet protocol standard. The task is to map the IP address to the access control address of the Ethernet physical media, that is, the MAC address. Because the network machine simulated by the snull network does not need to process the MAC address, it does not. Similarly, hard_header_cache does not need to be processed. It is set to NULL in the snull model.
How to operate network devices
The following describes how to perform specific operations on network devices. Before a network interface can operate data packets, the kernel must open the interface and assign the memory address. You can use the ifconfig command to do this. First, allocate the address through ioctl, and then open the IFF_UP flag, at this time, the driver function is not used, but the kernel is executing. When the slot input/output interface flag is opened, the open method is called to open the device interface. The method for closing an interface is the same as that for closing an interface. If the execution is successful, 0 is returned; otherwise, a negative number is returned.
The actual code of the driver completes many operations similar to char or block types. open calls and allocates system resources, stop and release system resources. Before the interface communicates with the outside world, you need to copy the address on the physical device to the dev-> dev_addr variable. The snull model driver uses the ASCII string ETH_ALEN to create a physical NIC address.
The open method also enables a transmission queue. The Kernel provides a function to implement a queue:
Void netif_start_queue (struct net_device * dev );
In addition, the snull model code is similar to the following:
int snull_open(struct net_device *dev) {MOD_INC_USE_COUNT; memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN); dev->dev_addr[ETH_ALEN-1] += (dev - snull_devs); netif_start_queue(dev); return 0; } |
Because there are no real hardware network devices in the snull model, there is not much code in the open method. The stop method is the same:
Int snull_release (struct net_device * dev) {/* release the port and irq, similar to fops-> close */netif_stop_queue (dev);/* cannot transmit data anymore */MOD_DEC_USE_COUNT; return 0 ;} |
There is a conflict between how to enable and disable interfaces.
The most important task of a network interface: sending and receiving network packets
Next, let's talk about the most important task to be done by the Network Interface: sending and receiving network packets. The sending package is easy to understand. First, in the advanced network layer, each package belongs to a network socket with a specific number and uses the sk_buff structure variable to list the incoming packets. The slot buffer (sk_buff) structural variables run through the entire linux network. The specific definition can be found in the linux/skbuff. h file.
The pointer to the sk_buff variable is usually called skb. The slot buffer is a complex structure. the kernel is responsible for providing a certain number of action functions to operate the structure, start to call the hard_start_transmit method function to get the packets to be sent in and out of the station queue. hard_start_xmit contains the address of the packet on the physical media. skb-> data points to the packet being transmitted, skb-> len is the length of the package in octal notation. The transfer code in the snull model is isolated from the driver code of the actual physical Nic. The transfer code of the snull package is as follows:
Int snull_tx (struct sk_buff * skb, struct net_device * dev) {int len; char * data; struct snull_priv * priv = (struct snull_priv *) dev-> priv; len = skb-> len <ETH_ZLEN? ETH_ZLEN: skb-> len; data = skb-> data; dev-> trans_start = jiffies;/* Save the timestamp */priv-> skb = skb; /* Remember the buffer pointer to interrupt the release at any time. The actual transmitted data is related to the device. */snull_hw_tx (data, len, dev); return 0;} is omitted here ;} |
So how to control transmission in concurrency? When the buffer is temporarily fully loaded, it must be paused through netif_stop_queue, and void netif_wake_queue (struct net_device * dev) will be used to continue the restart. The memory allocated by the device may contain multiple packets at the same time.
If the action set by a mechanism is not completed within the specified time, it is deemed to have exceeded the time. An error occurred. The related monitoring function is watchdog_timeo, which is in the net_device structure, jiffies is a time control table. Then, the tx_timeout method is called for processing. Once a timeout occurs, the driver code must be marked with an error. If an error occurs in the snull model, functions such as snull_interrupt and netif_wake_queue will be called.