diff mbox

[v2] Adding odp_packet_copy

Message ID 1396375267-1221-1-git-send-email-ciprian.barbu@linaro.org
State Under Review
Headers show

Commit Message

Ciprian Barbu April 1, 2014, 6:01 p.m. UTC
Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
---
 include/odp_packet.h                               |  9 +++++++
 .../linux-generic/include/odp_buffer_internal.h    |  2 ++
 platform/linux-generic/source/odp_buffer.c         |  5 ++++
 platform/linux-generic/source/odp_packet.c         | 28 ++++++++++++++++++++++
 test/packet_netmap/odp_example_pktio_netmap.c      | 14 +++--------
 5 files changed, 47 insertions(+), 11 deletions(-)

Comments

Ciprian Barbu April 3, 2014, 12:56 p.m. UTC | #1
Hi Petri,

I submitted a new patch version, I forgot to CC you in the patch email. Can
you give me your thoughts on it?

/Ciprian


On Tue, Apr 1, 2014 at 9:01 PM, Ciprian Barbu <ciprian.barbu@linaro.org>wrote:

> Signed-off-by: Ciprian Barbu <ciprian.barbu@linaro.org>
> ---
>  include/odp_packet.h                               |  9 +++++++
>  .../linux-generic/include/odp_buffer_internal.h    |  2 ++
>  platform/linux-generic/source/odp_buffer.c         |  5 ++++
>  platform/linux-generic/source/odp_packet.c         | 28
> ++++++++++++++++++++++
>  test/packet_netmap/odp_example_pktio_netmap.c      | 14 +++--------
>  5 files changed, 47 insertions(+), 11 deletions(-)
>
> diff --git a/include/odp_packet.h b/include/odp_packet.h
> index 3c7c9d0..3701078 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_dst Destination packet
> + * @param pkt_src Source packet
> + */
> +void odp_packet_copy(odp_packet_t pkt_dst, odp_packet_t pkt_src);
> +
>  #ifdef __cplusplus
>  }
>  #endif
> diff --git a/platform/linux-generic/include/odp_buffer_internal.h
> b/platform/linux-generic/include/odp_buffer_internal.h
> index c4e5b33..f8ebc7c 100644
> --- a/platform/linux-generic/include/odp_buffer_internal.h
> +++ b/platform/linux-generic/include/odp_buffer_internal.h
> @@ -107,6 +107,8 @@ typedef struct odp_buffer_chunk_hdr_t {
>
>  int odp_buffer_snprint(char *str, size_t n, odp_buffer_t buf);
>
> +void odp_buffer_copy_scatter(odp_buffer_t buf_dst, odp_buffer_t buf_src);
> +
>
>  #ifdef __cplusplus
>  }
> diff --git a/platform/linux-generic/source/odp_buffer.c
> b/platform/linux-generic/source/odp_buffer.c
> index f5b02fd..f25bd45 100644
> --- a/platform/linux-generic/source/odp_buffer.c
> +++ b/platform/linux-generic/source/odp_buffer.c
> @@ -122,3 +122,8 @@ void odp_buffer_print(odp_buffer_t buf)
>
>
>
> +void odp_buffer_copy_scatter(odp_buffer_t buf_dst, odp_buffer_t buf_src)
> +{
> +       (void)buf_dst;
> +       (void)buf_src;
> +}
> diff --git a/platform/linux-generic/source/odp_packet.c
> b/platform/linux-generic/source/odp_packet.c
> index 5f07374..5471b0a 100644
> --- a/platform/linux-generic/source/odp_packet.c
> +++ b/platform/linux-generic/source/odp_packet.c
> @@ -331,3 +331,31 @@ void odp_packet_print(odp_packet_t pkt)
>         printf("\n%s\n", str);
>  }
>
> +void odp_packet_copy(odp_packet_t pkt_dst, odp_packet_t pkt_src)
> +{
> +       odp_packet_hdr_t *const pkt_hdr_dst = odp_packet_hdr(pkt_dst);
> +       odp_packet_hdr_t *const pkt_hdr_src = odp_packet_hdr(pkt_src);
> +       const size_t start_offset = ODP_FIELD_SIZEOF(odp_packet_hdr_t,
> buf_hdr);
> +       uint8_t *start_src;
> +       uint8_t *start_dst;
> +       size_t len;
> +
> +       /* Copy packet header */
> +       start_dst = (uint8_t *)pkt_hdr_dst + start_offset;
> +       start_src = (uint8_t *)pkt_hdr_src + start_offset;
> +       len = ODP_OFFSETOF(odp_packet_hdr_t, payload) - start_offset;
> +       memcpy(start_dst, start_src, len);
> +
> +       /* Copy frame payload */
> +       start_dst = (uint8_t *)odp_packet_start(pkt_dst);
> +       start_src = (uint8_t *)odp_packet_start(pkt_src);
> +       len = pkt_hdr_src->frame_len;
> +       memcpy(start_dst, start_src, len);
> +
> +       /* Copy useful things from the buffer header */
> +       pkt_hdr_dst->buf_hdr.cur_offset = pkt_hdr_src->buf_hdr.cur_offset;
> +
> +       /* Create a copy of the scatter list */
> +       odp_buffer_copy_scatter(odp_buffer_from_packet(pkt_dst),
> +                               odp_buffer_from_packet(pkt_src));
> +}
> diff --git a/test/packet_netmap/odp_example_pktio_netmap.c
> b/test/packet_netmap/odp_example_pktio_netmap.c
> index 2d74f8a..586356d 100644
> --- a/test/packet_netmap/odp_example_pktio_netmap.c
> +++ b/test/packet_netmap/odp_example_pktio_netmap.c
> @@ -149,26 +149,16 @@ static void *pktio_queue_thread(void *arg)
>
>                 /* Lookup the thread associated with the entry */
>                 pktio_nr = args->pktio_tbl[pktio_tmp];
> -               odp_queue_enq(args->thread[pktio_nr].bridge_q, buf);
>
>                 /* Send back packets arrived on physical interface */
>                 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_copy, pkt);
>
>                         swap_pkt_addrs(&pkt_copy, 1);
>
> @@ -176,6 +166,8 @@ static void *pktio_queue_thread(void *arg)
>                         odp_queue_enq(outq_def, buf_copy);
>                 }
>
> +               odp_queue_enq(args->thread[pktio_nr].bridge_q, buf);
> +
>                 /* Print packet counts every once in a while */
>                 if (odp_unlikely(pkt_cnt++ % 100000 == 0)) {
>                         printf("  [%02i] pkt_cnt:%lu\n", thr, pkt_cnt);
> --
> 1.8.3.2
>
>
Petri Savolainen April 3, 2014, 2 p.m. UTC | #2
Hi,

Otherwise OK, but missing the dest buffer length check (and return an error 
code if it's too short), we talked about.

-Petri


On Thursday, 3 April 2014 15:56:00 UTC+3, Ciprian Barbu wrote:

> Hi Petri,
>
> I submitted a new patch version, I forgot to CC you in the patch email. 
> Can you give me your thoughts on it?
>
> /Ciprian
>
>
>
diff mbox

Patch

diff --git a/include/odp_packet.h b/include/odp_packet.h
index 3c7c9d0..3701078 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_dst Destination packet
+ * @param pkt_src Source packet
+ */
+void odp_packet_copy(odp_packet_t pkt_dst, odp_packet_t pkt_src);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/platform/linux-generic/include/odp_buffer_internal.h b/platform/linux-generic/include/odp_buffer_internal.h
index c4e5b33..f8ebc7c 100644
--- a/platform/linux-generic/include/odp_buffer_internal.h
+++ b/platform/linux-generic/include/odp_buffer_internal.h
@@ -107,6 +107,8 @@  typedef struct odp_buffer_chunk_hdr_t {
 
 int odp_buffer_snprint(char *str, size_t n, odp_buffer_t buf);
 
+void odp_buffer_copy_scatter(odp_buffer_t buf_dst, odp_buffer_t buf_src);
+
 
 #ifdef __cplusplus
 }
diff --git a/platform/linux-generic/source/odp_buffer.c b/platform/linux-generic/source/odp_buffer.c
index f5b02fd..f25bd45 100644
--- a/platform/linux-generic/source/odp_buffer.c
+++ b/platform/linux-generic/source/odp_buffer.c
@@ -122,3 +122,8 @@  void odp_buffer_print(odp_buffer_t buf)
 
 
 
+void odp_buffer_copy_scatter(odp_buffer_t buf_dst, odp_buffer_t buf_src)
+{
+	(void)buf_dst;
+	(void)buf_src;
+}
diff --git a/platform/linux-generic/source/odp_packet.c b/platform/linux-generic/source/odp_packet.c
index 5f07374..5471b0a 100644
--- a/platform/linux-generic/source/odp_packet.c
+++ b/platform/linux-generic/source/odp_packet.c
@@ -331,3 +331,31 @@  void odp_packet_print(odp_packet_t pkt)
 	printf("\n%s\n", str);
 }
 
+void odp_packet_copy(odp_packet_t pkt_dst, odp_packet_t pkt_src)
+{
+	odp_packet_hdr_t *const pkt_hdr_dst = odp_packet_hdr(pkt_dst);
+	odp_packet_hdr_t *const pkt_hdr_src = odp_packet_hdr(pkt_src);
+	const size_t start_offset = ODP_FIELD_SIZEOF(odp_packet_hdr_t, buf_hdr);
+	uint8_t *start_src;
+	uint8_t *start_dst;
+	size_t len;
+
+	/* Copy packet header */
+	start_dst = (uint8_t *)pkt_hdr_dst + start_offset;
+	start_src = (uint8_t *)pkt_hdr_src + start_offset;
+	len = ODP_OFFSETOF(odp_packet_hdr_t, payload) - start_offset;
+	memcpy(start_dst, start_src, len);
+
+	/* Copy frame payload */
+	start_dst = (uint8_t *)odp_packet_start(pkt_dst);
+	start_src = (uint8_t *)odp_packet_start(pkt_src);
+	len = pkt_hdr_src->frame_len;
+	memcpy(start_dst, start_src, len);
+
+	/* Copy useful things from the buffer header */
+	pkt_hdr_dst->buf_hdr.cur_offset = pkt_hdr_src->buf_hdr.cur_offset;
+
+	/* Create a copy of the scatter list */
+	odp_buffer_copy_scatter(odp_buffer_from_packet(pkt_dst),
+				odp_buffer_from_packet(pkt_src));
+}
diff --git a/test/packet_netmap/odp_example_pktio_netmap.c b/test/packet_netmap/odp_example_pktio_netmap.c
index 2d74f8a..586356d 100644
--- a/test/packet_netmap/odp_example_pktio_netmap.c
+++ b/test/packet_netmap/odp_example_pktio_netmap.c
@@ -149,26 +149,16 @@  static void *pktio_queue_thread(void *arg)
 
 		/* Lookup the thread associated with the entry */
 		pktio_nr = args->pktio_tbl[pktio_tmp];
-		odp_queue_enq(args->thread[pktio_nr].bridge_q, buf);
 
 		/* Send back packets arrived on physical interface */
 		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_copy, pkt);
 
 			swap_pkt_addrs(&pkt_copy, 1);
 
@@ -176,6 +166,8 @@  static void *pktio_queue_thread(void *arg)
 			odp_queue_enq(outq_def, buf_copy);
 		}
 
+		odp_queue_enq(args->thread[pktio_nr].bridge_q, buf);
+
 		/* Print packet counts every once in a while */
 		if (odp_unlikely(pkt_cnt++ % 100000 == 0)) {
 			printf("  [%02i] pkt_cnt:%lu\n", thr, pkt_cnt);