| 1 |
/* |
| 2 |
* standard Net-SNMP includes |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-includes.h> |
| 7 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 8 |
#include <net-snmp/data_access/interface.h> |
| 9 |
|
| 10 |
#if !(defined(HAVE_LIBNL3) && defined(HAVE_NETLINK_ROUTE_LINK_H)) |
| 11 |
#ifndef HAVE_LIBNL3 |
| 12 |
#error libnl3-devel (RedHat derivatives) / libnl-3-dev (Debian derivatives) required |
| 13 |
#endif |
| 14 |
#ifndef HAVE_NETLINK_ROUTE_LINK_H |
| 15 |
#error libnl-route-3-dev required (Debian derivatives) |
| 16 |
#endif |
| 17 |
#if !defined(HAVE_LIBNL3) && !defined(HAVE_NETLINK_ROUTE_LINK_H) && defined(__linux__) |
| 18 |
#error pkg-config/pkgconfig is required to detect libnl-3 |
| 19 |
#endif |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <netlink/cache.h> |
| 23 |
#include <netlink/netlink.h> |
| 24 |
#include <netlink/route/addr.h> |
| 25 |
|
| 26 |
/* |
| 27 |
* include our parent header |
| 28 |
*/ |
| 29 |
#include "etherlike-mib/dot3StatsTable/dot3StatsTable.h" |
| 30 |
#include "etherlike-mib/dot3StatsTable/dot3StatsTable_data_access.h" |
| 31 |
#include "etherlike-mib/dot3StatsTable/ioctl_imp_common.h" |
| 32 |
|
| 33 |
static void dot3StatsTable_add_interface(netsnmp_container *container, |
| 34 |
int if_index, |
| 35 |
struct rtnl_link *rtnl_link) |
| 36 |
{ |
| 37 |
uint64_t tx_err, tx_drop, tx_colls, tx_carrier; |
| 38 |
uint64_t rx_errs, rx_fifo, rx_frame; |
| 39 |
dot3StatsTable_rowreq_ctx *row; |
| 40 |
int rc; |
| 41 |
|
| 42 |
row = dot3StatsTable_allocate_rowreq_ctx(NULL); |
| 43 |
if (!row) |
| 44 |
return; |
| 45 |
dot3StatsTable_indexes_set(row, if_index); |
| 46 |
|
| 47 |
/* |
| 48 |
* See also dev_seq_printf_stats() in the Linux kernel source file |
| 49 |
* net/core/net-procfs.c. |
| 50 |
*/ |
| 51 |
rx_errs = rtnl_link_get_stat(rtnl_link, RTNL_LINK_RX_ERRORS); |
| 52 |
rx_fifo = rtnl_link_get_stat(rtnl_link, RTNL_LINK_RX_DROPPED) + |
| 53 |
rtnl_link_get_stat(rtnl_link, RTNL_LINK_RX_MISSED_ERR); |
| 54 |
rx_frame = rtnl_link_get_stat(rtnl_link, RTNL_LINK_MULTICAST); |
| 55 |
|
| 56 |
tx_err = rtnl_link_get_stat(rtnl_link, RTNL_LINK_TX_ERRORS); |
| 57 |
tx_drop = rtnl_link_get_stat(rtnl_link, RTNL_LINK_TX_DROPPED); |
| 58 |
tx_colls = rtnl_link_get_stat(rtnl_link, RTNL_LINK_TX_ABORT_ERR); |
| 59 |
tx_carrier = rtnl_link_get_stat(rtnl_link, RTNL_LINK_TX_CARRIER_ERR); |
| 60 |
|
| 61 |
row->column_exists_flags |= |
| 62 |
COLUMN_DOT3STATSINDEX_FLAG | |
| 63 |
COLUMN_DOT3STATSFCSERRORS_FLAG | |
| 64 |
COLUMN_DOT3STATSINTERNALMACRECEIVEERRORS_FLAG | |
| 65 |
COLUMN_DOT3STATSFRAMETOOLONGS_FLAG | |
| 66 |
COLUMN_DOT3STATSDEFERREDTRANSMISSIONS_FLAG | |
| 67 |
COLUMN_DOT3STATSINTERNALMACTRANSMITERRORS_FLAG | |
| 68 |
COLUMN_DOT3STATSSINGLECOLLISIONFRAMES_FLAG | |
| 69 |
COLUMN_DOT3STATSCARRIERSENSEERRORS_FLAG; |
| 70 |
row->data.dot3StatsFCSErrors = rx_errs; |
| 71 |
row->data.dot3StatsInternalMacReceiveErrors = rx_fifo; |
| 72 |
row->data.dot3StatsFrameTooLongs = rx_frame; |
| 73 |
row->data.dot3StatsDeferredTransmissions = tx_err; |
| 74 |
row->data.dot3StatsInternalMacTransmitErrors = tx_drop; |
| 75 |
row->data.dot3StatsSingleCollisionFrames = tx_colls; |
| 76 |
row->data.dot3StatsCarrierSenseErrors = tx_carrier; |
| 77 |
|
| 78 |
rc = CONTAINER_INSERT(container, row); |
| 79 |
netsnmp_assert(rc == 0); |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* @retval 0 success |
| 84 |
* @retval -1 getifaddrs failed |
| 85 |
* @retval -2 memory allocation failed |
| 86 |
*/ |
| 87 |
int dot3StatsTable_container_load_impl(netsnmp_container *container) |
| 88 |
{ |
| 89 |
struct nl_cache *link_cache; |
| 90 |
struct nl_object *nl_object; |
| 91 |
struct nl_sock *nl_sock; |
| 92 |
int ret; |
| 93 |
|
| 94 |
nl_sock = nl_socket_alloc(); |
| 95 |
if (!nl_sock) |
| 96 |
return -1; |
| 97 |
ret = nl_connect(nl_sock, NETLINK_ROUTE); |
| 98 |
if (ret < 0) |
| 99 |
return -1; |
| 100 |
ret = rtnl_link_alloc_cache(nl_sock, AF_UNSPEC, &link_cache); |
| 101 |
if (ret) { |
| 102 |
ret = -1; |
| 103 |
goto free_nl_sock; |
| 104 |
} |
| 105 |
|
| 106 |
for (nl_object = nl_cache_get_first(link_cache); nl_object; |
| 107 |
nl_object = nl_cache_get_next(nl_object)) { |
| 108 |
struct rtnl_link *rtnl_link = (void *)nl_object; |
| 109 |
int if_index = rtnl_link_get_ifindex(rtnl_link); |
| 110 |
|
| 111 |
netsnmp_assert(if_index > 0); |
| 112 |
if (if_index <= 0) |
| 113 |
continue; |
| 114 |
dot3StatsTable_add_interface(container, if_index, rtnl_link); |
| 115 |
} |
| 116 |
|
| 117 |
nl_cache_put(link_cache); |
| 118 |
|
| 119 |
free_nl_sock: |
| 120 |
nl_socket_free(nl_sock); |
| 121 |
|
| 122 |
return ret; |
| 123 |
} |