libvirt/src/esx/esx_interface_driver.c
Daniel P. Berrange 55ea7be7d9 Removing probing of secondary drivers
For stateless, client side drivers, it is never correct to
probe for secondary drivers. It is only ever appropriate to
use the secondary driver that is associated with the
hypervisor in question. As a result the ESX & HyperV drivers
have both been forced to do hacks where they register no-op
drivers for the ones they don't implement.

For stateful, server side drivers, we always just want to
use the same built-in shared driver. The exception is
virtualbox which is really a stateless driver and so wants
to use its own server side secondary drivers. To deal with
this virtualbox has to be built as 3 separate loadable
modules to allow registration to work in the right order.

This can all be simplified by introducing a new struct
recording the precise set of secondary drivers each
hypervisor driver wants

struct _virConnectDriver {
    virHypervisorDriverPtr hypervisorDriver;
    virInterfaceDriverPtr interfaceDriver;
    virNetworkDriverPtr networkDriver;
    virNodeDeviceDriverPtr nodeDeviceDriver;
    virNWFilterDriverPtr nwfilterDriver;
    virSecretDriverPtr secretDriver;
    virStorageDriverPtr storageDriver;
};

Instead of registering the hypervisor driver, we now
just register a virConnectDriver instead. This allows
us to remove all probing of secondary drivers. Once we
have chosen the primary driver, we immediately know the
correct secondary drivers to use.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2015-01-27 12:02:04 +00:00

271 lines
7.3 KiB
C

/*
* esx_interface_driver.c: interface driver functions for managing VMware ESX
* host interfaces
*
* Copyright (C) 2010-2011 Red Hat, Inc.
* Copyright (C) 2010-2012 Matthias Bolte <matthias.bolte@googlemail.com>
*
* 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, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include "internal.h"
#include "viralloc.h"
#include "viruuid.h"
#include "interface_conf.h"
#include "virsocketaddr.h"
#include "esx_private.h"
#include "esx_interface_driver.h"
#include "esx_vi.h"
#include "esx_vi_methods.h"
#include "esx_util.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_ESX
static int
esxConnectNumOfInterfaces(virConnectPtr conn)
{
esxPrivate *priv = conn->privateData;
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;
}
for (physicalNic = physicalNicList; physicalNic;
physicalNic = physicalNic->_next) {
++count;
}
esxVI_PhysicalNic_Free(&physicalNicList);
return count;
}
static int
esxConnectListInterfaces(virConnectPtr conn, char **const names, int maxnames)
{
bool success = false;
esxPrivate *priv = conn->privateData;
esxVI_PhysicalNic *physicalNicList = NULL;
esxVI_PhysicalNic *physicalNic = NULL;
int count = 0;
size_t i;
if (maxnames == 0)
return 0;
if (esxVI_EnsureSession(priv->primary) < 0 ||
esxVI_LookupPhysicalNicList(priv->primary, &physicalNicList) < 0) {
return -1;
}
for (physicalNic = physicalNicList; physicalNic;
physicalNic = physicalNic->_next) {
if (VIR_STRDUP(names[count], physicalNic->device) < 0)
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
esxConnectNumOfDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED)
{
/* ESX interfaces are always active */
return 0;
}
static int
esxConnectListDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED,
char **const names ATTRIBUTE_UNUSED,
int maxnames ATTRIBUTE_UNUSED)
{
/* ESX interfaces are always active */
return 0;
}
static virInterfacePtr
esxInterfaceLookupByName(virConnectPtr conn, const char *name)
{
virInterfacePtr iface = NULL;
esxPrivate *priv = conn->privateData;
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->privateData;
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->privateData;
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 */
if (physicalNic->spec->ip) {
protocol.family = (char *)"ipv4";
if (physicalNic->spec->ip->dhcp == esxVI_Boolean_True)
protocol.dhcp = 1;
if (physicalNic->spec->ip->ipAddress &&
physicalNic->spec->ip->subnetMask &&
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;
}
virInterfaceDriver esxInterfaceDriver = {
.connectNumOfInterfaces = esxConnectNumOfInterfaces, /* 0.10.0 */
.connectListInterfaces = esxConnectListInterfaces, /* 0.10.0 */
.connectNumOfDefinedInterfaces = esxConnectNumOfDefinedInterfaces, /* 0.10.0 */
.connectListDefinedInterfaces = esxConnectListDefinedInterfaces, /* 0.10.0 */
.interfaceLookupByName = esxInterfaceLookupByName, /* 0.10.0 */
.interfaceLookupByMACString = esxInterfaceLookupByMACString, /* 0.10.0 */
.interfaceGetXMLDesc = esxInterfaceGetXMLDesc, /* 0.10.0 */
.interfaceIsActive = esxInterfaceIsActive, /* 0.10.0 */
};