Commit edaea916 authored by Darek Stojaczyk's avatar Darek Stojaczyk Committed by Jim Harris
Browse files

vhost: move vid to session struct



Each connection is created with the `new_connection`
rte_vhost callback with a unique vid parameter. Storing
the vid inside the device struct was sufficient until
we wanted to have multiple connections per device.

Change-Id: Ic730d3377e1410499bdc163ce961863c530b880d
Signed-off-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/437775


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
parent 73844ccc
Loading
Loading
Loading
Loading
+39 −37
Original line number Diff line number Diff line
@@ -108,7 +108,6 @@ static void
spdk_vhost_log_req_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_virtqueue *virtqueue,
			uint16_t req_id)
{
	struct spdk_vhost_dev *vdev = vsession->vdev;
	struct vring_desc *desc, *desc_table;
	uint32_t desc_table_size;
	int rc;
@@ -129,7 +128,7 @@ spdk_vhost_log_req_desc(struct spdk_vhost_session *vsession, struct spdk_vhost_v
			 * 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. */
			rte_vhost_log_write(vdev->vid, desc->addr, desc->len);
			rte_vhost_log_write(vsession->vid, desc->addr, desc->len);
		}
		spdk_vhost_vring_desc_get_next(&desc, desc_table, desc_table_size);
	} while (desc);
@@ -140,7 +139,6 @@ spdk_vhost_log_used_vring_elem(struct spdk_vhost_session *vsession,
			       struct spdk_vhost_virtqueue *virtqueue,
			       uint16_t idx)
{
	struct spdk_vhost_dev *vdev = vsession->vdev;
	uint64_t offset, len;
	uint16_t vq_idx;

@@ -152,14 +150,13 @@ spdk_vhost_log_used_vring_elem(struct spdk_vhost_session *vsession,
	len = sizeof(virtqueue->vring.used->ring[idx]);
	vq_idx = virtqueue - vsession->virtqueue;

	rte_vhost_log_used_vring(vdev->vid, vq_idx, offset, len);
	rte_vhost_log_used_vring(vsession->vid, vq_idx, offset, len);
}

static void
spdk_vhost_log_used_vring_idx(struct spdk_vhost_session *vsession,
			      struct spdk_vhost_virtqueue *virtqueue)
{
	struct spdk_vhost_dev *vdev = vsession->vdev;
	uint64_t offset, len;
	uint16_t vq_idx;

@@ -171,7 +168,7 @@ spdk_vhost_log_used_vring_idx(struct spdk_vhost_session *vsession,
	len = sizeof(virtqueue->vring.used->idx);
	vq_idx = virtqueue - vsession->virtqueue;

	rte_vhost_log_used_vring(vdev->vid, vq_idx, offset, len);
	rte_vhost_log_used_vring(vsession->vid, vq_idx, offset, len);
}

/*
@@ -513,14 +510,14 @@ spdk_vhost_dev_find_by_id(unsigned id)
	return NULL;
}

static struct spdk_vhost_dev *
spdk_vhost_dev_find_by_vid(int vid)
static struct spdk_vhost_session *
spdk_vhost_session_find_by_vid(int vid)
{
	struct spdk_vhost_dev *vdev;

	TAILQ_FOREACH(vdev, &g_spdk_vhost_devices, tailq) {
		if (vdev->vid == vid) {
			return vdev;
		if (vdev->session.vid == vid) {
			return &vdev->session;
		}
	}

@@ -753,7 +750,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->session.vid = -1;
	vdev->lcore = -1;
	vdev->cpumask = cpumask;
	vdev->registered = true;
@@ -778,7 +775,7 @@ out:
int
spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev)
{
	if (vdev->vid != -1) {
	if (vdev->session.vid != -1) {
		SPDK_ERRLOG("Controller %s has still valid connection.\n", vdev->name);
		return -EBUSY;
	}
@@ -1030,13 +1027,14 @@ stop_device(int vid)
	uint16_t i;

	pthread_mutex_lock(&g_spdk_vhost_mutex);
	vdev = spdk_vhost_dev_find_by_vid(vid);
	if (vdev == NULL) {
		SPDK_ERRLOG("Couldn't find device with vid %d to stop.\n", vid);
	vsession = spdk_vhost_session_find_by_vid(vid);
	if (vsession == NULL) {
		SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
		pthread_mutex_unlock(&g_spdk_vhost_mutex);
		return;
	}

	vdev = vsession->vdev;
	if (vdev->lcore == -1) {
		SPDK_ERRLOG("Controller %s is not loaded.\n", vdev->name);
		pthread_mutex_unlock(&g_spdk_vhost_mutex);
@@ -1050,13 +1048,12 @@ stop_device(int vid)
		return;
	}

	vsession = &vdev->session;
	for (i = 0; i < vsession->max_queues; i++) {
		q = &vsession->virtqueue[i].vring;
		if (q->desc == NULL) {
			continue;
		}
		rte_vhost_set_vhost_vring_last_idx(vdev->vid, i, q->last_avail_idx, q->last_used_idx);
		rte_vhost_set_vhost_vring_last_idx(vsession->vid, i, q->last_avail_idx, q->last_used_idx);
	}

	spdk_vhost_session_mem_unregister(vsession);
@@ -1076,19 +1073,18 @@ start_device(int vid)

	pthread_mutex_lock(&g_spdk_vhost_mutex);

	vdev = spdk_vhost_dev_find_by_vid(vid);
	if (vdev == NULL) {
		SPDK_ERRLOG("Controller with vid %d doesn't exist.\n", vid);
	vsession = spdk_vhost_session_find_by_vid(vid);
	if (vsession == NULL) {
		SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
		goto out;
	}

	vdev = vsession->vdev;
	if (vdev->lcore != -1) {
		SPDK_ERRLOG("Controller %s already loaded.\n", vdev->name);
		goto out;
	}

	vsession = &vdev->session;

	vsession->max_queues = 0;
	memset(vsession->virtqueue, 0, sizeof(vsession->virtqueue));
	for (i = 0; i < SPDK_VHOST_MAX_VQUEUES; i++) {
@@ -1153,16 +1149,18 @@ out:
static int
get_config(int vid, uint8_t *config, uint32_t len)
{
	struct spdk_vhost_session *vsession;
	struct spdk_vhost_dev *vdev;
	int rc = -1;

	pthread_mutex_lock(&g_spdk_vhost_mutex);
	vdev = spdk_vhost_dev_find_by_vid(vid);
	if (vdev == NULL) {
		SPDK_ERRLOG("Controller with vid %d doesn't exist.\n", vid);
	vsession = spdk_vhost_session_find_by_vid(vid);
	if (vsession == NULL) {
		SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
		goto out;
	}

	vdev = vsession->vdev;
	if (vdev->backend->vhost_get_config) {
		rc = vdev->backend->vhost_get_config(vdev, config, len);
	}
@@ -1175,16 +1173,18 @@ out:
static int
set_config(int vid, uint8_t *config, uint32_t offset, uint32_t size, uint32_t flags)
{
	struct spdk_vhost_session *vsession;
	struct spdk_vhost_dev *vdev;
	int rc = -1;

	pthread_mutex_lock(&g_spdk_vhost_mutex);
	vdev = spdk_vhost_dev_find_by_vid(vid);
	if (vdev == NULL) {
		SPDK_ERRLOG("Controller with vid %d doesn't exist.\n", vid);
	vsession = spdk_vhost_session_find_by_vid(vid);
	if (vsession == NULL) {
		SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
		goto out;
	}

	vdev = vsession->vdev;
	if (vdev->backend->vhost_set_config) {
		rc = vdev->backend->vhost_set_config(vdev, config, offset, size, flags);
	}
@@ -1250,6 +1250,7 @@ static int
new_connection(int vid)
{
	struct spdk_vhost_dev *vdev;
	struct spdk_vhost_session *vsession;
	char ifname[PATH_MAX];

	pthread_mutex_lock(&g_spdk_vhost_mutex);
@@ -1267,14 +1268,15 @@ new_connection(int vid)
	}

	/* since pollers are not running it safe not to use spdk_event here */
	if (vdev->vid != -1) {
		SPDK_ERRLOG("Device with vid %d is already connected.\n", vid);
	vsession = &vdev->session;
	if (vsession->vid != -1) {
		SPDK_ERRLOG("Session with vid %d already exists.\n", vid);
		pthread_mutex_unlock(&g_spdk_vhost_mutex);
		return -1;
	}

	vdev->vid = vid;
	vdev->session.vdev = vdev;
	vsession->vdev = vdev;
	vsession->vid = vid;
	pthread_mutex_unlock(&g_spdk_vhost_mutex);
	return 0;
}
@@ -1282,18 +1284,18 @@ new_connection(int vid)
static void
destroy_connection(int vid)
{
	struct spdk_vhost_dev *vdev;
	struct spdk_vhost_session *vsession;

	pthread_mutex_lock(&g_spdk_vhost_mutex);
	vdev = spdk_vhost_dev_find_by_vid(vid);
	if (vdev == NULL) {
		SPDK_ERRLOG("Couldn't find device with vid %d to destroy connection for.\n", vid);
	vsession = spdk_vhost_session_find_by_vid(vid);
	if (vsession == NULL) {
		SPDK_ERRLOG("Couldn't find session with vid %d.\n", vid);
		pthread_mutex_unlock(&g_spdk_vhost_mutex);
		return;
	}

	/* since pollers are not running it safe not to use spdk_event here */
	vdev->vid = -1;
	vsession->vid = -1;
	pthread_mutex_unlock(&g_spdk_vhost_mutex);
}

+3 −2
Original line number Diff line number Diff line
@@ -140,8 +140,6 @@ struct spdk_vhost_dev {
	/* Unique device ID. */
	unsigned id;

	/* rte_vhost device ID. */
	int vid;
	int32_t lcore;
	struct spdk_cpuset *cpumask;
	bool registered;
@@ -169,6 +167,9 @@ struct spdk_vhost_dev {
	struct spdk_vhost_session {
		struct spdk_vhost_dev *vdev;

		/* rte_vhost connection ID. */
		int vid;

		struct rte_vhost_memory *mem;

		int task_cnt;
+3 −3
Original line number Diff line number Diff line
@@ -893,7 +893,7 @@ spdk_vhost_nvme_get_by_name(int vid)
	struct spdk_vhost_nvme_dev *nvme;

	TAILQ_FOREACH(nvme, &g_nvme_ctrlrs, tailq) {
		if (nvme->vdev.vid == vid) {
		if (nvme->vdev.session.vid == vid) {
			return nvme;
		}
	}
@@ -1084,7 +1084,7 @@ spdk_vhost_nvme_start_device(struct spdk_vhost_dev *vdev, void *event_ctx)
		return -1;
	}

	SPDK_NOTICELOG("Start Device %u, Path %s, lcore %d\n", vdev->vid,
	SPDK_NOTICELOG("Start Device %u, Path %s, lcore %d\n", vdev->session.vid,
		       vdev->path, vdev->lcore);

	for (i = 0; i < nvme->num_ns; i++) {
@@ -1171,7 +1171,7 @@ spdk_vhost_nvme_stop_device(struct spdk_vhost_dev *vdev, void *event_ctx)
	}

	free_task_pool(nvme);
	SPDK_NOTICELOG("Stopping Device %u, Path %s\n", vdev->vid, vdev->path);
	SPDK_NOTICELOG("Stopping Device %u, Path %s\n", vdev->session.vid, vdev->path);

	nvme->destroy_ctx.event_ctx = event_ctx;
	spdk_poller_unregister(&nvme->requestq_poller);
+9 −8
Original line number Diff line number Diff line
@@ -163,8 +163,8 @@ start_vdev(struct spdk_vhost_dev *vdev)
	mem->regions[1].size = 0x400000; /* 4 MB */
	mem->regions[1].host_user_addr = 0x2000000;

	vdev->vid = 0;
	vdev->lcore = 0;
	vdev->session.vid = 0;
	vdev->session.mem = mem;
}

@@ -173,7 +173,7 @@ stop_vdev(struct spdk_vhost_dev *vdev)
{
	free(vdev->session.mem);
	vdev->session.mem = NULL;
	vdev->vid = -1;
	vdev->session.vid = -1;
}

static void
@@ -302,19 +302,20 @@ create_controller_test(void)
}

static void
dev_find_by_vid_test(void)
session_find_by_vid_test(void)
{
	struct spdk_vhost_dev *vdev, *tmp;
	struct spdk_vhost_dev *vdev;
	struct spdk_vhost_session *tmp;
	int rc;

	rc = alloc_vdev(&vdev, "vdev_name_0", "0x1");
	SPDK_CU_ASSERT_FATAL(rc == 0 && vdev);

	tmp = spdk_vhost_dev_find_by_vid(vdev->vid);
	CU_ASSERT(tmp == vdev);
	tmp = spdk_vhost_session_find_by_vid(vdev->session.vid);
	CU_ASSERT(tmp == &vdev->session);

	/* Search for a device with incorrect vid */
	tmp = spdk_vhost_dev_find_by_vid(vdev->vid + 0xFF);
	tmp = spdk_vhost_session_find_by_vid(vdev->session.vid + 0xFF);
	CU_ASSERT(tmp == NULL);

	cleanup_vdev(vdev);
@@ -356,7 +357,7 @@ main(int argc, char **argv)
	if (
		CU_add_test(suite, "desc_to_iov", desc_to_iov_test) == NULL ||
		CU_add_test(suite, "create_controller", create_controller_test) == NULL ||
		CU_add_test(suite, "dev_find_by_vid", dev_find_by_vid_test) == NULL ||
		CU_add_test(suite, "session_find_by_vid", session_find_by_vid_test) == NULL ||
		CU_add_test(suite, "remove_controller", remove_controller_test) == NULL
	) {
		CU_cleanup_registry();