赞
踩
NAPI 是 Linux 上采用的一种提高网络处理效率的技术,它的核心概念就是不采用中断的方式读取数据,而代之以首先采用中断唤醒数据接收的服务程序,然后 通过poll的方法来轮询数据。采用NAPI技术可以大大改善短长度数据包接收的效率,减少中断触发的时间。
可以这样理解,在NAPI机制没有出现时,由于网络设备接收数据是采用中断方式的,假设每次数据包很小,小到只有几个字节。而刚好在1秒内有5000个这样的数据包。那么系统就是在1秒内产生5000个中断,这无疑给会占据CPU的大部分资源。NAPI的出现就是要解决这样的问题。
概念的东西就不多提了。下面开始源码之旅啦!
首先在分配CAN设备时,采用netif_napi_add函数注册轮询函数d_can_poll()。
struct net_device *alloc_d_can_dev(int num_objs)
{
struct net_device *dev;
struct d_can_priv *priv;
dev = alloc_candev(sizeof(struct d_can_priv), num_objs/2);
if (!dev)
return NULL;
priv = netdev_priv(dev);
netif_napi_add(dev, &priv->napi, d_can_poll, num_objs/2);
priv->dev = dev;
priv->can.bittiming_const = &d_can_bittiming_const;
priv->can.do_set_mode = d_can_set_mode;
priv->can.do_get_berr_counter = d_can_get_berr_counter;
priv->can.ctrlmode_supported = (CAN_CTRLMODE_LOOPBACK |
CAN_CTRLMODE_LISTENONLY |
CAN_CTRLMODE_BERR_REPORTING |
CAN_CTRLMODE_3_SAMPLES);
return dev;
}
netif_napi_add()函数原型如下:
void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
int (*poll)(struct napi_struct *, int), int weight)
{
INIT_LIST_HEAD(&napi->poll_list);
napi->gro_count = 0;
napi->gro_list = NULL;
napi->skb = NULL;
napi->poll = poll;
napi->weight = weight;
list_add(&napi->dev_list, &dev->napi_list);
napi->dev = dev;
#ifdef CONFIG_NETPOLL
spin_lock_init(&napi->poll_lock);
napi->poll_owner = -1;
#endif
set_bit(NAPI_STATE_SCHED, &napi->state);
}
轮询函数d_can_poll()如下:
static int d_can_poll(struct napi_struct *napi, int quota)
{
int lec_type = 0;
int work_done = 0;
struct net_device *dev = napi->dev;
struct d_can_priv *priv = netdev_priv(dev);
if (!priv->irqstatus)
goto end;
/* status events have the highest priority */
if (priv->irqstatus == STATUS_INTERRUPT) {
priv->current_status = d_can_read(priv, D_CAN_ES);
/* handle Tx/Rx events */
if (priv->current_status & D_CAN_ES_TXOK)
d_can_write(priv, D_CAN_ES,
priv->current_status & ~D_CAN_ES_TXOK);
if (priv->current_status & D_CAN_ES_RXOK)
d_can_write(priv, D_CAN_ES,
priv->current_status & ~D_CAN_ES_RXOK);
/* handle state changes */
if ((priv->current_status & D_CAN_ES_EWARN) &&
(!(priv->last_status & D_CAN_ES_EWARN))) {
netdev_dbg(dev, "entered error warning state\n");
work_done += d_can_handle_state_change(dev,
D_CAN_ERROR_WARNING);
}
if ((priv->current_status & D_CAN_ES_EPASS) &&
(!(priv->last_status & D_CAN_ES_EPASS))) {
netdev_dbg(dev, "entered error passive state\n");
work_done += d_can_handle_state_change(dev,
D_CAN_ERROR_PASSIVE);
}
if ((priv->current_status & D_CAN_ES_BOFF) &&
(!(priv->last_status & D_CAN_ES_BOFF))) {
netdev_dbg(dev, "entered bus off state\n");
work_done += d_can_handle_state_change(dev,
D_CAN_BUS_OFF);
}
/* handle bus recovery events */
if ((!(priv->current_status & D_CAN_ES_BOFF)) &&
(priv->last_status & D_CAN_ES_BOFF)) {
netdev_dbg(dev, "left bus off state\n");
priv->can.state = CAN_STATE_ERROR_ACTIVE;
}
if ((!(priv->current_status & D_CAN_ES_EPASS)) &&
(priv->last_status & D_CAN_ES_EPASS)) {
netdev_dbg(dev, "left error passive state\n");
priv->can.state = CAN_STATE_ERROR_ACTIVE;
}
priv->last_status = priv->current_status;
/* handle lec errors on the bus */
lec_type = d_can_has_handle_berr(priv);
if (lec_type)
work_done += d_can_handle_bus_err(dev, lec_type);
} else if ((priv->irqstatus >= D_CAN_MSG_OBJ_RX_FIRST) &&
(priv->irqstatus <= D_CAN_MSG_OBJ_RX_LAST)) {
/* handle events corresponding to receive message objects */
work_done += d_can_do_rx_poll(dev, (quota - work_done));
} else if ((priv->irqstatus >= D_CAN_MSG_OBJ_TX_FIRST) &&
(priv->irqstatus <= D_CAN_MSG_OBJ_TX_LAST)) {
/* handle events corresponding to transmit message objects */
d_can_do_tx(dev);
}
end:
if (work_done < quota) {
napi_complete(napi);
/* enable all IRQs */
d_can_interrupts(priv, ENABLE_ALL_INTERRUPTS);
}
return work_done;
}
在中断处理函数中,先禁止接收中断,且告诉网络子系统,将以轮询方式快速收包,其中禁止接收中断完全由硬件功能决定,而告诉内核将以轮询方式处理包则是使用函数napi_schedule()。
static irqreturn_t d_can_isr(int irq, void *dev_id)
{
struct net_device *dev = (struct net_device *)dev_id;
struct d_can_priv *priv = netdev_priv(dev);
priv->irqstatus = d_can_read(priv, D_CAN_INT);
if (!priv->irqstatus)
return IRQ_NONE;
/* disable all interrupts and schedule the NAPI */
d_can_interrupts(priv, DISABLE_ALL_INTERRUPTS);
napi_schedule(&priv->napi);
return IRQ_HANDLED;
}
其中的napi_schedule_prep()函数是为了判定现在是否已经进入了轮询模式。
static inline void napi_schedule(struct napi_struct *n)
{
if (napi_schedule_prep(n))
__napi_schedule(n);
}
轮询功能的关闭则需要使用:
if (work_done < quota) {
napi_complete(napi);
/* enable all IRQs */
d_can_interrupts(priv, ENABLE_ALL_INTERRUPTS);
}
因为可能存在多个napi_struct的实例,要求每个实例能够独立的使能或者禁止。因此,需要驱动开发人员保证在网卡接口关闭时,禁止所有的napi_struct的实例。在d_can_open()函数中使用napi_enable(),在d_can_close()函数中使用napi_disable()。
napi_enable(&priv->napi);
napi_disable(&priv->napi);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。