Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/src/main/java/com/cloud/network/vpc/VpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, Strin
* @param displayVpc should this VPC be displayed on public lists
* @param mtu what maximal transfer unit to us in this VPCs networks
* @param sourceNatIp the source NAT address to use for this VPC (must already be associated with the VPC)
* @param networkRate the rate in Mbps for the public interfaces of the VPC's virtual routers (0 = unlimited), null to keep it
* @return an object describing the current state of the VPC
* @throws ResourceUnavailableException if during restart some resources may not be available
* @throws InsufficientCapacityException if for instance no address space, compute or storage is sufficiently available
*/
Vpc updateVpc(long vpcId, String vpcName, String displayText, String customId, Boolean displayVpc, Integer mtu, String sourceNatIp, Boolean keepMacAddressOnPublicNic) throws ResourceUnavailableException, InsufficientCapacityException;
Vpc updateVpc(long vpcId, String vpcName, String displayText, String customId, Boolean displayVpc, Integer mtu, String sourceNatIp, Boolean keepMacAddressOnPublicNic,
Integer networkRate) throws ResourceUnavailableException, InsufficientCapacityException;

/**
* Lists VPC(s) based on the parameters passed to the API call
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,12 @@ public class ApiConstants {

public static final String KEEP_MAC_ADDRESS_ON_PUBLIC_NIC = "keepmacaddressonpublicnic";

public static final String PARAMETER_DESCRIPTION_VPC_NETWORK_RATE =
"Data transfer rate in megabits per second on the public interfaces of the VPC's virtual routers; 0 means unlimited." +
" Without it, the rate of the VPC's first network offering applies. A non-admin caller can set at most the value of" +
" the account setting vpc.max.network.rate, and only when that setting is above 0. A root admin can set -1 to" +
" return the VPC to the rate of its network offering.";

public static final String PARAMETER_DESCRIPTION_KEEP_MAC_ADDRESS_ON_PUBLIC_NIC =
"Indicates whether to use the same MAC address for the public NIC of VRs on the same network. If \"true\", when creating redundant routers or recreating" +
" a VR, CloudStack will use the same MAC address for the public NIC of all VRs. Otherwise, if \"false\", new public NICs will always have " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public class CreateVPCCmd extends BaseAsyncCreateCmd implements UserCmd {
type = CommandType.BOOLEAN, since = "4.23.0", authorized = {RoleType.Admin})
private boolean keepMacAddressOnPublicNic = true;

@Parameter(name = ApiConstants.NETWORKRATE, type = CommandType.INTEGER,
description = ApiConstants.PARAMETER_DESCRIPTION_VPC_NETWORK_RATE, since = "24.0")
private Integer networkRate;

// ///////////////////////////////////////////////////
// ///////////////// Accessors ///////////////////////
// ///////////////////////////////////////////////////
Expand Down Expand Up @@ -219,6 +223,10 @@ public boolean getUseVrIpResolver() {
return BooleanUtils.toBoolean(useVrIpResolver);
}

public Integer getNetworkRate() {
return networkRate;
}

public boolean getKeepMacAddressOnPublicNic() {
return keepMacAddressOnPublicNic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public class UpdateVPCCmd extends BaseAsyncCustomIdCmd implements UserCmd {
type = CommandType.BOOLEAN, since = "4.23.0", authorized = {RoleType.Admin})
private Boolean keepMacAddressOnPublicNic;

@Parameter(name = ApiConstants.NETWORKRATE, type = CommandType.INTEGER,
description = ApiConstants.PARAMETER_DESCRIPTION_VPC_NETWORK_RATE + " Applied to running virtual routers at once.",
since = "24.0")
private Integer networkRate;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -106,6 +111,10 @@ public Boolean getKeepMacAddressOnPublicNic() {
return keepMacAddressOnPublicNic;
}

public Integer getNetworkRate() {
return networkRate;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public class VpcResponse extends BaseResponseWithAnnotations implements Controll
@Param(description = ApiConstants.PARAMETER_DESCRIPTION_KEEP_MAC_ADDRESS_ON_PUBLIC_NIC, since = "4.23.0")
private Boolean keepMacAddressOnPublicNic;

@SerializedName(ApiConstants.NETWORKRATE)
@Param(description = "Data transfer rate in megabits per second set on the VPC for the public interfaces of its virtual routers; 0 means unlimited", since = "24.0")
private Integer networkRate;

public void setId(final String id) {
this.id = id;
}
Expand Down Expand Up @@ -374,4 +378,8 @@ public void addBgpPeer(BgpPeerResponse bgpPeer) {
public void setKeepMacAddressOnPublicNic(Boolean keepMacAddressOnPublicNic) {
this.keepMacAddressOnPublicNic = keepMacAddressOnPublicNic;
}

public void setNetworkRate(Integer networkRate) {
this.networkRate = networkRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testExecute() throws ResourceUnavailableException, InsufficientCapac
responseGenerator = Mockito.mock(ResponseGenerator.class);
cmd._responseGenerator = responseGenerator;
Mockito.verify(_vpcService, Mockito.times(0)).updateVpc(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean());
Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyInt());

}
}
14 changes: 14 additions & 0 deletions core/src/main/java/com/cloud/agent/api/UpdateVmNicCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class UpdateVmNicCommand extends Command {
String nicMacAddress;
String instanceName;
Boolean enabled;
Integer networkRateMbps;

@Override
public boolean executeInSequence() {
Expand All @@ -37,6 +38,15 @@ public UpdateVmNicCommand(String nicMacAdderss, String instanceName, Boolean ena
this.enabled = enabled;
}

/**
* @param enabled the NIC's link state, or null to leave it
* @param networkRateMbps the NIC's rate in Mbps (0 = unlimited), or null to leave it
*/
public UpdateVmNicCommand(String nicMacAddress, String instanceName, Boolean enabled, Integer networkRateMbps) {
this(nicMacAddress, instanceName, enabled);
this.networkRateMbps = networkRateMbps;
}

public String getNicMacAddress() {
return nicMacAddress;
}
Expand All @@ -48,4 +58,8 @@ public String getVmName() {
public Boolean isEnabled() {
return enabled;
}

public Integer getNetworkRateMbps() {
return networkRateMbps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@
import org.apache.cloudstack.framework.config.ConfigKey;

public interface VpcManager {
/**
* VPC detail holding the rate in Mbps for the public interfaces of the VPC's virtual routers (0 = unlimited).
*/
String NETWORK_RATE_DETAIL = "networkrate";

ConfigKey<Integer> VpcMaxNetworkRate = new ConfigKey<>(Integer.class,
"vpc.max.network.rate",
ConfigKey.CATEGORY_SYSTEM,
"0",
"The highest networkrate in Mbps a non-admin caller can set on a VPC of the account. 0 means such callers cannot set networkrate.",
true,
ConfigKey.Scope.Account,
null);

ConfigKey<Boolean> VpcTierNamePrepend = new ConfigKey<>(Boolean.class,
"vpc.tier.name.prepend",
ConfigKey.CATEGORY_NETWORK,
Expand All @@ -66,6 +80,12 @@ public interface VpcManager {
*/
public List<? extends Network> getVpcNetworks(long vpcId);

/**
* @return the networkrate in Mbps set on the VPC for the public interfaces of its virtual routers (0 = unlimited),
* or null when none is set
*/
Integer getVpcNetworkRate(long vpcId);

/**
* Returns all existing VPCs for a given account
* @param accountId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,25 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
*/
private static final String COMMAND_MERGE_SNAPSHOT = "virsh blockcommit %s %s --base %s";

/**
* Reads the libvirt and QEMU versions and hands them to {@link LibvirtVMDef}, whose device XML depends on them
* (NIC bandwidth, packed virtqueues, ...). Without this they are only known after the first VM is defined since
* the agent started, and XML built before that, for a NIC plug or a live NIC update, silently loses those parts.
*/
protected void readHypervisorVersions(final Connect conn) {
try {
hvVersion = conn.getVersion();
hvVersion = hvVersion % 1000000 / 1000;
hypervisorLibvirtVersion = conn.getLibVirVersion();
hypervisorQemuVersion = conn.getVersion();
} catch (final LibvirtException e) {
LOGGER.trace("Ignoring libvirt error.", e);
return;
}
LibvirtVMDef.setGlobalLibvirtVersion(hypervisorLibvirtVersion);
LibvirtVMDef.setGlobalQemuVersion(hypervisorQemuVersion);
}

public long getHypervisorLibvirtVersion() {
return hypervisorLibvirtVersion;
}
Expand Down Expand Up @@ -1420,14 +1439,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
}

hypervisorPath = getHypervisorPath(conn);
try {
hvVersion = conn.getVersion();
hvVersion = hvVersion % 1000000 / 1000;
hypervisorLibvirtVersion = conn.getLibVirVersion();
hypervisorQemuVersion = conn.getVersion();
} catch (final LibvirtException e) {
LOGGER.trace("Ignoring libvirt error.", e);
}
readHypervisorVersions(conn);

// Enable/disable IO driver for Qemu (in case it is not set CloudStack can also detect if its supported by qemu)
enableIoUring = isIoUringEnabled();
Expand Down Expand Up @@ -5058,7 +5070,8 @@ public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName)

public InterfaceDef getInterface(final Connect conn, final String vmName, final String macAddress) {
List<InterfaceDef> interfaces = getInterfaces(conn, vmName);
return interfaces.stream().filter(interfaceDef -> interfaceDef.getMacAddress().equals(macAddress))
// NICs the parser does not know, like direct or vhost-user ones, have no MAC address
return interfaces.stream().filter(interfaceDef -> macAddress.equals(interfaceDef.getMacAddress()))
.findFirst().orElseThrow(() -> new CloudRuntimeException(String.format("Unable to find NIC with MAC address %s.", macAddress)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ public boolean parseDomainXML(String domXML) {
def.setSlot(Integer.parseInt(slot, 16));
}

String linkState = getAttrValue("link", "state", nic);
if ("down".equalsIgnoreCase(linkState)) {
def.setLinkStateUp(false);
}

// without it a live update of the interface would ask libvirt to change the ROM BAR, which it refuses
if ("off".equalsIgnoreCase(getAttrValue("rom", "bar", nic))) {
def.setPxeDisable(true);
}

interfaces.add(def);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,14 @@ public boolean isLinkStateUp() {
return _linkStateUp;
}

public void setNetworkRateKBps(int networkRateKBps) {
_networkRateKBps = networkRateKBps;
}

public int getNetworkRateKBps() {
return _networkRateKBps;
}

public String getDpdkSourcePort() {
return _dpdkSourcePort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.UpdateVmNicAnswer;
import com.cloud.agent.api.UpdateVmNicCommand;
import com.cloud.hypervisor.kvm.resource.BridgeVifDriver;
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
import com.cloud.network.Networks.TrafficType;
import com.cloud.resource.CommandWrapper;
import com.cloud.resource.ResourceWrapper;
import org.libvirt.Connect;
Expand All @@ -33,11 +35,15 @@
@ResourceWrapper(handles = UpdateVmNicCommand.class)
public final class LibvirtUpdateVmNicCommandWrapper extends CommandWrapper<UpdateVmNicCommand, Answer, LibvirtComputingResource> {

// libvirt takes interface bandwidth in kilobytes per second
private static final int KBYTES_PER_SECOND_PER_MBPS = 125;

@Override
public Answer execute(UpdateVmNicCommand command, LibvirtComputingResource libvirtComputingResource) {
String nicMacAddress = command.getNicMacAddress();
String vmName = command.getVmName();
boolean isEnabled = command.isEnabled();
Boolean isEnabled = command.isEnabled();
Integer networkRateMbps = command.getNetworkRateMbps();

Domain vm = null;
try {
Expand All @@ -46,7 +52,18 @@ public Answer execute(UpdateVmNicCommand command, LibvirtComputingResource libvi
vm = libvirtComputingResource.getDomain(conn, vmName);

final InterfaceDef nic = libvirtComputingResource.getInterface(conn, vmName, nicMacAddress);
nic.setLinkStateUp(isEnabled);
if (isEnabled != null) {
nic.setLinkStateUp(isEnabled);
}
if (networkRateMbps != null) {
// the NIC is written back as parsed, which keeps only what the bridge VIF driver writes; others add
// more, like an Open vSwitch port and its VLAN tag
if (!(libvirtComputingResource.getVifDriver(TrafficType.Public, nic.getBrName()) instanceof BridgeVifDriver)) {
return new UpdateVmNicAnswer(command, false, String.format("The network rate of NIC %s can only be changed live with the bridge VIF " +
"driver; it applies at the next start of %s", nicMacAddress, vmName));
}
nic.setNetworkRateKBps(networkRateMbps > 0 ? networkRateMbps * KBYTES_PER_SECOND_PER_MBPS : 0);
}
vm.updateDeviceFlags(nic.toString(), Domain.DeviceModifyFlags.LIVE);

return new UpdateVmNicAnswer(command, true, "success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ public void setup() throws Exception {
LibvirtComputingResource.LOGGER = loggerMock;
}

@Test
public void readHypervisorVersionsMakesNicXmlCompleteBeforeAnyVmIsDefined() throws LibvirtException {
LibvirtVMDef.setGlobalLibvirtVersion(0);
LibvirtVMDef.setGlobalQemuVersion(0);
final Connect conn = Mockito.mock(Connect.class);
Mockito.when(conn.getLibVirVersion()).thenReturn(12_000_000L);
Mockito.when(conn.getVersion()).thenReturn(10_002_001L);

libvirtComputingResourceSpy.readHypervisorVersions(conn);

final InterfaceDef nic = new InterfaceDef();
nic.defBridgeNet("br0", null, "02:00:00:00:00:01", InterfaceDef.NicModel.VIRTIO, 125000);
nic.setPackedVirtQueues(true);
final String xml = nic.toString();
assertTrue(xml.contains("<inbound average='125000' peak='125000'/>"));
assertTrue(xml.contains("packed='on'"));
assertEquals(12_000_000L, libvirtComputingResourceSpy.getHypervisorLibvirtVersion());
assertEquals(10_002_001L, libvirtComputingResourceSpy.getHypervisorQemuVersion());
}

/**
This test tests if the Agent can handle a vmSpec coming
from a <=4.1 management server.
Expand Down Expand Up @@ -7639,4 +7659,15 @@ public void getPeriodAndQuotaTestPeriodIsEqualToMaximumWhenRequired() {
Pair<Integer, Long> result = libvirtComputingResourceSpy.getPeriodAndQuota(cpuQuotaPercentage);
Assert.assertEquals(expectedResult, result);
}

@Test
public void getInterfaceSkipsNicsWithoutAMacAddress() {
Connect conn = Mockito.mock(Connect.class);
InterfaceDef withoutMac = new InterfaceDef();
InterfaceDef nic = new InterfaceDef();
nic.defBridgeNet("cloudbr1", "vnet0", "1e:00:00:00:00:01", InterfaceDef.NicModel.VIRTIO);
Mockito.doReturn(List.of(withoutMac, nic)).when(libvirtComputingResourceSpy).getInterfaces(conn, "r-50-VM");

Assert.assertSame(nic, libvirtComputingResourceSpy.getInterface(conn, "r-50-VM", "1e:00:00:00:00:01"));
}
}
Loading