From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 92FE4C433EF for ; Wed, 22 Jun 2022 11:49:41 +0000 (UTC) Received: from localhost ([::1]:35792 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3yrc-0002Nd-IQ for qemu-devel@archiver.kernel.org; Wed, 22 Jun 2022 07:49:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35820) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3ypo-0000fH-5x for qemu-devel@nongnu.org; Wed, 22 Jun 2022 07:47:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:23587) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3ypk-0007cq-Va for qemu-devel@nongnu.org; Wed, 22 Jun 2022 07:47:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655898463; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jlFe2q3ftxTarDNb4Pq5wFaEsY6doNu1E/eP48D+MSg=; b=CVcSxORA6NyAeWjuF9GV/4wuXkECXhhmwQQPrZUux+DnogbD2jMCdVQfHoesKX+RM5XY4x 5nJ7do9Totb0BxdakVL3PNJgaFM7a3jg3sZi5sMQBnoL9k07q/3mSATsr7qvE3fm7fImVS ZXwTTWGr13s1y8PNqY7WY+uX8hBGujw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-85-curqY8fUNfiyGVGRbRebFw-1; Wed, 22 Jun 2022 07:47:40 -0400 X-MC-Unique: curqY8fUNfiyGVGRbRebFw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C3AAA811E7A for ; Wed, 22 Jun 2022 11:47:39 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.39.195.112]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 817584619F5; Wed, 22 Jun 2022 11:47:39 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 4C4EE21E690D; Wed, 22 Jun 2022 13:47:38 +0200 (CEST) From: Markus Armbruster To: Laurent Vivier Cc: qemu-devel@nongnu.org, Eric Blake , "Dr. David Alan Gilbert" , Paolo Bonzini , Daniel P. =?utf-8?Q?Berrang=C3=A9?= , Jason Wang , Stefano Brivio Subject: Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs References: <20220620101828.518865-1-lvivier@redhat.com> <20220620101828.518865-5-lvivier@redhat.com> <874k0fz7gg.fsf@pond.sub.org> <7eb9f5a3-5166-ee8d-86f8-1d05770331f6@redhat.com> <87tu8ev1ta.fsf@pond.sub.org> Date: Wed, 22 Jun 2022 13:47:38 +0200 In-Reply-To: (Laurent Vivier's message of "Tue, 21 Jun 2022 21:27:22 +0200") Message-ID: <87fsjwncmd.fsf@pond.sub.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Received-SPF: pass client-ip=170.10.129.124; envelope-from=armbru@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Laurent Vivier writes: > On 21/06/2022 10:49, Markus Armbruster wrote: >> Laurent Vivier writes: >> >>> On 20/06/2022 17:21, Markus Armbruster wrote: >>>> Laurent Vivier writes: >>>> >>>>> Copied from socket netdev file and modified to use SocketAddress >>>>> to be able to introduce new features like unix socket. >>>>> >>>>> "udp" and "mcast" are squashed into dgram netdev, multicast is detected >>>>> according to the IP address type. >>>>> "listen" and "connect" modes are managed by stream netdev. An optional >>>>> parameter "server" defines the mode (server by default) >>>>> >>>>> Signed-off-by: Laurent Vivier >>>>> Reviewed-by: Stefano Brivio >>>>> --- >> >> [...] >> >>>>> diff --git a/net/net.c b/net/net.c >>>>> index c337d3d753fe..440957b272ee 100644 >>>>> --- a/net/net.c >>>>> +++ b/net/net.c >>> ... >>>>> @@ -1612,7 +1617,19 @@ void net_init_clients(void) >>>>> */ >>>>> static bool netdev_is_modern(const char *optarg) >>>>> { >>>>> - return false; >>>>> + QDict *args; >>>>> + const char *type; >>>>> + bool is_modern; >>>>> + >>>>> + args = keyval_parse(optarg, "type", NULL, NULL); >>>>> + if (!args) { >>>>> + return false; >>>>> + } >>>>> + type = qdict_get_try_str(args, "type"); >>>>> + is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); >>>>> + qobject_unref(args); >>>>> + >>>>> + return is_modern; >>>>> } >>>> >>>> You could use g_autoptr here: >>>> >>>> g_autoptr(QDict) args = NULL; >>>> const char *type; >>>> bool is_modern; >>>> >>>> args = keyval_parse(optarg, "type", NULL, NULL); >>>> if (!args) { >>>> return false; >>>> } >>>> type = qdict_get_try_str(args, "type"); >>>> return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); >>>> >>>> Matter of taste; you decide. >>> >>> Looks good. We already had some series to convert existing code to g_autoptr(), so it >>> seems the way to do. >>> >>>> >>>> Now recall how this function is used: it decides whether to parse the >>>> modern way (with qobject_input_visitor_new_str()) or the traditional way >>>> (with qemu_opts_parse_noisily()). >>>> >>>> qemu_opts_parse_noisily() parses into a QemuOpts, for later use with the >>>> opts visitor. >>>> >>>> qobject_input_visitor_new_str() supports both dotted keys and JSON. The >>>> former is parsed with keyval_parse(), the latter with >>>> qobject_from_json(). It returns the resulting parse tree wrapped in a >>>> suitable QAPI input visitor. >>>> >>>> Issue 1: since we get there only when keyval_parse() succeeds, JSON is >>>> unreachable. Reproducer: >>>> >>>> $ qemu-system-x86_64 -netdev '{"id":"foo"}' >>>> upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missing >>>> >>>> This is parsed with qemu_opts_parse_noisily(), resulting in a QemuOpts >>>> with a single option 'type' with value '{"id":"foo"}'. The error >>>> message comes from the opts visitor. >>>> >>>> To fix this, make netdev_is_modern() return true when optarg[0] == '{'. >>>> This matches how qobject_input_visitor_new_str() recognizes JSON. >>> >>> OK >>> >>>> >>>> Issue 2: when keyval_parse() detects an error, we throw it away and fall >>>> back to QemuOpts. This is commonly what we want. But not always. For >>>> instance: >>>> >>>> $ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off' >>>> >>>> Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap: >>>> >>>> qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated >>>> Please use addr.ipv4-off=on instead >>>> qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missing >>>> >>>> We get this because netdev_is_modern() guesses wrongly: keyval_parse() >>>> fails with the perfectly reasonable error message "Expected '=' after >>>> parameter 'addr.ipv4-off'", but netdev_is_modern() ignores the error, >>>> and fails. We fall back to QemuOpts, and confusion ensues. >>>> >>>> I'm not sure we can do much better with reasonable effort. If we decide >>>> to accept this behavior, it should be documented at least in the source >>>> code. >>> >>> What about using modern syntax by default? >>> >>> args = keyval_parse(optarg, "type", NULL, NULL); >>> if (!args) { >>> /* cannot detect the syntax, use new style syntax */ >>> return true; >>> } >> >> As is, netdev_is_modern() has three cases: >> >> 1. keyval_parse() fails >> >> 2. keyval_parse() succeeds, but value of @type is not modern >> >> 3. keyval_parse() succeeds, and value of @type is modern >> >> In case 3. we're sure, because even if qemu_opts_parse_noisily() also >> succeeded, it would result in the same value of @type. >> >> In case 2, assuming traditional seems reasonable. The assumption can be >> wrong when the user intends modern, but fat-fingers the type=T part. >> >> In case 1, we know nothing. >> >> Guessing modern is wrong when the user intends traditional. This >> happens when a meant-to-be-traditional @optarg also parses as modern. >> Quite possible. > > I don't see why keyval_parse() fails in this case. Any example? Brain cramp on my part, I'm afraid %-} Let me start over. Guessing modern is wrong when the user intends traditional. Two sub-cases then: * @optarg parses fine as traditional. For instance, $ qemu-system-x86_64 -netdev type=user,id=foo,ipv4 parses with a warning: option 'ipv4' deprecated Please use ipv4=on instead This is how current master behaves. Guessing modern makes this fail instead: qemu-system-x86_64: -netdev type=user,id=foo,ipv4: Expected '=' after parameter 'ipv4' Regression. * @optarg fails to parse traditional, too. The error reporting is for modern even though the user intends traditional. Can be misleading. Example: $ qemu-system-x86_64 -netdev type=user,id=_,ipv4 Current master: qemu-system-x86_64: -netdev type=user,id=_,ipv4: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter. Guessing modern instead: qemu-system-x86_64: -netdev type=user,id=_,ipv4: Expected '=' after parameter 'ipv4' This should be rare in practice, as traditional parsing detects very few errors. >> Guessing traditional is wrong when the user intends modern. This >> happens when a meant-to-be-modern @optarg fails to parse as modern, >> i.e. whenever the user screws up modern syntax. > > This one is the example you gave (ipv4-off) Two sub-cases then: * @optarg parses fine as traditional. The parse result is unlikely to make sense, though. For instance, $ qemu-system-x86_64 -netdev type=stream,id=foo,server parses with a warning: qemu-system-x86_64: -netdev type=stream,id=foo,server: warning: short-form boolean option 'server' deprecated Please use server=on instead But the result fails in the opts visitor: qemu-system-x86_64: -netdev type=stream,id=foo,server: Parameter 'type' is missing In this case, we're better off with guessing modern: qemu-system-x86_64: -netdev type=stream,id=foo,server: Expected '=' after parameter * @optarg fails to parse traditional, too. The error reporting is for traditional even though the user intends modern. Can be misleading. This is my ipv4-off example. Can't win. Parsers simply don't compose that way. >> Which guess is less bad? I'm not sure. Thoughts? > > Perhaps we can simply fail if keyval_parse() fails? > > something like: > > args = keyval_parse(optarg, "type", NULL, &error_fatal); > type = qdict_get_try_str(args, "type"); > return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); This rejects working option arguments that don't also parse as modern, such as "-netdev type=user,id=foo,ipv4". Guessing traditional seems to be the least bad solution so far. Supporting both traditional and modern syntax in an option argument is a swamp. Can we bypass it somehow? -object uses traditional QemuOpts parsing for key=value,..., and modern parsing for JSON. Sticking to traditional sidesteps compatibility issues. But you have to use JSON for things traditional can't express. Thoughts?