interface_conf.c 42.9 KB
Newer Older
1 2 3
/*
 * interface_conf.c: interfaces XML handling
 *
4
 * Copyright (C) 2006-2010 Red Hat, Inc.
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
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 *         Laine Stump <laine@redhat.com>
 */

#include <config.h>
#include "virterror_internal.h"
#include "datatypes.h"

#include "interface_conf.h"

#include "memory.h"
#include "xml.h"
#include "uuid.h"
#include "util.h"
#include "buf.h"

#define VIR_FROM_THIS VIR_FROM_INTERFACE

VIR_ENUM_IMPL(virInterface,
              VIR_INTERFACE_TYPE_LAST,
              "ethernet", "bridge", "bond", "vlan" )

42 43 44 45 46 47 48
static virInterfaceDefPtr
virInterfaceDefParseXML(virConnectPtr conn,
                        xmlXPathContextPtr ctxt, int parentIfType);
static int
virInterfaceDefDevFormat(virConnectPtr conn, virBufferPtr buf,
                         const virInterfaceDefPtr def, int level);

49 50 51 52
#define virInterfaceReportError(conn, code, fmt...)                            \
        virReportErrorHelper(conn, VIR_FROM_INTERFACE, code, __FILE__,       \
                               __FUNCTION__, __LINE__, fmt)

53 54 55 56 57
static
void virInterfaceIpDefFree(virInterfaceIpDefPtr def) {
    if (def == NULL)
        return;
    VIR_FREE(def->address);
58
    VIR_FREE(def);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

static
void virInterfaceProtocolDefFree(virInterfaceProtocolDefPtr def) {
    int ii;

    if (def == NULL)
        return;
    for (ii = 0; ii < def->nips; ii++) {
        virInterfaceIpDefFree(def->ips[ii]);
    }
    VIR_FREE(def->ips);
    VIR_FREE(def->family);
    VIR_FREE(def->gateway);
    VIR_FREE(def);
}

76 77
void virInterfaceDefFree(virInterfaceDefPtr def)
{
78
    int i, pp;
79 80 81 82 83 84 85 86 87 88 89

    if (def == NULL)
        return;

    VIR_FREE(def->name);
    VIR_FREE(def->mac);

    switch (def->type) {
        case VIR_INTERFACE_TYPE_BRIDGE:
            for (i = 0;i < def->data.bridge.nbItf;i++) {
                if (def->data.bridge.itf[i] != NULL)
90
                    virInterfaceDefFree(def->data.bridge.itf[i]);
91 92 93 94 95 96 97 98 99
                else
                    break; /* to cope with half parsed data on errors */
            }
            VIR_FREE(def->data.bridge.itf);
            break;
        case VIR_INTERFACE_TYPE_BOND:
            VIR_FREE(def->data.bond.target);
            for (i = 0;i < def->data.bond.nbItf;i++) {
                if (def->data.bond.itf[i] != NULL)
100
                    virInterfaceDefFree(def->data.bond.itf[i]);
101 102 103 104 105 106 107 108 109 110 111
                else
                    break; /* to cope with half parsed data on errors */
            }
            VIR_FREE(def->data.bond.itf);
            break;
        case VIR_INTERFACE_TYPE_VLAN:
            VIR_FREE(def->data.vlan.tag);
            VIR_FREE(def->data.vlan.devname);
            break;
    }

112 113 114 115 116
    /* free all protos */
    for (pp = 0; pp < def->nprotos; pp++) {
        virInterfaceProtocolDefFree(def->protos[pp]);
    }
    VIR_FREE(def->protos);
117 118 119 120
    VIR_FREE(def);
}

static int
121 122
virInterfaceDefParseName(virConnectPtr conn, virInterfaceDefPtr def,
                         xmlXPathContextPtr ctxt) {
123 124 125 126 127 128 129 130 131
    char *tmp;

    tmp = virXPathString(conn, "string(./@name)", ctxt);
    if (tmp == NULL) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                              "%s",  _("interface has no name"));
        return(-1);
    }
    def->name = tmp;
132 133 134 135 136 137 138 139
    return(0);
}

static int
virInterfaceDefParseMtu(virConnectPtr conn, virInterfaceDefPtr def,
                        xmlXPathContextPtr ctxt) {
    unsigned long mtu;
    int ret;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

    ret = virXPathULong(conn, "string(./mtu/@size)", ctxt, &mtu);
    if ((ret == -2) || ((ret == 0) && (mtu > 100000))) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                         "%s", _("interface mtu value is improper"));
        return(-1);
    } else if (ret == 0) {
        def->mtu = (unsigned int) mtu;
    }
    return(0);
}

static int
virInterfaceDefParseStartMode(virConnectPtr conn, virInterfaceDefPtr def,
                              xmlXPathContextPtr ctxt) {
    char *tmp;

    tmp = virXPathString(conn, "string(./start/@mode)", ctxt);
158 159 160
    if (tmp == NULL)
        def->startmode = VIR_INTERFACE_START_UNSPECIFIED;
    else if (STREQ(tmp, "onboot"))
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
        def->startmode = VIR_INTERFACE_START_ONBOOT;
    else if (STREQ(tmp, "hotplug"))
        def->startmode = VIR_INTERFACE_START_HOTPLUG;
    else if (STREQ(tmp, "none"))
        def->startmode = VIR_INTERFACE_START_NONE;
    else {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("unknown interface startmode %s"), tmp);
        VIR_FREE(tmp);
        return(-1);
    }
    VIR_FREE(tmp);
    return(0);
}

static int
virInterfaceDefParseBondMode(virConnectPtr conn, xmlXPathContextPtr ctxt) {
    char *tmp;
    int ret = 0;

    tmp = virXPathString(conn, "string(./@mode)", ctxt);
    if (tmp == NULL)
        return(VIR_INTERFACE_BOND_NONE);
    if (STREQ(tmp, "balance-rr"))
        ret = VIR_INTERFACE_BOND_BALRR;
    else if (STREQ(tmp, "active-backup"))
        ret = VIR_INTERFACE_BOND_ABACKUP;
    else if (STREQ(tmp, "balance-xor"))
        ret = VIR_INTERFACE_BOND_BALXOR;
    else if (STREQ(tmp, "broadcast"))
        ret = VIR_INTERFACE_BOND_BCAST;
    else if (STREQ(tmp, "802.3ad"))
        ret = VIR_INTERFACE_BOND_8023AD;
    else if (STREQ(tmp, "balance-tlb"))
        ret = VIR_INTERFACE_BOND_BALTLB;
    else if (STREQ(tmp, "balance-alb"))
        ret = VIR_INTERFACE_BOND_BALALB;
    else {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("unknown bonding mode %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

static int
virInterfaceDefParseBondMiiCarrier(virConnectPtr conn, xmlXPathContextPtr ctxt) {
    char *tmp;
    int ret = 0;

    tmp = virXPathString(conn, "string(./miimon/@carrier)", ctxt);
    if (tmp == NULL)
        return(VIR_INTERFACE_BOND_MII_NONE);
    if (STREQ(tmp, "ioctl"))
        ret = VIR_INTERFACE_BOND_MII_IOCTL;
    else if (STREQ(tmp, "netif"))
        ret = VIR_INTERFACE_BOND_MII_NETIF;
    else {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("unknown mii bonding carrier %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

static int
virInterfaceDefParseBondArpValid(virConnectPtr conn, xmlXPathContextPtr ctxt) {
    char *tmp;
    int ret = 0;

    tmp = virXPathString(conn, "string(./arpmon/@validate)", ctxt);
    if (tmp == NULL)
        return(VIR_INTERFACE_BOND_ARP_NONE);
    if (STREQ(tmp, "active"))
        ret = VIR_INTERFACE_BOND_ARP_ACTIVE;
    else if (STREQ(tmp, "backup"))
        ret = VIR_INTERFACE_BOND_ARP_BACKUP;
    else if (STREQ(tmp, "all"))
        ret = VIR_INTERFACE_BOND_ARP_ALL;
    else {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("unknown arp bonding validate %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

static int
252
virInterfaceDefParseDhcp(virConnectPtr conn, virInterfaceProtocolDefPtr def,
253
                         xmlNodePtr dhcp, xmlXPathContextPtr ctxt) {
254
    xmlNodePtr save;
255 256 257
    char *tmp;
    int ret = 0;

258
    def->dhcp = 1;
259
    save = ctxt->node;
260 261 262 263 264
    ctxt->node = dhcp;
    /* Not much to do in the current version */
    tmp = virXPathString(conn, "string(./@peerdns)", ctxt);
    if (tmp) {
        if (STREQ(tmp, "yes"))
265
            def->peerdns = 1;
266
        else if (STREQ(tmp, "no"))
267
            def->peerdns = 0;
268 269 270 271 272 273 274
        else {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                              _("unknown dhcp peerdns value %s"), tmp);
            ret = -1;
        }
        VIR_FREE(tmp);
    } else
275
        def->peerdns = -1;
276

277
    ctxt->node = save;
278 279 280 281
    return(ret);
}

static int
282 283
virInterfaceDefParseIp(virConnectPtr conn, virInterfaceIpDefPtr def,
                       xmlXPathContextPtr ctxt) {
284 285 286 287
    int ret = 0;
    char *tmp;
    long l;

288 289
    tmp = virXPathString(conn, "string(./@address)", ctxt);
    def->address = tmp;
290
    if (tmp != NULL) {
291
        ret = virXPathLong(conn, "string(./@prefix)", ctxt, &l);
292
        if (ret == 0)
293
            def->prefix = (int) l;
294 295 296 297 298 299 300 301 302 303 304
        else if (ret == -2) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                              "%s", _("Invalid ip address prefix value"));
            return(-1);
        }
    }

    return(0);
}

static int
305
virInterfaceDefParseProtoIPv4(virConnectPtr conn, virInterfaceProtocolDefPtr def,
306
                              xmlXPathContextPtr ctxt) {
307 308 309 310 311 312 313
    xmlNodePtr dhcp;
    xmlNodePtr *ipNodes = NULL;
    int nIpNodes, ii, ret = -1;
    char *tmp;

    tmp = virXPathString(conn, "string(./route[1]/@gateway)", ctxt);
    def->gateway = tmp;
314

315
    dhcp = virXPathNode(conn, "./dhcp", ctxt);
316
    if (dhcp != NULL) {
317
        ret = virInterfaceDefParseDhcp(conn, def, dhcp, ctxt);
318 319 320
        if (ret != 0)
           return(ret);
    }
321 322

    nIpNodes = virXPathNodeSet(conn, "./ip", ctxt, &ipNodes);
323 324
    if (nIpNodes < 0)
        return -1;
325 326 327 328
    if (ipNodes == NULL)
        return 0;

    if (VIR_ALLOC_N(def->ips, nIpNodes) < 0) {
329
        virReportOOMError();
330 331 332 333 334 335 336
        goto error;
    }

    def->nips = 0;
    for (ii = 0; ii < nIpNodes; ii++) {

        virInterfaceIpDefPtr ip;
337

338
        if (VIR_ALLOC(ip) < 0) {
339
            virReportOOMError();
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
            goto error;
        }

        ctxt->node = ipNodes[ii];
        ret = virInterfaceDefParseIp(conn, ip, ctxt);
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

error:
    VIR_FREE(ipNodes);
    return(ret);
}

static int
virInterfaceDefParseProtoIPv6(virConnectPtr conn, virInterfaceProtocolDefPtr def,
                              xmlXPathContextPtr ctxt) {
    xmlNodePtr dhcp, autoconf;
    xmlNodePtr *ipNodes = NULL;
    int nIpNodes, ii, ret = -1;
    char *tmp;

    tmp = virXPathString(conn, "string(./route[1]/@gateway)", ctxt);
    def->gateway = tmp;

    autoconf = virXPathNode(conn, "./autoconf", ctxt);
    if (autoconf != NULL)
        def->autoconf = 1;

    dhcp = virXPathNode(conn, "./dhcp", ctxt);
375
    if (dhcp != NULL) {
376
        ret = virInterfaceDefParseDhcp(conn, def, dhcp, ctxt);
377 378 379
        if (ret != 0)
           return(ret);
    }
380

381
    nIpNodes = virXPathNodeSet(conn, "./ip", ctxt, &ipNodes);
382 383
    if (nIpNodes < 0)
        return -1;
384 385 386 387
    if (ipNodes == NULL)
        return 0;

    if (VIR_ALLOC_N(def->ips, nIpNodes) < 0) {
388
        virReportOOMError();
389 390 391 392 393 394 395 396 397
        goto error;
    }

    def->nips = 0;
    for (ii = 0; ii < nIpNodes; ii++) {

        virInterfaceIpDefPtr ip;

        if (VIR_ALLOC(ip) < 0) {
398
            virReportOOMError();
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            goto error;
        }

        ctxt->node = ipNodes[ii];
        ret = virInterfaceDefParseIp(conn, ip, ctxt);
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

error:
    VIR_FREE(ipNodes);
415 416 417 418 419 420
    return(ret);
}

static int
virInterfaceDefParseIfAdressing(virConnectPtr conn, virInterfaceDefPtr def,
                                xmlXPathContextPtr ctxt) {
421 422 423
    xmlNodePtr save;
    xmlNodePtr *protoNodes = NULL;
    int nProtoNodes, pp, ret = -1;
424 425 426
    char *tmp;

    save = ctxt->node;
427 428 429 430 431

    nProtoNodes = virXPathNodeSet(conn, "./protocol", ctxt, &protoNodes);
    if (nProtoNodes <= 0) {
        /* no protocols is an acceptable outcome */
        return 0;
432
    }
433 434

    if (VIR_ALLOC_N(def->protos, nProtoNodes) < 0) {
435
        virReportOOMError();
436
        goto error;
437 438
    }

439 440 441 442 443 444
    def->nprotos = 0;
    for (pp = 0; pp < nProtoNodes; pp++) {

        virInterfaceProtocolDefPtr proto;

        if (VIR_ALLOC(proto) < 0) {
445
            virReportOOMError();
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
            goto error;
        }

        ctxt->node = protoNodes[pp];
        tmp = virXPathString(conn, "string(./@family)", ctxt);
        if (tmp == NULL) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                    "%s", _("protocol misses the family attribute"));
            virInterfaceProtocolDefFree(proto);
            goto error;
        }
        proto->family = tmp;
        if (STREQ(tmp, "ipv4")) {
            ret = virInterfaceDefParseProtoIPv4(conn, proto, ctxt);
            if (ret != 0) {
                virInterfaceProtocolDefFree(proto);
                goto error;
            }
        } else if (STREQ(tmp, "ipv6")) {
            ret = virInterfaceDefParseProtoIPv6(conn, proto, ctxt);
            if (ret != 0) {
                virInterfaceProtocolDefFree(proto);
                goto error;
            }
        } else {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                    _("unsupported protocol family '%s'"), tmp);
            virInterfaceProtocolDefFree(proto);
            goto error;
        }
        def->protos[def->nprotos++] = proto;
    }

    ret = 0;

error:
    VIR_FREE(protoNodes);
483 484 485 486 487 488 489 490 491 492
    ctxt->node = save;
    return(ret);

}

static int
virInterfaceDefParseBridge(virConnectPtr conn, virInterfaceDefPtr def,
                           xmlXPathContextPtr ctxt) {
    xmlNodePtr *interfaces = NULL;
    xmlNodePtr bridge;
493
    virInterfaceDefPtr itf;
494 495 496 497 498
    int nbItf, i;
    int ret = 0;

    bridge = ctxt->node;
    nbItf = virXPathNodeSet(conn, "./interface", ctxt, &interfaces);
499
    if (nbItf < 0) {
500
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
501
                                "%s", _("bridge interfaces"));
502 503 504
        ret = -1;
        goto error;
    }
505 506
    if (nbItf > 0) {
        if (VIR_ALLOC_N(def->data.bridge.itf, nbItf) < 0) {
507
            virReportOOMError();
508 509 510
            ret = -1;
            goto error;
        }
511 512 513 514
        def->data.bridge.nbItf = nbItf;

        for (i = 0; i < nbItf;i++) {
            ctxt->node = interfaces[i];
515
            itf = virInterfaceDefParseXML(conn, ctxt, VIR_INTERFACE_TYPE_BRIDGE);
516 517 518 519 520 521 522
            if (itf == NULL) {
                ret = -1;
                def->data.bridge.nbItf = i;
                goto error;
            }
            def->data.bridge.itf[i] = itf;
        }
523 524 525 526 527 528 529 530 531 532 533 534 535
    }

error:
    VIR_FREE(interfaces);
    ctxt->node = bridge;
    return(ret);
}

static int
virInterfaceDefParseBondItfs(virConnectPtr conn, virInterfaceDefPtr def,
                             xmlXPathContextPtr ctxt) {
    xmlNodePtr *interfaces = NULL;
    xmlNodePtr bond = ctxt->node;
536
    virInterfaceDefPtr itf;
537 538 539 540 541 542 543 544 545 546 547
    int nbItf, i;
    int ret = 0;

    nbItf = virXPathNodeSet(conn, "./interface", ctxt, &interfaces);
    if (nbItf <= 0) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                "%s", _("bond has no interfaces"));
        ret = -1;
        goto error;
    }
    if (VIR_ALLOC_N(def->data.bond.itf, nbItf) < 0) {
548
        virReportOOMError();
549 550 551 552 553 554 555
        ret = -1;
        goto error;
    }
    def->data.bond.nbItf = nbItf;

    for (i = 0; i < nbItf;i++) {
        ctxt->node = interfaces[i];
556
        itf = virInterfaceDefParseXML(conn, ctxt, VIR_INTERFACE_TYPE_BOND);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
        if (itf == NULL) {
            ret = -1;
            def->data.bond.nbItf = i;
            goto error;
        }
        def->data.bond.itf[i] = itf;
    }

error:
    VIR_FREE(interfaces);
    ctxt->node = bond;
    return(ret);
}

static int
virInterfaceDefParseBond(virConnectPtr conn, virInterfaceDefPtr def,
                         xmlXPathContextPtr ctxt) {
    xmlNodePtr node;
575
    int ret = -1;
576 577 578 579 580 581
    unsigned long tmp;

    def->data.bond.mode = virInterfaceDefParseBondMode(conn, ctxt);
    if (def->data.bond.mode < 0)
        goto error;

582 583 584 585
    ret = virInterfaceDefParseBondItfs(conn, def, ctxt);
    if (ret != 0)
       goto error;

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    node = virXPathNode(conn, "./miimon[1]", ctxt);
    if (node != NULL) {
        def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_MII;

        ret = virXPathULong(conn, "string(./miimon/@freq)", ctxt, &tmp);
        if ((ret == -2) || (ret == -1)) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                     "%s", _("bond interface miimon freq missing or invalid"));
            goto error;
        }
        def->data.bond.frequency = (int) tmp;

        ret = virXPathULong(conn, "string(./miimon/@downdelay)", ctxt, &tmp);
        if (ret == -2) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                     "%s", _("bond interface miimon downdelay invalid"));
            goto error;
        } else if (ret == 0) {
            def->data.bond.downdelay = (int) tmp;
        }

        ret = virXPathULong(conn, "string(./miimon/@updelay)", ctxt, &tmp);
        if (ret == -2) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                     "%s", _("bond interface miimon updelay invalid"));
            goto error;
        } else if (ret == 0) {
            def->data.bond.updelay = (int) tmp;
        }

        def->data.bond.carrier = virInterfaceDefParseBondMiiCarrier(conn, ctxt);
617 618
        if (def->data.bond.carrier < 0) {
            ret = -1;
619
            goto error;
620
        }
621

622
    } else if ((node = virXPathNode(conn, "./arpmon[1]", ctxt)) != NULL) {
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

        def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_ARP;

        ret = virXPathULong(conn, "string(./arpmon/@interval)", ctxt, &tmp);
        if ((ret == -2) || (ret == -1)) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                 "%s", _("bond interface arpmon interval missing or invalid"));
            goto error;
        }
        def->data.bond.interval = (int) tmp;

        def->data.bond.target =
            virXPathString(conn, "string(./arpmon/@target)", ctxt);
        if (def->data.bond.target == NULL) {
            virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                 "%s", _("bond interface arpmon target missing"));
639
            ret = -1;
640 641 642 643
            goto error;
        }

        def->data.bond.validate = virInterfaceDefParseBondArpValid(conn, ctxt);
644 645
        if (def->data.bond.validate < 0) {
            ret = -1;
646
            goto error;
647
        }
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    }
error:
    return(ret);
}

static int
virInterfaceDefParseVlan(virConnectPtr conn, virInterfaceDefPtr def,
                         xmlXPathContextPtr ctxt) {
    def->data.vlan.tag = virXPathString(conn, "string(./@tag)", ctxt);
    if (def->data.vlan.tag == NULL) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                    "%s", _("vlan interface misses the tag attribute"));
        return(-1);
    }

    def->data.vlan.devname =
         virXPathString(conn, "string(./interface/@name)", ctxt);
    if (def->data.vlan.devname == NULL) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                    "%s", _("vlan interface misses name attribute"));
        return(-1);
    }
    return(0);
}

static virInterfaceDefPtr
674 675
virInterfaceDefParseXML(virConnectPtr conn,
                        xmlXPathContextPtr ctxt, int parentIfType) {
676 677 678 679 680 681 682 683 684
    virInterfaceDefPtr def;
    int type;
    char *tmp;
    xmlNodePtr cur = ctxt->node;

    /* check @type */
    tmp = virXPathString(conn, "string(./@type)", ctxt);
    if (tmp == NULL) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
685
                                "%s", _("interface misses the type attribute"));
686 687 688 689 690 691 692 693 694 695 696 697
        return(NULL);
    }
    type = virInterfaceTypeFromString(tmp);
    if (type == -1) {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("unknown interface type %s"), tmp);
        VIR_FREE(tmp);
        return(NULL);
    }
    VIR_FREE(tmp);

    if (VIR_ALLOC(def) < 0) {
698
        virReportOOMError();
699 700
        return NULL;
    }
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

    if (((parentIfType == VIR_INTERFACE_TYPE_BOND)
         && (type != VIR_INTERFACE_TYPE_ETHERNET))
        || ((parentIfType == VIR_INTERFACE_TYPE_BRIDGE)
            && (type != VIR_INTERFACE_TYPE_ETHERNET)
            && (type != VIR_INTERFACE_TYPE_BOND)
            && (type != VIR_INTERFACE_TYPE_VLAN))
        || (parentIfType == VIR_INTERFACE_TYPE_ETHERNET)
        || (parentIfType == VIR_INTERFACE_TYPE_VLAN))
        {
        virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                _("interface has unsupported type '%s'"),
                                virInterfaceTypeToString(type));
        goto error;
    }
716 717 718
    def->type = type;
    switch (type) {
        case VIR_INTERFACE_TYPE_ETHERNET:
719
            if (virInterfaceDefParseName(conn, def, ctxt) < 0)
720 721 722 723
                goto error;
            tmp = virXPathString(conn, "string(./mac/@address)", ctxt);
            if (tmp != NULL)
                def->mac = tmp;
724 725 726 727 728 729 730 731 732
            if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
                /* only recognize these in toplevel bond interfaces */
                if (virInterfaceDefParseStartMode(conn, def, ctxt) < 0)
                    goto error;
                if (virInterfaceDefParseMtu(conn, def, ctxt) < 0)
                    goto error;
                if (virInterfaceDefParseIfAdressing(conn, def, ctxt) < 0)
                    goto error;
            }
733 734 735 736
            break;
        case VIR_INTERFACE_TYPE_BRIDGE: {
            xmlNodePtr bridge;

737 738
            if (virInterfaceDefParseName(conn, def, ctxt) < 0)
                goto error;
739 740
            if (virInterfaceDefParseStartMode(conn, def, ctxt) < 0)
                goto error;
741
            if (virInterfaceDefParseMtu(conn, def, ctxt) < 0)
742 743 744 745 746 747 748
                goto error;
            if (virInterfaceDefParseIfAdressing(conn, def, ctxt) < 0)
                goto error;

            bridge = virXPathNode(conn, "./bridge[1]", ctxt);
            if (bridge == NULL) {
                virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
749
                                        "%s", _("bridge interface misses the bridge element"));
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
                goto error;
            }
            tmp = virXMLPropString(bridge, "stp");
            def->data.bridge.stp = -1;
            if (tmp != NULL) {
                if (STREQ(tmp, "on")) {
                    def->data.bridge.stp = 1;
                } else if (STREQ(tmp, "off")) {
                    def->data.bridge.stp = 0;
                } else {
                    virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                          _("bridge interface stp should be on or off got %s"),
                                            tmp);
                    VIR_FREE(tmp);
                    goto error;
                }
                VIR_FREE(tmp);
            }
768
            def->data.bridge.delay = virXMLPropString(bridge, "delay");
769 770 771 772 773 774 775
            ctxt->node = bridge;
            virInterfaceDefParseBridge(conn, def, ctxt);
            break;
        }
        case VIR_INTERFACE_TYPE_BOND: {
            xmlNodePtr bond;

776
            if (virInterfaceDefParseName(conn, def, ctxt) < 0)
777
                goto error;
778 779 780 781 782 783 784 785 786 787
            if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
                /* only recognize these in toplevel bond interfaces */
                if (virInterfaceDefParseStartMode(conn, def, ctxt) < 0)
                    goto error;
                if (virInterfaceDefParseMtu(conn, def, ctxt) < 0)
                    goto error;
                if (virInterfaceDefParseIfAdressing(conn, def, ctxt) < 0)
                    goto error;
            }

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 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
            bond = virXPathNode(conn, "./bond[1]", ctxt);
            if (bond == NULL) {
                virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                            "%s", _("bond interface misses the bond element"));
                goto error;
            }
            ctxt->node = bond;
            if (virInterfaceDefParseBond(conn, def, ctxt)  < 0)
                goto error;
            break;
        }
        case VIR_INTERFACE_TYPE_VLAN: {
            xmlNodePtr vlan;

            tmp = virXPathString(conn, "string(./@name)", ctxt);
            if (tmp != NULL)
                def->name = tmp;
            if (virInterfaceDefParseStartMode(conn, def, ctxt) < 0)
                goto error;
            if (virInterfaceDefParseIfAdressing(conn, def, ctxt) < 0)
                goto error;
            vlan = virXPathNode(conn, "./vlan[1]", ctxt);
            if (vlan == NULL) {
                virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                            "%s", _("vlan interface misses the vlan element"));
                goto error;
            }
            ctxt->node = vlan;
            if (virInterfaceDefParseVlan(conn, def, ctxt)  < 0)
                goto error;
            break;
        }

    }

    ctxt->node = cur;
    return def;

error:
    ctxt->node = cur;
    virInterfaceDefFree(def);
    return NULL;
}

virInterfaceDefPtr virInterfaceDefParseNode(virConnectPtr conn,
                                        xmlDocPtr xml,
                                        xmlNodePtr root)
{
    xmlXPathContextPtr ctxt = NULL;
    virInterfaceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "interface")) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("incorrect root element"));
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
847
        virReportOOMError();
848 849 850 851
        goto cleanup;
    }

    ctxt->node = root;
852
    def = virInterfaceDefParseXML(conn, ctxt, VIR_INTERFACE_TYPE_LAST);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

/* Called from SAX on parsing errors in the XML. */
static void
catchXMLError (void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

    if (ctxt) {
        virConnectPtr conn = ctxt->_private;

        if (conn &&
            conn->err.code == VIR_ERR_NONE &&
            ctxt->lastError.level == XML_ERR_FATAL &&
            ctxt->lastError.message != NULL) {
            virInterfaceReportError (conn, VIR_ERR_XML_DETAIL,
                                     _("at line %d: %s"),
                                     ctxt->lastError.line,
                                     ctxt->lastError.message);
        }
    }
}

virInterfaceDefPtr virInterfaceDefParseString(virConnectPtr conn,
                                          const char *xmlStr)
{
    xmlParserCtxtPtr pctxt;
    xmlDocPtr xml = NULL;
    xmlNodePtr root;
    virInterfaceDefPtr def = NULL;

    /* Set up a parser context so we can catch the details of XML errors. */
    pctxt = xmlNewParserCtxt ();
    if (!pctxt || !pctxt->sax)
        goto cleanup;
    pctxt->sax->error = catchXMLError;
    pctxt->_private = conn;

    if (conn) virResetError (&conn->err);
    xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "interface.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOWARNING);
    if (!xml) {
        if (conn && conn->err.code == VIR_ERR_NONE)
              virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                    "%s", _("failed to parse xml document"));
        goto cleanup;
    }

    if ((root = xmlDocGetRootElement(xml)) == NULL) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing root element"));
        goto cleanup;
    }

    def = virInterfaceDefParseNode(conn, xml, root);

cleanup:
    xmlFreeParserCtxt (pctxt);
    xmlFreeDoc (xml);
    return def;
}

virInterfaceDefPtr virInterfaceDefParseFile(virConnectPtr conn,
                                        const char *filename)
{
    xmlParserCtxtPtr pctxt;
    xmlDocPtr xml = NULL;
    xmlNodePtr root;
    virInterfaceDefPtr def = NULL;

    /* Set up a parser context so we can catch the details of XML errors. */
    pctxt = xmlNewParserCtxt ();
    if (!pctxt || !pctxt->sax)
        goto cleanup;
    pctxt->sax->error = catchXMLError;
    pctxt->_private = conn;

    if (conn) virResetError (&conn->err);
    xml = xmlCtxtReadFile (pctxt, filename, NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOWARNING);
    if (!xml) {
        if (conn && conn->err.code == VIR_ERR_NONE)
              virInterfaceReportError(conn, VIR_ERR_XML_ERROR,
                                    "%s", _("failed to parse xml document"));
        goto cleanup;
    }

    if ((root = xmlDocGetRootElement(xml)) == NULL) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              "%s", _("missing root element"));
        goto cleanup;
    }

    def = virInterfaceDefParseNode(conn, xml, root);

cleanup:
    xmlFreeParserCtxt (pctxt);
    xmlFreeDoc (xml);
    return def;
}

static int
virInterfaceBridgeDefFormat(virConnectPtr conn, virBufferPtr buf,
962
                            const virInterfaceDefPtr def, int level) {
963 964 965
    int i;
    int ret = 0;

966
    virBufferVSprintf(buf, "%*s  <bridge", level*2, "");
967
    if (def->data.bridge.stp == 1)
968
        virBufferAddLit(buf, " stp='on'");
969
    else if (def->data.bridge.stp == 0)
970 971 972 973
        virBufferAddLit(buf, " stp='off'");
    if (def->data.bridge.delay != NULL)
        virBufferVSprintf(buf, " delay='%s'", def->data.bridge.delay);
    virBufferAddLit(buf, ">\n");
974 975

    for (i = 0;i < def->data.bridge.nbItf;i++) {
976 977
        if (virInterfaceDefDevFormat(conn, buf,
                                     def->data.bridge.itf[i], level+2) < 0)
978 979 980
            ret = -1;
    }

981
    virBufferVSprintf(buf, "%*s  </bridge>\n", level*2, "");
982 983 984 985 986
    return(ret);
}

static int
virInterfaceBondDefFormat(virConnectPtr conn, virBufferPtr buf,
987
                          const virInterfaceDefPtr def, int level) {
988 989 990
    int i;
    int ret = 0;

991
    virBufferVSprintf(buf, "%*s  <bond", level*2, "");
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    if (def->data.bond.mode == VIR_INTERFACE_BOND_BALRR)
        virBufferAddLit(buf, " mode='balance-rr'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_ABACKUP)
        virBufferAddLit(buf, " mode='active-backup'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALXOR)
        virBufferAddLit(buf, " mode='balance-xor'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_BCAST)
        virBufferAddLit(buf, " mode='broadcast'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_8023AD)
        virBufferAddLit(buf, " mode='802.3ad'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALTLB)
        virBufferAddLit(buf, " mode='balance-tlb'");
    else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALALB)
        virBufferAddLit(buf, " mode='balance-alb'");
    virBufferAddLit(buf, ">\n");

    if (def->data.bond.monit == VIR_INTERFACE_BOND_MONIT_MII) {
1009 1010
        virBufferVSprintf(buf, "%*s    <miimon freq='%d'",
                          level*2, "", def->data.bond.frequency);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        if (def->data.bond.downdelay > 0)
            virBufferVSprintf(buf, " downdelay='%d'", def->data.bond.downdelay);
        if (def->data.bond.updelay > 0)
            virBufferVSprintf(buf, " updelay='%d'", def->data.bond.updelay);
        if (def->data.bond.carrier == VIR_INTERFACE_BOND_MII_IOCTL)
            virBufferAddLit(buf, " carrier='ioctl'");
        else if (def->data.bond.carrier == VIR_INTERFACE_BOND_MII_NETIF)
            virBufferAddLit(buf, " carrier='netif'");
        virBufferAddLit(buf, "/>\n");
    } else if (def->data.bond.monit == VIR_INTERFACE_BOND_MONIT_ARP) {
        if (def->data.bond.target == NULL) {
            virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                          "%s", _("bond arp monitoring has no target"));
            return(-1);
        }
1026 1027
        virBufferVSprintf(buf, "%*s    <arpmon interval='%d' target='%s'",
                          level*2, "",
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
                          def->data.bond.interval, def->data.bond.target);
        if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_ACTIVE)
            virBufferAddLit(buf, " validate='active'");
        else if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_BACKUP)
            virBufferAddLit(buf, " validate='backup'");
        else if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_ALL)
            virBufferAddLit(buf, " validate='all'");
        virBufferAddLit(buf, "/>\n");
    }
    for (i = 0;i < def->data.bond.nbItf;i++) {
1038
        if (virInterfaceDefDevFormat(conn, buf, def->data.bond.itf[i], level+2) < 0)
1039 1040 1041
            ret = -1;
    }

1042
    virBufferVSprintf(buf, "%*s  </bond>\n", level*2, "");
1043 1044 1045 1046 1047
    return(ret);
}

static int
virInterfaceVlanDefFormat(virConnectPtr conn, virBufferPtr buf,
1048
                          const virInterfaceDefPtr def, int level) {
1049 1050 1051 1052 1053 1054
    if (def->data.vlan.tag == NULL) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                "%s", _("vlan misses the tag name"));
        return(-1);
    }

1055 1056
    virBufferVSprintf(buf, "%*s  <vlan tag='%s'",
                      level*2, "", def->data.vlan.tag);
1057 1058
    if (def->data.vlan.devname != NULL) {
        virBufferAddLit(buf, ">\n");
1059 1060 1061
        virBufferVSprintf(buf, "%*s    <interface name='%s'/>\n",
                          level*2, "", def->data.vlan.devname);
        virBufferVSprintf(buf, "%*s  </vlan>\n", level*2, "");
1062 1063 1064 1065 1066 1067 1068
    } else
        virBufferAddLit(buf, "/>\n");
    return(0);
}

static int
virInterfaceProtocolDefFormat(virConnectPtr conn ATTRIBUTE_UNUSED,
1069 1070
                              virBufferPtr buf, const virInterfaceDefPtr def,
                              int level) {
1071 1072 1073 1074
    int pp, ii;

    for (pp = 0; pp < def->nprotos; pp++) {

1075 1076
        virBufferVSprintf(buf, "%*s  <protocol family='%s'>\n",
                          level*2, "", def->protos[pp]->family);
1077 1078

        if (def->protos[pp]->autoconf) {
1079
            virBufferVSprintf(buf, "%*s    <autoconf/>\n", level*2, "");
1080 1081 1082 1083
        }

        if (def->protos[pp]->dhcp) {
            if (def->protos[pp]->peerdns == 0)
1084 1085
                virBufferVSprintf(buf, "%*s    <dhcp peerdns='no'/>\n",
                                  level*2, "");
1086
            else if (def->protos[pp]->peerdns == 1)
1087 1088
                virBufferVSprintf(buf, "%*s    <dhcp peerdns='yes'/>\n",
                                  level*2, "");
1089
            else
1090
                virBufferVSprintf(buf, "%*s    <dhcp/>\n", level*2, "");
1091 1092 1093 1094 1095
        }

        for (ii = 0; ii < def->protos[pp]->nips; ii++) {
            if (def->protos[pp]->ips[ii]->address != NULL) {

1096
                virBufferVSprintf(buf, "%*s    <ip address='%s'", level*2, "",
1097 1098 1099 1100 1101 1102 1103 1104 1105
                                  def->protos[pp]->ips[ii]->address);
                if (def->protos[pp]->ips[ii]->prefix != 0) {
                    virBufferVSprintf(buf, " prefix='%d'",
                                      def->protos[pp]->ips[ii]->prefix);
                }
                virBufferAddLit(buf, "/>\n");
            }
        }
        if (def->protos[pp]->gateway != NULL) {
1106 1107
            virBufferVSprintf(buf, "%*s    <route gateway='%s'/>\n",
                              level*2, "", def->protos[pp]->gateway);
1108 1109
        }

1110
        virBufferVSprintf(buf, "%*s  </protocol>\n", level*2, "");
1111 1112 1113 1114 1115 1116
    }
    return(0);
}

static int
virInterfaceStartmodeDefFormat(virConnectPtr conn, virBufferPtr buf,
1117 1118
                               enum virInterfaceStartMode startmode,
                               int level) {
1119 1120
    const char *mode;
    switch (startmode) {
1121 1122
        case VIR_INTERFACE_START_UNSPECIFIED:
            return 0;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        case VIR_INTERFACE_START_NONE:
            mode = "none";
            break;
        case VIR_INTERFACE_START_ONBOOT:
            mode = "onboot";
            break;
        case VIR_INTERFACE_START_HOTPLUG:
            mode = "hotplug";
            break;
        default:
            virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("virInterfaceDefFormat unknown startmode"));
            return -1;
    }
1137
    virBufferVSprintf(buf, "%*s  <start mode='%s'/>\n", level*2, "", mode);
1138 1139 1140
    return(0);
}

1141 1142 1143
static int
virInterfaceDefDevFormat(virConnectPtr conn, virBufferPtr buf,
                         const virInterfaceDefPtr def, int level) {
1144
    const char *type = NULL;
1145

1146
    if (def == NULL) {
1147
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
1148 1149 1150 1151 1152 1153 1154
                        "%s", _("virInterfaceDefFormat NULL def"));
        goto cleanup;
    }

    if ((def->name == NULL) && (def->type != VIR_INTERFACE_TYPE_VLAN)) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                        "%s", _("virInterfaceDefFormat missing interface name"));
1155 1156 1157 1158 1159 1160 1161 1162 1163
        goto cleanup;
    }

    if (!(type = virInterfaceTypeToString(def->type))) {
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                        _("unexpected interface type %d"), def->type);
        goto cleanup;
    }

1164
    virBufferVSprintf(buf, "%*s<interface type='%s' ", level*2, "", type);
1165
    if (def->name != NULL)
1166 1167
        virBufferEscapeString(buf, "name='%s'", def->name);
    virBufferAddLit(buf, ">\n");
1168 1169 1170

    switch (def->type) {
        case VIR_INTERFACE_TYPE_ETHERNET:
1171
            virInterfaceStartmodeDefFormat(conn, buf, def->startmode, level);
1172
            if (def->mac != NULL)
1173 1174
                virBufferVSprintf(buf, "%*s  <mac address='%s'/>\n",
                                  level*2, "", def->mac);
1175
            if (def->mtu != 0)
1176 1177 1178
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
            virInterfaceProtocolDefFormat(conn, buf, def, level);
1179 1180
            break;
        case VIR_INTERFACE_TYPE_BRIDGE:
1181
            virInterfaceStartmodeDefFormat(conn, buf, def->startmode, level);
1182
            if (def->mtu != 0)
1183 1184 1185 1186
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
            virInterfaceProtocolDefFormat(conn, buf, def, level);
            virInterfaceBridgeDefFormat(conn, buf, def, level);
1187 1188
            break;
        case VIR_INTERFACE_TYPE_BOND:
1189
            virInterfaceStartmodeDefFormat(conn, buf, def->startmode, level);
1190
            if (def->mtu != 0)
1191 1192 1193 1194
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
            virInterfaceProtocolDefFormat(conn, buf, def, level);
            virInterfaceBondDefFormat(conn, buf, def, level);
1195 1196
            break;
        case VIR_INTERFACE_TYPE_VLAN:
1197
            virInterfaceStartmodeDefFormat(conn, buf, def->startmode, level);
1198
            if (def->mac != NULL)
1199 1200
                virBufferVSprintf(buf, "%*s  <mac address='%s'/>\n",
                                  level*2, "", def->mac);
1201
            if (def->mtu != 0)
1202 1203 1204 1205
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
            virInterfaceProtocolDefFormat(conn, buf, def, level);
            virInterfaceVlanDefFormat(conn, buf, def, level);
1206 1207 1208
            break;
    }

1209
    virBufferVSprintf(buf, "%*s</interface>\n", level*2, "");
1210

1211
    if (virBufferError(buf))
1212
        goto no_memory;
1213
    return 0;
1214
no_memory:
1215
    virReportOOMError();
1216
cleanup:
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    return -1;
}

char *virInterfaceDefFormat(virConnectPtr conn,
                          const virInterfaceDefPtr def)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (virInterfaceDefDevFormat(conn, &buf, def, 0) < 0) {
        virBufferFreeAndReset(&buf);
        return NULL;
    }
    return virBufferContentAndReset(&buf);
1230 1231
}

1232 1233
/* virInterfaceObj manipulation */

1234 1235 1236 1237 1238 1239 1240 1241 1242
void virInterfaceObjLock(virInterfaceObjPtr obj)
{
    virMutexLock(&obj->lock);
}

void virInterfaceObjUnlock(virInterfaceObjPtr obj)
{
    virMutexUnlock(&obj->lock);
}
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

void virInterfaceObjFree(virInterfaceObjPtr iface)
{
    if (!iface)
        return;

    virInterfaceDefFree(iface->def);
    virMutexDestroy(&iface->lock);
    VIR_FREE(iface);
}

/* virInterfaceObjList manipulation */

int virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
                                const char *mac,
                                virInterfaceObjPtr *matches, int maxmatches)
{
    unsigned int i, matchct = 0;

    for (i = 0 ; i < interfaces->count ; i++) {

        virInterfaceObjLock(interfaces->objs[i]);
        if (STRCASEEQ(interfaces->objs[i]->def->mac, mac)) {
            matchct++;
            if (matchct <= maxmatches) {
                matches[matchct - 1] = interfaces->objs[i];
                /* keep the lock if we're returning object to caller */
                /* it is the caller's responsibility to unlock *all* matches */
                continue;
            }
        }
        virInterfaceObjUnlock(interfaces->objs[i]);

    }
    return matchct;
}

virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr
                                          interfaces,
                                          const char *name)
{
    unsigned int i;

    for (i = 0 ; i < interfaces->count ; i++) {
        virInterfaceObjLock(interfaces->objs[i]);
        if (STREQ(interfaces->objs[i]->def->name, name))
            return interfaces->objs[i];
        virInterfaceObjUnlock(interfaces->objs[i]);
    }

    return NULL;
}

void virInterfaceObjListFree(virInterfaceObjListPtr interfaces)
{
    unsigned int i;

    for (i = 0 ; i < interfaces->count ; i++)
        virInterfaceObjFree(interfaces->objs[i]);

    VIR_FREE(interfaces->objs);
    interfaces->count = 0;
}

virInterfaceObjPtr virInterfaceAssignDef(virConnectPtr conn,
                                         virInterfaceObjListPtr interfaces,
                                         const virInterfaceDefPtr def)
{
1311
    virInterfaceObjPtr iface;
1312

1313 1314 1315 1316
    if ((iface = virInterfaceFindByName(interfaces, def->name))) {
        if (iface->def)
            virInterfaceDefFree(iface->def);
        iface->def = def;
1317

1318
        return iface;
1319 1320
    }

1321
    if (VIR_ALLOC(iface) < 0) {
1322
        virReportOOMError();
1323 1324
        return NULL;
    }
1325
    if (virMutexInit(&iface->lock) < 0) {
1326 1327
        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
                                "%s", _("cannot initialize mutex"));
1328
        VIR_FREE(iface);
1329 1330
        return NULL;
    }
1331 1332
    virInterfaceObjLock(iface);
    iface->def = def;
1333 1334

    if (VIR_REALLOC_N(interfaces->objs, interfaces->count + 1) < 0) {
1335
        virReportOOMError();
1336
        VIR_FREE(iface);
1337 1338 1339
        return NULL;
    }

1340
    interfaces->objs[interfaces->count] = iface;
1341 1342
    interfaces->count++;

1343
    return iface;
1344 1345 1346 1347

}

void virInterfaceRemove(virInterfaceObjListPtr interfaces,
1348
                        const virInterfaceObjPtr iface)
1349 1350 1351
{
    unsigned int i;

1352
    virInterfaceObjUnlock(iface);
1353 1354
    for (i = 0 ; i < interfaces->count ; i++) {
        virInterfaceObjLock(interfaces->objs[i]);
1355
        if (interfaces->objs[i] == iface) {
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
            virInterfaceObjUnlock(interfaces->objs[i]);
            virInterfaceObjFree(interfaces->objs[i]);

            if (i < (interfaces->count - 1))
                memmove(interfaces->objs + i, interfaces->objs + i + 1,
                        sizeof(*(interfaces->objs)) * (interfaces->count - (i + 1)));

            if (VIR_REALLOC_N(interfaces->objs, interfaces->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            interfaces->count--;

            break;
        }
        virInterfaceObjUnlock(interfaces->objs[i]);
    }
}