From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LWzWI-0003OE-LK for qemu-devel@nongnu.org; Tue, 10 Feb 2009 15:47:50 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LWzWI-0003O0-0K for qemu-devel@nongnu.org; Tue, 10 Feb 2009 15:47:50 -0500 Received: from [199.232.76.173] (port=52911 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LWzWH-0003Nx-QH for qemu-devel@nongnu.org; Tue, 10 Feb 2009 15:47:49 -0500 Received: from mx2.redhat.com ([66.187.237.31]:60724) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LWzWH-0008K7-7k for qemu-devel@nongnu.org; Tue, 10 Feb 2009 15:47:49 -0500 Message-Id: <20090210203206.954233132@emt.localdomain> References: <20090210203051.064692466@emt.localdomain> Date: Tue, 10 Feb 2009 18:30:55 -0200 From: Marcelo Tosatti Content-Disposition: inline; filename=dynamic-nic-info Subject: [Qemu-devel] [patch 04/19] qemu: dynamic nic info index allocation Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Anthony Liguori Cc: Marcelo Tosatti , qemu-devel Dynamically allocate nic info index, so to reuse indexes when devices are removed. Signed-off-by: Marcelo Tosatti Index: trunk/net.c =================================================================== --- trunk.orig/net.c +++ trunk/net.c @@ -1501,6 +1501,16 @@ VLANState *qemu_find_vlan(int id) return vlan; } +static int nic_get_free_idx(void) +{ + int index; + + for (index = 0; index < MAX_NICS; index++) + if (!nd_table[index].used) + return index; + return -1; +} + void qemu_check_nic_model(NICInfo *nd, const char *model) { const char *models[2]; @@ -1557,19 +1567,20 @@ int net_client_init(const char *device, if (!strcmp(device, "nic")) { NICInfo *nd; uint8_t *macaddr; + int idx = nic_get_free_idx(); - if (nb_nics >= MAX_NICS) { + if (idx == -1 || nb_nics >= MAX_NICS) { fprintf(stderr, "Too Many NICs\n"); return -1; } - nd = &nd_table[nb_nics]; + nd = &nd_table[idx]; macaddr = nd->macaddr; macaddr[0] = 0x52; macaddr[1] = 0x54; macaddr[2] = 0x00; macaddr[3] = 0x12; macaddr[4] = 0x34; - macaddr[5] = 0x56 + nb_nics; + macaddr[5] = 0x56 + idx; if (get_param_value(buf, sizeof(buf), "macaddr", p)) { if (parse_macaddr(macaddr, buf) < 0) { @@ -1582,6 +1593,7 @@ int net_client_init(const char *device, } nd->vlan = vlan; nd->name = name; + nd->used = 1; name = NULL; nb_nics++; vlan->nb_guest_devs++; Index: trunk/net.h =================================================================== --- trunk.orig/net.h +++ trunk/net.h @@ -65,6 +65,7 @@ struct NICInfo { const char *name; VLANState *vlan; void *private; + int used; }; extern int nb_nics; --