#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>
}
vui->nregions = 0;
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ for (q = 0; q < vui->num_qid; q++)
{
vq = &vui->vrings[q];
vq->avail = 0;
vui->use_tx_spinlock = 0;
while (1)
{
- for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
+ for (qid = 0; qid < vui->num_qid / 2; qid++)
{
vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
if (!rxvq->started || !rxvq->enabled)
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)
{
int i, found[2] = { }; //RX + TX
- for (i = 0; i < VHOST_VRING_MAX_N; i++)
+ for (i = 0; i < vui->num_qid; i++)
if (vui->vrings[i].started && vui->vrings[i].enabled)
found[i & 1] = 1;
u32 qid = uf->private_data & 0xff;
n = read (uf->file_descriptor, ((char *) &buff), 8);
- vu_log_debug (vui, "if %d KICK queue %d", uf->private_data >> 8, qid);
+ vu_log_debug (vui, "if %d KICK queue %d", vui->hw_if_index, qid);
if (!vui->vrings[qid].started ||
(vhost_user_intf_ready (vui) != vui->is_ready))
{
vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
{
vhost_user_vring_t *vring = &vui->vrings[qid];
+
clib_memset (vring, 0, sizeof (*vring));
vring->kickfd_idx = ~0;
vring->callfd_idx = ~0;
vring->errfd = -1;
vring->qid = -1;
+ clib_spinlock_init (&vring->vring_lock);
+
/*
* We have a bug with some qemu 2.5, and this may be a fix.
* Feel like interpretation holy text, but this is from vhost-user.txt.
vring->errfd = -1;
}
+ clib_spinlock_free (&vring->vring_lock);
+
// save the qid so that we don't need to unassign and assign_rx_thread
// when the interface comes back up. They are expensive calls.
u16 q = vui->vrings[qid].qid;
vui->is_ready = 0;
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ for (q = 0; q < vui->num_qid; q++)
vhost_user_vring_close (vui, q);
unmap_all_mem_regions (vui);
* Re-compute desc, used, and avail descriptor table if vring address
* is set.
*/
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ for (q = 0; q < vui->num_qid; q++)
{
if (vui->vrings[q].desc_user_addr &&
vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
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 */
- goto close_socket;
+ ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
+ (msg.state.index >= vui->num_qid))
+ {
+ vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
+ " %d, msg.state.index %d, curruent max q %d",
+ msg.state.num, msg.state.index, vui->num_qid);
+ goto close_socket;
+ }
vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
break;
vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
vui->hw_if_index, msg.state.index);
- if (msg.state.index >= VHOST_VRING_MAX_N)
+ if (msg.state.index >= vui->num_qid)
{
vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
- " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
+ " %u >= %u", msg.state.index, vui->num_qid);
goto close_socket;
}
vui->hw_if_index, msg.u64);
q = (u8) (msg.u64 & 0xFF);
-
- /* if there is old fd, delete and close it */
- if (vui->vrings[q].callfd_idx != ~0)
+ if (vui->num_qid > q)
{
- clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
- vui->vrings[q].callfd_idx);
- clib_file_del (&file_main, uf);
- vui->vrings[q].callfd_idx = ~0;
+ /* if there is old fd, delete and close it */
+ if (vui->vrings[q].callfd_idx != ~0)
+ {
+ clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
+ vui->vrings[q].callfd_idx);
+ clib_file_del (&file_main, uf);
+ vui->vrings[q].callfd_idx = ~0;
+ }
+ }
+ else if (vec_len (vui->vrings) > q)
+ {
+ /* grow vrings by pair (RX + TX) */
+ vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
+ }
+ else
+ {
+ u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
+
+ /*
+ * Double the array size if it is less than 64 entries.
+ * Slow down thereafter.
+ */
+ if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
+ new_max_q = vec_len (vui->vrings) << 1;
+ else
+ new_max_q = vec_len (vui->vrings) +
+ (VHOST_VRING_INIT_MQ_PAIR_SZ << 2);
+ if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
+ new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
+
+ /* sync with the worker threads, vrings may move due to realloc */
+ vlib_worker_thread_barrier_sync (vm);
+ vec_validate_aligned (vui->vrings, new_max_q - 1,
+ CLIB_CACHE_LINE_BYTES);
+ vlib_worker_thread_barrier_release (vm);
+
+ for (i = old_max_q; i < vec_len (vui->vrings); i++)
+ vhost_user_vring_init (vui, i);
+
+ /* grow vrings by pair (RX + TX) */
+ vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
}
if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
vui->hw_if_index, msg.u64);
q = (u8) (msg.u64 & 0xFF);
+ if (q >= vui->num_qid)
+ {
+ vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
+ " %u >= %u", q, vui->num_qid);
+ goto close_socket;
+ }
if (vui->vrings[q].kickfd_idx != ~0)
{
vui->hw_if_index, msg.u64);
q = (u8) (msg.u64 & 0xFF);
+ if (q >= vui->num_qid)
+ {
+ vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
+ " %u >= %u", q, vui->num_qid);
+ goto close_socket;
+ }
if (vui->vrings[q].errfd != -1)
close (vui->vrings[q].errfd);
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 >= vui->num_qid)
+ {
+ vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
+ " %u >= %u", msg.state.index, vui->num_qid);
+ 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))
break;
case VHOST_USER_GET_VRING_BASE:
- if (msg.state.index >= VHOST_VRING_MAX_N)
+ if (msg.state.index >= vui->num_qid)
{
vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
- " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
+ " %u >= %u", msg.state.index, vui->num_qid);
goto close_socket;
}
case VHOST_USER_GET_QUEUE_NUM:
msg.flags |= 4;
- msg.u64 = VHOST_VRING_MAX_N;
+ msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
msg.size = sizeof (msg.u64);
vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
vui->hw_if_index, msg.u64);
vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
vui->hw_if_index, msg.state.num ? "enable" : "disable",
msg.state.index);
- if (msg.state.index >= VHOST_VRING_MAX_N)
+ if (msg.state.index >= vui->num_qid)
{
vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
- " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
+ " %u >= %u", msg.state.index, vui->num_qid);
goto close_socket;
}
template.file_descriptor = client_fd;
template.private_data = vui - vhost_user_main.vhost_user_interfaces;
vui->clib_file_index = clib_file_add (&file_main, &template);
+ vui->num_qid = 2;
return 0;
}
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)
+ for (qid = 0; qid < vui->num_qid / 2; qid += 2)
{
vhost_user_vring_t *rxvq = &vui->vrings[qid];
vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
if ((next_timeout < timeout) && (next_timeout > 0.0))
timeout = next_timeout;
}
- });
+ }
/* *INDENT-ON* */
break;
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)
/* 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)
template.private_data =
vui - vhost_user_main.vhost_user_interfaces;
vui->clib_file_index = clib_file_add (&file_main, &template);
+ vui->num_qid = 2;
/* This sockfd is considered consumed */
sockfd = -1;
}
}
}
- });
+ }
/* *INDENT-ON* */
}
return 0;
vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
vhost_user_update_iface_state (vui);
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ for (q = 0; q < vui->num_qid; q++)
{
// Remove existing queue mapping for the interface
if (q & 1)
}
}
- clib_mem_free ((void *) vui->vring_locks[q]);
+ clib_spinlock_free (&vui->vrings[q].vring_lock);
}
if (vui->unix_server_index != ~0)
vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
hwif->name, hwif->dev_instance);
- for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
+ for (qid = 1; qid < vui->num_qid / 2; qid += 2)
{
vhost_user_vring_t *txvq = &vui->vrings[qid];
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
// Delete ethernet interface
ethernet_delete_interface (vnm, vui->hw_if_index);
+ // free vrings
+ vec_free (vui->vrings);
+
// Back to pool
pool_put (vum->vhost_user_interfaces, vui);
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;
mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
&vui->if_index, 0);
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1,
+ CLIB_CACHE_LINE_BYTES);
+ vui->num_qid = 2;
+ for (q = 0; q < vec_len (vui->vrings); q++)
vhost_user_vring_init (vui, q);
hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
if (sw_if_index)
*sw_if_index = vui->sw_if_index;
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
- {
- vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
- CLIB_CACHE_LINE_BYTES);
- clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
- }
-
vec_validate (vui->per_cpu_tx_qid,
vlib_get_thread_main ()->n_vlib_mains - 1);
vhost_user_tx_thread_placement (vui);
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++)
{
}
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",
vlib_cli_output (vm, "Interface: %U (ifindex %d)",
format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
hw_if_indices[i]);
+ vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
if (vui->enable_gso)
vlib_cli_output (vm, " GSO enable");
if (vui->enable_packed)
vlib_cli_output (vm, " rx placement: ");
- for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
+ for (qid = 1; qid < vui->num_qid / 2; qid += 2)
{
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)
&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",
vui->regions[j].mmap_offset,
pointer_to_uword (vui->region_mmap_addr[j]));
}
- for (q = 0; q < VHOST_VRING_MAX_N; q++)
+ for (q = 0; q < vui->num_qid; q++)
{
if (!vui->vrings[q].started)
continue;
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);
}
}