The neighbor of a neighbor subsystem Find neigh_lookup, ___ neigh_lookup_noref

Outline

Neighbor item lookup is performed by neigh_lookup correlation function;

___neigh_lookup_noref, the function (IPv4 address for the next hop ip) and output device according to find the primary key value in the neighbor of the hash table, find the item is returned;

neigh_lookup, the function calls ___neigh_lookup_noref function, and after a neighbor found items, a reference count is incremented, and then returns the item;

Source code analysis
. 1  struct Neighbor * neigh_lookup ( struct neigh_table * TBL, const  void * PKey,
 2                     struct the net_device * dev)
 . 3  {
 . 4      struct Neighbor * n-;
 . 5  
. 6      NEIGH_CACHE_STAT_INC (TBL, Lookups);
 . 7  
. 8      rcu_read_lock_bh ();
 . 9  
10      / * Non- reference lookup neighbor entry * / 
. 11      n-= __neigh_lookup_noref (TBL, PKey, dev);
 12 is  
13 is      / * increment the reference count * / 
14      IF (n-) {
 15          IF(! atomic_inc_not_zero (N- &> refcnt))
 16              n-= NULL;
 . 17          NEIGH_CACHE_STAT_INC (TBL, Hits);
 18 is      }
 . 19  
20 is      rcu_read_unlock_bh ();
 21 is  
22 is      / * return neighbor item * / 
23 is      return n-;
 24 }

 

1 static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl,
2                              const void *pkey,
3                              struct net_device *dev)
4 {
5     return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev);
6 }

 

 1 static inline struct neighbour *___neigh_lookup_noref(
 2     struct neigh_table *tbl,
 3     bool (*key_eq)(const struct neighbour *n, const void *pkey),
 4     __u32 (*hash)(const void *pkey,
 5               const struct net_device *dev,
 6               __u32 *hash_rnd),
 7     const void *pkey,
 8     struct net_device *dev)
 9 {
10     /* 获取hash */
11     struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht);
12     struct neighbour *n;
13     u32 hash_val;
14 
15     /* 计算hash值 */
16     hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
17 
18     /* 遍历hash表项 */
19     for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
20          n != NULL;
21          rcu_dereference_bh = n-(N-> Next)) {
 22 is          / * find the item is returned * / 
23 is          IF (N-> == dev dev && key_eq (n-, PKey))
 24              return n-;
 25      }
 26 is  
27      / * Not found returns NULL * / 
28      return NULL;
 29 }

 

Guess you like

Origin www.cnblogs.com/wanpengcoder/p/11755392.html