diff mbox series

[v2,2/2] linux-generic: packet: add debugging asserts in packet code

Message ID 1505746808-26297-3-git-send-email-odpbot@yandex.ru
State New
Headers show
Series [v2,1/2] linux-generic: packet: correct syntax issue | expand

Commit Message

Github ODP bot Sept. 18, 2017, 3 p.m. UTC
From: Bill Fischofer <bill.fischofer@linaro.org>


Restore the ODP_ASSERTS() that assist application debugging that were
lost in the recent packet reference rework. When using --enable-debug
insert guards to detect duplicate packet frees as well as attempts to
modify the shared data sections of packet references.

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

---
/** Email created from pull request 180 (Bill-Fischofer-Linaro:pkt-debug)
 ** https://github.com/Linaro/odp/pull/180
 ** Patch: https://github.com/Linaro/odp/pull/180.patch
 ** Base sha: e1c46f8e296a730ed27141a33189185bb7dfd1b1
 ** Merge commit sha: 8f6cbdeb075be6d397a255d8791d88a492b9d2db
 **/
 platform/linux-generic/odp_packet.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
diff mbox series

Patch

diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c
index a3fd0559f..4cd0a108c 100644
--- a/platform/linux-generic/odp_packet.c
+++ b/platform/linux-generic/odp_packet.c
@@ -354,6 +354,8 @@  static inline void link_segments(odp_packet_hdr_t *pkt_hdr[], int num)
 			hdr->buf_hdr.seg[i].hdr  = buf_hdr;
 			hdr->buf_hdr.seg[i].data = buf_hdr->base_data;
 			hdr->buf_hdr.seg[i].len  = BASE_LEN;
+			ODP_ASSERT((odp_atomic_store_u32(&hdr->buf_hdr.ref_cnt,
+							 1), 1));
 			cur++;
 
 			if (cur == num) {
@@ -388,6 +390,7 @@  static inline void init_segments(odp_packet_hdr_t *pkt_hdr[], int num)
 		hdr->buf_hdr.num_seg  = 1;
 		hdr->buf_hdr.next_seg = NULL;
 		hdr->buf_hdr.last_seg = &hdr->buf_hdr;
+		ODP_ASSERT((odp_atomic_store_u32(&hdr->buf_hdr.ref_cnt, 1), 1));
 
 		if (odp_unlikely(num > 1))
 			link_segments(pkt_hdr, num);
@@ -848,6 +851,8 @@  void odp_packet_free(odp_packet_t pkt)
 	odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt);
 	int num_seg = pkt_hdr->buf_hdr.segcount;
 
+	ODP_ASSERT(buffer_ref(&pkt_hdr->buf_hdr) >= 1);
+
 	if (odp_likely(CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1)) {
 		odp_buffer_hdr_t *buf_hdr[2];
 		int num = 1;
@@ -877,6 +882,8 @@  void odp_packet_free_multi(const odp_packet_t pkt[], int num)
 		odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt[i]);
 		int num_seg = pkt_hdr->buf_hdr.segcount;
 
+		ODP_ASSERT(buffer_ref(&pkt_hdr->buf_hdr) >= 1);
+
 		if (odp_unlikely(num_seg > 1)) {
 			free_all_segments(pkt_hdr, num_seg);
 			num_freed++;
@@ -1010,6 +1017,8 @@  void *odp_packet_pull_head(odp_packet_t pkt, uint32_t len)
 	if (len > pkt_hdr->frame_len)
 		return NULL;
 
+	ODP_ASSERT(len <= odp_packet_unshared_len(pkt));
+
 	pull_head(pkt_hdr, len);
 	return packet_data(pkt_hdr);
 }
@@ -1023,6 +1032,8 @@  int odp_packet_trunc_head(odp_packet_t *pkt, uint32_t len,
 	if (len > pkt_hdr->frame_len)
 		return -1;
 
+	ODP_ASSERT(len <= odp_packet_unshared_len(*pkt));
+
 	if (len < seg_len) {
 		pull_head(pkt_hdr, len);
 	} else if (CONFIG_PACKET_MAX_SEGS != 1) {
@@ -1057,6 +1068,8 @@  void *odp_packet_push_tail(odp_packet_t pkt, uint32_t len)
 	if (len > pkt_hdr->tailroom)
 		return NULL;
 
+	ODP_ASSERT(odp_packet_has_ref(pkt) == 0);
+
 	old_tail = packet_tail(pkt_hdr);
 	push_tail(pkt_hdr, len);
 
@@ -1072,6 +1085,8 @@  int odp_packet_extend_tail(odp_packet_t *pkt, uint32_t len,
 	uint32_t tail_off  = frame_len;
 	int ret = 0;
 
+	ODP_ASSERT(odp_packet_has_ref(*pkt) == 0);
+
 	if (len > tailroom) {
 		pool_t *pool = pkt_hdr->buf_hdr.pool_ptr;
 		int num;
@@ -1104,6 +1119,8 @@  void *odp_packet_pull_tail(odp_packet_t pkt, uint32_t len)
 	odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt);
 	seg_entry_t *last_seg     = seg_entry_last(pkt_hdr);
 
+	ODP_ASSERT(odp_packet_has_ref(pkt) == 0);
+
 	if (len > last_seg->len)
 		return NULL;
 
@@ -1123,6 +1140,8 @@  int odp_packet_trunc_tail(odp_packet_t *pkt, uint32_t len,
 	if (len > pkt_hdr->frame_len)
 		return -1;
 
+	ODP_ASSERT(odp_packet_has_ref(*pkt) == 0);
+
 	last     = packet_last_seg(pkt_hdr);
 	last_seg = seg_entry_last(pkt_hdr);
 	seg_len  = last_seg->len;
@@ -1386,6 +1405,8 @@  int odp_packet_align(odp_packet_t *pkt, uint32_t offset, uint32_t len,
 	if (align > ODP_CACHE_LINE_SIZE)
 		return -1;
 
+	ODP_ASSERT(odp_packet_has_ref(*pkt) == 0);
+
 	if (seglen >= len) {
 		misalign = align <= 1 ? 0 :
 			ROUNDUP_ALIGN(uaddr, align) - uaddr;
@@ -1423,6 +1444,8 @@  int odp_packet_concat(odp_packet_t *dst, odp_packet_t src)
 	uint32_t dst_len = dst_hdr->frame_len;
 	uint32_t src_len = src_hdr->frame_len;
 
+	ODP_ASSERT(odp_packet_has_ref(*dst) == 0);
+
 	/* Do a copy if packets are from different pools. */
 	if (odp_unlikely(dst_pool != src_pool)) {
 		if (odp_packet_extend_tail(dst, src_len, NULL, NULL) >= 0) {
@@ -1453,6 +1476,8 @@  int odp_packet_split(odp_packet_t *pkt, uint32_t len, odp_packet_t *tail)
 	if (len >= pktlen || tail == NULL)
 		return -1;
 
+	ODP_ASSERT(odp_packet_has_ref(*pkt) == 0);
+
 	*tail = odp_packet_copy_part(*pkt, len, pktlen - len,
 				     odp_packet_pool(*pkt));
 
@@ -1538,6 +1563,8 @@  int odp_packet_copy_from_mem(odp_packet_t pkt, uint32_t offset,
 	if (offset + len > pkt_hdr->frame_len)
 		return -1;
 
+	ODP_ASSERT(offset + len <= odp_packet_unshared_len(pkt));
+
 	while (len > 0) {
 		mapaddr = packet_map(pkt_hdr, offset, &seglen, NULL);
 		cpylen = len > seglen ? seglen : len;
@@ -1567,6 +1594,8 @@  int odp_packet_copy_from_pkt(odp_packet_t dst, uint32_t dst_offset,
 	    src_offset + len > src_hdr->frame_len)
 		return -1;
 
+	ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(dst));
+
 	overlap = (dst_hdr == src_hdr &&
 		   ((dst_offset <= src_offset &&
 		     dst_offset + len >= src_offset) ||
@@ -1607,6 +1636,8 @@  int odp_packet_copy_from_pkt(odp_packet_t dst, uint32_t dst_offset,
 int odp_packet_copy_data(odp_packet_t pkt, uint32_t dst_offset,
 			 uint32_t src_offset, uint32_t len)
 {
+	ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(pkt));
+
 	return odp_packet_copy_from_pkt(pkt, dst_offset,
 					pkt, src_offset, len);
 }
@@ -1614,6 +1645,8 @@  int odp_packet_copy_data(odp_packet_t pkt, uint32_t dst_offset,
 int odp_packet_move_data(odp_packet_t pkt, uint32_t dst_offset,
 			 uint32_t src_offset, uint32_t len)
 {
+	ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(pkt));
+
 	return odp_packet_copy_from_pkt(pkt, dst_offset,
 					pkt, src_offset, len);
 }
@@ -1629,6 +1662,8 @@  int _odp_packet_set_data(odp_packet_t pkt, uint32_t offset,
 	if (offset + len > pkt_hdr->frame_len)
 		return -1;
 
+	ODP_ASSERT(offset + len <= odp_packet_unshared_len(pkt));
+
 	while (len > 0) {
 		mapaddr = packet_map(pkt_hdr, offset, &seglen, NULL);
 		setlen = len > seglen ? seglen : len;