napi.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. .. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. .. _napi:
  3. ====
  4. NAPI
  5. ====
  6. NAPI is the event handling mechanism used by the Linux networking stack.
  7. The name NAPI no longer stands for anything in particular [#]_.
  8. In basic operation the device notifies the host about new events
  9. via an interrupt.
  10. The host then schedules a NAPI instance to process the events.
  11. The device may also be polled for events via NAPI without receiving
  12. interrupts first (:ref:`busy polling<poll>`).
  13. NAPI processing usually happens in the software interrupt context,
  14. but there is an option to use :ref:`separate kernel threads<threaded>`
  15. for NAPI processing.
  16. All in all NAPI abstracts away from the drivers the context and configuration
  17. of event (packet Rx and Tx) processing.
  18. Driver API
  19. ==========
  20. The two most important elements of NAPI are the struct napi_struct
  21. and the associated poll method. struct napi_struct holds the state
  22. of the NAPI instance while the method is the driver-specific event
  23. handler. The method will typically free Tx packets that have been
  24. transmitted and process newly received packets.
  25. .. _drv_ctrl:
  26. Control API
  27. -----------
  28. netif_napi_add() and netif_napi_del() add/remove a NAPI instance
  29. from the system. The instances are attached to the netdevice passed
  30. as argument (and will be deleted automatically when netdevice is
  31. unregistered). Instances are added in a disabled state.
  32. napi_enable() and napi_disable() manage the disabled state.
  33. A disabled NAPI can't be scheduled and its poll method is guaranteed
  34. to not be invoked. napi_disable() waits for ownership of the NAPI
  35. instance to be released.
  36. The control APIs are not idempotent. Control API calls are safe against
  37. concurrent use of datapath APIs but an incorrect sequence of control API
  38. calls may result in crashes, deadlocks, or race conditions. For example,
  39. calling napi_disable() multiple times in a row will deadlock.
  40. Datapath API
  41. ------------
  42. napi_schedule() is the basic method of scheduling a NAPI poll.
  43. Drivers should call this function in their interrupt handler
  44. (see :ref:`drv_sched` for more info). A successful call to napi_schedule()
  45. will take ownership of the NAPI instance.
  46. Later, after NAPI is scheduled, the driver's poll method will be
  47. called to process the events/packets. The method takes a ``budget``
  48. argument - drivers can process completions for any number of Tx
  49. packets but should only process up to ``budget`` number of
  50. Rx packets. Rx processing is usually much more expensive.
  51. In other words for Rx processing the ``budget`` argument limits how many
  52. packets driver can process in a single poll. Rx specific APIs like page
  53. pool or XDP cannot be used at all when ``budget`` is 0.
  54. skb Tx processing should happen regardless of the ``budget``, but if
  55. the argument is 0 driver cannot call any XDP (or page pool) APIs.
  56. .. warning::
  57. The ``budget`` argument may be 0 if core tries to only process
  58. skb Tx completions and no Rx or XDP packets.
  59. The poll method returns the amount of work done. If the driver still
  60. has outstanding work to do (e.g. ``budget`` was exhausted)
  61. the poll method should return exactly ``budget``. In that case,
  62. the NAPI instance will be serviced/polled again (without the
  63. need to be scheduled).
  64. If event processing has been completed (all outstanding packets
  65. processed) the poll method should call napi_complete_done()
  66. before returning. napi_complete_done() releases the ownership
  67. of the instance.
  68. .. warning::
  69. The case of finishing all events and using exactly ``budget``
  70. must be handled carefully. There is no way to report this
  71. (rare) condition to the stack, so the driver must either
  72. not call napi_complete_done() and wait to be called again,
  73. or return ``budget - 1``.
  74. If the ``budget`` is 0 napi_complete_done() should never be called.
  75. Call sequence
  76. -------------
  77. Drivers should not make assumptions about the exact sequencing
  78. of calls. The poll method may be called without the driver scheduling
  79. the instance (unless the instance is disabled). Similarly,
  80. it's not guaranteed that the poll method will be called, even
  81. if napi_schedule() succeeded (e.g. if the instance gets disabled).
  82. As mentioned in the :ref:`drv_ctrl` section - napi_disable() and subsequent
  83. calls to the poll method only wait for the ownership of the instance
  84. to be released, not for the poll method to exit. This means that
  85. drivers should avoid accessing any data structures after calling
  86. napi_complete_done().
  87. .. _drv_sched:
  88. Scheduling and IRQ masking
  89. --------------------------
  90. Drivers should keep the interrupts masked after scheduling
  91. the NAPI instance - until NAPI polling finishes any further
  92. interrupts are unnecessary.
  93. Drivers which have to mask the interrupts explicitly (as opposed
  94. to IRQ being auto-masked by the device) should use the napi_schedule_prep()
  95. and __napi_schedule() calls:
  96. .. code-block:: c
  97. if (napi_schedule_prep(&v->napi)) {
  98. mydrv_mask_rxtx_irq(v->idx);
  99. /* schedule after masking to avoid races */
  100. __napi_schedule(&v->napi);
  101. }
  102. IRQ should only be unmasked after a successful call to napi_complete_done():
  103. .. code-block:: c
  104. if (budget && napi_complete_done(&v->napi, work_done)) {
  105. mydrv_unmask_rxtx_irq(v->idx);
  106. return min(work_done, budget - 1);
  107. }
  108. napi_schedule_irqoff() is a variant of napi_schedule() which takes advantage
  109. of guarantees given by being invoked in IRQ context (no need to
  110. mask interrupts). napi_schedule_irqoff() will fall back to napi_schedule() if
  111. IRQs are threaded (such as if ``PREEMPT_RT`` is enabled).
  112. Instance to queue mapping
  113. -------------------------
  114. Modern devices have multiple NAPI instances (struct napi_struct) per
  115. interface. There is no strong requirement on how the instances are
  116. mapped to queues and interrupts. NAPI is primarily a polling/processing
  117. abstraction without specific user-facing semantics. That said, most networking
  118. devices end up using NAPI in fairly similar ways.
  119. NAPI instances most often correspond 1:1:1 to interrupts and queue pairs
  120. (queue pair is a set of a single Rx and single Tx queue).
  121. In less common cases a NAPI instance may be used for multiple queues
  122. or Rx and Tx queues can be serviced by separate NAPI instances on a single
  123. core. Regardless of the queue assignment, however, there is usually still
  124. a 1:1 mapping between NAPI instances and interrupts.
  125. It's worth noting that the ethtool API uses a "channel" terminology where
  126. each channel can be either ``rx``, ``tx`` or ``combined``. It's not clear
  127. what constitutes a channel; the recommended interpretation is to understand
  128. a channel as an IRQ/NAPI which services queues of a given type. For example,
  129. a configuration of 1 ``rx``, 1 ``tx`` and 1 ``combined`` channel is expected
  130. to utilize 3 interrupts, 2 Rx and 2 Tx queues.
  131. User API
  132. ========
  133. User interactions with NAPI depend on NAPI instance ID. The instance IDs
  134. are only visible to the user thru the ``SO_INCOMING_NAPI_ID`` socket option.
  135. It's not currently possible to query IDs used by a given device.
  136. Software IRQ coalescing
  137. -----------------------
  138. NAPI does not perform any explicit event coalescing by default.
  139. In most scenarios batching happens due to IRQ coalescing which is done
  140. by the device. There are cases where software coalescing is helpful.
  141. NAPI can be configured to arm a repoll timer instead of unmasking
  142. the hardware interrupts as soon as all packets are processed.
  143. The ``gro_flush_timeout`` sysfs configuration of the netdevice
  144. is reused to control the delay of the timer, while
  145. ``napi_defer_hard_irqs`` controls the number of consecutive empty polls
  146. before NAPI gives up and goes back to using hardware IRQs.
  147. .. _poll:
  148. Busy polling
  149. ------------
  150. Busy polling allows a user process to check for incoming packets before
  151. the device interrupt fires. As is the case with any busy polling it trades
  152. off CPU cycles for lower latency (production uses of NAPI busy polling
  153. are not well known).
  154. Busy polling is enabled by either setting ``SO_BUSY_POLL`` on
  155. selected sockets or using the global ``net.core.busy_poll`` and
  156. ``net.core.busy_read`` sysctls. An io_uring API for NAPI busy polling
  157. also exists.
  158. IRQ mitigation
  159. ---------------
  160. While busy polling is supposed to be used by low latency applications,
  161. a similar mechanism can be used for IRQ mitigation.
  162. Very high request-per-second applications (especially routing/forwarding
  163. applications and especially applications using AF_XDP sockets) may not
  164. want to be interrupted until they finish processing a request or a batch
  165. of packets.
  166. Such applications can pledge to the kernel that they will perform a busy
  167. polling operation periodically, and the driver should keep the device IRQs
  168. permanently masked. This mode is enabled by using the ``SO_PREFER_BUSY_POLL``
  169. socket option. To avoid system misbehavior the pledge is revoked
  170. if ``gro_flush_timeout`` passes without any busy poll call.
  171. The NAPI budget for busy polling is lower than the default (which makes
  172. sense given the low latency intention of normal busy polling). This is
  173. not the case with IRQ mitigation, however, so the budget can be adjusted
  174. with the ``SO_BUSY_POLL_BUDGET`` socket option.
  175. .. _threaded:
  176. Threaded NAPI
  177. -------------
  178. Threaded NAPI is an operating mode that uses dedicated kernel
  179. threads rather than software IRQ context for NAPI processing.
  180. The configuration is per netdevice and will affect all
  181. NAPI instances of that device. Each NAPI instance will spawn a separate
  182. thread (called ``napi/${ifc-name}-${napi-id}``).
  183. It is recommended to pin each kernel thread to a single CPU, the same
  184. CPU as the CPU which services the interrupt. Note that the mapping
  185. between IRQs and NAPI instances may not be trivial (and is driver
  186. dependent). The NAPI instance IDs will be assigned in the opposite
  187. order than the process IDs of the kernel threads.
  188. Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in
  189. netdev's sysfs directory.
  190. .. rubric:: Footnotes
  191. .. [#] NAPI was originally referred to as New API in 2.4 Linux.