From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wi0-f177.google.com (mail-wi0-f177.google.com [209.85.212.177]) by dpdk.org (Postfix) with ESMTP id 2B6A568A9 for ; Wed, 7 May 2014 13:38:53 +0200 (CEST) Received: by mail-wi0-f177.google.com with SMTP id f8so1140501wiw.16 for ; Wed, 07 May 2014 04:38:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=//+gxywfXXWXBBl3XsG8hCGwkCFmZxUc90nO4rxk85M=; b=aOB1WEAvu1D+CZNtRqGOZUVeTVKyqnboK9zTIZG7mafelu7pWPzA6fyBQH+ypYO3rZ 0bCHIAJV7rfP1gmuK8EkCaESGEgX+K+sx9wQSpWupYIwnihsxIw8ZBX4uGPuVdrkWZRy tmzH8g18a+6vK3JZXbqaUtNyLBBrOPS47zSrrk4+JhbEsuAGwQdL4BxollNrgX70h4RW agcq/Rv3NFWFPm4/FdbsqnKsXSRaWIzRis30XSOsivZUz+j15Ihghl2DRtRv8H2XDxhR 6nExHM5LSdvIbko7rlU5GB6WO8OhghvnqLHkoI1d7dP3RoezkkQNvcfers6hMsEw7hoj 3BYA== X-Gm-Message-State: ALoCoQn24jrcB7sKUp6LD4bQ9uDfSNiHUFIJQFCKDcawX6sghGfPRB3rTOAVJdKJEe/83+e2lyOP X-Received: by 10.180.21.180 with SMTP id w20mr7366178wie.34.1399462738927; Wed, 07 May 2014 04:38:58 -0700 (PDT) Received: from glumotte.dev.6wind.com (6wind.net2.nerim.net. [213.41.180.237]) by mx.google.com with ESMTPSA id s2sm31080513wia.7.2014.05.07.04.38.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 07 May 2014 04:38:58 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Date: Wed, 7 May 2014 13:38:31 +0200 Message-Id: <1399462711-12815-3-git-send-email-olivier.matz@6wind.com> X-Mailer: git-send-email 1.9.2 In-Reply-To: <1399462711-12815-1-git-send-email-olivier.matz@6wind.com> References: <1399462711-12815-1-git-send-email-olivier.matz@6wind.com> Subject: [dpdk-dev] [PATCH 2/2] ring: introduce rte_ring_init() 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: Wed, 07 May 2014 11:38:53 -0000 Allow to initialize a ring in an already allocated memory. The rte_ring_create() function that allocates a ring in a rte_memzone is still available and now uses the new rte_ring_init() function in order to factorize the code. Signed-off-by: Olivier Matz --- lib/librte_ring/rte_ring.c | 63 ++++++++++++++++++++++++++-------------------- lib/librte_ring/rte_ring.h | 51 +++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c index 4aa500f..a65f33e 100644 --- a/lib/librte_ring/rte_ring.c +++ b/lib/librte_ring/rte_ring.c @@ -112,18 +112,10 @@ ssize_t rte_ring_get_memsize(unsigned count) return sz; } -/* create the ring */ -struct rte_ring * -rte_ring_create(const char *name, unsigned count, int socket_id, - unsigned flags) +int +rte_ring_init(struct rte_ring *r, const char *name, unsigned count, + unsigned flags) { - char mz_name[RTE_MEMZONE_NAMESIZE]; - struct rte_ring *r; - const struct rte_memzone *mz; - ssize_t ring_size; - int mz_flags = 0; - struct rte_ring_list* ring_list = NULL; - /* compilation-time checks */ RTE_BUILD_BUG_ON((sizeof(struct rte_ring) & CACHE_LINE_MASK) != 0); @@ -140,11 +132,38 @@ rte_ring_create(const char *name, unsigned count, int socket_id, CACHE_LINE_MASK) != 0); #endif + /* init the ring structure */ + memset(r, 0, sizeof(*r)); + rte_snprintf(r->name, sizeof(r->name), "%s", name); + r->flags = flags; + r->prod.watermark = count; + r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ); + r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ); + r->prod.size = r->cons.size = count; + r->prod.mask = r->cons.mask = count-1; + r->prod.head = r->cons.head = 0; + r->prod.tail = r->cons.tail = 0; + + return 0; +} + +/* create the ring */ +struct rte_ring * +rte_ring_create(const char *name, unsigned count, int socket_id, + unsigned flags) +{ + char mz_name[RTE_MEMZONE_NAMESIZE]; + struct rte_ring *r; + const struct rte_memzone *mz; + ssize_t ring_size; + int mz_flags = 0; + struct rte_ring_list* ring_list = NULL; + /* check that we have an initialised tail queue */ - if ((ring_list = + if ((ring_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) { rte_errno = E_RTE_NO_TAILQ; - return NULL; + return NULL; } ring_size = rte_ring_get_memsize(count); @@ -163,26 +182,16 @@ rte_ring_create(const char *name, unsigned count, int socket_id, mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags); if (mz != NULL) { r = mz->addr; - - /* init the ring structure */ - memset(r, 0, sizeof(*r)); - rte_snprintf(r->name, sizeof(r->name), "%s", name); - r->flags = flags; - r->prod.watermark = count; - r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ); - r->cons.sc_dequeue = !!(flags & RING_F_SC_DEQ); - r->prod.size = r->cons.size = count; - r->prod.mask = r->cons.mask = count-1; - r->prod.head = r->cons.head = 0; - r->prod.tail = r->cons.tail = 0; - + /* no need to check return value here, we already checked the + * arguments above */ + rte_ring_init(r, name, count, flags); TAILQ_INSERT_TAIL(ring_list, r, next); } else { r = NULL; RTE_LOG(ERR, RING, "Cannot reserve memory\n"); } rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK); - + return r; } diff --git a/lib/librte_ring/rte_ring.h b/lib/librte_ring/rte_ring.h index e8493f2..c62e7d7 100644 --- a/lib/librte_ring/rte_ring.h +++ b/lib/librte_ring/rte_ring.h @@ -215,13 +215,54 @@ struct rte_ring { ssize_t rte_ring_get_memsize(unsigned count); /** + * Initialize a ring structure. + * + * Initialize a ring structure in memory pointed by "r". The size of the + * memory area must be large enough to store the ring structure and the + * object table. It is advised to use rte_ring_get_memsize() to get the + * appropriate size. + * + * The ring size is set to *count*, which must be a power of two. Water + * marking is disabled by default. The real usable ring size is + * *count-1* instead of *count* to differentiate a free ring from an + * empty ring. + * + * The ring is not added in RTE_TAILQ_RING global list. Indeed, the + * memory given by the caller may not be shareable among dpdk + * processes. + * + * @param r + * The pointer to the ring structure followed by the objects table. + * @param name + * The size of the ring. + * @param count + * The number of elements in the ring (must be a power of 2). + * @param flags + * An OR of the following: + * - RING_F_SP_ENQ: If this flag is set, the default behavior when + * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()`` + * is "single-producer". Otherwise, it is "multi-producers". + * - RING_F_SC_DEQ: If this flag is set, the default behavior when + * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()`` + * is "single-consumer". Otherwise, it is "multi-consumers". + * @return + * 0 on success, or a negative value on error. + */ +int rte_ring_init(struct rte_ring *r, const char *name, unsigned count, + unsigned flags); + +/** * Create a new ring named *name* in memory. * - * This function uses ``memzone_reserve()`` to allocate memory. Its size is - * set to *count*, which must be a power of two. Water marking is - * disabled by default. - * Note that the real usable ring size is *count-1* instead of - * *count*. + * This function uses ``memzone_reserve()`` to allocate memory. Then it + * calls rte_ring_init() to initialize an empty ring. + * + * The new ring size is set to *count*, which must be a power of + * two. Water marking is disabled by default. The real usable ring size + * is *count-1* instead of *count* to differentiate a free ring from an + * empty ring. + * + * The ring is added in RTE_TAILQ_RING list. * * @param name * The name of the ring. -- 1.9.2