[英]How can I get the pci address of a newly added NIC in C program without polling the kernel?
按照下面的鏈接,我了解到通過創建netlink套接字並收聽RTMGRP_LINK,我們可以檢測到事件(網絡接口創建/刪除/上/下事件)。
如何在不輪詢內核的情況下在C程序中監視NIC狀態(上/下)?
一旦檢測到新添加接口的pci地址,是否可以獲取?
知道其接口名稱(例如eth0)后,就可以使用ethtool API查詢該信息:
#include <linux/ethtool.h>
#include <linux/sockios.h>
.. and other socket headers.
...
int sock = socket(PF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;
struct ethtool_cmd cmd;
struct ethtool_drvinfo drvinfo;
memset(&ifr, 0, sizeof ifr);
memset(&cmd, 0, sizeof cmd);
memset(&drvinfo, 0, sizeof drvinfo);
strcpy(ifr.ifr_name, "eno1");
ifr.ifr_data = &drvinfo;
drvinfo.cmd = ETHTOOL_GDRVINFO;
if(ioctl(sock, SIOCETHTOOL, &ifr) < 0) {
perror("ioctl")
return;
}
現在,PCI總線地址在drvinfo.bus_info
可以作為字符串drvinfo.bus_info
-假設NIC實際上是PCI設備。
您也可以通過命令行進行檢查:
$ ethtool -i eno1
driver: e1000e
version: 2.3.2-k
firmware-version: 0.13-4
bus-info: 0000:00:19.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.