linux网络协议栈初始化Series---(7)

1595阅读 0评论2010-09-18 mtloveft
分类:LINUX

声明:本文为原创
#####请转贴时保留以下内容######
作者GTT
本文档归属http://oldtown.cublog.cn/.转载请注明出处!
请提出宝贵意见Mail:mtloveft@hotmail.com
Linux Version:2.6.33
提示本文是介绍linux 网络协议栈初始化
 
下面继续介绍网络设备层的初始化方法。也就是net_dev_init
 
dev_proc_init() dev_mcast_init()会涉及到proc file system,所以暂时先不解释这两个方法。
 
register_pernet_subsys(&netdev_net_ops)在网络命名中注册子系统。
netdev_net_ops定义如下

static struct pernet_operations __net_initdata netdev_net_ops = {
    .init = netdev_init,
    .exit = netdev_exit,
}

 
以前文章介绍过register_pernet_subsys这个方法,最后会执行.init方法即netdev_init
再来看看netdev_init的定义

/* Initialize per network namespace state */
static int __net_init netdev_init(struct net *net)
{
    INIT_LIST_HEAD(&net->dev_base_head);

    net->dev_name_head = netdev_create_hash();
    if (net->dev_name_head == NULL) goto err_name;

    net->dev_index_head = netdev_create_hash();
    if (net->dev_index_head == NULL) goto err_idx;

    return 0;

err_idx:
    kfree(net->dev_name_head);
err_name:
    return -ENOMEM;
}

初始化两个hash表,这两个hash表被保存在net命名空间里,一个是根据网络设备名称进行检索的hash表,
另一是根据网络设备index进行检索的hash表。
netdev_create_hash()方法很简单,就是在kernel space 申请内存,作为hash表的空间。然后初始化hash表。

static struct hlist_head *netdev_create_hash(void)
{
    int i;
    struct hlist_head *hash;

    hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
    if (hash != NULL)
        for (i = 0; i < NETDEV_HASHENTRIES; i++)
            INIT_HLIST_HEAD(&hash[i]);

    return hash;
}

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



上一篇:linux网络协议栈初始化Series---(6)
下一篇:linux网络协议栈初始化Series---(8)