All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Laurent Vivier <lvivier@redhat.com>
Cc: qemu-devel@nongnu.org,  Jason Wang <jasowang@redhat.com>,
	 Thomas Huth <thuth@redhat.com>
Subject: Re: [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
Date: Wed, 15 Jun 2022 13:56:36 +0200	[thread overview]
Message-ID: <87a6aegmx7.fsf@pond.sub.org> (raw)
In-Reply-To: <81563e7d-7743-beab-6eaa-3bb20be1b0df@redhat.com> (Laurent Vivier's message of "Thu, 9 Jun 2022 22:52:37 +0200")

Laurent Vivier <lvivier@redhat.com> writes:

> On 13/05/2022 13:21, Markus Armbruster wrote:
>> Laurent Vivier <lvivier@redhat.com> 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.

Go right ahead :)

>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>> ---
>>>   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.

The function's signature

    int net_client_parse(QemuOptsList *opts_list, const char *optarg)

suggests we're parsing @optarg into @opts_list.

We do only if !netdev_is_modern(optarg).  In other words, the function's
actual behavior doesn't match reasonable expectations based on
signature.  A function comment would mitigate.

If nd->is_netdev wasn't needed, we could code all this just like
-blockdev.  I think that would be simpler.

>> 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?

Thomas, I think you've done some work to replace use cases of -net.  Do
you know what's left?

>>> +
>>>       if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
>>>           return -1;
>>>       }
>> 
>
> Thanks,
> Laurent



  reply	other threads:[~2022-06-15 11:59 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-12  8:09 [RFC PATCH v2 0/8] qapi: net: add unix socket type support to netdev backend Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 1/8] net: introduce convert_host_port() Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily() Laurent Vivier
2022-05-13 11:21   ` Markus Armbruster
2022-06-09 20:52     ` Laurent Vivier
2022-06-15 11:56       ` Markus Armbruster [this message]
2022-05-12  8:09 ` [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs Laurent Vivier
2022-05-13 11:44   ` Markus Armbruster
2022-06-14 21:42     ` Laurent Vivier
2022-06-15 11:46       ` Markus Armbruster
2022-06-20  9:12         ` Laurent Vivier
2022-06-20 11:22           ` Markus Armbruster
2022-06-20 12:09             ` Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 4/8] net: stream: Don't ignore EINVAL on netdev socket connection Laurent Vivier
2022-05-12  8:39   ` Daniel P. Berrangé
2022-05-12  8:09 ` [RFC PATCH v2 5/8] net: stream: add unix socket Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 6/8] net: dgram: make dgram_dst generic Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 7/8] net: dgram: move mcast specific code from net_socket_fd_init_dgram() Laurent Vivier
2022-05-12  8:09 ` [RFC PATCH v2 8/8] net: dgram: add unix socket Laurent Vivier
2022-05-13 18:27 ` [RFC PATCH v2 0/8] qapi: net: add unix socket type support to netdev backend Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a6aegmx7.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.