From patchwork Thu Jun 16 16:45:37 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 100678 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7906BB6F73 for ; Fri, 17 Jun 2011 04:24:06 +1000 (EST) Received: from localhost ([::1]:46043 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QXHEY-0004Z9-6P for incoming@patchwork.ozlabs.org; Thu, 16 Jun 2011 14:24:02 -0400 Received: from eggs.gnu.org ([140.186.70.92]:41900) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QXFhR-0004jN-66 for qemu-devel@nongnu.org; Thu, 16 Jun 2011 12:45:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QXFhO-00072o-Al for qemu-devel@nongnu.org; Thu, 16 Jun 2011 12:45:45 -0400 Received: from oxygen.pond.sub.org ([78.46.104.156]:60728) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QXFhN-000727-M5 for qemu-devel@nongnu.org; Thu, 16 Jun 2011 12:45:42 -0400 Received: from oxygen-old.pond.sub.org (unknown [IPv6:2a01:4f8:130:9021::2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "oxygen.pond.sub.org", Issuer "CAcert Class 3 Root" (verified OK)) by oxygen.pond.sub.org (Postfix) with ESMTPS id 2717AA2466; Thu, 16 Jun 2011 18:45:38 +0200 (CEST) Received: from blackfin.pond.sub.org (p5B32B718.dip.t-dialin.net [91.50.183.24]) by oxygen-old.pond.sub.org (Postfix) with ESMTPA id CCB0427717A; Thu, 16 Jun 2011 18:45:37 +0200 (CEST) Received: by blackfin.pond.sub.org (Postfix, from userid 500) id 48FFE2C; Thu, 16 Jun 2011 18:45:37 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 16 Jun 2011 18:45:37 +0200 Message-Id: <1308242737-15449-3-git-send-email-armbru@redhat.com> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1308242737-15449-1-git-send-email-armbru@redhat.com> References: <1308242737-15449-1-git-send-email-armbru@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 78.46.104.156 Cc: markmc@redhat.com, aliguori@us.ibm.com Subject: [Qemu-devel] [PATCH 2/2] Fix netdev name lookup in -device, device_add, netdev_del X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 unique. 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=10.0.2.0, restricted=n peer=virtio1 virtio1: model=virtio-net-pci,macaddr=52:54:00:12:34:56 peer=hostnet0 virtio1: net=10.0.2.0, 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 --- 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; + } + } done: if (!vc) {