libmemif: refactor examples

- icmp_responder: responds to ICMPv4 and ARP requests
- loopback: connects two interfaces and sends a
  verification packet from master memif to slave memif
  where it is looped back
- loopback (reverse path): reverses direction of packet
  in loopback application (slave memif to master memif)

Type: refactor

Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Change-Id: Ie90aaa3367269408efb6c5d538ad5aa827432238
Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
diff --git a/extras/libmemif/examples/common/sender.c b/extras/libmemif/examples/common/sender.c
new file mode 100644
index 0000000..bad926f
--- /dev/null
+++ b/extras/libmemif/examples/common/sender.c
@@ -0,0 +1,55 @@
+#include <common.h>
+
+int
+send_packets (memif_connection_t *c, uint16_t qid,
+	      packet_generator_t *generator, uint32_t num_pkts,
+	      uint16_t max_pkt_size)
+{
+  int err, i;
+  uint16_t tx;
+
+  do
+    {
+      err = memif_buffer_alloc (c->conn, qid, c->tx_bufs,
+				num_pkts > MAX_MEMIF_BUFS ? MAX_MEMIF_BUFS :
+								  num_pkts,
+				&c->tx_buf_num, max_pkt_size);
+      /* suppress full ring error MEMIF_ERR_NOBUF_RING */
+      if (err != MEMIF_ERR_SUCCESS && err != MEMIF_ERR_NOBUF_RING)
+	{
+	  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
+	  goto error;
+	}
+
+      /* generate packet inside allocated buffers */
+      err = generator (c, num_pkts);
+      if (err != 0)
+	{
+	  INFO ("paclet generator error: %d", err);
+	  goto error;
+	}
+
+      err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &tx);
+      if (err != MEMIF_ERR_SUCCESS)
+	{
+	  INFO ("memif_tx_burst: %s", memif_strerror (err));
+	  goto error;
+	}
+      c->tx_buf_num -= tx;
+
+      /* Should never happen... */
+      if (c->tx_buf_num > 0)
+	{
+	  INFO ("Failed to send allocated packets");
+	  goto error;
+	}
+      num_pkts -= tx;
+    }
+  while (num_pkts > 0);
+
+  return 0;
+
+error:
+  /* TODO: free alloocated tx buffers */
+  return -1;
+}
\ No newline at end of file