docs: networking: document NAPI
Subsystems (4)
Jakub Kicinski
maintainerPATCH v3 1/1 docs: networking: document NAPI
Add basic documentation about NAPI. We can stop linking to the ancient doc on the LF wiki.
---
v3: rebase on net-next (to avoid ixgb conflict)
fold in grammar fixes from Stephen
v2: https://lore.kernel.org/all/[email protected]/
remove the links in CAN and in ICE as well
improve the start of the threaded NAPI section
name footnote
internal links from the intro to sections
various clarifications from Florian and Stephen
--- .../can/ctu/ctucanfd-driver.rst | 3 +- .../device_drivers/ethernet/intel/e100.rst | 3 +- .../device_drivers/ethernet/intel/i40e.rst | 4 +- .../device_drivers/ethernet/intel/ice.rst | 4 +- Documentation/networking/index.rst | 1 + Documentation/networking/napi.rst | 251 ++++++++++++++++++ include/linux/netdevice.h | 13 +- 7 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 Documentation/networking/napi.rst
Documentation/networking/device_drivers/can/ctu/ctucanfd-driver.rst +1 -2
@@ -229,8 +229,7 @@ frames for a while. This has a potential to avoid the costly round of enabling interrupts, handling an incoming IRQ in ISR, re-enabling the softirq and switching context back to softirq. -More detailed documentation of NAPI may be found on the pages of Linux -Foundation `<https://wiki.linuxfoundation.org/networking/napi>`_. +See :ref:`Documentation/networking/napi.rst <napi>` for more information. Integrating the core to Xilinx Zynq -----------------------------------
Documentation/networking/device_drivers/ethernet/intel/e100.rst +1 -2
@@ -151,8 +151,7 @@ NAPI NAPI (Rx polling mode) is supported in the e100 driver. -See https://wiki.linuxfoundation.org/networking/napi for more -information on NAPI. +See :ref:`Documentation/networking/napi.rst <napi>` for more information. Multiple Interfaces on Same Ethernet Broadcast Network ------------------------------------------------------
Documentation/networking/device_drivers/ethernet/intel/i40e.rst +2 -2
@@ -399,8 +399,8 @@ operate only in full duplex and only at their native speed. NAPI ---- NAPI (Rx polling mode) is supported in the i40e driver. -For more information on NAPI, see -https://wiki.linuxfoundation.org/networking/napi + +See :ref:`Documentation/networking/napi.rst <napi>` for more information. Flow Control ------------
Documentation/networking/device_drivers/ethernet/intel/ice.rst +2 -2
@@ -817,10 +817,10 @@ on your device, you may not be able to change the auto-negotiation setting. NAPI ---- + This driver supports NAPI (Rx polling mode). -For more information on NAPI, see -https://wiki.linuxfoundation.org/networking/napi +See :ref:`Documentation/networking/napi.rst <napi>` for more information. MACVLAN -------
Documentation/networking/index.rst +1 -0
@@ -73,6 +73,7 @@ Refer to :ref:`netdev-FAQ` for a guide on netdev development process specifics. mpls-sysctl mptcp-sysctl multiqueue + napi netconsole netdev-features netdevices
Documentation/networking/napi.rst new file +251 -0
251 lines not on this page · raw message
include/linux/netdevice.h +8 -5
@@ -509,15 +509,18 @@ static inline bool napi_reschedule(struct napi_struct *napi)return false; } -bool napi_complete_done (struct napi_struct *n,int work_done);/** -* napi_complete - NAPI processing complete -* @n: NAPI context +* napi_complete_done - NAPI processing complete +* @n: NAPI context +* @work_done: number of packets processed * -* Mark NAPI processing as complete. -* Consider using napi_complete_done() instead. +* Mark NAPI processing as complete. Should only be called if poll budget +* has not been completely consumed. +* Prefer over napi_complete(). * Return false if device should avoid rearming interrupts. */ +bool napi_complete_done (struct napi_struct *n,int work_done); +static inline bool napi_complete (struct napi_struct *n) {return napi_complete_done (n,0 );
Signature · 1 line
2.39.2
Quoted 40 lines · Jakub Kicinski
Add basic documentation about NAPI. We can stop linking to the ancient doc on the LF wiki.
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Reviewed-by Toke Høiland-Jørgensen <[email protected]>Reviewed-by Tony Nguyen <[email protected]>Reviewed-by Florian Fainelli <[email protected]>---
v3: rebase on net-next (to avoid ixgb conflict)
fold in grammar fixes from Stephen
v2: https://lore.kernel.org/all/[email protected]/
remove the links in CAN and in ICE as well
improve the start of the threaded NAPI section
name footnote
internal links from the intro to sections
various clarifications from Florian and Stephen--- .../can/ctu/ctucanfd-driver.rst | 3 +- .../device_drivers/ethernet/intel/e100.rst | 3 +- .../device_drivers/ethernet/intel/i40e.rst | 4 +- .../device_drivers/ethernet/intel/ice.rst | 4 +- Documentation/networking/index.rst | 1 + Documentation/networking/napi.rst | 251 ++++++++++++++++++ include/linux/netdevice.h | 13 +- 7 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 Documentation/networking/napi.rst
[snip]
Documentation/networking/napi.rst line 241
Quoted 11 lines
Documentation/networking/napi.rst new file +11
-0+Threaded NAPI +------------- + +Threaded NAPI is an operating mode that uses dedicated kernel +threads rather than software IRQ context for NAPI processing. +The configuration is per netdevice and will affect all +NAPI instances of that device. Each NAPI instance will spawn a separate +thread (called ``napi/${ifc-name}-${napi-id}``). + +It is recommended to pin each kernel thread to a single CPU, the same +CPU as services the interrupt. Note that the mapping between IRQs and
"... the same CPU as services the interrupt ...", should it be "the same CPU that services the interrupt" ?
Documentation/networking/napi.rst line 251
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0+NAPI instances may not be trivial (and is driver dependent). +The NAPI instance IDs will be assigned in the opposite order +than the process IDs of the kernel threads. + +Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in +netdev's sysfs directory. + +.. rubric:: Footnotes + +.. [#] NAPI was originally referred to as New API in 2.4 Linux.
Very nice! Other than the above looks good to me.
Cheers,
Nik
Jakub Kicinski
maintainerQuoted 5 lines · On Wed, 22 Mar 2023 12:39:59 +0200 Nikolay Aleksandrov wrote:
Documentation/networking/napi.rst line 241
Quoted 2 lines
Documentation/networking/napi.rst new file +2
-0+It is recommended to pin each kernel thread to a single CPU, the same +CPU as services the interrupt. Note that the mapping between IRQs and"... the same CPU as services the interrupt ...", should it be "the same CPU that services the interrupt" ?
"the same as" is a very common idiom.
There's a slight ellipsis there, perhaps, the full sentence is:
| It is recommended to pin each kernel thread to a single CPU, the same | CPU as [the CPU which] services the interrupt.
Let me add the missing part.
Quoted 10 lines · On Tue, 21 Mar 2023 22:38:48 -0700 Jakub Kicinski <[email protected]> wrote:
Add basic documentation about NAPI. We can stop linking to the ancient doc on the LF wiki.
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Reviewed-by Toke Høiland-Jørgensen <[email protected]>Reviewed-by Tony Nguyen <[email protected]>Reviewed-by Florian Fainelli <[email protected]>
Will add more about interrupt triggering in later patch.
Hi,
I have a few comments for your consideration.
Quoted 47 lines · Jakub Kicinski
Add basic documentation about NAPI. We can stop linking to the ancient doc on the LF wiki.
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Reviewed-by Toke Høiland-Jørgensen <[email protected]>Reviewed-by Tony Nguyen <[email protected]>Reviewed-by Florian Fainelli <[email protected]>---
v3: rebase on net-next (to avoid ixgb conflict)
fold in grammar fixes from Stephen
v2: https://lore.kernel.org/all/[email protected]/
remove the links in CAN and in ICE as well
improve the start of the threaded NAPI section
name footnote
internal links from the intro to sections
various clarifications from Florian and Stephen--- .../can/ctu/ctucanfd-driver.rst | 3 +- .../device_drivers/ethernet/intel/e100.rst | 3 +- .../device_drivers/ethernet/intel/i40e.rst | 4 +- .../device_drivers/ethernet/intel/ice.rst | 4 +- Documentation/networking/index.rst | 1 + Documentation/networking/napi.rst | 251 ++++++++++++++++++ include/linux/netdevice.h | 13 +- 7 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 Documentation/networking/napi.rstDocumentation/networking/napi.rst new file +0
-0
Documentation/networking/napi.rst line 10
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0@@ -0,0 +1,251 @@ +.. _napi: + +==== +NAPI +==== + +NAPI is the event handling mechanism used by the Linux networking stack. +The name NAPI no longer stands for anything in particular [#]_. + +In basic operation device notifies the host about new events via an interrupt.
{a | the} device
Documentation/networking/napi.rst line 12
Quoted 2 lines
Documentation/networking/napi.rst new file +2
-0+The host then schedules a NAPI instance to process the events. +Device may also be polled for events via NAPI without receiving
A device may also be
The device may also be
Devices may also be
Quoted 23 lines
Documentation/networking/napi.rst new file +23
-0+interrupts first (:ref:`busy polling<poll>`). + +NAPI processing usually happens in the software interrupt context, +but there is an option to use :ref:`separate kernel threads<threaded>` +for NAPI processing. + +All in all NAPI abstracts away from the drivers the context and configuration +of event (packet Rx and Tx) processing. + +Driver API +========== + +The two most important elements of NAPI are the struct napi_struct +and the associated poll method. struct napi_struct holds the state +of the NAPI instance while the method is the driver-specific event +handler. The method will typically free Tx packets that have been +transmitted and process newly received packets. + +.. _drv_ctrl: + +Control API +----------- +
[snip]
Quoted 8 lines
Documentation/networking/napi.rst new file +8
-0+ +Datapath API +------------ + +napi_schedule() is the basic method of scheduling a NAPI poll. +Drivers should call this function in their interrupt handler +(see :ref:`drv_sched` for more info). A successful call to napi_schedule() +will take ownership of the NAPI instance.
Documentation/networking/napi.rst line 67
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0+ +Later, after NAPI is scheduled, the driver's poll method will be +called to process the events/packets. The method takes a ``budget`` +argument - drivers can process completions for any number of Tx +packets but should only process up to ``budget`` number of +Rx packets. Rx processing is usually much more expensive. + +In other words, it is recommended to ignore the budget argument when +performing TX buffer reclamation to ensure that the reclamation is not +arbitrarily bounded, however, it is required to honor the budget argument
bounded; however
or
bounded. However
Documentation/networking/napi.rst line 77
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0+for RX processing. + +.. warning:: + + The ``budget`` argument may be 0 if core tries to only process Tx completions + and no Rx packets. + +The poll method returns the amount of work done. If the driver still +has outstanding work to do (e.g. ``budget`` was exhausted) +the poll method should return exactly ``budget``. In that case,
So it return the original 'budget' parameter value that was passed in to it?
Quoted 21 lines
Documentation/networking/napi.rst new file +21
-0+the NAPI instance will be serviced/polled again (without the +need to be scheduled). + +If event processing has been completed (all outstanding packets +processed) the poll method should call napi_complete_done() +before returning. napi_complete_done() releases the ownership +of the instance. + +.. warning:: + + The case of finishing all events and using exactly ``budget`` + must be handled carefully. There is no way to report this + (rare) condition to the stack, so the driver must either + not call napi_complete_done() and wait to be called again, + or return ``budget - 1``. + + If the ``budget`` is 0 napi_complete_done() should never be called. + +Call sequence +------------- +
[snip]
Quoted 6 lines
Documentation/networking/napi.rst new file +6
-0+ +.. _drv_sched: + +Scheduling and IRQ masking +-------------------------- +
[snip]
Quoted 11 lines
Documentation/networking/napi.rst new file +11
-0+ +Instance to queue mapping +------------------------- + +Modern devices have multiple NAPI instances (struct napi_struct) per +interface. There is no strong requirement on how the instances are +mapped to queues and interrupts. NAPI is primarily a polling/processing +abstraction without specific user-facing semantics. That said, most networking +devices end up using NAPI in fairly similar ways. + +NAPI instances most often correspond 1:1:1 to interrupts and queue pairs
Documentation/networking/napi.rst line 166
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0+(queue pair is a set of a single Rx and single Tx queue). + +In less common cases a NAPI instance may be used for multiple queues +or Rx and Tx queues can be serviced by separate NAPI instances on a single +core. Regardless of the queue assignment, however, there is usually still +a 1:1 mapping between NAPI instances and interrupts. + +It's worth noting that the ethtool API uses a "channel" terminology where +each channel can be either ``rx``, ``tx`` or ``combined``. It's not clear +what constitutes a channel, the recommended interpretation is to understand
channel;
Quoted 14 lines
Documentation/networking/napi.rst new file +14
-0+a channel as an IRQ/NAPI which services queues of a given type. For example, +a configuration of 1 ``rx``, 1 ``tx`` and 1 ``combined`` channel is expected +to utilize 3 interrupts, 2 Rx and 2 Tx queues. + +User API +======== + +User interactions with NAPI depend on NAPI instance ID. The instance IDs +are only visible to the user thru the ``SO_INCOMING_NAPI_ID`` socket option. +It's not currently possible to query IDs used by a given device. + +Software IRQ coalescing +----------------------- +
[snip]
Documentation/networking/napi.rst line 197
Quoted 7 lines
Documentation/networking/napi.rst new file +7
-0+ +.. _poll: + +Busy polling +------------ + +Busy polling allows user process to check for incoming packets before
allows a user process
or
allows user processes
Documentation/networking/napi.rst line 199
Quoted 2 lines
Documentation/networking/napi.rst new file +2
-0+the device interrupt fires. As is the case with any busy polling it trades +off CPU cycles for lower latency (in fact production uses of NAPI busy
I would drop "in fact".
Documentation/networking/napi.rst line 209
Quoted 10 lines
Documentation/networking/napi.rst new file +10
-0+polling are not well known). + +Busy polling is enabled by either setting ``SO_BUSY_POLL`` on +selected sockets or using the global ``net.core.busy_poll`` and +``net.core.busy_read`` sysctls. An io_uring API for NAPI busy polling +also exists. + +IRQ mitigation +--------------- +
[snip]
and in any case:
Thanks.
Signature · 1 line
~Randy
Jakub Kicinski
maintainerQuoted 10 lines · On Tue, 21 Mar 2023 22:38:48 -0700 Jakub Kicinski wrote:
Add basic documentation about NAPI. We can stop linking to the ancient doc on the LF wiki.
Signed-off-by Jakub Kicinski <[email protected]>Reviewed-by Bagas Sanjaya <[email protected]>Reviewed-by Toke Høiland-Jørgensen <[email protected]>Reviewed-by Tony Nguyen <[email protected]>Reviewed-by Florian Fainelli <[email protected]>
I think I'll just fold the latest round of rewordings when applying the patch. Thanks for reviews everyone!