summaryrefslogtreecommitdiffstats
path: root/netlink/stats.c
blob: 8620d8de15554e159b111c94fddb4bafcf71def3 (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
/*
 * stats.c - netlink implementation of stats
 *
 * Implementation of "ethtool -S <dev> [--groups <types>] etc."
 */

#include <errno.h>
#include <ctype.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>

#include "../internal.h"
#include "../common.h"
#include "netlink.h"
#include "parser.h"
#include "strset.h"

static int parse_rmon_hist_one(const char *grp_name, const struct nlattr *hist,
			       const char *dir)
{
	const struct nlattr *tb[ETHTOOL_A_STATS_GRP_HIST_VAL + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	unsigned long long val;
	unsigned int low, hi;
	int ret;

	ret = mnl_attr_parse_nested(hist, attr_cb, &tb_info);
	if (ret < 0) {
		fprintf(stderr, "invalid kernel response - malformed histogram entry\n");
		return 1;
	}

	if (!tb[ETHTOOL_A_STATS_GRP_HIST_BKT_LOW] ||
	    !tb[ETHTOOL_A_STATS_GRP_HIST_BKT_HI] ||
	    !tb[ETHTOOL_A_STATS_GRP_HIST_VAL]) {
		fprintf(stderr, "invalid kernel response - histogram entry missing attributes\n");
		return 1;
	}

	low = mnl_attr_get_u32(tb[ETHTOOL_A_STATS_GRP_HIST_BKT_LOW]);
	hi = mnl_attr_get_u32(tb[ETHTOOL_A_STATS_GRP_HIST_BKT_HI]);
	val = mnl_attr_get_u64(tb[ETHTOOL_A_STATS_GRP_HIST_VAL]);

	if (!is_json_context()) {
		fprintf(stdout, "%s-%s-etherStatsPkts", dir, grp_name);

		if (low && hi) {
			fprintf(stdout, "%uto%uOctets: %llu\n", low, hi, val);
		} else if (hi) {
			fprintf(stdout, "%uOctets: %llu\n", hi, val);
		} else if (low) {
			fprintf(stdout, "%utoMaxOctets: %llu\n", low, val);
		} else {
			fprintf(stderr, "invalid kernel response - bad histogram entry bounds\n");
			return 1;
		}
	} else {
		open_json_object(NULL);
		print_uint(PRINT_JSON, "low", NULL, low);
		print_uint(PRINT_JSON, "high", NULL, hi);
		print_u64(PRINT_JSON, "val", NULL, val);
		close_json_object();
	}

	return 0;
}

static int parse_rmon_hist(const struct nlattr *grp, const char *grp_name,
			   const char *name, const char *dir, unsigned int type)
{
	const struct nlattr *attr;

	open_json_array(name, "");

	mnl_attr_for_each_nested(attr, grp) {
		if (mnl_attr_get_type(attr) == type &&
		    parse_rmon_hist_one(grp_name, attr, dir))
			goto err_close_rmon;
	}
	close_json_array("");

	return 0;

err_close_rmon:
	close_json_array("");
	return 1;
}

static int parse_grp(struct nl_context *nlctx, const struct nlattr *grp,
		     const struct stringset *std_str)
{
	const struct nlattr *tb[ETHTOOL_A_STATS_GRP_SS_ID + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	bool hist_rx = false, hist_tx = false;
	const struct stringset *stat_str;
	const struct nlattr *attr, *stat;
	const char *std_name, *name;
	unsigned int ss_id, id, s;
	unsigned long long val;
	int ret;

	ret = mnl_attr_parse_nested(grp, attr_cb, &tb_info);
	if (ret < 0)
		return 1;

	if (!tb[ETHTOOL_A_STATS_GRP_ID])
		return 1;
	if (!tb[ETHTOOL_A_STATS_GRP_SS_ID])
		return 0;

	id = mnl_attr_get_u32(tb[ETHTOOL_A_STATS_GRP_ID]);
	ss_id = mnl_attr_get_u32(tb[ETHTOOL_A_STATS_GRP_SS_ID]);

	stat_str = global_stringset(ss_id, nlctx->ethnl2_socket);

	std_name = get_string(std_str, id);
	open_json_object(std_name);

	mnl_attr_for_each_nested(attr, grp) {
		switch (mnl_attr_get_type(attr)) {
		case ETHTOOL_A_STATS_GRP_STAT:
			break;
		case ETHTOOL_A_STATS_GRP_HIST_RX:
			hist_rx = true;
			continue;
		case ETHTOOL_A_STATS_GRP_HIST_TX:
			hist_tx = true;
			continue;
		default:
			continue;
		}

		stat = mnl_attr_get_payload(attr);
		ret = mnl_attr_validate(stat, MNL_TYPE_U64);
		if (ret) {
			fprintf(stderr, "invalid kernel response - bad statistic entry\n");
			goto err_close_grp;
		}
		s = mnl_attr_get_type(stat);
		name = get_string(stat_str, s);
		if (!name || !name[0])
			continue;

		if (!is_json_context())
			fprintf(stdout, "%s-%s: ", std_name, name);

		val = mnl_attr_get_u64(stat);
		print_u64(PRINT_ANY, name, "%llu\n", val);
	}

	if (hist_rx)
		parse_rmon_hist(grp, std_name, "rx-pktsNtoM", "rx",
				ETHTOOL_A_STATS_GRP_HIST_RX);
	if (hist_tx)
		parse_rmon_hist(grp, std_name, "tx-pktsNtoM", "tx",
				ETHTOOL_A_STATS_GRP_HIST_TX);

	close_json_object();

	return 0;

err_close_grp:
	close_json_object();
	return 1;
}

static int stats_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
	const struct nlattr *tb[ETHTOOL_A_STATS_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	struct nl_context *nlctx = data;
	const struct stringset *std_str;
	const struct nlattr *attr;
	bool silent;
	int err_ret;
	int ret;

	silent = nlctx->is_dump || nlctx->is_monitor;
	err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
	if (ret < 0)
		return err_ret;
	nlctx->devname = get_dev_name(tb[ETHTOOL_A_STATS_HEADER]);
	if (!dev_ok(nlctx))
		return err_ret;

	ret = netlink_init_ethnl2_socket(nlctx);
	if (ret < 0)
		return err_ret;
	std_str = global_stringset(ETH_SS_STATS_STD, nlctx->ethnl2_socket);

	if (silent)
		print_nl();

	open_json_object(NULL);

	print_string(PRINT_ANY, "ifname", "Standard stats for %s:\n",
		     nlctx->devname);

	mnl_attr_for_each(attr, nlhdr, GENL_HDRLEN) {
		if (mnl_attr_get_type(attr) == ETHTOOL_A_STATS_GRP) {
			ret = parse_grp(nlctx, attr, std_str);
			if (ret)
				goto err_close_dev;
		}
	}

	close_json_object();

	return MNL_CB_OK;

err_close_dev:
	close_json_object();
	return err_ret;
}

static const struct bitset_parser_data stats_parser_data = {
	.no_mask	= true,
	.force_hex	= false,
};

static int stats_parse_all_groups(struct nl_context *nlctx, uint16_t type,
				  const void *data, struct nl_msg_buff *msgbuff,
				  void *dest)
{
	const struct stringset *std_str;
	struct nlattr *nest;
	int i, ret, nbits;
	uint32_t *bits;

	if (data || dest)
		return -EFAULT;

	/* ethnl2 and strset code already does caching */
	ret = netlink_init_ethnl2_socket(nlctx);
	if (ret < 0)
		return ret;
	std_str = global_stringset(ETH_SS_STATS_STD, nlctx->ethnl2_socket);

	nbits = get_count(std_str);
	bits = calloc(DIV_ROUND_UP(nbits, 32), sizeof(uint32_t));
	if (!bits)
		return -ENOMEM;

	for (i = 0; i < nbits; i++)
		bits[i / 32] |= 1U << (i % 32);

	ret = -EMSGSIZE;
	nest = ethnla_nest_start(msgbuff, type);
	if (!nest)
		goto err_free;

	if (ethnla_put_flag(msgbuff, ETHTOOL_A_BITSET_NOMASK, true) ||
	    ethnla_put_u32(msgbuff, ETHTOOL_A_BITSET_SIZE, nbits) ||
	    ethnla_put(msgbuff, ETHTOOL_A_BITSET_VALUE,
		       DIV_ROUND_UP(nbits, 32) * sizeof(uint32_t), bits))
		goto err_cancel;

	ethnla_nest_end(msgbuff, nest);
	free(bits);
	return 0;

err_cancel:
	ethnla_nest_cancel(msgbuff, nest);
err_free:
	free(bits);
	return ret;
}

static const struct lookup_entry_u32 stats_src_values[] = {
	{ .arg = "aggregate",	.val = ETHTOOL_MAC_STATS_SRC_AGGREGATE },
	{ .arg = "emac",	.val = ETHTOOL_MAC_STATS_SRC_EMAC },
	{ .arg = "pmac",	.val = ETHTOOL_MAC_STATS_SRC_PMAC },
	{}
};

static const struct param_parser stats_params[] = {
	{
		.arg		= "--groups",
		.type		= ETHTOOL_A_STATS_GROUPS,
		.handler	= nl_parse_bitset,
		.handler_data	= &stats_parser_data,
		.min_argc	= 1,
		.alt_group	= 1,
	},
	{
		.arg		= "--all-groups",
		.type		= ETHTOOL_A_STATS_GROUPS,
		.handler	= stats_parse_all_groups,
		.alt_group	= 1,
	},
	{
		.arg		= "--src",
		.type		= ETHTOOL_A_STATS_SRC,
		.handler	= nl_parse_lookup_u32,
		.handler_data	= stats_src_values,
		.min_argc	= 1,
	},
	{}
};

int nl_gstats(struct cmd_context *ctx)
{
	struct nl_context *nlctx = ctx->nlctx;
	struct nl_socket *nlsk = nlctx->ethnl_socket;
	int ret;

	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_STATS_GET,
				      ETHTOOL_A_STATS_HEADER, 0);
	if (ret < 0)
		return ret;

	nlctx->cmd = "-S";
	nlctx->argp = ctx->argp;
	nlctx->argc = ctx->argc;
	nlctx->devname = ctx->devname;
	nlsk = nlctx->ethnl_socket;

	ret = nl_parser(nlctx, stats_params, NULL, PARSER_GROUP_NONE, NULL);
	if (ret < 0)
		return 1;

	new_json_obj(ctx->json);
	ret = nlsock_send_get_request(nlsk, stats_reply_cb);
	delete_json_obj();
	return ret;
}

bool nl_gstats_chk(struct cmd_context *ctx)
{
	return ctx->argc;
}