brintos

brintos / linux-shallow public Read only

0
0
Text · 9.7 KiB · dfa5d54 Raw
255 lines · plain
1.. SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)2 3.. _napi:4 5====6NAPI7====8 9NAPI is the event handling mechanism used by the Linux networking stack.10The name NAPI no longer stands for anything in particular [#]_.11 12In basic operation the device notifies the host about new events13via an interrupt.14The host then schedules a NAPI instance to process the events.15The device may also be polled for events via NAPI without receiving16interrupts first (:ref:`busy polling<poll>`).17 18NAPI processing usually happens in the software interrupt context,19but there is an option to use :ref:`separate kernel threads<threaded>`20for NAPI processing.21 22All in all NAPI abstracts away from the drivers the context and configuration23of event (packet Rx and Tx) processing.24 25Driver API26==========27 28The two most important elements of NAPI are the struct napi_struct29and the associated poll method. struct napi_struct holds the state30of the NAPI instance while the method is the driver-specific event31handler. The method will typically free Tx packets that have been32transmitted and process newly received packets.33 34.. _drv_ctrl:35 36Control API37-----------38 39netif_napi_add() and netif_napi_del() add/remove a NAPI instance40from the system. The instances are attached to the netdevice passed41as argument (and will be deleted automatically when netdevice is42unregistered). Instances are added in a disabled state.43 44napi_enable() and napi_disable() manage the disabled state.45A disabled NAPI can't be scheduled and its poll method is guaranteed46to not be invoked. napi_disable() waits for ownership of the NAPI47instance to be released.48 49The control APIs are not idempotent. Control API calls are safe against50concurrent use of datapath APIs but an incorrect sequence of control API51calls may result in crashes, deadlocks, or race conditions. For example,52calling napi_disable() multiple times in a row will deadlock.53 54Datapath API55------------56 57napi_schedule() is the basic method of scheduling a NAPI poll.58Drivers should call this function in their interrupt handler59(see :ref:`drv_sched` for more info). A successful call to napi_schedule()60will take ownership of the NAPI instance.61 62Later, after NAPI is scheduled, the driver's poll method will be63called to process the events/packets. The method takes a ``budget``64argument - drivers can process completions for any number of Tx65packets but should only process up to ``budget`` number of66Rx packets. Rx processing is usually much more expensive.67 68In other words for Rx processing the ``budget`` argument limits how many69packets driver can process in a single poll. Rx specific APIs like page70pool or XDP cannot be used at all when ``budget`` is 0.71skb Tx processing should happen regardless of the ``budget``, but if72the argument is 0 driver cannot call any XDP (or page pool) APIs.73 74.. warning::75 76   The ``budget`` argument may be 0 if core tries to only process77   skb Tx completions and no Rx or XDP packets.78 79The poll method returns the amount of work done. If the driver still80has outstanding work to do (e.g. ``budget`` was exhausted)81the poll method should return exactly ``budget``. In that case,82the NAPI instance will be serviced/polled again (without the83need to be scheduled).84 85If event processing has been completed (all outstanding packets86processed) the poll method should call napi_complete_done()87before returning. napi_complete_done() releases the ownership88of the instance.89 90.. warning::91 92   The case of finishing all events and using exactly ``budget``93   must be handled carefully. There is no way to report this94   (rare) condition to the stack, so the driver must either95   not call napi_complete_done() and wait to be called again,96   or return ``budget - 1``.97 98   If the ``budget`` is 0 napi_complete_done() should never be called.99 100Call sequence101-------------102 103Drivers should not make assumptions about the exact sequencing104of calls. The poll method may be called without the driver scheduling105the instance (unless the instance is disabled). Similarly,106it's not guaranteed that the poll method will be called, even107if napi_schedule() succeeded (e.g. if the instance gets disabled).108 109As mentioned in the :ref:`drv_ctrl` section - napi_disable() and subsequent110calls to the poll method only wait for the ownership of the instance111to be released, not for the poll method to exit. This means that112drivers should avoid accessing any data structures after calling113napi_complete_done().114 115.. _drv_sched:116 117Scheduling and IRQ masking118--------------------------119 120Drivers should keep the interrupts masked after scheduling121the NAPI instance - until NAPI polling finishes any further122interrupts are unnecessary.123 124Drivers which have to mask the interrupts explicitly (as opposed125to IRQ being auto-masked by the device) should use the napi_schedule_prep()126and __napi_schedule() calls:127 128.. code-block:: c129 130  if (napi_schedule_prep(&v->napi)) {131      mydrv_mask_rxtx_irq(v->idx);132      /* schedule after masking to avoid races */133      __napi_schedule(&v->napi);134  }135 136IRQ should only be unmasked after a successful call to napi_complete_done():137 138.. code-block:: c139 140  if (budget && napi_complete_done(&v->napi, work_done)) {141    mydrv_unmask_rxtx_irq(v->idx);142    return min(work_done, budget - 1);143  }144 145napi_schedule_irqoff() is a variant of napi_schedule() which takes advantage146of guarantees given by being invoked in IRQ context (no need to147mask interrupts). napi_schedule_irqoff() will fall back to napi_schedule() if148IRQs are threaded (such as if ``PREEMPT_RT`` is enabled).149 150Instance to queue mapping151-------------------------152 153Modern devices have multiple NAPI instances (struct napi_struct) per154interface. There is no strong requirement on how the instances are155mapped to queues and interrupts. NAPI is primarily a polling/processing156abstraction without specific user-facing semantics. That said, most networking157devices end up using NAPI in fairly similar ways.158 159NAPI instances most often correspond 1:1:1 to interrupts and queue pairs160(queue pair is a set of a single Rx and single Tx queue).161 162In less common cases a NAPI instance may be used for multiple queues163or Rx and Tx queues can be serviced by separate NAPI instances on a single164core. Regardless of the queue assignment, however, there is usually still165a 1:1 mapping between NAPI instances and interrupts.166 167It's worth noting that the ethtool API uses a "channel" terminology where168each channel can be either ``rx``, ``tx`` or ``combined``. It's not clear169what constitutes a channel; the recommended interpretation is to understand170a channel as an IRQ/NAPI which services queues of a given type. For example,171a configuration of 1 ``rx``, 1 ``tx`` and 1 ``combined`` channel is expected172to utilize 3 interrupts, 2 Rx and 2 Tx queues.173 174User API175========176 177User interactions with NAPI depend on NAPI instance ID. The instance IDs178are only visible to the user thru the ``SO_INCOMING_NAPI_ID`` socket option.179It's not currently possible to query IDs used by a given device.180 181Software IRQ coalescing182-----------------------183 184NAPI does not perform any explicit event coalescing by default.185In most scenarios batching happens due to IRQ coalescing which is done186by the device. There are cases where software coalescing is helpful.187 188NAPI can be configured to arm a repoll timer instead of unmasking189the hardware interrupts as soon as all packets are processed.190The ``gro_flush_timeout`` sysfs configuration of the netdevice191is reused to control the delay of the timer, while192``napi_defer_hard_irqs`` controls the number of consecutive empty polls193before NAPI gives up and goes back to using hardware IRQs.194 195.. _poll:196 197Busy polling198------------199 200Busy polling allows a user process to check for incoming packets before201the device interrupt fires. As is the case with any busy polling it trades202off CPU cycles for lower latency (production uses of NAPI busy polling203are not well known).204 205Busy polling is enabled by either setting ``SO_BUSY_POLL`` on206selected sockets or using the global ``net.core.busy_poll`` and207``net.core.busy_read`` sysctls. An io_uring API for NAPI busy polling208also exists.209 210IRQ mitigation211---------------212 213While busy polling is supposed to be used by low latency applications,214a similar mechanism can be used for IRQ mitigation.215 216Very high request-per-second applications (especially routing/forwarding217applications and especially applications using AF_XDP sockets) may not218want to be interrupted until they finish processing a request or a batch219of packets.220 221Such applications can pledge to the kernel that they will perform a busy222polling operation periodically, and the driver should keep the device IRQs223permanently masked. This mode is enabled by using the ``SO_PREFER_BUSY_POLL``224socket option. To avoid system misbehavior the pledge is revoked225if ``gro_flush_timeout`` passes without any busy poll call.226 227The NAPI budget for busy polling is lower than the default (which makes228sense given the low latency intention of normal busy polling). This is229not the case with IRQ mitigation, however, so the budget can be adjusted230with the ``SO_BUSY_POLL_BUDGET`` socket option.231 232.. _threaded:233 234Threaded NAPI235-------------236 237Threaded NAPI is an operating mode that uses dedicated kernel238threads rather than software IRQ context for NAPI processing.239The configuration is per netdevice and will affect all240NAPI instances of that device. Each NAPI instance will spawn a separate241thread (called ``napi/${ifc-name}-${napi-id}``).242 243It is recommended to pin each kernel thread to a single CPU, the same244CPU as the CPU which services the interrupt. Note that the mapping245between IRQs and NAPI instances may not be trivial (and is driver246dependent). The NAPI instance IDs will be assigned in the opposite247order than the process IDs of the kernel threads.248 249Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in250netdev's sysfs directory.251 252.. rubric:: Footnotes253 254.. [#] NAPI was originally referred to as New API in 2.4 Linux.255