暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

ethtool 命令

原创 逆风飞翔 2023-04-18
679

以CentOS为例,使用如下命令安装:

# yum -y install ethtool net-tools

ethtool命令用于获取以太网卡的配置信息,或者修改这些配置

-a 查看网卡中 接收模块RX、发送模块TX和Autonegotiate模块的状态:启动on 或 停用off。
-A 修改网卡中 接收模块RX、发送模块TX和Autonegotiate模块的状态:启动on 或 停用off。

-c display the Coalesce information of the specified ethernet card。
-C Change the Coalesce setting of the specified ethernet card。

-g Display the rx/tx ring parameter information of the specified ethernet card。
-G change the rx/tx ring setting of the specified ethernet card。

-i 显示网卡驱动的信息,如驱动的名称、版本等。
-d 显示register dump信息, 部分网卡驱动不支持该选项。

-e 显示EEPROM dump信息,部分网卡驱动不支持该选项。
-E 修改网卡EEPROM byte。

-k 显示网卡Offload参数的状态:on 或 off,包括rx-checksumming、tx-checksumming等。
-K 修改网卡Offload参数的状态。

-p 用于区别不同ethX对应网卡的物理位置,常用的方法是使网卡port上的led不断的闪;N指示了网卡闪的持续时间,以秒为单位。
-r 如果auto-negotiation模块的状态为on,则restarts auto-negotiation。

-S 显示NIC- and driver-specific 的统计参数,如网卡接收/发送的字节数、接收/发送的广播包个数等。

-t 让网卡执行自我检测,有两种模式:offline or online。

-s 修改网卡的部分配置,包括网卡速度、单工/全双工模式、mac地址等。


查看当前网卡的buffer size情况ethtool -g eth0

Ring parameters for eth0:
Pre-set maximums:
RX: 4096RX Mini: 0
RX Jumbo: 0
TX: 4096
Current hardware settings:
RX: 256
RX Mini: 0
RX Jumbo: 0
TX: 256

Pre-set maximums 中的 RX/TX 值为该网卡的 Buffer size 最大值;
Current hardware settings 中 RX/TX 值代表该网卡当前的 Buffer size 大小。
所以,设置的 Current hardware settings 的 RX/TX 值必须在 Pre-set maximums 的限制之内。

(3) RX overruns

表示了 fifo 的 overruns,这是由于 Ring Buffer(aka Driver Queue) 传输的 IO 大于 kernel 能够处理的 IO 导致的,而 Ring Buffer 则是指在发起 IRQ 请求之前的那块 buffer。很明显,
overruns 的增大意味着数据包没到 Ring Buffer 就被网卡物理层给丢弃了,而 CPU 无法即使的处理中断是造成 Ring Buffer 满的原因之一,上面那台有问题的机器就是因为 interruprs 分布的不均匀(都压在 core0),没有做 affinity 而造成的丢包。
缓冲区访问

网卡的内核缓冲区,是在PC内存中,由内核控制,而网卡会有FIFO缓冲区,或者ring buffer,这应该将两者区分开。FIFO比较小,里面有数据便会尽量将数据存在内核缓冲中。

网卡中的缓冲区既不属于内核空间,也不属于用户空间。它属于硬件缓冲,允许网卡与操作系统之间有个缓冲;

内核缓冲区在内核空间,在内存中,用于内核程序,做为读自或写往硬件的数据缓冲区;

用户缓冲区在用户空间,在内存中,用于用户程序,做为读自或写往硬件的数据缓冲区;

另外,为了加快数据的交互,可以将内核缓冲区映射到用户空间,这样,内核程序和用户程序就可以同时访问这一区间了。

对于有ring buffer的网卡,ring buffer是由驱动与网卡共享的,所以内核可以直接访问ring buffer,一般拷贝frames的副本到自己的内核空间进行处理(deliver到上层协议,之后的一个个skb就是按skb的指针传递方式传递,直到用户获得数据,所以,对于ring buffer网卡,大量拷贝发生在frame从ring buffer传递到内核控制的计算机内存里)。

3 丢包排查

网卡工作在数据链路层,数据量链路层,会做一些校验,封装成帧。我们可以查看校验是否出错,确定传输是否存在问题。然后从软件层面,是否因为缓冲区太小丢包。

3.1 先查看硬件情况

一台机器经常收到丢包的报警,先看看最底层的有没有问题:

(1) 查看工作模式是否正常

[root@localhost ~]# ethtool eth0 | egrep 'Speed|Duplex'

Speed: 1000Mb/s

Duplex: Full

(2) 查看检验是否正常

[root@localhost ~]# ethtool -S eth0 | grep crc

rx_crc_errors: 0


Speed,Duplex,CRC 之类的都没问题,基本可以排除物理层面的干扰。

3.2 通过 ifconfig 可以看到 overruns 是否一直增大

for i in `seq 1 100`; do ifconfig eth2 | grep RX | grep overruns; sleep 1; done

这里一直增加

RX packets:346547657 errors:0 dropped:0 overruns:35345 frame:0


3.3 查看buffer大小


可以通过ethtool来修改网卡的buffer size ,首先要网卡支持,我的服务器是是INTEL 的1000M网卡,我们看看ethtool说明

-g   –show-ringQueries the specified ethernet device for rx/tx ring parameter information.
-G   –set-ringChanges the rx/tx ring parameters of the specified ethernet device.


(1) 查看当前网卡的buffer size情况

ethtool -g eth0

[root@localhost ~]# ethtool -g eth0

Ring parameters for eth0:

Pre-set maximums:

RX: 4096

RX Mini: 0

RX Jumbo: 0

TX: 4096

Current hardware settings:

RX: 256

RX Mini: 0

RX Jumbo: 0

TX: 256


3.4 修改buffer size大小

ethtool -G eth0 rx 2048

ethtool -G eth0 tx 2048


调整 RX/TX 参数:


[root@centos75 ~]# ethtool -G eth0 rx 18000 tx 2500

将 ethtool 设置永久保存
将 ethtool 设置永久保存在网络设备有两种方法,一种是写入网口配置文件中,一种是开机自启动脚本。

解决方法一:
ethtool 设置可通过 /etc/sysconfig/network-scripts/ifcfg-ethX 文件保存,从而在设备下次启动时激活选项。
例如:ethtool -s eth0 speed 100 duplex full autoneg off
此指令将eth0设备设置为全双工自适应,速度为100Mbs。若要eth0启动时设置这些参数, 修改文件/etc/sysconfig/network-scripts/ifcfg-eth0 ,添加如下一行:
ETHTOOL_OPTS="speed 100 duplex full autoneg off"

解决方法二:
将ethtool设置写入/etc/rc.d/rc.local之

chmod +x /etc/rc.d/rc.local

[root@centos75 ~]# vi /etc/rc.d/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
ethtool -G eth0 rx 2048 tx 2048


由于我的是rx包会有droped的情况

我们用ethtool -G eth0 rx 2048

同样对于eth1也是如此ethtool -G eth1 rx 2048

再看看修改过后的ethtool -g eth0

Ring parameters for eth0:
Pre-set maximums:
RX: 4096
RX Mini: 0
RX Jumbo: 0
TX: 4096
Current hardware settings:
RX: 2048
RX Mini: 0
RX Jumbo: 0
TX: 2048

命令描述:

ethtool 是用于查询及设置网卡参数的命令。


使用概要:
ethtool ethx //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0、eth1等等
ethtool –h //显示ethtool的命令帮助(help)

ethtool –i ethX //查询ethX网口的相关信息
ethtool –d ethX //查询ethX网口注册性信息

ethtool –r ethX //重置ethX网口到自适应模式
ethtool –S ethX //查询ethX网口收发包统计

ethtool –s ethX [speed 10|100|1000] [duplex half|full] [autoneg on|off] //设置网口速率10/100/1000M、设置网口半/全双工、设置网口是否自协商


使用举例:
1)[root@Linux /]# ethtool eth1
Settings for eth1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: d
Link detected: yes
2)[root@linux /]# ethtool -i eth1
driver: e1000
version: 5.0.43-k1
firmware-version: N/A
bus-info: 06:08.1
3)[root@linux /]# ethtool -S eth1
NIC statistics:
rx_packets: 58068300
tx_packets: 87124083
rx_bytes: 1589713008
tx_bytes: 2165825901
rx_errors: 0
tx_errors: 0
rx_dropped: 0
tx_dropped: 0
multicast: 0
collisions: 0
rx_length_errors: 0
rx_over_errors: 0
rx_crc_errors: 0
rx_frame_errors: 0
rx_fifo_errors: 0
rx_missed_errors: 0
tx_aborted_errors: 0
tx_carrier_errors: 0
tx_fifo_errors: 0
tx_heartbeat_errors: 0
tx_window_errors: 0

4)[root@linux /]# ethtool -s eth1 autoneg off speed 100 duplex full


相关参考:
1)[root@linux /]# which ethtool //查询 ethtool 存放的路径
/sbin/ethtool
2)[root@linux /]# rpm -qf /sbin/ethtool //查询 ethtool 的版本信息
ethtool-1.6-5
3)将 ethtool 设置永久保存在网络设备的方法
解决方法一:
ethtool 设置可通过 /etc/sysconfig/network-scripts/ifcfg-ethX 文件保存,从而在设备下次启动时激活选项。

例如:ethtool -s eth0 speed 100 duplex full autoneg off

此指令将eth0设备设置为全双工自适应,速度为100Mbs。若要eth0启动时设置这些参数, 修改文件/etc/sysconfig/network-scripts/ifcfg-eth0 ,
添加如下一行:
ETHTOOL_OPTS="speed 100 duplex full autoneg off"

解决方法二:
将ethtool设置写入/etc/rc.d/rc.local之中。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论