Network protocol stack 13: Link Layer of Connect Function Decomposition

Source: Internet
Author: User

After the skb_buff data packet is transferred from the IP layer to the link layer, the link layer starts to process the data packet.

 

First, check whether the skb_buff data packet is not in the skb_buff linked list. If the data packet is still in (that is, skb_buff-> next! = NULL), it indicates that there is a problem with the above processing, the code should be avoided (Human avoidance, the code is still faulty), that is, the data packet cannot be sent. The processing method is, the device that sends the specified data packet is specified as null, that is, the data packet is not actually sent by the device.

 

Step 2: Determine whether the next hop MAC address is known, namely SKB-> ARP = 1. If not, call arp_find to find the MAC address corresponding to the IP address. If not, then, the message is returned directly and no longer sent.

 

Step 3: If the previous two steps are normal, it indicates that the data packet is normal. In this case, to determine whether the data is a local data packet that has just been downloaded, the data packet is queued again at the priority level, there are three priorities, each of which is a queue. data packets are inserted at the end of the queue with the corresponding priority.

 

Step 4: obtain a data packet from the top-priority queue header. This data packet may not be the data packet that has just been downloaded. If the data packet to be downloaded is larger than the sent data packet or has a re-transmitted data packet, the data packet obtained at this time is the previous data packet.

 

Step 5: Send the obtained data packet (call the sending function in the NIC Driver to send the data packet. This is the start of the NIC Driver). If the data packet is sent successfully, it is returned directly, if the packet fails to be sent, insert the packet to the header of the queue with the corresponding priority. When the packet is sent later, it is the first packet to be sent.

As there are various network interfaces, there is no way to abstract them. Therefore, when the driver of the network adapter is developed, you only need to call the corresponding upper-layer functions and then integrate the functions of the network adapter into the network protocol stack, complete route protocol stack.

 

 

In step 2, if the MAC address corresponding to the destination IP address does not exist (or the next hop), arp_find is used to find the MAC address corresponding to the corresponding IP address. The general process is as follows:

ARP will have an ARP cache locally. The so-called ARP cache adopts an array and linked list (or Queue) method in implementation. Each ARP cache table item is represented by an arp_table structure, therefore, each element in the array points to a linked list of the arp_table structure type. Addressing specific array elements is obtained by the resolved IP address through the hash algorithm. Therefore, the process of querying the ARP cache can be expressed as: first, the arp_table Structure linked list pointing to the corresponding element in the resolved IP address index array, and then traverse the linked list to match the IP address, if you are looking for a table that exactly matches an IP address, this table item is returned. If no table item is found, it indicates that no corresponding table item exists in the current ARP cache, at this time, the system creates a new ARP table item and sends an ARP request packet. The currently sent packets are cached in the packet cache queue set for this table item, when an ARP response packet is received, on the one hand, the original table item is created (the hardware address field is initialized), and on the other hand, all the data packets to be sent cached in the table item are sent out. The structure is as follows:

 

 

 

The arp_table struct is as follows:

 

Struct arp_table

{

The struct arp_table * Next;/* next field is used for concatenating the arp_table structures to form a linked list. */

 

Unsigned long last_used;/* last_used field indicates the time when the table item was last used */

 

The unsigned int flags;/* flags field is used to maintain some flag spaces of ARP table items, such as the current status of the table items. It can be used to prepare for extended functions of ARP cache table items. */

 

Unsigned long IP;/* the IP field indicates the IP address in the ing relationship indicated by the table entry */

Unsigned long mask;/* The Mask field is the network mask of the corresponding IP address. */

 

Unsigned char ha [max_addr_len];/* the HA field indicates the hardware address in the ing relationship */

Unsigned char hlen;/* hlen is the hardware address length */

Unsigned short htype;/* htype is the hardware address type */

 

Struct device * dev;/* Dev field indicates the network device bound to the ARP table entry. For hosts with only one network interface, of course, only one transmission channel is available for sending all data packets, however, for hosts with two or more network interfaces, there are multiple possible options for data packets. If you query the sending interface every time you send a message, the system overhead is unnecessary, since the creation of the Link Layer header in data frame creation requires hardware address resolution, that is, querying the ARP cache, a pointer to the corresponding sending interface is maintained in the ARP table item, you can solve the problem of which network interface to send data packets when creating data frames. In addition, for hosts with multiple network interfaces, each network port is connected to a different network, while the remote host may only belong to one network, therefore, when creating an ARP table item, we can know which network the host belongs to and initialize the network port device pointer connected to the network. It can be seen that the dev field value in the ARP cache table item comes from the route table item. The route table item is either manually configured or created based on the running routing protocol. Based on the network interface that receives the route data packet, We can initialize the network port device field in the route table item. */

 

Struct timer_list timer;/* the timer field is used to regularly resend arp request packets to prevent ARP requests from being responded. */

Int retries;/* Number of resends. The current maximum number of resends is 3, represented by the arp_max_tries variable */

 

Struct sk_buff_head SKB; /* The last field SKB indicates that the cached data packet queue is temporarily used because the IP address has not been resolved to the hardware address. The specific usage of this field is described in the following section when the function is introduced. */

 

};

 

The ARP cache array is defined as follows:

# Define arp_table_size 16

# Define full_arp_table_size (arp_table_size + 1)

Struct arp_table * arp_tables [full_arp_table_size] =

{

Null,

};

 

 

The system manages ARP cache through the above struct and array.

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.