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

/* PLCA_GET_CFG */

int plca_get_cfg_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
	const struct nlattr *tb[ETHTOOL_A_PLCA_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	struct nl_context *nlctx = data;
	bool silent;
	int idv = 255;
	int err_ret;
	int val;
	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_PLCA_HEADER]);
	if (!dev_ok(nlctx))
		return err_ret;

	if (silent)
		putchar('\n');

	printf("PLCA settings for %s:\n", nlctx->devname);

	// check if PLCA is enabled
	printf("\tEnabled: ");

	if (!tb[ETHTOOL_A_PLCA_ENABLED]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u8(tb[ETHTOOL_A_PLCA_ENABLED]);
		printf(val ? "Yes" : "No");
	}
	putchar('\n');

	// get node ID
	printf("\tlocal node ID: ");

	if (!tb[ETHTOOL_A_PLCA_NODE_ID]) {
		printf("not supported");
	} else {
		idv = mnl_attr_get_u32(tb[ETHTOOL_A_PLCA_NODE_ID]);
		printf("%u (%s)", idv,
		       idv == 0 ? "coordinator" :
		       idv == 255 ? "unconfigured" : "follower");
	}
	putchar('\n');

	// get node count
	printf("\tNode count: ");
	if (!tb[ETHTOOL_A_PLCA_NODE_CNT]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u32(tb[ETHTOOL_A_PLCA_NODE_CNT]);
		printf("%u", val);

		// The node count is ignored by follower nodes. However, it can
		// be pre-set to enable fast coordinator role switchover.
		// Therefore, on a follower node we still wanto to show it,
		// indicating it is not currently used.
		if (tb[ETHTOOL_A_PLCA_NODE_ID] && idv != 0)
			printf(" (ignored)");
	}
	putchar('\n');

	// get TO timer (transmit opportunity timer)
	printf("\tTO timer: ");
	if (!tb[ETHTOOL_A_PLCA_TO_TMR]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u32(tb[ETHTOOL_A_PLCA_TO_TMR]);
		printf("%u BT", val);
	}
	putchar('\n');

	// get burst count
	printf("\tBurst count: ");
	if (!tb[ETHTOOL_A_PLCA_BURST_CNT]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u32(tb[ETHTOOL_A_PLCA_BURST_CNT]);
		printf("%u (%s)", val,
		       val > 0 ? "enabled" : "disabled");
	}
	putchar('\n');

	// get burst timer
	printf("\tBurst timer: ");
	if (!tb[ETHTOOL_A_PLCA_BURST_TMR]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u32(tb[ETHTOOL_A_PLCA_BURST_TMR]);
		printf("%u BT", val);
	}
	putchar('\n');

	return MNL_CB_OK;
}


int nl_plca_get_cfg(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_PLCA_GET_CFG, true))
		return -EOPNOTSUPP;

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

	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PLCA_GET_CFG,
				      ETHTOOL_A_PLCA_HEADER, 0);

	if (ret < 0)
		return ret;

	return nlsock_send_get_request(nlsk, plca_get_cfg_reply_cb);
}

/* PLCA_SET_CFG */

static const struct param_parser set_plca_params[] = {
	{
		.arg		= "enable",
		.type		= ETHTOOL_A_PLCA_ENABLED,
		.handler	= nl_parse_u8bool,
		.min_argc	= 1,
	},
	{
		.arg		= "node-id",
		.type		= ETHTOOL_A_PLCA_NODE_ID,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{
		.arg		= "node-cnt",
		.type		= ETHTOOL_A_PLCA_NODE_CNT,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{
		.arg		= "to-tmr",
		.type		= ETHTOOL_A_PLCA_TO_TMR,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{
		.arg		= "burst-cnt",
		.type		= ETHTOOL_A_PLCA_BURST_CNT,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{
		.arg		= "burst-tmr",
		.type		= ETHTOOL_A_PLCA_BURST_TMR,
		.handler	= nl_parse_direct_u32,
		.min_argc	= 1,
	},
	{}
};

int nl_plca_set_cfg(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_PLCA_SET_CFG, false))
		return -EOPNOTSUPP;
	if (!ctx->argc) {
		fprintf(stderr,
			"ethtool (--set-plca-cfg): parameters missing\n");
		return 1;
	}

	nlctx->cmd = "--set-plca-cfg";
	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_PLCA_SET_CFG,
		       NLM_F_REQUEST | NLM_F_ACK);
	if (ret < 0)
		return 2;
	if (ethnla_fill_header(msgbuff, ETHTOOL_A_PLCA_HEADER,
			       ctx->devname, 0))
		return -EMSGSIZE;

	ret = nl_parser(nlctx, set_plca_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;
}

/* PLCA_GET_STATUS */

int plca_get_status_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
	const struct nlattr *tb[ETHTOOL_A_PLCA_MAX + 1] = {};
	DECLARE_ATTR_TB_INFO(tb);
	struct nl_context *nlctx = data;
	bool silent;
	int err_ret;
	int ret;
	u8 val;

	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_PLCA_HEADER]);
	if (!dev_ok(nlctx))
		return err_ret;

	if (silent)
		putchar('\n');

	printf("PLCA status of %s:\n", nlctx->devname);

	// check whether the Open Alliance TC14 standard memory map is supported
	printf("\tStatus: ");

	if (!tb[ETHTOOL_A_PLCA_STATUS]) {
		printf("not supported");
	} else {
		val = mnl_attr_get_u8(tb[ETHTOOL_A_PLCA_STATUS]);
		printf(val ? "on" : "off");
	}
	putchar('\n');

	return MNL_CB_OK;
}


int nl_plca_get_status(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_PLCA_GET_STATUS, true))
		return -EOPNOTSUPP;

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

	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PLCA_GET_STATUS,
				      ETHTOOL_A_PLCA_HEADER, 0);

	if (ret < 0)
		return ret;

	return nlsock_send_get_request(nlsk, plca_get_status_reply_cb);
}