网络设备之关闭

设备关闭操作主要完成设备启用标记位的清除,未发送完的数据发送完,发送禁用通知等操作;下面为相关函数的调用关系,其中dev_change_flags调用__dev_close的流程,已经在设备开启文章中做过介绍,此处不再分析;

1 /**
2  * 设备禁用函数调用关系
3  * dev_change_flags-->_dev_change_flags-->__dev_close
4  * dev_close-->dev_close_many-->__dev_close_many
5  */

 

 dev_close将设备加入到临时链表中,并且调用关闭多个设备的函数,对设备执行关闭操作;

 1 /* 设备关闭 */
 2 int dev_close(struct net_device *dev)
 3 {
 4     /* 启动状态 */
 5     if (dev->flags & IFF_UP) {
 6 
 7         /* 初始化single链表 */
 8         LIST_HEAD(single);
 9         /* 加入single链表 */
10         list_add(&dev->close_list, &single);
11 
12         /* 关闭设备 */
13         dev_close_many(&single, true);
14 
15         /* 删除链表 */
16         list_del(&single);
17     }
18     return 0;
19 }

 

dev_close_many函数会移除未运行的设备,然后调用__dev_close_many对设备进行关闭,关闭成功之后发送设备关闭消息,并移除关闭设备节点;

 1 /* 关闭设备 */
 2 int dev_close_many(struct list_head *head, bool unlink)
 3 {
 4     struct net_device *dev, *tmp;
 5 
 6     /* Remove the devices that don't need to be closed */
 7     /* 移除未运行的设备 */
 8     list_for_each_entry_safe(dev, tmp, head, close_list)
 9         if (!(dev->flags & IFF_UP))
10             list_del_init(&dev->close_list);
11 
12     /* 关闭设备 */
13     __dev_close_many(head);
14 
15     list_for_each_entry_safe(dev, tmp, head, close_list) {
16         /* netlink消息 */
17         rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
18 
19         /* 通知设备关闭 */
20         call_netdevice_notifiers(NETDEV_DOWN, dev);
21 
22         /* 移除设备节点 */
23         if (unlink)
24             list_del_init(&dev->close_list);
25     }
26 
27     return 0;
28 }

 

__dev_close_many通知设备正在关闭,等待未发送完的数据发送完,如果设备驱动实现了关闭操作函数,则调用之,最后清除开启标记;

 1 static int __dev_close_many(struct list_head *head)
 2 {
 3     struct net_device *dev;
 4 
 5     ASSERT_RTNL();
 6     might_sleep();
 7 
 8     list_for_each_entry(dev, head, close_list) {
 9         /* Temporarily disable netpoll until the interface is down */
10         /* 禁用netpoll */
11         netpoll_poll_disable(dev);
12 
13         /* 通知设备正在关闭 */
14         call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
15 
16         /* 清除start标志位 */
17         clear_bit(__LINK_STATE_START, &dev->state);
18 
19         /* Synchronize to scheduled poll. We cannot touch poll list, it
20          * can be even on different cpu. So just clear netif_running().
21          *
22          * dev->stop() will invoke napi_disable() on all of it's
23          * napi_struct instances on this device.
24          */
25         smp_mb__after_atomic(); /* Commit netif_running(). */
26     }
27 
28     /* 未发送完的数据发送完 */
29     dev_deactivate_many(head);
30 
31     list_for_each_entry(dev, head, close_list) {
32         const struct net_device_ops *ops = dev->netdev_ops;
33 
34         /*
35          *    Call the device specific close. This cannot fail.
36          *    Only if device is UP
37          *
38          *    We allow it to be called even after a DETACH hot-plug
39          *    event.
40          */
41         /* 调用设备关闭操作 */
42         if (ops->ndo_stop)
43             ops->ndo_stop(dev);
44 
45         /* 标记设备关闭 */
46         dev->flags &= ~IFF_UP;
47         /* 启用netpoll */
48         netpoll_poll_enable(dev);
49     }
50 
51     return 0;
52 }

 

posted @ 2017-09-15 15:00  AlexAlex  阅读(849)  评论(0编辑  收藏  举报