xref: /aosp_15_r20/external/ethtool/netlink/rss.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /*
2  * rss.c - netlink implementation of RSS context commands
3  *
4  * Implementation of "ethtool -x <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 "strset.h"
15 #include "parser.h"
16 
17 struct cb_args {
18 	struct nl_context	*nlctx;
19 	u32			num_rings;
20 };
21 
dump_json_rss_info(struct cmd_context * ctx,u32 * indir_table,u32 indir_size,u8 * hkey,u32 hkey_size,const struct stringset * hash_funcs,u8 hfunc)22 void dump_json_rss_info(struct cmd_context *ctx, u32 *indir_table,
23 			u32 indir_size, u8 *hkey, u32 hkey_size,
24 			const struct stringset *hash_funcs, u8 hfunc)
25 {
26 	unsigned int i;
27 
28 	open_json_object(NULL);
29 	print_string(PRINT_JSON, "ifname", NULL, ctx->devname);
30 	if (indir_size) {
31 		open_json_array("rss-indirection-table", NULL);
32 		for (i = 0; i < indir_size; i++)
33 			print_uint(PRINT_JSON, NULL, NULL, indir_table[i]);
34 		close_json_array("\n");
35 	}
36 
37 	if (hkey_size) {
38 		open_json_array("rss-hash-key", NULL);
39 		for (i = 0; i < hkey_size; i++)
40 			print_uint(PRINT_JSON, NULL, NULL, (u8)hkey[i]);
41 		close_json_array("\n");
42 	}
43 
44 	if (hfunc) {
45 		for (i = 0; i < get_count(hash_funcs); i++) {
46 			if (hfunc & (1 << i)) {
47 				print_string(PRINT_JSON, "rss-hash-function",
48 					     NULL, get_string(hash_funcs, i));
49 				break;
50 			}
51 		}
52 	}
53 
54 	close_json_object();
55 }
56 
get_channels_cb(const struct nlmsghdr * nlhdr,void * data)57 int get_channels_cb(const struct nlmsghdr *nlhdr, void *data)
58 {
59 	const struct nlattr *tb[ETHTOOL_A_CHANNELS_MAX + 1] = {};
60 	DECLARE_ATTR_TB_INFO(tb);
61 	struct cb_args *args = data;
62 	struct nl_context *nlctx = args->nlctx;
63 	bool silent;
64 	int err_ret;
65 	int ret;
66 
67 	silent = nlctx->is_dump || nlctx->is_monitor;
68 	err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
69 	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
70 	if (ret < 0)
71 		return err_ret;
72 	nlctx->devname = get_dev_name(tb[ETHTOOL_A_CHANNELS_HEADER]);
73 	if (!dev_ok(nlctx))
74 		return err_ret;
75 	if (tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT])
76 		args->num_rings = mnl_attr_get_u32(tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT]);
77 	if (tb[ETHTOOL_A_CHANNELS_RX_COUNT])
78 		args->num_rings += mnl_attr_get_u32(tb[ETHTOOL_A_CHANNELS_RX_COUNT]);
79 	return MNL_CB_OK;
80 }
81 
rss_reply_cb(const struct nlmsghdr * nlhdr,void * data)82 int rss_reply_cb(const struct nlmsghdr *nlhdr, void *data)
83 {
84 	const struct nlattr *tb[ETHTOOL_A_RSS_MAX + 1] = {};
85 	unsigned int indir_bytes = 0, hkey_bytes = 0;
86 	DECLARE_ATTR_TB_INFO(tb);
87 	struct cb_args *args = data;
88 	struct nl_context *nlctx = args->nlctx;
89 	const struct stringset *hash_funcs;
90 	u32 rss_hfunc = 0, indir_size;
91 	u32 *indir_table = NULL;
92 	u8 *hkey = NULL;
93 	bool silent;
94 	int err_ret;
95 	int ret;
96 
97 	silent = nlctx->is_dump || nlctx->is_monitor;
98 	err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
99 	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
100 	if (ret < 0)
101 		return err_ret;
102 	nlctx->devname = get_dev_name(tb[ETHTOOL_A_RSS_HEADER]);
103 	if (!dev_ok(nlctx))
104 		return err_ret;
105 
106 	show_cr();
107 
108 	if (tb[ETHTOOL_A_RSS_HFUNC])
109 		rss_hfunc = mnl_attr_get_u32(tb[ETHTOOL_A_RSS_HFUNC]);
110 
111 	if (tb[ETHTOOL_A_RSS_INDIR]) {
112 		indir_bytes = mnl_attr_get_payload_len(tb[ETHTOOL_A_RSS_INDIR]);
113 		indir_table = mnl_attr_get_payload(tb[ETHTOOL_A_RSS_INDIR]);
114 	}
115 
116 	if (tb[ETHTOOL_A_RSS_HKEY]) {
117 		hkey_bytes = mnl_attr_get_payload_len(tb[ETHTOOL_A_RSS_HKEY]);
118 		hkey = mnl_attr_get_payload(tb[ETHTOOL_A_RSS_HKEY]);
119 	}
120 
121 	/* Fetch RSS hash functions and their status and print */
122 	if (!nlctx->is_monitor) {
123 		ret = netlink_init_ethnl2_socket(nlctx);
124 		if (ret < 0)
125 			return MNL_CB_ERROR;
126 	}
127 	hash_funcs = global_stringset(ETH_SS_RSS_HASH_FUNCS,
128 				      nlctx->ethnl2_socket);
129 
130 	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
131 	if (ret < 0)
132 		return silent ? MNL_CB_OK : MNL_CB_ERROR;
133 
134 	nlctx->devname = get_dev_name(tb[ETHTOOL_A_RSS_HEADER]);
135 	if (!dev_ok(nlctx))
136 		return MNL_CB_OK;
137 
138 	/* Fetch ring count info into args->num_rings */
139 	ret = nlsock_prep_get_request(nlctx->ethnl2_socket,
140 				      ETHTOOL_MSG_CHANNELS_GET,
141 				      ETHTOOL_A_CHANNELS_HEADER, 0);
142 	if (ret < 0)
143 		return MNL_CB_ERROR;
144 
145 	ret = nlsock_sendmsg(nlctx->ethnl2_socket, NULL);
146 	if (ret < 0)
147 		return MNL_CB_ERROR;
148 
149 	ret = nlsock_process_reply(nlctx->ethnl2_socket, get_channels_cb, args);
150 	if (ret < 0)
151 		return MNL_CB_ERROR;
152 
153 	indir_size = indir_bytes / sizeof(u32);
154 	if (is_json_context()) {
155 		dump_json_rss_info(nlctx->ctx, (u32 *)indir_table, indir_size,
156 				   hkey, hkey_bytes, hash_funcs, rss_hfunc);
157 	} else {
158 		print_indir_table(nlctx->ctx, args->num_rings,
159 				  indir_size, (u32 *)indir_table);
160 		print_rss_hkey(hkey, hkey_bytes);
161 		printf("RSS hash function:\n");
162 		if (!rss_hfunc) {
163 			printf("    Operation not supported\n");
164 			return 0;
165 		}
166 		for (unsigned int i = 0; i < get_count(hash_funcs); i++) {
167 			printf("    %s: %s\n", get_string(hash_funcs, i),
168 			       (rss_hfunc & (1 << i)) ? "on" : "off");
169 		}
170 	}
171 
172 	return MNL_CB_OK;
173 }
174 
175 /* RSS_GET */
176 static const struct param_parser grss_params[] = {
177 	{
178 		.arg		= "context",
179 		.type		= ETHTOOL_A_RSS_CONTEXT,
180 		.handler	= nl_parse_direct_u32,
181 		.min_argc	= 1,
182 	},
183 	{}
184 };
185 
nl_grss(struct cmd_context * ctx)186 int nl_grss(struct cmd_context *ctx)
187 {
188 	struct nl_context *nlctx = ctx->nlctx;
189 	struct nl_socket *nlsk = nlctx->ethnl_socket;
190 	struct nl_msg_buff *msgbuff;
191 	struct cb_args args = {};
192 	int ret;
193 
194 	nlctx->cmd = "-x";
195 	nlctx->argp = ctx->argp;
196 	nlctx->argc = ctx->argc;
197 	nlctx->devname = ctx->devname;
198 	nlsk = nlctx->ethnl_socket;
199 	msgbuff = &nlsk->msgbuff;
200 
201 	if (netlink_cmd_check(ctx, ETHTOOL_MSG_RSS_GET, true))
202 		return -EOPNOTSUPP;
203 
204 	ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_RSS_GET,
205 		       NLM_F_REQUEST | NLM_F_ACK);
206 	if (ret < 0)
207 		return 1;
208 
209 	if (ethnla_fill_header(msgbuff, ETHTOOL_A_RSS_HEADER,
210 			       ctx->devname, 0))
211 		return -EMSGSIZE;
212 
213 	ret = nl_parser(nlctx, grss_params, NULL, PARSER_GROUP_NONE, NULL);
214 	if (ret < 0)
215 		goto err;
216 
217 	ret = nlsock_sendmsg(nlsk, NULL);
218 	if (ret < 0)
219 		goto err;
220 
221 	args.nlctx = nlctx;
222 	new_json_obj(ctx->json);
223 	ret = nlsock_process_reply(nlsk, rss_reply_cb, &args);
224 	delete_json_obj();
225 
226 	if (ret == 0)
227 		return 0;
228 err:
229 	return nlctx->exit_code ?: 1;
230 }
231