debug.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. #include "bitset.h"
  5. struct debug_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct debug_reply_data {
  9. struct ethnl_reply_data base;
  10. u32 msg_mask;
  11. };
  12. #define DEBUG_REPDATA(__reply_base) \
  13. container_of(__reply_base, struct debug_reply_data, base)
  14. const struct nla_policy ethnl_debug_get_policy[] = {
  15. [ETHTOOL_A_DEBUG_HEADER] =
  16. NLA_POLICY_NESTED(ethnl_header_policy),
  17. };
  18. static int debug_prepare_data(const struct ethnl_req_info *req_base,
  19. struct ethnl_reply_data *reply_base,
  20. struct genl_info *info)
  21. {
  22. struct debug_reply_data *data = DEBUG_REPDATA(reply_base);
  23. struct net_device *dev = reply_base->dev;
  24. int ret;
  25. if (!dev->ethtool_ops->get_msglevel)
  26. return -EOPNOTSUPP;
  27. ret = ethnl_ops_begin(dev);
  28. if (ret < 0)
  29. return ret;
  30. data->msg_mask = dev->ethtool_ops->get_msglevel(dev);
  31. ethnl_ops_complete(dev);
  32. return 0;
  33. }
  34. static int debug_reply_size(const struct ethnl_req_info *req_base,
  35. const struct ethnl_reply_data *reply_base)
  36. {
  37. const struct debug_reply_data *data = DEBUG_REPDATA(reply_base);
  38. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  39. return ethnl_bitset32_size(&data->msg_mask, NULL, NETIF_MSG_CLASS_COUNT,
  40. netif_msg_class_names, compact);
  41. }
  42. static int debug_fill_reply(struct sk_buff *skb,
  43. const struct ethnl_req_info *req_base,
  44. const struct ethnl_reply_data *reply_base)
  45. {
  46. const struct debug_reply_data *data = DEBUG_REPDATA(reply_base);
  47. bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
  48. return ethnl_put_bitset32(skb, ETHTOOL_A_DEBUG_MSGMASK, &data->msg_mask,
  49. NULL, NETIF_MSG_CLASS_COUNT,
  50. netif_msg_class_names, compact);
  51. }
  52. const struct ethnl_request_ops ethnl_debug_request_ops = {
  53. .request_cmd = ETHTOOL_MSG_DEBUG_GET,
  54. .reply_cmd = ETHTOOL_MSG_DEBUG_GET_REPLY,
  55. .hdr_attr = ETHTOOL_A_DEBUG_HEADER,
  56. .req_info_size = sizeof(struct debug_req_info),
  57. .reply_data_size = sizeof(struct debug_reply_data),
  58. .prepare_data = debug_prepare_data,
  59. .reply_size = debug_reply_size,
  60. .fill_reply = debug_fill_reply,
  61. };
  62. /* DEBUG_SET */
  63. const struct nla_policy ethnl_debug_set_policy[] = {
  64. [ETHTOOL_A_DEBUG_HEADER] =
  65. NLA_POLICY_NESTED(ethnl_header_policy),
  66. [ETHTOOL_A_DEBUG_MSGMASK] = { .type = NLA_NESTED },
  67. };
  68. int ethnl_set_debug(struct sk_buff *skb, struct genl_info *info)
  69. {
  70. struct ethnl_req_info req_info = {};
  71. struct nlattr **tb = info->attrs;
  72. struct net_device *dev;
  73. bool mod = false;
  74. u32 msg_mask;
  75. int ret;
  76. ret = ethnl_parse_header_dev_get(&req_info,
  77. tb[ETHTOOL_A_DEBUG_HEADER],
  78. genl_info_net(info), info->extack,
  79. true);
  80. if (ret < 0)
  81. return ret;
  82. dev = req_info.dev;
  83. ret = -EOPNOTSUPP;
  84. if (!dev->ethtool_ops->get_msglevel || !dev->ethtool_ops->set_msglevel)
  85. goto out_dev;
  86. rtnl_lock();
  87. ret = ethnl_ops_begin(dev);
  88. if (ret < 0)
  89. goto out_rtnl;
  90. msg_mask = dev->ethtool_ops->get_msglevel(dev);
  91. ret = ethnl_update_bitset32(&msg_mask, NETIF_MSG_CLASS_COUNT,
  92. tb[ETHTOOL_A_DEBUG_MSGMASK],
  93. netif_msg_class_names, info->extack, &mod);
  94. if (ret < 0 || !mod)
  95. goto out_ops;
  96. dev->ethtool_ops->set_msglevel(dev, msg_mask);
  97. ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
  98. out_ops:
  99. ethnl_ops_complete(dev);
  100. out_rtnl:
  101. rtnl_unlock();
  102. out_dev:
  103. ethnl_parse_header_dev_put(&req_info);
  104. return ret;
  105. }