1. 程式人生 > >Linux核心——遍歷 net_device 結構

Linux核心——遍歷 net_device 結構

linux核心版本 : 2.6.32<=(這個更早的也許就已經這樣了,具體是從哪個版本開始的,我沒有考證,需要在今後,發現後及時補充。)

extern struct net *init_net;	/* 存放net_device的全域性變數 */
extern rwlock_t                         dev_base_lock;          /* Device list lock */

/*這些是我們遍歷 net_device 結構的迴圈 */

#define for_each_netdev(net, d)         \
                list_for_each_entry(d, &(net)->dev_base_head, dev_list)
#define for_each_netdev_reverse(net, d) \
                list_for_each_entry_reverse(d, &(net)->dev_base_head, dev_list)
#define for_each_netdev_rcu(net, d)             \
                list_for_each_entry_rcu(d, &(net)->dev_base_head, dev_list)
#define for_each_netdev_safe(net, d, n) \
                list_for_each_entry_safe(d, n, &(net)->dev_base_head, dev_list)
#define for_each_netdev_continue(net, d)                \
                list_for_each_entry_continue(d, &(net)->dev_base_head, dev_list)
#define for_each_netdev_continue_rcu(net, d)            \
        list_for_each_entry_continue_rcu(d, &(net)->dev_base_head, dev_list)
#define for_each_netdev_in_bond_rcu(bond, slave)        \
                for_each_netdev_rcu(&init_net, slave)   \
                         if (netdev_master_upper_dev_get_rcu(slave) == bond)
#define net_device_entry(lh)    list_entry(lh, struct net_device, dev_list)


《深入理解Linux網路技術內幕》中介紹的是這樣子的:

480 extern struct net_device                *dev_base;              /* All devices */
481 extern rwlock_t                         dev_base_lock;          /* Device list lock */
遍歷方式:
struct net_device * dev = dev_base;
while(dev) {
	dev = dev->next;
}