Chinaunix首页 | 论坛 | 博客
  • 博客访问: 788501
  • 博文数量: 264
  • 博客积分: 592
  • 博客等级: 中士
  • 技术积分: 1574
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-24 22:02
文章分类

全部博文(264)

文章存档

2019年(2)

2018年(1)

2017年(1)

2016年(4)

2015年(14)

2014年(57)

2013年(88)

2012年(97)

分类: LINUX

2014-02-12 15:57:13

转:http://blog.csdn.net/nerdx/article/details/12797197
  1. //  路由缓存内存量指定办法:  
  2. //      1.通过启动参数rhash_entries指定hash表bucket个数  
  3. //      2.根据物理内存页数确定使用的内存量  
  4.   
  5. //  根据物理页数分配缓存内存:  
  6. //      1.goal目标内存页数=总页数/(2**(26-PAGE_SHIFT))  
  7. //      2.最接近goal内存页数的order,用于从伙伴系统中分配  
  8. //      3.rt_hash_mask=order页可容纳的bucket数  
  9. //      4.对齐rt_hash_mask到2的幂次  
  10. //      5.从伙伴系统中分配order页物理内存  
  11.   
  12. //  路由缓存阈值:  
  13. //      1.gc_thresh=bucket个数,当路由缓存数超过此阈值时,rt_garbage_collect同步回收内存  
  14. //      2.ip_rt_max_size=16*(bucket个数),当路由缓存超过此阈值时,dst_alloc会失败  
  15.   
  16. //  路由子系统初始化  
  17. //  调用路径:ip_init->ip_rt_init  
  18. //  函数主要任务:  
  19. //      1.分配dst缓存的slab缓存  
  20. //      2.分配路由缓存表  
  21. //      3.向netdev_chain注册监听块  
  22. //      4.初始化默认路由表  
  23. //      5.初始化路由缓存使用的定时器  
  24. //          5.1 垃圾回收定时器  
  25. //          5.2 缓存刷新定时器  
  26. //          5.3 缓存周期性刷新定时器  
  27. //      6.初始化IPSec与路由子系统交互的衔接点  
  28. 1.1 int __init ip_rt_init(void)  
  29. {  
  30.     int i, order, goal, rc = 0;  
  31.     //  
  32.     rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^  
  33.                  (jiffies ^ (jiffies >> 7)));  
  34.     //dst缓存  
  35.     ipv4_dst_ops.kmem_cachep = kmem_cache_create("ip_dst_cache",  
  36.                              sizeof(struct rtable),  
  37.                              0, SLAB_HWCACHE_ALIGN,  
  38.                              NULL, NULL);  
  39.   
  40.     //根据物理内存数计算路由缓存使用的内存页数  
  41.     goal = num_physpages >> (26 - PAGE_SHIFT);  
  42.     if (rhash_entries)//rhash_entries为启动参数,  
  43.         goal = (rhash_entries * sizeof(struct rt_hash_bucket)) >> PAGE_SHIFT;  
  44.     //计算缓存使用内存页个数以2为底的order  
  45.     for (order = 0; (1UL << order) < goal; order++);  
  46.   
  47.     //rt_hash_mask为bucket的个数  
  48.     do {  
  49.         rt_hash_mask = (1UL << order) * PAGE_SIZE /  
  50.             sizeof(struct rt_hash_bucket);  
  51.         //使rt_hash_mask 对齐到2的幂次  
  52.         while (rt_hash_mask & (rt_hash_mask - 1))  
  53.             rt_hash_mask--;  
  54.   
  55.         //分配路由缓存  
  56.         rt_hash_table = (struct rt_hash_bucket *)  
  57.             __get_free_pages(GFP_ATOMIC, order);  
  58.     } while (rt_hash_table == NULL && --order > 0);  
  59.   
  60.   
  61.     for (rt_hash_log = 0; (1 << rt_hash_log) != rt_hash_mask; rt_hash_log++);  
  62.   
  63.     //初始化每个bucket使用的自选锁  
  64.     rt_hash_mask--;  
  65.     for (i = 0; i <= rt_hash_mask; i++) {  
  66.         spin_lock_init(&rt_hash_table[i].lock);  
  67.         rt_hash_table[i].chain = NULL;  
  68.     }  
  69.   
  70.     //垃圾回收的域值  
  71.     ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);  
  72.     //路由缓存最多保存的缓存个数  
  73.     ip_rt_max_size = (rt_hash_mask + 1) * 16;  
  74.     //per-cpu统计变量  
  75.     rt_cache_stat = alloc_percpu(struct rt_cache_stat);  
  76.   
  77.     //向netdev_chain注册监听块,用netlink为地址和路由命令注册处理函数  
  78.     devinet_init();  
  79.     //初始化默认路由表  
  80.     ip_fib_init();  
  81.     //缓存刷新定时器  
  82.     init_timer(&rt_flush_timer);  
  83.     rt_flush_timer.function = rt_run_flush;  
  84.     //垃圾回收定时器  
  85.     init_timer(&rt_periodic_timer);  
  86.     rt_periodic_timer.function = rt_check_expire;  
  87.     //缓存刷新周期定时器  
  88.     init_timer(&rt_secret_timer);  
  89.     rt_secret_timer.function = rt_secret_rebuild;  
  90.   
  91.     rt_periodic_timer.expires = jiffies + net_random() % ip_rt_gc_interval +  
  92.                     ip_rt_gc_interval;  
  93.     add_timer(&rt_periodic_timer);  
  94.   
  95.     rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +  
  96.         ip_rt_secret_interval;  
  97.     add_timer(&rt_secret_timer);  
  98.   
  99.     //处理路由子系统与IPSec的衔接  
  100. #ifdef CONFIG_XFRM  
  101.     xfrm_init();  
  102.     xfrm4_init();  
  103. #endif  
  104.     return rc;  
  105. }  
  106. //  监听外部事件  
  107. //  调用路径:ip_rt_init->devinet_init  
  108. 1.2 void __init devinet_init(void)  
  109. {  
  110.     //netdev_chian监听块  
  111.     register_netdevice_notifier(&ip_netdev_notifier);  
  112.     //通过netlink为路由命令注册处理程序  
  113.     rtnetlink_links[PF_INET] = inet_rtnetlink_table;  
  114. }  
阅读(731) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~