CLOUDSTACK-3439: Include dynamically created nics in Prepare for migration command in KVM

This commit is contained in:
Kishan Kavala 2013-07-30 17:52:10 +05:30
parent 7cb1c6fa7b
commit 1550f5e26c
10 changed files with 126 additions and 14 deletions

View File

@ -66,6 +66,10 @@ public class NicProfile implements InternalIdentity {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDns2() {
return dns2;
}

View File

@ -80,4 +80,6 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
boolean deletePublicIPRange(long vlanDbId) ;
void lockRange(long vlandbId);
List<IPAddressVO> listByAssociatedVmId(long vmId);
}

View File

@ -409,6 +409,13 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
return findOneBy(sc);
}
@Override
public List<IPAddressVO> listByAssociatedVmId(long vmId) {
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("associatedWithVmId", vmId);
return listBy(sc);
}
@Override
public void lockRange(long vlandbId) {
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();

View File

@ -18,6 +18,7 @@ package com.cloud.hypervisor;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.inject.Inject;
@ -73,15 +74,22 @@ public abstract class HypervisorGuruBase extends AdapterBase implements Hypervis
// Workaround to make sure the TO has the UUID we need for Niciri integration
NicVO nicVO = _nicDao.findById(profile.getId());
to.setUuid(nicVO.getUuid());
//check whether the this nic has secondary ip addresses set
//set nic secondary ip address in NicTO which are used for security group
// configuration. Use full when vm stop/start
List <String> secIps = null;
if (nicVO.getSecondaryIp()) {
secIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nicVO.getId());
if(nicVO != null){
to.setUuid(nicVO.getUuid());
//check whether the this nic has secondary ip addresses set
//set nic secondary ip address in NicTO which are used for security group
// configuration. Use full when vm stop/start
List <String> secIps = null;
if (nicVO.getSecondaryIp()) {
secIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nicVO.getId());
}
to.setNicSecIps(secIps);
} else {
//Workaround for dynamically created nics
//FixMe: uuid and secondary IPs can be made part of nic profile
to.setUuid(UUID.randomUUID().toString());
}
to.setNicSecIps(secIps);
return to;
}

View File

@ -387,4 +387,5 @@ public interface NetworkManager {
PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, VlanType type, List<Long> vlanDbIds, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException;
void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest);
}

View File

@ -2168,6 +2168,80 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
}
}
/*
Prepare All Nics for migration including the nics dynamically created and not stored in DB
This is a temporary workaround work KVM migration
Once clean fix is added by stored dynamically nics is DB, this workaround won't be needed
*/
@Override
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
Long guestNetworkId = null;
for (NicVO nic : nics) {
NetworkVO network = _networksDao.findById(nic.getNetworkId());
if(network.getTrafficType().equals(TrafficType.Guest) && network.getGuestType().equals(GuestType.Isolated)){
guestNetworkId = network.getId();
}
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate,
_networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
if(guru instanceof NetworkMigrationResponder){
if(!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)){
s_logger.error("NetworkGuru "+guru+" prepareForMigration failed."); // XXX: Transaction error
}
}
for (NetworkElement element : _networkElements) {
if(element instanceof NetworkMigrationResponder){
if(!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)){
s_logger.error("NetworkElement "+element+" prepareForMigration failed."); // XXX: Transaction error
}
}
}
guru.updateNicProfile(profile, network);
vm.addNic(profile);
}
List<String> addedURIs = new ArrayList<String>();
if(guestNetworkId != null){
List<IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(guestNetworkId, null);
for (IPAddressVO userIp : publicIps){
PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
URI broadcastUri = BroadcastDomainType.Vlan.toUri(publicIp.getVlanTag());
long ntwkId = publicIp.getNetworkId();
Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ntwkId, vm.getId(),
broadcastUri.toString());
if(nic == null && !addedURIs.contains(broadcastUri.toString())){
//Nic details are not available in DB
//Create nic profile for migration
s_logger.debug("Creating nic profile for migration. BroadcastUri: "+broadcastUri.toString()+" NetworkId: "+ntwkId+" Vm: "+vm.getId());
NetworkVO network = _networksDao.findById(ntwkId);
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
NicProfile profile = new NicProfile();
profile.setDeviceId(255); //dummyId
profile.setIp4Address(userIp.getAddress().toString());
profile.setNetmask(publicIp.getNetmask());
profile.setGateway(publicIp.getGateway());
profile.setMacAddress(publicIp.getMacAddress());
profile.setBroadcastType(network.getBroadcastDomainType());
profile.setTrafficType(network.getTrafficType());
profile.setBroadcastUri(broadcastUri);
profile.setIsolationUri(IsolationType.Vlan.toUri(publicIp.getVlanTag()));
profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
profile.setName(_networkModel.getNetworkTag(vm.getHypervisorType(), network));
guru.updateNicProfile(profile, network);
vm.addNic(profile);
addedURIs.add(broadcastUri.toString());
}
}
}
}
private NicProfile findNicProfileById(VirtualMachineProfile<? extends VMInstanceVO> vm, long id){
for(NicProfile nic: vm.getNics()){
if(nic.getId() == id){
@ -4369,7 +4443,6 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
return profiles;
}
@Override
public int getNetworkLockTimeout() {
return _networkLockTimeout;

View File

@ -146,6 +146,8 @@ import com.cloud.network.dao.Site2SiteVpnGatewayDao;
import com.cloud.network.dao.UserIpv6AddressDao;
import com.cloud.network.dao.VirtualRouterProviderDao;
import com.cloud.network.dao.VpnUserDao;
import com.cloud.network.guru.NetworkGuru;
import com.cloud.network.guru.PublicNetworkGuru;
import com.cloud.network.lb.LoadBalancingRule;
import com.cloud.network.lb.LoadBalancingRule.LbDestination;
import com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy;
@ -194,6 +196,7 @@ import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.PasswordGenerator;
import com.cloud.utils.StringUtils;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.concurrency.NamedThreadFactory;
import com.cloud.utils.db.DB;
@ -237,6 +240,7 @@ import org.springframework.stereotype.Component;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

View File

@ -1521,12 +1521,19 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
}
VirtualMachineProfile<VMInstanceVO> vmSrc = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
for(NicProfile nic: _networkMgr.getNicProfiles(vm)){
vmSrc.addNic(nic);
}
VirtualMachineProfile<VMInstanceVO> profile = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
_networkMgr.prepareNicForMigration(profile, dest);
if(vm.getType().equals(VirtualMachine.Type.DomainRouter) && vm.getHypervisorType().equals(HypervisorType.KVM)){
//Include nics hot plugged and not stored in DB
_networkMgr.prepareAllNicsForMigration(profile, dest);
} else {
_networkMgr.prepareNicForMigration(profile, dest);
}
volumeMgr.prepareForMigration(profile, dest);
VirtualMachineTO to = toVmTO(profile);

View File

@ -937,6 +937,11 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void prepareNicForMigration(
VirtualMachineProfile<? extends VMInstanceVO> vm,

View File

@ -1413,11 +1413,12 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
@Override
public void prepareNicForMigration(
VirtualMachineProfile<? extends VMInstanceVO> vm,
DeployDestination dest) {