From patchwork Sun Oct 30 23:19:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 80103 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp2187026qge; Sun, 30 Oct 2016 16:20:13 -0700 (PDT) X-Received: by 10.237.36.28 with SMTP id r28mr20296583qtc.99.1477869613914; Sun, 30 Oct 2016 16:20:13 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id g3si15659747qtd.71.2016.10.30.16.20.13; Sun, 30 Oct 2016 16:20:13 -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 dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 932FC60E12; Sun, 30 Oct 2016 23:20:13 +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 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 7189260EE9; Sun, 30 Oct 2016 23:19:22 +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 6213260E12; Sun, 30 Oct 2016 23:19:17 +0000 (UTC) Received: from mail-oi0-f47.google.com (mail-oi0-f47.google.com [209.85.218.47]) by lists.linaro.org (Postfix) with ESMTPS id B668D60C54 for ; Sun, 30 Oct 2016 23:19:14 +0000 (UTC) Received: by mail-oi0-f47.google.com with SMTP id y2so193198010oie.0 for ; Sun, 30 Oct 2016 16:19:14 -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=SZCpGM6WapmjGNwBuUswcRnG9ePLyj9nnqxlQhbNbuQ=; b=iURaIHUNx2zbIPIcVpjmAvv9+bU5L9ofUeA4YkabwmwVuGXT3Y/6A9MP7S3gQyMo9V 8vuZWCGhCqo7Ovr87me/nT2bqVbTSy+jM6sYFzxnwiOmUNYygKKL8QSh9mWd4NbKPvMu I6qfmz3ihHXRnP4h/FTQfH1J0lwfnWE8dfRcQvpzaQJW2NNrNLR+RSJ41Yt5s5V3yWUq 3s9Oe0xci2ueJyb3A/F6enN3EEN3SuFjJEXvcppHiAauz/EKz64KaWkYqUDupQN2qnqp AMlUuXI/XHZo2vFrPwhUAAnBL5kBEWbLy6ubTi27k60ObdilBbonLSAbvUuzlj32Kll0 mE8w== X-Gm-Message-State: ABUngvdDKsvV/rIvtEreeAoOM1FlgfizsC7VAweIUttRFm+hjOUl6rw/CdR2F81ecmNo2lkIOf4= X-Received: by 10.157.39.33 with SMTP id r30mr4382944ota.168.1477869554108; Sun, 30 Oct 2016 16:19:14 -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 a65sm821425oih.6.2016.10.30.16.19.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 30 Oct 2016 16:19:13 -0700 (PDT) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Sun, 30 Oct 2016 18:19:06 -0500 Message-Id: <1477869549-6938-2-git-send-email-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1477869549-6938-1-git-send-email-bill.fischofer@linaro.org> References: <1477869549-6938-1-git-send-email-bill.fischofer@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCH 2/5] linux-generic: packet: implement reference apis 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" Implement the APIs: - odp_packet_ref_static() - odp_packet_ref() - odp_packet_ref_pkt() - odp_packet_is_ref() - odp_packet_has_ref() - odp_packet_unshared_len() This also involves functional upgrades to the existing packet manipulation APIs to work with packet references as input. Signed-off-by: Bill Fischofer --- .../linux-generic/include/odp_packet_internal.h | 54 ++++- platform/linux-generic/odp_packet.c | 267 ++++++++++++++++++--- 2 files changed, 283 insertions(+), 38 deletions(-) -- 2.7.4 diff --git a/platform/linux-generic/include/odp_packet_internal.h b/platform/linux-generic/include/odp_packet_internal.h index 392d670..60c3fdd 100644 --- a/platform/linux-generic/include/odp_packet_internal.h +++ b/platform/linux-generic/include/odp_packet_internal.h @@ -154,7 +154,7 @@ typedef struct { * packet_init(). Because of this any new fields added must be reviewed for * initialization requirements. */ -typedef struct { +typedef struct odp_packet_hdr_t { /* common buffer header */ odp_buffer_hdr_t buf_hdr; @@ -165,6 +165,10 @@ typedef struct { uint32_t headroom; uint32_t tailroom; + uint32_t ref_offset; + struct odp_packet_hdr_t *ref_hdr; + odp_atomic_u32_t ref_count; + odp_pktio_t input; /* Members below are not initialized by packet_init() */ @@ -188,6 +192,38 @@ static inline odp_packet_hdr_t *odp_packet_hdr(odp_packet_t pkt) return (odp_packet_hdr_t *)odp_buf_to_hdr((odp_buffer_t)pkt); } +static inline odp_packet_hdr_t *odp_packet_last_hdr(odp_packet_t pkt, + uint32_t *offset) +{ + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + uint32_t ref_offset = 0; + + while (pkt_hdr->ref_hdr) { + ref_offset = pkt_hdr->ref_offset; + pkt_hdr = pkt_hdr->ref_hdr; + } + + if (offset) + *offset = ref_offset; + return pkt_hdr; +} + +static inline odp_packet_hdr_t *odp_packet_prev_hdr(odp_packet_hdr_t *pkt_hdr, + odp_packet_hdr_t *cur_hdr, + uint32_t *offset) +{ + uint32_t ref_offset = 0; + + while (pkt_hdr->ref_hdr != cur_hdr) { + ref_offset = pkt_hdr->ref_offset; + pkt_hdr = pkt_hdr->ref_hdr; + } + + if (*offset) + *offset = ref_offset; + return pkt_hdr; +} + static inline void copy_packet_parser_metadata(odp_packet_hdr_t *src_hdr, odp_packet_hdr_t *dst_hdr) { @@ -207,6 +243,11 @@ static inline void copy_packet_cls_metadata(odp_packet_hdr_t *src_hdr, static inline void *packet_map(odp_packet_hdr_t *pkt_hdr, uint32_t offset, uint32_t *seglen) { + while (offset >= pkt_hdr->frame_len && pkt_hdr->ref_hdr) { + offset -= (pkt_hdr->frame_len - pkt_hdr->ref_offset); + pkt_hdr = pkt_hdr->ref_hdr; + } + if (offset > pkt_hdr->frame_len) return NULL; @@ -285,7 +326,16 @@ static inline void pull_tail(odp_packet_hdr_t *pkt_hdr, size_t len) static inline uint32_t packet_len(odp_packet_hdr_t *pkt_hdr) { - return pkt_hdr->frame_len; + uint32_t pkt_len = 0; + uint32_t offset = 0; + + do { + pkt_len += pkt_hdr->frame_len - offset; + offset = pkt_hdr->ref_offset; + pkt_hdr = pkt_hdr->ref_hdr; + } while (pkt_hdr); + + return pkt_len; } static inline void packet_set_len(odp_packet_hdr_t *pkt_hdr, uint32_t len) diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index c4cf324..ebd6e3a 100644 --- a/platform/linux-generic/odp_packet.c +++ b/platform/linux-generic/odp_packet.c @@ -73,6 +73,13 @@ static void packet_init(pool_entry_t *pool, odp_packet_hdr_t *pkt_hdr, (pool->s.seg_size * pkt_hdr->buf_hdr.segcount) - (pool->s.headroom + size); + /* By default packet has no referencees */ + pkt_hdr->ref_hdr = NULL; + pkt_hdr->ref_offset = 0; + + /* Start with a ref_count of 1 since packet is allocated */ + odp_atomic_init_u32(&pkt_hdr->ref_count, 1); + pkt_hdr->input = ODP_PKTIO_INVALID; } @@ -152,18 +159,29 @@ int odp_packet_alloc_multi(odp_pool_t pool_hdl, uint32_t len, return count; } -void odp_packet_free(odp_packet_t pkt) +static inline void packet_free(odp_packet_hdr_t *pkt_hdr) { - uint32_t pool_id = pool_id_from_buf((odp_buffer_t)pkt); + odp_packet_hdr_t *ref_hdr; - buffer_free(pool_id, (odp_buffer_t)pkt); + do { + ref_hdr = pkt_hdr->ref_hdr; + if (odp_atomic_fetch_dec_u32(&pkt_hdr->ref_count) == 1) + odp_buffer_free(pkt_hdr->buf_hdr.handle.handle); + pkt_hdr = ref_hdr; + } while (pkt_hdr); +} + +void odp_packet_free(odp_packet_t pkt) +{ + (void)packet_free(odp_packet_hdr(pkt)); } void odp_packet_free_multi(const odp_packet_t pkt[], int num) { - uint32_t pool_id = pool_id_from_buf((odp_buffer_t)pkt[0]); + int i; - buffer_free_multi(pool_id, (const odp_buffer_t * const)pkt, num); + for (i = 0; i < num; i++) + (void)packet_free(odp_packet_hdr(pkt[i])); } int odp_packet_reset(odp_packet_t pkt, uint32_t len) @@ -175,6 +193,9 @@ int odp_packet_reset(odp_packet_t pkt, uint32_t len) if (totsize > pkt_hdr->buf_hdr.size) return -1; + if (pkt_hdr->ref_hdr) + (void)packet_free(pkt_hdr->ref_hdr); + packet_init(pool, pkt_hdr, len, 0); return 0; @@ -217,8 +238,14 @@ void *odp_packet_head(odp_packet_t pkt) uint32_t odp_packet_buf_len(odp_packet_t pkt) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + uint32_t buf_len = 0; + + do { + buf_len += pkt_hdr->buf_hdr.size * pkt_hdr->buf_hdr.segcount; + pkt_hdr = pkt_hdr->ref_hdr; + } while (pkt_hdr); - return pkt_hdr->buf_hdr.size * pkt_hdr->buf_hdr.segcount; + return buf_len; } void *odp_packet_data(odp_packet_t pkt) @@ -231,7 +258,7 @@ void *odp_packet_data(odp_packet_t pkt) uint32_t odp_packet_seg_len(odp_packet_t pkt) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); - uint32_t seglen; + uint32_t seglen = 0; /* Call returns length of 1st data segment */ packet_map(pkt_hdr, 0, &seglen); @@ -240,7 +267,17 @@ uint32_t odp_packet_seg_len(odp_packet_t pkt) uint32_t odp_packet_len(odp_packet_t pkt) { - return odp_packet_hdr(pkt)->frame_len; + return packet_len(odp_packet_hdr(pkt)); +} + +uint32_t odp_packet_unshared_len(odp_packet_t pkt) +{ + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + + if (odp_atomic_load_u32(&pkt_hdr->ref_count) > 1) + return 0; + + return pkt_hdr->frame_len; } uint32_t odp_packet_headroom(odp_packet_t pkt) @@ -250,12 +287,12 @@ uint32_t odp_packet_headroom(odp_packet_t pkt) uint32_t odp_packet_tailroom(odp_packet_t pkt) { - return odp_packet_hdr(pkt)->tailroom; + return odp_packet_last_hdr(pkt, NULL)->tailroom; } void *odp_packet_tail(odp_packet_t pkt) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + odp_packet_hdr_t *pkt_hdr = odp_packet_last_hdr(pkt, NULL); return packet_map(pkt_hdr, pkt_hdr->frame_len, NULL); } @@ -301,22 +338,33 @@ int odp_packet_trunc_head(odp_packet_t *pkt, uint32_t len, void **data_ptr, uint32_t *seg_len) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(*pkt); + odp_packet_hdr_t *nxt_hdr; - if (len > pkt_hdr->frame_len) + if (len > packet_len(pkt_hdr)) return -1; + /* Special processing for spliced packets */ + while (len >= pkt_hdr->frame_len && pkt_hdr->ref_hdr) { + len -= (pkt_hdr->frame_len - pkt_hdr->ref_offset); + nxt_hdr = pkt_hdr->ref_hdr; + pkt_hdr->ref_hdr = NULL; + (void)packet_free(pkt_hdr); + pkt_hdr = nxt_hdr; + } + pull_head(pkt_hdr, len); if (pkt_hdr->headroom >= pkt_hdr->buf_hdr.segsize) pull_head_seg(pkt_hdr); if (data_ptr) *data_ptr = packet_map(pkt_hdr, 0, seg_len); + *pkt = (odp_packet_t)pkt_hdr->buf_hdr.handle.handle; return 0; } void *odp_packet_push_tail(odp_packet_t pkt, uint32_t len) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + odp_packet_hdr_t *pkt_hdr = odp_packet_last_hdr(pkt, NULL); uint32_t origin = pkt_hdr->frame_len; if (len > pkt_hdr->tailroom) @@ -329,7 +377,7 @@ void *odp_packet_push_tail(odp_packet_t pkt, uint32_t len) int odp_packet_extend_tail(odp_packet_t *pkt, uint32_t len, void **data_ptr, uint32_t *seg_len) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(*pkt); + odp_packet_hdr_t *pkt_hdr = odp_packet_last_hdr(*pkt, NULL); uint32_t origin = pkt_hdr->frame_len; if (len > pkt_hdr->tailroom && push_tail_seg(pkt_hdr, len)) @@ -344,9 +392,10 @@ int odp_packet_extend_tail(odp_packet_t *pkt, uint32_t len, void *odp_packet_pull_tail(odp_packet_t pkt, uint32_t len) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + uint32_t offset; + odp_packet_hdr_t *pkt_hdr = odp_packet_last_hdr(pkt, &offset); - if (len > pkt_hdr->frame_len) + if (len > pkt_hdr->frame_len - offset) return NULL; pull_tail(pkt_hdr, len); @@ -356,11 +405,24 @@ void *odp_packet_pull_tail(odp_packet_t pkt, uint32_t len) int odp_packet_trunc_tail(odp_packet_t *pkt, uint32_t len, void **tail_ptr, uint32_t *tailroom) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(*pkt); + uint32_t offset; + odp_packet_hdr_t *first_hdr = odp_packet_hdr(*pkt); + odp_packet_hdr_t *pkt_hdr, *prev_hdr; - if (len > pkt_hdr->frame_len) + if (len > packet_len(first_hdr)) return -1; + pkt_hdr = odp_packet_last_hdr(*pkt, &offset); + + /* Special processing for spliced packets */ + while (len >= pkt_hdr->frame_len - offset && first_hdr->ref_hdr) { + len -= (pkt_hdr->frame_len - offset); + prev_hdr = odp_packet_prev_hdr(first_hdr, pkt_hdr, &offset); + prev_hdr->ref_hdr = NULL; + (void)packet_free(pkt_hdr); + pkt_hdr = prev_hdr; + } + pull_tail(pkt_hdr, len); if (pkt_hdr->tailroom >= pkt_hdr->buf_hdr.segsize) pull_tail_seg(pkt_hdr); @@ -381,7 +443,12 @@ void *odp_packet_offset(odp_packet_t pkt, uint32_t offset, uint32_t *len, if (addr != NULL && seg != NULL) { odp_buffer_bits_t seghandle; - seghandle.handle = (odp_buffer_t)pkt; + while (offset >= pkt_hdr->frame_len && pkt_hdr->ref_hdr) { + offset -= (pkt_hdr->frame_len - pkt_hdr->ref_offset); + pkt_hdr = pkt_hdr->ref_hdr; + } + + seghandle.handle = pkt_hdr->buf_hdr.handle.handle; seghandle.seg = (pkt_hdr->headroom + offset) / pkt_hdr->buf_hdr.segsize; *seg = (odp_packet_seg_t)seghandle.handle; @@ -563,12 +630,22 @@ void odp_packet_ts_set(odp_packet_t pkt, odp_time_t timestamp) int odp_packet_is_segmented(odp_packet_t pkt) { - return odp_packet_hdr(pkt)->buf_hdr.segcount > 1; + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + + return pkt_hdr->buf_hdr.segcount > 1 || pkt_hdr->ref_hdr != NULL; } int odp_packet_num_segs(odp_packet_t pkt) { - return odp_packet_hdr(pkt)->buf_hdr.segcount; + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + int segcount = 0; + + do { + segcount += pkt_hdr->buf_hdr.segcount; + pkt_hdr = pkt_hdr->ref_hdr; + } while (pkt_hdr); + + return segcount; } odp_packet_seg_t odp_packet_first_seg(odp_packet_t pkt) @@ -578,7 +655,7 @@ odp_packet_seg_t odp_packet_first_seg(odp_packet_t pkt) odp_packet_seg_t odp_packet_last_seg(odp_packet_t pkt) { - odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + odp_packet_hdr_t *pkt_hdr = odp_packet_last_hdr(pkt, NULL); odp_buffer_bits_t seghandle; seghandle.handle = (odp_buffer_t)pkt; @@ -589,9 +666,22 @@ odp_packet_seg_t odp_packet_last_seg(odp_packet_t pkt) odp_packet_seg_t odp_packet_next_seg(odp_packet_t pkt, odp_packet_seg_t seg) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + odp_packet_seg_t next_seg; + odp_buffer_bits_t seghandle; + + seghandle.handle = (odp_buffer_t)seg; - return (odp_packet_seg_t)segment_next(&pkt_hdr->buf_hdr, - (odp_buffer_seg_t)seg); + while (seghandle.prefix != pkt_hdr->buf_hdr.handle.prefix && + pkt_hdr->ref_hdr) + pkt_hdr = pkt_hdr->ref_hdr; + + next_seg = (odp_packet_seg_t)segment_next(&pkt_hdr->buf_hdr, + (odp_buffer_seg_t)seg); + if (next_seg == ODP_PACKET_SEG_INVALID && pkt_hdr->ref_hdr) + next_seg = (odp_packet_seg_t) + pkt_hdr->ref_hdr->buf_hdr.handle.handle; + + return next_seg; } /* @@ -604,18 +694,36 @@ odp_packet_seg_t odp_packet_next_seg(odp_packet_t pkt, odp_packet_seg_t seg) void *odp_packet_seg_data(odp_packet_t pkt, odp_packet_seg_t seg) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + void *segmap; + uint32_t offset = 0; + + do { + segmap = segment_map(&pkt_hdr->buf_hdr, + (odp_buffer_seg_t)seg, NULL, + pkt_hdr->frame_len, + pkt_hdr->headroom + offset); + offset = pkt_hdr->ref_offset; + pkt_hdr = pkt_hdr->ref_hdr; + } while (!segmap && pkt_hdr); - return segment_map(&pkt_hdr->buf_hdr, (odp_buffer_seg_t)seg, NULL, - pkt_hdr->frame_len, pkt_hdr->headroom); + return segmap; } uint32_t odp_packet_seg_data_len(odp_packet_t pkt, odp_packet_seg_t seg) { odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); uint32_t seglen = 0; - - segment_map(&pkt_hdr->buf_hdr, (odp_buffer_seg_t)seg, &seglen, - pkt_hdr->frame_len, pkt_hdr->headroom); + void *segmap; + uint32_t offset = 0; + + do { + segmap = segment_map(&pkt_hdr->buf_hdr, + (odp_buffer_seg_t)seg, &seglen, + pkt_hdr->frame_len, + pkt_hdr->headroom + offset); + offset = pkt_hdr->ref_offset; + pkt_hdr = pkt_hdr->ref_hdr; + } while (!segmap && pkt_hdr); return seglen; } @@ -631,7 +739,7 @@ int odp_packet_add_data(odp_packet_t *pkt_ptr, uint32_t offset, uint32_t len) { odp_packet_t pkt = *pkt_ptr; odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); - uint32_t pktlen = pkt_hdr->frame_len; + uint32_t pktlen = packet_len(pkt_hdr); odp_packet_t newpkt; if (offset > pktlen) @@ -760,6 +868,93 @@ int odp_packet_split(odp_packet_t *pkt, uint32_t len, odp_packet_t *tail) } /* + * References + */ + +static inline void packet_ref(odp_packet_hdr_t *pkt_hdr) +{ + do { + odp_atomic_inc_u32(&pkt_hdr->ref_count); + pkt_hdr = pkt_hdr->ref_hdr; + } while (pkt_hdr); +} + +static inline odp_packet_t packet_splice(odp_packet_t pkt, uint32_t offset, + odp_packet_t hdr) +{ + odp_packet_hdr_t *ref_hdr, *pkt_hdr; + + pkt_hdr = odp_packet_hdr(pkt); + ref_hdr = odp_packet_hdr(hdr); + + while (ref_hdr->ref_hdr) { + printf("packet_splice looking for end of pkt %p...\n", ref_hdr); + ref_hdr = ref_hdr->ref_hdr; + } + + ref_hdr->ref_hdr = pkt_hdr; + ref_hdr->ref_offset = offset; + + packet_ref(pkt_hdr); + return hdr; +} + +odp_packet_t odp_packet_ref_static(odp_packet_t pkt) +{ + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + + packet_ref(pkt_hdr); + return pkt; +} + +odp_packet_t odp_packet_ref(odp_packet_t pkt, uint32_t offset) +{ + odp_packet_t hdr; + + if (pkt == ODP_PACKET_INVALID || offset >= odp_packet_len(pkt)) + return ODP_PACKET_INVALID; + + hdr = odp_packet_alloc(odp_packet_pool(pkt), 0); + + if (hdr == ODP_PACKET_INVALID) + return ODP_PACKET_INVALID; + + return packet_splice(pkt, offset, hdr); +} + +odp_packet_t odp_packet_ref_pkt(odp_packet_t pkt, uint32_t offset, + odp_packet_t hdr) +{ + if (pkt == ODP_PACKET_INVALID || + pkt == hdr || + offset >= odp_packet_len(pkt)) + return ODP_PACKET_INVALID; + + if (hdr == ODP_PACKET_INVALID) + return odp_packet_ref(pkt, offset); + + return packet_splice(pkt, offset, hdr); +} + +int odp_packet_is_ref(odp_packet_t pkt) +{ + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); + uint32_t ref_count = 0; + + while (pkt_hdr->ref_hdr) { + ref_count++; + pkt_hdr = pkt_hdr->ref_hdr; + } + + return ref_count; +} + +int odp_packet_has_ref(odp_packet_t pkt) +{ + return odp_atomic_load_u32(&odp_packet_hdr(pkt)->ref_count) - 1; +} + +/* * * Copy * ******************************************************** @@ -769,7 +964,7 @@ int odp_packet_split(odp_packet_t *pkt, uint32_t len, odp_packet_t *tail) odp_packet_t odp_packet_copy(odp_packet_t pkt, odp_pool_t pool) { odp_packet_hdr_t *srchdr = odp_packet_hdr(pkt); - uint32_t pktlen = srchdr->frame_len; + uint32_t pktlen = packet_len(srchdr); odp_packet_t newpkt = odp_packet_alloc(pool, pktlen); if (newpkt != ODP_PACKET_INVALID) { @@ -808,7 +1003,7 @@ int odp_packet_copy_to_mem(odp_packet_t pkt, uint32_t offset, uint8_t *dstaddr = (uint8_t *)dst; odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt); - if (offset + len > pkt_hdr->frame_len) + if (offset + len > packet_len(pkt_hdr)) return -1; while (len > 0) { @@ -832,7 +1027,7 @@ int odp_packet_copy_from_mem(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 (offset + len > pkt_hdr->frame_len) + if (offset + len > packet_len(pkt_hdr)) return -1; while (len > 0) { @@ -860,8 +1055,8 @@ int odp_packet_copy_from_pkt(odp_packet_t dst, uint32_t dst_offset, uint32_t src_seglen = 0; /* GCC */ int overlap; - if (dst_offset + len > dst_hdr->frame_len || - src_offset + len > src_hdr->frame_len) + if (dst_offset + len > packet_len(dst_hdr) || + src_offset + len > packet_len(src_hdr)) return -1; overlap = (dst_hdr == src_hdr && @@ -946,7 +1141,7 @@ void odp_packet_print(odp_packet_t pkt) len += snprintf(&str[len], n - len, " l4_offset %" PRIu32 "\n", hdr->p.l4_offset); len += snprintf(&str[len], n - len, - " frame_len %" PRIu32 "\n", hdr->frame_len); + " frame_len %" PRIu32 "\n", packet_len(hdr)); len += snprintf(&str[len], n - len, " input %" PRIu64 "\n", odp_pktio_to_u64(hdr->input)); @@ -1319,7 +1514,7 @@ parse_exit: */ int packet_parse_full(odp_packet_hdr_t *pkt_hdr) { - uint32_t seg_len; + uint32_t seg_len = 0; void *base = packet_map(pkt_hdr, 0, &seg_len); return packet_parse_common(&pkt_hdr->p, base, pkt_hdr->frame_len,