os-cmpi-xen

view src/Xen_NetworkPort_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 // Rajagopalan Subrahmanian, <raj.subrahmanian@unisys.com>
21 // Description:
22 // ============================================================================
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
28 #include <xen_common.h>
29 #include <xen_vm.h>
30 #include <xen_vif.h>
32 #include "Xen_NetworkPort_Resource.h"
34 /* Include the required CMPI data types, function headers, and macros. */
35 #include "cmpidt.h"
36 #include "cmpift.h"
37 #include "cmpimacs.h"
39 #include "xen_utils.h"
41 /* Include _SBLIM_TRACE() logging support */
42 #include "cmpitrace.h"
44 // ----------------------------------------------------------------------------
46 static int concat_vif_lists(xen_vif_set **target, xen_vif_set *source)
47 {
49 int total_size;
50 int i, j;
52 /* Nothing to concatenate if source is empty */
53 if (source == NULL || source->size == 0)
54 return 1;
56 /* If targe is empty, assign source to target */
57 if (*target == NULL) {
58 *target = source;
59 return 1;
60 }
62 /* realloc memory and append source to target */
63 total_size = (*target)->size + source->size;
64 *target = realloc(*target, sizeof(xen_vif_set) + (total_size * sizeof(xen_vif)));
65 if (*target == NULL)
66 return 0;
68 for (j = (*target)->size, i = 0; i < source->size; i++) {
69 (*target)->contents[j + i] = source->contents[i];
70 source->contents[i] = NULL;
71 }
72 (*target)->size = total_size;
74 /* Free source list - it has been copied to target */
75 xen_vif_set_free(source);
76 return 1;
77 }
80 /* Get a handle to the list of all system resources for this class. */
81 int Xen_NetworkPort_getResources(xen_utils_session *session,
82 _RESOURCES ** resources)
83 {
84 xen_domain_resources *res;
85 xen_vif_set *all_vifs = NULL;
86 xen_vm_record *vm_rec = NULL;
88 /* malloc a new handle for the resources list. */
89 *resources = (_RESOURCES *)calloc(1, sizeof(_RESOURCES));
90 if (*resources == NULL)
91 return 0;
93 /* Get a list of domain resources. */
94 if (!xen_utils_get_domain_resources(session, &res)) {
95 *resources = NULL;
96 return 0;
97 }
99 /* Create list of vif resources from vifs found in each domain. */
100 while (xen_utils_get_next_domain_resource(session, res, &vm_rec)) {
101 /* Ignore halted vms */
102 if (vm_rec->power_state == XEN_VM_POWER_STATE_HALTED) {
103 xen_vm_record_free(vm_rec);
104 continue;
105 }
107 xen_vif_set *vif_set;
108 if (!xen_vm_get_vifs(session->xen, &vif_set, vm_rec->handle))
109 goto Error;
111 if (!concat_vif_lists(&all_vifs, vif_set))
112 goto Error;
114 xen_vm_record_free(vm_rec);
115 }
117 (*resources)->vifs = all_vifs;
118 (*resources)->currentvifnum = 0;
120 return 1;
122 Error:
123 xen_vm_record_free(vm_rec);
124 xen_utils_free_domain_resources(res);
125 free(*resources);
126 *resources = NULL;
127 return 0;
128 }
130 // ----------------------------------------------------------------------------
132 /* Free/deallocate/cleanup the resources list after use. */
133 int Xen_NetworkPort_freeResources(_RESOURCES * resources)
134 {
135 if (resources != NULL) {
136 if (resources->vifs != NULL)
137 xen_vif_set_free(resources->vifs);
138 free(resources);
139 resources = NULL;
140 }
142 return 1;
143 }
145 // ----------------------------------------------------------------------------
147 /* Iterator to get the next resource from the resources list. */
148 int Xen_NetworkPort_getNextResource(xen_utils_session *session,
149 _RESOURCES * resources,
150 _RESOURCE ** resource)
151 {
152 if (resources == NULL || resources->vifs == NULL)
153 return 0;
155 /* Check if reached the end of the list of vifs. */
156 if (resources->currentvifnum == resources->vifs->size)
157 return 0;
159 /* Get the current vif record. */
160 if (!xen_vif_get_record(session->xen, resource, resources->vifs->contents[resources->currentvifnum]))
161 return 0;
163 resources->currentvifnum++;
164 return 1;
165 }
167 // ----------------------------------------------------------------------------
169 /* Get the specific resource that matches the CMPI object path. */
170 int Xen_NetworkPort_getResourceForObjectPath(xen_utils_session *session,
171 _RESOURCE ** resource,
172 const CMPIObjectPath * objectpath)
173 {
174 CMPIData namedata;
175 char *keyvalue;
176 int ccode;
177 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
179 if (CMIsNullObject(objectpath)) return 0;
181 /* Get the vif resource based on DeviceID (vif uuid) */
182 namedata = CMGetKey(objectpath, "DeviceID", &status);
183 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(namedata))
184 return 0;
186 keyvalue = CMGetCharPtr(namedata.value.string);
187 if ((keyvalue == NULL) || (*keyvalue == '\0'))
188 return 0;
190 xen_vif vif;
191 if (!xen_vif_get_by_uuid(session->xen, &vif, keyvalue))
192 return 0;
194 ccode = xen_vif_get_record(session->xen, resource, vif);
195 xen_vif_free(vif);
196 return ccode;
197 }
199 // ----------------------------------------------------------------------------
201 /* Free/deallocate/cleanup the resource after use. */
202 int Xen_NetworkPort_freeResource(_RESOURCE * resource)
203 {
204 xen_vif_record_free(resource);
205 return 1;
206 }
208 // ----------------------------------------------------------------------------
210 /* Set the property values of a CMPI instance from a specific resource. */
211 int Xen_NetworkPort_setInstanceFromResource(xen_utils_session *session,
212 _RESOURCE * resource,
213 const CMPIInstance * instance,
214 const CMPIBroker * broker)
215 {
216 xen_vm_record *alloced_vm_rec = NULL;
217 xen_vm_record *vm_rec;
219 if (resource == NULL) return 0;
220 if (CMIsNullObject(instance)) return 0;
222 xen_vm_record_opt *vm_rec_opt = resource->vm;
223 if (vm_rec_opt->is_record) {
224 vm_rec = vm_rec_opt->u.record;
225 }
226 else {
227 if (!xen_vm_get_record(session->xen, &vm_rec, vm_rec_opt->u.handle)) {
228 /* Error description in session object! */
229 return 0;
230 }
231 alloced_vm_rec = vm_rec;
232 }
234 /* Set the CMPIInstance properties from the resource data. */
235 CMSetProperty(instance, "SystemCreationClassName",(CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
236 CMSetProperty(instance, "SystemName",(CMPIValue *)vm_rec->name_label, CMPI_chars);
237 CMSetProperty(instance, "CreationClassName",(CMPIValue *)"Xen_NetworkPort", CMPI_chars);
239 CMSetProperty(instance, "DeviceID",(CMPIValue *)resource->uuid, CMPI_chars);
241 CMSetProperty(instance, "Caption",(CMPIValue *)"Network Port", CMPI_chars);
242 CMSetProperty(instance, "Description",(CMPIValue *)"Network Port", CMPI_chars);
244 //CMSetProperty(instance, "NICConfigInfo",(CMPIValue *)resource->vif[vifnum].params, CMPI_chars);
246 int link_tech = 2; /* 2 == Ethernet */
247 CMSetProperty(instance, "LinkTechnology",(CMPIValue *)&link_tech, CMPI_uint16);
249 if (resource->device) {
250 CMSetProperty(instance, "Name",(CMPIValue *)resource->device, CMPI_chars);
251 CMSetProperty(instance, "VIFName",(CMPIValue *)resource->device, CMPI_chars);
252 }
254 if (resource->mac)
255 CMSetProperty(instance, "PermanentAddress",(CMPIValue *)resource->mac, CMPI_chars);
256 if (resource->mtu > 0) {
257 CMSetProperty(instance, "SupportedMaximumTransmissionUnit",(CMPIValue *)&(resource->mtu), CMPI_uint64);
258 CMSetProperty(instance, "ActiveMaximumTransmissionUnit",(CMPIValue *)&(resource->mtu), CMPI_uint64);
259 }
261 char nic_config_info[512];
262 nic_config_info[0] = '\0';
263 if (resource->mac)
264 snprintf(nic_config_info, 512, "mac=%s", resource->mac);
265 /*if (resource->model) {
266 strncat(nic_config_info, ",model=", 512 - strlen(nic_config_info));
267 strncat(nic_config_info, resource->model, 512 - strlen(nic_config_info));
268 } NIC config info does not have model*/
269 if (nic_config_info[0] != '\0')
270 CMSetProperty(instance, "NICConfigInfo" , (CMPIValue *)nic_config_info, CMPI_chars);
272 if (vm_rec->power_state != XEN_VM_POWER_STATE_HALTED) {
273 CMSetProperty(instance, "Status", (CMPIValue *)"OK", CMPI_chars);
274 } else {
275 CMSetProperty(instance, "Status", (CMPIValue *)"No Contact", CMPI_chars);
276 }
278 if (alloced_vm_rec)
279 xen_vm_record_free(alloced_vm_rec);
281 return 1;
282 }
284 // ----------------------------------------------------------------------------
286 /* Delete the specified resource from the system. */
287 int Xen_NetworkPort_deleteResource(xen_utils_session *session,
288 _RESOURCE * resource)
289 {
290 return xen_vif_destroy(session->xen, resource->handle);
291 }
293 // ----------------------------------------------------------------------------
295 /* Modify the specified resource using the property values of a CMPI instance. */
296 int Xen_NetworkPort_setResourceFromInstance(_RESOURCE * resource,
297 const CMPIInstance * instance,
298 const CMPIBroker * broker)
299 {
300 /* Unsupported. */
301 return -1;
302 }
304 // ----------------------------------------------------------------------------
306 /* Create a new resource using the property values of a CMPI instance. */
307 int Xen_NetworkPort_createResourceFromInstance(_RESOURCES * resources,
308 _RESOURCE ** resource,
309 const CMPIInstance * instance,
310 const CMPIBroker * broker)
311 {
312 /* Unsupported. */
313 return -1;
314 }