From patchwork Tue Jan 19 13:09:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 8062111 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BCA9ABEEE5 for ; Tue, 19 Jan 2016 13:29:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 05DB020211 for ; Tue, 19 Jan 2016 13:29:55 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 584AA201FE for ; Tue, 19 Jan 2016 13:29:54 +0000 (UTC) Received: from localhost ([::1]:36895 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLW5L-0003Rj-DC for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jan 2016 08:12:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39617) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLW2S-00075h-B1 for qemu-devel@nongnu.org; Tue, 19 Jan 2016 08:09:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLW2R-0005tU-2s for qemu-devel@nongnu.org; Tue, 19 Jan 2016 08:09:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44622) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLW2O-0005se-Lp; Tue, 19 Jan 2016 08:09:32 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 6829BA7561; Tue, 19 Jan 2016 13:09:32 +0000 (UTC) Received: from t530wlan.home.berrange.com.com (vpn1-6-80.ams2.redhat.com [10.36.6.80]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0JD9Qav012785; Tue, 19 Jan 2016 08:09:31 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 19 Jan 2016 13:09:13 +0000 Message-Id: <1453208963-16834-4-git-send-email-berrange@redhat.com> In-Reply-To: <1453208963-16834-1-git-send-email-berrange@redhat.com> References: <1453208963-16834-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Paolo Bonzini , qemu-block@nongnu.org Subject: [Qemu-devel] [PATCH v3 03/13] nbd: convert blockdev NBD server to use I/O channels for connection setup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This converts the blockdev NBD server to use the QIOChannelSocket class for initial listener socket setup and accepting of client connections. Actual I/O is still being performed against the socket file descriptor using the POSIX socket APIs. Signed-off-by: Daniel P. Berrange --- blockdev-nbd.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/blockdev-nbd.c b/blockdev-nbd.c index 4a758ac..94b653d 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -17,32 +17,48 @@ #include "qmp-commands.h" #include "trace.h" #include "block/nbd.h" -#include "qemu/sockets.h" +#include "io/channel-socket.h" -static int server_fd = -1; +static QIOChannelSocket *server_ioc; +static int server_watch = -1; -static void nbd_accept(void *opaque) +static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition, + gpointer opaque) { - struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + QIOChannelSocket *cioc; + int fd; - int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); + cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), + NULL); + if (!cioc) { + return TRUE; + } + + fd = dup(cioc->fd); if (fd >= 0) { nbd_client_new(NULL, fd, nbd_client_put); } + object_unref(OBJECT(cioc)); + return TRUE; } void qmp_nbd_server_start(SocketAddress *addr, Error **errp) { - if (server_fd != -1) { + if (server_ioc) { error_setg(errp, "NBD server already running"); return; } - server_fd = socket_listen(addr, errp); - if (server_fd != -1) { - qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL); + server_ioc = qio_channel_socket_new(); + if (qio_channel_socket_listen_sync(server_ioc, addr, errp) < 0) { + return; } + + server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc), + G_IO_IN, + nbd_accept, + NULL, + NULL); } /* @@ -77,7 +93,7 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, NBDExport *exp; NBDCloseNotifier *n; - if (server_fd == -1) { + if (!server_ioc) { error_setg(errp, "NBD server not running"); return; } @@ -127,9 +143,12 @@ void qmp_nbd_server_stop(Error **errp) nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp)); } - if (server_fd != -1) { - qemu_set_fd_handler(server_fd, NULL, NULL, NULL); - close(server_fd); - server_fd = -1; + if (server_watch != -1) { + g_source_remove(server_watch); + server_watch = -1; + } + if (server_ioc) { + object_unref(OBJECT(server_ioc)); + server_ioc = NULL; } }