From patchwork Fri Oct 9 12:59:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 54699 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by patches.linaro.org (Postfix) with ESMTPS id D9F7022FFC for ; Fri, 9 Oct 2015 12:59:56 +0000 (UTC) Received: by wisv5 with SMTP id v5sf28605051wis.0 for ; Fri, 09 Oct 2015 05:59:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:delivered-to:from:to:date :message-id:in-reply-to:references:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :mime-version:content-type:content-transfer-encoding:errors-to :sender:x-original-sender:x-original-authentication-results :mailing-list; bh=uZ5ZJ0KhHt1gYW9W0taCqsfx/2OVP2xFtOcyZ4xcjBs=; b=HP0B7poez9MZm7aHTri3AH9gZhaSETgAHceCuq/SJsBt92Pc4d1Uffy1D69LmBxHhQ VNYeaHDYUjULdEaFO6KEVMrLCWEOuZUV8G1uE1akipgVXh43HrQBaYhIR5unYDJAXivB 5hDI08KUbWcbeoRSl8MYfT7UIdecubwYgYv/nsp9Lnsv/TtFcppKxNdS3WBohfmtsL18 QnpRMa4lEhbBA2TsbTbSDctewPhs04YV2BN2nHvLdCNANSyD/1kq4X1LhYTUOoMsG08a wdTNqedOth76eV5EHx1MfqBDfNRQv8gYb7Tlam31vssdx3w25sTpojDyVg0RTjVnG8PN ojLQ== X-Gm-Message-State: ALoCoQlp7Q8FpPP2/ajWtlqG0vw+GlzSjMQlMOOodpDWK4EvAX+ZA4dsIB+mo8OPoy/ZwK0OaTsK X-Received: by 10.194.118.65 with SMTP id kk1mr2595782wjb.5.1444395596186; Fri, 09 Oct 2015 05:59:56 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.16.210 with SMTP id 79ls307546lfq.12.gmail; Fri, 09 Oct 2015 05:59:56 -0700 (PDT) X-Received: by 10.25.41.79 with SMTP id p76mr4302846lfp.16.1444395596033; Fri, 09 Oct 2015 05:59:56 -0700 (PDT) Received: from mail-lb0-f171.google.com (mail-lb0-f171.google.com. [209.85.217.171]) by mx.google.com with ESMTPS id t6si1114328lbw.144.2015.10.09.05.59.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Oct 2015 05:59:56 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.171 as permitted sender) client-ip=209.85.217.171; Received: by lbwr8 with SMTP id r8so79313073lbw.2 for ; Fri, 09 Oct 2015 05:59:55 -0700 (PDT) X-Received: by 10.112.199.170 with SMTP id jl10mr945400lbc.88.1444395595882; Fri, 09 Oct 2015 05:59:55 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1193273lbq; Fri, 9 Oct 2015 05:59:54 -0700 (PDT) X-Received: by 10.107.169.216 with SMTP id f85mr14130314ioj.73.1444395594750; Fri, 09 Oct 2015 05:59:54 -0700 (PDT) Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id l5si11025272igj.9.2015.10.09.05.59.53; Fri, 09 Oct 2015 05:59:54 -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; Received: by lists.linaro.org (Postfix, from userid 109) id 09549619B2; Fri, 9 Oct 2015 12:59:53 +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=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, 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 680A5619F3; Fri, 9 Oct 2015 12:59:19 +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 40665619B2; Fri, 9 Oct 2015 12:59:12 +0000 (UTC) Received: from mail-lb0-f174.google.com (mail-lb0-f174.google.com [209.85.217.174]) by lists.linaro.org (Postfix) with ESMTPS id 08B5261831 for ; Fri, 9 Oct 2015 12:59:11 +0000 (UTC) Received: by lbwr8 with SMTP id r8so79290832lbw.2 for ; Fri, 09 Oct 2015 05:59:10 -0700 (PDT) X-Received: by 10.112.133.42 with SMTP id oz10mr6599163lbb.62.1444395549956; Fri, 09 Oct 2015 05:59:09 -0700 (PDT) Received: from localhost.localdomain (ppp91-76-161-180.pppoe.mtu-net.ru. [91.76.161.180]) by smtp.gmail.com with ESMTPSA id h196sm271039lfg.21.2015.10.09.05.59.09 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Oct 2015 05:59:09 -0700 (PDT) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Fri, 9 Oct 2015 15:59:05 +0300 Message-Id: <1444395547-13968-3-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1444395547-13968-1-git-send-email-maxim.uvarov@linaro.org> References: <1444395547-13968-1-git-send-email-maxim.uvarov@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCHv4 2/4] linux-generic: packet reference count support X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: maxim.uvarov@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.171 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Signed-off-by: Maxim Uvarov --- .../linux-generic/include/odp_buffer_internal.h | 8 ++- .../linux-generic/include/odp_packet_internal.h | 18 ++++++ platform/linux-generic/odp_packet.c | 65 ++++++++++++++++++++-- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/platform/linux-generic/include/odp_buffer_internal.h b/platform/linux-generic/include/odp_buffer_internal.h index 7f3fa1c..8416113 100644 --- a/platform/linux-generic/include/odp_buffer_internal.h +++ b/platform/linux-generic/include/odp_buffer_internal.h @@ -66,7 +66,11 @@ _ODP_STATIC_ASSERT((ODP_CONFIG_PACKET_BUF_LEN_MAX % #define ODP_BUFFER_POOL_BITS ODP_BITSIZE(ODP_CONFIG_POOLS) #define ODP_BUFFER_SEG_BITS ODP_BITSIZE(ODP_BUFFER_MAX_SEG) -#define ODP_BUFFER_INDEX_BITS (32 - ODP_BUFFER_POOL_BITS - ODP_BUFFER_SEG_BITS) +#define ODP_PACKET_REFS_BITS ODP_BITSIZE(ODP_CONFIG_PACKET_REFS) + +#define ODP_BUFFER_INDEX_BITS (32 - ODP_BUFFER_POOL_BITS - \ + ODP_BUFFER_SEG_BITS - \ + ODP_PACKET_REFS_BITS) #define ODP_BUFFER_PREFIX_BITS (ODP_BUFFER_POOL_BITS + ODP_BUFFER_INDEX_BITS) #define ODP_BUFFER_MAX_POOLS (1 << ODP_BUFFER_POOL_BITS) #define ODP_BUFFER_MAX_BUFFERS (1 << ODP_BUFFER_INDEX_BITS) @@ -80,6 +84,7 @@ typedef union odp_buffer_bits_t { uint32_t u32; struct { #if ODP_BYTE_ORDER == ODP_BIG_ENDIAN + uint32_t ref:ODP_PACKET_REFS_BITS; uint32_t pool_id:ODP_BUFFER_POOL_BITS; uint32_t index:ODP_BUFFER_INDEX_BITS; uint32_t seg:ODP_BUFFER_SEG_BITS; @@ -87,6 +92,7 @@ typedef union odp_buffer_bits_t { uint32_t seg:ODP_BUFFER_SEG_BITS; uint32_t index:ODP_BUFFER_INDEX_BITS; uint32_t pool_id:ODP_BUFFER_POOL_BITS; + uint32_t ref:ODP_PACKET_REFS_BITS; #endif }; diff --git a/platform/linux-generic/include/odp_packet_internal.h b/platform/linux-generic/include/odp_packet_internal.h index ba2cd7e..2d6b58f 100644 --- a/platform/linux-generic/include/odp_packet_internal.h +++ b/platform/linux-generic/include/odp_packet_internal.h @@ -272,12 +272,30 @@ odp_packet_t _odp_packet_alloc(odp_pool_t pool_hdl); int _odp_packet_parse(odp_packet_hdr_t *pkt_hdr); +void _odp_packet_init(odp_packet_t pkt); +void _odp_packet_get(odp_packet_t pkt); +uint32_t _odp_packet_put(odp_packet_t pkt); + /* Convert a packet handle to a buffer handle */ odp_buffer_t _odp_packet_to_buffer(odp_packet_t pkt); /* Convert a buffer handle to a packet handle */ odp_packet_t _odp_packet_from_buffer(odp_buffer_t buf); +/* Internal debug function to check if packet has references + * + * @param pkt Packet handle + * @retval 0 Packet does not have references + * @retval 1 Packet has references +*/ +static inline int _odp_packet_refcheck(odp_packet_t pkt) +{ + odp_buffer_t buf = _odp_packet_to_buffer(pkt); + odp_buffer_hdr_t *hdr = odp_buf_to_hdr(buf); + + return odp_atomic_load_u32(&hdr->ref_count) - 1; +} + #ifdef __cplusplus } #endif diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index 209a6e6..f6c5509 100644 --- a/platform/linux-generic/odp_packet.c +++ b/platform/linux-generic/odp_packet.c @@ -28,28 +28,34 @@ odp_packet_t odp_packet_alloc(odp_pool_t pool_hdl, uint32_t len) { pool_entry_t *pool = odp_pool_to_entry(pool_hdl); + odp_packet_t pkt = ODP_PACKET_INVALID; if (pool->s.params.type != ODP_POOL_PACKET) return ODP_PACKET_INVALID; /* Handle special case for zero-length packets */ if (len == 0) { - odp_packet_t pkt = + pkt = (odp_packet_t)buffer_alloc(pool_hdl, pool->s.params.buf.size); - if (pkt != ODP_PACKET_INVALID) + if (pkt != ODP_PACKET_INVALID) { pull_tail(odp_packet_hdr(pkt), pool->s.params.buf.size); - + _odp_packet_init(pkt); + } return pkt; } - return (odp_packet_t)buffer_alloc(pool_hdl, len); + pkt = (odp_packet_t)buffer_alloc(pool_hdl, len); + if (pkt != ODP_PACKET_INVALID) + _odp_packet_init(pkt); + return pkt; } void odp_packet_free(odp_packet_t pkt) { - odp_buffer_free((odp_buffer_t)pkt); + if (!_odp_packet_put(pkt)) + odp_buffer_free((odp_buffer_t)pkt); } int odp_packet_reset(odp_packet_t pkt, uint32_t len) @@ -85,6 +91,46 @@ odp_event_t odp_packet_to_event(odp_packet_t pkt) return (odp_event_t)pkt; } +void _odp_packet_init(odp_packet_t pkt) +{ + odp_buffer_t buf = _odp_packet_to_buffer(pkt); + odp_buffer_hdr_t *hdr = odp_buf_to_hdr(buf); + + odp_atomic_store_u32(&hdr->ref_count, 1); +} + +void _odp_packet_get(odp_packet_t pkt) +{ + odp_buffer_t buf = _odp_packet_to_buffer(pkt); + odp_buffer_hdr_t *hdr = odp_buf_to_hdr(buf); + + odp_atomic_inc_u32(&hdr->ref_count); +} + +uint32_t _odp_packet_put(odp_packet_t pkt) +{ + odp_buffer_t buf = _odp_packet_to_buffer(pkt); + odp_buffer_hdr_t *hdr = odp_buf_to_hdr(buf); + + return odp_atomic_fetch_dec_u32(&hdr->ref_count) - 1; +} + +odp_packet_t odp_packet_create_ref(odp_packet_t pkt) +{ + odp_buffer_t buf = _odp_packet_to_buffer(pkt); + odp_buffer_bits_t handle; + + _odp_packet_get(pkt); + + handle.handle = buf; + if (handle.ref > ODP_CONFIG_PACKET_REFS) + ODP_ABORT("Maximum %d refs exceeded.\n", + ODP_CONFIG_PACKET_REFS); + handle.ref += 1; + + return _odp_packet_from_buffer(handle.handle); +} + /* * * Pointers and lengths @@ -472,6 +518,9 @@ odp_packet_t odp_packet_rem_data(odp_packet_t pkt, uint32_t offset, uint32_t pktlen = pkt_hdr->frame_len; odp_packet_t newpkt; + if (_odp_packet_refcheck(pkt)) + ODP_ABORT("modifying with refcounters is prohibited\n"); + if (offset > pktlen || offset + len > pktlen) return ODP_PACKET_INVALID; @@ -577,6 +626,9 @@ int odp_packet_copydata_in(odp_packet_t pkt, uint32_t offset, const uint8_t *srcaddr = (const uint8_t *)src; odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + if (_odp_packet_refcheck(pkt)) + ODP_ABORT("modifying with refcounters is prohibited\n"); + if (offset + len > pkt_hdr->frame_len) return -1; @@ -679,6 +731,9 @@ int _odp_packet_copy_to_packet(odp_packet_t srcpkt, uint32_t srcoffset, uint32_t srcseglen = 0; /* GCC */ uint32_t dstseglen = 0; /* GCC */ + if (_odp_packet_refcheck(dstpkt)) + ODP_ABORT("copiyng to packet with refcounters is prohibited\n"); + if (srcoffset + len > srchdr->frame_len || dstoffset + len > dsthdr->frame_len) return -1;