pause.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. struct pause_req_info {
  5. struct ethnl_req_info base;
  6. };
  7. struct pause_reply_data {
  8. struct ethnl_reply_data base;
  9. struct ethtool_pauseparam pauseparam;
  10. struct ethtool_pause_stats pausestat;
  11. };
  12. #define PAUSE_REPDATA(__reply_base) \
  13. container_of(__reply_base, struct pause_reply_data, base)
  14. const struct nla_policy ethnl_pause_get_policy[] = {
  15. [ETHTOOL_A_PAUSE_HEADER] =
  16. NLA_POLICY_NESTED(ethnl_header_policy_stats),
  17. };
  18. static int pause_prepare_data(const struct ethnl_req_info *req_base,
  19. struct ethnl_reply_data *reply_base,
  20. struct genl_info *info)
  21. {
  22. struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
  23. struct net_device *dev = reply_base->dev;
  24. int ret;
  25. if (!dev->ethtool_ops->get_pauseparam)
  26. return -EOPNOTSUPP;
  27. ethtool_stats_init((u64 *)&data->pausestat,
  28. sizeof(data->pausestat) / 8);
  29. ret = ethnl_ops_begin(dev);
  30. if (ret < 0)
  31. return ret;
  32. dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
  33. if (req_base->flags & ETHTOOL_FLAG_STATS &&
  34. dev->ethtool_ops->get_pause_stats)
  35. dev->ethtool_ops->get_pause_stats(dev, &data->pausestat);
  36. ethnl_ops_complete(dev);
  37. return 0;
  38. }
  39. static int pause_reply_size(const struct ethnl_req_info *req_base,
  40. const struct ethnl_reply_data *reply_base)
  41. {
  42. int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */
  43. nla_total_size(sizeof(u8)) + /* _PAUSE_RX */
  44. nla_total_size(sizeof(u8)); /* _PAUSE_TX */
  45. if (req_base->flags & ETHTOOL_FLAG_STATS)
  46. n += nla_total_size(0) + /* _PAUSE_STATS */
  47. nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT;
  48. return n;
  49. }
  50. static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
  51. u16 padtype)
  52. {
  53. if (val == ETHTOOL_STAT_NOT_SET)
  54. return 0;
  55. if (nla_put_u64_64bit(skb, attrtype, val, padtype))
  56. return -EMSGSIZE;
  57. return 0;
  58. }
  59. static int pause_put_stats(struct sk_buff *skb,
  60. const struct ethtool_pause_stats *pause_stats)
  61. {
  62. const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
  63. struct nlattr *nest;
  64. nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
  65. if (!nest)
  66. return -EMSGSIZE;
  67. if (ethtool_put_stat(skb, pause_stats->tx_pause_frames,
  68. ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) ||
  69. ethtool_put_stat(skb, pause_stats->rx_pause_frames,
  70. ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad))
  71. goto err_cancel;
  72. nla_nest_end(skb, nest);
  73. return 0;
  74. err_cancel:
  75. nla_nest_cancel(skb, nest);
  76. return -EMSGSIZE;
  77. }
  78. static int pause_fill_reply(struct sk_buff *skb,
  79. const struct ethnl_req_info *req_base,
  80. const struct ethnl_reply_data *reply_base)
  81. {
  82. const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
  83. const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
  84. if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
  85. nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
  86. nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
  87. return -EMSGSIZE;
  88. if (req_base->flags & ETHTOOL_FLAG_STATS &&
  89. pause_put_stats(skb, &data->pausestat))
  90. return -EMSGSIZE;
  91. return 0;
  92. }
  93. const struct ethnl_request_ops ethnl_pause_request_ops = {
  94. .request_cmd = ETHTOOL_MSG_PAUSE_GET,
  95. .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY,
  96. .hdr_attr = ETHTOOL_A_PAUSE_HEADER,
  97. .req_info_size = sizeof(struct pause_req_info),
  98. .reply_data_size = sizeof(struct pause_reply_data),
  99. .prepare_data = pause_prepare_data,
  100. .reply_size = pause_reply_size,
  101. .fill_reply = pause_fill_reply,
  102. };
  103. /* PAUSE_SET */
  104. const struct nla_policy ethnl_pause_set_policy[] = {
  105. [ETHTOOL_A_PAUSE_HEADER] =
  106. NLA_POLICY_NESTED(ethnl_header_policy),
  107. [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 },
  108. [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 },
  109. [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 },
  110. };
  111. int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info)
  112. {
  113. struct ethtool_pauseparam params = {};
  114. struct ethnl_req_info req_info = {};
  115. struct nlattr **tb = info->attrs;
  116. const struct ethtool_ops *ops;
  117. struct net_device *dev;
  118. bool mod = false;
  119. int ret;
  120. ret = ethnl_parse_header_dev_get(&req_info,
  121. tb[ETHTOOL_A_PAUSE_HEADER],
  122. genl_info_net(info), info->extack,
  123. true);
  124. if (ret < 0)
  125. return ret;
  126. dev = req_info.dev;
  127. ops = dev->ethtool_ops;
  128. ret = -EOPNOTSUPP;
  129. if (!ops->get_pauseparam || !ops->set_pauseparam)
  130. goto out_dev;
  131. rtnl_lock();
  132. ret = ethnl_ops_begin(dev);
  133. if (ret < 0)
  134. goto out_rtnl;
  135. ops->get_pauseparam(dev, &params);
  136. ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
  137. ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
  138. ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
  139. ret = 0;
  140. if (!mod)
  141. goto out_ops;
  142. ret = dev->ethtool_ops->set_pauseparam(dev, &params);
  143. if (ret < 0)
  144. goto out_ops;
  145. ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
  146. out_ops:
  147. ethnl_ops_complete(dev);
  148. out_rtnl:
  149. rtnl_unlock();
  150. out_dev:
  151. ethnl_parse_header_dev_put(&req_info);
  152. return ret;
  153. }