From patchwork Fri Jul 20 12:01:39 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 172210 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E05622C00D3 for ; Fri, 20 Jul 2012 22:02:47 +1000 (EST) Received: from localhost ([::1]:54259 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsBuv-0004Uz-T8 for incoming@patchwork.ozlabs.org; Fri, 20 Jul 2012 08:02:45 -0400 Received: from eggs.gnu.org ([208.118.235.92]:44373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsBuU-00043r-3u for qemu-devel@nongnu.org; Fri, 20 Jul 2012 08:02:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SsBuK-0003U4-BI for qemu-devel@nongnu.org; Fri, 20 Jul 2012 08:02:18 -0400 Received: from e06smtp12.uk.ibm.com ([195.75.94.108]:56223) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SsBuJ-0003Tj-W7 for qemu-devel@nongnu.org; Fri, 20 Jul 2012 08:02:08 -0400 Received: from /spool/local by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 20 Jul 2012 13:02:06 +0100 Received: from d06nrmr1707.portsmouth.uk.ibm.com (9.149.39.225) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 20 Jul 2012 13:02:04 +0100 Received: from d06av04.portsmouth.uk.ibm.com (d06av04.portsmouth.uk.ibm.com [9.149.37.216]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6KC23WL639022 for ; Fri, 20 Jul 2012 13:02:03 +0100 Received: from d06av04.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q6KC23YM014475 for ; Fri, 20 Jul 2012 06:02:03 -0600 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.145]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q6KC23jB014470; Fri, 20 Jul 2012 06:02:03 -0600 From: Stefan Hajnoczi To: Date: Fri, 20 Jul 2012 13:01:39 +0100 Message-Id: <1342785709-3152-7-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1342785709-3152-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1342785709-3152-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12072012-8372-0000-0000-0000033E9A04 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.108 Cc: Paolo Bonzini , Zhi Yong Wu , lersek@redhat.com, Stefan Hajnoczi , Zhi Yong Wu Subject: [Qemu-devel] [PATCH 06/16] net: Convert qdev_prop_vlan to peer with hub 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 From: Zhi Yong Wu Instead of using VLANState use net/hub.h to support the vlan qdev property. The vlan qdev property becomes an alias for the peer qdev property but is represented as a VLAN ID number. When a VLAN ID is selected the device will really peer with a hub port. Signed-off-by: Zhi Yong Wu Signed-off-by: Stefan Hajnoczi --- hw/qdev-properties.c | 37 ++++++++++++++++++++++++------------- hw/qdev.h | 2 +- net.h | 2 +- net/hub.c | 25 +++++++++++++++++++++++++ net/hub.h | 1 + 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index 24b39e8..6ee1644 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -3,6 +3,7 @@ #include "qerror.h" #include "blockdev.h" #include "hw/block-common.h" +#include "net/hub.h" void *qdev_get_prop_ptr(DeviceState *dev, Property *prop) { @@ -624,13 +625,16 @@ PropertyInfo qdev_prop_netdev = { static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len) { - VLANState **ptr = qdev_get_prop_ptr(dev, prop); + VLANClientState **ptr = qdev_get_prop_ptr(dev, prop); if (*ptr) { - return snprintf(dest, len, "%d", (*ptr)->id); - } else { - return snprintf(dest, len, ""); + unsigned int id; + if (!net_hub_id_for_client(*ptr, &id)) { + return snprintf(dest, len, "%u", id); + } } + + return snprintf(dest, len, ""); } static void get_vlan(Object *obj, Visitor *v, void *opaque, @@ -638,11 +642,17 @@ static void get_vlan(Object *obj, Visitor *v, void *opaque, { DeviceState *dev = DEVICE(obj); Property *prop = opaque; - VLANState **ptr = qdev_get_prop_ptr(dev, prop); - int64_t id; + VLANClientState **ptr = qdev_get_prop_ptr(dev, prop); + int64_t id = -1; - id = *ptr ? (*ptr)->id : -1; - visit_type_int64(v, &id, name, errp); + if (*ptr) { + unsigned int hub_id; + if (!net_hub_id_for_client(*ptr, &hub_id)) { + id = (int64_t)hub_id; + } + } + + visit_type_int(v, &id, name, errp); } static void set_vlan(Object *obj, Visitor *v, void *opaque, @@ -650,10 +660,10 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque, { DeviceState *dev = DEVICE(obj); Property *prop = opaque; - VLANState **ptr = qdev_get_prop_ptr(dev, prop); + VLANClientState **ptr = qdev_get_prop_ptr(dev, prop); Error *local_err = NULL; int64_t id; - VLANState *vlan; + VLANClientState *hubport; if (dev->state != DEV_STATE_CREATED) { error_set(errp, QERR_PERMISSION_DENIED); @@ -669,13 +679,14 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque, *ptr = NULL; return; } - vlan = qemu_find_vlan(id, 1); - if (!vlan) { + + hubport = net_hub_port_find(id); + if (!hubport) { error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, prop->info->name); return; } - *ptr = vlan; + *ptr = hubport; } PropertyInfo qdev_prop_vlan = { diff --git a/hw/qdev.h b/hw/qdev.h index 247dd1e..d47c463 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -292,7 +292,7 @@ extern PropertyInfo qdev_prop_pci_host_devaddr; #define DEFINE_PROP_NETDEV(_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*) #define DEFINE_PROP_VLAN(_n, _s, _f) \ - DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*) + DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANClientState*) #define DEFINE_PROP_DRIVE(_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *) #define DEFINE_PROP_MACADDR(_n, _s, _f) \ diff --git a/net.h b/net.h index e9c92b2..4bccead 100644 --- a/net.h +++ b/net.h @@ -24,7 +24,7 @@ typedef struct NICConf { #define DEFINE_NIC_PROPERTIES(_state, _conf) \ DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ - DEFINE_PROP_VLAN("vlan", _state, _conf.vlan), \ + DEFINE_PROP_VLAN("vlan", _state, _conf.peer), \ DEFINE_PROP_NETDEV("netdev", _state, _conf.peer), \ DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1) diff --git a/net/hub.c b/net/hub.c index 5d6bbf6..6bd730f 100644 --- a/net/hub.c +++ b/net/hub.c @@ -189,6 +189,31 @@ VLANClientState *net_hub_find_client_by_name(unsigned int hub_id, } /** + * Find a available port on a hub; otherwise create one new port + */ +VLANClientState *net_hub_port_find(unsigned int hub_id) +{ + NetHub *hub; + NetHubPort *port; + VLANClientState *nc; + + QLIST_FOREACH(hub, &hubs, next) { + if (hub->id == hub_id) { + QLIST_FOREACH(port, &hub->ports, next) { + nc = port->nc.peer; + if (!nc) { + return &(port->nc); + } + } + break; + } + } + + nc = net_hub_add_port(hub_id, NULL); + return nc; +} + +/** * Print hub configuration */ void net_hub_info(Monitor *mon) diff --git a/net/hub.h b/net/hub.h index 71dbcd0..20bda21 100644 --- a/net/hub.h +++ b/net/hub.h @@ -25,5 +25,6 @@ VLANClientState *net_hub_find_client_by_name(unsigned int hub_id, void net_hub_info(Monitor *mon); int net_hub_id_for_client(VLANClientState *nc, unsigned int *id); void net_hub_check_clients(void); +VLANClientState *net_hub_port_find(unsigned int hub_id); #endif /* NET_HUB_H */