ethtool: add a new command for reading standard stats
[linux-2.6-microblaze.git] / net / ethtool / strset.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/ethtool.h>
4 #include <linux/phy.h>
5 #include "netlink.h"
6 #include "common.h"
7
8 struct strset_info {
9         bool per_dev;
10         bool free_strings;
11         unsigned int count;
12         const char (*strings)[ETH_GSTRING_LEN];
13 };
14
15 static const struct strset_info info_template[] = {
16         [ETH_SS_TEST] = {
17                 .per_dev        = true,
18         },
19         [ETH_SS_STATS] = {
20                 .per_dev        = true,
21         },
22         [ETH_SS_PRIV_FLAGS] = {
23                 .per_dev        = true,
24         },
25         [ETH_SS_FEATURES] = {
26                 .per_dev        = false,
27                 .count          = ARRAY_SIZE(netdev_features_strings),
28                 .strings        = netdev_features_strings,
29         },
30         [ETH_SS_RSS_HASH_FUNCS] = {
31                 .per_dev        = false,
32                 .count          = ARRAY_SIZE(rss_hash_func_strings),
33                 .strings        = rss_hash_func_strings,
34         },
35         [ETH_SS_TUNABLES] = {
36                 .per_dev        = false,
37                 .count          = ARRAY_SIZE(tunable_strings),
38                 .strings        = tunable_strings,
39         },
40         [ETH_SS_PHY_STATS] = {
41                 .per_dev        = true,
42         },
43         [ETH_SS_PHY_TUNABLES] = {
44                 .per_dev        = false,
45                 .count          = ARRAY_SIZE(phy_tunable_strings),
46                 .strings        = phy_tunable_strings,
47         },
48         [ETH_SS_LINK_MODES] = {
49                 .per_dev        = false,
50                 .count          = __ETHTOOL_LINK_MODE_MASK_NBITS,
51                 .strings        = link_mode_names,
52         },
53         [ETH_SS_MSG_CLASSES] = {
54                 .per_dev        = false,
55                 .count          = NETIF_MSG_CLASS_COUNT,
56                 .strings        = netif_msg_class_names,
57         },
58         [ETH_SS_WOL_MODES] = {
59                 .per_dev        = false,
60                 .count          = WOL_MODE_COUNT,
61                 .strings        = wol_mode_names,
62         },
63         [ETH_SS_SOF_TIMESTAMPING] = {
64                 .per_dev        = false,
65                 .count          = __SOF_TIMESTAMPING_CNT,
66                 .strings        = sof_timestamping_names,
67         },
68         [ETH_SS_TS_TX_TYPES] = {
69                 .per_dev        = false,
70                 .count          = __HWTSTAMP_TX_CNT,
71                 .strings        = ts_tx_type_names,
72         },
73         [ETH_SS_TS_RX_FILTERS] = {
74                 .per_dev        = false,
75                 .count          = __HWTSTAMP_FILTER_CNT,
76                 .strings        = ts_rx_filter_names,
77         },
78         [ETH_SS_UDP_TUNNEL_TYPES] = {
79                 .per_dev        = false,
80                 .count          = __ETHTOOL_UDP_TUNNEL_TYPE_CNT,
81                 .strings        = udp_tunnel_type_names,
82         },
83         [ETH_SS_STATS_STD] = {
84                 .per_dev        = false,
85                 .count          = __ETHTOOL_STATS_CNT,
86                 .strings        = stats_std_names,
87         },
88         [ETH_SS_STATS_ETH_PHY] = {
89                 .per_dev        = false,
90                 .count          = __ETHTOOL_A_STATS_ETH_PHY_CNT,
91                 .strings        = stats_eth_phy_names,
92         },
93 };
94
95 struct strset_req_info {
96         struct ethnl_req_info           base;
97         u32                             req_ids;
98         bool                            counts_only;
99 };
100
101 #define STRSET_REQINFO(__req_base) \
102         container_of(__req_base, struct strset_req_info, base)
103
104 struct strset_reply_data {
105         struct ethnl_reply_data         base;
106         struct strset_info              sets[ETH_SS_COUNT];
107 };
108
109 #define STRSET_REPDATA(__reply_base) \
110         container_of(__reply_base, struct strset_reply_data, base)
111
112 const struct nla_policy ethnl_strset_get_policy[] = {
113         [ETHTOOL_A_STRSET_HEADER]       =
114                 NLA_POLICY_NESTED(ethnl_header_policy),
115         [ETHTOOL_A_STRSET_STRINGSETS]   = { .type = NLA_NESTED },
116         [ETHTOOL_A_STRSET_COUNTS_ONLY]  = { .type = NLA_FLAG },
117 };
118
119 static const struct nla_policy get_stringset_policy[] = {
120         [ETHTOOL_A_STRINGSET_ID]        = { .type = NLA_U32 },
121 };
122
123 /**
124  * strset_include() - test if a string set should be included in reply
125  * @info: parsed client request
126  * @data: pointer to request data structure
127  * @id:   id of string set to check (ETH_SS_* constants)
128  */
129 static bool strset_include(const struct strset_req_info *info,
130                            const struct strset_reply_data *data, u32 id)
131 {
132         bool per_dev;
133
134         BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids));
135
136         if (info->req_ids)
137                 return info->req_ids & (1U << id);
138         per_dev = data->sets[id].per_dev;
139         if (!per_dev && !data->sets[id].strings)
140                 return false;
141
142         return data->base.dev ? per_dev : !per_dev;
143 }
144
145 static int strset_get_id(const struct nlattr *nest, u32 *val,
146                          struct netlink_ext_ack *extack)
147 {
148         struct nlattr *tb[ARRAY_SIZE(get_stringset_policy)];
149         int ret;
150
151         ret = nla_parse_nested(tb, ARRAY_SIZE(get_stringset_policy) - 1, nest,
152                                get_stringset_policy, extack);
153         if (ret < 0)
154                 return ret;
155         if (!tb[ETHTOOL_A_STRINGSET_ID])
156                 return -EINVAL;
157
158         *val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]);
159         return 0;
160 }
161
162 static const struct nla_policy strset_stringsets_policy[] = {
163         [ETHTOOL_A_STRINGSETS_STRINGSET]        = { .type = NLA_NESTED },
164 };
165
166 static int strset_parse_request(struct ethnl_req_info *req_base,
167                                 struct nlattr **tb,
168                                 struct netlink_ext_ack *extack)
169 {
170         struct strset_req_info *req_info = STRSET_REQINFO(req_base);
171         struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS];
172         struct nlattr *attr;
173         int rem, ret;
174
175         if (!nest)
176                 return 0;
177         ret = nla_validate_nested(nest,
178                                   ARRAY_SIZE(strset_stringsets_policy) - 1,
179                                   strset_stringsets_policy, extack);
180         if (ret < 0)
181                 return ret;
182
183         req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY];
184         nla_for_each_nested(attr, nest, rem) {
185                 u32 id;
186
187                 if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET,
188                               "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n",
189                               nla_type(attr)))
190                         return -EINVAL;
191
192                 ret = strset_get_id(attr, &id, extack);
193                 if (ret < 0)
194                         return ret;
195                 if (id >= ETH_SS_COUNT) {
196                         NL_SET_ERR_MSG_ATTR(extack, attr,
197                                             "unknown string set id");
198                         return -EOPNOTSUPP;
199                 }
200
201                 req_info->req_ids |= (1U << id);
202         }
203
204         return 0;
205 }
206
207 static void strset_cleanup_data(struct ethnl_reply_data *reply_base)
208 {
209         struct strset_reply_data *data = STRSET_REPDATA(reply_base);
210         unsigned int i;
211
212         for (i = 0; i < ETH_SS_COUNT; i++)
213                 if (data->sets[i].free_strings) {
214                         kfree(data->sets[i].strings);
215                         data->sets[i].strings = NULL;
216                         data->sets[i].free_strings = false;
217                 }
218 }
219
220 static int strset_prepare_set(struct strset_info *info, struct net_device *dev,
221                               unsigned int id, bool counts_only)
222 {
223         const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
224         const struct ethtool_ops *ops = dev->ethtool_ops;
225         void *strings;
226         int count, ret;
227
228         if (id == ETH_SS_PHY_STATS && dev->phydev &&
229             !ops->get_ethtool_phy_stats && phy_ops &&
230             phy_ops->get_sset_count)
231                 ret = phy_ops->get_sset_count(dev->phydev);
232         else if (ops->get_sset_count && ops->get_strings)
233                 ret = ops->get_sset_count(dev, id);
234         else
235                 ret = -EOPNOTSUPP;
236         if (ret <= 0) {
237                 info->count = 0;
238                 return 0;
239         }
240
241         count = ret;
242         if (!counts_only) {
243                 strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL);
244                 if (!strings)
245                         return -ENOMEM;
246                 if (id == ETH_SS_PHY_STATS && dev->phydev &&
247                     !ops->get_ethtool_phy_stats && phy_ops &&
248                     phy_ops->get_strings)
249                         phy_ops->get_strings(dev->phydev, strings);
250                 else
251                         ops->get_strings(dev, id, strings);
252                 info->strings = strings;
253                 info->free_strings = true;
254         }
255         info->count = count;
256
257         return 0;
258 }
259
260 static int strset_prepare_data(const struct ethnl_req_info *req_base,
261                                struct ethnl_reply_data *reply_base,
262                                struct genl_info *info)
263 {
264         const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
265         struct strset_reply_data *data = STRSET_REPDATA(reply_base);
266         struct net_device *dev = reply_base->dev;
267         unsigned int i;
268         int ret;
269
270         BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT);
271         memcpy(&data->sets, &info_template, sizeof(data->sets));
272
273         if (!dev) {
274                 for (i = 0; i < ETH_SS_COUNT; i++) {
275                         if ((req_info->req_ids & (1U << i)) &&
276                             data->sets[i].per_dev) {
277                                 if (info)
278                                         GENL_SET_ERR_MSG(info, "requested per device strings without dev");
279                                 return -EINVAL;
280                         }
281                 }
282                 return 0;
283         }
284
285         ret = ethnl_ops_begin(dev);
286         if (ret < 0)
287                 goto err_strset;
288         for (i = 0; i < ETH_SS_COUNT; i++) {
289                 if (!strset_include(req_info, data, i) ||
290                     !data->sets[i].per_dev)
291                         continue;
292
293                 ret = strset_prepare_set(&data->sets[i], dev, i,
294                                          req_info->counts_only);
295                 if (ret < 0)
296                         goto err_ops;
297         }
298         ethnl_ops_complete(dev);
299
300         return 0;
301 err_ops:
302         ethnl_ops_complete(dev);
303 err_strset:
304         strset_cleanup_data(reply_base);
305         return ret;
306 }
307
308 /* calculate size of ETHTOOL_A_STRSET_STRINGSET nest for one string set */
309 static int strset_set_size(const struct strset_info *info, bool counts_only)
310 {
311         unsigned int len = 0;
312         unsigned int i;
313
314         if (info->count == 0)
315                 return 0;
316         if (counts_only)
317                 return nla_total_size(2 * nla_total_size(sizeof(u32)));
318
319         for (i = 0; i < info->count; i++) {
320                 const char *str = info->strings[i];
321
322                 /* ETHTOOL_A_STRING_INDEX, ETHTOOL_A_STRING_VALUE, nest */
323                 len += nla_total_size(nla_total_size(sizeof(u32)) +
324                                       ethnl_strz_size(str));
325         }
326         /* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */
327         len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len);
328
329         return nla_total_size(len);
330 }
331
332 static int strset_reply_size(const struct ethnl_req_info *req_base,
333                              const struct ethnl_reply_data *reply_base)
334 {
335         const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
336         const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
337         unsigned int i;
338         int len = 0;
339         int ret;
340
341         for (i = 0; i < ETH_SS_COUNT; i++) {
342                 const struct strset_info *set_info = &data->sets[i];
343
344                 if (!strset_include(req_info, data, i))
345                         continue;
346
347                 ret = strset_set_size(set_info, req_info->counts_only);
348                 if (ret < 0)
349                         return ret;
350                 len += ret;
351         }
352
353         return len;
354 }
355
356 /* fill one string into reply */
357 static int strset_fill_string(struct sk_buff *skb,
358                               const struct strset_info *set_info, u32 idx)
359 {
360         struct nlattr *string_attr;
361         const char *value;
362
363         value = set_info->strings[idx];
364
365         string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING);
366         if (!string_attr)
367                 return -EMSGSIZE;
368         if (nla_put_u32(skb, ETHTOOL_A_STRING_INDEX, idx) ||
369             ethnl_put_strz(skb, ETHTOOL_A_STRING_VALUE, value))
370                 goto nla_put_failure;
371         nla_nest_end(skb, string_attr);
372
373         return 0;
374 nla_put_failure:
375         nla_nest_cancel(skb, string_attr);
376         return -EMSGSIZE;
377 }
378
379 /* fill one string set into reply */
380 static int strset_fill_set(struct sk_buff *skb,
381                            const struct strset_info *set_info, u32 id,
382                            bool counts_only)
383 {
384         struct nlattr *stringset_attr;
385         struct nlattr *strings_attr;
386         unsigned int i;
387
388         if (!set_info->per_dev && !set_info->strings)
389                 return -EOPNOTSUPP;
390         if (set_info->count == 0)
391                 return 0;
392         stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET);
393         if (!stringset_attr)
394                 return -EMSGSIZE;
395
396         if (nla_put_u32(skb, ETHTOOL_A_STRINGSET_ID, id) ||
397             nla_put_u32(skb, ETHTOOL_A_STRINGSET_COUNT, set_info->count))
398                 goto nla_put_failure;
399
400         if (!counts_only) {
401                 strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS);
402                 if (!strings_attr)
403                         goto nla_put_failure;
404                 for (i = 0; i < set_info->count; i++) {
405                         if (strset_fill_string(skb, set_info, i) < 0)
406                                 goto nla_put_failure;
407                 }
408                 nla_nest_end(skb, strings_attr);
409         }
410
411         nla_nest_end(skb, stringset_attr);
412         return 0;
413
414 nla_put_failure:
415         nla_nest_cancel(skb, stringset_attr);
416         return -EMSGSIZE;
417 }
418
419 static int strset_fill_reply(struct sk_buff *skb,
420                              const struct ethnl_req_info *req_base,
421                              const struct ethnl_reply_data *reply_base)
422 {
423         const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
424         const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
425         struct nlattr *nest;
426         unsigned int i;
427         int ret;
428
429         nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS);
430         if (!nest)
431                 return -EMSGSIZE;
432
433         for (i = 0; i < ETH_SS_COUNT; i++) {
434                 if (strset_include(req_info, data, i)) {
435                         ret = strset_fill_set(skb, &data->sets[i], i,
436                                               req_info->counts_only);
437                         if (ret < 0)
438                                 goto nla_put_failure;
439                 }
440         }
441
442         nla_nest_end(skb, nest);
443         return 0;
444
445 nla_put_failure:
446         nla_nest_cancel(skb, nest);
447         return ret;
448 }
449
450 const struct ethnl_request_ops ethnl_strset_request_ops = {
451         .request_cmd            = ETHTOOL_MSG_STRSET_GET,
452         .reply_cmd              = ETHTOOL_MSG_STRSET_GET_REPLY,
453         .hdr_attr               = ETHTOOL_A_STRSET_HEADER,
454         .req_info_size          = sizeof(struct strset_req_info),
455         .reply_data_size        = sizeof(struct strset_reply_data),
456         .allow_nodev_do         = true,
457
458         .parse_request          = strset_parse_request,
459         .prepare_data           = strset_prepare_data,
460         .reply_size             = strset_reply_size,
461         .fill_reply             = strset_fill_reply,
462         .cleanup_data           = strset_cleanup_data,
463 };