內核版本:Linux-2.6.34
網卡驅動:B4401
NAPI 是 Linux 在 2.5 之後引入的一整套網口資料處理 API,因此不少老驅動並不支援這種模式。簡單說,NAPI 是中斷與輪詢的混血:資料量很低或很高時,它能分別發揮中斷與輪詢的長處,表現不錯;但若流量不穩定、高不成低不就,NAPI 會在兩種模式之間反覆切換,反而虧在切換成本上。
後面會用到 netdev_priv()。每個網卡驅動都有自己的私有資料來維持運作,這塊資料緊接在 net_device 結構之後(記憶體意義上的「之後」),這個函數就是靠 dev 取到它。注意私有資料並不在 dev 結構體裡,而是貼在其後方:
static inline void *netdev_priv(const struct net_device *dev)
{
return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
}
要理解它,得先看 dev 是怎麼配置的:
alloc_netdev() -> alloc_netdev_mq()
struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
void (*setup)(struct net_device *),
unsigned int queue_count)
{
……
alloc_size = sizeof(struct net_device);
if (sizeof_priv) {
/* ensure 32-byte alignment of private area */
alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
alloc_size += sizeof_priv;
}
/* ensure 32-byte alignment of whole construct */
alloc_size += NETDEV_ALIGN - 1;
p = kzalloc(alloc_size, GFP_KERNEL);
if (!p) {
printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
return NULL;
}
……
可見配置 dev 時就一併把 private 的空間配置在後面,兩段各自對齊,padding 是補齊用的字節:
![Linux 內核剖析—網路[一]:收發資料包的呼叫全景](https://www.unixlinux.online/unixlinux/UploadFiles_9426/201703/2017030316382935.gif)
舉例:若 sizeof(net_device) 為 31B、private 為 45B,實際配置的樣子如下:
![Linux 內核剖析—網路[一]:收發資料包的呼叫全景](https://www.unixlinux.online/unixlinux/UploadFiles_9426/201703/2017030316382918.gif)
有資料收發或發生錯誤時會產生硬體中斷,進入這個函數。先取出私有資料(它就緊跟在 dev 之後):
struct b44 *bp = netdev_priv(dev);
讀出中斷狀態與遮罩字:
istat = br32(bp, B44_ISTAT); imask = br32(bp, B44_IMASK);
接著把 NAPI 標記為 SCHED、記錄中斷狀態、關掉中斷並排入排程:
if (istat) {
……
if (napi_schedule_prep(&bp->napi)) {
bp->istat = istat;
__b44_disable_ints(bp);
__napi_schedule(&bp->napi);
}
void __napi_schedule(struct napi_struct *n)
{
unsigned long flags;
local_irq_save(flags);
list_add_tail(&n->poll_list, &__get_cpu_var(softnet_data).poll_list);
__raise_softirq_irqoff(NET_RX_SOFTIRQ);
local_irq_restore(flags);
}
__get_cpu_var() 取得當前 CPU 的偏移量(與多 CPU 有關),把 napi 的 poll_list 掛到 softnet_data 鏈尾,然後挑起 NET_RX_SOFTIRQ 軟中斷。
到這裡還沒看到真正的收發函數,別急。net_dev_init() [dev.c] 註冊了兩個軟中斷處理函數,所以軟中斷最終會走進 net_rx_action():
open_softirq(NET_TX_SOFTIRQ, net_tx_action); open_softirq(NET_RX_SOFTIRQ, net_rx_action);
net_rx_action() 的實現:
static void net_rx_action(struct softirq_action *h)
{
struct list_head *list = &__get_cpu_var(softnet_data).poll_list; // [1]
……
n = list_first_entry(list, struct napi_struct, poll_list); // [2]
……
work = 0;
if (test_bit(NAPI_STATE_SCHED, &n->state)) {
work = n->poll(n, weight); // [3]
trace_napi_poll(n);
}
……
}
從 poll_list 頭部取出一個 napi_struct,然後呼叫它的 poll()。值得注意的是:硬體中斷時是往 poll_list 尾部加入,軟中斷處理時從頭部取出,理論上硬體中斷掛進去的東西,會由它引發的軟中斷處理掉。
poll 實際指向 b44_poll()。這是哪裡綁上的?在驅動初始化 b44_init_one() 裡有這麼一行:
netif_napi_add(dev, &bp->napi, b44_poll, 64);
而 netif_napi_add() 會初始化 napi 並掛進 dev 的隊列,其中:
napi->poll = poll;
於是 NET_RX_SOFTIRQ 的處理函數 net_rx_action() 裡呼叫的,就是 b44_poll()。
static int b44_poll(struct napi_struct *napi, int budget)
{
……
if (bp->istat & (ISTAT_TX | ISTAT_TO))
b44_tx(bp);
……
if (bp->istat & ISTAT_RX)
work_done += b44_rx(bp, budget);
if (bp->istat & ISTAT_ERRORS)
……
}
它檢查了四類中斷:
#define ISTAT_TO 0x00000080 /* General Purpose Timeout */ #define ISTAT_RX 0x00010000 /* RX Interrupt */ #define ISTAT_TX 0x01000000 /* TX Interrupt */ #define ISTAT_ERRORS (ISTAT_DSCE|ISTAT_DATAE|ISTAT_DPE|ISTAT_RDU|ISTAT_RFO|ISTAT_TFU)
TX 中斷呼叫 b44_tx 送包,RX 中斷呼叫 b44_rx 收包。至此,從網卡驅動收發資料包的呼叫路徑就完整了。
最後用一張圖總結:
![Linux 內核剖析—網路[一]:收發資料包的呼叫全景](https://www.unixlinux.online/unixlinux/UploadFiles_9426/201703/2017030316382927.gif)