xref: /linux/net/ethtool/strset.c (revision 521fe8bb5874963d5f6fd58d5c5ad80fbc9c6b1c)
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 };
54 
55 struct strset_req_info {
56 	struct ethnl_req_info		base;
57 	u32				req_ids;
58 	bool				counts_only;
59 };
60 
61 #define STRSET_REQINFO(__req_base) \
62 	container_of(__req_base, struct strset_req_info, base)
63 
64 struct strset_reply_data {
65 	struct ethnl_reply_data		base;
66 	struct strset_info		sets[ETH_SS_COUNT];
67 };
68 
69 #define STRSET_REPDATA(__reply_base) \
70 	container_of(__reply_base, struct strset_reply_data, base)
71 
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 },
76 };
77 
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 },
84 };
85 
86 /**
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)
90  */
91 static bool strset_include(const struct strset_req_info *info,
92 			   const struct strset_reply_data *data, u32 id)
93 {
94 	bool per_dev;
95 
96 	BUILD_BUG_ON(ETH_SS_COUNT >= BITS_PER_BYTE * sizeof(info->req_ids));
97 
98 	if (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)
102 		return false;
103 
104 	return data->base.dev ? per_dev : !per_dev;
105 }
106 
107 static int strset_get_id(const struct nlattr *nest, u32 *val,
108 			 struct netlink_ext_ack *extack)
109 {
110 	struct nlattr *tb[ETHTOOL_A_STRINGSET_MAX + 1];
111 	int ret;
112 
113 	ret = nla_parse_nested(tb, ETHTOOL_A_STRINGSET_MAX, nest,
114 			       get_stringset_policy, extack);
115 	if (ret < 0)
116 		return ret;
117 	if (!tb[ETHTOOL_A_STRINGSET_ID])
118 		return -EINVAL;
119 
120 	*val = nla_get_u32(tb[ETHTOOL_A_STRINGSET_ID]);
121 	return 0;
122 }
123 
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 },
128 };
129 
130 static int strset_parse_request(struct ethnl_req_info *req_base,
131 				struct nlattr **tb,
132 				struct netlink_ext_ack *extack)
133 {
134 	struct strset_req_info *req_info = STRSET_REQINFO(req_base);
135 	struct nlattr *nest = tb[ETHTOOL_A_STRSET_STRINGSETS];
136 	struct nlattr *attr;
137 	int rem, ret;
138 
139 	if (!nest)
140 		return 0;
141 	ret = nla_validate_nested(nest, ETHTOOL_A_STRINGSETS_MAX,
142 				  strset_stringsets_policy, extack);
143 	if (ret < 0)
144 		return ret;
145 
146 	req_info->counts_only = tb[ETHTOOL_A_STRSET_COUNTS_ONLY];
147 	nla_for_each_nested(attr, nest, rem) {
148 		u32 id;
149 
150 		if (WARN_ONCE(nla_type(attr) != ETHTOOL_A_STRINGSETS_STRINGSET,
151 			      "unexpected attrtype %u in ETHTOOL_A_STRSET_STRINGSETS\n",
152 			      nla_type(attr)))
153 			return -EINVAL;
154 
155 		ret = strset_get_id(attr, &id, extack);
156 		if (ret < 0)
157 			return ret;
158 		if (ret >= ETH_SS_COUNT) {
159 			NL_SET_ERR_MSG_ATTR(extack, attr,
160 					    "unknown string set id");
161 			return -EOPNOTSUPP;
162 		}
163 
164 		req_info->req_ids |= (1U << id);
165 	}
166 
167 	return 0;
168 }
169 
170 static void strset_cleanup_data(struct ethnl_reply_data *reply_base)
171 {
172 	struct strset_reply_data *data = STRSET_REPDATA(reply_base);
173 	unsigned int i;
174 
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;
180 		}
181 }
182 
183 static int strset_prepare_set(struct strset_info *info, struct net_device *dev,
184 			      unsigned int id, bool counts_only)
185 {
186 	const struct ethtool_ops *ops = dev->ethtool_ops;
187 	void *strings;
188 	int count, ret;
189 
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);
195 	else
196 		ret = -EOPNOTSUPP;
197 	if (ret <= 0) {
198 		info->count = 0;
199 		return 0;
200 	}
201 
202 	count = ret;
203 	if (!counts_only) {
204 		strings = kcalloc(count, ETH_GSTRING_LEN, GFP_KERNEL);
205 		if (!strings)
206 			return -ENOMEM;
207 		if (id == ETH_SS_PHY_STATS && dev->phydev &&
208 		    !ops->get_ethtool_phy_stats)
209 			phy_ethtool_get_strings(dev->phydev, strings);
210 		else
211 			ops->get_strings(dev, id, strings);
212 		info->strings = strings;
213 		info->free_strings = true;
214 	}
215 	info->count = count;
216 
217 	return 0;
218 }
219 
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)
223 {
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;
227 	unsigned int i;
228 	int ret;
229 
230 	BUILD_BUG_ON(ARRAY_SIZE(info_template) != ETH_SS_COUNT);
231 	memcpy(&data->sets, &info_template, sizeof(data->sets));
232 
233 	if (!dev) {
234 		for (i = 0; i < ETH_SS_COUNT; i++) {
235 			if ((req_info->req_ids & (1U << i)) &&
236 			    data->sets[i].per_dev) {
237 				if (info)
238 					GENL_SET_ERR_MSG(info, "requested per device strings without dev");
239 				return -EINVAL;
240 			}
241 		}
242 	}
243 
244 	ret = ethnl_ops_begin(dev);
245 	if (ret < 0)
246 		goto err_strset;
247 	for (i = 0; i < ETH_SS_COUNT; i++) {
248 		if (!strset_include(req_info, data, i) ||
249 		    !data->sets[i].per_dev)
250 			continue;
251 
252 		ret = strset_prepare_set(&data->sets[i], dev, i,
253 					 req_info->counts_only);
254 		if (ret < 0)
255 			goto err_ops;
256 	}
257 	ethnl_ops_complete(dev);
258 
259 	return 0;
260 err_ops:
261 	ethnl_ops_complete(dev);
262 err_strset:
263 	strset_cleanup_data(reply_base);
264 	return ret;
265 }
266 
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)
269 {
270 	unsigned int len = 0;
271 	unsigned int i;
272 
273 	if (info->count == 0)
274 		return 0;
275 	if (counts_only)
276 		return nla_total_size(2 * nla_total_size(sizeof(u32)));
277 
278 	for (i = 0; i < info->count; i++) {
279 		const char *str = info->strings[i];
280 
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));
284 	}
285 	/* ETHTOOL_A_STRINGSET_ID, ETHTOOL_A_STRINGSET_COUNT */
286 	len = 2 * nla_total_size(sizeof(u32)) + nla_total_size(len);
287 
288 	return nla_total_size(len);
289 }
290 
291 static int strset_reply_size(const struct ethnl_req_info *req_base,
292 			     const struct ethnl_reply_data *reply_base)
293 {
294 	const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
295 	const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
296 	unsigned int i;
297 	int len = 0;
298 	int ret;
299 
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];
303 
304 		if (!strset_include(req_info, data, i))
305 			continue;
306 
307 		ret = strset_set_size(set_info, req_info->counts_only);
308 		if (ret < 0)
309 			return ret;
310 		len += ret;
311 	}
312 
313 	return len;
314 }
315 
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)
319 {
320 	struct nlattr *string_attr;
321 	const char *value;
322 
323 	value = set_info->strings[idx];
324 
325 	string_attr = nla_nest_start(skb, ETHTOOL_A_STRINGS_STRING);
326 	if (!string_attr)
327 		return -EMSGSIZE;
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);
332 
333 	return 0;
334 nla_put_failure:
335 	nla_nest_cancel(skb, string_attr);
336 	return -EMSGSIZE;
337 }
338 
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,
342 			   bool counts_only)
343 {
344 	struct nlattr *stringset_attr;
345 	struct nlattr *strings_attr;
346 	unsigned int i;
347 
348 	if (!set_info->per_dev && !set_info->strings)
349 		return -EOPNOTSUPP;
350 	if (set_info->count == 0)
351 		return 0;
352 	stringset_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSETS_STRINGSET);
353 	if (!stringset_attr)
354 		return -EMSGSIZE;
355 
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;
359 
360 	if (!counts_only) {
361 		strings_attr = nla_nest_start(skb, ETHTOOL_A_STRINGSET_STRINGS);
362 		if (!strings_attr)
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;
367 		}
368 		nla_nest_end(skb, strings_attr);
369 	}
370 
371 	nla_nest_end(skb, stringset_attr);
372 	return 0;
373 
374 nla_put_failure:
375 	nla_nest_cancel(skb, stringset_attr);
376 	return -EMSGSIZE;
377 }
378 
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)
382 {
383 	const struct strset_req_info *req_info = STRSET_REQINFO(req_base);
384 	const struct strset_reply_data *data = STRSET_REPDATA(reply_base);
385 	struct nlattr *nest;
386 	unsigned int i;
387 	int ret;
388 
389 	nest = nla_nest_start(skb, ETHTOOL_A_STRSET_STRINGSETS);
390 	if (!nest)
391 		return -EMSGSIZE;
392 
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);
397 			if (ret < 0)
398 				goto nla_put_failure;
399 		}
400 	}
401 
402 	nla_nest_end(skb, nest);
403 	return 0;
404 
405 nla_put_failure:
406 	nla_nest_cancel(skb, nest);
407 	return ret;
408 }
409 
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,
419 
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,
425 };
426