netdev_queues.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_NET_QUEUES_H
  3. #define _LINUX_NET_QUEUES_H
  4. #include <linux/netdevice.h>
  5. /* See the netdev.yaml spec for definition of each statistic */
  6. struct netdev_queue_stats_rx {
  7. u64 bytes;
  8. u64 packets;
  9. u64 alloc_fail;
  10. u64 hw_drops;
  11. u64 hw_drop_overruns;
  12. u64 csum_unnecessary;
  13. u64 csum_none;
  14. u64 csum_bad;
  15. u64 hw_gro_packets;
  16. u64 hw_gro_bytes;
  17. u64 hw_gro_wire_packets;
  18. u64 hw_gro_wire_bytes;
  19. u64 hw_drop_ratelimits;
  20. };
  21. struct netdev_queue_stats_tx {
  22. u64 bytes;
  23. u64 packets;
  24. u64 hw_drops;
  25. u64 hw_drop_errors;
  26. u64 csum_none;
  27. u64 needs_csum;
  28. u64 hw_gso_packets;
  29. u64 hw_gso_bytes;
  30. u64 hw_gso_wire_packets;
  31. u64 hw_gso_wire_bytes;
  32. u64 hw_drop_ratelimits;
  33. u64 stop;
  34. u64 wake;
  35. };
  36. /**
  37. * struct netdev_stat_ops - netdev ops for fine grained stats
  38. * @get_queue_stats_rx: get stats for a given Rx queue
  39. * @get_queue_stats_tx: get stats for a given Tx queue
  40. * @get_base_stats: get base stats (not belonging to any live instance)
  41. *
  42. * Query stats for a given object. The values of the statistics are undefined
  43. * on entry (specifically they are *not* zero-initialized). Drivers should
  44. * assign values only to the statistics they collect. Statistics which are not
  45. * collected must be left undefined.
  46. *
  47. * Queue objects are not necessarily persistent, and only currently active
  48. * queues are queried by the per-queue callbacks. This means that per-queue
  49. * statistics will not generally add up to the total number of events for
  50. * the device. The @get_base_stats callback allows filling in the delta
  51. * between events for currently live queues and overall device history.
  52. * @get_base_stats can also be used to report any miscellaneous packets
  53. * transferred outside of the main set of queues used by the networking stack.
  54. * When the statistics for the entire device are queried, first @get_base_stats
  55. * is issued to collect the delta, and then a series of per-queue callbacks.
  56. * Only statistics which are set in @get_base_stats will be reported
  57. * at the device level, meaning that unlike in queue callbacks, setting
  58. * a statistic to zero in @get_base_stats is a legitimate thing to do.
  59. * This is because @get_base_stats has a second function of designating which
  60. * statistics are in fact correct for the entire device (e.g. when history
  61. * for some of the events is not maintained, and reliable "total" cannot
  62. * be provided).
  63. *
  64. * Device drivers can assume that when collecting total device stats,
  65. * the @get_base_stats and subsequent per-queue calls are performed
  66. * "atomically" (without releasing the rtnl_lock).
  67. *
  68. * Device drivers are encouraged to reset the per-queue statistics when
  69. * number of queues change. This is because the primary use case for
  70. * per-queue statistics is currently to detect traffic imbalance.
  71. */
  72. struct netdev_stat_ops {
  73. void (*get_queue_stats_rx)(struct net_device *dev, int idx,
  74. struct netdev_queue_stats_rx *stats);
  75. void (*get_queue_stats_tx)(struct net_device *dev, int idx,
  76. struct netdev_queue_stats_tx *stats);
  77. void (*get_base_stats)(struct net_device *dev,
  78. struct netdev_queue_stats_rx *rx,
  79. struct netdev_queue_stats_tx *tx);
  80. };
  81. void netdev_stat_queue_sum(struct net_device *netdev,
  82. int rx_start, int rx_end,
  83. struct netdev_queue_stats_rx *rx_sum,
  84. int tx_start, int tx_end,
  85. struct netdev_queue_stats_tx *tx_sum);
  86. /**
  87. * struct netdev_queue_mgmt_ops - netdev ops for queue management
  88. *
  89. * @ndo_queue_mem_size: Size of the struct that describes a queue's memory.
  90. *
  91. * @ndo_queue_mem_alloc: Allocate memory for an RX queue at the specified index.
  92. * The new memory is written at the specified address.
  93. *
  94. * @ndo_queue_mem_free: Free memory from an RX queue.
  95. *
  96. * @ndo_queue_start: Start an RX queue with the specified memory and at the
  97. * specified index.
  98. *
  99. * @ndo_queue_stop: Stop the RX queue at the specified index. The stopped
  100. * queue's memory is written at the specified address.
  101. */
  102. struct netdev_queue_mgmt_ops {
  103. size_t ndo_queue_mem_size;
  104. int (*ndo_queue_mem_alloc)(struct net_device *dev,
  105. void *per_queue_mem,
  106. int idx);
  107. void (*ndo_queue_mem_free)(struct net_device *dev,
  108. void *per_queue_mem);
  109. int (*ndo_queue_start)(struct net_device *dev,
  110. void *per_queue_mem,
  111. int idx);
  112. int (*ndo_queue_stop)(struct net_device *dev,
  113. void *per_queue_mem,
  114. int idx);
  115. };
  116. /**
  117. * DOC: Lockless queue stopping / waking helpers.
  118. *
  119. * The netif_txq_maybe_stop() and __netif_txq_completed_wake()
  120. * macros are designed to safely implement stopping
  121. * and waking netdev queues without full lock protection.
  122. *
  123. * We assume that there can be no concurrent stop attempts and no concurrent
  124. * wake attempts. The try-stop should happen from the xmit handler,
  125. * while wake up should be triggered from NAPI poll context.
  126. * The two may run concurrently (single producer, single consumer).
  127. *
  128. * The try-stop side is expected to run from the xmit handler and therefore
  129. * it does not reschedule Tx (netif_tx_start_queue() instead of
  130. * netif_tx_wake_queue()). Uses of the ``stop`` macros outside of the xmit
  131. * handler may lead to xmit queue being enabled but not run.
  132. * The waking side does not have similar context restrictions.
  133. *
  134. * The macros guarantee that rings will not remain stopped if there's
  135. * space available, but they do *not* prevent false wake ups when
  136. * the ring is full! Drivers should check for ring full at the start
  137. * for the xmit handler.
  138. *
  139. * All descriptor ring indexes (and other relevant shared state) must
  140. * be updated before invoking the macros.
  141. */
  142. #define netif_txq_try_stop(txq, get_desc, start_thrs) \
  143. ({ \
  144. int _res; \
  145. \
  146. netif_tx_stop_queue(txq); \
  147. /* Producer index and stop bit must be visible \
  148. * to consumer before we recheck. \
  149. * Pairs with a barrier in __netif_txq_completed_wake(). \
  150. */ \
  151. smp_mb__after_atomic(); \
  152. \
  153. /* We need to check again in a case another \
  154. * CPU has just made room available. \
  155. */ \
  156. _res = 0; \
  157. if (unlikely(get_desc >= start_thrs)) { \
  158. netif_tx_start_queue(txq); \
  159. _res = -1; \
  160. } \
  161. _res; \
  162. }) \
  163. /**
  164. * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
  165. * @txq: struct netdev_queue to stop/start
  166. * @get_desc: get current number of free descriptors (see requirements below!)
  167. * @stop_thrs: minimal number of available descriptors for queue to be left
  168. * enabled
  169. * @start_thrs: minimal number of descriptors to re-enable the queue, can be
  170. * equal to @stop_thrs or higher to avoid frequent waking
  171. *
  172. * All arguments may be evaluated multiple times, beware of side effects.
  173. * @get_desc must be a formula or a function call, it must always
  174. * return up-to-date information when evaluated!
  175. * Expected to be used from ndo_start_xmit, see the comment on top of the file.
  176. *
  177. * Returns:
  178. * 0 if the queue was stopped
  179. * 1 if the queue was left enabled
  180. * -1 if the queue was re-enabled (raced with waking)
  181. */
  182. #define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs) \
  183. ({ \
  184. int _res; \
  185. \
  186. _res = 1; \
  187. if (unlikely(get_desc < stop_thrs)) \
  188. _res = netif_txq_try_stop(txq, get_desc, start_thrs); \
  189. _res; \
  190. }) \
  191. /* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
  192. * @bytes != 0, regardless of kernel config.
  193. */
  194. static inline void
  195. netdev_txq_completed_mb(struct netdev_queue *dev_queue,
  196. unsigned int pkts, unsigned int bytes)
  197. {
  198. if (IS_ENABLED(CONFIG_BQL))
  199. netdev_tx_completed_queue(dev_queue, pkts, bytes);
  200. else if (bytes)
  201. smp_mb();
  202. }
  203. /**
  204. * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
  205. * @txq: struct netdev_queue to stop/start
  206. * @pkts: number of packets completed
  207. * @bytes: number of bytes completed
  208. * @get_desc: get current number of free descriptors (see requirements below!)
  209. * @start_thrs: minimal number of descriptors to re-enable the queue
  210. * @down_cond: down condition, predicate indicating that the queue should
  211. * not be woken up even if descriptors are available
  212. *
  213. * All arguments may be evaluated multiple times.
  214. * @get_desc must be a formula or a function call, it must always
  215. * return up-to-date information when evaluated!
  216. * Reports completed pkts/bytes to BQL.
  217. *
  218. * Returns:
  219. * 0 if the queue was woken up
  220. * 1 if the queue was already enabled (or disabled but @down_cond is true)
  221. * -1 if the queue was left unchanged (@start_thrs not reached)
  222. */
  223. #define __netif_txq_completed_wake(txq, pkts, bytes, \
  224. get_desc, start_thrs, down_cond) \
  225. ({ \
  226. int _res; \
  227. \
  228. /* Report to BQL and piggy back on its barrier. \
  229. * Barrier makes sure that anybody stopping the queue \
  230. * after this point sees the new consumer index. \
  231. * Pairs with barrier in netif_txq_try_stop(). \
  232. */ \
  233. netdev_txq_completed_mb(txq, pkts, bytes); \
  234. \
  235. _res = -1; \
  236. if (pkts && likely(get_desc >= start_thrs)) { \
  237. _res = 1; \
  238. if (unlikely(netif_tx_queue_stopped(txq)) && \
  239. !(down_cond)) { \
  240. netif_tx_wake_queue(txq); \
  241. _res = 0; \
  242. } \
  243. } \
  244. _res; \
  245. })
  246. #define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
  247. __netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)
  248. /* subqueue variants follow */
  249. #define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs) \
  250. ({ \
  251. struct netdev_queue *txq; \
  252. \
  253. txq = netdev_get_tx_queue(dev, idx); \
  254. netif_txq_try_stop(txq, get_desc, start_thrs); \
  255. })
  256. #define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
  257. ({ \
  258. struct netdev_queue *txq; \
  259. \
  260. txq = netdev_get_tx_queue(dev, idx); \
  261. netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs); \
  262. })
  263. #define netif_subqueue_completed_wake(dev, idx, pkts, bytes, \
  264. get_desc, start_thrs) \
  265. ({ \
  266. struct netdev_queue *txq; \
  267. \
  268. txq = netdev_get_tx_queue(dev, idx); \
  269. netif_txq_completed_wake(txq, pkts, bytes, \
  270. get_desc, start_thrs); \
  271. })
  272. #endif