esx_interface_driver.c 8.1 KB
Newer Older
1 2

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

#include <config.h>

#include "internal.h"
28
#include "viralloc.h"
29
#include "virlog.h"
30
#include "viruuid.h"
M
Matthias Bolte 已提交
31 32
#include "interface_conf.h"
#include "virsocketaddr.h"
33 34 35 36 37
#include "esx_private.h"
#include "esx_interface_driver.h"
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
38
#include "virstring.h"
39 40 41 42 43 44 45 46

#define VIR_FROM_THIS VIR_FROM_ESX



static virDrvOpenStatus
esxInterfaceOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
47
                 unsigned int flags)
48
{
E
Eric Blake 已提交
49 50
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

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

    conn->interfacePrivateData = conn->privateData;

    return VIR_DRV_OPEN_SUCCESS;
}



static int
esxInterfaceClose(virConnectPtr conn)
{
    conn->interfacePrivateData = NULL;

    return 0;
}



M
Matthias Bolte 已提交
72
static int
73
esxConnectNumOfInterfaces(virConnectPtr conn)
M
Matthias Bolte 已提交
74 75 76 77 78 79 80 81 82 83 84
{
    esxPrivate *priv = conn->interfacePrivateData;
    esxVI_PhysicalNic *physicalNicList = NULL;
    esxVI_PhysicalNic *physicalNic = NULL;
    int count = 0;

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

85
    for (physicalNic = physicalNicList; physicalNic;
M
Matthias Bolte 已提交
86 87 88 89 90 91 92 93 94 95 96 97
         physicalNic = physicalNic->_next) {
        ++count;
    }

    esxVI_PhysicalNic_Free(&physicalNicList);

    return count;
}



static int
98
esxConnectListInterfaces(virConnectPtr conn, char **const names, int maxnames)
M
Matthias Bolte 已提交
99 100 101 102 103 104
{
    bool success = false;
    esxPrivate *priv = conn->interfacePrivateData;
    esxVI_PhysicalNic *physicalNicList = NULL;
    esxVI_PhysicalNic *physicalNic = NULL;
    int count = 0;
105
    size_t i;
M
Matthias Bolte 已提交
106 107 108 109 110 111 112 113 114 115

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

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

116
    for (physicalNic = physicalNicList; physicalNic;
M
Matthias Bolte 已提交
117
         physicalNic = physicalNic->_next) {
118
        if (VIR_STRDUP(names[count], physicalNic->device) < 0)
M
Matthias Bolte 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            goto cleanup;

        ++count;
    }

    success = true;

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

        count = -1;
    }

    esxVI_PhysicalNic_Free(&physicalNicList);

    return count;
}



static int
143
esxConnectNumOfDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED)
M
Matthias Bolte 已提交
144 145 146 147 148 149 150 151
{
    /* ESX interfaces are always active */
    return 0;
}



static int
152 153 154
esxConnectListDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED,
                                char **const names ATTRIBUTE_UNUSED,
                                int maxnames ATTRIBUTE_UNUSED)
M
Matthias Bolte 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
{
    /* ESX interfaces are always active */
    return 0;
}



static virInterfacePtr
esxInterfaceLookupByName(virConnectPtr conn, const char *name)
{
    virInterfacePtr iface = NULL;
    esxPrivate *priv = conn->interfacePrivateData;
    esxVI_PhysicalNic *physicalNic = NULL;

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

    iface = virGetInterface(conn, physicalNic->device, physicalNic->mac);

    esxVI_PhysicalNic_Free(&physicalNic);

    return iface;
}



static virInterfacePtr
esxInterfaceLookupByMACString(virConnectPtr conn, const char *mac)
{
    virInterfacePtr iface = NULL;
    esxPrivate *priv = conn->interfacePrivateData;
    esxVI_PhysicalNic *physicalNic = NULL;

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupPhysicalNicByMACAddress(priv->primary, mac, &physicalNic,
                                            esxVI_Occurrence_RequiredItem) < 0) {
        return NULL;
    }

    iface = virGetInterface(conn, physicalNic->device, physicalNic->mac);

    esxVI_PhysicalNic_Free(&physicalNic);

    return iface;
}



static char *
esxInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags)
{
    char *xml = NULL;
    esxPrivate *priv = iface->conn->interfacePrivateData;
    esxVI_PhysicalNic *physicalNic = NULL;
    virInterfaceDef def;
    bool hasAddress = false;
    virInterfaceProtocolDefPtr protocols;
    virInterfaceProtocolDef protocol;
    virSocketAddr socketAddress;
    virInterfaceIpDefPtr ips;
    virInterfaceIpDef ip;

    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);

    memset(&def, 0, sizeof(def));
    memset(&protocol, 0, sizeof(protocol));
    memset(&socketAddress, 0, sizeof(socketAddress));
    memset(&ip, 0, sizeof(ip));

    if (esxVI_EnsureSession(priv->primary) < 0 ||
        esxVI_LookupPhysicalNicByMACAddress(priv->primary, iface->mac,
                                            &physicalNic,
                                            esxVI_Occurrence_RequiredItem) < 0) {
        return NULL;
    }

    def.type = VIR_INTERFACE_TYPE_ETHERNET;
    def.name = physicalNic->device;
    def.mac = physicalNic->mac;
    def.startmode = VIR_INTERFACE_START_ONBOOT;

    /* FIXME: Add support for IPv6, requires to use vSphere API 4.0 */
240
    if (physicalNic->spec->ip) {
M
Matthias Bolte 已提交
241 242 243 244 245 246
        protocol.family = (char *)"ipv4";

        if (physicalNic->spec->ip->dhcp == esxVI_Boolean_True) {
            protocol.dhcp = 1;
        }

247 248
        if (physicalNic->spec->ip->ipAddress &&
            physicalNic->spec->ip->subnetMask &&
M
Matthias Bolte 已提交
249 250 251 252 253 254 255 256 257 258 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
            strlen(physicalNic->spec->ip->ipAddress) > 0 &&
            strlen(physicalNic->spec->ip->subnetMask) > 0) {
            hasAddress = true;
        }

        if (protocol.dhcp || hasAddress) {
            protocols = &protocol;
            def.nprotos = 1;
            def.protos = &protocols;
        }

        if (hasAddress &&
            !(protocol.dhcp && (flags & VIR_INTERFACE_XML_INACTIVE))) {
            ips = &ip;
            protocol.nips = 1;
            protocol.ips = &ips;

            if (virSocketAddrParseIPv4(&socketAddress,
                                       physicalNic->spec->ip->subnetMask) < 0) {
                goto cleanup;
            }

            ip.address = physicalNic->spec->ip->ipAddress;
            ip.prefix = virSocketAddrGetNumNetmaskBits(&socketAddress);
        }
    }

    xml = virInterfaceDefFormat(&def);

  cleanup:
    esxVI_PhysicalNic_Free(&physicalNic);

    return xml;
}



static int
esxInterfaceIsActive(virInterfacePtr iface ATTRIBUTE_UNUSED)
{
    /* ESX interfaces are always active */
    return 1;
}



295
static virInterfaceDriver esxInterfaceDriver = {
296
    .name = "ESX",
297 298
    .interfaceOpen = esxInterfaceOpen, /* 0.7.6 */
    .interfaceClose = esxInterfaceClose, /* 0.7.6 */
299 300 301 302
    .connectNumOfInterfaces = esxConnectNumOfInterfaces, /* 0.10.0 */
    .connectListInterfaces = esxConnectListInterfaces, /* 0.10.0 */
    .connectNumOfDefinedInterfaces = esxConnectNumOfDefinedInterfaces, /* 0.10.0 */
    .connectListDefinedInterfaces = esxConnectListDefinedInterfaces, /* 0.10.0 */
M
Matthias Bolte 已提交
303 304 305 306
    .interfaceLookupByName = esxInterfaceLookupByName, /* 0.10.0 */
    .interfaceLookupByMACString = esxInterfaceLookupByMACString, /* 0.10.0 */
    .interfaceGetXMLDesc = esxInterfaceGetXMLDesc, /* 0.10.0 */
    .interfaceIsActive = esxInterfaceIsActive, /* 0.10.0 */
307 308 309 310 311 312 313 314 315
};



int
esxInterfaceRegister(void)
{
    return virRegisterInterfaceDriver(&esxInterfaceDriver);
}