numa_conf.c 24.7 KB
Newer Older
1
/*
2
 * numa_conf.c
3
 *
4
 * Copyright (C) 2014-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library 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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Martin Kletzander <mkletzan@redhat.com>
 */

#include <config.h>

25
#include "numa_conf.h"
26

27 28
#include "domain_conf.h"
#include "viralloc.h"
29
#include "virnuma.h"
30 31 32 33
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_DOMAIN

34 35 36 37 38 39
VIR_ENUM_IMPL(virDomainNumatuneMemMode,
              VIR_DOMAIN_NUMATUNE_MEM_LAST,
              "strict",
              "preferred",
              "interleave");

40 41
VIR_ENUM_IMPL(virDomainNumatunePlacement,
              VIR_DOMAIN_NUMATUNE_PLACEMENT_LAST,
42 43 44
              "default",
              "static",
              "auto");
45

46 47 48 49 50
VIR_ENUM_IMPL(virNumaMemAccess, VIR_NUMA_MEM_ACCESS_LAST,
              "default",
              "shared",
              "private");

51 52
typedef struct _virDomainNumaNode virDomainNumaNode;
typedef virDomainNumaNode *virDomainNumaNodePtr;
53

54
struct _virDomainNuma {
55
    struct {
56
        bool specified;
57 58 59 60 61
        virBitmapPtr nodeset;
        virDomainNumatuneMemMode mode;
        virDomainNumatunePlacement placement;
    } memory;               /* pinning for all the memory */

62
    struct _virDomainNumaNode {
63 64 65 66 67 68
        unsigned long long mem; /* memory size in KiB */
        virBitmapPtr cpumask;   /* bitmap of vCPUs corresponding to the node */
        virBitmapPtr nodeset;   /* host memory nodes where this guest node resides */
        virDomainNumatuneMemMode mode;  /* memory mode selection */
        virNumaMemAccess memAccess; /* shared memory access configuration */
    } *mem_nodes;           /* guest node configuration */
69 70
    size_t nmem_nodes;

71 72 73 74
    /* Future NUMA tuning related stuff should go here. */
};


75
inline bool
76
virDomainNumatuneNodeSpecified(virDomainNumaPtr numatune,
77 78 79 80 81 82 83 84 85 86
                               int cellid)
{
    if (numatune &&
        cellid >= 0 &&
        cellid < numatune->nmem_nodes)
        return numatune->mem_nodes[cellid].nodeset;

    return false;
}

87
static int
88
virDomainNumatuneNodeParseXML(virDomainNumaPtr numa,
89 90 91
                              xmlXPathContextPtr ctxt)
{
    char *tmp = NULL;
E
Eric Blake 已提交
92
    int n = 0;
93 94 95 96 97 98 99 100 101 102 103 104 105
    int ret = -1;
    size_t i = 0;
    xmlNodePtr *nodes = NULL;

    if ((n = virXPathNodeSet("./numatune/memnode", ctxt, &nodes)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot extract memnode nodes"));
        goto cleanup;
    }

    if (!n)
        return 0;

106 107
    if (numa->memory.specified &&
        numa->memory.placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO) {
108 109 110 111 112 113
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Per-node binding is not compatible with "
                         "automatic NUMA placement."));
        goto cleanup;
    }

114
    if (!numa->nmem_nodes) {
115 116 117 118 119 120 121 122 123
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("Element 'memnode' is invalid without "
                         "any guest NUMA cells"));
        goto cleanup;
    }

    for (i = 0; i < n; i++) {
        int mode = 0;
        unsigned int cellid = 0;
124
        virDomainNumaNodePtr mem_node = NULL;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        xmlNodePtr cur_node = nodes[i];

        tmp = virXMLPropString(cur_node, "cellid");
        if (!tmp) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing required cellid attribute "
                             "in memnode element"));
            goto cleanup;
        }
        if (virStrToLong_uip(tmp, NULL, 10, &cellid) < 0) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("Invalid cellid attribute in memnode element: %s"),
                           tmp);
            goto cleanup;
        }
        VIR_FREE(tmp);

142
        if (cellid >= numa->nmem_nodes) {
143 144 145 146 147 148
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Argument 'cellid' in memnode element must "
                             "correspond to existing guest's NUMA cell"));
            goto cleanup;
        }

149
        mem_node = &numa->mem_nodes[cellid];
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

        if (mem_node->nodeset) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("Multiple memnode elements with cellid %u"),
                           cellid);
            goto cleanup;
        }

        tmp = virXMLPropString(cur_node, "mode");
        if (!tmp) {
            mem_node->mode = VIR_DOMAIN_NUMATUNE_MEM_STRICT;
        } else {
            if ((mode = virDomainNumatuneMemModeTypeFromString(tmp)) < 0) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("Invalid mode attribute in memnode element"));
                goto cleanup;
            }
            VIR_FREE(tmp);
            mem_node->mode = mode;
        }

        tmp = virXMLPropString(cur_node, "nodeset");
        if (!tmp) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing required nodeset attribute "
                             "in memnode element"));
            goto cleanup;
        }
        if (virBitmapParse(tmp, 0, &mem_node->nodeset,
                           VIR_DOMAIN_CPUMASK_LEN) < 0)
            goto cleanup;
        VIR_FREE(tmp);
    }

    ret = 0;
 cleanup:
    VIR_FREE(nodes);
    VIR_FREE(tmp);
    return ret;
}

191
int
192
virDomainNumatuneParseXML(virDomainNumaPtr numa,
193
                          bool placement_static,
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                          xmlXPathContextPtr ctxt)
{
    char *tmp = NULL;
    int mode = -1;
    int n = 0;
    int placement = -1;
    int ret = -1;
    virBitmapPtr nodeset = NULL;
    xmlNodePtr node = NULL;

    if (virXPathInt("count(./numatune)", ctxt, &n) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot extract numatune nodes"));
        goto cleanup;
    } else if (n > 1) {
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("only one numatune is supported"));
        goto cleanup;
    }

    node = virXPathNode("./numatune/memory[1]", ctxt);

216 217 218 219 220 221 222 223
    if (!placement_static && !node)
        placement = VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO;

    if (node) {
        if ((tmp = virXMLPropString(node, "mode")) &&
            (mode = virDomainNumatuneMemModeTypeFromString(tmp)) < 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported NUMA memory tuning mode '%s'"), tmp);
224
            goto cleanup;
225 226
        }
        VIR_FREE(tmp);
227

228 229 230 231 232 233 234
        if ((tmp = virXMLPropString(node, "placement")) &&
            (placement = virDomainNumatunePlacementTypeFromString(tmp)) < 0) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported NUMA memory placement mode '%s'"), tmp);
            goto cleanup;
        }
        VIR_FREE(tmp);
235

236 237 238 239
        if ((tmp = virXMLPropString(node, "nodeset")) &&
            virBitmapParse(tmp, 0, &nodeset, VIR_DOMAIN_CPUMASK_LEN) < 0)
            goto cleanup;
        VIR_FREE(tmp);
240 241
    }

242
    if (virDomainNumatuneSet(numa,
243 244 245 246
                             placement_static,
                             placement,
                             mode,
                             nodeset) < 0)
247 248
        goto cleanup;

249
    if (virDomainNumatuneNodeParseXML(numa, ctxt) < 0)
250
        goto cleanup;
251

252 253 254 255 256 257 258 259 260
    ret = 0;
 cleanup:
    virBitmapFree(nodeset);
    VIR_FREE(tmp);
    return ret;
}

int
virDomainNumatuneFormatXML(virBufferPtr buf,
261
                           virDomainNumaPtr numatune)
262 263 264
{
    const char *tmp = NULL;
    char *nodeset = NULL;
265
    bool nodesetSpecified = false;
266
    size_t i = 0;
267 268 269 270

    if (!numatune)
        return 0;

271 272 273 274 275 276 277 278 279 280
    for (i = 0; i < numatune->nmem_nodes; i++) {
        if (numatune->mem_nodes[i].nodeset) {
            nodesetSpecified = true;
            break;
        }
    }

    if (!nodesetSpecified && !numatune->memory.specified)
        return 0;

281 282 283
    virBufferAddLit(buf, "<numatune>\n");
    virBufferAdjustIndent(buf, 2);

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    if (numatune->memory.specified) {
        tmp = virDomainNumatuneMemModeTypeToString(numatune->memory.mode);
        virBufferAsprintf(buf, "<memory mode='%s' ", tmp);

        if (numatune->memory.placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC) {
            if (!(nodeset = virBitmapFormat(numatune->memory.nodeset)))
                return -1;
            virBufferAsprintf(buf, "nodeset='%s'/>\n", nodeset);
            VIR_FREE(nodeset);
        } else if (numatune->memory.placement) {
            tmp = virDomainNumatunePlacementTypeToString(numatune->memory.placement);
            virBufferAsprintf(buf, "placement='%s'/>\n", tmp);
        }
    }

    for (i = 0; i < numatune->nmem_nodes; i++) {
300
        virDomainNumaNodePtr mem_node = &numatune->mem_nodes[i];
301

302 303 304 305
        if (!mem_node->nodeset)
            continue;

        if (!(nodeset = virBitmapFormat(mem_node->nodeset)))
306
            return -1;
307 308 309 310 311 312

        virBufferAsprintf(buf,
                          "<memnode cellid='%zu' mode='%s' nodeset='%s'/>\n",
                          i,
                          virDomainNumatuneMemModeTypeToString(mem_node->mode),
                          nodeset);
313 314 315 316 317 318 319 320 321
        VIR_FREE(nodeset);
    }

    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</numatune>\n");
    return 0;
}

void
322
virDomainNumaFree(virDomainNumaPtr numa)
323
{
324 325
    size_t i = 0;

326
    if (!numa)
327 328
        return;

329
    virBitmapFree(numa->memory.nodeset);
330 331
    for (i = 0; i < numa->nmem_nodes; i++) {
        virBitmapFree(numa->mem_nodes[i].cpumask);
332
        virBitmapFree(numa->mem_nodes[i].nodeset);
333
    }
334
    VIR_FREE(numa->mem_nodes);
335

336
    VIR_FREE(numa);
337 338 339
}

virDomainNumatuneMemMode
340
virDomainNumatuneGetMode(virDomainNumaPtr numatune,
341
                         int cellid)
342
{
343 344 345 346 347 348 349 350 351 352
    if (!numatune)
        return 0;

    if (virDomainNumatuneNodeSpecified(numatune, cellid))
        return numatune->mem_nodes[cellid].mode;

    if (numatune->memory.specified)
        return numatune->memory.mode;

    return 0;
353 354 355
}

virBitmapPtr
356
virDomainNumatuneGetNodeset(virDomainNumaPtr numatune,
357 358
                            virBitmapPtr auto_nodeset,
                            int cellid)
359 360 361 362
{
    if (!numatune)
        return NULL;

363 364
    if (numatune->memory.specified &&
        numatune->memory.placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO)
365 366
        return auto_nodeset;

367 368 369 370
    if (virDomainNumatuneNodeSpecified(numatune, cellid))
        return numatune->mem_nodes[cellid].nodeset;

    if (!numatune->memory.specified)
371 372 373 374 375 376
        return NULL;

    return numatune->memory.nodeset;
}

char *
377
virDomainNumatuneFormatNodeset(virDomainNumaPtr numatune,
378 379
                               virBitmapPtr auto_nodeset,
                               int cellid)
380 381
{
    return virBitmapFormat(virDomainNumatuneGetNodeset(numatune,
382 383
                                                       auto_nodeset,
                                                       cellid));
384 385
}

386

387
int
388
virDomainNumatuneMaybeGetNodeset(virDomainNumaPtr numatune,
389 390 391
                                 virBitmapPtr auto_nodeset,
                                 virBitmapPtr *retNodeset,
                                 int cellid)
392
{
393
    *retNodeset = NULL;
394 395 396 397

    if (!numatune)
        return 0;

398 399 400 401 402 403
    if (!virDomainNumatuneNodeSpecified(numatune, cellid) &&
        !numatune->memory.specified)
        return 0;

    if (numatune->memory.specified &&
        numatune->memory.placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO &&
404 405 406 407 408 409 410
        !auto_nodeset) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Advice from numad is needed in case of "
                         "automatic numa placement"));
        return -1;
    }

411 412 413 414 415 416 417
    *retNodeset = virDomainNumatuneGetNodeset(numatune, auto_nodeset, cellid);

    return 0;
}


int
418
virDomainNumatuneMaybeFormatNodeset(virDomainNumaPtr numatune,
419 420 421 422 423 424 425 426 427 428 429 430
                                    virBitmapPtr auto_nodeset,
                                    char **mask,
                                    int cellid)
{
    virBitmapPtr nodeset;

    if (virDomainNumatuneMaybeGetNodeset(numatune, auto_nodeset, &nodeset,
                                         cellid) < 0)
        return -1;

    if (nodeset &&
        !(*mask = virBitmapFormat(nodeset)))
431 432 433 434 435 436
        return -1;

    return 0;
}

int
437
virDomainNumatuneSet(virDomainNumaPtr numa,
438
                     bool placement_static,
439 440 441 442 443 444 445 446 447 448
                     int placement,
                     int mode,
                     virBitmapPtr nodeset)
{
    int ret = -1;

    /* No need to do anything in this case */
    if (mode == -1 && placement == -1 && !nodeset)
        return 0;

449
    if (!numa->memory.specified) {
450 451 452 453 454 455
        if (mode == -1)
            mode = VIR_DOMAIN_NUMATUNE_MEM_STRICT;
        if (placement == -1)
            placement = VIR_DOMAIN_NUMATUNE_PLACEMENT_DEFAULT;
    }

456 457 458 459 460 461 462 463
    /* Range checks */
    if (mode != -1 &&
        (mode < 0 || mode >= VIR_DOMAIN_NUMATUNE_MEM_LAST)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported numatune mode '%d'"),
                       mode);
        goto cleanup;
    }
464

465 466 467 468 469 470 471 472 473
    if (placement != -1 &&
        (placement < 0 || placement >= VIR_DOMAIN_NUMATUNE_PLACEMENT_LAST)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported numatune placement '%d'"),
                       mode);
        goto cleanup;
    }

    if (mode != -1)
474
        numa->memory.mode = mode;
475

476
    if (nodeset) {
477 478
        virBitmapFree(numa->memory.nodeset);
        if (!(numa->memory.nodeset = virBitmapNewCopy(nodeset)))
479 480 481 482 483 484
            goto cleanup;
        if (placement == -1)
            placement = VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC;
    }

    if (placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_DEFAULT) {
485
        if (numa->memory.nodeset || placement_static)
486 487 488 489 490 491
            placement = VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC;
        else
            placement = VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO;
    }

    if (placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_STATIC &&
492
        !numa->memory.nodeset) {
493 494 495 496 497 498
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("nodeset for NUMA memory tuning must be set "
                         "if 'placement' is 'static'"));
        goto cleanup;
    }

499 500
    /* setting nodeset when placement auto is invalid */
    if (placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO &&
501 502 503
        numa->memory.nodeset) {
        virBitmapFree(numa->memory.nodeset);
        numa->memory.nodeset = NULL;
504 505
    }

506
    if (placement != -1)
507
        numa->memory.placement = placement;
508

509
    numa->memory.specified = true;
510

511
    ret = 0;
512

513 514 515 516
 cleanup:
    return ret;
}

517
static bool
518 519
virDomainNumaNodesEqual(virDomainNumaPtr n1,
                        virDomainNumaPtr n2)
520 521 522 523 524 525 526
{
    size_t i = 0;

    if (n1->nmem_nodes != n2->nmem_nodes)
        return false;

    for (i = 0; i < n1->nmem_nodes; i++) {
527 528
        virDomainNumaNodePtr nd1 = &n1->mem_nodes[i];
        virDomainNumaNodePtr nd2 = &n2->mem_nodes[i];
529 530 531 532 533 534 535 536 537 538 539 540 541 542

        if (!nd1->nodeset && !nd2->nodeset)
            continue;

        if (nd1->mode != nd2->mode)
            return false;

        if (!virBitmapEqual(nd1->nodeset, nd2->nodeset))
            return false;
    }

    return true;
}

543
bool
544 545
virDomainNumaEquals(virDomainNumaPtr n1,
                    virDomainNumaPtr n2)
546 547 548 549 550 551 552
{
    if (!n1 && !n2)
        return true;

    if (!n1 || !n2)
        return false;

553
    if (!n1->memory.specified && !n2->memory.specified)
554
        return virDomainNumaNodesEqual(n1, n2);
555 556 557 558

    if (!n1->memory.specified || !n2->memory.specified)
        return false;

559 560 561 562 563 564
    if (n1->memory.mode != n2->memory.mode)
        return false;

    if (n1->memory.placement != n2->memory.placement)
        return false;

565 566 567
    if (!virBitmapEqual(n1->memory.nodeset, n2->memory.nodeset))
        return false;

568
    return virDomainNumaNodesEqual(n1, n2);
569 570 571
}

bool
572
virDomainNumatuneHasPlacementAuto(virDomainNumaPtr numatune)
573 574 575 576
{
    if (!numatune)
        return false;

577 578 579
    if (!numatune->memory.specified)
        return false;

580 581 582 583 584
    if (numatune->memory.placement == VIR_DOMAIN_NUMATUNE_PLACEMENT_AUTO)
        return true;

    return false;
}
585 586

bool
587
virDomainNumatuneHasPerNodeBinding(virDomainNumaPtr numatune)
588 589 590 591 592 593 594 595 596 597 598 599 600
{
    size_t i = 0;

    if (!numatune)
        return false;

    for (i = 0; i < numatune->nmem_nodes; i++) {
        if (numatune->mem_nodes[i].nodeset)
            return true;
    }

    return false;
}
601 602

int
603
virDomainNumatuneSpecifiedMaxNode(virDomainNumaPtr numatune)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
{
    int ret = -1;
    virBitmapPtr nodemask = NULL;
    size_t i;
    int bit;

    if (!numatune)
        return ret;

    nodemask = virDomainNumatuneGetNodeset(numatune, NULL, -1);
    if (nodemask)
        ret = virBitmapLastSetBit(nodemask);

    for (i = 0; i < numatune->nmem_nodes; i++) {
        nodemask = numatune->mem_nodes[i].nodeset;
        if (!nodemask)
            continue;

        bit = virBitmapLastSetBit(nodemask);
        if (bit > ret)
            ret = bit;
    }

    return ret;
}
629 630

bool
631
virDomainNumatuneNodesetIsAvailable(virDomainNumaPtr numatune,
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
                                    virBitmapPtr auto_nodeset)
{
    size_t i = 0;
    virBitmapPtr b = NULL;

    if (!numatune)
        return true;

    b = virDomainNumatuneGetNodeset(numatune, auto_nodeset, -1);
    if (!virNumaNodesetIsAvailable(b))
        return false;

    for (i = 0; i < numatune->nmem_nodes; i++) {
        b = virDomainNumatuneGetNodeset(numatune, auto_nodeset, i);
        if (!virNumaNodesetIsAvailable(b))
            return false;
    }

    return true;
}
652 653 654


int
655
virDomainNumaDefCPUParseXML(virDomainNumaPtr def,
656 657 658 659
                            xmlXPathContextPtr ctxt)
{
    xmlNodePtr *nodes = NULL;
    xmlNodePtr oldNode = ctxt->node;
660
    char *tmp = NULL;
661 662 663 664
    int n;
    size_t i;
    int ret = -1;

665
    /* check if NUMA definition is present */
666
    if (!virXPathNode("./cpu/numa[1]", ctxt))
667
        return 0;
668

669
    if ((n = virXPathNodeSet("./cpu/numa[1]/cell", ctxt, &nodes)) <= 0) {
670 671 672 673 674
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("NUMA topology defined without NUMA cells"));
        goto cleanup;
    }

675
    if (VIR_ALLOC_N(def->mem_nodes, n) < 0)
676
        goto cleanup;
677
    def->nmem_nodes = n;
678

679
    for (i = 0; i < n; i++) {
680
        int rc;
681 682 683 684 685 686 687 688 689
        unsigned int cur_cell = i;

        /* cells are in order of parsing or explicitly numbered */
        if ((tmp = virXMLPropString(nodes[i], "id"))) {
            if (virStrToLong_uip(tmp, NULL, 10, &cur_cell) < 0) {
                virReportError(VIR_ERR_XML_ERROR,
                               _("Invalid 'id' attribute in NUMA cell: '%s'"),
                               tmp);
                goto cleanup;
690 691 692 693 694 695 696
            }

            if (cur_cell >= n) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                               _("Exactly one 'cell' element per guest "
                                 "NUMA cell allowed, non-contiguous ranges or "
                                 "ranges not starting from 0 are not allowed"));
697
                goto cleanup;
698
            }
699 700
        }
        VIR_FREE(tmp);
701

702
        if (def->mem_nodes[cur_cell].cpumask) {
703 704 705 706 707
            virReportError(VIR_ERR_XML_ERROR,
                           _("Duplicate NUMA cell info for cell id '%u'"),
                           cur_cell);
            goto cleanup;
        }
708

709 710 711 712 713
        if (!(tmp = virXMLPropString(nodes[i], "cpus"))) {
            virReportError(VIR_ERR_XML_ERROR, "%s",
                           _("Missing 'cpus' attribute in NUMA cell"));
            goto cleanup;
        }
714

715
        if (virBitmapParse(tmp, 0, &def->mem_nodes[cur_cell].cpumask,
716
                           VIR_DOMAIN_CPUMASK_LEN) < 0)
717
            goto cleanup;
718

719
        if (virBitmapIsAllClear(def->mem_nodes[cur_cell].cpumask)) {
720 721 722 723
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                          _("NUMA cell %d has no vCPUs assigned"), cur_cell);
            goto cleanup;
        }
724
        VIR_FREE(tmp);
725

726 727
        ctxt->node = nodes[i];
        if (virDomainParseMemory("./@memory", "./@unit", ctxt,
728
                                 &def->mem_nodes[cur_cell].mem, true, false) < 0)
729
            goto cleanup;
730

731
        if ((tmp = virXMLPropString(nodes[i], "memAccess"))) {
732
            if ((rc = virNumaMemAccessTypeFromString(tmp)) <= 0) {
733 734 735 736
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("Invalid 'memAccess' attribute value '%s'"),
                               tmp);
                goto cleanup;
737
            }
738

739
            def->mem_nodes[cur_cell].memAccess = rc;
740
            VIR_FREE(tmp);
741 742 743 744 745 746 747 748
        }
    }

    ret = 0;

 cleanup:
    ctxt->node = oldNode;
    VIR_FREE(nodes);
749
    VIR_FREE(tmp);
750 751
    return ret;
}
752 753 754 755


int
virDomainNumaDefCPUFormat(virBufferPtr buf,
756
                          virDomainNumaPtr def)
757
{
758
    virNumaMemAccess memAccess;
759
    char *cpustr;
760
    size_t ncells = virDomainNumaGetNodeCount(def);
761 762
    size_t i;

763
    if (ncells == 0)
764 765 766 767
        return 0;

    virBufferAddLit(buf, "<numa>\n");
    virBufferAdjustIndent(buf, 2);
768
    for (i = 0; i < ncells; i++) {
769
        memAccess = virDomainNumaGetNodeMemoryAccessMode(def, i);
770

771
        if (!(cpustr = virBitmapFormat(virDomainNumaGetNodeCpumask(def, i))))
772 773 774 775 776
            return -1;

        virBufferAddLit(buf, "<cell");
        virBufferAsprintf(buf, " id='%zu'", i);
        virBufferAsprintf(buf, " cpus='%s'", cpustr);
777 778
        virBufferAsprintf(buf, " memory='%llu'",
                          virDomainNumaGetNodeMemorySize(def, i));
779 780 781
        virBufferAddLit(buf, " unit='KiB'");
        if (memAccess)
            virBufferAsprintf(buf, " memAccess='%s'",
782
                              virNumaMemAccessTypeToString(memAccess));
783 784 785 786 787 788 789 790
        virBufferAddLit(buf, "/>\n");
        VIR_FREE(cpustr);
    }
    virBufferAdjustIndent(buf, -2);
    virBufferAddLit(buf, "</numa>\n");

    return 0;
}
791 792 793


unsigned int
794
virDomainNumaGetCPUCountTotal(virDomainNumaPtr numa)
795 796 797 798
{
    size_t i;
    unsigned int ret = 0;

799
    for (i = 0; i < numa->nmem_nodes; i++)
800
        ret += virBitmapCountBits(virDomainNumaGetNodeCpumask(numa, i));
801 802 803

    return ret;
}
804 805 806 807 808 809 810 811 812 813 814


virDomainNumaPtr
virDomainNumaNew(void)
{
    virDomainNumaPtr ret = NULL;

    ignore_value(VIR_ALLOC(ret));

    return ret;
}
815 816


817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
bool
virDomainNumaCheckABIStability(virDomainNumaPtr src,
                               virDomainNumaPtr tgt)
{
    size_t i;

    if (virDomainNumaGetNodeCount(src) != virDomainNumaGetNodeCount(tgt)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Target NUMA node count '%zu' doesn't match "
                         "source '%zu'"),
                       virDomainNumaGetNodeCount(tgt),
                       virDomainNumaGetNodeCount(src));
        return false;
    }

    for (i = 0; i < virDomainNumaGetNodeCount(src); i++) {
        if (virDomainNumaGetNodeMemorySize(src, i) !=
            virDomainNumaGetNodeMemorySize(tgt, i)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Size of target NUMA node %zu (%llu) doesn't "
                             "match source (%llu)"), i,
                           virDomainNumaGetNodeMemorySize(tgt, i),
                           virDomainNumaGetNodeMemorySize(src, i));
            return false;
        }

        if (!virBitmapEqual(virDomainNumaGetNodeCpumask(src, i),
                            virDomainNumaGetNodeCpumask(tgt, i))) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Processor mask of target NUMA node %zu doesn't "
                             "match source"), i);
            return false;
        }
    }

    return true;
}


856
size_t
857
virDomainNumaGetNodeCount(virDomainNumaPtr numa)
858 859 860 861
{
    if (!numa)
        return 0;

862
    return numa->nmem_nodes;
863
}
864 865 866


virBitmapPtr
867
virDomainNumaGetNodeCpumask(virDomainNumaPtr numa,
868 869
                            size_t node)
{
870
    return numa->mem_nodes[node].cpumask;
871
}
872 873 874


virNumaMemAccess
875
virDomainNumaGetNodeMemoryAccessMode(virDomainNumaPtr numa,
876 877
                                     size_t node)
{
878
    return numa->mem_nodes[node].memAccess;
879
}
880 881 882


unsigned long long
883
virDomainNumaGetNodeMemorySize(virDomainNumaPtr numa,
884 885
                               size_t node)
{
886
    return numa->mem_nodes[node].mem;
887 888 889 890
}


void
891
virDomainNumaSetNodeMemorySize(virDomainNumaPtr numa,
892 893 894
                               size_t node,
                               unsigned long long size)
{
895
    numa->mem_nodes[node].mem = size;
896
}
897 898 899 900 901 902 903 904 905 906 907 908 909


unsigned long long
virDomainNumaGetMemorySize(virDomainNumaPtr numa)
{
    size_t i;
    unsigned long long ret = 0;

    for (i = 0; i < numa->nmem_nodes; i++)
        ret += numa->mem_nodes[i].mem;

    return ret;
}