channels.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. 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. const struct ethnl_request_ops ethnl_channels_request_ops = {
  76. .request_cmd = ETHTOOL_MSG_CHANNELS_GET,
  77. .reply_cmd = ETHTOOL_MSG_CHANNELS_GET_REPLY,
  78. .hdr_attr = ETHTOOL_A_CHANNELS_HEADER,
  79. .req_info_size = sizeof(struct channels_req_info),
  80. .reply_data_size = sizeof(struct channels_reply_data),
  81. .prepare_data = channels_prepare_data,
  82. .reply_size = channels_reply_size,
  83. .fill_reply = channels_fill_reply,
  84. };
  85. /* CHANNELS_SET */
  86. const struct nla_policy ethnl_channels_set_policy[] = {
  87. [ETHTOOL_A_CHANNELS_HEADER] =
  88. NLA_POLICY_NESTED(ethnl_header_policy),
  89. [ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 },
  90. [ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 },
  91. [ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 },
  92. [ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_U32 },
  93. };
  94. int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info)
  95. {
  96. unsigned int from_channel, old_total, i;
  97. bool mod = false, mod_combined = false;
  98. struct ethtool_channels channels = {};
  99. struct ethnl_req_info req_info = {};
  100. struct nlattr **tb = info->attrs;
  101. u32 err_attr, max_rx_in_use = 0;
  102. const struct ethtool_ops *ops;
  103. struct net_device *dev;
  104. int ret;
  105. ret = ethnl_parse_header_dev_get(&req_info,
  106. tb[ETHTOOL_A_CHANNELS_HEADER],
  107. genl_info_net(info), info->extack,
  108. true);
  109. if (ret < 0)
  110. return ret;
  111. dev = req_info.dev;
  112. ops = dev->ethtool_ops;
  113. ret = -EOPNOTSUPP;
  114. if (!ops->get_channels || !ops->set_channels)
  115. goto out_dev;
  116. rtnl_lock();
  117. ret = ethnl_ops_begin(dev);
  118. if (ret < 0)
  119. goto out_rtnl;
  120. ops->get_channels(dev, &channels);
  121. old_total = channels.combined_count +
  122. max(channels.rx_count, channels.tx_count);
  123. ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT],
  124. &mod);
  125. ethnl_update_u32(&channels.tx_count, tb[ETHTOOL_A_CHANNELS_TX_COUNT],
  126. &mod);
  127. ethnl_update_u32(&channels.other_count,
  128. tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], &mod);
  129. ethnl_update_u32(&channels.combined_count,
  130. tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], &mod_combined);
  131. mod |= mod_combined;
  132. ret = 0;
  133. if (!mod)
  134. goto out_ops;
  135. /* ensure new channel counts are within limits */
  136. if (channels.rx_count > channels.max_rx)
  137. err_attr = ETHTOOL_A_CHANNELS_RX_COUNT;
  138. else if (channels.tx_count > channels.max_tx)
  139. err_attr = ETHTOOL_A_CHANNELS_TX_COUNT;
  140. else if (channels.other_count > channels.max_other)
  141. err_attr = ETHTOOL_A_CHANNELS_OTHER_COUNT;
  142. else if (channels.combined_count > channels.max_combined)
  143. err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT;
  144. else
  145. err_attr = 0;
  146. if (err_attr) {
  147. ret = -EINVAL;
  148. NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr],
  149. "requested channel count exceeds maximum");
  150. goto out_ops;
  151. }
  152. /* ensure there is at least one RX and one TX channel */
  153. if (!channels.combined_count && !channels.rx_count)
  154. err_attr = ETHTOOL_A_CHANNELS_RX_COUNT;
  155. else if (!channels.combined_count && !channels.tx_count)
  156. err_attr = ETHTOOL_A_CHANNELS_TX_COUNT;
  157. else
  158. err_attr = 0;
  159. if (err_attr) {
  160. if (mod_combined)
  161. err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT;
  162. ret = -EINVAL;
  163. NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr],
  164. "requested channel counts would result in no RX or TX channel being configured");
  165. goto out_ops;
  166. }
  167. /* ensure the new Rx count fits within the configured Rx flow
  168. * indirection table settings
  169. */
  170. if (netif_is_rxfh_configured(dev) &&
  171. !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
  172. (channels.combined_count + channels.rx_count) <= max_rx_in_use) {
  173. ret = -EINVAL;
  174. GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing indirection table settings");
  175. goto out_ops;
  176. }
  177. /* Disabling channels, query zero-copy AF_XDP sockets */
  178. from_channel = channels.combined_count +
  179. min(channels.rx_count, channels.tx_count);
  180. for (i = from_channel; i < old_total; i++)
  181. if (xsk_get_pool_from_qid(dev, i)) {
  182. ret = -EINVAL;
  183. GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets");
  184. goto out_ops;
  185. }
  186. ret = dev->ethtool_ops->set_channels(dev, &channels);
  187. if (ret < 0)
  188. goto out_ops;
  189. ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
  190. out_ops:
  191. ethnl_ops_complete(dev);
  192. out_rtnl:
  193. rtnl_unlock();
  194. out_dev:
  195. dev_put(dev);
  196. return ret;
  197. }