From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wg0-f46.google.com (mail-wg0-f46.google.com [74.125.82.46]) by dpdk.org (Postfix) with ESMTP id E02929AA0 for ; Tue, 17 Mar 2015 21:16:23 +0100 (CET) Received: by wgbcc7 with SMTP id cc7so17836384wgb.0 for ; Tue, 17 Mar 2015 13:16:23 -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=wdcEWS6q35yVfPfss4Gym+Wl38eDBRAn9uMeLzcU060=; b=VKcoQ+uUU/yD0V4b4pJCvQbJ7sPL7zMkFQfcGklFLZ6yvhcYsJUE9fYoHmFLwdbNT4 bwDZ+ikWE9JHrN51XjnvV6l/GYqQpxpR6mLVrqY+kPFR1cUyoEBzenASIS3g2LPcdqX5 J8lfQHP35ITmrVeYMRRN4knlf4EQuyueERarU+dXarZxl/1NBLk8mTuTYSGzkTF8Wry+ oxwEA5O3BSMo5asLivGfcBs8k2zZUNPE1QCgNdp5BDjY3g0RbX4fgCN7A5bA7TCvp5Ol 5aKVBkRrT+Q+BmV6iCAT7CqUBryhYx0khYU8PFFAEwvLY/fWAhJ4sDpehve9N/i7G2g2 f67g== X-Received: by 10.194.57.206 with SMTP id k14mr138038026wjq.1.1426623383773; Tue, 17 Mar 2015 13:16:23 -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 dc9sm81685wib.9.2015.03.17.13.16.22 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 17 Mar 2015 13:16:23 -0700 (PDT) From: "vadim.suraev@gmail.com" To: dev@dpdk.org Date: Tue, 17 Mar 2015 22:16:08 +0200 Message-Id: <1426623368-29748-1-git-send-email-vadim.suraev@gmail.com> X-Mailer: git-send-email 1.7.9.5 Subject: [dpdk-dev] [PATCH] rte_mbuf: mbuf bulk alloc/free functions added + 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: , X-List-Received-Date: Tue, 17 Mar 2015 20:16:24 -0000 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 above Signed-off-by: vadim.suraev@gmail.com --- app/test/test_mbuf.c | 94 +++++++++++++++++++++++++++++++++++++++++++- lib/librte_mbuf/rte_mbuf.h | 89 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 1 deletion(-) diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c index 1ff66cb..b20c6a4 100644 --- a/app/test/test_mbuf.c +++ b/app/test/test_mbuf.c @@ -77,6 +77,7 @@ #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF) #define MAKE_STRING(x) # x +#define MBUF_POOL_LOCAL_CACHE_SIZE 32 static struct rte_mempool *pktmbuf_pool = NULL; @@ -405,6 +406,84 @@ test_pktmbuf_pool(void) return ret; } +/* test pktmbuf bulk allocation and freeing +*/ +static int +test_pktmbuf_pool_bulk(void) +{ + unsigned i; + /* size of mempool - size of local cache, otherwise may fail */ + unsigned mbufs_to_allocate = NB_MBUF - MBUF_POOL_LOCAL_CACHE_SIZE; + struct rte_mbuf *m[mbufs_to_allocate]; + int ret = 0; + unsigned mbuf_count_before_allocation = rte_mempool_count(pktmbuf_pool); + + for (i = 0; i < mbufs_to_allocate; i++) + m[i] = NULL; + /* alloc NB_MBUF-MBUF_POOL_LOCAL_CACHE_SIZE mbufs */ + ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, m, mbufs_to_allocate); + if (ret) { + printf("cannot allocate %d mbufs bulk mempool_cnt=%d ret=%d\n", + mbufs_to_allocate, + rte_mempool_count(pktmbuf_pool), + ret); + return -1; + } + if ((rte_mempool_count(pktmbuf_pool) + mbufs_to_allocate) != + mbuf_count_before_allocation) { + printf("mempool count %d + allocated %d != initial %d\n", + rte_mempool_count(pktmbuf_pool), + mbufs_to_allocate, + mbuf_count_before_allocation); + return -1; + } + /* free them */ + rte_pktmbuf_bulk_free(m, mbufs_to_allocate); + + 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; + } + for (i = 0; i < mbufs_to_allocate; i++) + m[i] = NULL; + + /* alloc NB_MBUF-MBUF_POOL_LOCAL_CACHE_SIZE mbufs */ + ret = rte_pktmbuf_alloc_bulk(pktmbuf_pool, m, mbufs_to_allocate); + if (ret) { + printf("cannot allocate %d mbufs bulk mempool_cnt=%d ret=%d\n", + mbufs_to_allocate, + rte_mempool_count(pktmbuf_pool), + ret); + return -1; + } + if ((rte_mempool_count(pktmbuf_pool) + mbufs_to_allocate) != + mbuf_count_before_allocation) { + printf("mempool count %d + allocated %d != initial %d\n", + rte_mempool_count(pktmbuf_pool), + mbufs_to_allocate, + mbuf_count_before_allocation); + return -1; + } + + /* chain it */ + for (i = 0; i < mbufs_to_allocate - 1; i++) { + m[i]->next = 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 */ @@ -766,7 +845,8 @@ test_mbuf(void) if (pktmbuf_pool == NULL) { pktmbuf_pool = rte_mempool_create("test_pktmbuf_pool", NB_MBUF, - MBUF_SIZE, 32, + MBUF_SIZE, + MBUF_POOL_LOCAL_CACHE_SIZE, sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, NULL, @@ -790,6 +870,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..a5029a0 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -825,6 +825,95 @@ static inline void rte_pktmbuf_free(struct rte_mbuf *m) } /** + * Allocate a bulk of mbufs, initiate 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; + + rc = rte_mempool_get_bulk(pool, (void **)mbufs, count); + if (unlikely(rc)) + 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; +} + +/** + * Free a bulk of mbufs into its original mempool. + * This function assumes refcnt equals 0 + * 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) +{ + unsigned idx; + + RTE_MBUF_ASSERT(count > 0); + + 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); +} + +/** + * Free chained (scattered) mbufs into its original mempool(s). + * + * @param head + * The head of mbufs to be freed chain. Must not be NULL + */ +static inline void rte_pktmbuf_free_chain(struct rte_mbuf *head) +{ + struct rte_mbuf *mbufs[head->nb_segs]; + unsigned mbufs_count = 0; + struct rte_mbuf *next; + + while (head) { + next = head->next; + head->next = NULL; + head = __rte_pktmbuf_prefree_seg(head); + if (likely(head != NULL)) { + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(head) == 0); + if (likely((!mbufs_count) || + (head->pool == mbufs[0]->pool))) + mbufs[mbufs_count++] = head; + else { + rte_mempool_put_bulk(mbufs[0]->pool, + (void **)mbufs, + mbufs_count); + mbufs_count = 0; + } + } + head = next; + } + if (mbufs_count > 0) + rte_mempool_put_bulk(mbufs[0]->pool, + (void **)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: -- 1.7.9.5