如何使用ethtooldrinfo来收集网络接口的驱动程序信息



我有一个网络接口,显示数据如下:

driver: r8152 
version: v1.12.12
firmware-version: rtl8153a-4 v2 02/07/20
expansion-rom-version:
bus-info: usb-0000:00:14.0-9
supports-statistics: yes
supports-test: no
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: no

然而,我无法通过这样的ioctl调用收集驾驶员信息:

socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (socketfd == -1)
printf ("error:socketfd no open");
struct ethtool_drvinfo drvrinfo = {0};
drvrinfo.cmd = ETHTOOL_GDRVINFO;
int x = ioctl(socketfd, SIOCETHTOOL, &drvrinfo);`

我不确定确切的流量,因为我是第一次使用它。请帮助

此信息的简单Linux转储。将enp0s5更改为您的接口名称。

样本输出:

% ./get-driver-info
driver: virtio_net
version: 1.0.0
firmware-version:
expansion-rom-version:
bus-info: 0000:00:05.0
supports-statistics: yes
supports-test: no
supports-eeprom-access: no
supports-priv-flags: no

Linux来源:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <net/if.h>
int main() {
char *devname = "enp0s5";
struct ifreq sif;
struct ethtool_drvinfo d;
int ret;
int sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0){
printf("Error socketn");
exit(1);
}
memset(&sif, 0, sizeof(struct ifreq));
strncpy(sif.ifr_name, devname, strlen(devname));
d.cmd = ETHTOOL_GDRVINFO;
sif.ifr_data = (caddr_t)&d;
ret = ioctl(sd, SIOCETHTOOL, &sif);
if(ret == -1){
perror("ioctl");
return 1;
}
printf("driver: %snversion: %sn", d.driver, d.version);
printf("firmware-version: %sn", d.fw_version);
printf("expansion-rom-version: %sn", d.fw_version);
printf("bus-info: %sn", d.bus_info);
printf("supports-statistics: %sn", d.n_stats ? "yes" : "no");
printf("supports-test: %sn", d.testinfo_len ? "yes" : "no");
printf("supports-eeprom-access: %sn", d.eedump_len ? "yes" : "no");
printf("supports-priv-flags: %sn", d.n_priv_flags ? "yes" : "no");
}

这些信息不存储在套接字中,但拥有一个开放的套接字是从内核查询有关特定网络接口的信息的一种方便方式。

最新更新