channels.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/xdp_sock_drv.h>
  3. #include "netlink.h"
  4. #include "common.h"
  5. struct channels_req_info {
  6. struct ethnl_req_info base;
  7. };
  8. struct channels_reply_data {
  9. struct ethnl_reply_data base;
  10. struct ethtool_channels channels;
  11. };
  12. #define CHANNELS_REPDATA(__reply_base) \
  13. container_of(__reply_base, struct channels_reply_data, base)
  14. const struct nla_policy ethnl_channels_get_policy[] = {
  15. [ETHTOOL_A_CHANNELS_HEADER] =
  16. NLA_POLICY_NESTED(ethnl_header_policy),
  17. };
  18. static int channels_prepare_data(const struct ethnl_req_info *req_base,
  19. struct ethnl_reply_data *reply_base,
  20. const struct genl_info *info)
  21. {
  22. struct channels_reply_data *data = CHANNELS_REPDATA(reply_base);
  23. struct net_device *dev = reply_base->dev;
  24. int ret;
  25. if (!dev->ethtool_ops->get_channels)
  26. return -EOPNOTSUPP;
  27. ret = ethnl_ops_begin(dev);
  28. if (ret < 0)
  29. return ret;
  30. dev->ethtool_ops->get_channels(dev, &data->channels);
  31. ethnl_ops_complete(dev);
  32. return 0;
  33. }
  34. static int channels_reply_size(const struct ethnl_req_info *req_base,
  35. const struct ethnl_reply_data *reply_base)
  36. {
  37. return nla_total_size(sizeof(u32)) + /* _CHANNELS_RX_MAX */
  38. nla_total_size(sizeof(u32)) + /* _CHANNELS_TX_MAX */
  39. nla_total_size(sizeof(u32)) + /* _CHANNELS_OTHER_MAX */
  40. nla_total_size(sizeof(u32)) + /* _CHANNELS_COMBINED_MAX */
  41. nla_total_size(sizeof(u32)) + /* _CHANNELS_RX_COUNT */
  42. nla_total_size(sizeof(u32)) + /* _CHANNELS_TX_COUNT */
  43. nla_total_size(sizeof(u32)) + /* _CHANNELS_OTHER_COUNT */
  44. nla_total_size(sizeof(u32)); /* _CHANNELS_COMBINED_COUNT */
  45. }
  46. static int channels_fill_reply(struct sk_buff *skb,
  47. const struct ethnl_req_info *req_base,
  48. const struct ethnl_reply_data *reply_base)
  49. {
  50. const struct channels_reply_data *data = CHANNELS_REPDATA(reply_base);
  51. const struct ethtool_channels *channels = &data->channels;
  52. if ((channels->max_rx &&
  53. (nla_put_u32(skb, ETHTOOL_A_CHANNELS_RX_MAX,
  54. channels->max_rx) ||
  55. nla_put_u32(skb, ETHTOOL_A_CHANNELS_RX_COUNT,
  56. channels->rx_count))) ||
  57. (channels->max_tx &&
  58. (nla_put_u32(skb, ETHTOOL_A_CHANNELS_TX_MAX,
  59. channels->max_tx) ||
  60. nla_put_u32(skb, ETHTOOL_A_CHANNELS_TX_COUNT,
  61. channels->tx_count))) ||
  62. (channels->max_other &&
  63. (nla_put_u32(skb, ETHTOOL_A_CHANNELS_OTHER_MAX,
  64. channels->max_other) ||
  65. nla_put_u32(skb, ETHTOOL_A_CHANNELS_OTHER_COUNT,
  66. channels->other_count))) ||
  67. (channels->max_combined &&
  68. (nla_put_u32(skb, ETHTOOL_A_CHANNELS_COMBINED_MAX,
  69. channels->max_combined) ||
  70. nla_put_u32(skb, ETHTOOL_A_CHANNELS_COMBINED_COUNT,
  71. channels->combined_count))))
  72. return -EMSGSIZE;
  73. return 0;
  74. }
  75. /* CHANNELS_SET */
  76. const struct nla_policy ethnl_channels_set_policy[] = {
  77. [ETHTOOL_A_CHANNELS_HEADER] =
  78. NLA_POLICY_NESTED(ethnl_header_policy),
  79. [ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 },
  80. [ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 },
  81. [ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 },
  82. [ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_U32 },
  83. };
  84. static int
  85. ethnl_set_channels_validate(struct ethnl_req_info *req_info,
  86. struct genl_info *info)
  87. {
  88. const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
  89. return ops->get_channels && ops->set_channels ? 1 : -EOPNOTSUPP;
  90. }
  91. static int
  92. ethnl_set_channels(struct ethnl_req_info *req_info, struct genl_info *info)
  93. {
  94. unsigned int from_channel, old_total, i;
  95. bool mod = false, mod_combined = false;
  96. struct net_device *dev = req_info->dev;
  97. struct ethtool_channels channels = {};
  98. struct nlattr **tb = info->attrs;
  99. u32 err_attr;
  100. int ret;
  101. dev->ethtool_ops->get_channels(dev, &channels);
  102. old_total = channels.combined_count +
  103. max(channels.rx_count, channels.tx_count);
  104. ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT],
  105. &mod);
  106. ethnl_update_u32(&channels.tx_count, tb[ETHTOOL_A_CHANNELS_TX_COUNT],
  107. &mod);
  108. ethnl_update_u32(&channels.other_count,
  109. tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], &mod);
  110. ethnl_update_u32(&channels.combined_count,
  111. tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], &mod_combined);
  112. mod |= mod_combined;
  113. if (!mod)
  114. return 0;
  115. /* ensure new channel counts are within limits */
  116. if (channels.rx_count > channels.max_rx)
  117. err_attr = ETHTOOL_A_CHANNELS_RX_COUNT;
  118. else if (channels.tx_count > channels.max_tx)
  119. err_attr = ETHTOOL_A_CHANNELS_TX_COUNT;
  120. else if (channels.other_count > channels.max_other)
  121. err_attr = ETHTOOL_A_CHANNELS_OTHER_COUNT;
  122. else if (channels.combined_count > channels.max_combined)
  123. err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT;
  124. else
  125. err_attr = 0;
  126. if (err_attr) {
  127. NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr],
  128. "requested channel count exceeds maximum");
  129. return -EINVAL;
  130. }
  131. /* ensure there is at least one RX and one TX channel */
  132. if (!channels.combined_count && !channels.rx_count)
  133. err_attr = ETHTOOL_A_CHANNELS_RX_COUNT;
  134. else if (!channels.combined_count && !channels.tx_count)
  135. err_attr = ETHTOOL_A_CHANNELS_TX_COUNT;
  136. else
  137. err_attr = 0;
  138. if (err_attr) {
  139. if (mod_combined)
  140. err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT;
  141. NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr],
  142. "requested channel counts would result in no RX or TX channel being configured");
  143. return -EINVAL;
  144. }
  145. ret = ethtool_check_max_channel(dev, channels, info);
  146. if (ret)
  147. return ret;
  148. /* Disabling channels, query zero-copy AF_XDP sockets */
  149. from_channel = channels.combined_count +
  150. min(channels.rx_count, channels.tx_count);
  151. for (i = from_channel; i < old_total; i++)
  152. if (xsk_get_pool_from_qid(dev, i)) {
  153. GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets");
  154. return -EINVAL;
  155. }
  156. ret = dev->ethtool_ops->set_channels(dev, &channels);
  157. return ret < 0 ? ret : 1;
  158. }
  159. const struct ethnl_request_ops ethnl_channels_request_ops = {
  160. .request_cmd = ETHTOOL_MSG_CHANNELS_GET,
  161. .reply_cmd = ETHTOOL_MSG_CHANNELS_GET_REPLY,
  162. .hdr_attr = ETHTOOL_A_CHANNELS_HEADER,
  163. .req_info_size = sizeof(struct channels_req_info),
  164. .reply_data_size = sizeof(struct channels_reply_data),
  165. .prepare_data = channels_prepare_data,
  166. .reply_size = channels_reply_size,
  167. .fill_reply = channels_fill_reply,
  168. .set_validate = ethnl_set_channels_validate,
  169. .set = ethnl_set_channels,
  170. .set_ntf_cmd = ETHTOOL_MSG_CHANNELS_NTF,
  171. };