请帮我获取ethtool设置(速度,双工,自动求反)。
如果使用ETHTOOL_GSET,我将获得ethtool设置。但是在ethtool.h中编写为使用ETHTOOL_GLINKSETTINGS而不是ETHTOOL_GSET。我不知道如何使用ETHTOOL_GLINKSETTINGS。
ETHTOOL_GSET
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
int main()
{
int s; // socket
int r; // result
struct ifreq ifReq;
strncpy(ifReq.ifr_name, "enp3s0", sizeof(ifReq.ifr_name));
struct ethtool_cmd ethtoolCmd;
ethtoolCmd.cmd = ETHTOOL_GSET;
ifReq.ifr_data = ðtoolCmd;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1)
{
r = ioctl(s, SIOCETHTOOL, &ifReq);
if (s != -1)
{
printf("%s | ethtool_cmd.speed = %i \n", ifReq.ifr_name, ethtoolCmd.speed);
printf("%s | ethtool_cmd.duplex = %i \n", ifReq.ifr_name, ethtoolCmd.duplex);
printf("%s | ethtool_cmd.autoneg = %i \n", ifReq.ifr_name, ethtoolCmd.autoneg);
}
else
printf("Error #r");
close(s);
}
else
printf("Error #s");
return 0;
}
结果:
enp3s0 | ethtool_cmd.speed = 1000
enp3s0 | ethtool_cmd.duplex = 1
enp3s0 | ethtool_cmd.autoneg = 1
ETHTOOL_GLINKSETTINGS
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
int main()
{
int s; // socket
int r; // result
struct ifreq ifReq;
strncpy(ifReq.ifr_name, "enp3s0", sizeof(ifReq.ifr_name));
struct ethtool_link_settings ethtoolLinkSettings;
ethtoolLinkSettings.cmd = ETHTOOL_GLINKSETTINGS;
ifReq.ifr_data = ðtoolLinkSettings;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1)
{
r = ioctl(s, SIOCETHTOOL, &ifReq);
if (s != -1)
{
printf("%s | ethtool_link_settings.speed = %i \n", ifReq.ifr_name, ethtoolLinkSettings.speed);
printf("%s | ethtool_link_settings.duplex = %i \n", ifReq.ifr_name, ethtoolLinkSettings.duplex);
printf("%s | ethtool_link_settings.autoneg = %i \n", ifReq.ifr_name, ethtoolLinkSettings.autoneg);
}
else
printf("Error #r");
close(s);
}
else
printf("Error #s");
return 0;
}
结果:
enp3s0 | ethtool_link_settings.speed = 0
enp3s0 | ethtool_link_settings.duplex = 45
enp3s0 | ethtool_link_settings.autoneg = 0
为什么ETHTOOL_GLINKSETTINGS返回不正确的值?问题是什么?
最佳答案
该问题是由以下错字引起的:
r = ioctl(s, SIOCETHTOOL, &ifReq);
if (s != -1)
您本来要检查
r
的值,但是却错误地检查了s
。如果您纠正了该错误,我相信您会得到一个错误(EOPNOTSUPP,不支持操作)。