概述
邻居项查找是通过neigh_lookup相关函数来进行的;
___neigh_lookup_noref,该函数根据输出设备和主键值(IPv4为下一跳ip地址)在邻居项hash表中查找,找到则返回该项;
neigh_lookup,该函数调用了___neigh_lookup_noref函数,并且在找到邻居项之后,进行引用计数的递增,然后返回该项;
源码分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev) { struct neighbour *n; NEIGH_CACHE_STAT_INC(tbl, lookups); rcu_read_lock_bh(); /* 非引用查找邻居项 */ n = __neigh_lookup_noref(tbl, pkey, dev); /* 增加引用计数 */ if (n) { if (!atomic_inc_not_zero(&n->refcnt)) n = NULL; NEIGH_CACHE_STAT_INC(tbl, hits); } rcu_read_unlock_bh(); /* 返回邻居项 */ return n; } |
1 2 3 4 5 6 |
static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl, const void *pkey, struct net_device *dev) { return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static inline struct neighbour *___neigh_lookup_noref( struct neigh_table *tbl, bool (*key_eq)(const struct neighbour *n, const void *pkey), __u32 (*hash)(const void *pkey, const struct net_device *dev, __u32 *hash_rnd), const void *pkey, struct net_device *dev) { /* 获取hash */ struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht); struct neighbour *n; u32 hash_val; /* 计算hash值 */ hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); /* 遍历hash表项 */ for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); n != NULL; n = rcu_dereference_bh(n->next)) { /* 找到则返回该项 */ if (n->dev == dev && key_eq(n, pkey)) return n; } /* 未找到则返回NULL */ return NULL; } |