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

[libvirt] [PATCH v2 07/12] Implement keepalive protocol in remote driver



---
 src/remote/remote_driver.c |   30 +++++++++++++
 src/rpc/virnetclient.c     |  102 ++++++++++++++++++++++++++++++++++++++++++-
 src/rpc/virnetclient.h     |    5 ++
 3 files changed, 134 insertions(+), 3 deletions(-)

diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 1217d94..e218c40 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -4082,6 +4082,34 @@ done:
 }
 
 
+static int
+remoteAllowKeepAlive(virConnectPtr conn)
+{
+    struct private_data *priv = conn->privateData;
+    int ret = -1;
+
+    remoteDriverLock(priv);
+    ret = virNetClientKeepAliveAdvertise(priv->client);
+    remoteDriverUnlock(priv);
+
+    return ret;
+}
+
+
+static int
+remoteStartKeepAlive(virConnectPtr conn, int interval, unsigned int count)
+{
+    struct private_data *priv = conn->privateData;
+    int ret = -1;
+
+    remoteDriverLock(priv);
+    ret = virNetClientKeepAliveStart(priv->client, interval, count);
+    remoteDriverUnlock(priv);
+
+    return ret;
+}
+
+
 #include "remote_client_bodies.h"
 #include "qemu_client_bodies.h"
 
@@ -4430,6 +4458,8 @@ static virDriver remote_driver = {
     .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
     .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
     .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
+    .allowKeepAlive = remoteAllowKeepAlive, /* 0.9.7 */
+    .startKeepAlive = remoteStartKeepAlive, /* 0.9.7 */
 };
 
 static virNetworkDriver network_driver = {
diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 45b0edb..dee5059 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -29,6 +29,7 @@
 
 #include "virnetclient.h"
 #include "virnetsocket.h"
+#include "virkeepalive.h"
 #include "memory.h"
 #include "threads.h"
 #include "virfile.h"
@@ -93,11 +94,12 @@ struct _virNetClient {
     size_t nstreams;
     virNetClientStreamPtr *streams;
 
+    virKeepAlivePtr keepalive;
     bool wantClose;
 };
 
 
-void virNetClientRequestClose(virNetClientPtr client);
+static void virNetClientRequestClose(virNetClientPtr client);
 
 static int virNetClientSendInternal(virNetClientPtr client,
                                     virNetMessagePtr msg,
@@ -127,11 +129,71 @@ static void virNetClientEventFree(void *opaque)
     virNetClientFree(client);
 }
 
+int
+virNetClientKeepAliveAdvertise(virNetClientPtr client)
+{
+    virKeepAlivePtr ka;
+    int ret = -1;
+
+    virNetClientLock(client);
+    if ((ka = client->keepalive))
+        virKeepAliveRef(ka);
+    virNetClientUnlock(client);
+
+    if (ka) {
+        ret = virKeepAliveAdvertise(ka);
+        virKeepAliveFree(ka);
+    } else {
+        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
+                    _("the caller doesn't support keepalive protocol;"
+                      " perhaps it's missing event loop implementation"));
+    }
+
+    return ret;
+}
+
+int
+virNetClientKeepAliveStart(virNetClientPtr client,
+                           int interval,
+                           unsigned int count)
+{
+    int ret = -1;
+
+    virNetClientLock(client);
+
+    if (!client->keepalive) {
+        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
+                    _("the caller doesn't support keepalive protocol;"
+                      " perhaps it's missing event loop implementation"));
+        goto cleanup;
+    }
+
+    ret = virKeepAliveStart(client->keepalive, interval, count);
+
+cleanup:
+    virNetClientUnlock(client);
+    return ret;
+}
+
+static void
+virNetClientKeepAliveDeadCB(void *opaque)
+{
+    virNetClientRequestClose(opaque);
+}
+
+static int
+virNetClientKeepAliveSendCB(void *opaque,
+                            virNetMessagePtr msg)
+{
+    return virNetClientSendInternal(opaque, msg, false, true);
+}
+
 static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
                                        const char *hostname)
 {
     virNetClientPtr client = NULL;
     int wakeupFD[2] = { -1, -1 };
+    virKeepAlivePtr ka = NULL;
 
     if (pipe2(wakeupFD, O_CLOEXEC) < 0) {
         virReportSystemError(errno, "%s",
@@ -164,9 +226,21 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
                                   client,
                                   virNetClientEventFree) < 0) {
         client->refs--;
-        VIR_DEBUG("Failed to add event watch, disabling events");
+        VIR_DEBUG("Failed to add event watch, disabling events and support for"
+                  " keepalive messages");
+    } else {
+        /* Keepalive protocol consists of async messages so it can only be used
+         * if the client supports them */
+        if (!(ka = virKeepAliveNew(-1, 0, client,
+                                   virNetClientKeepAliveSendCB,
+                                   virNetClientKeepAliveDeadCB,
+                                   virNetClientEventFree)))
+            goto error;
+        /* keepalive object has a reference to client */
+        client->refs++;
     }
 
+    client->keepalive = ka;
     VIR_DEBUG("client=%p refs=%d", client, client->refs);
     return client;
 
@@ -175,6 +249,10 @@ no_memory:
 error:
     VIR_FORCE_CLOSE(wakeupFD[0]);
     VIR_FORCE_CLOSE(wakeupFD[1]);
+    if (ka) {
+        virKeepAliveStop(ka);
+        virKeepAliveFree(ka);
+    }
     virNetClientFree(client);
     return NULL;
 }
@@ -304,6 +382,8 @@ void virNetClientFree(virNetClientPtr client)
 static void
 virNetClientCloseLocked(virNetClientPtr client)
 {
+    virKeepAlivePtr ka;
+
     VIR_DEBUG("client=%p, sock=%p", client, client->sock);
 
     if (!client->sock)
@@ -318,7 +398,20 @@ virNetClientCloseLocked(virNetClientPtr client)
     virNetSASLSessionFree(client->sasl);
     client->sasl = NULL;
 #endif
+    ka = client->keepalive;
+    client->keepalive = NULL;
     client->wantClose = false;
+
+    if (ka) {
+        client->refs++;
+        virNetClientUnlock(client);
+
+        virKeepAliveStop(ka);
+        virKeepAliveFree(ka);
+
+        virNetClientLock(client);
+        client->refs--;
+    }
 }
 
 void virNetClientClose(virNetClientPtr client)
@@ -331,7 +424,7 @@ void virNetClientClose(virNetClientPtr client)
     virNetClientUnlock(client);
 }
 
-void
+static void
 virNetClientRequestClose(virNetClientPtr client)
 {
     VIR_DEBUG("client=%p", client);
@@ -723,6 +816,9 @@ virNetClientCallDispatch(virNetClientPtr client)
               client->msg.header.proc, client->msg.header.type,
               client->msg.header.status, client->msg.header.serial);
 
+    if (virKeepAliveCheckMessage(client->keepalive, &client->msg))
+        return 0;
+
     switch (client->msg.header.type) {
     case VIR_NET_REPLY: /* Normal RPC replies */
         return virNetClientCallDispatchReply(client);
diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h
index 1fabcfd..e337ee0 100644
--- a/src/rpc/virnetclient.h
+++ b/src/rpc/virnetclient.h
@@ -87,4 +87,9 @@ int virNetClientGetTLSKeySize(virNetClientPtr client);
 void virNetClientFree(virNetClientPtr client);
 void virNetClientClose(virNetClientPtr client);
 
+int virNetClientKeepAliveAdvertise(virNetClientPtr client);
+int virNetClientKeepAliveStart(virNetClientPtr client,
+                               int interval,
+                               unsigned int count);
+
 #endif /* __VIR_NET_CLIENT_H__ */
-- 
1.7.6.1


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