DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Scott Mitchell <scott.k.mitch1@gmail.com>
Cc: dev@dpdk.org, Thomas Monjalon <thomas@monjalon.net>,
	David Marchand <david.marchand@redhat.com>
Subject: Re: [PATCH v7 0/4] net/af_packet: correctness, performance, cksum
Date: Tue, 1 Dec 2026 23:09:42 -0800	[thread overview]
Message-ID: <20261201230942.2e0f97ca@phoenix.local> (raw)
In-Reply-To: <20261201222405.88922-1-scott.k.mitch1@gmail.com>

On Tue,  1 Dec 2026 14:24:01 -0800
Scott Mitchell <scott.k.mitch1@gmail.com> wrote:

> Fixes and improvements for the af_packet PMD. Each patch adds tests to
> app/test/test_pmd_af_packet.c.
> 
> 1/4 fixes tp_status memory ordering and frame addressing.
> 2/4 bulk-frees TX mbufs.
> 3/4 adds a txpollnotrdy devarg so TX does not block in poll().
> 4/4 adds software TX checksum offload, shared with the tap driver.
> 
> Depends-on: patch-<fix id> ("test/cksum: fix EtherType of IPv6 TCP packet")

The long form AI review from Fable agrees with what I was going to
say about this series. There are sublties here because of differences
in atomics load/store vs fences and what C11 std is.

Review: [PATCH v7 0/4] net/af_packet fixes and tx features
Applied to main (ddb3b0c, 27.03-rc0). Each commit builds with
-Dwerror=true. Tests not run.

Series
------
The two real fixes in 1/4 are small (about 15 and 20 lines of driver
code) but arrive with 370 lines of test, new devarg validation and
unrelated cleanup, all under one Fixes tag and Cc: stable. That is
not backportable as is. Suggested split:

  1  fix tp_status memory ordering          (Fixes, stable)
  2  fix frame address with unused block tail (Fixes, stable)
  3  test: frame layout                      (stable optional)
  4  validate ring devargs                   (not stable)
  5  tx cleanup: overhead macro, bulk free, unlikely
  6  tx: non-blocking when ring is full
  7  net: add rte_net_ip_udptcp_cksum_mbuf() + test_cksum
  8  net/tap: use it
  9  net/af_packet: tx checksum offload + test

1/4 subject says "thread safety". Nothing here is about threads in
the application; it is ordering against the kernel side of the ring.


Patch 1/4
---------
Warning: ordering primitive

  tpacket_read_status() is a plain load followed by
  rte_atomic_thread_fence(acquire); tpacket_write_status() is a
  fence(release) followed by a plain store. The C11 model only gives
  a fence meaning relative to atomic operations, so this relies on
  what the compiler happens to emit. It does emit a barrier on arm64,
  so the generated code is right, but there is no reason not to use
  the defined form.

  What others do with the same ring:

    libpcap pcap-linux.c
        __atomic_load_n(&h.h2->tp_status, __ATOMIC_ACQUIRE)
        __atomic_store_n(&pkt->tp_status, TP_STATUS_KERNEL,
                         __ATOMIC_RELEASE)
    kernel net/packet/af_packet.c
        __packet_set_status: WRITE_ONCE(); smp_wmb()
        __packet_get_status: smp_rmb(); READ_ONCE()
        tpacket_rcv: smp_mb() between frame fill and status
    kernel selftest psock_tpacket.c
        plain store then __sync_synchronize()

  In DPDK, net/memif does load-acquire/store-release on the shared
  ring indexes and virtio does load-acquire on the weak-barrier
  path. Match libpcap and memif:

	static inline uint32_t
	tpacket_read_status(struct tpacket2_hdr *ppd)
	{
		return rte_atomic_load_explicit(
			(RTE_ATOMIC(uint32_t) *)&ppd->tp_status,
			rte_memory_order_acquire);
	}

	static inline void
	tpacket_write_status(struct tpacket2_hdr *ppd, uint32_t status)
	{
		rte_atomic_store_explicit(
			(RTE_ATOMIC(uint32_t) *)&ppd->tp_status, status,
			rte_memory_order_release);
	}

  tp_status is at offset 0 of a frame and frames are
  TPACKET_ALIGNMENT aligned, so the unaligned_uint32_t casts are
  not needed.

Warning: remove the new PMD_LOG(WARNING) block

  rte_eth_from_packet() is the probe path. A bad devarg is an error
  to return from probe, not something to warn about and carry on.
  As written, the three "must be" cases log a WARNING and then fail
  at setsockopt(PACKET_RX_RING) anyway, since they are exactly what
  packet_set_ring() rejects with EINVAL.

  Reject at probe, next to the existing framesize > blocksize test,
  so nothing later can see an invalid ring geometry:

	if (blocksize % pagesize != 0 ||
	    framesize % TPACKET_ALIGNMENT != 0 ||
	    framecount % frames_per_block != 0) {
		PMD_LOG(ERR,
			"%s: invalid AF_PACKET MMAP parameters", name);
		return -1;
	}

  Two of the sub-conditions in the patch cannot be true and go away:
    frames_per_block == 0
        framesize > blocksize already returned -1 above
    frames_per_block > UINT_MAX / blockcount
        blockcount = framecount / frames_per_block, so the product
        is <= framecount

  The two "hint" warnings (blocksize not a multiple of framesize,
  blocksize not a power of two) are valid configurations. The first
  is the layout the address fix in this patch exists to support.
  Drop both.

Info: num_rx_bytes/num_tx_bytes narrowed from unsigned long to
  uint32_t with no explanation. The queue counters are unsigned
  long. Drop this hunk, along with the int -> uint16_t and
  while -> do/while changes; none belong in a stable fix.


Patch 2/4
---------
Error: tx can stall permanently when the ring is full

	if (unlikely(num_tx > 0 &&
		     sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, ...

  sendto() is what makes the kernel walk SEND_REQUEST frames. Frames
  stay in SEND_REQUEST when tpacket_snd() bails out early: -ENETDOWN
  when the interface is down, or sock_alloc_send_skb() failing with
  MSG_DONTWAIT. The existing comment under this sendto() describes
  that case.

  Once every slot is SEND_REQUEST the next burst breaks at i == 0,
  num_tx is 0, and sendto() is never called again. Nothing else will
  move those frames, so the queue is dead even after the link comes
  back. Before this patch every tx_burst kicked the kernel and the
  ring drained.

  The test added in 3/4 builds exactly this state (link down, ring
  full, second burst returns 0) and then destroys the port. Bring
  the link up and burst again there and it will show the stall.

  Fix: keep the kick unconditional.

	if (unlikely(sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT,
			    NULL, 0) == -1 &&
		     errno != ENOBUFS && errno != EAGAIN)) {

Info: commit message says "RX/TX bulk free"; only tx is changed.
  The tp_len/tp_snaplen move is undone by 4/4.


Patch 3/4
---------
Warning: wrong fix for a blocking tx_burst

  tx_burst is not supposed to sleep. poll(-1) in the tx path is the
  bug; a devarg that lets the user opt out of it makes af_packet
  behave differently from every other ethdev and leaves the bad
  default. Remove the poll() and return short when the ring is full,
  which is the normal ethdev backpressure contract. No devarg, no doc
  change, and the test becomes "tx never blocks".

  If blocking has to stay for compatibility, the default should be
  non-blocking and the name should say what it does (tx_poll=1).

Warning: "bool sw_cksum" is added to struct pkt_tx_queue here but
  belongs to 4/4.

Info: the pfd setup is now conditional on txpollnotrdy; it saves
  nothing measurable and pfd.revents = 0 after memset() is redundant.
  The doc example command line gains txpollnotrdy=0, i.e. the
  example no longer shows defaults.


Patch 4/4
---------
Error: shared/indirect mbuf with L4 header not in first segment

  The length check was relaxed from the old tap test
  (data_len >= l2 + l3 + l4 header) to data_len >= l2 + l3 so the
  direct case can linearize. The copy path still needs the old
  condition:

	seg = rte_pktmbuf_copy(mbuf, mbuf->pool, 0, hdrlens);
	...
	rte_pktmbuf_adj(mbuf, hdrlens);
	rte_pktmbuf_chain(seg, mbuf);

  rte_pktmbuf_adj() returns NULL and does nothing when
  hdrlens > data_len. The result is seg (hdrlens bytes) chained in
  front of the untrimmed original: the headers go out twice and
  pkt_len is hdrlens too long. test_cksum_offload_cases() covers
  split headers only for the direct path.

	if (!RTE_MBUF_DIRECT(mbuf) || rte_mbuf_refcnt_read(mbuf) > 1) {
		struct rte_mbuf *seg;

		if (unlikely(rte_pktmbuf_data_len(mbuf) < hdrlens))
			return NULL;
		seg = rte_pktmbuf_copy(mbuf, mbuf->pool, 0, hdrlens);
		if (seg == NULL)
			return NULL;
		rte_pktmbuf_adj(mbuf, hdrlens);
		if (rte_pktmbuf_chain(seg, mbuf) != 0) {
			rte_pktmbuf_prepend(mbuf, hdrlens);
			rte_pktmbuf_free(seg);
			return NULL;
		}
		mbuf = seg;
	}

Warning: refcnt > 1 on a direct mbuf

  In that case rte_pktmbuf_adj() and rte_pktmbuf_chain() rewrite
  data_off, data_len and pkt_len of an rte_mbuf another holder still
  references. The other holder now sees a packet with its headers
  cut off. This was already true in tap, but it is now a public
  lib/net function documented as safe for shared mbufs. The test
  only covers the indirect (clone) case, where the metadata is
  private. Either return NULL for refcnt > 1 (as rte_vlan_insert()
  does) or copy the whole packet.

Warning: af_packet does not need to touch the mbuf

  The driver copies every packet into the ring frame. Compute the
  checksums there, after the copy and before the status write. The
  frame is contiguous and private, so there is no allocation, no
  linearize, no shared/indirect special case and no bufs[i] rewrite:

	static inline bool
	af_packet_tx_cksum(uint8_t *frame, const struct rte_mbuf *m)
	{
		uint64_t l4 = m->ol_flags & RTE_MBUF_F_TX_L4_MASK;
		uint32_t off = m->l2_len + m->l3_len;
		void *l3 = frame + m->l2_len;
		unaligned_uint16_t *ck;

		struct rte_udp_hdr *udp = (void *)(frame + off);
		struct rte_tcp_hdr *tcp = (void *)(frame + off);

		if (l4 == RTE_MBUF_F_TX_UDP_CKSUM)
			ck = &udp->dgram_cksum;
		else if (l4 == RTE_MBUF_F_TX_TCP_CKSUM)
			ck = &tcp->cksum;
		else if (l4 == RTE_MBUF_F_TX_L4_NO_CKSUM)
			ck = NULL;
		else
			return false;
		/* caller checks off + L4 header <= pkt_len */

		if (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
			struct rte_ipv4_hdr *ip = l3;

			ip->hdr_checksum = 0;
			ip->hdr_checksum = rte_ipv4_cksum(ip);
		}
		if (ck != NULL) {
			*ck = 0;
			*ck = (m->ol_flags & RTE_MBUF_F_TX_IPV4) ?
				rte_ipv4_udptcp_cksum(l3, frame + off) :
				rte_ipv6_udptcp_cksum(l3, frame + off);
		}
		return true;
	}

  That leaves rte_net_ip_udptcp_cksum_mbuf() with tap as its only
  user, which weakens the case for a new lib/net API. If it stays,
  it goes in its own patch with the test_cksum changes, and the tap
  conversion in another.

      parent reply	other threads:[~2026-12-02  7:09 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-27 18:13 [PATCH v1 0/3] net/af_packet: correctness fixes and improvements scott.k.mitch1
2026-01-27 18:13 ` [PATCH v1 1/3] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-27 18:39   ` Stephen Hemminger
2026-01-28  1:35     ` Scott Mitchell
2026-01-27 18:13 ` [PATCH v1 2/3] net/af_packet: RX/TX rte_memcpy, bulk free, prefetch scott.k.mitch1
2026-01-27 18:54   ` Stephen Hemminger
2026-01-28  1:23     ` Scott Mitchell
2026-01-28  9:49       ` Morten Brørup
2026-01-28 15:37         ` Scott Mitchell
2026-01-28 16:57           ` Stephen Hemminger
2026-01-27 18:13 ` [PATCH v1 3/3] net/af_packet: software checksum and tx poll control scott.k.mitch1
2026-01-27 18:57   ` Stephen Hemminger
2026-01-28  7:05     ` Scott Mitchell
2026-01-28 17:36       ` Stephen Hemminger
2026-01-28 18:59         ` Scott Mitchell
2026-01-27 20:45   ` [REVIEW] " Stephen Hemminger
2026-01-28  9:36 ` [PATCH v2 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-28 16:59     ` Stephen Hemminger
2026-01-28 18:00       ` Scott Mitchell
2026-01-28 18:28         ` Stephen Hemminger
2026-01-28  9:36   ` [PATCH v2 2/4] net/af_packet: RX/TX unlikely, bulk free, prefetch scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 4/4] net/af_packet: software checksum scott.k.mitch1
2026-01-28 18:27     ` Stephen Hemminger
2026-01-28 19:08       ` Scott Mitchell
2026-01-28 19:10   ` [PATCH v3 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 2/4] net/af_packet: RX/TX unlikely, bulk free, prefetch scott.k.mitch1
2026-01-29  1:07       ` Stephen Hemminger
2026-02-02  5:29         ` Scott Mitchell
2026-01-28 19:10     ` [PATCH v3 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 4/4] net/af_packet: software checksum scott.k.mitch1
2026-01-28 21:57       ` [REVIEW] " Stephen Hemminger
2026-02-02  7:55         ` Scott Mitchell
2026-02-02 16:58           ` Stephen Hemminger
2026-02-02  8:14     ` [PATCH v4 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-02 17:00         ` Stephen Hemminger
2026-02-02 18:47         ` Stephen Hemminger
2026-02-03  6:41           ` Scott Mitchell
2026-02-02 18:53       ` [PATCH v4 0/4] af_packet correctness, performance, cksum Stephen Hemminger
2026-02-03  7:07       ` [PATCH v5 " scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-03  8:20           ` Scott Mitchell
2026-02-03 14:12             ` Stephen Hemminger
2026-02-04  2:59               ` Scott Mitchell
2026-02-03 14:13           ` Stephen Hemminger
2026-02-04  1:39             ` Scott Mitchell
2026-02-05 21:27               ` Stephen Hemminger
2026-02-06  1:11         ` [PATCH v6 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-06  1:49           ` [PATCH v6 0/4] af_packet correctness, performance, cksum Stephen Hemminger
2026-02-06  4:45             ` Scott Mitchell
2026-02-06 14:36             ` Morten Brørup
2026-02-06 16:11               ` Stephen Hemminger
2026-12-01 22:24           ` [PATCH v7 0/4] net/af_packet: " Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 1/4] net/af_packet: fix thread safety and frame calculations Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 2/4] net/af_packet: RX/TX bulk free, unlikely hint Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 3/4] net/af_packet: tx poll control Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 4/4] net/af_packet: add software checksum offload support Scott Mitchell
2026-12-01 23:22               ` Scott Mitchell
2026-12-01 22:32             ` [PATCH v7 0/4] net/af_packet: correctness, performance, cksum Scott Mitchell
2026-12-02  7:09             ` Stephen Hemminger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201230942.2e0f97ca@phoenix.local \
    --to=stephen@networkplumber.org \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=scott.k.mitch1@gmail.com \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox