c++ 如何获取ethtool设置?

isr3a4wc  于 2022-11-27  发布在  其他
关注(0)|答案(3)|浏览(277)

请帮助我获得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 = &ethtoolCmd;

    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_闪烁设置

#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 = &ethtoolLinkSettings;

    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返回不正确的值?问题是什么?

tv6aics1

tv6aics11#

该问题是由以下排印错误引起的:

r = ioctl(s, SIOCETHTOOL, &ifReq);
    if (s != -1)

您本想检查r的值,但错误地检查了s。如果您更正了该错误,我相信您将得到一个错误(EOPNOTSUPP,不支持操作)。

amrnrhlw

amrnrhlw2#

这段代码肯定有问题

r = ioctl(s, SIOCETHTOOL, &ifReq);
    if (s != -1)

但修复此问题并不能解决问题,而且无法使用ETHTOOL_GLINKSETTINGS查询接口属性。查看头文件即可了解这一点。

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
fnx2tebb

fnx2tebb3#

正如您在ethtool.c中的函数do_ioctl_glinksettings()中所看到的,您应该在ethtoolLinkSettings缓冲区后面为可变大小的成员map_supportedmap_advertisingmap_lp_advertising保留一些空间(只能通过link_mode_masks间接访问)。
作为第一次调用ioctl()的结果,您将得到link_mode_masks_nwords中这些成员的真实的大小(带负号)。然后将link_mode_masks_nwords设置为该实际大小(非负数),并再次调用ioctl()。然后您将得到实际数据。
例如(未检测)

int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd > 0)
{
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, "enp3s0");

  char buffer[sizeof(struct ethtool_link_settings) + sizeof(__u32) * 3 * 128];
  struct ethtool_link_settings* ethtoolLinkSettings = (struct ethtool_link_settings*)buffer;
  memset(buffer, 0, sizeof(buffer));
  ethtoolLinkSettings->cmd = ETHTOOL_GLINKSETTINGS;
  ifr.ifr_data = (caddr_t)ethtoolLinkSettings;

  if (ioctl(fd, SIOCETHTOOL, &ifr) != -1)
  {
    if (ethtoolLinkSettings->link_mode_masks_nwords < 0)
    {
      ethtoolLinkSettings->cmd = ETHTOOL_GLINKSETTINGS;
      ethtoolLinkSettings->link_mode_masks_nwords = -ethtoolLinkSettings->link_mode_masks_nwords;

      if (ioctl(fd, SIOCETHTOOL, &ifr) != -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);
      }
    }
  }
}

相关问题