diff --git a/po/POTFILES.in b/po/POTFILES.in index 7c7f53078240a39ceaa6a81e237b7b74c9c7d2a0..50289a5c9817d6afea15ab43bdd7db00849938c6 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -42,6 +42,7 @@ src/conf/snapshot_conf.c src/conf/storage_conf.c src/conf/virchrdev.c src/conf/virdomainobjlist.c +src/conf/virinterfaceobj.c src/conf/virnodedeviceobj.c src/conf/virsecretobj.c src/cpu/cpu.c diff --git a/src/Makefile.am b/src/Makefile.am index 7d42eac3154b119c692df3b59d42255e94475102..c85927fb859c2850c46df988bed9887640e624c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -381,7 +381,8 @@ STORAGE_CONF_SOURCES = \ # Interface driver generic impl APIs INTERFACE_CONF_SOURCES = \ - conf/interface_conf.c conf/interface_conf.h + conf/interface_conf.c conf/interface_conf.h \ + conf/virinterfaceobj.c conf/virinterfaceobj.h # Secret driver generic impl APIs SECRET_CONF_SOURCES = \ diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c index e1e6a259d35ce048d55c96a089c4ec94017f3cbb..dc2ddd48fc1729e04cd36bb966fc6a43f715490a 100644 --- a/src/conf/interface_conf.c +++ b/src/conf/interface_conf.c @@ -1114,169 +1114,3 @@ char *virInterfaceDefFormat(const virInterfaceDef *def) } return virBufferContentAndReset(&buf); } - -/* virInterfaceObj manipulation */ - -void virInterfaceObjLock(virInterfaceObjPtr obj) -{ - virMutexLock(&obj->lock); -} - -void virInterfaceObjUnlock(virInterfaceObjPtr obj) -{ - virMutexUnlock(&obj->lock); -} - -void virInterfaceObjFree(virInterfaceObjPtr iface) -{ - if (!iface) - return; - - virInterfaceDefFree(iface->def); - virMutexDestroy(&iface->lock); - VIR_FREE(iface); -} - -/* virInterfaceObjList manipulation */ - -int virInterfaceFindByMACString(virInterfaceObjListPtr interfaces, - const char *mac, - virInterfaceObjPtr *matches, int maxmatches) -{ - size_t i; - unsigned int matchct = 0; - - for (i = 0; i < interfaces->count; i++) { - - virInterfaceObjLock(interfaces->objs[i]); - if (STRCASEEQ(interfaces->objs[i]->def->mac, mac)) { - matchct++; - if (matchct <= maxmatches) { - matches[matchct - 1] = interfaces->objs[i]; - /* keep the lock if we're returning object to caller */ - /* it is the caller's responsibility to unlock *all* matches */ - continue; - } - } - virInterfaceObjUnlock(interfaces->objs[i]); - - } - return matchct; -} - -virInterfaceObjPtr virInterfaceFindByName(virInterfaceObjListPtr interfaces, - const char *name) -{ - size_t i; - - for (i = 0; i < interfaces->count; i++) { - virInterfaceObjLock(interfaces->objs[i]); - if (STREQ(interfaces->objs[i]->def->name, name)) - return interfaces->objs[i]; - virInterfaceObjUnlock(interfaces->objs[i]); - } - - return NULL; -} - -void virInterfaceObjListFree(virInterfaceObjListPtr interfaces) -{ - size_t i; - - for (i = 0; i < interfaces->count; i++) - virInterfaceObjFree(interfaces->objs[i]); - - VIR_FREE(interfaces->objs); - interfaces->count = 0; -} - -int virInterfaceObjListClone(virInterfaceObjListPtr src, - virInterfaceObjListPtr dest) -{ - int ret = -1; - size_t i; - unsigned int cnt; - - if (!src || !dest) - goto cleanup; - - virInterfaceObjListFree(dest); /* start with an empty list */ - cnt = src->count; - for (i = 0; i < cnt; i++) { - virInterfaceDefPtr def = src->objs[i]->def; - virInterfaceDefPtr backup; - virInterfaceObjPtr iface; - char *xml = virInterfaceDefFormat(def); - - if (!xml) - goto cleanup; - - if ((backup = virInterfaceDefParseString(xml)) == NULL) { - VIR_FREE(xml); - goto cleanup; - } - - VIR_FREE(xml); - if ((iface = virInterfaceAssignDef(dest, backup)) == NULL) - goto cleanup; - virInterfaceObjUnlock(iface); /* was locked by virInterfaceAssignDef */ - } - - ret = cnt; - cleanup: - if ((ret < 0) && dest) - virInterfaceObjListFree(dest); - return ret; -} - -virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces, - virInterfaceDefPtr def) -{ - virInterfaceObjPtr iface; - - if ((iface = virInterfaceFindByName(interfaces, def->name))) { - virInterfaceDefFree(iface->def); - iface->def = def; - - return iface; - } - - if (VIR_ALLOC(iface) < 0) - return NULL; - if (virMutexInit(&iface->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("cannot initialize mutex")); - VIR_FREE(iface); - return NULL; - } - virInterfaceObjLock(iface); - - if (VIR_APPEND_ELEMENT_COPY(interfaces->objs, - interfaces->count, iface) < 0) { - virInterfaceObjFree(iface); - return NULL; - } - - iface->def = def; - return iface; - -} - -void virInterfaceRemove(virInterfaceObjListPtr interfaces, - virInterfaceObjPtr iface) -{ - size_t i; - - virInterfaceObjUnlock(iface); - for (i = 0; i < interfaces->count; i++) { - virInterfaceObjLock(interfaces->objs[i]); - if (interfaces->objs[i] == iface) { - virInterfaceObjUnlock(interfaces->objs[i]); - virInterfaceObjFree(interfaces->objs[i]); - - VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count); - break; - } - virInterfaceObjUnlock(interfaces->objs[i]); - } -} diff --git a/src/conf/interface_conf.h b/src/conf/interface_conf.h index 252320768def7d3dd2b324a59a3c95592ce848f7..7325d65f822e6ce97b72458a1fc3737c89eb3dde 100644 --- a/src/conf/interface_conf.h +++ b/src/conf/interface_conf.h @@ -161,46 +161,7 @@ struct _virInterfaceDef { virInterfaceProtocolDefPtr *protos; /* ptr to array of protos[nprotos] */ }; -typedef struct _virInterfaceObj virInterfaceObj; -typedef virInterfaceObj *virInterfaceObjPtr; -struct _virInterfaceObj { - virMutex lock; - - bool active; /* true if interface is active (up) */ - virInterfaceDefPtr def; /* The interface definition */ -}; - -typedef struct _virInterfaceObjList virInterfaceObjList; -typedef virInterfaceObjList *virInterfaceObjListPtr; -struct _virInterfaceObjList { - size_t count; - virInterfaceObjPtr *objs; -}; - -static inline bool -virInterfaceObjIsActive(const virInterfaceObj *iface) -{ - return iface->active; -} - -int virInterfaceFindByMACString(virInterfaceObjListPtr interfaces, - const char *mac, - virInterfaceObjPtr *matches, int maxmatches); -virInterfaceObjPtr virInterfaceFindByName(virInterfaceObjListPtr interfaces, - const char *name); - - void virInterfaceDefFree(virInterfaceDefPtr def); -void virInterfaceObjFree(virInterfaceObjPtr iface); -void virInterfaceObjListFree(virInterfaceObjListPtr vms); -int virInterfaceObjListClone(virInterfaceObjListPtr src, - virInterfaceObjListPtr dest); - - -virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces, - virInterfaceDefPtr def); -void virInterfaceRemove(virInterfaceObjListPtr interfaces, - virInterfaceObjPtr iface); virInterfaceDefPtr virInterfaceDefParseString(const char *xmlStr); virInterfaceDefPtr virInterfaceDefParseFile(const char *filename); @@ -209,12 +170,6 @@ virInterfaceDefPtr virInterfaceDefParseNode(xmlDocPtr xml, char *virInterfaceDefFormat(const virInterfaceDef *def); -void virInterfaceObjLock(virInterfaceObjPtr obj); -void virInterfaceObjUnlock(virInterfaceObjPtr obj); - -typedef bool (*virInterfaceObjListFilter)(virConnectPtr conn, - virInterfaceDefPtr def); - # define VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE \ (VIR_CONNECT_LIST_INTERFACES_ACTIVE | \ VIR_CONNECT_LIST_INTERFACES_INACTIVE) diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c new file mode 100644 index 0000000000000000000000000000000000000000..6c18911ff7d8bab0d5941610883556f946653fba --- /dev/null +++ b/src/conf/virinterfaceobj.c @@ -0,0 +1,201 @@ +/* + * 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 + * . + */ + +#include + +#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"); + + + +/* virInterfaceObj manipulation */ + +void virInterfaceObjLock(virInterfaceObjPtr obj) +{ + virMutexLock(&obj->lock); +} + +void virInterfaceObjUnlock(virInterfaceObjPtr obj) +{ + virMutexUnlock(&obj->lock); +} + +void virInterfaceObjFree(virInterfaceObjPtr iface) +{ + if (!iface) + return; + + virInterfaceDefFree(iface->def); + virMutexDestroy(&iface->lock); + VIR_FREE(iface); +} + +/* virInterfaceObjList manipulation */ + +int virInterfaceFindByMACString(virInterfaceObjListPtr interfaces, + const char *mac, + virInterfaceObjPtr *matches, int maxmatches) +{ + size_t i; + unsigned int matchct = 0; + + for (i = 0; i < interfaces->count; i++) { + + virInterfaceObjLock(interfaces->objs[i]); + if (STRCASEEQ(interfaces->objs[i]->def->mac, mac)) { + matchct++; + if (matchct <= maxmatches) { + matches[matchct - 1] = interfaces->objs[i]; + /* keep the lock if we're returning object to caller */ + /* it is the caller's responsibility to unlock *all* matches */ + continue; + } + } + virInterfaceObjUnlock(interfaces->objs[i]); + + } + return matchct; +} + +virInterfaceObjPtr virInterfaceFindByName(virInterfaceObjListPtr interfaces, + const char *name) +{ + size_t i; + + for (i = 0; i < interfaces->count; i++) { + virInterfaceObjLock(interfaces->objs[i]); + if (STREQ(interfaces->objs[i]->def->name, name)) + return interfaces->objs[i]; + virInterfaceObjUnlock(interfaces->objs[i]); + } + + return NULL; +} + +void virInterfaceObjListFree(virInterfaceObjListPtr interfaces) +{ + size_t i; + + for (i = 0; i < interfaces->count; i++) + virInterfaceObjFree(interfaces->objs[i]); + + VIR_FREE(interfaces->objs); + interfaces->count = 0; +} + +int virInterfaceObjListClone(virInterfaceObjListPtr src, + virInterfaceObjListPtr dest) +{ + int ret = -1; + size_t i; + unsigned int cnt; + + if (!src || !dest) + goto cleanup; + + virInterfaceObjListFree(dest); /* start with an empty list */ + cnt = src->count; + for (i = 0; i < cnt; i++) { + virInterfaceDefPtr def = src->objs[i]->def; + virInterfaceDefPtr backup; + virInterfaceObjPtr iface; + char *xml = virInterfaceDefFormat(def); + + if (!xml) + goto cleanup; + + if ((backup = virInterfaceDefParseString(xml)) == NULL) { + VIR_FREE(xml); + goto cleanup; + } + + VIR_FREE(xml); + if ((iface = virInterfaceAssignDef(dest, backup)) == NULL) + goto cleanup; + virInterfaceObjUnlock(iface); /* was locked by virInterfaceAssignDef */ + } + + ret = cnt; + cleanup: + if ((ret < 0) && dest) + virInterfaceObjListFree(dest); + return ret; +} + +virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces, + virInterfaceDefPtr def) +{ + virInterfaceObjPtr iface; + + if ((iface = virInterfaceFindByName(interfaces, def->name))) { + virInterfaceDefFree(iface->def); + iface->def = def; + + return iface; + } + + if (VIR_ALLOC(iface) < 0) + return NULL; + if (virMutexInit(&iface->lock) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("cannot initialize mutex")); + VIR_FREE(iface); + return NULL; + } + virInterfaceObjLock(iface); + + if (VIR_APPEND_ELEMENT_COPY(interfaces->objs, + interfaces->count, iface) < 0) { + virInterfaceObjFree(iface); + return NULL; + } + + iface->def = def; + return iface; + +} + +void virInterfaceRemove(virInterfaceObjListPtr interfaces, + virInterfaceObjPtr iface) +{ + size_t i; + + virInterfaceObjUnlock(iface); + for (i = 0; i < interfaces->count; i++) { + virInterfaceObjLock(interfaces->objs[i]); + if (interfaces->objs[i] == iface) { + virInterfaceObjUnlock(interfaces->objs[i]); + virInterfaceObjFree(interfaces->objs[i]); + + VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count); + break; + } + virInterfaceObjUnlock(interfaces->objs[i]); + } +} diff --git a/src/conf/virinterfaceobj.h b/src/conf/virinterfaceobj.h new file mode 100644 index 0000000000000000000000000000000000000000..51d7c75e08525cc9138e8ae7851885a544500f0a --- /dev/null +++ b/src/conf/virinterfaceobj.h @@ -0,0 +1,70 @@ +/* + * virinterfaceobj.h: interface object handling entry points + * (derived from interface_conf.h) + * + * 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 + * . + */ + +#ifndef __VIRINTERFACEOBJ_H__ +# define __VIRINTERFACEOBJ_H__ + +# include "internal.h" + +typedef struct _virInterfaceObj virInterfaceObj; +typedef virInterfaceObj *virInterfaceObjPtr; +struct _virInterfaceObj { + virMutex lock; + + bool active; /* true if interface is active (up) */ + virInterfaceDefPtr def; /* The interface definition */ +}; + +typedef struct _virInterfaceObjList virInterfaceObjList; +typedef virInterfaceObjList *virInterfaceObjListPtr; +struct _virInterfaceObjList { + size_t count; + virInterfaceObjPtr *objs; +}; + +static inline bool +virInterfaceObjIsActive(const virInterfaceObj *iface) +{ + return iface->active; +} + +int virInterfaceFindByMACString(virInterfaceObjListPtr interfaces, + const char *mac, + virInterfaceObjPtr *matches, int maxmatches); +virInterfaceObjPtr virInterfaceFindByName(virInterfaceObjListPtr interfaces, + const char *name); + + +void virInterfaceObjFree(virInterfaceObjPtr iface); +void virInterfaceObjListFree(virInterfaceObjListPtr vms); +int virInterfaceObjListClone(virInterfaceObjListPtr src, + virInterfaceObjListPtr dest); + + +virInterfaceObjPtr virInterfaceAssignDef(virInterfaceObjListPtr interfaces, + virInterfaceDefPtr def); +void virInterfaceRemove(virInterfaceObjListPtr interfaces, + virInterfaceObjPtr iface); + +void virInterfaceObjLock(virInterfaceObjPtr obj); +void virInterfaceObjUnlock(virInterfaceObjPtr obj); + +typedef bool (*virInterfaceObjListFilter)(virConnectPtr conn, + virInterfaceDefPtr def); +#endif /* __VIRINTERFACEOBJ_H__ */ diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c index 01816358456ac143a60733805fcebfabe82bf590..700a8a075c39146268ebfcdee82c74721816fff5 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -33,6 +33,7 @@ #include "virlog.h" #include "virstring.h" #include "viraccessapicheck.h" +#include "virinterfaceobj.h" #define VIR_FROM_THIS VIR_FROM_INTERFACE diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c index 5d0fc64e535cd2e755e6c24886c9c617017987a6..18a45fa854798b1ca5dd89c36e7dc0b598fc6c3c 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -34,6 +34,7 @@ #include "viralloc.h" #include "virstring.h" #include "viraccessapicheck.h" +#include "virinterfaceobj.h" #include "virnetdev.h" #define VIR_FROM_THIS VIR_FROM_INTERFACE diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index aed1d3d5022270519f0f5ba0c6760893fdec46fa..b39e17d463a2254e25a2afc412101b9f78b2edc3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -594,19 +594,11 @@ virDomainConfVMNWFilterTeardown; # conf/interface_conf.h -virInterfaceAssignDef; virInterfaceDefFormat; virInterfaceDefFree; virInterfaceDefParseFile; virInterfaceDefParseNode; virInterfaceDefParseString; -virInterfaceFindByMACString; -virInterfaceFindByName; -virInterfaceObjListClone; -virInterfaceObjListFree; -virInterfaceObjLock; -virInterfaceObjUnlock; -virInterfaceRemove; # conf/netdev_bandwidth_conf.h @@ -948,6 +940,17 @@ virDomainObjListRemoveLocked; virDomainObjListRename; +# conf/virinterfaceobj.h +virInterfaceAssignDef; +virInterfaceFindByMACString; +virInterfaceFindByName; +virInterfaceObjListClone; +virInterfaceObjListFree; +virInterfaceObjLock; +virInterfaceObjUnlock; +virInterfaceRemove; + + # conf/virnodedeviceobj.h virNodeDeviceObjAssignDef; virNodeDeviceObjFindByName; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 61c82b90cb0ee179d944ae9c82a303556e71f7f5..e72a91f8f1aea04ae3fd3cb9722d7cc23f456b4f 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -64,6 +64,7 @@ #include "virauth.h" #include "viratomic.h" #include "virdomainobjlist.h" +#include "virinterfaceobj.h" #include "virhostcpu.h" #define VIR_FROM_THIS VIR_FROM_TEST