- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
请帮我获取 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)
如何修复此问题并不能解决问题,并且无法使用 ETHTOOL_GLINKSETTINGS 查询接口(interface)属性。看一下头文件就可以看出这一点。
from /usr/include/linux/ethtool.h
1532 /**
1533 * struct ethtool_link_settings - link control and status
1534 *
1535 * IMPORTANT, Backward compatibility notice: When implementing new
1536 * user-space tools, please first try %ETHTOOL_GLINKSETTINGS, and
1537 * if it succeeds use %ETHTOOL_SLINKSETTINGS to change link
1538 * settings; do not use %ETHTOOL_SSET if %ETHTOOL_GLINKSETTINGS
1539 * succeeded: stick to %ETHTOOL_GLINKSETTINGS/%SLINKSETTINGS in
1540 * that case. Conversely, if %ETHTOOL_GLINKSETTINGS fails, use
1541 * %ETHTOOL_GSET to query and %ETHTOOL_SSET to change link
1542 * settings; do not use %ETHTOOL_SLINKSETTINGS if
1543 * %ETHTOOL_GLINKSETTINGS failed: stick to
1544 * %ETHTOOL_GSET/%ETHTOOL_SSET in that case.
我确实看到了与报告相同的行为,并观察到使用 ETHTOOL_GSET
使用 ETHTOOL_GSET
~/working/exp$./ethtool-exp ens33
ens33 | ethtool_cmd.speed = 1000
ens33 | ethtool_cmd.duplex = 1
ens33 | ethtool_cmd.advertising= 239
ens33 | ethtool_cmd.autoneg = 1
ens33 | ethtool_cmd.port= 0
ens33 | ethtool_cmd.phy_address= 0
ens33 | ethtool_cmd.transceiver= 0
ens33 | ethtool_cmd.maxrxpkt= 0
ens33 | ethtool_cmd.maxtxpkt= 0
使用 ETHTOOL_GLINKSETTINGS
~/working/exp$./ethtool-glink ens33
ens33 | ethtool_link_settings.speed = 0
ens33 | ethtool_link_settings.duplex = 0
ens33 | ethtool_link_settings.autoneg = 0
关于c++ - 如何获取ethtool设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41822920/
我正在一台 Linux 机器上创建一个以太网帧嗅探/转发程序,我将用它来嗅探从计算机传输到以太网的帧,反之亦然。 我正在尝试激活暂停帧,以便不会丢失任何数据包,因为到路由器的两步传输可能会导致一些延迟
我正在尝试使用命令 ethtool_cmd_speed(&cmd); 来获取接口(interface)速度;但是当我尝试编译它时,它会抛出错误,例如 对“ethtool_cmd_speed”的 und
如 this question ,我正在查看 ethtool -k eth0 的输出,我看到有些东西被标记为“[固定]”: rx-checksumming: off [fixed] tx-checks
我使用“ethtool -k”命令运行ethtool来查询网卡的卸载功能,输出如下: ethtool -k eth0 scatter-gather: on tx-scatter-gather: on
Linux 的 ethtool 命令中是否有一个选项可以直接从 NIC 检索当前传入的比特率或每秒数据包数? 最佳答案 不是每秒数据包,而是整体 RX/TX。您可以围绕它编写一个脚本: ethtool
我正在尝试使用 Ethtool ioctl API 从我的 NIC 检索链接速度数据,但我只是在 ethtool_link_settings 实例中取回零。使用 ethtool 命令行工具返回预期值,
我的 NIC 驱动程序不支持 H/W LRO,但会在驱动程序中模拟 LRO。现在可以使用“ethtool -k ethx gro off”禁用 GRO(这是一个 linux 网络堆栈功能)。这也适用于
用ifconfig命令工具检查出现Dropped packet。 The Dropped 统计了很多卷。 $ ifconfig eth0 eth0 Link encap:イーサネット ハー
我需要读取网络适配器的永久(内置)MAC 地址。由于MAC地址很容易被欺骗,我需要读取写在EEPROM上的真实MAC地址。我需要在 Linux 上使用 C++ 来完成。 我尝试使用 ethtool这非
我正在查看 virtio 驱动程序的代码: https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/tree/drivers/
ethtool 联机帮助页只给出了一个模糊的解释: rxvlan on|off Specifies whether RX VLAN acceleration should b
我尝试在 docker python:3.9.13-alpine3.16 中安装 python 模块 psutil但是报如下错误: Building wheels for collected pack
我是一名优秀的程序员,十分优秀!