From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by dpdk.org (Postfix) with ESMTP id 63F927E79 for ; Wed, 10 Dec 2014 03:22:50 +0100 (CET) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP; 09 Dec 2014 18:22:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.07,549,1413270000"; d="scan'208";a="635320273" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by fmsmga001.fm.intel.com with ESMTP; 09 Dec 2014 18:22:48 -0800 Received: from shecgisg003.sh.intel.com (shecgisg003.sh.intel.com [10.239.29.90]) by shvmail01.sh.intel.com with ESMTP id sBA2MkpX003757; Wed, 10 Dec 2014 10:22:46 +0800 Received: from shecgisg003.sh.intel.com (localhost [127.0.0.1]) by shecgisg003.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id sBA2Mhjb004187; Wed, 10 Dec 2014 10:22:45 +0800 Received: (from dayuqiu@localhost) by shecgisg003.sh.intel.com (8.13.6/8.13.6/Submit) id sBA2Mg0w004183; Wed, 10 Dec 2014 10:22:42 +0800 From: Michael Qiu To: dev@dpdk.org Date: Wed, 10 Dec 2014 10:22:36 +0800 Message-Id: <1418178156-4153-1-git-send-email-michael.qiu@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1418035455-9434-1-git-send-email-michael.qiu@intel.com> References: <1418035455-9434-1-git-send-email-michael.qiu@intel.com> Subject: [dpdk-dev] [PATCH v5] 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: Wed, 10 Dec 2014 02:22:51 -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 --- v5 --> v4 1. Move rte_eal_check_module() body to eal.c 2. Clean up "unsupported IOMMU type" log v4 --> v3: 1. Remove RTE_LOG for params check 2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded v3 --> v2: 1. Add error log in rte_eal_check_module() 2. Some code clean up. 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. lib/librte_eal/common/eal_private.h | 15 +++++++++++++++ lib/librte_eal/linuxapp/eal/eal.c | 27 +++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26 +++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index 232fcec..4183b54 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -203,4 +203,19 @@ 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 some error happens(NULL pointer or open failure) + * 0 means the module not loaded + * 1 means the module loaded + */ +inline int +rte_eal_check_module(const char *module_name); + #endif /* _EAL_PRIVATE_H_ */ diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c index 89f3b5e..40b462e 100644 --- a/lib/librte_eal/linuxapp/eal/eal.c +++ b/lib/librte_eal/linuxapp/eal/eal.c @@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void) { return ! internal_config.no_hugetlbfs; } + +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 (NULL == fd) { + RTE_LOG(ERR, EAL, "Open /proc/modules failed!" + " error %i (%s)\n", errno, strerror(errno)); + return -1; + } + while(!feof(fd)) { + fscanf(fd, "%30s %*[^\n]", mod_name); + if(!strcmp(mod_name, module_name)) { + ret = 1; + break; + } + } + fclose(fd); + + return ret; +} diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c index c1246e8..16fe10f 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,12 @@ 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" + " detected in VFIO\n"); close(vfio_container_fd); return -1; } @@ -783,11 +786,28 @@ pci_vfio_enable(void) { /* initialize group list */ int i; + int module_vfio_type1; for (i = 0; i < VFIO_MAX_GROUPS; i++) { vfio_cfg.vfio_groups[i].fd = -1; vfio_cfg.vfio_groups[i].group_no = -1; } + + module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1"); + + /* return error directly */ + if (module_vfio_type1 == -1) { + RTE_LOG(INFO, EAL, "Could not get loaded module details!\n"); + return -1; + } + + /* return 0 if VFIO modules not loaded */ + if (module_vfio_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