qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 07/19] Convert netdev client types to use an enumeration
Date: Mon,  7 Jun 2010 15:42:20 +0100	[thread overview]
Message-ID: <1275921752-29420-8-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1275921752-29420-1-git-send-email-berrange@redhat.com>

Declare an enumeration for all netdev client types, values
matching indexes in the net_client_types array. Use the
enum helpers for the string <-> int conversion of client types.

Before:

  $ qemu -net type=foo,sfs
  qemu: -net type=foo,sfs: Parameter 'type' expects a network client type

After:

  $ qemu -net type=foo,sfs
  qemu: -net type=foo,sfs: Parameter 'type' expects none, nic, user, tap, socket, dump

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 net.c |  124 ++++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 74 insertions(+), 50 deletions(-)

diff --git a/net.c b/net.c
index efa8b3d..5349001 100644
--- a/net.c
+++ b/net.c
@@ -42,6 +42,36 @@ static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
 
 int default_net = 1;
 
+enum {
+    NET_CLIENT_NONE,
+    NET_CLIENT_NIC,
+#ifdef CONFIG_SLIRP
+    NET_CLIENT_USER,
+#endif
+    NET_CLIENT_TAP,
+    NET_CLIENT_SOCKET,
+#ifdef CONFIG_VDE
+    NET_CLIENT_VDE,
+#endif
+    NET_CLIENT_DUMP,
+
+    NET_CLIENT_LAST
+};
+
+QEMU_ENUM_DECL(qemu_net_type);
+QEMU_ENUM_IMPL(qemu_net_type, NET_CLIENT_LAST,
+	       "none",
+	       "nic",
+#ifdef CONFIG_SLIRP
+	       "user",
+#endif
+	       "tap",
+	       "socket",
+#ifdef CONFIG_VDE
+	       "vde",
+#endif
+	       "dump");
+
 /***********************************************************/
 /* network device redirectors */
 
@@ -844,18 +874,15 @@ typedef int (*net_client_init_func)(QemuOpts *opts,
 #define NET_MAX_DESC 20
 
 static const struct {
-    const char *type;
     net_client_init_func init;
     QemuOptDesc desc[NET_MAX_DESC];
 } net_client_types[] = {
-    {
-        .type = "none",
+    { /* NET_CLIENT_NONE */
         .desc = {
             NET_COMMON_PARAMS_DESC,
             { /* end of list */ }
         },
-    }, {
-        .type = "nic",
+    }, { /* NET_CLIENT_NIC */
         .init = net_init_nic,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -884,8 +911,7 @@ static const struct {
             { /* end of list */ }
         },
 #ifdef CONFIG_SLIRP
-    }, {
-        .type = "user",
+    }, { /* NET_CLIENT_USER */
         .init = net_init_slirp,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -945,8 +971,7 @@ static const struct {
             { /* end of list */ }
         },
 #endif
-    }, {
-        .type = "tap",
+    }, { /* NET_CLIENT_TAP */
         .init = net_init_tap,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -988,8 +1013,7 @@ static const struct {
 #endif /* _WIN32 */
             { /* end of list */ }
         },
-    }, {
-        .type = "socket",
+    }, { /* NET_CLIENT_SOCKET */
         .init = net_init_socket,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -1013,8 +1037,7 @@ static const struct {
             { /* end of list */ }
         },
 #ifdef CONFIG_VDE
-    }, {
-        .type = "vde",
+    }, { /* NET_CLIENT_VDE */
         .init = net_init_vde,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -1038,8 +1061,7 @@ static const struct {
             { /* end of list */ }
         },
 #endif
-    }, {
-        .type = "dump",
+    }, { /* NET_CLIENT_DUMP */
         .init = net_init_dump,
         .desc = {
             NET_COMMON_PARAMS_DESC,
@@ -1055,30 +1077,41 @@ static const struct {
             { /* end of list */ }
         },
     },
-    { /* end of list */ }
 };
+verify(ARRAY_SIZE(net_client_types) == NET_CLIENT_LAST);
 
 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
 {
     const char *name;
-    const char *type;
-    int i;
+    const char *typestr;
+    VLANState *vlan = NULL;
+    int type;
 
-    type = qemu_opt_get(opts, "type");
-    if (!type) {
+    typestr = qemu_opt_get(opts, "type");
+    if (!typestr) {
         qerror_report(QERR_MISSING_PARAMETER, "type");
         return -1;
     }
+    type = qemu_net_type_from_string(typestr);
+
+    if (type < 0) {
+	char *valid = qemu_net_type_to_string_list();
+	qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
+		      valid);
+	qemu_free(valid);
+	return -1;
+    }
 
     if (is_netdev) {
-        if (strcmp(type, "tap") != 0 &&
+	if (type < 0 ||
+	    (type != NET_CLIENT_TAP &&
 #ifdef CONFIG_SLIRP
-            strcmp(type, "user") != 0 &&
+	     type != NET_CLIENT_USER &&
 #endif
 #ifdef CONFIG_VDE
-            strcmp(type, "vde") != 0 &&
+	     type != NET_CLIENT_VDE &&
 #endif
-            strcmp(type, "socket") != 0) {
+	     type != NET_CLIENT_SOCKET)) {
             qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
                           "a netdev backend type");
             return -1;
@@ -1103,35 +1136,26 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
         name = qemu_opt_get(opts, "name");
     }
 
-    for (i = 0; net_client_types[i].type != NULL; i++) {
-        if (!strcmp(net_client_types[i].type, type)) {
-            VLANState *vlan = NULL;
-
-            if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
-                return -1;
-            }
-
-            /* Do not add to a vlan if it's a -netdev or a nic with a
-             * netdev= parameter. */
-            if (!(is_netdev ||
-                  (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
-                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
-            }
+    if (qemu_opts_validate(opts, &net_client_types[type].desc[0]) == -1) {
+	return -1;
+    }
 
-            if (net_client_types[i].init) {
-                if (net_client_types[i].init(opts, mon, name, vlan) < 0) {
-                    /* TODO push error reporting into init() methods */
-                    qerror_report(QERR_DEVICE_INIT_FAILED, type);
-                    return -1;
-                }
-            }
-            return 0;
-        }
+    /* Do not add to a vlan if it's a -netdev or a nic with a
+     * netdev= parameter. */
+    if (!(is_netdev ||
+	  (type == NET_CLIENT_NIC && qemu_opt_get(opts, "netdev")))) {
+	vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
     }
 
-    qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
-                  "a network client type");
-    return -1;
+    if (net_client_types[type].init) {
+	if (net_client_types[type].init(opts, mon, name, vlan) < 0) {
+	    /* TODO push error reporting into init() methods */
+	    qerror_report(QERR_DEVICE_INIT_FAILED,
+			  qemu_net_type_to_string(type));
+	    return -1;
+	}
+    }
+    return 0;
 }
 
 static int net_host_check_device(const char *device)
-- 
1.6.6.1

  parent reply	other threads:[~2010-06-07 14:43 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-07 14:42 [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 01/19] Add support for JSON pretty printing Daniel P. Berrange
2010-06-09 19:51   ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 02/19] Add support for compile time assertions Daniel P. Berrange
2010-06-07 15:35   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 03/19] Add enum handlers for easy & efficient string <-> int conversion Daniel P. Berrange
2010-06-09 19:52   ` Luiz Capitulino
2010-06-26  6:52   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 04/19] Add support for a option parameter as an enum Daniel P. Berrange
2010-06-26  6:59   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 05/19] Ensure that QEMU exits if drive_add parsing fails Daniel P. Berrange
2010-06-26  7:04   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 06/19] Convert drive options to use enumeration data type Daniel P. Berrange
2010-06-09 19:52   ` Luiz Capitulino
2010-06-26  7:07   ` Markus Armbruster
2010-06-07 14:42 ` Daniel P. Berrange [this message]
2010-06-07 15:09   ` [Qemu-devel] [PATCH 07/19] Convert netdev client types to use an enumeration Anthony Liguori
2010-06-07 15:13     ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 08/19] Convert RTC to use enumerations for configuration parameters Daniel P. Berrange
2010-06-09 19:54   ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 09/19] Change 'query-version' to output broken down version string Daniel P. Berrange
2010-06-07 15:11   ` Anthony Liguori
2010-06-09 20:04     ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 10/19] Add a query-machines command to QMP Daniel P. Berrange
2010-06-07 15:13   ` Anthony Liguori
2010-06-07 16:44     ` Daniel P. Berrange
2010-06-07 17:07       ` Anthony Liguori
2010-06-07 17:14         ` Daniel P. Berrange
2010-06-09 20:06         ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 11/19] Add a query-devices " Daniel P. Berrange
2010-06-07 15:14   ` Anthony Liguori
2010-06-07 16:03     ` Daniel P. Berrange
2010-06-26  7:12       ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 12/19] Add a query-cputypes " Daniel P. Berrange
2010-06-26  7:18   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 13/19] Add a query-target " Daniel P. Berrange
2010-06-07 15:43   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 14/19] Add a query-argv " Daniel P. Berrange
2010-06-07 15:01   ` Anthony Liguori
2010-06-10 15:34     ` [Qemu-devel] " Paolo Bonzini
2010-06-26  7:30     ` [Qemu-devel] " Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 15/19] Expand query-argv to include help string Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 16/19] Add a query-netdev command to QMP Daniel P. Berrange
2010-06-07 15:15   ` Anthony Liguori
2010-06-26  7:32     ` Markus Armbruster
2010-06-07 19:13   ` Miguel Di Ciurcio Filho
2010-06-08  8:38     ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 17/19] Add a query-config " Daniel P. Berrange
2010-06-26  7:34   ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 18/19] Add option to turn on JSON pretty printing in monitor Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 19/19] Add a -capabilities argument to allow easy query for static QEMU info Daniel P. Berrange
2010-06-07 16:04   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:09     ` Daniel P. Berrange
2010-06-07 16:04   ` [Qemu-devel] " Anthony Liguori
2010-06-07 14:58 ` [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Anthony Liguori
2010-06-07 15:10   ` Daniel P. Berrange
2010-06-07 15:18     ` Anthony Liguori
2010-06-07 16:02   ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:03     ` Anthony Liguori
2010-06-07 16:07 ` [Qemu-devel] " Anthony Liguori
2010-06-09 20:25   ` Luiz Capitulino
2010-06-26  6:45 ` Markus Armbruster

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=1275921752-29420-8-git-send-email-berrange@redhat.com \
    --to=berrange@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).