diff mbox

[API-NEXT,1/2] linux-generic: packet: support multi-segment head/tail push/pull ops

Message ID 1458497716-12483-1-git-send-email-bill.fischofer@linaro.org
State Superseded
Headers show

Commit Message

Bill Fischofer March 20, 2016, 6:15 p.m. UTC
Add support for multi-segment push/pull operations for Monarch compliance.
If a push for more than the available headroom/tailroom is requested, then
allocate additional head/tail segments if possible to complete the
operation. Similarly, when pulling more than a single segment, allow
empty segments to exist that consist entirely of headroom/tailroom.

Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>
---
 .../linux-generic/include/odp_buffer_internal.h    |  4 ++
 .../linux-generic/include/odp_packet_internal.h    | 49 ++++++++++++++
 platform/linux-generic/odp_packet.c                | 10 ++-
 platform/linux-generic/odp_pool.c                  | 75 ++++++++++++++++++++++
 4 files changed, 136 insertions(+), 2 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_buffer_internal.h b/platform/linux-generic/include/odp_buffer_internal.h
index ea092ca..0a4c290 100644
--- a/platform/linux-generic/include/odp_buffer_internal.h
+++ b/platform/linux-generic/include/odp_buffer_internal.h
@@ -170,6 +170,10 @@  typedef struct {
 odp_buffer_t buffer_alloc(odp_pool_t pool, size_t size);
 int buffer_alloc_multi(odp_pool_t pool_hdl, size_t size,
 		       odp_buffer_t buf[], int num);
+int seg_alloc_head(odp_buffer_hdr_t *buf_hdr, int segcount);
+void seg_free_head(odp_buffer_hdr_t *buf_hdr, int segcount);
+int seg_alloc_tail(odp_buffer_hdr_t *buf_hdr, int segcount);
+void seg_free_tail(odp_buffer_hdr_t *buf_hdr, int segcount);
 
 #ifdef __cplusplus
 }
diff --git a/platform/linux-generic/include/odp_packet_internal.h b/platform/linux-generic/include/odp_packet_internal.h
index b632ece..77e32fe 100644
--- a/platform/linux-generic/include/odp_packet_internal.h
+++ b/platform/linux-generic/include/odp_packet_internal.h
@@ -210,6 +210,30 @@  static inline void pull_head(odp_packet_hdr_t *pkt_hdr, size_t len)
 	pkt_hdr->frame_len -= len;
 }
 
+static inline int push_head_seg(odp_packet_hdr_t *pkt_hdr, size_t len)
+{
+	uint32_t newsegcount =
+		(len - pkt_hdr->headroom + pkt_hdr->buf_hdr.size - 1) /
+		pkt_hdr->buf_hdr.size;
+
+	if (pkt_hdr->buf_hdr.segcount + newsegcount > ODP_BUFFER_MAX_SEG)
+		return -1;
+
+	if (seg_alloc_head(&pkt_hdr->buf_hdr, newsegcount))
+		return -1;
+
+	pkt_hdr->headroom += newsegcount * pkt_hdr->buf_hdr.size;
+	return 0;
+}
+
+static inline void pull_head_seg(odp_packet_hdr_t *pkt_hdr)
+{
+	uint32_t extrasegs = pkt_hdr->headroom / pkt_hdr->buf_hdr.size;
+
+	seg_free_head(&pkt_hdr->buf_hdr, extrasegs);
+	pkt_hdr->headroom -= extrasegs * pkt_hdr->buf_hdr.size;
+}
+
 static inline void push_tail(odp_packet_hdr_t *pkt_hdr, size_t len)
 {
 	pkt_hdr->tailroom  -= len;
@@ -223,6 +247,31 @@  static inline void pull_tail(odp_packet_hdr_t *pkt_hdr, size_t len)
 	pkt_hdr->frame_len -= len;
 }
 
+static inline int push_tail_seg(odp_packet_hdr_t *pkt_hdr, size_t len)
+{
+	uint32_t newsegcount =
+		(len - pkt_hdr->tailroom + pkt_hdr->buf_hdr.size - 1) /
+		pkt_hdr->buf_hdr.size;
+
+	if (pkt_hdr->buf_hdr.segcount + newsegcount > ODP_BUFFER_MAX_SEG)
+		return -1;
+
+	if (seg_alloc_tail(&pkt_hdr->buf_hdr, newsegcount))
+		return -1;
+
+	pkt_hdr->tailroom += newsegcount * pkt_hdr->buf_hdr.size;
+	return 0;
+}
+
+static inline void pull_tail_seg(odp_packet_hdr_t *pkt_hdr)
+{
+	uint32_t extrasegs = pkt_hdr->tailroom / pkt_hdr->buf_hdr.size;
+
+	seg_free_tail(&pkt_hdr->buf_hdr, extrasegs);
+
+	pkt_hdr->tailroom -= extrasegs * pkt_hdr->buf_hdr.size;
+}
+
 static inline uint32_t packet_len(odp_packet_hdr_t *pkt_hdr)
 {
 	return pkt_hdr->frame_len;
diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c
index aac42b6..4ea1812 100644
--- a/platform/linux-generic/odp_packet.c
+++ b/platform/linux-generic/odp_packet.c
@@ -255,7 +255,7 @@  void *odp_packet_push_head(odp_packet_t pkt, uint32_t len)
 {
 	odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt);
 
-	if (len > pkt_hdr->headroom)
+	if (len > pkt_hdr->headroom && push_head_seg(pkt_hdr, len))
 		return NULL;
 
 	push_head(pkt_hdr, len);
@@ -270,6 +270,9 @@  void *odp_packet_pull_head(odp_packet_t pkt, uint32_t len)
 		return NULL;
 
 	pull_head(pkt_hdr, len);
+	if (pkt_hdr->headroom > pkt_hdr->buf_hdr.size)
+		pull_head_seg(pkt_hdr);
+
 	return packet_map(pkt_hdr, 0, NULL);
 }
 
@@ -278,7 +281,7 @@  void *odp_packet_push_tail(odp_packet_t pkt, uint32_t len)
 	odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt);
 	uint32_t origin = pkt_hdr->frame_len;
 
-	if (len > pkt_hdr->tailroom)
+	if (len > pkt_hdr->tailroom && push_tail_seg(pkt_hdr, len))
 		return NULL;
 
 	push_tail(pkt_hdr, len);
@@ -293,6 +296,9 @@  void *odp_packet_pull_tail(odp_packet_t pkt, uint32_t len)
 		return NULL;
 
 	pull_tail(pkt_hdr, len);
+	if (pkt_hdr->tailroom > pkt_hdr->buf_hdr.size)
+		pull_tail_seg(pkt_hdr);
+
 	return packet_map(pkt_hdr, pkt_hdr->frame_len, NULL);
 }
 
diff --git a/platform/linux-generic/odp_pool.c b/platform/linux-generic/odp_pool.c
index f6fa8f5..e494d5a 100644
--- a/platform/linux-generic/odp_pool.c
+++ b/platform/linux-generic/odp_pool.c
@@ -500,6 +500,81 @@  int odp_pool_destroy(odp_pool_t pool_hdl)
 	return 0;
 }
 
+int seg_alloc_head(odp_buffer_hdr_t *buf_hdr,  int segcount)
+{
+	uint32_t pool_id = pool_handle_to_index(buf_hdr->pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+	void *newsegs[segcount];
+	int i;
+
+	for (i = 0; i < segcount; i++) {
+		newsegs[i] = get_blk(&pool->s);
+		if (newsegs[i] == NULL) {
+			while (--i >= 0)
+				ret_blk(&pool->s, newsegs[i]);
+			return -1;
+		}
+	}
+
+	for (i = buf_hdr->segcount - 1; i >= 0; i--)
+		buf_hdr->addr[i + segcount] = buf_hdr->addr[i];
+
+	for (i = 0; i < segcount; i++)
+		buf_hdr->addr[i] = newsegs[i];
+
+	buf_hdr->segcount += segcount;
+	return 0;
+}
+
+void seg_free_head(odp_buffer_hdr_t *buf_hdr, int segcount)
+{
+	uint32_t pool_id = pool_handle_to_index(buf_hdr->pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+	int s_cnt = buf_hdr->segcount;
+	int i;
+
+	for (i = 0; i < s_cnt; i++)
+		ret_blk(&pool->s, buf_hdr->addr[i]);
+
+	for (i = 0; i < s_cnt - segcount; i++)
+		buf_hdr->addr[i] = buf_hdr->addr[i + segcount];
+
+	buf_hdr->segcount -= segcount;
+}
+
+int seg_alloc_tail(odp_buffer_hdr_t *buf_hdr,  int segcount)
+{
+	uint32_t pool_id = pool_handle_to_index(buf_hdr->pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+	uint32_t s_cnt = buf_hdr->segcount;
+	int i;
+
+	for (i = 0; i < segcount; i++) {
+		buf_hdr->addr[s_cnt + i] = get_blk(&pool->s);
+		if (buf_hdr->addr[s_cnt + i] == NULL) {
+			while (--i >= 0)
+				ret_blk(&pool->s, buf_hdr->addr[s_cnt + i]);
+			return -1;
+		}
+	}
+
+	buf_hdr->segcount += segcount;
+	return 0;
+}
+
+void seg_free_tail(odp_buffer_hdr_t *buf_hdr, int segcount)
+{
+	uint32_t pool_id = pool_handle_to_index(buf_hdr->pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+	int s_cnt = buf_hdr->segcount;
+	int i;
+
+	for (i = s_cnt - 1; i > s_cnt - segcount; i--)
+		ret_blk(&pool->s, buf_hdr->addr[i]);
+
+	buf_hdr->segcount -= segcount;
+}
+
 odp_buffer_t buffer_alloc(odp_pool_t pool_hdl, size_t size)
 {
 	uint32_t pool_id = pool_handle_to_index(pool_hdl);