diff mbox

Adding odp_packet_copy

Message ID 1396025774-56222-1-git-send-email-ciprian.barbu@linaro.org
State Superseded
Headers show

Commit Message

Ciprian Barbu March 28, 2014, 4:56 p.m. UTC
odp_packet_copy for copying pkt contents and metadata

Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
---
 include/odp_packet.h                          |  9 +++++++++
 platform/linux-generic/source/odp_packet.c    | 24 ++++++++++++++++++++++++
 test/packet_netmap/odp_example_pktio_netmap.c | 11 +----------
 3 files changed, 34 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/include/odp_packet.h b/include/odp_packet.h
index 3c7c9d0..c0bedcd 100644
--- a/include/odp_packet.h
+++ b/include/odp_packet.h
@@ -209,6 +209,15 @@  void odp_packet_set_l4_offset(odp_packet_t pkt, size_t offset);
  */
 void odp_packet_print(odp_packet_t pkt);
 
+/**
+ * Copy contents and metadata from pkt_src to pkt_dst
+ * Useful when creating copies of packets
+ *
+ * @param pkt_src Source packet
+ * @param pkt_dst Destination packet
+ */
+void odp_packet_copy(odp_packet_t pkt_src, odp_packet_t pkt_dst);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/platform/linux-generic/source/odp_packet.c b/platform/linux-generic/source/odp_packet.c
index b990222..47cabd3 100644
--- a/platform/linux-generic/source/odp_packet.c
+++ b/platform/linux-generic/source/odp_packet.c
@@ -331,3 +331,27 @@  void odp_packet_print(odp_packet_t pkt)
 	printf("\n%s\n", str);
 }
 
+void odp_packet_copy(odp_packet_t pkt_src, odp_packet_t pkt_dst)
+{
+	odp_packet_hdr_t *const pkt_hdr_src = odp_packet_hdr(pkt_src);
+	odp_packet_hdr_t *const pkt_hdr_dst = odp_packet_hdr(pkt_dst);
+	const size_t start_offset = ODP_FIELD_SIZEOF(odp_packet_hdr_t, buf_hdr);
+	odp_pktio_t input;
+	uint8_t *start_src;
+	uint8_t *start_dst;
+	size_t len;
+
+	/* Preserve input */
+	input = pkt_hdr_dst->input;
+
+	start_src = (uint8_t *)pkt_hdr_src + start_offset;
+	start_dst = (uint8_t *)pkt_hdr_dst + start_offset;
+
+	len = ODP_OFFSETOF(odp_packet_hdr_t, payload) - start_offset;
+	len += pkt_hdr_src->frame_offset + pkt_hdr_src->frame_len;
+
+	memcpy(start_dst, start_src, len);
+
+	/* Restore input */
+	pkt_hdr_dst->input = input;
+}
diff --git a/test/packet_netmap/odp_example_pktio_netmap.c b/test/packet_netmap/odp_example_pktio_netmap.c
index 2d74f8a..f9ed3b3 100644
--- a/test/packet_netmap/odp_example_pktio_netmap.c
+++ b/test/packet_netmap/odp_example_pktio_netmap.c
@@ -155,20 +155,11 @@  static void *pktio_queue_thread(void *arg)
 		if (args->thread[pktio_nr].netmap_mode == ODP_NETMAP_MODE_HW) {
 			odp_packet_t pkt_copy;
 			odp_buffer_t buf_copy;
-			size_t frame_len = odp_packet_get_len(pkt);
-			size_t l2_offset = odp_packet_l2_offset(pkt);
-			size_t l3_offset = odp_packet_l3_offset(pkt);
 
 			buf_copy = odp_buffer_alloc(pkt_pool);
 			pkt_copy = odp_packet_from_buffer(buf_copy);
 
-			odp_packet_init(pkt_copy);
-			odp_packet_set_len(pkt_copy, frame_len);
-			odp_packet_set_l2_offset(pkt_copy, l2_offset);
-			odp_packet_set_l3_offset(pkt_copy, l3_offset);
-
-			memcpy(odp_buffer_addr(pkt_copy),
-			       odp_buffer_addr(pkt), frame_len);
+			odp_packet_copy(pkt, pkt_copy);
 
 			swap_pkt_addrs(&pkt_copy, 1);