Structure of the source code of the latest Linux stable kernel 2.4.x network interface (2)

Source: Internet
Author: User
Article title: Linux Latest stable kernel 2.4.x network interface source code structure (2 ). 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
  
If you pay attention to the startup information during Linux startup, or run the dmesg command in linux, you can see the output information of this program:
IP Protocols: ICMP, UDP, TCP, IGMP which means that the inet_protos [] array now has the inet_protocol data structure of ICMP, UDP, TCP, and IGMP Protocols, the data structure contains the processing functions of the data they receive.
  
Linux 2.4.16 in linux/include/linux/socket. h defines 32 supported BSDsocket protocols, including TCP/IP, IPX/SPX, and X.25. Each protocol also provides different services, for example, the TCP/IP protocol supports the connection service through the TCP protocol and the UDP protocol supports the non-connection service. it is necessary to provide unified interfaces to users with so many protocols, this consistency is implemented through socket.
  
In the BSD socket network programming mode, a series of unified functions are used to use communication services. For example, a typical TCP communication program is as follows:
  
Sock_descriptor = socket (AF_INET, SOCK_STREAM, 0 );
Connect (sock_descriptor, address ,);
Send (sock_descriptor, "hello world ");
Recv (sock_descriptor, buffer, 0 );
  
The first function specifies the protocol Inet protocol, namely the TCP/IP protocol, and uses connection-oriented services to correspond to the TCP protocol, subsequent operations will be performed using standard functions of socket.
  
We can see two problems from the above. First, the socket layer needs to be based on the protocol family specified by the user (above is AF_INET)
Select a protocol from the following 32 protocols to fulfill user requirements. when the protocol family is determined, it also maps specific services to specific protocols under the protocol family, for example, if you specify a connection-oriented service, the Inet protocol family maps to the TCP protocol.
  
Selecting a user-specified protocol from multiple protocols and handing the specific exit to the selected protocol is essentially the same as connecting the network core layer up and down, so the solution is the same, also through arrays. In Linux/net/socket. c, this array staticstruct net_proto_family * net_families [NPROTO] is defined. The element of the array has been determined. net_families [2] is the TCP/IP protocol, net_families [3]
  
It is the X.25 protocol. the specific protocol is defined in include/linux/socket. h. However, the ops of the data structure net_proto_family of each item is empty, that is, the address of the specific protocol processing function is unknown. The processing function of the protocol establishes a connection with ops through the sock_register () (Linux/net/socket. c) function. for example, the TCP/IP protocol establishes a connection like this:
  
Int _ init inet_init (void) (net/ipv4/af_inet.c)
{
(Void) sock_register (& inet_family_ops );
  
}
  
As long as AF_INET is given (defined as 2 in the macro), you can find the processing function in net_failies [2.
  
The protocol ING is completed, and now the service ING is required. Of course, it is impossible for the upper layer to know what protocols in the lower layer can correspond to specific services. Therefore, such mappings are naturally completed by the protocol family. In the TCP/IP protocol family, this ING is performed through struct
List_head inetsw [SOCK_MAX] (net/ipv4/af_inet.c)
  
This array is mapped. before talking about this array, let's look at another array, inetsw_array [] (net/ipv4/af_inet.c)
  
Static struct inet_protosw inetsw_array [] =
{
{
Type: SOCK_STREAM,
Protocol: IPPROTO_TCP,
Prot: & tcp_prot,
Ops: & inet_stream_ops,
Capability:-1,
No_check: 0,
Flags: INET_PROTOSW_PERMANENT,
},
  
{
Type: SOCK_DGRAM,
Protocol: IPPROTO_UDP,
Prot: & udp_prot,
Ops: & inet_dgram_ops,
Capability:-1,
No_check: UDP_CSUM_DEFAULT,
Flags: INET_PROTOSW_PERMANENT,
},
  
{
Type: SOCK_RAW,
Protocol: IPPROTO_IP,/* wild card */
Prot: & raw_prot,
Ops: & inet_dgram_ops,
Capability: CAP_NET_RAW,
No_check: UDP_CSUM_DEFAULT,
Flags: INET_PROTOSW_REUSE,
}
};
  
We can see that SOCK_STREAM maps to the TCP protocol, SOCK_DGRAM maps to the UDP protocol, and SOCK_RAW maps to the IP protocol. Now you only need to add the three items in inetsw_array to the array inetsw [SOCK_MAX]. the addition is implemented through the inet_register_protosw () function. In inet_init ()
  
(Net/ipv4/af_inet.c.
  
Another thing that needs to be mapped is socket, such as accept, send (),
  
How are the operation functions such as connect (), release (), and bind () mapped? Let's take a look at the TCP entry of the preceding array.
{
Type: SOCK_STREAM,
Protocol: IPPROTO_TCP,
Prot: & tcp_prot,
Ops: & inet_stream_ops,
Capability:-1,
No_check: 0,
Flags: INET_PROTOSW_PERMANENT,
},
  
We can see that this ING is mapped through ops and prot. let's take a look at tcp_prot:
  
Struct proto tcp_prot = {
Name: "TCP ",
Close: tcp_close,
Connect: tcp_v4_connect,
Disconnect: tcp_disconnect,
Accept: tcp_accept,
Ioctl: tcp_ioctl,
Init: tcp_v4_init_sock,
Destroy: tcp_v4_destroy_sock,
Shutdown: tcp_shutdown,
Setsockopt: tcp_setsockopt,
Getsockopt: tcp_getsockopt,
Sendmsg: tcp_sendmsg,
Recvmsg: tcp_recvmsg,
Backlog_rcv: tcp_v4_do_rcv,
Hash: tcp_v4_hash,
Unhash: tcp_unhash,
Get_port: tcp_v4_get_port,
};
Therefore, the ING has been completed. the user calls the connect () function and actually calls the tcp_v4_connect () function. According to this figure, it is much easier to read the source code.
  
Socket layer
  
In the previous section, we talked about most of the things to be discussed at the socket layer. now we only talk about the connection between the socket layer and the user.
  
The system calls socket (), bind (), connect (), accept, send (), and release () in Linux/net/socket. in c, the system calls the implemented function with the prefix of sys _ added to the corresponding function name.
  
Now let's take a look at what happened below when the user calls the socket () function.
  
Socket (AF_INET, SOCK_STREAM, 0) calls sys_socket (), sys_socket (), and then calls socket_creat (), socket_creat () find a suitable protocol family in net_families [] based on the protocol family parameter provided by the user. if the protocol family is not installed, request to install the module of the protocol family, then, call the processing handle of the create () function of the protocol family. According to the AF_INET parameter, inet_creat () is called. in inet_creat (), inetsw [SOCK_MAX]
  
Select the appropriate protocol and assign the Protocol operation set to socket. according to SOCK_STREAM, the TCP protocol is selected,
Inet_creat (){
Answer = inetsw [user requirements Service];
Sock-> ops = answer-> ops;
Sk-> prot = answer-> prot
}
  
So far, it's time for everyone to get through the source code.
  
Related Article

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.