libmemif: version 3.0
[vpp.git] / extras / libmemif / examples / icmp_responder-epoll / main.c
index fd354a3..0e9d96e 100644 (file)
@@ -86,6 +86,8 @@
 #define MAX_MEMIF_BUFS  256
 #define MAX_CONNS       50
 
+#define ICMPR_HEADROOM 64
+
 int epfd;
 int out_fd;
 uint8_t enable_log;
@@ -210,7 +212,10 @@ print_memif_details ()
       if (md.link_up_down)
        printf ("up\n");
       else
-       printf ("down\n");
+       {
+         printf ("down\n");
+         printf ("\treason: %s\n", md.error);
+       }
     }
   free (buf);
 }
@@ -282,7 +287,7 @@ int
 on_connect (memif_conn_handle_t conn, void *private_ctx)
 {
   INFO ("memif connected!");
-  memif_refill_queue (conn, 0, -1, 0);
+  memif_refill_queue (conn, 0, -1, ICMPR_HEADROOM);
   enable_log = 1;
   return 0;
 }
@@ -299,7 +304,7 @@ on_disconnect (memif_conn_handle_t conn, void *private_ctx)
 /* user needs to watch new fd or stop watching fd that is about to be closed.
     control fd will be modified during connection establishment to minimize CPU usage */
 int
-control_fd_update (int fd, uint8_t events)
+control_fd_update (int fd, uint8_t events, void *ctx)
 {
   /* convert memif event definitions to epoll events */
   if (events & MEMIF_FD_EVENT_DEL)
@@ -367,7 +372,6 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
   uint16_t rx = 0, tx = 0;
-  uint16_t fb = 0;
   int i = 0;                   /* rx buffer iterator */
   int j = 0;                   /* tx bufferiterator */
 
@@ -406,13 +410,12 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
          tx--;
        }
 
-      err = memif_refill_queue (c->conn, qid, rx, 0);
+      err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
       if (err != MEMIF_ERR_SUCCESS)
        INFO ("memif_buffer_free: %s", memif_strerror (err));
       rx -= rx;
 
-      DBG ("freed %d buffers. %u/%u alloc/free buffers",
-          rx, rx, MAX_MEMIF_BUFS - rx);
+      DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
 
       err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
       if (err != MEMIF_ERR_SUCCESS)
@@ -428,12 +431,12 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_refill_queue (c->conn, qid, rx, 0);
+  err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
   c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
-       fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
 }
 
@@ -453,10 +456,8 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
   uint16_t rx = 0, tx = 0;
-  uint16_t fb;
   int i;                       /* rx buffer iterator */
   int j;                       /* tx bufferiterator */
-  int prev_i;                  /* first allocated rx buffer */
 
   /* loop while there are packets in shm */
   do
@@ -471,7 +472,7 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
          goto error;
        }
 
-      prev_i = i = 0;
+      i = 0;
 
       /* loop while there are RX buffers to be processed */
       while (rx)
@@ -514,14 +515,13 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
            }
          /* mark memif buffers and shared memory buffers as free */
          /* free processed buffers */
-         err = memif_refill_queue (c->conn, qid, j, 0);
+         err = memif_refill_queue (c->conn, qid, j, ICMPR_HEADROOM);
          if (err != MEMIF_ERR_SUCCESS)
            INFO ("memif_buffer_free: %s", memif_strerror (err));
          rx -= j;
-         prev_i = i;
 
          DBG ("freed %d buffers. %u/%u alloc/free buffers",
-              fb, rx, MAX_MEMIF_BUFS - rx);
+              rx, rx, MAX_MEMIF_BUFS - rx);
 
          /* transmit allocated buffers */
          err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
@@ -540,12 +540,12 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_refill_queue (c->conn, qid, rx, 0);
+  err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
   c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
-       fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
 }
 
@@ -566,8 +566,6 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   int err = MEMIF_ERR_SUCCESS, ret_val;
   int i;
   uint16_t rx, tx;
-  uint16_t fb;
-  uint16_t pck_seq;
 
   do
     {
@@ -597,7 +595,7 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
            }
        }
 
-      err = memif_refill_queue (c->conn, qid, rx, 0);
+      err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
       if (err != MEMIF_ERR_SUCCESS)
        INFO ("memif_buffer_free: %s", memif_strerror (err));
       c->rx_buf_num -= rx;
@@ -609,12 +607,12 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_refill_queue (c->conn, qid, rx, 0);
+  err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
   c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
-       fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
 }
 
@@ -635,7 +633,6 @@ icmpr_memif_create (long index, long mode, char *s)
 
   /* setting memif connection arguments */
   memif_conn_args_t args;
-  int fd = -1;
   memset (&args, 0, sizeof (args));
   args.is_master = mode;
   args.log2_ring_size = 11;
@@ -992,6 +989,7 @@ icmpr_send_proc (void *data)
       count -= tx;
     }
   timespec_get (&end, TIME_UTC);
+  printf ("\n\n");
   INFO ("Pakcet sequence finished!");
   INFO ("Seq len: %u", seq);
   uint64_t t1 = end.tv_sec - start.tv_sec;
@@ -1101,7 +1099,6 @@ error:
 int
 user_input_handler ()
 {
-  int i;
   char *in = (char *) malloc (256);
   char *ui = fgets (in, 256, stdin);
   char *end;
@@ -1218,9 +1215,8 @@ done:
 int
 poll_event (int timeout)
 {
-  struct epoll_event evt, *e;
+  struct epoll_event evt;
   int app_err = 0, memif_err = 0, en = 0;
-  int tmp, nfd;
   uint32_t events = 0;
   struct timespec start, end;
   memset (&evt, 0, sizeof (evt));
@@ -1297,7 +1293,7 @@ main ()
   /* if valid callback is passed as argument, fd event polling will be done by user
      all file descriptors and events will be passed to user in this callback */
   /* if callback is set to NULL libmemif will handle fd event polling */
-  err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
+  err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
   if (err != MEMIF_ERR_SUCCESS)
     {
       INFO ("memif_init: %s", memif_strerror (err));