2017-03-02 14:12:20 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) Intel Corporation. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
2017-03-02 14:12:20 +00:00
|
|
|
#include "spdk/env.h"
|
2017-05-24 12:52:07 +00:00
|
|
|
#include "spdk/likely.h"
|
2017-08-11 23:27:04 +00:00
|
|
|
#include "spdk/string.h"
|
2017-05-26 12:00:56 +00:00
|
|
|
#include "spdk/util.h"
|
2017-08-09 17:05:06 +00:00
|
|
|
#include "spdk/barrier.h"
|
2017-03-02 14:12:20 +00:00
|
|
|
#include "spdk/vhost.h"
|
2017-05-22 12:53:27 +00:00
|
|
|
#include "vhost_internal.h"
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2019-01-07 10:38:39 +00:00
|
|
|
#include "spdk_internal/memory.h"
|
|
|
|
|
2017-12-24 23:43:41 +00:00
|
|
|
static uint32_t *g_num_ctrlrs;
|
2017-03-02 14:12:20 +00:00
|
|
|
|
|
|
|
/* Path to folder where character device will be created. Can be set by user. */
|
|
|
|
static char dev_dirname[PATH_MAX] = "";
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session_fn_ctx {
|
|
|
|
/** Device pointer obtained before enqueuing the event */
|
2017-07-26 10:00:26 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
/** ID of the session to send event to. */
|
|
|
|
uint32_t vsession_id;
|
|
|
|
|
2017-08-03 09:24:00 +00:00
|
|
|
/** User callback function to be executed on given lcore. */
|
2019-01-07 23:30:57 +00:00
|
|
|
spdk_vhost_session_fn cb_fn;
|
2017-08-03 09:24:00 +00:00
|
|
|
|
|
|
|
/** Semaphore used to signal that event is done. */
|
|
|
|
sem_t sem;
|
2017-09-07 18:34:15 +00:00
|
|
|
|
|
|
|
/** Response to be written by enqueued event. */
|
|
|
|
int response;
|
2017-08-03 09:24:00 +00:00
|
|
|
};
|
|
|
|
|
2017-08-01 16:08:32 +00:00
|
|
|
static int new_connection(int vid);
|
2017-10-03 07:00:33 +00:00
|
|
|
static int start_device(int vid);
|
|
|
|
static void stop_device(int vid);
|
2017-08-01 16:08:32 +00:00
|
|
|
static void destroy_connection(int vid);
|
2017-11-08 19:53:12 +00:00
|
|
|
static int get_config(int vid, uint8_t *config, uint32_t len);
|
|
|
|
static int set_config(int vid, uint8_t *config, uint32_t offset,
|
|
|
|
uint32_t size, uint32_t flags);
|
2017-08-01 13:46:51 +00:00
|
|
|
|
|
|
|
const struct vhost_device_ops g_spdk_vhost_ops = {
|
2017-10-03 07:00:33 +00:00
|
|
|
.new_device = start_device,
|
|
|
|
.destroy_device = stop_device,
|
2017-11-08 19:53:12 +00:00
|
|
|
.get_config = get_config,
|
|
|
|
.set_config = set_config,
|
2017-08-01 16:08:32 +00:00
|
|
|
.new_connection = new_connection,
|
|
|
|
.destroy_connection = destroy_connection,
|
2017-11-22 06:01:48 +00:00
|
|
|
.vhost_nvme_admin_passthrough = spdk_vhost_nvme_admin_passthrough,
|
|
|
|
.vhost_nvme_set_cq_call = spdk_vhost_nvme_set_cq_call,
|
|
|
|
.vhost_nvme_get_cap = spdk_vhost_nvme_get_cap,
|
2018-06-21 08:30:29 +00:00
|
|
|
.vhost_nvme_set_bar_mr = spdk_vhost_nvme_set_bar_mr,
|
2017-08-01 13:46:51 +00:00
|
|
|
};
|
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
static TAILQ_HEAD(, spdk_vhost_dev) g_spdk_vhost_devices = TAILQ_HEAD_INITIALIZER(
|
|
|
|
g_spdk_vhost_devices);
|
2017-07-25 18:40:17 +00:00
|
|
|
static pthread_mutex_t g_spdk_vhost_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2017-05-18 13:57:09 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
void *spdk_vhost_gpa_to_vva(struct spdk_vhost_session *vsession, uint64_t addr, uint64_t len)
|
2017-05-25 15:52:16 +00:00
|
|
|
{
|
2018-04-17 20:30:55 +00:00
|
|
|
void *vva;
|
|
|
|
uint64_t newlen;
|
|
|
|
|
|
|
|
newlen = len;
|
2018-12-13 10:51:34 +00:00
|
|
|
vva = (void *)rte_vhost_va_from_guest_pa(vsession->mem, addr, &newlen);
|
2018-04-17 20:30:55 +00:00
|
|
|
if (newlen != len) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vva;
|
|
|
|
|
2017-05-25 15:52:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 13:24:38 +00:00
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_req_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *virtqueue,
|
2018-01-03 13:24:38 +00:00
|
|
|
uint16_t req_id)
|
|
|
|
{
|
|
|
|
struct vring_desc *desc, *desc_table;
|
|
|
|
uint32_t desc_table_size;
|
|
|
|
int rc;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (spdk_likely(!spdk_vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) {
|
2018-01-03 13:24:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
rc = spdk_vhost_vq_get_desc(vsession, virtqueue, req_id, &desc, &desc_table, &desc_table_size);
|
2018-01-03 13:24:38 +00:00
|
|
|
if (spdk_unlikely(rc != 0)) {
|
|
|
|
SPDK_ERRLOG("Can't log used ring descriptors!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (spdk_vhost_vring_desc_is_wr(desc)) {
|
|
|
|
/* To be honest, only pages realy touched should be logged, but
|
|
|
|
* doing so would require tracking those changes in each backed.
|
|
|
|
* Also backend most likely will touch all/most of those pages so
|
|
|
|
* for lets assume we touched all pages passed to as writeable buffers. */
|
2018-12-13 12:07:11 +00:00
|
|
|
rte_vhost_log_write(vsession->vid, desc->addr, desc->len);
|
2018-01-03 13:24:38 +00:00
|
|
|
}
|
|
|
|
spdk_vhost_vring_desc_get_next(&desc, desc_table, desc_table_size);
|
|
|
|
} while (desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_used_vring_elem(struct spdk_vhost_session *vsession,
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue,
|
2018-01-03 13:24:38 +00:00
|
|
|
uint16_t idx)
|
|
|
|
{
|
|
|
|
uint64_t offset, len;
|
|
|
|
uint16_t vq_idx;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (spdk_likely(!spdk_vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) {
|
2018-01-03 13:24:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = offsetof(struct vring_used, ring[idx]);
|
|
|
|
len = sizeof(virtqueue->vring.used->ring[idx]);
|
2018-12-13 10:51:34 +00:00
|
|
|
vq_idx = virtqueue - vsession->virtqueue;
|
2018-01-03 13:24:38 +00:00
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
rte_vhost_log_used_vring(vsession->vid, vq_idx, offset, len);
|
2018-01-03 13:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_used_vring_idx(struct spdk_vhost_session *vsession,
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue)
|
2018-01-03 13:24:38 +00:00
|
|
|
{
|
|
|
|
uint64_t offset, len;
|
|
|
|
uint16_t vq_idx;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (spdk_likely(!spdk_vhost_dev_has_feature(vsession, VHOST_F_LOG_ALL))) {
|
2018-01-03 13:24:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = offsetof(struct vring_used, idx);
|
|
|
|
len = sizeof(virtqueue->vring.used->idx);
|
2018-12-13 10:51:34 +00:00
|
|
|
vq_idx = virtqueue - vsession->virtqueue;
|
2018-01-03 13:24:38 +00:00
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
rte_vhost_log_used_vring(vsession->vid, vq_idx, offset, len);
|
2018-01-03 13:24:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:52:07 +00:00
|
|
|
/*
|
|
|
|
* Get available requests from avail ring.
|
|
|
|
*/
|
|
|
|
uint16_t
|
2017-08-31 14:38:35 +00:00
|
|
|
spdk_vhost_vq_avail_ring_get(struct spdk_vhost_virtqueue *virtqueue, uint16_t *reqs,
|
|
|
|
uint16_t reqs_len)
|
2017-05-24 12:52:07 +00:00
|
|
|
{
|
2017-08-31 14:38:35 +00:00
|
|
|
struct rte_vhost_vring *vring = &virtqueue->vring;
|
|
|
|
struct vring_avail *avail = vring->avail;
|
|
|
|
uint16_t size_mask = vring->size - 1;
|
2019-02-24 23:46:37 +00:00
|
|
|
uint16_t last_idx = virtqueue->last_avail_idx, avail_idx = avail->idx;
|
2018-01-28 09:53:03 +00:00
|
|
|
uint16_t count, i;
|
2017-05-24 12:52:07 +00:00
|
|
|
|
2018-01-28 09:53:03 +00:00
|
|
|
count = avail_idx - last_idx;
|
2017-05-24 12:52:07 +00:00
|
|
|
if (spdk_likely(count == 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:53:03 +00:00
|
|
|
if (spdk_unlikely(count > vring->size)) {
|
|
|
|
/* TODO: the queue is unrecoverably broken and should be marked so.
|
|
|
|
* For now we will fail silently and report there are no new avail entries.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = spdk_min(count, reqs_len);
|
2019-02-24 23:46:37 +00:00
|
|
|
virtqueue->last_avail_idx += count;
|
2017-05-24 12:52:07 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2017-08-31 14:38:35 +00:00
|
|
|
reqs[i] = vring->avail->ring[(last_idx + i) & size_mask];
|
2017-05-24 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_RING,
|
2017-05-24 12:52:07 +00:00
|
|
|
"AVAIL: last_idx=%"PRIu16" avail_idx=%"PRIu16" count=%"PRIu16"\n",
|
|
|
|
last_idx, avail_idx, count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:55:00 +00:00
|
|
|
static bool
|
|
|
|
spdk_vhost_vring_desc_is_indirect(struct vring_desc *cur_desc)
|
|
|
|
{
|
|
|
|
return !!(cur_desc->flags & VRING_DESC_F_INDIRECT);
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:28:00 +00:00
|
|
|
int
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_get_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *virtqueue,
|
2017-08-31 14:38:35 +00:00
|
|
|
uint16_t req_idx, struct vring_desc **desc, struct vring_desc **desc_table,
|
|
|
|
uint32_t *desc_table_size)
|
2017-05-24 12:52:07 +00:00
|
|
|
{
|
2017-08-31 14:38:35 +00:00
|
|
|
if (spdk_unlikely(req_idx >= virtqueue->vring.size)) {
|
2017-10-09 15:28:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:38:35 +00:00
|
|
|
*desc = &virtqueue->vring.desc[req_idx];
|
2017-10-09 16:55:00 +00:00
|
|
|
|
|
|
|
if (spdk_vhost_vring_desc_is_indirect(*desc)) {
|
2018-12-13 10:51:34 +00:00
|
|
|
assert(spdk_vhost_dev_has_feature(vsession, VIRTIO_RING_F_INDIRECT_DESC));
|
2017-10-09 16:55:00 +00:00
|
|
|
*desc_table_size = (*desc)->len / sizeof(**desc);
|
2018-12-13 10:51:34 +00:00
|
|
|
*desc_table = spdk_vhost_gpa_to_vva(vsession, (*desc)->addr,
|
2018-04-17 20:30:55 +00:00
|
|
|
sizeof(**desc) * *desc_table_size);
|
2017-10-09 16:55:00 +00:00
|
|
|
*desc = *desc_table;
|
2017-10-13 13:30:05 +00:00
|
|
|
if (*desc == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:55:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:38:35 +00:00
|
|
|
*desc_table = virtqueue->vring.desc;
|
|
|
|
*desc_table_size = virtqueue->vring.size;
|
2017-10-09 15:28:00 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-05-24 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 17:45:56 +00:00
|
|
|
int
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_signal(struct spdk_vhost_session *vsession,
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue)
|
2017-09-11 17:45:56 +00:00
|
|
|
{
|
|
|
|
if (virtqueue->used_req_cnt == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtqueue->req_cnt += virtqueue->used_req_cnt;
|
|
|
|
virtqueue->used_req_cnt = 0;
|
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_RING,
|
2017-09-11 17:45:56 +00:00
|
|
|
"Queue %td - USED RING: sending IRQ: last used %"PRIu16"\n",
|
2019-02-24 23:46:37 +00:00
|
|
|
virtqueue - vsession->virtqueue, virtqueue->last_used_idx);
|
2017-09-11 17:45:56 +00:00
|
|
|
|
|
|
|
eventfd_write(virtqueue->vring.callfd, (eventfd_t)1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
check_session_io_stats(struct spdk_vhost_session *vsession, uint64_t now)
|
2017-09-11 17:45:56 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue;
|
2018-12-17 02:45:35 +00:00
|
|
|
uint32_t irq_delay_base = vsession->coalescing_delay_time_base;
|
|
|
|
uint32_t io_threshold = vsession->coalescing_io_rate_threshold;
|
2018-08-09 10:15:11 +00:00
|
|
|
int32_t irq_delay;
|
|
|
|
uint32_t req_cnt;
|
2017-09-11 17:45:56 +00:00
|
|
|
uint16_t q_idx;
|
|
|
|
|
2018-12-17 02:45:35 +00:00
|
|
|
if (now < vsession->next_stats_check_time) {
|
2017-09-11 17:45:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:45:35 +00:00
|
|
|
vsession->next_stats_check_time = now + vsession->stats_check_interval;
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
|
|
|
virtqueue = &vsession->virtqueue[q_idx];
|
2017-09-11 17:45:56 +00:00
|
|
|
|
|
|
|
req_cnt = virtqueue->req_cnt + virtqueue->used_req_cnt;
|
|
|
|
if (req_cnt <= io_threshold) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
irq_delay = (irq_delay_base * (req_cnt - io_threshold)) / io_threshold;
|
2018-08-09 10:15:11 +00:00
|
|
|
virtqueue->irq_delay_time = (uint32_t) spdk_max(0, irq_delay);
|
2017-09-11 17:45:56 +00:00
|
|
|
|
|
|
|
virtqueue->req_cnt = 0;
|
|
|
|
virtqueue->next_event_time = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_used_signal(struct spdk_vhost_session *vsession)
|
2017-09-11 17:45:56 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue;
|
|
|
|
uint64_t now;
|
|
|
|
uint16_t q_idx;
|
|
|
|
|
2018-12-17 02:45:35 +00:00
|
|
|
if (vsession->coalescing_delay_time_base == 0) {
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
|
|
|
virtqueue = &vsession->virtqueue[q_idx];
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2018-04-23 11:04:18 +00:00
|
|
|
if (virtqueue->vring.desc == NULL ||
|
|
|
|
(virtqueue->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
|
2017-09-11 17:45:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_signal(vsession, virtqueue);
|
2017-09-11 17:45:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
now = spdk_get_ticks();
|
2018-12-13 10:51:34 +00:00
|
|
|
check_session_io_stats(vsession, now);
|
2017-09-11 17:45:56 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (q_idx = 0; q_idx < vsession->max_queues; q_idx++) {
|
|
|
|
virtqueue = &vsession->virtqueue[q_idx];
|
2017-09-11 17:45:56 +00:00
|
|
|
|
|
|
|
/* No need for event right now */
|
|
|
|
if (now < virtqueue->next_event_time ||
|
|
|
|
(virtqueue->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (!spdk_vhost_vq_used_signal(vsession, virtqueue)) {
|
2017-09-11 17:45:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Syscall is quite long so update time */
|
|
|
|
now = spdk_get_ticks();
|
|
|
|
virtqueue->next_event_time = now + virtqueue->irq_delay_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:45:35 +00:00
|
|
|
static int
|
|
|
|
spdk_vhost_session_set_coalescing(struct spdk_vhost_dev *vdev,
|
|
|
|
struct spdk_vhost_session *vsession, void *ctx)
|
|
|
|
{
|
|
|
|
if (vdev == NULL || vsession == NULL) {
|
|
|
|
/* nothing to do */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-04 09:26:19 +00:00
|
|
|
vsession->coalescing_delay_time_base =
|
|
|
|
vdev->coalescing_delay_us * spdk_get_ticks_hz() / 1000000ULL;
|
|
|
|
vsession->coalescing_io_rate_threshold =
|
|
|
|
vdev->coalescing_iops_threshold * SPDK_VHOST_STATS_CHECK_INTERVAL_MS / 1000U;
|
2018-12-17 02:45:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:45:56 +00:00
|
|
|
int
|
|
|
|
spdk_vhost_set_coalescing(struct spdk_vhost_dev *vdev, uint32_t delay_base_us,
|
|
|
|
uint32_t iops_threshold)
|
|
|
|
{
|
|
|
|
uint64_t delay_time_base = delay_base_us * spdk_get_ticks_hz() / 1000000ULL;
|
2018-12-17 02:45:35 +00:00
|
|
|
uint32_t io_rate = iops_threshold * SPDK_VHOST_STATS_CHECK_INTERVAL_MS / 1000U;
|
2017-09-11 17:45:56 +00:00
|
|
|
|
|
|
|
if (delay_time_base >= UINT32_MAX) {
|
|
|
|
SPDK_ERRLOG("Delay time of %"PRIu32" is to big\n", delay_base_us);
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (io_rate == 0) {
|
|
|
|
SPDK_ERRLOG("IOPS rate of %"PRIu32" is too low. Min is %u\n", io_rate,
|
2018-12-17 02:45:35 +00:00
|
|
|
1000U / SPDK_VHOST_STATS_CHECK_INTERVAL_MS);
|
2017-09-11 17:45:56 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:10:58 +00:00
|
|
|
vdev->coalescing_delay_us = delay_base_us;
|
|
|
|
vdev->coalescing_iops_threshold = iops_threshold;
|
2018-12-17 02:45:35 +00:00
|
|
|
|
|
|
|
spdk_vhost_dev_foreach_session(vdev, spdk_vhost_session_set_coalescing, NULL);
|
2017-09-11 17:45:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:10:58 +00:00
|
|
|
void
|
|
|
|
spdk_vhost_get_coalescing(struct spdk_vhost_dev *vdev, uint32_t *delay_base_us,
|
|
|
|
uint32_t *iops_threshold)
|
|
|
|
{
|
|
|
|
if (delay_base_us) {
|
|
|
|
*delay_base_us = vdev->coalescing_delay_us;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iops_threshold) {
|
|
|
|
*iops_threshold = vdev->coalescing_iops_threshold;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 12:52:07 +00:00
|
|
|
/*
|
|
|
|
* Enqueue id and len to used ring.
|
|
|
|
*/
|
|
|
|
void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vq_used_ring_enqueue(struct spdk_vhost_session *vsession,
|
|
|
|
struct spdk_vhost_virtqueue *virtqueue,
|
2017-08-31 14:38:35 +00:00
|
|
|
uint16_t id, uint32_t len)
|
2017-05-24 12:52:07 +00:00
|
|
|
{
|
2017-08-31 14:38:35 +00:00
|
|
|
struct rte_vhost_vring *vring = &virtqueue->vring;
|
|
|
|
struct vring_used *used = vring->used;
|
2019-02-24 23:46:37 +00:00
|
|
|
uint16_t last_idx = virtqueue->last_used_idx & (vring->size - 1);
|
2017-05-24 12:52:07 +00:00
|
|
|
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_DEBUGLOG(SPDK_LOG_VHOST_RING,
|
2017-09-11 17:45:56 +00:00
|
|
|
"Queue %td - USED RING: last_idx=%"PRIu16" req id=%"PRIu16" len=%"PRIu32"\n",
|
2019-02-24 23:46:37 +00:00
|
|
|
virtqueue - vsession->virtqueue, virtqueue->last_used_idx, id, len);
|
2017-05-24 12:52:07 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_req_desc(vsession, virtqueue, id);
|
2018-01-03 13:24:38 +00:00
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
virtqueue->last_used_idx++;
|
2017-05-24 12:52:07 +00:00
|
|
|
used->ring[last_idx].id = id;
|
|
|
|
used->ring[last_idx].len = len;
|
|
|
|
|
2018-02-04 13:44:59 +00:00
|
|
|
/* Ensure the used ring is updated before we log it or increment used->idx. */
|
2018-01-29 16:32:00 +00:00
|
|
|
spdk_smp_wmb();
|
2018-02-04 13:44:59 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_used_vring_elem(vsession, virtqueue, last_idx);
|
2019-02-24 23:46:37 +00:00
|
|
|
* (volatile uint16_t *) &used->idx = virtqueue->last_used_idx;
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_log_used_vring_idx(vsession, virtqueue);
|
2017-07-19 11:46:20 +00:00
|
|
|
|
2018-01-29 16:32:00 +00:00
|
|
|
/* Ensure all our used ring changes are visible to the guest at the time
|
|
|
|
* of interrupt.
|
|
|
|
* TODO: this is currently an sfence on x86. For other architectures we
|
|
|
|
* will most likely need an smp_mb(), but smp_mb() is an overkill for x86.
|
|
|
|
*/
|
|
|
|
spdk_wmb();
|
|
|
|
|
2017-09-11 17:45:56 +00:00
|
|
|
virtqueue->used_req_cnt++;
|
2017-05-24 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 15:28:00 +00:00
|
|
|
int
|
|
|
|
spdk_vhost_vring_desc_get_next(struct vring_desc **desc,
|
|
|
|
struct vring_desc *desc_table, uint32_t desc_table_size)
|
2017-05-24 12:52:07 +00:00
|
|
|
{
|
2017-10-09 15:28:00 +00:00
|
|
|
struct vring_desc *old_desc = *desc;
|
|
|
|
uint16_t next_idx;
|
2017-05-24 12:52:07 +00:00
|
|
|
|
2017-10-09 15:28:00 +00:00
|
|
|
if ((old_desc->flags & VRING_DESC_F_NEXT) == 0) {
|
|
|
|
*desc = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_idx = old_desc->next;
|
|
|
|
if (spdk_unlikely(next_idx >= desc_table_size)) {
|
|
|
|
*desc = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*desc = &desc_table[next_idx];
|
|
|
|
return 0;
|
2017-05-24 12:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
spdk_vhost_vring_desc_is_wr(struct vring_desc *cur_desc)
|
|
|
|
{
|
|
|
|
return !!(cur_desc->flags & VRING_DESC_F_WRITE);
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:36:53 +00:00
|
|
|
int
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_vring_desc_to_iov(struct spdk_vhost_session *vsession, struct iovec *iov,
|
2017-07-28 15:36:53 +00:00
|
|
|
uint16_t *iov_index, const struct vring_desc *desc)
|
2017-06-02 17:48:11 +00:00
|
|
|
{
|
2017-07-28 15:36:53 +00:00
|
|
|
uint32_t remaining = desc->len;
|
2018-01-23 19:54:31 +00:00
|
|
|
uint32_t to_boundary;
|
2017-07-28 15:36:53 +00:00
|
|
|
uint32_t len;
|
|
|
|
uintptr_t payload = desc->addr;
|
|
|
|
uintptr_t vva;
|
|
|
|
|
2018-12-05 10:46:08 +00:00
|
|
|
do {
|
2017-07-28 15:36:53 +00:00
|
|
|
if (*iov_index >= SPDK_VHOST_IOVS_MAX) {
|
|
|
|
SPDK_ERRLOG("SPDK_VHOST_IOVS_MAX(%d) reached\n", SPDK_VHOST_IOVS_MAX);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-13 10:51:34 +00:00
|
|
|
vva = (uintptr_t)rte_vhost_gpa_to_vva(vsession->mem, payload);
|
2017-07-28 15:36:53 +00:00
|
|
|
if (vva == 0) {
|
|
|
|
SPDK_ERRLOG("gpa_to_vva(%p) == NULL\n", (void *)payload);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-01-07 12:05:17 +00:00
|
|
|
to_boundary = VALUE_2MB - _2MB_OFFSET(payload);
|
2018-01-23 19:54:31 +00:00
|
|
|
if (spdk_likely(remaining <= to_boundary)) {
|
|
|
|
len = remaining;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Descriptor crosses a 2MB hugepage boundary. vhost memory regions are allocated
|
|
|
|
* from hugepage memory, so this means this descriptor may be described by
|
|
|
|
* discontiguous vhost memory regions. Do not blindly split on the 2MB boundary,
|
|
|
|
* only split it if the two sides of the boundary do not map to the same vhost
|
|
|
|
* memory region. This helps ensure we do not exceed the max number of IOVs
|
|
|
|
* defined by SPDK_VHOST_IOVS_MAX.
|
|
|
|
*/
|
|
|
|
len = to_boundary;
|
|
|
|
while (len < remaining) {
|
2018-12-13 10:51:34 +00:00
|
|
|
if (vva + len != (uintptr_t)rte_vhost_gpa_to_vva(vsession->mem, payload + len)) {
|
2018-01-23 19:54:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-07 12:05:17 +00:00
|
|
|
len += spdk_min(remaining - len, VALUE_2MB);
|
2018-01-23 19:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 15:36:53 +00:00
|
|
|
iov[*iov_index].iov_base = (void *)vva;
|
|
|
|
iov[*iov_index].iov_len = len;
|
|
|
|
remaining -= len;
|
|
|
|
payload += len;
|
|
|
|
(*iov_index)++;
|
2018-12-05 10:46:08 +00:00
|
|
|
} while (remaining);
|
2017-07-28 15:36:53 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-06-02 17:48:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
static struct spdk_vhost_session *
|
|
|
|
spdk_vhost_session_find_by_id(struct spdk_vhost_dev *vdev, unsigned id)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_session *vsession;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(vsession, &vdev->vsessions, tailq) {
|
|
|
|
if (vsession->id == id) {
|
|
|
|
return vsession;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
static struct spdk_vhost_session *
|
|
|
|
spdk_vhost_session_find_by_vid(int vid)
|
2017-05-18 13:57:09 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_dev *vdev;
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-05-18 13:57:09 +00:00
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
TAILQ_FOREACH(vdev, &g_spdk_vhost_devices, tailq) {
|
2018-12-17 15:33:30 +00:00
|
|
|
TAILQ_FOREACH(vsession, &vdev->vsessions, tailq) {
|
|
|
|
if (vsession->vid == vid) {
|
|
|
|
return vsession;
|
|
|
|
}
|
2017-05-18 13:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:20:01 +00:00
|
|
|
#define SHIFT_2MB 21
|
|
|
|
#define SIZE_2MB (1ULL << SHIFT_2MB)
|
|
|
|
#define FLOOR_2MB(x) (((uintptr_t)x) / SIZE_2MB) << SHIFT_2MB
|
|
|
|
#define CEIL_2MB(x) ((((uintptr_t)x) + SIZE_2MB - 1) / SIZE_2MB) << SHIFT_2MB
|
|
|
|
|
2018-04-09 16:40:48 +00:00
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_mem_register(struct spdk_vhost_session *vsession)
|
2017-05-25 16:20:01 +00:00
|
|
|
{
|
|
|
|
struct rte_vhost_mem_region *region;
|
|
|
|
uint32_t i;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->mem->nregions; i++) {
|
2017-05-25 16:20:01 +00:00
|
|
|
uint64_t start, end, len;
|
2018-12-13 10:51:34 +00:00
|
|
|
region = &vsession->mem->regions[i];
|
2017-05-25 16:20:01 +00:00
|
|
|
start = FLOOR_2MB(region->mmap_addr);
|
|
|
|
end = CEIL_2MB(region->mmap_addr + region->mmap_size);
|
|
|
|
len = end - start;
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Registering VM memory for vtophys translation - 0x%jx len:0x%jx\n",
|
|
|
|
start, len);
|
2017-08-08 14:11:49 +00:00
|
|
|
|
|
|
|
if (spdk_mem_register((void *)start, len) != 0) {
|
|
|
|
SPDK_WARNLOG("Failed to register memory region %"PRIu32". Future vtophys translation might fail.\n",
|
|
|
|
i);
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-25 16:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:40:48 +00:00
|
|
|
static void
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_mem_unregister(struct spdk_vhost_session *vsession)
|
2017-05-25 16:20:01 +00:00
|
|
|
{
|
|
|
|
struct rte_vhost_mem_region *region;
|
|
|
|
uint32_t i;
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->mem->nregions; i++) {
|
2017-05-25 16:20:01 +00:00
|
|
|
uint64_t start, end, len;
|
2018-12-13 10:51:34 +00:00
|
|
|
region = &vsession->mem->regions[i];
|
2017-05-25 16:20:01 +00:00
|
|
|
start = FLOOR_2MB(region->mmap_addr);
|
|
|
|
end = CEIL_2MB(region->mmap_addr + region->mmap_size);
|
|
|
|
len = end - start;
|
|
|
|
|
2018-12-28 11:59:21 +00:00
|
|
|
if (spdk_vtophys((void *) start, NULL) == SPDK_VTOPHYS_ERROR) {
|
2017-08-08 14:11:49 +00:00
|
|
|
continue; /* region has not been registered */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_mem_unregister((void *)start, len) != 0) {
|
|
|
|
assert(false);
|
|
|
|
}
|
2017-05-25 16:20:01 +00:00
|
|
|
}
|
2017-11-22 06:01:48 +00:00
|
|
|
|
2017-05-25 16:20:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 15:25:35 +00:00
|
|
|
void
|
2017-05-22 13:54:09 +00:00
|
|
|
spdk_vhost_free_reactor(uint32_t lcore)
|
|
|
|
{
|
|
|
|
g_num_ctrlrs[lcore]--;
|
|
|
|
}
|
|
|
|
|
2019-01-13 14:13:46 +00:00
|
|
|
struct spdk_vhost_dev *
|
|
|
|
spdk_vhost_dev_next(struct spdk_vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
if (vdev == NULL) {
|
|
|
|
return TAILQ_FIRST(&g_spdk_vhost_devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TAILQ_NEXT(vdev, tailq);
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:08:07 +00:00
|
|
|
struct spdk_vhost_dev *
|
|
|
|
spdk_vhost_dev_find(const char *ctrlr_name)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
2018-02-12 20:02:15 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
2017-03-02 14:12:20 +00:00
|
|
|
size_t dev_dirname_len = strlen(dev_dirname);
|
|
|
|
|
|
|
|
if (strncmp(ctrlr_name, dev_dirname, dev_dirname_len) == 0) {
|
|
|
|
ctrlr_name += dev_dirname_len;
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
TAILQ_FOREACH(vdev, &g_spdk_vhost_devices, tailq) {
|
|
|
|
if (strcmp(vdev->name, ctrlr_name) == 0) {
|
|
|
|
return vdev;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:08:07 +00:00
|
|
|
return NULL;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 18:11:14 +00:00
|
|
|
static int
|
2017-12-21 16:48:31 +00:00
|
|
|
spdk_vhost_parse_core_mask(const char *mask, struct spdk_cpuset *cpumask)
|
2017-08-10 18:11:14 +00:00
|
|
|
{
|
2017-12-21 04:29:21 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (cpumask == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-10 18:11:14 +00:00
|
|
|
|
2017-08-31 20:01:14 +00:00
|
|
|
if (mask == NULL) {
|
2017-12-21 16:48:31 +00:00
|
|
|
spdk_cpuset_copy(cpumask, spdk_app_get_core_mask());
|
2017-08-10 18:11:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:29:21 +00:00
|
|
|
rc = spdk_app_parse_core_mask(mask, cpumask);
|
2017-12-21 16:48:31 +00:00
|
|
|
if (rc < 0) {
|
2017-12-21 04:29:21 +00:00
|
|
|
SPDK_ERRLOG("invalid cpumask %s\n", mask);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-10 18:11:14 +00:00
|
|
|
|
2017-12-21 16:48:31 +00:00
|
|
|
if (spdk_cpuset_count(cpumask) == 0) {
|
|
|
|
SPDK_ERRLOG("no cpu is selected among reactor mask(=%s)\n",
|
|
|
|
spdk_cpuset_fmt(spdk_app_get_core_mask()));
|
2017-08-10 18:11:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:15:01 +00:00
|
|
|
static void *
|
|
|
|
_start_rte_driver(void *arg)
|
|
|
|
{
|
|
|
|
char *path = arg;
|
|
|
|
|
|
|
|
if (rte_vhost_driver_start(path) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:53:27 +00:00
|
|
|
int
|
2018-01-26 13:09:39 +00:00
|
|
|
spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str,
|
2018-01-26 14:29:20 +00:00
|
|
|
const struct spdk_vhost_dev_backend *backend)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
2017-03-14 14:22:57 +00:00
|
|
|
struct stat file_stat;
|
2017-12-21 16:48:31 +00:00
|
|
|
struct spdk_cpuset *cpumask;
|
|
|
|
int rc;
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2017-06-02 17:51:19 +00:00
|
|
|
assert(vdev);
|
|
|
|
if (name == NULL) {
|
2017-05-18 15:39:28 +00:00
|
|
|
SPDK_ERRLOG("Can't register controller with no name\n");
|
2017-03-02 14:12:20 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-12-21 16:48:31 +00:00
|
|
|
cpumask = spdk_cpuset_alloc();
|
|
|
|
if (!cpumask) {
|
|
|
|
SPDK_ERRLOG("spdk_cpuset_alloc failed\n");
|
2018-02-12 20:52:34 +00:00
|
|
|
return -ENOMEM;
|
2017-12-21 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_vhost_parse_core_mask(mask_str, cpumask) != 0) {
|
|
|
|
SPDK_ERRLOG("cpumask %s is invalid (app mask is 0x%s)\n",
|
|
|
|
mask_str, spdk_cpuset_fmt(spdk_app_get_core_mask()));
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
2017-06-02 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (spdk_vhost_dev_find(name)) {
|
|
|
|
SPDK_ERRLOG("vhost controller %s already exists.\n", name);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EEXIST;
|
|
|
|
goto out;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 17:51:19 +00:00
|
|
|
if (snprintf(path, sizeof(path), "%s%s", dev_dirname, name) >= (int)sizeof(path)) {
|
|
|
|
SPDK_ERRLOG("Resulting socket path for controller %s is too long: %s%s\n", name, dev_dirname,
|
|
|
|
name);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 14:22:57 +00:00
|
|
|
/* Register vhost driver to handle vhost messages. */
|
|
|
|
if (stat(path, &file_stat) != -1) {
|
|
|
|
if (!S_ISSOCK(file_stat.st_mode)) {
|
2017-11-16 13:35:51 +00:00
|
|
|
SPDK_ERRLOG("Cannot create a domain socket at path \"%s\": "
|
|
|
|
"The file already exists and is not a socket.\n",
|
|
|
|
path);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-03-14 14:22:57 +00:00
|
|
|
} else if (unlink(path) != 0) {
|
2017-11-16 13:35:51 +00:00
|
|
|
SPDK_ERRLOG("Cannot create a domain socket at path \"%s\": "
|
|
|
|
"The socket already exists and failed to unlink.\n",
|
|
|
|
path);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-03-14 14:22:57 +00:00
|
|
|
}
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rte_vhost_driver_register(path, 0) != 0) {
|
2017-06-02 17:51:19 +00:00
|
|
|
SPDK_ERRLOG("Could not register controller %s with vhost library\n", name);
|
2017-03-02 14:12:20 +00:00
|
|
|
SPDK_ERRLOG("Check if domain socket %s already exists\n", path);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
2017-05-18 15:39:28 +00:00
|
|
|
if (rte_vhost_driver_set_features(path, backend->virtio_features) ||
|
|
|
|
rte_vhost_driver_disable_features(path, backend->disabled_features)) {
|
2017-06-02 17:51:19 +00:00
|
|
|
SPDK_ERRLOG("Couldn't set vhost features for controller %s\n", name);
|
|
|
|
|
|
|
|
rte_vhost_driver_unregister(path);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-05-08 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:46:51 +00:00
|
|
|
if (rte_vhost_driver_callback_register(path, &g_spdk_vhost_ops) != 0) {
|
2017-06-02 17:51:19 +00:00
|
|
|
rte_vhost_driver_unregister(path);
|
|
|
|
SPDK_ERRLOG("Couldn't register callbacks for controller %s\n", name);
|
2017-12-21 16:48:31 +00:00
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
2017-05-08 16:43:16 +00:00
|
|
|
}
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2018-04-09 16:15:01 +00:00
|
|
|
/* The following might start a POSIX thread that polls for incoming
|
|
|
|
* socket connections and calls backend->start/stop_device. These backend
|
|
|
|
* callbacks are also protected by the global SPDK vhost mutex, so we're
|
|
|
|
* safe with not initializing the vdev just yet.
|
|
|
|
*/
|
|
|
|
if (spdk_call_unaffinitized(_start_rte_driver, path) == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to start vhost driver for controller %s (%d): %s\n",
|
|
|
|
name, errno, spdk_strerror(errno));
|
|
|
|
rte_vhost_driver_unregister(path);
|
|
|
|
rc = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-06-02 17:51:19 +00:00
|
|
|
vdev->name = strdup(name);
|
2017-06-19 14:37:51 +00:00
|
|
|
vdev->path = strdup(path);
|
2017-06-02 17:51:19 +00:00
|
|
|
vdev->cpumask = cpumask;
|
2018-01-26 13:46:26 +00:00
|
|
|
vdev->registered = true;
|
2017-08-01 13:46:51 +00:00
|
|
|
vdev->backend = backend;
|
2018-12-17 15:33:30 +00:00
|
|
|
TAILQ_INIT(&vdev->vsessions);
|
2017-09-11 17:45:56 +00:00
|
|
|
spdk_vhost_set_coalescing(vdev, SPDK_VHOST_COALESCING_DELAY_BASE_US,
|
|
|
|
SPDK_VHOST_VQ_IOPS_COALESCING_THRESHOLD);
|
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
TAILQ_INSERT_TAIL(&g_spdk_vhost_devices, vdev, tailq);
|
2017-06-02 17:51:19 +00:00
|
|
|
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Controller %s: new controller added\n", vdev->name);
|
2017-05-18 15:39:28 +00:00
|
|
|
return 0;
|
2017-12-21 16:48:31 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
spdk_cpuset_free(cpumask);
|
|
|
|
return rc;
|
2017-05-18 15:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-01-26 13:09:39 +00:00
|
|
|
spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev)
|
2017-03-15 16:00:55 +00:00
|
|
|
{
|
2018-12-17 15:33:30 +00:00
|
|
|
if (!TAILQ_EMPTY(&vdev->vsessions)) {
|
2017-08-01 16:08:32 +00:00
|
|
|
SPDK_ERRLOG("Controller %s has still valid connection.\n", vdev->name);
|
2018-08-14 11:56:50 +00:00
|
|
|
return -EBUSY;
|
2017-03-15 16:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 13:46:26 +00:00
|
|
|
if (vdev->registered && rte_vhost_driver_unregister(vdev->path) != 0) {
|
2017-05-22 13:34:45 +00:00
|
|
|
SPDK_ERRLOG("Could not unregister controller %s with vhost library\n"
|
2017-12-05 11:06:19 +00:00
|
|
|
"Check if domain socket %s still exists\n",
|
|
|
|
vdev->name, vdev->path);
|
2017-05-22 13:34:45 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Controller %s: removed\n", vdev->name);
|
2017-05-22 13:34:45 +00:00
|
|
|
|
2017-06-02 17:51:19 +00:00
|
|
|
free(vdev->name);
|
2017-06-19 14:37:51 +00:00
|
|
|
free(vdev->path);
|
2017-12-21 16:48:31 +00:00
|
|
|
spdk_cpuset_free(vdev->cpumask);
|
2018-02-12 20:02:15 +00:00
|
|
|
TAILQ_REMOVE(&g_spdk_vhost_devices, vdev, tailq);
|
2017-05-22 13:34:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
static struct spdk_vhost_session *
|
|
|
|
spdk_vhost_session_next(struct spdk_vhost_dev *vdev, unsigned prev_id)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(vsession, &vdev->vsessions, tailq) {
|
|
|
|
if (vsession->id > prev_id) {
|
|
|
|
return vsession;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:47:35 +00:00
|
|
|
return NULL;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2017-05-17 12:42:20 +00:00
|
|
|
spdk_vhost_dev_get_name(struct spdk_vhost_dev *vdev)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
2017-05-17 12:42:20 +00:00
|
|
|
assert(vdev != NULL);
|
|
|
|
return vdev->name;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 20:58:50 +00:00
|
|
|
const struct spdk_cpuset *
|
|
|
|
spdk_vhost_dev_get_cpumask(struct spdk_vhost_dev *vdev)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
2017-05-17 12:42:20 +00:00
|
|
|
assert(vdev != NULL);
|
2018-02-12 20:58:50 +00:00
|
|
|
return vdev->cpumask;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 15:25:35 +00:00
|
|
|
uint32_t
|
2017-12-21 16:48:31 +00:00
|
|
|
spdk_vhost_allocate_reactor(struct spdk_cpuset *cpumask)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
|
|
|
uint32_t i, selected_core;
|
|
|
|
uint32_t min_ctrlrs;
|
|
|
|
|
|
|
|
min_ctrlrs = INT_MAX;
|
2017-12-24 23:26:52 +00:00
|
|
|
selected_core = spdk_env_get_first_core();
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2017-12-24 23:26:52 +00:00
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
2017-12-21 16:48:31 +00:00
|
|
|
if (!spdk_cpuset_get_cpu(cpumask, i)) {
|
2017-03-02 14:12:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_num_ctrlrs[i] < min_ctrlrs) {
|
|
|
|
selected_core = i;
|
|
|
|
min_ctrlrs = g_num_ctrlrs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_num_ctrlrs[selected_core]++;
|
|
|
|
return selected_core;
|
|
|
|
}
|
|
|
|
|
2017-09-07 18:34:15 +00:00
|
|
|
void
|
2019-01-22 11:57:09 +00:00
|
|
|
spdk_vhost_session_event_done(void *event_ctx, int response)
|
2017-09-07 18:34:15 +00:00
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session_fn_ctx *ctx = event_ctx;
|
2017-09-07 18:34:15 +00:00
|
|
|
|
|
|
|
ctx->response = response;
|
|
|
|
sem_post(&ctx->sem);
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:48:49 +00:00
|
|
|
static void
|
2017-07-26 10:00:26 +00:00
|
|
|
spdk_vhost_event_cb(void *arg1, void *arg2)
|
2017-09-04 18:48:49 +00:00
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session_fn_ctx *ctx = arg1;
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-09-04 18:48:49 +00:00
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_id(ctx->vdev, ctx->vsession_id);
|
|
|
|
ctx->cb_fn(ctx->vdev, vsession, ctx);
|
2017-09-07 19:15:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
static void spdk_vhost_external_event_foreach_continue(struct spdk_vhost_dev *vdev,
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session *vsession,
|
|
|
|
spdk_vhost_session_fn fn, void *arg);
|
2017-08-08 16:52:53 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
spdk_vhost_event_async_foreach_fn(void *arg1, void *arg2)
|
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session_fn_ctx *ctx = arg1;
|
|
|
|
struct spdk_vhost_session *vsession = NULL;
|
|
|
|
struct spdk_vhost_dev *vdev = ctx->vdev;
|
2017-08-08 16:52:53 +00:00
|
|
|
struct spdk_event *ev;
|
2019-01-07 23:38:48 +00:00
|
|
|
int rc;
|
2017-08-08 16:52:53 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_trylock(&g_spdk_vhost_mutex) != 0) {
|
|
|
|
ev = spdk_event_allocate(spdk_env_get_current_core(),
|
|
|
|
spdk_vhost_event_async_foreach_fn, arg1, arg2);
|
|
|
|
spdk_event_call(ev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_id(vdev, ctx->vsession_id);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
/* The session must have been removed in the meantime, so we
|
|
|
|
* just skip it in our foreach chain
|
|
|
|
*/
|
|
|
|
goto out_unlock_continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vsession->lcore >= 0 &&
|
2019-01-07 23:30:57 +00:00
|
|
|
(uint32_t)vsession->lcore != spdk_env_get_current_core()) {
|
|
|
|
/* if session has been relocated to other core, it is no longer thread-safe
|
|
|
|
* to access its contents here. Even though we're running under the global
|
|
|
|
* vhost mutex, the session itself (and its pollers) are not. We need to chase
|
|
|
|
* the session thread as many times as necessary.
|
2018-09-21 09:26:45 +00:00
|
|
|
*/
|
2019-01-07 23:30:57 +00:00
|
|
|
ev = spdk_event_allocate(vsession->lcore,
|
2018-09-21 09:26:45 +00:00
|
|
|
spdk_vhost_event_async_foreach_fn, arg1, arg2);
|
|
|
|
spdk_event_call(ev);
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return;
|
2017-08-08 16:52:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
rc = ctx->cb_fn(vdev, vsession, arg2);
|
2019-01-07 23:38:48 +00:00
|
|
|
if (rc < 0) {
|
2019-01-07 23:30:57 +00:00
|
|
|
goto out_unlock;
|
2019-01-07 23:38:48 +00:00
|
|
|
}
|
2018-09-21 09:26:45 +00:00
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
out_unlock_continue:
|
|
|
|
vsession = spdk_vhost_session_next(vdev, ctx->vsession_id);
|
2019-01-07 23:30:57 +00:00
|
|
|
spdk_vhost_external_event_foreach_continue(vdev, vsession, ctx->cb_fn, arg2);
|
|
|
|
out_unlock:
|
2017-08-08 16:52:53 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:57:09 +00:00
|
|
|
int
|
|
|
|
spdk_vhost_session_send_event(struct spdk_vhost_session *vsession,
|
|
|
|
spdk_vhost_session_fn cb_fn, unsigned timeout_sec,
|
|
|
|
const char *errmsg)
|
2017-09-04 18:48:49 +00:00
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session_fn_ctx ev_ctx = {0};
|
2017-09-04 18:48:49 +00:00
|
|
|
struct spdk_event *ev;
|
|
|
|
struct timespec timeout;
|
|
|
|
int rc;
|
|
|
|
|
2017-07-26 10:00:26 +00:00
|
|
|
rc = sem_init(&ev_ctx.sem, 0, 0);
|
|
|
|
if (rc != 0) {
|
2017-09-04 18:48:49 +00:00
|
|
|
SPDK_ERRLOG("Failed to initialize semaphore for vhost timed event\n");
|
2017-07-26 10:00:26 +00:00
|
|
|
return -errno;
|
|
|
|
}
|
2017-09-04 18:48:49 +00:00
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
ev_ctx.vdev = vsession->vdev;
|
2018-12-17 15:33:30 +00:00
|
|
|
ev_ctx.vsession_id = vsession->id;
|
2017-09-04 18:48:49 +00:00
|
|
|
ev_ctx.cb_fn = cb_fn;
|
2019-01-07 23:30:57 +00:00
|
|
|
ev = spdk_event_allocate(vsession->lcore, spdk_vhost_event_cb, &ev_ctx, NULL);
|
2017-09-04 18:48:49 +00:00
|
|
|
assert(ev);
|
|
|
|
spdk_event_call(ev);
|
2018-09-21 09:26:45 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
2017-09-04 18:48:49 +00:00
|
|
|
|
2017-08-04 10:02:03 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &timeout);
|
|
|
|
timeout.tv_sec += timeout_sec;
|
|
|
|
|
2017-09-04 18:48:49 +00:00
|
|
|
rc = sem_timedwait(&ev_ctx.sem, &timeout);
|
|
|
|
if (rc != 0) {
|
2017-10-03 07:05:10 +00:00
|
|
|
SPDK_ERRLOG("Timeout waiting for event: %s.\n", errmsg);
|
|
|
|
sem_wait(&ev_ctx.sem);
|
2017-09-04 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sem_destroy(&ev_ctx.sem);
|
2018-09-21 09:26:45 +00:00
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2017-09-07 18:34:15 +00:00
|
|
|
return ev_ctx.response;
|
2017-09-04 18:48:49 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 19:15:03 +00:00
|
|
|
static int
|
2019-01-07 23:30:57 +00:00
|
|
|
spdk_vhost_event_async_send_foreach_continue(struct spdk_vhost_session *vsession,
|
|
|
|
spdk_vhost_session_fn cb_fn, void *arg)
|
2017-09-07 19:15:03 +00:00
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_dev *vdev = vsession->vdev;
|
|
|
|
struct spdk_vhost_session_fn_ctx *ev_ctx;
|
2017-09-07 19:15:03 +00:00
|
|
|
struct spdk_event *ev;
|
|
|
|
|
|
|
|
ev_ctx = calloc(1, sizeof(*ev_ctx));
|
|
|
|
if (ev_ctx == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to alloc vhost event.\n");
|
2018-08-30 21:58:56 +00:00
|
|
|
assert(false);
|
2017-09-07 19:15:03 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:47:35 +00:00
|
|
|
ev_ctx->vdev = vdev;
|
2018-12-17 15:33:30 +00:00
|
|
|
ev_ctx->vsession_id = vsession->id;
|
2017-09-07 19:15:03 +00:00
|
|
|
ev_ctx->cb_fn = cb_fn;
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
ev = spdk_event_allocate(vsession->lcore,
|
|
|
|
spdk_vhost_event_async_foreach_fn, ev_ctx, arg);
|
2017-09-07 19:15:03 +00:00
|
|
|
assert(ev);
|
|
|
|
spdk_event_call(ev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-01 13:46:51 +00:00
|
|
|
static void
|
2017-10-03 07:00:33 +00:00
|
|
|
stop_device(int vid)
|
2017-05-25 14:12:31 +00:00
|
|
|
{
|
2017-08-01 13:46:51 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
2018-12-13 10:51:34 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2019-02-24 23:46:37 +00:00
|
|
|
struct spdk_vhost_virtqueue *q;
|
2017-08-04 10:02:03 +00:00
|
|
|
int rc;
|
2017-05-25 14:12:31 +00:00
|
|
|
uint16_t i;
|
|
|
|
|
2017-07-25 18:40:17 +00:00
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_vid(vid);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
|
2017-07-25 18:40:17 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
2017-08-01 13:46:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vdev = vsession->vdev;
|
2019-01-07 23:30:57 +00:00
|
|
|
if (vsession->lcore == -1) {
|
2017-08-01 16:08:32 +00:00
|
|
|
SPDK_ERRLOG("Controller %s is not loaded.\n", vdev->name);
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:57:09 +00:00
|
|
|
rc = vdev->backend->stop_session(vsession);
|
2017-08-04 10:02:03 +00:00
|
|
|
if (rc != 0) {
|
2017-08-01 13:46:51 +00:00
|
|
|
SPDK_ERRLOG("Couldn't stop device with vid %d.\n", vid);
|
2017-07-25 18:40:17 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
2017-08-01 13:46:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
2019-02-24 23:46:37 +00:00
|
|
|
q = &vsession->virtqueue[i];
|
|
|
|
if (q->vring.desc == NULL) {
|
2018-04-23 11:04:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
rte_vhost_set_vring_base(vsession->vid, i, q->last_avail_idx, q->last_used_idx);
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_mem_unregister(vsession);
|
|
|
|
free(vsession->mem);
|
2019-01-07 23:30:57 +00:00
|
|
|
vsession->lcore = -1;
|
2019-01-23 12:39:40 +00:00
|
|
|
assert(vdev->active_session_num > 0);
|
|
|
|
vdev->active_session_num--;
|
2017-07-25 18:40:17 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:46:51 +00:00
|
|
|
static int
|
2017-10-03 07:00:33 +00:00
|
|
|
start_device(int vid)
|
2017-05-25 14:12:31 +00:00
|
|
|
{
|
|
|
|
struct spdk_vhost_dev *vdev;
|
2018-12-13 10:51:34 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-07-25 18:40:17 +00:00
|
|
|
int rc = -1;
|
2017-05-25 14:12:31 +00:00
|
|
|
uint16_t i;
|
|
|
|
|
2017-07-25 18:40:17 +00:00
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_vid(vid);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
|
2017-07-25 18:40:17 +00:00
|
|
|
goto out;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vdev = vsession->vdev;
|
2019-01-07 23:30:57 +00:00
|
|
|
if (vsession->lcore != -1) {
|
2017-08-01 16:08:32 +00:00
|
|
|
SPDK_ERRLOG("Controller %s already loaded.\n", vdev->name);
|
2017-07-25 18:40:17 +00:00
|
|
|
goto out;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
vsession->max_queues = 0;
|
|
|
|
memset(vsession->virtqueue, 0, sizeof(vsession->virtqueue));
|
2018-04-23 11:04:18 +00:00
|
|
|
for (i = 0; i < SPDK_VHOST_MAX_VQUEUES; i++) {
|
2019-02-24 23:46:37 +00:00
|
|
|
struct spdk_vhost_virtqueue *q = &vsession->virtqueue[i];
|
|
|
|
|
|
|
|
if (rte_vhost_get_vhost_vring(vid, i, &q->vring)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (q->vring.desc == NULL || q->vring.size == 0) {
|
2018-04-23 11:04:18 +00:00
|
|
|
continue;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
if (rte_vhost_get_vring_base(vsession->vid, i, &q->last_avail_idx, &q->last_used_idx)) {
|
|
|
|
q->vring.desc = NULL;
|
2018-04-23 11:04:18 +00:00
|
|
|
continue;
|
2017-10-10 20:19:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:12:31 +00:00
|
|
|
/* Disable notifications. */
|
|
|
|
if (rte_vhost_enable_guest_notification(vid, i, 0) != 0) {
|
|
|
|
SPDK_ERRLOG("vhost device %d: Failed to disable guest notification on queue %"PRIu16"\n", vid, i);
|
2017-07-25 18:40:17 +00:00
|
|
|
goto out;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
vsession->max_queues = i + 1;
|
2018-04-23 11:04:18 +00:00
|
|
|
}
|
2017-05-25 14:12:31 +00:00
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (rte_vhost_get_negotiated_features(vid, &vsession->negotiated_features) != 0) {
|
2017-05-25 14:12:31 +00:00
|
|
|
SPDK_ERRLOG("vhost device %d: Failed to get negotiated driver features\n", vid);
|
2017-07-25 18:40:17 +00:00
|
|
|
goto out;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:51:34 +00:00
|
|
|
if (rte_vhost_get_mem_table(vid, &vsession->mem) != 0) {
|
2017-05-25 14:12:31 +00:00
|
|
|
SPDK_ERRLOG("vhost device %d: Failed to get guest memory table\n", vid);
|
2017-07-25 18:40:17 +00:00
|
|
|
goto out;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 10:16:27 +00:00
|
|
|
for (i = 0; i < vsession->mem->nregions; i++) {
|
2019-02-21 11:30:19 +00:00
|
|
|
uint64_t mmap_size = vsession->mem->regions[i].mmap_size;
|
|
|
|
|
|
|
|
if (mmap_size & MASK_2MB) {
|
|
|
|
SPDK_ERRLOG("vhost device %d: Guest mmaped memory size %" PRIx64
|
|
|
|
" is not a 2MB multiple\n", vid, mmap_size);
|
2018-12-05 10:16:27 +00:00
|
|
|
free(vsession->mem);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 18:27:41 +00:00
|
|
|
/*
|
|
|
|
* Not sure right now but this look like some kind of QEMU bug and guest IO
|
|
|
|
* might be frozed without kicking all queues after live-migration. This look like
|
|
|
|
* the previous vhost instance failed to effectively deliver all interrupts before
|
|
|
|
* the GET_VRING_BASE message. This shouldn't harm guest since spurious interrupts
|
|
|
|
* should be ignored by guest virtio driver.
|
|
|
|
*
|
|
|
|
* Tested on QEMU 2.10.91 and 2.11.50.
|
|
|
|
*/
|
2018-12-13 10:51:34 +00:00
|
|
|
for (i = 0; i < vsession->max_queues; i++) {
|
|
|
|
if (vsession->virtqueue[i].vring.callfd != -1) {
|
|
|
|
eventfd_write(vsession->virtqueue[i].vring.callfd, (eventfd_t)1);
|
2018-01-18 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:45:35 +00:00
|
|
|
spdk_vhost_session_set_coalescing(vdev, vsession, NULL);
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_mem_register(vsession);
|
2019-01-22 11:57:09 +00:00
|
|
|
rc = vdev->backend->start_session(vsession);
|
2017-08-01 13:46:51 +00:00
|
|
|
if (rc != 0) {
|
2018-12-13 10:51:34 +00:00
|
|
|
spdk_vhost_session_mem_unregister(vsession);
|
|
|
|
free(vsession->mem);
|
2019-01-23 12:39:40 +00:00
|
|
|
goto out;
|
2017-08-01 13:46:51 +00:00
|
|
|
}
|
2017-05-25 14:12:31 +00:00
|
|
|
|
2019-01-23 12:39:40 +00:00
|
|
|
assert(vdev->active_session_num < UINT32_MAX);
|
|
|
|
vdev->active_session_num++;
|
2017-07-25 18:40:17 +00:00
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return rc;
|
2017-05-25 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 19:53:12 +00:00
|
|
|
static int
|
|
|
|
get_config(int vid, uint8_t *config, uint32_t len)
|
|
|
|
{
|
2018-12-13 12:07:11 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-11-08 19:53:12 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_vid(vid);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
|
2017-11-08 19:53:12 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vdev = vsession->vdev;
|
2017-11-08 19:53:12 +00:00
|
|
|
if (vdev->backend->vhost_get_config) {
|
|
|
|
rc = vdev->backend->vhost_get_config(vdev, config, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_config(int vid, uint8_t *config, uint32_t offset, uint32_t size, uint32_t flags)
|
|
|
|
{
|
2018-12-13 12:07:11 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-11-08 19:53:12 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_vid(vid);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
|
2017-11-08 19:53:12 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vdev = vsession->vdev;
|
2017-11-08 19:53:12 +00:00
|
|
|
if (vdev->backend->vhost_set_config) {
|
|
|
|
rc = vdev->backend->vhost_set_config(vdev, config, offset, size, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:38:27 +00:00
|
|
|
int
|
|
|
|
spdk_vhost_set_socket_path(const char *basename)
|
2017-03-02 14:12:20 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-03-24 03:27:43 +00:00
|
|
|
if (basename && strlen(basename) > 0) {
|
2017-03-02 14:12:20 +00:00
|
|
|
ret = snprintf(dev_dirname, sizeof(dev_dirname) - 2, "%s", basename);
|
2018-04-18 22:50:50 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-03-24 03:27:43 +00:00
|
|
|
if ((size_t)ret >= sizeof(dev_dirname) - 2) {
|
2017-06-01 06:53:41 +00:00
|
|
|
SPDK_ERRLOG("Char dev dir path length %d is too long\n", ret);
|
2018-03-08 22:38:27 +00:00
|
|
|
return -EINVAL;
|
2017-03-24 03:27:43 +00:00
|
|
|
}
|
2017-03-02 14:12:20 +00:00
|
|
|
|
|
|
|
if (dev_dirname[ret - 1] != '/') {
|
|
|
|
dev_dirname[ret] = '/';
|
|
|
|
dev_dirname[ret + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:38:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:12:20 +00:00
|
|
|
static void *
|
|
|
|
session_shutdown(void *arg)
|
|
|
|
{
|
2017-05-17 12:42:20 +00:00
|
|
|
struct spdk_vhost_dev *vdev = NULL;
|
2017-09-05 15:13:50 +00:00
|
|
|
|
2018-02-12 20:02:15 +00:00
|
|
|
TAILQ_FOREACH(vdev, &g_spdk_vhost_devices, tailq) {
|
2017-12-05 11:06:19 +00:00
|
|
|
rte_vhost_driver_unregister(vdev->path);
|
2018-01-26 13:46:26 +00:00
|
|
|
vdev->registered = false;
|
2017-03-02 14:12:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_INFOLOG(SPDK_LOG_VHOST, "Exiting\n");
|
2017-10-05 14:02:49 +00:00
|
|
|
spdk_event_call((struct spdk_event *)arg);
|
2017-03-02 14:12:20 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-07-13 14:37:50 +00:00
|
|
|
void
|
2018-03-19 18:01:57 +00:00
|
|
|
spdk_vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w)
|
2017-07-13 14:37:50 +00:00
|
|
|
{
|
2018-03-19 18:01:57 +00:00
|
|
|
assert(vdev->backend->dump_info_json != NULL);
|
|
|
|
vdev->backend->dump_info_json(vdev, w);
|
2017-07-13 14:37:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 18:23:43 +00:00
|
|
|
int
|
2018-01-26 13:09:39 +00:00
|
|
|
spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev)
|
2017-09-05 18:23:43 +00:00
|
|
|
{
|
2019-01-07 23:30:57 +00:00
|
|
|
if (vdev->pending_async_op_num) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:09:39 +00:00
|
|
|
return vdev->backend->remove_device(vdev);
|
2017-09-05 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 16:08:32 +00:00
|
|
|
static int
|
|
|
|
new_connection(int vid)
|
|
|
|
{
|
|
|
|
struct spdk_vhost_dev *vdev;
|
2018-12-13 12:07:11 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-08-01 16:08:32 +00:00
|
|
|
char ifname[PATH_MAX];
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2018-12-17 15:33:30 +00:00
|
|
|
|
2017-08-01 16:08:32 +00:00
|
|
|
if (rte_vhost_get_ifname(vid, ifname, PATH_MAX) < 0) {
|
|
|
|
SPDK_ERRLOG("Couldn't get a valid ifname for device with vid %d\n", vid);
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vdev = spdk_vhost_dev_find(ifname);
|
|
|
|
if (vdev == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find device with vid %d to create connection for.\n", vid);
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
/* We expect sessions inside vdev->vsessions to be sorted in ascending
|
|
|
|
* order in regard of vsession->id. For now we always set id = vsessions_cnt++
|
|
|
|
* and append each session to the very end of the vsessions list.
|
|
|
|
* This is required for spdk_vhost_dev_foreach_session() to work.
|
|
|
|
*/
|
|
|
|
if (vdev->vsessions_num == UINT_MAX) {
|
|
|
|
assert(false);
|
|
|
|
return -EINVAL;
|
2018-12-14 13:44:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 14:07:02 +00:00
|
|
|
vsession = spdk_dma_zmalloc(sizeof(struct spdk_vhost_session) +
|
|
|
|
vdev->backend->session_ctx_size,
|
2018-12-14 13:44:46 +00:00
|
|
|
SPDK_CACHE_LINE_SIZE, NULL);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("spdk_dma_zmalloc failed\n");
|
2017-08-01 16:08:32 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession->vdev = vdev;
|
2018-12-17 15:33:30 +00:00
|
|
|
vsession->id = vdev->vsessions_num++;
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession->vid = vid;
|
2019-01-07 23:30:57 +00:00
|
|
|
vsession->lcore = -1;
|
2018-12-17 02:45:35 +00:00
|
|
|
vsession->next_stats_check_time = 0;
|
|
|
|
vsession->stats_check_interval = SPDK_VHOST_STATS_CHECK_INTERVAL_MS *
|
|
|
|
spdk_get_ticks_hz() / 1000UL;
|
2018-12-17 15:33:30 +00:00
|
|
|
TAILQ_INSERT_TAIL(&vdev->vsessions, vsession, tailq);
|
2017-08-01 16:08:32 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_connection(int vid)
|
|
|
|
{
|
2018-12-13 12:07:11 +00:00
|
|
|
struct spdk_vhost_session *vsession;
|
2017-08-01 16:08:32 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
2018-12-13 12:07:11 +00:00
|
|
|
vsession = spdk_vhost_session_find_by_vid(vid);
|
|
|
|
if (vsession == NULL) {
|
|
|
|
SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
|
2017-08-01 16:08:32 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:33:30 +00:00
|
|
|
TAILQ_REMOVE(&vsession->vdev->vsessions, vsession, tailq);
|
2018-12-14 13:44:46 +00:00
|
|
|
spdk_dma_free(vsession);
|
2017-08-01 16:08:32 +00:00
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:52:53 +00:00
|
|
|
static void
|
2018-02-12 20:02:15 +00:00
|
|
|
spdk_vhost_external_event_foreach_continue(struct spdk_vhost_dev *vdev,
|
2019-01-07 23:30:57 +00:00
|
|
|
struct spdk_vhost_session *vsession,
|
|
|
|
spdk_vhost_session_fn fn, void *arg)
|
2017-08-08 16:52:53 +00:00
|
|
|
{
|
2019-01-07 23:38:48 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
if (vsession == NULL) {
|
|
|
|
goto out_finish_foreach;
|
2017-08-08 16:52:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
while (vsession->lcore == -1) {
|
|
|
|
rc = fn(vdev, vsession, arg);
|
2019-01-07 23:38:48 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-17 15:33:30 +00:00
|
|
|
vsession = spdk_vhost_session_next(vdev, vsession->id);
|
2019-01-07 23:30:57 +00:00
|
|
|
if (vsession == NULL) {
|
|
|
|
goto out_finish_foreach;
|
2017-08-08 16:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
spdk_vhost_event_async_send_foreach_continue(vsession, fn, arg);
|
|
|
|
return;
|
|
|
|
|
|
|
|
out_finish_foreach:
|
|
|
|
/* there are no more sessions to iterate through, so call the
|
|
|
|
* fn one last time with vsession == NULL
|
|
|
|
*/
|
|
|
|
assert(vdev->pending_async_op_num > 0);
|
|
|
|
vdev->pending_async_op_num--;
|
|
|
|
fn(vdev, NULL, arg);
|
2017-08-08 16:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-15 23:49:00 +00:00
|
|
|
spdk_vhost_dev_foreach_session(struct spdk_vhost_dev *vdev,
|
|
|
|
spdk_vhost_session_fn fn, void *arg)
|
|
|
|
{
|
2018-12-17 15:33:30 +00:00
|
|
|
struct spdk_vhost_session *vsession = TAILQ_FIRST(&vdev->vsessions);
|
|
|
|
|
2019-01-07 23:30:57 +00:00
|
|
|
assert(vdev->pending_async_op_num < UINT32_MAX);
|
|
|
|
vdev->pending_async_op_num++;
|
2018-12-17 15:33:30 +00:00
|
|
|
spdk_vhost_external_event_foreach_continue(vdev, vsession, fn, arg);
|
2018-12-15 23:49:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:06:41 +00:00
|
|
|
void
|
|
|
|
spdk_vhost_lock(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&g_spdk_vhost_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_vhost_unlock(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&g_spdk_vhost_mutex);
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:47:59 +00:00
|
|
|
int
|
|
|
|
spdk_vhost_init(void)
|
|
|
|
{
|
2017-12-24 23:43:41 +00:00
|
|
|
uint32_t last_core;
|
2018-07-12 13:29:18 +00:00
|
|
|
size_t len;
|
2018-03-08 22:48:23 +00:00
|
|
|
int ret;
|
2017-12-24 23:43:41 +00:00
|
|
|
|
2018-07-12 13:29:18 +00:00
|
|
|
if (dev_dirname[0] == '\0') {
|
|
|
|
if (getcwd(dev_dirname, sizeof(dev_dirname) - 1) == NULL) {
|
|
|
|
SPDK_ERRLOG("getcwd failed (%d): %s\n", errno, spdk_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(dev_dirname);
|
|
|
|
if (dev_dirname[len - 1] != '/') {
|
|
|
|
dev_dirname[len] = '/';
|
|
|
|
dev_dirname[len + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 23:43:41 +00:00
|
|
|
last_core = spdk_env_get_last_core();
|
|
|
|
g_num_ctrlrs = calloc(last_core + 1, sizeof(uint32_t));
|
|
|
|
if (!g_num_ctrlrs) {
|
|
|
|
SPDK_ERRLOG("Could not allocate array size=%u for g_num_ctrlrs\n",
|
|
|
|
last_core + 1);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-08 22:48:23 +00:00
|
|
|
|
|
|
|
ret = spdk_vhost_scsi_controller_construct();
|
|
|
|
if (ret != 0) {
|
|
|
|
SPDK_ERRLOG("Cannot construct vhost controllers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = spdk_vhost_blk_controller_construct();
|
|
|
|
if (ret != 0) {
|
|
|
|
SPDK_ERRLOG("Cannot construct vhost block controllers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:01:48 +00:00
|
|
|
ret = spdk_vhost_nvme_controller_construct();
|
|
|
|
if (ret != 0) {
|
|
|
|
SPDK_ERRLOG("Cannot construct vhost NVMe controllers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:47:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
static void
|
|
|
|
_spdk_vhost_fini(void *arg1, void *arg2)
|
2018-01-26 13:46:26 +00:00
|
|
|
{
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_fini_cb fini_cb = arg1;
|
|
|
|
struct spdk_vhost_dev *vdev, *tmp;
|
2018-01-26 13:46:26 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_lock();
|
|
|
|
vdev = spdk_vhost_dev_next(NULL);
|
|
|
|
while (vdev != NULL) {
|
|
|
|
tmp = spdk_vhost_dev_next(vdev);
|
2018-01-26 13:46:26 +00:00
|
|
|
spdk_vhost_dev_remove(vdev);
|
2019-01-07 23:30:57 +00:00
|
|
|
/* don't care if it fails, there's nothing we can do for now */
|
2019-01-13 17:24:52 +00:00
|
|
|
vdev = tmp;
|
2018-01-26 13:46:26 +00:00
|
|
|
}
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_unlock();
|
2018-01-26 13:46:26 +00:00
|
|
|
|
|
|
|
/* All devices are removed now. */
|
|
|
|
free(g_num_ctrlrs);
|
|
|
|
fini_cb();
|
2018-01-26 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_vhost_fini(spdk_vhost_fini_cb fini_cb)
|
|
|
|
{
|
|
|
|
pthread_t tid;
|
|
|
|
int rc;
|
|
|
|
struct spdk_event *fini_ev;
|
|
|
|
|
|
|
|
fini_ev = spdk_event_allocate(spdk_env_get_current_core(), _spdk_vhost_fini, fini_cb, NULL);
|
|
|
|
|
|
|
|
/* rte_vhost API for removing sockets is not asynchronous. Since it may call SPDK
|
|
|
|
* ops for stopping a device or removing a connection, we need to call it from
|
|
|
|
* a separate thread to avoid deadlock.
|
|
|
|
*/
|
|
|
|
rc = pthread_create(&tid, NULL, &session_shutdown, fini_ev);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Failed to start session shutdown thread (%d): %s\n", rc, spdk_strerror(rc));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
pthread_detach(tid);
|
2017-11-29 06:47:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
void
|
|
|
|
spdk_vhost_config_json(struct spdk_json_write_ctx *w, struct spdk_event *done_ev)
|
2018-03-19 18:01:57 +00:00
|
|
|
{
|
2019-01-13 17:24:52 +00:00
|
|
|
struct spdk_vhost_dev *vdev;
|
2018-06-15 15:10:58 +00:00
|
|
|
uint32_t delay_base_us;
|
|
|
|
uint32_t iops_threshold;
|
2018-03-19 18:01:57 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_json_write_array_begin(w);
|
2018-06-15 15:10:58 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_lock();
|
|
|
|
vdev = spdk_vhost_dev_next(NULL);
|
|
|
|
while (vdev != NULL) {
|
|
|
|
vdev->backend->write_config_json(vdev, w);
|
2018-06-15 15:10:58 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_get_coalescing(vdev, &delay_base_us, &iops_threshold);
|
|
|
|
if (delay_base_us) {
|
|
|
|
spdk_json_write_object_begin(w);
|
|
|
|
spdk_json_write_named_string(w, "method", "set_vhost_controller_coalescing");
|
2018-06-15 15:10:58 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_json_write_named_object_begin(w, "params");
|
|
|
|
spdk_json_write_named_string(w, "ctrlr", vdev->name);
|
|
|
|
spdk_json_write_named_uint32(w, "delay_base_us", delay_base_us);
|
|
|
|
spdk_json_write_named_uint32(w, "iops_threshold", iops_threshold);
|
|
|
|
spdk_json_write_object_end(w);
|
2018-04-18 20:36:40 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
vdev = spdk_vhost_dev_next(vdev);
|
2018-04-18 20:36:40 +00:00
|
|
|
}
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_vhost_unlock();
|
2018-03-19 18:01:57 +00:00
|
|
|
|
2019-01-13 17:24:52 +00:00
|
|
|
spdk_json_write_array_end(w);
|
|
|
|
spdk_event_call(done_ev);
|
2018-03-19 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:00:56 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT("vhost", SPDK_LOG_VHOST)
|
2017-08-30 18:06:33 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT("vhost_ring", SPDK_LOG_VHOST_RING)
|