蘋果小豬研究室
  • 首頁
  • 中譯資料
  • 網路觀念
    • OSGi 平台
    • SCTP 通訊協定簡介
      • 1 多重串流 (Multi-streaming)
      • 2 路徑多宿 (Multi-homing)
      • 3 SACK 機制
      • 4 Path MTU Discovery
      • 5 Ordered/Unordered 傳送機制
      • 6. SCTP API
      • 7. TCP 與 SCTP 通訊協定比較
      • 8. 參考文獻
    • TCP over SCTP tunnel
    • TCP 與 SCTP 協定轉換機制
    • UPnP 與 UPnP AV
  • Linux 程式設計
    • [C] 產生亂數
    • [C] Pointer and array
    • [Script] strncmp
    • [C] Linux 取得 HD 序號
    • [C] 程式範例-切割 subnet
    • C struct 的使用
    • C 目錄相關
    • const 修飾詞
    • Dangling pointer(懸置指標)
    • dnsmole
    • dos2unix 程式碼
    • Function pointer in C
    • GCC header limits.h
    • GDB 參數
    • glibc detected (double free)
    • IEEE 754 and float 範例程式
    • kernel 中的網路卡名稱與命名順序關係
    • Linux 序列埠程式設計
    • loop device 的數目
    • Perl & LWP
    • Remote Control over CGI
    • 偵測 NAT 內部主機連線狀況
    • 利用 libpcap 取得所有的網路裝置名稱
    • 取得時間
    • strncpy
    • 從應用層程式取得傳輸層 TCP 資訊
    • 於 Linux 上撰寫 daemon server 要注意的
    • 為什麼需要 & 0377
    • 研究 setsid()
    • 與 HTTP 協定有關的 RFC
    • 製作 patch 檔
    • 設計在背景執行的 Linux 程式
    • 讀取 STDIN (Standard INPUT) 範例程式
    • 關於 Linux CPU smp_affinity
    • 陣列名稱與指標
  • 網路服務推薦
    • 專案管理工具 - clickup
    • 文件大師 - gitbook
  • glibc 中文手冊
    • GNU C 函式庫常見問題(FAQ)
  • Linux 系統操作
    • /proc/sys/net/ipv4/conf/eth* 目錄中的參數
    • Linux 伺服器效能調校
    • Linux 設定網卡速度
    • Proxy ARP
    • sysrq
    • Turn off beep in Linux system
    • umask()
    • 以 ethtool 查看網路卡狀態及設定
    • 使用 nameif 修改 Linux 網路卡名稱
    • 使用 xhost
    • 利用 sed 取出檔案中某行
    • 在 bash 中設定 Proxy server
    • 在 Linux 系統呈現 dd 的進度
    • 字串取代,使用 awk & sed
    • 尋找多種副檔名
    • 常用 VIM 選項
    • 自動 CPU 調頻節能控制
    • 自動執行程式
    • 設定 Debian 預設啟動服務
    • 設定 DHCPd server
    • 透過 RS-232 操作 Linux console
    • Linux connect to serial port
  • 資訊安全
    • ARP spoofing 技術管控區網流量實例
    • dsniff
    • Linux Socket Filtering
    • Linux 系統安全管理
    • Security 資訊
    • sniffer 相關工具 - IPgrad, tcpick
    • 以 Linux 實務 ARP Spoofing
    • 將文字隱藏於 JPEG 圖檔
    • 資安工具
    • 資安相關網站
    • 限制 localhost 的 P2P 上傳流量
  • 實用工具
    • 實驗基本數據製圖指令 gnuplot
  • 交換連結
  • 網頁設定
  • 網路應用
    • 網路時光機
  • Foldng@Home
  • 文獻推薦
    • 電子書
    • 提問的智慧
    • 好書
  • 隱私政策
Powered by GitBook
On this page
Edit on GitHub
  1. Linux 程式設計

kernel 中的網路卡名稱與命名順序關係

有天,WalkingIce 於 Linux 版發問了下列的問題:

看板: Linux 標 題: [問題] 關於eth0與網路卡的關係 發信站: 批踢踢實業 (Sat Apr 8 17:52:48 2006) 轉信站: SayYa!ctu-reader!news.nctu!ptt Origin: sally.csie.ntu.edu.tw 不知道標題這樣下有沒有問題:P 就我的認知,假設網路卡都沒問題可以讓kernel抓到之後 設定/etc/network/interfaces (以debian為例) 把這一類的設定填入 auto eth0 iface eth0 inet static address 192.168.1.1 再執行ifup eth0就可以啟動網路卡,並且有192.168.1.1這個ip 我很好奇的是,系統如何決定eth0這個代號會跟哪一個硬體扯上關係? 如果我插的是另外一張莫名其妙的卡而非網路卡,系統這時候又會怎麼應對呢? 此外,如果我還有另一張網路卡。這個代號的順序又是如何決定呢? 謝謝 <(_ _)>

這使得我那天熱血沸騰起來,直接翻出 Linux kernel source code 來讀: me="個人推敲" linux="/usr/src/linux-2.6.15.6"

$linux/drivers/net/Space.c:360

            /*  Statically configured drivers -- order matters here. */
            static int __init net_olddevs_init(void)
            {


                int num;


                if (loopback_init()) {
                        printk(KERN_ERR "Network loopback device setup failed\n");
                }


                ...


                 /*  me: NETDEV_BOOT_SETUP_MAX 預設值 8
                  *      意謂預設開機最多偵測的網卡數 == 8?
                 */
                for (num = 0; num < 8; ++num)
                    etherif_probe2(num);    <----------

                ...

            }
$linux/drivers/net/Space.c:300

        /*
         * Unified ethernet device probe, segmented per architecture and
         * per bus interface. This drives the legacy devices only for now.
         */


        static void __init ethif_probe2(int unit)
        {


                /*
                  *  me: netdev_boot_base 檢查此裝置是否已經 register
                 */
                unsigned long base_addr = netdev_boot_base("eth", unit);


                if (base_addr == 1)
                        return;


                /* 偵測網卡 */
                (void)( probe_list2(unit, m68k_probes, base_addr == 0) &&
                        probe_list2(unit, eisa_probes, base_addr == 0) &&
                        probe_list2(unit, mca_probes, base_addr == 0) &&
                        probe_list2(unit, isa_probes, base_addr == 0) &&
                        probe_list2(unit, parport_probes, base_addr == 0));
        }

Space.c 在開機時偵測 ISA, MCA, EISA, .. 這幾種類型的網卡,而 eth[0,1,2,3,...] 這樣的順序是去查 net device list, 如果還沒有註冊,則使用 register_netdev(dev) 來將該裝置 register 加入 list。

$linux/net/ethernet/eth.c

/* me: alloc_etherdev 配置了一個 ethernet 的 net_device 空間
* 並且由 ether_setup 設定 ethernet device.
* 經由程式碼可看出,其實又呼叫了 alloc_netdev(),
* 而在這邊,裝置的名稱直接指定為 "eth%d",代表為 ethernet device
* 而這裡還沒有處理 eth? 的 '?' 該是多少
*/

        298 struct net_device *alloc_etherdev(int sizeof_priv)
        299 {
        300         return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
        301 }

alloc_netdev 可參考 $linux/core/dev.c /* 決定 eth? 的 '?' 是多少是在下面處理的 */

在 register_netdev() {

          /*
           會用 dev_alloc_name(dev, dev_name) 來設定裝置的名稱 ethx,
            而 'x' 則經由計算在該 net device list 中的位置取得。
            該 list 的資料型態為 struct net_device。


            等知道該裝置的名稱為何時 ex. (dev->name="eth1")
           最後才 call register_netdevice(dev) 將該裝置加入 list 中。
          */
        }

而 PCI 網卡,如 8139too.c 也是一樣, 在 device driver 中也是要有 call register_netdev() 的動作。 以目前的了解,核心會將網路裝置用 link list 串起來,而編號順序應只是被放入 list 中的順序而已。 如果有三張同樣的網卡呢?我在 VMware 直接 dmesg 觀察三張 pcnet32 的結果,I/O address 比較小的會在前面。 ex. eth0 at 0x1080 eth1 at 0x1400 eth2 at 0x1480 後記:在我 trace 完上列這段流程之後,我才發現到,市面上已經有好幾本講 Linux kernel 網路的書,都已經有談到我上列的內容,而且內容更詳盡,於是我又去讀了這幾本書 =.= 參考文獻 1. Understanding Linux Kernel Internals 2. The Linux TCP/IP Stack: Networking Embedded System 3. Linux Network Architecture

PreviousIEEE 754 and float 範例程式NextLinux 序列埠程式設計

Last updated 2 years ago