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网络技术内幕-设备注册和初始化(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言sort函数排序二维数组,js
- 下一篇: linux删除副本文件,RMAN不能删除