Skip to content

Commit 2dc1a28

Browse files
author
Alena Prokharchyk
committed
VPC: set networkId to be null for IP address when the last PF/LB rule is removed for it and the IP belongs to VPC
Conflicts: server/src/com/cloud/network/rules/RulesManagerImpl.java
1 parent 8d16d7f commit 2dc1a28

6 files changed

Lines changed: 148 additions & 74 deletions

File tree

‎server/src/com/cloud/network/firewall/FirewallManagerImpl.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ public boolean applyRules(List<? extends FirewallRule> rules, boolean continueOn
425425
}
426426

427427
@DB
428-
protected void removeRule(FirewallRule rule) {
428+
@Override
429+
public void removeRule(FirewallRule rule) {
429430

430431
Transaction txn = Transaction.currentTxn();
431432
txn.start();
@@ -527,7 +528,7 @@ public void revokeRule(FirewallRuleVO rule, Account caller, long userId, boolean
527528
if (s_logger.isDebugEnabled()) {
528529
s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule);
529530
}
530-
_firewallDao.remove(rule.getId());
531+
removeRule(rule);
531532
generateUsageEvent = true;
532533
} else if (rule.getState() == State.Add || rule.getState() == State.Active) {
533534
rule.setState(State.Revoke);

‎server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply, Accoun
675675
s_logger.warn("Unable to remove firewall rule id=" + lb.getId() + " as it has related firewall rule id=" + relatedRule.getId() + "; leaving it in Revoke state");
676676
success = false;
677677
} else {
678-
_firewallDao.remove(lb.getId());
678+
_firewallMgr.removeRule(lb);
679679
}
680680

681681
_elbMgr.handleDeleteLoadBalancerRule(lb, callerUserId, caller);
@@ -720,6 +720,7 @@ public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean
720720
}
721721

722722
LoadBalancer result = _elbMgr.handleCreateLoadBalancerRule(lb, lbOwner, lb.getNetworkId());
723+
boolean performedIpAssoc = false;
723724
if (result == null) {
724725
IpAddress ip = null;
725726
Network guestNetwork = _networkMgr.getNetwork(lb.getNetworkId());
@@ -732,8 +733,13 @@ public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean
732733
try {
733734
if (ipAddressVO != null) {
734735
if (ipAddressVO.getAssociatedWithNetworkId() == null) {
736+
//set networkId just for verification purposes
737+
ipAddressVO.setAssociatedWithNetworkId(lb.getNetworkId());
738+
_networkMgr.checkIpForService(ipAddressVO, Service.Lb);
739+
735740
s_logger.debug("The ip is not associated with the network id="+ lb.getNetworkId() + " so assigning");
736741
ipAddressVO = _networkMgr.associateIPToGuestNetwork(ipAddrId, lb.getNetworkId());
742+
boolean perfomedIpAssoc = true;
737743
}
738744
_networkMgr.checkIpForService(ipAddressVO, Service.Lb);
739745
}
@@ -752,6 +758,15 @@ public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean
752758
s_logger.debug("Releasing system IP address " + ip + " as corresponding lb rule failed to create");
753759
_networkMgr.handleSystemIpRelease(ip);
754760
}
761+
// release ip address if ipassoc was perfored
762+
if (performedIpAssoc) {
763+
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
764+
ip = _ipAddressDao.findById(ip.getId());
765+
if (ip != null && ip.getVpcId() != null && _firewallDao.listByIp(ip.getId()).isEmpty()) {
766+
s_logger.debug("Releasing VPC ip address " + ip + " as LB rule failed to create");
767+
_networkMgr.unassignIPFromVpcNetwork(ip.getId());
768+
}
769+
}
755770
}
756771
}
757772

@@ -771,7 +786,6 @@ public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb, boolean ope
771786
long sourceIpId = lb.getSourceIpAddressId();
772787

773788
IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId);
774-
Long networkId = ipAddr.getSourceNetworkId();
775789
// make sure ip address exists
776790
if (ipAddr == null || !ipAddr.readyToUse()) {
777791
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id specified");
@@ -786,7 +800,7 @@ public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb, boolean ope
786800
_firewallMgr.validateFirewallRule(caller.getCaller(), ipAddr, srcPortStart, srcPortEnd, lb.getProtocol(),
787801
Purpose.LoadBalancing, FirewallRuleType.User);
788802

789-
networkId = ipAddr.getAssociatedWithNetworkId();
803+
Long networkId = ipAddr.getAssociatedWithNetworkId();
790804
if (networkId == null) {
791805
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule ; specified sourceip id is not associated with any network");
792806
ex.addProxyObject(ipAddr, sourceIpId, "sourceIpId");
@@ -842,7 +856,7 @@ public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb, boolean ope
842856

843857
txn.start();
844858
_firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
845-
_lbDao.remove(newRule.getId());
859+
removeLBRule(newRule);
846860

847861
txn.commit();
848862
}
@@ -897,7 +911,7 @@ protected boolean applyLoadBalancerRules(List<LoadBalancerVO> lbs, boolean updat
897911
boolean checkForReleaseElasticIp = false;
898912
txn.start();
899913
if (lb.getState() == FirewallRule.State.Revoke) {
900-
_lbDao.remove(lb.getId());
914+
removeLBRule(lb);
901915
s_logger.debug("LB " + lb.getId() + " is successfully removed");
902916
checkForReleaseElasticIp = true;
903917
} else if (lb.getState() == FirewallRule.State.Add) {
@@ -1290,4 +1304,17 @@ public LoadBalancerVO findById(long lbId) {
12901304
return _lbDao.findById(lbId);
12911305
}
12921306

1307+
@DB
1308+
protected void removeLBRule(LoadBalancerVO rule) {
1309+
Transaction txn = Transaction.currentTxn();
1310+
txn.start();
1311+
_lbDao.remove(rule.getId());
1312+
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
1313+
IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
1314+
if (ip != null && ip.getVpcId() != null && _firewallDao.listByIp(ip.getId()).isEmpty()) {
1315+
_networkMgr.unassignIPFromVpcNetwork(ip.getId());
1316+
}
1317+
1318+
txn.commit();
1319+
}
12931320
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ protected boolean addPublicIpToVpc(VirtualRouter router, Network publicNetwork,
310310
NicProfile publicNic = _itMgr.addVmToNetwork(router, publicNetwork, defaultNic);
311311
//setup public network
312312
if (publicNic != null) {
313+
if (ipAddress.isSourceNat()) {
314+
if (router.getPublicIpAddress() == null) {
315+
DomainRouterVO routerVO = _routerDao.findById(router.getId());
316+
routerVO.setPublicIpAddress(ipAddress.getAddress().toString());
317+
routerVO.setPublicNetmask(ipAddress.getNetmask());
318+
routerVO.setPublicMacAddress(ipAddress.getMacAddress());
319+
_routerDao.update(routerVO.getId(), routerVO);
320+
}
321+
}
313322
publicNic.setDefaultNic(true);
314323
if (ipAddress != null) {
315324
IPAddressVO ipVO = _ipAddressDao.findById(ipAddress.getId());

‎server/src/com/cloud/network/rules/FirewallManager.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ FirewallRule createFirewallRule(long ipAddrId, Account caller, String xId, Integ
7676

7777
boolean addSystemFirewallRules(IPAddressVO ip, Account acct);
7878

79+
/**
80+
* @param rule
81+
*/
82+
void removeRule(FirewallRule rule);
83+
7984
}

‎server/src/com/cloud/network/rules/RulesManagerImpl.java‎

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -173,92 +173,113 @@ public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long
173173

174174
Long networkId = rule.getNetworkId();
175175
//associate ip address to network (if needed)
176+
boolean performedIpAssoc = false;
176177
if (ipAddress.getAssociatedWithNetworkId() == null) {
178+
//set networkId just for verification purposes
179+
ipAddress.setAssociatedWithNetworkId(networkId);
180+
_networkMgr.checkIpForService(ipAddress, Service.PortForwarding);
181+
177182
s_logger.debug("The ip is not associated with the network id="+ networkId + " so assigning");
178183
try {
179184
ipAddress = _networkMgr.associateIPToGuestNetwork(ipAddrId, networkId);
185+
performedIpAssoc = true;
180186
} catch (Exception ex) {
181187
throw new CloudRuntimeException("Failed to associate ip to network as " +
182188
"a part of port forwarding rule creation");
183189
}
190+
} else {
191+
_networkMgr.checkIpForService(ipAddress, Service.PortForwarding);
184192
}
185193

186-
_networkMgr.checkIpForService(ipAddress, Service.PortForwarding);
187-
188-
_firewallMgr.validateFirewallRule(caller, ipAddress, rule.getSourcePortStart(), rule.getSourcePortEnd(),
189-
rule.getProtocol(), Purpose.PortForwarding, FirewallRuleType.User);
190-
191-
Long accountId = ipAddress.getAllocatedToAccountId();
192-
Long domainId = ipAddress.getAllocatedInDomainId();
194+
try {
195+
_firewallMgr.validateFirewallRule(caller, ipAddress, rule.getSourcePortStart(), rule.getSourcePortEnd(),
196+
rule.getProtocol(), Purpose.PortForwarding, FirewallRuleType.User);
193197

194-
// start port can't be bigger than end port
195-
if (rule.getDestinationPortStart() > rule.getDestinationPortEnd()) {
196-
throw new InvalidParameterValueException("Start port can't be bigger than end port");
197-
}
198+
Long accountId = ipAddress.getAllocatedToAccountId();
199+
Long domainId = ipAddress.getAllocatedInDomainId();
198200

199-
// check that the port ranges are of equal size
200-
if ((rule.getDestinationPortEnd() - rule.getDestinationPortStart()) != (rule.getSourcePortEnd() - rule.getSourcePortStart())) {
201-
throw new InvalidParameterValueException("Source port and destination port ranges should be of equal sizes.");
202-
}
201+
// start port can't be bigger than end port
202+
if (rule.getDestinationPortStart() > rule.getDestinationPortEnd()) {
203+
throw new InvalidParameterValueException("Start port can't be bigger than end port");
204+
}
203205

204-
// validate user VM exists
205-
UserVm vm = _vmDao.findById(vmId);
206-
if (vm == null) {
207-
throw new InvalidParameterValueException("Unable to create port forwarding rule on address " + ipAddress +
208-
", invalid virtual machine id specified (" + vmId + ").");
209-
} else {
210-
checkRuleAndUserVm(rule, vm, caller);
211-
}
206+
// check that the port ranges are of equal size
207+
if ((rule.getDestinationPortEnd() - rule.getDestinationPortStart()) != (rule.getSourcePortEnd() - rule.getSourcePortStart())) {
208+
throw new InvalidParameterValueException("Source port and destination port ranges should be of equal sizes.");
209+
}
212210

211+
// validate user VM exists
212+
UserVm vm = _vmDao.findById(vmId);
213+
if (vm == null) {
214+
throw new InvalidParameterValueException("Unable to create port forwarding rule on address " + ipAddress +
215+
", invalid virtual machine id specified (" + vmId + ").");
216+
} else {
217+
checkRuleAndUserVm(rule, vm, caller);
218+
}
213219

214-
// Verify that vm has nic in the network
215-
Ip dstIp = rule.getDestinationIpAddress();
216-
Nic guestNic = _networkMgr.getNicInNetwork(vmId, networkId);
217-
if (guestNic == null || guestNic.getIp4Address() == null) {
218-
throw new InvalidParameterValueException("Vm doesn't belong to network associated with ipAddress");
219-
} else {
220-
dstIp = new Ip(guestNic.getIp4Address());
221-
}
222220

223-
Transaction txn = Transaction.currentTxn();
224-
txn.start();
221+
// Verify that vm has nic in the network
222+
Ip dstIp = rule.getDestinationIpAddress();
223+
Nic guestNic = _networkMgr.getNicInNetwork(vmId, networkId);
224+
if (guestNic == null || guestNic.getIp4Address() == null) {
225+
throw new InvalidParameterValueException("Vm doesn't belong to network associated with ipAddress");
226+
} else {
227+
dstIp = new Ip(guestNic.getIp4Address());
228+
}
225229

226-
PortForwardingRuleVO newRule = new PortForwardingRuleVO(rule.getXid(), rule.getSourceIpAddressId(),
227-
rule.getSourcePortStart(), rule.getSourcePortEnd(), dstIp, rule.getDestinationPortStart(),
228-
rule.getDestinationPortEnd(), rule.getProtocol().toLowerCase(), networkId, accountId, domainId, vmId);
229-
newRule = _portForwardingDao.persist(newRule);
230+
Transaction txn = Transaction.currentTxn();
231+
txn.start();
230232

231-
// create firewallRule for 0.0.0.0/0 cidr
232-
if (openFirewall) {
233-
_firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(),
234-
rule.getProtocol(), null, null, newRule.getId(), networkId);
235-
}
233+
PortForwardingRuleVO newRule = new PortForwardingRuleVO(rule.getXid(), rule.getSourceIpAddressId(),
234+
rule.getSourcePortStart(), rule.getSourcePortEnd(), dstIp, rule.getDestinationPortStart(),
235+
rule.getDestinationPortEnd(), rule.getProtocol().toLowerCase(), networkId, accountId, domainId, vmId);
236+
newRule = _portForwardingDao.persist(newRule);
236237

237-
try {
238-
_firewallMgr.detectRulesConflict(newRule);
239-
if (!_firewallDao.setStateToAdd(newRule)) {
240-
throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
238+
// create firewallRule for 0.0.0.0/0 cidr
239+
if (openFirewall) {
240+
_firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(),
241+
rule.getProtocol(), null, null, newRule.getId(), networkId);
241242
}
242-
UserContext.current().setEventDetails("Rule Id: " + newRule.getId());
243-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(),
244-
ipAddress.getDataCenterId(), newRule.getId(), null);
245-
_usageEventDao.persist(usageEvent);
246-
txn.commit();
247-
return newRule;
248-
} catch (Exception e) {
249-
if (newRule != null) {
250-
txn.start();
251-
// no need to apply the rule as it wasn't programmed on the backend yet
252-
_firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
253-
_portForwardingDao.remove(newRule.getId());
243+
244+
try {
245+
_firewallMgr.detectRulesConflict(newRule);
246+
if (!_firewallDao.setStateToAdd(newRule)) {
247+
throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
248+
}
249+
UserContext.current().setEventDetails("Rule Id: " + newRule.getId());
250+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(),
251+
ipAddress.getDataCenterId(), newRule.getId(), null);
252+
_usageEventDao.persist(usageEvent);
254253
txn.commit();
255-
}
254+
return newRule;
255+
} catch (Exception e) {
256+
if (newRule != null) {
257+
txn.start();
258+
// no need to apply the rule as it wasn't programmed on the backend yet
259+
_firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
260+
removePFRule(newRule);
261+
txn.commit();
262+
}
256263

257-
if (e instanceof NetworkRuleConflictException) {
258-
throw (NetworkRuleConflictException) e;
264+
if (e instanceof NetworkRuleConflictException) {
265+
throw (NetworkRuleConflictException) e;
266+
}
267+
268+
throw new CloudRuntimeException("Unable to add rule for the ip id=" + ipAddrId, e);
269+
}
270+
} finally {
271+
// release ip address if ipassoc was perfored
272+
if (performedIpAssoc) {
273+
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
274+
IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
275+
if (ip != null && ip.getVpcId() != null && _firewallDao.listByIp(ip.getId()).isEmpty()) {
276+
s_logger.debug("Releasing VPC ip address " + ip + " as PF rule failed to create");
277+
_networkMgr.unassignIPFromVpcNetwork(ip.getId());
278+
}
259279
}
260-
throw new CloudRuntimeException("Unable to add rule for the ip id=" + ipAddrId, e);
261280
}
281+
282+
262283
}
263284

264285
@Override
@@ -326,7 +347,7 @@ public StaticNatRule createStaticNatRule(StaticNatRule rule, boolean openFirewal
326347
txn.start();
327348
// no need to apply the rule as it wasn't programmed on the backend yet
328349
_firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
329-
_portForwardingDao.remove(newRule.getId());
350+
_firewallMgr.removeRule(newRule);
330351
txn.commit();
331352
}
332353

@@ -1017,7 +1038,7 @@ public FirewallRuleVO[] reservePorts(IpAddress ip, String protocol, FirewallRule
10171038
txn.start();
10181039

10191040
for (FirewallRuleVO newRule : rules) {
1020-
_portForwardingDao.remove(newRule.getId());
1041+
_firewallMgr.removeRule(newRule);
10211042
}
10221043
txn.commit();
10231044
}
@@ -1271,4 +1292,18 @@ public void getSystemIpAndEnableStaticNatForVm(VirtualMachine vm, boolean getNew
12711292
}
12721293
}
12731294
}
1295+
1296+
@DB
1297+
protected void removePFRule(PortForwardingRuleVO rule) {
1298+
Transaction txn = Transaction.currentTxn();
1299+
txn.start();
1300+
_portForwardingDao.remove(rule.getId());
1301+
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
1302+
IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
1303+
if (ip != null && ip.getVpcId() != null && _firewallDao.listByIp(ip.getId()).isEmpty()) {
1304+
_networkMgr.unassignIPFromVpcNetwork(ip.getId());
1305+
}
1306+
1307+
txn.commit();
1308+
}
12741309
}

‎server/src/com/cloud/network/rules/dao/PortForwardingRulesDaoImpl.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import javax.ejb.Local;
1818

19-
import org.apache.log4j.Logger;
20-
2119
import com.cloud.network.dao.FirewallRulesCidrsDaoImpl;
2220
import com.cloud.network.rules.FirewallRule.Purpose;
2321
import com.cloud.network.rules.FirewallRule.State;
@@ -30,7 +28,6 @@
3028

3129
@Local(value=PortForwardingRulesDao.class)
3230
public class PortForwardingRulesDaoImpl extends GenericDaoBase<PortForwardingRuleVO, Long> implements PortForwardingRulesDao {
33-
private static final Logger s_logger = Logger.getLogger(PortForwardingRulesDaoImpl.class);
3431

3532
protected final SearchBuilder<PortForwardingRuleVO> AllFieldsSearch;
3633
protected final SearchBuilder<PortForwardingRuleVO> ApplicationSearch;

0 commit comments

Comments
 (0)