2017-03-02 14:12:20 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* SPDK vhost
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SPDK_VHOST_H
|
|
|
|
#define SPDK_VHOST_H
|
|
|
|
|
2017-05-01 20:22:48 +00:00
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
2017-03-02 14:12:20 +00:00
|
|
|
#include "spdk/event.h"
|
2018-03-19 18:01:57 +00:00
|
|
|
#include "spdk/json.h"
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2017-12-07 20:25:19 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-03-01 06:35:06 +00:00
|
|
|
/**
|
|
|
|
* Callback funcion for spdk_vhost_fini().
|
|
|
|
*/
|
2018-01-26 10:55:05 +00:00
|
|
|
typedef void (*spdk_vhost_fini_cb)(void);
|
|
|
|
|
2018-03-08 22:38:27 +00:00
|
|
|
/**
|
|
|
|
* Set the path to the directory where vhost sockets will be created.
|
|
|
|
*
|
|
|
|
* This function must be called before spdk_vhost_init().
|
|
|
|
*
|
|
|
|
* \param basename Path to vhost socket directory
|
|
|
|
*
|
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
|
|
|
int spdk_vhost_set_socket_path(const char *basename);
|
|
|
|
|
2018-03-01 06:35:06 +00:00
|
|
|
/**
|
|
|
|
* Init vhost environment.
|
|
|
|
*
|
|
|
|
* \return 0 on success, -1 on failure.
|
|
|
|
*/
|
2017-06-14 23:23:52 +00:00
|
|
|
int spdk_vhost_init(void);
|
2018-03-01 06:35:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up the environment of vhost after finishing the vhost application.
|
|
|
|
*
|
|
|
|
* \param fini_cb Called when the cleanup operation completes.
|
|
|
|
*/
|
2018-01-26 10:55:05 +00:00
|
|
|
void spdk_vhost_fini(spdk_vhost_fini_cb fini_cb);
|
2017-06-13 17:41:35 +00:00
|
|
|
|
2018-03-19 18:01:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write vhost subsystem configuration into provided JSON context.
|
|
|
|
*
|
|
|
|
* \param w JSON write context
|
|
|
|
* \param done_ev call this event when done.
|
|
|
|
*/
|
|
|
|
void spdk_vhost_config_json(struct spdk_json_write_ctx *w, struct spdk_event *done_ev);
|
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Deinit vhost application. This is called once by SPDK app layer.
|
2017-08-10 17:51:40 +00:00
|
|
|
*/
|
2017-03-02 14:12:20 +00:00
|
|
|
void spdk_vhost_shutdown_cb(void);
|
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
|
|
|
* SPDK vhost device (vdev). An equivalent of Virtio device.
|
|
|
|
* Both virtio-blk and virtio-scsi devices are represented by this
|
|
|
|
* struct. For virtio-scsi a single vhost device (also called SCSI
|
2017-11-22 11:20:03 +00:00
|
|
|
* controller) may contain multiple SCSI targets (devices), each of
|
2017-08-10 17:51:40 +00:00
|
|
|
* which may contain multiple logical units (SCSI LUNs). For now
|
2017-11-22 11:20:03 +00:00
|
|
|
* only one LUN per target is available.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
|
|
|
* All vdev-changing functions operate directly on this object.
|
|
|
|
* Note that \c spdk_vhost_dev cannot be acquired. This object is
|
|
|
|
* only accessible as a callback parameter via \c
|
|
|
|
* spdk_vhost_call_external_event and it's derivatives. This ensures
|
|
|
|
* that all access to the vdev is piped through a single,
|
|
|
|
* thread-safe API.
|
|
|
|
*/
|
2017-05-17 12:42:20 +00:00
|
|
|
struct spdk_vhost_dev;
|
2017-03-02 14:12:20 +00:00
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
|
|
|
* Synchronized vhost event used for user callbacks.
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev vhost device.
|
|
|
|
* \param arg user-provided parameter.
|
|
|
|
*
|
|
|
|
* \return 0 on success, -1 on failure.
|
2017-08-10 17:51:40 +00:00
|
|
|
*/
|
|
|
|
typedef int (*spdk_vhost_event_fn)(struct spdk_vhost_dev *vdev, void *arg);
|
|
|
|
|
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Get the name of the vhost device. This is equal to the filename
|
2017-08-10 17:51:40 +00:00
|
|
|
* of socket file. The name is constant throughout the lifetime of
|
|
|
|
* a vdev.
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev vhost device.
|
|
|
|
*
|
|
|
|
* \return name of the vdev.
|
2017-08-10 17:51:40 +00:00
|
|
|
*/
|
|
|
|
const char *spdk_vhost_dev_get_name(struct spdk_vhost_dev *vdev);
|
2017-07-26 10:00:26 +00:00
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Get cpuset of the vhost device. The cpuset is constant throughout the lifetime
|
|
|
|
* of a vdev. It is a subset of SPDK app cpuset vhost was started with.
|
|
|
|
*
|
2018-06-18 14:39:39 +00:00
|
|
|
* \param vdev vhost device.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
2018-02-12 20:58:50 +00:00
|
|
|
* \return cpuset of the vdev.
|
2017-08-10 17:51:40 +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-05-17 12:42:20 +00:00
|
|
|
|
2017-09-11 17:45:56 +00:00
|
|
|
/**
|
|
|
|
* By default, events are generated when asked, but for high queue depth and
|
|
|
|
* high IOPS this prove to be inefficient both for guest kernel that have to
|
|
|
|
* handle a lot more IO completions and for SPDK vhost that need to make more
|
|
|
|
* syscalls. If enabled, limit amount of events (IRQs) sent to initiator by SPDK
|
|
|
|
* vhost effectively coalescing couple of completions. This of cource introduce
|
|
|
|
* IO latency penalty proportional to event delay time.
|
|
|
|
*
|
|
|
|
* Actual events delay time when is calculated according to below formula:
|
|
|
|
* if (delay_base == 0 || IOPS < iops_threshold) {
|
|
|
|
* delay = 0;
|
|
|
|
* } else if (IOPS < iops_threshold) {
|
|
|
|
* delay = delay_base * (iops - iops_threshold) / iops_threshold;
|
|
|
|
* }
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev vhost device.
|
2017-09-11 17:45:56 +00:00
|
|
|
* \param delay_base_us Base delay time in microseconds. If 0, coalescing is disabled.
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param iops_threshold IOPS threshold when coalescing is activated.
|
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);
|
|
|
|
|
2018-06-15 15:10:58 +00:00
|
|
|
/**
|
|
|
|
* Get coalescing parameters.
|
|
|
|
*
|
|
|
|
* \see spdk_vhost_set_coalescing
|
|
|
|
*
|
|
|
|
* \param vdev vhost device.
|
|
|
|
* \param delay_base_us Optional pointer to store base delay time.
|
|
|
|
* \param iops_threshold Optional pointer to store IOPS threshold.
|
|
|
|
*/
|
|
|
|
void spdk_vhost_get_coalescing(struct spdk_vhost_dev *vdev, uint32_t *delay_base_us,
|
|
|
|
uint32_t *iops_threshold);
|
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
|
|
|
* Construct an empty vhost SCSI device. This will create a
|
|
|
|
* Unix domain socket together with a vhost-user slave server waiting
|
|
|
|
* for a connection on this socket. Creating the vdev does not
|
|
|
|
* start any I/O pollers and does not hog the CPU. I/O processing
|
|
|
|
* starts after receiving proper message on the created socket.
|
|
|
|
* See QEMU's vhost-user documentation for details.
|
|
|
|
* All physical devices have to be separately attached to this
|
2017-11-22 11:20:03 +00:00
|
|
|
* vdev via \c spdk_vhost_scsi_dev_add_tgt().
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
|
|
|
* This function is thread-safe.
|
|
|
|
*
|
|
|
|
* \param name name of the vhost device. The name will also be used
|
|
|
|
* for socket name, which is exactly \c socket_base_dir/name
|
2018-06-18 14:39:39 +00:00
|
|
|
* \param cpumask string containing cpumask in hex. The leading *0x*
|
2017-08-10 17:51:40 +00:00
|
|
|
* is allowed but not required. The mask itself can be constructed as:
|
|
|
|
* ((1 << cpu0) | (1 << cpu1) | ... | (1 << cpuN)).
|
2018-03-01 06:35:06 +00:00
|
|
|
*
|
2017-08-10 17:51:40 +00:00
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
2017-08-10 18:11:14 +00:00
|
|
|
int spdk_vhost_scsi_dev_construct(const char *name, const char *cpumask);
|
2017-08-10 17:51:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-22 11:20:03 +00:00
|
|
|
* Construct and attach new SCSI target to the vhost SCSI device
|
2017-08-10 17:51:40 +00:00
|
|
|
* on given (unoccupied) slot. The device will be created with a single
|
|
|
|
* LUN0 associated with given SPDK bdev. Currently only one LUN per
|
|
|
|
* device is supported.
|
|
|
|
*
|
2018-06-29 11:53:04 +00:00
|
|
|
* If the vhost SCSI device has an active connection and has negotiated
|
|
|
|
* \c VIRTIO_SCSI_F_HOTPLUG feature, the new SCSI target should be
|
|
|
|
* automatically detected by the other side.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev vhost SCSI device.
|
|
|
|
* \param scsi_tgt_num slot to attach to.
|
|
|
|
* \param bdev_name name of the SPDK bdev to associate with SCSI LUN0.
|
|
|
|
*
|
2017-08-10 17:51:40 +00:00
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
2017-11-22 11:20:03 +00:00
|
|
|
int spdk_vhost_scsi_dev_add_tgt(struct spdk_vhost_dev *vdev, unsigned scsi_tgt_num,
|
2018-01-10 05:55:53 +00:00
|
|
|
const char *bdev_name);
|
2017-08-10 17:51:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-22 11:20:03 +00:00
|
|
|
* Get SCSI target from vhost SCSI device on given slot. Max
|
2018-03-01 06:35:06 +00:00
|
|
|
* number of available slots is defined by.
|
2017-08-10 17:51:40 +00:00
|
|
|
* \c SPDK_VHOST_SCSI_CTRLR_MAX_DEVS.
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev vhost SCSI device.
|
|
|
|
* \param num slot id.
|
|
|
|
*
|
|
|
|
* \return SCSI device on given slot or NULL.
|
2017-08-10 17:51:40 +00:00
|
|
|
*/
|
2017-11-22 11:20:03 +00:00
|
|
|
struct spdk_scsi_dev *spdk_vhost_scsi_dev_get_tgt(struct spdk_vhost_dev *vdev, uint8_t num);
|
2017-08-10 17:51:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-22 11:20:03 +00:00
|
|
|
* Detach and destruct SCSI target from a vhost SCSI device.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
2018-12-16 22:53:59 +00:00
|
|
|
* The device will be deleted after all pending I/O is finished.
|
|
|
|
* If the driver supports VIRTIO_SCSI_F_HOTPLUG, then a hotremove
|
|
|
|
* notification will be sent.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
|
|
|
* \param vdev vhost SCSI device
|
2017-11-22 11:20:03 +00:00
|
|
|
* \param scsi_tgt_num slot id to delete target from
|
|
|
|
* \param cb_fn callback to be fired once target has been successfully
|
2017-08-10 17:51:40 +00:00
|
|
|
* deleted. The first parameter of callback function is the vhost SCSI
|
|
|
|
* device, the second is user provided argument *cb_arg*.
|
|
|
|
* \param cb_arg parameter to be passed to *cb_fn*.
|
2018-03-01 06:35:06 +00:00
|
|
|
*
|
2017-08-10 17:51:40 +00:00
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
2017-11-22 11:20:03 +00:00
|
|
|
int spdk_vhost_scsi_dev_remove_tgt(struct spdk_vhost_dev *vdev, unsigned scsi_tgt_num,
|
2017-07-25 13:42:27 +00:00
|
|
|
spdk_vhost_event_fn cb_fn, void *cb_arg);
|
2017-06-02 16:00:22 +00:00
|
|
|
|
2017-08-10 17:51:40 +00:00
|
|
|
/**
|
|
|
|
* Construct a vhost blk device. This will create a Unix domain
|
|
|
|
* socket together with a vhost-user slave server waiting for a
|
|
|
|
* connection on this socket. Creating the vdev does not start
|
|
|
|
* any I/O pollers and does not hog the CPU. I/O processing starts
|
|
|
|
* after receiving proper message on the created socket.
|
|
|
|
* See QEMU's vhost-user documentation for details. Vhost blk
|
|
|
|
* device is tightly associated with given SPDK bdev. Given
|
|
|
|
* bdev can not be changed, unless it has been hotremoved. This
|
|
|
|
* would result in all I/O failing with virtio \c VIRTIO_BLK_S_IOERR
|
|
|
|
* error code.
|
|
|
|
*
|
|
|
|
* This function is thread-safe.
|
|
|
|
*
|
|
|
|
* \param name name of the vhost blk device. The name will also be
|
|
|
|
* used for socket name, which is exactly \c socket_base_dir/name
|
2018-06-18 14:39:39 +00:00
|
|
|
* \param cpumask string containing cpumask in hex. The leading *0x*
|
2017-08-10 17:51:40 +00:00
|
|
|
* is allowed but not required. The mask itself can be constructed as:
|
|
|
|
* ((1 << cpu0) | (1 << cpu1) | ... | (1 << cpuN)).
|
|
|
|
* \param dev_name bdev name to associate with this vhost device
|
|
|
|
* \param readonly if set, all writes to the device will fail with
|
|
|
|
* \c VIRTIO_BLK_S_IOERR error code.
|
2018-03-01 06:35:06 +00:00
|
|
|
*
|
2017-08-10 17:51:40 +00:00
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
2017-08-10 18:11:14 +00:00
|
|
|
int spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_name,
|
2017-06-19 14:37:51 +00:00
|
|
|
bool readonly);
|
2017-08-10 17:51:40 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Remove a vhost device. The device must not have any open connections on it's socket.
|
|
|
|
*
|
|
|
|
* \param vdev vhost blk device.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
|
|
|
* \return 0 on success, negative errno on error.
|
|
|
|
*/
|
2018-01-26 13:09:39 +00:00
|
|
|
int spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev);
|
2017-08-10 17:51:40 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Get underlying SPDK bdev from vhost blk device. The bdev might be NULL, as it
|
|
|
|
* could have been hotremoved.
|
|
|
|
*
|
2018-06-18 14:39:39 +00:00
|
|
|
* \param ctrlr vhost blk device.
|
2017-08-10 17:51:40 +00:00
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \return SPDK bdev associated with given vdev.
|
2017-08-10 17:51:40 +00:00
|
|
|
*/
|
2017-05-24 14:48:22 +00:00
|
|
|
struct spdk_bdev *spdk_vhost_blk_get_dev(struct spdk_vhost_dev *ctrlr);
|
2017-06-02 16:00:22 +00:00
|
|
|
|
2017-09-07 19:15:03 +00:00
|
|
|
/**
|
2018-03-01 06:35:06 +00:00
|
|
|
* Call function on reactor of given vhost device. If device is not in use, the
|
|
|
|
* event will be called right away on the caller's thread.
|
2017-09-07 19:15:03 +00:00
|
|
|
*
|
|
|
|
* This function is thread safe.
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param vdev_name name of the vhost device to run this event on.
|
|
|
|
* \param fn function to be called. The first parameter of callback function is
|
|
|
|
* either actual spdk_vhost_dev pointer or NULL in case vdev with given name doesn't
|
2017-09-07 19:15:03 +00:00
|
|
|
* exist. The second param is user provided argument *arg*.
|
|
|
|
* \param arg parameter to be passed to *fn*.
|
|
|
|
*/
|
2017-08-10 17:51:40 +00:00
|
|
|
void spdk_vhost_call_external_event(const char *vdev_name, spdk_vhost_event_fn fn, void *arg);
|
2017-09-07 19:15:03 +00:00
|
|
|
|
2017-08-08 16:52:53 +00:00
|
|
|
/**
|
2017-08-10 17:51:40 +00:00
|
|
|
* Call function for each available vhost device on
|
|
|
|
* it's reactor. This will call given function in a chain,
|
2017-08-08 16:52:53 +00:00
|
|
|
* meaning that each callback will be called after the
|
|
|
|
* previous one has finished. After given function has
|
2017-08-10 17:51:40 +00:00
|
|
|
* been called for all vdevs, it will be called once
|
|
|
|
* again with first param - vhost device- set to NULL.
|
2017-08-08 16:52:53 +00:00
|
|
|
*
|
|
|
|
* This function is thread safe.
|
|
|
|
*
|
2018-03-01 06:35:06 +00:00
|
|
|
* \param fn function to be called for each vdev. The first param will be
|
|
|
|
* either vdev pointer or NULL. The second param is user provided argument *arg*.
|
2017-08-08 16:52:53 +00:00
|
|
|
* \param arg parameter to be passed to *fn*.
|
|
|
|
*/
|
|
|
|
void spdk_vhost_call_external_event_foreach(spdk_vhost_event_fn fn, void *arg);
|
|
|
|
|
2017-12-07 20:25:19 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-02 14:12:20 +00:00
|
|
|
#endif /* SPDK_VHOST_H */
|