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

[Libvir] [patch 1/7] Merge the active and inactive network/guest lists



Merge the ->activevms and ->inactivevms into a single
->vms list where each VM has a "active" flag in order
to make things easier to manage.

Likewise for networks.

Signed-off-by: Mark McLoughlin <markmc redhat com>

Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -1215,7 +1215,7 @@ struct qemud_vm *qemudLoadConfigXML(stru
     xmlFreeDoc(xml);
 
     if ((vm = qemudFindVMByName(server, def->name))) {
-        if (vm->id == -1) {
+        if (!vm->active) {
             qemudFreeVMDef(vm->def);
             vm->def = def;
         } else {
@@ -1263,8 +1263,8 @@ struct qemud_vm *qemudLoadConfigXML(stru
     }
 
     if (newVM) {
-        vm->next = server->inactivevms;
-        server->inactivevms = vm;
+        vm->next = server->vms;
+        server->vms = vm;
         server->ninactivevms++;
     }
 
@@ -1611,8 +1611,8 @@ struct qemud_network *qemudLoadNetworkCo
     }
 
     if (newNetwork) {
-        network->next = server->inactivenetworks;
-        server->inactivenetworks = network;
+        network->next = server->networks;
+        server->networks = network;
         server->ninactivenetworks++;
     }
 
Index: libvirt/qemud/driver.c
===================================================================
--- libvirt.orig/qemud/driver.c
+++ libvirt/qemud/driver.c
@@ -222,10 +222,10 @@ static int qemudGetProcessInfo(unsigned 
 }
 
 struct qemud_vm *qemudFindVMByID(const struct qemud_server *server, int id) {
-    struct qemud_vm *vm = server->activevms;
+    struct qemud_vm *vm = server->vms;
 
     while (vm) {
-        if (vm->id == id)
+        if (vm->active && vm->id == id)
             return vm;
         vm = vm->next;
     }
@@ -235,7 +235,7 @@ struct qemud_vm *qemudFindVMByID(const s
 
 struct qemud_vm *qemudFindVMByUUID(const struct qemud_server *server,
                                    const unsigned char *uuid) {
-    struct qemud_vm *vm = server->activevms;
+    struct qemud_vm *vm = server->vms;
 
     while (vm) {
         if (!memcmp(vm->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
@@ -243,19 +243,12 @@ struct qemud_vm *qemudFindVMByUUID(const
         vm = vm->next;
     }
 
-    vm = server->inactivevms;
-    while (vm) {
-        if (!memcmp(vm->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
-            return vm;
-        vm = vm->next;
-    }
-
     return NULL;
 }
 
 struct qemud_vm *qemudFindVMByName(const struct qemud_server *server,
                                    const char *name) {
-    struct qemud_vm *vm = server->activevms;
+    struct qemud_vm *vm = server->vms;
 
     while (vm) {
         if (!strcmp(vm->def->name, name))
@@ -263,13 +256,6 @@ struct qemud_vm *qemudFindVMByName(const
         vm = vm->next;
     }
 
-    vm = server->inactivevms;
-    while (vm) {
-        if (!strcmp(vm->def->name, name))
-            return vm;
-        vm = vm->next;
-    }
-
     return NULL;
 }
 
@@ -278,12 +264,14 @@ int qemudGetVersion(struct qemud_server 
 }
 
 int qemudListDomains(struct qemud_server *server, int *ids, int nids) {
-    struct qemud_vm *vm = server->activevms;
+    struct qemud_vm *vm = server->vms;
     int got = 0;
     while (vm && got < nids) {
-        ids[got] = vm->id;
+        if (vm->active) {
+            ids[got] = vm->id;
+            got++;
+        }
         vm = vm->next;
-        got++;
     }
     return got;
 }
@@ -444,13 +432,15 @@ int qemudDomainDumpXML(struct qemud_serv
 
 
 int qemudListDefinedDomains(struct qemud_server *server, char *const*names, int nnames) {
-    struct qemud_vm *vm = server->inactivevms;
+    struct qemud_vm *vm = server->vms;
     int got = 0;
     while (vm && got < nnames) {
-        strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
-        names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+        if (!vm->active) {
+            strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
+            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            got++;
+        }
         vm = vm->next;
-        got++;
     }
     return got;
 }
@@ -462,26 +452,10 @@ int qemudNumDefinedDomains(struct qemud_
 
 
 int qemudDomainStart(struct qemud_server *server, struct qemud_vm *vm) {
-    struct qemud_vm *prev = NULL, *curr = server->inactivevms;
-    if (qemudStartVMDaemon(server, vm) < 0) {
+    if (qemudStartVMDaemon(server, vm) < 0)
         return 1;
-    }
-
-    while (curr) {
-        if (curr == vm) {
-            if (prev)
-                prev->next = curr->next;
-            else
-                server->inactivevms = curr->next;
-            server->ninactivevms--;
-            break;
-        }
-        prev = curr;
-        curr = curr->next;
-    }
 
-    vm->next = server->activevms;
-    server->activevms = vm;
+    server->ninactivevms--;
     server->nactivevms++;
     server->nvmfds += 2;
 
@@ -495,14 +469,14 @@ struct qemud_vm *qemudDomainDefine(struc
 
 int qemudDomainUndefine(struct qemud_server *server, const unsigned char *uuid) {
     struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
-    struct qemud_vm *prev = NULL, *curr = server->inactivevms;
+    struct qemud_vm *prev = NULL, *curr = server->vms;
 
     if (!vm) {
         qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
         return -1;
     }
 
-    if (vm->pid != -1) {
+    if (vm->active) {
         qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot delete active domain");
         return -1;
     }
@@ -517,7 +491,7 @@ int qemudDomainUndefine(struct qemud_ser
             if (prev) {
                 prev->next = curr->next;
             } else {
-                server->inactivevms = curr->next;
+                server->vms = curr->next;
             }
             server->ninactivevms--;
             break;
@@ -534,7 +508,7 @@ int qemudDomainUndefine(struct qemud_ser
 
 struct qemud_network *qemudFindNetworkByUUID(const struct qemud_server *server,
                                              const unsigned char *uuid) {
-    struct qemud_network *network = server->activenetworks;
+    struct qemud_network *network = server->networks;
 
     while (network) {
         if (!memcmp(network->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
@@ -542,19 +516,12 @@ struct qemud_network *qemudFindNetworkBy
         network = network->next;
     }
 
-    network = server->inactivenetworks;
-    while (network) {
-        if (!memcmp(network->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
-            return network;
-        network = network->next;
-    }
-
     return NULL;
 }
 
 struct qemud_network *qemudFindNetworkByName(const struct qemud_server *server,
                                              const char *name) {
-    struct qemud_network *network = server->activenetworks;
+    struct qemud_network *network = server->networks;
 
     while (network) {
         if (!strcmp(network->def->name, name))
@@ -562,13 +529,6 @@ struct qemud_network *qemudFindNetworkBy
         network = network->next;
     }
 
-    network = server->inactivenetworks;
-    while (network) {
-        if (!strcmp(network->def->name, name))
-            return network;
-        network = network->next;
-    }
-
     return NULL;
 }
 
@@ -577,13 +537,15 @@ int qemudNumNetworks(struct qemud_server
 }
 
 int qemudListNetworks(struct qemud_server *server, char *const*names, int nnames) {
-    struct qemud_network *network = server->activenetworks;
+    struct qemud_network *network = server->networks;
     int got = 0;
     while (network && got < nnames) {
-        strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
-        names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+        if (network->active) {
+            strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
+            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            got++;
+        }
         network = network->next;
-        got++;
     }
     return got;
 }
@@ -593,13 +555,15 @@ int qemudNumDefinedNetworks(struct qemud
 }
 
 int qemudListDefinedNetworks(struct qemud_server *server, char *const*names, int nnames) {
-    struct qemud_network *network = server->inactivenetworks;
+    struct qemud_network *network = server->networks;
     int got = 0;
     while (network && got < nnames) {
-        strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
-        names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+        if (!network->active) {
+            strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
+            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            got++;
+        }
         network = network->next;
-        got++;
     }
     return got;
 }
@@ -625,7 +589,7 @@ struct qemud_network *qemudNetworkDefine
 
 int qemudNetworkUndefine(struct qemud_server *server, const unsigned char *uuid) {
     struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
-    struct qemud_network *prev = NULL, *curr = server->inactivenetworks;
+    struct qemud_network *prev = NULL, *curr = server->networks;
 
     if (!network) {
         qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no network with matching uuid");
@@ -642,7 +606,7 @@ int qemudNetworkUndefine(struct qemud_se
             if (prev) {
                 prev->next = curr->next;
             } else {
-                server->inactivenetworks = curr->next;
+                server->networks = curr->next;
             }
             server->ninactivenetworks--;
             break;
@@ -658,26 +622,10 @@ int qemudNetworkUndefine(struct qemud_se
 }
 
 int qemudNetworkStart(struct qemud_server *server, struct qemud_network *network) {
-    struct qemud_network *prev = NULL, *curr = server->inactivenetworks;
-    if (qemudStartNetworkDaemon(server, network) < 0) {
+    if (qemudStartNetworkDaemon(server, network) < 0)
         return 1;
-    }
-
-    while (curr) {
-        if (curr == network) {
-            if (prev)
-                prev->next = curr->next;
-            else
-                server->inactivenetworks = curr->next;
-            server->ninactivenetworks--;
-            break;
-        }
-        prev = curr;
-        curr = curr->next;
-    }
 
-    network->next = server->activenetworks;
-    server->activenetworks = network;
+    server->ninactivenetworks--;
     server->nactivenetworks++;
 
     return 0;
Index: libvirt/qemud/internal.h
===================================================================
--- libvirt.orig/qemud/internal.h
+++ libvirt/qemud/internal.h
@@ -209,6 +209,8 @@ struct qemud_vm {
     struct qemud_vm_def *def; /* The current definition */
     struct qemud_vm_def *newDef; /* New definition to activate at shutdown */
 
+    unsigned int active : 1;
+
     struct qemud_vm *next;
 };
 
@@ -280,14 +282,12 @@ struct qemud_server {
     int sigread;
     int nvmfds;
     int nactivevms;
-    struct qemud_vm *activevms;
     int ninactivevms;
-    struct qemud_vm *inactivevms;
+    struct qemud_vm *vms;
     int nextvmid;
     int nactivenetworks;
-    struct qemud_network *activenetworks;
     int ninactivenetworks;
-    struct qemud_network *inactivenetworks;
+    struct qemud_network *networks;
     brControl *brctl;
     iptablesContext *iptables;
     char configDir[PATH_MAX];
Index: libvirt/qemud/qemud.c
===================================================================
--- libvirt.orig/qemud/qemud.c
+++ libvirt/qemud/qemud.c
@@ -95,39 +95,43 @@ static int qemudDispatchSignal(struct qe
         qemudLog(QEMUD_WARN, "Shutting down on signal %d", sigc);
 
         /* shutdown active VMs */
-        vm = server->activevms;
+        vm = server->vms;
         while (vm) {
             struct qemud_vm *next = vm->next;
-            qemudShutdownVMDaemon(server, vm);
+            if (vm->active)
+                qemudShutdownVMDaemon(server, vm);
             vm = next;
         }
 
         /* free inactive VMs */
-        vm = server->inactivevms;
+        vm = server->vms;
         while (vm) {
             struct qemud_vm *next = vm->next;
             qemudFreeVM(vm);
             vm = next;
         }
-        server->inactivevms = NULL;
+        server->vms = NULL;
+        server->nactivevms = 0;
         server->ninactivevms = 0;
 
         /* shutdown active networks */
-        network = server->activenetworks;
+        network = server->networks;
         while (network) {
             struct qemud_network *next = network->next;
-            qemudShutdownNetworkDaemon(server, network);
+            if (network->active)
+                qemudShutdownNetworkDaemon(server, network);
             network = next;
         }
 
         /* free inactive networks */
-        network = server->inactivenetworks;
+        network = server->networks;
         while (network) {
             struct qemud_network *next = network->next;
             qemudFreeNetwork(network);
             network = next;
         }
-        server->inactivenetworks = NULL;
+        server->networks = NULL;
+        server->nactivenetworks = 0;
         server->ninactivenetworks = 0;
 
         server->shutdown = 1;
@@ -586,6 +590,7 @@ int qemudStartVMDaemon(struct qemud_serv
 
     if (qemudExec(server, argv, &vm->pid, &vm->stdout, &vm->stderr) == 0) {
         vm->id = server->nextvmid++;
+        vm->active = 1;
         ret = 0;
     }
 
@@ -805,50 +810,24 @@ qemudNetworkIfaceDisconnect(struct qemud
 }
 
 int qemudShutdownVMDaemon(struct qemud_server *server, struct qemud_vm *vm) {
-    struct qemud_vm *prev = NULL, *curr = server->activevms;
     struct qemud_vm_net_def *net;
 
-    qemudLog(QEMUD_INFO, "Shutting down VM '%s'", vm->def->name);
-
-    /* Already cleaned-up */
-    if (vm->pid < 0)
+    if (!vm->active)
         return 0;
 
-    kill(vm->pid, SIGTERM);
-
-    /* Move it to inactive vm list */
-    while (curr) {
-        if (curr == vm) {
-            if (prev) {
-                prev->next = curr->next;
-            } else {
-                server->activevms = curr->next;
-            }
-            server->nactivevms--;
-
-            curr->next = server->inactivevms;
-            server->inactivevms = curr;
-            server->ninactivevms++;
-            break;
-        }
-        prev = curr;
-        curr = curr->next;
-    }
+    qemudLog(QEMUD_INFO, "Shutting down VM '%s'", vm->def->name);
 
-    if (!curr) {
-        qemudDebug("Could not find VM to shutdown");
-        return 0;
-    }
+    kill(vm->pid, SIGTERM);
 
-    qemudVMData(server, vm, curr->stdout);
-    qemudVMData(server, vm, curr->stderr);
-    close(curr->stdout);
-    close(curr->stderr);
-    if (curr->monitor != -1)
-        close(curr->monitor);
-    curr->stdout = -1;
-    curr->stderr = -1;
-    curr->monitor = -1;
+    qemudVMData(server, vm, vm->stdout);
+    qemudVMData(server, vm, vm->stderr);
+    close(vm->stdout);
+    close(vm->stderr);
+    if (vm->monitor != -1)
+        close(vm->monitor);
+    vm->stdout = -1;
+    vm->stderr = -1;
+    vm->monitor = -1;
     server->nvmfds -= 2;
 
     net = vm->def->nets;
@@ -867,6 +846,7 @@ int qemudShutdownVMDaemon(struct qemud_s
 
     vm->pid = -1;
     vm->id = -1;
+    vm->active = 0;
 
     if (vm->newDef) {
         qemudFreeVMDef(vm->def);
@@ -874,6 +854,9 @@ int qemudShutdownVMDaemon(struct qemud_s
         vm->newDef = NULL;
     }
 
+    server->nactivevms--;
+    server->ninactivevms++;
+
     return 0;
 }
 
@@ -1209,7 +1192,6 @@ int qemudStartNetworkDaemon(struct qemud
 
 int qemudShutdownNetworkDaemon(struct qemud_server *server,
                                struct qemud_network *network) {
-    struct qemud_network *prev, *curr;
     int err;
 
     qemudLog(QEMUD_INFO, "Shutting down network '%s'", network->def->name);
@@ -1233,27 +1215,6 @@ int qemudShutdownNetworkDaemon(struct qe
                  network->bridge, strerror(err));
     }
 
-    /* Move it to inactive networks list */
-    prev = NULL;
-    curr = server->activenetworks;
-    while (curr) {
-        if (curr == network) {
-            if (prev) {
-                prev->next = curr->next;
-            } else {
-                server->activenetworks = curr->next;
-            }
-            server->nactivenetworks--;
-
-            curr->next = server->inactivenetworks;
-            server->inactivenetworks = curr;
-            server->ninactivenetworks++;
-            break;
-        }
-        prev = curr;
-        curr = curr->next;
-    }
-
     if (network->dnsmasqPid > 0 &&
         waitpid(network->dnsmasqPid, NULL, WNOHANG) != network->dnsmasqPid) {
         kill(network->dnsmasqPid, SIGKILL);
@@ -1271,6 +1232,9 @@ int qemudShutdownNetworkDaemon(struct qe
         network->newDef = NULL;
     }
 
+    server->nactivenetworks--;
+    server->ninactivenetworks++;
+
     return 0;
 }
 
@@ -1278,7 +1242,7 @@ int qemudShutdownNetworkDaemon(struct qe
 static int qemudDispatchPoll(struct qemud_server *server, struct pollfd *fds) {
     struct qemud_socket *sock = server->sockets;
     struct qemud_client *client = server->clients;
-    struct qemud_vm *vm = server->activevms;
+    struct qemud_vm *vm;
     struct qemud_vm *tmp;
     struct qemud_network *network, *prevnet;
     int ret = 0;
@@ -1314,12 +1278,18 @@ static int qemudDispatchPoll(struct qemu
         fd++;
         client = next;
     }
+    vm = server->vms;
     while (vm) {
         struct qemud_vm *next = vm->next;
         int failed = 0,
             stdoutfd = vm->stdout,
             stderrfd = vm->stderr;
 
+        if (!vm->active) {
+            vm = next;
+            continue;
+        }
+
         if (stdoutfd != -1) {
             if (fds[fd].revents) {
                 if (fds[fd].revents == POLLIN) {
@@ -1353,17 +1323,18 @@ static int qemudDispatchPoll(struct qemu
 
     /* Cleanup any VMs which shutdown & dont have an associated
        config file */
-    vm = server->inactivevms;
+    vm = server->vms;
     tmp = NULL;
     while (vm) {
-        if (!vm->configFile[0]) {
+        if (!vm->active && !vm->configFile[0]) {
             struct qemud_vm *next = vm->next;
             if (tmp) {
                 tmp->next = next;
             } else {
-                server->inactivevms = next;
+                server->vms = next;
             }
             qemudFreeVM(vm);
+            server->ninactivevms--;
             vm = next;
         } else {
             tmp = vm;
@@ -1372,17 +1343,18 @@ static int qemudDispatchPoll(struct qemu
     }
 
     /* Cleanup any networks too */
-    network = server->inactivenetworks;
+    network = server->networks;
     prevnet = NULL;
     while (network) {
-        if (!network->configFile[0]) {
+        if (!network->active && !network->configFile[0]) {
             struct qemud_network *next = network->next;
             if (prevnet) {
                 prevnet->next = next;
             } else {
-                server->inactivenetworks = next;
+                server->networks = next;
             }
             qemudFreeNetwork(network);
+            server->ninactivenetworks--;
             network = next;
         } else {
             prevnet = network;
@@ -1419,7 +1391,9 @@ static void qemudPreparePoll(struct qemu
             fds[fd].events = POLLIN | POLLERR | POLLHUP;
         fd++;
     }
-    for (vm = server->activevms ; vm ; vm = vm->next) {
+    for (vm = server->vms ; vm ; vm = vm->next) {
+        if (!vm->active)
+            continue;
         if (vm->stdout != -1) {
             fds[fd].fd = vm->stdout;
             fds[fd].events = POLLIN | POLLERR | POLLHUP;

-- 


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