Skip to content

Commit

Permalink
Merge pull request #23 from bmork/ethtool
Browse files Browse the repository at this point in the history
Optionally use ethtool statistics counters instead of /proc/net/dev
  • Loading branch information
troglobit authored Jan 16, 2021
2 parents 9481b19 + 543f355 commit 45765e3
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AM_CPPFLAGS = -DSYSCONFDIR=\"@sysconfdir@\" -DRUNSTATEDIR=\"@runstated
mini_snmpd_SOURCES = mini-snmpd.c mini-snmpd.h linux.c freebsd.c mib.c \
globals.c protocol.c utils.c compat.h
if HAVE_CONFUSE
mini_snmpd_SOURCES += conf.c
mini_snmpd_SOURCES += conf.c linux_ethtool.c
endif
mini_snmpd_CPPFLAGS = $(AM_CPPFLAGS)
mini_snmpd_CFLAGS = -W -Wall -Wextra -std=gnu99
Expand Down
19 changes: 19 additions & 0 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "ethtool-conf.h"
#include "mini-snmpd.h"

static cfg_t *cfg = NULL;
Expand Down Expand Up @@ -66,6 +67,21 @@ static size_t get_list(cfg_t *cfg, const char *key, char **list, size_t len)
int read_config(char *file)
{
int rc = 0;
cfg_opt_t ethtool_opts[] = {
CFG_STR("rx_bytes", NULL, CFGF_NONE),
CFG_STR("rx_mc_packets", NULL, CFGF_NONE),
CFG_STR("rx_bc_packets", NULL, CFGF_NONE),
CFG_STR("rx_packets", NULL, CFGF_NONE),
CFG_STR("rx_errors", NULL, CFGF_NONE),
CFG_STR("rx_drops", NULL, CFGF_NONE),
CFG_STR("tx_bytes", NULL, CFGF_NONE),
CFG_STR("tx_mc_packets", NULL, CFGF_NONE),
CFG_STR("tx_bc_packets", NULL, CFGF_NONE),
CFG_STR("tx_packets", NULL, CFGF_NONE),
CFG_STR("tx_errors", NULL, CFGF_NONE),
CFG_STR("tx_drops", NULL, CFGF_NONE),
CFG_END()
};
cfg_opt_t opts[] = {
CFG_STR ("location", NULL, CFGF_NONE),
CFG_STR ("contact", NULL, CFGF_NONE),
Expand All @@ -76,6 +92,7 @@ int read_config(char *file)
CFG_STR ("vendor", VENDOR, CFGF_NONE),
CFG_STR_LIST("disk-table", "/", CFGF_NONE),
CFG_STR_LIST("iface-table", NULL, CFGF_NONE),
CFG_SEC("ethtool", ethtool_opts, CFGF_MULTI | CFGF_TITLE | CFGF_NO_TITLE_DUPES),
CFG_END()
};

Expand Down Expand Up @@ -118,6 +135,8 @@ int read_config(char *file)

g_vendor = get_string(cfg, "vendor");

ethtool_xlate_cfg(cfg);

error:
cfg_free(cfg);
return rc;
Expand Down
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ AC_ARG_ENABLE(ipv6,
AS_HELP_STRING([--disable-ipv6], [Disable IPv6 support, enabled by default]),
[enable_ipv6=$enableval], [enable_ipv6=yes])

AC_ARG_ENABLE(ethtool,
AS_HELP_STRING([--enable-ethtool], [Enable ethtool interface stats, disabled by default]),
[enable_ethtool=$enableval], [enable_ethtool=no])

### Enable features ###########################################################################
AS_IF([test "x$with_vendor" != "xno"],[
AS_IF([test "x$vendor" = "xyes"],[
Expand All @@ -74,6 +78,9 @@ AS_IF([test "x$enable_demo" = "xyes"],[
AS_IF([test "x$enable_ipv6" != "xno"],[
AC_DEFINE(CONFIG_ENABLE_IPV6, 1, [Define to enable IPv6 support.])])

AS_IF([test "x$enable_ethtool" != "xno"],[
AC_DEFINE(CONFIG_ENABLE_ETHTOOL, 1, [Define to enable ethtool stats.])])

# Check where to install the systemd .service file
AS_IF([test "x$with_systemd" = "xyes" -o "x$with_systemd" = "xauto"], [
def_systemd=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
Expand Down Expand Up @@ -124,6 +131,7 @@ cat <<EOF
mini-snmpd.conf...: $with_config
demo mode.........: $enable_demo
systemd...........: $with_systemd
ethtool stats.....: $enable_ethtool

------------- Compiler version --------------
$($CC --version || true)
Expand Down
37 changes: 37 additions & 0 deletions ethtool-conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Linux ethtool helpers
*
* Copyright (C) 2020 Bjørn Mork <bjorn@mork.no>
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in the
* packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* See COPYING for GPL licensing information.
*/

#ifndef ETHTOOL_CONF_H_
#define ETHTOOL_CONF_H_

#include <confuse.h>
#include "config.h"
#include "mini-snmpd.h"

#ifdef CONFIG_ENABLE_ETHTOOL
void ethtool_xlate_cfg(cfg_t *cfg);
#else
static inline void ethtool_xlate_cfg(cfg_t *cfg)
{
if (cfg_size(cfg, "ethtool") > 0)
logit(LOG_WARNING, 0, "No ethtool support. Ignoring config section");
}
#endif

#endif /* ETHTOOL_CONF_H_ */

/* vim: ts=4 sts=4 sw=4 nowrap
*/

26 changes: 14 additions & 12 deletions linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,20 @@ void get_netinfo(netinfo_t *netinfo)
sll = (struct sockaddr_ll *)ifa->ifa_addr;
memcpy(netinfo->mac_addr[i], sll->sll_addr, sizeof(netinfo->mac_addr[i]));

/* XXX: Tx multicast and Rx/Tx broadcast not available atm. */
fields[i].prefix = g_interface_list[i];
fields[i].len = 12;
fields[i].value[0] = &netinfo->rx_bytes[i];
fields[i].value[1] = &netinfo->rx_packets[i];
fields[i].value[2] = &netinfo->rx_errors[i];
fields[i].value[3] = &netinfo->rx_drops[i];
fields[i].value[7] = &netinfo->rx_mc_packets[i];
fields[i].value[8] = &netinfo->tx_bytes[i];
fields[i].value[9] = &netinfo->tx_packets[i];
fields[i].value[10] = &netinfo->tx_errors[i];
fields[i].value[11] = &netinfo->tx_drops[i];
if (ethtool_gstats(i, netinfo, &fields[i]) < 0) {
/* XXX: Tx multicast and Rx/Tx broadcast not available atm. */
fields[i].prefix = g_interface_list[i];
fields[i].len = 12;
fields[i].value[0] = &netinfo->rx_bytes[i];
fields[i].value[1] = &netinfo->rx_packets[i];
fields[i].value[2] = &netinfo->rx_errors[i];
fields[i].value[3] = &netinfo->rx_drops[i];
fields[i].value[7] = &netinfo->rx_mc_packets[i];
fields[i].value[8] = &netinfo->tx_bytes[i];
fields[i].value[9] = &netinfo->tx_packets[i];
fields[i].value[10] = &netinfo->tx_errors[i];
fields[i].value[11] = &netinfo->tx_drops[i];
}

if (-1 == read_file_value(&netinfo->if_mtu[i], "/sys/class/net/%s/mtu", g_interface_list[i]))
netinfo->if_mtu[i] = 1500; /* Fallback */
Expand Down
Loading

0 comments on commit 45765e3

Please sign in to comment.