内核中的UDP socket流程(3)(4)——sock_create

来源:互联网 发布:状态机编程的优缺点 编辑:程序博客网 时间:2024/05/16 14:23
好了,闲话少说。上次看到了sys_socket调用sock_create的地方了。下面开始研究sock_create了。
sys_socket将自己的参数family, type, protocol传给sock_create,而sock为sock_create的输出值。

retval = sock_create(family, type, protocol, &sock);


下面看sock_create的代码

int sock_create(int family, int type, int protocol, struct socket **res)
{
    return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
}

sock_create不过是一个包装函数,它通过__sock_create真正的去创建socket。
其中,current是当前task的指针——实际上current是一个宏,这个宏在linux内核中已经存在很久了。task的类型为struct task_struct。nsproxy是它的一个成员变量,是task的命名空间指针,下面是它的定义:

struct nsproxy {
    atomic_t count;
    struct uts_namespace *uts_ns;
    struct ipc_namespace *ipc_ns;
    struct mnt_namespace *mnt_ns;
    struct pid_namespace *pid_ns;
    struct net      *net_ns;
};

与咱们有关的就是最后一个指针,net_ns。命名空间namespace是linux在2.6中引入的特性,是为了虚拟化而做的。具体定义可以参考http://oldwiki.linux-vserver.org/Namespaces
uts_ns: UTS命名空间包含了运行内核的名称、版本、底层体系机构类型等信息。UTS是UNIX Timesharing System的缩写;
ipc_ns:保存所有与进程通信(IPC)有关的信息
mnt_ns:保存已mount的文件系统信息;
pid_ns:有关进程ID的信息;
net_ns:这是与咱们有关的,包含所有网络相关的命名空间。
这是第一个参数。
最后一个参数,则表明这个创建的socket是否为kernel层创建的。对于我们来说,这个socket是为应用层创建的,所有最后一个参数是0.

下面开始分析__sock_create

static int __sock_create(struct net *net, int family, int type, int protocol,
             struct socket **res, int kern)
{
    int err;
    struct socket *sock;
    const struct net_proto_family *pf;

    /*
     * Check protocol is in range
     */

    if (family < 0 || family >= NPROTO)
        return -EAFNOSUPPORT;
    if (type < 0 || type >= SOCK_MAX)
        return -EINVAL;

     /*
     * Skip some codes
     */

}

首先这个函数,先对family和type进行检查,查看是否超出正常范围

    /* Compatibility.

     This uglymoron is moved from INET layer to here to avoid
     deadlock in module load.
     */

    if (family == PF_INET && type == SOCK_PACKET) {
        static int warned;
        if (!warned) {
            warned = 1;
            printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
             current->comm);
        }
        family = PF_PACKET;
    }

    err = security_socket_create(family, type, protocol, kern);
    if (err)
        return err;

首先是对PF_INET,SOCK_PACKET的兼容性处理。security_socket_create函数是一个空函数,省略。

下面是去申请一个socket node,我们需要进入sock_alloc中看如何分配的节点。

    /*
     *    Allocate the socket and allow the family to set things up. if
     *    the protocol is 0, the family is instructed to select an appropriate
     *    default.
     */

    sock = sock_alloc();
    if (!sock) {
        if (net_ratelimit())
            printk(KERN_WARNING "socket: no more sockets\n");
        return -ENFILE;    /* Not exactly a match, but its the
                 closest posix thing */

    }



下面是sock_alloc的代码

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

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

static struct socket *sock_alloc(void)
{
    struct inode *inode;
    struct socket *sock;

    inode = new_inode_pseudo(sock_mnt->mnt_sb);
    if (!inode)
        return NULL;

    sock = SOCKET_I(inode);

    kmemcheck_annotate_bitfield(sock, type);
    inode->i_mode = S_IFSOCK | S_IRWXUGO;
    inode->i_uid = current_fsuid();
    inode->i_gid = current_fsgid();

    percpu_add(sockets_in_use, 1);
    return sock;
}

其中sock_mnt是一个全局变量,它在sock_init中被初始化的。并挂载到VFS层上。在sock_alloc函数中,首先是从sock_mnt->mnt_sb即socket的super block中申请一个节点。然后通过SOCKET_I宏,取得inode对应的socket的地址。
这里之所以这样做的,原因是因为,inode的分配是统一的,但是每个inode的用途是不一样的。
在socket中,申请的inode的节点,实际上对应的结构应该为下面这个结构。

struct socket_alloc {
    struct socket socket;
    struct inode vfs_inode;
};

new_inode中实际上是申请了一个struct socket_alloc的地址,但是返回的确是socket_all->vs_inode的地址。所以在这里需要使用SOCKET_I宏,来从inode地址中,得到socket的地址。

后面的宏kmemcheck_annotate_bitfield是一个内存检查,略过。然后,设置inode的mode,uid,gid。
percpu_add(sockets_in_use, 1)是增加sockets_in_use的统计计数——这个计数变量是每个cpu的私有变量。

然后,我们就回到__sock_create。

    rcu_read_lock();
    pf = rcu_dereference(net_families[family]);
    err = -EAFNOSUPPORT;
    if (!pf)
        goto out_release;

    /*
     * We will call the ->create function, that possibly is in a loadable
     * module, so we have to bump that loadable module refcnt first.
     */

    if (!try_module_get(pf->owner))
        goto out_release;

    /* Now protected by module ref count */
    rcu_read_unlock();

通过RCU机制,获得pf(family)对应的net_families中的指针。

下面通过调用函数指针调用用户指定family的函数去创建socket。

    err = pf->create(net, sock, protocol, kern);
    if (err < 0)
        goto out_module_put;

对于TCP/IP来说,family是PF_INET。

现在又需要跳到文件linux/net/ipv4/af_inet.c,下面是PF_INET对应的协议域定义

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

所以对于对于TCP/IP的socket来说,sock_create中这里的create函数实际上指向的是inet_create——这个函数明天再说,继续sock_create的代码。

    /*
     * Now to bump the refcnt of the [loadable] module that owns this
     * socket at sock_release time we decrement its refcnt.
     */

    if (!try_module_get(sock->ops->owner))
        goto out_module_busy;

    /*
     * Now that we're done with the ->create function, the [loadable]
     * module can have its refcnt decremented
     */

    module_put(pf->owner);
    err = security_socket_post_create(sock, family, type, protocol, kern);
    if (err)
        goto out_sock_release;
    *res = sock;

    return 0;

这几行代码的注释已经很清楚,到此sock_create已经完结。

原创粉丝点击