From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LUi4o-0002kI-27 for qemu-devel@nongnu.org; Wed, 04 Feb 2009 08:46:02 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LUi4m-0002jd-Rd for qemu-devel@nongnu.org; Wed, 04 Feb 2009 08:46:01 -0500 Received: from [199.232.76.173] (port=37308 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LUi4m-0002jL-EW for qemu-devel@nongnu.org; Wed, 04 Feb 2009 08:46:00 -0500 Received: from mx2.redhat.com ([66.187.237.31]:34123) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LUi4l-0000d0-N9 for qemu-devel@nongnu.org; Wed, 04 Feb 2009 08:46:00 -0500 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n14DjxZv010617 for ; Wed, 4 Feb 2009 08:45:59 -0500 Message-Id: <20090204133923.489981298@localhost.localdomain> References: <20090204133303.113145633@localhost.localdomain> Date: Wed, 04 Feb 2009 11:33:07 -0200 From: Marcelo Tosatti Content-Disposition: inline; filename=dynamic-nic-info Subject: [Qemu-devel] [patch 04/18] 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: qemu-devel@nongnu.org Cc: Marcelo Tosatti 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 @@ -1515,6 +1515,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]; @@ -1571,19 +1581,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) { @@ -1596,6 +1607,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; --