4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "spdk/stdinc.h"
44 #include "spdk/cpuset.h"
45 #include "spdk/json.h"
46 #include "spdk/thread.h"
53 * Callback for spdk_vhost_init().
55 * \param rc 0 on success, negative errno on failure
57 typedef void (*spdk_vhost_init_cb
)(int rc
);
59 /** Callback for spdk_vhost_fini(). */
60 typedef void (*spdk_vhost_fini_cb
)(void);
63 * Set the path to the directory where vhost sockets will be created.
65 * This function must be called before spdk_vhost_init().
67 * \param basename Path to vhost socket directory
69 * \return 0 on success, negative errno on error.
71 int spdk_vhost_set_socket_path(const char *basename
);
74 * Init vhost environment.
76 * \param init_cb Function to be called when the initialization is complete.
78 void spdk_vhost_init(spdk_vhost_init_cb init_cb
);
81 * Clean up the environment of vhost.
83 * \param fini_cb Function to be called when the cleanup is complete.
85 void spdk_vhost_fini(spdk_vhost_fini_cb fini_cb
);
89 * Write vhost subsystem configuration into provided JSON context.
91 * \param w JSON write context
93 void spdk_vhost_config_json(struct spdk_json_write_ctx
*w
);
96 * Deinit vhost application. This is called once by SPDK app layer.
98 void spdk_vhost_shutdown_cb(void);
101 * SPDK vhost device (vdev). An equivalent of Virtio device.
102 * Both virtio-blk and virtio-scsi devices are represented by this
103 * struct. For virtio-scsi a single vhost device (also called SCSI
104 * controller) may contain multiple SCSI targets (devices), each of
105 * which may contain multiple logical units (SCSI LUNs). For now
106 * only one LUN per target is available.
108 * All vdev-changing functions operate directly on this object.
109 * Note that \c spdk_vhost_dev cannot be acquired. This object is
110 * only accessible as a callback parameter via \c
111 * spdk_vhost_call_external_event and it's derivatives. This ensures
112 * that all access to the vdev is piped through a single,
115 struct spdk_vhost_dev
;
118 * Lock the global vhost mutex synchronizing all the vhost device accesses.
120 void spdk_vhost_lock(void);
123 * Lock the global vhost mutex synchronizing all the vhost device accesses.
125 * \return 0 if the mutex could be locked immediately, negative errno otherwise.
127 int spdk_vhost_trylock(void);
130 * Unlock the global vhost mutex.
132 void spdk_vhost_unlock(void);
135 * Find a vhost device by name.
137 * \return vhost device or NULL
139 struct spdk_vhost_dev
*spdk_vhost_dev_find(const char *name
);
142 * Get the next vhost device. If there's no more devices to iterate
143 * through, NULL will be returned.
145 * \param vdev vhost device. If NULL, this function will return the
147 * \return vdev vhost device or NULL
149 struct spdk_vhost_dev
*spdk_vhost_dev_next(struct spdk_vhost_dev
*vdev
);
152 * Synchronized vhost event used for user callbacks.
154 * \param vdev vhost device.
155 * \param arg user-provided parameter.
157 * \return 0 on success, -1 on failure.
159 typedef int (*spdk_vhost_event_fn
)(struct spdk_vhost_dev
*vdev
, void *arg
);
162 * Get the name of the vhost device. This is equal to the filename
163 * of socket file. The name is constant throughout the lifetime of
166 * \param vdev vhost device.
168 * \return name of the vdev.
170 const char *spdk_vhost_dev_get_name(struct spdk_vhost_dev
*vdev
);
173 * Get cpuset of the vhost device. The cpuset is constant throughout the lifetime
174 * of a vdev. It is a subset of SPDK app cpuset vhost was started with.
176 * \param vdev vhost device.
178 * \return cpuset of the vdev.
180 const struct spdk_cpuset
*spdk_vhost_dev_get_cpumask(struct spdk_vhost_dev
*vdev
);
183 * By default, events are generated when asked, but for high queue depth and
184 * high IOPS this prove to be inefficient both for guest kernel that have to
185 * handle a lot more IO completions and for SPDK vhost that need to make more
186 * syscalls. If enabled, limit amount of events (IRQs) sent to initiator by SPDK
187 * vhost effectively coalescing couple of completions. This of cource introduce
188 * IO latency penalty proportional to event delay time.
190 * Actual events delay time when is calculated according to below formula:
191 * if (delay_base == 0 || IOPS < iops_threshold) {
193 * } else if (IOPS < iops_threshold) {
194 * delay = delay_base * (iops - iops_threshold) / iops_threshold;
197 * \param vdev vhost device.
198 * \param delay_base_us Base delay time in microseconds. If 0, coalescing is disabled.
199 * \param iops_threshold IOPS threshold when coalescing is activated.
201 int spdk_vhost_set_coalescing(struct spdk_vhost_dev
*vdev
, uint32_t delay_base_us
,
202 uint32_t iops_threshold
);
205 * Get coalescing parameters.
207 * \see spdk_vhost_set_coalescing
209 * \param vdev vhost device.
210 * \param delay_base_us Optional pointer to store base delay time.
211 * \param iops_threshold Optional pointer to store IOPS threshold.
213 void spdk_vhost_get_coalescing(struct spdk_vhost_dev
*vdev
, uint32_t *delay_base_us
,
214 uint32_t *iops_threshold
);
217 * Construct an empty vhost SCSI device. This will create a
218 * Unix domain socket together with a vhost-user slave server waiting
219 * for a connection on this socket. Creating the vdev does not
220 * start any I/O pollers and does not hog the CPU. I/O processing
221 * starts after receiving proper message on the created socket.
222 * See QEMU's vhost-user documentation for details.
223 * All physical devices have to be separately attached to this
224 * vdev via \c spdk_vhost_scsi_dev_add_tgt().
226 * This function is thread-safe.
228 * \param name name of the vhost device. The name will also be used
229 * for socket name, which is exactly \c socket_base_dir/name
230 * \param cpumask string containing cpumask in hex. The leading *0x*
231 * is allowed but not required. The mask itself can be constructed as:
232 * ((1 << cpu0) | (1 << cpu1) | ... | (1 << cpuN)).
234 * \return 0 on success, negative errno on error.
236 int spdk_vhost_scsi_dev_construct(const char *name
, const char *cpumask
);
239 * Construct and attach new SCSI target to the vhost SCSI device
240 * on given (unoccupied) slot. The device will be created with a single
241 * LUN0 associated with given SPDK bdev. Currently only one LUN per
242 * device is supported.
244 * If the vhost SCSI device has an active connection and has negotiated
245 * \c VIRTIO_SCSI_F_HOTPLUG feature, the new SCSI target should be
246 * automatically detected by the other side.
248 * \param vdev vhost SCSI device.
249 * \param scsi_tgt_num slot to attach to or negative value to use first free.
250 * \param bdev_name name of the SPDK bdev to associate with SCSI LUN0.
252 * \return value >= 0 on success - the SCSI target ID, negative errno code:
253 * -EINVAL - one of the arguments is invalid:
254 * - vdev is not vhost SCSI device
255 * - SCSI target ID is out of range
256 * - bdev name is NULL
257 * - can't create SCSI LUN because of other errors e.g.: bdev does not exist
258 * -ENOSPC - scsi_tgt_num is -1 and maximum targets in vhost SCSI device reached
259 * -EEXIST - SCSI target ID already exists
261 int spdk_vhost_scsi_dev_add_tgt(struct spdk_vhost_dev
*vdev
, int scsi_tgt_num
,
262 const char *bdev_name
);
265 * Get SCSI target from vhost SCSI device on given slot. Max
266 * number of available slots is defined by.
267 * \c SPDK_VHOST_SCSI_CTRLR_MAX_DEVS.
269 * \param vdev vhost SCSI device.
270 * \param num slot id.
272 * \return SCSI device on given slot or NULL.
274 struct spdk_scsi_dev
*spdk_vhost_scsi_dev_get_tgt(struct spdk_vhost_dev
*vdev
, uint8_t num
);
277 * Detach and destruct SCSI target from a vhost SCSI device.
279 * The device will be deleted after all pending I/O is finished.
280 * If the driver supports VIRTIO_SCSI_F_HOTPLUG, then a hotremove
281 * notification will be sent.
283 * \param vdev vhost SCSI device
284 * \param scsi_tgt_num slot id to delete target from
285 * \param cb_fn callback to be fired once target has been successfully
286 * deleted. The first parameter of callback function is the vhost SCSI
287 * device, the second is user provided argument *cb_arg*.
288 * \param cb_arg parameter to be passed to *cb_fn*.
290 * \return 0 on success, negative errno on error.
292 int spdk_vhost_scsi_dev_remove_tgt(struct spdk_vhost_dev
*vdev
, unsigned scsi_tgt_num
,
293 spdk_vhost_event_fn cb_fn
, void *cb_arg
);
296 * Construct a vhost blk device. This will create a Unix domain
297 * socket together with a vhost-user slave server waiting for a
298 * connection on this socket. Creating the vdev does not start
299 * any I/O pollers and does not hog the CPU. I/O processing starts
300 * after receiving proper message on the created socket.
301 * See QEMU's vhost-user documentation for details. Vhost blk
302 * device is tightly associated with given SPDK bdev. Given
303 * bdev can not be changed, unless it has been hotremoved. This
304 * would result in all I/O failing with virtio \c VIRTIO_BLK_S_IOERR
307 * This function is thread-safe.
309 * \param name name of the vhost blk device. The name will also be
310 * used for socket name, which is exactly \c socket_base_dir/name
311 * \param cpumask string containing cpumask in hex. The leading *0x*
312 * is allowed but not required. The mask itself can be constructed as:
313 * ((1 << cpu0) | (1 << cpu1) | ... | (1 << cpuN)).
314 * \param dev_name bdev name to associate with this vhost device
315 * \param readonly if set, all writes to the device will fail with
316 * \c VIRTIO_BLK_S_IOERR error code.
317 * \param packed_ring this controller supports packed ring if set.
319 * \return 0 on success, negative errno on error.
321 int spdk_vhost_blk_construct(const char *name
, const char *cpumask
, const char *dev_name
,
322 bool readonly
, bool packed_ring
);
325 * Remove a vhost device. The device must not have any open connections on it's socket.
327 * \param vdev vhost blk device.
329 * \return 0 on success, negative errno on error.
331 int spdk_vhost_dev_remove(struct spdk_vhost_dev
*vdev
);
337 #endif /* SPDK_VHOST_H */