xref: /linux/net/ethtool/rss.c (revision c0ef1446959101d23fdf1b1bdefc6613a83dba03)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <net/netdev_lock.h>
4 
5 #include "netlink.h"
6 #include "common.h"
7 
8 struct rss_req_info {
9 	struct ethnl_req_info		base;
10 	u32				rss_context;
11 };
12 
13 struct rss_reply_data {
14 	struct ethnl_reply_data		base;
15 	bool				no_key_fields;
16 	u32				indir_size;
17 	u32				hkey_size;
18 	u32				hfunc;
19 	u32				input_xfrm;
20 	u32				*indir_table;
21 	u8				*hkey;
22 };
23 
24 #define RSS_REQINFO(__req_base) \
25 	container_of(__req_base, struct rss_req_info, base)
26 
27 #define RSS_REPDATA(__reply_base) \
28 	container_of(__reply_base, struct rss_reply_data, base)
29 
30 const struct nla_policy ethnl_rss_get_policy[] = {
31 	[ETHTOOL_A_RSS_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
32 	[ETHTOOL_A_RSS_CONTEXT] = { .type = NLA_U32 },
33 	[ETHTOOL_A_RSS_START_CONTEXT] = { .type = NLA_U32 },
34 };
35 
36 static int
37 rss_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb,
38 		  struct netlink_ext_ack *extack)
39 {
40 	struct rss_req_info *request = RSS_REQINFO(req_info);
41 
42 	if (tb[ETHTOOL_A_RSS_CONTEXT])
43 		request->rss_context = nla_get_u32(tb[ETHTOOL_A_RSS_CONTEXT]);
44 	if (tb[ETHTOOL_A_RSS_START_CONTEXT]) {
45 		NL_SET_BAD_ATTR(extack, tb[ETHTOOL_A_RSS_START_CONTEXT]);
46 		return -EINVAL;
47 	}
48 
49 	return 0;
50 }
51 
52 static int
53 rss_prepare_get(const struct rss_req_info *request, struct net_device *dev,
54 		struct rss_reply_data *data, const struct genl_info *info)
55 {
56 	struct ethtool_rxfh_param rxfh = {};
57 	const struct ethtool_ops *ops;
58 	u32 total_size, indir_bytes;
59 	u8 *rss_config;
60 	int ret;
61 
62 	ops = dev->ethtool_ops;
63 
64 	ret = ethnl_ops_begin(dev);
65 	if (ret < 0)
66 		return ret;
67 
68 	data->indir_size = 0;
69 	data->hkey_size = 0;
70 	if (ops->get_rxfh_indir_size)
71 		data->indir_size = ops->get_rxfh_indir_size(dev);
72 	if (ops->get_rxfh_key_size)
73 		data->hkey_size = ops->get_rxfh_key_size(dev);
74 
75 	indir_bytes = data->indir_size * sizeof(u32);
76 	total_size = indir_bytes + data->hkey_size;
77 	rss_config = kzalloc(total_size, GFP_KERNEL);
78 	if (!rss_config) {
79 		ret = -ENOMEM;
80 		goto out_ops;
81 	}
82 
83 	if (data->indir_size)
84 		data->indir_table = (u32 *)rss_config;
85 	if (data->hkey_size)
86 		data->hkey = rss_config + indir_bytes;
87 
88 	rxfh.indir_size = data->indir_size;
89 	rxfh.indir = data->indir_table;
90 	rxfh.key_size = data->hkey_size;
91 	rxfh.key = data->hkey;
92 
93 	ret = ops->get_rxfh(dev, &rxfh);
94 	if (ret)
95 		goto out_ops;
96 
97 	data->hfunc = rxfh.hfunc;
98 	data->input_xfrm = rxfh.input_xfrm;
99 out_ops:
100 	ethnl_ops_complete(dev);
101 	return ret;
102 }
103 
104 static int
105 rss_prepare_ctx(const struct rss_req_info *request, struct net_device *dev,
106 		struct rss_reply_data *data, const struct genl_info *info)
107 {
108 	struct ethtool_rxfh_context *ctx;
109 	u32 total_size, indir_bytes;
110 	u8 *rss_config;
111 
112 	data->no_key_fields = !dev->ethtool_ops->rxfh_per_ctx_key;
113 
114 	ctx = xa_load(&dev->ethtool->rss_ctx, request->rss_context);
115 	if (!ctx)
116 		return -ENOENT;
117 
118 	data->indir_size = ctx->indir_size;
119 	data->hkey_size = ctx->key_size;
120 	data->hfunc = ctx->hfunc;
121 	data->input_xfrm = ctx->input_xfrm;
122 
123 	indir_bytes = data->indir_size * sizeof(u32);
124 	total_size = indir_bytes + data->hkey_size;
125 	rss_config = kzalloc(total_size, GFP_KERNEL);
126 	if (!rss_config)
127 		return -ENOMEM;
128 
129 	data->indir_table = (u32 *)rss_config;
130 	memcpy(data->indir_table, ethtool_rxfh_context_indir(ctx), indir_bytes);
131 
132 	if (data->hkey_size) {
133 		data->hkey = rss_config + indir_bytes;
134 		memcpy(data->hkey, ethtool_rxfh_context_key(ctx),
135 		       data->hkey_size);
136 	}
137 
138 	return 0;
139 }
140 
141 static int
142 rss_prepare(const struct rss_req_info *request, struct net_device *dev,
143 	    struct rss_reply_data *data, const struct genl_info *info)
144 {
145 	if (request->rss_context)
146 		return rss_prepare_ctx(request, dev, data, info);
147 	return rss_prepare_get(request, dev, data, info);
148 }
149 
150 static int
151 rss_prepare_data(const struct ethnl_req_info *req_base,
152 		 struct ethnl_reply_data *reply_base,
153 		 const struct genl_info *info)
154 {
155 	struct rss_reply_data *data = RSS_REPDATA(reply_base);
156 	struct rss_req_info *request = RSS_REQINFO(req_base);
157 	struct net_device *dev = reply_base->dev;
158 	const struct ethtool_ops *ops;
159 	int ret;
160 
161 	ops = dev->ethtool_ops;
162 	if (!ops->get_rxfh)
163 		return -EOPNOTSUPP;
164 
165 	/* Some drivers don't handle rss_context */
166 	if (request->rss_context && !ops->create_rxfh_context)
167 		return -EOPNOTSUPP;
168 
169 	mutex_lock(&dev->ethtool->rss_lock);
170 	ret = rss_prepare(request, dev, data, info);
171 	mutex_unlock(&dev->ethtool->rss_lock);
172 
173 	return ret;
174 }
175 
176 static int
177 rss_reply_size(const struct ethnl_req_info *req_base,
178 	       const struct ethnl_reply_data *reply_base)
179 {
180 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
181 	int len;
182 
183 	len = nla_total_size(sizeof(u32)) +	/* _RSS_CONTEXT */
184 	      nla_total_size(sizeof(u32)) +	/* _RSS_HFUNC */
185 	      nla_total_size(sizeof(u32)) +	/* _RSS_INPUT_XFRM */
186 	      nla_total_size(sizeof(u32) * data->indir_size) + /* _RSS_INDIR */
187 	      nla_total_size(data->hkey_size);	/* _RSS_HKEY */
188 
189 	return len;
190 }
191 
192 static int
193 rss_fill_reply(struct sk_buff *skb, const struct ethnl_req_info *req_base,
194 	       const struct ethnl_reply_data *reply_base)
195 {
196 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
197 	struct rss_req_info *request = RSS_REQINFO(req_base);
198 
199 	if (request->rss_context &&
200 	    nla_put_u32(skb, ETHTOOL_A_RSS_CONTEXT, request->rss_context))
201 		return -EMSGSIZE;
202 
203 	if ((data->indir_size &&
204 	     nla_put(skb, ETHTOOL_A_RSS_INDIR,
205 		     sizeof(u32) * data->indir_size, data->indir_table)))
206 		return -EMSGSIZE;
207 
208 	if (data->no_key_fields)
209 		return 0;
210 
211 	if ((data->hfunc &&
212 	     nla_put_u32(skb, ETHTOOL_A_RSS_HFUNC, data->hfunc)) ||
213 	    (data->input_xfrm &&
214 	     nla_put_u32(skb, ETHTOOL_A_RSS_INPUT_XFRM, data->input_xfrm)) ||
215 	    (data->hkey_size &&
216 	     nla_put(skb, ETHTOOL_A_RSS_HKEY, data->hkey_size, data->hkey)))
217 		return -EMSGSIZE;
218 
219 	return 0;
220 }
221 
222 static void rss_cleanup_data(struct ethnl_reply_data *reply_base)
223 {
224 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
225 
226 	kfree(data->indir_table);
227 }
228 
229 struct rss_nl_dump_ctx {
230 	unsigned long		ifindex;
231 	unsigned long		ctx_idx;
232 
233 	/* User wants to only dump contexts from given ifindex */
234 	unsigned int		match_ifindex;
235 	unsigned int		start_ctx;
236 };
237 
238 static struct rss_nl_dump_ctx *rss_dump_ctx(struct netlink_callback *cb)
239 {
240 	NL_ASSERT_CTX_FITS(struct rss_nl_dump_ctx);
241 
242 	return (struct rss_nl_dump_ctx *)cb->ctx;
243 }
244 
245 int ethnl_rss_dump_start(struct netlink_callback *cb)
246 {
247 	const struct genl_info *info = genl_info_dump(cb);
248 	struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
249 	struct ethnl_req_info req_info = {};
250 	struct nlattr **tb = info->attrs;
251 	int ret;
252 
253 	/* Filtering by context not supported */
254 	if (tb[ETHTOOL_A_RSS_CONTEXT]) {
255 		NL_SET_BAD_ATTR(info->extack, tb[ETHTOOL_A_RSS_CONTEXT]);
256 		return -EINVAL;
257 	}
258 	if (tb[ETHTOOL_A_RSS_START_CONTEXT]) {
259 		ctx->start_ctx = nla_get_u32(tb[ETHTOOL_A_RSS_START_CONTEXT]);
260 		ctx->ctx_idx = ctx->start_ctx;
261 	}
262 
263 	ret = ethnl_parse_header_dev_get(&req_info,
264 					 tb[ETHTOOL_A_RSS_HEADER],
265 					 sock_net(cb->skb->sk), cb->extack,
266 					 false);
267 	if (req_info.dev) {
268 		ctx->match_ifindex = req_info.dev->ifindex;
269 		ctx->ifindex = ctx->match_ifindex;
270 		ethnl_parse_header_dev_put(&req_info);
271 		req_info.dev = NULL;
272 	}
273 
274 	return ret;
275 }
276 
277 static int
278 rss_dump_one_ctx(struct sk_buff *skb, struct netlink_callback *cb,
279 		 struct net_device *dev, u32 rss_context)
280 {
281 	const struct genl_info *info = genl_info_dump(cb);
282 	struct rss_reply_data data = {};
283 	struct rss_req_info req = {};
284 	void *ehdr;
285 	int ret;
286 
287 	req.rss_context = rss_context;
288 
289 	ehdr = ethnl_dump_put(skb, cb, ETHTOOL_MSG_RSS_GET_REPLY);
290 	if (!ehdr)
291 		return -EMSGSIZE;
292 
293 	ret = ethnl_fill_reply_header(skb, dev, ETHTOOL_A_RSS_HEADER);
294 	if (ret < 0)
295 		goto err_cancel;
296 
297 	/* Context 0 is not currently storred or cached in the XArray */
298 	if (!rss_context)
299 		ret = rss_prepare_get(&req, dev, &data, info);
300 	else
301 		ret = rss_prepare_ctx(&req, dev, &data, info);
302 	if (ret)
303 		goto err_cancel;
304 
305 	ret = rss_fill_reply(skb, &req.base, &data.base);
306 	if (ret)
307 		goto err_cleanup;
308 	genlmsg_end(skb, ehdr);
309 
310 	rss_cleanup_data(&data.base);
311 	return 0;
312 
313 err_cleanup:
314 	rss_cleanup_data(&data.base);
315 err_cancel:
316 	genlmsg_cancel(skb, ehdr);
317 	return ret;
318 }
319 
320 static int
321 rss_dump_one_dev(struct sk_buff *skb, struct netlink_callback *cb,
322 		 struct net_device *dev)
323 {
324 	struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
325 	int ret;
326 
327 	if (!dev->ethtool_ops->get_rxfh)
328 		return 0;
329 
330 	if (!ctx->ctx_idx) {
331 		ret = rss_dump_one_ctx(skb, cb, dev, 0);
332 		if (ret)
333 			return ret;
334 		ctx->ctx_idx++;
335 	}
336 
337 	for (; xa_find(&dev->ethtool->rss_ctx, &ctx->ctx_idx,
338 		       ULONG_MAX, XA_PRESENT); ctx->ctx_idx++) {
339 		ret = rss_dump_one_ctx(skb, cb, dev, ctx->ctx_idx);
340 		if (ret)
341 			return ret;
342 	}
343 	ctx->ctx_idx = ctx->start_ctx;
344 
345 	return 0;
346 }
347 
348 int ethnl_rss_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
349 {
350 	struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
351 	struct net *net = sock_net(skb->sk);
352 	struct net_device *dev;
353 	int ret = 0;
354 
355 	rtnl_lock();
356 	for_each_netdev_dump(net, dev, ctx->ifindex) {
357 		if (ctx->match_ifindex && ctx->match_ifindex != ctx->ifindex)
358 			break;
359 
360 		netdev_lock_ops(dev);
361 		ret = rss_dump_one_dev(skb, cb, dev);
362 		netdev_unlock_ops(dev);
363 		if (ret)
364 			break;
365 	}
366 	rtnl_unlock();
367 
368 	return ret;
369 }
370 
371 /* RSS_NTF */
372 
373 void ethtool_rss_notify(struct net_device *dev, u32 rss_context)
374 {
375 	struct rss_req_info req_info = {
376 		.rss_context = rss_context,
377 	};
378 
379 	ethnl_notify(dev, ETHTOOL_MSG_RSS_NTF, &req_info.base);
380 }
381 
382 const struct ethnl_request_ops ethnl_rss_request_ops = {
383 	.request_cmd		= ETHTOOL_MSG_RSS_GET,
384 	.reply_cmd		= ETHTOOL_MSG_RSS_GET_REPLY,
385 	.hdr_attr		= ETHTOOL_A_RSS_HEADER,
386 	.req_info_size		= sizeof(struct rss_req_info),
387 	.reply_data_size	= sizeof(struct rss_reply_data),
388 
389 	.parse_request		= rss_parse_request,
390 	.prepare_data		= rss_prepare_data,
391 	.reply_size		= rss_reply_size,
392 	.fill_reply		= rss_fill_reply,
393 	.cleanup_data		= rss_cleanup_data,
394 };
395