Skip to content

Commit e9ce381

Browse files
committed
Merge branch '4.14'
2 parents 8e03374 + a979ab9 commit e9ce381

15 files changed

Lines changed: 584 additions & 146 deletions

File tree

api/src/main/java/com/cloud/agent/api/to/IpAddressTO.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class IpAddressTO {
3535
private String networkName;
3636
private Integer nicDevId;
3737
private boolean newNic;
38+
private boolean isPrivateGateway;
3839

3940
public IpAddressTO(long accountId, String ipAddress, boolean add, boolean firstIP, boolean sourceNat, String broadcastUri, String vlanGateway, String vlanNetmask,
4041
String vifMacAddress, Integer networkRate, boolean isOneToOneNat) {
@@ -133,4 +134,12 @@ public boolean isNewNic() {
133134
public void setNewNic(boolean newNic) {
134135
this.newNic = newNic;
135136
}
137+
138+
public boolean isPrivateGateway() {
139+
return isPrivateGateway;
140+
}
141+
142+
public void setPrivateGateway(boolean isPrivateGateway) {
143+
this.isPrivateGateway = isPrivateGateway;
144+
}
136145
}

core/src/main/java/com/cloud/agent/resource/virtualnetwork/facade/IpAssociationConfigItem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
4242
for (final IpAddressTO ip : command.getIpAddresses()) {
4343
final IpAddress ipAddress = new IpAddress(ip.getPublicIp(), ip.isSourceNat(), ip.isAdd(), ip.isOneToOneNat(), ip.isFirstIP(), ip.getVlanGateway(), ip.getVlanNetmask(),
4444
ip.getVifMacAddress(), ip.getNicDevId(), ip.isNewNic(), ip.getTrafficType().toString());
45+
ipAddress.setPrivateGateway(ip.isPrivateGateway());
4546
ips.add(ipAddress);
4647
}
4748

core/src/main/java/com/cloud/agent/resource/virtualnetwork/model/IpAddress.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class IpAddress {
3232
private Integer nicDevId;
3333
private boolean newNic;
3434
private String nwType;
35+
private boolean isPrivateGateway;
3536

3637
public IpAddress() {
3738
// Empty constructor for (de)serialization
@@ -133,4 +134,12 @@ public void setNewNic(boolean newNic) {
133134
this.newNic = newNic;
134135
}
135136

137+
public boolean isPrivateGateway() {
138+
return isPrivateGateway;
139+
}
140+
141+
public void setPrivateGateway(boolean isPrivateGateway) {
142+
this.isPrivateGateway = isPrivateGateway;
143+
}
144+
136145
}

server/src/main/java/com/cloud/network/router/CommandSetupHelper.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@
106106
import com.cloud.network.vpc.StaticRouteProfile;
107107
import com.cloud.network.vpc.Vpc;
108108
import com.cloud.network.vpc.VpcGateway;
109-
import com.cloud.network.vpc.VpcGatewayVO;
110109
import com.cloud.network.vpc.dao.VpcDao;
111-
import com.cloud.network.vpc.dao.VpcGatewayDao;
112110
import com.cloud.offering.NetworkOffering;
113111
import com.cloud.offerings.NetworkOfferingVO;
114112
import com.cloud.offerings.dao.NetworkOfferingDao;
@@ -174,8 +172,6 @@ public class CommandSetupHelper {
174172
@Inject
175173
private VpcDao _vpcDao;
176174
@Inject
177-
private VpcGatewayDao _vpcGatewayDao;
178-
@Inject
179175
private VlanDao _vlanDao;
180176
@Inject
181177
private IPAddressDao _ipAddressDao;
@@ -734,8 +730,7 @@ public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
734730
final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, BroadcastDomainType.fromString(ipAddr.getVlanTag()).toString(), ipAddr.getGateway(),
735731
ipAddr.getNetmask(), macAddress, networkRate, ipAddr.isOneToOneNat());
736732

737-
ip.setTrafficType(getNetworkTrafficType(network));
738-
ip.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
733+
setIpAddressNetworkParams(ip, network, router);
739734
ipsToSend[i++] = ip;
740735
if (ipAddr.isSourceNat()) {
741736
sourceNatIpAdd = new Pair<IpAddressTO, Long>(ip, ipAddr.getNetworkId());
@@ -859,8 +854,7 @@ public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
859854
final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask,
860855
vifMacAddress, networkRate, ipAddr.isOneToOneNat());
861856

862-
ip.setTrafficType(getNetworkTrafficType(network));
863-
ip.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
857+
setIpAddressNetworkParams(ip, network, router);
864858
ipsToSend[i++] = ip;
865859
/*
866860
* send the firstIP = true for the first Add, this is to create
@@ -987,8 +981,7 @@ public void createVpcAssociatePrivateIPCommands(final VirtualRouter router, fina
987981
final IpAddressTO ip = new IpAddressTO(Account.ACCOUNT_ID_SYSTEM, ipAddr.getIpAddress(), add, false, ipAddr.getSourceNat(), ipAddr.getBroadcastUri(),
988982
ipAddr.getGateway(), ipAddr.getNetmask(), ipAddr.getMacAddress(), null, false);
989983

990-
ip.setTrafficType(getNetworkTrafficType(network));
991-
ip.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
984+
setIpAddressNetworkParams(ip, network, router);
992985
ipsToSend[i++] = ip;
993986

994987
}
@@ -1145,13 +1138,16 @@ protected String getGuestDhcpRange(final NicProfile guestNic, final Network gues
11451138
return dhcpRange;
11461139
}
11471140

1148-
private TrafficType getNetworkTrafficType(Network network) {
1149-
final VpcGatewayVO gateway = _vpcGatewayDao.getVpcGatewayByNetworkId(network.getId());
1150-
if (gateway != null) {
1141+
private void setIpAddressNetworkParams(IpAddressTO ipAddress, final Network network, final VirtualRouter router) {
1142+
if (_networkModel.isPrivateGateway(network.getId())) {
11511143
s_logger.debug("network " + network.getId() + " (name: " + network.getName() + " ) is a vpc private gateway, set traffic type to Public");
1152-
return TrafficType.Public;
1144+
ipAddress.setTrafficType(TrafficType.Public);
1145+
ipAddress.setPrivateGateway(true);
11531146
} else {
1154-
return network.getTrafficType();
1147+
ipAddress.setTrafficType(network.getTrafficType());
1148+
ipAddress.setPrivateGateway(false);
11551149
}
1150+
ipAddress.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
11561151
}
1152+
11571153
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import com.cloud.network.vpc.StaticRouteProfile;
7878
import com.cloud.network.vpc.Vpc;
7979
import com.cloud.network.vpc.VpcGateway;
80-
import com.cloud.network.vpc.VpcGatewayVO;
8180
import com.cloud.network.vpc.VpcManager;
8281
import com.cloud.network.vpc.VpcVO;
8382
import com.cloud.network.vpc.dao.PrivateIpDao;
@@ -277,15 +276,6 @@ public boolean finalizeVirtualMachineProfile(final VirtualMachineProfile profile
277276
if (defaultDns2 != null) {
278277
buf.append(" dns2=").append(defaultDns2);
279278
}
280-
281-
VpcGatewayVO privateGatewayForVpc = _vpcGatewayDao.getPrivateGatewayForVpc(domainRouterVO.getVpcId());
282-
if (privateGatewayForVpc != null) {
283-
String ip4Address = privateGatewayForVpc.getIp4Address();
284-
buf.append(" privategateway=").append(ip4Address);
285-
s_logger.debug("Set privategateway field in cmd_line.json to " + ip4Address);
286-
} else {
287-
buf.append(" privategateway=None");
288-
}
289279
}
290280
}
291281

server/src/main/java/com/cloud/network/rules/NicPlugInOutRules.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.cloud.agent.api.Command;
3030
import com.cloud.agent.api.NetworkUsageCommand;
3131
import com.cloud.agent.manager.Commands;
32+
import com.cloud.dc.DataCenterVO;
33+
import com.cloud.dc.dao.DataCenterDao;
3234
import com.cloud.exception.ConcurrentOperationException;
3335
import com.cloud.exception.InsufficientCapacityException;
3436
import com.cloud.exception.ResourceUnavailableException;
@@ -38,6 +40,9 @@
3840
import com.cloud.network.Networks.BroadcastDomainType;
3941
import com.cloud.network.Networks.IsolationType;
4042
import com.cloud.network.PublicIpAddress;
43+
import com.cloud.network.dao.FirewallRulesDao;
44+
import com.cloud.network.dao.IPAddressDao;
45+
import com.cloud.network.dao.IPAddressVO;
4146
import com.cloud.network.router.VirtualRouter;
4247
import com.cloud.network.vpc.VpcManager;
4348
import com.cloud.network.vpc.VpcVO;
@@ -51,6 +56,9 @@
5156
import com.cloud.vm.VirtualMachineManager;
5257
import com.cloud.vm.dao.NicDao;
5358

59+
import org.apache.cloudstack.network.topology.NetworkTopology;
60+
import org.apache.cloudstack.network.topology.NetworkTopologyContext;
61+
5462
public class NicPlugInOutRules extends RuleApplier {
5563

5664
private static final Logger s_logger = Logger.getLogger(NicPlugInOutRules.class);
@@ -75,6 +83,28 @@ public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter
7583

7684
NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
7785
VirtualMachineManager itMgr = visitor.getVirtualNetworkApplianceFactory().getItMgr();
86+
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
87+
88+
// de-associate IPs before unplugging nics
89+
if (!nicsToUnplug.isEmpty()) {
90+
NetworkTopologyContext networkTopologyContext = visitor.getVirtualNetworkApplianceFactory().getNetworkTopologyContext();
91+
final DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
92+
final DataCenterVO dcVO = dcDao.findById(router.getDataCenterId());
93+
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
94+
95+
final String typeString = "vpc ip association before unplugging nics";
96+
final boolean isPodLevelException = false;
97+
final boolean failWhenDisconnect = false;
98+
final Long podId = null;
99+
final VpcIpAssociationRules ipAssociationRules = new VpcIpAssociationRules(_network, _ipAddresses);
100+
final boolean result = networkTopology.applyRules(_network, router, typeString, isPodLevelException, podId, failWhenDisconnect,
101+
new RuleApplierWrapper<RuleApplier>(ipAssociationRules));
102+
if (!result) {
103+
s_logger.warn("Failed to de-associate IPs before unplugging nics");
104+
return false;
105+
}
106+
}
107+
78108
// 1) Unplug the nics
79109
for (Entry<String, PublicIpAddress> entry : nicsToUnplug.entrySet()) {
80110
Network publicNtwk = null;
@@ -159,6 +189,9 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
159189

160190
VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
161191
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
192+
IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
193+
FirewallRulesDao rulesDao = visitor.getVirtualNetworkApplianceFactory().getFirewallRulesDao();
194+
162195
// find out nics to unplug
163196
for (PublicIpAddress ip : _ipAddresses) {
164197
long publicNtwkId = ip.getNetworkId();
@@ -170,10 +203,26 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
170203
}
171204

172205
if (ip.getState() == IpAddress.State.Releasing) {
173-
Nic nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
206+
NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
174207
if (nic != null) {
175-
nicsToUnplug.put(ip.getVlanTag(), ip);
176-
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
208+
final List<IPAddressVO> allIps = ipAddressDao.listByAssociatedVpc(ip.getVpcId(), null);
209+
boolean ipUpdated = false;
210+
for (IPAddressVO allIp : allIps) {
211+
if (allIp.getId() != ip.getId() && allIp.getVlanId() == ip.getVlanId()
212+
&& (allIp.isSourceNat()
213+
|| rulesDao.countRulesByIpIdAndState(allIp.getId(), FirewallRule.State.Active) > 0
214+
|| (allIp.isOneToOneNat() && allIp.getRuleState() == null))) {
215+
s_logger.debug("Updating the nic " + nic + " with new ip address " + allIp.getAddress().addr());
216+
nic.setIPv4Address(allIp.getAddress().addr());
217+
nicDao.update(nic.getId(), nic);
218+
ipUpdated = true;
219+
break;
220+
}
221+
}
222+
if (!ipUpdated) {
223+
nicsToUnplug.put(ip.getVlanTag(), ip);
224+
s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
225+
}
177226
}
178227
}
179228
}
@@ -215,4 +264,4 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
215264

216265
return nicsToChange;
217266
}
218-
}
267+
}

server/src/main/java/com/cloud/network/rules/VirtualNetworkApplianceFactory.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.dc.dao.VlanDao;
2727
import com.cloud.network.IpAddressManager;
2828
import com.cloud.network.NetworkModel;
29+
import com.cloud.network.dao.FirewallRulesDao;
2930
import com.cloud.network.dao.IPAddressDao;
3031
import com.cloud.network.dao.LoadBalancerDao;
3132
import com.cloud.network.dao.NetworkDao;
@@ -43,6 +44,8 @@
4344
import com.cloud.vm.dao.NicIpAliasDao;
4445
import com.cloud.vm.dao.UserVmDao;
4546

47+
import org.apache.cloudstack.network.topology.NetworkTopologyContext;
48+
4649
public class VirtualNetworkApplianceFactory {
4750

4851
@Inject
@@ -83,6 +86,8 @@ public class VirtualNetworkApplianceFactory {
8386
private IpAddressManager _ipAddrMgr;
8487
@Inject
8588
private NetworkACLManager _networkACLMgr;
89+
@Inject
90+
private FirewallRulesDao _rulesDao;
8691

8792
@Autowired
8893
@Qualifier("networkHelper")
@@ -91,6 +96,9 @@ public class VirtualNetworkApplianceFactory {
9196
@Inject
9297
private NicProfileHelper _nicProfileHelper;
9398

99+
@Inject
100+
private NetworkTopologyContext _networkTopologyContext;
101+
94102
public NetworkModel getNetworkModel() {
95103
return _networkModel;
96104
}
@@ -174,4 +182,12 @@ public NetworkHelper getNetworkHelper() {
174182
public NicProfileHelper getNicProfileHelper() {
175183
return _nicProfileHelper;
176184
}
177-
}
185+
186+
public NetworkTopologyContext getNetworkTopologyContext() {
187+
return _networkTopologyContext;
188+
}
189+
190+
public FirewallRulesDao getFirewallRulesDao() {
191+
return _rulesDao;
192+
}
193+
}

server/src/main/java/org/apache/cloudstack/network/topology/AdvancedNetworkTopology.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ public boolean associatePublicIP(final Network network, final List<? extends Pub
214214
final boolean result = applyRules(network, router, typeString, isPodLevelException, podId, failWhenDisconnect, new RuleApplierWrapper<RuleApplier>(ipAssociationRules));
215215

216216
if (result) {
217-
_advancedVisitor.visit(nicPlugInOutRules);
217+
if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
218+
s_logger.debug("Router " + router.getInstanceName() + " is in " + router.getState() + ", so not sending NicPlugInOutRules command to the backend");
219+
} else {
220+
_advancedVisitor.visit(nicPlugInOutRules);
221+
}
218222
}
219223

220224
return result;
@@ -241,4 +245,4 @@ public boolean applyNetworkACLs(final Network network, final List<? extends Netw
241245
final boolean result = applyRules(network, router, typeString, isPodLevelException, podId, failWhenDisconnect, new RuleApplierWrapper<RuleApplier>(aclsRules));
242246
return result;
243247
}
244-
}
248+
}

systemvm/debian/opt/cloud/bin/configure.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,13 @@ def getGatewayByIp(self, ipa):
817817
return interface.get_gateway()
818818
return None
819819

820+
def getPrivateGatewayNetworks(self):
821+
interfaces = []
822+
for interface in self.config.address().get_interfaces():
823+
if interface.is_private_gateway():
824+
interfaces.append(interface)
825+
return interfaces
826+
820827
def portsToString(self, ports, delimiter):
821828
ports_parts = ports.split(":", 2)
822829
if ports_parts[0] == ports_parts[1]:
@@ -948,12 +955,21 @@ def processStaticNatRule(self, rule):
948955
if device is None:
949956
raise Exception("Ip address %s has no device in the ips databag" % rule["public_ip"])
950957

958+
chain_name = "PREROUTING-%s-def" % device
951959
self.fw.append(["mangle", "front",
952-
"-A PREROUTING -d %s/32 -m state --state NEW -j CONNMARK --save-mark --nfmask 0xffffffff --ctmask 0xffffffff" %
953-
rule["public_ip"]])
954-
self.fw.append(["mangle", "front",
955-
"-A PREROUTING -d %s/32 -m state --state NEW -j MARK --set-xmark %s/0xffffffff" %
956-
(rule["public_ip"], hex(100 + int(device[len("eth"):])))])
960+
"-A PREROUTING -s %s/32 -m state --state NEW -j %s" %
961+
(rule["internal_ip"], chain_name)])
962+
self.fw.append(["mangle", "",
963+
"-A %s -j MARK --set-xmark %s/0xffffffff" %
964+
(chain_name, hex(100 + int(device[len("eth"):])))])
965+
self.fw.append(["mangle", "",
966+
"-A %s -j CONNMARK --save-mark --nfmask 0xffffffff --ctmask 0xffffffff" %
967+
chain_name])
968+
private_gateways = self.getPrivateGatewayNetworks()
969+
for private_gw in private_gateways:
970+
self.fw.append(["mangle", "front", "-A %s -d %s -j RETURN" %
971+
(chain_name, private_gw.get_network())])
972+
957973
self.fw.append(["nat", "front",
958974
"-A PREROUTING -d %s/32 -j DNAT --to-destination %s" % (rule["public_ip"], rule["internal_ip"])])
959975
self.fw.append(["nat", "front",

0 commit comments

Comments
 (0)