[libvirt] [PATCH] Add interface object list manipulation functions.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



These functions are useful for an implementation of an interface driver
that doesn't use a library that manages its own list of interfaces
(as netcf does), for example the test interface driver.
---
 src/interface_conf.c |  136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/interface_conf.h |   15 ++++-
 2 files changed, 148 insertions(+), 3 deletions(-)

diff --git a/src/interface_conf.c b/src/interface_conf.c
index 0b73c97..6a0b35e 100644
--- a/src/interface_conf.c
+++ b/src/interface_conf.c
@@ -1128,6 +1128,8 @@ cleanup:
     return NULL;
 }
 
+/* virInterfaceObj manipulation */
+
 void virInterfaceObjLock(virInterfaceObjPtr obj)
 {
     virMutexLock(&obj->lock);
@@ -1137,3 +1139,137 @@ 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(const virInterfaceObjListPtr interfaces,
+                                const char *mac,
+                                virInterfaceObjPtr *matches, int maxmatches)
+{
+    unsigned int i, matchct = 0;
+
+    for (i = 0 ; i < interfaces->count ; i++) {
+        int lock_for_caller = 0;
+        virInterfaceObjLock(interfaces->objs[i]);
+        if (STREQ(interfaces->objs[i]->def->mac, mac)) {
+            if (matchct < maxmatches) {
+                matches[matchct] = interfaces->objs[i];
+                lock_for_caller = 1;
+            }
+        matchct++;
+        }
+        if (!lock_for_caller) {
+            /* it is the caller's responsibility to unlock *all* matches */
+            virInterfaceObjUnlock(interfaces->objs[i]);
+        }
+    }
+
+    return matchct;
+}
+
+virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr
+                                          interfaces,
+                                          const char *name)
+{
+    unsigned int 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)
+{
+    unsigned int i;
+
+    for (i = 0 ; i < interfaces->count ; i++)
+        virInterfaceObjFree(interfaces->objs[i]);
+
+    VIR_FREE(interfaces->objs);
+    interfaces->count = 0;
+}
+
+virInterfaceObjPtr virInterfaceAssignDef(virConnectPtr conn,
+                                         virInterfaceObjListPtr interfaces,
+                                         const virInterfaceDefPtr def)
+{
+    virInterfaceObjPtr interface;
+
+    if ((interface = virInterfaceFindByName(interfaces, def->name))) {
+        if (interface->def)
+            virInterfaceDefFree(interface->def);
+        interface->def = def;
+
+        return interface;
+    }
+
+    if (VIR_ALLOC(interface) < 0) {
+        virReportOOMError(conn);
+        return NULL;
+    }
+    if (virMutexInit(&interface->lock) < 0) {
+        virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
+                                "%s", _("cannot initialize mutex"));
+        VIR_FREE(interface);
+        return NULL;
+    }
+    virInterfaceObjLock(interface);
+    interface->def = def;
+
+    if (VIR_REALLOC_N(interfaces->objs, interfaces->count + 1) < 0) {
+        virReportOOMError(conn);
+        VIR_FREE(interface);
+        return NULL;
+    }
+
+    interfaces->objs[interfaces->count] = interface;
+    interfaces->count++;
+
+    return interface;
+
+}
+
+void virInterfaceRemove(virInterfaceObjListPtr interfaces,
+                        const virInterfaceObjPtr interface)
+{
+    unsigned int i;
+
+    virInterfaceObjUnlock(interface);
+    for (i = 0 ; i < interfaces->count ; i++) {
+        virInterfaceObjLock(interfaces->objs[i]);
+        if (interfaces->objs[i] == interface) {
+            virInterfaceObjUnlock(interfaces->objs[i]);
+            virInterfaceObjFree(interfaces->objs[i]);
+
+            if (i < (interfaces->count - 1))
+                memmove(interfaces->objs + i, interfaces->objs + i + 1,
+                        sizeof(*(interfaces->objs)) * (interfaces->count - (i + 1)));
+
+            if (VIR_REALLOC_N(interfaces->objs, interfaces->count - 1) < 0) {
+                ; /* Failure to reduce memory allocation isn't fatal */
+            }
+            interfaces->count--;
+
+            break;
+        }
+        virInterfaceObjUnlock(interfaces->objs[i]);
+    }
+}
+
+
+
diff --git a/src/interface_conf.h b/src/interface_conf.h
index c77a230..aea1208 100644
--- a/src/interface_conf.h
+++ b/src/interface_conf.h
@@ -160,6 +160,7 @@ typedef virInterfaceObj *virInterfaceObjPtr;
 struct _virInterfaceObj {
     virMutex lock;
 
+    int active:1;           /* 1 if interface is active (up) */
     virInterfaceDefPtr def; /* The interface definition */
 };
 
@@ -170,9 +171,17 @@ struct _virInterfaceObjList {
     virInterfaceObjPtr *objs;
 };
 
-virInterfaceObjPtr virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
-                                               const char *mac);
-virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr interfaces,
+static inline int
+virInterfaceIsActive(const virInterfaceObjPtr iface)
+{
+    return iface->active;
+}
+
+int virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
+                                const char *mac,
+                                virInterfaceObjPtr *matches, int maxmatches);
+virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr
+                                          interfaces,
                                           const char *name);
 
 
-- 
1.6.0.6

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]