Skip to contentSkip to conversation

rtnetlink: add new RTM_GETSTATS message to dump link stats

PATCH Superseded Roopa Prabhu 4 participants 13 messages Apr 9, 2016 – Apr 14, 2016

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 rtnetlink: add new RTM_GETSTATS message to dump link stats

From: Roopa Prabhu <[email protected]>

This patch adds a new RTM_GETSTATS message to query link stats via netlink from the kernel. RTM_NEWLINK also dumps stats today, but RTM_NEWLINK returns a lot more than just stats and is expensive in some cases when frequent polling for stats from userspace is a common operation.

RTM_GETSTATS is an attempt to provide a light weight netlink message to explicity query only link stats from the kernel on an interface. The idea is to also keep it extensible so that new kinds of stats can be added to it in the future.

This patch adds the following attribute for NETDEV stats:

struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
        [IFLA_STATS_LINK64]  = { .len = sizeof(struct rtnl_link_stats64) },
};

This patch also allows for af family stats (an example af stats for IPV6 is available with the second patch in the series).

Like any other rtnetlink message, RTM_GETSTATS can be used to get stats of a single interface or all interfaces with NLM_F_DUMP.

Future possible new types of stat attributes:
- IFLA_MPLS_STATS (nested. for mpls/mdev stats)
- IFLA_EXTENDED_STATS (nested. extended software netdev stats like bridge, vlan, vxlan etc)
- IFLA_EXTENDED_HW_STATS (nested. extended hardware stats which are available via ethtool today)

This patch also declares a filter mask for all stat attributes. User has to provide a mask of stats attributes to query. This will be specified in a new hdr 'struct if_stats_msg' for stats messages.

Without any attributes in the filter_mask, no stats will be returned.

This patch has been tested with mofified iproute2 ifstat.

---
 include/net/rtnetlink.h        |   5 ++
 include/uapi/linux/if_link.h   |  18 ++++
 include/uapi/linux/rtnetlink.h |   5 ++
 net/core/rtnetlink.c           | 200 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 228 insertions(+)
include/net/rtnetlink.h +5 -0
index 2f87c1b..fa68158 100644
@@ -131,6 +131,11 @@ struct rtnl_af_ops {
 						    const struct nlattr *attr);
 	int			(*set_link_af)(struct net_device *dev,
 					       const struct nlattr *attr);
+	size_t			(*get_link_af_stats_size)(const struct net_device *dev,
+							  u32 filter_mask);
+	int			(*fill_link_af_stats)(struct sk_buff *skb,
+						      const struct net_device *dev,
+						      u32 filter_mask);
 };
 
 void __rtnl_af_unregister(struct rtnl_af_ops *ops);
include/uapi/linux/if_link.h +18 -0
index 9427f17..4cfd029 100644
@@ -780,4 +780,22 @@ enum {
 
 #define IFLA_HSR_MAX (__IFLA_HSR_MAX - 1)
 
+/* STATS section */
+
+struct if_stats_msg {
+	__u8  family;
+	__u32 ifindex;
+	__u32 filter_mask;
+};
+
+enum {
+	IFLA_STATS_UNSPEC,
+	IFLA_STATS_LINK64,
+	__IFLA_STATS_MAX,
+};
+
+#define IFLA_STATS_MAX (__IFLA_STATS_MAX - 1)
+
+#define IFLA_STATS_FILTER_BIT(ATTR)	(1 << (ATTR))
+
 #endif /* _UAPI_LINUX_IF_LINK_H */
include/uapi/linux/rtnetlink.h +5 -0
index ca764b5..cc885c4 100644
@@ -139,6 +139,11 @@ enum {
 	RTM_GETNSID = 90,
 #define RTM_GETNSID RTM_GETNSID
 
+	RTM_NEWSTATS = 92,
+#define RTM_NEWSTATS RTM_NEWSTATS
+	RTM_GETSTATS = 94,
+#define RTM_GETSTATS RTM_GETSTATS
+
 	__RTM_MAX,
 #define RTM_MAX		(((__RTM_MAX + 3) & ~3) - 1)
 };
net/core/rtnetlink.c +200 -0

210 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw

Thanks for doing the work Roopa and I apologize for late comments below:

Quoted 9 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch also allows for af family stats (an example af stats for IPV6 is available with the second patch in the series).

Like any other rtnetlink message, RTM_GETSTATS can be used to get stats of a single interface or all interfaces with NLM_F_DUMP.

Quoted 7 lines

Future possible new types of stat attributes:
- IFLA_MPLS_STATS (nested. for mpls/mdev stats)
- IFLA_EXTENDED_STATS (nested. extended software netdev stats like bridge, vlan, vxlan etc)
- IFLA_EXTENDED_HW_STATS (nested. extended hardware stats which are available via ethtool today)

I got the extended_hw_stats (which are very common in a lot of ASICS) if you mean stats on packet sizes. But would the other extended stats not just be per netdev kind specific? We have concept of XSTATS which maybe a fit.

Quoted 6 lines

This patch also declares a filter mask for all stat attributes. User has to provide a mask of stats attributes to query. This will be specified in a new hdr 'struct if_stats_msg' for stats messages.

Without any attributes in the filter_mask, no stats will be returned.

Should such a command then not be rejected with an error code?

include/uapi/linux/if_link.h enum { · line 789

Quoted 7 lines
include/uapi/linux/if_link.h +7 -0
+/* STATS section */
+
+struct if_stats_msg {
+	__u8  family;
+	__u32 ifindex;
+	__u32 filter_mask;
+};

Needs to be 32 bit aligned.
Do you need 32 bits for the filter mask? Perhaps a 16bit mask and an 8bit pad for future use.

struct if_stats_msg {
           __u32 ifindex;
	   __u16 filter_mask;
	   __u8  family;
           __u8 pad; /* future use */
};

Or you could reverse those (from smallest to largest).
BTW, any plans to do the cool feature where i inject a timeout period and i just get STATS events ;-> The filter struct would have to be more sophisticated - user would need to pass a list of ifindices and filter_mask as well as timeout.

cheers,
jamal

author
#2 lore raw
Quoted 22 lines · Jamal Hadi Salim

Thanks for doing the work Roopa and I apologize for late comments below:

Quoted 9 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch also allows for af family stats (an example af stats for IPV6 is available with the second patch in the series).

Like any other rtnetlink message, RTM_GETSTATS can be used to get stats of a single interface or all interfaces with NLM_F_DUMP.

Quoted 7 lines

Future possible new types of stat attributes:
- IFLA_MPLS_STATS (nested. for mpls/mdev stats)
- IFLA_EXTENDED_STATS (nested. extended software netdev stats like bridge, vlan, vxlan etc)
- IFLA_EXTENDED_HW_STATS (nested. extended hardware stats which are available via ethtool today)

Quoted 4 lines

I got the extended_hw_stats (which are very common in a lot of ASICS) if you mean stats on packet sizes. But would the other extended stats not just be per netdev kind specific? We have concept of XSTATS which maybe a fit.

This EXTENDED_HW_STATS is for ethtool like extended hw stats. This is keeping in mind that we want to also move ethtool to netlink in the future and with switchdev it becomes more necessary that we provide all stats closer to the other netdev stats. So far hw extended stats have always been available through this separate ethtool channel. The intent here is to unify the api for extended hw and software only stats.

XSTATS is per netdev can be included as a nested attribute inside IFLA_EXTENDED_STATS which are per netdev. bridge vlan stats will also fall here.

And this api provides netdev specific stats. We have always mapped all
asic stats to the switch port netdev stats. and this api does not cover the non-netdev specific stats. If you are for example asking for stats for a hardware offloaded bridge, then yes, they will fall here and be available on the bridge netdev. For asic stats that don't map to any netdev, devlink will be an appropriate infrastructure IMO.

I am not sure if I answered your question :).

Quoted 9 lines
Quoted 7 lines

This patch also declares a filter mask for all stat attributes. User has to provide a mask of stats attributes to query. This will be specified in a new hdr 'struct if_stats_msg' for stats messages.

Without any attributes in the filter_mask, no stats will be returned.

Should such a command then not be rejected with an error code?

Dumps with no data are not rejected with an error code AFAIK. ie they don't return -ENODATA. This is consistent with all other dumps (unless i missed it).
But, if there is a need for an error code, i can certainly check again.

Quoted 10 lines

include/uapi/linux/if_link.h enum { · line 789

Quoted 7 lines
include/uapi/linux/if_link.h +7 -0
+/* STATS section */
+
+struct if_stats_msg {
+    __u8  family;
+    __u32 ifindex;
+    __u32 filter_mask;
+};

Needs to be 32 bit aligned.
Do you need 32 bits for the filter mask?

yes, i think we should keep it minimum 32 bits.

Quoted 10 lines

Perhaps a 16bit mask and an 8bit pad for future use.

struct if_stats_msg {
           __u32 ifindex;
       __u16 filter_mask;
       __u8  family;
           __u8 pad; /* future use */
};

Or you could reverse those (from smallest to largest).

The __u8 family needs to be the first field in the structure and at the first byte in the header data.
hence family is first and i added the others after that. It follows the format for existing such structs (for other message types).

Quoted 4 lines

BTW, any plans to do the cool feature where i inject a timeout period and i just get STATS events ;-> The filter struct would have to be more sophisticated - user would need to pass a list of ifindices and filter_mask as well as timeout.

Yeah i remember :). But deferring it for a later incremental feature. That needs some more thought. Right now there is an urgent need to get the basic get stats api for a bunch of other stats: mpls, bridge vlan etc. Because it is not clean to extend the current stats infra part of the link message for this. So trying to get this in first.

And this patchset only adds a handler for RTM_NEWSTATS dump and get stats. Your stats events request should be part of the RTM_NEWSTATS handler and can include other attributes (like timeout) in the future.

Thanks,
Roopa

#5 lore raw
In reply to Roopa Prabhu
Quoted 17 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch adds a new RTM_GETSTATS message to query link stats via netlink from the kernel. RTM_NEWLINK also dumps stats today, but RTM_NEWLINK returns a lot more than just stats and is expensive in some cases when frequent polling for stats from userspace is a common operation.

RTM_GETSTATS is an attempt to provide a light weight netlink message to explicity query only link stats from the kernel on an interface. The idea is to also keep it extensible so that new kinds of stats can be added to it in the future.

This patch adds the following attribute for NETDEV stats:

struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
        [IFLA_STATS_LINK64]  = { .len = sizeof(struct rtnl_link_stats64) },
};
Quoted 5 lines

This patch also allows for af family stats (an example af stats for IPV6 is available with the second patch in the series).

Like any other rtnetlink message, RTM_GETSTATS can be used to get stats of a single interface or all interfaces with NLM_F_DUMP.

Awesome stuff Roopa.

This currently ties everything to a net_device with a selector to include certain bits of that net_device. How about we take it half a step further and allow for non net_device stats such as IP, TCP, routing or ipsec stats to be retrieved as well?

A simple array of nested attributes replacing IFLA_STATS_* would allow for that, e.g.

1. {.type = ST_IPSTATS, value = { ...} }

2. {.type = ST_LINK, .value = {
    {.type = ST_LINK_NAME, .value = "eth0"},
    {.type = ST_LINK_Q, .value = 10}

}}

3. ...

So for your initial patch, you'd simply add a new top level attribute which ties all the net_device specific statistics to a top level attribute and we can add the non net_device specific stats later on. We can't do that later on though without breaking ABI so that would have to go in with the first iteration.

We can preserve all the existing attribute formats, we simply have to introduce new attribute types which don't overlap and document which statistic identifier maps to which existing attribute id.

#3 lore raw
In reply to roopa
Quoted 5 lines · roopa

This EXTENDED_HW_STATS is for ethtool like extended hw stats. This is keeping in mind that we want to also move ethtool to netlink in the future and with switchdev it becomes more necessary that we provide all stats closer to the other netdev stats. So far hw extended stats have always been available through this separate ethtool channel. The intent here is to unify the api for extended hw and software only stats.

Ok, so these are _not_ the stats which are broken down by packet size ranges which are quiet common; but rather "proprietary" per port type h/w stats? I browsed a couple of users of ethtool_stats and i see they return proprietary looking 64 bit counters (batman for example has a very strange meaning to those stats).
What i meant is a lot of ASICS have counters for byte ranges [64bytes-128bytes], [128bytes-256bytes], etc - sorry i cant pin a name to those but i am sure you have seen them and i thought those at minimal need their own TLV since they are always fixed.

Quoted 3 lines

XSTATS is per netdev can be included as a nested attribute inside IFLA_EXTENDED_STATS which are per netdev. bridge vlan stats will also fall here.

And you are going to distinguish which come from hardware and which are software derived?

Quoted 8 lines

And this api provides netdev specific stats. We have always mapped all
asic stats to the switch port netdev stats. and this api does not cover the non-netdev specific stats. If you are for example asking for stats for a hardware offloaded bridge, then yes, they will fall here and be available on the bridge netdev. For asic stats that don't map to any netdev, devlink will be an appropriate infrastructure IMO.

I am not sure if I answered your question :).

It is useful to have this discussion; unfortunately these user APIS once are in will never be allowed to change. The answers could come in time.

Quoted 5 lines
Quoted 1 line

Should such a command then not be rejected with an error code?

Dumps with no data are not rejected with an error code AFAIK. ie they don't return -ENODATA. This is consistent with all other dumps (unless i missed it).
But, if there is a need for an error code, i can certainly check again.

It is mostly because you chose a whitelist filter i.e you list what is allowed to be sent back. And if such a list is missing there needs to be an opposite default (which is a deny all).

Quoted 11 lines
Quoted 10 lines

include/uapi/linux/if_link.h enum { · line 789

Quoted 7 lines
include/uapi/linux/if_link.h +7 -0
+/* STATS section */
+
+struct if_stats_msg {
+    __u8  family;
+    __u32 ifindex;
+    __u32 filter_mask;
+};

Needs to be 32 bit aligned.
Do you need 32 bits for the filter mask?

yes, i think we should keep it minimum 32 bits.

Ok, that is fine then. I thought it wont exceed
3-4 per port type but i could be wrong. 32 bits
should be safer.

Quoted 9 lines
Quoted 9 lines

Perhaps a 16bit mask and an 8bit pad for future use.

struct if_stats_msg {
            __u32 ifindex;
        __u16 filter_mask;
        __u8  family;
            __u8 pad; /* future use */
};
Quoted 5 lines
Quoted 1 line

Or you could reverse those (from smallest to largest).

The __u8 family needs to be the first field in the structure and at the first byte in the header data.
hence family is first and i added the others after that. It follows the format for existing such structs (for other message types).

True, but it must be 32 bit aligned. So something like:

struct if_stats_msg {
     __u8  family;
     __u8 pad1;
     __u16 pad2;
    __u32 ifindex;
    __u32 filter_mask;
}
Quoted 1 line

Yeah i remember :). But deferring it for a later incremental feature. That needs some more thought.

NP ;->

Quoted 3 lines

Right now there is an urgent need to get the basic get stats api for a bunch of other stats: mpls, bridge vlan etc. Because it is not clean to extend the current stats infra part of the link message for this. So trying to get this in first.

Agreed.
The only thing i strongly feel about is the if_stats_msg - please fix that and lets get at least the basics in. We can resolve other things with further discussions.

Quoted 2 lines

And this patchset only adds a handler for RTM_NEWSTATS dump and get stats. Your stats events request should be part of the RTM_NEWSTATS handler and can include other attributes (like timeout) in the future.

Ok.

cheers,
jamal

Quoted 3 lines

Thanks,
Roopa

author
#6 lore raw
In reply to Thomas Graf
Quoted 21 lines · Thomas Graf
Quoted 20 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch adds a new RTM_GETSTATS message to query link stats via netlink from the kernel. RTM_NEWLINK also dumps stats today, but RTM_NEWLINK returns a lot more than just stats and is expensive in some cases when frequent polling for stats from userspace is a common operation.

RTM_GETSTATS is an attempt to provide a light weight netlink message to explicity query only link stats from the kernel on an interface. The idea is to also keep it extensible so that new kinds of stats can be added to it in the future.

This patch adds the following attribute for NETDEV stats:

struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
        [IFLA_STATS_LINK64]  = { .len = sizeof(struct rtnl_link_stats64) },
};

This patch also allows for af family stats (an example af stats for IPV6 is available with the second patch in the series).

Quoted 8 lines
Quoted 2 lines

Like any other rtnetlink message, RTM_GETSTATS can be used to get stats of a single interface or all interfaces with NLM_F_DUMP.

Awesome stuff Roopa.

This currently ties everything to a net_device with a selector to include certain bits of that net_device. How about we take it half a step further and allow for non net_device stats such as IP, TCP, routing or ipsec stats to be retrieved as well?

yes, absolutely. and that is also the goal.

Quoted 11 lines

A simple array of nested attributes replacing IFLA_STATS_* would allow for that, e.g.

1. {.type = ST_IPSTATS, value = { ...} }

2. {.type = ST_LINK, .value = {
    {.type = ST_LINK_NAME, .value = "eth0"},
    {.type = ST_LINK_Q, .value = 10}

}}

3. ...

One thing though, Its unclear to me if we absolutely need the additional nest. Every stats netlink msg has an ifindex in the header (if_stats_msg) if the scope of the stats is a netdev. If the msg does not have an ifindex in the if_stats_msg, it represents a global stat. ie Cant a dump, include other stats netlink msgs after all the netdev msgs are done when the filter has global stat filters ?. same will apply to RTM_GETSTATS (without NLM_F_DUMP).

Since the msg may potentially have more nest levels
in the IFLA_EXT_STATS categories, just trying to see if i can avoid adding another top-level nest. We can sure add it if there is no other way to include global stats in the same dump.

Quoted 5 lines

So for your initial patch, you'd simply add a new top level attribute which ties all the net_device specific statistics to a top level attribute and we can add the non net_device specific stats later on. We can't do that later on though without breaking ABI so that would have to go in with the first iteration.

agreed

Quoted 4 lines

We can preserve all the existing attribute formats, we simply have to introduce new attribute types which don't overlap and document which statistic identifier maps to which existing attribute id.

sure,

thanks Thomas.

author
#4 lore raw
In reply to Jamal Hadi Salim
Quoted 8 lines · Jamal Hadi Salim
Quoted 5 lines · roopa

This EXTENDED_HW_STATS is for ethtool like extended hw stats. This is keeping in mind that we want to also move ethtool to netlink in the future and with switchdev it becomes more necessary that we provide all stats closer to the other netdev stats. So far hw extended stats have always been available through this separate ethtool channel. The intent here is to unify the api for extended hw and software only stats.

Quoted 9 lines

Ok, so these are _not_ the stats which are broken down by packet size ranges which are quiet common; but rather "proprietary" per port type h/w stats? I browsed a couple of users of ethtool_stats and i see they return proprietary looking 64 bit counters (batman for example has a very strange meaning to those stats).
What i meant is a lot of ASICS have counters for byte ranges [64bytes-128bytes], [128bytes-256bytes], etc - sorry i cant pin a name to those but i am sure you have seen them and i thought those at minimal need their own TLV since they are always fixed.

yes, I think i have seen this. But, these if presented by the driver or hardware,
can be part of the extended hw stats (just like how ethtool would do).
So, there is the base netdev stats IFLA_STATS_LINK64 which is nothing but
rtnl_link_stats64 (which this patch adds). And this patch does not add but lists
examples for future additions IFLA_STATS_EXTENDED (for example: bridge
(can include igmp, stp, vlan stats here). bond can includelacp, and other stats here). All these will be TLV based. and note that this patch does notrestrict or define the design for these yet.

Quoted 7 lines
Quoted 4 lines

XSTATS is per netdev can be included as a nested attribute inside IFLA_EXTENDED_STATS which are per netdev. bridge vlan stats will also fall here.

And you are going to distinguish which come from hardware and which are software derived?

a) IFLA_EXTENDED_STATS - these are all software. ( for example: bridge (can include igmp, stp, vlan stats here).
bond can include lacp, and other stats here).

b) IFLA_HW_EXTENDED_STATS - for hardware stats. for a switch port these will be similar to ethtool
physical port stats today. For logical devices, this can include specific hw stats that switch asics
provide and those which cannot be added to the software stats.

And since this patch does not define the format for these stats yet, we can potentially design it when we see the first case of such stats.

Quoted 7 lines
Quoted 7 lines

And this api provides netdev specific stats. We have always mapped all
asic stats to the switch port netdev stats. and this api does not cover the non-netdev specific stats. If you are for example asking for stats for a hardware offloaded bridge, then yes, they will fall here and be available on the bridge netdev. For asic stats that don't map to any netdev, devlink will be an appropriate infrastructure IMO.

Quoted 5 lines
Quoted 2 lines

I am not sure if I answered your question :).

It is useful to have this discussion; unfortunately these user APIS once are in will never be allowed to change. The answers could come in time.

yep, agreed.

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

Should such a command then not be rejected with an error code?

Dumps with no data are not rejected with an error code AFAIK. ie they don't return -ENODATA. This is consistent with all other dumps (unless i missed it).
But, if there is a need for an error code, i can certainly check again.

It is mostly because you chose a whitelist filter i.e you list what is allowed to be sent back. And if such a list is missing there needs to be an opposite default (which is a deny all).

ok, by default no-filter = no-data is what we decided. And that keeps the api simple. will think some more about if we should return an err in case of no-filter.

Quoted 2 lines

[snip]

Quoted 9 lines

True, but it must be 32 bit aligned. So something like:

struct if_stats_msg {
     __u8  family;
     __u8 pad1;
     __u16 pad2;
    __u32 ifindex;
    __u32 filter_mask;
}

ok ack

Quoted 5 lines
Quoted 2 lines

Yeah i remember :). But deferring it for a later incremental feature. That needs some more thought.

NP ;->

Quoted 8 lines
Quoted 3 lines

Right now there is an urgent need to get the basic get stats api for a bunch of other stats: mpls, bridge vlan etc. Because it is not clean to extend the current stats infra part of the link message for this. So trying to get this in first.

Agreed.
The only thing i strongly feel about is the if_stats_msg - please fix that and lets get at least the basics in. We can resolve other things with further discussions.

will do. Thanks for the review.

author
#7 lore raw
In reply to roopa
Quoted 1 line · roopa

On 4/10/16, 1:16 AM, Thomas Graf wrote:

[snip]

Quoted 24 lines
Quoted 5 lines

This currently ties everything to a net_device with a selector to include certain bits of that net_device. How about we take it half a step further and allow for non net_device stats such as IP, TCP, routing or ipsec stats to be retrieved as well?

yes, absolutely. and that is also the goal.

Quoted 11 lines

A simple array of nested attributes replacing IFLA_STATS_* would allow for that, e.g.

1. {.type = ST_IPSTATS, value = { ...} }

2. {.type = ST_LINK, .value = {
    {.type = ST_LINK_NAME, .value = "eth0"},
    {.type = ST_LINK_Q, .value = 10}

}}

3. ...

One thing though, Its unclear to me if we absolutely need the additional nest. Every stats netlink msg has an ifindex in the header (if_stats_msg) if the scope of the stats is a netdev. If the msg does not have an ifindex in the if_stats_msg, it represents a global stat. ie Cant a dump, include other stats netlink msgs after all the netdev msgs are done when the filter has global stat filters ?. same will apply to RTM_GETSTATS (without NLM_F_DUMP).

Quoted 5 lines

Since the msg may potentially have more nest levels
in the IFLA_EXT_STATS categories, just trying to see if i can avoid adding another top-level nest. We can sure add it if there is no other way to include global stats in the same dump.

Just wanted to elaborate on what i was trying to say:

Top level stats attributes can be netdev or global attributes: We can include string "LINK" in the names of all stats belonging to a netdev to make it easier to recognize the netdev stats (example):
IFLA_STATS_LINK64, (netdev)
IFLA_STATS_LINK_INET6, (netdev)
IFLA_STATS_TCP, (non-netdev, global tcp stats)

RTM_GETSTATS (NLM_F_DUMP)  with user given filter_mask {
        If filter-mask contains any link stats, start with per netdev stats messages:
        { if_stats_msg.ifindex = 1,
          if_stats_msg.filter_mask = mask of included link stats,
          <stats attributes> }

        { if_stats_msg.ifindex = 2,
          if_stats_msg.filter_mask = mask of included link stats,
          <stats attributes> }

        global stats (if user given filter mask contains global filters.):
        { if_stats_msg.ifindex = 0,
          if_stats_msg.filter_mask = mask of included global stats,
          <stats attributes> }
}

We will need a field in netlink_callback to indicate global or netdev stats when the stats crosses skb boundaries. A single nlmsg cannot have both netdev and global stats.

Non-dump RTM_GETSTATS examples:

RTM_GETSTATS with valid ifindex and filter_mask {
filter_mask cannot have global stats (return -EINVAL) { if_stats_msg.ifindex = <user_given_ifindex>,
if_stats_msg.filter_mask = mask of included link stats, <stats attributes> }
}

RTM_GETSTATS with ifindex = 0 and filter_mask {
filter_mask cannot have link stats (return -EINVAL) { if_stats_msg.ifindex = 0,
if_stats_msg.filter_mask = mask of included global link stats, <stats attributes> }
}

Will this not work ?

Thanks,
Roopa

#8 lore raw
Quoted 5 lines · roopa

Top level stats attributes can be netdev or global attributes: We can include string "LINK" in the names of all stats belonging to a netdev to make it easier to recognize the netdev stats (example):
IFLA_STATS_LINK64, (netdev)
IFLA_STATS_LINK_INET6, (netdev)
IFLA_STATS_TCP, (non-netdev, global tcp stats)

This is fine as well. It means that we cant mix netdev and non-netdev stats or stats for multiple netdevs in the same request which would not be the case if you nest it and have a top level attribute which is a list of requests. That may be borderline to overengineering though so I'm fine this as well.

Quoted 2 lines

We will need a field in netlink_callback to indicate global or netdev stats when the stats crosses skb boundaries. A single nlmsg cannot have both netdev and global stats.

I would treat each IFLA_STATS_ as its own nlmsg in the reply and enforce an NLM_F_DUMP request for any multi request message.

#9 lore raw
Quoted 7 lines · Thomas Graf
Quoted 5 lines · roopa

Top level stats attributes can be netdev or global attributes: We can include string "LINK" in the names of all stats belonging to a netdev to make it easier to recognize the netdev stats (example):
IFLA_STATS_LINK64, (netdev)
IFLA_STATS_LINK_INET6, (netdev)
IFLA_STATS_TCP, (non-netdev, global tcp stats)

Quoted 5 lines

This is fine as well. It means that we cant mix netdev and non-netdev stats or stats for multiple netdevs in the same request which would not be the case if you nest it and have a top level attribute which is a list of requests. That may be borderline to overengineering though so I'm fine this as well.

Well - using a subheader which has ifindex on it for non-netdev stats seems wrong then.

cheers,
jamal

maintainer
#11 lore raw
In reply to Roopa Prabhu

From: Roopa Prabhu <[email protected]>
Date: Fri, 8 Apr 2016 23:38:11 -0700

Quoted 4 lines

This patch adds a new RTM_GETSTATS message to query link stats via netlink from the kernel. RTM_NEWLINK also dumps stats today, but RTM_NEWLINK returns a lot more than just stats and is expensive in some cases when frequent polling for stats from userspace is a common operation.

Great work. One thing catches my eye:

net/core/rtnetlink.c out: · line 3483

Quoted 9 lines
net/core/rtnetlink.c +9 -0
+	if (filter_mask & IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK64)) {
+		attr = nla_reserve(skb, IFLA_STATS_LINK64,
+				   sizeof(struct rtnl_link_stats64));
+		if (!attr)
+			return -EMSGSIZE;
+
+		stats = dev_get_stats(dev, &temp);
+
+		copy_rtnl_link_stats64(nla_data(attr), stats);

This extra copy bothers me, so I tried to figure out what is going on here.

dev_get_stats() always returns the rtnl_link_stats64 pointer it was given. We should be able to pass, therefore, nla_data(attr), straight there to avoid the copy.

Bonding even uses dev_get_stats() in this way.

The existing rtnl_fill_stats() can be improved similarly but is of course a separate change. In that case, we'd do something like:

	struct rtnl_link_stats64 *sp;

	attr = nla_reserve(skb, IFLA_STATS64,
			   sizeof(struct rtnl_link_stats64));
	if (!attr)
		return -EMSGSIZE;

	sp = nla_data(attr);
	dev_get_stats(dev, sp);

	attr = nla_reserve(skb, IFLA_STATS,
			   sizeof(struct rtnl_link_stats));
	if (!attr)
		return -EMSGSIZE;

	copy_rtnl_link_stats(nla_data(attr), sp);

Thanks.

author
#12 lore raw
Quoted 21 lines · David Miller

From: Roopa Prabhu <[email protected]>
Date: Fri, 8 Apr 2016 23:38:11 -0700

Quoted 4 lines

This patch adds a new RTM_GETSTATS message to query link stats via netlink from the kernel. RTM_NEWLINK also dumps stats today, but RTM_NEWLINK returns a lot more than just stats and is expensive in some cases when frequent polling for stats from userspace is a common operation.

Great work. One thing catches my eye:

net/core/rtnetlink.c out: · line 3483

Quoted 9 lines
net/core/rtnetlink.c +9 -0
+	if (filter_mask & IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK64)) {
+		attr = nla_reserve(skb, IFLA_STATS_LINK64,
+				   sizeof(struct rtnl_link_stats64));
+		if (!attr)
+			return -EMSGSIZE;
+
+		stats = dev_get_stats(dev, &temp);
+
+		copy_rtnl_link_stats64(nla_data(attr), stats);

This extra copy bothers me, so I tried to figure out what is going on here.

Quoted 3 lines

dev_get_stats() always returns the rtnl_link_stats64 pointer it was given. We should be able to pass, therefore, nla_data(attr), straight there to avoid the copy.

nice catch. I picked this up straight from rtnl_fill_stats. agree, also thanks
for the example below.

Quoted 6 lines

Bonding even uses dev_get_stats() in this way.

The existing rtnl_fill_stats() can be improved similarly but is of course a separate change. In that case, we'd do something like:

Quoted 16 lines
	struct rtnl_link_stats64 *sp;

	attr = nla_reserve(skb, IFLA_STATS64,
			   sizeof(struct rtnl_link_stats64));
	if (!attr)
		return -EMSGSIZE;

	sp = nla_data(attr);
	dev_get_stats(dev, sp);

	attr = nla_reserve(skb, IFLA_STATS,
			   sizeof(struct rtnl_link_stats));
	if (!attr)
		return -EMSGSIZE;

	copy_rtnl_link_stats(nla_data(attr), sp);

I will submit a separate patch for this with some testing.

Will send a v3 out before end of this week.

Thank you!.

author
#10 lore raw
In reply to Jamal Hadi Salim
Quoted 15 lines · Jamal Hadi Salim
Quoted 7 lines · Thomas Graf
Quoted 5 lines · roopa

Top level stats attributes can be netdev or global attributes: We can include string "LINK" in the names of all stats belonging to a netdev to make it easier to recognize the netdev stats (example):
IFLA_STATS_LINK64, (netdev)
IFLA_STATS_LINK_INET6, (netdev)
IFLA_STATS_TCP, (non-netdev, global tcp stats)

Quoted 5 lines

This is fine as well. It means that we cant mix netdev and non-netdev stats or stats for multiple netdevs in the same request which would not be the case if you nest it and have a top level attribute which is a list of requests. That may be borderline to overengineering though so I'm fine this as well.

Quoted 2 lines

Well - using a subheader which has ifindex on it for non-netdev stats seems wrong then.

I think its ok for ifindex to be optional.
Almost all msg types have subheaders with ifindex and it is almost always optional in the GET request (eg RTM_GETLINK with NLM_F_DUMP ifindex is optional and never checked). Here we make ifindex optional both ways.

Its optional in the GET request with NLM_F_DUMP and it is also optional in the msg to userspace (NEW). And no ifindex in NEW will indicate that the message is not tied to a specific netdev (ie global stats).

Thanks!