1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/ethtool.h>
12 const char (*strings)[ETH_GSTRING_LEN];
15 static const struct strset_info info_template[] = {
22 [ETH_SS_PRIV_FLAGS] = {
27 .count = ARRAY_SIZE(netdev_features_strings),
28 .strings = netdev_features_strings,
30 [ETH_SS_RSS_HASH_FUNCS] = {
32 .count = ARRAY_SIZE(rss_hash_func_strings),
33 .strings = rss_hash_func_strings,
37 .count = ARRAY_SIZE(tunable_strings),
38 .strings = tunable_strings,
40 [ETH_SS_PHY_STATS] = {
43 [ETH_SS_PHY_TUNABLES] = {
45 .count = ARRAY_SIZE(phy_tunable_strings),
46 .strings = phy_tunable_strings,
48 [ETH_SS_LINK_MODES] = {
50 .count = __ETHTOOL_LINK_MODE_MASK_NBITS,
51 .strings = link_mode_names,
55 struct strset_req_info {
56 struct ethnl_req_info base;
61 #define STRSET_REQINFO(__req_base) \
62 container_of(__req_base, struct strset_req_info, base)
64 struct strset_reply_data {
65 struct ethnl_reply_data base;
66 struct strset_info sets[ETH_SS_COUNT];
69 #define STRSET_REPDATA(__reply_base) \
70 container_of(__reply_base, struct strset_reply_data, base)
72 static const struct nla_policy strset_get_policy[ETHTOOL_A_STRSET_MAX + 1] = {
73 [ETHTOOL_A_STRSET_UNSPEC] = { .type = NLA_REJECT },
74 [ETHTOOL_A_STRSET_HEADER] = { .type = NLA_NESTED },
75 [ETHTOOL_A_STRSET_STRINGSETS] = { .type = NLA_NESTED },
78 static const struct nla_policy
79 get_stringset_policy[ETHTOOL_A_STRINGSET_MAX + 1] = {
80 [ETHTOOL_A_STRINGSET_UNSPEC] = { .type = NLA_REJECT },
81 [ETHTOOL_A_STRINGSET_ID] = { .type = NLA_U32 },
82 [ETHTOOL_A_STRINGSET_COUNT] = { .type = NLA_REJECT },
83 [ETHTOOL_A_STRINGSET_STRINGS] = { .type = NLA_REJECT },
87 * strset_include() - test if a string set should be included in reply
88 * @data: pointer to request data structure
89 * @id: id of string set to check (ETH_SS_* constants)
91 static bool strset_include(const struct strset_req_info *info,
92 const struct strset_reply_data *data, u32 id)
96 BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids));
99 return info->req_ids & (1U << id);
100 per_dev = data->sets[id].per_dev;
101 if (!per_dev && !data->sets[id].strings)
104 return data->base.dev ? per_dev : !per_dev;
107 static int strset_get_id(const struct nlattr *nest, u32 *val,
108 struct netlink_ext_ack *extack)
110 struct nlattr *tb[ETHTOOL_A_STRINGSET_MAX + 1];
113 ret = nla_parse_nested(tb, ETHTOOL_A_STRINGSET_MAX, nest,
114 get_stringset_policy, extack);
117 if (!tb[ETHTOOL_A_STRINGSET_ID])
120 *val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]);
124 static const struct nla_policy
125 strset_stringsets_policy[ETHTOOL_A_STRINGSETS_MAX + 1] = {
126 [ETHTOOL_A_STRINGSETS_UNSPEC] = { .type = NLA_REJECT },
127 [ETHTOOL_A_STRINGSETS_STRINGSET] = { .type = NLA_NESTED },
130 static int strset_parse_request(struct ethnl_req_info *req_base,
132 struct netlink_ext_ack *extack)
134 struct strset_req_info *req_info = STRSET_REQINFO(req_base);
135 struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS];
141 ret = nla_validate_nested(nest, ETHTOOL_A_STRINGSETS_MAX,
142 strset_stringsets_policy, extack);
146 req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY];
147 nla_for_each_nested(attr, nest, rem) {
150 if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET,
151 "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n",
155 ret = strset_get_id(attr, &id, extack);
158 if (ret >= ETH_SS_COUNT) {
159 NL_SET_ERR_MSG_ATTR(extack, attr,
160 "unknown string set id");
164 req_info->req_ids |= (1U << id);
170 static void strset_cleanup_data(struct ethnl_reply_data *reply_base)
172 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
175 for (i = 0; i < ETH_SS_COUNT; i++)
176 if (data->sets[i].free_strings) {
177 kfree(data->sets[i].strings);
178 data->sets[i].strings = NULL;
179 data->sets[i].free_strings = false;
183 static int strset_prepare_set(struct strset_info *info, struct net_device *dev,
184 unsigned int id, bool counts_only)
186 const struct ethtool_ops *ops = dev->ethtool_ops;
190 if (id == ETH_SS_PHY_STATS && dev->phydev &&
191 !ops->get_ethtool_phy_stats)
192 ret = phy_ethtool_get_sset_count(dev->phydev);
193 else if (ops->get_sset_count && ops->get_strings)
194 ret = ops->get_sset_count(dev, id);
204 strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL);
207 if (id == ETH_SS_PHY_STATS && dev->phydev &&
208 !ops->get_ethtool_phy_stats)
209 phy_ethtool_get_strings(dev->phydev, strings);
211 ops->get_strings(dev, id, strings);
212 info->strings = strings;
213 info->free_strings = true;
220 static int strset_prepare_data(const struct ethnl_req_info *req_base,
221 struct ethnl_reply_data *reply_base,
222 struct genl_info *info)
224 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
225 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
226 struct net_device *dev = reply_base->dev;
230 BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT);
231 memcpy(&data->sets, &info_template, sizeof(data->sets));
234 for (i = 0; i < ETH_SS_COUNT; i++) {
235 if ((req_info->req_ids & (1U << i)) &&
236 data->sets[i].per_dev) {
238 GENL_SET_ERR_MSG(info, "requested per device strings without dev");
244 ret = ethnl_ops_begin(dev);
247 for (i = 0; i < ETH_SS_COUNT; i++) {
248 if (!strset_include(req_info, data, i) ||
249 !data->sets[i].per_dev)
252 ret = strset_prepare_set(&data->sets[i], dev, i,
253 req_info->counts_only);
257 ethnl_ops_complete(dev);
261 ethnl_ops_complete(dev);
263 strset_cleanup_data(reply_base);
267 /* calculate size of ETHTOOL_A_STRSET_STRINGSET nest for one string set */
268 static int strset_set_size(const struct strset_info *info, bool counts_only)
270 unsigned int len = 0;
273 if (info->count == 0)
276 return nla_total_size(2 * nla_total_size(sizeof(u32)));
278 for (i = 0; i < info->count; i++) {
279 const char *str = info->strings[i];
281 /* ETHTOOL_A_STRING_INDEX, ETHTOOL_A_STRING_VALUE, nest */
282 len += nla_total_size(nla_total_size(sizeof(u32)) +
283 ethnl_strz_size(str));
285 /* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */
286 len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len);
288 return nla_total_size(len);
291 static int strset_reply_size(const struct ethnl_req_info *req_base,
292 const struct ethnl_reply_data *reply_base)
294 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
295 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
300 len += ethnl_reply_header_size();
301 for (i = 0; i < ETH_SS_COUNT; i++) {
302 const struct strset_info *set_info = &data->sets[i];
304 if (!strset_include(req_info, data, i))
307 ret = strset_set_size(set_info, req_info->counts_only);
316 /* fill one string into reply */
317 static int strset_fill_string(struct sk_buff *skb,
318 const struct strset_info *set_info, u32 idx)
320 struct nlattr *string_attr;
323 value = set_info->strings[idx];
325 string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING);
328 if (nla_put_u32(skb, ETHTOOL_A_STRING_INDEX, idx) ||
329 ethnl_put_strz(skb, ETHTOOL_A_STRING_VALUE, value))
330 goto nla_put_failure;
331 nla_nest_end(skb, string_attr);
335 nla_nest_cancel(skb, string_attr);
339 /* fill one string set into reply */
340 static int strset_fill_set(struct sk_buff *skb,
341 const struct strset_info *set_info, u32 id,
344 struct nlattr *stringset_attr;
345 struct nlattr *strings_attr;
348 if (!set_info->per_dev && !set_info->strings)
350 if (set_info->count == 0)
352 stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET);
356 if (nla_put_u32(skb, ETHTOOL_A_STRINGSET_ID, id) ||
357 nla_put_u32(skb, ETHTOOL_A_STRINGSET_COUNT, set_info->count))
358 goto nla_put_failure;
361 strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS);
363 goto nla_put_failure;
364 for (i = 0; i < set_info->count; i++) {
365 if (strset_fill_string(skb, set_info, i) < 0)
366 goto nla_put_failure;
368 nla_nest_end(skb, strings_attr);
371 nla_nest_end(skb, stringset_attr);
375 nla_nest_cancel(skb, stringset_attr);
379 static int strset_fill_reply(struct sk_buff *skb,
380 const struct ethnl_req_info *req_base,
381 const struct ethnl_reply_data *reply_base)
383 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
384 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
389 nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS);
393 for (i = 0; i < ETH_SS_COUNT; i++) {
394 if (strset_include(req_info, data, i)) {
395 ret = strset_fill_set(skb, &data->sets[i], i,
396 req_info->counts_only);
398 goto nla_put_failure;
402 nla_nest_end(skb, nest);
406 nla_nest_cancel(skb, nest);
410 const struct ethnl_request_ops ethnl_strset_request_ops = {
411 .request_cmd = ETHTOOL_MSG_STRSET_GET,
412 .reply_cmd = ETHTOOL_MSG_STRSET_GET_REPLY,
413 .hdr_attr = ETHTOOL_A_STRSET_HEADER,
414 .max_attr = ETHTOOL_A_STRSET_MAX,
415 .req_info_size = sizeof(struct strset_req_info),
416 .reply_data_size = sizeof(struct strset_reply_data),
417 .request_policy = strset_get_policy,
418 .allow_nodev_do = true,
420 .parse_request = strset_parse_request,
421 .prepare_data = strset_prepare_data,
422 .reply_size = strset_reply_size,
423 .fill_reply = strset_fill_reply,
424 .cleanup_data = strset_cleanup_data,