os-cmpi-xen

view src/Xen_NetworkPortSettingData_Resource.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents 7ca3bbf0810a
children
line source
1 // Copyright (C) 2006 IBM Corporation
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Authors: Dr. Gareth S. Bestor, <bestor@us.ibm.com>
18 // Tokunbo Adeshiyan, <tokunbo@us.ibm.com>
19 // Contributors: Jim Fehlig, <jfehlig@novell.com>
20 // Description:
21 // ============================================================================
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #include <xen_common.h>
28 #include <xen_vm.h>
29 #include <xen_vif.h>
31 #include "Xen_NetworkPortSettingData_Resource.h"
33 /* Include the required CMPI data types, function headers, and macros. */
34 #include "cmpidt.h"
35 #include "cmpift.h"
36 #include "cmpimacs.h"
38 #include "xen_utils.h"
39 #include "provider_common.h"
42 // ----------------------------------------------------------------------------
44 static int concat_vif_lists(xen_vif_set **target, xen_vif_set *source)
45 {
47 int total_size;
48 int i, j;
50 /* Nothing to concatenate if source is empty */
51 if (source == NULL || source->size == 0)
52 return 1;
54 /* If targe is empty, assign source to target */
55 if (*target == NULL) {
56 *target = source;
57 return 1;
58 }
60 /* realloc memory and append source to target */
61 total_size = (*target)->size + source->size;
62 *target = realloc(*target, sizeof(xen_vif_set) + (total_size * sizeof(xen_vif)));
63 if (*target == NULL)
64 return 0;
66 for (j = (*target)->size, i = 0; i < source->size; i++) {
67 (*target)->contents[j + i] = source->contents[i];
68 source->contents[i] = NULL;
69 }
70 (*target)->size = total_size;
72 /* Free source list - it has been copied to target */
73 xen_vif_set_free(source);
74 return 1;
75 }
78 /* Get a handle to the list of all system resources for this class. */
79 int Xen_NetworkPortSettingData_getResources(xen_utils_session *session,
80 _RESOURCES ** resources)
81 {
82 xen_domain_resources *res;
83 xen_vif_set *all_vifs = NULL;
84 xen_vm_record *vm_rec = NULL;
86 /* malloc a new handle for the resources list. */
87 *resources = (_RESOURCES *)calloc(1, sizeof(_RESOURCES));
88 if (*resources == NULL)
89 return 0;
91 /* Get a list of domain resources. */
92 if (!xen_utils_get_domain_resources(session, &res)) {
93 *resources = NULL;
94 return 0;
95 }
97 /* Create list of vif resources from vifs found in each domain. */
98 while (xen_utils_get_next_domain_resource(session, res, &vm_rec)) {
99 xen_vif_set *vif_set;
100 if (!xen_vm_get_vifs(session->xen, &vif_set, vm_rec->handle))
101 goto Error;
103 if (!concat_vif_lists(&all_vifs, vif_set))
104 goto Error;
106 xen_vm_record_free(vm_rec);
107 }
109 (*resources)->vifs = all_vifs;
110 (*resources)->currentvifnum = 0;
112 return 1;
114 Error:
115 xen_vm_record_free(vm_rec);
116 xen_utils_free_domain_resources(res);
117 free(*resources);
118 *resources = NULL;
119 return 0;
120 }
122 // ----------------------------------------------------------------------------
124 /* Free/deallocate/cleanup the resources list after use. */
125 int Xen_NetworkPortSettingData_freeResources(_RESOURCES * resources)
126 {
127 if (resources != NULL) {
128 if (resources->vifs != NULL)
129 xen_vif_set_free(resources->vifs);
130 free(resources);
131 resources = NULL;
132 }
134 return 1;
135 }
137 // ----------------------------------------------------------------------------
139 /* Iterator to get the next resource from the resources list. */
140 int Xen_NetworkPortSettingData_getNextResource(xen_utils_session *session,
141 _RESOURCES * resources,
142 _RESOURCE ** resource)
143 {
144 if (resources == NULL || resources->vifs == NULL)
145 return 0;
147 /* Check if reached the end of the list of vifs. */
148 if (resources->currentvifnum == resources->vifs->size)
149 return 0;
151 /* Get the current vif record. */
152 if (!xen_vif_get_record(session->xen, resource, resources->vifs->contents[resources->currentvifnum]))
153 return 0;
155 resources->currentvifnum++;
156 return 1;
157 }
159 // ----------------------------------------------------------------------------
161 /* Get the specific resource that matches the CMPI object path. */
162 int Xen_NetworkPortSettingData_getResourceForObjectPath(xen_utils_session *session,
163 _RESOURCE ** resource,
164 const CMPIObjectPath * objectpath)
165 {
166 char *instId;
167 char uuid[MAX_SYSTEM_NAME_LEN];
168 int ccode;
169 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
171 if (CMIsNullObject(objectpath)) return 0;
173 /* Obtain the target resource identifier from the CMPIObjectPath key(s). */
174 CMPIData iddata = CMGetKey(objectpath, "InstanceID", &status);
175 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(iddata)) return 0;
177 instId = CMGetCharPtr(iddata.value.string);
178 if ((instId == NULL) || (*instId == '\0')) return 0;
180 /* Extract the disk uuid from InstanceID property. */
181 if (!_CMPIStrncpyDeviceNameFromID(uuid, instId, MAX_SYSTEM_NAME_LEN))
182 return 0;
184 xen_vif vif;
185 if (!xen_vif_get_by_uuid(session->xen, &vif, uuid))
186 return 0;
188 ccode = xen_vif_get_record(session->xen, resource, vif);
189 xen_vif_free(vif);
190 return ccode;
191 }
193 // ----------------------------------------------------------------------------
195 /* Free/deallocate/cleanup the resource after use. */
196 int Xen_NetworkPortSettingData_freeResource(_RESOURCE * resource)
197 {
198 xen_vif_record_free(resource);
199 return 1;
200 }
202 // ----------------------------------------------------------------------------
204 /* Set the property values of a CMPI instance from a specific resource. */
205 int Xen_NetworkPortSettingData_setInstanceFromResource(xen_utils_session *session,
206 _RESOURCE * resource,
207 const CMPIInstance * instance,
208 const CMPIBroker * broker)
209 {
210 char buf[MAX_INSTANCEID_LEN];
211 xen_vm_record *alloced_vm_rec = NULL;
212 xen_vm_record *vm_rec;
214 if (resource == NULL) return 0;
215 if (CMIsNullObject(instance)) return 0;
217 xen_vm_record_opt *vm_rec_opt = resource->vm;
218 if (vm_rec_opt->is_record) {
219 vm_rec = vm_rec_opt->u.record;
220 }
221 else {
222 if (!xen_vm_get_record(session->xen, &vm_rec, vm_rec_opt->u.handle)) {
223 /* Error description in session object! */
224 return 0;
225 }
226 alloced_vm_rec = vm_rec;
227 }
229 /* Set the CMPIInstance properties from the resource data. */
230 snprintf(buf, MAX_INSTANCEID_LEN, "Xen:%s:%s", vm_rec->name_label, resource->uuid);
231 CMSetProperty(instance, "InstanceID",(CMPIValue *)buf, CMPI_chars);
232 CMSetProperty(instance, "ElementName",(CMPIValue *)resource->device, CMPI_chars);
234 int type = 10; /* 10 == Ethernet Adapter */
235 CMSetProperty(instance, "ResourceType",(CMPIValue *)&type, CMPI_uint16);
237 int consumerVisibility = 3; /* 3 == Virtualized */
238 CMSetProperty(instance, "ConsumerVisibility" , (CMPIValue *)&consumerVisibility, CMPI_uint16);
240 unsigned long long nics = 1;
241 CMSetProperty(instance, "Reservation",(CMPIValue *)&nics, CMPI_uint64);
242 CMSetProperty(instance, "Limit",(CMPIValue *)&nics, CMPI_uint64);
243 CMSetProperty(instance, "AllocationUnits",(CMPIValue *)"Interfaces", CMPI_chars);
244 CMSetProperty(instance, "VirtualQuantity",(CMPIValue *)&nics, CMPI_uint64);
246 int alloctype = 1;
247 CMSetProperty(instance, "AutomaticAllocation" , (CMPIValue *)&alloctype, CMPI_boolean);
248 CMSetProperty(instance, "AutomaticDeallocation" , (CMPIValue *)&alloctype, CMPI_boolean);
250 char nic_config_info[512];
251 nic_config_info[0] = '\0';
252 if (resource->mac)
253 snprintf(nic_config_info, 512, "mac=%s", resource->mac);
254 /* if (resource->model) {
255 strncat(nic_config_info, ",model=", 512 - strlen(nic_config_info));
256 strncat(nic_config_info, resource->model, 512 - strlen(nic_config_info));
257 }Nic config info does have model*/
258 if (nic_config_info[0] != '\0')
259 CMSetProperty(instance, "NICConfigInfo" , (CMPIValue *)nic_config_info, CMPI_chars);
261 /*
262 * TODO:
263 * PoolID, Weight
264 */
266 return 1;
267 }
269 // ----------------------------------------------------------------------------
271 /* Delete the specified resource from the system. */
272 int Xen_NetworkPortSettingData_deleteResource(xen_utils_session *session,
273 _RESOURCE * resource)
274 {
275 return xen_vif_destroy(session->xen, resource->handle);
276 }
278 // ----------------------------------------------------------------------------
280 /* Modify the specified resource using the property values of a CMPI instance. */
281 int Xen_NetworkPortSettingData_setResourceFromInstance(_RESOURCE * resource,
282 const CMPIInstance * instance,
283 const CMPIBroker * broker)
284 {
285 /* Unsupported. */
286 return -1;
287 }
289 // ----------------------------------------------------------------------------
291 /* Create a new resource using the property values of a CMPI instance. */
292 int Xen_NetworkPortSettingData_createResourceFromInstance(_RESOURCES * resources,
293 _RESOURCE ** resource,
294 const CMPIInstance * instance,
295 const CMPIBroker * broker)
296 {
297 /* Unsupported. */
298 return -1;
299 }