From patchwork Thu Jun 13 09:03:03 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 251013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E65A02C009C for ; Thu, 13 Jun 2013 19:05:00 +1000 (EST) Received: from localhost ([::1]:41190 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Un3Sk-000645-Lc for incoming@patchwork.ozlabs.org; Thu, 13 Jun 2013 05:04:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51839) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Un3SF-0005s6-ST for qemu-devel@nongnu.org; Thu, 13 Jun 2013 05:04:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Un3SD-0003Yx-Ty for qemu-devel@nongnu.org; Thu, 13 Jun 2013 05:04:27 -0400 Received: from mail-ie0-x22e.google.com ([2607:f8b0:4001:c03::22e]:63606) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Un3SD-0003Yi-MO for qemu-devel@nongnu.org; Thu, 13 Jun 2013 05:04:25 -0400 Received: by mail-ie0-f174.google.com with SMTP id 9so10408436iec.33 for ; Thu, 13 Jun 2013 02:04:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=1nhz+T4Ev8wUTC26gHYiCmIxR5AOz0l2B5q9hqLauxo=; b=HbGfL+M6PKrbbRw/FulJUnFzFUOwClMUSSLnqMTLPfsJeSSWqBaaiMLIfuClQGm07N xknCNAYJZ3f4YEIsCchpCiFzkprOu8MmMKbcHUCwUiSseG8OJx4/F0CJyTPDXYfoUotl /5VpM2ShwGGEvqsXeZI7e4HEpzHHFIVyb9zg6JspfVxQ9Z2r+sRBAAkhP1HZ+xzzdurC LjkEAM0CCO0zcAwQG2Z1PS7360TSM6CcE/IFK/jt6GEQb1eau+m05sB5NLKGi6o479Fn vWKFJKZEnG35QRhZ8U4shIaLUw2+BZ2vtY7rsmMe7AMjw4j3ODOMj8x9EGRlNMQDzfBa 3QYA== X-Received: by 10.50.67.110 with SMTP id m14mr5030148igt.37.1371114265101; Thu, 13 Jun 2013 02:04:25 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id ie16sm5515373igb.4.2013.06.13.02.04.21 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 13 Jun 2013 02:04:24 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Thu, 13 Jun 2013 17:03:03 +0800 Message-Id: <1371114186-8854-4-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1371114186-8854-1-git-send-email-qemulist@gmail.com> References: <1371114186-8854-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c03::22e Cc: mdroth , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2 3/6] net: make netclient re-entrant with refcnt 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: Liu Ping Fan With refcnt, NetClientState's user can run agaist deleter. Signed-off-by: Liu Ping Fan --- hw/core/qdev-properties-system.c | 14 ++++++++++++ include/net/net.h | 3 +++ net/hub.c | 3 +++ net/net.c | 47 +++++++++++++++++++++++++++++++++++++--- net/slirp.c | 3 ++- 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c index 0eada32..41cc7e6 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -302,6 +302,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque, return; } + /* inc ref, released when unset property */ hubport = net_hub_port_find(id); if (!hubport) { error_set(errp, QERR_INVALID_PARAMETER_VALUE, @@ -311,11 +312,24 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque, *ptr = hubport; } +static void release_vlan(Object *obj, const char *name, void *opaque) +{ + DeviceState *dev = DEVICE(obj); + Property *prop = opaque; + NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop); + NetClientState **ptr = &peers_ptr->ncs[0]; + + if (*ptr) { + netclient_unref(*ptr); + } +} + PropertyInfo qdev_prop_vlan = { .name = "vlan", .print = print_vlan, .get = get_vlan, .set = set_vlan, + .release = release_vlan, }; int qdev_prop_set_drive(DeviceState *dev, const char *name, diff --git a/include/net/net.h b/include/net/net.h index ea46f13..1a31d1b 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -64,6 +64,7 @@ typedef struct NetClientInfo { } NetClientInfo; struct NetClientState { + int ref; NetClientInfo *info; int link_down; QTAILQ_ENTRY(NetClientState) next; @@ -92,6 +93,8 @@ typedef struct NICState { NetClientState *qemu_find_netdev(const char *id); int qemu_find_net_clients_except(const char *id, NetClientState **ncs, NetClientOptionsKind type, int max); +void netclient_ref(NetClientState *nc); +void netclient_unref(NetClientState *nc); NetClientState *qemu_new_net_client(NetClientInfo *info, NetClientState *peer, const char *model, diff --git a/net/hub.c b/net/hub.c index df32074..9c6c559 100644 --- a/net/hub.c +++ b/net/hub.c @@ -201,6 +201,7 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name) peer = port->nc.peer; if (peer && strcmp(peer->name, name) == 0) { + netclient_ref(peer); return peer; } } @@ -223,6 +224,7 @@ NetClientState *net_hub_port_find(int hub_id) QLIST_FOREACH(port, &hub->ports, next) { nc = port->nc.peer; if (!nc) { + netclient_ref(&port->nc); return &(port->nc); } } @@ -231,6 +233,7 @@ NetClientState *net_hub_port_find(int hub_id) } nc = net_hub_add_port(hub_id, NULL); + netclient_ref(nc); return nc; } diff --git a/net/net.c b/net/net.c index 717db12..478a719 100644 --- a/net/net.c +++ b/net/net.c @@ -45,6 +45,7 @@ # define CONFIG_NET_BRIDGE #endif +static QemuMutex net_clients_lock; static QTAILQ_HEAD(, NetClientState) net_clients; int default_net = 1; @@ -165,6 +166,7 @@ static char *assign_name(NetClientState *nc1, const char *model) char buf[256]; int id = 0; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc == nc1) { continue; @@ -173,6 +175,7 @@ static char *assign_name(NetClientState *nc1, const char *model) id++; } } + qemu_mutex_unlock(&net_clients_lock); snprintf(buf, sizeof(buf), "%s.%d", model, id); @@ -203,9 +206,13 @@ static void qemu_net_client_setup(NetClientState *nc, assert(!peer->peer); nc->peer = peer; peer->peer = nc; + netclient_ref(peer); + netclient_ref(nc); } qemu_mutex_init(&nc->peer_lock); + qemu_mutex_lock(&net_clients_lock); QTAILQ_INSERT_TAIL(&net_clients, nc, next); + qemu_mutex_unlock(&net_clients_lock); nc->send_queue = qemu_new_net_queue(nc); nc->destructor = destructor; @@ -221,6 +228,7 @@ NetClientState *qemu_new_net_client(NetClientInfo *info, assert(info->size >= sizeof(NetClientState)); nc = g_malloc0(info->size); + netclient_ref(nc); qemu_net_client_setup(nc, info, peer, model, name, qemu_net_client_destructor); @@ -281,7 +289,9 @@ void *qemu_get_nic_opaque(NetClientState *nc) static void qemu_cleanup_net_client(NetClientState *nc) { + qemu_mutex_lock(&net_clients_lock); QTAILQ_REMOVE(&net_clients, nc, next); + qemu_mutex_unlock(&net_clients_lock); if (nc->info->cleanup) { nc->info->cleanup(nc); @@ -303,6 +313,18 @@ static void qemu_free_net_client(NetClientState *nc) } } +void netclient_ref(NetClientState *nc) +{ + __sync_add_and_fetch(&nc->ref, 1); +} + +void netclient_unref(NetClientState *nc) +{ + if (__sync_sub_and_fetch(&nc->ref, 1) == 0) { + qemu_free_net_client(nc); + } +} + /* elimate the reference and sync with exit of rx/tx action. * And flush out peer's queue. */ @@ -331,8 +353,10 @@ static void qemu_net_client_detach_flush(NetClientState *nc) nc->peer = NULL; if (peer) { qemu_net_queue_purge(peer->send_queue, nc); + netclient_unref(peer); } qemu_mutex_unlock(&nc->peer_lock); + netclient_unref(nc); } void qemu_del_net_client(NetClientState *nc) @@ -378,7 +402,7 @@ void qemu_del_net_client(NetClientState *nc) for (i = 0; i < queues; i++) { qemu_net_client_detach_flush(ncs[i]); qemu_cleanup_net_client(ncs[i]); - qemu_free_net_client(ncs[i]); + netclient_unref(ncs[i]); } } @@ -389,7 +413,7 @@ void qemu_del_nic(NICState *nic) /* If this is a peer NIC and peer has already been deleted, free it now. */ if (nic->peer_deleted) { for (i = 0; i < queues; i++) { - qemu_free_net_client(nic->pending_peer[i]); + netclient_unref(nic->pending_peer[i]); } } @@ -398,7 +422,7 @@ void qemu_del_nic(NICState *nic) qemu_net_client_detach_flush(nc); qemu_cleanup_net_client(nc); - qemu_free_net_client(nc); + netclient_unref(nc); } g_free(nic->pending_peer); @@ -409,6 +433,7 @@ void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) { NetClientState *nc; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { if (nc->queue_index == 0) { @@ -416,6 +441,7 @@ void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) } } } + qemu_mutex_unlock(&net_clients_lock); } int qemu_can_send_packet_nolock(NetClientState *sender) @@ -630,13 +656,17 @@ NetClientState *qemu_find_netdev(const char *id) { NetClientState *nc; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) continue; if (!strcmp(nc->name, id)) { + netclient_ref(nc); + qemu_mutex_unlock(&net_clients_lock); return nc; } } + qemu_mutex_unlock(&net_clients_lock); return NULL; } @@ -647,6 +677,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs, NetClientState *nc; int ret = 0; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == type) { continue; @@ -658,6 +689,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs, ret++; } } + qemu_mutex_unlock(&net_clients_lock); return ret; } @@ -963,9 +995,11 @@ void net_host_device_remove(Monitor *mon, const QDict *qdict) } if (!net_host_check_device(nc->model)) { monitor_printf(mon, "invalid host network device %s\n", device); + netclient_unref(nc); return; } qemu_del_net_client(nc); + netclient_unref(nc); } void netdev_add(QemuOpts *opts, Error **errp) @@ -1021,6 +1055,7 @@ void qmp_netdev_del(const char *id, Error **errp) } qemu_del_net_client(nc); + netclient_unref(nc); qemu_opts_del(opts); } @@ -1039,6 +1074,7 @@ void do_info_network(Monitor *mon, const QDict *qdict) net_hub_info(mon); + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { peer = nc->peer; type = nc->info->type; @@ -1056,6 +1092,7 @@ void do_info_network(Monitor *mon, const QDict *qdict) print_net_client(mon, peer); } } + qemu_mutex_unlock(&net_clients_lock); } void qmp_set_link(const char *name, bool up, Error **errp) @@ -1109,6 +1146,7 @@ void net_cleanup(void) qemu_del_net_client(nc); } } + qemu_mutex_destroy(&net_clients_lock); } void net_check_clients(void) @@ -1130,6 +1168,7 @@ void net_check_clients(void) net_hub_check_clients(); + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (!nc->peer) { fprintf(stderr, "Warning: %s %s has no peer\n", @@ -1137,6 +1176,7 @@ void net_check_clients(void) "nic" : "netdev", nc->name); } } + qemu_mutex_unlock(&net_clients_lock); /* Check that all NICs requested via -net nic actually got created. * NICs created via -device don't need to be checked here because @@ -1194,6 +1234,7 @@ int net_init_clients(void) #endif } + qemu_mutex_init(&net_clients_lock); QTAILQ_INIT(&net_clients); if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1) diff --git a/net/slirp.c b/net/slirp.c index b3f35d5..e541548 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -346,7 +346,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, host_addr, host_port); - + netclient_unref(&s->nc); monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, err ? "not found" : "removed"); return; @@ -437,6 +437,7 @@ void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) } if (s) { slirp_hostfwd(s, redir_str, 0); + netclient_unref(&s->nc); } }