mbuf: add a copy routine
This is a commonly used operation that surprisingly the DPDK has not supported. The new rte_pktmbuf_copy does a deep copy of packet. This is a complete copy including meta-data. It handles the case where the source mbuf comes from a pool with larger data area than the destination pool. The routine also has options for skipping data, or truncating at a fixed length. This patch also introduces internal inline to copy the metadata fields of mbuf. Add a test for this new function, based of the clone tests. Signed-off-by: Stephen Hemminger <stephen@networkplumber.org> Acked-by: Olivier Matz <olivier.matz@6wind.com>
This commit is contained in:
parent
1d2db47c9f
commit
c3a90c381d
@ -399,6 +399,158 @@ testclone_testupdate_testdetach(struct rte_mempool *pktmbuf_pool)
|
||||
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)
|
||||
@ -1203,6 +1355,11 @@ test_mbuf(void)
|
||||
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;
|
||||
|
@ -321,6 +321,83 @@ __rte_pktmbuf_linearize(struct rte_mbuf *mbuf)
|
||||
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)
|
||||
|
@ -1684,6 +1684,19 @@ rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr,
|
||||
*/
|
||||
#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.
|
||||
*
|
||||
@ -1721,23 +1734,17 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
|
||||
mi->ol_flags = m->ol_flags | IND_ATTACHED_MBUF;
|
||||
}
|
||||
|
||||
__rte_pktmbuf_copy_hdr(mi, m);
|
||||
|
||||
mi->data_off = m->data_off;
|
||||
mi->data_len = m->data_len;
|
||||
mi->buf_iova = m->buf_iova;
|
||||
mi->buf_addr = m->buf_addr;
|
||||
mi->buf_len = m->buf_len;
|
||||
|
||||
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->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);
|
||||
@ -1908,7 +1915,7 @@ static inline void rte_pktmbuf_free(struct rte_mbuf *m)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -1927,6 +1934,32 @@ static inline void rte_pktmbuf_free(struct rte_mbuf *m)
|
||||
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.
|
||||
*
|
||||
|
@ -58,4 +58,6 @@ EXPERIMENTAL {
|
||||
global:
|
||||
|
||||
rte_mbuf_check;
|
||||
rte_pktmbuf_copy;
|
||||
|
||||
} DPDK_18.08;
|
||||
|
Loading…
Reference in New Issue
Block a user