文章目录
前面的笔记分析了net_device的分配和注册流程,在可以进行数据收发之前,还需要打开net_device。对应的net_device不再工作时需要关闭net_device,这篇笔记来分析下这两个流程是如何实现的。
打开net_device
当执行“ip link set dev {DEVICE} up”时,将会打开net_device。该命令对应到设备接口层,将由dev_open()函数完成具体的打开流程。
/**
* dev_open - prepare an interface for use.
* @dev: device to open
*
* Takes a device from down to up state. The device's private open
* function is invoked and then the multicast lists are loaded. Finally
* the device is moved into the up state and a %NETDEV_UP message is
* sent to the netdev notifier chain.
*
* Calling this function on an active interface is a nop. On a failure
* a negative errno code is returned.
*/
int dev_open(struct net_device *dev)
{
const struct net_device_ops *ops = dev->netdev_ops;
int ret = 0;
ASSERT_RTNL(); // 一定是在持有RTNL锁的情况下调用
// 如果设备已经打开,直接返回打开成功
if (dev->flags & IFF_UP)
return 0;
// 设备必须已经注册或者未挂起,即外部可见
if (