Skip to contentSkip to conversation

mbuf: fix packet copy

PATCH Superseded Morten Brørup 2 participants 4 messages Jan 15 – Jan 16

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mbuf: fix packet copy

Stephen,

As the author of rte_pktmbuf_copy(), can you please review this patch?

You might find my answers to Konstantin's review informative:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/#181914

Venlig hilsen / Kind regards,
-Morten Brørup

From: Morten Brørup [mailto:[email protected]]
Sent: Wednesday, 19 November 2025 13.04

Requests for copying the at the end of a packet incorrectly returned NULL, as if copying past the end of a packet.

When allocating copies from a mempool using pinned external buffers, the external flag was not preserved in these mbufs.

Fixes c3a90c381daa ("mbuf: add a copy routine")
---
 lib/mbuf/rte_mbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/mbuf/rte_mbuf.c +3 -3
index 0d931c7a15..e639aff03e 100644
@@ -675,7 +675,7 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
 	__rte_mbuf_sanity_check(m, 1);
 
 	/* check for request to copy at offset past end of mbuf */
-	if (unlikely(off >= m->pkt_len))
+	if (unlikely(off > m->pkt_len))
 		return NULL;
 
 	mc = rte_pktmbuf_alloc(mp);
@@ -688,8 +688,8 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
 
 	__rte_pktmbuf_copy_hdr(mc, m);
 
-	/* copied mbuf is not indirect or external */
-	mc->ol_flags = m->ol_flags & ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);
+	/* copy flags except indirect and external, and preserve flags of newly allocated mbuf */
+	mc->ol_flags |= m->ol_flags & ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);
 
 	prev = &mc->next;
 	m_last = mc;
Signature · 1 line
2.43.0
#1 lore raw

Forwarding an email is apparently a bad idea; patchwork picked it up as a new patch.

Venlig hilsen / Kind regards,
-Morten Brørup

Quoted 69 lines

-----Original Message-----
From: Morten Brørup [mailto:[email protected]]
Sent: Thursday, 15 January 2026 11.53
To: Stephen Hemminger
Cc: [email protected]; [email protected]
Subject: FW: [PATCH] mbuf: fix packet copy

Stephen,

As the author of rte_pktmbuf_copy(), can you please review this patch?

You might find my answers to Konstantin's review informative:
https://patchwork.dpdk.org/project/dpdk/patch/20251119120403.907511-1-
[email protected]/#181914

Venlig hilsen / Kind regards,
-Morten Brørup

From: Morten Brørup [mailto:[email protected]]
Sent: Wednesday, 19 November 2025 13.04

Requests for copying the at the end of a packet incorrectly returned NULL,
as if copying past the end of a packet.

When allocating copies from a mempool using pinned external buffers, the
external flag was not preserved in these mbufs.

Fixes c3a90c381daa ("mbuf: add a copy routine")
---
 lib/mbuf/rte_mbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/mbuf/rte_mbuf.c +1 -1
index 0d931c7a15..e639aff03e 100644
@@ -675,7 +675,7 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct
 rte_mempool *mp,
 	__rte_mbuf_sanity_check(m, 1);
 
 	/* check for request to copy at offset past end of mbuf */
-	if (unlikely(off >= m->pkt_len))
+	if (unlikely(off > m->pkt_len))
 		return NULL;
 
lib/mbuf/rte_mbuf.c +1 -2
 	mc = rte_pktmbuf_alloc(mp);
@@ -688,8 +688,8 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct
 rte_mempool *mp,
 
 	__rte_pktmbuf_copy_hdr(mc, m);
 
-	/* copied mbuf is not indirect or external */
-	mc->ol_flags = m->ol_flags &
 ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);
+	/* copy flags except indirect and external, and preserve flags of

newly allocated mbuf */

lib/mbuf/rte_mbuf.c +1 -0
+	mc->ol_flags |= m->ol_flags &

~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);

lib/mbuf/rte_mbuf.c +0 -1
 	prev = &mc->next;
 	m_last = mc;
--

2.43.0

#2 lore raw
In reply to Morten Brørup
Quoted 33 lines · On Thu, 15 Jan 2026 11:53:19 +0100 Morten Brørup <[email protected]>…

Stephen,

As the author of rte_pktmbuf_copy(), can you please review this patch?

You might find my answers to Konstantin's review informative:
https://patchwork.dpdk.org/project/dpdk/patch/[email protected]/#181914

Venlig hilsen / Kind regards,
-Morten Brørup

From: Morten Brørup [mailto:[email protected]]
Sent: Wednesday, 19 November 2025 13.04

Requests for copying the at the end of a packet incorrectly returned NULL, as if copying past the end of a packet.

When allocating copies from a mempool using pinned external buffers, the external flag was not preserved in these mbufs.

Fixes c3a90c381daa ("mbuf: add a copy routine")
---
 lib/mbuf/rte_mbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/mbuf/rte_mbuf.c +0 -0
index 0d931c7a15..e639aff03e 100644

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, · line 679

Quoted 6 lines
lib/mbuf/rte_mbuf.c +1 -1
@@ -675,7 +675,7 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
 	__rte_mbuf_sanity_check(m, 1);
 
 	/* check for request to copy at offset past end of mbuf */
-	if (unlikely(off >= m->pkt_len))
+	if (unlikely(off > m->pkt_len))
 		return NULL;

It makes more sense to return NULL (as error) rather than creating a 0 length mbuf in this corner case.

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, · line 692

Quoted 9 lines
lib/mbuf/rte_mbuf.c +2 -2
 	mc = rte_pktmbuf_alloc(mp);
@@ -688,8 +688,8 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
 
 	__rte_pktmbuf_copy_hdr(mc, m);
 
-	/* copied mbuf is not indirect or external */
-	mc->ol_flags = m->ol_flags & ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);
+	/* copy flags except indirect and external, and preserve flags of newly allocated mbuf */
+	mc->ol_flags |= m->ol_flags & ~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);

Should have space in expression.
At that point it is a new mbuf (the copy) so offload flags should be clear, not sure what the issue is here.
But hadn't expected usage of this function with an external mbuf pool.

Quoted 3 lines
lib/mbuf/rte_mbuf.c +0 -0
 	prev = &mc->next;
 	m_last = mc;
#3 lore raw
Quoted 46 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Friday, 16 January 2026 06.55

Quoted 3 lines · On Thu, 15 Jan 2026 11:53:19 +0100 Morten Brørup <[email protected]>…

Stephen,

As the author of rte_pktmbuf_copy(), can you please review this

patch?

Quoted 3 lines

You might find my answers to Konstantin's review informative:
https://patchwork.dpdk.org/project/dpdk/patch/20251119120403.907511-

[email protected]/#181914

Quoted 10 lines

Venlig hilsen / Kind regards,
-Morten Brørup

From: Morten Brørup [mailto:[email protected]]
Sent: Wednesday, 19 November 2025 13.04

Requests for copying the at the end of a packet incorrectly returned

NULL,

Quoted 3 lines

as if copying past the end of a packet.

When allocating copies from a mempool using pinned external buffers,

the

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct

Quoted 14 lines

external flag was not preserved in these mbufs.

Fixes c3a90c381daa ("mbuf: add a copy routine")
---
 lib/mbuf/rte_mbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/mbuf/rte_mbuf.c +0 -0
index 0d931c7a15..e639aff03e 100644
@@ -675,7 +675,7 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct

rte_mempool *mp,

Quoted 2 lines
lib/mbuf/rte_mbuf.c +0 -0
 	__rte_mbuf_sanity_check(m, 1);
 
Quoted 7 lines

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp, · line 679

Quoted 4 lines
lib/mbuf/rte_mbuf.c +1 -1
 	/* check for request to copy at offset past end of mbuf */
-	if (unlikely(off >= m->pkt_len))
+	if (unlikely(off > m->pkt_len))
 		return NULL;

It makes more sense to return NULL (as error) rather than creating a 0 length mbuf in this corner case.

As replied to Kontantin, 0 length buffers are perfectly valid, so a library should not optimize them away, on an assumption that they are useless.
E.g. consider TCP, which carries many empty packets, adding feedback information (ACK, SACK) to the TCP header. I know it's a very theoretical example stripping all headers and then adding headers again; but I can creatively imagine something like that. If a normal application don't want to deal with 0 length buffers, it can optimize them away.
But if an exotic application does want to deal with 0 length buffers, it would be a bug if the library optimized them away.

Quoted 10 lines

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct

Quoted 3 lines
lib/mbuf/rte_mbuf.c +0 -0
 	mc = rte_pktmbuf_alloc(mp);
@@ -688,8 +688,8 @@ rte_pktmbuf_copy(const struct rte_mbuf *m, struct

rte_mempool *mp,

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,

Quoted 5 lines
lib/mbuf/rte_mbuf.c +0 -2
 	__rte_pktmbuf_copy_hdr(mc, m);
 
-	/* copied mbuf is not indirect or external */
-	mc->ol_flags = m->ol_flags &

~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);

Quoted 6 lines

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct · line 690

Quoted 1 line
lib/mbuf/rte_mbuf.c +1 -0
+	/* copy flags except indirect and external, and preserve flags of

newly allocated mbuf */

lib/mbuf/rte_mbuf.c rte_pktmbuf_copy(const struct rte_mbuf *m, struct · line 691

Quoted 1 line
lib/mbuf/rte_mbuf.c +1 -0
+	mc->ol_flags |= m->ol_flags &

~(RTE_MBUF_F_INDIRECT|RTE_MBUF_F_EXTERNAL);

Should have space in expression.

Yes. Copy-paste bug. :-)

Quoted 4 lines

At that point it is a new mbuf (the copy) so offload flags should be clear, not sure
what the issue is here.
But hadn't expected usage of this function with an external mbuf pool.

I consider pinned external buffers exotic too, but we need to support them throughout DPDK. That's the downside of exotic features in core libraries.

Quoted 5 lines
Quoted 5 lines
lib/mbuf/rte_mbuf.c +0 -0
 	prev = &mc->next;
 	m_last = mc;