From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:51025) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US0zc-0004Rk-Ka for qemu-devel@nongnu.org; Tue, 16 Apr 2013 04:11:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US0za-0007kw-Gu for qemu-devel@nongnu.org; Tue, 16 Apr 2013 04:11:56 -0400 Received: from mail-we0-x22b.google.com ([2a00:1450:400c:c03::22b]:44682) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US0za-0007km-BK for qemu-devel@nongnu.org; Tue, 16 Apr 2013 04:11:54 -0400 Received: by mail-we0-f171.google.com with SMTP id i48so153384wef.16 for ; Tue, 16 Apr 2013 01:11:53 -0700 (PDT) Date: Tue, 16 Apr 2013 10:11:50 +0200 From: Stefan Hajnoczi Message-ID: <20130416081150.GC6308@stefanha-thinkpad.redhat.com> References: <1365693118-6603-1-git-send-email-akong@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1365693118-6603-1-git-send-email-akong@redhat.com> Subject: Re: [Qemu-devel] [RFC PATCH] net: introduce monitor command to query mactables List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Amos Kong Cc: aliguori@us.ibm.com, qemu-devel@nongnu.org, laine@redhat.com, mst@redhat.com On Thu, Apr 11, 2013 at 11:11:58PM +0800, Amos Kong wrote: > +static MacTableInfo *virtio_net_query_mactable(NetClientState *nc) > +{ > + VirtIONet *n = qemu_get_nic_opaque(nc); > + MacTableInfo *info; > + StringList *str_list = NULL; > + StringList *entry; > + char str[12]; > + int i; > + > + info = g_malloc0(sizeof(*info)); > + info->name = g_strdup(nc->name); > + > + info->promisc = n->promisc; > + info->has_promisc = true; > + info->allmulti = n->allmulti; > + info->has_allmulti = true; > + info->alluni = n->alluni; > + info->has_alluni = true; > + info->nomulti = n->nomulti; > + info->has_nomulti = true; > + info->nouni = n->nouni; > + info->has_nouni = true; > + info->nobcast = n->nobcast; > + info->has_nobcast = true; > + info->multi_overflow = n->mac_table.multi_overflow; > + info->has_multi_overflow = true; > + info->uni_overflow = n->mac_table.uni_overflow; > + info->has_uni_overflow = true; > + > + for (i = 0; i < n->mac_table.first_multi; i++) { > + info->has_unicast = true; > + entry = g_malloc0(sizeof(*entry)); > + sprintf(str, > + "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", > + n->mac_table.macs[i * ETH_ALEN], > + n->mac_table.macs[i * ETH_ALEN + 1], > + n->mac_table.macs[i * ETH_ALEN + 2], > + n->mac_table.macs[i * ETH_ALEN + 3], > + n->mac_table.macs[i * ETH_ALEN + 4], > + n->mac_table.macs[i * ETH_ALEN + 5]); Buffer overflow, char str[12], but luckily... > + entry->value = g_malloc0(sizeof(String *)); > + entry->value->str = g_strdup(str); ...these lines can be replaced with g_strdup_printf(): https://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html#g-strdup-printf > diff --git a/net/net.c b/net/net.c > index 7869161..2103e7f 100644 > --- a/net/net.c > +++ b/net/net.c > @@ -964,6 +964,29 @@ void print_net_client(Monitor *mon, NetClientState *nc) > nc->info_str); > } > > +MacTableInfoList *qmp_query_mac_table(Error **errp) > +{ > + NetClientState *nc; > + MacTableInfoList *table_list = NULL; > + > + QTAILQ_FOREACH(nc, &net_clients, next) { > + MacTableInfoList *entry; > + MacTableInfo *info; > + > + if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) { > + continue; > + } > + if (nc->info->query_mac_table) { > + info = nc->info->query_mac_table(nc); > + entry = g_malloc0(sizeof(*entry)); > + entry->value = info; > + entry->next = table_list; > + table_list = entry; > + } > + } > + return table_list; > +} Please add an optional net client name argument so the user can query just a single NIC. This saves users from having to parse out a specific NIC when they just want to query one. Stefan