libmemif: ubuntu 18.04 build fix
[vpp.git] / extras / libmemif / src / main.c
index 2e2602e..cb24083 100644 (file)
@@ -406,17 +406,16 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
 {
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   libmemif_main_t *lm = &libmemif_main;
+  memset (lm, 0, sizeof (libmemif_main_t));
 
   if (app_name)
     {
-      lm->app_name = malloc (strlen (app_name) + sizeof (char));
-      memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
+      uint8_t len = (strlen (app_name) < MEMIF_NAME_LEN)
+       ? MEMIF_NAME_LEN : strlen (app_name);
       strncpy ((char *) lm->app_name, app_name, strlen (app_name));
     }
   else
     {
-      lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char));
-      memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
       strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
               strlen (MEMIF_DEFAULT_APP_NAME));
     }
@@ -438,8 +437,6 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
       DBG ("libmemif event polling initialized");
     }
 
-  memset (&lm->ms, 0, sizeof (memif_socket_t));
-
   lm->control_list_len = 2;
   lm->interrupt_list_len = 2;
   lm->listener_list_len = 1;
@@ -490,7 +487,6 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
   lm->arm.it_value.tv_nsec = 0;
   lm->arm.it_interval.tv_sec = 2;
   lm->arm.it_interval.tv_nsec = 0;
-  memset (&lm->disarm, 0, sizeof (lm->disarm));
 
   if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
     {
@@ -524,8 +520,8 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
   if (conn == NULL)
     return MEMIF_ERR_NOCONN;
   uint8_t num =
-    (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.
-    num_m2s_rings;
+    (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
 
@@ -859,21 +855,20 @@ memif_control_fd_handler (int fd, uint8_t events)
          if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
            {
              num =
-               (((memif_connection_t *) e->data_struct)->args.
-                is_master) ? ((memif_connection_t *) e->data_struct)->
-               run_args.num_s2m_rings : ((memif_connection_t *) e->
-                                         data_struct)->run_args.
-               num_m2s_rings;
+               (((memif_connection_t *) e->data_struct)->
+                args.is_master) ? ((memif_connection_t *) e->
+                                   data_struct)->run_args.
+               num_s2m_rings : ((memif_connection_t *) e->data_struct)->
+               run_args.num_m2s_rings;
              for (i = 0; i < num; i++)
                {
-                 if (((memif_connection_t *) e->data_struct)->rx_queues[i].
-                     int_fd == fd)
+                 if (((memif_connection_t *) e->data_struct)->
+                     rx_queues[i].int_fd == fd)
                    {
-                     ((memif_connection_t *) e->
-                      data_struct)->on_interrupt ((void *) e->data_struct,
-                                                  ((memif_connection_t *)
-                                                   e->data_struct)->
-                                                  private_ctx, i);
+                     ((memif_connection_t *) e->data_struct)->
+                       on_interrupt ((void *) e->data_struct,
+                                     ((memif_connection_t *) e->
+                                      data_struct)->private_ctx, i);
                      return MEMIF_ERR_SUCCESS;
                    }
                }
@@ -900,24 +895,24 @@ memif_control_fd_handler (int fd, uint8_t events)
          if (events & MEMIF_FD_EVENT_READ)
            {
              err =
-               ((memif_connection_t *) e->data_struct)->read_fn (e->
-                                                                 data_struct);
+               ((memif_connection_t *) e->data_struct)->
+               read_fn (e->data_struct);
              if (err != MEMIF_ERR_SUCCESS)
                return err;
            }
          if (events & MEMIF_FD_EVENT_WRITE)
            {
              err =
-               ((memif_connection_t *) e->data_struct)->write_fn (e->
-                                                                  data_struct);
+               ((memif_connection_t *) e->data_struct)->
+               write_fn (e->data_struct);
              if (err != MEMIF_ERR_SUCCESS)
                return err;
            }
          if (events & MEMIF_FD_EVENT_ERROR)
            {
              err =
-               ((memif_connection_t *) e->data_struct)->error_fn (e->
-                                                                  data_struct);
+               ((memif_connection_t *) e->data_struct)->
+               error_fn (e->data_struct);
              if (err != MEMIF_ERR_SUCCESS)
                return err;
            }
@@ -1034,8 +1029,8 @@ memif_disconnect_internal (memif_connection_t * c)
   if (c->tx_queues != NULL)
     {
       num =
-       (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-       num_s2m_rings;
+       (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+       run_args.num_s2m_rings;
       for (i = 0; i < num; i++)
        {
          mq = &c->tx_queues[i];
@@ -1055,8 +1050,8 @@ memif_disconnect_internal (memif_connection_t * c)
   if (c->rx_queues != NULL)
     {
       num =
-       (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-       num_m2s_rings;
+       (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+       run_args.num_m2s_rings;
       for (i = 0; i < num; i++)
        {
          mq = &c->rx_queues[i];
@@ -1208,8 +1203,8 @@ memif_connect1 (memif_connection_t * c)
     }
 
   num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   for (i = 0; i < num; i++)
     {
       mq = &c->tx_queues[i];
@@ -1226,8 +1221,8 @@ memif_connect1 (memif_connection_t * c)
        }
     }
   num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   for (i = 0; i < num; i++)
     {
       mq = &c->rx_queues[i];
@@ -1388,8 +1383,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
   if (c->fd < 0)
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->tx_queues[qid];
@@ -1399,17 +1394,19 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
   uint16_t head = ring->head;
   uint16_t tail = ring->tail;
+  uint16_t ring_size;
   uint16_t s0, s1, ns;
   *count_out = 0;
   int i, err = MEMIF_ERR_SUCCESS;      /* 0 */
 
-  ns = (1 << mq->log2_ring_size) - head + tail;
+  ring_size = (1 << mq->log2_ring_size);
+  ns = ring_size - head + tail;
 
   /* calculate number of chain buffers */
   if (size > ring->desc[0].buffer_length)
     {
-      chain_buf = size / ring->desc[s0].buffer_length;
-      if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
+      chain_buf = size / ring->desc[0].buffer_length;
+      if (((size % ring->desc[0].buffer_length) != 0) || (size == 0))
        chain_buf++;
     }
 
@@ -1499,8 +1496,8 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
   if (c->fd < 0)
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   libmemif_main_t *lm = &libmemif_main;
@@ -1522,14 +1519,14 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
          b0 = (bufs + *count_out);
          b1 = (bufs + *count_out + 1);
          chain_buf0 =
-           b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-         if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
-             0)
+           b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+         if ((b0->buffer_len %
+              ring->desc[b0->desc_index & mask].buffer_length) != 0)
            chain_buf0++;
          chain_buf1 =
-           b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
-         if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
-             0)
+           b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+         if ((b1->buffer_len %
+              ring->desc[b1->desc_index & mask].buffer_length) != 0)
            chain_buf1++;
          tail = b1->desc_index + chain_buf1;
          b0->data = NULL;
@@ -1540,8 +1537,10 @@ memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
          mq->alloc_bufs -= chain_buf0 + chain_buf1;
        }
       b0 = (bufs + *count_out);
-      chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-      if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
+      chain_buf0 =
+       b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+      if ((b0->buffer_len %
+          ring->desc[b0->desc_index & mask].buffer_length) != 0)
        chain_buf0++;
       tail = b0->desc_index + chain_buf0;
       b0->data = NULL;
@@ -1567,8 +1566,8 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
   if (c->fd < 0)
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->tx_queues[qid];
@@ -1588,15 +1587,15 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
          b0 = (bufs + curr_buf);
          b1 = (bufs + curr_buf + 1);
          chain_buf0 =
-           b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-         if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
-             0)
+           b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+         if ((b0->buffer_len %
+              ring->desc[b0->desc_index & mask].buffer_length) != 0)
            chain_buf0++;
 
          chain_buf1 =
-           b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
-         if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
-             0)
+           b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+         if ((b1->buffer_len %
+              ring->desc[b1->desc_index & mask].buffer_length) != 0)
            chain_buf1++;
 
          for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
@@ -1702,7 +1701,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
                }
            }
 
-         head = (b1->desc_index + chain_buf1) & mask;
+         head = b1->desc_index + chain_buf1;
 
          b0->data = NULL;
 #ifdef MEMIF_DBG
@@ -1721,8 +1720,10 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
        }
 
       b0 = (bufs + curr_buf);
-      chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-      if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
+      chain_buf0 =
+       b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+      if ((b0->buffer_len %
+          ring->desc[b0->desc_index & mask].buffer_length) != 0)
        chain_buf0++;
 
       for (i = 0; i < chain_buf0; i++)
@@ -1747,7 +1748,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
 #endif /* MEMIF_DBG_SHM */
        }
 
-      head = (b0->desc_index + chain_buf0) & mask;
+      head = b0->desc_index + chain_buf0;
 
       b0->data = NULL;
 #ifdef MEMIF_DBG
@@ -1788,8 +1789,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
   if (c->fd < 0)
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->rx_queues[qid];
@@ -1829,19 +1830,23 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
          i = 0;
          print_bytes (b0->data +
                       ring->desc[b0->desc_index & mask].buffer_length * i++,
-                      ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+                      ring->desc[b0->desc_index & mask].buffer_length,
+                      DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
          ns--;
          *rx += 1;
-         while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
+         while (ring->desc[mq->last_head & mask].
+                flags & MEMIF_DESC_FLAG_NEXT)
            {
              ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
              mq->last_head++;
              b0->data_len += ring->desc[mq->last_head & mask].length;
-             b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
+             b0->buffer_len +=
+               ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
              print_bytes (b0->data +
-                          ring->desc[b0->desc_index & mask].buffer_length * i++,
+                          ring->desc[b0->desc_index & mask].buffer_length *
+                          i++,
                           ring->desc[b0->desc_index & mask].buffer_length,
                           DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
@@ -1858,19 +1863,23 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
          i = 0;
          print_bytes (b1->data +
                       ring->desc[b1->desc_index & mask].buffer_length * i++,
-                      ring->desc[b1->desc_index & mask].buffer_length, DBG_TX_BUF);
+                      ring->desc[b1->desc_index & mask].buffer_length,
+                      DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
          ns--;
          *rx += 1;
-         while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
+         while (ring->desc[mq->last_head & mask].
+                flags & MEMIF_DESC_FLAG_NEXT)
            {
              ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
              mq->last_head++;
              b1->data_len += ring->desc[mq->last_head & mask].length;
-             b1->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
+             b1->buffer_len +=
+               ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
              print_bytes (b1->data +
-                          ring->desc[b1->desc_index & mask].buffer_length * i++,
+                          ring->desc[b1->desc_index & mask].buffer_length *
+                          i++,
                           ring->desc[b1->desc_index & mask].buffer_length,
                           DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
@@ -1889,10 +1898,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
       b0->data_len = ring->desc[mq->last_head & mask].length;
       b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
-         i = 0;
+      i = 0;
       print_bytes (b0->data +
                   ring->desc[b0->desc_index & mask].buffer_length * i++,
-                  ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+                  ring->desc[b0->desc_index & mask].buffer_length,
+                  DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
       ns--;
       *rx += 1;
@@ -1906,7 +1916,8 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
 #ifdef MEMIF_DBG_SHM
          print_bytes (b0->data +
                       ring->desc[b0->desc_index & mask].buffer_length * i++,
-                      ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+                      ring->desc[b0->desc_index & mask].buffer_length,
+                      DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
          ns--;
          *rx += 1;
@@ -2007,8 +2018,8 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
     err = MEMIF_ERR_NOBUF_DET;
 
   md->rx_queues_num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
 
   l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
   if (l0 + l1 <= buflen)
@@ -2023,12 +2034,15 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
     {
       md->rx_queues[i].qid = i;
       md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
+      md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
+      md->rx_queues[i].head = c->rx_queues[i].ring->head;
+      md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
       md->rx_queues[i].buffer_size = c->run_args.buffer_size;
     }
 
   md->tx_queues_num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
 
   l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
   if (l0 + l1 <= buflen)
@@ -2043,6 +2057,9 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
     {
       md->tx_queues[i].qid = i;
       md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
+      md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
+      md->tx_queues[i].head = c->tx_queues[i].ring->head;
+      md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
       md->tx_queues[i].buffer_size = c->run_args.buffer_size;
     }
 
@@ -2061,8 +2078,8 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd)
   if (c->fd < 0)
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
 
@@ -2075,9 +2092,6 @@ int
 memif_cleanup ()
 {
   libmemif_main_t *lm = &libmemif_main;
-  if (lm->app_name)
-    free (lm->app_name);
-  lm->app_name = NULL;
   if (lm->control_list)
     free (lm->control_list);
   lm->control_list = NULL;