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