summaryrefslogtreecommitdiffstats
path: root/netlink/eee.c
blob: 51b9d105dbc12f74d653a4ad9f465b8da9f22a42 (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
/*
 * eee.c - netlink implementation of eee commands
 *
 * Implementation of "ethtool --show-eee <dev>" and
 * "ethtool --set-eee <dev> ..."
 */

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

#include "../internal.h"
#include "../common.h"
#include "netlink.h"
#include "bitset.h"
#include "parser.h"
#include "../json_writer.h"

/* EEE_GET */

int eee_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
	const struct nlattr *tb[ETHTOOL_A_EEE_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	bool enabled, active, tx_lpi_enabled, status_support;
	struct nl_context *nlctx = data;
	bool silent;
	int err_ret;
	int ret;

	silent = nlctx->is_dump || nlctx->is_monitor || is_json_context();
	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_EEE_HEADER]);
	if (!dev_ok(nlctx))
		return err_ret;

	if (!tb[ETHTOOL_A_EEE_MODES_OURS] ||
	    !tb[ETHTOOL_A_EEE_ACTIVE] || !tb[ETHTOOL_A_EEE_ENABLED] ||
	    !tb[ETHTOOL_A_EEE_TX_LPI_ENABLED] ||
	    !tb[ETHTOOL_A_EEE_TX_LPI_TIMER]) {
		fprintf(stderr, "Malformed response from kernel\n");
		return err_ret;
	}
	active = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_ACTIVE]);
	enabled = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_ENABLED]);
	tx_lpi_enabled = mnl_attr_get_u8(tb[ETHTOOL_A_EEE_TX_LPI_ENABLED]);
	status_support = bitset_is_empty(tb[ETHTOOL_A_EEE_MODES_OURS], true, &ret);

	if (silent)
		putchar('\n');
	print_string(PRINT_ANY, "ifname", "EEE settings for %s:\n", nlctx->devname);
	print_string(PRINT_FP, NULL, "\tEEE status: ", NULL);
	if (status_support) {
		print_string(PRINT_ANY, "status", "%s\n", "not supported");
		return MNL_CB_OK;
	}
	if (!enabled)
		print_string(PRINT_ANY, "status", "%s\n", "disabled");
	else
		print_string(PRINT_ANY, "status", "enabled - %s\n", active ? "active" : "inactive");
	print_string(PRINT_FP, NULL, "\tTx LPI: ", NULL);
	if (tx_lpi_enabled)
		print_uint(PRINT_ANY, "tx-lpi", "%u (us)\n",
		       mnl_attr_get_u32(tb[ETHTOOL_A_EEE_TX_LPI_TIMER]));
	else
		print_string(PRINT_FP, NULL, "%s\n", "disabled");

	ret = dump_link_modes(nlctx, tb[ETHTOOL_A_EEE_MODES_OURS], true,
			      LM_CLASS_REAL,
			      "Supported EEE link modes:  ", NULL, "\n",
			      "Not reported", "supported-eee-link-modes");
	if (ret < 0)
		return err_ret;
	ret = dump_link_modes(nlctx, tb[ETHTOOL_A_EEE_MODES_OURS], false,
			      LM_CLASS_REAL,
			      "Advertised EEE link modes:  ", NULL, "\n",
			      "Not reported", "advertised-eee-link-modes");
	if (ret < 0)
		return err_ret;
	ret = dump_link_modes(nlctx, tb[ETHTOOL_A_EEE_MODES_PEER], false,
			      LM_CLASS_REAL,
			      "Link partner advertised EEE link modes:  ", NULL,
			      "\n", "Not reported", "link-partner-advertised-eee-link-modes");
	if (ret < 0)
		return err_ret;

	return MNL_CB_OK;
}

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

	if (netlink_cmd_check(ctx, ETHTOOL_MSG_EEE_GET, true))
		return -EOPNOTSUPP;
	if (ctx->argc > 0) {
		fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
			*ctx->argp);
		return 1;
	}

	new_json_obj(ctx->json);
	open_json_object(NULL);

	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_EEE_GET,
				      ETHTOOL_A_EEE_HEADER, 0);
	if (ret < 0)
		goto out;
	ret =  nlsock_send_get_request(nlsk, eee_reply_cb);
out:
	close_json_object();
	delete_json_obj();
	return ret;
}

/* EEE_SET */

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

static const struct param_parser seee_params[] = {
	{
		.arg		= "advertise",
		.type		= ETHTOOL_A_EEE_MODES_OURS,
		.handler	= nl_parse_bitset,
		.handler_data	= &advertise_parser_data,
		.min_argc	= 1,
	},
	{
		.arg		= "tx-lpi",
		.type		= ETHTOOL_A_EEE_TX_LPI_ENABLED,
		.handler	= nl_parse_u8bool,
		.min_argc	= 1,
	},
	{
		.arg		= "tx-timer",
		.type		= ETHTOOL_A_EEE_TX_LPI_TIMER,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{
		.arg		= "eee",
		.type		= ETHTOOL_A_EEE_ENABLED,
		.handler	= nl_parse_u8bool,
		.min_argc	= 1,
	},
	{}
};

int nl_seee(struct cmd_context *ctx)
{
	struct nl_context *nlctx = ctx->nlctx;
	struct nl_msg_buff *msgbuff;
	struct nl_socket *nlsk;
	int ret;

	if (netlink_cmd_check(ctx, ETHTOOL_MSG_EEE_SET, false))
		return -EOPNOTSUPP;
	if (!ctx->argc) {
		fprintf(stderr, "ethtool (--set-eee): parameters missing\n");
		return 1;
	}

	nlctx->cmd = "--set-eee";
	nlctx->argp = ctx->argp;
	nlctx->argc = ctx->argc;
	nlctx->devname = ctx->devname;
	nlsk = nlctx->ethnl_socket;
	msgbuff = &nlsk->msgbuff;

	ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_EEE_SET,
		       NLM_F_REQUEST | NLM_F_ACK);
	if (ret < 0)
		return 2;
	if (ethnla_fill_header(msgbuff, ETHTOOL_A_EEE_HEADER,
			       ctx->devname, 0))
		return -EMSGSIZE;

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

	ret = nlsock_sendmsg(nlsk, NULL);
	if (ret < 0)
		return 76;
	ret = nlsock_process_reply(nlsk, nomsg_reply_cb, nlctx);
	if (ret == 0)
		return 0;
	else
		return nlctx->exit_code ?: 76;
}