netdev_bandwidth_conf.c 8.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2009-2015 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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
15
 * License along with this library.  If not, see
O
Osier Yang 已提交
16
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 *
 * Authors:
 *     Michal Privoznik <mprivozn@redhat.com>
 *     Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include "netdev_bandwidth_conf.h"
26
#include "virerror.h"
27
#include "viralloc.h"
28
#include "virstring.h"
29 30 31 32 33 34 35 36 37 38

#define VIR_FROM_THIS VIR_FROM_NONE

static int
virNetDevBandwidthParseRate(xmlNodePtr node, virNetDevBandwidthRatePtr rate)
{
    int ret = -1;
    char *average = NULL;
    char *peak = NULL;
    char *burst = NULL;
39
    char *floor = NULL;
40 41

    if (!node || !rate) {
42
        virReportError(VIR_ERR_INVALID_ARG, "%s",
43 44 45 46 47 48 49
                       _("invalid argument supplied"));
        return -1;
    }

    average = virXMLPropString(node, "average");
    peak = virXMLPropString(node, "peak");
    burst = virXMLPropString(node, "burst");
50
    floor = virXMLPropString(node, "floor");
51 52

    if (average) {
53
        if (virStrToLong_ullp(average, NULL, 10, &rate->average) < 0) {
54
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
55
                           _("could not convert bandwidth average value '%s'"),
56 57 58
                           average);
            goto cleanup;
        }
59
    } else if (!floor) {
60
        virReportError(VIR_ERR_XML_DETAIL, "%s",
61 62 63 64 65 66 67
                       _("Missing mandatory average or floor attributes"));
        goto cleanup;
    }

    if ((peak || burst) && !average) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("'peak' and 'burst' require 'average' attribute"));
68 69 70
        goto cleanup;
    }

71
    if (peak && virStrToLong_ullp(peak, NULL, 10, &rate->peak) < 0) {
72
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
73
                       _("could not convert bandwidth peak value '%s'"),
74 75 76 77
                       peak);
        goto cleanup;
    }

78
    if (burst && virStrToLong_ullp(burst, NULL, 10, &rate->burst) < 0) {
79
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
80
                       _("could not convert bandwidth burst value '%s'"),
81 82 83 84
                       burst);
        goto cleanup;
    }

85
    if (floor && virStrToLong_ullp(floor, NULL, 10, &rate->floor) < 0) {
86 87 88 89 90 91
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("could not convert bandwidth floor value '%s'"),
                       floor);
        goto cleanup;
    }

92 93
    ret = 0;

94
 cleanup:
95 96 97
    VIR_FREE(average);
    VIR_FREE(peak);
    VIR_FREE(burst);
98
    VIR_FREE(floor);
99 100 101 102 103 104

    return ret;
}

/**
 * virNetDevBandwidthParse:
105
 * @bandwidth: parsed bandwidth
106
 * @node: XML node
107
 * @net_type: one of virDomainNetType
108
 *
109 110 111
 * Parse bandwidth XML and return pointer to structure.
 * @net_type tell to which type will/is interface connected to.
 * Pass -1 if this is not called on interface.
112 113 114
 *
 * Returns !NULL on success, NULL on error.
 */
115 116 117
int
virNetDevBandwidthParse(virNetDevBandwidthPtr *bandwidth,
                        xmlNodePtr node,
118
                        int net_type)
119
{
120
    int ret = -1;
121
    virNetDevBandwidthPtr def = NULL;
122
    xmlNodePtr cur;
123 124
    xmlNodePtr in = NULL, out = NULL;

125
    if (VIR_ALLOC(def) < 0)
126
        return ret;
127 128

    if (!node || !xmlStrEqual(node->name, BAD_CAST "bandwidth")) {
129
        virReportError(VIR_ERR_INVALID_ARG, "%s",
130
                       _("invalid argument supplied"));
131
        goto cleanup;
132 133
    }

134 135
    cur = node->children;

136 137 138 139
    while (cur) {
        if (cur->type == XML_ELEMENT_NODE) {
            if (xmlStrEqual(cur->name, BAD_CAST "inbound")) {
                if (in) {
140
                    virReportError(VIR_ERR_XML_DETAIL, "%s",
141 142
                                   _("Only one child <inbound> "
                                     "element allowed"));
143
                    goto cleanup;
144 145 146 147
                }
                in = cur;
            } else if (xmlStrEqual(cur->name, BAD_CAST "outbound")) {
                if (out) {
148
                    virReportError(VIR_ERR_XML_DETAIL, "%s",
149 150
                                   _("Only one child <outbound> "
                                     "element allowed"));
151
                    goto cleanup;
152 153 154 155 156 157 158 159 160
                }
                out = cur;
            }
            /* Silently ignore unknown elements */
        }
        cur = cur->next;
    }

    if (in) {
161
        if (VIR_ALLOC(def->in) < 0)
162
            goto cleanup;
163 164 165

        if (virNetDevBandwidthParseRate(in, def->in) < 0) {
            /* helper reported error for us */
166
            goto cleanup;
167
        }
168 169 170 171 172 173 174 175 176 177 178 179

        if (def->in->floor && net_type != VIR_DOMAIN_NET_TYPE_NETWORK) {
            if (net_type == -1) {
                /* 'floor' on network isn't supported */
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("floor attribute isn't supported for "
                                 "network's bandwidth yet"));
            } else {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("floor attribute is supported only for "
                                 "interfaces of type network"));
            }
180
            goto cleanup;
181
        }
182 183 184
    }

    if (out) {
185
        if (VIR_ALLOC(def->out) < 0)
186
            goto cleanup;
187 188 189

        if (virNetDevBandwidthParseRate(out, def->out) < 0) {
            /* helper reported error for us */
190
            goto cleanup;
191
        }
192 193 194 195 196

        if (def->out->floor) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("'floor' attribute allowed "
                             "only in <inbound> element"));
197
            goto cleanup;
198
        }
199 200
    }

201 202 203 204 205 206
    if (!def->in && !def->out)
        VIR_FREE(def);

    *bandwidth = def;
    def = NULL;
    ret = 0;
207

208
 cleanup:
209
    virNetDevBandwidthFree(def);
210
    return ret;
211 212 213 214 215 216 217 218 219 220 221 222
}

static int
virNetDevBandwidthRateFormat(virNetDevBandwidthRatePtr def,
                             virBufferPtr buf,
                             const char *elem_name)
{
    if (!buf || !elem_name)
        return -1;
    if (!def)
        return 0;

223
    if (def->average || def->floor) {
224
        virBufferAsprintf(buf, "<%s", elem_name);
225 226 227

        if (def->average)
            virBufferAsprintf(buf, " average='%llu'", def->average);
228 229 230 231

        if (def->peak)
            virBufferAsprintf(buf, " peak='%llu'", def->peak);

232 233 234
        if (def->floor)
            virBufferAsprintf(buf, " floor='%llu'", def->floor);

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
        if (def->burst)
            virBufferAsprintf(buf, " burst='%llu'", def->burst);
        virBufferAddLit(buf, "/>\n");
    }

    return 0;
}

/**
 * virNetDevBandwidthDefFormat:
 * @def: Data source
 * @buf: Buffer to print to
 *
 * Formats bandwidth and prepend each line with @indent.
 * @buf may use auto-indentation.
 *
 * Returns 0 on success, else -1.
 */
int
virNetDevBandwidthFormat(virNetDevBandwidthPtr def, virBufferPtr buf)
{
    int ret = -1;

    if (!buf)
        goto cleanup;

    if (!def) {
        ret = 0;
        goto cleanup;
    }

    virBufferAddLit(buf, "<bandwidth>\n");
267
    virBufferAdjustIndent(buf, 2);
268 269 270
    if (virNetDevBandwidthRateFormat(def->in, buf, "inbound") < 0 ||
        virNetDevBandwidthRateFormat(def->out, buf, "outbound") < 0)
        goto cleanup;
271
    virBufferAdjustIndent(buf, -2);
272 273 274 275
    virBufferAddLit(buf, "</bandwidth>\n");

    ret = 0;

276
 cleanup:
277 278
    return ret;
}
279 280 281 282 283 284 285 286 287

void
virDomainClearNetBandwidth(virDomainObjPtr vm)
{
    size_t i;
    virDomainNetType type;

    for (i = 0; i < vm->def->nnets; i++) {
        type = virDomainNetGetActualType(vm->def->nets[i]);
288 289
        if (virDomainNetGetActualBandwidth(vm->def->nets[i]) &&
            virNetDevSupportBandwidth(type))
290 291 292
            virNetDevBandwidthClear(vm->def->nets[i]->ifname);
    }
}