libmemif: fix chain buffer support

Type: fix

This patch fixes chain buffer support as transmit side
missing to set the flag to the descriptor in case of
chain buffers.

Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
Change-Id: I73ff11be69a388f14fea39a19272d8eb76148fba
diff --git a/extras/libmemif/examples/common/icmp_proto.c b/extras/libmemif/examples/common/icmp_proto.c
index fafc8e4..70825ea 100644
--- a/extras/libmemif/examples/common/icmp_proto.c
+++ b/extras/libmemif/examples/common/icmp_proto.c
@@ -198,54 +198,6 @@
   return sizeof (struct icmphdr);
 }
 
-int
-resolve_packet (void *in_pck, ssize_t in_size, void *out_pck,
-		uint32_t *out_size, uint8_t ip_addr[4], uint8_t hw_addr[6])
-{
-  struct ether_header *eh;
-  struct ether_arp *eah;
-  struct iphdr *ip, *ip_out;
-  struct icmphdr *icmp;
-  *out_size = 0;
-
-  if ((in_pck == NULL) || (out_pck == NULL))
-    return -1;
-
-  eh = (struct ether_header *) in_pck;
-  *out_size = resolve_eth (eh, out_pck, hw_addr);
-
-  if (eh->ether_type == 0x0608)
-    {
-      eah = (struct ether_arp *) (in_pck + *out_size);
-      *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
-    }
-  else if (eh->ether_type == 0x0008)
-    {
-#ifdef ICMP_DBG
-      print_packet (in_pck + *out_size);
-#endif
-      ip = (struct iphdr *) (in_pck + *out_size);
-      ip_out = (struct iphdr *) (out_pck + *out_size);
-      *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
-      if (ip->protocol == 1)
-	{
-	  icmp = (struct icmphdr *) (in_pck + *out_size);
-	  *out_size += resolve_icmp (icmp, out_pck + *out_size);
-	  ((struct icmphdr *) (out_pck + *out_size - sizeof (struct icmphdr)))
-	    ->checksum = cksum (out_pck + *out_size - sizeof (struct icmphdr),
-				sizeof (struct icmphdr));
-	  /* payload */
-	  memcpy (out_pck + *out_size, in_pck + *out_size,
-		  in_size - *out_size);
-	  *out_size = in_size;
-	  ip_out->tot_len =
-	    __bswap_16 (*out_size - sizeof (struct ether_header));
-	  ip_out->check = cksum (ip_out, sizeof (struct iphdr));
-	}
-    }
-  return 0;
-}
-
 static ssize_t
 generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
 {
@@ -373,8 +325,8 @@
   while (0)
 
 int
-resolve_packet_zero_copy (void *pck, uint32_t *size, uint8_t ip_addr[4],
-			  uint8_t hw_addr[6])
+resolve_packet (void *pck, uint32_t *size, uint8_t ip_addr[4],
+		uint8_t hw_addr[6])
 {
   struct ether_header *eh;
   struct ether_arp *eah;
@@ -385,6 +337,10 @@
   if (pck == NULL)
     return 0;
 
+#ifdef ICMP_DBG
+  print_packet (pck);
+#endif
+
   GET_HEADER (eh, struct ether_header, pck, offset);
 
   memcpy (eh->ether_dhost, eh->ether_shost, 6);
@@ -450,8 +406,7 @@
 }
 
 int
-resolve_packet_zero_copy_add_encap (void **pck_, uint32_t *size,
-				    uint8_t ip_addr[4])
+resolve_packet_with_encap (void **pck_, uint32_t *size, uint8_t ip_addr[4])
 {
   struct ether_header *eh;
   struct iphdr *ip;
diff --git a/extras/libmemif/examples/common/icmp_proto.h b/extras/libmemif/examples/common/icmp_proto.h
index e346a3b..1371146 100644
--- a/extras/libmemif/examples/common/icmp_proto.h
+++ b/extras/libmemif/examples/common/icmp_proto.h
@@ -24,17 +24,12 @@
   ICMPR_FLOW_MODE_IP,
 } icmpr_flow_mode_t;
 
-int resolve_packet (void *in_pck, ssize_t in_size, void *out_pck,
-		    uint32_t *out_size, uint8_t ip_addr[4],
+/* resolve packet in place */
+int resolve_packet (void *pck, uint32_t *size, uint8_t ip_addr[4],
 		    uint8_t hw_addr[6]);
 
-/* resolve packet in place */
-int resolve_packet_zero_copy (void *pck, uint32_t *size, uint8_t ip_addr[4],
-			      uint8_t hw_addr[6]);
-
 /* resolve packet in place and add eth encap */
-int resolve_packet_zero_copy_add_encap (void **pck, uint32_t *size,
-					uint8_t ip_addr[4]);
+int resolve_packet_with_encap (void **pck, uint32_t *size, uint8_t ip_addr[4]);
 
 int generate_packet (void *pck, uint32_t *size, uint8_t saddr[4],
 		     uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq);
diff --git a/extras/libmemif/examples/common/packet_handler.c b/extras/libmemif/examples/common/packet_handler.c
index 705cc72..9d3e4d7 100644
--- a/extras/libmemif/examples/common/packet_handler.c
+++ b/extras/libmemif/examples/common/packet_handler.c
@@ -47,19 +47,39 @@
     {
       for (i = 0; i < c->tx_buf_num; i++)
 	{
-	  resolve_packet (c->rx_bufs[i].data, c->rx_bufs[i].len,
-			  c->tx_bufs[i].data, &c->tx_bufs[i].len, c->ip_addr,
-			  c->hw_addr);
+	  uint32_t len;
+	  void *packet = c->rx_bufs[i].data;
+
+	  memcpy (c->tx_bufs[i].data, c->rx_bufs[i].data, c->rx_bufs[i].len);
+	  c->tx_bufs[i].flags = c->rx_bufs[i].flags;
+	  len = c->tx_bufs[i].len = c->rx_bufs[i].len;
+
+	  while (c->rx_bufs[i].flags & MEMIF_BUFFER_FLAG_NEXT)
+	    {
+	      i++;
+	      memcpy (c->tx_bufs[i].data, c->rx_bufs[i].data,
+		      c->rx_bufs[i].len);
+	      c->tx_bufs[i].flags = c->rx_bufs[i].flags;
+	      len += c->tx_bufs[i].len = c->rx_bufs[i].len;
+	    }
+
+	  resolve_packet (packet, &len, c->ip_addr, c->hw_addr);
 	}
     }
   else
     {
       for (i = 0; i < c->rx_buf_num; i++)
 	{
-	  resolve_packet_zero_copy (c->rx_bufs[i].data, &c->rx_bufs[i].len,
-				    c->ip_addr, c->hw_addr);
+	  uint32_t len = c->rx_bufs[i].len;
+	  void *packet = c->rx_bufs[i].data;
+	  while (c->rx_bufs[i].flags & MEMIF_BUFFER_FLAG_NEXT)
+	    {
+	      i++;
+	      len += c->rx_bufs[i].len;
+	    }
+	  resolve_packet (packet, &len, c->ip_addr, c->hw_addr);
 	}
     }
 
   return 0;
-}
\ No newline at end of file
+}