All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [PATCH 10/36] qemu-option: restrict qemu_opts_set to merge-lists QemuOpts
Date: Mon, 23 Nov 2020 09:14:09 -0500	[thread overview]
Message-ID: <20201123141435.2726558-11-pbonzini@redhat.com> (raw)
In-Reply-To: <20201123141435.2726558-1-pbonzini@redhat.com>

qemu_opts_set is used to create default network backends and to
parse sugar options -kernel, -initrd, -append, -bios and -dtb.
These are very different uses:

I would *expect* a function named qemu_opts_set to set an option in a
merge-lists QemuOptsList, such as -kernel, and possibly to set an option
in a non-merge-lists QemuOptsList with non-NULL id, similar to -set.

However, it wouldn't *work* to use qemu_opts_set for the latter
because qemu_opts_set uses fail_if_exists==1. So, for non-merge-lists
QemuOptsList and non-NULL id, the semantics of qemu_opts_set (fail if the
(QemuOptsList, id) pair already exists) are debatable.

On the other hand, I would not expect qemu_opts_set to create a
non-merge-lists QemuOpts with a single option; which it does, though.
For this case of non-merge-lists QemuOptsList and NULL id, qemu_opts_set
hardly adds value over qemu_opts_parse.  It does skip some parsing and
unescaping, but that's not needed when creating default network
backends.

So qemu_opts_set has warty behavior for non-merge-lists QemuOptsList
if id is non-NULL, and it's mostly pointless if id is NULL.  My
solution to keeping the API as simple as possible is to limit
qemu_opts_set to merge-lists QemuOptsList.  For them, it's useful (we
don't want comma-unescaping for -kernel) *and* has sane semantics.
Network backend creation is switched to qemu_opts_parse.

qemu_opts_set is now only used on merge-lists QemuOptsList... except
in the testcase, which is changed to use a merge-list QemuOptsList.

With this change we can also remove the id parameter.  With the
parameter always NULL, we know that qemu_opts_create cannot fail
and can pass &error_abort to it.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/option.h  |  3 +--
 softmmu/vl.c           | 19 +++++++------------
 tests/test-qemu-opts.c | 20 +++++++++++++++++---
 util/qemu-option.c     |  9 +++------
 4 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/include/qemu/option.h b/include/qemu/option.h
index ac69352e0e..f73e0dc7d9 100644
--- a/include/qemu/option.h
+++ b/include/qemu/option.h
@@ -119,8 +119,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
                            int fail_if_exists, Error **errp);
 void qemu_opts_reset(QemuOptsList *list);
 void qemu_opts_loc_restore(QemuOpts *opts);
-bool qemu_opts_set(QemuOptsList *list, const char *id,
-                   const char *name, const char *value, Error **errp);
+bool qemu_opts_set(QemuOptsList *list, const char *name, const char *value, Error **errp);
 const char *qemu_opts_id(QemuOpts *opts);
 void qemu_opts_set_id(QemuOpts *opts, char *id);
 void qemu_opts_del(QemuOpts *opts);
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 1c08505f3a..10b6152e36 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -3384,20 +3384,16 @@ void qemu_init(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_kernel:
-                qemu_opts_set(qemu_find_opts("machine"), NULL, "kernel", optarg,
-                              &error_abort);
+                qemu_opts_set(qemu_find_opts("machine"), "kernel", optarg, &error_abort);
                 break;
             case QEMU_OPTION_initrd:
-                qemu_opts_set(qemu_find_opts("machine"), NULL, "initrd", optarg,
-                              &error_abort);
+                qemu_opts_set(qemu_find_opts("machine"), "initrd", optarg, &error_abort);
                 break;
             case QEMU_OPTION_append:
-                qemu_opts_set(qemu_find_opts("machine"), NULL, "append", optarg,
-                              &error_abort);
+                qemu_opts_set(qemu_find_opts("machine"), "append", optarg, &error_abort);
                 break;
             case QEMU_OPTION_dtb:
-                qemu_opts_set(qemu_find_opts("machine"), NULL, "dtb", optarg,
-                              &error_abort);
+                qemu_opts_set(qemu_find_opts("machine"), "dtb", optarg, &error_abort);
                 break;
             case QEMU_OPTION_cdrom:
                 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
@@ -3507,8 +3503,7 @@ void qemu_init(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_bios:
-                qemu_opts_set(qemu_find_opts("machine"), NULL, "firmware", optarg,
-                              &error_abort);
+                qemu_opts_set(qemu_find_opts("machine"), "firmware", optarg, &error_abort);
                 break;
             case QEMU_OPTION_singlestep:
                 singlestep = 1;
@@ -4391,9 +4386,9 @@ void qemu_init(int argc, char **argv, char **envp)
 
     if (default_net) {
         QemuOptsList *net = qemu_find_opts("net");
-        qemu_opts_set(net, NULL, "type", "nic", &error_abort);
+        qemu_opts_parse(net, "nic", true, &error_abort);
 #ifdef CONFIG_SLIRP
-        qemu_opts_set(net, NULL, "type", "user", &error_abort);
+        qemu_opts_parse(net, "user", true, &error_abort);
 #endif
     }
 
diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c
index 297ffe79dd..2aab831d10 100644
--- a/tests/test-qemu-opts.c
+++ b/tests/test-qemu-opts.c
@@ -84,11 +84,25 @@ static QemuOptsList opts_list_03 = {
     },
 };
 
+static QemuOptsList opts_list_04 = {
+    .name = "opts_list_04",
+    .head = QTAILQ_HEAD_INITIALIZER(opts_list_04.head),
+    .merge_lists = true,
+    .desc = {
+        {
+            .name = "str3",
+            .type = QEMU_OPT_STRING,
+        },
+        { /* end of list */ }
+    },
+};
+
 static void register_opts(void)
 {
     qemu_add_opts(&opts_list_01);
     qemu_add_opts(&opts_list_02);
     qemu_add_opts(&opts_list_03);
+    qemu_add_opts(&opts_list_04);
 }
 
 static void test_find_unknown_opts(void)
@@ -402,17 +416,17 @@ static void test_qemu_opts_set(void)
     QemuOpts *opts;
     const char *opt;
 
-    list = qemu_find_opts("opts_list_01");
+    list = qemu_find_opts("opts_list_04");
     g_assert(list != NULL);
     g_assert(QTAILQ_EMPTY(&list->head));
-    g_assert_cmpstr(list->name, ==, "opts_list_01");
+    g_assert_cmpstr(list->name, ==, "opts_list_04");
 
     /* should not find anything at this point */
     opts = qemu_opts_find(list, NULL);
     g_assert(opts == NULL);
 
     /* implicitly create opts and set str3 value */
-    qemu_opts_set(list, NULL, "str3", "value", &error_abort);
+    qemu_opts_set(list, "str3", "value", &error_abort);
     g_assert(!QTAILQ_EMPTY(&list->head));
 
     /* get the just created opts */
diff --git a/util/qemu-option.c b/util/qemu-option.c
index acefbc23fa..25792159ba 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -670,15 +670,12 @@ void qemu_opts_loc_restore(QemuOpts *opts)
     loc_restore(&opts->loc);
 }
 
-bool qemu_opts_set(QemuOptsList *list, const char *id,
-                   const char *name, const char *value, Error **errp)
+bool qemu_opts_set(QemuOptsList *list, const char *name, const char *value, Error **errp)
 {
     QemuOpts *opts;
 
-    opts = qemu_opts_create(list, id, 1, errp);
-    if (!opts) {
-        return false;
-    }
+    assert(list->merge_lists);
+    opts = qemu_opts_create(list, NULL, 0, &error_abort);
     return qemu_opt_set(opts, name, value, errp);
 }
 
-- 
2.26.2




  parent reply	other threads:[~2020-11-23 14:20 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 14:13 [PATCH v3 00/36] cleanup qemu_init and make sense of command line processing Paolo Bonzini
2020-11-23 14:14 ` [PATCH 01/36] vl: extract validation of -smp to machine.c Paolo Bonzini
2020-11-23 14:14 ` [PATCH 02/36] vl: remove bogus check Paolo Bonzini
2020-11-23 14:14 ` [PATCH 03/36] vl: split various early command line options to a separate function Paolo Bonzini
2020-11-26 16:47   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 04/36] vl: move various initialization routines out of qemu_init Paolo Bonzini
2020-11-23 14:14 ` [PATCH 05/36] vl: extract qemu_init_subsystems Paolo Bonzini
2020-11-23 14:14 ` [PATCH 06/36] vl: move prelaunch part of qemu_init to new functions Paolo Bonzini
2020-11-23 14:14 ` [PATCH 07/36] vl: extract various command line validation snippets to a new function Paolo Bonzini
2020-11-23 14:14 ` [PATCH 08/36] vl: preconfig and loadvm are mutually exclusive Paolo Bonzini
2020-11-23 14:14 ` [PATCH 09/36] vl: extract various command line desugaring snippets to a new function Paolo Bonzini
2020-11-23 14:14 ` Paolo Bonzini [this message]
2020-11-23 14:14 ` [PATCH 11/36] vl: create "-net nic -net user" default earlier Paolo Bonzini
2020-11-23 14:14 ` [PATCH 12/36] vl: load plugins as late as possible Paolo Bonzini
2020-11-26 16:54   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 13/36] vl: move semihosting command line fallback to qemu_init_board Paolo Bonzini
2020-11-26 17:10   ` Igor Mammedov
2020-11-27  5:03     ` Paolo Bonzini
2020-11-27 10:31       ` Igor Mammedov
2020-11-27 11:22         ` Paolo Bonzini
2020-11-27 12:12           ` Igor Mammedov
2020-11-27 12:22             ` Paolo Bonzini
2020-11-23 14:14 ` [PATCH 14/36] vl: extract default devices to separate functions Paolo Bonzini
2020-11-26 17:29   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 15/36] vl: move CHECKPOINT_INIT after preconfig Paolo Bonzini
2020-11-26 17:36   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 16/36] vl: separate qemu_create_early_backends Paolo Bonzini
2020-11-23 14:14 ` [PATCH 17/36] vl: separate qemu_create_late_backends Paolo Bonzini
2020-11-23 14:14 ` [PATCH 18/36] vl: separate qemu_create_machine Paolo Bonzini
2020-11-23 14:14 ` [PATCH 19/36] vl: separate qemu_apply_machine_options Paolo Bonzini
2020-11-23 14:14 ` [PATCH 20/36] vl: separate qemu_resolve_machine_memdev Paolo Bonzini
2020-11-26 17:39   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 21/36] vl: initialize displays before preconfig loop Paolo Bonzini
2020-11-26 17:51   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 22/36] vl: move -global check earlier Paolo Bonzini
2020-11-26 17:55   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 23/36] migration, vl: start migration via qmp_migrate_incoming Paolo Bonzini
2020-11-26 18:04   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 24/36] vl: start VM via qmp_cont Paolo Bonzini
2020-11-23 14:14 ` [PATCH 25/36] hmp: introduce cmd_available Paolo Bonzini
2020-11-23 14:14 ` [PATCH 26/36] remove preconfig state Paolo Bonzini
2020-11-26 18:55   ` Igor Mammedov
2020-11-27  5:19     ` Paolo Bonzini
2020-11-27 10:50       ` Igor Mammedov
2020-11-27 11:50         ` Paolo Bonzini
2020-11-30 12:41           ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 27/36] vl: remove separate preconfig main_loop Paolo Bonzini
2020-11-30 12:46   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 28/36] vl: allow -incoming defer with -preconfig Paolo Bonzini
2020-11-27 10:51   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 29/36] vl: extract softmmu/datadir.c Paolo Bonzini
2020-11-27 12:06   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 30/36] vl: extract machine done notifiers Paolo Bonzini
2020-11-27 12:14   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 31/36] vl: extract softmmu/rtc.c Paolo Bonzini
2020-11-27 12:43   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 32/36] vl: extract softmmu/runstate.c Paolo Bonzini
2020-11-27 12:59   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 33/36] vl: extract softmmu/globals.c Paolo Bonzini
2020-11-27 13:19   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 34/36] vl: remove serial_max_hds Paolo Bonzini
2020-11-27 13:11   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 35/36] vl: clean up -boot variables Paolo Bonzini
2020-11-27 13:12   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 36/36] vl: move all generic initialization out of vl.c Paolo Bonzini
2020-11-27 13:30   ` Igor Mammedov
2020-11-27 12:00 ` [PATCH 37/36] machine: introduce MachineInitPhase Paolo Bonzini
2020-11-27 13:29   ` Igor Mammedov
2020-11-27 15:29     ` Paolo Bonzini
2020-11-30 12:50 ` [PATCH v3 00/36] cleanup qemu_init and make sense of command line processing Igor Mammedov

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=20201123141435.2726558-11-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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.