2011-09-22 11:40:00 +00:00
|
|
|
/*
|
|
|
|
* virkeepalive.c: keepalive handling
|
|
|
|
*
|
2013-01-30 11:47:19 +00:00
|
|
|
* Copyright (C) 2011-2013 Red Hat, Inc.
|
2011-09-22 11:40:00 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-09-22 11:40:00 +00:00
|
|
|
*
|
|
|
|
* Author: Jiri Denemark <jdenemar@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2011-09-22 11:40:00 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2011-09-22 11:40:00 +00:00
|
|
|
#include "virnetsocket.h"
|
|
|
|
#include "virkeepaliveprotocol.h"
|
|
|
|
#include "virkeepalive.h"
|
2014-02-27 13:41:11 +00:00
|
|
|
#include "virprobe.h"
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("rpc.keepalive");
|
|
|
|
|
2011-09-22 11:40:00 +00:00
|
|
|
struct _virKeepAlive {
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLockable parent;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
int interval;
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int countToDeath;
|
|
|
|
time_t lastPacketReceived;
|
2012-06-12 21:42:32 +00:00
|
|
|
time_t intervalStart;
|
2011-09-22 11:40:00 +00:00
|
|
|
int timer;
|
|
|
|
|
|
|
|
virKeepAliveSendFunc sendCB;
|
|
|
|
virKeepAliveDeadFunc deadCB;
|
|
|
|
virKeepAliveFreeFunc freeCB;
|
|
|
|
void *client;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-11 13:35:50 +00:00
|
|
|
static virClassPtr virKeepAliveClass;
|
|
|
|
static void virKeepAliveDispose(void *obj);
|
|
|
|
|
|
|
|
static int virKeepAliveOnceInit(void)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
if (!(virKeepAliveClass = virClassNew(virClassForObjectLockable(),
|
2013-01-09 17:37:27 +00:00
|
|
|
"virKeepAlive",
|
2012-07-11 13:35:50 +00:00
|
|
|
sizeof(virKeepAlive),
|
|
|
|
virKeepAliveDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virKeepAlive)
|
|
|
|
|
2011-09-22 11:40:00 +00:00
|
|
|
static virNetMessagePtr
|
2012-06-12 07:01:49 +00:00
|
|
|
virKeepAliveMessage(virKeepAlivePtr ka, int proc)
|
2011-09-22 11:40:00 +00:00
|
|
|
{
|
|
|
|
virNetMessagePtr msg;
|
2012-06-12 07:01:49 +00:00
|
|
|
const char *procstr = NULL;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
switch (proc) {
|
|
|
|
case KEEPALIVE_PROC_PING:
|
|
|
|
procstr = "request";
|
|
|
|
break;
|
|
|
|
case KEEPALIVE_PROC_PONG:
|
|
|
|
procstr = "response";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VIR_WARN("Refusing to send unknown keepalive message: %d", proc);
|
2011-09-22 11:40:00 +00:00
|
|
|
return NULL;
|
2012-06-12 07:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(msg = virNetMessageNew(false)))
|
|
|
|
goto error;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
msg->header.prog = KEEPALIVE_PROGRAM;
|
|
|
|
msg->header.vers = KEEPALIVE_PROTOCOL_VERSION;
|
|
|
|
msg->header.type = VIR_NET_MESSAGE;
|
|
|
|
msg->header.proc = proc;
|
|
|
|
|
|
|
|
if (virNetMessageEncodeHeader(msg) < 0 ||
|
|
|
|
virNetMessageEncodePayloadEmpty(msg) < 0) {
|
|
|
|
virNetMessageFree(msg);
|
2012-06-12 07:01:49 +00:00
|
|
|
goto error;
|
2011-09-22 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
VIR_DEBUG("Sending keepalive %s to client %p", procstr, ka->client);
|
2011-09-22 11:40:00 +00:00
|
|
|
PROBE(RPC_KEEPALIVE_SEND,
|
|
|
|
"ka=%p client=%p prog=%d vers=%d proc=%d",
|
|
|
|
ka, ka->client, msg->header.prog, msg->header.vers, msg->header.proc);
|
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
return msg;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2012-06-12 07:01:49 +00:00
|
|
|
VIR_WARN("Failed to generate keepalive %s", procstr);
|
|
|
|
return NULL;
|
2011-09-22 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 21:41:25 +00:00
|
|
|
static bool
|
|
|
|
virKeepAliveTimerInternal(virKeepAlivePtr ka,
|
|
|
|
virNetMessagePtr *msg)
|
2011-09-22 11:40:00 +00:00
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
2013-01-30 11:47:19 +00:00
|
|
|
int timeval;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2012-06-12 21:42:32 +00:00
|
|
|
if (ka->interval <= 0 || ka->intervalStart == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (now - ka->intervalStart < ka->interval) {
|
2013-01-30 11:47:19 +00:00
|
|
|
timeval = ka->interval - (now - ka->intervalStart);
|
|
|
|
virEventUpdateTimeout(ka->timer, timeval * 1000);
|
2012-06-12 21:41:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2013-01-30 11:47:19 +00:00
|
|
|
timeval = now - ka->lastPacketReceived;
|
2011-09-22 11:40:00 +00:00
|
|
|
PROBE(RPC_KEEPALIVE_TIMEOUT,
|
|
|
|
"ka=%p client=%p countToDeath=%d idle=%d",
|
2013-01-30 11:47:19 +00:00
|
|
|
ka, ka->client, ka->countToDeath, timeval);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
if (ka->countToDeath == 0) {
|
2015-09-11 15:07:56 +00:00
|
|
|
VIR_DEBUG("No response from client %p after %d keepalive messages "
|
|
|
|
"in %d seconds",
|
|
|
|
ka->client, ka->count, timeval);
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("connection closed due to keepalive timeout"));
|
2012-06-12 21:41:25 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
ka->countToDeath--;
|
2012-06-12 21:42:32 +00:00
|
|
|
ka->intervalStart = now;
|
2012-06-12 07:01:49 +00:00
|
|
|
*msg = virKeepAliveMessage(ka, KEEPALIVE_PROC_PING);
|
2012-06-12 21:41:25 +00:00
|
|
|
virEventUpdateTimeout(ka->timer, ka->interval * 1000);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virKeepAliveTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
|
|
|
|
{
|
|
|
|
virKeepAlivePtr ka = opaque;
|
|
|
|
virNetMessagePtr msg = NULL;
|
|
|
|
bool dead;
|
2012-06-12 07:01:49 +00:00
|
|
|
void *client;
|
2012-06-12 21:41:25 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2012-06-12 21:41:25 +00:00
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
client = ka->client;
|
2012-06-12 21:41:25 +00:00
|
|
|
dead = virKeepAliveTimerInternal(ka, &msg);
|
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
if (!dead && !msg)
|
|
|
|
goto cleanup;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2012-07-11 13:35:50 +00:00
|
|
|
virObjectRef(ka);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
if (dead) {
|
|
|
|
ka->deadCB(client);
|
|
|
|
} else if (ka->sendCB(client, msg) < 0) {
|
|
|
|
VIR_WARN("Failed to send keepalive request to client %p", client);
|
|
|
|
virNetMessageFree(msg);
|
2011-09-22 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2012-07-11 13:35:50 +00:00
|
|
|
virObjectUnref(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
cleanup:
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virKeepAlivePtr
|
|
|
|
virKeepAliveNew(int interval,
|
|
|
|
unsigned int count,
|
|
|
|
void *client,
|
|
|
|
virKeepAliveSendFunc sendCB,
|
|
|
|
virKeepAliveDeadFunc deadCB,
|
|
|
|
virKeepAliveFreeFunc freeCB)
|
|
|
|
{
|
|
|
|
virKeepAlivePtr ka;
|
|
|
|
|
|
|
|
VIR_DEBUG("client=%p, interval=%d, count=%u", client, interval, count);
|
|
|
|
|
2012-07-11 13:35:50 +00:00
|
|
|
if (virKeepAliveInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
if (!(ka = virObjectLockableNew(virKeepAliveClass)))
|
2011-09-22 11:40:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ka->interval = interval;
|
|
|
|
ka->count = count;
|
|
|
|
ka->countToDeath = count;
|
|
|
|
ka->timer = -1;
|
|
|
|
ka->client = client;
|
|
|
|
ka->sendCB = sendCB;
|
|
|
|
ka->deadCB = deadCB;
|
|
|
|
ka->freeCB = freeCB;
|
|
|
|
|
|
|
|
PROBE(RPC_KEEPALIVE_NEW,
|
2012-07-11 13:35:50 +00:00
|
|
|
"ka=%p client=%p",
|
|
|
|
ka, ka->client);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
return ka;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-07-11 13:35:50 +00:00
|
|
|
virKeepAliveDispose(void *obj)
|
2011-09-22 11:40:00 +00:00
|
|
|
{
|
2012-07-11 13:35:50 +00:00
|
|
|
virKeepAlivePtr ka = obj;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2013-03-13 19:17:32 +00:00
|
|
|
PROBE(RPC_KEEPALIVE_DISPOSE,
|
|
|
|
"ka=%p", ka);
|
|
|
|
|
2011-09-22 11:40:00 +00:00
|
|
|
ka->freeCB(ka->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virKeepAliveStart(virKeepAlivePtr ka,
|
|
|
|
int interval,
|
|
|
|
unsigned int count)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
time_t delay;
|
|
|
|
int timeout;
|
2012-06-12 21:42:32 +00:00
|
|
|
time_t now;
|
2011-09-22 11:40:00 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
if (ka->timer >= 0) {
|
|
|
|
VIR_DEBUG("Keepalive messages already enabled");
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interval > 0) {
|
|
|
|
if (ka->interval > 0) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("keepalive interval already set"));
|
2011-09-22 11:40:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-02-05 22:58:25 +00:00
|
|
|
/* Guard against overflow */
|
|
|
|
if (interval > INT_MAX / 1000) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("keepalive interval %d too large"), interval);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-09-22 11:40:00 +00:00
|
|
|
ka->interval = interval;
|
|
|
|
ka->count = count;
|
|
|
|
ka->countToDeath = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ka->interval <= 0) {
|
|
|
|
VIR_DEBUG("Keepalive messages disabled by configuration");
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROBE(RPC_KEEPALIVE_START,
|
|
|
|
"ka=%p client=%p interval=%d count=%u",
|
|
|
|
ka, ka->client, interval, count);
|
|
|
|
|
2012-06-12 21:42:32 +00:00
|
|
|
now = time(NULL);
|
|
|
|
delay = now - ka->lastPacketReceived;
|
2011-09-22 11:40:00 +00:00
|
|
|
if (delay > ka->interval)
|
|
|
|
timeout = 0;
|
|
|
|
else
|
|
|
|
timeout = ka->interval - delay;
|
2012-06-12 21:42:32 +00:00
|
|
|
ka->intervalStart = now - (ka->interval - timeout);
|
2011-09-22 11:40:00 +00:00
|
|
|
ka->timer = virEventAddTimeout(timeout * 1000, virKeepAliveTimer,
|
2012-07-11 13:35:50 +00:00
|
|
|
ka, virObjectFreeCallback);
|
2011-09-22 11:40:00 +00:00
|
|
|
if (ka->timer < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* the timer now has another reference to this object */
|
2012-07-11 13:35:50 +00:00
|
|
|
virObjectRef(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
cleanup:
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 06:58:04 +00:00
|
|
|
void
|
|
|
|
virKeepAliveStop(virKeepAlivePtr ka)
|
2011-09-22 11:40:00 +00:00
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
PROBE(RPC_KEEPALIVE_STOP,
|
2012-06-12 06:58:04 +00:00
|
|
|
"ka=%p client=%p",
|
|
|
|
ka, ka->client);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
if (ka->timer > 0) {
|
|
|
|
virEventRemoveTimeout(ka->timer);
|
|
|
|
ka->timer = -1;
|
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 21:42:32 +00:00
|
|
|
int
|
|
|
|
virKeepAliveTimeout(virKeepAlivePtr ka)
|
|
|
|
{
|
|
|
|
int timeout;
|
|
|
|
|
|
|
|
if (!ka)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2012-06-12 21:42:32 +00:00
|
|
|
|
|
|
|
if (ka->interval <= 0 || ka->intervalStart == 0) {
|
|
|
|
timeout = -1;
|
|
|
|
} else {
|
|
|
|
timeout = ka->interval - (time(NULL) - ka->intervalStart);
|
|
|
|
if (timeout < 0)
|
|
|
|
timeout = 0;
|
2013-02-05 22:58:25 +00:00
|
|
|
/* Guard against overflow */
|
|
|
|
if (timeout > INT_MAX / 1000)
|
|
|
|
timeout = INT_MAX / 1000;
|
2012-06-12 21:42:32 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2012-06-12 21:42:32 +00:00
|
|
|
|
|
|
|
if (timeout < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return timeout * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
virKeepAliveTrigger(virKeepAlivePtr ka,
|
|
|
|
virNetMessagePtr *msg)
|
|
|
|
{
|
|
|
|
bool dead;
|
|
|
|
|
|
|
|
*msg = NULL;
|
|
|
|
if (!ka)
|
|
|
|
return false;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2012-06-12 21:42:32 +00:00
|
|
|
dead = virKeepAliveTimerInternal(ka, msg);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2012-06-12 21:42:32 +00:00
|
|
|
|
|
|
|
return dead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 11:40:00 +00:00
|
|
|
bool
|
|
|
|
virKeepAliveCheckMessage(virKeepAlivePtr ka,
|
2012-06-12 07:01:49 +00:00
|
|
|
virNetMessagePtr msg,
|
|
|
|
virNetMessagePtr *response)
|
2011-09-22 11:40:00 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
VIR_DEBUG("ka=%p, client=%p, msg=%p",
|
|
|
|
ka, ka ? ka->client : "(null)", msg);
|
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
*response = NULL;
|
2011-09-22 11:40:00 +00:00
|
|
|
if (!ka)
|
|
|
|
return false;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
ka->countToDeath = ka->count;
|
2012-06-12 21:42:32 +00:00
|
|
|
ka->lastPacketReceived = ka->intervalStart = time(NULL);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
if (msg->header.prog == KEEPALIVE_PROGRAM &&
|
|
|
|
msg->header.vers == KEEPALIVE_PROTOCOL_VERSION &&
|
|
|
|
msg->header.type == VIR_NET_MESSAGE) {
|
|
|
|
PROBE(RPC_KEEPALIVE_RECEIVED,
|
|
|
|
"ka=%p client=%p prog=%d vers=%d proc=%d",
|
|
|
|
ka, ka->client, msg->header.prog,
|
|
|
|
msg->header.vers, msg->header.proc);
|
|
|
|
ret = true;
|
|
|
|
switch (msg->header.proc) {
|
|
|
|
case KEEPALIVE_PROC_PING:
|
|
|
|
VIR_DEBUG("Got keepalive request from client %p", ka->client);
|
2012-06-12 07:01:49 +00:00
|
|
|
*response = virKeepAliveMessage(ka, KEEPALIVE_PROC_PONG);
|
2011-09-22 11:40:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEEPALIVE_PROC_PONG:
|
|
|
|
VIR_DEBUG("Got keepalive response from client %p", ka->client);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
VIR_DEBUG("Ignoring unknown keepalive message %d from client %p",
|
|
|
|
msg->header.proc, ka->client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ka->timer >= 0)
|
|
|
|
virEventUpdateTimeout(ka->timer, ka->interval * 1000);
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(ka);
|
2011-09-22 11:40:00 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|