Commit 7697079b73360cef3d8f1cd014a87bd923464b26

Authored by aliguori
1 parent 7d5aca9e

qemu: dynamic nic info index allocation (Marcelo Tosatti)

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
Showing 2 changed files with 16 additions and 3 deletions
@@ -1501,6 +1501,16 @@ VLANState *qemu_find_vlan(int id) @@ -1501,6 +1501,16 @@ VLANState *qemu_find_vlan(int id)
1501 return vlan; 1501 return vlan;
1502 } 1502 }
1503 1503
  1504 +static int nic_get_free_idx(void)
  1505 +{
  1506 + int index;
  1507 +
  1508 + for (index = 0; index < MAX_NICS; index++)
  1509 + if (!nd_table[index].used)
  1510 + return index;
  1511 + return -1;
  1512 +}
  1513 +
1504 void qemu_check_nic_model(NICInfo *nd, const char *model) 1514 void qemu_check_nic_model(NICInfo *nd, const char *model)
1505 { 1515 {
1506 const char *models[2]; 1516 const char *models[2];
@@ -1557,19 +1567,20 @@ int net_client_init(const char *device, const char *p) @@ -1557,19 +1567,20 @@ int net_client_init(const char *device, const char *p)
1557 if (!strcmp(device, "nic")) { 1567 if (!strcmp(device, "nic")) {
1558 NICInfo *nd; 1568 NICInfo *nd;
1559 uint8_t *macaddr; 1569 uint8_t *macaddr;
  1570 + int idx = nic_get_free_idx();
1560 1571
1561 - if (nb_nics >= MAX_NICS) { 1572 + if (idx == -1 || nb_nics >= MAX_NICS) {
1562 fprintf(stderr, "Too Many NICs\n"); 1573 fprintf(stderr, "Too Many NICs\n");
1563 return -1; 1574 return -1;
1564 } 1575 }
1565 - nd = &nd_table[nb_nics]; 1576 + nd = &nd_table[idx];
1566 macaddr = nd->macaddr; 1577 macaddr = nd->macaddr;
1567 macaddr[0] = 0x52; 1578 macaddr[0] = 0x52;
1568 macaddr[1] = 0x54; 1579 macaddr[1] = 0x54;
1569 macaddr[2] = 0x00; 1580 macaddr[2] = 0x00;
1570 macaddr[3] = 0x12; 1581 macaddr[3] = 0x12;
1571 macaddr[4] = 0x34; 1582 macaddr[4] = 0x34;
1572 - macaddr[5] = 0x56 + nb_nics; 1583 + macaddr[5] = 0x56 + idx;
1573 1584
1574 if (get_param_value(buf, sizeof(buf), "macaddr", p)) { 1585 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1575 if (parse_macaddr(macaddr, buf) < 0) { 1586 if (parse_macaddr(macaddr, buf) < 0) {
@@ -1582,6 +1593,7 @@ int net_client_init(const char *device, const char *p) @@ -1582,6 +1593,7 @@ int net_client_init(const char *device, const char *p)
1582 } 1593 }
1583 nd->vlan = vlan; 1594 nd->vlan = vlan;
1584 nd->name = name; 1595 nd->name = name;
  1596 + nd->used = 1;
1585 name = NULL; 1597 name = NULL;
1586 nb_nics++; 1598 nb_nics++;
1587 vlan->nb_guest_devs++; 1599 vlan->nb_guest_devs++;
@@ -65,6 +65,7 @@ struct NICInfo { @@ -65,6 +65,7 @@ struct NICInfo {
65 const char *name; 65 const char *name;
66 VLANState *vlan; 66 VLANState *vlan;
67 void *private; 67 void *private;
  68 + int used;
68 }; 69 };
69 70
70 extern int nb_nics; 71 extern int nb_nics;