Skip to content

Commit c498d2d

Browse files
bvbharatSheng Yang
authored andcommitted
CLOUDSTACK-4083 if a failure occurs while adding VM to another network (this should be the first vm in the subnet). The ip alias created as a part of this process is not removed.
(Sheng: remove the unnecessary import, remove blank line, upper case the bug id) Signed-off-by: Sheng Yang <sheng.yang@citrix.com>
1 parent 70facdc commit c498d2d

5 files changed

Lines changed: 70 additions & 76 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,6 @@ Map<String, String> finalizeServicesAndProvidersForNetwork(NetworkOffering offer
388388
PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, VlanType type, List<Long> vlanDbIds, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException;
389389

390390
void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanceVO> vm, DeployDestination dest);
391+
392+
void removeDhcpServiceInSubnet(NicVO nic);
391393
}

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

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

40+
import com.cloud.vm.NicIpAlias;
41+
import com.cloud.vm.dao.NicIpAliasDao;
42+
import com.cloud.vm.dao.NicIpAliasVO;
4043
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
4144
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4245
import org.apache.cloudstack.region.PortableIp;
@@ -266,6 +269,10 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
266269
DataCenterVnetDao _datacenterVnetDao;
267270
@Inject
268271
NetworkAccountDao _networkAccountDao;
272+
@Inject
273+
protected NicIpAliasDao _nicIpAliasDao;
274+
@Inject
275+
protected IPAddressDao _publicIpAddressDao;
269276

270277
List<NetworkGuru> _networkGurus;
271278
public List<NetworkGuru> getNetworkGurus() {
@@ -2429,6 +2436,11 @@ protected void removeNic(VirtualMachineProfile<? extends VMInstanceVO> vm, NicVO
24292436
}
24302437
}
24312438

2439+
// remove the dhcpservice ip if this is the last nic in subnet.
2440+
if (vm.getType() == Type.User && isDhcpAccrossMultipleSubnetsSupported(network) && isLastNicInSubnet(nic) &&
2441+
network.getTrafficType() == TrafficType.Guest && network.getGuestType() == GuestType.Shared) {
2442+
removeDhcpServiceInSubnet(nic);
2443+
}
24322444
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
24332445
guru.deallocate(network, profile, vm);
24342446
_nicDao.remove(nic.getId());
@@ -2439,6 +2451,52 @@ protected void removeNic(VirtualMachineProfile<? extends VMInstanceVO> vm, NicVO
24392451
}
24402452
}
24412453

2454+
public boolean isDhcpAccrossMultipleSubnetsSupported(Network network) {
2455+
DhcpServiceProvider dhcpServiceProvider = getDhcpServiceProvider(network);
2456+
Map <Network.Capability, String> capabilities = dhcpServiceProvider.getCapabilities().get(Network.Service.Dhcp);
2457+
String supportsMultipleSubnets = capabilities.get(Network.Capability.DhcpAccrossMultipleSubnets);
2458+
if (supportsMultipleSubnets != null && Boolean.valueOf(supportsMultipleSubnets)) {
2459+
return true;
2460+
}
2461+
return false;
2462+
}
2463+
2464+
private boolean isLastNicInSubnet(NicVO nic) {
2465+
if (_nicDao.listByNetworkIdTypeAndGatewayAndBroadcastUri(nic.getNetworkId(), VirtualMachine.Type.User, nic.getGateway(), nic.getBroadcastUri()).size() > 1) {
2466+
return false;
2467+
}
2468+
return true;
2469+
}
2470+
2471+
@DB
2472+
@Override
2473+
public void removeDhcpServiceInSubnet(NicVO nic) {
2474+
Network network = _networksDao.findById(nic.getNetworkId());
2475+
DhcpServiceProvider dhcpServiceProvider = getDhcpServiceProvider(network);
2476+
try {
2477+
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active);
2478+
if (ipAlias != null) {
2479+
ipAlias.setState(NicIpAlias.state.revoked);
2480+
Transaction txn = Transaction.currentTxn();
2481+
txn.start();
2482+
_nicIpAliasDao.update(ipAlias.getId(),ipAlias);
2483+
IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address());
2484+
_publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId());
2485+
txn.commit();
2486+
if (!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) {
2487+
s_logger.warn("Failed to remove the ip alias on the router, marking it as removed in db and freed the allocated ip " + ipAlias.getIp4Address());
2488+
}
2489+
}
2490+
}
2491+
catch (ResourceUnavailableException e) {
2492+
//failed to remove the dhcpconfig on the router.
2493+
s_logger.info ("Unable to delete the ip alias due to unable to contact the virtualrouter.");
2494+
}
2495+
2496+
}
2497+
2498+
2499+
24422500
@Override
24432501
public void expungeNics(VirtualMachineProfile<? extends VMInstanceVO> vm) {
24442502
List<NicVO> nics = _nicDao.listByVmIdIncludingRemoved(vm.getId());

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

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@
3737
import javax.naming.ConfigurationException;
3838

3939
import com.cloud.dc.dao.VlanDao;
40-
import com.cloud.network.Networks;
4140
import com.cloud.network.dao.IPAddressDao;
42-
import com.cloud.network.dao.IPAddressVO;
43-
import com.cloud.network.element.DhcpServiceProvider;
44-
import com.cloud.vm.dao.NicIpAliasDao;
45-
import com.cloud.vm.dao.NicIpAliasVO;
4641
import org.apache.log4j.Logger;
4742

4843
import com.cloud.agent.AgentManager;
@@ -185,14 +180,8 @@
185180
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
186181
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
187182
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
188-
import org.apache.log4j.Logger;
189183

190184
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
191-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
192-
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
193-
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
194-
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
195-
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
196185

197186
@Local(value = VirtualMachineManager.class)
198187
public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMachineManager, Listener {
@@ -280,8 +269,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
280269
protected IPAddressDao _publicIpAddressDao;
281270
@Inject
282271
protected VlanDao _vlanDao;
283-
@Inject
284-
protected NicIpAliasDao _nicIpAliasDao;
285272

286273
protected List<DeploymentPlanner> _planners;
287274
public List<DeploymentPlanner> getPlanners() {
@@ -475,10 +462,6 @@ public <T extends VMInstanceVO> boolean advanceExpunge(T vm, User caller, Accoun
475462
s_logger.debug("Destroying vm " + vm);
476463
}
477464

478-
if (vm.getType() == VirtualMachine.Type.User) {
479-
removeDhcpServiceInsubnet(vm);
480-
}
481-
482465
VirtualMachineProfile<T> profile = new VirtualMachineProfileImpl<T>(vm);
483466

484467
HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
@@ -536,40 +519,7 @@ public <T extends VMInstanceVO> boolean advanceExpunge(T vm, User caller, Accoun
536519
return true;
537520
}
538521

539-
@DB
540-
private void removeDhcpServiceInsubnet(VirtualMachine vm) {
541-
//list all the nics which belong to this vm and are the last nics in the subnets.
542-
//we are using the info in these nics to remove the dhcp sercvice for these subnets.
543-
List<NicVO> nicList = listLastNicsInSubnet(vm);
544-
545-
if(nicList != null && nicList.size() != 0) {
546-
for (NicVO nic : nicList) {
547-
//free the ipalias on the routers corresponding to each of the nics.
548-
Network network = _networkDao.findById(nic.getNetworkId());
549-
DhcpServiceProvider dhcpServiceProvider = _networkMgr.getDhcpServiceProvider(network);
550-
try {
551-
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active);
552-
if (ipAlias != null) {
553-
ipAlias.setState(NicIpAlias.state.revoked);
554-
Transaction txn = Transaction.currentTxn();
555-
txn.start();
556-
_nicIpAliasDao.update(ipAlias.getId(),ipAlias);
557-
IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address());
558-
_publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId());
559-
txn.commit();
560-
if (!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) {
561-
s_logger.warn("Failed to remove the ip alias on the router, marking it as removed in db and freed the allocated ip " + ipAlias.getIp4Address());
562-
}
563-
}
564-
}
565-
catch (ResourceUnavailableException e) {
566-
//failed to remove the dhcpconfig on the router.
567-
s_logger.info ("Unable to delete the ip alias due to unable to contact the virtualrouter.");
568-
}
569522

570-
}
571-
}
572-
}
573523

574524
@Override
575525
public boolean start() {
@@ -1413,27 +1363,6 @@ public <T extends VMInstanceVO> boolean destroy(T vm, User user, Account caller)
14131363
return true;
14141364
}
14151365

1416-
//list all the nics which belong to this vm and are the last nics in the subnets.
1417-
//we are using the info in these nics to remove the dhcp sercvice for these subnets.
1418-
private List<NicVO> listLastNicsInSubnet(VirtualMachine vm) {
1419-
List<NicVO> nicList = _nicsDao.listByVmId(vm.getId());
1420-
List<NicVO> copyOfnicList = new ArrayList<NicVO>(nicList);
1421-
for (NicVO nic : nicList) {
1422-
Network network = _networkDao.findById(nic.getNetworkId());
1423-
DhcpServiceProvider dhcpServiceProvider = _networkMgr.getDhcpServiceProvider(network);
1424-
Map <Network.Capability, String> capabilities = dhcpServiceProvider.getCapabilities().get(Network.Service.Dhcp);
1425-
String supportsMultipleSubnets = capabilities.get(Network.Capability.DhcpAccrossMultipleSubnets);
1426-
if ((supportsMultipleSubnets != null && Boolean.valueOf(supportsMultipleSubnets) && network.getTrafficType() == Networks.TrafficType.Guest && network.getGuestType() == Network.GuestType.Shared)) {
1427-
//including the ip of the vm and the ipAlias
1428-
if (_nicsDao.listByNetworkIdTypeAndGatewayAndBroadcastUri(nic.getNetworkId(), VirtualMachine.Type.User, nic.getGateway(), nic.getBroadcastUri()).size() > 1) {
1429-
copyOfnicList.remove(nic);
1430-
}
1431-
} else {
1432-
copyOfnicList.remove(nic);
1433-
}
1434-
}
1435-
return copyOfnicList;
1436-
}
14371366

14381367
protected boolean checkVmOnHost(VirtualMachine vm, long hostId) throws AgentUnavailableException, OperationTimedoutException {
14391368
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
@@ -3037,11 +2966,6 @@ public boolean removeNicFromVm(VirtualMachine vm, NicVO nic) throws ConcurrentOp
30372966
_networkModel.isSecurityGroupSupportedInNetwork(network),
30382967
_networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
30392968

3040-
// Adding this to the dhcpservice config if this is the last nic in subnet.
3041-
if (vm.getType() == VirtualMachine.Type.User) {
3042-
removeDhcpServiceInsubnet(vm);
3043-
}
3044-
30452969
//1) Unplug the nic
30462970
if (vm.getState() == State.Running) {
30472971
NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,11 @@ public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanc
942942
//To change body of implemented methods use File | Settings | File Templates.
943943
}
944944

945+
@Override
946+
public void removeDhcpServiceInSubnet(NicVO nic) {
947+
//To change body of implemented methods use File | Settings | File Templates.
948+
}
949+
945950
@Override
946951
public void prepareNicForMigration(
947952
VirtualMachineProfile<? extends VMInstanceVO> vm,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,11 @@ public void prepareAllNicsForMigration(VirtualMachineProfile<? extends VMInstanc
14181418
//To change body of implemented methods use File | Settings | File Templates.
14191419
}
14201420

1421+
@Override
1422+
public void removeDhcpServiceInSubnet(NicVO nic) {
1423+
//To change body of implemented methods use File | Settings | File Templates.
1424+
}
1425+
14211426
@Override
14221427
public void prepareNicForMigration(
14231428
VirtualMachineProfile<? extends VMInstanceVO> vm,

0 commit comments

Comments
 (0)