Skip to content

Commit f767adf

Browse files
committed
Bug-Id: CLOUDSTACK-3439: Include dynamically created nics in Prepare for migration command in KVM
1 parent a520309 commit f767adf

8 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public String getName() {
7070
return name;
7171
}
7272

73+
public void setName(String name) {
74+
this.name = name;
75+
}
76+
7377
public String getDns2() {
7478
return dns2;
7579
}
@@ -371,4 +375,8 @@ public void setIp6Dns2(String ip6Dns2) {
371375
this.ip6Dns2 = ip6Dns2;
372376
}
373377

378+
public void setNetworId(long networkId){
379+
this.networkId = networkId;
380+
}
381+
374382
}

engine/api/src/org/apache/cloudstack/engine/orchestration/service/NetworkOrchestrationService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,6 @@ void implementNetworkElementsAndResources(DeployDestination dest, ReservationCon
217217
void removeDhcpServiceInSubnet(Nic nic);
218218

219219
boolean resourceCountNeedsUpdate(NetworkOffering ntwkOff, ACLType aclType);
220+
221+
void prepareAllNicsForMigration(VirtualMachineProfile vm, DeployDestination dest);
220222
}

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,6 +3284,7 @@ private void orchestrateMigrateForScale(String vmUuid, long srcHostId, DeployDes
32843284

32853285
VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
32863286
_networkMgr.prepareNicForMigration(profile, dest);
3287+
32873288
volumeMgr.prepareForMigration(profile, dest);
32883289

32893290
VirtualMachineTO to = toVmTO(profile);

engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.inject.Inject;
3838
import javax.naming.ConfigurationException;
3939

40+
import com.cloud.network.Networks;
4041
import org.apache.log4j.Logger;
4142
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
4243
import org.apache.cloudstack.context.CallContext;
@@ -1376,6 +1377,11 @@ public NicProfile prepareNic(VirtualMachineProfile vmProfile, DeployDestination
13761377

13771378
@Override
13781379
public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination dest) {
1380+
if(vm.getType().equals(VirtualMachine.Type.DomainRouter) && vm.getHypervisorType().equals(HypervisorType.KVM)){
1381+
//Include nics hot plugged and not stored in DB
1382+
prepareAllNicsForMigration(vm, dest);
1383+
return;
1384+
}
13791385
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
13801386
ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
13811387
for (NicVO nic : nics) {
@@ -1409,6 +1415,86 @@ public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination d
14091415
}
14101416
}
14111417

1418+
/*
1419+
Prepare All Nics for migration including the nics dynamically created and not stored in DB
1420+
This is a temporary workaround work KVM migration
1421+
Once clean fix is added by stored dynamically nics is DB, this workaround won't be needed
1422+
*/
1423+
@Override
1424+
public void prepareAllNicsForMigration(VirtualMachineProfile vm, DeployDestination dest) {
1425+
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
1426+
ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
1427+
Long guestNetworkId = null;
1428+
for (NicVO nic : nics) {
1429+
NetworkVO network = _networksDao.findById(nic.getNetworkId());
1430+
if(network.getTrafficType().equals(TrafficType.Guest) && network.getGuestType().equals(GuestType.Isolated)){
1431+
guestNetworkId = network.getId();
1432+
}
1433+
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
1434+
1435+
NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
1436+
NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate,
1437+
_networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
1438+
if(guru instanceof NetworkMigrationResponder){
1439+
if(!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)){
1440+
s_logger.error("NetworkGuru "+guru+" prepareForMigration failed."); // XXX: Transaction error
1441+
}
1442+
}
1443+
List<Provider> providersToImplement = getNetworkProviders(network.getId());
1444+
for (NetworkElement element : networkElements) {
1445+
if (providersToImplement.contains(element.getProvider())) {
1446+
if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) {
1447+
throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId());
1448+
}
1449+
if(element instanceof NetworkMigrationResponder){
1450+
if(!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)){
1451+
s_logger.error("NetworkElement "+element+" prepareForMigration failed."); // XXX: Transaction error
1452+
}
1453+
}
1454+
}
1455+
}
1456+
guru.updateNicProfile(profile, network);
1457+
vm.addNic(profile);
1458+
}
1459+
1460+
List<String> addedURIs = new ArrayList<String>();
1461+
if(guestNetworkId != null){
1462+
List<IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(guestNetworkId, null);
1463+
for (IPAddressVO userIp : publicIps){
1464+
PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
1465+
URI broadcastUri = BroadcastDomainType.Vlan.toUri(publicIp.getVlanTag());
1466+
long ntwkId = publicIp.getNetworkId();
1467+
Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ntwkId, vm.getId(),
1468+
broadcastUri.toString());
1469+
if(nic == null && !addedURIs.contains(broadcastUri.toString())){
1470+
//Nic details are not available in DB
1471+
//Create nic profile for migration
1472+
s_logger.debug("Creating nic profile for migration. BroadcastUri: "+broadcastUri.toString()+" NetworkId: "+ntwkId+" Vm: "+vm.getId());
1473+
NetworkVO network = _networksDao.findById(ntwkId);
1474+
Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
1475+
NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
1476+
NicProfile profile = new NicProfile();
1477+
profile.setDeviceId(255); //dummyId
1478+
profile.setIp4Address(userIp.getAddress().toString());
1479+
profile.setNetmask(publicIp.getNetmask());
1480+
profile.setGateway(publicIp.getGateway());
1481+
profile.setMacAddress(publicIp.getMacAddress());
1482+
profile.setBroadcastType(network.getBroadcastDomainType());
1483+
profile.setTrafficType(network.getTrafficType());
1484+
profile.setBroadcastUri(broadcastUri);
1485+
profile.setIsolationUri(Networks.IsolationType.Vlan.toUri(publicIp.getVlanTag()));
1486+
profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
1487+
profile.setName(_networkModel.getNetworkTag(vm.getHypervisorType(), network));
1488+
profile.setNetworId(network.getId());
1489+
1490+
guru.updateNicProfile(profile, network);
1491+
vm.addNic(profile);
1492+
addedURIs.add(broadcastUri.toString());
1493+
}
1494+
}
1495+
}
1496+
}
1497+
14121498
private NicProfile findNicProfileById(VirtualMachineProfile vm, long id) {
14131499
for (NicProfile nic : vm.getNics()) {
14141500
if (nic.getId() == id) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
8484

8585
void lockRange(long vlandbId);
8686

87+
List<IPAddressVO> listByAssociatedVmId(long vmId);
8788
}

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

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

433+
@Override
434+
public List<IPAddressVO> listByAssociatedVmId(long vmId) {
435+
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
436+
sc.setParameters("associatedWithVmId", vmId);
437+
return listBy(sc);
438+
}
439+
433440
@Override
434441
public void lockRange(long vlandbId) {
435442
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();

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

Lines changed: 4 additions & 0 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

@@ -112,6 +113,9 @@ public NicTO toNicTO(NicProfile profile) {
112113
to.setNicSecIps(secIps);
113114
} else {
114115
s_logger.warn("Unabled to load NicVO for NicProfile " + profile.getId());
116+
//Workaround for dynamically created nics
117+
//FixMe: uuid and secondary IPs can be made part of nic profile
118+
to.setUuid(UUID.randomUUID().toString());
115119
}
116120

117121
//check whether the this nic has secondary ip addresses set

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,11 @@ public boolean resourceCountNeedsUpdate(NetworkOffering ntwkOff, ACLType aclType
838838
return false; //To change body of implemented methods use File | Settings | File Templates.
839839
}
840840

841+
@Override
842+
public void prepareAllNicsForMigration(VirtualMachineProfile vm, DeployDestination dest) {
843+
return;
844+
}
845+
841846
@Override
842847
public void prepareNicForMigration(VirtualMachineProfile vm, DeployDestination dest) {
843848
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)