linux 如何使用ethtool_drvinfo收集网络接口的驱动程序信息?

mgdq6dx1  于 2022-12-26  发布在  Linux
关注(0)|答案(1)|浏览(206)

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

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);`

我第一次使用,不太清楚具体的流程,请帮助

qf9go6mv

qf9go6mv1#

这个信息的简单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 socket\n");
    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: %s\nversion: %s\n", d.driver, d.version);
  printf("firmware-version: %s\n", d.fw_version);
  printf("expansion-rom-version: %s\n", d.fw_version);
  printf("bus-info: %s\n", d.bus_info);
  printf("supports-statistics: %s\n", d.n_stats ? "yes" : "no");
  printf("supports-test: %s\n", d.testinfo_len ? "yes" : "no");
  printf("supports-eeprom-access: %s\n", d.eedump_len ? "yes" : "no");
  printf("supports-priv-flags: %s\n", d.n_priv_flags ? "yes" : "no");
}

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

相关问题