build: add snap packaging (experimental)
[vpp.git] / extras / libmemif / examples / icmp_responder-epoll / main.c
index 634e180..0e9d96e 100644 (file)
 
 #include <time.h>
 
+#ifndef TIME_UTC
+#define TIME_UTC 1
+#endif
+
 #include <libmemif.h>
 #include <icmp_proto.h>
 
@@ -82,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;
@@ -206,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);
 }
@@ -278,6 +287,7 @@ int
 on_connect (memif_conn_handle_t conn, void *private_ctx)
 {
   INFO ("memif connected!");
+  memif_refill_queue (conn, 0, -1, ICMPR_HEADROOM);
   enable_log = 1;
   return 0;
 }
@@ -294,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)
@@ -319,7 +329,7 @@ icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
   memif_connection_t *c = &memif_connection[index];
   int err;
   /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
-  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 0);
+  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 128);
   if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
     {
       INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -361,10 +371,9 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
     }
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
-  uint16_t rx, tx;
-  uint16_t fb;
-  int i;                       /* rx buffer iterator */
-  int j;                       /* tx bufferiterator */
+  uint16_t rx = 0, tx = 0;
+  int i = 0;                   /* rx buffer iterator */
+  int j = 0;                   /* tx bufferiterator */
 
   /* loop while there are packets in shm */
   do
@@ -380,54 +389,34 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
        }
 
       i = 0;
-
-      /* try to alloc required number of buffers buffers */
-      err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
+      memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
+      err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
       if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
        {
          INFO ("memif_buffer_alloc: %s", memif_strerror (err));
          goto error;
        }
       j = 0;
-      c->tx_err_counter = rx - tx;
+      c->tx_err_counter += rx - tx;
 
-      /* process bufers */
       while (tx)
        {
-         while (tx > 2)
-           {
-             resolve_packet ((void *) (c->rx_bufs + i)->data,
-                             (c->rx_bufs + i)->data_len,
-                             (void *) (c->tx_bufs + j)->data,
-                             &(c->tx_bufs + j)->data_len, c->ip_addr);
-             resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
-                             (c->rx_bufs + i + 1)->data_len,
-                             (void *) (c->tx_bufs + j + 1)->data,
-                             &(c->tx_bufs + j + 1)->data_len, c->ip_addr);
-
-             i += 2;
-             j += 2;
-             tx -= 2;
-           }
          resolve_packet ((void *) (c->rx_bufs + i)->data,
-                         (c->rx_bufs + i)->data_len,
+                         (c->rx_bufs + i)->len,
                          (void *) (c->tx_bufs + j)->data,
-                         &(c->tx_bufs + j)->data_len, c->ip_addr);
+                         &(c->tx_bufs + j)->len, c->ip_addr);
          i++;
          j++;
          tx--;
        }
-      /* mark memif buffers and shared memory buffers as free */
-      /* free processed buffers */
-      err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+
+      err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
       if (err != MEMIF_ERR_SUCCESS)
        INFO ("memif_buffer_free: %s", memif_strerror (err));
-      rx -= fb;
+      rx -= rx;
 
-      DBG ("freed %d buffers. %u/%u alloc/free buffers",
-          fb, rx, MAX_MEMIF_BUFS - rx);
+      DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
 
-      /* transmit allocated buffers */
       err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
       if (err != MEMIF_ERR_SUCCESS)
        {
@@ -442,12 +431,12 @@ on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  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 -= fb;
+  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;
 }
 
@@ -466,11 +455,9 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
     }
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
-  uint16_t rx, tx;
-  uint16_t fb;
+  uint16_t rx = 0, tx = 0;
   int i;                       /* rx buffer iterator */
   int j;                       /* tx bufferiterator */
-  int prev_i;                  /* first allocated rx buffer */
 
   /* loop while there are packets in shm */
   do
@@ -485,14 +472,14 @@ 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)
        {
 
          /* try to alloc required number of buffers buffers */
-         err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
+         err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
          if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
            {
              INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -506,37 +493,35 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
              while (tx > 2)
                {
                  resolve_packet ((void *) (c->rx_bufs + i)->data,
-                                 (c->rx_bufs + i)->data_len,
+                                 (c->rx_bufs + i)->len,
                                  (void *) (c->tx_bufs + j)->data,
-                                 &(c->tx_bufs + j)->data_len, c->ip_addr);
+                                 &(c->tx_bufs + j)->len, c->ip_addr);
                  resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
-                                 (c->rx_bufs + i + 1)->data_len,
+                                 (c->rx_bufs + i + 1)->len,
                                  (void *) (c->tx_bufs + j + 1)->data,
-                                 &(c->tx_bufs + j + 1)->data_len,
-                                 c->ip_addr);
+                                 &(c->tx_bufs + j + 1)->len, c->ip_addr);
 
                  i += 2;
                  j += 2;
                  tx -= 2;
                }
              resolve_packet ((void *) (c->rx_bufs + i)->data,
-                             (c->rx_bufs + i)->data_len,
+                             (c->rx_bufs + i)->len,
                              (void *) (c->tx_bufs + j)->data,
-                             &(c->tx_bufs + j)->data_len, c->ip_addr);
+                             &(c->tx_bufs + j)->len, c->ip_addr);
              i++;
              j++;
              tx--;
            }
          /* mark memif buffers and shared memory buffers as free */
          /* free processed buffers */
-         err = memif_buffer_free (c->conn, qid, c->rx_bufs + prev_i, j, &fb);
+         err = memif_refill_queue (c->conn, qid, j, ICMPR_HEADROOM);
          if (err != MEMIF_ERR_SUCCESS)
            INFO ("memif_buffer_free: %s", memif_strerror (err));
-         rx -= fb;
-         prev_i = i;
+         rx -= j;
 
          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);
@@ -555,12 +540,12 @@ on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  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 -= fb;
+  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;
 }
 
@@ -581,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
     {
@@ -605,17 +588,17 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
              if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
                break;
              resolve_packet ((void *) (c->rx_bufs + i)->data,
-                             (c->rx_bufs + i)->data_len,
+                             (c->rx_bufs + i)->len,
                              (void *) (c->tx_bufs + i)->data,
-                             &(c->tx_bufs + i)->data_len, c->ip_addr);
+                             &(c->tx_bufs + i)->len, c->ip_addr);
              icmpr_tx_burst (c->index, qid);
            }
        }
 
-      err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+      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 -= fb;
+      c->rx_buf_num -= rx;
 
 
     }
@@ -624,12 +607,12 @@ on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  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 -= fb;
+  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;
 }
 
@@ -650,15 +633,13 @@ 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;
   args.buffer_size = 2048;
-  args.num_s2m_rings = 2;
-  args.num_m2s_rings = 2;
+  args.num_s2m_rings = 1;
+  args.num_m2s_rings = 1;
   strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
-  strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
   args.mode = 0;
   /* socket filename is not specified, because this app is supposed to
      connect to VPP over memif. so default socket filename will be used */
@@ -779,7 +760,7 @@ print_help ()
   printf (" (debug)");
 #endif
   printf ("\n");
-  printf ("memif version: %d\n", MEMIF_VERSION);
+  printf ("memif version: %d\n", memif_get_version ());
   printf ("commands:\n");
   printf ("\thelp - prints this help\n");
   printf ("\texit - exit app\n");
@@ -968,7 +949,7 @@ icmpr_send_proc (void *data)
       i = 0;
       err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
                                MAX_MEMIF_BUFS >
-                               count ? count : MAX_MEMIF_BUFS, &tx, 0);
+                               count ? count : MAX_MEMIF_BUFS, &tx, 128);
       if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
        {
          INFO ("memif_buffer_alloc: %s Thread exiting...\n",
@@ -982,16 +963,16 @@ icmpr_send_proc (void *data)
          while (tx > 2)
            {
              generate_packet ((void *) c->tx_bufs[i].data,
-                              &c->tx_bufs[i].data_len, c->ip_addr,
+                              &c->tx_bufs[i].len, c->ip_addr,
                               d->ip_daddr, d->hw_daddr, seq++);
              generate_packet ((void *) c->tx_bufs[i + 1].data,
-                              &c->tx_bufs[i + 1].data_len, c->ip_addr,
+                              &c->tx_bufs[i + 1].len, c->ip_addr,
                               d->ip_daddr, d->hw_daddr, seq++);
              i += 2;
              tx -= 2;
            }
          generate_packet ((void *) c->tx_bufs[i].data,
-                          &c->tx_bufs[i].data_len, c->ip_addr,
+                          &c->tx_bufs[i].len, c->ip_addr,
                           d->ip_daddr, d->hw_daddr, seq++);
          i++;
          tx--;
@@ -1008,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;
@@ -1117,7 +1099,6 @@ error:
 int
 user_input_handler ()
 {
-  int i;
   char *in = (char *) malloc (256);
   char *ui = fgets (in, 256, stdin);
   char *end;
@@ -1234,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));
@@ -1313,13 +1293,17 @@ 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);
+  err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
   if (err != MEMIF_ERR_SUCCESS)
-    INFO ("memif_init: %s", memif_strerror (err));
+    {
+      INFO ("memif_init: %s", memif_strerror (err));
+      icmpr_free ();
+      exit (-1);
+    }
 
   for (i = 0; i < MAX_CONNS; i++)
     {
-      memif_connection[i].conn = NULL;
+      memset (&memif_connection[i], 0, sizeof (memif_connection_t));
       ctx[i] = i;
     }