From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pd0-f177.google.com (mail-pd0-f177.google.com [209.85.192.177]) by dpdk.org (Postfix) with ESMTP id A247A7DF4 for ; Fri, 5 Dec 2014 07:50:10 +0100 (CET) Received: by mail-pd0-f177.google.com with SMTP id ft15so111065pdb.22 for ; Thu, 04 Dec 2014 22:50:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tP8BFCJWvPv4Tul2ldZ2PIzyR5baTH4UQDvg7MWPYMg=; b=XQdJBR1F583NVxuZV2g+vp5XpfPVd25OKp3VulexZdwZfpCvjCMOw1NgW82z0r/ccE fVvUrXMoffiR+vT3GcYCR+p+iYPa7EQPxoax0/ittX7jmrTLiqPk/PzW5lxECbm7aQJw y2Pp8NU2+4tq+uIof2h3E7ZQ1R7kUrc7GoOQRx3lGHys5rrE+RqEfPZ35uw17REcpUKM lmP4/DjKLl3bRhC0kzGdnmPJbAF4immvYbIwwifc+yRPHs9Bv1MYoG/I62M/AVb/cRxK OG/fINzF6rxfhCJKjLUpMdAP/QQFdNCVxFAmcNmiQHrv7RJXX4ja8sizyg6dGOm6vAzJ uhSw== X-Received: by 10.68.57.225 with SMTP id l1mr32217202pbq.87.1417762209870; Thu, 04 Dec 2014 22:50:09 -0800 (PST) Received: from localhost.localdomain.localdomain ([180.99.188.137]) by mx.google.com with ESMTPSA id dk5sm27878980pbc.9.2014.12.04.22.50.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Dec 2014 22:50:09 -0800 (PST) From: Michael Qiu X-Google-Original-From: Michael Qiu To: dev@dpdk.org Date: Thu, 4 Dec 2014 18:00:27 +0800 Message-Id: <1417687227-21854-1-git-send-email-michael.qiu@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1417664219-19679-1-git-send-email-michael.qiu@intel.com> References: <1417664219-19679-1-git-send-email-michael.qiu@intel.com> Subject: [dpdk-dev] [PATCH v2] VFIO: Avoid to enable vfio while the module not loaded X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Dec 2014 06:50:11 -0000 When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description. This action is not safe, and of cause got error messages: EAL: Detected 40 lcore(s) EAL: unsupported IOMMU type! EAL: VFIO support could not be initialized EAL: Setting up memory... This may make user confuse, this patch make it reasonable and much more soomth to user. Signed-off-by: Michael Qiu --- v2 --> v1: 1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module(). To make it linuxapp only. 2. Some code clean up. Tested in host and guest, the below is results in VM: 1. VM | without patch | os: 3.11.10-301.fc20.x86_64 | VFIO not loaded EAL: Support maximum 64 logical core(s) by configuration. EAL: Detected 2 lcore(s) EAL: cannot open VFIO container, error 2 (No such file or directory) EAL: VFIO support could not be initialized EAL: Setting up memory... 2. VM | without patch | os: 3.11.10-301.fc20.x86_64 | VFIO loaded vfio_iommu_type1 not loaded modprobe vfio lsmod | grep "vfio" vfio 19626 0 modprobe vfio_iommu_type1 modprobe: ERROR: could not insert 'vfio_iommu_type1': No such device ls /dev/vfio/vfio /dev/vfio/vfio EAL: Support maximum 64 logical core(s) by configuration. EAL: Detected 2 lcore(s) EAL: unsupported IOMMU type! EAL: VFIO support could not be initialized EAL: Setting up memory... 3. VM | with patch | os: 3.11.10-301.fc20.x86_64 | VFIO(vfio_iommu_type1 never can be loaded inside VM) EAL: Detected 2 lcore(s) EAL: VFIO modules not all loaded, skip VFIO support ... EAL: Setting up memory... lib/librte_eal/common/eal_private.h | 36 ++++++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 24 +++++++++++++++++--- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index 232fcec..e741bdb 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -35,6 +35,7 @@ #define _EAL_PRIVATE_H_ #include +#include /** * Initialize the memzone subsystem (private to eal). @@ -203,4 +204,39 @@ int rte_eal_alarm_init(void); */ int rte_eal_dev_init(void); +/** + * Function is to check if the kernel module(like, vfio, vfio_iommu_type1, + * etc.) loaded. + * + * @param module_name + * The module's name which need to be checked + * + * @return + * -1 means error happens(NULL pointer or open failure) + * 0 means the module not loaded + * 1 means the module loaded + */ +static inline int +rte_eal_check_module(const char *module_name) +{ + char mod_name[30]; /* Any module names can be longer than 30 bytes? */ + int ret = 0; + + if (NULL == module_name) + return -1; + FILE * fd = fopen("/proc/modules", "r"); + if (fd == NULL) + return -1; + while(!feof(fd)) { + fscanf(fd, "%s %*[^\n]", mod_name); + if(!strcmp(mod_name, module_name)) { + ret = 1; + break; + } + } + fclose(fd); + + return ret; +} + #endif /* _EAL_PRIVATE_H_ */ diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c index c1246e8..52ab2d0 100644 --- a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +++ b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c @@ -44,6 +44,7 @@ #include #include #include +#include #include "eal_filesystem.h" #include "eal_pci_init.h" @@ -339,10 +340,13 @@ pci_vfio_get_container_fd(void) ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU); if (ret != 1) { if (ret < 0) - RTE_LOG(ERR, EAL, " could not get IOMMU type, " - "error %i (%s)\n", errno, strerror(errno)); + RTE_LOG(ERR, EAL, " could not get IOMMU type," + " error %i (%s)\n", errno, + strerror(errno)); else - RTE_LOG(ERR, EAL, " unsupported IOMMU type!\n"); + RTE_LOG(ERR, EAL, " unsupported IOMMU type! " + "expect: VFIO_TYPE1_IOMMU, " + "actual: %d\n", ret); close(vfio_container_fd); return -1; } @@ -788,6 +792,20 @@ pci_vfio_enable(void) vfio_cfg.vfio_groups[i].fd = -1; vfio_cfg.vfio_groups[i].group_no = -1; } + + /* return error directly */ + if (rte_eal_check_module("vfio") == -1 || + rte_eal_check_module("vfio_iommu_type1") == -1) + return -1; + + /* return 0 if not all VFIO modules loaded */ + if (rte_eal_check_module("vfio") == 0 || + rte_eal_check_module("vfio_iommu_type1") == 0) { + RTE_LOG(INFO, EAL, "VFIO modules not all loaded," + " skip VFIO support ...\n"); + return 0; + } + vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd(); /* check if we have VFIO driver enabled */ -- 1.9.3