From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pd0-f176.google.com (mail-pd0-f176.google.com [209.85.192.176]) by dpdk.org (Postfix) with ESMTP id 2B0A18045 for ; Thu, 4 Dec 2014 13:55:13 +0100 (CET) Received: by mail-pd0-f176.google.com with SMTP id y10so17591075pdj.35 for ; Thu, 04 Dec 2014 04:55:12 -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; bh=+10/NFMnYDXnjcPM36J2OL4p8oPH2QZvcDyZkYb/Kdg=; b=tKxFEH5CQgtxrVSZH9cXbBo58Z7wapG0LrZQiF1cWdoL0eM6hQgJ/LdgYXTJQJFNZN +Tl7Z8/ilP98XmbgOZbd2XIvRC/He2kF24CGHYStPJoNkNI9+k9v9+5EsD3pYeOSlL57 tzvw8jDuilJ86UGWb4c9rJGavAJG/79Mtae83ypgqUoyVm+e8e+ecHYvPP0ysk8dihBs GDGP3tIAFuX41F+Hu8IT5Y1GvQmrS51cnQRY2CfuFGXmYfN3A0d4zHrazsQ0kaMd3sf3 OYQUTKUO5rcfWzZkHIRZsHHjdc73oNPlFRc0aqMjM8mJ6MOIrYioEvD8ofJEY8+fu3WD D5Jw== X-Received: by 10.70.94.168 with SMTP id dd8mr18353956pdb.76.1417697712247; Thu, 04 Dec 2014 04:55:12 -0800 (PST) Received: from localhost.localdomain.localdomain ([180.99.189.223]) by mx.google.com with ESMTPSA id be3sm9645200pbc.33.2014.12.04.04.55.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Dec 2014 04:55:11 -0800 (PST) From: Michael Qiu X-Google-Original-From: Michael Qiu To: dev@dpdk.org Date: Thu, 4 Dec 2014 11:36:59 +0800 Message-Id: <1417664219-19679-1-git-send-email-michael.qiu@intel.com> X-Mailer: git-send-email 1.9.3 Subject: [dpdk-dev] [PATCH] 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: Thu, 04 Dec 2014 12:55:13 -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 --- lib/librte_eal/common/include/rte_common.h | 37 ++++++++++++++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 23 +++++++++++++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index 921b91f..333aa6b 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -50,6 +50,8 @@ extern "C" { #include #include #include +#include +#include /*********** Macros to eliminate unused variable warnings ********/ @@ -382,6 +384,41 @@ rte_exit(int exit_code, const char *format, ...) __attribute__((noreturn)) __attribute__((format(printf, 2, 3))); +/** + * 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 + */ +static inline int +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; +} + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c index c1246e8..a11cc4b 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" @@ -342,7 +343,8 @@ pci_vfio_get_container_fd(void) 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: 1, actual: %d\n", ret); close(vfio_container_fd); return -1; } @@ -788,13 +790,20 @@ pci_vfio_enable(void) vfio_cfg.vfio_groups[i].fd = -1; vfio_cfg.vfio_groups[i].group_no = -1; } - vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd(); - /* check if we have VFIO driver enabled */ - if (vfio_cfg.vfio_container_fd != -1) - vfio_cfg.vfio_enabled = 1; - else - RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n"); + if (check_module("vfio") == 1 && + check_module("vfio_iommu_type1") == 1) { + vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd(); + + /* check if we have VFIO driver enabled */ + if (vfio_cfg.vfio_container_fd != -1) + vfio_cfg.vfio_enabled = 1; + else + RTE_LOG(INFO, EAL, "VFIO support could not be" + " initialized\n"); + } else + RTE_LOG(INFO, EAL, "VFIO modules are not all loaded," + " skip VFIO support ...\n"); return 0; } -- 1.9.3