platform: add network statistics

Make network traffic statistics data available through the platform.
This commit is contained in:
Alfonso Sanchez-Beato
2016-08-10 11:54:30 +02:00
committed by Thomas Haller
parent 3bc5c7dbc1
commit 6ed939e841
3 changed files with 63 additions and 0 deletions

View File

@@ -1476,6 +1476,15 @@ _new_from_nl_link (NMPlatform *platform, const NMPCache *cache, struct nlmsghdr
nl_info_data = li[IFLA_INFO_DATA];
}
if (tb[IFLA_STATS64]) {
struct rtnl_link_stats64 *stats = nla_data (tb[IFLA_STATS64]);
obj->link.rx_packets = stats->rx_packets;
obj->link.rx_bytes = stats->rx_bytes;
obj->link.tx_packets = stats->tx_packets;
obj->link.tx_bytes = stats->tx_bytes;
}
obj->link.n_ifi_flags = ifi->ifi_flags;
obj->link.connected = NM_FLAGS_HAS (obj->link.n_ifi_flags, IFF_LOWER_UP);
obj->link.arptype = ifi->ifi_type;
@@ -3732,6 +3741,7 @@ event_valid_msg (NMPlatform *platform, struct nl_msg *msg, gboolean handle_event
case RTM_NEWLINK:
case RTM_NEWADDR:
case RTM_NEWROUTE:
case RTM_GETLINK:
cache_op = nmp_cache_update_netlink (priv->cache, obj, &obj_cache, &was_visible, cache_pre_hook, platform);
cache_post (platform, msghdr, cache_op, obj, obj_cache);
@@ -4517,6 +4527,27 @@ link_get_dev_id (NMPlatform *platform, int ifindex)
return errno ? 0 : (int) int_val;
}
static gboolean
link_get_stats (NMPlatform *platform, int ifindex,
guint64 *rx_packets, guint64 *rx_bytes,
guint64 *tx_packets, guint64 *tx_bytes)
{
nm_auto_pop_netns NMPNetns *netns = NULL;
const NMPObject *obj;
obj = cache_lookup_link (platform, ifindex);
if (!obj)
return FALSE;
*rx_packets = obj->link.rx_packets;
*rx_bytes = obj->link.rx_bytes;
*tx_packets = obj->link.tx_packets;
*tx_bytes = obj->link.tx_bytes;
return TRUE;
}
static int
vlan_add (NMPlatform *platform,
const char *name,
@@ -6509,6 +6540,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
platform_class->link_get_physical_port_id = link_get_physical_port_id;
platform_class->link_get_dev_id = link_get_dev_id;
platform_class->link_get_stats = link_get_stats;
platform_class->link_get_wake_on_lan = link_get_wake_on_lan;
platform_class->link_get_driver_info = link_get_driver_info;

View File

@@ -1265,6 +1265,21 @@ nm_platform_link_get_dev_id (NMPlatform *self, int ifindex)
return 0;
}
gboolean nm_platform_link_get_stats (NMPlatform *self, int ifindex,
guint64 *rx_packets, guint64 *rx_bytes,
guint64 *tx_packets, guint64 *tx_bytes)
{
_CHECK_SELF (self, klass, 0);
g_return_val_if_fail (ifindex >= 0, 0);
if (klass->link_get_stats)
return klass->link_get_stats (self, ifindex,
rx_packets, rx_bytes,
tx_packets, tx_bytes);
return FALSE;
}
/**
* nm_platform_link_get_wake_onlan:
* @self: platform instance
@@ -3777,6 +3792,10 @@ int
nm_platform_link_cmp (const NMPlatformLink *a, const NMPlatformLink *b)
{
_CMP_SELF (a, b);
_CMP_FIELD (a, b, rx_packets);
_CMP_FIELD (a, b, rx_bytes);
_CMP_FIELD (a, b, tx_packets);
_CMP_FIELD (a, b, tx_bytes);
_CMP_FIELD (a, b, ifindex);
_CMP_FIELD (a, b, type);
_CMP_FIELD_STR (a, b, name);

View File

@@ -156,6 +156,12 @@ struct _NMPlatformLink {
* initialized with memset(0) has and unset value.*/
guint8 inet6_addr_gen_mode_inv;
/* Statistics */
guint64 rx_packets;
guint64 rx_bytes;
guint64 tx_packets;
guint64 tx_bytes;
/* @connected is mostly identical to (@n_ifi_flags & IFF_UP). Except for bridge/bond masters,
* where we coerce the link as disconnect if it has no slaves. */
bool connected:1;
@@ -532,6 +538,9 @@ typedef struct {
char * (*link_get_physical_port_id) (NMPlatform *, int ifindex);
guint (*link_get_dev_id) (NMPlatform *, int ifindex);
gboolean (*link_get_stats) (NMPlatform *platform, int ifindex,
guint64 *rx_packets, guint64 *rx_bytes,
guint64 *tx_packets, guint64 *tx_bytes);
gboolean (*link_get_wake_on_lan) (NMPlatform *, int ifindex);
gboolean (*link_get_driver_info) (NMPlatform *,
int ifindex,
@@ -763,6 +772,9 @@ gboolean nm_platform_link_set_mtu (NMPlatform *self, int ifindex, guint32 mtu);
char *nm_platform_link_get_physical_port_id (NMPlatform *self, int ifindex);
guint nm_platform_link_get_dev_id (NMPlatform *self, int ifindex);
gboolean nm_platform_link_get_stats (NMPlatform *self, int ifindex,
guint64 *rx_packets, guint64 *rx_bytes,
guint64 *tx_packets, guint64 *tx_bytes);
gboolean nm_platform_link_get_wake_on_lan (NMPlatform *self, int ifindex);
gboolean nm_platform_link_get_driver_info (NMPlatform *self,
int ifindex,