这次主要介绍一些ip层管理以及统计相关的东西.
首先来看 long-living ip peer information.
我们知道ip协议是无状态的协议.这里内核为了提升性能.为每个目的ip地址(换句话说,也就是和本机进行通信过的主机)保存了一些信息.
peer子系统一般是被tcp,或者routing子系统所使用.
这个信息的数据结构是inet_peer,它是一棵avl树,每个节点的key就是一个ip地址.由于是avl树,因此每次搜索都是O(lg n):
- struct inet_peer
- {
- ///avl树的左子树和右子树
- struct inet_peer *avl_left, *avl_right;
- ///远端peer的ip地址
- __be32 v4daddr; /* peer's address */
- ///树的高度
- __u16 avl_height;
- ///下一个使用这个peer的包id(我们的包id的选择,就是基于这个域,也就是每次通过传入ip地址,从而得到当前应使用的id(通过inet_getid函数)).
- __u16 ip_id_count; /* IP ID for the next packet */
- ///这个链表包含了所有定时器到期的peer(由于peer初始化的时候内存大小有限制,因此我们就需要定时将在给定时间内没有使用的peer放到这个链表中).这里只有当它的引用计数为0时,才会最终从unused中移除.
- struct list_head unused;
- ///当这个inet_peer元素被加入到unused链表中(通过inet_putpeer)的时间.
- __u32 dtime; /* the time of last use of not
- ///引用计数 * referenced entries */
- atomic_t refcnt;
- ///帧结束的计数器.
- atomic_t rid; /* Frag reception counter */
- ///下面这两个是被tcp使用来管理时间戳的.
- __u32 tcp_ts;
- unsigned long tcp_ts_stamp;
- };
peer子系统的初始化是在inet_initpeers中进行的,它是被ipv4协议的初始化函数ip_init调用的.这个函数的主要任务有三个:
1 allocate一个将要保存inet_peer数据的cache.
2 定义能被inet_peer所使用的最大内存限制.
3 开启gc定时器.
- ///内存限制
- extern int inet_peer_threshold;
- ///cache
- static struct kmem_cache *peer_cachep __read_mostly;
- ///定时器.可以看到它的处理函数是peer_check_expire,我们后面会介绍这个函数.
- static DEFINE_TIMER(peer_periodic_timer, peer_check_expire, 0, 0);
- ///相应的读写锁.
- static DEFINE_RWLOCK(peer_pool_lock);
- void __init inet_initpeers(void)
- {
- struct sysinfo si;
- /* Use the straight interface to information about memory. */
- si_meminfo(&si);
- ///上面是取得系统的一些信息,我们这里主要用到的就是内存信息,因此这里通过总内存大小,来对inet_peer_threshold进行赋值.
- if (si.totalram <= (32768*1024)/PAGE_SIZE)
- inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
- if (si.totalram <= (16384*1024)/PAGE_SIZE)
- inet_peer_threshold >>= 1; /* about 512KB */
- if (si.totalram <= (8192*1024)/PAGE_SIZE)
- inet_peer_threshold >>= 2; /* about 128KB */
- ///create一个cache.
- peer_cachep = kmem_cache_create("inet_peer_cache",
- sizeof(struct inet_peer),
- 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
- NULL);
- ///初始化定时器.
- peer_periodic_timer.expires = jiffies
- + net_random() % inet_peer_gc_maxtime
- + inet_peer_gc_maxtime;
- add_timer(&peer_periodic_timer);
- }
peer系统的核心函数就是inet_getpeer,它是提供给其他子系统的接口,它封装了lookup函数,而loopup函数只是很简单的avl树的查找函数.
而 inet_getpeer函数它通过传入的key(也就是ip地址)和一个flag(比如赋值为create),可以做到,当查找失败后,能创建一个新的树的拣点,并初始化ip包 的id(使用id模块的secure_ip_id)来初始化.
首先来看它的调用图,然后我们再分析整个函数:
这里只有一个要注意的就是我们检测peer是否存在,检测了两次,这是我们在第二次得到锁之前与第一次释放锁之后,这段时间内有可能一个新的peer被加入.
- struct inet_peer *inet_getpeer(__be32 daddr, int create)
- {
- struct inet_peer *p, *n;
- struct inet_peer **stack[PEER_MAXDEPTH], ***stackptr;
- ///查找是否存在这个peer节点.
- read_lock_bh(&peer_pool_lock);
- p = lookup(daddr, NULL);
- ///存在的话引用计数加1.
- if (p != peer_avl_empty)
- atomic_inc(&p->refcnt);
- read_unlock_bh(&peer_pool_lock);
- if (p != peer_avl_empty) {
- ///如果这个节点在unused中,则从unused中移除它.并返回
- unlink_from_unused(p);
- return p;
- }
- ///如果create参数为null,则返回null.
- if (!create)
- return NULL;
- ///开始创建一个新的peer节点.
- n = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
- if (n == NULL)
- return NULL;
- n->v4daddr = daddr;
- atomic_set(&n->refcnt, 1);
- atomic_set(&n->rid, 0);
- ///得到合适的包id.
- n->ip_id_count = secure_ip_id(daddr);
- n->tcp_ts_stamp = 0;
- write_lock_bh(&peer_pool_lock);
- /* Check if an entry has suddenly appeared. */
- p = lookup(daddr, stack);
- if (p != peer_avl_empty)
- goto out_free;
- ///加入到avl树.
- link_to_pool(n);
- ///初始化它的unused链表.
- INIT_LIST_HEAD(&n->unused);
- peer_total++;
- write_unlock_bh(&peer_pool_lock);
- ///如果此时内存超过限制,则remove掉链表头的元素(也就是LRU算法了,后面我们会分析cleanup_once这个函数.
- if (peer_total >= inet_peer_threshold)
- /* Remove one less-recently-used entry. */
- cleanup_once(0);
- return n;
- out_free:
- ........................................
- }
接下来我们来看clean_once这个函数.这个函数不仅会被inet_getpeer调用,还会被peer_periodic_timer调用:
- static int cleanup_once(unsigned long ttl)
- {
- struct inet_peer *p = NULL;
- /* Remove the first entry from the list of unused nodes. */
- spin_lock_bh(&inet_peer_unused_lock);
- if (!list_empty(&unused_peers)) {
- __u32 delta;
- p = list_first_entry(&unused_peers, struct inet_peer, unused);
- ///计算出这个peer最后一次被使用(也就是操作引用计数)到当前过去了多久.
- delta = (__u32)jiffies - p->dtime;
- ///如果这个时间小于传进来的ttl,就不进行任何操作.直接返回(这个ttl也就表示一个在unused链表中的元素在删除前,需要等待多久).而我们上面的inet_getpeer中,传进来的是0,这就会直接删除掉第一个peer.
- if (delta < ttl) {
- /* Do not prune fresh entries. */
- spin_unlock_bh(&inet_peer_unused_lock);
- return -1;
- }
- list_del_init(&p->unused);
- ///引用计数-1.
- atomic_inc(&p->refcnt);
- }
- spin_unlock_bh(&inet_peer_unused_lock);
- if (p == NULL)
- /* It means that the total number of USED entries has
- * grown over inet_peer_threshold. It shouldn't really
- * happen because of entry limits in route cache. */
- return -1;
- ///这个函数就简单介绍一下,先会判断p的引用计数,如果引用计数为1,则说明可以从avl树中删除它,然后将它彻底free掉.当引用技术不为1,则会将它直接加入到unused链表中(这里要注意,它并没有从avl树中删除).
- unlink_from_pool(p);
- return 0;
- }
接下来来看定时器处理函数:
- static void peer_check_expire(unsigned long dummy)