vhost: abstract away vhost device array

Added vdev->id field and refactored the hot
vhost external event code operating on
g_spdk_vhost_devices array, so that we will
be able to switch from that array to a linked
list with unlimited capacity more easily.
This patch does not change any functionality
on its own.

We would very willingly reuse vdev->vid here,
but it's available only on devices with active
connections.

Change-Id: Ife911a6ca1531aade374b99ef4502cf77f691fa2
Signed-off-by: Slawomir Mrozowicz <slawomirx.mrozowicz@intel.com>
Signed-off-by: Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/389071
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Dariusz Stojaczyk 2018-02-09 19:47:35 +01:00 committed by Jim Harris
parent 66ced7deb0
commit cd0ef03b38
2 changed files with 40 additions and 38 deletions

View File

@ -53,7 +53,7 @@ struct spdk_vhost_dev_event_ctx {
/** Pointer to the controller obtained before enqueuing the event */
struct spdk_vhost_dev *vdev;
/** Index of the ctrlr to send event to. */
/** ID of the vdev to send event to. */
unsigned vdev_id;
/** User callback function to be executed on given lcore. */
@ -470,6 +470,22 @@ spdk_vhost_dev_has_feature(struct spdk_vhost_dev *vdev, unsigned feature_id)
return vdev->negotiated_features & (1ULL << feature_id);
}
static struct spdk_vhost_dev *
spdk_vhost_dev_find_by_id(unsigned id)
{
unsigned i;
struct spdk_vhost_dev *vdev;
for (i = 0; i < MAX_VHOST_DEVICES; i++) {
vdev = g_spdk_vhost_devices[i];
if (vdev && vdev->id == id) {
return vdev;
}
}
return NULL;
}
static struct spdk_vhost_dev *
spdk_vhost_dev_find_by_vid(int vid)
{
@ -704,6 +720,7 @@ spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const cha
vdev->name = strdup(name);
vdev->path = strdup(path);
vdev->id = ctrlr_num;
vdev->vid = -1;
vdev->lcore = -1;
vdev->cpumask = cpumask;
@ -736,24 +753,11 @@ out:
int
spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev)
{
unsigned ctrlr_num;
if (vdev->vid != -1) {
SPDK_ERRLOG("Controller %s has still valid connection.\n", vdev->name);
return -ENODEV;
}
for (ctrlr_num = 0; ctrlr_num < MAX_VHOST_DEVICES; ctrlr_num++) {
if (g_spdk_vhost_devices[ctrlr_num] == vdev) {
break;
}
}
if (ctrlr_num == MAX_VHOST_DEVICES) {
SPDK_ERRLOG("Trying to remove invalid controller: %s.\n", vdev->name);
return -ENOSPC;
}
if (vdev->registered && rte_vhost_driver_unregister(vdev->path) != 0) {
SPDK_ERRLOG("Could not unregister controller %s with vhost library\n"
"Check if domain socket %s still exists\n",
@ -766,22 +770,20 @@ spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev)
free(vdev->name);
free(vdev->path);
spdk_cpuset_free(vdev->cpumask);
g_spdk_vhost_devices[ctrlr_num] = NULL;
g_spdk_vhost_devices[vdev->id] = NULL;
return 0;
}
static int
static struct spdk_vhost_dev *
spdk_vhost_dev_next(int i)
{
for (i++; i < MAX_VHOST_DEVICES; i++) {
if (g_spdk_vhost_devices[i] == NULL) {
continue;
if (g_spdk_vhost_devices[i]) {
return g_spdk_vhost_devices[i];
}
return i;
}
return -1;
return NULL;
}
const char *
@ -853,7 +855,7 @@ spdk_vhost_event_async_fn(void *arg1, void *arg2)
return;
}
vdev = g_spdk_vhost_devices[ctx->vdev_id];
vdev = spdk_vhost_dev_find_by_id(ctx->vdev_id);
if (vdev != ctx->vdev) {
/* vdev has been changed after enqueuing this event */
vdev = NULL;
@ -882,7 +884,7 @@ spdk_vhost_event_async_foreach_fn(void *arg1, void *arg2)
return;
}
vdev = g_spdk_vhost_devices[ctx->vdev_id];
vdev = spdk_vhost_dev_find_by_id(ctx->vdev_id);
if (vdev == ctx->vdev) {
ctx->cb_fn(vdev, arg2);
}
@ -929,7 +931,7 @@ spdk_vhost_event_send(struct spdk_vhost_dev *vdev, spdk_vhost_event_fn cb_fn,
}
static int
spdk_vhost_event_async_send(unsigned vdev_id, spdk_vhost_event_fn cb_fn, void *arg,
spdk_vhost_event_async_send(struct spdk_vhost_dev *vdev, spdk_vhost_event_fn cb_fn, void *arg,
bool foreach)
{
struct spdk_vhost_dev_event_ctx *ev_ctx;
@ -942,8 +944,8 @@ spdk_vhost_event_async_send(unsigned vdev_id, spdk_vhost_event_fn cb_fn, void *a
return -ENOMEM;
}
ev_ctx->vdev = g_spdk_vhost_devices[vdev_id];
ev_ctx->vdev_id = vdev_id;
ev_ctx->vdev = vdev;
ev_ctx->vdev_id = vdev->id;
ev_ctx->cb_fn = cb_fn;
fn = foreach ? spdk_vhost_event_async_foreach_fn : spdk_vhost_event_async_fn;
@ -1251,22 +1253,20 @@ void
spdk_vhost_call_external_event(const char *ctrlr_name, spdk_vhost_event_fn fn, void *arg)
{
struct spdk_vhost_dev *vdev;
int vdev_id;
pthread_mutex_lock(&g_spdk_vhost_mutex);
vdev_id = spdk_vhost_dev_find_id(ctrlr_name);
vdev = spdk_vhost_dev_find(ctrlr_name);
if (vdev_id == -1) {
if (vdev == NULL) {
pthread_mutex_unlock(&g_spdk_vhost_mutex);
fn(NULL, arg);
return;
}
vdev = g_spdk_vhost_devices[vdev_id];
if (vdev->lcore == -1) {
fn(vdev, arg);
} else {
spdk_vhost_event_async_send(vdev_id, fn, arg, false);
spdk_vhost_event_async_send(vdev, fn, arg, false);
}
pthread_mutex_unlock(&g_spdk_vhost_mutex);
@ -1277,24 +1277,22 @@ spdk_vhost_external_event_foreach_continue(int vdev_id, spdk_vhost_event_fn fn,
{
struct spdk_vhost_dev *vdev;
vdev_id = spdk_vhost_dev_next(vdev_id);
if (vdev_id == -1) {
vdev = spdk_vhost_dev_next(vdev_id);
if (vdev == NULL) {
fn(NULL, arg);
return;
}
vdev = g_spdk_vhost_devices[vdev_id];
while (vdev->lcore == -1) {
fn(vdev, arg);
vdev_id = spdk_vhost_dev_next(vdev_id);
if (vdev_id == -1) {
vdev = spdk_vhost_dev_next(vdev->id);
if (vdev == NULL) {
fn(NULL, arg);
return;
}
vdev = g_spdk_vhost_devices[vdev_id];
}
spdk_vhost_event_async_send(vdev_id, fn, arg, true);
spdk_vhost_event_async_send(vdev, fn, arg, true);
}
void

View File

@ -137,6 +137,10 @@ struct spdk_vhost_dev {
char *name;
char *path;
/* Unique device ID. */
unsigned id;
/* rte_vhost device ID. */
int vid;
int task_cnt;
int32_t lcore;