coalesce.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include "netlink.h"
  3. #include "common.h"
  4. struct coalesce_req_info {
  5. struct ethnl_req_info base;
  6. };
  7. struct coalesce_reply_data {
  8. struct ethnl_reply_data base;
  9. struct ethtool_coalesce coalesce;
  10. struct kernel_ethtool_coalesce kernel_coalesce;
  11. u32 supported_params;
  12. };
  13. #define COALESCE_REPDATA(__reply_base) \
  14. container_of(__reply_base, struct coalesce_reply_data, base)
  15. #define __SUPPORTED_OFFSET ETHTOOL_A_COALESCE_RX_USECS
  16. static u32 attr_to_mask(unsigned int attr_type)
  17. {
  18. return BIT(attr_type - __SUPPORTED_OFFSET);
  19. }
  20. /* build time check that indices in ethtool_ops::supported_coalesce_params
  21. * match corresponding attribute types with an offset
  22. */
  23. #define __CHECK_SUPPORTED_OFFSET(x) \
  24. static_assert((ETHTOOL_ ## x) == \
  25. BIT((ETHTOOL_A_ ## x) - __SUPPORTED_OFFSET))
  26. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS);
  27. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES);
  28. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_IRQ);
  29. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_IRQ);
  30. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS);
  31. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES);
  32. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_IRQ);
  33. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_IRQ);
  34. __CHECK_SUPPORTED_OFFSET(COALESCE_STATS_BLOCK_USECS);
  35. __CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_RX);
  36. __CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_TX);
  37. __CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_LOW);
  38. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_LOW);
  39. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_LOW);
  40. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_LOW);
  41. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_LOW);
  42. __CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_HIGH);
  43. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_HIGH);
  44. __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_HIGH);
  45. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_HIGH);
  46. __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_HIGH);
  47. __CHECK_SUPPORTED_OFFSET(COALESCE_RATE_SAMPLE_INTERVAL);
  48. const struct nla_policy ethnl_coalesce_get_policy[] = {
  49. [ETHTOOL_A_COALESCE_HEADER] =
  50. NLA_POLICY_NESTED(ethnl_header_policy),
  51. };
  52. static int coalesce_prepare_data(const struct ethnl_req_info *req_base,
  53. struct ethnl_reply_data *reply_base,
  54. struct genl_info *info)
  55. {
  56. struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base);
  57. struct netlink_ext_ack *extack = info ? info->extack : NULL;
  58. struct net_device *dev = reply_base->dev;
  59. int ret;
  60. if (!dev->ethtool_ops->get_coalesce)
  61. return -EOPNOTSUPP;
  62. data->supported_params = dev->ethtool_ops->supported_coalesce_params;
  63. ret = ethnl_ops_begin(dev);
  64. if (ret < 0)
  65. return ret;
  66. ret = dev->ethtool_ops->get_coalesce(dev, &data->coalesce,
  67. &data->kernel_coalesce, extack);
  68. ethnl_ops_complete(dev);
  69. return ret;
  70. }
  71. static int coalesce_reply_size(const struct ethnl_req_info *req_base,
  72. const struct ethnl_reply_data *reply_base)
  73. {
  74. return nla_total_size(sizeof(u32)) + /* _RX_USECS */
  75. nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES */
  76. nla_total_size(sizeof(u32)) + /* _RX_USECS_IRQ */
  77. nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_IRQ */
  78. nla_total_size(sizeof(u32)) + /* _TX_USECS */
  79. nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES */
  80. nla_total_size(sizeof(u32)) + /* _TX_USECS_IRQ */
  81. nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_IRQ */
  82. nla_total_size(sizeof(u32)) + /* _STATS_BLOCK_USECS */
  83. nla_total_size(sizeof(u8)) + /* _USE_ADAPTIVE_RX */
  84. nla_total_size(sizeof(u8)) + /* _USE_ADAPTIVE_TX */
  85. nla_total_size(sizeof(u32)) + /* _PKT_RATE_LOW */
  86. nla_total_size(sizeof(u32)) + /* _RX_USECS_LOW */
  87. nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_LOW */
  88. nla_total_size(sizeof(u32)) + /* _TX_USECS_LOW */
  89. nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_LOW */
  90. nla_total_size(sizeof(u32)) + /* _PKT_RATE_HIGH */
  91. nla_total_size(sizeof(u32)) + /* _RX_USECS_HIGH */
  92. nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_HIGH */
  93. nla_total_size(sizeof(u32)) + /* _TX_USECS_HIGH */
  94. nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_HIGH */
  95. nla_total_size(sizeof(u32)) + /* _RATE_SAMPLE_INTERVAL */
  96. nla_total_size(sizeof(u8)) + /* _USE_CQE_MODE_TX */
  97. nla_total_size(sizeof(u8)); /* _USE_CQE_MODE_RX */
  98. }
  99. static bool coalesce_put_u32(struct sk_buff *skb, u16 attr_type, u32 val,
  100. u32 supported_params)
  101. {
  102. if (!val && !(supported_params & attr_to_mask(attr_type)))
  103. return false;
  104. return nla_put_u32(skb, attr_type, val);
  105. }
  106. static bool coalesce_put_bool(struct sk_buff *skb, u16 attr_type, u32 val,
  107. u32 supported_params)
  108. {
  109. if (!val && !(supported_params & attr_to_mask(attr_type)))
  110. return false;
  111. return nla_put_u8(skb, attr_type, !!val);
  112. }
  113. static int coalesce_fill_reply(struct sk_buff *skb,
  114. const struct ethnl_req_info *req_base,
  115. const struct ethnl_reply_data *reply_base)
  116. {
  117. const struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base);
  118. const struct kernel_ethtool_coalesce *kcoal = &data->kernel_coalesce;
  119. const struct ethtool_coalesce *coal = &data->coalesce;
  120. u32 supported = data->supported_params;
  121. if (coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS,
  122. coal->rx_coalesce_usecs, supported) ||
  123. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES,
  124. coal->rx_max_coalesced_frames, supported) ||
  125. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_IRQ,
  126. coal->rx_coalesce_usecs_irq, supported) ||
  127. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ,
  128. coal->rx_max_coalesced_frames_irq, supported) ||
  129. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS,
  130. coal->tx_coalesce_usecs, supported) ||
  131. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES,
  132. coal->tx_max_coalesced_frames, supported) ||
  133. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_IRQ,
  134. coal->tx_coalesce_usecs_irq, supported) ||
  135. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ,
  136. coal->tx_max_coalesced_frames_irq, supported) ||
  137. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_STATS_BLOCK_USECS,
  138. coal->stats_block_coalesce_usecs, supported) ||
  139. coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX,
  140. coal->use_adaptive_rx_coalesce, supported) ||
  141. coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX,
  142. coal->use_adaptive_tx_coalesce, supported) ||
  143. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_LOW,
  144. coal->pkt_rate_low, supported) ||
  145. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_LOW,
  146. coal->rx_coalesce_usecs_low, supported) ||
  147. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW,
  148. coal->rx_max_coalesced_frames_low, supported) ||
  149. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_LOW,
  150. coal->tx_coalesce_usecs_low, supported) ||
  151. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW,
  152. coal->tx_max_coalesced_frames_low, supported) ||
  153. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_HIGH,
  154. coal->pkt_rate_high, supported) ||
  155. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_HIGH,
  156. coal->rx_coalesce_usecs_high, supported) ||
  157. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH,
  158. coal->rx_max_coalesced_frames_high, supported) ||
  159. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_HIGH,
  160. coal->tx_coalesce_usecs_high, supported) ||
  161. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH,
  162. coal->tx_max_coalesced_frames_high, supported) ||
  163. coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL,
  164. coal->rate_sample_interval, supported) ||
  165. coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_TX,
  166. kcoal->use_cqe_mode_tx, supported) ||
  167. coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_RX,
  168. kcoal->use_cqe_mode_rx, supported))
  169. return -EMSGSIZE;
  170. return 0;
  171. }
  172. const struct ethnl_request_ops ethnl_coalesce_request_ops = {
  173. .request_cmd = ETHTOOL_MSG_COALESCE_GET,
  174. .reply_cmd = ETHTOOL_MSG_COALESCE_GET_REPLY,
  175. .hdr_attr = ETHTOOL_A_COALESCE_HEADER,
  176. .req_info_size = sizeof(struct coalesce_req_info),
  177. .reply_data_size = sizeof(struct coalesce_reply_data),
  178. .prepare_data = coalesce_prepare_data,
  179. .reply_size = coalesce_reply_size,
  180. .fill_reply = coalesce_fill_reply,
  181. };
  182. /* COALESCE_SET */
  183. const struct nla_policy ethnl_coalesce_set_policy[] = {
  184. [ETHTOOL_A_COALESCE_HEADER] =
  185. NLA_POLICY_NESTED(ethnl_header_policy),
  186. [ETHTOOL_A_COALESCE_RX_USECS] = { .type = NLA_U32 },
  187. [ETHTOOL_A_COALESCE_RX_MAX_FRAMES] = { .type = NLA_U32 },
  188. [ETHTOOL_A_COALESCE_RX_USECS_IRQ] = { .type = NLA_U32 },
  189. [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ] = { .type = NLA_U32 },
  190. [ETHTOOL_A_COALESCE_TX_USECS] = { .type = NLA_U32 },
  191. [ETHTOOL_A_COALESCE_TX_MAX_FRAMES] = { .type = NLA_U32 },
  192. [ETHTOOL_A_COALESCE_TX_USECS_IRQ] = { .type = NLA_U32 },
  193. [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ] = { .type = NLA_U32 },
  194. [ETHTOOL_A_COALESCE_STATS_BLOCK_USECS] = { .type = NLA_U32 },
  195. [ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX] = { .type = NLA_U8 },
  196. [ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX] = { .type = NLA_U8 },
  197. [ETHTOOL_A_COALESCE_PKT_RATE_LOW] = { .type = NLA_U32 },
  198. [ETHTOOL_A_COALESCE_RX_USECS_LOW] = { .type = NLA_U32 },
  199. [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW] = { .type = NLA_U32 },
  200. [ETHTOOL_A_COALESCE_TX_USECS_LOW] = { .type = NLA_U32 },
  201. [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW] = { .type = NLA_U32 },
  202. [ETHTOOL_A_COALESCE_PKT_RATE_HIGH] = { .type = NLA_U32 },
  203. [ETHTOOL_A_COALESCE_RX_USECS_HIGH] = { .type = NLA_U32 },
  204. [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH] = { .type = NLA_U32 },
  205. [ETHTOOL_A_COALESCE_TX_USECS_HIGH] = { .type = NLA_U32 },
  206. [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH] = { .type = NLA_U32 },
  207. [ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL] = { .type = NLA_U32 },
  208. [ETHTOOL_A_COALESCE_USE_CQE_MODE_TX] = NLA_POLICY_MAX(NLA_U8, 1),
  209. [ETHTOOL_A_COALESCE_USE_CQE_MODE_RX] = NLA_POLICY_MAX(NLA_U8, 1),
  210. };
  211. int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info)
  212. {
  213. struct kernel_ethtool_coalesce kernel_coalesce = {};
  214. struct ethtool_coalesce coalesce = {};
  215. struct ethnl_req_info req_info = {};
  216. struct nlattr **tb = info->attrs;
  217. const struct ethtool_ops *ops;
  218. struct net_device *dev;
  219. u32 supported_params;
  220. bool mod = false;
  221. int ret;
  222. u16 a;
  223. ret = ethnl_parse_header_dev_get(&req_info,
  224. tb[ETHTOOL_A_COALESCE_HEADER],
  225. genl_info_net(info), info->extack,
  226. true);
  227. if (ret < 0)
  228. return ret;
  229. dev = req_info.dev;
  230. ops = dev->ethtool_ops;
  231. ret = -EOPNOTSUPP;
  232. if (!ops->get_coalesce || !ops->set_coalesce)
  233. goto out_dev;
  234. /* make sure that only supported parameters are present */
  235. supported_params = ops->supported_coalesce_params;
  236. for (a = ETHTOOL_A_COALESCE_RX_USECS; a < __ETHTOOL_A_COALESCE_CNT; a++)
  237. if (tb[a] && !(supported_params & attr_to_mask(a))) {
  238. ret = -EINVAL;
  239. NL_SET_ERR_MSG_ATTR(info->extack, tb[a],
  240. "cannot modify an unsupported parameter");
  241. goto out_dev;
  242. }
  243. rtnl_lock();
  244. ret = ethnl_ops_begin(dev);
  245. if (ret < 0)
  246. goto out_rtnl;
  247. ret = ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
  248. info->extack);
  249. if (ret < 0)
  250. goto out_ops;
  251. ethnl_update_u32(&coalesce.rx_coalesce_usecs,
  252. tb[ETHTOOL_A_COALESCE_RX_USECS], &mod);
  253. ethnl_update_u32(&coalesce.rx_max_coalesced_frames,
  254. tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES], &mod);
  255. ethnl_update_u32(&coalesce.rx_coalesce_usecs_irq,
  256. tb[ETHTOOL_A_COALESCE_RX_USECS_IRQ], &mod);
  257. ethnl_update_u32(&coalesce.rx_max_coalesced_frames_irq,
  258. tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ], &mod);
  259. ethnl_update_u32(&coalesce.tx_coalesce_usecs,
  260. tb[ETHTOOL_A_COALESCE_TX_USECS], &mod);
  261. ethnl_update_u32(&coalesce.tx_max_coalesced_frames,
  262. tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES], &mod);
  263. ethnl_update_u32(&coalesce.tx_coalesce_usecs_irq,
  264. tb[ETHTOOL_A_COALESCE_TX_USECS_IRQ], &mod);
  265. ethnl_update_u32(&coalesce.tx_max_coalesced_frames_irq,
  266. tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ], &mod);
  267. ethnl_update_u32(&coalesce.stats_block_coalesce_usecs,
  268. tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS], &mod);
  269. ethnl_update_bool32(&coalesce.use_adaptive_rx_coalesce,
  270. tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX], &mod);
  271. ethnl_update_bool32(&coalesce.use_adaptive_tx_coalesce,
  272. tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX], &mod);
  273. ethnl_update_u32(&coalesce.pkt_rate_low,
  274. tb[ETHTOOL_A_COALESCE_PKT_RATE_LOW], &mod);
  275. ethnl_update_u32(&coalesce.rx_coalesce_usecs_low,
  276. tb[ETHTOOL_A_COALESCE_RX_USECS_LOW], &mod);
  277. ethnl_update_u32(&coalesce.rx_max_coalesced_frames_low,
  278. tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW], &mod);
  279. ethnl_update_u32(&coalesce.tx_coalesce_usecs_low,
  280. tb[ETHTOOL_A_COALESCE_TX_USECS_LOW], &mod);
  281. ethnl_update_u32(&coalesce.tx_max_coalesced_frames_low,
  282. tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW], &mod);
  283. ethnl_update_u32(&coalesce.pkt_rate_high,
  284. tb[ETHTOOL_A_COALESCE_PKT_RATE_HIGH], &mod);
  285. ethnl_update_u32(&coalesce.rx_coalesce_usecs_high,
  286. tb[ETHTOOL_A_COALESCE_RX_USECS_HIGH], &mod);
  287. ethnl_update_u32(&coalesce.rx_max_coalesced_frames_high,
  288. tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH], &mod);
  289. ethnl_update_u32(&coalesce.tx_coalesce_usecs_high,
  290. tb[ETHTOOL_A_COALESCE_TX_USECS_HIGH], &mod);
  291. ethnl_update_u32(&coalesce.tx_max_coalesced_frames_high,
  292. tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH], &mod);
  293. ethnl_update_u32(&coalesce.rate_sample_interval,
  294. tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL], &mod);
  295. ethnl_update_u8(&kernel_coalesce.use_cqe_mode_tx,
  296. tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_TX], &mod);
  297. ethnl_update_u8(&kernel_coalesce.use_cqe_mode_rx,
  298. tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_RX], &mod);
  299. ret = 0;
  300. if (!mod)
  301. goto out_ops;
  302. ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
  303. info->extack);
  304. if (ret < 0)
  305. goto out_ops;
  306. ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
  307. out_ops:
  308. ethnl_ops_complete(dev);
  309. out_rtnl:
  310. rtnl_unlock();
  311. out_dev:
  312. ethnl_parse_header_dev_put(&req_info);
  313. return ret;
  314. }