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,
63 [ETH_SS_SOF_TIMESTAMPING] = {
65 .count = __SOF_TIMESTAMPING_CNT,
66 .strings = sof_timestamping_names,
68 [ETH_SS_TS_TX_TYPES] = {
70 .count = __HWTSTAMP_TX_CNT,
71 .strings = ts_tx_type_names,
73 [ETH_SS_TS_RX_FILTERS] = {
75 .count = __HWTSTAMP_FILTER_CNT,
76 .strings = ts_rx_filter_names,
78 [ETH_SS_UDP_TUNNEL_TYPES] = {
80 .count = __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
81 .strings = udp_tunnel_type_names,
83 [ETH_SS_STATS_STD] = {
85 .count = __ETHTOOL_STATS_CNT,
86 .strings = stats_std_names,
88 [ETH_SS_STATS_ETH_PHY] = {
90 .count = __ETHTOOL_A_STATS_ETH_PHY_CNT,
91 .strings = stats_eth_phy_names,
93 [ETH_SS_STATS_ETH_MAC] = {
95 .count = __ETHTOOL_A_STATS_ETH_MAC_CNT,
96 .strings = stats_eth_mac_names,
98 [ETH_SS_STATS_ETH_CTRL] = {
100 .count = __ETHTOOL_A_STATS_ETH_CTRL_CNT,
101 .strings = stats_eth_ctrl_names,
103 [ETH_SS_STATS_RMON] = {
105 .count = __ETHTOOL_A_STATS_RMON_CNT,
106 .strings = stats_rmon_names,
110 struct strset_req_info {
111 struct ethnl_req_info base;
116 #define STRSET_REQINFO(__req_base) \
117 container_of(__req_base, struct strset_req_info, base)
119 struct strset_reply_data {
120 struct ethnl_reply_data base;
121 struct strset_info sets[ETH_SS_COUNT];
124 #define STRSET_REPDATA(__reply_base) \
125 container_of(__reply_base, struct strset_reply_data, base)
127 const struct nla_policy ethnl_strset_get_policy[] = {
128 [ETHTOOL_A_STRSET_HEADER] =
129 NLA_POLICY_NESTED(ethnl_header_policy),
130 [ETHTOOL_A_STRSET_STRINGSETS] = { .type = NLA_NESTED },
131 [ETHTOOL_A_STRSET_COUNTS_ONLY] = { .type = NLA_FLAG },
134 static const struct nla_policy get_stringset_policy[] = {
135 [ETHTOOL_A_STRINGSET_ID] = { .type = NLA_U32 },
139 * strset_include() - test if a string set should be included in reply
140 * @info: parsed client request
141 * @data: pointer to request data structure
142 * @id: id of string set to check (ETH_SS_* constants)
144 static bool strset_include(const struct strset_req_info *info,
145 const struct strset_reply_data *data, u32 id)
149 BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids));
152 return info->req_ids & (1U << id);
153 per_dev = data->sets[id].per_dev;
154 if (!per_dev && !data->sets[id].strings)
157 return data->base.dev ? per_dev : !per_dev;
160 static int strset_get_id(const struct nlattr *nest, u32 *val,
161 struct netlink_ext_ack *extack)
163 struct nlattr *tb[ARRAY_SIZE(get_stringset_policy)];
166 ret = nla_parse_nested(tb, ARRAY_SIZE(get_stringset_policy) - 1, nest,
167 get_stringset_policy, extack);
170 if (NL_REQ_ATTR_CHECK(extack, nest, tb, ETHTOOL_A_STRINGSET_ID))
173 *val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]);
177 static const struct nla_policy strset_stringsets_policy[] = {
178 [ETHTOOL_A_STRINGSETS_STRINGSET] = { .type = NLA_NESTED },
181 static int strset_parse_request(struct ethnl_req_info *req_base,
183 struct netlink_ext_ack *extack)
185 struct strset_req_info *req_info = STRSET_REQINFO(req_base);
186 struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS];
192 ret = nla_validate_nested(nest,
193 ARRAY_SIZE(strset_stringsets_policy) - 1,
194 strset_stringsets_policy, extack);
198 req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY];
199 nla_for_each_nested(attr, nest, rem) {
202 if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET,
203 "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n",
207 ret = strset_get_id(attr, &id, extack);
210 if (id >= ETH_SS_COUNT) {
211 NL_SET_ERR_MSG_ATTR(extack, attr,
212 "unknown string set id");
216 req_info->req_ids |= (1U << id);
222 static void strset_cleanup_data(struct ethnl_reply_data *reply_base)
224 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
227 for (i = 0; i < ETH_SS_COUNT; i++)
228 if (data->sets[i].free_strings) {
229 kfree(data->sets[i].strings);
230 data->sets[i].strings = NULL;
231 data->sets[i].free_strings = false;
235 static int strset_prepare_set(struct strset_info *info, struct net_device *dev,
236 unsigned int id, bool counts_only)
238 const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
239 const struct ethtool_ops *ops = dev->ethtool_ops;
243 if (id == ETH_SS_PHY_STATS && dev->phydev &&
244 !ops->get_ethtool_phy_stats && phy_ops &&
245 phy_ops->get_sset_count)
246 ret = phy_ops->get_sset_count(dev->phydev);
247 else if (ops->get_sset_count && ops->get_strings)
248 ret = ops->get_sset_count(dev, id);
258 strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL);
261 if (id == ETH_SS_PHY_STATS && dev->phydev &&
262 !ops->get_ethtool_phy_stats && phy_ops &&
263 phy_ops->get_strings)
264 phy_ops->get_strings(dev->phydev, strings);
266 ops->get_strings(dev, id, strings);
267 info->strings = strings;
268 info->free_strings = true;
275 static int strset_prepare_data(const struct ethnl_req_info *req_base,
276 struct ethnl_reply_data *reply_base,
277 struct genl_info *info)
279 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
280 struct strset_reply_data *data = STRSET_REPDATA(reply_base);
281 struct net_device *dev = reply_base->dev;
285 BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT);
286 memcpy(&data->sets, &info_template, sizeof(data->sets));
289 for (i = 0; i < ETH_SS_COUNT; i++) {
290 if ((req_info->req_ids & (1U << i)) &&
291 data->sets[i].per_dev) {
293 GENL_SET_ERR_MSG(info, "requested per device strings without dev");
300 ret = ethnl_ops_begin(dev);
303 for (i = 0; i < ETH_SS_COUNT; i++) {
304 if (!strset_include(req_info, data, i) ||
305 !data->sets[i].per_dev)
308 ret = strset_prepare_set(&data->sets[i], dev, i,
309 req_info->counts_only);
313 ethnl_ops_complete(dev);
317 ethnl_ops_complete(dev);
319 strset_cleanup_data(reply_base);
323 /* calculate size of ETHTOOL_A_STRSET_STRINGSET nest for one string set */
324 static int strset_set_size(const struct strset_info *info, bool counts_only)
326 unsigned int len = 0;
329 if (info->count == 0)
332 return nla_total_size(2 * nla_total_size(sizeof(u32)));
334 for (i = 0; i < info->count; i++) {
335 const char *str = info->strings[i];
337 /* ETHTOOL_A_STRING_INDEX, ETHTOOL_A_STRING_VALUE, nest */
338 len += nla_total_size(nla_total_size(sizeof(u32)) +
339 ethnl_strz_size(str));
341 /* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */
342 len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len);
344 return nla_total_size(len);
347 static int strset_reply_size(const struct ethnl_req_info *req_base,
348 const struct ethnl_reply_data *reply_base)
350 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
351 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
356 len += nla_total_size(0); /* ETHTOOL_A_STRSET_STRINGSETS */
358 for (i = 0; i < ETH_SS_COUNT; i++) {
359 const struct strset_info *set_info = &data->sets[i];
361 if (!strset_include(req_info, data, i))
364 ret = strset_set_size(set_info, req_info->counts_only);
373 /* fill one string into reply */
374 static int strset_fill_string(struct sk_buff *skb,
375 const struct strset_info *set_info, u32 idx)
377 struct nlattr *string_attr;
380 value = set_info->strings[idx];
382 string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING);
385 if (nla_put_u32(skb, ETHTOOL_A_STRING_INDEX, idx) ||
386 ethnl_put_strz(skb, ETHTOOL_A_STRING_VALUE, value))
387 goto nla_put_failure;
388 nla_nest_end(skb, string_attr);
392 nla_nest_cancel(skb, string_attr);
396 /* fill one string set into reply */
397 static int strset_fill_set(struct sk_buff *skb,
398 const struct strset_info *set_info, u32 id,
401 struct nlattr *stringset_attr;
402 struct nlattr *strings_attr;
405 if (!set_info->per_dev && !set_info->strings)
407 if (set_info->count == 0)
409 stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET);
413 if (nla_put_u32(skb, ETHTOOL_A_STRINGSET_ID, id) ||
414 nla_put_u32(skb, ETHTOOL_A_STRINGSET_COUNT, set_info->count))
415 goto nla_put_failure;
418 strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS);
420 goto nla_put_failure;
421 for (i = 0; i < set_info->count; i++) {
422 if (strset_fill_string(skb, set_info, i) < 0)
423 goto nla_put_failure;
425 nla_nest_end(skb, strings_attr);
428 nla_nest_end(skb, stringset_attr);
432 nla_nest_cancel(skb, stringset_attr);
436 static int strset_fill_reply(struct sk_buff *skb,
437 const struct ethnl_req_info *req_base,
438 const struct ethnl_reply_data *reply_base)
440 const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
441 const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
446 nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS);
450 for (i = 0; i < ETH_SS_COUNT; i++) {
451 if (strset_include(req_info, data, i)) {
452 ret = strset_fill_set(skb, &data->sets[i], i,
453 req_info->counts_only);
455 goto nla_put_failure;
459 nla_nest_end(skb, nest);
463 nla_nest_cancel(skb, nest);
467 const struct ethnl_request_ops ethnl_strset_request_ops = {
468 .request_cmd = ETHTOOL_MSG_STRSET_GET,
469 .reply_cmd = ETHTOOL_MSG_STRSET_GET_REPLY,
470 .hdr_attr = ETHTOOL_A_STRSET_HEADER,
471 .req_info_size = sizeof(struct strset_req_info),
472 .reply_data_size = sizeof(struct strset_reply_data),
473 .allow_nodev_do = true,
475 .parse_request = strset_parse_request,
476 .prepare_data = strset_prepare_data,
477 .reply_size = strset_reply_size,
478 .fill_reply = strset_fill_reply,
479 .cleanup_data = strset_cleanup_data,