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 data->no_key_fields = !dev->ethtool_ops->rxfh_per_ctx_key;
111
112 ctx = xa_load(&dev->ethtool->rss_ctx, request->rss_context);
113 if (!ctx)
114 return -ENOENT;
115
116 data->indir_size = ctx->indir_size;
117 data->hkey_size = ctx->key_size;
118 data->hfunc = ctx->hfunc;
119 data->input_xfrm = ctx->input_xfrm;
120
121 indir_bytes = data->indir_size * sizeof(u32);
122 total_size = indir_bytes + data->hkey_size;
123 rss_config = kzalloc(total_size, GFP_KERNEL);
124 if (!rss_config)
125 return -ENOMEM;
126
127 data->indir_table = (u32 *)rss_config;
128 memcpy(data->indir_table, ethtool_rxfh_context_indir(ctx), indir_bytes);
129
130 if (data->hkey_size) {
131 data->hkey = rss_config + indir_bytes;
132 memcpy(data->hkey, ethtool_rxfh_context_key(ctx),
133 data->hkey_size);
134 }
135
136 return 0;
137 }
138
139 static int
rss_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,const struct genl_info * info)140 rss_prepare_data(const struct ethnl_req_info *req_base,
141 struct ethnl_reply_data *reply_base,
142 const struct genl_info *info)
143 {
144 struct rss_reply_data *data = RSS_REPDATA(reply_base);
145 struct rss_req_info *request = RSS_REQINFO(req_base);
146 struct net_device *dev = reply_base->dev;
147 const struct ethtool_ops *ops;
148
149 ops = dev->ethtool_ops;
150 if (!ops->get_rxfh)
151 return -EOPNOTSUPP;
152
153 /* Some drivers don't handle rss_context */
154 if (request->rss_context) {
155 if (!ops->cap_rss_ctx_supported && !ops->create_rxfh_context)
156 return -EOPNOTSUPP;
157
158 return rss_prepare_ctx(request, dev, data, info);
159 }
160
161 return rss_prepare_get(request, dev, data, info);
162 }
163
164 static int
rss_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)165 rss_reply_size(const struct ethnl_req_info *req_base,
166 const struct ethnl_reply_data *reply_base)
167 {
168 const struct rss_reply_data *data = RSS_REPDATA(reply_base);
169 int len;
170
171 len = nla_total_size(sizeof(u32)) + /* _RSS_CONTEXT */
172 nla_total_size(sizeof(u32)) + /* _RSS_HFUNC */
173 nla_total_size(sizeof(u32)) + /* _RSS_INPUT_XFRM */
174 nla_total_size(sizeof(u32) * data->indir_size) + /* _RSS_INDIR */
175 nla_total_size(data->hkey_size); /* _RSS_HKEY */
176
177 return len;
178 }
179
180 static int
rss_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)181 rss_fill_reply(struct sk_buff *skb, const struct ethnl_req_info *req_base,
182 const struct ethnl_reply_data *reply_base)
183 {
184 const struct rss_reply_data *data = RSS_REPDATA(reply_base);
185 struct rss_req_info *request = RSS_REQINFO(req_base);
186
187 if (request->rss_context &&
188 nla_put_u32(skb, ETHTOOL_A_RSS_CONTEXT, request->rss_context))
189 return -EMSGSIZE;
190
191 if ((data->indir_size &&
192 nla_put(skb, ETHTOOL_A_RSS_INDIR,
193 sizeof(u32) * data->indir_size, data->indir_table)))
194 return -EMSGSIZE;
195
196 if (data->no_key_fields)
197 return 0;
198
199 if ((data->hfunc &&
200 nla_put_u32(skb, ETHTOOL_A_RSS_HFUNC, data->hfunc)) ||
201 (data->input_xfrm &&
202 nla_put_u32(skb, ETHTOOL_A_RSS_INPUT_XFRM, data->input_xfrm)) ||
203 (data->hkey_size &&
204 nla_put(skb, ETHTOOL_A_RSS_HKEY, data->hkey_size, data->hkey)))
205 return -EMSGSIZE;
206
207 return 0;
208 }
209
rss_cleanup_data(struct ethnl_reply_data * reply_base)210 static void rss_cleanup_data(struct ethnl_reply_data *reply_base)
211 {
212 const struct rss_reply_data *data = RSS_REPDATA(reply_base);
213
214 kfree(data->indir_table);
215 }
216
217 struct rss_nl_dump_ctx {
218 unsigned long ifindex;
219 unsigned long ctx_idx;
220
221 /* User wants to only dump contexts from given ifindex */
222 unsigned int match_ifindex;
223 unsigned int start_ctx;
224 };
225
rss_dump_ctx(struct netlink_callback * cb)226 static struct rss_nl_dump_ctx *rss_dump_ctx(struct netlink_callback *cb)
227 {
228 NL_ASSERT_CTX_FITS(struct rss_nl_dump_ctx);
229
230 return (struct rss_nl_dump_ctx *)cb->ctx;
231 }
232
ethnl_rss_dump_start(struct netlink_callback * cb)233 int ethnl_rss_dump_start(struct netlink_callback *cb)
234 {
235 const struct genl_info *info = genl_info_dump(cb);
236 struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
237 struct ethnl_req_info req_info = {};
238 struct nlattr **tb = info->attrs;
239 int ret;
240
241 /* Filtering by context not supported */
242 if (tb[ETHTOOL_A_RSS_CONTEXT]) {
243 NL_SET_BAD_ATTR(info->extack, tb[ETHTOOL_A_RSS_CONTEXT]);
244 return -EINVAL;
245 }
246 if (tb[ETHTOOL_A_RSS_START_CONTEXT]) {
247 ctx->start_ctx = nla_get_u32(tb[ETHTOOL_A_RSS_START_CONTEXT]);
248 ctx->ctx_idx = ctx->start_ctx;
249 }
250
251 ret = ethnl_parse_header_dev_get(&req_info,
252 tb[ETHTOOL_A_RSS_HEADER],
253 sock_net(cb->skb->sk), cb->extack,
254 false);
255 if (req_info.dev) {
256 ctx->match_ifindex = req_info.dev->ifindex;
257 ctx->ifindex = ctx->match_ifindex;
258 ethnl_parse_header_dev_put(&req_info);
259 req_info.dev = NULL;
260 }
261
262 return ret;
263 }
264
265 static int
rss_dump_one_ctx(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,u32 rss_context)266 rss_dump_one_ctx(struct sk_buff *skb, struct netlink_callback *cb,
267 struct net_device *dev, u32 rss_context)
268 {
269 const struct genl_info *info = genl_info_dump(cb);
270 struct rss_reply_data data = {};
271 struct rss_req_info req = {};
272 void *ehdr;
273 int ret;
274
275 req.rss_context = rss_context;
276
277 ehdr = ethnl_dump_put(skb, cb, ETHTOOL_MSG_RSS_GET_REPLY);
278 if (!ehdr)
279 return -EMSGSIZE;
280
281 ret = ethnl_fill_reply_header(skb, dev, ETHTOOL_A_RSS_HEADER);
282 if (ret < 0)
283 goto err_cancel;
284
285 /* Context 0 is not currently storred or cached in the XArray */
286 if (!rss_context)
287 ret = rss_prepare_get(&req, dev, &data, info);
288 else
289 ret = rss_prepare_ctx(&req, dev, &data, info);
290 if (ret)
291 goto err_cancel;
292
293 ret = rss_fill_reply(skb, &req.base, &data.base);
294 if (ret)
295 goto err_cleanup;
296 genlmsg_end(skb, ehdr);
297
298 rss_cleanup_data(&data.base);
299 return 0;
300
301 err_cleanup:
302 rss_cleanup_data(&data.base);
303 err_cancel:
304 genlmsg_cancel(skb, ehdr);
305 return ret;
306 }
307
308 static int
rss_dump_one_dev(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev)309 rss_dump_one_dev(struct sk_buff *skb, struct netlink_callback *cb,
310 struct net_device *dev)
311 {
312 struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
313 int ret;
314
315 if (!dev->ethtool_ops->get_rxfh)
316 return 0;
317
318 if (!ctx->ctx_idx) {
319 ret = rss_dump_one_ctx(skb, cb, dev, 0);
320 if (ret)
321 return ret;
322 ctx->ctx_idx++;
323 }
324
325 for (; xa_find(&dev->ethtool->rss_ctx, &ctx->ctx_idx,
326 ULONG_MAX, XA_PRESENT); ctx->ctx_idx++) {
327 ret = rss_dump_one_ctx(skb, cb, dev, ctx->ctx_idx);
328 if (ret)
329 return ret;
330 }
331 ctx->ctx_idx = ctx->start_ctx;
332
333 return 0;
334 }
335
ethnl_rss_dumpit(struct sk_buff * skb,struct netlink_callback * cb)336 int ethnl_rss_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
337 {
338 struct rss_nl_dump_ctx *ctx = rss_dump_ctx(cb);
339 struct net *net = sock_net(skb->sk);
340 struct net_device *dev;
341 int ret = 0;
342
343 rtnl_lock();
344 for_each_netdev_dump(net, dev, ctx->ifindex) {
345 if (ctx->match_ifindex && ctx->match_ifindex != ctx->ifindex)
346 break;
347
348 ret = rss_dump_one_dev(skb, cb, dev);
349 if (ret)
350 break;
351 }
352 rtnl_unlock();
353
354 return ret;
355 }
356
357 const struct ethnl_request_ops ethnl_rss_request_ops = {
358 .request_cmd = ETHTOOL_MSG_RSS_GET,
359 .reply_cmd = ETHTOOL_MSG_RSS_GET_REPLY,
360 .hdr_attr = ETHTOOL_A_RSS_HEADER,
361 .req_info_size = sizeof(struct rss_req_info),
362 .reply_data_size = sizeof(struct rss_reply_data),
363
364 .parse_request = rss_parse_request,
365 .prepare_data = rss_prepare_data,
366 .reply_size = rss_reply_size,
367 .fill_reply = rss_fill_reply,
368 .cleanup_data = rss_cleanup_data,
369 };
370