return -1;
}
+static int
+test_pktmbuf_copy(struct rte_mempool *pktmbuf_pool)
+{
+ struct rte_mbuf *m = NULL;
+ struct rte_mbuf *copy = NULL;
+ struct rte_mbuf *copy2 = NULL;
+ struct rte_mbuf *clone = NULL;
+ unaligned_uint32_t *data;
+
+ /* alloc a mbuf */
+ m = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m == NULL)
+ GOTO_FAIL("ooops not allocating mbuf");
+
+ if (rte_pktmbuf_pkt_len(m) != 0)
+ GOTO_FAIL("Bad length");
+
+ rte_pktmbuf_append(m, sizeof(uint32_t));
+ data = rte_pktmbuf_mtod(m, unaligned_uint32_t *);
+ *data = MAGIC_DATA;
+
+ /* copy the allocated mbuf */
+ copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
+ if (copy == NULL)
+ GOTO_FAIL("cannot copy data\n");
+
+ if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
+ GOTO_FAIL("copy length incorrect\n");
+
+ if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
+ GOTO_FAIL("copy data length incorrect\n");
+
+ data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
+ if (*data != MAGIC_DATA)
+ GOTO_FAIL("invalid data in copy\n");
+
+ /* free the copy */
+ rte_pktmbuf_free(copy);
+ copy = NULL;
+
+ /* same test with a cloned mbuf */
+ clone = rte_pktmbuf_clone(m, pktmbuf_pool);
+ if (clone == NULL)
+ GOTO_FAIL("cannot clone data\n");
+
+ if (!RTE_MBUF_CLONED(clone))
+ GOTO_FAIL("clone did not give a cloned mbuf\n");
+
+ copy = rte_pktmbuf_copy(clone, pktmbuf_pool, 0, UINT32_MAX);
+ if (copy == NULL)
+ GOTO_FAIL("cannot copy cloned mbuf\n");
+
+ if (RTE_MBUF_CLONED(copy))
+ GOTO_FAIL("copy of clone is cloned?\n");
+
+ if (rte_pktmbuf_pkt_len(copy) != sizeof(uint32_t))
+ GOTO_FAIL("copy clone length incorrect\n");
+
+ if (rte_pktmbuf_data_len(copy) != sizeof(uint32_t))
+ GOTO_FAIL("copy clone data length incorrect\n");
+
+ data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
+ if (*data != MAGIC_DATA)
+ GOTO_FAIL("invalid data in clone copy\n");
+ rte_pktmbuf_free(clone);
+ rte_pktmbuf_free(copy);
+ copy = NULL;
+ clone = NULL;
+
+
+ /* same test with a chained mbuf */
+ m->next = rte_pktmbuf_alloc(pktmbuf_pool);
+ if (m->next == NULL)
+ GOTO_FAIL("Next Pkt Null\n");
+ m->nb_segs = 2;
+
+ rte_pktmbuf_append(m->next, sizeof(uint32_t));
+ m->pkt_len = 2 * sizeof(uint32_t);
+ data = rte_pktmbuf_mtod(m->next, unaligned_uint32_t *);
+ *data = MAGIC_DATA + 1;
+
+ copy = rte_pktmbuf_copy(m, pktmbuf_pool, 0, UINT32_MAX);
+ if (copy == NULL)
+ GOTO_FAIL("cannot copy data\n");
+
+ if (rte_pktmbuf_pkt_len(copy) != 2 * sizeof(uint32_t))
+ GOTO_FAIL("chain copy length incorrect\n");
+
+ if (rte_pktmbuf_data_len(copy) != 2 * sizeof(uint32_t))
+ GOTO_FAIL("chain copy data length incorrect\n");
+
+ data = rte_pktmbuf_mtod(copy, unaligned_uint32_t *);
+ if (data[0] != MAGIC_DATA || data[1] != MAGIC_DATA + 1)
+ GOTO_FAIL("invalid data in copy\n");
+
+ rte_pktmbuf_free(copy2);
+
+ /* test offset copy */
+ copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
+ sizeof(uint32_t), UINT32_MAX);
+ if (copy2 == NULL)
+ GOTO_FAIL("cannot copy the copy\n");
+
+ if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
+ GOTO_FAIL("copy with offset, length incorrect\n");
+
+ if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
+ GOTO_FAIL("copy with offset, data length incorrect\n");
+
+ data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
+ if (data[0] != MAGIC_DATA + 1)
+ GOTO_FAIL("copy with offset, invalid data\n");
+
+ rte_pktmbuf_free(copy2);
+
+ /* test truncation copy */
+ copy2 = rte_pktmbuf_copy(copy, pktmbuf_pool,
+ 0, sizeof(uint32_t));
+ if (copy2 == NULL)
+ GOTO_FAIL("cannot copy the copy\n");
+
+ if (rte_pktmbuf_pkt_len(copy2) != sizeof(uint32_t))
+ GOTO_FAIL("copy with truncate, length incorrect\n");
+
+ if (rte_pktmbuf_data_len(copy2) != sizeof(uint32_t))
+ GOTO_FAIL("copy with truncate, data length incorrect\n");
+
+ data = rte_pktmbuf_mtod(copy2, unaligned_uint32_t *);
+ if (data[0] != MAGIC_DATA)
+ GOTO_FAIL("copy with truncate, invalid data\n");
+
+ /* free mbuf */
+ rte_pktmbuf_free(m);
+ rte_pktmbuf_free(copy);
+ rte_pktmbuf_free(copy2);
+
+ m = NULL;
+ copy = NULL;
+ copy2 = NULL;
+ printf("%s ok\n", __func__);
+ return 0;
+
+fail:
+ if (m)
+ rte_pktmbuf_free(m);
+ if (copy)
+ rte_pktmbuf_free(copy);
+ if (copy2)
+ rte_pktmbuf_free(copy2);
+ return -1;
+}
+
static int
test_attach_from_different_pool(struct rte_mempool *pktmbuf_pool,
struct rte_mempool *pktmbuf_pool2)
goto err;
}
+ if (test_pktmbuf_copy(pktmbuf_pool) < 0) {
+ printf("test_pktmbuf_copy() failed\n");
+ goto err;
+ }
+
if (test_attach_from_different_pool(pktmbuf_pool, pktmbuf_pool2) < 0) {
printf("test_attach_from_different_pool() failed\n");
goto err;
return 0;
}
+/* Create a deep copy of mbuf */
+struct rte_mbuf *
+rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
+ uint32_t off, uint32_t len)
+{
+ const struct rte_mbuf *seg = m;
+ struct rte_mbuf *mc, *m_last, **prev;
+
+ /* garbage in check */
+ __rte_mbuf_sanity_check(m, 1);
+
+ /* check for request to copy at offset past end of mbuf */
+ if (unlikely(off >= m->pkt_len))
+ return NULL;
+
+ mc = rte_pktmbuf_alloc(mp);
+ if (unlikely(mc == NULL))
+ return NULL;
+
+ /* truncate requested length to available data */
+ if (len > m->pkt_len - off)
+ len = m->pkt_len - off;
+
+ __rte_pktmbuf_copy_hdr(mc, m);
+
+ /* copied mbuf is not indirect or external */
+ mc->ol_flags = m->ol_flags & ~(IND_ATTACHED_MBUF|EXT_ATTACHED_MBUF);
+
+ prev = &mc->next;
+ m_last = mc;
+ while (len > 0) {
+ uint32_t copy_len;
+
+ /* skip leading mbuf segments */
+ while (off >= seg->data_len) {
+ off -= seg->data_len;
+ seg = seg->next;
+ }
+
+ /* current buffer is full, chain a new one */
+ if (rte_pktmbuf_tailroom(m_last) == 0) {
+ m_last = rte_pktmbuf_alloc(mp);
+ if (unlikely(m_last == NULL)) {
+ rte_pktmbuf_free(mc);
+ return NULL;
+ }
+ ++mc->nb_segs;
+ *prev = m_last;
+ prev = &m_last->next;
+ }
+
+ /*
+ * copy the min of data in input segment (seg)
+ * vs space available in output (m_last)
+ */
+ copy_len = RTE_MIN(seg->data_len - off, len);
+ if (copy_len > rte_pktmbuf_tailroom(m_last))
+ copy_len = rte_pktmbuf_tailroom(m_last);
+
+ /* append from seg to m_last */
+ rte_memcpy(rte_pktmbuf_mtod_offset(m_last, char *,
+ m_last->data_len),
+ rte_pktmbuf_mtod_offset(seg, char *, off),
+ copy_len);
+
+ /* update offsets and lengths */
+ m_last->data_len += copy_len;
+ mc->pkt_len += copy_len;
+ off += copy_len;
+ len -= copy_len;
+ }
+
+ /* garbage out check */
+ __rte_mbuf_sanity_check(mc, 1);
+ return mc;
+}
+
/* dump a mbuf on console */
void
rte_pktmbuf_dump(FILE *f, const struct rte_mbuf *m, unsigned dump_len)
*/
#define rte_pktmbuf_detach_extbuf(m) rte_pktmbuf_detach(m)
+/* internal */
+static inline void
+__rte_pktmbuf_copy_hdr(struct rte_mbuf *mdst, const struct rte_mbuf *msrc)
+{
+ mdst->port = msrc->port;
+ mdst->vlan_tci = msrc->vlan_tci;
+ mdst->vlan_tci_outer = msrc->vlan_tci_outer;
+ mdst->tx_offload = msrc->tx_offload;
+ mdst->hash = msrc->hash;
+ mdst->packet_type = msrc->packet_type;
+ mdst->timestamp = msrc->timestamp;
+}
+
/**
* Attach packet mbuf to another packet mbuf.
*
mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
}
- mi->buf_iova = m->buf_iova;
- mi->buf_addr = m->buf_addr;
- mi->buf_len = m->buf_len;
+ __rte_pktmbuf_copy_hdr(mi, m);
mi->data_off = m->data_off;
mi->data_len = m->data_len;
- mi->port = m->port;
- mi->vlan_tci = m->vlan_tci;
- mi->vlan_tci_outer = m->vlan_tci_outer;
- mi->tx_offload = m->tx_offload;
- mi->hash = m->hash;
+ mi->buf_iova = m->buf_iova;
+ mi->buf_addr = m->buf_addr;
+ mi->buf_len = m->buf_len;
mi->next = NULL;
mi->pkt_len = mi->data_len;
mi->nb_segs = 1;
- mi->packet_type = m->packet_type;
- mi->timestamp = m->timestamp;
__rte_mbuf_sanity_check(mi, 1);
__rte_mbuf_sanity_check(m, 0);
}
/**
- * Creates a "clone" of the given packet mbuf.
+ * Create a "clone" of the given packet mbuf.
*
* Walks through all segments of the given packet mbuf, and for each of them:
* - Creates a new packet mbuf from the given pool.
struct rte_mbuf *
rte_pktmbuf_clone(struct rte_mbuf *md, struct rte_mempool *mp);
+/**
+ * Create a full copy of a given packet mbuf.
+ *
+ * Copies all the data from a given packet mbuf to a newly allocated
+ * set of mbufs. The private data are is not copied.
+ *
+ * @param m
+ * The packet mbuf to be copiedd.
+ * @param mp
+ * The mempool from which the "clone" mbufs are allocated.
+ * @param offset
+ * The number of bytes to skip before copying.
+ * If the mbuf does not have that many bytes, it is an error
+ * and NULL is returned.
+ * @param length
+ * The upper limit on bytes to copy. Passing UINT32_MAX
+ * means all data (after offset).
+ * @return
+ * - The pointer to the new "clone" mbuf on success.
+ * - NULL if allocation fails.
+ */
+__rte_experimental
+struct rte_mbuf *
+rte_pktmbuf_copy(const struct rte_mbuf *m, struct rte_mempool *mp,
+ uint32_t offset, uint32_t length);
+
/**
* Adds given value to the refcnt of all packet mbuf segments.
*