Skip to content

libeth: add libeth_xdp helper lib

Alexander Lobakin bpf intel-wired-lan lkml netdev 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest17 patches 1 replyIn review
  2. v116 patches 7 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/17libeth, libie: clean symbol exports up a littledrivers/net/ethernet/intel/libeth/rx.c drivers/net/ethernet/intel/libie/rx.c+14 -71 reply
2/17libeth: convert to netmemdrivers/net/ethernet/intel/iavf/iavf_txrx.c drivers/net/ethernet/intel/idpf/idpf_singleq_txrx.c drivers/net/ethernet/intel/idpf/idpf_txrx.c +2 more+46 -360 replies
3/17libeth: support native XDP and register memory modeldrivers/net/ethernet/intel/libeth/rx.c include/net/libeth/rx.h+20 -60 replies
4/17libeth: xdp: add XDP_TX buffers sendingdrivers/net/ethernet/intel/libeth/Kconfig drivers/net/ethernet/intel/libeth/Makefile drivers/net/ethernet/intel/libeth/xdp.c +2 more+652 -50 replies
5/17libeth: xdp: add .ndo_xdp_xmit() helpersdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/tx.h include/net/libeth/xdp.h+328 -50 replies
6/17libeth: xdp: add XDPSQE completion helpersdrivers/net/ethernet/intel/libeth/Makefile drivers/net/ethernet/intel/libeth/priv.h drivers/net/ethernet/intel/libeth/tx.c +4 more+221 -20 replies
7/17libeth: xdp: add XDPSQ locking helpersdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/types.h include/net/libeth/xdp.h+192 -30 replies
8/17libeth: xdp: add XDPSQ cleanup timersdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/types.h include/net/libeth/xdp.h+100 -10 replies
9/17libeth: xdp: add helpers for preparing/processing &libeth_xdp_buffdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/types.h include/net/libeth/xdp.h+264 -00 replies
10/17libeth: xdp: add XDP prog run and verdict result handlingdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/types.h include/net/libeth/xdp.h+330 -00 replies
11/17libeth: xdp: add templates for building driver-side callbacksinclude/net/libeth/xdp.h+195 -00 replies
12/17libeth: xdp: add RSS hash hint and XDP features setup helpersdrivers/net/ethernet/intel/libeth/xdp.c include/net/libeth/xdp.h+159 -00 replies
13/17libeth: xsk: add XSk XDP_TX sending helpersdrivers/net/ethernet/intel/libeth/Kconfig drivers/net/ethernet/intel/libeth/Makefile drivers/net/ethernet/intel/libeth/priv.h +6 more+226 -90 replies
14/17libeth: xsk: add XSk xmit functionsdrivers/net/ethernet/intel/libeth/priv.h drivers/net/ethernet/intel/libeth/xdp.c drivers/net/ethernet/intel/libeth/xsk.c +3 more+248 -170 replies
15/17libeth: xsk: add XSk Rx processing supportdrivers/net/ethernet/intel/libeth/priv.h drivers/net/ethernet/intel/libeth/xdp.c drivers/net/ethernet/intel/libeth/xsk.c +2 more+398 -80 replies
16/17libeth: xsk: add XSkFQ refill and XSk wakeup helpersdrivers/net/ethernet/intel/libeth/xsk.c include/net/libeth/xsk.h+222 -00 replies
17/17libeth: xdp, xsk: access adjacent u32s as u64 where applicableinclude/net/libeth/xdp.h include/net/libeth/xsk.h+31 -80 replies

Reviews trailers per patch

2 of 17 patches have a Reviewed-by or Acked-by.

PatchMina AlmasryAleksandr Loktionov
1/17R-b
2/17R-b
3/17
4/17
5/17
6/17
7/17
8/17
9/17
10/17
11/17
12/17
13/17
14/17
15/17
16/17
17/17

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH iwl-next v2 00/17] libeth: add libeth_xdp helper lib

Time to add XDP helpers infra to libeth to greatly simplify adding XDP to idpf and iavf, as well as improve and extend XDP in ice and i40e. Any vendor is free to reuse helpers. If this happens, I'm fine with moving the folder of out intel/.

The helpers greatly simplify building xdp_buff, running a prog, handling the verdict, implement XDP_TX, .ndo_xdp_xmit, XDP buffer completion. Same applies to XSk (with XSk xmit instead of .ndo_xdp_xmit, plus stuff like XSk wakeup).
They are entirely generic with no HW definitions or assumptions. HW-specific stuff like parsing Rx desc / filling Tx desc is passed from the driver as inline callbacks.

For now, key assumptions that optimize performance / avoid code bloat, but might not fit every driver in driver/net/:
* netmem holding the buffers are always order-0;
* driver has separate XDP Tx queues, doesn't use stack queues for that. For best efficiency, you may want to have nr_cpu_ids XDP queues, but less (queue sharing) is also supported;
* XDP Tx queues are interrupt-less and use "lazy" cleaning only when there are less than 1/4 free Tx descriptors of the queue size;
* main target platforms are 64-bit, although 32-bit is also fully supported, but the code might be not as optimized for them.

Library code already supports multi-buffer for all kinds of Tx and both header split and no split for Rx and Tx. Frags can come from devmem/io_uring etc., direct struct page * is used only for header buffers for which it's always true.
Drivers are free to pass their own Rx hints and XSK xmit hints ops.

XDP_TX and ndo_xdp_xmit use onstack bulk for the frames to be sent and send them by batches of 16 buffers. This eats ~280 bytes on the stack, but gives good boosts and allow to greatly optimize the main sending function leaving it without any error/exception paths.

XSk xmit fills Tx descriptors in the loop unrolled by 8. This was proven to improve perf on ice and i40e. XDP_TX and ndo_xdp_xmit doesn't use unrolling as I wasn't able to get any improvements in those scenenarios from this, while +1 Kb for their sending functions for nothing doesn't sound reasonable.

XSk wakeup, instead of traditionally used "SW interrupts" provided by NICs, uses IPI to schedule NAPI on the CPU corresponding to the given queue pair. It gives better control over CPU distribution and in general performs way better than "SW interrupts", plus allows us to not pass any HW-specific callbacks there.

The code is built the way that all callbacks passed from drivers get inlined; in general, most of hotpath gets inlined. Everything slow/exception lands to .c files in the libeth folder, doesn't create copies in the drivers themselves and doesn't overloat hotpath.
Sure, inlining means that hotpath will be compiled into every driver that uses the lib, but the core code is written in one place, so no copying of bugs happens. Fixed once -- works everywhere.

The last commit might look like sorta hack, but it gives really good boosts and decreases object code size, plus there are checks that all those wider accesses are fully safe, so I don't feel anything bad about it.

An example of using libeth_xdp can be found either on my GitHub or on the mailing lists here ("XDP for idpf"). Macros for building driver XDP functions lead to that some implementations (XDP_TX, ndo_xdp_xmit etc.) consist of really only a few lines.

Alexander Lobakin (17):
libeth, libie: clean symbol exports up a little
libeth: convert to netmem
libeth: support native XDP and register memory model
libeth: xdp: add XDP_TX buffers sending
libeth: xdp: add .ndo_xdp_xmit() helpers
libeth: xdp: add XDPSQE completion helpers
libeth: xdp: add XDPSQ locking helpers
libeth: xdp: add XDPSQ cleanup timers
libeth: xdp: add helpers for preparing/processing &libeth_xdp_buff
libeth: xdp: add XDP prog run and verdict result handling
libeth: xdp: add templates for building driver-side callbacks
libeth: xdp: add RSS hash hint and XDP features setup helpers
libeth: xsk: add XSk XDP_TX sending helpers
libeth: xsk: add XSk xmit functions
libeth: xsk: add XSk Rx processing support
libeth: xsk: add XSkFQ refill and XSk wakeup helpers
libeth: xdp, xsk: access adjacent u32s as u64 where applicable

 drivers/net/ethernet/intel/libeth/Kconfig     |   10 +-
 drivers/net/ethernet/intel/libeth/Makefile    |    8 +-
 include/net/libeth/types.h                    |  106 +-
 drivers/net/ethernet/intel/libeth/priv.h      |   37 +
 include/net/libeth/rx.h                       |   28 +-
 include/net/libeth/tx.h                       |   36 +-
 include/net/libeth/xdp.h                      | 1879 +++++++++++++++++
 include/net/libeth/xsk.h                      |  685 ++++++
 drivers/net/ethernet/intel/iavf/iavf_txrx.c   |   14 +-
 .../ethernet/intel/idpf/idpf_singleq_txrx.c   |    2 +-
 drivers/net/ethernet/intel/idpf/idpf_txrx.c   |   36 +-
 drivers/net/ethernet/intel/libeth/rx.c        |   42 +-
 drivers/net/ethernet/intel/libeth/tx.c        |   41 +
 drivers/net/ethernet/intel/libeth/xdp.c       |  451 ++++
 drivers/net/ethernet/intel/libeth/xsk.c       |  271 +++
 drivers/net/ethernet/intel/libie/rx.c         |    7 +-
 16 files changed, 3596 insertions(+), 57 deletions(-)
 create mode 100644 drivers/net/ethernet/intel/libeth/priv.h
 create mode 100644 include/net/libeth/xdp.h
 create mode 100644 include/net/libeth/xsk.h
 create mode 100644 drivers/net/ethernet/intel/libeth/tx.c
 create mode 100644 drivers/net/ethernet/intel/libeth/xdp.c
 create mode 100644 drivers/net/ethernet/intel/libeth/xsk.c

---
Was a part of "XDP for idpf" series, now submitted separately with proper splitting into atomic commits. I hope 17 patches out of 15 maximum allowed is not a problem.

All checkpatch warnings are false-positives unless someone disagrees. Checked with --strict --codespell, a well as building with W=12 and sparse/smatch.
W=12 is clean, sparse only warns about "context imbalance", but it's intended that libeth_xdp_tx_init_bulk() takes rcu_read_lock() and libeth_xdp_rx_finalize() does rcu_read_unlock(). This is how Eth drivers with enabled XDP do: lock before the NAPI polling loop, unlock after the loop is done and the pending XDP_TX and XDP_REDIRECT frames/maps are flushed (both functions are always required to be called when using libeth_xdp helpers on Rx).

From v1[0]:
* move EXPORT_SYMBOL*() stuff to a separate (1st) commit;
* annotate a couple big objects on the stack with __uninitialized to mitigate performance loss with CONFIG_INIT_STACK_ALL_* (Kees);
* 0001: idpf: drop invalid packets if header split workaround failed (Jakub);
* 0003: don't support XDP + netmem for now until the core is ready for this (Jakub);
* 0009: use new __DEFINE_FLEX() after the changes from the hardening PR (Kees).

[0] https://lore.kernel.org/netdev/[email protected]

Signature · 1 line
2.49.0