~ubuntu-branches/ubuntu/maverick/libvirt/maverick

« back to all changes in this revision

Viewing changes to src/xen_unified.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-06-25 18:51:21 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream) (0.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: james.westby@ubuntu.com-20080625185121-8dku38gpoluks1bx
Tags: upstream-0.4.4
ImportĀ upstreamĀ versionĀ 0.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "xm_internal.h"
41
41
#include "xml.h"
42
42
#include "util.h"
 
43
#include "memory.h"
43
44
 
44
45
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__)
45
46
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
172
173
    if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
173
174
        return(NULL);
174
175
 
175
 
    cpulist = calloc(nb_cpu, sizeof(*cpulist));
176
 
    if (cpulist == NULL)
 
176
    if (VIR_ALLOC_N(cpulist, nb_cpu) < 0)
177
177
        goto done;
178
 
    cpuinfo = malloc(sizeof(*cpuinfo) * nb_vcpu);
179
 
    if (cpuinfo == NULL)
 
178
    if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0)
180
179
        goto done;
181
180
    cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
182
 
    cpumap = (unsigned char *) calloc(nb_vcpu, cpumaplen);
183
 
    if (cpumap == NULL)
 
181
    if (xalloc_oversized(nb_vcpu, cpumaplen) ||
 
182
        VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0)
184
183
        goto done;
185
184
 
186
185
    if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
187
186
                                          cpumap, cpumaplen)) >= 0) {
188
 
        for (n = 0 ; n < ncpus ; n++) {
189
 
            for (m = 0 ; m < nb_cpu; m++) {
190
 
                if ((cpulist[m] == 0) &&
191
 
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
192
 
                    cpulist[m] = 1;
193
 
                    nb++;
194
 
                    /* if all CPU are used just return NULL */
195
 
                    if (nb == nb_cpu)
196
 
                        goto done;
 
187
        for (n = 0 ; n < ncpus ; n++) {
 
188
            for (m = 0 ; m < nb_cpu; m++) {
 
189
                if ((cpulist[m] == 0) &&
 
190
                    (VIR_CPU_USABLE(cpumap, cpumaplen, n, m))) {
 
191
                    cpulist[m] = 1;
 
192
                    nb++;
 
193
                    /* if all CPU are used just return NULL */
 
194
                    if (nb == nb_cpu)
 
195
                        goto done;
197
196
 
198
 
                }
199
 
            }
200
 
        }
 
197
                }
 
198
            }
 
199
        }
201
200
        res = virSaveCpuSet(dom->conn, cpulist, nb_cpu);
202
201
    }
203
202
 
204
203
done:
205
 
    free(cpulist);
206
 
    free(cpumap);
207
 
    free(cpuinfo);
 
204
    VIR_FREE(cpulist);
 
205
    VIR_FREE(cpumap);
 
206
    VIR_FREE(cpuinfo);
208
207
    return(res);
209
208
}
210
209
 
230
229
    FILE *fh;
231
230
 
232
231
    if (fh = fopen("/dev/xen/domcaps", "r")) {
233
 
        fclose(fh);
 
232
        fclose(fh);
234
233
        return("xen:///");
235
234
    }
236
235
#endif
245
244
 
246
245
    /* Refuse any scheme which isn't "xen://" or "http://". */
247
246
    if (uri->scheme &&
248
 
        strcasecmp(uri->scheme, "xen") != 0 &&
249
 
        strcasecmp(uri->scheme, "http") != 0)
 
247
        STRCASENEQ(uri->scheme, "xen") &&
 
248
        STRCASENEQ(uri->scheme, "http"))
250
249
        return VIR_DRV_OPEN_DECLINED;
251
250
 
252
251
    /* xmlParseURI will parse a naked string like "foo" as a URI with
258
257
        return VIR_DRV_OPEN_DECLINED;
259
258
 
260
259
    /* Refuse any xen:// URI with a server specified - allow remote to do it */
261
 
    if (uri->scheme && strcasecmp(uri->scheme, "xen") == 0 && uri->server)
 
260
    if (uri->scheme && STRCASEEQ(uri->scheme, "xen") && uri->server)
262
261
        return VIR_DRV_OPEN_DECLINED;
263
262
 
264
263
    /* Allocate per-connection private data. */
265
 
    priv = calloc (1, sizeof *priv);
266
 
    if (!priv) {
 
264
    if (VIR_ALLOC(priv) < 0) {
267
265
        xenUnifiedError (NULL, VIR_ERR_NO_MEMORY, "allocating private data");
268
266
        return VIR_DRV_OPEN_ERROR;
269
267
    }
342
340
    DEBUG0("Failed to activate a mandatory sub-driver");
343
341
    for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++)
344
342
        if (priv->opened[i]) drivers[i]->close(conn);
345
 
    free(priv);
 
343
    VIR_FREE(priv);
346
344
    return VIR_DRV_OPEN_ERROR;
347
345
}
348
346
 
961
959
            char *cpus, *res;
962
960
            cpus = xenDomainUsedCpus(dom);
963
961
            res = xenDaemonDomainDumpXML(dom, flags, cpus);
964
 
                free(cpus);
 
962
            VIR_FREE(cpus);
965
963
            return(res);
966
964
        }
967
965
        if (priv->opened[XEN_UNIFIED_PROXY_OFFSET])
1128
1126
    return -1;
1129
1127
}
1130
1128
 
 
1129
static int
 
1130
xenUnifiedDomainGetAutostart (virDomainPtr dom, int *autostart)
 
1131
{
 
1132
    GET_PRIVATE(dom->conn);
 
1133
    int i;
 
1134
 
 
1135
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
 
1136
        if (priv->opened[i] && drivers[i]->domainGetAutostart &&
 
1137
            drivers[i]->domainGetAutostart (dom, autostart) == 0)
 
1138
            return 0;
 
1139
 
 
1140
    return -1;
 
1141
}
 
1142
 
 
1143
static int
 
1144
xenUnifiedDomainSetAutostart (virDomainPtr dom, int autostart)
 
1145
{
 
1146
    GET_PRIVATE(dom->conn);
 
1147
    int i;
 
1148
 
 
1149
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i)
 
1150
        if (priv->opened[i] && drivers[i]->domainSetAutostart &&
 
1151
            drivers[i]->domainSetAutostart (dom, autostart) == 0)
 
1152
            return 0;
 
1153
 
 
1154
    return -1;
 
1155
}
 
1156
 
1131
1157
static char *
1132
1158
xenUnifiedDomainGetSchedulerType (virDomainPtr dom, int *nparams)
1133
1159
{
1138
1164
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; i++) {
1139
1165
        if (priv->opened[i] && drivers[i]->domainGetSchedulerType) {
1140
1166
            schedulertype = drivers[i]->domainGetSchedulerType (dom, nparams);
1141
 
            if (schedulertype != NULL)
1142
 
                return(schedulertype);
 
1167
            if (schedulertype != NULL)
 
1168
                return(schedulertype);
1143
1169
        }
1144
1170
    }
1145
1171
    return(NULL);
1155
1181
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
1156
1182
        if (priv->opened[i] && drivers[i]->domainGetSchedulerParameters) {
1157
1183
           ret = drivers[i]->domainGetSchedulerParameters(dom, params, nparams);
1158
 
           if (ret == 0)
1159
 
               return(0);
1160
 
        }
 
1184
           if (ret == 0)
 
1185
               return(0);
 
1186
        }
1161
1187
    }
1162
1188
    return(-1);
1163
1189
}
1172
1198
    for (i = 0; i < XEN_UNIFIED_NR_DRIVERS; ++i) {
1173
1199
        if (priv->opened[i] && drivers[i]->domainSetSchedulerParameters) {
1174
1200
           ret = drivers[i]->domainSetSchedulerParameters(dom, params, nparams);
1175
 
           if (ret == 0)
1176
 
               return 0;
1177
 
        }
 
1201
           if (ret == 0)
 
1202
               return 0;
 
1203
        }
1178
1204
    }
1179
1205
 
1180
1206
    return(-1);
1207
1233
}
1208
1234
 
1209
1235
static int
 
1236
xenUnifiedDomainBlockPeek (virDomainPtr dom, const char *path,
 
1237
                           unsigned long long offset, size_t size,
 
1238
                           void *buffer, unsigned int flags ATTRIBUTE_UNUSED)
 
1239
{
 
1240
    int r;
 
1241
    GET_PRIVATE (dom->conn);
 
1242
 
 
1243
    if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) {
 
1244
        r = xenDaemonDomainBlockPeek (dom, path, offset, size, buffer);
 
1245
        if (r != -2) return r;
 
1246
        /* r == -2 means declined, so fall through to XM driver ... */
 
1247
    }
 
1248
 
 
1249
    if (priv->opened[XEN_UNIFIED_XM_OFFSET]) {
 
1250
        if (xenXMDomainBlockPeek (dom, path, offset, size, buffer) == 0)
 
1251
            return 0;
 
1252
    }
 
1253
 
 
1254
    xenUnifiedError (dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
 
1255
    return -1;
 
1256
}
 
1257
 
 
1258
static int
1210
1259
xenUnifiedNodeGetCellsFreeMemory (virConnectPtr conn, unsigned long long *freeMems,
1211
1260
                                  int startCell, int maxCells)
1212
1261
{
1230
1279
    if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) {
1231
1280
        ret = xenHypervisorNodeGetCellsFreeMemory (conn, &freeMem,
1232
1281
                                                    -1, 1);
1233
 
        if (ret != 1)
1234
 
            return (0);
1235
 
        return(freeMem);
 
1282
        if (ret != 1)
 
1283
            return (0);
 
1284
        return(freeMem);
1236
1285
    }
1237
1286
 
1238
1287
    xenUnifiedError (conn, VIR_ERR_NO_SUPPORT, __FUNCTION__);
1291
1340
    .domainUndefine             = xenUnifiedDomainUndefine,
1292
1341
    .domainAttachDevice                 = xenUnifiedDomainAttachDevice,
1293
1342
    .domainDetachDevice                 = xenUnifiedDomainDetachDevice,
 
1343
    .domainGetAutostart             = xenUnifiedDomainGetAutostart,
 
1344
    .domainSetAutostart             = xenUnifiedDomainSetAutostart,
1294
1345
    .domainGetSchedulerType     = xenUnifiedDomainGetSchedulerType,
1295
1346
    .domainGetSchedulerParameters       = xenUnifiedDomainGetSchedulerParameters,
1296
1347
    .domainSetSchedulerParameters       = xenUnifiedDomainSetSchedulerParameters,
1299
1350
    .domainMigrateFinish                = xenUnifiedDomainMigrateFinish,
1300
1351
    .domainBlockStats   = xenUnifiedDomainBlockStats,
1301
1352
    .domainInterfaceStats = xenUnifiedDomainInterfaceStats,
 
1353
    .domainBlockPeek    = xenUnifiedDomainBlockPeek,
1302
1354
    .nodeGetCellsFreeMemory = xenUnifiedNodeGetCellsFreeMemory,
1303
1355
    .getFreeMemory = xenUnifiedNodeGetFreeMemory,
1304
1356
};
1324
1376
}
1325
1377
 
1326
1378
#endif /* WITH_XEN */
1327
 
 
1328
 
/*
1329
 
 * vim: set tabstop=4:
1330
 
 * vim: set shiftwidth=4:
1331
 
 * vim: set expandtab:
1332
 
 */
1333
 
/*
1334
 
 * Local variables:
1335
 
 *  indent-tabs-mode: nil
1336
 
 *  c-indent-level: 4
1337
 
 *  c-basic-offset: 4
1338
 
 *  tab-width: 4
1339
 
 * End:
1340
 
 */