Code learning in TCP/IP (1)

Source: Internet
Author: User

I would like to wait and read the books again. However, the company will have a seminar later. I will give you some comments!

Open source is widely usedProgramPersonnel progress step by step, just a month to understand the realization of TCP/IP, more and more feel their own shortcomings. The grasp of the overall protocol, the division of tasks, the conversion between the kernel and the user, and the lock settings all require artistic solutions, and even a macro and a joint are unexpected.

Read from the beginning again, with respect, into the master'sCode.

 

In the TCP/IP Implementation of bsd4.4, the buffer for data transmission is named mbuf. This structure requires the following functions:

1. the header information of each level should be easily added.

2. save large-capacity content. Upload

3. Delivery plug-in information

4. Mark the group header.

At the same time, the layers of buffer generation, deletion, and modification are not fixed. For example, an Ethernet driver can call m_devget to cache a frame, it may also be the sendto request cache to save the content sent by the plug-in layer. The processor priority needs to be carefully designed.

First, let's take a look at the standard structure:

Every mbuf is a standard 128 byte, so that different purposes and types of mbuf can be viewed from a unified perspective, and the specific difference is that the combined structure is used, use flags to interpret data at specific locations.

Mbuf can be divided into the following parts:

[1] header structure m_hdr, which is a uniform type of all types of mbuf (identified by mh_flags.

Including the pointer mh_nextpkt pointing to the next unrelated mbuf chain pointing to the next cached mh_next's own data length its own Data Pointer mbuf usage mbuf type

 

[2] The Group header structure pkthdr. When mbuf is used to store the group header, this structure is used to locate the total group length stored in a specific location and the IFNet structure of the receiving interface.

 

[3] Cluster Structure-starting address of a cluster: an unused pointer and the total size of the Cluster

With [2] and [3], you can complete the four main types of mbuf --

Pure data content: Flags = 0 and m_databuf [mlen] In m_dat

The header of the short datagram group: m_pkthdr joins mh_pkthdr in m_dat and mh_databuf [mhlen] In mh_dat.

Excluding the first big data: m_ext

The Group header that contains more data: m_ext/m_pkthdr, mh_pkthdr in m_dat, and mh_databuf [mhlen] In mh_dat.

At the same time, there is a very good example of macro definition. Because the general structure is abstracted as a public struct, the traditional use will become M. m_hdr.mh_next.

Unified, using the simplified technology: "We will see that this technology is widely used in net/3.Source code, As long as a structure contains other structures or joins this situation"

# Define m_pkthdr m_dat.mh.mh_pkthdr

......

 

To my eye-catching point:

Mget is a macro used to allocate a cache zone. If the allocation fails, call the following code-call the drain function of the protocol, which may generate new available space, then try to allocate the cache again.

 
StructMbuf*
M_retry (I, T)
IntI, T;
{
StructMbuf*M;
M_reclaim ();
# DefineM_retry (I, T) (struct mbuf *) 0
Mget (M, I, T );
# UNDEFM_retry
Return(M );
}

here, the mget macro is used again to apply for a buffer. Prevent new expansion from calling m_retry again. Define m_retry as a null pointer, call mget, and restore it In the next statement.

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.