From patchwork Mon Feb 29 23:19:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 8460351 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B61549F38C for ; Mon, 29 Feb 2016 23:29:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D72A320220 for ; Mon, 29 Feb 2016 23:29:07 +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 C97FC2010B for ; Mon, 29 Feb 2016 23:29:06 +0000 (UTC) Received: from localhost ([::1]:39737 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaXFS-00011w-7G for patchwork-qemu-devel@patchwork.kernel.org; Mon, 29 Feb 2016 18:29:06 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44740) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaX6i-0001yg-8n for qemu-devel@nongnu.org; Mon, 29 Feb 2016 18:20:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aaX6g-0006Fk-PW for qemu-devel@nongnu.org; Mon, 29 Feb 2016 18:20:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33051) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaX6d-0006DS-52; Mon, 29 Feb 2016 18:19:59 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id D30CA7F086; Mon, 29 Feb 2016 23:19:58 +0000 (UTC) Received: from localhost (ovpn-116-81.ams2.redhat.com [10.36.116.81]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1TNJv2G010365 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 29 Feb 2016 18:19:58 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Tue, 1 Mar 2016 00:19:27 +0100 Message-Id: <1456787973-19348-11-git-send-email-mreitz@redhat.com> In-Reply-To: <1456787973-19348-1-git-send-email-mreitz@redhat.com> References: <1456787973-19348-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Markus Armbruster , qemu-devel@nongnu.org, Max Reitz , Paolo Bonzini , Luiz Capitulino Subject: [Qemu-devel] [PATCH v2 10/16] block/nbd: Accept SocketAddress 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 Add a new option "address" to the NBD block driver which accepts a SocketAddress. "path", "host" and "port" are still supported as legacy options and are mapped to their corresponding SocketAddress representation. Signed-off-by: Max Reitz --- block/nbd.c | 93 +++++++++++++++++++++++++++---------------- tests/qemu-iotests/051.out | 4 +- tests/qemu-iotests/051.pc.out | 4 +- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 86907bc..213ba70 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -31,6 +31,8 @@ #include "qemu/uri.h" #include "block/block_int.h" #include "qemu/module.h" +#include "qapi-visit.h" +#include "qapi/qmp-input-visitor.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qjson.h" #include "qapi/qmp/qint.h" @@ -127,7 +129,9 @@ static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) if (!strcmp(e->key, "host") || !strcmp(e->key, "port") || !strcmp(e->key, "path") - || !strcmp(e->key, "export")) + || !strcmp(e->key, "export") + || !strcmp(e->key, "address") + || !strncmp(e->key, "address.", 8)) { error_setg(errp, "Option '%s' cannot be used with a file name", e->key); @@ -201,44 +205,64 @@ out: g_free(file); } +static bool nbd_process_legacy_socket_options(QDict *options, Error **errp) +{ + if (qdict_haskey(options, "path") && qdict_haskey(options, "host")) { + error_setg(errp, "path and host may not be used at the same time"); + return false; + } else if (qdict_haskey(options, "path")) { + if (qdict_haskey(options, "port")) { + error_setg(errp, "port may not be used without host"); + return false; + } + + qdict_put(options, "address.type", qstring_from_str("unix")); + qdict_change_key(options, "path", "address.data.path"); + } else if (qdict_haskey(options, "host")) { + qdict_put(options, "address.type", qstring_from_str("inet")); + qdict_change_key(options, "host", "address.data.host"); + if (!qdict_change_key(options, "port", "address.data.port")) { + qdict_put(options, "address.data.port", + qstring_from_str(stringify(NBD_DEFAULT_PORT))); + } + } + + return true; +} + static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export, Error **errp) { - SocketAddress *saddr; + SocketAddress *saddr = NULL; + QDict *addr = NULL; + QmpInputVisitor *iv; + Error *local_err = NULL; - if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) { - if (qdict_haskey(options, "path")) { - error_setg(errp, "path and host may not be used at the same time"); - } else { - error_setg(errp, "one of path and host must be specified"); - } - return NULL; + if (!nbd_process_legacy_socket_options(options, errp)) { + goto fail; } - if (qdict_haskey(options, "port") && !qdict_haskey(options, "host")) { - error_setg(errp, "port may not be used without host"); - return NULL; + + qdict_extract_subqdict(options, &addr, "address."); + if (!qdict_unflatten(addr, errp)) { + goto fail; } - saddr = g_new0(SocketAddress, 1); + if (!qdict_size(addr)) { + error_setg(errp, "NBD server address missing"); + goto fail; + } - if (qdict_haskey(options, "path")) { - saddr->type = SOCKET_ADDRESS_KIND_UNIX; - saddr->u.q_unix = g_new0(UnixSocketAddress, 1); - saddr->u.q_unix->path = g_strdup(qdict_get_str(options, "path")); - qdict_del(options, "path"); - } else { - saddr->type = SOCKET_ADDRESS_KIND_INET; - saddr->u.inet = g_new0(InetSocketAddress, 1); - saddr->u.inet->host = g_strdup(qdict_get_str(options, "host")); - if (!qdict_get_try_str(options, "port")) { - saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); - } else { - saddr->u.inet->port = g_strdup(qdict_get_str(options, "port")); - } - qdict_del(options, "host"); - qdict_del(options, "port"); + iv = qmp_input_visitor_new(QOBJECT(addr)); + visit_type_SocketAddress(qmp_input_get_visitor(iv), NULL, &saddr, + &local_err); + qmp_input_visitor_cleanup(iv); + if (local_err) { + error_propagate(errp, local_err); + goto fail; } + /* TODO: Detect superfluous (unused) options under in addr */ + s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; *export = g_strdup(qdict_get_try_str(options, "export")); @@ -246,7 +270,12 @@ static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export, qdict_del(options, "export"); } + QDECREF(addr); return saddr; + +fail: + QDECREF(addr); + return NULL; } NbdClientSession *nbd_get_client_session(BlockDriverState *bs) @@ -425,9 +454,6 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) const char *tlscreds = qdict_get_try_str(options, "tls-creds"); if (qdict_get_try_str(options, "address.type")) { - /* This path will only be possible as of a future patch; - * TODO: Remove this note once it is */ - const char *type = qdict_get_str(options, "address.type"); if (!strcmp(type, "unix")) { @@ -471,9 +497,6 @@ static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) } if (qdict_get_try_str(options, "address.type")) { - /* This path will only be possible as of a future patch; - * TODO: Remove this note once it is */ - const QDictEntry *e; for (e = qdict_first(options); e; e = qdict_next(options, e)) { if (!strncmp(e->key, "address.", 8)) { diff --git a/tests/qemu-iotests/051.out b/tests/qemu-iotests/051.out index 5166bea..82c1694 100644 --- a/tests/qemu-iotests/051.out +++ b/tests/qemu-iotests/051.out @@ -222,7 +222,7 @@ Testing: -drive driver=file QEMU_PROG: -drive driver=file: The 'file' block driver requires a file name Testing: -drive driver=nbd -QEMU_PROG: -drive driver=nbd: one of path and host must be specified +QEMU_PROG: -drive driver=nbd: NBD server address missing Testing: -drive driver=raw QEMU_PROG: -drive driver=raw: Can't use 'raw' as a block driver for the protocol level @@ -231,7 +231,7 @@ Testing: -drive file.driver=file QEMU_PROG: -drive file.driver=file: The 'file' block driver requires a file name Testing: -drive file.driver=nbd -QEMU_PROG: -drive file.driver=nbd: one of path and host must be specified +QEMU_PROG: -drive file.driver=nbd: NBD server address missing Testing: -drive file.driver=raw QEMU_PROG: -drive file.driver=raw: Can't use 'raw' as a block driver for the protocol level diff --git a/tests/qemu-iotests/051.pc.out b/tests/qemu-iotests/051.pc.out index d0bbfcb..45137b7 100644 --- a/tests/qemu-iotests/051.pc.out +++ b/tests/qemu-iotests/051.pc.out @@ -316,7 +316,7 @@ Testing: -drive driver=file QEMU_PROG: -drive driver=file: The 'file' block driver requires a file name Testing: -drive driver=nbd -QEMU_PROG: -drive driver=nbd: one of path and host must be specified +QEMU_PROG: -drive driver=nbd: NBD server address missing Testing: -drive driver=raw QEMU_PROG: -drive driver=raw: Can't use 'raw' as a block driver for the protocol level @@ -325,7 +325,7 @@ Testing: -drive file.driver=file QEMU_PROG: -drive file.driver=file: The 'file' block driver requires a file name Testing: -drive file.driver=nbd -QEMU_PROG: -drive file.driver=nbd: one of path and host must be specified +QEMU_PROG: -drive file.driver=nbd: NBD server address missing Testing: -drive file.driver=raw QEMU_PROG: -drive file.driver=raw: Can't use 'raw' as a block driver for the protocol level