From patchwork Tue Jan 19 16:10:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8063761 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 BE0AF9F8AA for ; Tue, 19 Jan 2016 16:21:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1280720452 for ; Tue, 19 Jan 2016 16:21:01 +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 408FF2045E for ; Tue, 19 Jan 2016 16:21:00 +0000 (UTC) Received: from localhost ([::1]:37855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLZ1f-0005oN-F1 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jan 2016 11:20:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42527) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYs9-0004tP-Tl for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:11:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLYs5-00027f-Ar for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:11:09 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53823) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYs5-000271-3W for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:11:05 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id D4318357E90; Tue, 19 Jan 2016 16:11:04 +0000 (UTC) Received: from red.redhat.com (ovpn-113-211.phx2.redhat.com [10.3.113.211]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0JGAlYb008625; Tue, 19 Jan 2016 11:11:04 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 19 Jan 2016 09:10:31 -0700 Message-Id: <1453219845-30939-24-git-send-email-eblake@redhat.com> In-Reply-To: <1453219845-30939-1-git-send-email-eblake@redhat.com> References: <1453219845-30939-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: marcandre.lureau@redhat.com, armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v9 23/37] qmp: Support explicit null during input visit 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 Implement the new type_null() callback for the qmp input visitor. While we don't yet have a use for this in qapi (the generator will need some tweaks first), one usage is already envisioned: when changing blockdev parameters, it would be nice to have a difference between leaving a tuning parameter unchanged (omit that parameter from the struct) and to explicitly reset the parameter to its default without having to know what the default value is (specify the parameter with an explicit null value, which will require us to allow a qapi alternate that chooses between the normal value and an explicit null). At any rate, we can test this without the use of generated qapi by manually using visit_start_struct()/visit_end_struct(). Signed-off-by: Eric Blake --- v9: new patch --- include/qapi/visitor-impl.h | 2 +- qapi/qmp-input-visitor.c | 14 ++++++++++++++ tests/test-qmp-input-visitor.c | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index 8705136..95408a5 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -76,7 +76,7 @@ struct Visitor void (*type_any)(Visitor *v, const char *name, QObject **obj, Error **errp); /* Must be provided to visit explicit null values (right now, only the - * dealloc visitor supports this). */ + * dealloc and qmp-input visitors support this). */ void (*type_null)(Visitor *v, const char *name, Error **errp); /* May be NULL; most useful for input visitors. */ diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 597652c..ad23bec 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -315,6 +315,19 @@ static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj, *obj = qobj; } +static void qmp_input_type_null(Visitor *v, const char *name, Error **errp) +{ + QmpInputVisitor *qiv = to_qiv(v); + QObject *qobj = qmp_input_get_object(qiv, name, true); + + if (qobject_type(qobj) == QTYPE_QNULL) { + return; + } + + error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", + "null"); +} + static void qmp_input_optional(Visitor *v, const char *name, bool *present) { QmpInputVisitor *qiv = to_qiv(v); @@ -358,6 +371,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj) v->visitor.type_str = qmp_input_type_str; v->visitor.type_number = qmp_input_type_number; v->visitor.type_any = qmp_input_type_any; + v->visitor.type_null = qmp_input_type_null; v->visitor.optional = qmp_input_optional; v->visitor.get_next_type = qmp_input_get_next_type; diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c index f6bd408..6489e4a 100644 --- a/tests/test-qmp-input-visitor.c +++ b/tests/test-qmp-input-visitor.c @@ -278,6 +278,30 @@ static void test_visitor_in_any(TestInputVisitorData *data, qobject_decref(res); } +static void test_visitor_in_null(TestInputVisitorData *data, + const void *unused) +{ + Visitor *v; + QObject *null; + + v = visitor_input_test_init(data, "null"); + visit_type_null(v, NULL, &error_abort); + + v = visitor_input_test_init(data, "{ 'a': null }"); + visit_start_struct(v, NULL, NULL, 0, &error_abort); + visit_type_null(v, "a", &error_abort); + visit_end_struct(v, &error_abort); + + /* Check that qnull reference counting is sane: + * 1 for global use, 1 for our qnull() use, and 1 still owned by 'v' + * until it is torn down */ + null = qnull(); + g_assert(null->refcnt == 3); + visitor_input_teardown(data, NULL); + g_assert(null->refcnt == 2); + qobject_decref(null); +} + static void test_visitor_in_union_flat(TestInputVisitorData *data, const void *unused) { @@ -792,6 +816,8 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_list); input_visitor_test_add("/visitor/input/any", &in_visitor_data, test_visitor_in_any); + input_visitor_test_add("/visitor/input/null", + &in_visitor_data, test_visitor_in_null); input_visitor_test_add("/visitor/input/union-flat", &in_visitor_data, test_visitor_in_union_flat); input_visitor_test_add("/visitor/input/alternate",