Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vpc.max.networks setting & settings to limit the number of NICs for each hypervisor #8654

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public interface NetworkOrchestrationService {

static final ConfigKey<Boolean> TUNGSTEN_ENABLED = new ConfigKey<>(Boolean.class, "tungsten.plugin.enable", "Advanced", "false",
"Indicates whether to enable the Tungsten plugin", false, ConfigKey.Scope.Zone, null);
ConfigKey<Integer> VirtualMachineMaxNicsKvm = new ConfigKey<>("Advanced", Integer.class, "virtual.machine.max.nics.kvm", "23",
"The maximum number of NICs supported by the KVM hypervsior.", true, Scope.Cluster);

ConfigKey<Integer> VirtualMachineMaxNicsVmware = new ConfigKey<>("Advanced", Integer.class, "virtual.machine.max.nics.vmware", "10",
"The maximum number of NICs supported by the VMware hypervsior.", true, Scope.Cluster);

ConfigKey<Integer> VirtualMachineMaxNicsXenserver = new ConfigKey<>("Advanced", Integer.class, "virtual.machine.max.nics.xenserver", "7",
"The maximum number of NICs supported by the XenServer hypervsior.", true, Scope.Cluster);

static final ConfigKey<Boolean> NSX_ENABLED = new ConfigKey<>(Boolean.class, "nsx.plugin.enable", "Advanced", "false",
"Indicates whether to enable the NSX plugin", false, ConfigKey.Scope.Zone, null);
Expand Down Expand Up @@ -348,4 +356,16 @@ void implementNetworkElementsAndResources(DeployDestination dest, ReservationCon
Pair<NicProfile, Integer> importNic(final String macAddress, int deviceId, final Network network, final Boolean isDefaultNic, final VirtualMachine vm, final Network.IpAddresses ipAddresses, final DataCenter datacenter, boolean forced) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException;

void unmanageNics(VirtualMachineProfile vm);

/**
* Returns the maximum number of NICs that the given virtual machine can have considering its hypervisor.
* <br/><br/>
* First we try to retrieve the setting value from the cluster where the virtual machine is deployed. If the cluster does not exist, we try to retrieve the setting value from the virtual machine hypervisor type.
* In last case, if the virtual machine does not have a hypervisor type, we retrieve the smallest value between the {@link NetworkOrchestrationService#VirtualMachineMaxNicsKvm}, {@link NetworkOrchestrationService#VirtualMachineMaxNicsVmware}
* and {@link NetworkOrchestrationService#VirtualMachineMaxNicsXenserver} global settings.
*
* @param virtualMachine Virtual machine to get the maximum number of NICs.
* @return The maximum number of NICs that the virtual machine can have.
*/
int getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.cloud.utils.Pair;
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
import org.apache.cloudstack.framework.config.ConfigKey;

import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientAddressCapacityException;
Expand All @@ -39,6 +40,9 @@
import com.cloud.user.Account;

public interface VpcManager {
ConfigKey<Integer> VpcMaxNetworks = new ConfigKey<>("Advanced", Integer.class, "vpc.max.networks", "3",
"Maximum number of networks per VPC.", true, ConfigKey.Scope.Account);

/**
* Returns all the Guest networks that are part of VPC
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.googlecode.ipv6.IPv6Address;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;

/**
Expand Down Expand Up @@ -1040,6 +1041,13 @@ public void saveExtraDhcpOptions(final String networkUuid, final Long nicId, fin
public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final Network network, final Boolean isDefaultNic, int deviceId, final VirtualMachineProfile vm)
throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {

int virtualMachineMaxNicsValue = getVirtualMachineMaxNicsValue(vm.getVirtualMachine());
List<NicVO> nics = _nicDao.listByVmId(vm.getId());

if (nics.size() >= virtualMachineMaxNicsValue) {
throw new CloudRuntimeException(String.format("Failed to allocate NIC on network [%s] because VM [%s] has reached its maximum NIC capacity [%s].", network.getUuid(), vm.getUuid(), virtualMachineMaxNicsValue));
}

final NetworkVO ntwkVO = _networksDao.findById(network.getId());
logger.debug("Allocating nic for vm " + vm.getVirtualMachine() + " in network " + network + " with requested profile " + requested);
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, ntwkVO.getGuruName());
Expand Down Expand Up @@ -1090,6 +1098,77 @@ public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final N
return new Pair<NicProfile, Integer>(vmNic, Integer.valueOf(deviceId));
}

@Override
public int getVirtualMachineMaxNicsValue(VirtualMachine virtualMachine) {
Integer virtualMachineMaxNicsValue = getVirtualMachineMaxNicsValueFromCluster(virtualMachine);

if (virtualMachineMaxNicsValue != null) {
return virtualMachineMaxNicsValue;
}

if (virtualMachine.getHypervisorType() == null) {
logger.debug("Using the smallest setting global value between {}, {} and {} as the VM {} does not have a hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsXenserver, virtualMachine.getUuid());
return NumberUtils.min(VirtualMachineMaxNicsKvm.value(), VirtualMachineMaxNicsVmware.value(), VirtualMachineMaxNicsXenserver.value());
}

return getVirtualMachineMaxNicsValueFromVmHypervisorType(virtualMachine);
}

/**
* Searches the maximum virtual machine NICs based on the hypervisor type of the cluster where the instance is deployed.
*
* @param virtualMachine Virtual machine to get the cluster.
* @return The maximum number of NICs that the virtual machine can have.
*/
protected Integer getVirtualMachineMaxNicsValueFromCluster(VirtualMachine virtualMachine) {
HostVO host = _hostDao.findById(virtualMachine.getHostId());
if (host == null) {
return null;
}

ClusterVO cluster = clusterDao.findById(host.getClusterId());
if (cluster == null) {
return null;
}

int virtualMachineMaxNicsValue;
HypervisorType hypervisor = cluster.getHypervisorType();

if (HypervisorType.KVM.equals(hypervisor)) {
weizhouapache marked this conversation as resolved.
Show resolved Hide resolved
virtualMachineMaxNicsValue = VirtualMachineMaxNicsKvm.valueIn(cluster.getId());
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsKvm, virtualMachineMaxNicsValue);
} else if (HypervisorType.VMware.equals(hypervisor)) {
virtualMachineMaxNicsValue = VirtualMachineMaxNicsVmware.valueIn(cluster.getId());
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsVmware, virtualMachineMaxNicsValue);
} else {
virtualMachineMaxNicsValue = VirtualMachineMaxNicsXenserver.valueIn(cluster.getId());
logger.debug("The cluster {} where the VM is deployed uses the {} hypervisor. Therefore, the {} setting value [{}] will be used.", cluster.getName(), hypervisor, VirtualMachineMaxNicsXenserver, virtualMachineMaxNicsValue);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you extract this ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, to reopen @hsato03 , but for readability this and Wei's remarks still makes sense.


return virtualMachineMaxNicsValue;
}

/**
* Searches the maximum virtual machine NICs based on the virtual machine hypervisor type.
*
* @param virtualMachine Virtual machine to get the hypervisor type.
* @return The maximum number of NICs that the virtual machine can have.
*/
protected int getVirtualMachineMaxNicsValueFromVmHypervisorType(VirtualMachine virtualMachine) {
HypervisorType virtualMachineHypervisorType = virtualMachine.getHypervisorType();

if (HypervisorType.KVM.equals(virtualMachineHypervisorType)) {
weizhouapache marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsKvm.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
return VirtualMachineMaxNicsKvm.value();
} else if (HypervisorType.VMware.equals(virtualMachineHypervisorType)) {
logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsVmware.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
return VirtualMachineMaxNicsVmware.value();
}

logger.debug("Using the {} setting global value {} as the VM {} has the {} hypervisor type and is not deployed on either a host or a cluster.", VirtualMachineMaxNicsXenserver, VirtualMachineMaxNicsXenserver.value(), virtualMachine.getUuid(), virtualMachineHypervisorType);
return VirtualMachineMaxNicsXenserver.value();
}

private boolean isNicAllocatedForNsxPublicNetworkOnVR(Network network, NicProfile requested, VirtualMachineProfile vm) {
if (ObjectUtils.anyNull(network, requested, vm)) {
return false;
Expand Down Expand Up @@ -4787,6 +4866,6 @@ public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[]{NetworkGcWait, NetworkGcInterval, NetworkLockTimeout,
GuestDomainSuffix, NetworkThrottlingRate, MinVRVersion,
PromiscuousMode, MacAddressChanges, ForgedTransmits, MacLearning, RollingRestartEnabled,
TUNGSTEN_ENABLED, NSX_ENABLED };
TUNGSTEN_ENABLED, NSX_ENABLED, VirtualMachineMaxNicsKvm, VirtualMachineMaxNicsVmware, VirtualMachineMaxNicsXenserver };
}
}
Loading
Loading