Skip to contentSkip to conversation

Port -net to QemuOpts

PATCH Superseded Mark McLoughlin 4 participants 34 messages Sep 10, 2009 – Sep 18, 2009 1 message not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Port -net to QemuOpts

Hi,
Here's a fairly straightforward port of -net to QemuOpts.

Chatting to Gerd, he favours:

1) Adding explicit support to QemuOpts for type specific parameters so that it can be used by the chardev code too

2) Adding a flags field which could be used to indicate that a parameter can be specified multiple times

Both sound good, but IMHO would be fine after this -net series.

Testing details:

- I've just now rebased to Anthony's queue, and haven't completely re-done my testing. The conflicts were fairly trivial, though

- Build tested all targets, also with mingw and with VDE enabled

- Tried to excercise as many of the options as possible, including stuff like hostfwd

- Tested monitor commands too

- Runtime testing of basic net functionality

Cheers,
Mark.

#1 lore raw

PATCH 1/19 Suppress more more kraxelisms

Let's kick off this series with some of the more critical fixes.

---
 hw/qdev.c     |    2 +-
 qemu-config.c |    2 +-
 qemu-option.c |    6 +++---
 vl.c          |    2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
hw/qdev.c +1 -1
index 6451b8a..c0ef55b 100644
@@ -137,7 +137,7 @@ static int set_property(const char *name, const char *value, void *opaque)
     if (strcmp(name, "bus") == 0)
         return 0;
 
-    if (-1 == qdev_prop_parse(dev, name, value)) {
+    if (qdev_prop_parse(dev, name, value) == -1) {
         qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
                    name, value, dev->info->name);
         return -1;
qemu-config.c +1 -1
index f6f4cb4..61e6ed1 100644
@@ -186,7 +186,7 @@ int qemu_set_option(const char *str)
         return -1;
     }
 
-    if (-1 == qemu_opt_set(opts, arg, str+offset+1)) {
+    if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
         fprintf(stderr, "failed to set \"%s\" for %s \"%s\"\n",
                 arg, lists[i]->name, id);
         return -1;
qemu-option.c +3 -3
index 0c2101e..f1a666f 100644
@@ -267,7 +267,7 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
     // Process parameter
     switch (list->type) {
     case OPT_FLAG:
-        if (-1 == parse_option_bool(name, value, &flag))
+        if (parse_option_bool(name, value, &flag) == -1)
             return -1;
         list->value.n = flag;
         break;
@@ -282,7 +282,7 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
         break;
 
     case OPT_SIZE:
-        if (-1 == parse_option_size(name, value, &list->value.n))
+        if (parse_option_size(name, value, &list->value.n) == -1)
             return -1;
         break;
 
@@ -745,7 +745,7 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname
         }
         if (strcmp(option, "id") != 0) {
             /* store and parse */
-            if (-1 == qemu_opt_set(opts, option, value)) {
+            if (qemu_opt_set(opts, option, value) == -1) {
                 return -1;
             }
         }
vl.c +1 -1
index 1dc3ffa..6da83a6 100644
@@ -5153,7 +5153,7 @@ int main(int argc, char **argv, char **envp)
                     fprintf(stderr, "parse error: %s\n", optarg);
                     exit(1);
                 }
-                if (NULL == qemu_chr_open_opts(opts, NULL)) {
+                if (!qemu_chr_open_opts(opts, NULL)) {
                     exit(1);
                 }
                 break;
Signature · 1 line
1.6.2.5
Acked-by on v1 1/19
#2 lore raw

Re: [Qemu-devel] [PATCH 01/19] Suppress more more kraxelisms

Quoted 3 lines · Mark McLoughlin

Let's kick off this series with some of the more critical fixes.

Quoted 81 lines
---
 hw/qdev.c     |    2 +-
 qemu-config.c |    2 +-
 qemu-option.c |    6 +++---
 vl.c          |    2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
hw/qdev.c +1 -1
index 6451b8a..c0ef55b 100644
@@ -137,7 +137,7 @@ static int set_property(const char *name, const char *value, void *opaque)
     if (strcmp(name, "bus") == 0)
         return 0;
 
-    if (-1 == qdev_prop_parse(dev, name, value)) {
+    if (qdev_prop_parse(dev, name, value) == -1) {
         qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
                    name, value, dev->info->name);
         return -1;
qemu-config.c +1 -1
index f6f4cb4..61e6ed1 100644
@@ -186,7 +186,7 @@ int qemu_set_option(const char *str)
         return -1;
     }
 
-    if (-1 == qemu_opt_set(opts, arg, str+offset+1)) {
+    if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
         fprintf(stderr, "failed to set \"%s\" for %s \"%s\"\n",
                 arg, lists[i]->name, id);
         return -1;
qemu-option.c +3 -3
index 0c2101e..f1a666f 100644
@@ -267,7 +267,7 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
     // Process parameter
     switch (list->type) {
     case OPT_FLAG:
-        if (-1 == parse_option_bool(name, value, &flag))
+        if (parse_option_bool(name, value, &flag) == -1)
             return -1;
         list->value.n = flag;
         break;
@@ -282,7 +282,7 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,
         break;
 
     case OPT_SIZE:
-        if (-1 == parse_option_size(name, value, &list->value.n))
+        if (parse_option_size(name, value, &list->value.n) == -1)
             return -1;
         break;
 
@@ -745,7 +745,7 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname
         }
         if (strcmp(option, "id") != 0) {
             /* store and parse */
-            if (-1 == qemu_opt_set(opts, option, value)) {
+            if (qemu_opt_set(opts, option, value) == -1) {
                 return -1;
             }
         }
vl.c +1 -1
index 1dc3ffa..6da83a6 100644
@@ -5153,7 +5153,7 @@ int main(int argc, char **argv, char **envp)
                     fprintf(stderr, "parse error: %s\n", optarg);
                     exit(1);
                 }
-                if (NULL == qemu_chr_open_opts(opts, NULL)) {
+                if (!qemu_chr_open_opts(opts, NULL)) {
                     exit(1);
                 }
                 break;
Signature · 1 line
1.6.2.5
#3 lore raw

PATCH 2/19 Remove bogus error message from qemu_opts_set()

In reply to Mark McLoughlin

The only way qemu_opts_create() can fail is if a QemuOpts with that id already exists and fail_if_exists=1. In that case, we already print an error which makes more sense than the one in qemu_opts_set().

---
 qemu-option.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
qemu-option.c +1 -4
index f1a666f..4d544c7 100644
@@ -669,11 +669,8 @@ int qemu_opts_set(QemuOptsList *list, const char *id,
     QemuOpts *opts;
 
     opts = qemu_opts_create(list, id, 1);
-    if (opts == NULL) {
-        fprintf(stderr, "id \"%s\" not found for \"%s\"\n",
-                id, list->name);
+    if (opts == NULL)
         return -1;
-    }
     return qemu_opt_set(opts, name, value);
 }
 
Signature · 1 line
1.6.2.5
#4 lore raw

Re: [Qemu-devel] [PATCH 02/19] Remove bogus error message from qemu_opts_set()

Quoted 14 lines · Mark McLoughlin

The only way qemu_opts_create() can fail is if a QemuOpts with that id already exists and fail_if_exists=1. In that case, we already print an error which makes more sense than the one in qemu_opts_set().

---
 qemu-option.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
qemu-option.c +0 -0
index f1a666f..4d544c7 100644

qemu-option.c int qemu_opts_set(QemuOptsList *list, const char *id, · line 676

Quoted 9 lines
qemu-option.c +1 -4
@@ -669,11 +669,8 @@ int qemu_opts_set(QemuOptsList *list, const char *id,
     QemuOpts *opts;
 
     opts = qemu_opts_create(list, id, 1);
-    if (opts == NULL) {
-        fprintf(stderr, "id \"%s\" not found for \"%s\"\n",
-                id, list->name);
+    if (opts == NULL)
         return -1;
-    }

I thought we are supposed to keep {} around?
Yes I hate this too.

Quoted 7 lines
qemu-option.c +0 -0
     return qemu_opt_set(opts, name, value);
 }
 
Signature · 1 line
1.6.2.5
#5 lore raw

PATCH 3/19 Remove double error message in qemu_option_set()

In reply to Mark McLoughlin

qemu_opt_set() prints an error message in all failure cases, so qemu_set_option() doesn't need to print another error.

---
 qemu-config.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
qemu-config.c +2 -4
index 61e6ed1..733cc24 100644
@@ -186,11 +186,9 @@ int qemu_set_option(const char *str)
         return -1;
     }
 
-    if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
-        fprintf(stderr, "failed to set \"%s\" for %s \"%s\"\n",
-                arg, lists[i]->name, id);
+    if (qemu_opt_set(opts, arg, str+offset+1) == -1)
         return -1;
-    }
+
     return 0;
 }
 
Signature · 1 line
1.6.2.5
#6 lore raw

Re: [Qemu-devel] [PATCH 03/19] Remove double error message in qemu_option_set()

Quoted 4 lines · Mark McLoughlin

qemu_opt_set() prints an error message in all failure cases, so qemu_set_option() doesn't need to print another error.

Same comment about {} here

Quoted 26 lines
---
 qemu-config.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
qemu-config.c +2 -4
index 61e6ed1..733cc24 100644
@@ -186,11 +186,9 @@ int qemu_set_option(const char *str)
         return -1;
     }
 
-    if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
-        fprintf(stderr, "failed to set \"%s\" for %s \"%s\"\n",
-                arg, lists[i]->name, id);
+    if (qemu_opt_set(opts, arg, str+offset+1) == -1)
         return -1;
-    }
+
     return 0;
 }
 
Signature · 1 line
1.6.2.5
#7 lore raw

Re: braces [was Re: [Qemu-devel] [PATCH 03/19] Remove double error message in qemu_option_set()]

Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Mark McLoughlin

qemu_opt_set() prints an error message in all failure cases, so qemu_set_option() doesn't need to print another error.

Same comment about {} here

That's 3 of these you and Juan have found. And, genuinely, I try not to do this. Just shows how much of reflex it is.

IMHO,

if (blaa(foo, bar, doodah) == NULL)

      return -1;

is far nicer than:

  if (blaa(foo, bar, doodah) == NULL) {
      return -1;
  }

but OTOH, this:

  if (blaa(foo, bar, doodah) == NULL) {
      return doodah(foo, bar, blaa);
  }

*is* arguably better than:

  if (blaa(foo, bar, doodah) == NULL)
      return doodah(foo, bar, blaa);

There's enough of both ways in the code that I think either should be acceptable.

Cheers,
Mark.

#8 lore raw

Re: braces [was Re: [Qemu-devel] [PATCH 03/19] Remove double error message in qemu_option_set()]

Quoted 25 lines · Mark McLoughlin
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Mark McLoughlin

qemu_opt_set() prints an error message in all failure cases, so qemu_set_option() doesn't need to print another error.

Same comment about {} here

That's 3 of these you and Juan have found. And, genuinely, I try not to do this. Just shows how much of reflex it is.

IMHO,

if (blaa(foo, bar, doodah) == NULL)

      return -1;

is far nicer than:

  if (blaa(foo, bar, doodah) == NULL) {
      return -1;
  }

but OTOH, this:

Quoted 8 lines
  if (blaa(foo, bar, doodah) == NULL) {
      return doodah(foo, bar, blaa);
  }

*is* arguably better than:

  if (blaa(foo, bar, doodah) == NULL)
      return doodah(foo, bar, blaa);

And
if (!blaa(foo, bar, doodah))
is even better :)

Quoted 5 lines

There's enough of both ways in the code that I think either should be acceptable.

Cheers,
Mark.

#9 lore raw

Re: braces [was Re: [Qemu-devel] [PATCH 03/19] Remove double error message in qemu_option_set()]

In reply to Mark McLoughlin
Quoted 14 lines · Mark McLoughlin
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Mark McLoughlin

qemu_opt_set() prints an error message in all failure cases, so qemu_set_option() doesn't need to print another error.

Same comment about {} here

That's 3 of these you and Juan have found. And, genuinely, I try not to do this. Just shows how much of reflex it is.

IMHO,

Quoted 8 lines

if (blaa(foo, bar, doodah) == NULL)

      return -1;

is far nicer than:

  if (blaa(foo, bar, doodah) == NULL) {
      return -1;
  }

BTW in patch 1 of the series you convert

    if (foo == NULL)

to

    if (!foo)

so maybe you'd want to be consistent in the rest of the series as well?

Amit

#10 lore raw

PATCH 4/19 Remove double error message for -device option parsing

In reply to Mark McLoughlin

qemu_opts_parse() gives a suitable error message in all failure cases so we can remove the error message from the caller.

---
 vl.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
vl.c +1 -4
index 6da83a6..ed07d50 100644
@@ -5251,11 +5251,8 @@ int main(int argc, char **argv, char **envp)
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
-                opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
-                if (!opts) {
-                    fprintf(stderr, "parse error: %s\n", optarg);
+                if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver"))
                     exit(1);
-                }
                 break;
             case QEMU_OPTION_smp:
                 smp_parse(optarg);
Signature · 1 line
1.6.2.5

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

[Qemu-devel] Re: [PATCH 04/19] Remove double error message for -device option parsing

Quoted 20 lines · Juan Quintela
Quoted 19 lines · Mark McLoughlin

qemu_opts_parse() gives a suitable error message in all failure cases so we can remove the error message from the caller.

---
 vl.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
vl.c +0 -3
index 6da83a6..ed07d50 100644
@@ -5251,11 +5251,8 @@ int main(int argc, char **argv, char **envp)
                 add_device_config(DEV_USB, optarg);
                 break;
             case QEMU_OPTION_device:
-                opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
-                if (!opts) {
-                    fprintf(stderr, "parse error: %s\n", optarg);
Quoted 8 lines

vl.c int main(int argc, char **argv, char **envp) · line 5258

Quoted 6 lines
vl.c +1 -1
+                if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver"))
                     exit(1);
-                }
                 break;
             case QEMU_OPTION_smp:
                 smp_parse(optarg);

This was the only use of opts variable, you have to remove it.

I did, but then re-added it when I re-based to Anthony's queue. The chardev stuff uses it.

Quoted 1 line

And you shouldn't remove the braces (read the qemu coding style).

Yeah, I try not to, it was a reflex, I guess.

(It's also not helped that a lot of the existing code doesn't follow this aspect of the coding style)

Cheers,
Mark.

#13 lore raw

PATCH 5/19 Make qemu_opts_parse() handle empty strings

In reply to Mark McLoughlin

Rather than making callers explicitly handle empty strings by using qemu_opts_create(), we can easily have qemu_opts_parse() handle empty parameter strings.

---
 qemu-option.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
qemu-option.c +1 -3
index 4d544c7..d312257 100644
@@ -711,8 +711,7 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname
     char option[128], value[128];
     const char *p,*pe,*pc;
 
-    p = params;
-    for(;;) {
+    for (p = params; *p != '\0'; p++) {
         pe = strchr(p, '=');
         pc = strchr(p, ',');
         if (!pe || (pc && pc < pe)) {
@@ -749,7 +748,6 @@ int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname
         if (*p != ',') {
             break;
         }
-        p++;
     }
     return 0;
 }
Signature · 1 line
1.6.2.5
#14 lore raw

PATCH 6/19 Add qemu_opts_validate() for post parsing validation

In reply to Mark McLoughlin

Several qemu command line options have a parameter whose value affects what other parameters are accepted for the option.

In these cases, we can have an empty description table in the QemuOptsList and once the option has been parsed we can use a suitable description table to validate the other parameters based on the value of that parameter.

---
 qemu-option.c |   32 ++++++++++++++++++++++++++++++++
 qemu-option.h |    1 +
 2 files changed, 33 insertions(+), 0 deletions(-)
qemu-option.c +32 -0
index d312257..0f517d1 100644
@@ -777,6 +777,38 @@ QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *fi
     return opts;
 }
 
+/* Validate parsed opts against descriptions where no
+ * descriptions were provided in the QemuOptsList.
+ */
+int qemu_opts_validate(QemuOpts *opts, QemuOptDesc *desc)
+{
+    QemuOpt *opt;
+
+    assert(opts->list->desc[0].name == NULL);
+
+    TAILQ_FOREACH(opt, &opts->head, next) {
+        int i;
+
+        for (i = 0; desc[i].name != NULL; i++) {
+            if (strcmp(desc[i].name, opt->name) == 0) {
+                break;
+            }
+        }
+        if (desc[i].name == NULL) {
+            fprintf(stderr, "option \"%s\" is not valid for %s\n",
+                    opt->name, opts->list->name);
+            return -1;
+        }
+
+        opt->desc = &desc[i];
+
+        if (qemu_opt_parse(opt) < 0)
+            return -1;
+    }
+
+    return 0;
+}
+
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
                       int abort_on_failure)
 {
qemu-option.h +1 -0
index 9e52625..e816d95 100644
@@ -114,6 +114,7 @@ int qemu_opts_set(QemuOptsList *list, const char *id,
                   const char *name, const char *value);
 const char *qemu_opts_id(QemuOpts *opts);
 void qemu_opts_del(QemuOpts *opts);
+int qemu_opts_validate(QemuOpts *opts, QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname);
 
Signature · 1 line
1.6.2.5
#15 lore raw

Re: [Qemu-devel] [PATCH 06/19] Add qemu_opts_validate() for post parsing validation

Quoted 7 lines · Mark McLoughlin

Several qemu command line options have a parameter whose value affects what other parameters are accepted for the option.

In these cases, we can have an empty description table in the QemuOptsList and once the option has been parsed we can use a suitable description table to validate the other parameters based on the value of that parameter.

Hmm. The "empty description" mode is intended to be used in case the verification isn't done by QemuOpts at all (i.e. for -devices, where the qdev property code will do the checks). I really like to have the option description hooked into QemuOptsList.

I can think of two possible ways to do that:

(1) We can stick all possible values info QemuOptsList->desc. Then have separate data structures to describe which fields are allowed in which cases (and, while being at it, which fields are mandatory).

(2) We have multiple QemuOptDesc lists for the different cases.

I tend to prefer the first. We can do the option parsing unconditionally in qemu_opt_set() then and don't have two different possible code paths for that.

Comments? Other ideas?

cheers,
Gerd

#16 lore raw

Re: [Qemu-devel] [PATCH 06/19] Add qemu_opts_validate() for post parsing validation

Quoted 14 lines · Gerd Hoffmann
Quoted 7 lines · Mark McLoughlin

Several qemu command line options have a parameter whose value affects what other parameters are accepted for the option.

In these cases, we can have an empty description table in the QemuOptsList and once the option has been parsed we can use a suitable description table to validate the other parameters based on the value of that parameter.

Hmm. The "empty description" mode is intended to be used in case the verification isn't done by QemuOpts at all (i.e. for -devices, where the qdev property code will do the checks). I really like to have the option description hooked into QemuOptsList.

Quoted 5 lines

I can think of two possible ways to do that:

(1) We can stick all possible values info QemuOptsList->desc. Then have separate data structures to describe which fields are allowed in which cases (and, while being at it, which fields are mandatory).

You'd need to make it part of the QemuOptDesc to make it easy to figure out from reading the code which parameters apply to which types.

e.g. a QemuOptDesc::firstname_values field

Quoted 1 line

(2) We have multiple QemuOptDesc lists for the different cases.

As with what I did, you'll have some parameters which are common to different types.

Quoted 3 lines

I tend to prefer the first. We can do the option parsing unconditionally in qemu_opt_set() then and don't have two different possible code paths for that.

Sounds reasonable, except it is nice that right now we have the parameter descriptions associated with, and close to, the init function.

Cheers,
Mark.

#17 lore raw

Re: [Qemu-devel] [PATCH 06/19] Add qemu_opts_validate() for post parsing validation

Quoted 9 lines · Mark McLoughlin
Quoted 3 lines · Gerd Hoffmann

(1) We can stick all possible values info QemuOptsList->desc. Then have separate data structures to describe which fields are allowed in which cases (and, while being at it, which fields are mandatory).

You'd need to make it part of the QemuOptDesc to make it easy to figure out from reading the code which parameters apply to which types.

e.g. a QemuOptDesc::firstname_values field

Yes, that would work as well.

Quoted 4 lines
Quoted 1 line

(2) We have multiple QemuOptDesc lists for the different cases.

As with what I did, you'll have some parameters which are common to different types.

One of the reasons I'd prefer (1).

cheers,
Gerd

#18 lore raw

PATCH 7/19 Never overwrite a QemuOpt

In reply to Mark McLoughlin

Rather than overwriting a QemuOpt, just add a new one to the tail and always do a reverse search for parameters to preserve the same behaviour. We use this order so that foreach() iterates over the opts in their original order.

This will allow us handle options where multiple values for the same parameter is allowed - e.g. -net user,hostfwd=

---
 qemu-option.c |   51 +++++++++++++++++++++++----------------------------
 1 files changed, 23 insertions(+), 28 deletions(-)
qemu-option.c +23 -28
index 0f517d1..376efc1 100644
@@ -483,7 +483,7 @@ struct QemuOpt {
 struct QemuOpts {
     const char *id;
     QemuOptsList *list;
-    TAILQ_HEAD(, QemuOpt) head;
+    TAILQ_HEAD(QemuOptHead, QemuOpt) head;
     TAILQ_ENTRY(QemuOpts) next;
 };
 
@@ -491,7 +491,7 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
 {
     QemuOpt *opt;
 
-    TAILQ_FOREACH(opt, &opts->head, next) {
+    TAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
         if (strcmp(opt->name, name) != 0)
             continue;
         return opt;
@@ -565,36 +565,31 @@ static void qemu_opt_del(QemuOpt *opt)
 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
 {
     QemuOpt *opt;
+    QemuOptDesc *desc = opts->list->desc;
+    int i;
 
-    opt = qemu_opt_find(opts, name);
-    if (!opt) {
-        QemuOptDesc *desc = opts->list->desc;
-        int i;
-
-        for (i = 0; desc[i].name != NULL; i++) {
-            if (strcmp(desc[i].name, name) == 0) {
-                break;
-            }
-        }
-        if (desc[i].name == NULL) {
-            if (i == 0) {
-                /* empty list -> allow any */;
-            } else {
-                fprintf(stderr, "option \"%s\" is not valid for %s\n",
-                        name, opts->list->name);
-                return -1;
-            }
+    for (i = 0; desc[i].name != NULL; i++) {
+        if (strcmp(desc[i].name, name) == 0) {
+            break;
         }
-        opt = qemu_mallocz(sizeof(*opt));
-        opt->name = qemu_strdup(name);
-        opt->opts = opts;
-        TAILQ_INSERT_TAIL(&opts->head, opt, next);
-        if (desc[i].name != NULL) {
-            opt->desc = desc+i;
+    }
+    if (desc[i].name == NULL) {
+        if (i == 0) {
+            /* empty list -> allow any */;
+        } else {
+            fprintf(stderr, "option \"%s\" is not valid for %s\n",
+                    name, opts->list->name);
+            return -1;
         }
     }
-    qemu_free((/* !const */ char*)opt->str);
-    opt->str = NULL;
+
+    opt = qemu_mallocz(sizeof(*opt));
+    opt->name = qemu_strdup(name);
+    opt->opts = opts;
+    TAILQ_INSERT_TAIL(&opts->head, opt, next);
+    if (desc[i].name != NULL) {
+        opt->desc = desc+i;
+    }
     if (value) {
         opt->str = qemu_strdup(value);
     }
Signature · 1 line
1.6.2.5
#19 lore raw

Re: [Qemu-devel] [PATCH 07/19] Never overwrite a QemuOpt

Quoted 4 lines · Mark McLoughlin

Rather than overwriting a QemuOpt, just add a new one to the tail and always do a reverse search for parameters to preserve the same behaviour. We use this order so that foreach() iterates over the opts in their original order.

I think we should simply add a flag to QemuOptsDesc saying 'this can have multiple instances' instead of doing these reverse search tricks to maintain backward compatibility.

cheers,
Gerd

#20 lore raw

PATCH 8/19 Add qemu_net_opts

In reply to Mark McLoughlin

The first step in porting -net to QemuOpts. We do not include parameter descriptions in the QemuOptsList because we use the first parameter to choose which descriptions validate against.

---
 qemu-config.c |   13 +++++++++++++
 qemu-config.h |    1 +
 2 files changed, 14 insertions(+), 0 deletions(-)
qemu-config.c +13 -0
index 733cc24..275de4f 100644
@@ -151,10 +151,23 @@ QemuOptsList qemu_device_opts = {
     },
 };
 
+QemuOptsList qemu_net_opts = {
+    .name = "net",
+    .head = TAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
+    .desc = {
+        /*
+         * no elements => accept any params
+         * validation will happen later
+         */
+        { /* end if list */ }
+    },
+};
+
 static QemuOptsList *lists[] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
     &qemu_device_opts,
+    &qemu_net_opts,
     NULL,
 };
 
qemu-config.h +1 -0
index 13b0f19..852fe52 100644
@@ -4,6 +4,7 @@
 extern QemuOptsList qemu_drive_opts;
 extern QemuOptsList qemu_chardev_opts;
 extern QemuOptsList qemu_device_opts;
+extern QemuOptsList qemu_net_opts;
 
 int qemu_set_option(const char *str);
 
Signature · 1 line
1.6.2.5
#21 lore raw

PATCH 9/19 Port -net none and -net nic to QemuOpts

In reply to Mark McLoughlin

We use a table of network types to look up the initialization function and parameter descriptions in net_client_init().

For now, we use QemuOpts for the 'none' and 'nic' types. Subsequent patches port the other types too and the special casing is removed.

We're not parsing the full -net option string here as the type has been stripped from the string, so we do not use qemu_opts_parse() 'firstname' facility. This will also be rectified in subsequent patches.

No functional changes are introduced by this patch.

---
 net.c |  227 +++++++++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 150 insertions(+), 77 deletions(-)
net.c +149 -76
index 340177e..b002ae5 100644
@@ -112,6 +112,7 @@
 #include "audio/audio.h"
 #include "qemu_socket.h"
 #include "qemu-log.h"
+#include "qemu-config.h"
 
 #include "slirp/libslirp.h"
 
@@ -2400,6 +2401,141 @@ static int net_handle_fd_param(Monitor *mon, const char *param)
     }
 }
 
+static int net_init_nic(QemuOpts *opts, Monitor *mon)
+{
+    int idx;
+    NICInfo *nd;
+
+    idx = nic_get_free_idx();
+    if (idx == -1 || nb_nics >= MAX_NICS) {
+        qemu_error("Too Many NICs\n");
+        return -1;
+    }
+
+    nd = &nd_table[idx];
+
+    memset(nd, 0, sizeof(*nd));
+
+    nd->vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    nd->id      = qemu_opts_id(opts);
+    nd->name    = qemu_opt_get(opts, "name");
+    nd->model   = qemu_opt_get(opts, "model");
+    nd->devaddr = qemu_opt_get(opts, "addr");
+
+    nd->macaddr[0] = 0x52;
+    nd->macaddr[1] = 0x54;
+    nd->macaddr[2] = 0x00;
+    nd->macaddr[3] = 0x12;
+    nd->macaddr[4] = 0x34;
+    nd->macaddr[5] = 0x56 + idx;
+
+    if (qemu_opt_get(opts, "macaddr") &&
+        parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
+        qemu_error("invalid syntax for ethernet address\n");
+        return -1;
+    }
+
+    nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
+    if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
+        (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
+        qemu_error("invalid # of vectors: %d\n", nd->nvectors);
+        return -1;
+    }
+
+    nd->used = 1;
+    nd->vlan->nb_guest_devs++;
+    nb_nics++;
+
+    return idx;
+}
+
+#define NET_COMMON_PARAMS_DESC                     \
+    {                                              \
+        .name = "type",                            \
+        .type = QEMU_OPT_STRING,                   \
+        .help = "net client type (nic, tap etc.)", \
+     }, {                                          \
+        .name = "vlan",                            \
+        .type = QEMU_OPT_NUMBER,                   \
+        .help = "vlan number",                     \
+     }, {                                          \
+        .name = "name",                            \
+        .type = QEMU_OPT_STRING,                   \
+        .help = "identifier for monitor commands", \
+     }
+
+typedef int (*net_client_init_func)(QemuOpts *opts, Monitor *mon);
+
+/* magic number, but compiler will warn if too small */
+#define NET_MAX_DESC 20
+
+static struct {
+    const char *type;
+    net_client_init_func init;
+    QemuOptDesc desc[NET_MAX_DESC];
+} net_client_types[] = {
+    {
+        .type = "none",
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            { /* end of list */ }
+        },
+    }, {
+        .type = "nic",
+        .init = net_init_nic,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "macaddr",
+                .type = QEMU_OPT_STRING,
+                .help = "MAC address",
+            }, {
+                .name = "model",
+                .type = QEMU_OPT_STRING,
+                .help = "device model (e1000, rtl8139, virtio etc.)",
+            }, {
+                .name = "addr",
+                .type = QEMU_OPT_STRING,
+                .help = "PCI device address",
+            }, {
+                .name = "vectors",
+                .type = QEMU_OPT_NUMBER,
+                .help = "number of MSI-x vectors, 0 to disable MSI-X",
+            },
+            { /* end of list */ }
+        },
+    },
+    { /* end of list */ }
+};
+
+static int net_client_init_from_opts(Monitor *mon, QemuOpts *opts)
+{
+    const char *type;
+    int i;
+
+    type = qemu_opt_get(opts, "type");
+    if (!type) {
+        qemu_error("No type specified for -net\n");
+        return -1;
+    }
+
+    for (i = 0; net_client_types[i].type != NULL; i++) {
+        if (!strcmp(net_client_types[i].type, type)) {
+            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1)
+                return -1;
+
+            if (net_client_types[i].init)
+                return net_client_types[i].init(opts, NULL);
+            else
+                return 0;
+        }
+    }
+
+    qemu_error("Invalid -net type '%s'\n", type);
+    return -1;
+}
+
 int net_client_init(Monitor *mon, const char *device, const char *p)
 {
     char buf[1024];
@@ -2407,6 +2543,19 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
     VLANState *vlan;
     char *name = NULL;
 
+    if (!strcmp(device, "none") ||
+        !strcmp(device, "nic")) {
+        QemuOpts *opts;
+
+        opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
+        if (!opts)
+            return -1;
+
+        qemu_opt_set(opts, "type", device);
+
+        return net_client_init_from_opts(mon, opts);
+    }
+
     vlan_id = 0;
     if (get_param_value(buf, sizeof(buf), "vlan", p)) {
         vlan_id = strtol(buf, NULL, 0);
@@ -2416,83 +2565,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
     if (get_param_value(buf, sizeof(buf), "name", p)) {
         name = qemu_strdup(buf);
     }
-    if (!strcmp(device, "nic")) {
-        static const char * const nic_params[] = {
-            "vlan", "name", "macaddr", "model", "addr", "id", "vectors", NULL
-        };
-        NICInfo *nd;
-        uint8_t *macaddr;
-        int idx = nic_get_free_idx();
 
-        if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
-            config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
-            ret = -1;
-            goto out;
-        }
-        if (idx == -1 || nb_nics >= MAX_NICS) {
-            config_error(mon, "Too Many NICs\n");
-            ret = -1;
-            goto out;
-        }
-        nd = &nd_table[idx];
-        macaddr = nd->macaddr;
-        macaddr[0] = 0x52;
-        macaddr[1] = 0x54;
-        macaddr[2] = 0x00;
-        macaddr[3] = 0x12;
-        macaddr[4] = 0x34;
-        macaddr[5] = 0x56 + idx;
-
-        if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
-            if (parse_macaddr(macaddr, buf) < 0) {
-                config_error(mon, "invalid syntax for ethernet address\n");
-                ret = -1;
-                goto out;
-            }
-        }
-        if (get_param_value(buf, sizeof(buf), "model", p)) {
-            nd->model = strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "addr", p)) {
-            nd->devaddr = strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "id", p)) {
-            nd->id = strdup(buf);
-        }
-        nd->nvectors = NIC_NVECTORS_UNSPECIFIED;
-        if (get_param_value(buf, sizeof(buf), "vectors", p)) {
-            char *endptr;
-            long vectors = strtol(buf, &endptr, 0);
-            if (*endptr) {
-                config_error(mon, "invalid syntax for # of vectors\n");
-                ret = -1;
-                goto out;
-            }
-            if (vectors < 0 || vectors > 0x7ffffff) {
-                config_error(mon, "invalid # of vectors\n");
-                ret = -1;
-                goto out;
-            }
-            nd->nvectors = vectors;
-        }
-        nd->vlan = vlan;
-        nd->name = name;
-        nd->used = 1;
-        name = NULL;
-        nb_nics++;
-        vlan->nb_guest_devs++;
-        ret = idx;
-    } else
-    if (!strcmp(device, "none")) {
-        if (*p != '\0') {
-            config_error(mon, "'none' takes no parameters\n");
-            ret = -1;
-            goto out;
-        }
-        /* does nothing. It is needed to signal that no network cards
-           are wanted */
-        ret = 0;
-    } else
 #ifdef CONFIG_SLIRP
     if (!strcmp(device, "user")) {
         static const char * const slirp_params[] = {
Signature · 1 line
1.6.2.5
#22 lore raw

Re: [Qemu-devel] [PATCH 09/19] Port -net none and -net nic to QemuOpts

Hi,

net.c static int net_handle_fd_param(Monitor *mon, const char *param) · line 2465

Quoted 5 lines
net.c +4 -0
+#define NET_COMMON_PARAMS_DESC                     \
 
+        .name = "name",                            \
+        .type = QEMU_OPT_STRING,                   \
+        .help = "identifier for monitor commands", \

QemuOpts has id= which IMHO should be used here instead for consistency reasons. I suspect we have to support name= as well for backward compatibility ...

cheers,
Gerd

#23 lore raw

Re: [Qemu-devel] [PATCH 09/19] Port -net none and -net nic to QemuOpts

Quoted 11 lines · Gerd Hoffmann

Hi,

net.c static int net_handle_fd_param(Monitor *mon, const char *param) · line 2465

Quoted 5 lines
net.c +4 -0
+#define NET_COMMON_PARAMS_DESC                     \
 
+        .name = "name",                            \
+        .type = QEMU_OPT_STRING,                   \
+        .help = "identifier for monitor commands", \

QemuOpts has id= which IMHO should be used here instead for consistency reasons. I suspect we have to support name= as well for backward compatibility ...

Yes, libvirt uses name= already

Cheers,
Mark.

#24 lore raw

PATCH 10/19 Port -net user to QemuOpts

In reply to Mark McLoughlin

The handling of guestfwd and hostfwd requires the previous changes to allow multiple values for each parameter. The only way to access those multiple values is to use qemu_opt_foreach().

---
 net.c |  250 +++++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 149 insertions(+), 101 deletions(-)
net.c +149 -101
index b002ae5..aa5a677 100644
@@ -2450,6 +2450,90 @@ static int net_init_nic(QemuOpts *opts, Monitor *mon)
     return idx;
 }
 
+static int net_init_slirp_configs(const char *name, const char *value, void *opaque)
+{
+    struct slirp_config_str *config;
+
+    if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0)
+        return 0;
+
+    config = qemu_mallocz(sizeof(*config));
+
+    pstrcpy(config->str, sizeof(config->str), value);
+
+    if (!strcmp(name, "hostfwd"))
+        config->flags = SLIRP_CFG_HOSTFWD;
+
+    config->next = slirp_configs;
+    slirp_configs = config;
+
+    return 0;
+}
+
+static int net_init_slirp(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    const char *name;
+    const char *vhost;
+    const char *vhostname;
+    const char *vdhcp_start;
+    const char *vnamesrv;
+    const char *tftp_export;
+    const char *bootfile;
+    const char *smb_export;
+    const char *vsmbsrv;
+    char *vnet = NULL;
+    int restricted = 0;
+    int ret;
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name = qemu_opt_get(opts, "name");
+
+    vhost       = qemu_opt_get(opts, "host");
+    vhostname   = qemu_opt_get(opts, "hostname");
+    vdhcp_start = qemu_opt_get(opts, "dhcpstart");
+    vnamesrv    = qemu_opt_get(opts, "dns");
+    tftp_export = qemu_opt_get(opts, "tftp");
+    bootfile    = qemu_opt_get(opts, "bootfile");
+    smb_export  = qemu_opt_get(opts, "smb");
+    vsmbsrv     = qemu_opt_get(opts, "smbserver");
+
+    if (qemu_opt_get(opts, "ip")) {
+        const char *ip = qemu_opt_get(opts, "ip");
+        int l = strlen(ip) + strlen("/24") + 1;
+
+        vnet = qemu_malloc(l);
+
+        /* emulate legacy ip= parameter */
+        pstrcpy(vnet, l, ip);
+        pstrcat(vnet, l, "/24");
+    }
+
+    if (qemu_opt_get(opts, "net")) {
+        if (vnet)
+            qemu_free(vnet);
+        vnet = qemu_strdup(qemu_opt_get(opts, "net"));
+    }
+
+    if (qemu_opt_get(opts, "restrict") &&
+        qemu_opt_get(opts, "restrict")[0] == 'y')
+        restricted = 1;
+
+    qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0);
+
+    ret = net_slirp_init(mon, vlan, "user", name, restricted, vnet, vhost,
+                         vhostname, tftp_export, bootfile, vdhcp_start,
+                         vnamesrv, smb_export, vsmbsrv);
+
+    if (ret != -1)
+        vlan->nb_host_devs++;
+
+    qemu_free(vnet);
+
+    return ret;
+}
+
 #define NET_COMMON_PARAMS_DESC                     \
     {                                              \
         .name = "type",                            \
@@ -2505,6 +2589,68 @@ static struct {
             },
             { /* end of list */ }
         },
+#ifdef CONFIG_SLIRP
+    }, {
+        .type = "user",
+        .init = net_init_slirp,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "hostname",
+                .type = QEMU_OPT_STRING,
+                .help = "client hostname reported by the builtin DHCP server",
+            }, {
+                .name = "restrict",
+                .type = QEMU_OPT_STRING,
+                .help = "isolate the guest from the host (y|yes|n|no)",
+            }, {
+                .name = "ip",
+                .type = QEMU_OPT_STRING,
+                .help = "legacy parameter, use net= instead",
+            }, {
+                .name = "net",
+                .type = QEMU_OPT_STRING,
+                .help = "IP address and optional netmask",
+            }, {
+                .name = "host",
+                .type = QEMU_OPT_STRING,
+                .help = "guest-visible address of the host",
+            }, {
+                .name = "tftp",
+                .type = QEMU_OPT_STRING,
+                .help = "root directory of the built-in TFTP server",
+            }, {
+                .name = "bootfile",
+                .type = QEMU_OPT_STRING,
+                .help = "BOOTP filename, for use with tftp=",
+            }, {
+                .name = "dhcpstart",
+                .type = QEMU_OPT_STRING,
+                .help = "the first of the 16 IPs the built-in DHCP server can assign",
+            }, {
+                .name = "dns",
+                .type = QEMU_OPT_STRING,
+                .help = "guest-visible address of the virtual nameserver",
+            }, {
+                .name = "smb",
+                .type = QEMU_OPT_STRING,
+                .help = "root directory of the built-in SMB server",
+            }, {
+                .name = "smbserver",
+                .type = QEMU_OPT_STRING,
+                .help = "IP address of the built-in SMB server",
+            }, {
+                .name = "hostfwd",
+                .type = QEMU_OPT_STRING,
+                .help = "guest port number to forward incoming TCP or UDP connections",
+            }, {
+                .name = "guestfwd",
+                .type = QEMU_OPT_STRING,
+                .help = "IP address and port to forward guest TCP connections",
+            },
+            { /* end of list */ }
+        },
+#endif
     },
     { /* end of list */ }
 };
@@ -2544,7 +2690,8 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
     char *name = NULL;
 
     if (!strcmp(device, "none") ||
-        !strcmp(device, "nic")) {
+        !strcmp(device, "nic") ||
+        !strcmp(device, "user")) {
         QemuOpts *opts;
 
         opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
@@ -2567,106 +2714,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
     }
 
 #ifdef CONFIG_SLIRP
-    if (!strcmp(device, "user")) {
-        static const char * const slirp_params[] = {
-            "vlan", "name", "hostname", "restrict", "ip", "net", "host",
-            "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver",
-            "hostfwd", "guestfwd", NULL
-        };
-        struct slirp_config_str *config;
-        int restricted = 0;
-        char *vnet = NULL;
-        char *vhost = NULL;
-        char *vhostname = NULL;
-        char *tftp_export = NULL;
-        char *bootfile = NULL;
-        char *vdhcp_start = NULL;
-        char *vnamesrv = NULL;
-        char *smb_export = NULL;
-        char *vsmbsrv = NULL;
-        const char *q;
-
-        if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
-            config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
-            ret = -1;
-            goto out;
-        }
-        if (get_param_value(buf, sizeof(buf), "ip", p)) {
-            int vnet_buflen = strlen(buf) + strlen("/24") + 1;
-            /* emulate legacy parameter */
-            vnet = qemu_malloc(vnet_buflen);
-            pstrcpy(vnet, vnet_buflen, buf);
-            pstrcat(vnet, vnet_buflen, "/24");
-        }
-        if (get_param_value(buf, sizeof(buf), "net", p)) {
-            vnet = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "host", p)) {
-            vhost = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "hostname", p)) {
-            vhostname = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "restrict", p)) {
-            restricted = (buf[0] == 'y') ? 1 : 0;
-        }
-        if (get_param_value(buf, sizeof(buf), "dhcpstart", p)) {
-            vdhcp_start = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "dns", p)) {
-            vnamesrv = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "tftp", p)) {
-            tftp_export = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
-            bootfile = qemu_strdup(buf);
-        }
-        if (get_param_value(buf, sizeof(buf), "smb", p)) {
-            smb_export = qemu_strdup(buf);
-            if (get_param_value(buf, sizeof(buf), "smbserver", p)) {
-                vsmbsrv = qemu_strdup(buf);
-            }
-        }
-        q = p;
-        while (1) {
-            config = qemu_malloc(sizeof(*config));
-            if (!get_next_param_value(config->str, sizeof(config->str),
-                                      "hostfwd", &q)) {
-                break;
-            }
-            config->flags = SLIRP_CFG_HOSTFWD;
-            config->next = slirp_configs;
-            slirp_configs = config;
-            config = NULL;
-        }
-        q = p;
-        while (1) {
-            config = qemu_malloc(sizeof(*config));
-            if (!get_next_param_value(config->str, sizeof(config->str),
-                                      "guestfwd", &q)) {
-                break;
-            }
-            config->flags = 0;
-            config->next = slirp_configs;
-            slirp_configs = config;
-            config = NULL;
-        }
-        qemu_free(config);
-        vlan->nb_host_devs++;
-        ret = net_slirp_init(mon, vlan, device, name, restricted, vnet, vhost,
-                             vhostname, tftp_export, bootfile, vdhcp_start,
-                             vnamesrv, smb_export, vsmbsrv);
-        qemu_free(vnet);
-        qemu_free(vhost);
-        qemu_free(vhostname);
-        qemu_free(tftp_export);
-        qemu_free(bootfile);
-        qemu_free(vdhcp_start);
-        qemu_free(vnamesrv);
-        qemu_free(smb_export);
-        qemu_free(vsmbsrv);
-    } else if (!strcmp(device, "channel")) {
+    if (!strcmp(device, "channel")) {
         if (TAILQ_EMPTY(&slirp_stacks)) {
             struct slirp_config_str *config;
 
Signature · 1 line
1.6.2.5
#25 lore raw

PATCH 11/19 Port -net tap to QemuOpts

In reply to Mark McLoughlin

Some parameters are not valid with fd=. Rather than having a separate parameter description table for validating fd=, it's easir to just check for those invalid parameters later.

Note, the need to possible lookup a file descriptor name from the monitor is the reason why all these init functions are passed a Monitor pointer.

---
 net.c |  219 +++++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 132 insertions(+), 87 deletions(-)
net.c +132 -87
index aa5a677..55792a5 100644
@@ -1377,25 +1377,22 @@ static void tap_send(void *opaque)
  */
 #define TAP_DEFAULT_SNDBUF 1024*1024
 
-static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
+static void tap_set_sndbuf(TAPState *s, QemuOpts *opts, Monitor *mon)
 {
-    int sndbuf = TAP_DEFAULT_SNDBUF;
-
-    if (sndbuf_str) {
-        sndbuf = atoi(sndbuf_str);
-    }
+    int sndbuf;
 
+    sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
     if (!sndbuf) {
         sndbuf = INT_MAX;
     }
 
-    if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && sndbuf_str) {
+    if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) {
         config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
                      strerror(errno));
     }
 }
 #else
-static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
+static void tap_set_sndbuf(TAPState *s, QemuOpts *opts, Monitor *mon)
 {
     if (sndbuf_str) {
         config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
@@ -2534,6 +2531,86 @@ static int net_init_slirp(QemuOpts *opts, Monitor *mon)
     return ret;
 }
 
+#ifdef _WIN32
+static int net_init_tap_win32(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    const char *name;
+    const char *ifname;
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name   = qemu_opt_get(opts, "name");
+    ifname = qemu_opt_get(opts, "ifname");
+
+    if (!ifname) {
+        qemu_error("tap: no interface name\n");
+        return -1;
+    }
+
+    if (tap_win32_init(vlan, "tap", name, ifname) == -1)
+        return -1;
+
+    vlan->nb_host_devs++;
+
+    return 0;
+}
+#elif !defined(_AIX)
+static int net_init_tap(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    const char *name;
+    TAPState *s;
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name = qemu_opt_get(opts, "name");
+
+    if (qemu_opt_get(opts, "fd")) {
+        int fd;
+
+        if (qemu_opt_get(opts, "ifname") ||
+            qemu_opt_get(opts, "script") ||
+            qemu_opt_get(opts, "downscript")) {
+            qemu_error("ifname=, script= and downscript= is invalid with fd=\n");
+            return -1;
+        }
+
+        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
+        if (fd == -1)
+            return -1;
+
+        fcntl(fd, F_SETFL, O_NONBLOCK);
+
+        s = net_tap_fd_init(vlan, "tap", name, fd);
+        if (!s)
+            close(fd);
+    } else {
+        const char *ifname, *script, *downscript;
+
+        ifname     = qemu_opt_get(opts, "ifname");
+        script     = qemu_opt_get(opts, "script");
+        downscript = qemu_opt_get(opts, "downscript");
+
+        if (!script)
+            script = DEFAULT_NETWORK_SCRIPT;
+        if (!downscript)
+            downscript = DEFAULT_NETWORK_DOWN_SCRIPT;
+
+        s = net_tap_init(vlan, "tap", name, ifname, script, downscript);
+    }
+
+    if (!s)
+        return -1;
+
+    tap_set_sndbuf(s, opts, mon);
+
+    vlan->nb_host_devs++;
+
+    return 0;
+}
+#endif
+
 #define NET_COMMON_PARAMS_DESC                     \
     {                                              \
         .name = "type",                            \
@@ -2651,6 +2728,51 @@ static struct {
             { /* end of list */ }
         },
 #endif
+#ifdef _WIN32
+    }, {
+        .type = "tap",
+        .init = net_init_tap_win32,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "ifname",
+                .type = QEMU_OPT_STRING,
+                .help = "interface name",
+            },
+            { /* end of list */ }
+        },
+#elif !defined(_AIX)
+    }, {
+        .type = "tap",
+        .init = net_init_tap,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "fd",
+                .type = QEMU_OPT_STRING,
+                .help = "file descriptor of an already opened tap",
+            }, {
+                .name = "ifname",
+                .type = QEMU_OPT_STRING,
+                .help = "interface name",
+            }, {
+                .name = "script",
+                .type = QEMU_OPT_STRING,
+                .help = "script to initialize the interface",
+            }, {
+                .name = "downscript",
+                .type = QEMU_OPT_STRING,
+                .help = "script to shut down the interface",
+#ifdef TUNSETSNDBUF
+            }, {
+                .name = "sndbuf",
+                .type = QEMU_OPT_SIZE,
+                .help = "send buffer limit"
+#endif
+            },
+            { /* end of list */ }
+        },
+#endif
     },
     { /* end of list */ }
 };
@@ -2691,7 +2813,8 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
 
     if (!strcmp(device, "none") ||
         !strcmp(device, "nic") ||
-        !strcmp(device, "user")) {
+        !strcmp(device, "user") ||
+        !strcmp(device, "tap")) {
         QemuOpts *opts;
 
         opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
@@ -2729,84 +2852,6 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         ret = 0;
     } else
 #endif
-#ifdef _WIN32
-    if (!strcmp(device, "tap")) {
-        static const char * const tap_params[] = {
-            "vlan", "name", "ifname", NULL
-        };
-        char ifname[64];
-
-        if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
-            config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
-            ret = -1;
-            goto out;
-        }
-        if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
-            config_error(mon, "tap: no interface name\n");
-            ret = -1;
-            goto out;
-        }
-        vlan->nb_host_devs++;
-        ret = tap_win32_init(vlan, device, name, ifname);
-    } else
-#elif defined (_AIX)
-#else
-    if (!strcmp(device, "tap")) {
-        char ifname[64], chkbuf[64];
-        char setup_script[1024], down_script[1024];
-        TAPState *s;
-        int fd;
-        vlan->nb_host_devs++;
-        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
-            static const char * const fd_params[] = {
-                "vlan", "name", "fd", "sndbuf", NULL
-            };
-            ret = -1;
-            if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                goto out;
-            }
-            fd = net_handle_fd_param(mon, buf);
-            if (fd == -1) {
-                goto out;
-            }
-            fcntl(fd, F_SETFL, O_NONBLOCK);
-            s = net_tap_fd_init(vlan, device, name, fd);
-            if (!s) {
-                close(fd);
-            }
-        } else {
-            static const char * const tap_params[] = {
-                "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
-            };
-            if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                ret = -1;
-                goto out;
-            }
-            if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
-                ifname[0] = '\0';
-            }
-            if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
-                pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
-            }
-            if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
-                pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
-            }
-            s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
-        }
-        if (s != NULL) {
-            const char *sndbuf_str = NULL;
-            if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
-                sndbuf_str = buf;
-            }
-            tap_set_sndbuf(s, sndbuf_str, mon);
-            ret = 0;
-        } else {
-            ret = -1;
-        }
-    } else
-#endif
     if (!strcmp(device, "socket")) {
         char chkbuf[64];
         if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
Signature · 1 line
1.6.2.5
#26 lore raw

PATCH 12/19 Port -net socket to QemuOpts

In reply to Mark McLoughlin
---
 net.c |  168 ++++++++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 109 insertions(+), 59 deletions(-)
net.c +109 -59
index 55792a5..c3414eb 100644
@@ -2611,6 +2611,89 @@ static int net_init_tap(QemuOpts *opts, Monitor *mon)
 }
 #endif
 
+static int net_init_socket(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    const char *name;
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name = qemu_opt_get(opts, "name");
+
+    if (qemu_opt_get(opts, "fd")) {
+        int fd;
+
+        if (qemu_opt_get(opts, "listen") ||
+            qemu_opt_get(opts, "connect") ||
+            qemu_opt_get(opts, "mcast")) {
+            qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
+            return -1;
+        }
+
+        fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
+        if (fd == -1) {
+            return -1;
+        }
+
+        if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
+            close(fd);
+            return -1;
+        }
+    } else if (qemu_opt_get(opts, "listen")) {
+        const char *listen;
+
+        if (qemu_opt_get(opts, "fd") ||
+            qemu_opt_get(opts, "connect") ||
+            qemu_opt_get(opts, "mcast")) {
+            qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
+            return -1;
+        }
+
+        listen = qemu_opt_get(opts, "listen");
+
+        if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
+            return -1;
+        }
+    } else if (qemu_opt_get(opts, "connect")) {
+        const char *connect;
+
+        if (qemu_opt_get(opts, "fd") ||
+            qemu_opt_get(opts, "listen") ||
+            qemu_opt_get(opts, "mcast")) {
+            qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
+            return -1;
+        }
+
+        connect = qemu_opt_get(opts, "connect");
+
+        if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
+            return -1;
+        }
+    } else if (qemu_opt_get(opts, "mcast")) {
+        const char *mcast;
+
+        if (qemu_opt_get(opts, "fd") ||
+            qemu_opt_get(opts, "connect") ||
+            qemu_opt_get(opts, "listen")) {
+            qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
+            return -1;
+        }
+
+        mcast = qemu_opt_get(opts, "mcast");
+
+        if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) {
+            return -1;
+        }
+    } else {
+        qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
+        return -1;
+    }
+
+    vlan->nb_host_devs++;
+
+    return 0;
+}
+
 #define NET_COMMON_PARAMS_DESC                     \
     {                                              \
         .name = "type",                            \
@@ -2773,6 +2856,30 @@ static struct {
             { /* end of list */ }
         },
 #endif
+    }, {
+        .type = "socket",
+        .init = net_init_socket,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "fd",
+                .type = QEMU_OPT_STRING,
+                .help = "file descriptor of an already opened socket",
+            }, {
+                .name = "listen",
+                .type = QEMU_OPT_STRING,
+                .help = "port number, and optional hostname, to listen on",
+            }, {
+                .name = "connect",
+                .type = QEMU_OPT_STRING,
+                .help = "port number, and optional hostname, to connect to",
+            }, {
+                .name = "mcast",
+                .type = QEMU_OPT_STRING,
+                .help = "UDP multicast address and port number",
+            },
+            { /* end of list */ }
+        },
     },
     { /* end of list */ }
 };
@@ -2814,7 +2921,8 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
     if (!strcmp(device, "none") ||
         !strcmp(device, "nic") ||
         !strcmp(device, "user") ||
-        !strcmp(device, "tap")) {
+        !strcmp(device, "tap") ||
+        !strcmp(device, "socket")) {
         QemuOpts *opts;
 
         opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
@@ -2852,64 +2960,6 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         ret = 0;
     } else
 #endif
-    if (!strcmp(device, "socket")) {
-        char chkbuf[64];
-        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
-            static const char * const fd_params[] = {
-                "vlan", "name", "fd", NULL
-            };
-            int fd;
-            ret = -1;
-            if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                goto out;
-            }
-            fd = net_handle_fd_param(mon, buf);
-            if (fd == -1) {
-                goto out;
-            }
-            if (!net_socket_fd_init(vlan, device, name, fd, 1)) {
-                close(fd);
-                goto out;
-            }
-            ret = 0;
-        } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
-            static const char * const listen_params[] = {
-                "vlan", "name", "listen", NULL
-            };
-            if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                ret = -1;
-                goto out;
-            }
-            ret = net_socket_listen_init(vlan, device, name, buf);
-        } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
-            static const char * const connect_params[] = {
-                "vlan", "name", "connect", NULL
-            };
-            if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                ret = -1;
-                goto out;
-            }
-            ret = net_socket_connect_init(vlan, device, name, buf);
-        } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
-            static const char * const mcast_params[] = {
-                "vlan", "name", "mcast", NULL
-            };
-            if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
-                config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
-                ret = -1;
-                goto out;
-            }
-            ret = net_socket_mcast_init(vlan, device, name, buf);
-        } else {
-            config_error(mon, "Unknown socket options: %s\n", p);
-            ret = -1;
-            goto out;
-        }
-        vlan->nb_host_devs++;
-    } else
 #ifdef CONFIG_VDE
     if (!strcmp(device, "vde")) {
         static const char * const vde_params[] = {
Signature · 1 line
1.6.2.5
#27 lore raw

PATCH 13/19 Port -net vde to QemuOpts

In reply to Mark McLoughlin

The net_vde_init() change is needed because we now pass NULL pointers instead of empty strings for group/sock if they're not set.

---
 net.c |   93 +++++++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 57 insertions(+), 36 deletions(-)
net.c +57 -36
index c3414eb..3297cb1 100644
@@ -1745,8 +1745,8 @@ static int net_vde_init(VLANState *vlan, const char *model,
                         int port, const char *group, int mode)
 {
     VDEState *s;
-    char *init_group = strlen(group) ? (char *)group : NULL;
-    char *init_sock = strlen(sock) ? (char *)sock : NULL;
+    char *init_group = (char *)group;
+    char *init_sock = (char *)sock;
 
     struct vde_open_args args = {
         .port = port,
@@ -2694,6 +2694,33 @@ static int net_init_socket(QemuOpts *opts, Monitor *mon)
     return 0;
 }
 
+#ifdef CONFIG_VDE
+static int net_init_vde(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    const char *name;
+    const char *sock;
+    const char *group;
+    int port, mode;
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name  = qemu_opt_get(opts, "name");
+    sock  = qemu_opt_get(opts, "sock");
+    group = qemu_opt_get(opts, "group");
+
+    port = qemu_opt_get_number(opts, "port", 0);
+    mode = qemu_opt_get_number(opts, "mode", 0700);
+
+    if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1)
+        return -1;
+
+    vlan->nb_host_devs++;
+
+    return 0;
+}
+#endif
+
 #define NET_COMMON_PARAMS_DESC                     \
     {                                              \
         .name = "type",                            \
@@ -2880,6 +2907,32 @@ static struct {
             },
             { /* end of list */ }
         },
+#ifdef CONFIG_VDE
+    }, {
+        .type = "vde",
+        .init = net_init_vde,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "sock",
+                .type = QEMU_OPT_STRING,
+                .help = "socket path",
+            }, {
+                .name = "port",
+                .type = QEMU_OPT_NUMBER,
+                .help = "port number",
+            }, {
+                .name = "group",
+                .type = QEMU_OPT_STRING,
+                .help = "group owner of socket",
+            }, {
+                .name = "mode",
+                .type = QEMU_OPT_NUMBER,
+                .help = "permissions for socket",
+            },
+            { /* end of list */ }
+        },
+#endif
     },
     { /* end of list */ }
 };
@@ -2922,7 +2975,8 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         !strcmp(device, "nic") ||
         !strcmp(device, "user") ||
         !strcmp(device, "tap") ||
-        !strcmp(device, "socket")) {
+        !strcmp(device, "socket") ||
+        !strcmp(device, "vde")) {
         QemuOpts *opts;
 
         opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
@@ -2960,39 +3014,6 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         ret = 0;
     } else
 #endif
-#ifdef CONFIG_VDE
-    if (!strcmp(device, "vde")) {
-        static const char * const vde_params[] = {
-            "vlan", "name", "sock", "port", "group", "mode", NULL
-        };
-        char vde_sock[1024], vde_group[512];
-	int vde_port, vde_mode;
-
-        if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
-            config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
-            ret = -1;
-            goto out;
-        }
-        vlan->nb_host_devs++;
-        if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
-	    vde_sock[0] = '\0';
-	}
-	if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
-	    vde_port = strtol(buf, NULL, 10);
-	} else {
-	    vde_port = 0;
-	}
-	if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
-	    vde_group[0] = '\0';
-	}
-	if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
-	    vde_mode = strtol(buf, NULL, 8);
-	} else {
-	    vde_mode = 0700;
-	}
-	ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
-    } else
-#endif
     if (!strcmp(device, "dump")) {
         int len = 65536;
 
Signature · 1 line
1.6.2.5
#28 lore raw

PATCH 14/19 Port -net dump to QemuOpts

In reply to Mark McLoughlin

Note, not incrementing nb_host_devs in net_init_dump() is intentional.

---
 net.c |   54 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 42 insertions(+), 12 deletions(-)
net.c +42 -12
index 3297cb1..54483b4 100644
@@ -2721,6 +2721,29 @@ static int net_init_vde(QemuOpts *opts, Monitor *mon)
 }
 #endif
 
+static int net_init_dump(QemuOpts *opts, Monitor *mon)
+{
+    VLANState *vlan;
+    int len;
+    const char *file;
+    const char *name;
+    char def_file[128];
+
+    vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+
+    name = qemu_opt_get(opts, "name");
+
+    file = qemu_opt_get(opts, "file");
+    if (!file) {
+        snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id);
+        file = def_file;
+    }
+
+    len = qemu_opt_get_size(opts, "len", 65536);
+
+    return net_dump_init(mon, vlan, "dump", name, file, len);
+}
+
 #define NET_COMMON_PARAMS_DESC                     \
     {                                              \
         .name = "type",                            \
@@ -2933,6 +2956,22 @@ static struct {
             { /* end of list */ }
         },
 #endif
+    }, {
+        .type = "dump",
+        .init = net_init_dump,
+        .desc = {
+            NET_COMMON_PARAMS_DESC,
+            {
+                .name = "len",
+                .type = QEMU_OPT_SIZE,
+                .help = "per-packet size limit (64k default)",
+            }, {
+                .name = "file",
+                .type = QEMU_OPT_STRING,
+                .help = "dump file path (default is qemu-vlan0.pcap)",
+            },
+            { /* end of list */ }
+        },
     },
     { /* end of list */ }
 };
@@ -2976,7 +3015,8 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         !strcmp(device, "user") ||
         !strcmp(device, "tap") ||
         !strcmp(device, "socket") ||
-        !strcmp(device, "vde")) {
+        !strcmp(device, "vde") ||
+        !strcmp(device, "dump")) {
         QemuOpts *opts;
 
         opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
@@ -3014,17 +3054,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         ret = 0;
     } else
 #endif
-    if (!strcmp(device, "dump")) {
-        int len = 65536;
-
-        if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
-            len = strtol(buf, NULL, 0);
-        }
-        if (!get_param_value(buf, sizeof(buf), "file", p)) {
-            snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
-        }
-        ret = net_dump_init(mon, vlan, device, name, buf, len);
-    } else {
+    {
         config_error(mon, "Unknown network device: %s\n", device);
         ret = -1;
         goto out;
Signature · 1 line
1.6.2.5
#29 lore raw

PATCH 15/19 Clean up legacy code in net_client_init()

In reply to Mark McLoughlin

Now that we've ported everything over to QemuOpts, we can kill off all the cruft in net_client_init().

Note, the 'channel' type requires special handling as it uses a format that QemuOpts can't parse

---
 net.c |   56 +++++++++++---------------------------------------------
 1 files changed, 11 insertions(+), 45 deletions(-)
net.c +11 -45
index 54483b4..14271ff 100644
@@ -3005,38 +3005,7 @@ static int net_client_init_from_opts(Monitor *mon, QemuOpts *opts)
 
 int net_client_init(Monitor *mon, const char *device, const char *p)
 {
-    char buf[1024];
-    int vlan_id, ret;
-    VLANState *vlan;
-    char *name = NULL;
-
-    if (!strcmp(device, "none") ||
-        !strcmp(device, "nic") ||
-        !strcmp(device, "user") ||
-        !strcmp(device, "tap") ||
-        !strcmp(device, "socket") ||
-        !strcmp(device, "vde") ||
-        !strcmp(device, "dump")) {
-        QemuOpts *opts;
-
-        opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
-        if (!opts)
-            return -1;
-
-        qemu_opt_set(opts, "type", device);
-
-        return net_client_init_from_opts(mon, opts);
-    }
-
-    vlan_id = 0;
-    if (get_param_value(buf, sizeof(buf), "vlan", p)) {
-        vlan_id = strtol(buf, NULL, 0);
-    }
-    vlan = qemu_find_vlan(vlan_id, 1);
-
-    if (get_param_value(buf, sizeof(buf), "name", p)) {
-        name = qemu_strdup(buf);
-    }
+    QemuOpts *opts;
 
 #ifdef CONFIG_SLIRP
     if (!strcmp(device, "channel")) {
@@ -3051,20 +3020,17 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
         } else {
             slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1);
         }
-        ret = 0;
-    } else
-#endif
-    {
-        config_error(mon, "Unknown network device: %s\n", device);
-        ret = -1;
-        goto out;
-    }
-    if (ret < 0) {
-        config_error(mon, "Could not initialize device '%s'\n", device);
+        return 0;
     }
-out:
-    qemu_free(name);
-    return ret;
+#endif
+
+    opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
+    if (!opts)
+      return -1;
+
+    qemu_opt_set(opts, "type", device);
+
+    return net_client_init_from_opts(mon, opts);
 }
 
 void net_client_uninit(NICInfo *nd)
Signature · 1 line
1.6.2.5
#30 lore raw

PATCH 16/19 Port host_net_add monitor command to QemuOpts

In reply to Mark McLoughlin

Here is where we rely on qemu_opts_parse() to handle an empty string. We could alternatively explicitly handle this here by using qemu_opts_create() when we're not supplied any parameters, but its cleaner this way.

---
 net.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
net.c +13 -2
index 14271ff..7812018 100644
@@ -3064,13 +3064,24 @@ static int net_host_check_device(const char *device)
 void net_host_device_add(Monitor *mon, const QDict *qdict)
 {
     const char *device = qdict_get_str(qdict, "device");
-    const char *opts = qdict_get_try_str(qdict, "opts");
+    const char *opts_str = qdict_get_try_str(qdict, "opts");
+    QemuOpts *opts;
 
     if (!net_host_check_device(device)) {
         monitor_printf(mon, "invalid host network device %s\n", device);
         return;
     }
-    if (net_client_init(mon, device, opts ? opts : "") < 0) {
+
+    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
+    if (!opts) {
+        monitor_printf(mon, "parsing network options '%s' failed\n",
+                       opts_str ? opts_str : "");
+        return;
+    }
+
+    qemu_opt_set(opts, "type", device);
+
+    if (net_client_init_from_opts(mon, opts) < 0) {
         monitor_printf(mon, "adding host network device %s failed\n", device);
     }
 }
Signature · 1 line
1.6.2.5
#31 lore raw

PATCH 17/19 Port usb net to QemuOpts

In reply to Mark McLoughlin

We need net_client_init_from_opts() exported for this

---
 net.c |    2 +-
 net.h |    2 ++
 vl.c  |   17 +++++++++++++----
 3 files changed, 16 insertions(+), 5 deletions(-)
net.c +1 -1
index 7812018..bd6c305 100644
@@ -2976,7 +2976,7 @@ static struct {
     { /* end of list */ }
 };
 
-static int net_client_init_from_opts(Monitor *mon, QemuOpts *opts)
+int net_client_init_from_opts(Monitor *mon, QemuOpts *opts)
 {
     const char *type;
     int i;
net.h +2 -0
index f46e6c6..5833958 100644
@@ -4,6 +4,7 @@
 #include "sys-queue.h"
 #include "qemu-common.h"
 #include "qdict.h"
+#include "qemu-option.h"
 
 /* VLANs support */
 
@@ -135,6 +136,7 @@ extern const char *legacy_tftp_prefix;
 extern const char *legacy_bootp_filename;
 
 int net_client_init(Monitor *mon, const char *device, const char *p);
+int net_client_init_from_opts(Monitor *mon, QemuOpts *opts);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
 void net_slirp_smb(const char *exported_dir);
vl.c +13 -4
index ed07d50..18f115a 100644
@@ -2506,12 +2506,21 @@ static int usb_device_add(const char *devname, int is_hotplug)
         dev = usb_baum_init();
 #endif
     } else if (strstart(devname, "net:", &p)) {
-        int nic = nb_nics;
+        QemuOpts *opts;
+        int idx;
 
-        if (net_client_init(NULL, "nic", p) < 0)
+        opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
+        if (!opts)
             return -1;
-        nd_table[nic].model = "usb";
-        dev = usb_net_init(&nd_table[nic]);
+
+        qemu_opt_set(opts, "type", "nic");
+
+        idx = net_client_init_from_opts(NULL, opts);
+        if (idx == -1)
+            return -1;
+
+        nd_table[idx].model = "usb";
+        dev = usb_net_init(&nd_table[idx]);
     } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
         dev = usb_bt_init(devname[2] ? hci_init(p) :
                         bt_new_hci(qemu_find_bt_vlan(0)));
Signature · 1 line
1.6.2.5
#32 lore raw

PATCH 18/19 Port PCI NIC hotplug to QemuOpts

In reply to Mark McLoughlin
---
 hw/pci-hotplug.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)
hw/pci-hotplug.c +14 -2
index 5348dd1..930a3eb 100644
@@ -32,14 +32,26 @@
 #include "block_int.h"
 #include "scsi-disk.h"
 #include "virtio-blk.h"
+#include "qemu-config.h"
 
 #if defined(TARGET_I386) || defined(TARGET_X86_64)
 static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
-                                       const char *devaddr, const char *opts)
+                                       const char *devaddr,
+                                       const char *opts_str)
 {
+    QemuOpts *opts;
     int ret;
 
-    ret = net_client_init(mon, "nic", opts);
+    opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
+    if (!opts) {
+        monitor_printf(mon, "parsing network options '%s' failed\n",
+                       opts_str ? opts_str : "");
+        return NULL;
+    }
+
+    qemu_opt_set(opts, "type", "nic");
+
+    ret = net_client_init_from_opts(mon, opts);
     if (ret < 0)
         return NULL;
     if (nd_table[ret].devaddr) {
Signature · 1 line
1.6.2.5
#33 lore raw

PATCH 19/19 Final net cleanup after conversion to QemuOpts

In reply to Mark McLoughlin

Now that net_client_init() has no users, kill it off and rename net_client_init_from_opts().

There is no further need for the old code in net_client_parse() either. We use qemu_opts_parse() 'firstname' facitity for that. Instead, move the special handling of the 'vmchannel' type there.

Simplify the vl.c code into merely call net_client_parse() for each -net command line option and then calling net_init_clients() later to iterate over the options and create the clients.

---
 hw/pci-hotplug.c |    2 +-
 net.c            |  106 +++++++++++++++++++++++++++---------------------------
 net.h            |    5 +--
 vl.c             |   30 ++-------------
 4 files changed, 60 insertions(+), 83 deletions(-)
hw/pci-hotplug.c +1 -1
index 930a3eb..253dd59 100644
@@ -51,7 +51,7 @@ static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon,
 
     qemu_opt_set(opts, "type", "nic");
 
-    ret = net_client_init_from_opts(mon, opts);
+    ret = net_client_init(mon, opts);
     if (ret < 0)
         return NULL;
     if (nd_table[ret].devaddr) {
net.c +53 -53

139 lines not on this page · raw message

net.h +2 -3
index 5833958..9812aad 100644
@@ -135,16 +135,15 @@ void net_checksum_calculate(uint8_t *data, int length);
 extern const char *legacy_tftp_prefix;
 extern const char *legacy_bootp_filename;
 
-int net_client_init(Monitor *mon, const char *device, const char *p);
-int net_client_init_from_opts(Monitor *mon, QemuOpts *opts);
+int net_client_init(Monitor *mon, QemuOpts *opts);
 void net_client_uninit(NICInfo *nd);
 int net_client_parse(const char *str);
+int net_init_clients(void);
 void net_slirp_smb(const char *exported_dir);
 void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict);
 void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict);
 void net_slirp_redir(const char *redir_str);
 void net_cleanup(void);
-void net_client_check(void);
 void net_set_boot_mask(int boot_mask);
 void net_host_device_add(Monitor *mon, const QDict *qdict);
 void net_host_device_remove(Monitor *mon, const QDict *qdict);
vl.c +4 -26
index 18f115a..9285d6b 100644
@@ -2515,7 +2515,7 @@ static int usb_device_add(const char *devname, int is_hotplug)
 
         qemu_opt_set(opts, "type", "nic");
 
-        idx = net_client_init_from_opts(NULL, opts);
+        idx = net_client_init(NULL, opts);
         if (idx == -1)
             return -1;
 
@@ -4435,8 +4435,6 @@ int qemu_uuid_parse(const char *str, uint8_t *uuid)
     return 0;
 }
 
-#define MAX_NET_CLIENTS 32
-
 #ifndef _WIN32
 
 static void termsig_handler(int signal)
@@ -4640,8 +4638,6 @@ int main(int argc, char **argv, char **envp)
     DisplayState *ds;
     DisplayChangeListener *dcl;
     int cyls, heads, secs, translation;
-    const char *net_clients[MAX_NET_CLIENTS];
-    int nb_net_clients;
     QemuOpts *hda_opts = NULL, *opts;
     int optind;
     const char *r, *optarg;
@@ -4742,7 +4738,6 @@ int main(int argc, char **argv, char **envp)
         node_cpumask[i] = 0;
     }
 
-    nb_net_clients = 0;
     nb_numa_nodes = 0;
     nb_nics = 0;
 
@@ -4988,12 +4983,8 @@ int main(int argc, char **argv, char **envp)
                 break;
 #endif
             case QEMU_OPTION_net:
-                if (nb_net_clients >= MAX_NET_CLIENTS) {
-                    fprintf(stderr, "qemu: too many network clients\n");
+                if (net_client_parse(optarg) == -1)
                     exit(1);
-                }
-                net_clients[nb_net_clients] = optarg;
-                nb_net_clients++;
                 break;
 #ifdef CONFIG_SLIRP
             case QEMU_OPTION_tftp:
@@ -5578,25 +5569,12 @@ int main(int argc, char **argv, char **envp)
     socket_init();
 #endif
 
-    /* init network clients */
-    if (nb_net_clients == 0) {
-        /* if no clients, we use a default config */
-        net_clients[nb_net_clients++] = "nic";
-#ifdef CONFIG_SLIRP
-        net_clients[nb_net_clients++] = "user";
-#endif
-    }
-
-    for(i = 0;i < nb_net_clients; i++) {
-        if (net_client_parse(net_clients[i]) < 0)
-            exit(1);
-    }
+    if (net_init_clients() < 0)
+        exit(1);
 
     net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
     net_set_boot_mask(net_boot);
 
-    net_client_check();
-
     /* init the bluetooth world */
     if (foreach_device_config(DEV_BT, bt_parse))
         exit(1);
Signature · 1 line
1.6.2.5
#34 lore raw
In reply to Mark McLoughlin

Hi Anthony,

Quoted 2 lines · Mark McLoughlin

Hi,
Here's a fairly straightforward port of -net to QemuOpts.

What happened to this patch series? AFAIR, it was in your staging tree
for a while but never got pushed upstream?

Cheers,
Mark.