pause.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. enum ethtool_mac_stats_src src;
  7. };
  8. #define PAUSE_REQINFO(__req_base) \
  9. container_of(__req_base, struct pause_req_info, base)
  10. struct pause_reply_data {
  11. struct ethnl_reply_data base;
  12. struct ethtool_pauseparam pauseparam;
  13. struct ethtool_pause_stats pausestat;
  14. };
  15. #define PAUSE_REPDATA(__reply_base) \
  16. container_of(__reply_base, struct pause_reply_data, base)
  17. const struct nla_policy ethnl_pause_get_policy[] = {
  18. [ETHTOOL_A_PAUSE_HEADER] =
  19. NLA_POLICY_NESTED(ethnl_header_policy_stats),
  20. [ETHTOOL_A_PAUSE_STATS_SRC] =
  21. NLA_POLICY_MAX(NLA_U32, ETHTOOL_MAC_STATS_SRC_PMAC),
  22. };
  23. static int pause_parse_request(struct ethnl_req_info *req_base,
  24. struct nlattr **tb,
  25. struct netlink_ext_ack *extack)
  26. {
  27. enum ethtool_mac_stats_src src = ETHTOOL_MAC_STATS_SRC_AGGREGATE;
  28. struct pause_req_info *req_info = PAUSE_REQINFO(req_base);
  29. if (tb[ETHTOOL_A_PAUSE_STATS_SRC]) {
  30. if (!(req_base->flags & ETHTOOL_FLAG_STATS)) {
  31. NL_SET_ERR_MSG_MOD(extack,
  32. "ETHTOOL_FLAG_STATS must be set when requesting a source of stats");
  33. return -EINVAL;
  34. }
  35. src = nla_get_u32(tb[ETHTOOL_A_PAUSE_STATS_SRC]);
  36. }
  37. req_info->src = src;
  38. return 0;
  39. }
  40. static int pause_prepare_data(const struct ethnl_req_info *req_base,
  41. struct ethnl_reply_data *reply_base,
  42. const struct genl_info *info)
  43. {
  44. const struct pause_req_info *req_info = PAUSE_REQINFO(req_base);
  45. struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
  46. enum ethtool_mac_stats_src src = req_info->src;
  47. struct net_device *dev = reply_base->dev;
  48. int ret;
  49. if (!dev->ethtool_ops->get_pauseparam)
  50. return -EOPNOTSUPP;
  51. ethtool_stats_init((u64 *)&data->pausestat,
  52. sizeof(data->pausestat) / 8);
  53. data->pausestat.src = src;
  54. ret = ethnl_ops_begin(dev);
  55. if (ret < 0)
  56. return ret;
  57. if ((src == ETHTOOL_MAC_STATS_SRC_EMAC ||
  58. src == ETHTOOL_MAC_STATS_SRC_PMAC) &&
  59. !__ethtool_dev_mm_supported(dev)) {
  60. NL_SET_ERR_MSG_MOD(info->extack,
  61. "Device does not support MAC merge layer");
  62. ethnl_ops_complete(dev);
  63. return -EOPNOTSUPP;
  64. }
  65. dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
  66. if (req_base->flags & ETHTOOL_FLAG_STATS &&
  67. dev->ethtool_ops->get_pause_stats)
  68. dev->ethtool_ops->get_pause_stats(dev, &data->pausestat);
  69. ethnl_ops_complete(dev);
  70. return 0;
  71. }
  72. static int pause_reply_size(const struct ethnl_req_info *req_base,
  73. const struct ethnl_reply_data *reply_base)
  74. {
  75. int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */
  76. nla_total_size(sizeof(u8)) + /* _PAUSE_RX */
  77. nla_total_size(sizeof(u8)); /* _PAUSE_TX */
  78. if (req_base->flags & ETHTOOL_FLAG_STATS)
  79. n += nla_total_size(0) + /* _PAUSE_STATS */
  80. nla_total_size(sizeof(u32)) + /* _PAUSE_STATS_SRC */
  81. nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT;
  82. return n;
  83. }
  84. static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
  85. u16 padtype)
  86. {
  87. if (val == ETHTOOL_STAT_NOT_SET)
  88. return 0;
  89. if (nla_put_u64_64bit(skb, attrtype, val, padtype))
  90. return -EMSGSIZE;
  91. return 0;
  92. }
  93. static int pause_put_stats(struct sk_buff *skb,
  94. const struct ethtool_pause_stats *pause_stats)
  95. {
  96. const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
  97. struct nlattr *nest;
  98. if (nla_put_u32(skb, ETHTOOL_A_PAUSE_STATS_SRC, pause_stats->src))
  99. return -EMSGSIZE;
  100. nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
  101. if (!nest)
  102. return -EMSGSIZE;
  103. if (ethtool_put_stat(skb, pause_stats->tx_pause_frames,
  104. ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) ||
  105. ethtool_put_stat(skb, pause_stats->rx_pause_frames,
  106. ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad))
  107. goto err_cancel;
  108. nla_nest_end(skb, nest);
  109. return 0;
  110. err_cancel:
  111. nla_nest_cancel(skb, nest);
  112. return -EMSGSIZE;
  113. }
  114. static int pause_fill_reply(struct sk_buff *skb,
  115. const struct ethnl_req_info *req_base,
  116. const struct ethnl_reply_data *reply_base)
  117. {
  118. const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
  119. const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
  120. if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
  121. nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
  122. nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
  123. return -EMSGSIZE;
  124. if (req_base->flags & ETHTOOL_FLAG_STATS &&
  125. pause_put_stats(skb, &data->pausestat))
  126. return -EMSGSIZE;
  127. return 0;
  128. }
  129. /* PAUSE_SET */
  130. const struct nla_policy ethnl_pause_set_policy[] = {
  131. [ETHTOOL_A_PAUSE_HEADER] =
  132. NLA_POLICY_NESTED(ethnl_header_policy),
  133. [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 },
  134. [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 },
  135. [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 },
  136. };
  137. static int
  138. ethnl_set_pause_validate(struct ethnl_req_info *req_info,
  139. struct genl_info *info)
  140. {
  141. const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
  142. return ops->get_pauseparam && ops->set_pauseparam ? 1 : -EOPNOTSUPP;
  143. }
  144. static int
  145. ethnl_set_pause(struct ethnl_req_info *req_info, struct genl_info *info)
  146. {
  147. struct net_device *dev = req_info->dev;
  148. struct ethtool_pauseparam params = {};
  149. struct nlattr **tb = info->attrs;
  150. bool mod = false;
  151. int ret;
  152. dev->ethtool_ops->get_pauseparam(dev, &params);
  153. ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
  154. ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
  155. ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
  156. if (!mod)
  157. return 0;
  158. ret = dev->ethtool_ops->set_pauseparam(dev, &params);
  159. return ret < 0 ? ret : 1;
  160. }
  161. const struct ethnl_request_ops ethnl_pause_request_ops = {
  162. .request_cmd = ETHTOOL_MSG_PAUSE_GET,
  163. .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY,
  164. .hdr_attr = ETHTOOL_A_PAUSE_HEADER,
  165. .req_info_size = sizeof(struct pause_req_info),
  166. .reply_data_size = sizeof(struct pause_reply_data),
  167. .parse_request = pause_parse_request,
  168. .prepare_data = pause_prepare_data,
  169. .reply_size = pause_reply_size,
  170. .fill_reply = pause_fill_reply,
  171. .set_validate = ethnl_set_pause_validate,
  172. .set = ethnl_set_pause,
  173. .set_ntf_cmd = ETHTOOL_MSG_PAUSE_NTF,
  174. };