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,
53 [ETH_SS_MSG_CLASSES] = {
55 .count = NETIF_MSG_CLASS_COUNT,
56 .strings = netif_msg_class_names,
58 [ETH_SS_WOL_MODES] = {
60 .count = WOL_MODE_COUNT,
61 .strings = wol_mode_names,
65 struct strset_req_info {
66 struct ethnl_req_info base;
71 #define STRSET_REQINFO(__req_base) \
72 container_of(__req_base, struct strset_req_info, base)
74 struct strset_reply_data {
75 struct ethnl_reply_data base;
76 struct strset_info sets[ETH_SS_COUNT];
79 #define STRSET_REPDATA(__reply_base) \
80 container_of(__reply_base, struct strset_reply_data, base)
82 static const struct nla_policy strset_get_policy[ETHTOOL_A_STRSET_MAX + 1] = {
83 [ETHTOOL_A_STRSET_UNSPEC] = { .type = NLA_REJECT },
84 [ETHTOOL_A_STRSET_HEADER] = { .type = NLA_NESTED },
85 [ETHTOOL_A_STRSET_STRINGSETS] = { .type = NLA_NESTED },
88 static const struct nla_policy
89 get_stringset_policy[ETHTOOL_A_STRINGSET_MAX + 1] = {
90 [ETHTOOL_A_STRINGSET_UNSPEC] = { .type = NLA_REJECT },
91 [ETHTOOL_A_STRINGSET_ID] = { .type = NLA_U32 },
92 [ETHTOOL_A_STRINGSET_COUNT] = { .type = NLA_REJECT },
93 [ETHTOOL_A_STRINGSET_STRINGS] = { .type = NLA_REJECT },
97 * strset_include() - test if a string set should be included in reply
98 * @info: parsed client request
99 * @data: pointer to request data structure
100 * @id: id of string set to check (ETH_SS_* constants)
102 static bool strset_include(const struct strset_req_info *info,
103 const struct strset_reply_data *data, u32 id)
107 BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids));
110 return info->req_ids & (1U << id);
111 per_dev = data->sets[id].per_dev;
112 if (!per_dev && !data->sets[id].strings)
115 return data->base.dev ? per_dev : !per_dev;
118 static int strset_get_id(const struct nlattr *nest, u32 *val,
119 struct netlink_ext_ack *extack)
121 struct nlattr *tb[ETHTOOL_A_STRINGSET_MAX + 1];
124 ret = nla_parse_nested(tb, ETHTOOL_A_STRINGSET_MAX, nest,
125 get_stringset_policy, extack);
128 if (!tb[ETHTOOL_A_STRINGSET_ID])
131 *val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]);
135 static const struct nla_policy
136 strset_stringsets_policy[ETHTOOL_A_STRINGSETS_MAX + 1] = {
137 [ETHTOOL_A_STRINGSETS_UNSPEC] = { .type = NLA_REJECT },
138 [ETHTOOL_A_STRINGSETS_STRINGSET] = { .type = NLA_NESTED },
141 static int strset_parse_request(struct ethnl_req_info *req_base,
143 struct netlink_ext_ack *extack)
145 struct strset_req_info *req_info = STRSET_REQINFO(req_base);
146 struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS];
152 ret = nla_validate_nested(nest, ETHTOOL_A_STRINGSETS_MAX,
153 strset_stringsets_policy, extack);
157 req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY];
158 nla_for_each_nested(attr, nest, rem) {
161 if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET,
162 "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n",
166 ret = strset_get_id(attr, &id, extack);
169 if (ret >= ETH_SS_COUNT) {
170 NL_SET_ERR_MSG_ATTR(extack, attr,
171 "unknown string set id");
175 req_info->req_ids |= (1U << id);
181 static void strset_cleanup_data(struct ethnl_reply_data *reply_base)
183 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
186 for (i = 0; i < ETH_SS_COUNT; i++)
187 if (data->sets[i].free_strings) {
188 kfree(data->sets[i].strings);
189 data->sets[i].strings = NULL;
190 data->sets[i].free_strings = false;
194 static int strset_prepare_set(struct strset_info *info, struct net_device *dev,
195 unsigned int id, bool counts_only)
197 const struct ethtool_ops *ops = dev->ethtool_ops;
201 if (id == ETH_SS_PHY_STATS && dev->phydev &&
202 !ops->get_ethtool_phy_stats)
203 ret = phy_ethtool_get_sset_count(dev->phydev);
204 else if (ops->get_sset_count && ops->get_strings)
205 ret = ops->get_sset_count(dev, id);
215 strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL);
218 if (id == ETH_SS_PHY_STATS && dev->phydev &&
219 !ops->get_ethtool_phy_stats)
220 phy_ethtool_get_strings(dev->phydev, strings);
222 ops->get_strings(dev, id, strings);
223 info->strings = strings;
224 info->free_strings = true;
231 static int strset_prepare_data(const struct ethnl_req_info *req_base,
232 struct ethnl_reply_data *reply_base,
233 struct genl_info *info)
235 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
236 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
237 struct net_device *dev = reply_base->dev;
241 BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT);
242 memcpy(&data->sets, &info_template, sizeof(data->sets));
245 for (i = 0; i < ETH_SS_COUNT; i++) {
246 if ((req_info->req_ids & (1U << i)) &&
247 data->sets[i].per_dev) {
249 GENL_SET_ERR_MSG(info, "requested per device strings without dev");
256 ret = ethnl_ops_begin(dev);
259 for (i = 0; i < ETH_SS_COUNT; i++) {
260 if (!strset_include(req_info, data, i) ||
261 !data->sets[i].per_dev)
264 ret = strset_prepare_set(&data->sets[i], dev, i,
265 req_info->counts_only);
269 ethnl_ops_complete(dev);
273 ethnl_ops_complete(dev);
275 strset_cleanup_data(reply_base);
279 /* calculate size of ETHTOOL_A_STRSET_STRINGSET nest for one string set */
280 static int strset_set_size(const struct strset_info *info, bool counts_only)
282 unsigned int len = 0;
285 if (info->count == 0)
288 return nla_total_size(2 * nla_total_size(sizeof(u32)));
290 for (i = 0; i < info->count; i++) {
291 const char *str = info->strings[i];
293 /* ETHTOOL_A_STRING_INDEX, ETHTOOL_A_STRING_VALUE, nest */
294 len += nla_total_size(nla_total_size(sizeof(u32)) +
295 ethnl_strz_size(str));
297 /* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */
298 len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len);
300 return nla_total_size(len);
303 static int strset_reply_size(const struct ethnl_req_info *req_base,
304 const struct ethnl_reply_data *reply_base)
306 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
307 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
312 len += ethnl_reply_header_size();
313 for (i = 0; i < ETH_SS_COUNT; i++) {
314 const struct strset_info *set_info = &data->sets[i];
316 if (!strset_include(req_info, data, i))
319 ret = strset_set_size(set_info, req_info->counts_only);
328 /* fill one string into reply */
329 static int strset_fill_string(struct sk_buff *skb,
330 const struct strset_info *set_info, u32 idx)
332 struct nlattr *string_attr;
335 value = set_info->strings[idx];
337 string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING);
340 if (nla_put_u32(skb, ETHTOOL_A_STRING_INDEX, idx) ||
341 ethnl_put_strz(skb, ETHTOOL_A_STRING_VALUE, value))
342 goto nla_put_failure;
343 nla_nest_end(skb, string_attr);
347 nla_nest_cancel(skb, string_attr);
351 /* fill one string set into reply */
352 static int strset_fill_set(struct sk_buff *skb,
353 const struct strset_info *set_info, u32 id,
356 struct nlattr *stringset_attr;
357 struct nlattr *strings_attr;
360 if (!set_info->per_dev && !set_info->strings)
362 if (set_info->count == 0)
364 stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET);
368 if (nla_put_u32(skb, ETHTOOL_A_STRINGSET_ID, id) ||
369 nla_put_u32(skb, ETHTOOL_A_STRINGSET_COUNT, set_info->count))
370 goto nla_put_failure;
373 strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS);
375 goto nla_put_failure;
376 for (i = 0; i < set_info->count; i++) {
377 if (strset_fill_string(skb, set_info, i) < 0)
378 goto nla_put_failure;
380 nla_nest_end(skb, strings_attr);
383 nla_nest_end(skb, stringset_attr);
387 nla_nest_cancel(skb, stringset_attr);
391 static int strset_fill_reply(struct sk_buff *skb,
392 const struct ethnl_req_info *req_base,
393 const struct ethnl_reply_data *reply_base)
395 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
396 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
401 nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS);
405 for (i = 0; i < ETH_SS_COUNT; i++) {
406 if (strset_include(req_info, data, i)) {
407 ret = strset_fill_set(skb, &data->sets[i], i,
408 req_info->counts_only);
410 goto nla_put_failure;
414 nla_nest_end(skb, nest);
418 nla_nest_cancel(skb, nest);
422 const struct ethnl_request_ops ethnl_strset_request_ops = {
423 .request_cmd = ETHTOOL_MSG_STRSET_GET,
424 .reply_cmd = ETHTOOL_MSG_STRSET_GET_REPLY,
425 .hdr_attr = ETHTOOL_A_STRSET_HEADER,
426 .max_attr = ETHTOOL_A_STRSET_MAX,
427 .req_info_size = sizeof(struct strset_req_info),
428 .reply_data_size = sizeof(struct strset_reply_data),
429 .request_policy = strset_get_policy,
430 .allow_nodev_do = true,
432 .parse_request = strset_parse_request,
433 .prepare_data = strset_prepare_data,
434 .reply_size = strset_reply_size,
435 .fill_reply = strset_fill_reply,
436 .cleanup_data = strset_cleanup_data,