strset.c 12 KB

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