interface_conf.c 39.1 KB
Newer Older
1 2 3
/*
 * interface_conf.c: interfaces XML handling
 *
4
 * Copyright (C) 2006-2010, 2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Author: Daniel Veillard <veillard@redhat.com>
 *         Laine Stump <laine@redhat.com>
 */

#include <config.h>
25
#include "virerror.h"
26 27 28 29
#include "datatypes.h"

#include "interface_conf.h"

30
#include "viralloc.h"
31
#include "virxml.h"
32
#include "viruuid.h"
33
#include "virbuffer.h"
34 35 36 37 38

#define VIR_FROM_THIS VIR_FROM_INTERFACE

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

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

47 48 49 50 51
static
void virInterfaceIpDefFree(virInterfaceIpDefPtr def) {
    if (def == NULL)
        return;
    VIR_FREE(def->address);
52
    VIR_FREE(def);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

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

70 71
void virInterfaceDefFree(virInterfaceDefPtr def)
{
72
    int i, pp;
73 74 75 76 77 78 79 80 81

    if (def == NULL)
        return;

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

    switch (def->type) {
        case VIR_INTERFACE_TYPE_BRIDGE:
82
            VIR_FREE(def->data.bridge.delay);
83
            for (i = 0; i < def->data.bridge.nbItf; i++) {
84
                if (def->data.bridge.itf[i] == NULL)
85
                    break; /* to cope with half parsed data on errors */
86
                virInterfaceDefFree(def->data.bridge.itf[i]);
87 88 89 90 91
            }
            VIR_FREE(def->data.bridge.itf);
            break;
        case VIR_INTERFACE_TYPE_BOND:
            VIR_FREE(def->data.bond.target);
92
            for (i = 0; i < def->data.bond.nbItf; i++) {
93
                if (def->data.bond.itf[i] == NULL)
94
                    break; /* to cope with half parsed data on errors */
95
                virInterfaceDefFree(def->data.bond.itf[i]);
96 97 98 99 100 101 102 103 104
            }
            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;
    }

105 106 107 108 109
    /* free all protos */
    for (pp = 0; pp < def->nprotos; pp++) {
        virInterfaceProtocolDefFree(def->protos[pp]);
    }
    VIR_FREE(def->protos);
110 111 112 113
    VIR_FREE(def);
}

static int
114
virInterfaceDefParseName(virInterfaceDefPtr def,
115
                         xmlXPathContextPtr ctxt) {
116 117
    char *tmp;

118
    tmp = virXPathString("string(./@name)", ctxt);
119
    if (tmp == NULL) {
120 121
        virReportError(VIR_ERR_XML_ERROR,
                       "%s",  _("interface has no name"));
122
        return -1;
123 124
    }
    def->name = tmp;
125
    return 0;
126 127 128
}

static int
129
virInterfaceDefParseMtu(virInterfaceDefPtr def,
130 131 132
                        xmlXPathContextPtr ctxt) {
    unsigned long mtu;
    int ret;
133

134
    ret = virXPathULong("string(./mtu/@size)", ctxt, &mtu);
135
    if ((ret == -2) || ((ret == 0) && (mtu > 100000))) {
136 137
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("interface mtu value is improper"));
138
        return -1;
139 140 141
    } else if (ret == 0) {
        def->mtu = (unsigned int) mtu;
    }
142
    return 0;
143 144 145
}

static int
146
virInterfaceDefParseStartMode(virInterfaceDefPtr def,
147 148 149
                              xmlXPathContextPtr ctxt) {
    char *tmp;

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

static int
170
virInterfaceDefParseBondMode(xmlXPathContextPtr ctxt) {
171 172 173
    char *tmp;
    int ret = 0;

174
    tmp = virXPathString("string(./@mode)", ctxt);
175
    if (tmp == NULL)
176
        return VIR_INTERFACE_BOND_NONE;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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 {
192 193
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown bonding mode %s"), tmp);
194 195 196
        ret = -1;
    }
    VIR_FREE(tmp);
197
    return ret;
198 199 200
}

static int
201
virInterfaceDefParseBondMiiCarrier(xmlXPathContextPtr ctxt) {
202 203 204
    char *tmp;
    int ret = 0;

205
    tmp = virXPathString("string(./miimon/@carrier)", ctxt);
206
    if (tmp == NULL)
207
        return VIR_INTERFACE_BOND_MII_NONE;
208 209 210 211 212
    if (STREQ(tmp, "ioctl"))
        ret = VIR_INTERFACE_BOND_MII_IOCTL;
    else if (STREQ(tmp, "netif"))
        ret = VIR_INTERFACE_BOND_MII_NETIF;
    else {
213 214
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown mii bonding carrier %s"), tmp);
215 216 217
        ret = -1;
    }
    VIR_FREE(tmp);
218
    return ret;
219 220 221
}

static int
222
virInterfaceDefParseBondArpValid(xmlXPathContextPtr ctxt) {
223 224 225
    char *tmp;
    int ret = 0;

226
    tmp = virXPathString("string(./arpmon/@validate)", ctxt);
227
    if (tmp == NULL)
228
        return VIR_INTERFACE_BOND_ARP_NONE;
229 230 231 232 233 234 235
    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 {
236 237
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown arp bonding validate %s"), tmp);
238 239 240
        ret = -1;
    }
    VIR_FREE(tmp);
241
    return ret;
242 243 244
}

static int
245
virInterfaceDefParseDhcp(virInterfaceProtocolDefPtr def,
246
                         xmlNodePtr dhcp, xmlXPathContextPtr ctxt) {
247
    xmlNodePtr save;
248 249 250
    char *tmp;
    int ret = 0;

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

270
    ctxt->node = save;
271
    return ret;
272 273 274
}

static int
275
virInterfaceDefParseIp(virInterfaceIpDefPtr def,
276
                       xmlXPathContextPtr ctxt) {
277 278 279 280
    int ret = 0;
    char *tmp;
    long l;

281
    tmp = virXPathString("string(./@address)", ctxt);
282
    def->address = tmp;
283
    if (tmp != NULL) {
284
        ret = virXPathLong("string(./@prefix)", ctxt, &l);
285
        if (ret == 0)
286
            def->prefix = (int) l;
287
        else if (ret == -2) {
288 289
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("Invalid ip address prefix value"));
290
            return -1;
291 292 293
        }
    }

294
    return 0;
295 296 297
}

static int
298
virInterfaceDefParseProtoIPv4(virInterfaceProtocolDefPtr def,
299
                              xmlXPathContextPtr ctxt) {
300 301 302 303 304
    xmlNodePtr dhcp;
    xmlNodePtr *ipNodes = NULL;
    int nIpNodes, ii, ret = -1;
    char *tmp;

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

308
    dhcp = virXPathNode("./dhcp", ctxt);
309
    if (dhcp != NULL) {
310
        ret = virInterfaceDefParseDhcp(def, dhcp, ctxt);
311
        if (ret != 0)
312
           return ret;
313
    }
314

315
    nIpNodes = virXPathNodeSet("./ip", ctxt, &ipNodes);
316 317
    if (nIpNodes < 0)
        return -1;
318 319 320 321
    if (ipNodes == NULL)
        return 0;

    if (VIR_ALLOC_N(def->ips, nIpNodes) < 0) {
322
        virReportOOMError();
323 324 325 326 327 328 329
        goto error;
    }

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

        virInterfaceIpDefPtr ip;
330

331
        if (VIR_ALLOC(ip) < 0) {
332
            virReportOOMError();
333 334 335 336
            goto error;
        }

        ctxt->node = ipNodes[ii];
337
        ret = virInterfaceDefParseIp(ip, ctxt);
338 339 340 341 342 343 344 345 346 347 348
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

error:
    VIR_FREE(ipNodes);
349
    return ret;
350 351 352
}

static int
353
virInterfaceDefParseProtoIPv6(virInterfaceProtocolDefPtr def,
354 355 356 357 358 359
                              xmlXPathContextPtr ctxt) {
    xmlNodePtr dhcp, autoconf;
    xmlNodePtr *ipNodes = NULL;
    int nIpNodes, ii, ret = -1;
    char *tmp;

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

363
    autoconf = virXPathNode("./autoconf", ctxt);
364 365 366
    if (autoconf != NULL)
        def->autoconf = 1;

367
    dhcp = virXPathNode("./dhcp", ctxt);
368
    if (dhcp != NULL) {
369
        ret = virInterfaceDefParseDhcp(def, dhcp, ctxt);
370
        if (ret != 0)
371
           return ret;
372
    }
373

374
    nIpNodes = virXPathNodeSet("./ip", ctxt, &ipNodes);
375 376
    if (nIpNodes < 0)
        return -1;
377 378 379 380
    if (ipNodes == NULL)
        return 0;

    if (VIR_ALLOC_N(def->ips, nIpNodes) < 0) {
381
        virReportOOMError();
382 383 384 385 386 387 388 389 390
        goto error;
    }

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

        virInterfaceIpDefPtr ip;

        if (VIR_ALLOC(ip) < 0) {
391
            virReportOOMError();
392 393 394 395
            goto error;
        }

        ctxt->node = ipNodes[ii];
396
        ret = virInterfaceDefParseIp(ip, ctxt);
397 398 399 400 401 402 403 404 405 406 407
        if (ret != 0) {
            virInterfaceIpDefFree(ip);
            goto error;
        }
        def->ips[def->nips++] = ip;
    }

    ret = 0;

error:
    VIR_FREE(ipNodes);
408
    return ret;
409 410 411
}

static int
412
virInterfaceDefParseIfAdressing(virInterfaceDefPtr def,
413
                                xmlXPathContextPtr ctxt) {
414 415 416
    xmlNodePtr save;
    xmlNodePtr *protoNodes = NULL;
    int nProtoNodes, pp, ret = -1;
417 418 419
    char *tmp;

    save = ctxt->node;
420

421
    nProtoNodes = virXPathNodeSet("./protocol", ctxt, &protoNodes);
422 423 424 425
    if (nProtoNodes < 0)
        goto error;

    if (nProtoNodes == 0) {
426 427
        /* no protocols is an acceptable outcome */
        return 0;
428
    }
429 430

    if (VIR_ALLOC_N(def->protos, nProtoNodes) < 0) {
431
        virReportOOMError();
432
        goto error;
433 434
    }

435 436 437 438 439 440
    def->nprotos = 0;
    for (pp = 0; pp < nProtoNodes; pp++) {

        virInterfaceProtocolDefPtr proto;

        if (VIR_ALLOC(proto) < 0) {
441
            virReportOOMError();
442 443 444 445
            goto error;
        }

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

    ret = 0;

error:
    VIR_FREE(protoNodes);
479
    ctxt->node = save;
480
    return ret;
481 482 483 484

}

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

    bridge = ctxt->node;
494
    nbItf = virXPathNodeSet("./interface", ctxt, &interfaces);
495
    if (nbItf < 0) {
496 497 498
        ret = -1;
        goto error;
    }
499 500
    if (nbItf > 0) {
        if (VIR_ALLOC_N(def->data.bridge.itf, nbItf) < 0) {
501
            virReportOOMError();
502 503 504
            ret = -1;
            goto error;
        }
505 506
        def->data.bridge.nbItf = nbItf;

507
        for (i = 0; i < nbItf; i++) {
508
            ctxt->node = interfaces[i];
509
            itf = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_BRIDGE);
510 511 512 513 514 515 516
            if (itf == NULL) {
                ret = -1;
                def->data.bridge.nbItf = i;
                goto error;
            }
            def->data.bridge.itf[i] = itf;
        }
517 518 519 520 521
    }

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

static int
526
virInterfaceDefParseBondItfs(virInterfaceDefPtr def,
527 528 529
                             xmlXPathContextPtr ctxt) {
    xmlNodePtr *interfaces = NULL;
    xmlNodePtr bond = ctxt->node;
530
    virInterfaceDefPtr itf;
531 532 533
    int nbItf, i;
    int ret = 0;

534
    nbItf = virXPathNodeSet("./interface", ctxt, &interfaces);
535 536 537 538 539 540
    if (nbItf < 0) {
        ret = -1;
        goto error;
    }

    if (nbItf == 0) {
541 542
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("bond has no interfaces"));
543 544 545
        ret = -1;
        goto error;
    }
546

547
    if (VIR_ALLOC_N(def->data.bond.itf, nbItf) < 0) {
548
        virReportOOMError();
549 550 551 552 553
        ret = -1;
        goto error;
    }
    def->data.bond.nbItf = nbItf;

554
    for (i = 0; i < nbItf; i++) {
555
        ctxt->node = interfaces[i];
556
        itf = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_BOND);
557 558 559 560 561 562 563 564 565 566 567
        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;
568
    return ret;
569 570 571
}

static int
572
virInterfaceDefParseBond(virInterfaceDefPtr def,
573
                         xmlXPathContextPtr ctxt) {
J
Jim Fehlig 已提交
574
    int res;
575

576
    def->data.bond.mode = virInterfaceDefParseBondMode(ctxt);
577
    if (def->data.bond.mode < 0)
J
Jim Fehlig 已提交
578
        return -1;
579

J
Jim Fehlig 已提交
580 581
    if (virInterfaceDefParseBondItfs(def, ctxt) != 0)
        return -1;
582

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

J
Jim Fehlig 已提交
586 587 588
        res = virXPathInt("string(./miimon/@freq)", ctxt,
                          &def->data.bond.frequency);
        if ((res == -2) || (res == -1)) {
589 590
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("bond interface miimon freq missing or invalid"));
J
Jim Fehlig 已提交
591
            return -1;
592 593
        }

J
Jim Fehlig 已提交
594 595 596
        res = virXPathInt("string(./miimon/@downdelay)", ctxt,
                          &def->data.bond.downdelay);
        if (res == -2) {
597 598
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("bond interface miimon downdelay invalid"));
J
Jim Fehlig 已提交
599
            return -1;
600 601
        }

J
Jim Fehlig 已提交
602 603 604
        res = virXPathInt("string(./miimon/@updelay)", ctxt,
                          &def->data.bond.updelay);
        if (res == -2) {
605 606
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("bond interface miimon updelay invalid"));
J
Jim Fehlig 已提交
607
            return -1;
608 609
        }

610
        def->data.bond.carrier = virInterfaceDefParseBondMiiCarrier(ctxt);
J
Jim Fehlig 已提交
611 612
        if (def->data.bond.carrier < 0)
            return -1;
613

614
    } else if (virXPathNode("./arpmon[1]", ctxt) != NULL) {
615 616 617

        def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_ARP;

J
Jim Fehlig 已提交
618 619 620
        res = virXPathInt("string(./arpmon/@interval)", ctxt,
                          &def->data.bond.interval);
        if ((res == -2) || (res == -1)) {
621 622
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("bond interface arpmon interval missing or invalid"));
J
Jim Fehlig 已提交
623
            return -1;
624 625 626
        }

        def->data.bond.target =
627
            virXPathString("string(./arpmon/@target)", ctxt);
628
        if (def->data.bond.target == NULL) {
629 630
            virReportError(VIR_ERR_XML_ERROR,
                           "%s", _("bond interface arpmon target missing"));
J
Jim Fehlig 已提交
631
            return -1;
632 633
        }

634
        def->data.bond.validate = virInterfaceDefParseBondArpValid(ctxt);
J
Jim Fehlig 已提交
635 636
        if (def->data.bond.validate < 0)
            return -1;
637
    }
J
Jim Fehlig 已提交
638 639

    return 0;
640 641 642
}

static int
643
virInterfaceDefParseVlan(virInterfaceDefPtr def,
644
                         xmlXPathContextPtr ctxt) {
645
    def->data.vlan.tag = virXPathString("string(./@tag)", ctxt);
646
    if (def->data.vlan.tag == NULL) {
647 648
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("vlan interface misses the tag attribute"));
649
        return -1;
650 651 652
    }

    def->data.vlan.devname =
653
         virXPathString("string(./interface/@name)", ctxt);
654
    if (def->data.vlan.devname == NULL) {
655 656
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("vlan interface misses name attribute"));
657
        return -1;
658
    }
659
    return 0;
660 661 662
}

static virInterfaceDefPtr
663
virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType) {
664 665 666 667 668 669
    virInterfaceDefPtr def;
    int type;
    char *tmp;
    xmlNodePtr cur = ctxt->node;

    /* check @type */
670
    tmp = virXPathString("string(./@type)", ctxt);
671
    if (tmp == NULL) {
672 673
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("interface misses the type attribute"));
674
        return NULL;
675 676 677
    }
    type = virInterfaceTypeFromString(tmp);
    if (type == -1) {
678 679
        virReportError(VIR_ERR_XML_ERROR,
                       _("unknown interface type %s"), tmp);
680
        VIR_FREE(tmp);
681
        return NULL;
682 683 684 685
    }
    VIR_FREE(tmp);

    if (VIR_ALLOC(def) < 0) {
686
        virReportOOMError();
687 688
        return NULL;
    }
689 690 691 692 693 694 695 696 697 698

    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))
        {
699 700 701
        virReportError(VIR_ERR_XML_ERROR,
                       _("interface has unsupported type '%s'"),
                       virInterfaceTypeToString(type));
702 703
        goto error;
    }
704 705 706
    def->type = type;
    switch (type) {
        case VIR_INTERFACE_TYPE_ETHERNET:
707
            if (virInterfaceDefParseName(def, ctxt) < 0)
708
                goto error;
709
            tmp = virXPathString("string(./mac/@address)", ctxt);
710 711
            if (tmp != NULL)
                def->mac = tmp;
712 713
            if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
                /* only recognize these in toplevel bond interfaces */
714
                if (virInterfaceDefParseStartMode(def, ctxt) < 0)
715
                    goto error;
716
                if (virInterfaceDefParseMtu(def, ctxt) < 0)
717
                    goto error;
718
                if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
719 720
                    goto error;
            }
721 722 723 724
            break;
        case VIR_INTERFACE_TYPE_BRIDGE: {
            xmlNodePtr bridge;

725
            if (virInterfaceDefParseName(def, ctxt) < 0)
726
                goto error;
727
            if (virInterfaceDefParseStartMode(def, ctxt) < 0)
728
                goto error;
729
            if (virInterfaceDefParseMtu(def, ctxt) < 0)
730
                goto error;
731
            if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
732 733
                goto error;

734
            bridge = virXPathNode("./bridge[1]", ctxt);
735
            if (bridge == NULL) {
736 737
                virReportError(VIR_ERR_XML_ERROR,
                               "%s", _("bridge interface misses the bridge element"));
738 739 740 741 742 743 744 745 746 747
                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 {
748 749 750
                    virReportError(VIR_ERR_XML_ERROR,
                                   _("bridge interface stp should be on or off got %s"),
                                   tmp);
751 752 753 754 755
                    VIR_FREE(tmp);
                    goto error;
                }
                VIR_FREE(tmp);
            }
756
            def->data.bridge.delay = virXMLPropString(bridge, "delay");
757
            ctxt->node = bridge;
758 759
            if (virInterfaceDefParseBridge(def, ctxt) < 0)
                goto error;
760 761 762 763 764
            break;
        }
        case VIR_INTERFACE_TYPE_BOND: {
            xmlNodePtr bond;

765
            if (virInterfaceDefParseName(def, ctxt) < 0)
766
                goto error;
767 768
            if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
                /* only recognize these in toplevel bond interfaces */
769
                if (virInterfaceDefParseStartMode(def, ctxt) < 0)
770
                    goto error;
771
                if (virInterfaceDefParseMtu(def, ctxt) < 0)
772
                    goto error;
773
                if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
774 775 776
                    goto error;
            }

777
            bond = virXPathNode("./bond[1]", ctxt);
778
            if (bond == NULL) {
779 780
                virReportError(VIR_ERR_XML_ERROR,
                               "%s", _("bond interface misses the bond element"));
781 782 783
                goto error;
            }
            ctxt->node = bond;
784
            if (virInterfaceDefParseBond(def, ctxt)  < 0)
785 786 787 788 789 790
                goto error;
            break;
        }
        case VIR_INTERFACE_TYPE_VLAN: {
            xmlNodePtr vlan;

791
            tmp = virXPathString("string(./@name)", ctxt);
792 793
            if (tmp != NULL)
                def->name = tmp;
794
            if (virInterfaceDefParseStartMode(def, ctxt) < 0)
795
                goto error;
796
            if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
797
                goto error;
798
            vlan = virXPathNode("./vlan[1]", ctxt);
799
            if (vlan == NULL) {
800 801
                virReportError(VIR_ERR_XML_ERROR,
                               "%s", _("vlan interface misses the vlan element"));
802 803 804
                goto error;
            }
            ctxt->node = vlan;
805
            if (virInterfaceDefParseVlan(def, ctxt)  < 0)
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
                goto error;
            break;
        }

    }

    ctxt->node = cur;
    return def;

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

821 822
virInterfaceDefPtr virInterfaceDefParseNode(xmlDocPtr xml,
                                            xmlNodePtr root)
823 824 825 826 827
{
    xmlXPathContextPtr ctxt = NULL;
    virInterfaceDefPtr def = NULL;

    if (!xmlStrEqual(root->name, BAD_CAST "interface")) {
828 829 830 831
        virReportError(VIR_ERR_XML_ERROR,
                       _("unexpected root element <%s>, "
                         "expecting <interface>"),
                       root->name);
832 833 834 835 836
        return NULL;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
837
        virReportOOMError();
838 839 840 841
        goto cleanup;
    }

    ctxt->node = root;
842
    def = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_LAST);
843 844 845 846 847 848

cleanup:
    xmlXPathFreeContext(ctxt);
    return def;
}

J
Jiri Denemark 已提交
849 850 851
static virInterfaceDefPtr
virInterfaceDefParse(const char *xmlStr,
                     const char *filename)
852
{
J
Jiri Denemark 已提交
853
    xmlDocPtr xml;
854 855
    virInterfaceDefPtr def = NULL;

856
    if ((xml = virXMLParse(filename, xmlStr, _("(interface_definition)")))) {
J
Jiri Denemark 已提交
857 858
        def = virInterfaceDefParseNode(xml, xmlDocGetRootElement(xml));
        xmlFreeDoc(xml);
859 860 861 862 863
    }

    return def;
}

J
Jiri Denemark 已提交
864
virInterfaceDefPtr virInterfaceDefParseString(const char *xmlStr)
865
{
J
Jiri Denemark 已提交
866 867
    return virInterfaceDefParse(xmlStr, NULL);
}
868

J
Jiri Denemark 已提交
869 870 871
virInterfaceDefPtr virInterfaceDefParseFile(const char *filename)
{
    return virInterfaceDefParse(NULL, filename);
872 873 874
}

static int
875
virInterfaceBridgeDefFormat(virBufferPtr buf,
876
                            const virInterfaceDefPtr def, int level) {
877 878 879
    int i;
    int ret = 0;

880
    virBufferAsprintf(buf, "%*s  <bridge", level*2, "");
881
    if (def->data.bridge.stp == 1)
882
        virBufferAddLit(buf, " stp='on'");
883
    else if (def->data.bridge.stp == 0)
884 885
        virBufferAddLit(buf, " stp='off'");
    if (def->data.bridge.delay != NULL)
886
        virBufferAsprintf(buf, " delay='%s'", def->data.bridge.delay);
887
    virBufferAddLit(buf, ">\n");
888

889
    for (i = 0; i < def->data.bridge.nbItf; i++) {
890
        if (virInterfaceDefDevFormat(buf,
891
                                     def->data.bridge.itf[i], level+2) < 0)
892 893 894
            ret = -1;
    }

895
    virBufferAsprintf(buf, "%*s  </bridge>\n", level*2, "");
896
    return ret;
897 898 899
}

static int
900
virInterfaceBondDefFormat(virBufferPtr buf,
901
                          const virInterfaceDefPtr def, int level) {
902 903 904
    int i;
    int ret = 0;

905
    virBufferAsprintf(buf, "%*s  <bond", level*2, "");
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    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) {
923
        virBufferAsprintf(buf, "%*s    <miimon freq='%d'",
924
                          level*2, "", def->data.bond.frequency);
925
        if (def->data.bond.downdelay > 0)
926
            virBufferAsprintf(buf, " downdelay='%d'", def->data.bond.downdelay);
927
        if (def->data.bond.updelay > 0)
928
            virBufferAsprintf(buf, " updelay='%d'", def->data.bond.updelay);
929 930 931 932 933 934 935
        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) {
936 937
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("bond arp monitoring has no target"));
938
            return -1;
939
        }
940
        virBufferAsprintf(buf, "%*s    <arpmon interval='%d' target='%s'",
941
                          level*2, "",
942 943 944 945 946 947 948 949 950
                          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");
    }
951
    for (i = 0; i < def->data.bond.nbItf; i++) {
952
        if (virInterfaceDefDevFormat(buf, def->data.bond.itf[i], level+2) < 0)
953 954 955
            ret = -1;
    }

956
    virBufferAsprintf(buf, "%*s  </bond>\n", level*2, "");
957
    return ret;
958 959 960
}

static int
961
virInterfaceVlanDefFormat(virBufferPtr buf,
962
                          const virInterfaceDefPtr def, int level) {
963
    if (def->data.vlan.tag == NULL) {
964 965
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("vlan misses the tag name"));
966
        return -1;
967 968
    }

969
    virBufferAsprintf(buf, "%*s  <vlan tag='%s'",
970
                      level*2, "", def->data.vlan.tag);
971 972
    if (def->data.vlan.devname != NULL) {
        virBufferAddLit(buf, ">\n");
973
        virBufferAsprintf(buf, "%*s    <interface name='%s'/>\n",
974
                          level*2, "", def->data.vlan.devname);
975
        virBufferAsprintf(buf, "%*s  </vlan>\n", level*2, "");
976 977
    } else
        virBufferAddLit(buf, "/>\n");
978
    return 0;
979 980 981
}

static int
982
virInterfaceProtocolDefFormat(virBufferPtr buf, const virInterfaceDefPtr def,
983
                              int level) {
984 985 986 987
    int pp, ii;

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

988
        virBufferAsprintf(buf, "%*s  <protocol family='%s'>\n",
989
                          level*2, "", def->protos[pp]->family);
990 991

        if (def->protos[pp]->autoconf) {
992
            virBufferAsprintf(buf, "%*s    <autoconf/>\n", level*2, "");
993 994 995 996
        }

        if (def->protos[pp]->dhcp) {
            if (def->protos[pp]->peerdns == 0)
997
                virBufferAsprintf(buf, "%*s    <dhcp peerdns='no'/>\n",
998
                                  level*2, "");
999
            else if (def->protos[pp]->peerdns == 1)
1000
                virBufferAsprintf(buf, "%*s    <dhcp peerdns='yes'/>\n",
1001
                                  level*2, "");
1002
            else
1003
                virBufferAsprintf(buf, "%*s    <dhcp/>\n", level*2, "");
1004 1005 1006 1007 1008
        }

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

1009
                virBufferAsprintf(buf, "%*s    <ip address='%s'", level*2, "",
1010 1011
                                  def->protos[pp]->ips[ii]->address);
                if (def->protos[pp]->ips[ii]->prefix != 0) {
1012
                    virBufferAsprintf(buf, " prefix='%d'",
1013 1014 1015 1016 1017 1018
                                      def->protos[pp]->ips[ii]->prefix);
                }
                virBufferAddLit(buf, "/>\n");
            }
        }
        if (def->protos[pp]->gateway != NULL) {
1019
            virBufferAsprintf(buf, "%*s    <route gateway='%s'/>\n",
1020
                              level*2, "", def->protos[pp]->gateway);
1021 1022
        }

1023
        virBufferAsprintf(buf, "%*s  </protocol>\n", level*2, "");
1024
    }
1025
    return 0;
1026 1027 1028
}

static int
1029
virInterfaceStartmodeDefFormat(virBufferPtr buf,
1030 1031
                               enum virInterfaceStartMode startmode,
                               int level) {
1032 1033
    const char *mode;
    switch (startmode) {
1034 1035
        case VIR_INTERFACE_START_UNSPECIFIED:
            return 0;
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        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:
1046 1047
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("virInterfaceDefFormat unknown startmode"));
1048 1049
            return -1;
    }
1050
    virBufferAsprintf(buf, "%*s  <start mode='%s'/>\n", level*2, "", mode);
1051
    return 0;
1052 1053
}

1054
static int
1055
virInterfaceDefDevFormat(virBufferPtr buf,
1056
                         const virInterfaceDefPtr def, int level) {
1057
    const char *type = NULL;
1058

1059
    if (def == NULL) {
1060 1061
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("virInterfaceDefFormat NULL def"));
1062 1063 1064 1065
        goto cleanup;
    }

    if ((def->name == NULL) && (def->type != VIR_INTERFACE_TYPE_VLAN)) {
1066 1067
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("virInterfaceDefFormat missing interface name"));
1068 1069 1070 1071
        goto cleanup;
    }

    if (!(type = virInterfaceTypeToString(def->type))) {
1072 1073
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected interface type %d"), def->type);
1074 1075 1076
        goto cleanup;
    }

1077
    virBufferAsprintf(buf, "%*s<interface type='%s' ", level*2, "", type);
1078
    if (def->name != NULL)
1079 1080
        virBufferEscapeString(buf, "name='%s'", def->name);
    virBufferAddLit(buf, ">\n");
1081 1082 1083

    switch (def->type) {
        case VIR_INTERFACE_TYPE_ETHERNET:
1084
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1085
            if (def->mac != NULL)
1086
                virBufferAsprintf(buf, "%*s  <mac address='%s'/>\n",
1087
                                  level*2, "", def->mac);
1088
            if (def->mtu != 0)
1089
                virBufferAsprintf(buf, "%*s  <mtu size='%d'/>\n",
1090
                                  level*2, "", def->mtu);
1091
            virInterfaceProtocolDefFormat(buf, def, level);
1092 1093
            break;
        case VIR_INTERFACE_TYPE_BRIDGE:
1094
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1095
            if (def->mtu != 0)
1096
                virBufferAsprintf(buf, "%*s  <mtu size='%d'/>\n",
1097
                                  level*2, "", def->mtu);
1098 1099
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceBridgeDefFormat(buf, def, level);
1100 1101
            break;
        case VIR_INTERFACE_TYPE_BOND:
1102
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1103
            if (def->mtu != 0)
1104
                virBufferAsprintf(buf, "%*s  <mtu size='%d'/>\n",
1105
                                  level*2, "", def->mtu);
1106 1107
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceBondDefFormat(buf, def, level);
1108 1109
            break;
        case VIR_INTERFACE_TYPE_VLAN:
1110
            virInterfaceStartmodeDefFormat(buf, def->startmode, level);
1111
            if (def->mac != NULL)
1112
                virBufferAsprintf(buf, "%*s  <mac address='%s'/>\n",
1113
                                  level*2, "", def->mac);
1114
            if (def->mtu != 0)
1115
                virBufferAsprintf(buf, "%*s  <mtu size='%d'/>\n",
1116
                                  level*2, "", def->mtu);
1117 1118
            virInterfaceProtocolDefFormat(buf, def, level);
            virInterfaceVlanDefFormat(buf, def, level);
1119 1120 1121
            break;
    }

1122
    virBufferAsprintf(buf, "%*s</interface>\n", level*2, "");
1123

1124
    if (virBufferError(buf))
1125
        goto no_memory;
1126
    return 0;
1127
no_memory:
1128
    virReportOOMError();
1129
cleanup:
1130 1131 1132
    return -1;
}

1133
char *virInterfaceDefFormat(const virInterfaceDefPtr def)
1134 1135 1136
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;

1137
    if (virInterfaceDefDevFormat(&buf, def, 0) < 0) {
1138 1139 1140 1141
        virBufferFreeAndReset(&buf);
        return NULL;
    }
    return virBufferContentAndReset(&buf);
1142 1143
}

1144 1145
/* virInterfaceObj manipulation */

1146 1147 1148 1149 1150 1151 1152 1153 1154
void virInterfaceObjLock(virInterfaceObjPtr obj)
{
    virMutexLock(&obj->lock);
}

void virInterfaceObjUnlock(virInterfaceObjPtr obj)
{
    virMutexUnlock(&obj->lock);
}
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

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;

1174
    for (i = 0; i < interfaces->count; i++) {
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

        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;

1198
    for (i = 0; i < interfaces->count; i++) {
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
        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;

1212
    for (i = 0; i < interfaces->count; i++)
1213 1214 1215 1216 1217 1218
        virInterfaceObjFree(interfaces->objs[i]);

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

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 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
int virInterfaceObjListClone(virInterfaceObjListPtr src,
                             virInterfaceObjListPtr dest)
{
    int ret = -1;
    unsigned int i, cnt;

    if (!src || !dest)
        goto cleanup;

    virInterfaceObjListFree(dest); /* start with an empty list */
    cnt = src->count;
    for (i = 0; i < cnt; i++) {
        virInterfaceDefPtr def = src->objs[i]->def;
        virInterfaceDefPtr backup;
        virInterfaceObjPtr iface;
        char *xml = virInterfaceDefFormat(def);

        if (!xml)
            goto cleanup;

        if ((backup = virInterfaceDefParseString(xml)) == NULL) {
            VIR_FREE(xml);
            goto cleanup;
        }

        VIR_FREE(xml);
        if ((iface = virInterfaceAssignDef(dest, backup)) == NULL)
            goto cleanup;
        virInterfaceObjUnlock(iface); /* was locked by virInterfaceAssignDef */
    }

    ret = cnt;
cleanup:
    if ((ret < 0) && dest)
       virInterfaceObjListFree(dest);
    return ret;
}

1257
virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces,
1258 1259
                                         const virInterfaceDefPtr def)
{
1260
    virInterfaceObjPtr iface;
1261

1262
    if ((iface = virInterfaceFindByName(interfaces, def->name))) {
1263
        virInterfaceDefFree(iface->def);
1264
        iface->def = def;
1265

1266
        return iface;
1267 1268
    }

1269
    if (VIR_ALLOC(iface) < 0) {
1270
        virReportOOMError();
1271 1272
        return NULL;
    }
1273
    if (virMutexInit(&iface->lock) < 0) {
1274 1275
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot initialize mutex"));
1276
        VIR_FREE(iface);
1277 1278
        return NULL;
    }
1279 1280
    virInterfaceObjLock(iface);
    iface->def = def;
1281 1282

    if (VIR_REALLOC_N(interfaces->objs, interfaces->count + 1) < 0) {
1283
        virReportOOMError();
1284
        VIR_FREE(iface);
1285 1286 1287
        return NULL;
    }

1288
    interfaces->objs[interfaces->count] = iface;
1289 1290
    interfaces->count++;

1291
    return iface;
1292 1293 1294 1295

}

void virInterfaceRemove(virInterfaceObjListPtr interfaces,
1296
                        const virInterfaceObjPtr iface)
1297 1298 1299
{
    unsigned int i;

1300
    virInterfaceObjUnlock(iface);
1301
    for (i = 0; i < interfaces->count; i++) {
1302
        virInterfaceObjLock(interfaces->objs[i]);
1303
        if (interfaces->objs[i] == iface) {
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
            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]);
    }
}