virinterfaceobj.c 7.7 KB
Newer Older
J
John Ferlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * virinterfaceobj.c: interface object handling
 *                    (derived from interface_conf.c)
 *
 * 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 "datatypes.h"
#include "interface_conf.h"

#include "viralloc.h"
#include "virerror.h"
#include "virinterfaceobj.h"
#include "virlog.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_INTERFACE

VIR_LOG_INIT("conf.virinterfaceobj");

35
struct _virInterfaceObj {
36
    virObjectLockable parent;
37 38 39 40

    bool active;           /* true if interface is active (up) */
    virInterfaceDefPtr def; /* The interface definition */
};
J
John Ferlan 已提交
41

42 43 44 45
struct _virInterfaceObjList {
    size_t count;
    virInterfaceObjPtr *objs;
};
J
John Ferlan 已提交
46 47 48

/* virInterfaceObj manipulation */

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static virClassPtr virInterfaceObjClass;
static void virInterfaceObjDispose(void *obj);

static int
virInterfaceObjOnceInit(void)
{
    if (!(virInterfaceObjClass = virClassNew(virClassForObjectLockable(),
                                             "virInterfaceObj",
                                             sizeof(virInterfaceObj),
                                             virInterfaceObjDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virInterfaceObj)


static void
virInterfaceObjDispose(void *opaque)
{
    virInterfaceObjPtr obj = opaque;

    virInterfaceDefFree(obj->def);
}


76
static virInterfaceObjPtr
77
virInterfaceObjNew(virInterfaceDefPtr def)
78 79 80
{
    virInterfaceObjPtr obj;

81
    if (virInterfaceObjInitialize() < 0)
82 83
        return NULL;

84
    if (!(obj = virObjectLockableNew(virInterfaceObjClass)))
85 86
        return NULL;

87
    virObjectLock(obj);
88
    obj->def = def;
89 90 91 92 93

    return obj;
}


94
void
95
virInterfaceObjEndAPI(virInterfaceObjPtr *obj)
J
John Ferlan 已提交
96
{
97
    if (!*obj)
J
John Ferlan 已提交
98 99
        return;

100 101 102
    virObjectUnlock(*obj);
    virObjectUnref(*obj);
    *obj = NULL;
J
John Ferlan 已提交
103 104 105
}


106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
virInterfaceDefPtr
virInterfaceObjGetDef(virInterfaceObjPtr obj)
{
    return obj->def;
}


bool
virInterfaceObjIsActive(virInterfaceObjPtr obj)
{
    return obj->active;
}


void
virInterfaceObjSetActive(virInterfaceObjPtr obj,
                         bool active)
{
    obj->active = active;
}


128
/* virInterfaceObjList manipulation */
129 130 131 132 133 134 135 136 137 138 139
virInterfaceObjListPtr
virInterfaceObjListNew(void)
{
    virInterfaceObjListPtr interfaces;

    if (VIR_ALLOC(interfaces) < 0)
        return NULL;
    return interfaces;
}


140
int
141 142
virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces,
                                   const char *mac,
143
                                   char **const matches,
144
                                   int maxmatches)
J
John Ferlan 已提交
145 146
{
    size_t i;
147
    int matchct = 0;
J
John Ferlan 已提交
148 149

    for (i = 0; i < interfaces->count; i++) {
150 151
        virInterfaceObjPtr obj = interfaces->objs[i];
        virInterfaceDefPtr def;
J
John Ferlan 已提交
152

153
        virObjectLock(obj);
154 155
        def = obj->def;
        if (STRCASEEQ(def->mac, mac)) {
156 157
            if (matchct < maxmatches) {
                if (VIR_STRDUP(matches[matchct], def->name) < 0) {
158
                    virObjectUnlock(obj);
159 160 161
                    goto error;
                }
                matchct++;
J
John Ferlan 已提交
162 163
            }
        }
164
        virObjectUnlock(obj);
J
John Ferlan 已提交
165 166
    }
    return matchct;
167 168 169 170 171 172

 error:
    while (--matchct >= 0)
        VIR_FREE(matches[matchct]);

    return -1;
J
John Ferlan 已提交
173 174
}

175 176

virInterfaceObjPtr
177 178
virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces,
                              const char *name)
J
John Ferlan 已提交
179 180 181 182
{
    size_t i;

    for (i = 0; i < interfaces->count; i++) {
183 184 185
        virInterfaceObjPtr obj = interfaces->objs[i];
        virInterfaceDefPtr def;

186
        virObjectLock(obj);
187 188
        def = obj->def;
        if (STREQ(def->name, name))
189 190
            return virObjectRef(obj);
        virObjectUnlock(obj);
J
John Ferlan 已提交
191 192 193 194 195
    }

    return NULL;
}

196 197 198

void
virInterfaceObjListFree(virInterfaceObjListPtr interfaces)
J
John Ferlan 已提交
199 200 201 202
{
    size_t i;

    for (i = 0; i < interfaces->count; i++)
203
        virObjectUnref(interfaces->objs[i]);
J
John Ferlan 已提交
204
    VIR_FREE(interfaces->objs);
205
    VIR_FREE(interfaces);
J
John Ferlan 已提交
206 207
}

208

209 210
virInterfaceObjListPtr
virInterfaceObjListClone(virInterfaceObjListPtr interfaces)
J
John Ferlan 已提交
211 212 213
{
    size_t i;
    unsigned int cnt;
214
    virInterfaceObjListPtr dest;
J
John Ferlan 已提交
215

216 217
    if (!interfaces)
        return NULL;
J
John Ferlan 已提交
218

219 220 221 222
    if (!(dest = virInterfaceObjListNew()))
        return NULL;

    cnt = interfaces->count;
J
John Ferlan 已提交
223
    for (i = 0; i < cnt; i++) {
224
        virInterfaceObjPtr srcobj = interfaces->objs[i];
J
John Ferlan 已提交
225
        virInterfaceDefPtr backup;
226
        virInterfaceObjPtr obj;
227
        char *xml = virInterfaceDefFormat(srcobj->def);
J
John Ferlan 已提交
228 229

        if (!xml)
230
            goto error;
J
John Ferlan 已提交
231

232
        if (!(backup = virInterfaceDefParseString(xml))) {
J
John Ferlan 已提交
233
            VIR_FREE(xml);
234
            goto error;
J
John Ferlan 已提交
235 236 237
        }

        VIR_FREE(xml);
238
        if (!(obj = virInterfaceObjListAssignDef(dest, backup)))
239
            goto error;
240
        virInterfaceObjEndAPI(&obj);
J
John Ferlan 已提交
241 242
    }

243 244 245 246 247
    return dest;

 error:
    virInterfaceObjListFree(dest);
    return NULL;
J
John Ferlan 已提交
248 249
}

250 251

virInterfaceObjPtr
252 253
virInterfaceObjListAssignDef(virInterfaceObjListPtr interfaces,
                             virInterfaceDefPtr def)
J
John Ferlan 已提交
254
{
255
    virInterfaceObjPtr obj;
J
John Ferlan 已提交
256

257
    if ((obj = virInterfaceObjListFindByName(interfaces, def->name))) {
258 259
        virInterfaceDefFree(obj->def);
        obj->def = def;
J
John Ferlan 已提交
260

261
        return obj;
J
John Ferlan 已提交
262 263
    }

264
    if (!(obj = virInterfaceObjNew(def)))
J
John Ferlan 已提交
265 266 267
        return NULL;

    if (VIR_APPEND_ELEMENT_COPY(interfaces->objs,
268
                                interfaces->count, obj) < 0) {
269
        obj->def = NULL;
270
        virInterfaceObjEndAPI(&obj);
J
John Ferlan 已提交
271 272
        return NULL;
    }
273
    return virObjectRef(obj);
J
John Ferlan 已提交
274 275
}

276 277

void
278 279
virInterfaceObjListRemove(virInterfaceObjListPtr interfaces,
                          virInterfaceObjPtr obj)
J
John Ferlan 已提交
280 281 282
{
    size_t i;

283
    virObjectUnlock(obj);
J
John Ferlan 已提交
284
    for (i = 0; i < interfaces->count; i++) {
285
        virObjectLock(interfaces->objs[i]);
286
        if (interfaces->objs[i] == obj) {
287 288
            virObjectUnlock(interfaces->objs[i]);
            virObjectUnref(interfaces->objs[i]);
J
John Ferlan 已提交
289 290 291 292

            VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count);
            break;
        }
293
        virObjectUnlock(interfaces->objs[i]);
J
John Ferlan 已提交
294 295
    }
}
296 297 298


int
299 300
virInterfaceObjListNumOfInterfaces(virInterfaceObjListPtr interfaces,
                                   bool wantActive)
301 302 303 304 305 306
{
    size_t i;
    int ninterfaces = 0;

    for (i = 0; (i < interfaces->count); i++) {
        virInterfaceObjPtr obj = interfaces->objs[i];
307
        virObjectLock(obj);
308 309
        if (wantActive == virInterfaceObjIsActive(obj))
            ninterfaces++;
310
        virObjectUnlock(obj);
311 312 313 314
    }

    return ninterfaces;
}
315 316 317


int
318 319 320 321
virInterfaceObjListGetNames(virInterfaceObjListPtr interfaces,
                            bool wantActive,
                            char **const names,
                            int maxnames)
322 323 324 325 326 327
{
    int nnames = 0;
    size_t i;

    for (i = 0; i < interfaces->count && nnames < maxnames; i++) {
        virInterfaceObjPtr obj = interfaces->objs[i];
328 329
        virInterfaceDefPtr def;

330
        virObjectLock(obj);
331
        def = obj->def;
332
        if (wantActive == virInterfaceObjIsActive(obj)) {
333
            if (VIR_STRDUP(names[nnames], def->name) < 0) {
334
                virObjectUnlock(obj);
335 336 337 338
                goto failure;
            }
            nnames++;
        }
339
        virObjectUnlock(obj);
340 341 342 343 344 345 346 347 348 349
    }

    return nnames;

 failure:
    while (--nnames >= 0)
        VIR_FREE(names[nnames]);

    return -1;
}