From 803b069703326e9674341247ac971f7dc59d84fe Mon Sep 17 00:00:00 2001
From: Intel <intel.com>
Date: Mon, 3 Jun 2013 00:00:00 +0000
Subject: [PATCH] lib: add rte_hexdump and remove duplicated code

Signed-off-by: Intel
---
 app/test/test.c                             |  28 -----
 app/test/test.h                             |   2 -
 app/test/test_ring.c                        |  37 +++---
 lib/librte_eal/common/Makefile              |   1 +
 lib/librte_eal/common/eal_common_hexdump.c  | 122 ++++++++++++++++++++
 lib/librte_eal/common/include/rte_hexdump.h |  84 ++++++++++++++
 lib/librte_eal/linuxapp/eal/Makefile        |   1 +
 lib/librte_mbuf/rte_mbuf.c                  |  30 +----
 8 files changed, 229 insertions(+), 76 deletions(-)
 mode change 100644 => 100755 app/test/test.h
 create mode 100755 lib/librte_eal/common/eal_common_hexdump.c
 create mode 100755 lib/librte_eal/common/include/rte_hexdump.h

diff --git a/app/test/test.c b/app/test/test.c
index a5b072c439..f0209b5fad 100644
--- a/app/test/test.c
+++ b/app/test/test.c
@@ -94,34 +94,6 @@ do_recursive_call(void)
 }
 #endif
 
-void
-test_hexdump(const char *title, const void *buf, unsigned int len)
-{
-	unsigned int i, out, ofs;
-	const unsigned char *data = buf;
-#define LINE_LEN 80
-	char line[LINE_LEN];	/* space needed 8+16*3+3+16 == 75 */
-
-	printf("%s at [%p], len=%u\n", title, data, len);
-	ofs = 0;
-	while (ofs < len) {
-		/* format 1 line in the buffer, then use printf to print them */
-		out = rte_snprintf(line, LINE_LEN, "%08X", ofs);
-		for (i = 0; ofs+i < len && i < 16; i++)
-			out += rte_snprintf(line+out, LINE_LEN - out, " %02X",
-					data[ofs+i]&0xff);
-		for(; i <= 16; i++)
-			out += rte_snprintf(line+out, LINE_LEN - out, "   ");
-		for(i = 0; ofs < len && i < 16; i++, ofs++) {
-			unsigned char c = data[ofs];
-			if (!isascii(c) || !isprint(c))
-				c = '.';
-			out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
-		}
-		printf("%s\n", line);
-	}
-}
-
 int
 main(int argc, char **argv)
 {
diff --git a/app/test/test.h b/app/test/test.h
old mode 100644
new mode 100755
index 9ca894ae87..4de2788bf6
--- a/app/test/test.h
+++ b/app/test/test.h
@@ -46,8 +46,6 @@ extern const char *prgname;
 
 extern cmdline_parse_ctx_t main_ctx[];
 
-void test_hexdump(const char *title, const void *buf, unsigned int len);
-
 int main(int argc, char **argv);
 
 int test_pci(void);
diff --git a/app/test/test_ring.c b/app/test/test_ring.c
index 9ed41e47cf..2699e1ef13 100644
--- a/app/test/test_ring.c
+++ b/app/test/test_ring.c
@@ -58,6 +58,7 @@
 #include <rte_random.h>
 #include <rte_common.h>
 #include <rte_errno.h>
+#include <rte_hexdump.h>
 
 #include <cmdline_parse.h>
 
@@ -696,8 +697,8 @@ test_ring_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -742,8 +743,8 @@ test_ring_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -764,8 +765,8 @@ test_ring_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -810,8 +811,8 @@ test_ring_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -905,8 +906,8 @@ test_ring_burst_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -971,8 +972,8 @@ test_ring_burst_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -1020,8 +1021,8 @@ test_ring_burst_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -1043,8 +1044,8 @@ test_ring_burst_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
@@ -1093,8 +1094,8 @@ test_ring_burst_basic(void)
 
 	/* check data */
 	if (memcmp(src, dst, cur_dst - dst)) {
-		test_hexdump("src", src, cur_src - src);
-		test_hexdump("dst", dst, cur_dst - dst);
+		rte_hexdump("src", src, cur_src - src);
+		rte_hexdump("dst", dst, cur_dst - dst);
 		printf("data after dequeue is not the same\n");
 		goto fail;
 	}
diff --git a/lib/librte_eal/common/Makefile b/lib/librte_eal/common/Makefile
index efcde6a4ea..2604074811 100644
--- a/lib/librte_eal/common/Makefile
+++ b/lib/librte_eal/common/Makefile
@@ -39,6 +39,7 @@ INC += rte_pci_dev_ids.h rte_per_lcore.h rte_prefetch.h rte_random.h
 INC += rte_rwlock.h rte_spinlock.h rte_tailq.h rte_interrupts.h rte_alarm.h
 INC += rte_string_fns.h rte_cpuflags.h rte_version.h rte_tailq_elem.h
 INC += rte_eal_memconfig.h rte_malloc_heap.h
+INC += rte_hexdump.h
 
 ifeq ($(CONFIG_RTE_INSECURE_FUNCTION_WARNING),y)
 INC += rte_warnings.h
diff --git a/lib/librte_eal/common/eal_common_hexdump.c b/lib/librte_eal/common/eal_common_hexdump.c
new file mode 100755
index 0000000000..eb72f33e40
--- /dev/null
+++ b/lib/librte_eal/common/eal_common_hexdump.c
@@ -0,0 +1,122 @@
+/*-
+ *   BSD LICENSE
+ * 
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ * 
+ *   Redistribution and use in source and binary forms, with or without 
+ *   modification, are permitted provided that the following conditions 
+ *   are met:
+ * 
+ *     * Redistributions of source code must retain the above copyright 
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright 
+ *       notice, this list of conditions and the following disclaimer in 
+ *       the documentation and/or other materials provided with the 
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its 
+ *       contributors may be used to endorse or promote products derived 
+ *       from this software without specific prior written permission.
+ * 
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <rte_hexdump.h>
+#include <rte_string_fns.h>
+
+#define LINE_LEN 128
+
+/**************************************************************************//**
+*
+* rte_hexdump - Dump out memory in a special hex dump format.
+*
+* DESCRIPTION
+* Dump out the message buffer in a special hex dump output format with characters
+* printed for each line of 16 hex values.
+*
+* RETURNS: N/A
+*
+* SEE ALSO:
+*/
+
+void
+rte_hexdump(const char * title, const void * buf, unsigned int len)
+{
+    unsigned int i, out, ofs;
+    const unsigned char *data = buf;
+    char line[LINE_LEN];    /* space needed 8+16*3+3+16 == 75 */
+
+    printf("%s at [%p], len=%u\n", (title)? title  : "  Dump data", data, len);
+    ofs = 0;
+    while (ofs < len) {
+        /* format the line in the buffer, then use printf to output to screen */
+        out = rte_snprintf(line, LINE_LEN, "%08X:", ofs);
+        for (i = 0; ((ofs + i) < len) && (i < 16); i++)
+            out += rte_snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
+        for(; i <= 16; i++)
+            out += rte_snprintf(line+out, LINE_LEN - out, " | ");
+        for(i = 0; (ofs < len) && (i < 16); i++, ofs++) {
+            unsigned char c = data[ofs];
+            if ( (c < ' ') || (c > '~'))
+                c = '.';
+            out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
+        }
+        printf("%s\n", line);
+    }
+    fflush(stdout);
+}
+
+/**************************************************************************//**
+*
+* rte_memdump - Dump out memory in hex bytes with colons.
+*
+* DESCRIPTION
+* Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
+*
+* RETURNS: N/A
+*
+* SEE ALSO:
+*/
+
+void
+rte_memdump(const char * title, const void * buf, unsigned int len)
+{
+    unsigned int i, out;
+    const unsigned char *data = buf;
+    char line[LINE_LEN];
+
+    if ( title )
+    	printf("%s: ", title);
+
+    line[0] = '\0';
+    for (i = 0, out = 0; i < len; i++) {
+    	// Make sure we do not overrun the line buffer length.
+		if ( out >= (LINE_LEN - 4) ) {
+			printf("%s", line);
+			out = 0;
+			line[out] = '\0';
+		}
+		out += rte_snprintf(line+out, LINE_LEN - out, "%02x%s",
+				(data[i] & 0xff), ((i+1) < len)? ":" : "");
+    }
+    if ( out > 0 )
+    	printf("%s", line);
+   	printf("\n");
+
+    fflush(stdout);
+}
+
diff --git a/lib/librte_eal/common/include/rte_hexdump.h b/lib/librte_eal/common/include/rte_hexdump.h
new file mode 100755
index 0000000000..b0d5c3af78
--- /dev/null
+++ b/lib/librte_eal/common/include/rte_hexdump.h
@@ -0,0 +1,84 @@
+/*-
+ *   BSD LICENSE
+ * 
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ * 
+ *   Redistribution and use in source and binary forms, with or without 
+ *   modification, are permitted provided that the following conditions 
+ *   are met:
+ * 
+ *     * Redistributions of source code must retain the above copyright 
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright 
+ *       notice, this list of conditions and the following disclaimer in 
+ *       the documentation and/or other materials provided with the 
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its 
+ *       contributors may be used to endorse or promote products derived 
+ *       from this software without specific prior written permission.
+ * 
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ */
+
+#ifndef _RTE_HEXDUMP_H_
+#define _RTE_HEXDUMP_H_
+
+/**
+ * @file
+ * Simple API to dump out memory in a special hex format.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+* Dump out memory in a special hex dump format.
+*
+* @param title
+* 		If not NULL this string is printed as a header to the output.
+* @param buf
+* 		This is the buffer address to print out.
+* @param len
+* 		The number of bytes to dump out
+* @return
+* 		None.
+*/
+
+extern void
+rte_hexdump(const char * title, const void * buf, unsigned int len);
+
+/**
+* Dump out memory in a hex format with colons between bytes.
+*
+* @param title
+* 		If not NULL this string is printed as a header to the output.
+* @param buf
+* 		This is the buffer address to print out.
+* @param len
+* 		The number of bytes to dump out
+* @return
+* 		None.
+*/
+
+void
+rte_memdump(const char * title, const void * buf, unsigned int len);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_HEXDUMP_H_ */
diff --git a/lib/librte_eal/linuxapp/eal/Makefile b/lib/librte_eal/linuxapp/eal/Makefile
index c3fe583bab..f66c4c7bf7 100644
--- a/lib/librte_eal/linuxapp/eal/Makefile
+++ b/lib/librte_eal/linuxapp/eal/Makefile
@@ -66,6 +66,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_EAL_LINUXAPP) += eal_common_memory.c
 SRCS-$(CONFIG_RTE_LIBRTE_EAL_LINUXAPP) += eal_common_tailqs.c
 SRCS-$(CONFIG_RTE_LIBRTE_EAL_LINUXAPP) += eal_common_errno.c
 SRCS-$(CONFIG_RTE_LIBRTE_EAL_LINUXAPP) += eal_common_cpuflags.c
+SRCS-$(CONFIG_RTE_LIBRTE_EAL_LINUXAPP) += eal_common_hexdump.c
 
 CFLAGS_eal.o := -D_GNU_SOURCE
 CFLAGS_eal_thread.o := -D_GNU_SOURCE
diff --git a/lib/librte_mbuf/rte_mbuf.c b/lib/librte_mbuf/rte_mbuf.c
index 4f944c4dc2..2377db23b8 100644
--- a/lib/librte_mbuf/rte_mbuf.c
+++ b/lib/librte_mbuf/rte_mbuf.c
@@ -58,6 +58,7 @@
 #include <rte_mempool.h>
 #include <rte_mbuf.h>
 #include <rte_string_fns.h>
+#include <rte_hexdump.h>
 
 /*
  * ctrlmbuf constructor, given as a callback function to
@@ -139,33 +140,6 @@ rte_pktmbuf_init(struct rte_mempool *mp,
 	m->pkt.in_port = 0xff;
 }
 
-static void
-rte_pktmbuf_hexdump(const void *buf, unsigned int len)
-{
-	unsigned int i, out, ofs;
-	const unsigned char *data = buf;
-#define LINE_LEN 80
-	char line[LINE_LEN];
-
-	printf("  dump data at 0x%p, len=%u\n", data, len);
-	ofs = 0;
-	while (ofs < len) {
-		out = rte_snprintf(line, LINE_LEN, "  %08X", ofs);
-		for (i = 0; ofs+i < len && i < 16; i++)
-			out += rte_snprintf(line+out, LINE_LEN - out, " %02X",
-					data[ofs+i]&0xff);
-		for (; i <= 16; i++)
-			out += rte_snprintf(line+out, LINE_LEN - out, "   ");
-		for (i = 0; ofs < len && i < 16; i++, ofs++) {
-			unsigned char c = data[ofs];
-			if (!isascii(c) || !isprint(c))
-				c = '.';
-			out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
-		}
-		printf("%s\n", line);
-	}
-}
-
 /* do some sanity checks on a mbuf: panic if it fails */
 void
 rte_mbuf_sanity_check(const struct rte_mbuf *m, enum rte_mbuf_type t,
@@ -243,7 +217,7 @@ rte_pktmbuf_dump(const struct rte_mbuf *m, unsigned dump_len)
 		if (len > m->pkt.data_len)
 			len = m->pkt.data_len;
 		if (len != 0)
-			rte_pktmbuf_hexdump(m->pkt.data, len);
+			rte_hexdump(NULL, m->pkt.data, len);
 		dump_len -= len;
 		m = m->pkt.next;
 		nb_segs --;
-- 
2.39.5