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