xref: /aosp_15_r20/external/ethtool/netlink/pse-pd.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /*
2  * pse.c - netlink implementation of pse commands
3  *
4  * Implementation of "ethtool --show-pse <dev>" and
5  * "ethtool --set-pse <dev> ..."
6  */
7 
8 #include <errno.h>
9 #include <ctype.h>
10 #include <inttypes.h>
11 #include <string.h>
12 #include <stdio.h>
13 
14 #include "../internal.h"
15 #include "../common.h"
16 #include "netlink.h"
17 #include "parser.h"
18 
19 /* PSE_GET */
20 
podl_pse_admin_state_name(u32 val)21 static const char *podl_pse_admin_state_name(u32 val)
22 {
23 	switch (val) {
24 	case ETHTOOL_PODL_PSE_ADMIN_STATE_UNKNOWN:
25 		return "unknown";
26 	case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
27 		return "disabled";
28 	case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
29 		return "enabled";
30 	default:
31 		return "unsupported";
32 	}
33 }
34 
podl_pse_pw_d_status_name(u32 val)35 static const char *podl_pse_pw_d_status_name(u32 val)
36 {
37 	switch (val) {
38 	case ETHTOOL_PODL_PSE_PW_D_STATUS_UNKNOWN:
39 		return "unknown";
40 	case ETHTOOL_PODL_PSE_PW_D_STATUS_DISABLED:
41 		return "disabled";
42 	case ETHTOOL_PODL_PSE_PW_D_STATUS_SEARCHING:
43 		return "searching";
44 	case ETHTOOL_PODL_PSE_PW_D_STATUS_DELIVERING:
45 		return "delivering power";
46 	case ETHTOOL_PODL_PSE_PW_D_STATUS_SLEEP:
47 		return "sleep";
48 	case ETHTOOL_PODL_PSE_PW_D_STATUS_IDLE:
49 		return "idle";
50 	case ETHTOOL_PODL_PSE_PW_D_STATUS_ERROR:
51 		return "error";
52 	default:
53 		return "unsupported";
54 	}
55 }
56 
pse_reply_cb(const struct nlmsghdr * nlhdr,void * data)57 int pse_reply_cb(const struct nlmsghdr *nlhdr, void *data)
58 {
59 	const struct nlattr *tb[ETHTOOL_A_PSE_MAX + 1] = {};
60 	struct nl_context *nlctx = data;
61 	DECLARE_ATTR_TB_INFO(tb);
62 	bool silent;
63 	int err_ret;
64 	int ret;
65 
66 	silent = nlctx->is_dump || nlctx->is_monitor;
67 	err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
68 	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
69 	if (ret < 0)
70 		return err_ret;
71 	nlctx->devname = get_dev_name(tb[ETHTOOL_A_PSE_HEADER]);
72 	if (!dev_ok(nlctx))
73 		return err_ret;
74 
75 	if (silent)
76 		print_nl();
77 
78 	open_json_object(NULL);
79 
80 	print_string(PRINT_ANY, "ifname", "PSE attributes for %s:\n",
81 		     nlctx->devname);
82 
83 	if (tb[ETHTOOL_A_PODL_PSE_ADMIN_STATE]) {
84 		u32 val;
85 
86 		val = mnl_attr_get_u32(tb[ETHTOOL_A_PODL_PSE_ADMIN_STATE]);
87 		print_string(PRINT_ANY, "podl-pse-admin-state",
88 			     "PoDL PSE Admin State: %s\n",
89 			     podl_pse_admin_state_name(val));
90 	}
91 
92 	if (tb[ETHTOOL_A_PODL_PSE_PW_D_STATUS]) {
93 		u32 val;
94 
95 		val = mnl_attr_get_u32(tb[ETHTOOL_A_PODL_PSE_PW_D_STATUS]);
96 		print_string(PRINT_ANY, "podl-pse-power-detection-status",
97 			     "PoDL PSE Power Detection Status: %s\n",
98 			     podl_pse_pw_d_status_name(val));
99 	}
100 
101 	close_json_object();
102 
103 	return MNL_CB_OK;
104 }
105 
nl_gpse(struct cmd_context * ctx)106 int nl_gpse(struct cmd_context *ctx)
107 {
108 	struct nl_context *nlctx = ctx->nlctx;
109 	struct nl_socket *nlsk;
110 	int ret;
111 
112 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_PSE_GET, true))
113 		return -EOPNOTSUPP;
114 	if (ctx->argc > 0) {
115 		fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
116 			*ctx->argp);
117 		return 1;
118 	}
119 
120 	nlsk = nlctx->ethnl_socket;
121 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_PSE_GET,
122 				      ETHTOOL_A_PSE_HEADER, 0);
123 	if (ret < 0)
124 		return ret;
125 
126 	new_json_obj(ctx->json);
127 	ret = nlsock_send_get_request(nlsk, pse_reply_cb);
128 	delete_json_obj();
129 
130 	return ret;
131 }
132 
133 /* PSE_SET */
134 
135 static const struct lookup_entry_u32 podl_pse_admin_control_values[] = {
136 	{ .arg = "enable",	.val = ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED },
137 	{ .arg = "disable",	.val = ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED },
138 	{}
139 };
140 
141 static const struct param_parser spse_params[] = {
142 	{
143 		.arg		= "podl-pse-admin-control",
144 		.type		= ETHTOOL_A_PODL_PSE_ADMIN_CONTROL,
145 		.handler	= nl_parse_lookup_u32,
146 		.handler_data	= podl_pse_admin_control_values,
147 		.min_argc	= 1,
148 	},
149 	{}
150 };
151 
nl_spse(struct cmd_context * ctx)152 int nl_spse(struct cmd_context *ctx)
153 {
154 	struct nl_context *nlctx = ctx->nlctx;
155 	struct nl_msg_buff *msgbuff;
156 	struct nl_socket *nlsk;
157 	int ret;
158 
159 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_PSE_SET, false))
160 		return -EOPNOTSUPP;
161 	if (!ctx->argc) {
162 		fprintf(stderr, "ethtool (--set-pse): parameters missing\n");
163 		return 1;
164 	}
165 
166 	nlctx->cmd = "--set-pse";
167 	nlctx->argp = ctx->argp;
168 	nlctx->argc = ctx->argc;
169 	nlctx->devname = ctx->devname;
170 	nlsk = nlctx->ethnl_socket;
171 	msgbuff = &nlsk->msgbuff;
172 
173 	ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_PSE_SET,
174 		       NLM_F_REQUEST | NLM_F_ACK);
175 	if (ret < 0)
176 		return 2;
177 	if (ethnla_fill_header(msgbuff, ETHTOOL_A_PSE_HEADER,
178 			       ctx->devname, 0))
179 		return -EMSGSIZE;
180 
181 	ret = nl_parser(nlctx, spse_params, NULL, PARSER_GROUP_NONE, NULL);
182 	if (ret < 0)
183 		return 1;
184 
185 	ret = nlsock_sendmsg(nlsk, NULL);
186 	if (ret < 0)
187 		return 83;
188 	ret = nlsock_process_reply(nlsk, nomsg_reply_cb, nlctx);
189 	if (ret == 0)
190 		return 0;
191 	else
192 		return nlctx->exit_code ?: 83;
193 }
194