コード例 #1
0
ファイル: cpu_conf.c プロジェクト: czchen/debian-libvirt
int
virCPUDefFormatBufFull(virBufferPtr buf,
                       virCPUDefPtr def,
                       unsigned int flags)
{
    if (!def)
        return 0;

    virBufferAddLit(buf, "<cpu");
    if (def->type == VIR_CPU_TYPE_GUEST) {
        const char *tmp;

        if (def->mode != VIR_CPU_MODE_CUSTOM || def->model) {
            if (!(tmp = virCPUModeTypeToString(def->mode))) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU mode %d"), def->mode);
                return -1;
            }
            virBufferAsprintf(buf, " mode='%s'", tmp);
        }

        if (def->model &&
            (def->mode == VIR_CPU_MODE_CUSTOM ||
             (flags & VIR_DOMAIN_XML_UPDATE_CPU))) {
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
                return -1;
            }
            virBufferAsprintf(buf, " match='%s'", tmp);
        }
    }
    virBufferAddLit(buf, ">\n");

    if (def->arch)
        virBufferAsprintf(buf, "  <arch>%s</arch>\n",
                          virArchToString(def->arch));

    virBufferAdjustIndent(buf, 2);
    if (virCPUDefFormatBuf(buf, def, flags) < 0)
        return -1;
    virBufferAdjustIndent(buf, -2);

    virBufferAddLit(buf, "</cpu>\n");

    return 0;
}
コード例 #2
0
ファイル: cpu_conf.c プロジェクト: hw-claudio/libvirt
int
virCPUDefFormatBuf(virBufferPtr buf,
                   virCPUDefPtr def,
                   const char *indent,
                   int flags)
{
    unsigned int i;

    if (!def)
        return 0;

    if (indent == NULL)
        indent = "";

    if (!def->model && def->nfeatures) {
        virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                "%s", _("Non-empty feature list specified without CPU model"));
        return -1;
    }

    if (!(flags & VIR_CPU_FORMAT_EMBEDED)) {
        if (def->type == VIR_CPU_TYPE_GUEST && def->model) {
            const char *match;
            if (!(match = virCPUMatchTypeToString(def->match))) {
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unexpected CPU match policy %d"), def->match);
                return -1;
            }

            virBufferAsprintf(buf, "%s<cpu match='%s'>\n", indent, match);
        }
        else
            virBufferAsprintf(buf, "%s<cpu>\n", indent);

        if (def->arch)
            virBufferAsprintf(buf, "%s  <arch>%s</arch>\n", indent, def->arch);
    }

    if (def->model)
        virBufferAsprintf(buf, "%s  <model>%s</model>\n", indent, def->model);

    if (def->vendor) {
        virBufferAsprintf(buf, "%s  <vendor>%s</vendor>\n",
                          indent, def->vendor);
    }

    if (def->sockets && def->cores && def->threads) {
        virBufferAsprintf(buf, "%s  <topology", indent);
        virBufferAsprintf(buf, " sockets='%u'", def->sockets);
        virBufferAsprintf(buf, " cores='%u'", def->cores);
        virBufferAsprintf(buf, " threads='%u'", def->threads);
        virBufferAddLit(buf, "/>\n");
    }

    for (i = 0 ; i < def->nfeatures ; i++) {
        virCPUFeatureDefPtr feature = def->features + i;

        if (!feature->name) {
            virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("Missing CPU feature name"));
            return -1;
        }

        if (def->type == VIR_CPU_TYPE_GUEST) {
            const char *policy;

            policy = virCPUFeaturePolicyTypeToString(feature->policy);
            if (!policy) {
                virCPUReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unexpected CPU feature policy %d"), feature->policy);
                return -1;
            }
            virBufferAsprintf(buf, "%s  <feature policy='%s' name='%s'/>\n",
                    indent, policy, feature->name);
        }
        else {
            virBufferAsprintf(buf, "%s  <feature name='%s'/>\n",
                    indent, feature->name);
        }
    }

    if (!(flags & VIR_CPU_FORMAT_EMBEDED))
        virBufferAsprintf(buf, "%s</cpu>\n", indent);

    return 0;
}
コード例 #3
0
ファイル: cpu_conf.c プロジェクト: libvirt/libvirt
int
virCPUDefFormatBufFull(virBufferPtr buf,
                       virCPUDefPtr def,
                       virDomainNumaPtr numa)
{
    int ret = -1;
    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;

    if (!def)
        return 0;

    /* Format attributes for guest CPUs unless they only specify
     * topology or cache. */
    if (def->type == VIR_CPU_TYPE_GUEST &&
        (def->mode != VIR_CPU_MODE_CUSTOM || def->model)) {
        const char *tmp;

        if (!(tmp = virCPUModeTypeToString(def->mode))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unexpected CPU mode %d"), def->mode);
            goto cleanup;
        }
        virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);

        if (def->mode == VIR_CPU_MODE_CUSTOM) {
            if (!(tmp = virCPUMatchTypeToString(def->match))) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Unexpected CPU match policy %d"),
                               def->match);
                goto cleanup;
            }
            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
        }

        if (def->check) {
            virBufferAsprintf(&attributeBuf, " check='%s'",
                              virCPUCheckTypeToString(def->check));
        }
    }

    /* Format children */
    virBufferSetChildIndent(&childrenBuf, buf);
    if (def->type == VIR_CPU_TYPE_HOST && def->arch)
        virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
                          virArchToString(def->arch));
    if (virCPUDefFormatBuf(&childrenBuf, def) < 0)
        goto cleanup;

    if (virDomainNumaDefCPUFormatXML(&childrenBuf, numa) < 0)
        goto cleanup;

    if (virBufferCheckError(&attributeBuf) < 0 ||
        virBufferCheckError(&childrenBuf) < 0)
        goto cleanup;

    /* Put it all together */
    if (virBufferUse(&attributeBuf) || virBufferUse(&childrenBuf)) {
        virBufferAddLit(buf, "<cpu");

        if (virBufferUse(&attributeBuf))
            virBufferAddBuffer(buf, &attributeBuf);

        if (virBufferUse(&childrenBuf)) {
            virBufferAddLit(buf, ">\n");
            virBufferAddBuffer(buf, &childrenBuf);
            virBufferAddLit(buf, "</cpu>\n");
        } else {
            virBufferAddLit(buf, "/>\n");
        }
    }

    ret = 0;
 cleanup:
    virBufferFreeAndReset(&attributeBuf);
    virBufferFreeAndReset(&childrenBuf);
    return ret;
}