Patchwork [4/4] Allow default network type to be overridden

login
register
mail settings
Submitter Anthony Liguori
Date Jan. 21, 2010, 6:48 p.m.
Message ID <1264099733-29666-5-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/43446/
State New
Headers show

Comments

Anthony Liguori - Jan. 21, 2010, 6:48 p.m.
Introduce a default option to the network device which specifies that this is
a default network device.  This approach should generalize to any other device.

The meaning of a default device is as follows: a default device is added to a
machine IIF defaults aren't disable (via -default or -nodefaults) and a
non-default device of this type hasn't been added.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 net.c |   42 +++++++++++++++++++++++++++++++++++++-----
 1 files changed, 37 insertions(+), 5 deletions(-)
Gerd Hoffmann - Jan. 22, 2010, 11 a.m.
On 01/21/10 19:48, Anthony Liguori wrote:
> Introduce a default option to the network device which specifies that this is
> a default network device.  This approach should generalize to any other device.

> The meaning of a default device is as follows: a default device is added to a
> machine IIF defaults aren't disable (via -default or -nodefaults) and a
> non-default device of this type hasn't been added.

I don't like the idea to have two different mechanisms for configuring 
defaults ([default] section + default = "on").  I'd suggest to pick one 
and apply it everythere.

cheers,
   Gerd
Anthony Liguori - Jan. 22, 2010, 2:44 p.m.
On 01/22/2010 05:00 AM, Gerd Hoffmann wrote:
> On 01/21/10 19:48, Anthony Liguori wrote:
>> Introduce a default option to the network device which specifies that 
>> this is
>> a default network device.  This approach should generalize to any 
>> other device.
>
>> The meaning of a default device is as follows: a default device is 
>> added to a
>> machine IIF defaults aren't disable (via -default or -nodefaults) and a
>> non-default device of this type hasn't been added.
>
> I don't like the idea to have two different mechanisms for configuring 
> defaults ([default] section + default = "on").  I'd suggest to pick 
> one and apply it everythere.

I agree that it's awkward.  Need to think a bit about it.

Regards,

Anthony Liguori

> cheers,
>   Gerd
>

Patch

diff --git a/net.c b/net.c
index 6ef93e6..1aa3c77 100644
--- a/net.c
+++ b/net.c
@@ -833,6 +833,10 @@  static int net_init_nic(QemuOpts *opts,
         .name = "name",                            \
         .type = QEMU_OPT_STRING,                   \
         .help = "identifier for monitor commands", \
+     }, {                                          \
+        .name = "default",                         \
+        .type = QEMU_OPT_BOOL,                     \
+        .help = "act as default network device",   \
      }
 
 typedef int (*net_client_init_func)(QemuOpts *opts,
@@ -1056,6 +1060,15 @@  int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
     const char *type;
     int i;
 
+    /* Do not create default network devices if no defaults */
+    if (!default_net) {
+        const char *opt = qemu_opt_get(opts, "default");
+
+        if (opt && strcmp(opt, "on") == 0) {
+            return 0;
+        }
+    }
+
     type = qemu_opt_get(opts, "type");
 
     if (!is_netdev) {
@@ -1320,10 +1333,22 @@  static int net_init_netdev(QemuOpts *opts, void *dummy)
 int net_init_clients(void)
 {
     if (default_net) {
-        /* if no clients, we use a default config */
-        qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
+        QemuOpts *opts;
+
+        opts = qemu_opts_create(&qemu_net_opts, NULL, 1);
+        if (opts == NULL) {
+            return -1;
+        }
+        qemu_opt_set(opts, "type", "nic");
+        qemu_opt_set(opts, "default", "on");
+
 #ifdef CONFIG_SLIRP
-        qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
+        opts = qemu_opts_create(&qemu_net_opts, NULL, 1);
+        if (opts == NULL) {
+            return -1;
+        }
+        qemu_opt_set(opts, "type", "user");
+        qemu_opt_set(opts, "default", "on");
 #endif
     }
 
@@ -1344,6 +1369,8 @@  int net_init_clients(void)
 
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
 {
+    QemuOpts *opts;
+    const char *opt;
 #if defined(CONFIG_SLIRP)
     int ret;
     if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
@@ -1351,10 +1378,15 @@  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
     }
 #endif
 
-    if (!qemu_opts_parse(opts_list, optarg, "type")) {
+    opts = qemu_opts_parse(opts_list, optarg, "type");
+    if (opts == NULL) {
         return -1;
     }
 
-    default_net = 0;
+    opt = qemu_opt_get(opts, "default");
+    if (!opt || strcmp(opt, "off") == 0) {
+        default_net = 0;
+    }
+
     return 0;
 }