~ubuntu-branches/ubuntu/raring/libvirt/raring

« back to all changes in this revision

Viewing changes to src/esx/esx_interface_driver.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-11-19 10:41:02 UTC
  • mfrom: (1.2.15) (223.1.2 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121119104102-l6ewdppikysbzztu
Tags: 1.0.0-0ubuntu2
debian/patches/add-armhf-sysinfo-infomration.patch: Disable
to fix FTBFS on arm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 *                         host interfaces
5
5
 *
6
6
 * Copyright (C) 2010-2011 Red Hat, Inc.
7
 
 * Copyright (C) 2010 Matthias Bolte <matthias.bolte@googlemail.com>
 
7
 * Copyright (C) 2010-2012 Matthias Bolte <matthias.bolte@googlemail.com>
8
8
 *
9
9
 * This library is free software; you can redistribute it and/or
10
10
 * modify it under the terms of the GNU Lesser General Public
17
17
 * Lesser General Public License for more details.
18
18
 *
19
19
 * You should have received a copy of the GNU Lesser General Public
20
 
 * License along with this library; if not, write to the Free Software
21
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
20
 * License along with this library.  If not, see
 
21
 * <http://www.gnu.org/licenses/>.
22
22
 *
23
23
 */
24
24
 
29
29
#include "memory.h"
30
30
#include "logging.h"
31
31
#include "uuid.h"
 
32
#include "interface_conf.h"
 
33
#include "virsocketaddr.h"
32
34
#include "esx_private.h"
33
35
#include "esx_interface_driver.h"
34
36
#include "esx_vi.h"
67
69
 
68
70
 
69
71
 
 
72
static int
 
73
esxNumberOfInterfaces(virConnectPtr conn)
 
74
{
 
75
    esxPrivate *priv = conn->interfacePrivateData;
 
76
    esxVI_PhysicalNic *physicalNicList = NULL;
 
77
    esxVI_PhysicalNic *physicalNic = NULL;
 
78
    int count = 0;
 
79
 
 
80
    if (esxVI_EnsureSession(priv->primary) < 0 ||
 
81
        esxVI_LookupPhysicalNicList(priv->primary, &physicalNicList) < 0) {
 
82
        return -1;
 
83
    }
 
84
 
 
85
    for (physicalNic = physicalNicList; physicalNic != NULL;
 
86
         physicalNic = physicalNic->_next) {
 
87
        ++count;
 
88
    }
 
89
 
 
90
    esxVI_PhysicalNic_Free(&physicalNicList);
 
91
 
 
92
    return count;
 
93
}
 
94
 
 
95
 
 
96
 
 
97
static int
 
98
esxListInterfaces(virConnectPtr conn, char **const names, int maxnames)
 
99
{
 
100
    bool success = false;
 
101
    esxPrivate *priv = conn->interfacePrivateData;
 
102
    esxVI_PhysicalNic *physicalNicList = NULL;
 
103
    esxVI_PhysicalNic *physicalNic = NULL;
 
104
    int count = 0;
 
105
    int i;
 
106
 
 
107
    if (maxnames == 0) {
 
108
        return 0;
 
109
    }
 
110
 
 
111
    if (esxVI_EnsureSession(priv->primary) < 0 ||
 
112
        esxVI_LookupPhysicalNicList(priv->primary, &physicalNicList) < 0) {
 
113
        return -1;
 
114
    }
 
115
 
 
116
    for (physicalNic = physicalNicList; physicalNic != NULL;
 
117
         physicalNic = physicalNic->_next) {
 
118
        names[count] = strdup(physicalNic->device);
 
119
 
 
120
        if (names[count] == NULL) {
 
121
            virReportOOMError();
 
122
            goto cleanup;
 
123
        }
 
124
 
 
125
        ++count;
 
126
    }
 
127
 
 
128
    success = true;
 
129
 
 
130
  cleanup:
 
131
    if (! success) {
 
132
        for (i = 0; i < count; ++i) {
 
133
            VIR_FREE(names[i]);
 
134
        }
 
135
 
 
136
        count = -1;
 
137
    }
 
138
 
 
139
    esxVI_PhysicalNic_Free(&physicalNicList);
 
140
 
 
141
    return count;
 
142
}
 
143
 
 
144
 
 
145
 
 
146
static int
 
147
esxNumberOfDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED)
 
148
{
 
149
    /* ESX interfaces are always active */
 
150
    return 0;
 
151
}
 
152
 
 
153
 
 
154
 
 
155
static int
 
156
esxListDefinedInterfaces(virConnectPtr conn ATTRIBUTE_UNUSED,
 
157
                         char **const names ATTRIBUTE_UNUSED,
 
158
                         int maxnames ATTRIBUTE_UNUSED)
 
159
{
 
160
    /* ESX interfaces are always active */
 
161
    return 0;
 
162
}
 
163
 
 
164
 
 
165
 
 
166
static virInterfacePtr
 
167
esxInterfaceLookupByName(virConnectPtr conn, const char *name)
 
168
{
 
169
    virInterfacePtr iface = NULL;
 
170
    esxPrivate *priv = conn->interfacePrivateData;
 
171
    esxVI_PhysicalNic *physicalNic = NULL;
 
172
 
 
173
    if (esxVI_EnsureSession(priv->primary) < 0 ||
 
174
        esxVI_LookupPhysicalNicByMACAddress(priv->primary, name, &physicalNic,
 
175
                                            esxVI_Occurrence_RequiredItem) < 0) {
 
176
        return NULL;
 
177
    }
 
178
 
 
179
    iface = virGetInterface(conn, physicalNic->device, physicalNic->mac);
 
180
 
 
181
    esxVI_PhysicalNic_Free(&physicalNic);
 
182
 
 
183
    return iface;
 
184
}
 
185
 
 
186
 
 
187
 
 
188
static virInterfacePtr
 
189
esxInterfaceLookupByMACString(virConnectPtr conn, const char *mac)
 
190
{
 
191
    virInterfacePtr iface = NULL;
 
192
    esxPrivate *priv = conn->interfacePrivateData;
 
193
    esxVI_PhysicalNic *physicalNic = NULL;
 
194
 
 
195
    if (esxVI_EnsureSession(priv->primary) < 0 ||
 
196
        esxVI_LookupPhysicalNicByMACAddress(priv->primary, mac, &physicalNic,
 
197
                                            esxVI_Occurrence_RequiredItem) < 0) {
 
198
        return NULL;
 
199
    }
 
200
 
 
201
    iface = virGetInterface(conn, physicalNic->device, physicalNic->mac);
 
202
 
 
203
    esxVI_PhysicalNic_Free(&physicalNic);
 
204
 
 
205
    return iface;
 
206
}
 
207
 
 
208
 
 
209
 
 
210
static char *
 
211
esxInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags)
 
212
{
 
213
    char *xml = NULL;
 
214
    esxPrivate *priv = iface->conn->interfacePrivateData;
 
215
    esxVI_PhysicalNic *physicalNic = NULL;
 
216
    virInterfaceDef def;
 
217
    bool hasAddress = false;
 
218
    virInterfaceProtocolDefPtr protocols;
 
219
    virInterfaceProtocolDef protocol;
 
220
    virSocketAddr socketAddress;
 
221
    virInterfaceIpDefPtr ips;
 
222
    virInterfaceIpDef ip;
 
223
 
 
224
    virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);
 
225
 
 
226
    memset(&def, 0, sizeof(def));
 
227
    memset(&protocol, 0, sizeof(protocol));
 
228
    memset(&socketAddress, 0, sizeof(socketAddress));
 
229
    memset(&ip, 0, sizeof(ip));
 
230
 
 
231
    if (esxVI_EnsureSession(priv->primary) < 0 ||
 
232
        esxVI_LookupPhysicalNicByMACAddress(priv->primary, iface->mac,
 
233
                                            &physicalNic,
 
234
                                            esxVI_Occurrence_RequiredItem) < 0) {
 
235
        return NULL;
 
236
    }
 
237
 
 
238
    def.type = VIR_INTERFACE_TYPE_ETHERNET;
 
239
    def.name = physicalNic->device;
 
240
    def.mac = physicalNic->mac;
 
241
    def.startmode = VIR_INTERFACE_START_ONBOOT;
 
242
 
 
243
    /* FIXME: Add support for IPv6, requires to use vSphere API 4.0 */
 
244
    if (physicalNic->spec->ip != NULL) {
 
245
        protocol.family = (char *)"ipv4";
 
246
 
 
247
        if (physicalNic->spec->ip->dhcp == esxVI_Boolean_True) {
 
248
            protocol.dhcp = 1;
 
249
        }
 
250
 
 
251
        if (physicalNic->spec->ip->ipAddress != NULL &&
 
252
            physicalNic->spec->ip->subnetMask != NULL &&
 
253
            strlen(physicalNic->spec->ip->ipAddress) > 0 &&
 
254
            strlen(physicalNic->spec->ip->subnetMask) > 0) {
 
255
            hasAddress = true;
 
256
        }
 
257
 
 
258
        if (protocol.dhcp || hasAddress) {
 
259
            protocols = &protocol;
 
260
            def.nprotos = 1;
 
261
            def.protos = &protocols;
 
262
        }
 
263
 
 
264
        if (hasAddress &&
 
265
            !(protocol.dhcp && (flags & VIR_INTERFACE_XML_INACTIVE))) {
 
266
            ips = &ip;
 
267
            protocol.nips = 1;
 
268
            protocol.ips = &ips;
 
269
 
 
270
            if (virSocketAddrParseIPv4(&socketAddress,
 
271
                                       physicalNic->spec->ip->subnetMask) < 0) {
 
272
                goto cleanup;
 
273
            }
 
274
 
 
275
            ip.address = physicalNic->spec->ip->ipAddress;
 
276
            ip.prefix = virSocketAddrGetNumNetmaskBits(&socketAddress);
 
277
        }
 
278
    }
 
279
 
 
280
    xml = virInterfaceDefFormat(&def);
 
281
 
 
282
  cleanup:
 
283
    esxVI_PhysicalNic_Free(&physicalNic);
 
284
 
 
285
    return xml;
 
286
}
 
287
 
 
288
 
 
289
 
 
290
static int
 
291
esxInterfaceIsActive(virInterfacePtr iface ATTRIBUTE_UNUSED)
 
292
{
 
293
    /* ESX interfaces are always active */
 
294
    return 1;
 
295
}
 
296
 
 
297
 
 
298
 
70
299
static virInterfaceDriver esxInterfaceDriver = {
71
300
    .name = "ESX",
72
301
    .open = esxInterfaceOpen, /* 0.7.6 */
73
302
    .close = esxInterfaceClose, /* 0.7.6 */
 
303
    .numOfInterfaces = esxNumberOfInterfaces, /* 0.10.0 */
 
304
    .listInterfaces = esxListInterfaces, /* 0.10.0 */
 
305
    .numOfDefinedInterfaces = esxNumberOfDefinedInterfaces, /* 0.10.0 */
 
306
    .listDefinedInterfaces = esxListDefinedInterfaces, /* 0.10.0 */
 
307
    .interfaceLookupByName = esxInterfaceLookupByName, /* 0.10.0 */
 
308
    .interfaceLookupByMACString = esxInterfaceLookupByMACString, /* 0.10.0 */
 
309
    .interfaceGetXMLDesc = esxInterfaceGetXMLDesc, /* 0.10.0 */
 
310
    .interfaceIsActive = esxInterfaceIsActive, /* 0.10.0 */
74
311
};
75
312
 
76
313