1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright 2009-2017 Citrix Ltd and other contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
#include "libxl_osdeps.h"
#include "libxl_internal.h"
/* Returns:
* 0 - success
* ERROR_FAIL + errno == ENOENT - no entry found
* ERROR_$FOO + errno != ENOENT - other failure
*/
static int cpupool_info(libxl__gc *gc,
libxl_cpupoolinfo *info,
uint32_t poolid,
bool exact /* exactly poolid or >= poolid */)
{
xc_cpupoolinfo_t *xcinfo;
int rc = ERROR_FAIL;
xcinfo = xc_cpupool_getinfo(CTX->xch, poolid);
if (xcinfo == NULL)
{
if (exact || errno != ENOENT)
LOGE(ERROR, "failed to get info for cpupool%d", poolid);
return ERROR_FAIL;
}
if (exact && xcinfo->cpupool_id != poolid)
{
LOG(ERROR, "got info for cpupool%d, wanted cpupool%d\n",
xcinfo->cpupool_id, poolid);
goto out;
}
info->poolid = xcinfo->cpupool_id;
info->pool_name = libxl_cpupoolid_to_name(CTX, info->poolid);
if (!info->pool_name) {
rc = ERROR_FAIL;
goto out;
}
info->sched = xcinfo->sched_id;
info->n_dom = xcinfo->n_dom;
rc = libxl_cpu_bitmap_alloc(CTX, &info->cpumap, 0);
if (rc)
goto out;
memcpy(info->cpumap.map, xcinfo->cpumap, info->cpumap.size);
rc = 0;
out:
xc_cpupool_infofree(CTX->xch, xcinfo);
return rc;
}
int libxl_cpupool_info(libxl_ctx *ctx,
libxl_cpupoolinfo *info, uint32_t poolid)
{
GC_INIT(ctx);
int rc = cpupool_info(gc, info, poolid, true);
GC_FREE;
return rc;
}
libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool_out)
{
GC_INIT(ctx);
libxl_cpupoolinfo info, *ptr;
int i;
uint32_t poolid;
ptr = NULL;
poolid = 0;
for (i = 0;; i++) {
libxl_cpupoolinfo_init(&info);
if (cpupool_info(gc, &info, poolid, false)) {
libxl_cpupoolinfo_dispose(&info);
if (errno != ENOENT) goto out;
break;
}
ptr = libxl__realloc(NOGC, ptr, (i+1) * sizeof(libxl_cpupoolinfo));
ptr[i] = info;
poolid = info.poolid + 1;
/* Don't dispose of info because it will be returned to caller */
}
*nb_pool_out = i;
GC_FREE;
return ptr;
out:
libxl_cpupoolinfo_list_free(ptr, i);
*nb_pool_out = 0;
GC_FREE;
return NULL;
}
int libxl_get_freecpus(libxl_ctx *ctx, libxl_bitmap *cpumap)
{
int ncpus;
ncpus = libxl_get_max_cpus(ctx);
if (ncpus < 0)
return ncpus;
cpumap->map = xc_cpupool_freeinfo(ctx->xch);
if (cpumap->map == NULL)
return ERROR_FAIL;
cpumap->size = (ncpus + 7) / 8;
return 0;
}
int libxl_cpupool_create(libxl_ctx *ctx, const char *name,
libxl_scheduler sched,
libxl_bitmap cpumap, libxl_uuid *uuid,
uint32_t *poolid)
{
GC_INIT(ctx);
int rc;
int i;
xs_transaction_t t;
char *uuid_string;
uint32_t xcpoolid;
/* Accept '0' as 'any poolid' for backwards compatibility */
if ( *poolid == LIBXL_CPUPOOL_POOLID_ANY
|| *poolid == 0 )
xcpoolid = XC_CPUPOOL_POOLID_ANY;
else
xcpoolid = *poolid;
uuid_string = libxl__uuid2string(gc, *uuid);
if (!uuid_string) {
GC_FREE;
return ERROR_NOMEM;
}
rc = xc_cpupool_create(ctx->xch, &xcpoolid, sched);
if (rc) {
LOGEV(ERROR, rc, "Could not create cpupool");
GC_FREE;
return ERROR_FAIL;
}
*poolid = xcpoolid;
libxl_for_each_bit(i, cpumap)
if (libxl_bitmap_test(&cpumap, i)) {
rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
if (rc) {
LOGEV(ERROR, rc, "Error moving cpu to cpupool");
libxl_cpupool_destroy(ctx, *poolid);
GC_FREE;
return ERROR_FAIL;
}
}
for (;;) {
t = xs_transaction_start(ctx->xsh);
xs_mkdir(ctx->xsh, t, GCSPRINTF("/local/pool/%d", *poolid));
libxl__xs_printf(gc, t,
GCSPRINTF("/local/pool/%d/uuid", *poolid),
"%s", uuid_string);
libxl__xs_printf(gc, t,
GCSPRINTF("/local/pool/%d/name", *poolid),
"%s", name);
if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
GC_FREE;
return 0;
}
}
}
int libxl_cpupool_destroy(libxl_ctx *ctx, uint32_t poolid)
{
GC_INIT(ctx);
int rc, i;
xc_cpupoolinfo_t *info;
xs_transaction_t t;
libxl_bitmap cpumap;
info = xc_cpupool_getinfo(ctx->xch, poolid);
if (info == NULL) {
GC_FREE;
return ERROR_NOMEM;
}
rc = ERROR_INVAL;
if ((info->cpupool_id != poolid) || (info->n_dom))
goto out;
rc = libxl_cpu_bitmap_alloc(ctx, &cpumap, 0);
if (rc)
goto out;
memcpy(cpumap.map, info->cpumap, cpumap.size);
libxl_for_each_bit(i, cpumap)
if (libxl_bitmap_test(&cpumap, i)) {
rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
if (rc) {
LOGEV(ERROR, rc, "Error removing cpu from cpupool");
rc = ERROR_FAIL;
goto out1;
}
}
rc = xc_cpupool_destroy(ctx->xch, poolid);
if (rc) {
LOGEV(ERROR, rc, "Could not destroy cpupool");
rc = ERROR_FAIL;
goto out1;
}
for (;;) {
t = xs_transaction_start(ctx->xsh);
xs_rm(ctx->xsh, XBT_NULL, GCSPRINTF("/local/pool/%d", poolid));
if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
break;
}
rc = 0;
out1:
libxl_bitmap_dispose(&cpumap);
out:
xc_cpupool_infofree(ctx->xch, info);
GC_FREE;
return rc;
}
int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
{
GC_INIT(ctx);
xs_transaction_t t;
xc_cpupoolinfo_t *info;
int rc;
info = xc_cpupool_getinfo(ctx->xch, poolid);
if (info == NULL) {
GC_FREE;
return ERROR_NOMEM;
}
rc = ERROR_INVAL;
if (info->cpupool_id != poolid)
goto out;
rc = 0;
for (;;) {
t = xs_transaction_start(ctx->xsh);
libxl__xs_printf(gc, t,
GCSPRINTF("/local/pool/%d/name", poolid),
"%s", name);
if (xs_transaction_end(ctx->xsh, t, 0))
break;
if (errno == EAGAIN)
continue;
rc = ERROR_FAIL;
break;
}
out:
xc_cpupool_infofree(ctx->xch, info);
GC_FREE;
return rc;
}
int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
{
GC_INIT(ctx);
int rc = 0;
rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
if (rc) {
LOGE(ERROR, "Error moving cpu %d to cpupool", cpu);
rc = ERROR_FAIL;
}
GC_FREE;
return rc;
}
int libxl_cpupool_cpuadd_cpumap(libxl_ctx *ctx, uint32_t poolid,
const libxl_bitmap *cpumap)
{
int c, ncpus = 0, rc = 0;
libxl_for_each_set_bit(c, *cpumap) {
if (!libxl_cpupool_cpuadd(ctx, poolid, c))
ncpus++;
}
if (ncpus != libxl_bitmap_count_set(cpumap))
rc = ERROR_FAIL;
return rc;
}
int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
{
int rc = 0;
int cpu, nr;
libxl_bitmap freemap;
libxl_cputopology *topology;
if (libxl_get_freecpus(ctx, &freemap)) {
return ERROR_FAIL;
}
topology = libxl_get_cpu_topology(ctx, &nr);
if (!topology) {
rc = ERROR_FAIL;
goto out;
}
*cpus = 0;
for (cpu = 0; cpu < nr; cpu++) {
if (libxl_bitmap_test(&freemap, cpu) && (topology[cpu].node == node) &&
!libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
(*cpus)++;
}
libxl_cputopology_dispose(&topology[cpu]);
}
free(topology);
out:
libxl_bitmap_dispose(&freemap);
return rc;
}
int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
{
GC_INIT(ctx);
int rc = 0;
rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
if (rc) {
LOGE(ERROR, "Error removing cpu %d from cpupool", cpu);
rc = ERROR_FAIL;
}
GC_FREE;
return rc;
}
int libxl_cpupool_cpuremove_cpumap(libxl_ctx *ctx, uint32_t poolid,
const libxl_bitmap *cpumap)
{
int c, ncpus = 0, rc = 0;
libxl_for_each_set_bit(c, *cpumap) {
if (!libxl_cpupool_cpuremove(ctx, poolid, c))
ncpus++;
}
if (ncpus != libxl_bitmap_count_set(cpumap))
rc = ERROR_FAIL;
return rc;
}
int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
{
int ret = 0;
int n_pools;
int p;
int cpu, nr_cpus;
libxl_cputopology *topology;
libxl_cpupoolinfo *poolinfo;
poolinfo = libxl_list_cpupool(ctx, &n_pools);
if (!poolinfo) {
return ERROR_NOMEM;
}
topology = libxl_get_cpu_topology(ctx, &nr_cpus);
if (!topology) {
ret = ERROR_FAIL;
goto out;
}
*cpus = 0;
for (p = 0; p < n_pools; p++) {
if (poolinfo[p].poolid == poolid) {
for (cpu = 0; cpu < nr_cpus; cpu++) {
if ((topology[cpu].node == node) &&
libxl_bitmap_test(&poolinfo[p].cpumap, cpu) &&
!libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
(*cpus)++;
}
}
}
}
libxl_cputopology_list_free(topology, nr_cpus);
out:
libxl_cpupoolinfo_list_free(poolinfo, n_pools);
return ret;
}
int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
{
GC_INIT(ctx);
int rc;
rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
if (rc) {
LOGEVD(ERROR, rc, domid, "Error moving domain to cpupool");
GC_FREE;
return ERROR_FAIL;
}
GC_FREE;
return 0;
}
/*
* Local variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
|