From patchwork Fri Mar 13 10:14:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "vadim.suraev@gmail.com" X-Patchwork-Id: 4018 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 0EB43568F; Fri, 13 Mar 2015 11:14:42 +0100 (CET) Received: from mail-wg0-f43.google.com (mail-wg0-f43.google.com [74.125.82.43]) by dpdk.org (Postfix) with ESMTP id 89C33A6A for ; Fri, 13 Mar 2015 11:14:40 +0100 (CET) Received: by wghl18 with SMTP id l18so22136909wgh.5 for ; Fri, 13 Mar 2015 03:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=9nodbLXQ1pHZMJXbJ9y/g21QpQe7w36OlznPQiL3v+M=; b=rY1dbd+rJHGU0+rTg9MYEG1gIjkmgCbvnsYO7gZV7n8e7pk8R+T3ZF3NnH5MHYmarN IyixXqNnm+zVXl4+sudXMuoN6sy4zyzcFIm+zbN8NzJyoaBHIsVUr//pdnjlta7xF0CH t0zOwXOc0FKt9tAiQWtmRwPBhFPvFReHnYXgPlhYUXPClzCfe/a6KtaGRxARlJO73NMQ Kd7sLJly2iEcUWXfqqvcAGIJ+Z7JSY9uepAoqVPXeDJ8KJxyrqgMbujiDdH7uJIbFvBe DT3FFJz6me40WK93sHm6quWTZhaFf9JDxO0eFqQf2re7E08DybBX26tsHKoMkNzPx65t O6Ng== X-Received: by 10.180.90.113 with SMTP id bv17mr13925115wib.3.1426241680384; Fri, 13 Mar 2015 03:14:40 -0700 (PDT) Received: from ubuntu.ubuntu-domain (bzq-79-176-41-72.red.bezeqint.net. [79.176.41.72]) by mx.google.com with ESMTPSA id u16sm2169768wjr.5.2015.03.13.03.14.38 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 13 Mar 2015 03:14:39 -0700 (PDT) From: "vadim.suraev@gmail.com" To: dev@dpdk.org Date: Fri, 13 Mar 2015 12:14:23 +0200 Message-Id: <1426241664-26458-1-git-send-email-vadim.suraev@gmail.com> X-Mailer: git-send-email 1.7.9.5 Subject: [dpdk-dev] [PATCH] rte_mbuf: bulk allocation and freeing functions + unittest 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" From: "vadim.suraev@gmail.com" - an API function to allocate a bulk of rte_mbufs - an API function to free a bulk of rte_mbufs - an API function to free a chained rte_mbuf - unittest for aboce Signed-off-by: vadim.suraev@gmail.com --- app/test/test_mbuf.c | 73 ++++++++++++++++++++++++++++++++ lib/librte_mbuf/rte_mbuf.h | 101 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c index 1ff66cb..a557705 100644 --- a/app/test/test_mbuf.c +++ b/app/test/test_mbuf.c @@ -405,6 +405,67 @@ test_pktmbuf_pool(void) return ret; } +/* test pktmbuf bulk allocation and freeing +*/ +static int +test_pktmbuf_pool_bulk(void) +{ + unsigned i; + unsigned mbufs_to_allocate = NB_MBUF - 32; /* size of mempool - size of local cache, otherwise may fail */ + struct rte_mbuf *m[mbufs_to_allocate]; + int ret = 0; + unsigned mbuf_count_before_allocation = rte_mempool_count(pktmbuf_pool); + + for (i=0; inext = m[i + 1]; + m[0]->nb_segs++; + } + /* free them */ + rte_pktmbuf_free_chain(m[0]); + + if (rte_mempool_count(pktmbuf_pool) != mbuf_count_before_allocation) { + printf("mempool count %d != initial %d\n", + rte_mempool_count(pktmbuf_pool), mbuf_count_before_allocation); + return -1; + } + return ret; +} + /* * test that the pointer to the data on a packet mbuf is set properly */ @@ -790,6 +851,18 @@ test_mbuf(void) return -1; } + /* test bulk allocation and freeing */ + if (test_pktmbuf_pool_bulk() < 0) { + printf("test_pktmbuf_pool_bulk() failed\n"); + return -1; + } + + /* once again to ensure all mbufs were freed */ + if (test_pktmbuf_pool_bulk() < 0) { + printf("test_pktmbuf_pool_bulk() failed\n"); + return -1; + } + /* test that the pointer to the data on a packet mbuf is set properly */ if (test_pktmbuf_pool_ptr() < 0) { printf("test_pktmbuf_pool_ptr() failed\n"); diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h index 17ba791..482920e 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -825,6 +825,107 @@ static inline void rte_pktmbuf_free(struct rte_mbuf *m) } /** + * Allocates a bulk of mbufs, initiates refcnt and resets + * + * @param pool + * memory pool to allocate from + * @param mbufs + * Array of pointers to mbuf + * @param count + * Array size + */ +static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs, unsigned count) +{ + unsigned idx; + int rc = 0; + + if (unlikely(rc = rte_mempool_get_bulk(pool, (void **)mbufs, count))) { + return rc; + } + + for (idx = 0; idx < count; idx++) { + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0); + rte_mbuf_refcnt_set(mbufs[idx], 1); + rte_pktmbuf_reset(mbufs[idx]); + } + return rc; +} + +/** + * Frees a bulk of mbufs into its original mempool. + * It is responsibility of caller to update and check refcnt + * as well as to check for attached mbufs to be freed + * + * @param mbufs + * Array of pointers to mbuf + * @param count + * Array size + */ + +static inline void rte_pktmbuf_bulk_raw_free(struct rte_mbuf **mbufs, unsigned count) +{ + if (unlikely(count == 0)) + return; + rte_mempool_put_bulk(mbufs[0]->pool, (void **)mbufs, count); +} + +/** + * Frees a bulk of mbufs into its original mempool. + * This function assumes refcnt has been already decremented + * as well as the freed mbufs are direct + * + * @param mbufs + * Array of pointers to mbuf + * @param count + * Array size + */ + +static inline void rte_pktmbuf_bulk_free(struct rte_mbuf **mbufs, unsigned count) +{ + if (unlikely(count == 0)) + return; + unsigned idx; + + for(idx = 0; idx < count; idx++) { + rte_mbuf_refcnt_update(mbufs[idx], -1); + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) == 0); + } + rte_mempool_put_bulk(mbufs[0]->pool, (void **)mbufs, count); +} + +/** + * Frees chained (scattered) mbufs into its original mempool(s). + * + * @param head + * The head of mbufs to be freed chain + */ + +static inline void rte_pktmbuf_free_chain(struct rte_mbuf *head) +{ + if (likely(head != NULL)) { + struct rte_mbuf *mbufs[head->nb_segs]; + unsigned mbufs_count = 0; + struct rte_mbuf *next; + + while (head) { + next = head->next; + head->next = NULL; + if (likely(NULL != (head = __rte_pktmbuf_prefree_seg(head)))) { + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(head) == 0); + if (likely((!mbufs_count) || (head->pool == mbufs[0]->pool))) + mbufs[mbufs_count++] = head; + else { + rte_pktmbuf_bulk_raw_free(mbufs, mbufs_count); + mbufs_count = 0; + } + } + head = next; + } + rte_pktmbuf_bulk_raw_free(mbufs, mbufs_count); + } +} + +/** * Creates a "clone" of the given packet mbuf. * * Walks through all segments of the given packet mbuf, and for each of them: