From patchwork Fri Sep 11 15:08:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 77431 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 97A87A04BB; Fri, 11 Sep 2020 17:08:53 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 155ED1C11C; Fri, 11 Sep 2020 17:08:25 +0200 (CEST) Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by dpdk.org (Postfix) with ESMTP id 9F1D61C10A for ; Fri, 11 Sep 2020 17:08:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599836901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w5qwil9EsbcrtNICJvzjCKjKps/MQm2VHK60/brsOwo=; b=GA5by83zXYhFdZYIaz4oLAdNF8d1n7IwiXdpnRX/5uBVQoyWSI4buW+NObZ4Pq+Isv60mW spXbvTUAY0j1zG5VrbNSnA0kLLtGRJYsRV63CnfWbzePi9PbpLfzmr4QXbvL2h2OEKNpMb xfR4jIlaJLTJBML+RxJTnBn+gq7qUyE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-128-3M4KjLSVPZuDlFIqFIQ_PQ-1; Fri, 11 Sep 2020 11:08:19 -0400 X-MC-Unique: 3M4KjLSVPZuDlFIqFIQ_PQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 62FB1801E53; Fri, 11 Sep 2020 15:08:18 +0000 (UTC) Received: from localhost.localdomain (unknown [10.36.110.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1A39E3C89; Fri, 11 Sep 2020 15:08:16 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, patrick.fu@intel.com, amorenoz@redhat.com Cc: Maxime Coquelin Date: Fri, 11 Sep 2020 17:08:01 +0200 Message-Id: <20200911150805.79901-4-maxime.coquelin@redhat.com> In-Reply-To: <20200911150805.79901-1-maxime.coquelin@redhat.com> References: <20200911150805.79901-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=maxime.coquelin@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH 3/7] net/virtio: introduce Vhost-vDPA backend type X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Introduce virtio_user_backend_type function and its enum in order to support more than two backends with vDPA backend addition. Backend type is determined by checking the virtio-user control file information at runtime. Signed-off-by: Maxime Coquelin Signed-off-by: Adrian Moreno --- .../net/virtio/virtio_user/virtio_user_dev.c | 86 +++++++++++++++---- .../net/virtio/virtio_user/virtio_user_dev.h | 10 ++- drivers/net/virtio/virtio_user_ethdev.c | 3 +- 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c index 2a0c861085..93274b2a94 100644 --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -111,15 +113,55 @@ virtio_user_queue_setup(struct virtio_user_dev *dev, return 0; } -int -is_vhost_user_by_type(const char *path) +static uint32_t +vdpa_dynamic_major_num(void) +{ + FILE *fp; + char *line = NULL; + size_t size; + char name[11]; + bool found = false; + uint32_t num; + + fp = fopen("/proc/devices", "r"); + if (fp == NULL) { + PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s", + strerror(errno)); + return UNNAMED_MAJOR; + } + + while (getline(&line, &size, fp) > 0) { + char *stripped = line + strspn(line, " "); + if ((sscanf(stripped, "%u %10s", &num, name) == 2) && + (strncmp(name, "vhost-vdpa", 10) == 0)) { + found = true; + break; + } + } + fclose(fp); + return found ? num : UNNAMED_MAJOR; +} + +enum virtio_user_backend_type +virtio_user_backend_type(const char *path) { struct stat sb; - if (stat(path, &sb) == -1) - return 0; + if (stat(path, &sb) == -1) { + PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path, + strerror(errno)); + return VIRTIO_USER_BACKEND_UNKNOWN; + } - return S_ISSOCK(sb.st_mode); + if (S_ISSOCK(sb.st_mode)) { + return VIRTIO_USER_BACKEND_VHOST_USER; + } else if (S_ISCHR(sb.st_mode)) { + if (major(sb.st_rdev) == MISC_MAJOR) + return VIRTIO_USER_BACKEND_VHOST_KERNEL; + if (major(sb.st_rdev) == vdpa_dynamic_major_num()) + return VIRTIO_USER_BACKEND_VHOST_VDPA; + } + return VIRTIO_USER_BACKEND_UNKNOWN; } int @@ -144,7 +186,8 @@ virtio_user_start_device(struct virtio_user_dev *dev) rte_mcfg_mem_read_lock(); pthread_mutex_lock(&dev->mutex); - if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0) + if (virtio_user_backend_type(dev->path) == + VIRTIO_USER_BACKEND_VHOST_USER && dev->vhostfd < 0) goto error; /* Step 0: tell vhost to create queues */ @@ -359,17 +402,19 @@ virtio_user_dev_setup(struct virtio_user_dev *dev) dev->vhostfds = NULL; dev->tapfds = NULL; + dev->backend_type = virtio_user_backend_type(dev->path); + if (dev->is_server) { - if (access(dev->path, F_OK) == 0 && - !is_vhost_user_by_type(dev->path)) { - PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!"); + if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) { + PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!"); return -1; } dev->ops = &virtio_ops_user; } else { - if (is_vhost_user_by_type(dev->path)) { + if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) { dev->ops = &virtio_ops_user; - } else { + } else if (dev->backend_type == + VIRTIO_USER_BACKEND_VHOST_KERNEL) { dev->ops = &virtio_ops_kernel; dev->vhostfds = malloc(dev->max_queue_pairs * @@ -457,7 +502,8 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, return -1; } - if (!is_vhost_user_by_type(dev->path)) + if (virtio_user_backend_type(dev->path) != + VIRTIO_USER_BACKEND_VHOST_USER) dev->unsupported_features |= (1ULL << VHOST_USER_F_PROTOCOL_FEATURES); @@ -505,8 +551,6 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES; } - - if (!mrg_rxbuf) dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF); @@ -539,7 +583,8 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, } /* The backend will not report this feature, we add it explicitly */ - if (is_vhost_user_by_type(dev->path)) + if (virtio_user_backend_type(dev->path) == + VIRTIO_USER_BACKEND_VHOST_USER) dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS); /* @@ -790,9 +835,11 @@ virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status) { int ret; uint64_t arg = status; + enum virtio_user_backend_type backend_type = + virtio_user_backend_type(dev->path); /* Vhost-user only for now */ - if (!is_vhost_user_by_type(dev->path)) + if (backend_type != VIRTIO_USER_BACKEND_VHOST_USER) return 0; if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))) @@ -813,9 +860,11 @@ virtio_user_update_status(struct virtio_user_dev *dev) { uint64_t ret; int err; + enum virtio_user_backend_type backend_type = + virtio_user_backend_type(dev->path); /* Vhost-user only for now */ - if (!is_vhost_user_by_type(dev->path)) + if (backend_type != VIRTIO_USER_BACKEND_VHOST_USER) return 0; if (!(dev->protocol_features & (1UL << VHOST_USER_PROTOCOL_F_STATUS))) @@ -828,7 +877,8 @@ virtio_user_update_status(struct virtio_user_dev *dev) return -1; } if (ret > UINT8_MAX) { - PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS response 0x%" PRIx64 "\n", ret); + PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS " + "response 0x%" PRIx64 "\n", ret); return -1; } diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.h b/drivers/net/virtio/virtio_user/virtio_user_dev.h index 9377d5ba66..4bb7cdbd26 100644 --- a/drivers/net/virtio/virtio_user/virtio_user_dev.h +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.h @@ -10,6 +10,13 @@ #include "../virtio_pci.h" #include "../virtio_ring.h" +enum virtio_user_backend_type { + VIRTIO_USER_BACKEND_UNKNOWN, + VIRTIO_USER_BACKEND_VHOST_USER, + VIRTIO_USER_BACKEND_VHOST_KERNEL, + VIRTIO_USER_BACKEND_VHOST_VDPA, +}; + struct virtio_user_queue { uint16_t used_idx; bool avail_wrap_counter; @@ -17,6 +24,7 @@ struct virtio_user_queue { }; struct virtio_user_dev { + enum virtio_user_backend_type backend_type; /* for vhost_user backend */ int vhostfd; int listenfd; /* listening fd */ @@ -60,7 +68,7 @@ struct virtio_user_dev { bool started; }; -int is_vhost_user_by_type(const char *path); +enum virtio_user_backend_type virtio_user_backend_type(const char *path); int virtio_user_start_device(struct virtio_user_dev *dev); int virtio_user_stop_device(struct virtio_user_dev *dev); int virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c index 60d17af888..2163d0e30d 100644 --- a/drivers/net/virtio/virtio_user_ethdev.c +++ b/drivers/net/virtio/virtio_user_ethdev.c @@ -632,7 +632,8 @@ virtio_user_pmd_probe(struct rte_vdev_device *dev) } if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) { - if (is_vhost_user_by_type(path)) { + if (virtio_user_backend_type(path) != + VIRTIO_USER_BACKEND_VHOST_KERNEL) { PMD_INIT_LOG(ERR, "arg %s applies only to vhost-kernel backend", VIRTIO_USER_ARG_INTERFACE_NAME);