Structure of the source code of the latest Linux stable kernel 2.4.x network interface (1)
Source: Internet
Author: User
Article title: Linux Latest stable kernel 2.4.x network interface source code structure (1 ). 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.
Author: Li Yuanjia
I. Preface
In the Linux source code, the implementation of network interfaces is worth reading. by reading the source code, we not only have a deeper understanding of network protocols, but also help in network programming, more accurate understanding and grasp of application functions.
This article focuses on the overall structure of the network interface program, hoping to serve as some guiding texts when reading the source code.
This article takes Linux2.4.16 kernel as the object to explain, the kernel source code can be downloaded on the http://www.kernel.org. When I read the source code, refer to the example.
2. Structure of network interface program
Linux network interfaces are divided into four parts: Network Device Interface, network interface core, network protocol family, and network interface socket layer.
The network device interface is mainly responsible for receiving and sending data from physical media. The implemented file is under the linu/driver/net directory.
The core part of the network interface is the key part of the entire network interface. It provides a unified sending interface for the network protocol and Shields various physical media, at the same time, it is responsible for distributing packets from lower layers to appropriate protocols. It is the central part of the network interface. Its main implementation files are in the linux/net/core directory, where linux/net/core/dev. c is the main file for management.
The network protocol family is part of the implementation of various specific protocols. Linux supports TCP/IP, IPX, X.25, AppleTalk, and other protocols. the source code of various protocols is named in the linux/net/directory. Here we mainly discuss the TCP/IP (IPv4) protocol. the Source Code is linux/net/ipv4, and linux/net/ipv4/af_inet.c is the main file management.
The network interface Socket layer is the programming interface for network services provided by users. The main source code is in linux/net/socket. c
III. Network Device Interface
There are many different types of network interface devices on the physical layer. in the 28 lines of the file include/linux/if_arp.h, the identifier of the various physical devices that ARP can process is defined. Network Device interfaces are responsible for controlling the specific physical media, receiving and sending data from the physical media, and setting various physical media such as the maximum data packet. Here we take the simple 3Com3c501 NIC driver as an example to explain how this layer works. The source code is in Linux/drivers/net/3c501. c.
We intuitively consider that the most important thing for a network card is to receive and send data. here we will look at the process of receiving and sending data.
Sending is relatively simple, in Linux/drivers/net/3c501. the el_start_xmit () function starting with row 475 in c is the function that actually sends data to the 3Com3c501 Ethernet card. the specific sending work is nothing more than reading and writing some registers. the Source Code comment is clear, let's take a look.
Receiving is relatively complicated. Generally, when a new package arrives or a package is sent, an interruption occurs. Linux/drivers/net/3c501. in the el_interrupt () function starting from 572 in c, the first half processes the report after the packet is sent, and the second half processes a new package, that is, new data is received. The el_interrupt () function does not perform too much processing on the new package, so it is handed over to the receiving handler el_receive (). El_receive () first checks whether the received package is correct. if it is a "good" package, a buffer structure (dev_alloc_skb () will be allocated to the package ()), in this way, the driver receives the package and calls the netif_rx () (net/core/dev. c1214 rows), and send the package to the upper layer.
Now the driver has the ability to send and receive data. how can the driver establish a connection with the upper layer? That is to say, after receiving the package, how can we send it to the upper layer and how can the upper layer call the sending function of the driver?
The bottom-up relationship is the netif_rx () (net/core/dev. c 1214 rows) implemented by the function. The driver uses this function to send the received data to the upper layer. Note that all Nic drivers need to call this function, this is the bridge between the core layer of the network interface and the network interface device.
The relationship from top to bottom is complex. The core layer of the network interface needs to know how many network devices can be used, and the function entry address of each device must be known. The core layer of the network interface shouted, "Hey, how many devices can send packets for me? If you can send the message to me, please make a team !". This team started with dev_base. the pointer structnet_device * dev_base (Linux/include/linux/netdevice. h row 436) is to save all the devices known to the core layer of the network interface. For the core layer of the network interface, all devices are in a net_device structure. h, line 233 is defined as an abstract device from the perspective of the core layer of the network interface. let's take a look at the features of the network device from the perspective of the core layer of the network interface:
If the core layer of the network interface needs to send data from the lower layer, after dev_base finds the device, it directly calls the dev-> hard_start_xmit () function to send data packets to the lower layer.
The driver needs to let the core layer of the network interface know its own existence. of course, it needs to add the pointer chain pointed to by dev_base, and then map its own functions and various parameters to the corresponding fields in net_device. The pointer chain to which dev_base points is added through the register_netdev (& dev_3c50) function (linux/drivers/net/net_init.c, line 532)
Created. The relationship between your function and the corresponding fields and various parameters in net_device is established in Elasticsearch probe1 () (Linux/drivers/net/3c501. c:
Further corresponding work is carried out in ether_setup (dev) (drivers/net/net_init.c, line 405. We noticed dev-> hard_start_xmit = & el_start_xmit. in this way, the relationship between the sending function is established. the upper layer only knows to call dev-> hard_start_xmit to send data, the above statement tells the upper layer of the actual sending function of the driver.
IV. core parts of network interfaces
I just talked about how the driver connects to the core layer of the network interface. The core layer of the network interface knows that the function entry of the driver and the driver is directed to the device chain through * dev_base, and the lower layer is called by the function netif_rx () of this layer () (net/core/dev. c
1214 rows) pass the data to this layer.
The upper layer of the core layer of the network interface is the specific network protocol, and the lower layer is the driver. we have resolved the relationship between the lower layer and the upper layer, but the relationship has not been resolved. First, we will discuss the relationship between the core layer of the network interface and the network protocol family. this relationship is essentially the relationship between receiving and sending.
Network protocols, such as IP and ARP, transmit data packets to this layer when sending data packets. what functions does this transfer take place? The network interface core layer uses dev_queue_xmit () (net/core/dev. c, line975) this function provides a unified sending interface to the upper layer. that is to say, whether it is an IP address or an ARP protocol, this function transmits the data to this layer, you can call this function when you want to send data. Dev_queue_xmit () is executed in dev-> hard_start_xmit (), while dev-> hard_start_xmit () calls the actual driver to complete the task of sending. For example, in the above example, calling dev-> hard_start_xmit () actually calls el_start_xmit ().
Now we will discuss the receipt situation. The core layer of the network interface receives data sent from the upper layer through the function netif_rx () (net/core/dev. c 1214). at this time, the data packets must be sent to the upper layer. All the lower-layer protocols of the protocol family need to receive data, TCP/IP IP protocol and ARP protocol, and SPX/IPX protocol, appleTalk's DDP and AARP protocols both need to receive data directly from the network interface core layer. how does the network interface core layer send packets to these protocols? In this case, the relationship with the lower layer is very similar. There may be many Nic drivers under the core layer of the network interface. in order to know how to send data to these drivers, the previous and later sections are outdated, the link is resolved through the * dev_base pointer. the link to the upper layer is resolved through static struct packet_ptype_base [16] (net/core/dev. c line 164. This array contains the protocol for receiving data packets and the entry to their receiving functions.
From the above, we can see that the IP protocol receives data through the ip_rcv () function, while the ARP protocol uses arp_rcv, the core layer of the network interface only needs to pass the data through this array to the upper-layer function.
If you want to add yourself to this array through a protocol, you can use the dev_add_pack () (net/core/dev. c, line233) function to delete the array through the dev_remove_pack () function. The Ip layer registration is performed in the initialization function void _ init ip_init (void) (net/ipv4/ip_output.c, line 1003)
Go back to our discussion about receipt. the netif_rx () (net/core/dev. row c) receives the data sent from the upper layer to see what this function has done.
Because the service is still interrupted, nothing can be processed too much, and the rest is done through cpu_raise_softirq (this_cpu, NET_RX_SOFTIRQ)
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.