Merge remote-tracking branch 'apache/4.17' into main

This commit is contained in:
Abhishek Kumar 2022-09-06 18:47:47 +05:30
commit 687a21c116
5 changed files with 94 additions and 24 deletions

View File

@ -16,21 +16,33 @@
// under the License. // under the License.
package org.apache.cloudstack.utils.linux; package org.apache.cloudstack.utils.linux;
import com.cloud.hypervisor.kvm.resource.LibvirtCapXMLParser; import java.io.FileReader;
import com.cloud.hypervisor.kvm.resource.LibvirtConnection; import java.io.IOException;
import com.cloud.utils.script.Script; import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.cloudstack.utils.security.ParserUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.libvirt.Connect; import org.libvirt.Connect;
import org.libvirt.LibvirtException; import org.libvirt.LibvirtException;
import org.libvirt.NodeInfo; import org.libvirt.NodeInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.FileReader; import com.cloud.hypervisor.kvm.resource.LibvirtCapXMLParser;
import java.io.IOException; import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
import java.io.Reader; import com.cloud.utils.script.Script;
import java.util.ArrayList;
import java.util.List;
public class KVMHostInfo { public class KVMHostInfo {
@ -82,7 +94,7 @@ public class KVMHostInfo {
return this.capabilities; return this.capabilities;
} }
protected static long getCpuSpeed(final NodeInfo nodeInfo) { protected static long getCpuSpeed(final String cpabilities, final NodeInfo nodeInfo) {
long speed = 0L; long speed = 0L;
speed = getCpuSpeedFromCommandLscpu(); speed = getCpuSpeedFromCommandLscpu();
if(speed > 0L) { if(speed > 0L) {
@ -94,6 +106,11 @@ public class KVMHostInfo {
return speed; return speed;
} }
speed = getCpuSpeedFromHostCapabilities(cpabilities);
if(speed > 0L) {
return speed;
}
LOGGER.info(String.format("Using the value [%s] provided by Libvirt.", nodeInfo.mhz)); LOGGER.info(String.format("Using the value [%s] provided by Libvirt.", nodeInfo.mhz));
speed = nodeInfo.mhz; speed = nodeInfo.mhz;
return speed; return speed;
@ -125,12 +142,41 @@ public class KVMHostInfo {
} }
} }
protected static long getCpuSpeedFromHostCapabilities(final String capabilities) {
LOGGER.info("Fetching CPU speed from \"host capabilities\"");
long speed = 0L;
try {
DocumentBuilderFactory docFactory = ParserUtils.getSaferDocumentBuilderFactory();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(capabilities)));
Element rootElement = doc.getDocumentElement();
NodeList nodes = rootElement.getElementsByTagName("cpu");
Node node = nodes.item(0);
nodes = ((Element)node).getElementsByTagName("counter");
for (int i = 0; i < nodes.getLength(); i++) {
node = nodes.item(i);
NamedNodeMap attributes = node.getAttributes();
Node nameNode = attributes.getNamedItem("name");
Node freqNode = attributes.getNamedItem("frequency");
if (nameNode != null && "tsc".equals(nameNode.getNodeValue()) && freqNode != null && StringUtils.isNotEmpty(freqNode.getNodeValue())) {
speed = Long.parseLong(freqNode.getNodeValue()) / 1000000;
LOGGER.info(String.format("Retrieved value [%s] from \"host capabilities\". This corresponds to a CPU speed of [%s] MHz.", freqNode.getNodeValue(), speed));
}
}
} catch (Exception ex) {
LOGGER.error("Unable to fetch CPU speed from \"host capabilities\"", ex);
speed = 0L;
}
return speed;
}
private void getHostInfoFromLibvirt() { private void getHostInfoFromLibvirt() {
try { try {
final Connect conn = LibvirtConnection.getConnection(); final Connect conn = LibvirtConnection.getConnection();
final NodeInfo hosts = conn.nodeInfo(); final NodeInfo hosts = conn.nodeInfo();
final String capabilities = conn.getCapabilities();
if (this.cpuSpeed == 0) { if (this.cpuSpeed == 0) {
this.cpuSpeed = getCpuSpeed(hosts); this.cpuSpeed = getCpuSpeed(capabilities, hosts);
} else { } else {
LOGGER.debug(String.format("Using existing configured CPU frequency %s", this.cpuSpeed)); LOGGER.debug(String.format("Using existing configured CPU frequency %s", this.cpuSpeed));
} }
@ -146,7 +192,7 @@ public class KVMHostInfo {
this.cpus = hosts.cpus; this.cpus = hosts.cpus;
final LibvirtCapXMLParser parser = new LibvirtCapXMLParser(); final LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
parser.parseCapabilitiesXML(conn.getCapabilities()); parser.parseCapabilitiesXML(capabilities);
final ArrayList<String> oss = parser.getGuestOsType(); final ArrayList<String> oss = parser.getGuestOsType();
for (final String s : oss) { for (final String s : oss) {
/* /*

View File

@ -16,23 +16,22 @@
// under the License. // under the License.
package org.apache.cloudstack.utils.linux; package org.apache.cloudstack.utils.linux;
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.Assume;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.libvirt.Connect; import org.libvirt.Connect;
import org.mockito.Mockito;
import org.libvirt.NodeInfo; import org.libvirt.NodeInfo;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest(value = {LibvirtConnection.class}) @PrepareForTest(value = {LibvirtConnection.class})
@PowerMockIgnore({"javax.xml.*", "org.w3c.dom.*", "org.apache.xerces.*", "org.xml.*"}) @PowerMockIgnore({"javax.xml.*", "org.w3c.dom.*", "org.apache.xerces.*", "org.xml.*"})
@ -45,7 +44,21 @@ public class KVMHostInfoTest {
Assume.assumeTrue(SystemUtils.IS_OS_LINUX); Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
NodeInfo nodeInfo = Mockito.mock(NodeInfo.class); NodeInfo nodeInfo = Mockito.mock(NodeInfo.class);
nodeInfo.mhz = 1000; nodeInfo.mhz = 1000;
Assert.assertThat(KVMHostInfo.getCpuSpeed(nodeInfo), Matchers.greaterThan(0l)); Assert.assertThat(KVMHostInfo.getCpuSpeed(null, nodeInfo), Matchers.greaterThan(0l));
}
@Test
public void getCpuSpeedFromHostCapabilities() {
String capabilities = "<host>\n" +
"<uuid>8a330742-345f-b0df-7954-c9960b88116c</uuid>\n" +
" <cpu>\n" +
" <arch>x86_64</arch>\n" +
" <model>Opteron_G2</model>\n" +
" <vendor>AMD</vendor>\n" +
" <counter name='tsc' frequency='2350000000' scaling='no'/>\n" +
" </cpu>\n" +
"</host>\n";;
Assert.assertEquals(2350L, KVMHostInfo.getCpuSpeedFromHostCapabilities(capabilities));
} }
@Test @Test

14
pom.xml
View File

@ -623,6 +623,20 @@
<groupId>org.opensaml</groupId> <groupId>org.opensaml</groupId>
<artifactId>opensaml</artifactId> <artifactId>opensaml</artifactId>
<version>${cs.opensaml.version}</version> <version>${cs.opensaml.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.owasp.esapi</groupId> <groupId>org.owasp.esapi</groupId>

View File

@ -4640,7 +4640,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
} }
if (!StringUtils.isAllEmpty(ipv6Range, vlan.getIp6Range())) { if (!StringUtils.isAllEmpty(ipv6Range, vlan.getIp6Range())) {
String r1 = StringUtils.isEmpty(ipv6Range) ? NetUtils.getIpv6RangeFromCidr(vlanIp6Cidr) : ipv6Range; String r1 = StringUtils.isEmpty(ipv6Range) ? NetUtils.getIpv6RangeFromCidr(vlanIp6Cidr) : ipv6Range;
String r2 = StringUtils.isEmpty(vlan.getIp6Range()) ? NetUtils.getIpv6RangeFromCidr(vlanIp6Cidr) : vlan.getIp6Range(); String r2 = StringUtils.isEmpty(vlan.getIp6Range()) ? NetUtils.getIpv6RangeFromCidr(vlan.getIp6Cidr()) : vlan.getIp6Range();
if(NetUtils.isIp6RangeOverlap(r1, r2)) { if(NetUtils.isIp6RangeOverlap(r1, r2)) {
throw new InvalidParameterValueException(String.format("The IPv6 range with tag: %s already has IPs that overlap with the new range.", throw new InvalidParameterValueException(String.format("The IPv6 range with tag: %s already has IPs that overlap with the new range.",
vlan.getVlanTag())); vlan.getVlanTag()));

View File

@ -239,7 +239,7 @@
</div> </div>
<div class="form__item" v-if="!basicGuestNetwork && form.iptype != 'ip6'"> <div class="form__item" v-if="!basicGuestNetwork && form.iptype != 'ip6'">
<div style="color: black;">{{ $t('label.set.reservation') }}</div> <div style="color: black;">{{ $t('label.set.reservation') }}</div>
<a-switch @change="handleShowAccountFields" /> <a-switch v-model:checked="showAccountFields" @change="handleShowAccountFields" />
</div> </div>
<div v-if="showAccountFields && !basicGuestNetwork" style="margin-top: 20px;"> <div v-if="showAccountFields && !basicGuestNetwork" style="margin-top: 20px;">
<div v-html="$t('label.set.reservation.desc')"></div> <div v-html="$t('label.set.reservation.desc')"></div>
@ -555,12 +555,9 @@ export default {
this.fetchDomains() this.fetchDomains()
}, },
handleShowAccountFields () { handleShowAccountFields () {
if (this.showAccountFields === false) { if (this.showAccountFields) {
this.showAccountFields = true
this.fetchDomains() this.fetchDomains()
return
} }
this.showAccountFields = false
}, },
handleOpenAddIpRangeModal () { handleOpenAddIpRangeModal () {
this.initAddIpRangeForm() this.initAddIpRangeForm()