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 774D8C433EF for ; Thu, 9 Jun 2022 21:19:37 +0000 (UTC) Received: from localhost ([::1]:52490 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nzPZ2-0007sR-Gl for qemu-devel@archiver.kernel.org; Thu, 09 Jun 2022 17:19:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47910) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nzP95-0003fu-Pd for qemu-devel@nongnu.org; Thu, 09 Jun 2022 16:52:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:32214) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nzP91-00042P-Sg for qemu-devel@nongnu.org; Thu, 09 Jun 2022 16:52:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654807961; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KmGtJ+z0uxqtZARm1e++PYWcnDb0B2zAgj2YP2NlTI0=; b=ZqaeLgpeIbGpgH2/DUbTuaJsLjuvMXrTKRj+J75s9QS6/JGZW9CL7mAfWtcSQtH8YE1HQ5 qBgmBtcBvgAmVr3yLYPACtJPkhZnwtNwABkkKWBeObjOsSAkur2r6yMN7bSTBg5X2iS7IV WZLmNh/s4cAGCUTZfHLGiZWB4D16qp4= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-595-SVdTVdbyMGqCWs-_ng-UCA-1; Thu, 09 Jun 2022 16:52:40 -0400 X-MC-Unique: SVdTVdbyMGqCWs-_ng-UCA-1 Received: by mail-qt1-f199.google.com with SMTP id 15-20020ac8570f000000b00304e9e3b19aso11371697qtw.8 for ; Thu, 09 Jun 2022 13:52:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=KmGtJ+z0uxqtZARm1e++PYWcnDb0B2zAgj2YP2NlTI0=; b=zMxXelGYcHU3rkvSPfEEbHCs1myniVeJXKYV0p8H8l2J2tVHCco4iNpEH4R6JKHX9g Dm+8sLOUUf9mYgKRyflxUzMZh7PAUgoIcijtYsr4iLPFKYQ4JcAcvzA8bj0Dr3rJgVUU 8Y3tfOdmsNoQGPXn2mTk5pgIpW1P5JZb523rJkqTCxkr7DmL4m1ESpLFQUTvLTXGQIw2 mC3mgki1YgSnnp5v7/N0MnNPo1+ke4tbcSBSNWaVLaOXiMGFtOkjNB93nDttM28X2C9/ ffy0WUNCo8RY5FjbnPwil1/ZeFBmWstvo9vLIu1ymwv6s1ZAffNt4bg/yKVxM2k7zhIb 3z5w== X-Gm-Message-State: AOAM5303Lou+DxZKo85O1+BdtOHWyGapjA4VR3Z8egyl+Y7Myh2sivlT HopuHcLX8IDvbt5Eh32yctLOjCUjtQgCQgAdcUr/nhLbcZyOkABkyZ9tg458vhZN5FBrfnKJKUg 132pJSl24xhUrnsM= X-Received: by 2002:a05:620a:4155:b0:6a5:76e5:82c6 with SMTP id k21-20020a05620a415500b006a576e582c6mr28003152qko.420.1654807960194; Thu, 09 Jun 2022 13:52:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsAMlZpo/SvG8VQ3hCZ9eOhpMVjm/VOk+xd2VMlvsB47ZH65nHfJuPGgGIe3ES/q9lrKsJrQ== X-Received: by 2002:a05:620a:4155:b0:6a5:76e5:82c6 with SMTP id k21-20020a05620a415500b006a576e582c6mr28003140qko.420.1654807959832; Thu, 09 Jun 2022 13:52:39 -0700 (PDT) Received: from [192.168.100.42] ([82.142.8.70]) by smtp.gmail.com with ESMTPSA id g4-20020a05620a40c400b006a74458410csm141261qko.123.2022.06.09.13.52.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Jun 2022 13:52:39 -0700 (PDT) Message-ID: <81563e7d-7743-beab-6eaa-3bb20be1b0df@redhat.com> Date: Thu, 9 Jun 2022 22:52:37 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily() Content-Language: en-US To: Markus Armbruster Cc: qemu-devel@nongnu.org, Jason Wang , Thomas Huth References: <20220512080932.735962-1-lvivier@redhat.com> <20220512080932.735962-3-lvivier@redhat.com> <87v8u91xwd.fsf@pond.sub.org> From: Laurent Vivier In-Reply-To: <87v8u91xwd.fsf@pond.sub.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=170.10.129.124; envelope-from=lvivier@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 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, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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" On 13/05/2022 13:21, Markus Armbruster wrote: > Laurent Vivier writes: > >> As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field >> of Netdev structure can collides with "type" field of SocketAddress), > > To remember how this works, I have to write a more verbose version of > the above. Why not post it then, so here goes. > > qemu_init() passes the argument of -netdev, -nic, and -net to > net_client_parse(). > > net_client_parse() parses with qemu_opts_parse_noisily(), passing > QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and > qemu_net_opts for -net. Their desc[] are all empty, which means any > keys are accepted. The result of the parse (a QemuOpts) is stored in > the QemuOptsList. > > Note that QemuOpts is flat by design. In some places, we layer non-flat > on top using dotted keys convention, but not here. > > net_init_clients() iterates over the stored QemuOpts, and passes them to > net_init_netdev(), net_param_nic(), or net_init_client(), respectively. > > These functions pass the QemuOpts to net_client_init(). They also do > other things with the QemuOpts, which we can ignore here. > > net_client_init() uses the opts visitor to convert the (flat) QemOpts to > a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP > command netdev_add. > > The opts visitor was an early attempt to support QAPI in > (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see > commit eb7ee2cbeb "qapi: introduce OptsVisitor". > > A more modern way to support QAPI is qobject_input_visitor_new_str(). > It uses keyval_parse() instead of QemuOpts for KEY=VALUE,... syntax, and > it also supports JSON syntax. The former isn't quite as expressive as > JSON, but it's a lot closer than QemuOpts + opts visitor. > >> we introduce a way to bypass qemu_opts_parse_noisily() and use directly >> visit_type_Netdev() to parse the backend parameters. > > This commit paves the way to use of the modern way instead. I'm going to copy your analysis to the commit message of the patch. > >> Signed-off-by: Laurent Vivier >> --- >> net/net.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 54 insertions(+) >> >> diff --git a/net/net.c b/net/net.c >> index 58c05c200622..2aab7167316c 100644 >> --- a/net/net.c >> +++ b/net/net.c >> @@ -54,6 +54,7 @@ >> #include "net/colo-compare.h" >> #include "net/filter.h" >> #include "qapi/string-output-visitor.h" >> +#include "qapi/qobject-input-visitor.h" >> >> /* Net bridge is currently not supported for W32. */ >> #if !defined(_WIN32) >> @@ -63,6 +64,17 @@ >> static VMChangeStateEntry *net_change_state_entry; >> static QTAILQ_HEAD(, NetClientState) net_clients; >> >> +typedef struct NetdevQueueEntry { >> + bool is_netdev; >> + Netdev *nd; >> + Location loc; >> + QSIMPLEQ_ENTRY(NetdevQueueEntry) entry; >> +} NetdevQueueEntry; >> + >> +typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue; >> + >> +static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue); >> + >> /***********************************************************/ >> /* network device redirectors */ >> >> @@ -1559,6 +1571,19 @@ int net_init_clients(Error **errp) >> >> QTAILQ_INIT(&net_clients); >> >> + while (!QSIMPLEQ_EMPTY(&nd_queue)) { >> + NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue); >> + >> + QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry); >> + loc_push_restore(&nd->loc); >> + if (net_client_init1(nd->nd, nd->is_netdev, errp) < 0) { > > I think you need to loc_pop() here. > >> + return -1; >> + } > > Since the only caller passes &error_fatal, I'd be tempted to ditch the > @errp argument, and simply do > > net_client_init1(nd->nd, nd->is_netdev, &error_fatal); > > It's what we do for -blockdev, -device, and -object. I've added a patch to remove the @errp from the net_init_clients() arguments. > >> + loc_pop(&nd->loc); >> + qapi_free_Netdev(nd->nd); >> + g_free(nd); >> + } >> + >> if (qemu_opts_foreach(qemu_find_opts("netdev"), >> net_init_netdev, NULL, errp)) { >> return -1; >> @@ -1575,8 +1600,37 @@ int net_init_clients(Error **errp) >> return 0; >> } >> >> +/* >> + * netdev_is_modern() returns true when the backend needs to bypass >> + * qemu_opts_parse_noisily() >> + */ >> +static bool netdev_is_modern(const char *optarg) >> +{ >> + return false; >> +} >> + >> int net_client_parse(QemuOptsList *opts_list, const char *optarg) >> { >> + if (netdev_is_modern(optarg)) { >> + /* >> + * We need to bypass qemu_opts_parse_noisily() to accept >> + * new style object like addr.type=inet in SocketAddress >> + */ > > I'm not sure this will makes sense to future readers. > > What about "Use modern, more expressive syntax"? Done. > >> + Visitor *v; >> + NetdevQueueEntry *nd; >> + >> + v = qobject_input_visitor_new_str(optarg, "type", >> + &error_fatal); >> + nd = g_new(NetdevQueueEntry, 1); >> + visit_type_Netdev(v, NULL, &nd->nd, &error_fatal); >> + visit_free(v); >> + loc_save(&nd->loc); >> + nd->is_netdev = strcmp(opts_list->name, "netdev") == 0; >> + >> + QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry); >> + return 0; >> + } > > Matches what we do for -blockdev, except we additionally have > nd->is_netdev. We need it for calling net_client_init1(). > > If netdev_is_modern(optarg), then the only use of parameter @opts_list > is opts_list->name in the initialization of nd->is_netdev. > > There's a bit of code smell, I'm afraid. I don't see what is the problem. > > I believe @is_netdev needs to be true for -netdev, -nic, and netdev_add; > false for -net. > > Will we ever use the modern syntax with -net? Yes, I think we should support the same syntax with -netdev and -net. My first iteration was to pass is_netdev=true to net_client_init1() and Stefano reported a problem with "-net" with things like that: -net dgram,id=socket0,local.type=inet,local.host=localhost,local.port=1234,\ remote.type=inet,remote.host=localhost,remote.port=1235 -net nic,model=virtio,macaddr=9a:2b:2c:2d:2e:2f > > Any chance we can deprecate -net? Who can decide of that? > >> + >> if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { >> return -1; >> } > Thanks, Laurent