Remove code that reserves extra interfaces based on the global setting router.extra.public.nics in case of VMware. (#5912)

Added hot plugging of vifs whenever required in case of isolated networks. VPC networks already uses hotplugging of vifs.
This commit is contained in:
Harikrishna 2022-02-04 17:11:56 +05:30 committed by GitHub
parent ecf85aa65b
commit 56a8852c4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 149 additions and 105 deletions

View File

@ -18,6 +18,8 @@ package com.cloud.agent.api.to;
import com.cloud.network.Networks.TrafficType;
import java.util.Map;
public class IpAddressTO {
private long accountId;
@ -36,6 +38,8 @@ public class IpAddressTO {
private Integer nicDevId;
private boolean newNic;
private boolean isPrivateGateway;
private NicTO nicTO;
Map<String, String> details;
public IpAddressTO(long accountId, String ipAddress, boolean add, boolean firstIP, boolean sourceNat, String broadcastUri, String vlanGateway, String vlanNetmask,
String vifMacAddress, Integer networkRate, boolean isOneToOneNat) {
@ -142,4 +146,21 @@ public class IpAddressTO {
public void setPrivateGateway(boolean isPrivateGateway) {
this.isPrivateGateway = isPrivateGateway;
}
public NicTO getNicTO() {
return nicTO;
}
public void setNicTO(NicTO nicTO) {
this.nicTO = nicTO;
}
public Map<String, String> getDetails() {
return details;
}
public void setDetails(Map<String, String> details) {
this.details = details;
}
}

View File

@ -214,7 +214,7 @@ class VmwareVmImplementer {
NicTO[] nics = to.getNics();
// reserve extra NICs
NicTO[] expandedNics = new NicTO[nics.length + vmwareMgr.getRouterExtraPublicNics()];
NicTO[] expandedNics = new NicTO[nics.length];
int i = 0;
int deviceId = -1;
for (i = 0; i < nics.length; i++) {
@ -227,7 +227,7 @@ class VmwareVmImplementer {
long networkId = publicNicProfile.getNetworkId();
NetworkVO network = networkDao.findById(networkId);
for (; i < nics.length + vmwareMgr.getRouterExtraPublicNics(); i++) {
for (; i < nics.length; i++) {
NicTO nicTo = new NicTO();
nicTo.setDeviceId(deviceId++);

View File

@ -85,8 +85,6 @@ public interface VmwareManager {
Pair<Integer, Integer> getAddiionalVncPortRange();
int getRouterExtraPublicNics();
boolean beginExclusiveOperation(int timeOutSeconds);
void endExclusiveOperation();

View File

@ -253,7 +253,6 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw
private String _recycleHungWorker = "false";
private int _additionalPortRangeStart;
private int _additionalPortRangeSize;
private int _routerExtraPublicNics = 2;
private int _vCenterSessionTimeout = 1200000; // Timeout in milliseconds
private String _rootDiskController = DiskControllerType.ide.toString();
@ -374,8 +373,6 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw
_additionalPortRangeSize = Math.min(1000, 65535 - _additionalPortRangeStart);
}
_routerExtraPublicNics = NumbersUtil.parseInt(_configDao.getValue(Config.RouterExtraPublicNics.key()), 2);
_vCenterSessionTimeout = NumbersUtil.parseInt(_configDao.getValue(Config.VmwareVcenterSessionTimeout.key()), 1200) * 1000;
s_logger.info("VmwareManagerImpl config - vmware.vcenter.session.timeout: " + _vCenterSessionTimeout);
@ -1069,11 +1066,6 @@ public class VmwareManagerImpl extends ManagerBase implements VmwareManager, Vmw
return new Pair<Integer, Integer>(_additionalPortRangeStart, _additionalPortRangeSize);
}
@Override
public int getRouterExtraPublicNics() {
return _routerExtraPublicNics;
}
@Override
public Map<String, String> getNexusVSMCredentialsByClusterId(Long clusterId) {
CiscoNexusVSMDeviceVO nexusVSM = null;

View File

@ -1268,26 +1268,38 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd));
}
try {
VirtualEthernetCardType nicDeviceType = null;
if (cmd.getDetails() != null) {
nicDeviceType = VirtualEthernetCardType.valueOf(cmd.getDetails().get("nicAdapter"));
}
plugNicCommandInternal(cmd.getVmName(), nicDeviceType, cmd.getNic(), cmd.getVMType());
return new PlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
}
}
private void plugNicCommandInternal(String vmName, VirtualEthernetCardType nicDeviceType, NicTO nicTo, VirtualMachine.Type vmType) throws Exception {
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getVmName();
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
}
if (vmMo == null) {
String msg = "Router " + vmName + " no longer exists to execute PlugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
if (vmMo == null) {
String msg = "Router " + vmName + " no longer exists to execute PlugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
@ -1296,54 +1308,45 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
}
*/
// Fallback to E1000 if no specific nicAdapter is passed
VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
Map<String, String> details = cmd.getDetails();
if (details != null) {
nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
}
// Fallback to E1000 if no specific nicAdapter is passed
if (nicDeviceType == null) {
nicDeviceType = VirtualEthernetCardType.E1000;
}
// find a usable device number in VMware environment
VirtualDevice[] nicDevices = vmMo.getSortedNicDevices();
int deviceNumber = -1;
for (VirtualDevice device : nicDevices) {
if (device.getUnitNumber() > deviceNumber)
deviceNumber = device.getUnitNumber();
}
deviceNumber++;
// find a usable device number in VMware environment
VirtualDevice[] nicDevices = vmMo.getSortedNicDevices();
int deviceNumber = -1;
for (VirtualDevice device : nicDevices) {
if (device.getUnitNumber() > deviceNumber)
deviceNumber = device.getUnitNumber();
}
deviceNumber++;
NicTO nicTo = cmd.getNic();
VirtualDevice nic;
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid,
nicTo.getMac(), deviceNumber + 1, true, true);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(),
nicTo.getMac(), deviceNumber + 1, true, true);
}
VirtualDevice nic;
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, vmType);
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid,
nicTo.getMac(), deviceNumber + 1, true, true);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(),
nicTo.getMac(), deviceNumber + 1, true, true);
}
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(nic);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(nic);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure devices when running PlugNicCommand");
}
return new PlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure devices when running PlugNicCommand");
}
}
@ -1613,7 +1616,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
}
if (addVif) {
plugPublicNic(vmMo, vlanId, ip);
NicTO nicTO = ip.getNicTO();
VirtualEthernetCardType nicDeviceType = null;
if (ip.getDetails() != null) {
nicDeviceType = VirtualEthernetCardType.valueOf(ip.getDetails().get("nicAdapter"));
}
plugNicCommandInternal(routerName, nicDeviceType, nicTO, VirtualMachine.Type.DomainRouter);
publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (publicNicInfo.first().intValue() >= 0) {
networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
@ -2313,39 +2321,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
int nicMask = 0;
int nicCount = 0;
if (vmSpec.getType() == VirtualMachine.Type.DomainRouter) {
int extraPublicNics = mgr.getRouterExtraPublicNics();
if (extraPublicNics > 0 && vmSpec.getDetails().containsKey("PeerRouterInstanceName")) {
//Set identical MAC address for RvR on extra public interfaces
String peerRouterInstanceName = vmSpec.getDetails().get("PeerRouterInstanceName");
VirtualMachineMO peerVmMo = hyperHost.findVmOnHyperHost(peerRouterInstanceName);
if (peerVmMo == null) {
peerVmMo = hyperHost.findVmOnPeerHyperHost(peerRouterInstanceName);
}
if (peerVmMo != null) {
String oldMacSequence = generateMacSequence(nics);
for (int nicIndex = nics.length - extraPublicNics; nicIndex < nics.length; nicIndex++) {
VirtualDevice nicDevice = peerVmMo.getNicDeviceByIndex(nics[nicIndex].getDeviceId());
if (nicDevice != null) {
String mac = ((VirtualEthernetCard) nicDevice).getMacAddress();
if (mac != null) {
s_logger.info("Use same MAC as previous RvR, the MAC is " + mac + " for extra NIC with device id: " + nics[nicIndex].getDeviceId());
nics[nicIndex].setMac(mac);
}
}
}
if (!StringUtils.isBlank(vmSpec.getBootArgs())) {
String newMacSequence = generateMacSequence(nics);
vmSpec.setBootArgs(replaceNicsMacSequenceInBootArgs(oldMacSequence, newMacSequence, vmSpec));
}
}
}
}
VirtualEthernetCardType nicDeviceType;
NiciraNvpApiVersion.logNiciraApiVersion();

View File

@ -26,6 +26,14 @@ import java.util.Map;
import javax.inject.Inject;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.network.dao.NetworkDetailVO;
import com.cloud.network.dao.NetworkDetailsDao;
import com.cloud.offerings.dao.NetworkOfferingDetailsDao;
import com.cloud.vm.VmDetailConstants;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -179,6 +187,12 @@ public class CommandSetupHelper {
private RouterControlHelper _routerControlHelper;
@Inject
private HostDao _hostDao;
@Inject
ConfigurationManager _configMgr;
@Inject
private NetworkOfferingDetailsDao networkOfferingDetailsDao;
@Inject
private NetworkDetailsDao networkDetailsDao;
@Autowired
@Qualifier("networkHelper")
@ -848,6 +862,15 @@ public class CommandSetupHelper {
vifMacAddress, networkRate, ipAddr.isOneToOneNat());
setIpAddressNetworkParams(ip, network, router);
if (router.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
Map<String, String> details = new HashMap<>();
String defaultSystemVmNicAdapterType = _configDao.getValue(Config.VmwareSystemVmNicDeviceType.key());
if (defaultSystemVmNicAdapterType == null) {
defaultSystemVmNicAdapterType = Config.VmwareSystemVmNicDeviceType.getDefaultValue();
}
details.put(VmDetailConstants.NIC_ADAPTER, defaultSystemVmNicAdapterType);
ip.setDetails(details);
}
ipsToSend[i++] = ip;
/*
* send the firstIP = true for the first Add, this is to create
@ -1152,6 +1175,41 @@ public class CommandSetupHelper {
ipAddress.setPrivateGateway(false);
}
ipAddress.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
final NetworkOfferingVO networkOfferingVO = _networkOfferingDao.findById(network.getNetworkOfferingId());
NicTO nicTO = new NicTO();
nicTO.setMac(ipAddress.getVifMacAddress());
nicTO.setType(ipAddress.getTrafficType());
nicTO.setGateway(ipAddress.getVlanGateway());
nicTO.setBroadcastUri(BroadcastDomainType.fromString(ipAddress.getBroadcastUri()));
nicTO.setType(network.getTrafficType());
nicTO.setName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
nicTO.setBroadcastType(network.getBroadcastDomainType());
nicTO.setIsolationuri(network.getBroadcastUri());
nicTO.setNetworkRateMbps(_configMgr.getNetworkOfferingNetworkRate(networkOfferingVO.getId(), network.getDataCenterId()));
nicTO.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
nicTO.setDetails(getNicDetails(network));
ipAddress.setNicTO(nicTO);
}
private Map<NetworkOffering.Detail, String> getNicDetails(Network network) {
if (network == null) {
s_logger.debug("Unable to get NIC details as the network is null");
return null;
}
Map<NetworkOffering.Detail, String> details = networkOfferingDetailsDao.getNtwkOffDetails(network.getNetworkOfferingId());
if (details != null) {
details.putIfAbsent(NetworkOffering.Detail.PromiscuousMode, NetworkOrchestrationService.PromiscuousMode.value().toString());
details.putIfAbsent(NetworkOffering.Detail.MacAddressChanges, NetworkOrchestrationService.MacAddressChanges.value().toString());
details.putIfAbsent(NetworkOffering.Detail.ForgedTransmits, NetworkOrchestrationService.ForgedTransmits.value().toString());
details.putIfAbsent(NetworkOffering.Detail.MacLearning, NetworkOrchestrationService.MacLearning.value().toString());
}
NetworkDetailVO pvlantypeDetail = networkDetailsDao.findDetail(network.getId(), ApiConstants.ISOLATED_PVLAN_TYPE);
if (pvlantypeDetail != null) {
details.putIfAbsent(NetworkOffering.Detail.pvlanType, pvlantypeDetail.getValue());
}
return details;
}
}

View File

@ -2063,7 +2063,7 @@ Configurable, StateListener<VirtualMachine.State, VirtualMachine.Event, VirtualM
buf.append(" dnssearchorder=").append(domain_suffix);
}
if (profile.getHypervisorType() == HypervisorType.VMware || profile.getHypervisorType() == HypervisorType.Hyperv) {
if (profile.getHypervisorType() == HypervisorType.Hyperv) {
buf.append(" extra_pubnics=" + _routerExtraPublicNics);
}