在net中有很多成员,这些都是全局量,它们的初始化在不同的函数进行实现:
[ net/core/net_namespace.c ]
static struct pernet_operations __net_initdata netdev_net_ops = {
.init = netdev_init,
.exit = netdev_exit,
};
/* Initialize per network namespace state */
static int __net_init netdev_init(struct net *net)
{
/* 初始化网络设备列表
* init_net此列表已经在系统启动时进行了初始化
*/
if (net != &init_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;
}
static void __net_exit netdev_exit(struct net *net)
{
kfree(net->dev_name_head);
kfree(net->dev_index_head);
}
上面就是用了pernet_operations结构的方法进行初始化,当调用register_pernet_subsys(&netdev_net_ops)时,就会调用netdev_init。
[ net/core/net-procfs.c ]
static struct pernet_operations __net_initdata dev_proc_ops = {
.init = dev_proc_net_init,
.exit = dev_proc_net_exit,
};
static void __net_exit dev_proc_net_exit(struct net *net)
{
wext_proc_exit(net);
remove_proc_entry("ptype", net->proc_net);
remove_proc_entry("softnet_stat", net->proc_net);
remove_proc_entry("dev", net->proc_net);
}
static int __net_init dev_proc_net_init(struct net *net)
{
int rc = -ENOMEM;
if (!proc_create("dev", S_IRUGO, net->proc_net, &dev_seq_fops)) // /proc/net/dev
goto out;
if (!proc_create("softnet_stat", S_IRUGO, net->proc_net, // /proc/net/softnet_stat
&softnet_seq_fops))
goto out_dev;
if (!proc_create("ptype", S_IRUGO, net->proc_net, &ptype_seq_fops)) // /proc/net/ptype
goto out_softnet;