日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux内核网络raw_cpu_add,深入理解Linux网络技术内幕-设备注册和初始化(四)

發布時間:2025/3/11 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux内核网络raw_cpu_add,深入理解Linux网络技术内幕-设备注册和初始化(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

static void rollback_registered_many(struct list_head *head)

{

struct net_device *dev, *tmp;

BUG_ON(dev_boot_phase);

ASSERT_RTNL();? //檢查是否獲取了rtnl互斥量

list_for_each_entry_safe(dev, tmp, head, unreg_list) {

/* Some devices call without registering

* for initialization unwind. Remove those

* devices and proceed with the remaining.

*/

if (dev->reg_state == NETREG_UNINITIALIZED) { //處理在注冊過程中失敗的設備

pr_debug("unregister_netdevice: device %s/%p never "

"was registered\n", dev->name, dev);

WARN_ON(1);

list_del(&dev->unreg_list);//將其從鏈表刪除即可

continue;

}

BUG_ON(dev->reg_state != NETREG_REGISTERED); //程序到這里則設備不可能不處于已注冊狀態

}

/* If device is running, close it first. */

dev_close_many(head); //關閉在運行的設備

list_for_each_entry(dev, head, unreg_list) {

/* And unlink it from device chain. */

unlist_netdevice(dev);

/*

這里說明,只是將其從系統中的三個鏈表上移除

static void unlist_netdevice(struct net_device *dev)

{

ASSERT_RTNL();

/* Unlink dev from the device chain */

write_lock_bh(&dev_base_lock); //dev_base_lock是保證這三個鏈表互斥的讀寫鎖

list_del_rcu(&dev->dev_list);

hlist_del_rcu(&dev->name_hlist);

hlist_del_rcu(&dev->index_hlist);

write_unlock_bh(&dev_base_lock);

}

*/

dev->reg_state = NETREG_UNREGISTERING; //然后,更新設備的注冊狀態

}

synchronize_net();

list_for_each_entry(dev, head, unreg_list) {

/* Shutdown queueing discipline. */

dev_shutdown(dev);? //處理設備的接收隊列等

/* Notify protocols, that we are about to destroy

this device. They should clean all the things.

*/

call_netdevice_notifiers(NETDEV_UNREGISTER, dev);? //發出注銷通知

if (!dev->rtnl_link_ops ||

dev->rtnl_link_state == RTNL_LINK_INITIALIZED)

rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);

/*

*??? Flush the unicast and multicast chains

*/

dev_uc_flush(dev);

dev_mc_flush(dev);

if (dev->netdev_ops->ndo_uninit) //處理私有數據區

dev->netdev_ops->ndo_uninit(dev);

/* Notifier chain MUST detach us from master device. */

WARN_ON(dev->master);

/* Remove entries from kobject tree */

netdev_unregister_kobject(dev); //從內核移除對象,涉及到內核設備管理層的東西

}

/* Process any work delayed until the end of the batch */

dev = list_first_entry(head, struct net_device, unreg_list);

call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);

rcu_barrier();

list_for_each_entry(dev, head, unreg_list)

dev_put(dev); //釋放設備,對其引用計數減一

}

這里在獲取鎖的時間范圍內的注銷操作就完成了,這時設備已經和內核的設備鏈脫離了關系,也就是內核已經不知道這個設備的存在了。但這個設備可以還被內核中的其他模塊,因此,剩余的操作需要在釋放了rtnl互斥量后,在net_run_todo函數中處理。

//在這里,釋放了互斥量后,可以在等待設備的引用計數歸零過程中睡眠

void netdev_run_todo(void)

{

struct list_head list;

/* Snapshot list, allow later requests */

list_replace_init(&net_todo_list, &list);? //復制全局變量net_todo_list的值,然后初始化

__rtnl_unlock();? //釋放互斥量

while (!list_empty(&list)) { //對鏈表中的元素進行處理

struct net_device *dev

= list_first_entry(&list, struct net_device, todo_list); //處理一個,移除一個

list_del(&dev->todo_list);

if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { //內核出現重大BUG

printk(KERN_ERR "network todo '%s' but state %d\n",

dev->name, dev->reg_state);

dump_stack();

continue;

}

dev->reg_state = NETREG_UNREGISTERED;? //更新注冊狀態

on_each_cpu(flush_backlog, dev, 1);

netdev_wait_allrefs(dev);? //等待引用計數歸零,可能睡眠

/* paranoia */

BUG_ON(netdev_refcnt_read(dev));

WARN_ON(rcu_dereference_raw(dev->ip_ptr));

WARN_ON(rcu_dereference_raw(dev->ip6_ptr));

WARN_ON(dev->dn_ptr);

if (dev->destructor)

dev->destructor(dev);

/* Free network device */

kobject_put(&dev->dev.kobj); //釋放這個結構,到這里設備的注銷完全完成,net_device結構將被釋放

}

}

其實查看netdev_wait_allrefs函數,就是定時查看設備的引用計數是否為0,不為0則再次向其他模塊發設備注銷通知,讓它們釋放這個設備,然后進入休眠等待的過程。

static void netdev_wait_allrefs(struct net_device *dev)

{

unsigned long rebroadcast_time, warning_time;

int refcnt;

linkwatch_forget_dev(dev);

rebroadcast_time = warning_time = jiffies;

refcnt = netdev_refcnt_read(dev);

while (refcnt != 0) {

if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { //過一秒

rtnl_lock();

/* Rebroadcast unregister notification */

call_netdevice_notifiers(NETDEV_UNREGISTER, dev); //發注銷廣播通知

/* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users

* should have already handle it the first time */

if (test_bit(__LINK_STATE_LINKWATCH_PENDING,

&dev->state)) {

/* We must not have linkwatch events

* pending on unregister. If this

* happens, we simply run the queue

* unscheduled, resulting in a noop

* for this device.

*/

linkwatch_run_queue();

}

__rtnl_unlock();

rebroadcast_time = jiffies;

}

msleep(250);? //睡眠250毫秒

refcnt = netdev_refcnt_read(dev);? //奪取引用計數

if (time_after(jiffies, warning_time + 10 * HZ)) { //等待10秒

printk(KERN_EMERG "unregister_netdevice: "

"waiting for %s to become free. Usage "

"count = %d\n",

dev->name, refcnt);

warning_time = jiffies;

}

}

}

總結

以上是生活随笔為你收集整理的linux内核网络raw_cpu_add,深入理解Linux网络技术内幕-设备注册和初始化(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。