diff mbox

[API-NEXT,2/2] linux-generic: packet: implement splice/reference apis

Message ID 1475875243-10929-3-git-send-email-bill.fischofer@linaro.org
State New
Headers show

Commit Message

Bill Fischofer Oct. 7, 2016, 9:20 p.m. UTC
Implement the odp_packet_splice(), odp_packet_ref(), and
odp_packet_is_spliced() APIs. This also involves functional upgrades to
the existing head/tail manipulation APIs to be able to deal correctly with
spliced packets as input.

Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>

---
 .../linux-generic/include/odp_packet_internal.h    |  54 +++++++-
 platform/linux-generic/odp_packet.c                | 149 ++++++++++++++++++---
 2 files changed, 184 insertions(+), 19 deletions(-)

-- 
2.7.4
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_packet_internal.h b/platform/linux-generic/include/odp_packet_internal.h
index 392d670..b6ab954 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;
 
+	struct odp_packet_hdr_t *ref_hdr;
+	uint32_t ref_offset;
+	odp_atomic_u32_t refcount;
+
 	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..2d4998c 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->refcount, 1);
+
 	pkt_hdr->input = ODP_PKTIO_INVALID;
 }
 
@@ -152,18 +159,34 @@  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 int packet_free(odp_packet_hdr_t *pkt_hdr)
 {
-	uint32_t pool_id = pool_id_from_buf((odp_buffer_t)pkt);
+	uint32_t refcount;
+
+	if (pkt_hdr->ref_hdr && packet_free(pkt_hdr->ref_hdr))
+		pkt_hdr->ref_hdr = NULL;
+
+	refcount = odp_atomic_fetch_dec_u32(&pkt_hdr->refcount);
+
+	if (refcount > 1)
+		return 0;
+
+	odp_buffer_free(pkt_hdr->buf_hdr.handle.handle);
 
-	buffer_free(pool_id, (odp_buffer_t)pkt);
+	return 1;
+}
+
+void odp_packet_free(odp_packet_t pkt)
+{
+	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++)
+		packet_free(odp_packet_hdr(pkt[i]));
 }
 
 int odp_packet_reset(odp_packet_t pkt, uint32_t len)
@@ -231,7 +254,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 +263,7 @@  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_headroom(odp_packet_t pkt)
@@ -250,12 +273,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 +324,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) {
+		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 +363,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 +378,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 +391,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) {
+		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);
@@ -760,6 +808,73 @@  int odp_packet_split(odp_packet_t *pkt, uint32_t len, odp_packet_t *tail)
 }
 
 /*
+ * Reference / Splice
+ */
+
+static inline odp_packet_t packet_splice(odp_packet_t refpkt,
+					 odp_packet_t pkt, uint32_t offset)
+{
+	odp_packet_hdr_t *ref_hdr, *pkt_hdr, *next_hdr;
+
+	ref_hdr = odp_packet_hdr(refpkt);
+	pkt_hdr = odp_packet_hdr(pkt);
+
+	next_hdr = pkt_hdr;
+
+	do {
+		odp_atomic_inc_u32(&next_hdr->refcount);
+		next_hdr = next_hdr->ref_hdr;
+	} while (next_hdr);
+
+	while (ref_hdr->ref_hdr)
+		ref_hdr = ref_hdr->ref_hdr;
+
+	ref_hdr->ref_hdr    = pkt_hdr;
+	ref_hdr->ref_offset = offset;
+
+	return refpkt;
+}
+
+odp_packet_t odp_packet_ref(odp_packet_t pkt, uint32_t offset)
+{
+	odp_packet_t refpkt;
+
+	if (pkt == ODP_PACKET_INVALID || offset > odp_packet_len(pkt))
+		return ODP_PACKET_INVALID;
+
+	refpkt = odp_packet_alloc(odp_packet_pool(pkt), 0);
+
+	if (refpkt == ODP_PACKET_INVALID)
+		return ODP_PACKET_INVALID;
+
+	return packet_splice(refpkt, pkt, offset);
+}
+
+odp_packet_t odp_packet_splice(odp_packet_t refpkt,
+			       odp_packet_t pkt, uint32_t offset)
+{
+	if (pkt    == ODP_PACKET_INVALID ||
+	    refpkt == ODP_PACKET_INVALID ||
+	    offset > odp_packet_len(pkt))
+		return ODP_PACKET_INVALID;
+
+	return packet_splice(refpkt, pkt, offset);
+}
+
+int odp_packet_is_spliced(odp_packet_t pkt)
+{
+	odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt);
+	uint32_t splice_count = 0;
+
+	while (pkt_hdr->ref_hdr) {
+		splice_count++;
+		pkt_hdr = pkt_hdr->ref_hdr;
+	}
+
+	return splice_count;
+}
+
+/*
  *
  * Copy
  * ********************************************************
@@ -1319,7 +1434,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,