nvmf/vfio-user: optimize a few cases in nvmf_vfio_user_listen()
1. use the transport lock to protect transport endpoints list. 2. don't use mixed errno and -1 as the return value, use -1 for all error cases. Change-Id: I657fa06a6d82ee8dbeefaa3397df2285ba574b80 Signed-off-by: Changpeng Liu <changpeng.liu@intel.com> Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9579 Community-CI: Mellanox Build Bot Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com> Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com>
This commit is contained in:
parent
4b47b6e31c
commit
d3c9d3f3e5
@ -505,7 +505,9 @@ nvmf_vfio_user_destroy_endpoint(struct nvmf_vfio_user_endpoint *endpoint)
|
|||||||
close(endpoint->devmem_fd);
|
close(endpoint->devmem_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
vfu_destroy_ctx(endpoint->vfu_ctx);
|
if (endpoint->vfu_ctx) {
|
||||||
|
vfu_destroy_ctx(endpoint->vfu_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
pthread_mutex_destroy(&endpoint->lock);
|
pthread_mutex_destroy(&endpoint->lock);
|
||||||
free(endpoint);
|
free(endpoint);
|
||||||
@ -2045,85 +2047,94 @@ nvmf_vfio_user_listen(struct spdk_nvmf_transport *transport,
|
|||||||
{
|
{
|
||||||
struct nvmf_vfio_user_transport *vu_transport;
|
struct nvmf_vfio_user_transport *vu_transport;
|
||||||
struct nvmf_vfio_user_endpoint *endpoint, *tmp;
|
struct nvmf_vfio_user_endpoint *endpoint, *tmp;
|
||||||
char *path = NULL;
|
char path[PATH_MAX] = {};
|
||||||
char uuid[PATH_MAX] = {};
|
char uuid[PATH_MAX] = {};
|
||||||
int fd;
|
int ret;
|
||||||
int err;
|
|
||||||
|
|
||||||
vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
|
vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
|
||||||
transport);
|
transport);
|
||||||
|
|
||||||
|
pthread_mutex_lock(&vu_transport->lock);
|
||||||
TAILQ_FOREACH_SAFE(endpoint, &vu_transport->endpoints, link, tmp) {
|
TAILQ_FOREACH_SAFE(endpoint, &vu_transport->endpoints, link, tmp) {
|
||||||
/* Only compare traddr */
|
/* Only compare traddr */
|
||||||
if (strncmp(endpoint->trid.traddr, trid->traddr, sizeof(endpoint->trid.traddr)) == 0) {
|
if (strncmp(endpoint->trid.traddr, trid->traddr, sizeof(endpoint->trid.traddr)) == 0) {
|
||||||
|
pthread_mutex_unlock(&vu_transport->lock);
|
||||||
return -EEXIST;
|
return -EEXIST;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&vu_transport->lock);
|
||||||
|
|
||||||
endpoint = calloc(1, sizeof(*endpoint));
|
endpoint = calloc(1, sizeof(*endpoint));
|
||||||
if (!endpoint) {
|
if (!endpoint) {
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_init(&endpoint->lock, NULL);
|
||||||
endpoint->devmem_fd = -1;
|
endpoint->devmem_fd = -1;
|
||||||
memcpy(&endpoint->trid, trid, sizeof(endpoint->trid));
|
memcpy(&endpoint->trid, trid, sizeof(endpoint->trid));
|
||||||
|
|
||||||
err = asprintf(&path, "%s/bar0", endpoint_id(endpoint));
|
ret = snprintf(path, PATH_MAX, "%s/bar0", endpoint_id(endpoint));
|
||||||
if (err == -1) {
|
if (ret < 0 || ret >= PATH_MAX) {
|
||||||
|
SPDK_ERRLOG("%s: error to get socket path: %s.\n", endpoint_id(endpoint), spdk_strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
ret = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||||
if (fd == -1) {
|
if (ret == -1) {
|
||||||
SPDK_ERRLOG("%s: failed to open device memory at %s: %m\n",
|
SPDK_ERRLOG("%s: failed to open device memory at %s: %s.\n",
|
||||||
endpoint_id(endpoint), path);
|
endpoint_id(endpoint), path, spdk_strerror(errno));
|
||||||
err = fd;
|
|
||||||
free(path);
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
free(path);
|
|
||||||
|
|
||||||
endpoint->devmem_fd = fd;
|
endpoint->devmem_fd = ret;
|
||||||
err = ftruncate(fd, NVMF_VFIO_USER_DOORBELLS_OFFSET + NVMF_VFIO_USER_DOORBELLS_SIZE);
|
ret = ftruncate(endpoint->devmem_fd,
|
||||||
if (err != 0) {
|
NVMF_VFIO_USER_DOORBELLS_OFFSET + NVMF_VFIO_USER_DOORBELLS_SIZE);
|
||||||
|
if (ret != 0) {
|
||||||
|
SPDK_ERRLOG("%s: error to ftruncate file %s: %s.\n", endpoint_id(endpoint), path,
|
||||||
|
spdk_strerror(errno));
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint->doorbells = mmap(NULL, NVMF_VFIO_USER_DOORBELLS_SIZE,
|
endpoint->doorbells = mmap(NULL, NVMF_VFIO_USER_DOORBELLS_SIZE,
|
||||||
PROT_READ | PROT_WRITE, MAP_SHARED, fd, NVMF_VFIO_USER_DOORBELLS_OFFSET);
|
PROT_READ | PROT_WRITE, MAP_SHARED, endpoint->devmem_fd, NVMF_VFIO_USER_DOORBELLS_OFFSET);
|
||||||
if (endpoint->doorbells == MAP_FAILED) {
|
if (endpoint->doorbells == MAP_FAILED) {
|
||||||
|
SPDK_ERRLOG("%s: error to mmap file %s: %s.\n", endpoint_id(endpoint), path, spdk_strerror(errno));
|
||||||
endpoint->doorbells = NULL;
|
endpoint->doorbells = NULL;
|
||||||
err = -errno;
|
ret = -1;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(uuid, PATH_MAX, "%s/cntrl", endpoint_id(endpoint));
|
ret = snprintf(uuid, PATH_MAX, "%s/cntrl", endpoint_id(endpoint));
|
||||||
|
if (ret < 0 || ret >= PATH_MAX) {
|
||||||
|
SPDK_ERRLOG("%s: error to get ctrlr file path: %s\n", endpoint_id(endpoint), spdk_strerror(errno));
|
||||||
|
ret = -1;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
endpoint->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, uuid, LIBVFIO_USER_FLAG_ATTACH_NB,
|
endpoint->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, uuid, LIBVFIO_USER_FLAG_ATTACH_NB,
|
||||||
endpoint, VFU_DEV_TYPE_PCI);
|
endpoint, VFU_DEV_TYPE_PCI);
|
||||||
if (endpoint->vfu_ctx == NULL) {
|
if (endpoint->vfu_ctx == NULL) {
|
||||||
SPDK_ERRLOG("%s: error creating libmuser context: %m\n",
|
SPDK_ERRLOG("%s: error creating libmuser context: %m\n",
|
||||||
endpoint_id(endpoint));
|
endpoint_id(endpoint));
|
||||||
err = -1;
|
ret = -1;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
vfu_setup_log(endpoint->vfu_ctx, vfio_user_log, vfio_user_get_log_level());
|
vfu_setup_log(endpoint->vfu_ctx, vfio_user_log, vfio_user_get_log_level());
|
||||||
|
|
||||||
err = vfio_user_dev_info_fill(vu_transport, endpoint);
|
ret = vfio_user_dev_info_fill(vu_transport, endpoint);
|
||||||
if (err < 0) {
|
if (ret < 0) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_init(&endpoint->lock, NULL);
|
|
||||||
TAILQ_INSERT_TAIL(&vu_transport->endpoints, endpoint, link);
|
TAILQ_INSERT_TAIL(&vu_transport->endpoints, endpoint, link);
|
||||||
SPDK_DEBUGLOG(nvmf_vfio, "%s: doorbells %p\n", uuid, endpoint->doorbells);
|
SPDK_DEBUGLOG(nvmf_vfio, "%s: doorbells %p\n", uuid, endpoint->doorbells);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
if (err != 0) {
|
if (ret != 0) {
|
||||||
nvmf_vfio_user_destroy_endpoint(endpoint);
|
nvmf_vfio_user_destroy_endpoint(endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
Reference in New Issue
Block a user