Linux kernel network stack 3 - create socket

2011-06-17  来源:本站原创  分类:OS  人气:82 

1, examples and function entry:
1) The sample code is as follows:

int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

2) entry:
net / Socket.c: sys_socketcall (), call number according to subsystem, create socket performs sys_socket () function;

2, distribution socket structure:
1) Call Chain:
net / Socket.c: sys_socket () -> sock_create ()->__ sock_create () -> sock_alloc ();

2) create the socket file system i-node:

inode = new_inode(sock_mnt->mnt_sb);

which, sock_mnt root file system for the socket is installed in the kernel socket file system initialization assignment, mnt_sb of the file system mount point of the superblock object pointer;
Here, new_inode function is a common file system functions, its role is in the appropriate file system to create a inode; the main code is as follows (fs / Inode.c):

struct inode *new_inode(struct super_block *sb) {
        struct inode * inode;
        inode = alloc_inode(sb);
        return inode;

distribution function here called alloc_inode inode structure (fs / Inode.c):

static struct inode *alloc_inode(struct super_block *sb) {
        struct inode *inode;

        if (sb->s_op->alloc_inode)
                inode = sb->s_op->alloc_inode(sb);
                inode = (struct inode *) kmem_cache_alloc(inode_cachep, GFP_KERNEL);

on top of the conditional: if (sb-> s_op-> alloc_inode), meaning that if the current file system super block inode has its own distribution operation functions, then call its own functions assigned inode, or from the public high-speed allocated a cache inode;

3) Create a socket special inode:
In "socket file system registered" after an article mentioned in the installation of the socket file system is initialized the file system's superblock, then the operation will initialize the superblock pointer s_op to sockfs_ops structure; so at this time inode allocation function calls sock_alloc_inode to complete:

static struct inode *sock_alloc_inode(struct super_block *sb) {
        struct socket_alloc *ei;
        ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
        return &ei->vfs_inode;

From here you can see, in fact, assigned a socket_alloc structure, the structure contains the socket and the inode:

struct socket_alloc {
        struct socket socket;
        struct inode vfs_inode;

but eventually returns to the structure in the inode member; this point, socket structure and distribution of the inode structure are completed; inode allocation, the application can be carried out by the file descriptor of the socket read () / write () of class operation, this is a virtual file system (VFS) to complete.

3, according to the inode get socket object:
Because creating a common file system inode is logical, so the return value is the inode object pointer; but here, after creating the socket's inode, inode needs to be based on socket objects; inline SOCKET_I resulting from:

static inline struct socket *SOCKET_I(struct inode *inode)
        return &container_of(inode, struct socket_alloc, vfs_inode)->socket;

look container_of macro (include / linux / Kernel.h):

#define container_of(ptr, type, member) ({                   \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) );})

and offsetof macro (include / linux / Stddef.h):

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

1) offerset (TYPE, MEMBER) macro function: return TYPE MEMBER member structure of the offset;
Look at an example, suppose there is a structure A as follows:

struct struct_A {
    char a;
    int b;

which members of a structure relative to the offset is 0, members of the b phase offset for the structure of 1; structure struct_A variable m in memory address structure is as follows:

Linux kernel network stack 3 - create socket

We look offset macro:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

can be understood, the 0 address forced into a TYPE structure pointer, and then get MEMBER member's address, the address is exactly equal to the structure TYPE MEMBER member of the offset;
Or take the example above, for instance, as shown, offset (struct_A, b) is 1, exactly equal to its offset;
As shown below:
Linux kernel network stack 3 - create socket

2) container_of (ptr, type, member) macro function: return ptr pointer to the structure; which ptr is the body type of the guitar body member pointer member variables;
Let's look at the implementation:

#define container_of(ptr, type, member) ({                   \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) );})

the ptr pointer into a char *, and then subtracting its offset in the structure, get the ptr the address of where the structure, and finally cast into a type *;

Back sock_alloc function, SOCKET_I socket made according to inode variables, record the current process some information, such as fsuid, fsgid, and increase sockets_in_use value (the number of variables that create socket); created socket variables in __sock_create () function to set the type for the application to pass down to the type, the above example is SOCK_STREAM;

4, using the protocol suite to initialize socket:
1) protocol suite concept:
Agreement protocol suite is composed of more than a communication protocol stack, such as our most familiar with TCP / IP (AF_INET Internet protocol suite), including TCP, IP, ICMP, ARP and other protocols;

2) Linux support for the protocol family:

Linux2.6.26 33 agreement to support domain, the net / Socket.c define global variables:

static const struct net_proto_family *net_families[NPROTO] __read_mostly;

in / include / linux / socket.h defines macros for each protocol domain, occupied by the array for each protocol, a domain, such as AF_INET take net_families [2], if net_families [2] = null, then the current kernel module is not registered AF_INET;

3) The registered domain AF_INET protocol:

In "socket file system register" mentioned in the system initialization, AF_INET registration is also done through this;

Initialization entry net/ipv4/Af_inet.c:

static int __init inet_init(void) {
        //  For high speed to the different socket buffers
        rc = proto_register(&tcp_prot, 1);
        rc = proto_register(&udp_prot, 1);
        rc = proto_register(&raw_prot, 1);
        /*  All socket types type By inetsw management  */
        for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)

        for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)

This call sock_register function to complete the registration:

int sock_register(const struct net_proto_family *ops) {
        int err;
        if (net_families[ops->family])
                err = -EEXIST;
        else {
                net_families[ops->family] = ops;
                err = 0;

according to the AF_INET protocol family domain inet_family_ops registered to the kernel net_families array; Here is its definition:

static struct net_proto_family inet_family_ops = {
        .family = PF_INET,
        .create = inet_create,
        .owner  = THIS_MODULE,

one, family specifies the type of protocol domain, create links to the appropriate protocol domain socket creation function;

4) Socket type

In the same protocol domain, there may be multiple socket types; as there AF_INET domain socket stream (SOCK_STREAM), datagram sockets (SOCK_DGRAM), the original socket (SOCK_RAW), in these three types of socket protocols are built on TCP, UDP, ICMP / IGMP, etc.

In the Linux kernel, that the domain structure struct proto type of a socket, which provides that all operations on the socket type and related data (in the kernel initialization will assign the appropriate high-speed buffer, see above mentioned inet_init function).

AF_IENT domains defined by these three types of socket structure inet_protosw (net/ipv4/Af_inet.c) expressed as follows:

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_sockraw_ops,
               .capability = CAP_NET_RAW,
               .no_check =   UDP_CSUM_DEFAULT,
               .flags =      INET_PROTOSW_REUSE,

which, tcp_prot (net/ipv4/Tcp_ipv4.c), udp_prot (net/ipv4/Udp.c), raw_prot (net/ipv4/Raw.c) represent three types of sockets, socket, respectively, corresponding word of the operations and related data; ops members of all the operations of the protocol field set for three different socket types, there are three different domains operating inet_stream_ops, inet_dgram_ops, inet_sockraw_ops, its definition are located net/ipv4/Af_inet. c lower;

Kernel initialization, in inet_init, the store will be a different socket to the global variable inetsw unified management; inetsw is an array of linked lists, each of which is a struct inet_protosw structure of the list, a total of SOCK_MAX items In inet_init AF_INET domain initialization function when the function is called inet_register_protosw the array inetsw_array all socket types defined in the array registered to inetsw; the same socket type, different types of socket protocols stored through the list In the inetsw array to the socket type for the index, the actual use of the system when using only inetsw, instead inetsw_array;

5) Use to initialize the socket domain

Understanding of the above knowledge, we go back net / Socket.c: sys_socket () -> sock_create ()->__ sock_create () in:

const struct net_proto_family *pf;
pf = rcu_dereference(net_families[family]);
err = pf->create(net, sock, protocol);

above code, locate the kernel initialization domain registration agreement, and then call its create method;

Unfinished, the next blog post be continued;

  • Linux kernel network stack 3 - create socket 2011-06-17

    1, examples and function entry: 1) The sample code is as follows: int server_sockfd = socket(AF_INET, SOCK_STREAM, 0); 2) entry: net / Socket.c: sys_socketcall (), call number according to subsystem, create socket performs sys_socket () function; 2,

  • Linux kernel network stack 4 - Create a socket (continued) 2011-06-17

    Connected to the chapter "Creating socket" article; 5, distribution sock structure: Examples in this article will call the method that inet_create inet_family_ops.create method to complete the creation of socket; its call chain is as follows: ne

  • Linux kernel network stack 7-socket port management 2011-07-05

    First, the antecedent Review On a "socket address binding" referred to in the application delivery over the port in the kernel need to check whether the port is available: if (sk->sk_prot->get_port(sk, snum)) { inet->saddr = inet->rc

  • Linux kernel network stack 5-socket address bind 2011-06-24

    A, socket bound entrance 1, the sample code struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = inet_addr(""); server_address.sin_port = htons(9734); server_len = sizeof(server_address)

  • Linux kernel network stack 8-socket listen 2011-08-02

    A few questions about the following questions <br /> students can ignore the following: 1, listen to do what the main library? 2, What is the maximum number of concurrent connection requests? 3, what is the queue of pending connections? socket liste

  • Linux kernel network stack 1 - socket file system registration 2011-06-14

    First, the registration time 1, the kernel initialization complete; 2, the kernel initialization process (init / main.c): kernel_init () -> do_basic_setup () -> do_initcalls () -> do_one_initcall (); 3, socket file system registration process (ne

  • Linux kernel network stack 6-socket address bind (continued) 2011-06-24

    Connected to the article "socket address binding" Third, depending on the agreement to bind the above code to complete Step 3 is based on the application when creating the socket passed to the kernel socket type and protocol field to determine w

  • Linux kernel network stack 2-socket to the kernel function from the library 2011-06-14

    A, socket () library function to the system calls to the kernel 1, Linux is running the C library glibc; 2, socket () call as follows: 1) socket ()->__ socket (): glibc-2.3.6/sysdept/generic/socket.c (weak_alias (name1, name2)) 2) __socket (): glibc-

  • Linux kernel netlink protocol suite implementation (on) 2011-01-10

    This document Copyleft owned yfydz all use under the GPL, can be freely copied, reproduced, reprinted, please maintain the integrity of the document, for any commercial purposes is strictly prohibited. msn: [email protected] Source: http://yfydz.

  • Linux kernel analysis 2010-08-06

    As the objective of this paper is to introduce Linux kernel and explore its architecture and main components, so first of all recall a brief history of Linux and then examine the Linux kernel high-level architecture, and finally introduce its major s

  • View Linux kernel. CPU. Memory and other components version of the commands and methods 2010-10-14

    This article comes from, " Wong Po's blog "blog, be sure to keep this source View Kernel version: uname-a more / etc / * release more / etc / redhat-release more / proc / version View CPU Information:

  • See the Linux kernel. CPU. Memory and the version of the component commands and methods 2010-10-14

    This article comes from " yellow treasure of the blog "blog, be sure to keep this source Check kernel version: uname-a more / etc / * release more / etc / redhat-release more / proc / version View CPU

  • Linux2.6 kernel network programming epoll 2011-03-18

    (1) Introduction: First of all, I strongly suggest that you read the writings of Richard Stevens "TCP / IP Illustracted Volume 1,2,3" and "UNIX Network Programming Volume 1,2". Although he has left us more than five years, but his book

  • Analysis of the Linux kernel (rpm) 2011-07-21

    linux kernel is a large and complex operating system's core, but despite the huge, but the use of the concept of layered subsystems and good for the organization. In this article, you will explore the general structure of the Linux kernel, and learn

  • linux kernel tcp congestion handling (1) 2010-03-12

    This time we analyze tcp congestion control, we need to know the protocol stack is very conservative, meaning that as long as there is a missing segment have been determined, it will think that the congestion occurred. And now there is another, that

  • linux kernel learning materials 2010-04-02

    Recent self-Linux kernel side of things, the past two days under the shallow of the kernel boot process, in this record. Here, the Linux boot loader is not involved in virtual environment, that system does not run on top of the hypervisor. Linux thro

  • Efficient learning Linux kernel 2010-07-19

    End of the world's grief and octopus brother also retired, and even, if you're in the worships money and women are suddenly a lot less. This is the "Linux kernel cultivation of the Road" in the excellent and Down, china-pub also has started sell

  • linux kernel compilation reserved 2010-09-18

    Reprinted from Now popular ODL (only disk linux) to do the kernel part of those contents of the article does not describe this. I was doing, after the completion of order information. BY THE

  • Linux kernel and driver programming - Linux Proc File System reveal the mystery of 2010-10-12

    Abstract This paper introduces the Linux proc file system in some detail. Proc file system, first introduced the basic concept / stored / read time / loading, and then describes how to use the proc file system reads the system / kernel in a variety o

  • IPSEC Linux kernel implementation (1) --- reserved 2010-11-15

    Copyleft this document owned by yfydz all, the use of GPL, free to copy, reprint, reproduced keep the documents for completeness, for any commercial purposes is strictly prohibited. MSN: [email protected] Source: 1. Introdu