Skip to content

Commit 1550f5e

Browse files
author
Kishan Kavala
committed
CLOUDSTACK-3439: Include dynamically created nics in Prepare for migration command in KVM
1 parent 7cb1c6f commit 1550f5e

10 files changed

Lines changed: 126 additions & 14 deletions

File tree

‎api/src/com/cloud/vm/NicProfile.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public String getName() {
6666
return name;
6767
}
6868

69+
public void setName(String name) {
70+
this.name = name;
71+
}
72+
6973
public String getDns2() {
7074
return dns2;
7175
}

‎engine/schema/src/com/cloud/network/dao/IPAddressDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
8080
boolean deletePublicIPRange(long vlanDbId) ;
8181

8282
void lockRange(long vlandbId);
83+
84+
List<IPAddressVO> listByAssociatedVmId(long vmId);
8385
}

‎engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,13 @@ public IPAddressVO findByAssociatedVmIdAndVmIp(long vmId, String vmIp) {
409409
return findOneBy(sc);
410410
}
411411

412+
@Override
413+
public List<IPAddressVO> listByAssociatedVmId(long vmId) {
414+
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
415+
sc.setParameters("associatedWithVmId", vmId);
416+
return listBy(sc);
417+
}
418+
412419
@Override
413420
public void lockRange(long vlandbId) {
414421
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();

‎server/src/com/cloud/hypervisor/HypervisorGuruBase.java‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.UUID;
2122

2223
import javax.inject.Inject;
2324

@@ -73,15 +74,22 @@ public NicTO toNicTO(NicProfile profile) {
7374

7475
// Workaround to make sure the TO has the UUID we need for Niciri integration
7576
NicVO nicVO = _nicDao.findById(profile.getId());
76-
to.setUuid(nicVO.getUuid());
77-
//check whether the this nic has secondary ip addresses set
78-
//set nic secondary ip address in NicTO which are used for security group
79-
// configuration. Use full when vm stop/start
80-
List <String> secIps = null;
81-
if (nicVO.getSecondaryIp()) {
82-
secIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nicVO.getId());
77+
if(nicVO != null){
78+
to.setUuid(nicVO.getUuid());
79+
//check whether the this nic has secondary ip addresses set
80+
//set nic secondary ip address in NicTO which are used for security group
81+
// configuration. Use full when vm stop/start
82+
List <String> secIps = null;
83+
if (nicVO.getSecondaryIp()) {
84+
secIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nicVO.getId());
85+
}
86+
to.setNicSecIps(secIps);
87+
} else {
88+
//Workaround for dynamically created nics
89+
//FixMe: uuid and secondary IPs can be made part of nic profile
90+
to.setUuid(UUID.randomUUID().toString());
8391
}
84-
to.setNicSecIps(secIps);
92+
8593
return to;
8694
}
8795

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,5 @@ Map<String, String> finalizeServicesAndProvidersForNetwork(NetworkOffering offer
387387

388388
PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, VlanType type, List<Long> vlanDbIds, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException;
389389

390+
void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest);
390391
}

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,80 @@ public void prepareNicForMigration(VirtualMachineProfile<? extends VMInstanceVO>
21682168
}
21692169
}
21702170

2171+
/*
2172+
Prepare All Nics for migration including the nics dynamically created and not stored in DB
2173+
This is a temporary workaround work KVM migration
2174+
Once clean fix is added by stored dynamically nics is DB, this workaround won't be needed
2175+
*/
2176+
@Override
2177+
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
2178+
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
2179+
ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
2180+
Long guestNetworkId = null;
2181+
for (NicVO nic : nics) {
2182+
NetworkVO network = _networksDao.findById(nic.getNetworkId());
2183+
if(network.getTrafficType().equals(TrafficType.Guest) && network.getGuestType().equals(GuestType.Isolated)){
2184+
guestNetworkId = network.getId();
2185+
}
2186+
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
2187+
2188+
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
2189+
NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate,
2190+
_networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
2191+
if(guru instanceof NetworkMigrationResponder){
2192+
if(!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)){
2193+
s_logger.error("NetworkGuru "+guru+" prepareForMigration failed."); // XXX: Transaction error
2194+
}
2195+
}
2196+
for (NetworkElement element : _networkElements) {
2197+
if(element instanceof NetworkMigrationResponder){
2198+
if(!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)){
2199+
s_logger.error("NetworkElement "+element+" prepareForMigration failed."); // XXX: Transaction error
2200+
}
2201+
}
2202+
}
2203+
guru.updateNicProfile(profile, network);
2204+
vm.addNic(profile);
2205+
}
2206+
2207+
List<String> addedURIs = new ArrayList<String>();
2208+
if(guestNetworkId != null){
2209+
List<IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(guestNetworkId, null);
2210+
for (IPAddressVO userIp : publicIps){
2211+
PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
2212+
URI broadcastUri = BroadcastDomainType.Vlan.toUri(publicIp.getVlanTag());
2213+
long ntwkId = publicIp.getNetworkId();
2214+
Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ntwkId, vm.getId(),
2215+
broadcastUri.toString());
2216+
if(nic == null && !addedURIs.contains(broadcastUri.toString())){
2217+
//Nic details are not available in DB
2218+
//Create nic profile for migration
2219+
s_logger.debug("Creating nic profile for migration. BroadcastUri: "+broadcastUri.toString()+" NetworkId: "+ntwkId+" Vm: "+vm.getId());
2220+
NetworkVO network = _networksDao.findById(ntwkId);
2221+
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
2222+
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
2223+
NicProfile profile = new NicProfile();
2224+
profile.setDeviceId(255); //dummyId
2225+
profile.setIp4Address(userIp.getAddress().toString());
2226+
profile.setNetmask(publicIp.getNetmask());
2227+
profile.setGateway(publicIp.getGateway());
2228+
profile.setMacAddress(publicIp.getMacAddress());
2229+
profile.setBroadcastType(network.getBroadcastDomainType());
2230+
profile.setTrafficType(network.getTrafficType());
2231+
profile.setBroadcastUri(broadcastUri);
2232+
profile.setIsolationUri(IsolationType.Vlan.toUri(publicIp.getVlanTag()));
2233+
profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
2234+
profile.setName(_networkModel.getNetworkTag(vm.getHypervisorType(), network));
2235+
2236+
guru.updateNicProfile(profile, network);
2237+
vm.addNic(profile);
2238+
addedURIs.add(broadcastUri.toString());
2239+
}
2240+
}
2241+
}
2242+
}
2243+
2244+
21712245
private NicProfile findNicProfileById(VirtualMachineProfile<? extends VMInstanceVO> vm, long id){
21722246
for(NicProfile nic: vm.getNics()){
21732247
if(nic.getId() == id){
@@ -4369,7 +4443,6 @@ public List<NicProfile> getNicProfiles(VirtualMachine vm) {
43694443
return profiles;
43704444
}
43714445

4372-
43734446
@Override
43744447
public int getNetworkLockTimeout() {
43754448
return _networkLockTimeout;

‎server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
import com.cloud.network.dao.UserIpv6AddressDao;
147147
import com.cloud.network.dao.VirtualRouterProviderDao;
148148
import com.cloud.network.dao.VpnUserDao;
149+
import com.cloud.network.guru.NetworkGuru;
150+
import com.cloud.network.guru.PublicNetworkGuru;
149151
import com.cloud.network.lb.LoadBalancingRule;
150152
import com.cloud.network.lb.LoadBalancingRule.LbDestination;
151153
import com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy;
@@ -194,6 +196,7 @@
194196
import com.cloud.utils.Pair;
195197
import com.cloud.utils.PasswordGenerator;
196198
import com.cloud.utils.StringUtils;
199+
import com.cloud.utils.component.AdapterBase;
197200
import com.cloud.utils.component.ManagerBase;
198201
import com.cloud.utils.concurrency.NamedThreadFactory;
199202
import com.cloud.utils.db.DB;
@@ -237,6 +240,7 @@
237240
import javax.ejb.Local;
238241
import javax.inject.Inject;
239242
import javax.naming.ConfigurationException;
243+
import java.net.URI;
240244
import java.util.ArrayList;
241245
import java.util.Calendar;
242246
import java.util.Collections;

‎server/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,12 +1521,19 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
15211521
}
15221522

15231523
VirtualMachineProfile<VMInstanceVO> vmSrc = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
1524+
15241525
for(NicProfile nic: _networkMgr.getNicProfiles(vm)){
15251526
vmSrc.addNic(nic);
15261527
}
15271528

15281529
VirtualMachineProfile<VMInstanceVO> profile = new VirtualMachineProfileImpl<VMInstanceVO>(vm);
1529-
_networkMgr.prepareNicForMigration(profile, dest);
1530+
1531+
if(vm.getType().equals(VirtualMachine.Type.DomainRouter) && vm.getHypervisorType().equals(HypervisorType.KVM)){
1532+
//Include nics hot plugged and not stored in DB
1533+
_networkMgr.prepareAllNicsForMigration(profile, dest);
1534+
} else {
1535+
_networkMgr.prepareNicForMigration(profile, dest);
1536+
}
15301537
volumeMgr.prepareForMigration(profile, dest);
15311538

15321539
VirtualMachineTO to = toVmTO(profile);

‎server/test/com/cloud/network/MockNetworkManagerImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,11 @@ public PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account ow
937937
return null; //To change body of implemented methods use File | Settings | File Templates.
938938
}
939939

940+
@Override
941+
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
942+
//To change body of implemented methods use File | Settings | File Templates.
943+
}
944+
940945
@Override
941946
public void prepareNicForMigration(
942947
VirtualMachineProfile<? extends VMInstanceVO> vm,

‎server/test/com/cloud/vpc/MockNetworkManagerImpl.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,11 +1413,12 @@ public PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account ow
14131413
return null; //To change body of implemented methods use File | Settings | File Templates.
14141414
}
14151415

1416+
@Override
1417+
public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest) {
1418+
//To change body of implemented methods use File | Settings | File Templates.
1419+
}
14161420

1417-
1418-
1419-
1420-
@Override
1421+
@Override
14211422
public void prepareNicForMigration(
14221423
VirtualMachineProfile<? extends VMInstanceVO> vm,
14231424
DeployDestination dest) {

0 commit comments

Comments
 (0)