interface_conf.c 41.1 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
static virInterfaceDefPtr
43
virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType);
44
static int
45
virInterfaceDefDevFormat(virBufferPtr buf,
46 47
                         const virInterfaceDefPtr def, int level);

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

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

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);
}

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

    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)
89
                    virInterfaceDefFree(def->data.bridge.itf[i]);
90 91 92 93 94 95 96 97 98
                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)
99
                    virInterfaceDefFree(def->data.bond.itf[i]);
100 101 102 103 104 105 106 107 108 109 110
                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;
    }

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

static int
120
virInterfaceDefParseName(virInterfaceDefPtr def,
121
                         xmlXPathContextPtr ctxt) {
122 123
    char *tmp;

124
    tmp = virXPathString("string(./@name)", ctxt);
125
    if (tmp == NULL) {
126 127
        virInterfaceReportError(VIR_ERR_XML_ERROR,
                                "%s",  _("interface has no name"));
128 129 130
        return(-1);
    }
    def->name = tmp;
131 132 133 134
    return(0);
}

static int
135
virInterfaceDefParseMtu(virInterfaceDefPtr def,
136 137 138
                        xmlXPathContextPtr ctxt) {
    unsigned long mtu;
    int ret;
139

140
    ret = virXPathULong("string(./mtu/@size)", ctxt, &mtu);
141
    if ((ret == -2) || ((ret == 0) && (mtu > 100000))) {
142
        virInterfaceReportError(VIR_ERR_XML_ERROR,
143 144 145 146 147 148 149 150 151
                         "%s", _("interface mtu value is improper"));
        return(-1);
    } else if (ret == 0) {
        def->mtu = (unsigned int) mtu;
    }
    return(0);
}

static int
152
virInterfaceDefParseStartMode(virInterfaceDefPtr def,
153 154 155
                              xmlXPathContextPtr ctxt) {
    char *tmp;

156
    tmp = virXPathString("string(./start/@mode)", ctxt);
157 158 159
    if (tmp == NULL)
        def->startmode = VIR_INTERFACE_START_UNSPECIFIED;
    else if (STREQ(tmp, "onboot"))
160 161 162 163 164 165
        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 {
166
        virInterfaceReportError(VIR_ERR_XML_ERROR,
167 168 169 170 171 172 173 174 175
                                _("unknown interface startmode %s"), tmp);
        VIR_FREE(tmp);
        return(-1);
    }
    VIR_FREE(tmp);
    return(0);
}

static int
176
virInterfaceDefParseBondMode(xmlXPathContextPtr ctxt) {
177 178 179
    char *tmp;
    int ret = 0;

180
    tmp = virXPathString("string(./@mode)", ctxt);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    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 {
198
        virInterfaceReportError(VIR_ERR_XML_ERROR,
199 200 201 202 203 204 205 206
                                _("unknown bonding mode %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

static int
207
virInterfaceDefParseBondMiiCarrier(xmlXPathContextPtr ctxt) {
208 209 210
    char *tmp;
    int ret = 0;

211
    tmp = virXPathString("string(./miimon/@carrier)", ctxt);
212 213 214 215 216 217 218
    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 {
219
        virInterfaceReportError(VIR_ERR_XML_ERROR,
220 221 222 223 224 225 226 227
                                _("unknown mii bonding carrier %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

static int
228
virInterfaceDefParseBondArpValid(xmlXPathContextPtr ctxt) {
229 230 231
    char *tmp;
    int ret = 0;

232
    tmp = virXPathString("string(./arpmon/@validate)", ctxt);
233 234 235 236 237 238 239 240 241
    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 {
242
        virInterfaceReportError(VIR_ERR_XML_ERROR,
243 244 245 246 247 248 249 250
                                _("unknown arp bonding validate %s"), tmp);
        ret = -1;
    }
    VIR_FREE(tmp);
    return(ret);
}

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

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

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

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

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

    return(0);
}

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

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

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

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

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

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

        virInterfaceIpDefPtr ip;
336

337
        if (VIR_ALLOC(ip) < 0) {
338
            virReportOOMError();
339 340 341 342
            goto error;
        }

        ctxt->node = ipNodes[ii];
343
        ret = virInterfaceDefParseIp(ip, ctxt);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

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

static int
359
virInterfaceDefParseProtoIPv6(virInterfaceProtocolDefPtr def,
360 361 362 363 364 365
                              xmlXPathContextPtr ctxt) {
    xmlNodePtr dhcp, autoconf;
    xmlNodePtr *ipNodes = NULL;
    int nIpNodes, ii, ret = -1;
    char *tmp;

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

369
    autoconf = virXPathNode("./autoconf", ctxt);
370 371 372
    if (autoconf != NULL)
        def->autoconf = 1;

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

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

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

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

        virInterfaceIpDefPtr ip;

        if (VIR_ALLOC(ip) < 0) {
397
            virReportOOMError();
398 399 400 401
            goto error;
        }

        ctxt->node = ipNodes[ii];
402
        ret = virInterfaceDefParseIp(ip, ctxt);
403 404 405 406 407 408 409 410 411 412 413
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

error:
    VIR_FREE(ipNodes);
414 415 416 417
    return(ret);
}

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

    save = ctxt->node;
426

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

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

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

        virInterfaceProtocolDefPtr proto;

        if (VIR_ALLOC(proto) < 0) {
444
            virReportOOMError();
445 446 447 448
            goto error;
        }

        ctxt->node = protoNodes[pp];
449
        tmp = virXPathString("string(./@family)", ctxt);
450
        if (tmp == NULL) {
451
            virInterfaceReportError(VIR_ERR_XML_ERROR,
452 453 454 455 456 457
                                    "%s", _("protocol misses the family attribute"));
            virInterfaceProtocolDefFree(proto);
            goto error;
        }
        proto->family = tmp;
        if (STREQ(tmp, "ipv4")) {
458
            ret = virInterfaceDefParseProtoIPv4(proto, ctxt);
459 460 461 462 463
            if (ret != 0) {
                virInterfaceProtocolDefFree(proto);
                goto error;
            }
        } else if (STREQ(tmp, "ipv6")) {
464
            ret = virInterfaceDefParseProtoIPv6(proto, ctxt);
465 466 467 468 469
            if (ret != 0) {
                virInterfaceProtocolDefFree(proto);
                goto error;
            }
        } else {
470
            virInterfaceReportError(VIR_ERR_XML_ERROR,
471 472 473 474 475 476 477 478 479 480 481
                                    _("unsupported protocol family '%s'"), tmp);
            virInterfaceProtocolDefFree(proto);
            goto error;
        }
        def->protos[def->nprotos++] = proto;
    }

    ret = 0;

error:
    VIR_FREE(protoNodes);
482 483 484 485 486 487
    ctxt->node = save;
    return(ret);

}

static int
488
virInterfaceDefParseBridge(virInterfaceDefPtr def,
489 490 491
                           xmlXPathContextPtr ctxt) {
    xmlNodePtr *interfaces = NULL;
    xmlNodePtr bridge;
492
    virInterfaceDefPtr itf;
493 494 495 496
    int nbItf, i;
    int ret = 0;

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

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

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

static int
531
virInterfaceDefParseBondItfs(virInterfaceDefPtr def,
532 533 534
                             xmlXPathContextPtr ctxt) {
    xmlNodePtr *interfaces = NULL;
    xmlNodePtr bond = ctxt->node;
535
    virInterfaceDefPtr itf;
536 537 538
    int nbItf, i;
    int ret = 0;

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

    for (i = 0; i < nbItf;i++) {
        ctxt->node = interfaces[i];
555
        itf = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_BOND);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        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
571
virInterfaceDefParseBond(virInterfaceDefPtr def,
572 573
                         xmlXPathContextPtr ctxt) {
    xmlNodePtr node;
574
    int ret = -1;
575 576
    unsigned long tmp;

577
    def->data.bond.mode = virInterfaceDefParseBondMode(ctxt);
578 579 580
    if (def->data.bond.mode < 0)
        goto error;

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

585
    node = virXPathNode("./miimon[1]", ctxt);
586 587 588
    if (node != NULL) {
        def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_MII;

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

597
        ret = virXPathULong("string(./miimon/@downdelay)", ctxt, &tmp);
598
        if (ret == -2) {
599
            virInterfaceReportError(VIR_ERR_XML_ERROR,
600 601 602 603 604 605
                     "%s", _("bond interface miimon downdelay invalid"));
            goto error;
        } else if (ret == 0) {
            def->data.bond.downdelay = (int) tmp;
        }

606
        ret = virXPathULong("string(./miimon/@updelay)", ctxt, &tmp);
607
        if (ret == -2) {
608
            virInterfaceReportError(VIR_ERR_XML_ERROR,
609 610 611 612 613 614
                     "%s", _("bond interface miimon updelay invalid"));
            goto error;
        } else if (ret == 0) {
            def->data.bond.updelay = (int) tmp;
        }

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

621
    } else if ((node = virXPathNode("./arpmon[1]", ctxt)) != NULL) {
622 623 624

        def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_ARP;

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

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

642
        def->data.bond.validate = virInterfaceDefParseBondArpValid(ctxt);
643 644
        if (def->data.bond.validate < 0) {
            ret = -1;
645
            goto error;
646
        }
647 648 649 650 651 652
    }
error:
    return(ret);
}

static int
653
virInterfaceDefParseVlan(virInterfaceDefPtr def,
654
                         xmlXPathContextPtr ctxt) {
655
    def->data.vlan.tag = virXPathString("string(./@tag)", ctxt);
656
    if (def->data.vlan.tag == NULL) {
657
        virInterfaceReportError(VIR_ERR_XML_ERROR,
658 659 660 661 662
                    "%s", _("vlan interface misses the tag attribute"));
        return(-1);
    }

    def->data.vlan.devname =
663
         virXPathString("string(./interface/@name)", ctxt);
664
    if (def->data.vlan.devname == NULL) {
665
        virInterfaceReportError(VIR_ERR_XML_ERROR,
666 667 668 669 670 671 672
                    "%s", _("vlan interface misses name attribute"));
        return(-1);
    }
    return(0);
}

static virInterfaceDefPtr
673
virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType) {
674 675 676 677 678 679
    virInterfaceDefPtr def;
    int type;
    char *tmp;
    xmlNodePtr cur = ctxt->node;

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

    if (VIR_ALLOC(def) < 0) {
696
        virReportOOMError();
697 698
        return NULL;
    }
699 700 701 702 703 704 705 706 707 708

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

735
            if (virInterfaceDefParseName(def, ctxt) < 0)
736
                goto error;
737
            if (virInterfaceDefParseStartMode(def, ctxt) < 0)
738
                goto error;
739
            if (virInterfaceDefParseMtu(def, ctxt) < 0)
740
                goto error;
741
            if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
742 743
                goto error;

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

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

786
            bond = virXPathNode("./bond[1]", ctxt);
787
            if (bond == NULL) {
788
                virInterfaceReportError(VIR_ERR_XML_ERROR,
789 790 791 792
                            "%s", _("bond interface misses the bond element"));
                goto error;
            }
            ctxt->node = bond;
793
            if (virInterfaceDefParseBond(def, ctxt)  < 0)
794 795 796 797 798 799
                goto error;
            break;
        }
        case VIR_INTERFACE_TYPE_VLAN: {
            xmlNodePtr vlan;

800
            tmp = virXPathString("string(./@name)", ctxt);
801 802
            if (tmp != NULL)
                def->name = tmp;
803
            if (virInterfaceDefParseStartMode(def, ctxt) < 0)
804
                goto error;
805
            if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
806
                goto error;
807
            vlan = virXPathNode("./vlan[1]", ctxt);
808
            if (vlan == NULL) {
809
                virInterfaceReportError(VIR_ERR_XML_ERROR,
810 811 812 813
                            "%s", _("vlan interface misses the vlan element"));
                goto error;
            }
            ctxt->node = vlan;
814
            if (virInterfaceDefParseVlan(def, ctxt)  < 0)
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
                goto error;
            break;
        }

    }

    ctxt->node = cur;
    return def;

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

830 831
virInterfaceDefPtr virInterfaceDefParseNode(xmlDocPtr xml,
                                            xmlNodePtr root)
832 833 834 835 836
{
    xmlXPathContextPtr ctxt = NULL;
    virInterfaceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "interface")) {
837
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
838 839 840 841 842 843
                              "%s", _("incorrect root element"));
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
844
        virReportOOMError();
845 846 847 848
        goto cleanup;
    }

    ctxt->node = root;
849
    def = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_LAST);
850 851 852 853 854 855 856 857 858 859 860 861 862

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) {
863
        if (virGetLastError() == NULL &&
864 865
            ctxt->lastError.level == XML_ERR_FATAL &&
            ctxt->lastError.message != NULL) {
866
            virInterfaceReportError (VIR_ERR_XML_DETAIL,
867 868 869 870 871 872 873
                                     _("at line %d: %s"),
                                     ctxt->lastError.line,
                                     ctxt->lastError.message);
        }
    }
}

874
virInterfaceDefPtr virInterfaceDefParseString(const char *xmlStr)
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
{
    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;

    xml = xmlCtxtReadDoc (pctxt, BAD_CAST xmlStr, "interface.xml", NULL,
                          XML_PARSE_NOENT | XML_PARSE_NONET |
                          XML_PARSE_NOWARNING);
    if (!xml) {
891 892
        if (virGetLastError() == NULL)
            virInterfaceReportError(VIR_ERR_XML_ERROR,
893 894 895 896 897
                                    "%s", _("failed to parse xml document"));
        goto cleanup;
    }

    if ((root = xmlDocGetRootElement(xml)) == NULL) {
898
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
899 900 901 902
                              "%s", _("missing root element"));
        goto cleanup;
    }

903
    def = virInterfaceDefParseNode(xml, root);
904 905 906 907 908 909 910

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

911
virInterfaceDefPtr virInterfaceDefParseFile(const char *filename)
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
{
    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;

    xml = xmlCtxtReadFile (pctxt, filename, NULL,
                           XML_PARSE_NOENT | XML_PARSE_NONET |
                           XML_PARSE_NOWARNING);
    if (!xml) {
928 929
        if (virGetLastError() == NULL)
            virInterfaceReportError(VIR_ERR_XML_ERROR,
930 931 932 933 934
                                    "%s", _("failed to parse xml document"));
        goto cleanup;
    }

    if ((root = xmlDocGetRootElement(xml)) == NULL) {
935
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
936 937 938 939
                              "%s", _("missing root element"));
        goto cleanup;
    }

940
    def = virInterfaceDefParseNode(xml, root);
941 942 943 944 945 946 947 948

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

static int
949
virInterfaceBridgeDefFormat(virBufferPtr buf,
950
                            const virInterfaceDefPtr def, int level) {
951 952 953
    int i;
    int ret = 0;

954
    virBufferVSprintf(buf, "%*s  <bridge", level*2, "");
955
    if (def->data.bridge.stp == 1)
956
        virBufferAddLit(buf, " stp='on'");
957
    else if (def->data.bridge.stp == 0)
958 959 960 961
        virBufferAddLit(buf, " stp='off'");
    if (def->data.bridge.delay != NULL)
        virBufferVSprintf(buf, " delay='%s'", def->data.bridge.delay);
    virBufferAddLit(buf, ">\n");
962 963

    for (i = 0;i < def->data.bridge.nbItf;i++) {
964
        if (virInterfaceDefDevFormat(buf,
965
                                     def->data.bridge.itf[i], level+2) < 0)
966 967 968
            ret = -1;
    }

969
    virBufferVSprintf(buf, "%*s  </bridge>\n", level*2, "");
970 971 972 973
    return(ret);
}

static int
974
virInterfaceBondDefFormat(virBufferPtr buf,
975
                          const virInterfaceDefPtr def, int level) {
976 977 978
    int i;
    int ret = 0;

979
    virBufferVSprintf(buf, "%*s  <bond", level*2, "");
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    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) {
997 998
        virBufferVSprintf(buf, "%*s    <miimon freq='%d'",
                          level*2, "", def->data.bond.frequency);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        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) {
1010
            virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1011 1012 1013
                          "%s", _("bond arp monitoring has no target"));
            return(-1);
        }
1014 1015
        virBufferVSprintf(buf, "%*s    <arpmon interval='%d' target='%s'",
                          level*2, "",
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
                          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++) {
1026
        if (virInterfaceDefDevFormat(buf, def->data.bond.itf[i], level+2) < 0)
1027 1028 1029
            ret = -1;
    }

1030
    virBufferVSprintf(buf, "%*s  </bond>\n", level*2, "");
1031 1032 1033 1034
    return(ret);
}

static int
1035
virInterfaceVlanDefFormat(virBufferPtr buf,
1036
                          const virInterfaceDefPtr def, int level) {
1037
    if (def->data.vlan.tag == NULL) {
1038
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1039 1040 1041 1042
                                "%s", _("vlan misses the tag name"));
        return(-1);
    }

1043 1044
    virBufferVSprintf(buf, "%*s  <vlan tag='%s'",
                      level*2, "", def->data.vlan.tag);
1045 1046
    if (def->data.vlan.devname != NULL) {
        virBufferAddLit(buf, ">\n");
1047 1048 1049
        virBufferVSprintf(buf, "%*s    <interface name='%s'/>\n",
                          level*2, "", def->data.vlan.devname);
        virBufferVSprintf(buf, "%*s  </vlan>\n", level*2, "");
1050 1051 1052 1053 1054 1055
    } else
        virBufferAddLit(buf, "/>\n");
    return(0);
}

static int
1056
virInterfaceProtocolDefFormat(virBufferPtr buf, const virInterfaceDefPtr def,
1057
                              int level) {
1058 1059 1060 1061
    int pp, ii;

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

1062 1063
        virBufferVSprintf(buf, "%*s  <protocol family='%s'>\n",
                          level*2, "", def->protos[pp]->family);
1064 1065

        if (def->protos[pp]->autoconf) {
1066
            virBufferVSprintf(buf, "%*s    <autoconf/>\n", level*2, "");
1067 1068 1069 1070
        }

        if (def->protos[pp]->dhcp) {
            if (def->protos[pp]->peerdns == 0)
1071 1072
                virBufferVSprintf(buf, "%*s    <dhcp peerdns='no'/>\n",
                                  level*2, "");
1073
            else if (def->protos[pp]->peerdns == 1)
1074 1075
                virBufferVSprintf(buf, "%*s    <dhcp peerdns='yes'/>\n",
                                  level*2, "");
1076
            else
1077
                virBufferVSprintf(buf, "%*s    <dhcp/>\n", level*2, "");
1078 1079 1080 1081 1082
        }

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

1083
                virBufferVSprintf(buf, "%*s    <ip address='%s'", level*2, "",
1084 1085 1086 1087 1088 1089 1090 1091 1092
                                  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) {
1093 1094
            virBufferVSprintf(buf, "%*s    <route gateway='%s'/>\n",
                              level*2, "", def->protos[pp]->gateway);
1095 1096
        }

1097
        virBufferVSprintf(buf, "%*s  </protocol>\n", level*2, "");
1098 1099 1100 1101 1102
    }
    return(0);
}

static int
1103
virInterfaceStartmodeDefFormat(virBufferPtr buf,
1104 1105
                               enum virInterfaceStartMode startmode,
                               int level) {
1106 1107
    const char *mode;
    switch (startmode) {
1108 1109
        case VIR_INTERFACE_START_UNSPECIFIED:
            return 0;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
        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:
1120
            virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1121 1122 1123
                        "%s", _("virInterfaceDefFormat unknown startmode"));
            return -1;
    }
1124
    virBufferVSprintf(buf, "%*s  <start mode='%s'/>\n", level*2, "", mode);
1125 1126 1127
    return(0);
}

1128
static int
1129
virInterfaceDefDevFormat(virBufferPtr buf,
1130
                         const virInterfaceDefPtr def, int level) {
1131
    const char *type = NULL;
1132

1133
    if (def == NULL) {
1134
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1135 1136 1137 1138 1139
                        "%s", _("virInterfaceDefFormat NULL def"));
        goto cleanup;
    }

    if ((def->name == NULL) && (def->type != VIR_INTERFACE_TYPE_VLAN)) {
1140
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1141
                        "%s", _("virInterfaceDefFormat missing interface name"));
1142 1143 1144 1145
        goto cleanup;
    }

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

1151
    virBufferVSprintf(buf, "%*s<interface type='%s' ", level*2, "", type);
1152
    if (def->name != NULL)
1153 1154
        virBufferEscapeString(buf, "name='%s'", def->name);
    virBufferAddLit(buf, ">\n");
1155 1156 1157

    switch (def->type) {
        case VIR_INTERFACE_TYPE_ETHERNET:
1158
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1159
            if (def->mac != NULL)
1160 1161
                virBufferVSprintf(buf, "%*s  <mac address='%s'/>\n",
                                  level*2, "", def->mac);
1162
            if (def->mtu != 0)
1163 1164
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
1165
            virInterfaceProtocolDefFormat(buf, def, level);
1166 1167
            break;
        case VIR_INTERFACE_TYPE_BRIDGE:
1168
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1169
            if (def->mtu != 0)
1170 1171
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
1172 1173
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceBridgeDefFormat(buf, def, level);
1174 1175
            break;
        case VIR_INTERFACE_TYPE_BOND:
1176
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1177
            if (def->mtu != 0)
1178 1179
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
1180 1181
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceBondDefFormat(buf, def, level);
1182 1183
            break;
        case VIR_INTERFACE_TYPE_VLAN:
1184
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1185
            if (def->mac != NULL)
1186 1187
                virBufferVSprintf(buf, "%*s  <mac address='%s'/>\n",
                                  level*2, "", def->mac);
1188
            if (def->mtu != 0)
1189 1190
                virBufferVSprintf(buf, "%*s  <mtu size='%d'/>\n",
                                  level*2, "", def->mtu);
1191 1192
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceVlanDefFormat(buf, def, level);
1193 1194 1195
            break;
    }

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

1198
    if (virBufferError(buf))
1199
        goto no_memory;
1200
    return 0;
1201
no_memory:
1202
    virReportOOMError();
1203
cleanup:
1204 1205 1206
    return -1;
}

1207
char *virInterfaceDefFormat(const virInterfaceDefPtr def)
1208 1209 1210
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1211
    if (virInterfaceDefDevFormat(&buf, def, 0) < 0) {
1212 1213 1214 1215
        virBufferFreeAndReset(&buf);
        return NULL;
    }
    return virBufferContentAndReset(&buf);
1216 1217
}

1218 1219
/* virInterfaceObj manipulation */

1220 1221 1222 1223 1224 1225 1226 1227 1228
void virInterfaceObjLock(virInterfaceObjPtr obj)
{
    virMutexLock(&obj->lock);
}

void virInterfaceObjUnlock(virInterfaceObjPtr obj)
{
    virMutexUnlock(&obj->lock);
}
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 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

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;
}

1293
virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces,
1294 1295
                                         const virInterfaceDefPtr def)
{
1296
    virInterfaceObjPtr iface;
1297

1298 1299 1300 1301
    if ((iface = virInterfaceFindByName(interfaces, def->name))) {
        if (iface->def)
            virInterfaceDefFree(iface->def);
        iface->def = def;
1302

1303
        return iface;
1304 1305
    }

1306
    if (VIR_ALLOC(iface) < 0) {
1307
        virReportOOMError();
1308 1309
        return NULL;
    }
1310
    if (virMutexInit(&iface->lock) < 0) {
1311
        virInterfaceReportError(VIR_ERR_INTERNAL_ERROR,
1312
                                "%s", _("cannot initialize mutex"));
1313
        VIR_FREE(iface);
1314 1315
        return NULL;
    }
1316 1317
    virInterfaceObjLock(iface);
    iface->def = def;
1318 1319

    if (VIR_REALLOC_N(interfaces->objs, interfaces->count + 1) < 0) {
1320
        virReportOOMError();
1321
        VIR_FREE(iface);
1322 1323 1324
        return NULL;
    }

1325
    interfaces->objs[interfaces->count] = iface;
1326 1327
    interfaces->count++;

1328
    return iface;
1329 1330 1331 1332

}

void virInterfaceRemove(virInterfaceObjListPtr interfaces,
1333
                        const virInterfaceObjPtr iface)
1334 1335 1336
{
    unsigned int i;

1337
    virInterfaceObjUnlock(iface);
1338 1339
    for (i = 0 ; i < interfaces->count ; i++) {
        virInterfaceObjLock(interfaces->objs[i]);
1340
        if (interfaces->objs[i] == iface) {
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
            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]);
    }
}