Skip to content

Commit d4eea1f

Browse files
[Linux] net_if_stats() returns an incorrect speed value for 100GbE network cards (#2096)
1 parent 072e3bf commit d4eea1f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

HISTORY.rst

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
``min`` and ``max`` are in MHz.
3131
- 2050_, [Linux]: `virtual_memory()`_ may raise ``ValueError`` if running in a
3232
LCX container.
33+
- 2095_, [Linux]: `net_if_stats()` returns incorrect interface speed for 100GbE
34+
network cards
3335

3436
5.9.0
3537
=====

psutil/_psutil_linux.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ psutil_net_if_duplex_speed(PyObject* self, PyObject* args) {
417417
int sock = 0;
418418
int ret;
419419
int duplex;
420+
__u32 uint_speed;
420421
int speed;
421422
struct ifreq ifr;
422423
struct ethtool_cmd ethcmd;
@@ -438,7 +439,15 @@ psutil_net_if_duplex_speed(PyObject* self, PyObject* args) {
438439

439440
if (ret != -1) {
440441
duplex = ethcmd.duplex;
441-
speed = ethcmd.speed;
442+
// speed is returned from ethtool as a __u32 ranging from 0 to INT_MAX
443+
// or SPEED_UNKNOWN (-1)
444+
uint_speed = ethtool_cmd_speed(&ethcmd);
445+
if (uint_speed == (__u32)SPEED_UNKNOWN || uint_speed > INT_MAX) {
446+
speed = 0;
447+
}
448+
else {
449+
speed = (int)uint_speed;
450+
}
442451
}
443452
else {
444453
if ((errno == EOPNOTSUPP) || (errno == EINVAL)) {

0 commit comments

Comments
 (0)