xref: /aosp_15_r20/external/ethtool/netlink/coalesce.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /*
2  * coalesce.c - netlink implementation of coalescing commands
3  *
4  * Implementation of "ethtool -c <dev>" and "ethtool -C <dev> ..."
5  */
6 
7 #include <errno.h>
8 #include <string.h>
9 #include <stdio.h>
10 
11 #include "../internal.h"
12 #include "../common.h"
13 #include "netlink.h"
14 #include "parser.h"
15 
16 /* COALESCE_GET */
17 
coalesce_reply_cb(const struct nlmsghdr * nlhdr,void * data)18 int coalesce_reply_cb(const struct nlmsghdr *nlhdr, void *data)
19 {
20 	const struct nlattr *tb[ETHTOOL_A_COALESCE_MAX + 1] = {};
21 	DECLARE_ATTR_TB_INFO(tb);
22 	struct nl_context *nlctx = data;
23 	bool silent;
24 	int err_ret;
25 	int ret;
26 
27 	silent = nlctx->is_dump || nlctx->is_monitor;
28 	err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
29 	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
30 	if (ret < 0)
31 		return err_ret;
32 	nlctx->devname = get_dev_name(tb[ETHTOOL_A_COALESCE_HEADER]);
33 	if (!dev_ok(nlctx))
34 		return err_ret;
35 
36 	open_json_object(NULL);
37 
38 	if (silent)
39 		show_cr();
40 	print_string(PRINT_ANY, "ifname", "Coalesce parameters for %s:\n",
41 		     nlctx->devname);
42 	show_bool("rx", "Adaptive RX: %s  ",
43 		  tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]);
44 	show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]);
45 	show_u32("stats-block-usecs", "stats-block-usecs:\t",
46 		 tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS]);
47 	show_u32("sample-interval", "sample-interval:\t",
48 		 tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL]);
49 	show_u32("pkt-rate-low", "pkt-rate-low:\t\t",
50 		 tb[ETHTOOL_A_COALESCE_PKT_RATE_LOW]);
51 	show_u32("pkt-rate-high", "pkt-rate-high:\t\t",
52 		 tb[ETHTOOL_A_COALESCE_PKT_RATE_HIGH]);
53 	show_cr();
54 	show_u32("rx-usecs", "rx-usecs:\t", tb[ETHTOOL_A_COALESCE_RX_USECS]);
55 	show_u32("rx-frames", "rx-frames:\t",
56 		 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES]);
57 	show_u32("rx-usecs-irq", "rx-usecs-irq:\t",
58 		 tb[ETHTOOL_A_COALESCE_RX_USECS_IRQ]);
59 	show_u32("rx-frames-irq", "rx-frames-irq:\t",
60 		 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ]);
61 	show_cr();
62 	show_u32("tx-usecs", "tx-usecs:\t", tb[ETHTOOL_A_COALESCE_TX_USECS]);
63 	show_u32("tx-frames", "tx-frames:\t",
64 		 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES]);
65 	show_u32("tx-usecs-irq", "tx-usecs-irq:\t",
66 		 tb[ETHTOOL_A_COALESCE_TX_USECS_IRQ]);
67 	show_u32("tx-frames-irq", "tx-frames-irq:\t",
68 		 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ]);
69 	show_cr();
70 	show_u32("rx-usecs-low", "rx-usecs-low:\t",
71 		 tb[ETHTOOL_A_COALESCE_RX_USECS_LOW]);
72 	show_u32("rx-frame-low", "rx-frame-low:\t",
73 		 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW]);
74 	show_u32("tx-usecs-low", "tx-usecs-low:\t",
75 		 tb[ETHTOOL_A_COALESCE_TX_USECS_LOW]);
76 	show_u32("tx-frame-low", "tx-frame-low:\t",
77 		 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW]);
78 	show_cr();
79 	show_u32("rx-usecs-high", "rx-usecs-high:\t",
80 		 tb[ETHTOOL_A_COALESCE_RX_USECS_HIGH]);
81 	show_u32("rx-frame-high", "rx-frame-high:\t",
82 		 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH]);
83 	show_u32("tx-usecs-high", "tx-usecs-high:\t",
84 		 tb[ETHTOOL_A_COALESCE_TX_USECS_HIGH]);
85 	show_u32("tx-frame-high", "tx-frame-high:\t",
86 		 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH]);
87 	show_cr();
88 	show_bool("rx", "CQE mode RX: %s  ",
89 		  tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_RX]);
90 	show_bool("tx", "TX: %s\n", tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_TX]);
91 	show_cr();
92 	show_u32("tx-aggr-max-bytes", "tx-aggr-max-bytes:\t",
93 		 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES]);
94 	show_u32("tx-aggr-max-frames", "tx-aggr-max-frames:\t",
95 		 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES]);
96 	show_u32("tx-aggr-time-usecs", "tx-aggr-time-usecs\t",
97 		 tb[ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS]);
98 	show_cr();
99 
100 	close_json_object();
101 
102 	return MNL_CB_OK;
103 }
104 
nl_gcoalesce(struct cmd_context * ctx)105 int nl_gcoalesce(struct cmd_context *ctx)
106 {
107 	struct nl_context *nlctx = ctx->nlctx;
108 	struct nl_socket *nlsk = nlctx->ethnl_socket;
109 	int ret;
110 
111 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_COALESCE_GET, true))
112 		return -EOPNOTSUPP;
113 	if (ctx->argc > 0) {
114 		fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
115 			*ctx->argp);
116 		return 1;
117 	}
118 
119 	ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_COALESCE_GET,
120 				      ETHTOOL_A_COALESCE_HEADER, 0);
121 	if (ret < 0)
122 		return ret;
123 
124 	new_json_obj(ctx->json);
125 	ret = nlsock_send_get_request(nlsk, coalesce_reply_cb);
126 	delete_json_obj();
127 	return ret;
128 }
129 
130 /* COALESCE_SET */
131 
132 static const struct param_parser scoalesce_params[] = {
133 	{
134 		.arg		= "adaptive-rx",
135 		.type		= ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX,
136 		.handler	= nl_parse_u8bool,
137 		.min_argc	= 1,
138 	},
139 	{
140 		.arg		= "adaptive-tx",
141 		.type		= ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX,
142 		.handler	= nl_parse_u8bool,
143 		.min_argc	= 1,
144 	},
145 	{
146 		.arg		= "sample-interval",
147 		.type		= ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL,
148 		.handler	= nl_parse_direct_u32,
149 		.min_argc	= 1,
150 	},
151 	{
152 		.arg		= "stats-block-usecs",
153 		.type		= ETHTOOL_A_COALESCE_STATS_BLOCK_USECS,
154 		.handler	= nl_parse_direct_u32,
155 		.min_argc	= 1,
156 	},
157 	{
158 		.arg		= "pkt-rate-low",
159 		.type		= ETHTOOL_A_COALESCE_PKT_RATE_LOW,
160 		.handler	= nl_parse_direct_u32,
161 		.min_argc	= 1,
162 	},
163 	{
164 		.arg		= "pkt-rate-high",
165 		.type		= ETHTOOL_A_COALESCE_PKT_RATE_HIGH,
166 		.handler	= nl_parse_direct_u32,
167 		.min_argc	= 1,
168 	},
169 	{
170 		.arg		= "rx-usecs",
171 		.type		= ETHTOOL_A_COALESCE_RX_USECS,
172 		.handler	= nl_parse_direct_u32,
173 		.min_argc	= 1,
174 	},
175 	{
176 		.arg		= "rx-frames",
177 		.type		= ETHTOOL_A_COALESCE_RX_MAX_FRAMES,
178 		.handler	= nl_parse_direct_u32,
179 		.min_argc	= 1,
180 	},
181 	{
182 		.arg		= "rx-usecs-irq",
183 		.type		= ETHTOOL_A_COALESCE_RX_USECS_IRQ,
184 		.handler	= nl_parse_direct_u32,
185 		.min_argc	= 1,
186 	},
187 	{
188 		.arg		= "rx-frames-irq",
189 		.type		= ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ,
190 		.handler	= nl_parse_direct_u32,
191 		.min_argc	= 1,
192 	},
193 	{
194 		.arg		= "tx-usecs",
195 		.type		= ETHTOOL_A_COALESCE_TX_USECS,
196 		.handler	= nl_parse_direct_u32,
197 		.min_argc	= 1,
198 	},
199 	{
200 		.arg		= "tx-frames",
201 		.type		= ETHTOOL_A_COALESCE_TX_MAX_FRAMES,
202 		.handler	= nl_parse_direct_u32,
203 		.min_argc	= 1,
204 	},
205 	{
206 		.arg		= "tx-usecs-irq",
207 		.type		= ETHTOOL_A_COALESCE_TX_USECS_IRQ,
208 		.handler	= nl_parse_direct_u32,
209 		.min_argc	= 1,
210 	},
211 	{
212 		.arg		= "tx-frames-irq",
213 		.type		= ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ,
214 		.handler	= nl_parse_direct_u32,
215 		.min_argc	= 1,
216 	},
217 	{
218 		.arg		= "rx-usecs-low",
219 		.type		= ETHTOOL_A_COALESCE_RX_USECS_LOW,
220 		.handler	= nl_parse_direct_u32,
221 		.min_argc	= 1,
222 	},
223 	{
224 		.arg		= "rx-frames-low",
225 		.type		= ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW,
226 		.handler	= nl_parse_direct_u32,
227 		.min_argc	= 1,
228 	},
229 	{
230 		.arg		= "tx-usecs-low",
231 		.type		= ETHTOOL_A_COALESCE_TX_USECS_LOW,
232 		.handler	= nl_parse_direct_u32,
233 		.min_argc	= 1,
234 	},
235 	{
236 		.arg		= "tx-frames-low",
237 		.type		= ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW,
238 		.handler	= nl_parse_direct_u32,
239 		.min_argc	= 1,
240 	},
241 	{
242 		.arg		= "rx-usecs-high",
243 		.type		= ETHTOOL_A_COALESCE_RX_USECS_HIGH,
244 		.handler	= nl_parse_direct_u32,
245 		.min_argc	= 1,
246 	},
247 	{
248 		.arg		= "rx-frames-high",
249 		.type		= ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH,
250 		.handler	= nl_parse_direct_u32,
251 		.min_argc	= 1,
252 	},
253 	{
254 		.arg		= "tx-usecs-high",
255 		.type		= ETHTOOL_A_COALESCE_TX_USECS_HIGH,
256 		.handler	= nl_parse_direct_u32,
257 		.min_argc	= 1,
258 	},
259 	{
260 		.arg		= "tx-frames-high",
261 		.type		= ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH,
262 		.handler	= nl_parse_direct_u32,
263 		.min_argc	= 1,
264 	},
265 	{
266 		.arg		= "cqe-mode-rx",
267 		.type		= ETHTOOL_A_COALESCE_USE_CQE_MODE_RX,
268 		.handler	= nl_parse_u8bool,
269 		.min_argc	= 1,
270 	},
271 	{
272 		.arg		= "cqe-mode-tx",
273 		.type		= ETHTOOL_A_COALESCE_USE_CQE_MODE_TX,
274 		.handler	= nl_parse_u8bool,
275 		.min_argc	= 1,
276 	},
277 	{
278 		.arg		= "tx-aggr-max-bytes",
279 		.type		= ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES,
280 		.handler	= nl_parse_direct_u32,
281 		.min_argc	= 1,
282 	},
283 	{
284 		.arg		= "tx-aggr-max-frames",
285 		.type		= ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES,
286 		.handler	= nl_parse_direct_u32,
287 		.min_argc	= 1,
288 	},
289 	{
290 		.arg		= "tx-aggr-time-usecs",
291 		.type		= ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS,
292 		.handler	= nl_parse_direct_u32,
293 		.min_argc	= 1,
294 	},
295 	{}
296 };
297 
nl_scoalesce(struct cmd_context * ctx)298 int nl_scoalesce(struct cmd_context *ctx)
299 {
300 	struct nl_context *nlctx = ctx->nlctx;
301 	struct nl_msg_buff *msgbuff;
302 	struct nl_socket *nlsk;
303 	int ret;
304 
305 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_COALESCE_SET, false))
306 		return -EOPNOTSUPP;
307 
308 	nlctx->cmd = "-C";
309 	nlctx->argp = ctx->argp;
310 	nlctx->argc = ctx->argc;
311 	nlctx->devname = ctx->devname;
312 	nlsk = nlctx->ethnl_socket;
313 	msgbuff = &nlsk->msgbuff;
314 
315 	ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_COALESCE_SET,
316 		       NLM_F_REQUEST | NLM_F_ACK);
317 	if (ret < 0)
318 		return 2;
319 	if (ethnla_fill_header(msgbuff, ETHTOOL_A_COALESCE_HEADER,
320 			       ctx->devname, 0))
321 		return -EMSGSIZE;
322 
323 	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE, NULL);
324 	if (ret < 0)
325 		return 1;
326 
327 	ret = nlsock_sendmsg(nlsk, NULL);
328 	if (ret < 0)
329 		return 1;
330 	ret = nlsock_process_reply(nlsk, nomsg_reply_cb, nlctx);
331 	if (ret == 0)
332 		return 0;
333 	else
334 		return nlctx->exit_code ?: 1;
335 }
336