esx_network_driver.c 28.7 KB
Newer Older
1
/*
2
 * esx_network_driver.c: network driver functions for managing VMware ESX
3 4
 *                       host networks
 *
5
 * Copyright (C) 2010-2012 Red Hat, Inc.
M
Matthias Bolte 已提交
6
 * Copyright (C) 2010-2012 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24 25
 *
 */

#include <config.h>

M
Matthias Bolte 已提交
26
#include "md5.h"
27
#include "internal.h"
28
#include "viralloc.h"
29
#include "viruuid.h"
M
Matthias Bolte 已提交
30
#include "network_conf.h"
31 32 33 34 35
#include "esx_private.h"
#include "esx_network_driver.h"
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
36
#include "virstring.h"
37 38 39

#define VIR_FROM_THIS VIR_FROM_ESX

M
Matthias Bolte 已提交
40 41 42 43 44 45
/*
 * The UUID of a network is the MD5 sum of it's key. Therefore, verify that
 * UUID and MD5 sum match in size, because we rely on that.
 */
verify(MD5_DIGEST_SIZE == VIR_UUID_BUFLEN);

46 47 48 49 50


static virDrvOpenStatus
esxNetworkOpen(virConnectPtr conn,
               virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
51
               unsigned int flags)
52
{
E
Eric Blake 已提交
53 54
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

55
    if (conn->driver->no != VIR_DRV_ESX) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        return VIR_DRV_OPEN_DECLINED;
    }

    conn->networkPrivateData = conn->privateData;

    return VIR_DRV_OPEN_SUCCESS;
}



static int
esxNetworkClose(virConnectPtr conn)
{
    conn->networkPrivateData = NULL;

    return 0;
}



M
Matthias Bolte 已提交
76
static int
77
esxConnectNumOfNetworks(virConnectPtr conn)
M
Matthias Bolte 已提交
78 79 80 81 82 83 84 85 86 87 88 89
{
    esxPrivate *priv = conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    int count = 0;

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupHostVirtualSwitchList(priv->primary,
                                          &hostVirtualSwitchList) < 0) {
        return -1;
    }

90
    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
M
Matthias Bolte 已提交
91 92 93 94 95 96 97 98 99 100 101 102
         hostVirtualSwitch = hostVirtualSwitch->_next) {
        ++count;
    }

    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitchList);

    return count;
}



static int
103
esxConnectListNetworks(virConnectPtr conn, char **const names, int maxnames)
M
Matthias Bolte 已提交
104 105 106 107 108 109
{
    bool success = false;
    esxPrivate *priv = conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    int count = 0;
110
    size_t i;
M
Matthias Bolte 已提交
111 112 113 114 115 116 117 118 119 120 121

    if (maxnames == 0) {
        return 0;
    }

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupHostVirtualSwitchList(priv->primary,
                                          &hostVirtualSwitchList) < 0) {
        return -1;
    }

122
    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
M
Matthias Bolte 已提交
123
         hostVirtualSwitch = hostVirtualSwitch->_next) {
124
        if (VIR_STRDUP(names[count], hostVirtualSwitch->name) < 0)
M
Matthias Bolte 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            goto cleanup;

        ++count;
    }

    success = true;

  cleanup:
    if (! success) {
        for (i = 0; i < count; ++i) {
            VIR_FREE(names[i]);
        }

        count = -1;
    }

    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitchList);

    return count;
}



static int
149
esxConnectNumOfDefinedNetworks(virConnectPtr conn ATTRIBUTE_UNUSED)
M
Matthias Bolte 已提交
150 151 152 153 154 155 156 157
{
    /* ESX networks are always active */
    return 0;
}



static int
158 159 160
esxConnectListDefinedNetworks(virConnectPtr conn ATTRIBUTE_UNUSED,
                              char **const names ATTRIBUTE_UNUSED,
                              int maxnames ATTRIBUTE_UNUSED)
M
Matthias Bolte 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
    /* ESX networks are always active */
    return 0;
}



static virNetworkPtr
esxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
    virNetworkPtr network = NULL;
    esxPrivate *priv = conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitchList = NULL;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    unsigned char md5[MD5_DIGEST_SIZE]; /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupHostVirtualSwitchList(priv->primary,
                                          &hostVirtualSwitchList) < 0) {
        return NULL;
    }

184
    for (hostVirtualSwitch = hostVirtualSwitchList; hostVirtualSwitch;
M
Matthias Bolte 已提交
185 186 187 188 189 190 191 192
         hostVirtualSwitch = hostVirtualSwitch->_next) {
        md5_buffer(hostVirtualSwitch->key, strlen(hostVirtualSwitch->key), md5);

        if (memcmp(uuid, md5, VIR_UUID_BUFLEN) == 0) {
            break;
        }
    }

193
    if (!hostVirtualSwitch) {
M
Matthias Bolte 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        virUUIDFormat(uuid, uuid_string);

        virReportError(VIR_ERR_NO_NETWORK,
                       _("Could not find HostVirtualSwitch with UUID '%s'"),
                       uuid_string);

        goto cleanup;
    }

    network = virGetNetwork(conn, hostVirtualSwitch->name, uuid);

  cleanup:
    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitchList);

    return network;
}



static virNetworkPtr
esxNetworkLookupByName(virConnectPtr conn, const char *name)
{
    virNetworkPtr network = NULL;
    esxPrivate *priv = conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    unsigned char md5[MD5_DIGEST_SIZE]; /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupHostVirtualSwitchByName(priv->primary, name,
                                            &hostVirtualSwitch,
                                            esxVI_Occurrence_RequiredItem) < 0) {
        return NULL;
    }

    /*
     * HostVirtualSwitch doesn't have a UUID, but we can use the key property
     * as source for a UUID. The key is unique per host and cannot change
     * during the lifetime of the HostVirtualSwitch.
     *
     * The MD5 sum of the key can be used as UUID, assuming MD5 is considered
     * to be collision-free enough for this use case.
     */
    md5_buffer(hostVirtualSwitch->key, strlen(hostVirtualSwitch->key), md5);

    network = virGetNetwork(conn, hostVirtualSwitch->name, md5);

    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitch);

    return network;
}



static int
esxBandwidthToShapingPolicy(virNetDevBandwidthPtr bandwidth,
                            esxVI_HostNetworkTrafficShapingPolicy **shapingPolicy)
{
    int result = -1;

253
    if (!shapingPolicy || *shapingPolicy) {
M
Matthias Bolte 已提交
254 255 256 257
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
        return -1;
    }

258
    if (!bandwidth->in || !bandwidth->out ||
M
Matthias Bolte 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        bandwidth->in->average != bandwidth->out->average ||
        bandwidth->in->peak != bandwidth->out->peak ||
        bandwidth->in->burst != bandwidth->out->burst) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Different inbound and outbound bandwidth is unsupported"));
        return -1;
    }

    if (bandwidth->in->average == 0 && bandwidth->in->peak == 0 &&
        bandwidth->in->burst == 0) {
        return 0;
    }

    if (esxVI_HostNetworkTrafficShapingPolicy_Alloc(shapingPolicy) < 0) {
        goto cleanup;
    }

    (*shapingPolicy)->enabled = esxVI_Boolean_True;

    if (bandwidth->in->average > 0) {
        if (esxVI_Long_Alloc(&(*shapingPolicy)->averageBandwidth) < 0) {
            goto cleanup;
        }

        /* Scale kilobytes per second to bits per second */
        (*shapingPolicy)->averageBandwidth->value = bandwidth->in->average * 8 * 1000;
    }

    if (bandwidth->in->peak > 0) {
        if (esxVI_Long_Alloc(&(*shapingPolicy)->peakBandwidth) < 0) {
            goto cleanup;
        }

        /* Scale kilobytes per second to bits per second */
        (*shapingPolicy)->peakBandwidth->value = bandwidth->in->peak * 8 * 1000;
    }

    if (bandwidth->in->burst > 0) {
        if (esxVI_Long_Alloc(&(*shapingPolicy)->burstSize) < 0) {
            goto cleanup;
        }

        /* Scale kilobytes to bytes */
        (*shapingPolicy)->burstSize->value = bandwidth->in->burst * 1024;
    }

    result = 0;

  cleanup:
    if (result < 0) {
        esxVI_HostNetworkTrafficShapingPolicy_Free(shapingPolicy);
    }

    return result;
}



static virNetworkPtr
esxNetworkDefineXML(virConnectPtr conn, const char *xml)
{
    virNetworkPtr network = NULL;
    esxPrivate *priv = conn->networkPrivateData;
    virNetworkDefPtr def = NULL;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    esxVI_HostPortGroup *hostPortGroupList = NULL;
    esxVI_HostPortGroup *hostPortGroup = NULL;
    esxVI_HostVirtualSwitchSpec *hostVirtualSwitchSpec = NULL;
    esxVI_HostVirtualSwitchBondBridge *hostVirtualSwitchBondBridge = NULL;
    esxVI_PhysicalNic *physicalNicList = NULL;
    esxVI_PhysicalNic *physicalNic = NULL;
    esxVI_HostPortGroupSpec *hostPortGroupSpec = NULL;
331
    size_t i;
M
Matthias Bolte 已提交
332 333 334 335 336 337 338 339 340 341

    unsigned char md5[MD5_DIGEST_SIZE]; /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

    /* Parse network XML */
    def = virNetworkDefParseString(xml);

342
    if (!def) {
M
Matthias Bolte 已提交
343 344 345 346 347 348 349 350 351 352
        return NULL;
    }

    /* Check if an existing HostVirtualSwitch should be edited */
    if (esxVI_LookupHostVirtualSwitchByName(priv->primary, def->name,
                                            &hostVirtualSwitch,
                                            esxVI_Occurrence_OptionalItem) < 0) {
        goto cleanup;
    }

353
    if (hostVirtualSwitch) {
M
Matthias Bolte 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        /* FIXME */
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("HostVirtualSwitch already exists, editing existing "
                         "ones is not supported yet"));
        goto cleanup;
    }

    /* UUID is derived from the HostVirtualSwitch's key and cannot be specified */
    if (def->uuid_specified) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Cannot use predefined UUID"));
        goto cleanup;
    }

    /* FIXME: Add support for NAT */
369 370
    if (def->forward.type != VIR_NETWORK_FORWARD_NONE &&
        def->forward.type != VIR_NETWORK_FORWARD_BRIDGE) {
M
Matthias Bolte 已提交
371 372
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported forward mode '%s'"),
373
                       virNetworkForwardTypeToString(def->forward.type));
M
Matthias Bolte 已提交
374 375 376 377 378 379 380 381 382 383
        goto cleanup;
    }

    /* Verify that specified HostPortGroups don't exist already */
    if (def->nPortGroups > 0) {
        if (esxVI_LookupHostPortGroupList(priv->primary, &hostPortGroupList) < 0) {
            goto cleanup;
        }

        for (i = 0; i < def->nPortGroups; ++i) {
384
            for (hostPortGroup = hostPortGroupList; hostPortGroup;
M
Matthias Bolte 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                 hostPortGroup = hostPortGroup->_next) {
                if (STREQ(def->portGroups[i].name, hostPortGroup->spec->name)) {
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("HostPortGroup with name '%s' exists already"),
                                   def->portGroups[i].name);
                    goto cleanup;
                }
            }
        }
    }

    /* Create HostVirtualSwitch */
    if (esxVI_HostVirtualSwitchSpec_Alloc(&hostVirtualSwitchSpec) < 0 ||
        esxVI_Int_Alloc(&hostVirtualSwitchSpec->numPorts) < 0) {
        goto cleanup;
    }

402
    if (def->forward.type != VIR_NETWORK_FORWARD_NONE && def->forward.nifs > 0) {
M
Matthias Bolte 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415
        if (esxVI_HostVirtualSwitchBondBridge_Alloc
              (&hostVirtualSwitchBondBridge) < 0) {
            goto cleanup;
        }

        hostVirtualSwitchSpec->bridge =
          (esxVI_HostVirtualSwitchBridge *)hostVirtualSwitchBondBridge;

        /* Lookup PhysicalNic list and match by name to get key */
        if (esxVI_LookupPhysicalNicList(priv->primary, &physicalNicList) < 0) {
            goto cleanup;
        }

416
        for (i = 0; i < def->forward.nifs; ++i) {
M
Matthias Bolte 已提交
417 418
            bool found = false;

419
            if (def->forward.ifs[i].type !=
420 421 422 423 424 425 426 427
                VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unsupported device type in network %s "
                                 "interface pool"),
                               def->name);
                goto cleanup;
            }

428
            for (physicalNic = physicalNicList; physicalNic;
M
Matthias Bolte 已提交
429
                 physicalNic = physicalNic->_next) {
430
                if (STREQ(def->forward.ifs[i].device.dev, physicalNic->device)) {
M
Matthias Bolte 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444
                    if (esxVI_String_AppendValueToList
                          (&hostVirtualSwitchBondBridge->nicDevice,
                           physicalNic->key) < 0) {
                        goto cleanup;
                    }

                    found = true;
                    break;
                }
            }

            if (! found) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Could not find PhysicalNic with name '%s'"),
445
                               def->forward.ifs[i].device.dev);
M
Matthias Bolte 已提交
446 447 448 449 450 451 452
                goto cleanup;
            }
        }
    }

    hostVirtualSwitchSpec->numPorts->value = 128;

453
    if (def->bandwidth) {
M
Matthias Bolte 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        if (esxVI_HostNetworkPolicy_Alloc(&hostVirtualSwitchSpec->policy) < 0) {
            goto cleanup;
        }

        if (esxBandwidthToShapingPolicy
              (def->bandwidth,
               &hostVirtualSwitchSpec->policy->shapingPolicy) < 0) {
            goto cleanup;
        }
    }

    if (esxVI_AddVirtualSwitch
          (priv->primary,
           priv->primary->hostSystem->configManager->networkSystem,
           def->name, hostVirtualSwitchSpec) < 0) {
        goto cleanup;
    }

    /* Create HostPortGroup(s) */
    for (i = 0; i < def->nPortGroups; ++i) {
        esxVI_HostPortGroupSpec_Free(&hostPortGroupSpec);

        if (esxVI_HostPortGroupSpec_Alloc(&hostPortGroupSpec) < 0 ||
            esxVI_HostNetworkPolicy_Alloc(&hostPortGroupSpec->policy) < 0 ||
            esxVI_Int_Alloc(&hostPortGroupSpec->vlanId) < 0 ||
479 480
            VIR_STRDUP(hostPortGroupSpec->name, def->portGroups[i].name) < 0 ||
            VIR_STRDUP(hostPortGroupSpec->vswitchName, def->name) < 0) {
M
Matthias Bolte 已提交
481 482 483 484 485
            goto cleanup;
        }

        hostPortGroupSpec->vlanId->value = 0;

486
        if (def->portGroups[i].bandwidth) {
M
Matthias Bolte 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
            if (esxBandwidthToShapingPolicy
                  (def->portGroups[i].bandwidth,
                   &hostPortGroupSpec->policy->shapingPolicy) < 0) {
                goto cleanup;
            }
        }

        if (esxVI_AddPortGroup
              (priv->primary,
               priv->primary->hostSystem->configManager->networkSystem,
               hostPortGroupSpec) < 0) {
            goto cleanup;
        }
    }

    /* Lookup created HostVirtualSwitch to get the UUID */
    if (esxVI_LookupHostVirtualSwitchByName(priv->primary, def->name,
                                            &hostVirtualSwitch,
                                            esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    md5_buffer(hostVirtualSwitch->key, strlen(hostVirtualSwitch->key), md5);

    network = virGetNetwork(conn, hostVirtualSwitch->name, md5);

  cleanup:
    virNetworkDefFree(def);
    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitch);
    esxVI_HostPortGroup_Free(&hostPortGroupList);
    esxVI_HostVirtualSwitchSpec_Free(&hostVirtualSwitchSpec);
    esxVI_PhysicalNic_Free(&physicalNicList);
    esxVI_HostPortGroupSpec_Free(&hostPortGroupSpec);

    return network;
}



static int
esxNetworkUndefine(virNetworkPtr network)
{
    int result = -1;
    esxPrivate *priv = network->conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    esxVI_HostPortGroup *hostPortGroupList = NULL;
    esxVI_String *hostPortGroupKey = NULL;
    esxVI_HostPortGroup *hostPortGroup = NULL;
    esxVI_HostPortGroupPort *hostPortGroupPort = NULL;

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

    /* Lookup HostVirtualSwitch and HostPortGroup list*/
    if (esxVI_LookupHostVirtualSwitchByName(priv->primary, network->name,
                                            &hostVirtualSwitch,
                                            esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_LookupHostPortGroupList(priv->primary, &hostPortGroupList) < 0) {
        goto cleanup;
    }

    /* Verify that the HostVirtualSwitch is connected to virtual machines only */
    for (hostPortGroupKey = hostVirtualSwitch->portgroup;
551
         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
M
Matthias Bolte 已提交
552 553
        bool found = false;

554
        for (hostPortGroup = hostPortGroupList; hostPortGroup;
M
Matthias Bolte 已提交
555 556 557
             hostPortGroup = hostPortGroup->_next) {
            if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                for (hostPortGroupPort = hostPortGroup->port;
558
                     hostPortGroupPort;
M
Matthias Bolte 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
                     hostPortGroupPort = hostPortGroupPort->_next) {
                    if (STRNEQ(hostPortGroupPort->type, "virtualMachine")) {
                        virReportError(VIR_ERR_OPERATION_INVALID,
                                       _("Cannot undefine HostVirtualSwitch that has a '%s' port"),
                                       hostPortGroupPort->type);
                        goto cleanup;
                    }
                }

                found = true;
                break;
            }
        }

        if (! found) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not find HostPortGroup for key '%s'"),
                           hostPortGroupKey->value);
            goto cleanup;
        }
    }

    /* Remove all HostPortGroups from the HostVirtualSwitch */
    for (hostPortGroupKey = hostVirtualSwitch->portgroup;
583
         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
M
Matthias Bolte 已提交
584 585
        bool found = false;

586
        for (hostPortGroup = hostPortGroupList; hostPortGroup;
M
Matthias Bolte 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
             hostPortGroup = hostPortGroup->_next) {
            if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                if (esxVI_RemovePortGroup
                      (priv->primary,
                       priv->primary->hostSystem->configManager->networkSystem,
                       hostPortGroup->spec->name) < 0) {
                    goto cleanup;
                }

                found = true;
                break;
            }
        }

        if (! found) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Could not find HostPortGroup for key '%s'"),
                           hostPortGroupKey->value);
            goto cleanup;
        }
    }

    /* Finally, remove HostVirtualSwitch itself */
    if (esxVI_RemoveVirtualSwitch
          (priv->primary,
           priv->primary->hostSystem->configManager->networkSystem,
           network->name) < 0) {
        goto cleanup;
    }

    result = 0;

  cleanup:
    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitch);
    esxVI_HostPortGroup_Free(&hostPortGroupList);

    return result;
}



static int
esxShapingPolicyToBandwidth(esxVI_HostNetworkTrafficShapingPolicy *shapingPolicy,
                            virNetDevBandwidthPtr *bandwidth)
{
632
    if (!bandwidth || *bandwidth) {
M
Matthias Bolte 已提交
633 634 635 636
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
        return -1;
    }

637
    if (!shapingPolicy || shapingPolicy->enabled != esxVI_Boolean_True) {
M
Matthias Bolte 已提交
638 639 640 641 642
        return 0;
    }

    if (VIR_ALLOC(*bandwidth) < 0 ||
        VIR_ALLOC((*bandwidth)->in) < 0 ||
643
        VIR_ALLOC((*bandwidth)->out) < 0)
M
Matthias Bolte 已提交
644 645
        return -1;

646
    if (shapingPolicy->averageBandwidth) {
M
Matthias Bolte 已提交
647 648 649 650 651
        /* Scale bits per second to kilobytes per second */
        (*bandwidth)->in->average = shapingPolicy->averageBandwidth->value / 8 / 1000;
        (*bandwidth)->out->average = shapingPolicy->averageBandwidth->value / 8 / 1000;
    }

652
    if (shapingPolicy->peakBandwidth) {
M
Matthias Bolte 已提交
653 654 655 656 657
        /* Scale bits per second to kilobytes per second */
        (*bandwidth)->in->peak = shapingPolicy->peakBandwidth->value / 8 / 1000;
        (*bandwidth)->out->peak = shapingPolicy->peakBandwidth->value / 8 / 1000;
    }

658
    if (shapingPolicy->burstSize) {
M
Matthias Bolte 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
        /* Scale bytes to kilobytes */
        (*bandwidth)->in->burst = shapingPolicy->burstSize->value / 1024;
        (*bandwidth)->out->burst = shapingPolicy->burstSize->value / 1024;
    }

    return 0;
}



static char *
esxNetworkGetXMLDesc(virNetworkPtr network_, unsigned int flags)
{
    char *xml = NULL;
    esxPrivate *priv = network_->conn->networkPrivateData;
    esxVI_HostVirtualSwitch *hostVirtualSwitch = NULL;
    int count = 0;
    esxVI_PhysicalNic *physicalNicList = NULL;
    esxVI_PhysicalNic *physicalNic = NULL;
    esxVI_String *physicalNicKey = NULL;
    esxVI_HostPortGroup *hostPortGroupList = NULL;
    esxVI_HostPortGroup *hostPortGroup = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *networkList = NULL;
    esxVI_ObjectContent *network = NULL;
    esxVI_String *networkNameList = NULL;
    esxVI_String *hostPortGroupKey = NULL;
    esxVI_String *networkName = NULL;
    virNetworkDefPtr def;

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return NULL;
    }

693
    if (VIR_ALLOC(def) < 0)
M
Matthias Bolte 已提交
694 695 696 697 698 699 700 701 702 703 704
        goto cleanup;

    /* Lookup HostVirtualSwitch */
    if (esxVI_LookupHostVirtualSwitchByName(priv->primary, network_->name,
                                            &hostVirtualSwitch,
                                            esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    md5_buffer(hostVirtualSwitch->key, strlen(hostVirtualSwitch->key), def->uuid);

705
    if (VIR_STRDUP(def->name, hostVirtualSwitch->name) < 0)
M
Matthias Bolte 已提交
706 707
        goto cleanup;

708
    def->forward.type = VIR_NETWORK_FORWARD_NONE;
M
Matthias Bolte 已提交
709 710 711 712 713

    /* Count PhysicalNics on HostVirtualSwitch */
    count = 0;

    for (physicalNicKey = hostVirtualSwitch->pnic;
714
         physicalNicKey; physicalNicKey = physicalNicKey->_next) {
M
Matthias Bolte 已提交
715 716 717 718
        ++count;
    }

    if (count > 0) {
719
        def->forward.type = VIR_NETWORK_FORWARD_BRIDGE;
M
Matthias Bolte 已提交
720

721
        if (VIR_ALLOC_N(def->forward.ifs, count) < 0)
M
Matthias Bolte 已提交
722 723 724 725 726 727 728 729
            goto cleanup;

        /* Find PhysicalNic by key */
        if (esxVI_LookupPhysicalNicList(priv->primary, &physicalNicList) < 0) {
            goto cleanup;
        }

        for (physicalNicKey = hostVirtualSwitch->pnic;
730
             physicalNicKey; physicalNicKey = physicalNicKey->_next) {
M
Matthias Bolte 已提交
731 732
            bool found = false;

733
            for (physicalNic = physicalNicList; physicalNic;
M
Matthias Bolte 已提交
734 735
                 physicalNic = physicalNic->_next) {
                if (STREQ(physicalNicKey->value, physicalNic->key)) {
736
                    def->forward.ifs[def->forward.nifs].type
737
                        = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV;
738 739
                    if (VIR_STRDUP(def->forward.ifs[def->forward.nifs].device.dev,
                                   physicalNic->device) < 0)
M
Matthias Bolte 已提交
740 741
                        goto cleanup;

742
                    ++def->forward.nifs;
M
Matthias Bolte 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

                    found = true;
                    break;
                }
            }

            if (! found) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Could not find PhysicalNic with key '%s'"),
                               physicalNicKey->value);
                goto cleanup;
            }
        }
    }

    /* Count HostPortGroups on HostVirtualSwitch */
    count = 0;

    for (hostPortGroupKey = hostVirtualSwitch->portgroup;
762
         hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
M
Matthias Bolte 已提交
763 764 765 766
        ++count;
    }

    if (count > 0) {
767
        if (VIR_ALLOC_N(def->portGroups, count) < 0)
M
Matthias Bolte 已提交
768 769 770 771 772 773 774 775 776
            goto cleanup;

        /* Lookup Network list and create name list */
        if (esxVI_String_AppendValueToList(&propertyNameList, "name") < 0 ||
            esxVI_LookupNetworkList(priv->primary, propertyNameList,
                                    &networkList) < 0) {
            goto cleanup;
        }

777
        for (network = networkList; network; network = network->_next) {
M
Matthias Bolte 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            char *tmp = NULL;

            if (esxVI_GetStringValue(network, "name", &tmp,
                                     esxVI_Occurrence_RequiredItem) < 0 ||
                esxVI_String_AppendValueToList(&networkNameList, tmp) < 0) {
                goto cleanup;
            }
        }

        /* Find HostPortGroup by key */
        if (esxVI_LookupHostPortGroupList(priv->primary, &hostPortGroupList) < 0) {
            goto cleanup;
        }

        for (hostPortGroupKey = hostVirtualSwitch->portgroup;
793
             hostPortGroupKey; hostPortGroupKey = hostPortGroupKey->_next) {
M
Matthias Bolte 已提交
794 795
            bool found = false;

796
            for (hostPortGroup = hostPortGroupList; hostPortGroup;
M
Matthias Bolte 已提交
797 798 799
                 hostPortGroup = hostPortGroup->_next) {
                if (STREQ(hostPortGroupKey->value, hostPortGroup->key)) {
                    /* Find Network for HostPortGroup, there might be none */
800
                    for (networkName = networkNameList; networkName;
M
Matthias Bolte 已提交
801 802
                         networkName = networkName->_next) {
                        if (STREQ(networkName->value, hostPortGroup->spec->name)) {
803 804
                            if (VIR_STRDUP(def->portGroups[def->nPortGroups].name,
                                           networkName->value) < 0)
M
Matthias Bolte 已提交
805 806
                                goto cleanup;

807
                            if (hostPortGroup->spec->policy) {
M
Matthias Bolte 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
                                if (esxShapingPolicyToBandwidth
                                      (hostPortGroup->spec->policy->shapingPolicy,
                                       &def->portGroups[def->nPortGroups].bandwidth) < 0) {
                                    ++def->nPortGroups;
                                    goto cleanup;
                                }
                            }

                            ++def->nPortGroups;
                            break;
                        }
                    }

                    found = true;
                    break;
                }
            }

            if (! found) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Could not find HostPortGroup with key '%s'"),
                               hostPortGroupKey->value);
                goto cleanup;
            }
        }
    }

835
    if (hostVirtualSwitch->spec->policy) {
M
Matthias Bolte 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
        if (esxShapingPolicyToBandwidth
              (hostVirtualSwitch->spec->policy->shapingPolicy,
               &def->bandwidth) < 0) {
            goto cleanup;
        }
    }

    xml = virNetworkDefFormat(def, flags);

  cleanup:
    esxVI_HostVirtualSwitch_Free(&hostVirtualSwitch);
    esxVI_PhysicalNic_Free(&physicalNicList);
    esxVI_HostPortGroup_Free(&hostPortGroupList);
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&networkList);
    esxVI_String_Free(&networkNameList);
    virNetworkDefFree(def);

    return xml;
}



static int
esxNetworkGetAutostart(virNetworkPtr network ATTRIBUTE_UNUSED,
                       int *autostart)
{
    /* ESX networks are always active */
    *autostart = 1;

    return 0;
}



static int
esxNetworkSetAutostart(virNetworkPtr network ATTRIBUTE_UNUSED,
                       int autostart)
{
    /* Just accept autostart activation, but fail on autostart deactivation */
    autostart = (autostart != 0);

    if (! autostart) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot deactivate network autostart"));
        return -1;
    }

    return 0;
}



static int
esxNetworkIsActive(virNetworkPtr network ATTRIBUTE_UNUSED)
{
    /* ESX networks are always active */
    return 1;
}



static int
esxNetworkIsPersistent(virNetworkPtr network ATTRIBUTE_UNUSED)
{
    /* ESX has no concept of transient networks, so all of them are persistent */
    return 1;
}



907
static virNetworkDriver esxNetworkDriver = {
908
    .name = "ESX",
909 910
    .networkOpen = esxNetworkOpen, /* 0.7.6 */
    .networkClose = esxNetworkClose, /* 0.7.6 */
911 912 913 914
    .connectNumOfNetworks = esxConnectNumOfNetworks, /* 0.10.0 */
    .connectListNetworks = esxConnectListNetworks, /* 0.10.0 */
    .connectNumOfDefinedNetworks = esxConnectNumOfDefinedNetworks, /* 0.10.0 */
    .connectListDefinedNetworks = esxConnectListDefinedNetworks, /* 0.10.0 */
M
Matthias Bolte 已提交
915 916 917 918 919 920 921 922 923
    .networkLookupByUUID = esxNetworkLookupByUUID, /* 0.10.0 */
    .networkLookupByName = esxNetworkLookupByName, /* 0.10.0 */
    .networkDefineXML = esxNetworkDefineXML, /* 0.10.0 */
    .networkUndefine = esxNetworkUndefine, /* 0.10.0 */
    .networkGetXMLDesc = esxNetworkGetXMLDesc, /* 0.10.0 */
    .networkGetAutostart = esxNetworkGetAutostart, /* 0.10.0 */
    .networkSetAutostart = esxNetworkSetAutostart, /* 0.10.0 */
    .networkIsActive = esxNetworkIsActive, /* 0.10.0 */
    .networkIsPersistent = esxNetworkIsPersistent, /* 0.10.0 */
924 925 926 927 928 929 930 931 932
};



int
esxNetworkRegister(void)
{
    return virRegisterNetworkDriver(&esxNetworkDriver);
}