From patchwork Thu Nov 24 08:56:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Matz X-Patchwork-Id: 17227 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 6C915558D; Thu, 24 Nov 2016 09:57:26 +0100 (CET) Received: from proxy.6wind.com (host.76.145.23.62.rev.coltfrance.com [62.23.145.76]) by dpdk.org (Postfix) with ESMTP id 7B3B34A59 for ; Thu, 24 Nov 2016 09:56:57 +0100 (CET) Received: from glumotte.dev.6wind.com (unknown [10.16.0.195]) by proxy.6wind.com (Postfix) with ESMTP id AE46B26D2B; Thu, 24 Nov 2016 09:56:52 +0100 (CET) From: Olivier Matz To: dev@dpdk.org, yuanhan.liu@linux.intel.com Cc: maxime.coquelin@redhat.com, huawei.xie@intel.com, stephen@networkplumber.org Date: Thu, 24 Nov 2016 09:56:37 +0100 Message-Id: <1479977798-13417-5-git-send-email-olivier.matz@6wind.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1479977798-13417-1-git-send-email-olivier.matz@6wind.com> References: <1479977798-13417-1-git-send-email-olivier.matz@6wind.com> Subject: [dpdk-dev] [PATCH 4/5] mbuf: new helper to copy data from a mbuf X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Signed-off-by: Olivier Matz --- app/test/test_mbuf.c | 7 +++++++ lib/librte_mbuf/rte_mbuf.h | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c index 5f1bc5d..73fd7df 100644 --- a/app/test/test_mbuf.c +++ b/app/test/test_mbuf.c @@ -451,6 +451,13 @@ testclone_testupdate_testdetach(void) GOTO_FAIL("invalid data"); if (data != check_data) GOTO_FAIL("data should have been copied"); + if (rte_pktmbuf_read_copy(m2, 0, sizeof(uint32_t), check_data) < 0) + GOTO_FAIL("cannot copy data"); + if (check_data[0] != MAGIC_DATA) + GOTO_FAIL("invalid data"); + if (data != check_data) + GOTO_FAIL("data should have been copied"); + /* free mbuf */ rte_pktmbuf_free(m); m = NULL; diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h index e898d25..edae89f 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -1643,7 +1643,7 @@ static inline int rte_pktmbuf_data_is_shared(const struct rte_mbuf *m, } /** - * @internal used by rte_pktmbuf_read(). + * @internal used by rte_pktmbuf_read() and rte_pktmbuf_read_copy(). */ void *__rte_pktmbuf_read(const struct rte_mbuf *m, uint32_t off, uint32_t len, void *buf); @@ -1728,6 +1728,36 @@ static inline int rte_pktmbuf_write(const struct rte_mbuf *m, } /** + * Copy data from a mbuf into a linear buffer + * + * @param m + * The pointer to the mbuf. + * @param off + * The offset of the data in the mbuf. + * @param len + * The amount of bytes to copy. + * @param buf + * The buffer where data is copied, it should be at least + * as large as len. + * @return + * - (0) on success + * - (-1) on error: mbuf is too small + */ +static inline int rte_pktmbuf_read_copy(const struct rte_mbuf *m, + uint32_t off, uint32_t len, void *buf) +{ + if (likely(off + len <= rte_pktmbuf_data_len(m))) { + rte_memcpy(buf, rte_pktmbuf_mtod_offset(m, char *, off), len); + return 0; + } + + if (__rte_pktmbuf_read(m, off, len, buf) == NULL) + return -1; + + return 0; +} + +/** * Chain an mbuf to another, thereby creating a segmented packet. * * Note: The implementation will do a linear walk over the segments to find