Skip to content

Commit dff712f

Browse files
Abhinandan Prateekroot
authored andcommitted
bug 10731: sending source cidrs to the required router element, on domR allowing these source cidrs thru iptables
status 10731: resolved fixed
1 parent c03d360 commit dff712f

13 files changed

Lines changed: 50 additions & 34 deletions

File tree

‎api/src/com/cloud/agent/api/to/LoadBalancerTO.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@
2020
import java.util.List;
2121

2222
import com.cloud.network.lb.LoadBalancingRule.LbDestination;
23+
import com.cloud.utils.StringUtils;
2324

2425

2526
public class LoadBalancerTO {
2627
String srcIp;
2728
int srcPort;
2829
String protocol;
30+
List<String> sourceCidrs;
2931
String algorithm;
3032
boolean revoked;
3133
boolean alreadyAdded;
3234
DestinationTO[] destinations;
3335

3436

35-
public LoadBalancerTO (String srcIp, int srcPort, String protocol, String algorithm, boolean revoked, boolean alreadyAdded, List<LbDestination> destinations) {
37+
public LoadBalancerTO (String srcIp, int srcPort, String protocol, List<String> sourceCidrs, String algorithm, boolean revoked, boolean alreadyAdded, List<LbDestination> destinations) {
3638
this.srcIp = srcIp;
3739
this.srcPort = srcPort;
3840
this.protocol = protocol;
3941
this.algorithm = algorithm;
4042
this.revoked = revoked;
4143
this.alreadyAdded = alreadyAdded;
4244
this.destinations = new DestinationTO[destinations.size()];
45+
this.sourceCidrs = sourceCidrs;
4346
int i = 0;
4447
for (LbDestination destination : destinations) {
4548
this.destinations[i++] = new DestinationTO(destination.getIpAddress(), destination.getDestinationPortStart(), destination.isRevoked(), false);
@@ -56,6 +59,14 @@ public String getSrcIp() {
5659
public int getSrcPort() {
5760
return srcPort;
5861
}
62+
63+
public List<String> getSourceCidrs(){
64+
return sourceCidrs;
65+
}
66+
67+
public String getStringSourceCidrs(){
68+
return StringUtils.join(sourceCidrs, "-");
69+
}
5970

6071
public String getAlgorithm() {
6172
return algorithm;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public String getCommandName() {
108108
return s_name;
109109
}
110110

111-
@Override
112111
public void setSourceCidrList(List<String> cidrs){
113112
cidrlist = cidrs;
114113
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,4 @@ public interface PortForwardingRule extends FirewallRule {
4949
* @return source cidr to forward
5050
*/
5151
List<String> getSourceCidrList();
52-
/**
53-
* @return source cidr to forward
54-
*/
55-
void setSourceCidrList(List<String> cidrs);
5652
}

‎core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
155155
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
156156
import com.cloud.agent.api.to.IpAddressTO;
157+
import com.cloud.agent.api.to.LoadBalancerTO;
157158
import com.cloud.agent.api.to.NicTO;
158159
import com.cloud.agent.api.to.PortForwardingRuleTO;
159160
import com.cloud.agent.api.to.StaticNatRuleTO;
@@ -1283,7 +1284,7 @@ protected Answer execute(final LoadBalancerConfigCommand cmd) {
12831284
if (routerIp == null) {
12841285
return new Answer(cmd);
12851286
}
1286-
1287+
12871288
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
12881289
String[] config = cfgtr.generateConfiguration(cmd);
12891290
String[][] rules = cfgtr.generateFwRules(cmd);

‎core/src/com/cloud/network/HAProxyConfigurator.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public String[][] generateFwRules(LoadBalancerConfigCommand lbCmd) {
227227
StringBuilder sb = new StringBuilder();
228228
sb.append(lbTO.getSrcIp()).append(":");
229229
sb.append(lbTO.getSrcPort()).append(":");
230+
if (lbTO.getSourceCidrs() != null && lbTO.getSourceCidrs().size() > 0){
231+
sb.append(lbTO.getStringSourceCidrs()).append(":");
232+
}
233+
else {
234+
sb.append("0/0:");
235+
}
230236
String lbRuleEntry = sb.toString();
231237
if (!lbTO.isRevoked()) {
232238
toAdd.add(lbRuleEntry);
@@ -236,7 +242,7 @@ public String[][] generateFwRules(LoadBalancerConfigCommand lbCmd) {
236242
}
237243
toRemove.removeAll(toAdd);
238244
result[ADD] = toAdd.toArray(new String[toAdd.size()]);
239-
result[REMOVE] = toRemove.toArray(new String[toRemove.size()]);
245+
result[REMOVE] = toRemove.toArray(new String[toRemove.size()]);
240246

241247
return result;
242248
}

‎patches/systemvm/debian/config/root/loadbalancer.sh‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ fw_entry() {
6464
for i in $a
6565
do
6666
local pubIp=$(echo $i | cut -d: -f1)
67-
local dport=$(echo $i | cut -d: -f2)
67+
local dport=$(echo $i | cut -d: -f2)
68+
local cidrs=$(echo $i | cut -d: -f3 | sed 's/-/,/')
6869

6970
for vif in $VIF_LIST; do
70-
iptables -D INPUT -i $vif -p tcp -d $pubIp --dport $dport -j ACCEPT 2> /dev/null
71-
iptables -A INPUT -i $vif -p tcp -d $pubIp --dport $dport -j ACCEPT
71+
iptables -D INPUT -i $vif -s $cidrs -p tcp -d $pubIp --dport $dport -j ACCEPT 2> /dev/null
72+
iptables -A INPUT -i $vif -s $cidrs -p tcp -d $pubIp --dport $dport -j ACCEPT
7273

7374
if [ $? -gt 0 ]
7475
then
@@ -80,10 +81,11 @@ fw_entry() {
8081
for i in $r
8182
do
8283
local pubIp=$(echo $i | cut -d: -f1)
83-
local dport=$(echo $i | cut -d: -f2)
84+
local dport=$(echo $i | cut -d: -f2)
85+
local cidrs=$(echo $i | cut -d: -f3 | sed 's/-/,/')
8486

8587
for vif in $VIF_LIST; do
86-
iptables -D INPUT -i $vif -p tcp -d $pubIp --dport $dport -j ACCEPT
88+
iptables -D INPUT -i $vif -s $cidrs -p tcp -d $pubIp --dport $dport -j ACCEPT
8789
done
8890
done
8991

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public Long getId() {
5353
public long getFirewallRuleId() {
5454
return firewallRuleId;
5555
}
56+
57+
public void setFirewallRuleId(long firewallRuleId){
58+
this.firewallRuleId = firewallRuleId;
59+
}
5660

5761
public String getCidr() {
5862
return sourceCidrList;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.cloud.network;
2020

21+
import java.util.ArrayList;
2122
import java.util.List;
2223

2324
import javax.persistence.Column;
@@ -81,7 +82,6 @@ public String getDescription() {
8182
public void setSourceCidrList(List<String> sourceCidrs) {
8283
this.sourceCidrs=sourceCidrs;
8384
}
84-
8585
@Override
8686
public List<String> getSourceCidrList() {
8787
return sourceCidrs;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,9 @@ public LoadBalancerVO persist(LoadBalancerVO loadBalancerRule) {
142142
txn.start();
143143

144144
LoadBalancerVO dbfirewallRule = super.persist(loadBalancerRule);
145-
146145
saveSourceCidrs(loadBalancerRule);
147-
loadSourceCidrs(dbfirewallRule);
148146

149147
txn.commit();
150-
151148
return dbfirewallRule;
152149
}
153150

@@ -161,10 +158,9 @@ public boolean update(Long loadBalancerRuleId, LoadBalancerVO loadBalancerRule)
161158
if (!persisted) {
162159
return persisted;
163160
}
164-
165161
saveSourceCidrs(loadBalancerRule);
162+
166163
txn.commit();
167-
168164
return persisted;
169165
}
170166

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.cloud.network.Network;
5353
import com.cloud.network.Network.Service;
5454
import com.cloud.network.NetworkManager;
55+
import com.cloud.network.dao.FirewallRulesCidrsDao;
5556
import com.cloud.network.dao.FirewallRulesDao;
5657
import com.cloud.network.dao.IPAddressDao;
5758
import com.cloud.network.dao.LoadBalancerDao;
@@ -61,6 +62,7 @@
6162
import com.cloud.network.rules.FirewallRule.Purpose;
6263
import com.cloud.network.rules.FirewallRuleVO;
6364
import com.cloud.network.rules.LoadBalancer;
65+
import com.cloud.network.rules.PortForwardingRuleVO;
6466
import com.cloud.network.rules.RulesManager;
6567
import com.cloud.user.Account;
6668
import com.cloud.user.AccountManager;
@@ -118,6 +120,8 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager,
118120
NicDao _nicDao;
119121
@Inject
120122
UsageEventDao _usageEventDao;
123+
@Inject
124+
FirewallRulesCidrsDao _firewallCidrsDao;
121125

122126
@Override
123127
@DB
@@ -222,7 +226,7 @@ private boolean removeFromLoadBalancerInternal(long loadBalancerId, List<Long> i
222226
_lb2VmMapDao.persist(map);
223227
s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + instanceId);
224228
}
225-
229+
226230
if (!applyLoadBalancerConfig(loadBalancerId)) {
227231
s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for vms " + instanceIds);
228232
throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for vms " + instanceIds);
@@ -431,7 +435,6 @@ public boolean applyLoadBalancerConfig(long lbRuleId) throws ResourceUnavailable
431435
@Override
432436
public boolean applyLoadBalancersForNetwork(long networkId) throws ResourceUnavailableException {
433437
List<LoadBalancerVO> lbs = _lbDao.listByNetworkId(networkId);
434-
435438
if (lbs != null) {
436439
return applyLoadBalancerRules(lbs);
437440
} else {
@@ -462,10 +465,10 @@ protected boolean applyLoadBalancerRules(List<LoadBalancerVO> lbs) throws Resour
462465
txn.start();
463466
if (lb.getState() == FirewallRule.State.Revoke) {
464467
_lbDao.remove(lb.getId());
465-
s_logger.debug("LB " + lb.getId() + " is successfully removed");
468+
s_logger.warn("LB " + lb.getId() + " is successfully removed");
466469
} else if (lb.getState() == FirewallRule.State.Add) {
467470
lb.setState(FirewallRule.State.Active);
468-
s_logger.debug("LB rule " + lb.getId() + " state is set to Active");
471+
s_logger.warn("LB rule " + lb.getId() + " state is set to Active");
469472
_lbDao.persist(lb);
470473
}
471474

@@ -484,7 +487,7 @@ protected boolean applyLoadBalancerRules(List<LoadBalancerVO> lbs) throws Resour
484487

485488
if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) {
486489
lb.setState(FirewallRule.State.Add);
487-
_lbDao.persist(lb);
490+
_lbDao.persist(lb);
488491
s_logger.debug("LB rule " + lb.getId() + " state is set to Add as there are no more active LB-VM mappings");
489492
}
490493

0 commit comments

Comments
 (0)