[2/2] Fix netdev name lookup in -device, device_add, netdev_del

Message ID 1308242737-15449-3-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster June 16, 2011, 4:45 p.m.
qemu_find_netdev() looks up members of non_vlan_clients by name.  It
happily returns the first match.  Trouble is the names need not be

non_vlan_clients contains host parts (netdevs) and guest parts (NICs).

Netdevs have unique names: a netdev's name is a (mandatory)
qemu_netdev_opts ID, and these are unique.

NIC names are not unique.  If a NIC has a qdev ID (which is unique),
that's its name.  Else, we make up a name.  The made-up names are
unique, but they can clash with qdev IDs.  Even if NICs had unique
names, they could still clash with netdev names.

Callers of qemu_find_netdev():

* net_init_nic() wants a netdev.  It happens to work because it runs
  before NICs get added to non_vlan_clients.

* do_netdev_del() wants a netdev.  If it gets a NIC, it complains and
  fails.  Bug: a netdev with the same name that comes later in
  non_vlan_clients can't be deleted:

    $ qemu-system-x86_64 -nodefaults -vnc :0 -S -monitor stdio -netdev user,id=hostnet0 -device virtio-net-pci,netdev=hostnet0,id=virtio1
    (qemu) netdev_add user,id=virtio1
    (qemu) info network
    Devices not on any VLAN:
      hostnet0: net=, restricted=n peer=virtio1
      virtio1: model=virtio-net-pci,macaddr=52:54:00:12:34:56 peer=hostnet0
      virtio1: net=, restricted=n
    (qemu) netdev_del virtio1
    Device 'virtio1' not found

* parse_netdev() wants a netdev.  If it gets a NIC, it gets confused.
  With the test setup above:

    (qemu) device_add virtio-net-pci,netdev=virtio1
    Property 'virtio-net-pci.netdev' can't take value 'virtio1', it's in use

  You can even connect two NICs to each other:

    $ qemu-system-x86_64 -nodefaults -vnc :0 -S -monitor stdio -device virtio-net-pci,id=virtio1 -device e1000,netdev=virtio1
    Devices not on any VLAN:
      virtio1: model=virtio-net-pci,macaddr=52:54:00:12:34:56 peer=e1000.0
      e1000.0: model=e1000,macaddr=52:54:00:12:34:57 peer=virtio1
    (qemu) q
    Segmentation fault (core dumped)

* do_set_link() works fine for both netdevs and NICs.  Whether it
  really makes sense for netdevs is debatable, but that's outside this
  patch's scope.

Change qemu_find_netdev() to return only netdevs.  This fixes the
netdev_del and device_add/-device bugs demonstrated above.

To avoid changing set_link, make do_set_link() search non_vlan_clients
by hand instead of calling qemu_find_netdev().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
 net.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)


diff --git a/net.c b/net.c
index 8754627..c5c4b6b 100644
--- a/net.c
+++ b/net.c
@@ -658,6 +658,8 @@  VLANClientState *qemu_find_netdev(const char *id)
     VLANClientState *vc;
     QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
+        if (vc->info->type == NET_CLIENT_TYPE_NIC)
+            continue;
         if (!strcmp(vc->name, id)) {
             return vc;
@@ -1217,7 +1219,7 @@  int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
     VLANClientState *vc;
     vc = qemu_find_netdev(id);
-    if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
+    if (!vc) {
         qerror_report(QERR_DEVICE_NOT_FOUND, id);
         return -1;
@@ -1262,7 +1264,11 @@  int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
-    vc = qemu_find_netdev(name);
+    QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
+        if (!strcmp(vc->name, name)) {
+            goto done;
+        }
+    }
     if (!vc) {