netdev_queues.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /**
  6. * struct netdev_config - queue-related configuration for a netdev
  7. * @hds_thresh: HDS Threshold value.
  8. * @hds_config: HDS value from userspace.
  9. */
  10. struct netdev_config {
  11. u32 hds_thresh;
  12. u8 hds_config;
  13. };
  14. struct netdev_queue_config {
  15. u32 rx_page_size;
  16. };
  17. /* See the netdev.yaml spec for definition of each statistic */
  18. struct netdev_queue_stats_rx {
  19. u64 bytes;
  20. u64 packets;
  21. u64 alloc_fail;
  22. u64 hw_drops;
  23. u64 hw_drop_overruns;
  24. u64 csum_complete;
  25. u64 csum_unnecessary;
  26. u64 csum_none;
  27. u64 csum_bad;
  28. u64 hw_gro_packets;
  29. u64 hw_gro_bytes;
  30. u64 hw_gro_wire_packets;
  31. u64 hw_gro_wire_bytes;
  32. u64 hw_drop_ratelimits;
  33. };
  34. struct netdev_queue_stats_tx {
  35. u64 bytes;
  36. u64 packets;
  37. u64 hw_drops;
  38. u64 hw_drop_errors;
  39. u64 csum_none;
  40. u64 needs_csum;
  41. u64 hw_gso_packets;
  42. u64 hw_gso_bytes;
  43. u64 hw_gso_wire_packets;
  44. u64 hw_gso_wire_bytes;
  45. u64 hw_drop_ratelimits;
  46. u64 stop;
  47. u64 wake;
  48. };
  49. /**
  50. * struct netdev_stat_ops - netdev ops for fine grained stats
  51. * @get_queue_stats_rx: get stats for a given Rx queue
  52. * @get_queue_stats_tx: get stats for a given Tx queue
  53. * @get_base_stats: get base stats (not belonging to any live instance)
  54. *
  55. * Query stats for a given object. The values of the statistics are undefined
  56. * on entry (specifically they are *not* zero-initialized). Drivers should
  57. * assign values only to the statistics they collect. Statistics which are not
  58. * collected must be left undefined.
  59. *
  60. * Queue objects are not necessarily persistent, and only currently active
  61. * queues are queried by the per-queue callbacks. This means that per-queue
  62. * statistics will not generally add up to the total number of events for
  63. * the device. The @get_base_stats callback allows filling in the delta
  64. * between events for currently live queues and overall device history.
  65. * @get_base_stats can also be used to report any miscellaneous packets
  66. * transferred outside of the main set of queues used by the networking stack.
  67. * When the statistics for the entire device are queried, first @get_base_stats
  68. * is issued to collect the delta, and then a series of per-queue callbacks.
  69. * Only statistics which are set in @get_base_stats will be reported
  70. * at the device level, meaning that unlike in queue callbacks, setting
  71. * a statistic to zero in @get_base_stats is a legitimate thing to do.
  72. * This is because @get_base_stats has a second function of designating which
  73. * statistics are in fact correct for the entire device (e.g. when history
  74. * for some of the events is not maintained, and reliable "total" cannot
  75. * be provided).
  76. *
  77. * Ops are called under the instance lock if netdev_need_ops_lock()
  78. * returns true, otherwise under rtnl_lock.
  79. * Device drivers can assume that when collecting total device stats,
  80. * the @get_base_stats and subsequent per-queue calls are performed
  81. * "atomically" (without releasing the relevant lock).
  82. *
  83. * Device drivers are encouraged to reset the per-queue statistics when
  84. * number of queues change. This is because the primary use case for
  85. * per-queue statistics is currently to detect traffic imbalance.
  86. */
  87. struct netdev_stat_ops {
  88. void (*get_queue_stats_rx)(struct net_device *dev, int idx,
  89. struct netdev_queue_stats_rx *stats);
  90. void (*get_queue_stats_tx)(struct net_device *dev, int idx,
  91. struct netdev_queue_stats_tx *stats);
  92. void (*get_base_stats)(struct net_device *dev,
  93. struct netdev_queue_stats_rx *rx,
  94. struct netdev_queue_stats_tx *tx);
  95. };
  96. void netdev_stat_queue_sum(struct net_device *netdev,
  97. int rx_start, int rx_end,
  98. struct netdev_queue_stats_rx *rx_sum,
  99. int tx_start, int tx_end,
  100. struct netdev_queue_stats_tx *tx_sum);
  101. enum {
  102. /* The queue checks and honours the page size qcfg parameter */
  103. QCFG_RX_PAGE_SIZE = 0x1,
  104. };
  105. /**
  106. * struct netdev_queue_mgmt_ops - netdev ops for queue management
  107. *
  108. * @ndo_queue_mem_size: Size of the struct that describes a queue's memory.
  109. *
  110. * @ndo_queue_mem_alloc: Allocate memory for an RX queue at the specified index.
  111. * The new memory is written at the specified address.
  112. *
  113. * @ndo_queue_mem_free: Free memory from an RX queue.
  114. *
  115. * @ndo_queue_start: Start an RX queue with the specified memory and at the
  116. * specified index.
  117. *
  118. * @ndo_queue_stop: Stop the RX queue at the specified index. The stopped
  119. * queue's memory is written at the specified address.
  120. *
  121. * @ndo_queue_get_dma_dev: Get dma device for zero-copy operations to be used
  122. * for this queue. Return NULL on error.
  123. *
  124. * @ndo_default_qcfg: (Optional) Populate queue config struct with defaults.
  125. * Queue config structs are passed to this helper before
  126. * the user-requested settings are applied.
  127. *
  128. * @ndo_validate_qcfg: (Optional) Check if queue config is supported.
  129. * Called when configuration affecting a queue may be
  130. * changing, either due to NIC-wide config, or config
  131. * scoped to the queue at a specified index.
  132. * When NIC-wide config is changed the callback will
  133. * be invoked for all queues.
  134. *
  135. * @supported_params: Bitmask of supported parameters, see QCFG_*.
  136. *
  137. * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
  138. * the interface is closed. @ndo_queue_start and @ndo_queue_stop will only
  139. * be called for an interface which is open.
  140. */
  141. struct netdev_queue_mgmt_ops {
  142. size_t ndo_queue_mem_size;
  143. int (*ndo_queue_mem_alloc)(struct net_device *dev,
  144. struct netdev_queue_config *qcfg,
  145. void *per_queue_mem,
  146. int idx);
  147. void (*ndo_queue_mem_free)(struct net_device *dev,
  148. void *per_queue_mem);
  149. int (*ndo_queue_start)(struct net_device *dev,
  150. struct netdev_queue_config *qcfg,
  151. void *per_queue_mem,
  152. int idx);
  153. int (*ndo_queue_stop)(struct net_device *dev,
  154. void *per_queue_mem,
  155. int idx);
  156. void (*ndo_default_qcfg)(struct net_device *dev,
  157. struct netdev_queue_config *qcfg);
  158. int (*ndo_validate_qcfg)(struct net_device *dev,
  159. struct netdev_queue_config *qcfg,
  160. struct netlink_ext_ack *extack);
  161. struct device * (*ndo_queue_get_dma_dev)(struct net_device *dev,
  162. int idx);
  163. unsigned int supported_params;
  164. };
  165. void netdev_queue_config(struct net_device *dev, int rxq,
  166. struct netdev_queue_config *qcfg);
  167. bool netif_rxq_has_unreadable_mp(struct net_device *dev, int idx);
  168. /**
  169. * DOC: Lockless queue stopping / waking helpers.
  170. *
  171. * The netif_txq_maybe_stop() and __netif_txq_completed_wake()
  172. * macros are designed to safely implement stopping
  173. * and waking netdev queues without full lock protection.
  174. *
  175. * We assume that there can be no concurrent stop attempts and no concurrent
  176. * wake attempts. The try-stop should happen from the xmit handler,
  177. * while wake up should be triggered from NAPI poll context.
  178. * The two may run concurrently (single producer, single consumer).
  179. *
  180. * The try-stop side is expected to run from the xmit handler and therefore
  181. * it does not reschedule Tx (netif_tx_start_queue() instead of
  182. * netif_tx_wake_queue()). Uses of the ``stop`` macros outside of the xmit
  183. * handler may lead to xmit queue being enabled but not run.
  184. * The waking side does not have similar context restrictions.
  185. *
  186. * The macros guarantee that rings will not remain stopped if there's
  187. * space available, but they do *not* prevent false wake ups when
  188. * the ring is full! Drivers should check for ring full at the start
  189. * for the xmit handler.
  190. *
  191. * All descriptor ring indexes (and other relevant shared state) must
  192. * be updated before invoking the macros.
  193. */
  194. #define netif_txq_try_stop(txq, get_desc, start_thrs) \
  195. ({ \
  196. int _res; \
  197. \
  198. netif_tx_stop_queue(txq); \
  199. /* Producer index and stop bit must be visible \
  200. * to consumer before we recheck. \
  201. * Pairs with a barrier in __netif_txq_completed_wake(). \
  202. */ \
  203. smp_mb__after_atomic(); \
  204. \
  205. /* We need to check again in a case another \
  206. * CPU has just made room available. \
  207. */ \
  208. _res = 0; \
  209. if (unlikely(get_desc >= start_thrs)) { \
  210. netif_tx_start_queue(txq); \
  211. _res = -1; \
  212. } \
  213. _res; \
  214. }) \
  215. /**
  216. * netif_txq_maybe_stop() - locklessly stop a Tx queue, if needed
  217. * @txq: struct netdev_queue to stop/start
  218. * @get_desc: get current number of free descriptors (see requirements below!)
  219. * @stop_thrs: minimal number of available descriptors for queue to be left
  220. * enabled
  221. * @start_thrs: minimal number of descriptors to re-enable the queue, can be
  222. * equal to @stop_thrs or higher to avoid frequent waking
  223. *
  224. * All arguments may be evaluated multiple times, beware of side effects.
  225. * @get_desc must be a formula or a function call, it must always
  226. * return up-to-date information when evaluated!
  227. * Expected to be used from ndo_start_xmit, see the comment on top of the file.
  228. *
  229. * Returns:
  230. * 0 if the queue was stopped
  231. * 1 if the queue was left enabled
  232. * -1 if the queue was re-enabled (raced with waking)
  233. */
  234. #define netif_txq_maybe_stop(txq, get_desc, stop_thrs, start_thrs) \
  235. ({ \
  236. int _res; \
  237. \
  238. _res = 1; \
  239. if (unlikely(get_desc < stop_thrs)) \
  240. _res = netif_txq_try_stop(txq, get_desc, start_thrs); \
  241. _res; \
  242. }) \
  243. /* Variant of netdev_tx_completed_queue() which guarantees smp_mb() if
  244. * @bytes != 0, regardless of kernel config.
  245. */
  246. static inline void
  247. netdev_txq_completed_mb(struct netdev_queue *dev_queue,
  248. unsigned int pkts, unsigned int bytes)
  249. {
  250. if (IS_ENABLED(CONFIG_BQL))
  251. netdev_tx_completed_queue(dev_queue, pkts, bytes);
  252. else if (bytes)
  253. smp_mb();
  254. }
  255. /**
  256. * __netif_txq_completed_wake() - locklessly wake a Tx queue, if needed
  257. * @txq: struct netdev_queue to stop/start
  258. * @pkts: number of packets completed
  259. * @bytes: number of bytes completed
  260. * @get_desc: get current number of free descriptors (see requirements below!)
  261. * @start_thrs: minimal number of descriptors to re-enable the queue
  262. * @down_cond: down condition, predicate indicating that the queue should
  263. * not be woken up even if descriptors are available
  264. *
  265. * All arguments may be evaluated multiple times.
  266. * @get_desc must be a formula or a function call, it must always
  267. * return up-to-date information when evaluated!
  268. * Reports completed pkts/bytes to BQL.
  269. *
  270. * Returns:
  271. * 0 if the queue was woken up
  272. * 1 if the queue was already enabled (or disabled but @down_cond is true)
  273. * -1 if the queue was left unchanged (@start_thrs not reached)
  274. */
  275. #define __netif_txq_completed_wake(txq, pkts, bytes, \
  276. get_desc, start_thrs, down_cond) \
  277. ({ \
  278. int _res; \
  279. \
  280. /* Report to BQL and piggy back on its barrier. \
  281. * Barrier makes sure that anybody stopping the queue \
  282. * after this point sees the new consumer index. \
  283. * Pairs with barrier in netif_txq_try_stop(). \
  284. */ \
  285. netdev_txq_completed_mb(txq, pkts, bytes); \
  286. \
  287. _res = -1; \
  288. if (pkts && likely(get_desc >= start_thrs)) { \
  289. _res = 1; \
  290. if (unlikely(netif_tx_queue_stopped(txq)) && \
  291. !(down_cond)) { \
  292. netif_tx_wake_queue(txq); \
  293. _res = 0; \
  294. } \
  295. } \
  296. _res; \
  297. })
  298. #define netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs) \
  299. __netif_txq_completed_wake(txq, pkts, bytes, get_desc, start_thrs, false)
  300. /* subqueue variants follow */
  301. #define netif_subqueue_try_stop(dev, idx, get_desc, start_thrs) \
  302. ({ \
  303. struct netdev_queue *_txq; \
  304. \
  305. _txq = netdev_get_tx_queue(dev, idx); \
  306. netif_txq_try_stop(_txq, get_desc, start_thrs); \
  307. })
  308. static inline void netif_subqueue_sent(const struct net_device *dev,
  309. unsigned int idx, unsigned int bytes)
  310. {
  311. struct netdev_queue *txq;
  312. txq = netdev_get_tx_queue(dev, idx);
  313. netdev_tx_sent_queue(txq, bytes);
  314. }
  315. static inline unsigned int netif_xmit_timeout_ms(struct netdev_queue *txq)
  316. {
  317. unsigned long trans_start = READ_ONCE(txq->trans_start);
  318. if (netif_xmit_stopped(txq) &&
  319. time_after(jiffies, trans_start + txq->dev->watchdog_timeo))
  320. return jiffies_to_msecs(jiffies - trans_start);
  321. return 0;
  322. }
  323. #define netif_subqueue_maybe_stop(dev, idx, get_desc, stop_thrs, start_thrs) \
  324. ({ \
  325. struct netdev_queue *_txq; \
  326. \
  327. _txq = netdev_get_tx_queue(dev, idx); \
  328. netif_txq_maybe_stop(_txq, get_desc, stop_thrs, start_thrs); \
  329. })
  330. #define netif_subqueue_completed_wake(dev, idx, pkts, bytes, \
  331. get_desc, start_thrs) \
  332. ({ \
  333. struct netdev_queue *_txq; \
  334. \
  335. _txq = netdev_get_tx_queue(dev, idx); \
  336. netif_txq_completed_wake(_txq, pkts, bytes, \
  337. get_desc, start_thrs); \
  338. })
  339. struct device *netdev_queue_get_dma_dev(struct net_device *dev, int idx);
  340. #endif