summaryrefslogtreecommitdiffstats
path: root/net/ethtool/stats.c
blob: b7642dc96d5072d66efb43b63516f258551ac70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-License-Identifier: GPL-2.0-only

#include "netlink.h"
#include "common.h"
#include "bitset.h"

struct stats_req_info {
	struct ethnl_req_info		base;
	DECLARE_BITMAP(stat_mask, __ETHTOOL_STATS_CNT);
};

#define STATS_REQINFO(__req_base) \
	container_of(__req_base, struct stats_req_info, base)

struct stats_reply_data {
	struct ethnl_reply_data		base;
	struct ethtool_eth_phy_stats	phy_stats;
	struct ethtool_eth_mac_stats	mac_stats;
	struct ethtool_eth_ctrl_stats	ctrl_stats;
	struct ethtool_rmon_stats	rmon_stats;
	const struct ethtool_rmon_hist_range	*rmon_ranges;
};

#define STATS_REPDATA(__reply_base) \
	container_of(__reply_base, struct stats_reply_data, base)

const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN] = {
	[ETHTOOL_STATS_ETH_PHY]			= "eth-phy",
	[ETHTOOL_STATS_ETH_MAC]			= "eth-mac",
	[ETHTOOL_STATS_ETH_CTRL]		= "eth-ctrl",
	[ETHTOOL_STATS_RMON]			= "rmon",
};

const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN] = {
	[ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR]	= "SymbolErrorDuringCarrier",
};

const char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN] = {
	[ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT]	= "FramesTransmittedOK",
	[ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL]	= "SingleCollisionFrames",
	[ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL]	= "MultipleCollisionFrames",
	[ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT]	= "FramesReceivedOK",
	[ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR]	= "FrameCheckSequenceErrors",
	[ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR]	= "AlignmentErrors",
	[ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES]	= "OctetsTransmittedOK",
	[ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER]	= "FramesWithDeferredXmissions",
	[ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL]	= "LateCollisions",
	[ETHTOOL_A_STATS_ETH_MAC_11_XS_COL]	= "FramesAbortedDueToXSColls",
	[ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR]	= "FramesLostDueToIntMACXmitError",
	[ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR]	= "CarrierSenseErrors",
	[ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES]	= "OctetsReceivedOK",
	[ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR]	= "FramesLostDueToIntMACRcvError",
	[ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST]	= "MulticastFramesXmittedOK",
	[ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST]	= "BroadcastFramesXmittedOK",
	[ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER]	= "FramesWithExcessiveDeferral",
	[ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST]	= "MulticastFramesReceivedOK",
	[ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST]	= "BroadcastFramesReceivedOK",
	[ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR]	= "InRangeLengthErrors",
	[ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN]	= "OutOfRangeLengthField",
	[ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR]	= "FrameTooLongErrors",
};

const char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN] = {
	[ETHTOOL_A_STATS_ETH_CTRL_3_TX]		= "MACControlFramesTransmitted",
	[ETHTOOL_A_STATS_ETH_CTRL_4_RX]		= "MACControlFramesReceived",
	[ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP]	= "UnsupportedOpcodesReceived",
};

const char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN] = {
	[ETHTOOL_A_STATS_RMON_UNDERSIZE]	= "etherStatsUndersizePkts",
	[ETHTOOL_A_STATS_RMON_OVERSIZE]		= "etherStatsOversizePkts",
	[ETHTOOL_A_STATS_RMON_FRAG]		= "etherStatsFragments",
	[ETHTOOL_A_STATS_RMON_JABBER]		= "etherStatsJabbers",
};

const struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_GROUPS + 1] = {
	[ETHTOOL_A_STATS_HEADER]	=
		NLA_POLICY_NESTED(ethnl_header_policy),
	[ETHTOOL_A_STATS_GROUPS]	= { .type = NLA_NESTED },
};

static int stats_parse_request(struct ethnl_req_info *req_base,
			       struct nlattr **tb,
			       struct netlink_ext_ack *extack)
{
	struct stats_req_info *req_info = STATS_REQINFO(req_base);
	bool mod = false;
	int err;

	err = ethnl_update_bitset(req_info->stat_mask, __ETHTOOL_STATS_CNT,
				  tb[ETHTOOL_A_STATS_GROUPS], stats_std_names,
				  extack, &mod);
	if (err)
		return err;

	if (!mod) {
		NL_SET_ERR_MSG(extack, "no stats requested");
		return -EINVAL;
	}

	return 0;
}

static int stats_prepare_data(const struct ethnl_req_info *req_base,
			      struct ethnl_reply_data *reply_base,
			      struct genl_info *info)
{
	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
	struct stats_reply_data *data = STATS_REPDATA(reply_base);
	struct net_device *dev = reply_base->dev;
	int ret;

	ret = ethnl_ops_begin(dev);
	if (ret < 0)
		return ret;

	/* Mark all stats as unset (see ETHTOOL_STAT_NOT_SET) to prevent them
	 * from being reported to user space in case driver did not set them.
	 */
	memset(&data->phy_stats, 0xff, sizeof(data->phy_stats));
	memset(&data->mac_stats, 0xff, sizeof(data->mac_stats));
	memset(&data->ctrl_stats, 0xff, sizeof(data->mac_stats));
	memset(&data->rmon_stats, 0xff, sizeof(data->rmon_stats));

	if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask) &&
	    dev->ethtool_ops->get_eth_phy_stats)
		dev->ethtool_ops->get_eth_phy_stats(dev, &data->phy_stats);
	if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask) &&
	    dev->ethtool_ops->get_eth_mac_stats)
		dev->ethtool_ops->get_eth_mac_stats(dev, &data->mac_stats);
	if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask) &&
	    dev->ethtool_ops->get_eth_ctrl_stats)
		dev->ethtool_ops->get_eth_ctrl_stats(dev, &data->ctrl_stats);
	if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask) &&
	    dev->ethtool_ops->get_rmon_stats)
		dev->ethtool_ops->get_rmon_stats(dev, &data->rmon_stats,
						 &data->rmon_ranges);

	ethnl_ops_complete(dev);
	return 0;
}

static int stats_reply_size(const struct ethnl_req_info *req_base,
			    const struct ethnl_reply_data *reply_base)
{
	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
	unsigned int n_grps = 0, n_stats = 0;
	int len = 0;

	if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask)) {
		n_stats += sizeof(struct ethtool_eth_phy_stats) / sizeof(u64);
		n_grps++;
	}
	if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask)) {
		n_stats += sizeof(struct ethtool_eth_mac_stats) / sizeof(u64);
		n_grps++;
	}
	if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask)) {
		n_stats += sizeof(struct ethtool_eth_ctrl_stats) / sizeof(u64);
		n_grps++;
	}
	if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask)) {
		n_stats += sizeof(struct ethtool_rmon_stats) / sizeof(u64);
		n_grps++;
		/* Above includes the space for _A_STATS_GRP_HIST_VALs */

		len += (nla_total_size(0) +	/* _A_STATS_GRP_HIST */
			nla_total_size(4) +	/* _A_STATS_GRP_HIST_BKT_LOW */
			nla_total_size(4)) *	/* _A_STATS_GRP_HIST_BKT_HI */
			ETHTOOL_RMON_HIST_MAX * 2;
	}

	len += n_grps * (nla_total_size(0) + /* _A_STATS_GRP */
			 nla_total_size(4) + /* _A_STATS_GRP_ID */
			 nla_total_size(4)); /* _A_STATS_GRP_SS_ID */
	len += n_stats * (nla_total_size(0) + /* _A_STATS_GRP_STAT */
			  nla_total_size_64bit(sizeof(u64)));

	return len;
}

static int stat_put(struct sk_buff *skb, u16 attrtype, u64 val)
{
	struct nlattr *nest;
	int ret;

	if (val == ETHTOOL_STAT_NOT_SET)
		return 0;

	/* We want to start stats attr types from 0, so we don't have a type
	 * for pad inside ETHTOOL_A_STATS_GRP_STAT. Pad things on the outside
	 * of ETHTOOL_A_STATS_GRP_STAT. Since we're one nest away from the
	 * actual attr we're 4B off - nla_need_padding_for_64bit() & co.
	 * can't be used.
	 */
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
	if (!IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
		if (!nla_reserve(skb, ETHTOOL_A_STATS_GRP_PAD, 0))
			return -EMSGSIZE;
#endif

	nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP_STAT);
	if (!nest)
		return -EMSGSIZE;

	ret = nla_put_u64_64bit(skb, attrtype, val, -1 /* not used */);
	if (ret) {
		nla_nest_cancel(skb, nest);
		return ret;
	}

	nla_nest_end(skb, nest);
	return 0;
}

static int stats_put_phy_stats(struct sk_buff *skb,
			       const struct stats_reply_data *data)
{
	if (stat_put(skb, ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
		     data->phy_stats.SymbolErrorDuringCarrier))
		return -EMSGSIZE;
	return 0;
}

static int stats_put_mac_stats(struct sk_buff *skb,
			       const struct stats_reply_data *data)
{
	if (stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT,
		     data->mac_stats.FramesTransmittedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL,
		     data->mac_stats.SingleCollisionFrames) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL,
		     data->mac_stats.MultipleCollisionFrames) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT,
		     data->mac_stats.FramesReceivedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR,
		     data->mac_stats.FrameCheckSequenceErrors) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR,
		     data->mac_stats.AlignmentErrors) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES,
		     data->mac_stats.OctetsTransmittedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER,
		     data->mac_stats.FramesWithDeferredXmissions) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL,
		     data->mac_stats.LateCollisions) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_11_XS_COL,
		     data->mac_stats.FramesAbortedDueToXSColls) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR,
		     data->mac_stats.FramesLostDueToIntMACXmitError) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR,
		     data->mac_stats.CarrierSenseErrors) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES,
		     data->mac_stats.OctetsReceivedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR,
		     data->mac_stats.FramesLostDueToIntMACRcvError) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST,
		     data->mac_stats.MulticastFramesXmittedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST,
		     data->mac_stats.BroadcastFramesXmittedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER,
		     data->mac_stats.FramesWithExcessiveDeferral) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST,
		     data->mac_stats.MulticastFramesReceivedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST,
		     data->mac_stats.BroadcastFramesReceivedOK) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR,
		     data->mac_stats.InRangeLengthErrors) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN,
		     data->mac_stats.OutOfRangeLengthField) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR,
		     data->mac_stats.FrameTooLongErrors))
		return -EMSGSIZE;
	return 0;
}

static int stats_put_ctrl_stats(struct sk_buff *skb,
				const struct stats_reply_data *data)
{
	if (stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_3_TX,
		     data->ctrl_stats.MACControlFramesTransmitted) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_4_RX,
		     data->ctrl_stats.MACControlFramesReceived) ||
	    stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP,
		     data->ctrl_stats.UnsupportedOpcodesReceived))
		return -EMSGSIZE;
	return 0;
}

static int stats_put_rmon_hist(struct sk_buff *skb, u32 attr, const u64 *hist,
			       const struct ethtool_rmon_hist_range *ranges)
{
	struct nlattr *nest;
	int i;

	if (!ranges)
		return 0;

	for (i = 0; i <	ETHTOOL_RMON_HIST_MAX; i++) {
		if (!ranges[i].low && !ranges[i].high)
			break;
		if (hist[i] == ETHTOOL_STAT_NOT_SET)
			continue;

		nest = nla_nest_start(skb, attr);
		if (!nest)
			return -EMSGSIZE;

		if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_LOW,
				ranges[i].low) ||
		    nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_HI,
				ranges[i].high) ||
		    nla_put_u64_64bit(skb, ETHTOOL_A_STATS_GRP_HIST_VAL,
				      hist[i], ETHTOOL_A_STATS_GRP_PAD))
			goto err_cancel_hist;

		nla_nest_end(skb, nest);
	}

	return 0;

err_cancel_hist:
	nla_nest_cancel(skb, nest);
	return -EMSGSIZE;
}

static int stats_put_rmon_stats(struct sk_buff *skb,
				const struct stats_reply_data *data)
{
	if (stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_RX,
				data->rmon_stats.hist, data->rmon_ranges) ||
	    stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_TX,
				data->rmon_stats.hist_tx, data->rmon_ranges))
		return -EMSGSIZE;

	if (stat_put(skb, ETHTOOL_A_STATS_RMON_UNDERSIZE,
		     data->rmon_stats.undersize_pkts) ||
	    stat_put(skb, ETHTOOL_A_STATS_RMON_OVERSIZE,
		     data->rmon_stats.oversize_pkts) ||
	    stat_put(skb, ETHTOOL_A_STATS_RMON_FRAG,
		     data->rmon_stats.fragments) ||
	    stat_put(skb, ETHTOOL_A_STATS_RMON_JABBER,
		     data->rmon_stats.jabbers))
		return -EMSGSIZE;

	return 0;
}

static int stats_put_stats(struct sk_buff *skb,
			   const struct stats_reply_data *data,
			   u32 id, u32 ss_id,
			   int (*cb)(struct sk_buff *skb,
				     const struct stats_reply_data *data))
{
	struct nlattr *nest;

	nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP);
	if (!nest)
		return -EMSGSIZE;

	if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_ID, id) ||
	    nla_put_u32(skb, ETHTOOL_A_STATS_GRP_SS_ID, ss_id))
		goto err_cancel;

	if (cb(skb, data))
		goto err_cancel;

	nla_nest_end(skb, nest);
	return 0;

err_cancel:
	nla_nest_cancel(skb, nest);
	return -EMSGSIZE;
}

static int stats_fill_reply(struct sk_buff *skb,
			    const struct ethnl_req_info *req_base,
			    const struct ethnl_reply_data *reply_base)
{
	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
	const struct stats_reply_data *data = STATS_REPDATA(reply_base);
	int ret = 0;

	if (!ret && test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask))
		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_PHY,
				      ETH_SS_STATS_ETH_PHY,
				      stats_put_phy_stats);
	if (!ret && test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask))
		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_MAC,
				      ETH_SS_STATS_ETH_MAC,
				      stats_put_mac_stats);
	if (!ret && test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask))
		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_CTRL,
				      ETH_SS_STATS_ETH_CTRL,
				      stats_put_ctrl_stats);
	if (!ret && test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask))
		ret = stats_put_stats(skb, data, ETHTOOL_STATS_RMON,
				      ETH_SS_STATS_RMON, stats_put_rmon_stats);

	return ret;
}

const struct ethnl_request_ops ethnl_stats_request_ops = {
	.request_cmd		= ETHTOOL_MSG_STATS_GET,
	.reply_cmd		= ETHTOOL_MSG_STATS_GET_REPLY,
	.hdr_attr		= ETHTOOL_A_STATS_HEADER,
	.req_info_size		= sizeof(struct stats_req_info),
	.reply_data_size	= sizeof(struct stats_reply_data),

	.parse_request		= stats_parse_request,
	.prepare_data		= stats_prepare_data,
	.reply_size		= stats_reply_size,
	.fill_reply		= stats_fill_reply,
};