mbuf: fix packet copy

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

When allocating the mbuf for the copy from a mempool using pinned external
buffers, the external flag in this mbuf was not preserved.

Fixes: c3a90c381daa ("mbuf: add a copy routine")

Signed-off-by: Morten Brørup <[email protected]>
Acked-by: Konstantin Ananyev <[email protected]>
---
v2:
* Improved comment about preserving flags for newly allocated mbuf
  potentially using pinned external buffer.
* Added missing spaces in expression. (Stephen)
---
 lib/mbuf/rte_mbuf.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/mbuf/rte_mbuf.c b/lib/mbuf/rte_mbuf.c
index 0d931c7a15..a5d16e4c97 100644
--- a/lib/mbuf/rte_mbuf.c
+++ b/lib/mbuf/rte_mbuf.c
@@ -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,12 @@ 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,
+        * while preserving flags of newly allocated mbuf
+        * (specifically RTE_MBUF_F_EXTERNAL if using pinned external buffer)
+        */
+       mc->ol_flags |= m->ol_flags & ~(RTE_MBUF_F_INDIRECT | 
RTE_MBUF_F_EXTERNAL);
 
        prev = &mc->next;
        m_last = mc;
-- 
2.43.0

Reply via email to