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

#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"

/* PSE_GET */

static const char *podl_pse_admin_state_name(u32 val)
{
	switch (val) {
	case ETHTOOL_PODL_PSE_ADMIN_STATE_UNKNOWN:
		return "unknown";
	case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
		return "disabled";
	case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
		return "enabled";
	default:
		return "unsupported";
	}
}

static const char *podl_pse_pw_d_status_name(u32 val)
{
	switch (val) {
	case ETHTOOL_PODL_PSE_PW_D_STATUS_UNKNOWN:
		return "unknown";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_DISABLED:
		return "disabled";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_SEARCHING:
		return "searching";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_DELIVERING:
		return "delivering power";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_SLEEP:
		return "sleep";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_IDLE:
		return "idle";
	case ETHTOOL_PODL_PSE_PW_D_STATUS_ERROR:
		return "error";
	default:
		return "unsupported";
	}
}

int pse_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
	const struct nlattr *tb[ETHTOOL_A_PSE_MAX + 1] = {};
	struct nl_context *nlctx = data;
	DECLARE_ATTR_TB_INFO(tb);
	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_PSE_HEADER]);
	if (!dev_ok(nlctx))
		return err_ret;

	if (silent)
		print_nl();

	open_json_object(NULL);

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

	if (tb[ETHTOOL_A_PODL_PSE_ADMIN_STATE]) {
		u32 val;

		val = mnl_attr_get_u32(tb[ETHTOOL_A_PODL_PSE_ADMIN_STATE]);
		print_string(PRINT_ANY, "podl-pse-admin-state",
			     "PoDL PSE Admin State: %s\n",
			     podl_pse_admin_state_name(val));
	}

	if (tb[ETHTOOL_A_PODL_PSE_PW_D_STATUS]) {
		u32 val;

		val = mnl_attr_get_u32(tb[ETHTOOL_A_PODL_PSE_PW_D_STATUS]);
		print_string(PRINT_ANY, "podl-pse-power-detection-status",
			     "PoDL PSE Power Detection Status: %s\n",
			     podl_pse_pw_d_status_name(val));
	}

	close_json_object();

	return MNL_CB_OK;
}

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

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

	nlsk = nlctx->ethnl_socket;
	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PSE_GET,
				      ETHTOOL_A_PSE_HEADER, 0);
	if (ret < 0)
		return ret;

	new_json_obj(ctx->json);
	ret = nlsock_send_get_request(nlsk, pse_reply_cb);
	delete_json_obj();

	return ret;
}

/* PSE_SET */

static const struct lookup_entry_u32 podl_pse_admin_control_values[] = {
	{ .arg = "enable",	.val = ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED },
	{ .arg = "disable",	.val = ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED },
	{}
};

static const struct param_parser spse_params[] = {
	{
		.arg		= "podl-pse-admin-control",
		.type		= ETHTOOL_A_PODL_PSE_ADMIN_CONTROL,
		.handler	= nl_parse_lookup_u32,
		.handler_data	= podl_pse_admin_control_values,
		.min_argc	= 1,
	},
	{}
};

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

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

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

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