misc: move to new pool_foreach macros
[vpp.git] / src / vnet / devices / virtio / vhost_user.c
index d24e516..45897b6 100644 (file)
@@ -33,8 +33,6 @@
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
 
-#include <vnet/ip/ip.h>
-
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/devices/devices.h>
 #include <vnet/feature/feature.h>
@@ -170,9 +168,9 @@ vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid)
   vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
                                    vhost_user_input_node.index);
   vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, q, ~0);
-  if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
+  if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
     /* Set polling as the default */
-    txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
+    txvq->mode = VNET_HW_IF_RX_MODE_POLLING;
   txvq->qid = q;
   rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
   if (rv)
@@ -453,21 +451,21 @@ vhost_user_socket_read (clib_file_t * uf)
     {
     case VHOST_USER_GET_FEATURES:
       msg.flags |= 4;
-      msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
-       (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
-       (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
-       (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
-       (1ULL << FEAT_VHOST_F_LOG_ALL) |
-       (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
-       (1ULL << FEAT_VIRTIO_NET_F_MQ) |
-       (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
-       (1ULL << FEAT_VIRTIO_F_VERSION_1);
+      msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
+       VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
+       VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
+       VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
+       VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
+       VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
+       VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
+       VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
+       VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
       msg.u64 &= vui->feature_mask;
 
       if (vui->enable_gso)
        msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
       if (vui->enable_packed)
-       msg.u64 |= (1ULL << FEAT_VIRTIO_F_RING_PACKED);
+       msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
 
       msg.size = sizeof (msg.u64);
       vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
@@ -488,14 +486,14 @@ vhost_user_socket_read (clib_file_t * uf)
       vui->features = msg.u64;
 
       if (vui->features &
-         ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
-          (1ULL << FEAT_VIRTIO_F_VERSION_1)))
+         (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
+          VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
        vui->virtio_net_hdr_sz = 12;
       else
        vui->virtio_net_hdr_sz = 10;
 
       vui->is_any_layout =
-       (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
+       (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
 
       ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
       vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
@@ -600,7 +598,8 @@ vhost_user_socket_read (clib_file_t * uf)
 
       if ((msg.state.num > 32768) ||   /* maximum ring size is 32768 */
          (msg.state.num == 0) ||       /* it cannot be zero */
-         ((msg.state.num - 1) & msg.state.num))        /* must be power of 2 */
+         ((msg.state.num - 1) & msg.state.num) ||      /* must be power of 2 */
+         (msg.state.index >= VHOST_VRING_MAX_N))
        goto close_socket;
       vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
       break;
@@ -649,7 +648,7 @@ vhost_user_socket_read (clib_file_t * uf)
 
       /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
          the ring is initialized in an enabled state. */
-      if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
+      if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
        vui->vrings[msg.state.index].enabled = 1;
 
       vui->vrings[msg.state.index].last_used_idx =
@@ -680,6 +679,8 @@ vhost_user_socket_read (clib_file_t * uf)
                    vui->hw_if_index, msg.u64);
 
       q = (u8) (msg.u64 & 0xFF);
+      if (q >= VHOST_VRING_MAX_N)
+       goto close_socket;
 
       /* if there is old fd, delete and close it */
       if (vui->vrings[q].callfd_idx != ~0)
@@ -713,6 +714,8 @@ vhost_user_socket_read (clib_file_t * uf)
                    vui->hw_if_index, msg.u64);
 
       q = (u8) (msg.u64 & 0xFF);
+      if (q >= VHOST_VRING_MAX_N)
+       goto close_socket;
 
       if (vui->vrings[q].kickfd_idx != ~0)
        {
@@ -752,6 +755,8 @@ vhost_user_socket_read (clib_file_t * uf)
                    vui->hw_if_index, msg.u64);
 
       q = (u8) (msg.u64 & 0xFF);
+      if (q >= VHOST_VRING_MAX_N)
+       goto close_socket;
 
       if (vui->vrings[q].errfd != -1)
        close (vui->vrings[q].errfd);
@@ -771,6 +776,8 @@ vhost_user_socket_read (clib_file_t * uf)
       vu_log_debug (vui,
                    "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
                    vui->hw_if_index, msg.state.index, msg.state.num);
+      if (msg.state.index >= VHOST_VRING_MAX_N)
+       goto close_socket;
       vlib_worker_thread_barrier_sync (vm);
       vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
       if (vhost_user_is_packed_ring_supported (vui))
@@ -807,7 +814,7 @@ vhost_user_socket_read (clib_file_t * uf)
 
          if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
            vui->vrings[msg.state.index].avail_wrap_counter =
-             VIRTQ_DESC_F_AVAIL;
+             VRING_DESC_F_AVAIL;
        }
       vlib_worker_thread_barrier_release (vm);
       break;
@@ -1119,7 +1126,7 @@ vhost_user_send_interrupt_process (vlib_main_t * vm,
 
        case ~0:
          /* *INDENT-OFF* */
-         pool_foreach (vui, vum->vhost_user_interfaces, {
+         pool_foreach (vui, vum->vhost_user_interfaces) {
              next_timeout = timeout;
              for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
                {
@@ -1147,7 +1154,7 @@ vhost_user_send_interrupt_process (vlib_main_t * vm,
                  if ((next_timeout < timeout) && (next_timeout > 0.0))
                    timeout = next_timeout;
                }
-         });
+         }
           /* *INDENT-ON* */
          break;
 
@@ -1198,7 +1205,7 @@ vhost_user_process (vlib_main_t * vm,
       timeout = 3.0;
 
       /* *INDENT-OFF* */
-      pool_foreach (vui, vum->vhost_user_interfaces, {
+      pool_foreach (vui, vum->vhost_user_interfaces) {
 
          if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
              if (vui->clib_file_index == ~0)
@@ -1223,6 +1230,7 @@ vhost_user_process (vlib_main_t * vm,
                  /* try to connect */
                  strncpy (sun.sun_path, (char *) vui->sock_filename,
                           sizeof (sun.sun_path) - 1);
+                 sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
 
                  /* Avoid hanging VPP if the other end does not accept */
                  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
@@ -1265,7 +1273,7 @@ vhost_user_process (vlib_main_t * vm,
                    }
                }
          }
-      });
+      }
       /* *INDENT-ON* */
     }
   return 0;
@@ -1358,8 +1366,8 @@ vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
       if (txvq->qid == -1)
        continue;
       if ((vum->ifq_count > 0) &&
-         ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
-          (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
+         ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
+          (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
        {
          vum->ifq_count--;
          // Stop the timer if there is no more interrupt interface/queue
@@ -1400,9 +1408,9 @@ vhost_user_exit (vlib_main_t * vm)
 
   vlib_worker_thread_barrier_sync (vlib_get_main ());
   /* *INDENT-OFF* */
-  pool_foreach (vui, vum->vhost_user_interfaces, {
+  pool_foreach (vui, vum->vhost_user_interfaces) {
       vhost_user_delete_if (vnm, vm, vui->sw_if_index);
-  });
+  }
   /* *INDENT-ON* */
   vlib_worker_thread_barrier_release (vlib_get_main ());
   return 0;
@@ -1710,7 +1718,7 @@ vhost_user_connect_command_fn (vlib_main_t * vm,
   /* GSO feature is disable by default */
   feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS;
   /* packed-ring feature is disable by default */
-  feature_mask &= ~(1ULL << FEAT_VIRTIO_F_RING_PACKED);
+  feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (line_input, "socket %s", &sock_filename))
@@ -1819,15 +1827,13 @@ vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
   vhost_user_intf_details_t *vuid = NULL;
   u32 *hw_if_indices = 0;
   vnet_hw_interface_t *hi;
-  u8 *s = NULL;
   int i;
 
   if (!out_vuids)
     return -1;
 
-  pool_foreach (vui, vum->vhost_user_interfaces,
-               vec_add1 (hw_if_indices, vui->hw_if_index);
-    );
+  pool_foreach (vui, vum->vhost_user_interfaces)
+    vec_add1 (hw_if_indices, vui->hw_if_index);
 
   for (i = 0; i < vec_len (hw_if_indices); i++)
     {
@@ -1841,17 +1847,13 @@ vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
       vuid->num_regions = vui->nregions;
       vuid->is_server = vui->unix_server_index != ~0;
       vuid->sock_errno = vui->sock_errno;
-      strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
-              sizeof (vuid->sock_filename));
-      vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
-      s = format (s, "%v%c", hi->name, 0);
-
-      strncpy ((char *) vuid->if_name, (char *) s,
-              ARRAY_LEN (vuid->if_name) - 1);
-      _vec_len (s) = 0;
+      snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
+               "%s", vui->sock_filename);
+      memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
+               clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
+      vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
     }
 
-  vec_free (s);
   vec_free (hw_if_indices);
 
   *out_vuids = r_vuids;
@@ -1929,7 +1931,7 @@ vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
          vlib_cli_output (vm, "%U", format_vhost_user_desc,
                           "  %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
                           desc_table, j, &mem_hint);
-         if (show_verbose && (desc_table[j].flags & VIRTQ_DESC_F_INDIRECT))
+         if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
            {
              n_entries = desc_table[j].len / sizeof (vring_desc_t);
              desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
@@ -2017,7 +2019,7 @@ vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q,
          vlib_cli_output (vm, "%U", format_vhost_user_packed_desc,
                           "  %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
                           desc_table, j, &mem_hint);
-         if (show_verbose && (desc_table[j].flags & VIRTQ_DESC_F_INDIRECT))
+         if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
            {
              n_entries = desc_table[j].len >> 4;
              desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
@@ -2064,7 +2066,7 @@ show_vhost_user_command_fn (vlib_main_t * vm,
 
   static struct feat_struct feat_array[] = {
 #define _(s,b) { .str = #s, .bit = b, },
-    foreach_virtio_net_feature
+    foreach_virtio_net_features
 #undef _
     {.str = NULL}
   };
@@ -2107,9 +2109,8 @@ show_vhost_user_command_fn (vlib_main_t * vm,
     }
   if (vec_len (hw_if_indices) == 0)
     {
-      pool_foreach (vui, vum->vhost_user_interfaces,
-                   vec_add1 (hw_if_indices, vui->hw_if_index);
-       );
+      pool_foreach (vui, vum->vhost_user_interfaces)
+       vec_add1 (hw_if_indices, vui->hw_if_index);
     }
   vlib_cli_output (vm, "Virtio vhost-user interfaces");
   vlib_cli_output (vm, "Global:\n  coalesce frames %d time %e",
@@ -2169,7 +2170,7 @@ show_vhost_user_command_fn (vlib_main_t * vm,
        {
          vnet_main_t *vnm = vnet_get_main ();
          uword thread_index;
-         vnet_hw_interface_rx_mode mode;
+         vnet_hw_if_rx_mode mode;
          vhost_user_vring_t *txvq = &vui->vrings[qid];
 
          if (txvq->qid == -1)
@@ -2181,7 +2182,7 @@ show_vhost_user_command_fn (vlib_main_t * vm,
                                         &mode);
          vlib_cli_output (vm, "   thread %d on vring %d, %U\n",
                           thread_index, qid,
-                          format_vnet_hw_interface_rx_mode, mode);
+                          format_vnet_hw_if_rx_mode, mode);
        }
 
       vlib_cli_output (vm, " tx placement: %s\n",
@@ -2506,9 +2507,8 @@ vhost_user_unmap_all (void)
 
   if (vum->dont_dump_vhost_user_memory)
     {
-      pool_foreach (vui, vum->vhost_user_interfaces,
-                   unmap_all_mem_regions (vui);
-       );
+      pool_foreach (vui, vum->vhost_user_interfaces)
+       unmap_all_mem_regions (vui);
     }
 }