[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 7/9] tools/libs/light: Make Libxl PCI get values from xl pcid instead of libxl side



From: Anastasiia Lukianenko <anastasiia_lukianenko@xxxxxxxx>

pci_multifunction_check needs to use "ls" and "lstat" commands to get
information from sysfs.
Add "is_exists" command processing to xl pcid daemon to make possible
pci_multifunction_check read directories and check lstat by using xl pcid.

Signed-off-by: Anastasiia Lukianenko <anastasiia_lukianenko@xxxxxxxx>
---
 tools/include/pcid.h          |  1 +
 tools/libs/light/libxl_pci.c  | 47 ++++++++++++++++++-----------------
 tools/libs/light/libxl_pcid.c | 47 +++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 23 deletions(-)

diff --git a/tools/include/pcid.h b/tools/include/pcid.h
index f39011ecb8..5c8efbb435 100644
--- a/tools/include/pcid.h
+++ b/tools/include/pcid.h
@@ -33,6 +33,7 @@
 
 #define PCID_CMD_WRITE           "write"
 #define PCID_CMD_READ_HEX        "read_hex"
+#define PCID_CMD_EXISTS          "exists"
 #define PCID_CMD_PCI_PATH        "pci_path"
 #define PCID_CMD_PCI_INFO        "pci_info"
 
diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c
index d5ddca4964..ab6709890e 100644
--- a/tools/libs/light/libxl_pci.c
+++ b/tools/libs/light/libxl_pci.c
@@ -1091,45 +1091,46 @@ int libxl_device_pci_assignable_remove(libxl_ctx *ctx, 
libxl_device_pci *pci,
 */
 static int pci_multifunction_check(libxl__gc *gc, libxl_device_pci *pci, 
unsigned int *func_mask)
 {
-    struct dirent *de;
-    DIR *dir;
-
     *func_mask = 0;
+    struct vchan_info *vchan;
+    libxl__json_object *result = NULL, *args = NULL;
+    const libxl__json_object *lstat_obj, *dir;
+    const char *dir_name;
+    int i;
 
-    dir = opendir(SYSFS_PCI_DEV);
-    if ( NULL == dir ) {
-        LOGE(ERROR, "Couldn't open %s", SYSFS_PCI_DEV);
+    vchan = pci_prepare_vchan(gc);
+    if (!vchan)
         return -1;
-    }
 
-    while( (de = readdir(dir)) ) {
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI_DEV);
+    result = vchan_send_command(gc, vchan, PCID_CMD_LIST, args);
+    if (!result)
+        return -1;
+
+    for (i = 0; (dir = libxl__json_array_get(result, i)); i++) {
+        dir_name = libxl__json_object_get_string(dir);
         unsigned dom, bus, dev, func;
-        struct stat st;
         char *path;
 
-        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
+        if (sscanf(dir_name, PCI_BDF, &dom, &bus, &dev, &func) != 4)
             continue;
-        if ( pci->domain != dom )
+        if (pci->domain != dom)
             continue;
-        if ( pci->bus != bus )
+        if (pci->bus != bus)
             continue;
-        if ( pci->dev != dev )
+        if (pci->dev != dev)
             continue;
 
-        path = GCSPRINTF("%s/" PCI_BDF, SYSFS_PCIBACK_DRIVER, dom, bus, dev, 
func);
-        if ( lstat(path, &st) ) {
-            if ( errno == ENOENT )
-                LOG(ERROR, PCI_BDF " is not assigned to pciback driver",
-                    dom, bus, dev, func);
-            else
-                LOGE(ERROR, "Couldn't lstat %s", path);
-            closedir(dir);
+        path = GCSPRINTF("/" PCI_BDF, dom, bus, dev, func);
+        libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, 
PCID_PCIBACK_DRIVER);
+        libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, path);
+        lstat_obj = vchan_send_command(gc, vchan, PCID_CMD_EXISTS, args);
+        if (!lstat_obj)
             return -1;
-        }
+
         (*func_mask) |= (1 << func);
     }
 
-    closedir(dir);
     return 0;
 }
 
diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c
index 0f736c68af..28d773f48d 100644
--- a/tools/libs/light/libxl_pcid.c
+++ b/tools/libs/light/libxl_pcid.c
@@ -34,6 +34,10 @@
 #include <libxl_json.h>
 #include <dirent.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
 #define DOM0_ID 0
 
 static struct libxl__json_object *process_ls_cmd(libxl__gc *gc,
@@ -202,6 +206,47 @@ out:
     return result;
 }
 
+static libxl__json_object *process_exists_cmd(libxl__gc *gc,
+                                              const struct libxl__json_object 
*resp)
+{
+    libxl__json_object *result = NULL;
+    const struct libxl__json_object *args, *pci_path, *pci_info;
+    char *full_path;
+    struct stat st;
+
+    args = libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP);
+    if (!args)
+        goto out;
+    pci_path = libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY);
+    if (!pci_path)
+        goto out;
+    pci_info = libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY);
+    if (!pci_info)
+        goto out;
+    if (strcmp(pci_path->u.string, PCID_PCIBACK_DRIVER) == 0)
+        full_path = libxl__sprintf(gc, SYSFS_PCIBACK_DRIVER"%s", 
pci_info->u.string);
+    else
+        full_path = pci_info->u.string;
+
+    if (lstat(full_path, &st)) {
+        if (errno == ENOENT)
+            LOGE(ERROR, "%s is not assigned to pciback driver", full_path);
+        else
+            LOGE(ERROR, "Couldn't lstat %s", full_path);
+        goto out;
+    }
+
+    result = libxl__json_object_alloc(gc, JSON_STRING);
+    if (!result) {
+        LOGE(ERROR, "Memory allocation failed\n");
+        goto out;
+    }
+    result->u.string = pci_path->u.string;
+
+out:
+    return result;
+}
+
 static int pcid_handle_message(libxl__gc *gc, const libxl__json_object 
*request,
                                libxl__json_object **result)
 {
@@ -221,6 +266,8 @@ static int pcid_handle_message(libxl__gc *gc, const 
libxl__json_object *request,
        *result = process_write_cmd(gc, request);
     else if (strcmp(command_name, PCID_CMD_READ_HEX) == 0)
         *result = process_read_hex_cmd(gc, request);
+    else if (strcmp(command_name, PCID_CMD_EXISTS) == 0)
+        *result = process_exists_cmd(gc, request);
     else
         return ERROR_NOTFOUND;
 
-- 
2.17.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.