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