Linux Intel網絡卡IGB驅動修改mac位址

2021-09-04 12:57:45 字數 3609 閱讀 1074

實然心血來潮,想研究一下intel網絡卡mac位址是怎麼設定的。本文使用igb驅動,適合於i211等網絡卡。

1、通過某種方式讀取到mac位址,如果沒有,則會有隨機的mac位址,比如從eeprom中讀取乙個預先定義的mac號;

2、賦值到netdev的dev_addr——這個值是驅動中使用的位址;

3、註冊網路驅動,多個網絡卡則要進行多次註冊;

4、如果在使用者空間更改mac位址,則使用net_device_ops對應的函式,在該實現函式中要更新netdev的dev_addr,還有可能要修改具體晶元的eeprom。

一、igb驅動

igb的初始化在igb_main.c檔案igb_init_module()函式:

呼叫pci_register_driver註冊pci驅動,最終會呼叫到同檔案的igb_probe函式,在probe函式中進行各種初始化工作,包括讀取nvm的mac位址,註冊網路裝置。

/* copy the mac address out of the nvm */

// 讀取eeprom的mac位址,實際呼叫的函式為igb_read_mac_addr_82575()。

if (hw->mac.ops.read_mac_addr(hw))

dev_err(&pdev->dev, "nvm read error\n");

// 拷貝到netdev的dev_addr,這個是核心中使用的變數

memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);

// 判斷mac是否合法

if (!is_valid_ether_addr(netdev->dev_addr))

二、新增自定義的mac位址

// new add by late lee

unsigned char mac = ;

// 根據pci位址做mac區別,防止使用同一mac

if (pdev->bus->number == 1)

else if (pdev->bus->number == 2)

// 設定到nvm

// 存疑:手冊好像提到invm有次數限制,但看得不是很明白,故存疑

// igb_rar_set_qsel(adapter, mac, 0, adapter->vfs_allocated_count);

// end adding

/* copy the mac address out of the nvm */

if (hw->mac.ops.read_mac_addr(hw))

dev_err(&pdev->dev, "nvm read error\n");

// add by late lee

printk(kern_err "read mac addr: %pm but will update it!!\n", hw->mac.addr);

// 拷貝到hw結構體,其它函式需要使用

memcpy(hw->mac.addr, mac, 6);

// end adding

memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);

思路如下:

1、首先根據pci位址變換不同的mac號,否則多個網絡卡都有相同的mac號。

2、將自定義的mac位址拷貝到hw結構體的mac.addr中,注意,這個值會在igb_configure_rx函式(最後還是呼叫igb_rar_set_qsel)中呼叫到,因此要同步更新。如果只更新netdev的dev_addr是無法使用網路通訊的。

效果如下:

root@latelee:~# ifconfig

eth0      link encap:ethernet  hwaddr 6c:61:74:75:6c:65

...eth1      link encap:ethernet  hwaddr 6c:61:74:75:6c:66

...注:

1、網路操作函式集net_device_ops有設定mac號介面:ndo_set_mac_address,對於igb驅動呼叫igb_set_mac,最後會呼叫igb_rar_set_qsel寫入了invm中。

2、i211手冊,table 8-6,0x5404 + 8 *n、0x5400 + 8*n分別是高、低位址,共32位位元組。從**上看,除了讀取mac位址外,還有其它用途。

三、其它

下面是igb_probe函式棧呼叫:

[    3.295643]  dump_stack+0x41/0x57

[    3.300176]  igb_probe+0xedf/0x1100

[    3.304881]  local_pci_probe+0x17/0x50

[    3.309848]  ? pci_match_device+0xc3/0xe0

[    3.315148]  pci_device_probe+0x58/0x80

[    3.320202]  driver_probe_device+0x6f/0x200

[    3.325601]  ? pci_match_device+0xc3/0xe0

[    3.330825]  __driver_attach+0x79/0x80

[    3.335789]  bus_for_each_dev+0x68/0x90

[    3.340841]  driver_attach+0x19/0x20

[    3.345630]  ? driver_probe_device+0x200/0x200

[    3.351288]  bus_add_driver+0x14f/0x1d0

[    3.356342]  ? mdio_bus_init+0x38/0x38

[    3.361308]  ? mdio_bus_init+0x38/0x38

[    3.366272]  driver_register+0x54/0xe0

[    3.371242]  ? printk+0x38/0x3a

[    3.375598]  __pci_register_driver+0x2e/0x40

[    3.381084]  igb_init_module+0x5c/0x7a

[    3.386051]  do_one_initcall+0x72/0x190

[    3.391105]  ? mdio_bus_init+0x38/0x38

[    3.396081]  ? parse_args+0x1ab/0x370

[    3.400966]  ? __wake_up+0x40/0x50

[    3.405584]  kernel_init_freeable+0x124/0x1c9

[    3.411163]  ? kernel_init_freeable+0x1c9/0x1c9

[    3.416924]  kernel_init+0xb/0xe0

[    3.421455]  ret_from_kernel_thread+0x21/0x30

[    3.427029]  ? rest_init+0x70/0x70

---------------------  

原文:  

從載入igb驅動引出一些想法

1.載入igb驅動的問題 今日載入igb驅動,自以為已經將其 折磨 得爐火純青,可是在使用rss 0引數載入igb的時候卻發現只有eth0的佇列是8個,而其他的網絡卡佇列只有乙個。檢視其所有的引數都沒有找到解決方案,最終只有看 了。然而看完 之後使用modinfo igb發現,其rss引數後面有乙個...

單網絡卡ovs網絡卡配置

openstack 和kolla部署過程中經常需要配置ovs,單網絡卡的時候就需要增加乙個網橋配置 centos 單網絡卡 em1 em1原配置 cat etc sysconfig network scripts ifcfg em1 type ethernet bootproto static de...

多網絡卡指定網絡卡傳送資料

環境 2個網絡卡,分別在不同網路環境。區域網一 閘道器 10.9.16.254 區域網二 閘道器 10.9.24.1 電腦位址2 10.9.24.88 如下 include stdafx.h include include pragma comment lib,ws2 32.lib includev...