Added network events to the bridged network driver

This commit is contained in:
Cédric Bosdonnat 2013-12-11 11:38:02 +01:00 committed by Daniel P. Berrange
parent 61ac8ce0a9
commit cd88e9293a
2 changed files with 86 additions and 0 deletions

View File

@ -70,6 +70,7 @@
#include "virfile.h"
#include "virstring.h"
#include "viraccessapicheck.h"
#include "network_event.h"
#define VIR_FROM_THIS VIR_FROM_NETWORK
@ -438,6 +439,8 @@ networkStateInitialize(bool privileged,
networkReloadFirewallRules(driverState);
networkRefreshDaemons(driverState);
driverState->networkEventState = virObjectEventStateNew();
networkDriverUnlock(driverState);
#ifdef HAVE_FIREWALLD
@ -532,6 +535,8 @@ networkStateCleanup(void) {
networkDriverLock(driverState);
virObjectEventStateFree(driverState->networkEventState);
/* free inactive networks */
virNetworkObjListFree(&driverState->networks);
@ -2290,6 +2295,48 @@ cleanup:
return ret;
}
static int
networkConnectNetworkEventRegisterAny(virConnectPtr conn,
virNetworkPtr net,
int eventID,
virConnectNetworkEventGenericCallback callback,
void *opaque,
virFreeCallback freecb)
{
virNetworkDriverStatePtr driver = conn->networkPrivateData;
int ret = -1;
if (virConnectNetworkEventRegisterAnyEnsureACL(conn) < 0)
goto cleanup;
if (virNetworkEventStateRegisterID(conn, driver->networkEventState,
net, eventID,
VIR_OBJECT_EVENT_CALLBACK(callback),
opaque, freecb, &ret) < 0)
ret = -1;
cleanup:
return ret;
}
static int
networkConnectNetworkEventDeregisterAny(virConnectPtr conn,
int callbackID)
{
virNetworkDriverStatePtr driver = conn->networkPrivateData;
int ret = -1;
if (virConnectNetworkEventDeregisterAnyEnsureACL(conn) < 0)
goto cleanup;
ret = virObjectEventStateDeregisterID(conn,
driver->networkEventState,
callbackID);
cleanup:
return ret;
}
static int networkIsActive(virNetworkPtr net)
{
virNetworkObjPtr obj;
@ -2483,6 +2530,7 @@ static virNetworkPtr networkCreateXML(virConnectPtr conn, const char *xml) {
virNetworkDefPtr def;
virNetworkObjPtr network = NULL;
virNetworkPtr ret = NULL;
virObjectEventPtr event = NULL;
networkDriverLock(driver);
@ -2509,11 +2557,17 @@ static virNetworkPtr networkCreateXML(virConnectPtr conn, const char *xml) {
goto cleanup;
}
event = virNetworkEventLifecycleNew(network->def->name,
network->def->uuid,
VIR_NETWORK_EVENT_STARTED);
VIR_INFO("Creating network '%s'", network->def->name);
ret = virGetNetwork(conn, network->def->name, network->def->uuid);
cleanup:
virNetworkDefFree(def);
if (event)
virObjectEventStateQueue(driver->networkEventState, event);
if (network)
virNetworkObjUnlock(network);
networkDriverUnlock(driver);
@ -2526,6 +2580,7 @@ static virNetworkPtr networkDefineXML(virConnectPtr conn, const char *xml) {
bool freeDef = true;
virNetworkObjPtr network = NULL;
virNetworkPtr ret = NULL;
virObjectEventPtr event = NULL;
networkDriverLock(driver);
@ -2565,10 +2620,15 @@ static virNetworkPtr networkDefineXML(virConnectPtr conn, const char *xml) {
goto cleanup;
}
event = virNetworkEventLifecycleNew(def->name, def->uuid,
VIR_NETWORK_EVENT_DEFINED);
VIR_INFO("Defining network '%s'", def->name);
ret = virGetNetwork(conn, def->name, def->uuid);
cleanup:
if (event)
virObjectEventStateQueue(driver->networkEventState, event);
if (freeDef)
virNetworkDefFree(def);
if (network)
@ -2583,6 +2643,7 @@ networkUndefine(virNetworkPtr net) {
virNetworkObjPtr network;
int ret = -1;
bool active = false;
virObjectEventPtr event = NULL;
networkDriverLock(driver);
@ -2610,6 +2671,10 @@ networkUndefine(virNetworkPtr net) {
virNetworkDefFree(network->newDef);
network->newDef = NULL;
event = virNetworkEventLifecycleNew(network->def->name,
network->def->uuid,
VIR_NETWORK_EVENT_UNDEFINED);
VIR_INFO("Undefining network '%s'", network->def->name);
if (!active) {
if (networkRemoveInactive(driver, network) < 0) {
@ -2622,6 +2687,8 @@ networkUndefine(virNetworkPtr net) {
ret = 0;
cleanup:
if (event)
virObjectEventStateQueue(driver->networkEventState, event);
if (network)
virNetworkObjUnlock(network);
networkDriverUnlock(driver);
@ -2805,6 +2872,7 @@ static int networkCreate(virNetworkPtr net) {
virNetworkDriverStatePtr driver = net->conn->networkPrivateData;
virNetworkObjPtr network;
int ret = -1;
virObjectEventPtr event = NULL;
networkDriverLock(driver);
network = virNetworkFindByUUID(&driver->networks, net->uuid);
@ -2820,7 +2888,13 @@ static int networkCreate(virNetworkPtr net) {
ret = networkStartNetwork(driver, network);
event = virNetworkEventLifecycleNew(network->def->name,
network->def->uuid,
VIR_NETWORK_EVENT_STARTED);
cleanup:
if (event)
virObjectEventStateQueue(driver->networkEventState, event);
if (network)
virNetworkObjUnlock(network);
networkDriverUnlock(driver);
@ -2831,6 +2905,7 @@ static int networkDestroy(virNetworkPtr net) {
virNetworkDriverStatePtr driver = net->conn->networkPrivateData;
virNetworkObjPtr network;
int ret = -1;
virObjectEventPtr event = NULL;
networkDriverLock(driver);
network = virNetworkFindByUUID(&driver->networks, net->uuid);
@ -2853,6 +2928,10 @@ static int networkDestroy(virNetworkPtr net) {
if ((ret = networkShutdownNetwork(driver, network)) < 0)
goto cleanup;
event = virNetworkEventLifecycleNew(network->def->name,
network->def->uuid,
VIR_NETWORK_EVENT_STOPPED);
if (!network->persistent) {
if (networkRemoveInactive(driver, network) < 0) {
network = NULL;
@ -2863,6 +2942,8 @@ static int networkDestroy(virNetworkPtr net) {
}
cleanup:
if (event)
virObjectEventStateQueue(driver->networkEventState, event);
if (network)
virNetworkObjUnlock(network);
networkDriverUnlock(driver);
@ -3021,6 +3102,8 @@ static virNetworkDriver networkDriver = {
.connectNumOfDefinedNetworks = networkConnectNumOfDefinedNetworks, /* 0.2.0 */
.connectListDefinedNetworks = networkConnectListDefinedNetworks, /* 0.2.0 */
.connectListAllNetworks = networkConnectListAllNetworks, /* 0.10.2 */
.connectNetworkEventRegisterAny = networkConnectNetworkEventRegisterAny, /* 1.2.1 */
.connectNetworkEventDeregisterAny = networkConnectNetworkEventDeregisterAny, /* 1.2.1 */
.networkLookupByUUID = networkLookupByUUID, /* 0.2.0 */
.networkLookupByName = networkLookupByName, /* 0.2.0 */
.networkCreateXML = networkCreateXML, /* 0.2.0 */

View File

@ -29,6 +29,7 @@
# include "virthread.h"
# include "virdnsmasq.h"
# include "network_conf.h"
# include "object_event.h"
/* Main driver state */
struct _virNetworkDriverState {
@ -43,6 +44,8 @@ struct _virNetworkDriverState {
char *dnsmasqStateDir;
char *radvdStateDir;
dnsmasqCapsPtr dnsmasqCaps;
virObjectEventStatePtr networkEventState;
};
typedef struct _virNetworkDriverState virNetworkDriverState;