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

[PATCH 6/9] tools/libs/light: Add "read" command to xl pcid and libxl PCI



From: Anastasiia Lukianenko <anastasiia_lukianenko@xxxxxxxx>

Libxl PCI can use xl pcid server to read integer value from the given path.

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

diff --git a/tools/include/pcid.h b/tools/include/pcid.h
index 935d99b186..f39011ecb8 100644
--- a/tools/include/pcid.h
+++ b/tools/include/pcid.h
@@ -32,6 +32,7 @@
 #define PCID_CMD_DIR_ID          "dir_id"
 
 #define PCID_CMD_WRITE           "write"
+#define PCID_CMD_READ_HEX        "read_hex"
 #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 03ce42dec3..d5ddca4964 100644
--- a/tools/libs/light/libxl_pci.c
+++ b/tools/libs/light/libxl_pci.c
@@ -634,83 +634,94 @@ static int sysfs_dev_unbind(libxl__gc *gc, 
libxl_device_pci *pci,
 static uint16_t sysfs_dev_get_vendor(libxl__gc *gc, libxl_device_pci *pci)
 {
     char *pci_device_vendor_path =
-            GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/vendor",
-                      pci->domain, pci->bus, pci->dev, pci->func);
-    uint16_t read_items;
+            GCSPRINTF("/"PCI_BDF"/vendor", pci->domain, pci->bus,
+                      pci->dev, pci->func);
     uint16_t pci_device_vendor;
+    struct vchan_info *vchan;
+    libxl__json_object *args = NULL, *result = NULL;
 
-    FILE *f = fopen(pci_device_vendor_path, "r");
-    if (!f) {
-        LOGE(ERROR,
-             "pci device "PCI_BDF" does not have vendor attribute",
-             pci->domain, pci->bus, pci->dev, pci->func);
-        return 0xffff;
-    }
-    read_items = fscanf(f, "0x%hx\n", &pci_device_vendor);
-    fclose(f);
-    if (read_items != 1) {
-        LOGE(ERROR,
-             "cannot read vendor of pci device "PCI_BDF,
-             pci->domain, pci->bus, pci->dev, pci->func);
-        return 0xffff;
-    }
+    vchan = pci_prepare_vchan(gc);
+    if (!vchan)
+        goto fail;
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO,
+                                  pci_device_vendor_path);
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID,
+                                  PCID_PCI_DEV);
+    result = vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args);
+    if (!result)
+        goto fail;
+
+    pci_device_vendor = libxl__json_object_get_integer(result);
 
     return pci_device_vendor;
+
+fail:
+    LOGE(ERROR,
+         "cannot read vendor of pci device "PCI_BDF,
+         pci->domain, pci->bus, pci->dev, pci->func);
+    return 0xffff;
 }
 
 static uint16_t sysfs_dev_get_device(libxl__gc *gc, libxl_device_pci *pci)
 {
     char *pci_device_device_path =
-            GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/device",
-                      pci->domain, pci->bus, pci->dev, pci->func);
-    uint16_t read_items;
+            GCSPRINTF("/"PCI_BDF"/device", pci->domain, pci->bus,
+                      pci->dev, pci->func);
     uint16_t pci_device_device;
+    struct vchan_info *vchan;
+    libxl__json_object *args = NULL, *result = NULL;
 
-    FILE *f = fopen(pci_device_device_path, "r");
-    if (!f) {
-        LOGE(ERROR,
-             "pci device "PCI_BDF" does not have device attribute",
-             pci->domain, pci->bus, pci->dev, pci->func);
-        return 0xffff;
-    }
-    read_items = fscanf(f, "0x%hx\n", &pci_device_device);
-    fclose(f);
-    if (read_items != 1) {
-        LOGE(ERROR,
-             "cannot read device of pci device "PCI_BDF,
-             pci->domain, pci->bus, pci->dev, pci->func);
-        return 0xffff;
-    }
+    vchan = pci_prepare_vchan(gc);
+    if (!vchan)
+        goto fail;
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO,
+                                  pci_device_device_path);
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID,
+                                  PCID_PCI_DEV);
+    result = vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args);
+    if (!result)
+        goto fail;
+
+    pci_device_device = libxl__json_object_get_integer(result);
 
     return pci_device_device;
+
+fail:
+    LOGE(ERROR,
+         "cannot read device of pci device "PCI_BDF,
+         pci->domain, pci->bus, pci->dev, pci->func);
+    return 0xffff;
 }
 
 static int sysfs_dev_get_class(libxl__gc *gc, libxl_device_pci *pci,
                                unsigned long *class)
 {
-    char *pci_device_class_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/class",
-                     pci->domain, pci->bus, pci->dev, pci->func);
-    int read_items, ret = 0;
+    char *pci_device_class_path = GCSPRINTF("/"PCI_BDF"/class",
+                                            pci->domain, pci->bus,
+                                            pci->dev, pci->func);
+    struct vchan_info *vchan;
+    libxl__json_object *args = NULL, *result = NULL;
 
-    FILE *f = fopen(pci_device_class_path, "r");
-    if (!f) {
-        LOGE(ERROR,
-             "pci device "PCI_BDF" does not have class attribute",
-             pci->domain, pci->bus, pci->dev, pci->func);
-        ret = ERROR_FAIL;
-        goto out;
-    }
-    read_items = fscanf(f, "0x%lx\n", class);
-    fclose(f);
-    if (read_items != 1) {
-        LOGE(ERROR,
-             "cannot read class of pci device "PCI_BDF,
-             pci->domain, pci->bus, pci->dev, pci->func);
-        ret = ERROR_FAIL;
-    }
+    vchan = pci_prepare_vchan(gc);
+    if (!vchan)
+        goto fail;
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO,
+                                  pci_device_class_path);
+    libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID,
+                                  PCID_PCI_DEV);
+    result = vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args);
+    if (!result)
+        goto fail;
 
-out:
-    return ret;
+    *class = libxl__json_object_get_integer(result);
+
+    return 0;
+
+fail:
+    LOGE(ERROR,
+         "cannot read class of pci device "PCI_BDF,
+         pci->domain, pci->bus, pci->dev, pci->func);
+    return ERROR_FAIL;
 }
 
 /*
diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c
index ee4c832779..0f736c68af 100644
--- a/tools/libs/light/libxl_pcid.c
+++ b/tools/libs/light/libxl_pcid.c
@@ -154,6 +154,54 @@ out:
     return result;
 }
 
+static libxl__json_object *process_read_hex_cmd(libxl__gc *gc,
+                                                const struct 
libxl__json_object *resp)
+{
+    libxl__json_object *result = NULL;
+    const struct libxl__json_object *args, *dir_id, *pci_info;
+    char *full_path;
+    uint16_t read_items;
+    long long read_number;
+
+    args = libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP);
+    if (!args)
+        goto out;
+    dir_id = libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY);
+    if (!dir_id)
+        goto out;
+    pci_info = libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY);
+    if (!pci_info)
+        goto out;
+
+    if (strcmp(PCID_PCI_DEV, dir_id->u.string) == 0)
+        full_path = libxl__sprintf(gc, SYSFS_PCI_DEV"%s", pci_info->u.string);
+    else
+        full_path = pci_info->u.string;
+
+    FILE *f = fopen(full_path, "r");
+    if (!f) {
+        LOGE(ERROR, "PCI device %s does not have needed attribute\n",
+                full_path);
+        goto out;
+    }
+    read_items = fscanf(f, "0x%llx\n", &read_number);
+    fclose(f);
+    if (read_items != 1) {
+        LOGE(ERROR, "Cannot read attribute of pci device %s\n", full_path);
+        goto out;
+    }
+
+    result = libxl__json_object_alloc(gc, JSON_INTEGER);
+    if (!result) {
+        LOGE(ERROR, "Memory allocation failed\n");
+        goto out;
+    }
+    result->u.i = read_number;
+
+out:
+    return result;
+}
+
 static int pcid_handle_message(libxl__gc *gc, const libxl__json_object 
*request,
                                libxl__json_object **result)
 {
@@ -171,6 +219,8 @@ static int pcid_handle_message(libxl__gc *gc, const 
libxl__json_object *request,
        *result = process_ls_cmd(gc, request);
     else if (strcmp(PCID_CMD_WRITE, command_name) == 0)
        *result = process_write_cmd(gc, request);
+    else if (strcmp(command_name, PCID_CMD_READ_HEX) == 0)
+        *result = process_read_hex_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®.