Skip to content

Ignore virtual interfaces for network IO accounting #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
#ifdef HAVE_LIBHWLOC
Panel_add(super, (Object*) CheckItem_newByRef("Show topology when selecting affinity by default", &(settings->topologyAffinity)));
#endif
#ifdef IGNORE_VIRTUAL_INTF
Panel_add(super, (Object*) CheckItem_newByRef("Ignore virtual network interfaces to count rx and tx values", &(settings->ignoreVirtualNetworkInterfaces)));
#endif

return this;
}
4 changes: 4 additions & 0 deletions NetworkIOMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ static void NetworkIOMeter_updateValues(Meter* this) {

/* update only every 500ms to have a sane span for rate calculation */
if (passedTimeInMs > 500) {
#ifdef IGNORE_VIRTUAL_INTF
hasNewData = Platform_getNetworkIO(&data, host->settings->ignoreVirtualNetworkInterfaces);
Copy link
Contributor

@Explorer09 Explorer09 Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like two kinds of function prototypes that depend on a compile time option. Even though the code can compile, the braces are unbalanced and would very likely confuse IDEs.

Another point is, you might not need to pass the boolean setting as a function argument. I suggest making it a member of the NetworkIOData (bool ignoreVirtualInterfaces;), and let NetworkIOMeter_updateValues set it before calling Platform_getNetworkIO.

#else
hasNewData = Platform_getNetworkIO(&data);
#endif
if (!hasNewData) {
status = RATESTATUS_NODATA;
} else if (cached_last_update == 0) {
Expand Down
3 changes: 3 additions & 0 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ typedef struct Settings_ {

bool changed;
uint64_t lastUpdate;
#ifdef IGNORE_VIRTUAL_INTF
bool ignoreVirtualNetworkInterfaces;
#endif
} Settings;

#define Settings_cpuId(settings, cpu) ((settings)->countCPUsFromOne ? (cpu)+1 : (cpu))
Expand Down
10 changes: 10 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,16 @@ if test "$enable_sensors" = yes || test "$my_htop_platform" = freebsd; then
AC_DEFINE([BUILD_WITH_CPU_TEMP], [1], [Define if CPU temperature option should be enabled.])
fi

AC_ARG_ENABLE([ignore-virtual-intf],
[AS_HELP_STRING([--enable-ignore-virtual-intf],
[Ignore virtual network interfaces in NetworkIO meter [default=no]])],
[enable_ignore_virtual_intf=yes],
[enable_ignore_virtual_intf=no])

if test "x$enable_ignore_virtual_intf" = "xyes"; then
AC_DEFINE([IGNORE_VIRTUAL_INTF], [1], [Ignore virtual network interfaces])
fi

# ----------------------------------------------------------------------


Expand Down
18 changes: 17 additions & 1 deletion linux/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ void Platform_setBindings(Htop_Action* keys) {
keys[KEY_F(20)] = Platform_actionHigherAutogroupPriority; // Shift-F8
}

#ifdef IGNORE_VIRTUAL_INTF
static bool Platform_isVirtualNetworkInterface(const char* name) {
return (strncmp(name, "docker", 6) == 0 || strncmp(name, "veth", 4) == 0 ||
strncmp(name, "virbr", 5) == 0 || strncmp(name, "tun", 3) == 0 ||
strncmp(name, "tap", 3) == 0 || strncmp(name, "vboxnet", 7) == 0);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is quite incomplete, and IMO hard-coding it like this is not ideal. I imagine the BSDs and other platforms may have a similar requirement.

The solution we use in pcp.io is to have an interfaces.conf configuration file (optionally, somewhere below /etc) that provides a regular expression - if interfaces match, they are culled from the calculation. This is platform agnostic - different platforms could have different config files - and indeed different sites may have their own naming conventions, custom drivers, etc.

This is the current regex we're using:

# interfaces.conf
#
# Regular expression describing network interfaces (from the
# network.interfaces.* metrics instance domain) that will be
# *excluded* from the network.all.* metrics calculation when
# aggregating (sum) stats from physical interfaces.
#
# Comments are the hash-to-end-of-line variety and any / all
# whitespace characters are removed before pmdalinux(1) uses
# regcomp(3) to compile the regular expression.
#

^(lo |
  bond[0-9]+ |
  team[0-9]+ |
  tun[0-9]+ |
  virbr[0-9]+ |
  virbr[0-9]+-nic |
  cni[0-9]+ |
  cni-podman[0-9]+ |
  docker[0-9]+ |
  veth[0-9]+ |
  face)$

Using a similar approach to this could simplify and improve this PR. Can do away with all the configure.ac/ifdef changes as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, instead of relying on the name of the interface, using the interface type can help simplify things even more. Otherwise you risk breaking things depending on some settings (e.g. systemd's unusable names vs. the good ones like eth0) …

#endif

const MeterClass* const Platform_meterTypes[] = {
&CPUMeter_class,
&ClockMeter_class,
Expand Down Expand Up @@ -695,7 +703,11 @@ bool Platform_getDiskIO(DiskIOData* data) {
return true;
}

#ifdef IGNORE_VIRTUAL_INTF
bool Platform_getNetworkIO(NetworkIOData* data, bool ignoreVirtual) {
#else
bool Platform_getNetworkIO(NetworkIOData* data) {
#endif
FILE* fp = fopen(PROCDIR "/net/dev", "r");
if (!fp)
return false;
Expand All @@ -712,7 +724,11 @@ bool Platform_getNetworkIO(NetworkIOData* data) {
&packetsTransmitted) != 5)
continue;

if (String_eq(interfaceName, "lo:"))
if (String_eq(interfaceName, "lo:")
#ifdef IGNORE_VIRTUAL_INTF
|| (ignoreVirtual == true && Platform_isVirtualNetworkInterface(interfaceName))
#endif
)
continue;

data->bytesReceived += bytesReceived;
Expand Down
5 changes: 4 additions & 1 deletion linux/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ void Platform_getPressureStall(const char* file, bool some, double* ten, double*
void Platform_getFileDescriptors(double* used, double* max);

bool Platform_getDiskIO(DiskIOData* data);

#ifdef IGNORE_VIRTUAL_INTF
bool Platform_getNetworkIO(NetworkIOData* data, bool ignoreVirtual);
#else
bool Platform_getNetworkIO(NetworkIOData* data);
#endif

void Platform_getBattery(double* percent, ACPresence* isOnAC);

Expand Down