From patchwork Sun Mar 5 12:21:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 94905 Delivered-To: patch@linaro.org Received: by 10.140.82.71 with SMTP id g65csp989502qgd; Sun, 5 Mar 2017 04:27:39 -0800 (PST) X-Received: by 10.233.221.2 with SMTP id r2mr917412qkf.159.1488716859458; Sun, 05 Mar 2017 04:27:39 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id l76si13386805qkh.86.2017.03.05.04.27.39; Sun, 05 Mar 2017 04:27:39 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 2453560AD1; Sun, 5 Mar 2017 12:27:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 7B45963D29; Sun, 5 Mar 2017 12:22:42 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 8427360C0A; Sun, 5 Mar 2017 12:22:31 +0000 (UTC) Received: from mail-wm0-f43.google.com (mail-wm0-f43.google.com [74.125.82.43]) by lists.linaro.org (Postfix) with ESMTPS id 3234360A4D for ; Sun, 5 Mar 2017 12:21:38 +0000 (UTC) Received: by mail-wm0-f43.google.com with SMTP id n11so45133955wma.0 for ; Sun, 05 Mar 2017 04:21:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BMWWmJc2KSP9UYQADtoIAILgFntjUQ7acJq0ZeCsgwI=; b=t+Nk4TxyUOXpTNgKbixlj9rVeVCnhyCb1j9Rtgl70tJz28ZE6OvVXZgYt4/e5VUlwV mJ00sM4nEpcKyUsNslbzeCUxa23/+yIrCbNe+GMA4rvoLAxdujKlEaFyuY9ouz38Aota 6Pfbh5xHs6yCcE3wV+/ek5jsEkmbNAUBWxGgf3cJ+OUs+V5Lx5m/L7whvpuMfWUJXNlc uTJi45IuRhlmUJP0UjLRNp5+3M9SWu6++vXjRmfoQe7TjX+MtfdeI8/IL9oI29sO+xqE F7Y6UEmcMPlKyAKASVdj0J+tasjmQdlxnFZ8aW58e9XtpEADwSrBsmAKfDvfWNg2X7D4 Ok9Q== X-Gm-Message-State: AMke39mYaf5dCH/uL5EJpH6liwCBReVWOOcLeKr4oc4U/W7N8Q6nrnegStB06k06SVxlDoRYTSI= X-Received: by 10.28.103.3 with SMTP id b3mr9292570wmc.99.1488716497193; Sun, 05 Mar 2017 04:21:37 -0800 (PST) Received: from Ubuntu15.localdomain ([109.74.56.122]) by smtp.gmail.com with ESMTPSA id 127sm10686971wmt.20.2017.03.05.04.21.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 05 Mar 2017 04:21:36 -0800 (PST) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Sun, 5 Mar 2017 13:21:23 +0100 Message-Id: <1488716485-15002-10-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1488716485-15002-1-git-send-email-bill.fischofer@linaro.org> References: <1488716485-15002-1-git-send-email-bill.fischofer@linaro.org> Subject: [lng-odp] [PATCH 09/11] linux-generic: packet: restructure free routines for references X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Restructure the various packet/segment free routines to support references. Signed-off-by: Bill Fischofer --- platform/linux-generic/odp_packet.c | 146 +++++++++++++++++++++++++++--------- 1 file changed, 111 insertions(+), 35 deletions(-) -- 2.5.0 diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index c9cb3ef..9ddc852 100644 --- a/platform/linux-generic/odp_packet.c +++ b/platform/linux-generic/odp_packet.c @@ -478,13 +478,18 @@ static inline odp_packet_hdr_t *add_segments(odp_packet_hdr_t *pkt_hdr, static inline void free_bufs(odp_packet_hdr_t *pkt_hdr, int first, int num) { - int i; + int i, nfree; odp_buffer_t buf[num]; - for (i = 0; i < num; i++) - buf[i] = buffer_handle(pkt_hdr->buf_hdr.seg[first + i].hdr); + for (i = 0, nfree = 0; i < num; i++) { + odp_packet_hdr_t *hdr = pkt_hdr->buf_hdr.seg[first + i].hdr; + + if (packet_ref_count(hdr) == 1 || packet_ref_dec(hdr) == 1) + buf[nfree++] = buffer_handle(hdr); + } - buffer_free_multi(buf, num); + if (nfree > 0) + buffer_free_multi(buf, nfree); } static inline odp_packet_hdr_t *free_segments(odp_packet_hdr_t *pkt_hdr, @@ -495,11 +500,16 @@ static inline odp_packet_hdr_t *free_segments(odp_packet_hdr_t *pkt_hdr, if (head) { odp_packet_hdr_t *new_hdr; - int i; + int i, nfree; odp_buffer_t buf[num]; - for (i = 0; i < num; i++) - buf[i] = buffer_handle(pkt_hdr->buf_hdr.seg[i].hdr); + for (i = 0, nfree = 0; i < num; i++) { + new_hdr = pkt_hdr->buf_hdr.seg[i].hdr; + + if (packet_ref_count(new_hdr) == 1 || + packet_ref_dec(new_hdr) == 1) + buf[nfree++] = buffer_handle(new_hdr); + } /* First remaining segment is the new packet descriptor */ new_hdr = pkt_hdr->buf_hdr.seg[num].hdr; @@ -517,7 +527,8 @@ static inline odp_packet_hdr_t *free_segments(odp_packet_hdr_t *pkt_hdr, pkt_hdr = new_hdr; - buffer_free_multi(buf, num); + if (nfree > 0) + buffer_free_multi(buf, nfree); } else { /* Free last 'num' bufs */ free_bufs(pkt_hdr, num_remain, num); @@ -630,47 +641,109 @@ int odp_packet_alloc_multi(odp_pool_t pool_hdl, uint32_t len, return num; } -void odp_packet_free(odp_packet_t pkt) +static inline odp_packet_hdr_t *packet_free_to_list(odp_packet_hdr_t *pkt_hdr, + odp_buffer_t buf[], + int nbufs, + int *nfree) { - odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt); - odp_buffer_t hdl = buffer_handle(pkt_hdr); + odp_packet_hdr_t *ref_hdr; + uint32_t ref_count; + int num_seg, i; - int num_seg = pkt_hdr->buf_hdr.segcount; + do { + ref_count = packet_ref_count(pkt_hdr); + num_seg = pkt_hdr->buf_hdr.segcount; + ref_hdr = pkt_hdr->ref_hdr; - if (odp_likely(CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1)) - buffer_free_multi(&hdl, 1); - else - free_bufs(pkt_hdr, 0, num_seg); + if (odp_likely((CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1) && + ref_count == 1)) { + if (*nfree >= nbufs) + break; + + buf[(*nfree)++] = buffer_handle(pkt_hdr); + } else { + if (*nfree + num_seg >= nbufs) + break; + + for (i = 0; i < num_seg; i++) { + odp_packet_hdr_t *hdr = + pkt_hdr->buf_hdr.seg[i].hdr; + + if (packet_ref_count(hdr) == 1 || + packet_ref_dec(hdr) == 1) + buf[(*nfree)++] = buffer_handle(hdr); + } + } + + pkt_hdr = ref_hdr; + } while (pkt_hdr); + + return pkt_hdr; +} + +static inline void packet_free(odp_packet_hdr_t *pkt_hdr) +{ + odp_packet_hdr_t *ref_hdr; + uint32_t ref_count; + int num_seg; + + do { + ref_count = packet_ref_count(pkt_hdr); + num_seg = pkt_hdr->buf_hdr.segcount; + ref_hdr = pkt_hdr->ref_hdr; + + if (odp_likely((CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1) && + ref_count == 1)) + buffer_free_multi((odp_buffer_t *) + &pkt_hdr->buf_hdr.handle.handle, 1); + else + free_bufs(pkt_hdr, 0, num_seg); + + pkt_hdr = ref_hdr; + } while (pkt_hdr); +} + +void odp_packet_free(odp_packet_t pkt) +{ + packet_free(packet_hdr(pkt)); } void odp_packet_free_multi(const odp_packet_t pkt[], int num) { - if (CONFIG_PACKET_MAX_SEGS == 1) { - buffer_free_multi((const odp_buffer_t * const)pkt, num); - } else { - odp_buffer_t buf[num * CONFIG_PACKET_MAX_SEGS]; - int i, j; - int bufs = 0; + int nbufs = num * CONFIG_PACKET_MAX_SEGS * 2; + odp_buffer_t buf[nbufs]; + int nfree = 0; + int i = 0; + odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt[0]); + uint32_t pool_id = pool_id_from_buf(pkt_hdr->buf_hdr.handle.handle); + uint32_t next_pool; + + while (1) { + pkt_hdr = packet_free_to_list(pkt_hdr, buf, nbufs, &nfree); - for (i = 0; i < num; i++) { - odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt[i]); - int num_seg = pkt_hdr->buf_hdr.segcount; - odp_buffer_hdr_t *buf_hdr = &pkt_hdr->buf_hdr; + if (pkt_hdr) { + buffer_free_to_pool(pool_id, buf, nfree); + nfree = 0; + continue; + } - buf[bufs] = buffer_handle(pkt_hdr); - bufs++; + if (++i == num) + break; - if (odp_likely(num_seg == 1)) - continue; + pkt_hdr = packet_hdr(pkt[i]); + next_pool = pool_id_from_buf(pkt_hdr->buf_hdr.handle.handle); - for (j = 1; j < num_seg; j++) { - buf[bufs] = buffer_handle(buf_hdr->seg[j].hdr); - bufs++; + if (odp_unlikely(next_pool != pool_id)) { + if (nfree > 0) { + buffer_free_to_pool(pool_id, buf, nfree); + nfree = 0; } + pool_id = next_pool; } - - buffer_free_multi(buf, bufs); } + + if (nfree > 0) + buffer_free_to_pool(pool_id, buf, nfree); } int odp_packet_reset(odp_packet_t pkt, uint32_t len) @@ -681,6 +754,9 @@ int odp_packet_reset(odp_packet_t pkt, uint32_t len) if (len > pool->headroom + pool->data_size + pool->tailroom) return -1; + if (pkt_hdr->ref_hdr) + packet_free(pkt_hdr->ref_hdr); + packet_init(pkt_hdr, len, 0); return 0;