Skip to content

Commit 62b3d54

Browse files
committed
bug 8753: never release public Ip address without ensuring that all corresponding resources (PF/StaticNat/Lb rules) are cleaned up.
Fixed couple of other problems along: * when expunge PF/Static nat rules as a part of vmExpunge/IpRelease process, first mark all rules as Revoke, and then send commands to the backend. Group commands by Ip address. Before we used to do Revoke/Send per rule basis. * When release source nat rule, make sure that corresponding vpn (if exists) is being expunged.
1 parent ec76406 commit 62b3d54

15 files changed

Lines changed: 347 additions & 167 deletions

‎api/src/com/cloud/api/commands/DisassociateIPAddrCmd.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public String getEventDescription() {
9292
public long getEntityOwnerId() {
9393
if (ownerId == null) {
9494
IpAddress ip = getIpAddress(id);
95+
if (ip == null) {
96+
throw new InvalidParameterValueException("Unable to find ip address by id=" + id);
97+
}
9598
ownerId = ip.getAccountId();
9699
}
97100
return ownerId;

‎api/src/com/cloud/network/rules/RulesService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface RulesService {
5555

5656
boolean enableOneToOneNat(long ipAddressId, long vmId) throws NetworkRuleConflictException;
5757

58-
boolean disableOneToOneNat(long ipAddressId);
58+
boolean disableOneToOneNat(long ipAddressId) throws ResourceUnavailableException;
5959

6060
PortForwardingRule getPortForwardigRule(long ruleId);
6161
FirewallRule getFirewallRule(long ruleId);

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

Lines changed: 120 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
440440
List<PublicIp> publicIps = new ArrayList<PublicIp>();
441441
if (userIps != null && !userIps.isEmpty()) {
442442
for (IPAddressVO userIp : userIps) {
443-
PublicIp publicIp = new PublicIp(userIp, _vlanDao.findById(userIp.getVlanId()), userIp.getMacAddress());
444-
publicIps.add(publicIp);
443+
PublicIp publicIp = new PublicIp(userIp, _vlanDao.findById(userIp.getVlanId()), userIp.getMacAddress());
444+
publicIps.add(publicIp);
445445
}
446446
}
447447

@@ -468,7 +468,13 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
468468
markPublicIpAsAllocated(addr);
469469

470470
} else if (addr.getState() == IpAddress.State.Releasing) {
471-
unassignPublicIpAddress(addr);
471+
//Cleanup all the resources for ip address if there are any, and only then unassign ip in the system
472+
if (cleanupIpResources(addr.getId(), Account.ACCOUNT_ID_SYSTEM, _accountMgr.getSystemAccount())) {
473+
unassignPublicIpAddress(addr);
474+
} else {
475+
success = false;
476+
s_logger.warn("Failed to release resources for ip address id=" + addr.getId());
477+
}
472478
}
473479
}
474480
}
@@ -608,8 +614,10 @@ public IpAddress associateIP(AssociateIPAddrCmd cmd) throws ResourceAllocationEx
608614

609615
@Override
610616
public boolean releasePublicIpAddress(long addrId, long userId, Account caller) {
617+
618+
//mark ip address as Releasing
611619
IPAddressVO ip = _ipAddressDao.markAsUnavailable(addrId);
612-
assert (ip != null) : "Unable to mark the ip address id=" + addrId + " owned by " + ip.getAccountId() + " as unavailable.";
620+
assert (ip != null) : "Unable to mark the ip address id=" + addrId + " as unavailable.";
613621
if (ip == null) {
614622
return true;
615623
}
@@ -619,30 +627,24 @@ public boolean releasePublicIpAddress(long addrId, long userId, Account caller)
619627
}
620628

621629
boolean success = true;
622-
try {
623-
if (!_rulesMgr.revokeAllRules(addrId, userId, caller)) {
624-
s_logger.warn("Unable to revoke all the port forwarding rules for ip " + ip);
625-
success = false;
626-
}
627-
} catch (ResourceUnavailableException e) {
628-
s_logger.warn("Unable to revoke all the port forwarding rules for ip " + ip, e);
629-
success = false;
630-
}
631-
632-
if (!_lbMgr.removeAllLoadBalanacers(addrId, caller, userId)) {
633-
s_logger.warn("Unable to revoke all the load balancer rules for ip " + ip);
630+
631+
//Cleanup all ip address resources - PF/LB/Static nat rules
632+
if (cleanupIpResources(addrId, userId, caller)) {
633+
unassignPublicIpAddress(ip);
634+
} else {
634635
success = false;
636+
s_logger.warn("Failed to release resources for ip address id=" + addrId);
635637
}
636638

637-
if (ip.getAssociatedWithNetworkId() != null) {
639+
if (ip.getAssociatedWithNetworkId() != null) {
638640
Network network = _networksDao.findById(ip.getAssociatedWithNetworkId());
639641
try {
640642
if (!applyIpAssociations(network, true)) {
641643
s_logger.warn("Unable to apply ip address associations for " + network);
642644
success = false;
643645
}
644646
} catch (ResourceUnavailableException e) {
645-
throw new CloudRuntimeException("We should nver get to here because we used true when applyIpAssociations", e);
647+
throw new CloudRuntimeException("We should never get to here because we used true when applyIpAssociations", e);
646648
}
647649
}
648650

@@ -1828,7 +1830,7 @@ public boolean deleteNetwork(long networkId){
18281830
List<UserVmVO> userVms = _vmDao.listByNetworkId(networkId);
18291831

18301832
for (UserVmVO vm : userVms) {
1831-
if (!(vm.getState() == VirtualMachine.State.Error || vm.getState() == VirtualMachine.State.Expunging)) {
1833+
if (!(vm.getState() == VirtualMachine.State.Error || (vm.getState() == VirtualMachine.State.Expunging && vm.getRemoved() != null))) {
18321834
throw new InvalidParameterValueException("Can't delete the network, not all user vms are expunged");
18331835
}
18341836
}
@@ -1907,8 +1909,7 @@ public void shutdownNetwork(long networkId, ReservationContext context) {
19071909
txn.commit();
19081910
}
19091911

1910-
@DB
1911-
@Override
1912+
@Override @DB
19121913
public boolean destroyNetwork(long networkId, ReservationContext context) {
19131914
Account callerAccount = _accountMgr.getAccount(context.getCaller().getAccountId());
19141915

@@ -1930,17 +1931,7 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
19301931

19311932
boolean success = true;
19321933

1933-
// release ip addresses associated with the network if there are any - for Virtual case
1934-
List<IPAddressVO> ipsToRelease = _ipAddressDao.listByAssociatedNetwork(networkId);
1935-
if (ipsToRelease != null && !ipsToRelease.isEmpty()) {
1936-
for (IPAddressVO ip : ipsToRelease) {
1937-
//delete load balancer rules associated with the ip address before unassigning it
1938-
_lbMgr.removeAllLoadBalanacers(ip.getId(), callerAccount, context.getCaller().getId());
1939-
unassignPublicIpAddress(ip);
1940-
}
1941-
1942-
s_logger.debug("Ip addresses associated with network " + networkId + " are unassigned successfully as a part of network id=" + networkId + " destroy");
1943-
}
1934+
cleanupNetworkResources(networkId, callerAccount, context.getCaller().getId());
19441935

19451936
for (NetworkElement element : _networkElements) {
19461937
try {
@@ -1985,6 +1976,52 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
19851976

19861977
return success;
19871978
}
1979+
1980+
private boolean cleanupNetworkResources(long networkId, Account caller, long callerUserId) {
1981+
boolean success = true;
1982+
Network network = getNetwork(networkId);
1983+
1984+
//remove all PF/Static Nat rules for the network
1985+
try {
1986+
if (_rulesMgr.revokeAllRulesForNetwork(networkId, callerUserId, caller)) {
1987+
s_logger.debug("Successfully cleaned up portForwarding/staticNat rules for network id=" + networkId);
1988+
} else {
1989+
success = false;
1990+
s_logger.warn("Failed to release portForwarding/StaticNat rules as a part of network id=" + networkId + " cleanup");
1991+
}
1992+
} catch (ResourceUnavailableException ex) {
1993+
success = false;
1994+
//shouldn't even come here as network is being cleaned up after all network elements are shutdown
1995+
s_logger.warn("Failed to release portForwarding/StaticNat rules as a part of network id=" + networkId + " cleanup due to resourceUnavailable ", ex);
1996+
}
1997+
1998+
//remove all LB rules for the network
1999+
if (_lbMgr.removeAllLoadBalanacersForNetwork(networkId, caller, callerUserId)) {
2000+
s_logger.debug("Successfully cleaned up load balancing rules for network id=" + networkId);
2001+
} else {
2002+
//shouldn't even come here as network is being cleaned up after all network elements are shutdown
2003+
success = false;
2004+
s_logger.warn("Failed to cleanup LB rules as a part of network id=" + networkId + " cleanup");
2005+
}
2006+
2007+
//release all ip addresses
2008+
List<IPAddressVO> ipsToRelease = _ipAddressDao.listByAssociatedNetwork(networkId);
2009+
for (IPAddressVO ipToRelease : ipsToRelease) {
2010+
IPAddressVO ip = _ipAddressDao.markAsUnavailable(ipToRelease.getId());
2011+
assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable.";
2012+
}
2013+
2014+
try {
2015+
if (!applyIpAssociations(network, true)) {
2016+
s_logger.warn("Unable to apply ip address associations for " + network);
2017+
success = false;
2018+
}
2019+
} catch (ResourceUnavailableException e) {
2020+
throw new CloudRuntimeException("We should never get to here because we used true when applyIpAssociations", e);
2021+
}
2022+
2023+
return success;
2024+
}
19882025

19892026
private boolean deleteVlansInNetwork(long networkId, long userId) {
19902027
List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
@@ -2080,7 +2117,7 @@ public void run() {
20802117

20812118
@Override @ActionEvent (eventType=EventTypes.EVENT_NETWORK_RESTART, eventDescription="restarting network", async=true)
20822119
public boolean restartNetwork(RestartNetworkCmd cmd) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
2083-
// This method restarts all network elements belonging to the network
2120+
// This method restarts all network elements belonging to the network and re-applies all the rules
20842121
Long networkId = cmd.getNetworkId();
20852122
NetworkVO network = _networksDao.findById(networkId);
20862123
Account owner = _accountMgr.getAccount(network.getAccountId());
@@ -2090,51 +2127,61 @@ public boolean restartNetwork(RestartNetworkCmd cmd) throws ConcurrentOperationE
20902127
ReservationContext context = new ReservationContextImpl(null, null, caller, owner);
20912128

20922129
_accountMgr.checkAccess(callerAccount, network);
2130+
2131+
boolean success = true;
20932132

20942133
s_logger.debug("Restarting network " + networkId + "...");
20952134
for (NetworkElement element : _networkElements) {
20962135
//stop and start the network element
20972136
if (!element.restart(network, context)) {
20982137
s_logger.warn("Failed to restart network element(s) as a part of network id" + networkId + " restart");
2099-
return false;
2138+
success = false;
21002139
}
21012140
}
21022141

21032142
//associate all ip addresses
21042143
if (!applyIpAssociations(network, false)) {
21052144
s_logger.warn("Failed to apply ip addresses as a part of network id" + networkId + " restart");
2106-
return false;
2145+
success = false;
21072146
}
21082147

21092148
//apply port forwarding rules
21102149
if (!_rulesMgr.applyPortForwardingRulesForNetwork(networkId, false, context.getAccount())) {
21112150
s_logger.warn("Failed to reapply port forwarding rule(s) as a part of network id=" + networkId + " restart");
2151+
success = false;
21122152
}
21132153

21142154
//apply static nat rules
21152155
if (!_rulesMgr.applyStaticNatRulesForNetwork(networkId, false, context.getAccount())) {
21162156
s_logger.warn("Failed to reapply static nat rule(s) as a part of network id=" + networkId + " restart");
2157+
success = false;
21172158
}
21182159

21192160
//apply load balancer rules
21202161
if (!_lbMgr.applyLoadBalancersForNetwork(networkId)) {
21212162
s_logger.warn("Failed to reapply load balancer rules as a part of network id=" + networkId + " restart");
2122-
return false;
2163+
success = false;
21232164
}
21242165

21252166
//apply vpn rules
21262167
List<? extends RemoteAccessVpn> vpnsToReapply = _vpnMgr.listRemoteAccessVpns(networkId);
21272168
if (vpnsToReapply != null) {
21282169
for (RemoteAccessVpn vpn : vpnsToReapply) {
2170+
//Start remote access vpn per ip
21292171
if (_vpnMgr.startRemoteAccessVpn(vpn.getServerAddressId()) == null) {
2130-
s_logger.warn("Failed to reapply load balancer rules as a part of network id=" + networkId + " restart");
2131-
return false;
2172+
s_logger.warn("Failed to reapply vpn rules as a part of network id=" + networkId + " restart");
2173+
success = false;
21322174
}
21332175
}
21342176
}
21352177

2136-
s_logger.debug("Network id=" + networkId + " is restarted successfully.");
2137-
return true;
2178+
if (success) {
2179+
s_logger.debug("Network id=" + networkId + " is restarted successfully.");
2180+
} else {
2181+
s_logger.warn("Network id=" + networkId + " failed to restart.");
2182+
}
2183+
2184+
return success;
21382185
}
21392186

21402187
@Override
@@ -2451,4 +2498,37 @@ public boolean isServiceSupported(long networkId, Network.Service service) {
24512498

24522499
return false;
24532500
}
2501+
2502+
private boolean cleanupIpResources(long ipId, long userId, Account caller) {
2503+
boolean success = true;
2504+
2505+
try {
2506+
s_logger.debug("Revoking all PF/StaticNat rules as a part of public IP id=" + ipId + " release...");
2507+
if (!_rulesMgr.revokeAllRulesForIp(ipId, userId, caller)) {
2508+
s_logger.warn("Unable to revoke all the port forwarding rules for ip id=" + ipId + " as a part of ip release");
2509+
success = false;
2510+
}
2511+
} catch (ResourceUnavailableException e) {
2512+
s_logger.warn("Unable to revoke all the port forwarding rules for ip id=" + ipId + " as a part of ip release", e);
2513+
success = false;
2514+
}
2515+
2516+
s_logger.debug("Revoking all LB rules as a part of public IP id=" + ipId + " release...");
2517+
if (!_lbMgr.removeAllLoadBalanacersForIp(ipId, caller, userId)) {
2518+
s_logger.warn("Unable to revoke all the load balancer rules for ip id=" + ipId + " as a part of ip release");
2519+
success = false;
2520+
}
2521+
2522+
//remote access vpn can be enabled only for static nat ip, so this part should never be executed under normal conditions
2523+
//only when ip address failed to be cleaned up as a part of account destroy and was marked as Releasing, this part of the code would be triggered
2524+
s_logger.debug("Cleaning up remote access vpns as a part of public IP id=" + ipId + " release...");
2525+
try {
2526+
_vpnMgr.destroyRemoteAccessVpn(ipId);
2527+
} catch (ResourceUnavailableException e) {
2528+
s_logger.warn("Unable to destroy remote access vpn for ip id=" + ipId + " as a part of ip release", e);
2529+
success = false;
2530+
}
2531+
2532+
return success;
2533+
}
24542534
}

‎server/src/com/cloud/network/dao/FirewallRulesDao.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
* Data Access Object for user_ip_address and ip_forwarding tables
2929
*/
3030
public interface FirewallRulesDao extends GenericDao<FirewallRuleVO, Long> {
31-
List<FirewallRuleVO> listByIpAndNotRevoked(long ipAddressId, FirewallRule.Purpose purpose);
31+
32+
List<FirewallRuleVO> listByIpAndPurposeAndNotRevoked(long ipAddressId, FirewallRule.Purpose purpose);
33+
34+
List<FirewallRuleVO> listByNetworkAndPurposeAndNotRevoked(long networkId, FirewallRule.Purpose purpose);
3235

3336
boolean setStateToAdd(FirewallRuleVO rule);
3437

@@ -38,7 +41,7 @@ public interface FirewallRulesDao extends GenericDao<FirewallRuleVO, Long> {
3841

3942
List<? extends FirewallRule> listByIpAndPurpose(long ipAddressId, FirewallRule.Purpose purpose);
4043

41-
List<FirewallRuleVO> listByNetworkIdAndPurpose(long networkId, FirewallRule.Purpose purpose);
44+
List<FirewallRuleVO> listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose);
4245

4346
List<FirewallRuleVO> listStaticNatByVmId(long vmId);
4447

‎server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
4242
private static final Logger s_logger = Logger.getLogger(FirewallRulesDaoImpl.class);
4343

4444
protected final SearchBuilder<FirewallRuleVO> AllFieldsSearch;
45-
protected final SearchBuilder<FirewallRuleVO> IpNotRevokedSearch;
45+
protected final SearchBuilder<FirewallRuleVO> NotRevokedSearch;
4646
protected final SearchBuilder<FirewallRuleVO> ReleaseSearch;
4747
protected SearchBuilder<FirewallRuleVO> VmSearch;
4848

@@ -60,11 +60,12 @@ protected FirewallRulesDaoImpl() {
6060
AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
6161
AllFieldsSearch.done();
6262

63-
IpNotRevokedSearch = createSearchBuilder();
64-
IpNotRevokedSearch.and("ipId", IpNotRevokedSearch.entity().getSourceIpAddressId(), Op.EQ);
65-
IpNotRevokedSearch.and("state", IpNotRevokedSearch.entity().getState(), Op.NEQ);
66-
IpNotRevokedSearch.and("purpose", IpNotRevokedSearch.entity().getPurpose(), Op.EQ);
67-
IpNotRevokedSearch.done();
63+
NotRevokedSearch = createSearchBuilder();
64+
NotRevokedSearch.and("ipId", NotRevokedSearch.entity().getSourceIpAddressId(), Op.EQ);
65+
NotRevokedSearch.and("state", NotRevokedSearch.entity().getState(), Op.NEQ);
66+
NotRevokedSearch.and("purpose", NotRevokedSearch.entity().getPurpose(), Op.EQ);
67+
NotRevokedSearch.and("networkId", NotRevokedSearch.entity().getNetworkId(), Op.EQ);
68+
NotRevokedSearch.done();
6869

6970
ReleaseSearch = createSearchBuilder();
7071
ReleaseSearch.and("protocol", ReleaseSearch.entity().getProtocol(), Op.EQ);
@@ -96,8 +97,8 @@ public List<FirewallRuleVO> listByIpAndPurpose(long ipId, FirewallRule.Purpose p
9697
}
9798

9899
@Override
99-
public List<FirewallRuleVO> listByIpAndNotRevoked(long ipId, FirewallRule.Purpose purpose) {
100-
SearchCriteria<FirewallRuleVO> sc = IpNotRevokedSearch.create();
100+
public List<FirewallRuleVO> listByIpAndPurposeAndNotRevoked(long ipId, FirewallRule.Purpose purpose) {
101+
SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
101102
sc.setParameters("ipId", ipId);
102103
sc.setParameters("state", State.Revoke);
103104

@@ -108,9 +109,21 @@ public List<FirewallRuleVO> listByIpAndNotRevoked(long ipId, FirewallRule.Purpos
108109
return listBy(sc);
109110
}
110111

112+
@Override
113+
public List<FirewallRuleVO> listByNetworkAndPurposeAndNotRevoked(long networkId, FirewallRule.Purpose purpose) {
114+
SearchCriteria<FirewallRuleVO> sc = NotRevokedSearch.create();
115+
sc.setParameters("networkId", networkId);
116+
sc.setParameters("state", State.Revoke);
117+
118+
if (purpose != null) {
119+
sc.setParameters("purpose", purpose);
120+
}
121+
122+
return listBy(sc);
123+
}
111124

112125
@Override
113-
public List<FirewallRuleVO> listByNetworkIdAndPurpose(long networkId, FirewallRule.Purpose purpose) {
126+
public List<FirewallRuleVO> listByNetworkAndPurpose(long networkId, FirewallRule.Purpose purpose) {
114127
SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
115128
sc.setParameters("purpose", purpose);
116129
sc.setParameters("networkId", networkId);

‎server/src/com/cloud/network/dao/IPAddressDao.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
4646

4747
int countIPsForDashboard(long dcId, boolean onlyCountAllocated);
4848

49-
List<IPAddressVO> listByAssociatedVmId(long vmId);
49+
IPAddressVO findByAssociatedVmId(long vmId);
5050

5151
IPAddressVO findByAccountAndIp(long accountId, String ipAddress);
5252
}

0 commit comments

Comments
 (0)