看源代码,发现路由最初初始化的函数在net/ipv4/route.c
int __init ip_rt_init(void)
{
int rc = 0;
#ifdef CONFIG_NET_CLS_ROUTE
ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct));
if (!ip_rt_acct)
panic("IP: failed to allocate ip_rt_acct\n");
#endif
/*既然是全局变量,肯定很重要*/
ipv4_dst_ops.kmem_cachep =
kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
/*创建struct rt_hash_bucket 类型的哈希表*/
rt_hash_table = (struct rt_hash_bucket *)
alloc_large_system_hash("IP route cache",
sizeof(struct rt_hash_bucket),
rhash_entries,
(num_physpages >= 128 * 1024) ? 15 : 17,
0,
&rt_hash_log,
&rt_hash_mask,
0);
memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
rt_hash_lock_init();
/*垃圾回收阀值*/
ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
ip_rt_max_size = (rt_hash_mask + 1) * 16;
/*主要的初始化函数*/
devinet_init();
ip_fib_init();
/* All the timers, started at system startup tend
to synchronize. Perturb it a bit.
*/
schedule_delayed_work(&expires_work,
net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
if (register_pernet_subsys(&rt_secret_timer_ops))
printk(KERN_ERR "Unable to setup rt_secret_timer\n");
if (ip_rt_proc_init())
printk(KERN_ERR "Unable to create route proc files\n");
#ifdef CONFIG_XFRM
xfrm_init();
xfrm4_init();
#endif
/*注册rtnetlink 消息, 这里是获取路由, 在devinet_init()中还有其他三种消息*/
rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
#ifdef CONFIG_SYSCTL
register_pernet_subsys(&sysctl_route_ops);
#endif
return rc;
}
/*
* allocate a large system hash table from bootmem
* - it is assumed that the hash table must contain an exact power-of-2
* quantity of entries(假定哈希表包含2的指数级的表项)
* - limit is the number of hash buckets, not the total allocation size
* (limit 是哈希桶的数量, 不是所分配的整个空间的大小)
*/
void *__init alloc_large_system_hash(const char *tablename,
unsigned long bucketsize,
unsigned long numentries,
int scale,
int flags,
unsigned int *_hash_shift,
unsigned int *_hash_mask,
unsigned long limit)
void __init devinet_init(void)
{
/*还是初始struct net结构中的部分变量*/
register_pernet_subsys(&devinet_ops);
register_gifconf(PF_INET, inet_gifconf);
/*注册一个通知链,主要处理设备的状态改变*/
register_netdevice_notifier(&ip_netdev_notifier);
/*另外的两个消息*/
rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);
}
这是ip_netdev_notifier的初始化值:
static struct notifier_block ip_netdev_notifier = {
.notifier_call = inetdev_event,
};
ip_fib_init()在上一篇中已经介绍过!
阅读(2909) | 评论(1) | 转发(0) |