jollen.org

Jollen 的 Blog
Jollen's email: jollen # jollen.org
more:  Jollen's Training

register_netdev 的入門實例: bonding.c

.作者:jollen/
.日期:December 2, 2007 2:30 PM


今天在「Linux 驅動程式: 進階」的訓練課程中提到,找到正碓的入門實例,比起什麼都來得重要。

有時,老舊的程式碼,更容易幫助初學者掌握基本的架構與 API 用法。在 Linux 驅動程式的課程裡,我推薦同學研究一個早期的網路介面驅動程式,叫做 bonding.c。bonding 是一個用來合併網路卡的驅動程式,而早期的 bonding 驅動程式並沒有太多額外的 algorithm 實作,因此我建議同學,拿出 kernel 2.4.14 裡的 bonding.c 來做實例研究。由於這個版本的 bonding 驅動程式非常赤祼祼地呈現 register_netdev、struct net_device 與封包傳送的介面實作,因此肯定比許多最新的網路卡驅動程式更適合初學者。

kernel 2.4.14 的 bonding.c 可以在 2.6.x 的 kernel 上使用,美中不足的是,使用古老的 bonding driver 無法將 eth 介面合併進來,但至少可以配合 ifconfig 指令來做動態的操作與觀察,例如了解 'ifconfig bond0 down' 與 struct net_device::close 的實作。

如果要實際把玩 bonding 的功能,只要直接使用 kernel 2.6.x 本身的 bonding driver 即可。另外,kernel 2.4.14 的 bonding.c 必須修改一行 code,才能在 kernel 2.6.x 的環境下順利載入。以下提供此修改的 patch:

--- bonding.c.orig      2007-12-02 21:15:03.000000000 +0800
+++ bonding.c   2007-12-02 22:07:44.000000000 +0800
@@ -275,12 +275,11 @@
        /* Find a name for this unit */
        int err;
 
-       dev_bond.init = bond_init;
-
        err = dev_alloc_name(&dev_bond,"bond%d");
        if (err<0)
                return err;
 
+       bond_init(&dev_bond);
        SET_MODULE_OWNER(&dev_bond);
        if (register_netdev(&dev_bond) != 0)
                return -EIO;

Tags:

純手工打造說明:技術專欄文章為 Jollen 原創,內容皆為人工撰寫,無 AI 生成。轉載請註明出處與作者,並全文引用。轉載時請在文章開頭或結尾明顯處註明「本文出處:https://www.jollen.org,已取得原作者同意並授權使用。」
訂閱電子報:不定期 Jollen's Blog 精選文章隨 Moko365 電子報寄送;請透過 Moko365 電子報訂閱(可隨時取消)。

Copyright(c) 2001–2007 www.jollen.org. All rights reserved.
Last update: 2026-07-22