Kernel: 2.6.34
TCP is the most widely used transport layer protocol, which provides the connection-oriented and reliable byte-throttling service, but it is precisely because of these characteristics that TCP is more complex than UDP, and it is divided into two parts [creation and use]. This article mainly includes the creation of TCP and the process of shaking hands three times.
When programming, you typically create a TCP Socket with the following statement:
Sockets (Af_inet, SOCK_DGRAM, ipproto_tcp)
Starting from this analysis, call interface [NET/SOCKET.C]: syscall_define3 (socket)
Two-step key operations are performed: Sock_create () and SOCK_MAP_FD ()
retval = sock_create (family, type, protocol, &sock);
if (retval < 0)
goto out;
retval = SOCK_MAP_FD (sock, Flags & (O_cloexec | O_nonblock));
if (retval < 0)
Goto out_release;
Sock_create () is used to create a SOCKET,SOCK_MAP_FD () map to a file descriptor, so that the socket can be accessed through FD, focusing on the creation process of the sock_create ().
Sock_create ()-> __sock_create ()
From the __sock_create () code, you see that the creation consists of two steps: Sock_alloc () and Pf->create (). Sock_alloc () allocates the sock memory space and initializes the Inode;pf->create () to initialize SK.
Sock =
Sock_alloc ();
Sock->type = type;
..... PF = rcu_dereference (net_families[family]);
..... Pf->create (NET, sock, Protocol, Kern);
Sock_alloc ()
Allocates space, allocates nodes (including sockets) through New_inode (), then obtains sock through Socket_i macros, in fact the inode and Sock are distributed together in New_inode (), and the structure is called Sock_alloc.
Inode = New_inode (SOCK_MNT->MNT_SB);
Sock = Socket_i (inode);
Sets the parameters of the Inode and returns sock.
inode-
>i_mode = S_ifsock | S_irwxugo;
Inode->i_uid = Current_fsuid ();
Inode->i_gid = Current_fsgid ();
Return sock;
Continue to look down on the specific creation process: New_inode (), after the allocation, will set the I_ino and I_state values.
struct Inode *new_inode (struct super_block *sb)
{
...
Inode = Alloc_inode (SB);
if (inode) {
spin_lock (&inode_lock);
__inode_add_to_lists (SB, NULL, inode);
Inode->i_ino = ++last_ino;
inode->i_state = 0;
Spin_unlock (&inode_lock);
}
return inode;
}
The Alloc_inode ()-> Sb->s_op->alloc_inode (), SB is SOCK_MNT->MNT_SB, so Alloc_inode () points to the SOCKFS action function Sock_ Alloc_inode.
static const struct
super_operations sockfs_ops = {
. Alloc_inode = Sock_alloc_inode,
. Destroy_inode =sock_ Destroy_inode,
. Statfs = Simple_statfs,
};