]> git.proxmox.com Git - qemu.git/commitdiff
qemu: dynamic nic info index allocation (Marcelo Tosatti)
authoraliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
Wed, 11 Feb 2009 15:20:03 +0000 (15:20 +0000)
committeraliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
Wed, 11 Feb 2009 15:20:03 +0000 (15:20 +0000)
Dynamically allocate nic info index, so to reuse indexes when devices are
removed.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6595 c046a42c-6fe2-441c-8c8c-71466251a162

net.c
net.h

diff --git a/net.c b/net.c
index e7c097e1c8ba69e0e96aca7ce986f1086c850cc4..6edeee81da80fb19dddb0fc2575a756a80f3a448 100644 (file)
--- a/net.c
+++ b/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, const char *p)
     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, const char *p)
         }
         nd->vlan = vlan;
         nd->name = name;
+        nd->used = 1;
         name = NULL;
         nb_nics++;
         vlan->nb_guest_devs++;
diff --git a/net.h b/net.h
index 84c3454746605fe8c628064484799a9e0e1df70d..6ddd06fa1a06c8be1d50c3efeacb8262372ac646 100644 (file)
--- a/net.h
+++ b/net.h
@@ -65,6 +65,7 @@ struct NICInfo {
     const char *name;
     VLANState *vlan;
     void *private;
+    int used;
 };
 
 extern int nb_nics;