manimnchn 2020-07-26
网络设备的打开函数需要完成如下工作:
网络设备的关闭函数需要完成如下工作:
Linux 内核提供的 netif_start_queue( ) 和 netif_stop_queue( ) 连个函数的原型为:
static inline void netif_tx_start_queue(struct netdev_queue *dev_queue) { clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state); } /** * netif_start_queue - allow transmit * @dev: network device * * Allow upper layers to call the device hard_start_xmit routine. */ static inline void netif_start_queue(struct net_device *dev) { netif_tx_start_queue(netdev_get_tx_queue(dev, 0)); }
static inline void netif_tx_stop_queue(struct netdev_queue *dev_queue) { if (WARN_ON(!dev_queue)) { pr_info("netif_stop_queue() cannot be called before register_netdev()\n"); return; } set_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state); } /** * netif_stop_queue - stop transmitted packets * @dev: network device * * Stop upper layers calling the device hard_start_xmit routine. * Used for flow control when transmit resources are unavailable. */ static inline void netif_stop_queue(struct net_device *dev) { netif_tx_stop_queue(netdev_get_tx_queue(dev, 0)); }
根据以上分析,可得出如下代码所示的网络设备打开和释放函数的模板。
/* * 网络设备打开和释放函数模板 */ static int xxx_open(struct net_device *dev) { /* 申请端口、IRQ等,类似于fops->open */ ret = request_irq(dev->irq, &xxx_interrupt, 0, dev->name, dev); ··· netif_start_queue(dev); ··· } static int xxx_release(struct net_device *dev) { /* 释放端口、IRQ 等,类似于fops->close */ free_irq(dev->irq, dev); ··· netif_stop_queue(dev); ··· }