1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-12 08:58:17 +03:00

feature #3028: Remove devices not shown in monitor from host. Recover

constness of test method. Get rid of unneeded methods
This commit is contained in:
Ruben S. Montero 2015-08-27 16:19:58 +02:00
parent b60ecc4418
commit 6757fa6672
3 changed files with 79 additions and 134 deletions

View File

@ -449,7 +449,7 @@ public:
* @return true if the share can host the VM * @return true if the share can host the VM
*/ */
bool test_capacity(long long cpu, long long mem, long long disk, bool test_capacity(long long cpu, long long mem, long long disk,
vector<Attribute *> &pci, string& error) vector<Attribute *> &pci, string& error) const
{ {
return host_share.test(cpu, mem, disk, pci, error); return host_share.test(cpu, mem, disk, pci, error);
} }

View File

@ -79,7 +79,7 @@ public:
* @param devs list of requested devices by the VM. * @param devs list of requested devices by the VM.
* @return true if all the devices are available. * @return true if all the devices are available.
*/ */
bool test(vector<Attribute *> &devs) const; bool test(const vector<Attribute *> &devs) const;
/** /**
* Assign the requested devices to the given VM. The assigned devices will * Assign the requested devices to the given VM. The assigned devices will
@ -89,10 +89,7 @@ public:
* assigned devices. * assigned devices.
* @param vmid of the VM * @param vmid of the VM
*/ */
void add(vector<Attribute *> &devs, int vmid) void add(vector<Attribute *> &devs, int vmid);
{
test_set(devs, vmid);
}
/** /**
* Remove the VM assignment from the PCI device list * Remove the VM assignment from the PCI device list
@ -117,51 +114,6 @@ private:
*/ */
int init(); int init();
/**
* Test if a PCIDevice matches the vendor, device and class request spec
* and can be assigned. If free, it is stored in the assigned set
* @param vendor_id id in uint form 0 means *
* @param device_id id in uint form 0 means *
* @param class_id id in uint form 0 means *
* @param pci requested pci device
* @param vmid if not -1 it will also assign the PCI device to the VM,
* and the pci attribute will be extended with device information.
* @param assigned set of addresses already assigned devices, it will
* include the selected device if found; useful to iterate.
*
* @return true if a device was found.
*/
bool test(unsigned int vendor_id, unsigned int device_id,
unsigned int class_id, const VectorAttribute *pci,
std::set<string> &assigned) const;
/**
* Test if a PCIDevice matches the vendor, device and class request spec
* and can be assigned. It will assign it if requested.
* @param vendor_id id in uint form 0 means *
* @param device_id id in uint form 0 means *
* @param class_id id in uint form 0 means *
* @param pci requested pci device
* @param vmid if not -1 it will also assign the PCI device to the VM,
* and the pci attribute will be extended with device information.
* @param assigned set of addresses already assigned devices, it will
* include the selected device if found; useful to iterate.
*
* @return true if a device was found.
*/
bool test_set(unsigned int vendor_id, unsigned int device_id,
unsigned int class_id, VectorAttribute *pci, int vmid,
std::set<string> &assigned) const;
/**
* Test if the given list of PCIDevices can be assigned to the VM
* @param devs, list of PCI devices
* @param vmid if not -1 it will assign the devices to the VM
*
* @return true if the PCIDevice list can be assigned.
*/
bool test_set(vector<Attribute *> &devs, int vmid) const;
/** /**
* Gets a 4 hex digits value from attribute * Gets a 4 hex digits value from attribute
* @param name of the attribute * @param name of the attribute

View File

@ -41,6 +41,8 @@ int HostSharePCI::from_xml_node(const xmlNodePtr node)
return init(); return init();
} }
/* ------------------------------------------------------------------------*/
int HostSharePCI::init() int HostSharePCI::init()
{ {
vector<Attribute *> devices; vector<Attribute *> devices;
@ -67,43 +69,19 @@ int HostSharePCI::init()
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
bool HostSharePCI::test(unsigned int vendor_id, unsigned int device_id, bool HostSharePCI::test(const vector<Attribute *> &devs) const
unsigned int class_id, const VectorAttribute * devreq,
std::set<string>& assigned) const
{ {
map<string, PCIDevice *>::const_iterator it; vector<Attribute *>::const_iterator it;
map<string, PCIDevice *>::const_iterator jt;
for (it=pci_devices.begin(); it!=pci_devices.end(); it++)
{
PCIDevice * dev = it->second;
if ((class_id == 0 || dev->class_id == class_id) &&
(vendor_id == 0 || dev->vendor_id == vendor_id) &&
(device_id == 0 || dev->device_id == device_id) &&
dev->vmid == -1 &&
assigned.find(dev->address) == assigned.end())
{
assigned.insert(dev->address);
return true;
}
}
return false;
}
/* ------------------------------------------------------------------------*/
bool HostSharePCI::test(vector<Attribute *> &devs) const
{
vector<Attribute *>::iterator it;
std::set<string> assigned; std::set<string> assigned;
unsigned int vendor_id, device_id, class_id; unsigned int vendor_id, device_id, class_id;
bool found;
for ( it=devs.begin(); it!= devs.end(); it++) for ( it=devs.begin(); it!= devs.end(); it++)
{ {
VectorAttribute * pci = dynamic_cast<VectorAttribute *>(*it); const VectorAttribute* pci = dynamic_cast<const VectorAttribute *>(*it);
if ( pci == 0 ) if ( pci == 0 )
{ {
@ -114,7 +92,24 @@ bool HostSharePCI::test(vector<Attribute *> &devs) const
device_id = get_pci_value("DEVICE", pci); device_id = get_pci_value("DEVICE", pci);
class_id = get_pci_value("CLASS", pci); class_id = get_pci_value("CLASS", pci);
if (!test(vendor_id, device_id, class_id, pci, assigned)) for (jt=pci_devices.begin(), found=false; jt!=pci_devices.end(); jt++)
{
PCIDevice * dev = jt->second;
if ((class_id == 0 || dev->class_id == class_id) &&
(vendor_id == 0 || dev->vendor_id == vendor_id) &&
(device_id == 0 || dev->device_id == device_id) &&
dev->vmid == -1 &&
assigned.find(dev->address) == assigned.end())
{
assigned.insert(dev->address);
found=true;
break;
}
}
if (!found)
{ {
return false; return false;
} }
@ -127,73 +122,47 @@ bool HostSharePCI::test(vector<Attribute *> &devs) const
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
bool HostSharePCI::test_set(unsigned int vendor_id, unsigned int device_id, void HostSharePCI::add(vector<Attribute *> &devs, int vmid)
unsigned int class_id, VectorAttribute * devreq, int vmid,
std::set<string>& assigned) const
{ {
map<string, PCIDevice *>::const_iterator it; vector<Attribute *>::iterator it;
map<string, PCIDevice *>::const_iterator jt;
for (it=pci_devices.begin(); it!=pci_devices.end(); it++) unsigned int vendor_id, device_id, class_id;
for ( it=devs.begin(); it!= devs.end(); it++)
{ {
PCIDevice * dev = it->second; VectorAttribute * pci = dynamic_cast<VectorAttribute *>(*it);
if ( pci == 0 )
{
return;
}
vendor_id = get_pci_value("VENDOR", pci);
device_id = get_pci_value("DEVICE", pci);
class_id = get_pci_value("CLASS", pci);
for (jt=pci_devices.begin(); jt!=pci_devices.end(); jt++)
{
PCIDevice * dev = jt->second;
if ((class_id == 0 || dev->class_id == class_id) && if ((class_id == 0 || dev->class_id == class_id) &&
(vendor_id == 0 || dev->vendor_id == vendor_id) && (vendor_id == 0 || dev->vendor_id == vendor_id) &&
(device_id == 0 || dev->device_id == device_id) && (device_id == 0 || dev->device_id == device_id) &&
dev->vmid == -1 && dev->vmid == -1 )
assigned.find(dev->address) == assigned.end())
{
assigned.insert(dev->address);
if (vmid != -1)
{ {
dev->vmid = vmid; dev->vmid = vmid;
dev->attrs->replace("VMID", vmid); dev->attrs->replace("VMID", vmid);
devreq->replace("DOMAIN",dev->attrs->vector_value("DOMAIN")); pci->replace("DOMAIN",dev->attrs->vector_value("DOMAIN"));
devreq->replace("BUS",dev->attrs->vector_value("BUS")); pci->replace("BUS",dev->attrs->vector_value("BUS"));
devreq->replace("SLOT",dev->attrs->vector_value("SLOT")); pci->replace("SLOT",dev->attrs->vector_value("SLOT"));
devreq->replace("FUNCTION",dev->attrs->vector_value("FUNCTION")); pci->replace("FUNCTION",dev->attrs->vector_value("FUNCTION"));
devreq->replace("ADDRESS",dev->attrs->vector_value("ADDRESS")); pci->replace("ADDRESS",dev->attrs->vector_value("ADDRESS"));
}
return true;
} }
} }
return false;
} }
/* ------------------------------------------------------------------------*/
bool HostSharePCI::test_set(vector<Attribute *> &devs, int vmid) const
{
vector<Attribute *>::iterator it;
std::set<string> assigned;
unsigned int vendor_id, device_id, class_id;
for ( it=devs.begin(); it!= devs.end(); it++)
{
VectorAttribute * pci = dynamic_cast<VectorAttribute *>(*it);
if ( pci == 0 )
{
return false;
}
vendor_id = get_pci_value("VENDOR", pci);
device_id = get_pci_value("DEVICE", pci);
class_id = get_pci_value("CLASS", pci);
if (!test_set(vendor_id, device_id, class_id, pci, vmid, assigned))
{
return false;
}
}
return true;
} }
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/
@ -233,6 +202,14 @@ void HostSharePCI::set_monitorization(vector<Attribute*> &pci_att)
string address; string address;
std::set<string> missing;
std::set<string>::iterator jt;
for (pci_it = pci_devices.begin(); pci_it != pci_devices.end(); pci_it++)
{
missing.insert(pci_it->first);
}
for (it = pci_att.begin(); it != pci_att.end(); it++) for (it = pci_att.begin(); it != pci_att.end(); it++)
{ {
VectorAttribute * pci = dynamic_cast<VectorAttribute *>(*it); VectorAttribute * pci = dynamic_cast<VectorAttribute *>(*it);
@ -254,7 +231,9 @@ void HostSharePCI::set_monitorization(vector<Attribute*> &pci_att)
if (pci_it != pci_devices.end()) if (pci_it != pci_devices.end())
{ {
missing.erase(address);
delete pci; delete pci;
continue; continue;
} }
@ -264,6 +243,20 @@ void HostSharePCI::set_monitorization(vector<Attribute*> &pci_att)
set(pci); set(pci);
} }
for ( jt = missing.begin() ; jt != missing.end(); jt ++ )
{
pci_it = pci_devices.find(*jt);
remove(pci_it->second->attrs);
delete pci_it->second->attrs;
delete pci_it->second;
pci_devices.erase(pci_it);
}
}; };
/* ------------------------------------------------------------------------*/ /* ------------------------------------------------------------------------*/