From patchwork Sat Apr 22 08:25:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 9694275 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 16B12600CA for ; Sat, 22 Apr 2017 08:36:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C63E285FB for ; Sat, 22 Apr 2017 08:36:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F406E28614; Sat, 22 Apr 2017 08:36:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7B18B285FB for ; Sat, 22 Apr 2017 08:36:56 +0000 (UTC) Received: from localhost ([::1]:34730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1qXH-0000QO-Nb for patchwork-qemu-devel@patchwork.kernel.org; Sat, 22 Apr 2017 04:36:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59875) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1qNZ-0001qj-TD for qemu-devel@nongnu.org; Sat, 22 Apr 2017 04:26:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1qNW-0000jo-PZ for qemu-devel@nongnu.org; Sat, 22 Apr 2017 04:26:53 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3402 helo=dggrg03-dlp.huawei.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1d1qNV-0000jG-T2 for qemu-devel@nongnu.org; Sat, 22 Apr 2017 04:26:50 -0400 Received: from 172.30.72.57 (EHLO DGGEML403-HUB.china.huawei.com) ([172.30.72.57]) by dggrg03-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id AMH71123; Sat, 22 Apr 2017 16:26:27 +0800 (CST) Received: from localhost (10.177.24.212) by DGGEML403-HUB.china.huawei.com (10.3.17.33) with Microsoft SMTP Server id 14.3.301.0; Sat, 22 Apr 2017 16:26:16 +0800 From: zhanghailiang To: , Date: Sat, 22 Apr 2017 16:25:41 +0800 Message-ID: <1492849558-17540-2-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 2.7.2.windows.1 In-Reply-To: <1492849558-17540-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1492849558-17540-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090201.58FB13B4.0008, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 339b3fca4e9c846839fa64341d32c787 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.189 Subject: [Qemu-devel] [PATCH v2 01/18] net/colo: Add notifier/callback related helpers for filter X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lizhijian@cn.fujitsu.com, xiecl.fnst@cn.fujitsu.com, zhanghailiang , zhangchen.fnst@cn.fujitsu.com, quintela@redhat.com, Jason Wang Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP We will use this notifier to help COLO to notify filter object to do something, like do checkpoint, or process failover event. Cc: Jason Wang Signed-off-by: zhanghailiang Signed-off-by: Zhang Chen Signed-off-by: Li Zhijian --- net/colo.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/colo.h | 19 +++++++++++ 2 files changed, 124 insertions(+) diff --git a/net/colo.c b/net/colo.c index 8cc166b..8aef670 100644 --- a/net/colo.c +++ b/net/colo.c @@ -15,6 +15,7 @@ #include "qemu/osdep.h" #include "trace.h" #include "net/colo.h" +#include "qapi/error.h" uint32_t connection_key_hash(const void *opaque) { @@ -209,3 +210,107 @@ Connection *connection_get(GHashTable *connection_track_table, return conn; } + +static gboolean +filter_notify_prepare(GSource *source, gint *timeout) +{ + *timeout = -1; + + return FALSE; +} + +static gboolean +filter_notify_check(GSource *source) +{ + FilterNotifier *notify = (FilterNotifier *)source; + + return notify->pfd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR); +} + +static gboolean +filter_notify_dispatch(GSource *source, + GSourceFunc callback, + gpointer user_data) +{ + FilterNotifier *notify = (FilterNotifier *)source; + int revents; + uint64_t value; + int ret; + + revents = notify->pfd.revents & notify->pfd.events; + if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) { + ret = filter_notifier_get(notify, &value); + if (notify->cb && !ret) { + notify->cb(notify, value); + } + } + return TRUE; +} + +static void +filter_notify_finalize(GSource *source) +{ + FilterNotifier *notify = (FilterNotifier *)source; + + event_notifier_cleanup(¬ify->event); +} + +static GSourceFuncs notifier_source_funcs = { + filter_notify_prepare, + filter_notify_check, + filter_notify_dispatch, + filter_notify_finalize, +}; + +FilterNotifier *filter_notifier_new(FilterNotifierCallback *cb, + void *opaque, Error **errp) +{ + FilterNotifier *notify; + int ret; + + notify = (FilterNotifier *)g_source_new(¬ifier_source_funcs, + sizeof(FilterNotifier)); + ret = event_notifier_init(¬ify->event, false); + if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to initialize event notifier"); + goto fail; + } + notify->pfd.fd = event_notifier_get_fd(¬ify->event); + notify->pfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR; + notify->cb = cb; + notify->opaque = opaque; + g_source_add_poll(¬ify->source, ¬ify->pfd); + + return notify; + +fail: + g_source_destroy(¬ify->source); + return NULL; +} + +int filter_notifier_set(FilterNotifier *notify, uint64_t value) +{ + ssize_t ret; + + do { + ret = write(notify->event.wfd, &value, sizeof(value)); + } while (ret < 0 && errno == EINTR); + + /* EAGAIN is fine, a read must be pending. */ + if (ret < 0 && errno != EAGAIN) { + return -errno; + } + return 0; +} + +int filter_notifier_get(FilterNotifier *notify, uint64_t *value) +{ + ssize_t len; + + /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ + do { + len = read(notify->event.rfd, value, sizeof(*value)); + } while ((len == -1 && errno == EINTR)); + + return len != sizeof(*value) ? -1 : 0; +} diff --git a/net/colo.h b/net/colo.h index cd9027f..b586db3 100644 --- a/net/colo.h +++ b/net/colo.h @@ -19,6 +19,7 @@ #include "qemu/jhash.h" #include "qemu/timer.h" #include "slirp/tcp.h" +#include "qemu/event_notifier.h" #define HASHTABLE_MAX_SIZE 16384 @@ -89,4 +90,22 @@ void connection_hashtable_reset(GHashTable *connection_track_table); Packet *packet_new(const void *data, int size); void packet_destroy(void *opaque, void *user_data); +typedef void FilterNotifierCallback(void *opaque, int value); +typedef struct FilterNotifier { + GSource source; + EventNotifier event; + GPollFD pfd; + FilterNotifierCallback *cb; + void *opaque; +} FilterNotifier; + +FilterNotifier *filter_notifier_new(FilterNotifierCallback *cb, + void *opaque, Error **errp); +int filter_notifier_set(FilterNotifier *notify, uint64_t value); +int filter_notifier_get(FilterNotifier *notify, uint64_t *value); + +enum { + COLO_CHECKPOINT = 2, + COLO_FAILOVER, +}; #endif /* QEMU_COLO_PROXY_H */