Skip to content

Commit f09ea6f

Browse files
kuba-moodavem330
authored andcommitted
ethtool: add a new command for reading standard stats
Add an interface for reading standard stats, including stats which don't have a corresponding control interface. Start with IEEE 802.3 PHY stats. There seems to be only one stat to expose there. Define API to not require user space changes when new stats or groups are added. Groups are based on bitset, stats have a string set associated. v1: wrap stats in a nest Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ddc78b3 commit f09ea6f

File tree

8 files changed

+287
-1
lines changed

8 files changed

+287
-1
lines changed

include/linux/ethtool.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ static inline void ethtool_stats_init(u64 *stats, unsigned int n)
250250
stats[n] = ETHTOOL_STAT_NOT_SET;
251251
}
252252

253+
/* Basic IEEE 802.3 PHY statistics (30.3.2.1.*), not otherwise exposed
254+
* via a more targeted API.
255+
*/
256+
struct ethtool_eth_phy_stats {
257+
u64 SymbolErrorDuringCarrier;
258+
};
259+
253260
/**
254261
* struct ethtool_pause_stats - statistics for IEEE 802.3x pause frames
255262
* @tx_pause_frames: transmitted pause frame count. Reported to user space
@@ -487,6 +494,7 @@ struct ethtool_module_eeprom {
487494
* @get_module_eeprom_by_page: Get a region of plug-in module EEPROM data from
488495
* specified page. Returns a negative error code or the amount of bytes
489496
* read.
497+
* @get_eth_phy_stats: Query some of the IEEE 802.3 PHY statistics.
490498
*
491499
* All operations are optional (i.e. the function pointer may be set
492500
* to %NULL) and callers must take this into account. Callers must
@@ -597,6 +605,8 @@ struct ethtool_ops {
597605
int (*get_module_eeprom_by_page)(struct net_device *dev,
598606
const struct ethtool_module_eeprom *page,
599607
struct netlink_ext_ack *extack);
608+
void (*get_eth_phy_stats)(struct net_device *dev,
609+
struct ethtool_eth_phy_stats *phy_stats);
600610
};
601611

602612
int ethtool_check_ops(const struct ethtool_ops *ops);

include/uapi/linux/ethtool.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ enum ethtool_link_ext_substate_cable_issue {
669669
* @ETH_SS_TS_TX_TYPES: timestamping Tx types
670670
* @ETH_SS_TS_RX_FILTERS: timestamping Rx filters
671671
* @ETH_SS_UDP_TUNNEL_TYPES: UDP tunnel types
672+
* @ETH_SS_STATS_STD: standardized stats
673+
* @ETH_SS_STATS_ETH_PHY: names of IEEE 802.3 PHY statistics
672674
*
673675
* @ETH_SS_COUNT: number of defined string sets
674676
*/
@@ -689,6 +691,8 @@ enum ethtool_stringset {
689691
ETH_SS_TS_TX_TYPES,
690692
ETH_SS_TS_RX_FILTERS,
691693
ETH_SS_UDP_TUNNEL_TYPES,
694+
ETH_SS_STATS_STD,
695+
ETH_SS_STATS_ETH_PHY,
692696

693697
/* add new constants above here */
694698
ETH_SS_COUNT

include/uapi/linux/ethtool_netlink.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum {
4545
ETHTOOL_MSG_FEC_GET,
4646
ETHTOOL_MSG_FEC_SET,
4747
ETHTOOL_MSG_MODULE_EEPROM_GET,
48+
ETHTOOL_MSG_STATS_GET,
4849

4950
/* add new constants above here */
5051
__ETHTOOL_MSG_USER_CNT,
@@ -86,6 +87,7 @@ enum {
8687
ETHTOOL_MSG_FEC_GET_REPLY,
8788
ETHTOOL_MSG_FEC_NTF,
8889
ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY,
90+
ETHTOOL_MSG_STATS_GET_REPLY,
8991

9092
/* add new constants above here */
9193
__ETHTOOL_MSG_KERNEL_CNT,
@@ -679,6 +681,51 @@ enum {
679681
ETHTOOL_A_MODULE_EEPROM_MAX = (__ETHTOOL_A_MODULE_EEPROM_CNT - 1)
680682
};
681683

684+
/* STATS */
685+
686+
enum {
687+
ETHTOOL_A_STATS_UNSPEC,
688+
ETHTOOL_A_STATS_PAD,
689+
ETHTOOL_A_STATS_HEADER, /* nest - _A_HEADER_* */
690+
ETHTOOL_A_STATS_GROUPS, /* bitset */
691+
692+
ETHTOOL_A_STATS_GRP, /* nest - _A_STATS_GRP_* */
693+
694+
/* add new constants above here */
695+
__ETHTOOL_A_STATS_CNT,
696+
ETHTOOL_A_STATS_MAX = (__ETHTOOL_A_STATS_CNT - 1)
697+
};
698+
699+
enum {
700+
ETHTOOL_STATS_ETH_PHY,
701+
702+
/* add new constants above here */
703+
__ETHTOOL_STATS_CNT
704+
};
705+
706+
enum {
707+
ETHTOOL_A_STATS_GRP_UNSPEC,
708+
ETHTOOL_A_STATS_GRP_PAD,
709+
710+
ETHTOOL_A_STATS_GRP_ID, /* u32 */
711+
ETHTOOL_A_STATS_GRP_SS_ID, /* u32 */
712+
713+
ETHTOOL_A_STATS_GRP_STAT, /* nest */
714+
715+
/* add new constants above here */
716+
__ETHTOOL_A_STATS_GRP_CNT,
717+
ETHTOOL_A_STATS_GRP_MAX = (__ETHTOOL_A_STATS_CNT - 1)
718+
};
719+
720+
enum {
721+
/* 30.3.2.1.5 aSymbolErrorDuringCarrier */
722+
ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
723+
724+
/* add new constants above here */
725+
__ETHTOOL_A_STATS_ETH_PHY_CNT,
726+
ETHTOOL_A_STATS_ETH_PHY_MAX = (__ETHTOOL_A_STATS_ETH_PHY_CNT - 1)
727+
};
728+
682729
/* generic netlink info */
683730
#define ETHTOOL_GENL_NAME "ethtool"
684731
#define ETHTOOL_GENL_VERSION 1

net/ethtool/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ obj-$(CONFIG_ETHTOOL_NETLINK) += ethtool_nl.o
77
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o \
88
linkstate.o debug.o wol.o features.o privflags.o rings.o \
99
channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \
10-
tunnels.o fec.o eeprom.o
10+
tunnels.o fec.o eeprom.o stats.o

net/ethtool/netlink.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
247247
[ETHTOOL_MSG_FEC_GET] = &ethnl_fec_request_ops,
248248
[ETHTOOL_MSG_TSINFO_GET] = &ethnl_tsinfo_request_ops,
249249
[ETHTOOL_MSG_MODULE_EEPROM_GET] = &ethnl_module_eeprom_request_ops,
250+
[ETHTOOL_MSG_STATS_GET] = &ethnl_stats_request_ops,
250251
};
251252

252253
static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -942,6 +943,15 @@ static const struct genl_ops ethtool_genl_ops[] = {
942943
.policy = ethnl_module_eeprom_get_policy,
943944
.maxattr = ARRAY_SIZE(ethnl_module_eeprom_get_policy) - 1,
944945
},
946+
{
947+
.cmd = ETHTOOL_MSG_STATS_GET,
948+
.doit = ethnl_default_doit,
949+
.start = ethnl_default_start,
950+
.dumpit = ethnl_default_dumpit,
951+
.done = ethnl_default_done,
952+
.policy = ethnl_stats_get_policy,
953+
.maxattr = ARRAY_SIZE(ethnl_stats_get_policy) - 1,
954+
},
945955
};
946956

947957
static const struct genl_multicast_group ethtool_nl_mcgrps[] = {

net/ethtool/netlink.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ extern const struct ethnl_request_ops ethnl_eee_request_ops;
346346
extern const struct ethnl_request_ops ethnl_tsinfo_request_ops;
347347
extern const struct ethnl_request_ops ethnl_fec_request_ops;
348348
extern const struct ethnl_request_ops ethnl_module_eeprom_request_ops;
349+
extern const struct ethnl_request_ops ethnl_stats_request_ops;
349350

350351
extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
351352
extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
@@ -380,6 +381,7 @@ extern const struct nla_policy ethnl_tunnel_info_get_policy[ETHTOOL_A_TUNNEL_INF
380381
extern const struct nla_policy ethnl_fec_get_policy[ETHTOOL_A_FEC_HEADER + 1];
381382
extern const struct nla_policy ethnl_fec_set_policy[ETHTOOL_A_FEC_AUTO + 1];
382383
extern const struct nla_policy ethnl_module_eeprom_get_policy[ETHTOOL_A_MODULE_EEPROM_DATA + 1];
384+
extern const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1];
383385

384386
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
385387
int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);
@@ -399,4 +401,7 @@ int ethnl_tunnel_info_start(struct netlink_callback *cb);
399401
int ethnl_tunnel_info_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
400402
int ethnl_set_fec(struct sk_buff *skb, struct genl_info *info);
401403

404+
extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
405+
extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
406+
402407
#endif /* _NET_ETHTOOL_NETLINK_H */

net/ethtool/stats.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include "netlink.h"
4+
#include "common.h"
5+
#include "bitset.h"
6+
7+
struct stats_req_info {
8+
struct ethnl_req_info base;
9+
DECLARE_BITMAP(stat_mask, __ETHTOOL_STATS_CNT);
10+
};
11+
12+
#define STATS_REQINFO(__req_base) \
13+
container_of(__req_base, struct stats_req_info, base)
14+
15+
struct stats_reply_data {
16+
struct ethnl_reply_data base;
17+
struct ethtool_eth_phy_stats phy_stats;
18+
};
19+
20+
#define STATS_REPDATA(__reply_base) \
21+
container_of(__reply_base, struct stats_reply_data, base)
22+
23+
const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN] = {
24+
[ETHTOOL_STATS_ETH_PHY] = "eth-phy",
25+
};
26+
27+
const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN] = {
28+
[ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR] = "SymbolErrorDuringCarrier",
29+
};
30+
31+
const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1] = {
32+
[ETHTOOL_A_STATS_HEADER] =
33+
NLA_POLICY_NESTED(ethnl_header_policy),
34+
[ETHTOOL_A_STATS_GROUPS] = { .type = NLA_NESTED },
35+
};
36+
37+
static int stats_parse_request(struct ethnl_req_info *req_base,
38+
struct nlattr **tb,
39+
struct netlink_ext_ack *extack)
40+
{
41+
struct stats_req_info *req_info = STATS_REQINFO(req_base);
42+
bool mod = false;
43+
int err;
44+
45+
err = ethnl_update_bitset(req_info->stat_mask, __ETHTOOL_STATS_CNT,
46+
tb[ETHTOOL_A_STATS_GROUPS], stats_std_names,
47+
extack, &mod);
48+
if (err)
49+
return err;
50+
51+
if (!mod) {
52+
NL_SET_ERR_MSG(extack, "no stats requested");
53+
return -EINVAL;
54+
}
55+
56+
return 0;
57+
}
58+
59+
static int stats_prepare_data(const struct ethnl_req_info *req_base,
60+
struct ethnl_reply_data *reply_base,
61+
struct genl_info *info)
62+
{
63+
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
64+
struct stats_reply_data *data = STATS_REPDATA(reply_base);
65+
struct net_device *dev = reply_base->dev;
66+
int ret;
67+
68+
ret = ethnl_ops_begin(dev);
69+
if (ret < 0)
70+
return ret;
71+
72+
memset(&data->phy_stats, 0xff, sizeof(data->phy_stats));
73+
74+
if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask) &&
75+
dev->ethtool_ops->get_eth_phy_stats)
76+
dev->ethtool_ops->get_eth_phy_stats(dev, &data->phy_stats);
77+
78+
ethnl_ops_complete(dev);
79+
return 0;
80+
}
81+
82+
static int stats_reply_size(const struct ethnl_req_info *req_base,
83+
const struct ethnl_reply_data *reply_base)
84+
{
85+
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
86+
unsigned int n_grps = 0, n_stats = 0;
87+
int len = 0;
88+
89+
if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask)) {
90+
n_stats += sizeof(struct ethtool_eth_phy_stats) / sizeof(u64);
91+
n_grps++;
92+
}
93+
94+
len += n_grps * (nla_total_size(0) + /* _A_STATS_GRP */
95+
nla_total_size(4) + /* _A_STATS_GRP_ID */
96+
nla_total_size(4)); /* _A_STATS_GRP_SS_ID */
97+
len += n_stats * (nla_total_size(0) + /* _A_STATS_GRP_STAT */
98+
nla_total_size_64bit(sizeof(u64)));
99+
100+
return len;
101+
}
102+
103+
static int stat_put(struct sk_buff *skb, u16 attrtype, u64 val)
104+
{
105+
struct nlattr *nest;
106+
int ret;
107+
108+
if (val == ETHTOOL_STAT_NOT_SET)
109+
return 0;
110+
111+
/* We want to start stats attr types from 0, so we don't have a type
112+
* for pad inside ETHTOOL_A_STATS_GRP_STAT. Pad things on the outside
113+
* of ETHTOOL_A_STATS_GRP_STAT. Since we're one nest away from the
114+
* actual attr we're 4B off - nla_need_padding_for_64bit() & co.
115+
* can't be used.
116+
*/
117+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
118+
if (!IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
119+
if (!nla_reserve(skb, ETHTOOL_A_STATS_GRP_PAD, 0))
120+
return -EMSGSIZE;
121+
#endif
122+
123+
nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP_STAT);
124+
if (!nest)
125+
return -EMSGSIZE;
126+
127+
ret = nla_put_u64_64bit(skb, attrtype, val, -1 /* not used */);
128+
if (ret) {
129+
nla_nest_cancel(skb, nest);
130+
return ret;
131+
}
132+
133+
nla_nest_end(skb, nest);
134+
return 0;
135+
}
136+
137+
static int stats_put_phy_stats(struct sk_buff *skb,
138+
const struct stats_reply_data *data)
139+
{
140+
if (stat_put(skb, ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
141+
data->phy_stats.SymbolErrorDuringCarrier))
142+
return -EMSGSIZE;
143+
return 0;
144+
}
145+
146+
static int stats_put_stats(struct sk_buff *skb,
147+
const struct stats_reply_data *data,
148+
u32 id, u32 ss_id,
149+
int (*cb)(struct sk_buff *skb,
150+
const struct stats_reply_data *data))
151+
{
152+
struct nlattr *nest;
153+
154+
nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP);
155+
if (!nest)
156+
return -EMSGSIZE;
157+
158+
if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_ID, id) ||
159+
nla_put_u32(skb, ETHTOOL_A_STATS_GRP_SS_ID, ss_id))
160+
goto err_cancel;
161+
162+
if (cb(skb, data))
163+
goto err_cancel;
164+
165+
nla_nest_end(skb, nest);
166+
return 0;
167+
168+
err_cancel:
169+
nla_nest_cancel(skb, nest);
170+
return -EMSGSIZE;
171+
}
172+
173+
static int stats_fill_reply(struct sk_buff *skb,
174+
const struct ethnl_req_info *req_base,
175+
const struct ethnl_reply_data *reply_base)
176+
{
177+
const struct stats_req_info *req_info = STATS_REQINFO(req_base);
178+
const struct stats_reply_data *data = STATS_REPDATA(reply_base);
179+
int ret = 0;
180+
181+
if (!ret && test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask))
182+
ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_PHY,
183+
ETH_SS_STATS_ETH_PHY,
184+
stats_put_phy_stats);
185+
186+
return ret;
187+
}
188+
189+
const struct ethnl_request_ops ethnl_stats_request_ops = {
190+
.request_cmd = ETHTOOL_MSG_STATS_GET,
191+
.reply_cmd = ETHTOOL_MSG_STATS_GET_REPLY,
192+
.hdr_attr = ETHTOOL_A_STATS_HEADER,
193+
.req_info_size = sizeof(struct stats_req_info),
194+
.reply_data_size = sizeof(struct stats_reply_data),
195+
196+
.parse_request = stats_parse_request,
197+
.prepare_data = stats_prepare_data,
198+
.reply_size = stats_reply_size,
199+
.fill_reply = stats_fill_reply,
200+
};

net/ethtool/strset.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ static const struct strset_info info_template[] = {
8080
.count = __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
8181
.strings = udp_tunnel_type_names,
8282
},
83+
[ETH_SS_STATS_STD] = {
84+
.per_dev = false,
85+
.count = __ETHTOOL_STATS_CNT,
86+
.strings = stats_std_names,
87+
},
88+
[ETH_SS_STATS_ETH_PHY] = {
89+
.per_dev = false,
90+
.count = __ETHTOOL_A_STATS_ETH_PHY_CNT,
91+
.strings = stats_eth_phy_names,
92+
},
8393
};
8494

8595
struct strset_req_info {

0 commit comments

Comments
 (0)