[Libvirt-cim] [PATCH] ProcessorRASD, the class that won't go away

Jay Gagnon grendel at linux.vnet.ibm.com
Wed May 14 15:25:15 UTC 2008


# HG changeset patch
# User Jay Gagnon <grendel at linux.vnet.ibm.com>
# Date 1210778696 14400
# Node ID 5d6e45906bf07c81d973e2edcc10dcefbc265dab
# Parent  0b26d79ea3283461b349d20dbcfdf1715344afcd
ProcessorRASD, the class that won't go away

Around and round we go on the merry-go-round of indecision, hopefully for our last ride.  The definitely probably absolutely tentative final plan is that the default representation of processors as virt_device structs will be one per domain, with a quantity.  Virt_Devices will just need to be told how to handle that, and all the RASD stuff will be much happier for it.  And then we will never speak of this again.

So today being my last official day on the team, I kind of have to get this out in a not perfect state.  From what I can see it does what it is supposed to do, but it is entirely possible (likely?) that there are a few things in here that aren't really great things to do.  I wish I could polish it more but I think I've at least got it to the point where any work left to be done is generic "not a great idea in C" problems, as opposed to the much more annoying "what exactly are we supposed to do in this case" problems.

Signed-off-by: Jay Gagnon <grendel at linux.vnet.ibm.com>

diff -r 0b26d79ea328 -r 5d6e45906bf0 libxkutil/device_parsing.c
--- a/libxkutil/device_parsing.c	Fri May 02 16:32:37 2008 -0400
+++ b/libxkutil/device_parsing.c	Wed May 14 11:24:56 2008 -0400
@@ -337,7 +337,6 @@ static int parse_vcpu_device(xmlNode *no
         struct virt_device *list = NULL;
         char *count_str;
         int count;
-        int i;
 
         count_str = get_node_content(node);
         if (count_str == NULL)
@@ -347,24 +346,15 @@ static int parse_vcpu_device(xmlNode *no
 
         free(count_str);
 
-        list = calloc(count, sizeof(*list));
+        list = calloc(1, sizeof(*list));
         if (list == NULL)
                 goto err;
-
-        for (i = 0; i < count; i++) {
-                struct virt_device *vdev = &list[i];
-                struct vcpu_device *cdev = &vdev->dev.vcpu;
-
-                cdev->number = i;
-
-                vdev->type = CIM_RES_TYPE_PROC;
-                if (asprintf(&vdev->id, "%i", i) == -1)
-                        vdev->id = NULL;
-        }
+        
+        list->dev.vcpu.quantity = count;
 
         *vdevs = list;
 
-        return count;
+        return 1;
  err:
         free(list);
 
@@ -620,7 +610,7 @@ struct virt_device *virt_device_dup(stru
                 dev->dev.mem.size = _dev->dev.mem.size;
                 dev->dev.mem.maxsize = _dev->dev.mem.maxsize;
         } else if (dev->type == CIM_RES_TYPE_PROC) {
-                dev->dev.vcpu.number = _dev->dev.vcpu.number;
+                dev->dev.vcpu.quantity = _dev->dev.vcpu.quantity;
         } else if (dev->type == CIM_RES_TYPE_EMU) {
                 DUP_FIELD(dev, _dev, dev.emu.path);
         } else if (dev->type == CIM_RES_TYPE_GRAPHICS) {
@@ -672,6 +662,32 @@ static int _get_mem_device(const char *x
         return 1;
 }
 
+static int _get_proc_device(const char *xml, struct virt_device **list)
+{
+        struct virt_device *proc_devs = NULL;
+        struct virt_device *proc_dev = NULL;
+        int ret;
+
+        ret = parse_devices(xml, &proc_devs, CIM_RES_TYPE_PROC);
+        if (ret <= 0)
+                return ret;
+
+        proc_dev = malloc(sizeof(*proc_dev));
+        if (proc_dev == NULL)
+                return 0;
+
+        memset(proc_dev, 0, sizeof(*proc_dev));
+
+        proc_dev->type = CIM_RES_TYPE_PROC;
+        proc_dev->id = strdup("proc");
+        proc_dev->dev.vcpu.quantity = proc_devs[0].dev.vcpu.quantity;
+        *list = proc_dev;
+
+        cleanup_virt_devices(&proc_devs, ret);
+
+        return 1;
+};
+
 int get_devices(virDomainPtr dom, struct virt_device **list, int type)
 {
         char *xml;
@@ -683,6 +699,8 @@ int get_devices(virDomainPtr dom, struct
 
         if (type == CIM_RES_TYPE_MEM)
                 ret = _get_mem_device(xml, list);
+        else if (type == CIM_RES_TYPE_PROC)
+                ret = _get_proc_device(xml, list);
         else
                 ret = parse_devices(xml, list, type);
 
diff -r 0b26d79ea328 -r 5d6e45906bf0 libxkutil/device_parsing.h
--- a/libxkutil/device_parsing.h	Fri May 02 16:32:37 2008 -0400
+++ b/libxkutil/device_parsing.h	Wed May 14 11:24:56 2008 -0400
@@ -50,7 +50,7 @@ struct mem_device {
 };
 
 struct vcpu_device {
-        uint64_t number;
+        uint64_t quantity;
 };
 
 struct emu_device {
diff -r 0b26d79ea328 -r 5d6e45906bf0 src/Virt_Device.c
--- a/src/Virt_Device.c	Fri May 02 16:32:37 2008 -0400
+++ b/src/Virt_Device.c	Wed May 14 11:24:56 2008 -0400
@@ -174,23 +174,6 @@ static CMPIInstance *mem_instance(const 
         return inst;
 }
 
-static CMPIInstance *vcpu_instance(const CMPIBroker *broker,
-                                   struct vcpu_device *dev,
-                                   const virDomainPtr dom,
-                                   const char *ns)
-{
-        CMPIInstance *inst;
-        virConnectPtr conn;
-
-        conn = virDomainGetConnect(dom);
-        inst = get_typed_instance(broker,
-                                  pfx_from_conn(conn),
-                                  "Processor",
-                                  ns);
-
-        return inst;
-}
-
 static int device_set_devid(CMPIInstance *instance,
                             struct virt_device *dev,
                             const virDomainPtr dom)
@@ -229,43 +212,114 @@ static int device_set_systemname(CMPIIns
         return 1;
 }
 
-static CMPIInstance *device_instance(const CMPIBroker *broker,
-                                     struct virt_device *dev,
-                                     const virDomainPtr dom,
-                                     const char *ns)
-{
-        CMPIInstance *instance;
-
-        if (dev->type == CIM_RES_TYPE_NET)
-                instance = net_instance(broker,
-                                        &dev->dev.net,
-                                        dom,
-                                        ns);
-        else if (dev->type == CIM_RES_TYPE_DISK)
-                instance = disk_instance(broker,
-                                         &dev->dev.disk,
-                                         dom,
-                                         ns);
-        else if (dev->type == CIM_RES_TYPE_MEM)
-                instance = mem_instance(broker,
-                                        &dev->dev.mem,
-                                        dom,
-                                        ns);
-        else if (dev->type == CIM_RES_TYPE_PROC)
-                instance = vcpu_instance(broker,
-                                         &dev->dev.vcpu,
-                                         dom,
-                                         ns);
-        else
-                return NULL;
-
-        if (!instance)
-                return NULL;
-
-        device_set_devid(instance, dev, dom);
-        device_set_systemname(instance, dom);
-
-        return instance;
+static char *get_vcpu_inst_id(const virDomainPtr dom,
+                              int proc_num)
+{
+        int rc;
+        char *id_num = NULL;
+        char *dev_id = NULL;
+
+        rc = asprintf(&id_num, "%d", proc_num);
+        if (rc == -1) {
+                free(dev_id);
+                dev_id = NULL;
+                goto out;
+        }
+        
+        dev_id = get_fq_devid((char *)virDomainGetName(dom), id_num);
+        free(id_num);
+
+ out:
+        return dev_id;
+}                    
+
+static bool vcpu_instances(const CMPIBroker *broker,
+                           const virDomainPtr dom,
+                           const char *ns,
+                           uint64_t proc_count,
+                           struct inst_list *list)
+{
+        int i;
+        char *dev_id;
+        CMPIInstance *inst;
+        virConnectPtr conn;
+
+        for (i = 0; i < proc_count; i++) {
+                conn = virDomainGetConnect(dom);
+                inst = get_typed_instance(broker,
+                                          pfx_from_conn(conn),
+                                          "Processor",
+                                          ns);
+                if (inst == NULL)
+                        return false;
+
+                dev_id = get_vcpu_inst_id(dom, i);
+                CMSetProperty(inst, "DeviceID",
+                              (CMPIValue *)dev_id, CMPI_chars);
+                free(dev_id);
+                
+                device_set_systemname(inst, dom);
+                inst_list_add(list, inst);
+        }
+
+        return true;
+}
+
+static bool device_instances(const CMPIBroker *broker,
+                             struct virt_device *devs,
+                             int count,
+                             const virDomainPtr dom,
+                             const char *ns,
+                             struct inst_list *list)
+{
+        int i;
+        bool ret;
+        uint64_t proc_count = 0;
+        bool proc_found = false;
+        CMPIInstance *instance = NULL;
+
+        for (i = 0; i < count; i++) {
+                struct virt_device *dev = &devs[i];
+
+                if (dev->type == CIM_RES_TYPE_NET)
+                        instance = net_instance(broker,
+                                                &dev->dev.net,
+                                                dom,
+                                                ns);
+                else if (dev->type == CIM_RES_TYPE_DISK)
+                        instance = disk_instance(broker,
+                                                 &dev->dev.disk,
+                                                 dom,
+                                                 ns);
+                else if (dev->type == CIM_RES_TYPE_MEM)
+                        instance = mem_instance(broker,
+                                                &dev->dev.mem,
+                                                dom,
+                                                ns);
+                else if (dev->type == CIM_RES_TYPE_PROC) {
+                        proc_found = true;
+                        proc_count = dev->dev.vcpu.quantity;
+                        continue;
+                } else
+                        return false;
+
+                if (!instance)
+                        return false;
+                
+                device_set_devid(instance, dev, dom);
+                device_set_systemname(instance, dom);
+                inst_list_add(list, instance);
+        }
+
+        if (proc_count) {
+                ret = vcpu_instances(broker,
+                                     dom,
+                                     ns,
+                                     proc_count,
+                                     list);
+        }
+
+        return true;
 }
 
 uint16_t res_type_from_device_classname(const char *classname)
@@ -290,25 +344,27 @@ static CMPIStatus _get_devices(const CMP
 {
         CMPIStatus s = {CMPI_RC_OK, NULL};
         int count;
-        int i;
+        bool rc;
         struct virt_device *devs = NULL;
 
         count = get_devices(dom, &devs, type);
         if (count <= 0)
                 goto out;
 
-        for (i = 0; i < count; i++) {
-                CMPIInstance *dev = NULL;
-
-                dev = device_instance(broker,
-                                      &devs[i],
-                                      dom,
-                                      NAMESPACE(reference));
-                if (dev)
-                        inst_list_add(list, dev);
-
-                cleanup_virt_device(&devs[i]);
-        }
+        rc = device_instances(broker, 
+                              devs, 
+                              count,
+                              dom, 
+                              NAMESPACE(reference),
+                              list);
+
+        if (!rc) {
+                CU_DEBUG("Problem getting device instances");
+                cu_statusf(broker, &s,
+                           CMPI_RC_ERR_FAILED,
+                           "Couldn't get device instances");
+
+        cleanup_virt_devices(&devs, count);
 
  out:
         free(devs);
@@ -427,6 +483,29 @@ static int parse_devid(const char *devid
         return 1;
 }
 
+static int proc_dev_list(uint64_t quantity,
+                         struct virt_device **list)
+{
+        int i;
+        int rc;
+        
+
+        *list = (struct virt_device *)calloc(quantity, 
+                                             sizeof(struct virt_device));
+
+        for (i = 0; i < quantity; i++) {
+                char *dev_num;
+
+                rc = asprintf(&dev_num, "%d", i);
+
+                (*list)[i].id = strdup(dev_num);
+
+                free(dev_num);
+        }
+
+        return quantity;
+}
+
 static struct virt_device *find_dom_dev(virDomainPtr dom, 
                                         char *device,
                                         int type)
@@ -439,6 +518,17 @@ static struct virt_device *find_dom_dev(
         count = get_devices(dom, &list, type);
         if (!count)
                 goto out;
+
+        if (type == CIM_RES_TYPE_PROC) {
+                struct virt_device *tmp_list;
+                int tmp_count;
+
+                tmp_count = proc_dev_list(list[0].dev.vcpu.quantity,
+                                          &tmp_list);
+                cleanup_virt_devices(&list, count);
+                list = tmp_list;
+                count = tmp_count;
+        }
 
         for (i = 0; i < count; i++) {
                 if (STREQC(device, list[i].id))
@@ -462,10 +552,13 @@ CMPIStatus get_device_by_name(const CMPI
         CMPIStatus s = {CMPI_RC_OK, NULL};
         char *domain = NULL;
         char *device = NULL;
-        CMPIInstance *instance = NULL;
         virConnectPtr conn = NULL;
         virDomainPtr dom = NULL;
         struct virt_device *dev = NULL;
+        struct inst_list tmp_list;
+        bool rc;
+
+        inst_list_init(&tmp_list);
 
         conn = connect_by_classname(broker, CLASSNAME(reference), &s);
         if (conn == NULL) {
@@ -501,13 +594,27 @@ CMPIStatus get_device_by_name(const CMPI
                 goto err;
         }
 
-        instance = device_instance(broker, 
-                                   dev, 
-                                   dom, 
-                                   NAMESPACE(reference));
+        if (type == CIM_RES_TYPE_PROC) {
+                CU_DEBUG("PROC");
+                rc = vcpu_instances(broker,
+                                    dom,
+                                    NAMESPACE(reference),
+                                    1,
+                                    &tmp_list);
+        } else {
+                
+                rc = device_instances(broker, 
+                                      dev, 
+                                      1,
+                                      dom, 
+                                      NAMESPACE(reference),
+                                      &tmp_list);
+        }
+
         cleanup_virt_device(dev);
 
-        *_inst = instance;
+        *_inst = tmp_list.list[0];
+        CU_DEBUG("cleaning up");
 
  err:
         virDomainFree(dom);
@@ -515,6 +622,7 @@ CMPIStatus get_device_by_name(const CMPI
         free(device);
 
  out:
+        inst_list_free(&tmp_list);
         virConnectClose(conn);
 
         return s;        
diff -r 0b26d79ea328 -r 5d6e45906bf0 src/Virt_RASD.c
--- a/src/Virt_RASD.c	Fri May 02 16:32:37 2008 -0400
+++ b/src/Virt_RASD.c	Wed May 14 11:24:56 2008 -0400
@@ -173,10 +173,8 @@ static CMPIInstance *rasd_from_vdev(cons
                 CMSetProperty(inst, "Limit",
                               (CMPIValue *)&dev->dev.mem.maxsize, CMPI_uint64);
         } else if (dev->type == CIM_RES_TYPE_PROC) {
-                /* This would be the place to set the virtualquantity. */
-                uint64_t quantity = dev->dev.vcpu.number + 1;
                 CMSetProperty(inst, "VirtualQuantity",
-                              (CMPIValue *)&quantity, CMPI_uint64);
+                              (CMPIValue *)&dev->dev.vcpu.quantity, CMPI_uint64);
         }
 
         /* FIXME: Put the HostResource in place */




More information about the Libvirt-cim mailing list