From patchwork Sun Mar 12 12:30:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 95176 Delivered-To: patch@linaro.org Received: by 10.140.82.71 with SMTP id g65csp784916qgd; Sun, 12 Mar 2017 05:37:45 -0700 (PDT) X-Received: by 10.98.159.149 with SMTP id v21mr32238902pfk.57.1489322265394; Sun, 12 Mar 2017 05:37:45 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id z5si8644454pgn.374.2017.03.12.05.37.44; Sun, 12 Mar 2017 05:37:45 -0700 (PDT) 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 9F844607FD; Sun, 12 Mar 2017 12:37:44 +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_H2, 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 40A9E60A2E; Sun, 12 Mar 2017 12:32:03 +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 3B2C363CB3; Sun, 12 Mar 2017 12:31:58 +0000 (UTC) Received: from mail-oi0-f48.google.com (mail-oi0-f48.google.com [209.85.218.48]) by lists.linaro.org (Postfix) with ESMTPS id 6930963C94 for ; Sun, 12 Mar 2017 12:30:59 +0000 (UTC) Received: by mail-oi0-f48.google.com with SMTP id 2so67862123oif.0 for ; Sun, 12 Mar 2017 05:30:59 -0700 (PDT) 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=Eqy28sdBs4VzcJkTkeEgAv4AFOQjJa3f9n9DtwaJke4=; b=W2JrImksQABBaMFmk/MPSoPG/7RWDcFK440K+NtbPlHInK36SqyGskmkPDlP4n8+ZT Fzlyp8bQMKEBk+1NVHugBfoaubUc45u/ljTbL/BCtpbLOYY/hit/TsHc2JOsx2PBApxH 3+gx8x9yLQG5Gioku9h2YiMdN+rUJ9tS1GbooQKHjlTDsvHS/xMuJh3rLX91DoMmHQbl 4hLQSxjjYXEsoBYF5JHD+s9gywd+8QoTQhHTUsd6mPYJ5HNzvQGcz1TMh8X4TCbqD3M6 1R9UpaFA596ucUmH5I+NdSoamIwTUE763KXwI5U39GvOmCUEiw1NexPJL0WckO8nkHSl fgFg== X-Gm-Message-State: AMke39kg1QN/hrfAtjN9d5WIkCQHBYIfanksOCH2Y8WZtprdz4Iwp6fBGTqw9J146JEU/5x4ZThUdw== X-Received: by 10.202.76.215 with SMTP id z206mr14946664oia.68.1489321858575; Sun, 12 Mar 2017 05:30:58 -0700 (PDT) Received: from localhost.localdomain (cpe-70-121-83-241.austin.res.rr.com. [70.121.83.241]) by smtp.gmail.com with ESMTPSA id 62sm6663155otq.34.2017.03.12.05.30.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 12 Mar 2017 05:30:58 -0700 (PDT) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Sun, 12 Mar 2017 07:30:43 -0500 Message-Id: <20170312123045.3852-10-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.12.0.rc1 In-Reply-To: <20170312123045.3852-1-bill.fischofer@linaro.org> References: <20170312123045.3852-1-bill.fischofer@linaro.org> Subject: [lng-odp] [PATCHv2 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.12.0.rc1 diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index c9cb3ef2..9ddc8522 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;