Skip to content

Commit 2b65552

Browse files
author
Pearl Dsilva
committed
Include cleanup of n/w resources on n/w deletion + prevent n/w resource deletion on detroy/stop/unplug of VM from persistent n/w
1 parent c92652b commit 2b65552

File tree

20 files changed

+352
-44
lines changed

20 files changed

+352
-44
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.agent.api;
19+
20+
public class CleanupPersistentNetworkResourceAnswer extends Answer{
21+
public CleanupPersistentNetworkResourceAnswer() {
22+
}
23+
24+
public CleanupPersistentNetworkResourceAnswer(CleanupPersistentNetworkResourceCommand cmd, boolean success, String result) {
25+
super(cmd, success, result);
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.agent.api;
19+
20+
import com.cloud.agent.api.to.NicTO;
21+
22+
public class CleanupPersistentNetworkResourceCommand extends Command{
23+
NicTO nicTO;
24+
25+
protected CleanupPersistentNetworkResourceCommand() {}
26+
27+
public CleanupPersistentNetworkResourceCommand(NicTO nicTO) {
28+
this.nicTO = nicTO;
29+
}
30+
31+
public NicTO getNicTO() {
32+
return nicTO;
33+
}
34+
35+
public void setNicTO(NicTO nicTO) {
36+
this.nicTO = nicTO;
37+
}
38+
39+
@Override
40+
public boolean executeInSequence() {
41+
return false;
42+
}
43+
}

core/src/main/java/com/cloud/agent/api/UnPlugNicCommand.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
package com.cloud.agent.api;
2121

22+
import java.util.Map;
23+
2224
import com.cloud.agent.api.to.NicTO;
2325

2426
public class UnPlugNicCommand extends Command {
2527
NicTO nic;
2628
String instanceName;
29+
Map<String, Boolean> vlanToPersistenceMap;
2730

2831
public NicTO getNic() {
2932
return nic;
@@ -45,4 +48,12 @@ public UnPlugNicCommand(NicTO nic, String instanceName) {
4548
public String getVmName() {
4649
return instanceName;
4750
}
51+
52+
public Map<String, Boolean> getVlanToPersistenceMap() {
53+
return vlanToPersistenceMap;
54+
}
55+
56+
public void setVlanToPersistenceMap(Map<String, Boolean> vlanToPersistenceMap) {
57+
this.vlanToPersistenceMap = vlanToPersistenceMap;
58+
}
4859
}

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,6 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl
19741974
vmGuru.prepareStop(profile);
19751975

19761976
Map<String, Boolean> vlanToPersistenceMap = getVlanToPersistenceMapForVM(vm.getId());
1977-
19781977
final StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false, cleanUpEvenIfUnableToStop);
19791978
stop.setControlIp(getControlNicIpForVM(vm));
19801979
if (MapUtils.isNotEmpty(vlanToPersistenceMap)) {
@@ -4419,6 +4418,10 @@ public boolean unplugNic(final Network network, final NicTO nic, final VirtualMa
44194418
try {
44204419
final Commands cmds = new Commands(Command.OnError.Stop);
44214420
final UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName());
4421+
Map<String, Boolean> vlanToPersistenceMap = getVlanToPersistenceMapForVM(vm.getId());
4422+
if (MapUtils.isNotEmpty(vlanToPersistenceMap)) {
4423+
unplugNicCmd.setVlanToPersistenceMap(vlanToPersistenceMap);
4424+
}
44224425
cmds.addCommand("unplugnic", unplugNicCmd);
44234426
_agentMgr.send(dest.getHost().getId(), cmds);
44244427

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import javax.inject.Inject;
4141
import javax.naming.ConfigurationException;
4242

43+
import com.cloud.agent.api.CleanupPersistentNetworkResourceAnswer;
44+
import com.cloud.agent.api.CleanupPersistentNetworkResourceCommand;
4345
import com.cloud.agent.api.SetupPersistentNetworkAnswer;
4446
import com.cloud.agent.api.SetupPersistentNetworkCommand;
4547
import com.cloud.dc.ClusterVO;
@@ -1223,7 +1225,7 @@ private Pair<Boolean, NicTO> isNtwConfiguredInCluster(HostVO hostVO, Map<Long, L
12231225
if (hosts == null) {
12241226
hosts = new ArrayList<>();
12251227
}
1226-
if (hostVO.getHypervisorType() == HypervisorType.KVM) {
1228+
if (hostVO.getHypervisorType() == HypervisorType.KVM || hostVO.getHypervisorType() == HypervisorType.XenServer ) {
12271229
hosts.add(hostVO.getId());
12281230
clusterToHostsMap.put(clusterId, hosts);
12291231
return new Pair<>(false, createNicTOFromNetworkAndOffering(networkVO, networkOfferingVO, hostVO));
@@ -1267,6 +1269,15 @@ private void setupPersistentNetwork(NetworkVO network, NetworkOfferingVO offerin
12671269
}
12681270
}
12691271

1272+
private boolean networkMeetsPersistenceCriteria(NetworkVO network, NetworkOfferingVO offering, boolean cleanup) {
1273+
boolean criteriaMet = offering.isPersistent() &&
1274+
(network.getBroadcastUri() != null && BroadcastDomainType.getSchemeValue(network.getBroadcastUri()) == BroadcastDomainType.Vlan);
1275+
if (!cleanup) {
1276+
return criteriaMet && network.getGuestType() == GuestType.L2;
1277+
} else {
1278+
return criteriaMet && (network.getGuestType() == GuestType.L2 || network.getGuestType() == GuestType.Isolated);
1279+
}
1280+
}
12701281
@Override
12711282
@DB
12721283
public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final DeployDestination dest, final ReservationContext context) throws ConcurrentOperationException,
@@ -1326,8 +1337,7 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final
13261337
implementNetworkElementsAndResources(dest, context, network, offering);
13271338

13281339
long dcId = dest.getDataCenter().getId();
1329-
if (network.getGuestType() == GuestType.L2 && offering.isPersistent() &&
1330-
(network.getBroadcastUri() != null && BroadcastDomainType.getSchemeValue(network.getBroadcastUri()) == BroadcastDomainType.Vlan)) {
1340+
if (networkMeetsPersistenceCriteria(network,offering, false)) {
13311341
setupPersistentNetwork(network, offering, dcId);
13321342
}
13331343
if (isSharedNetworkWithServices(network)) {
@@ -2948,6 +2958,32 @@ public boolean shutdownNetworkElementsAndResources(final ReservationContext cont
29482958
return success;
29492959
}
29502960

2961+
private void cleanupPersistentnNetworkResources(NetworkVO network) {
2962+
long networkOfferingId = network.getNetworkOfferingId();
2963+
NetworkOfferingVO offering = _networkOfferingDao.findById(networkOfferingId);
2964+
if (offering != null) {
2965+
if (networkMeetsPersistenceCriteria(network, offering, true)) {
2966+
List<HostVO> hosts = resourceManager.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.Routing, network.getDataCenterId());
2967+
for (HostVO host : hosts) {
2968+
try {
2969+
NicTO to = createNicTOFromNetworkAndOffering(network, offering, host);
2970+
CleanupPersistentNetworkResourceCommand cmd = new CleanupPersistentNetworkResourceCommand(to);
2971+
CleanupPersistentNetworkResourceAnswer answer = (CleanupPersistentNetworkResourceAnswer) _agentMgr.send(host.getId(), cmd);
2972+
if (answer == null) {
2973+
s_logger.warn("Unable to get an answer to the CleanupPersistentNetworkResourceCommand from agent:" + host.getId());
2974+
}
2975+
2976+
if (!answer.getResult()) {
2977+
s_logger.warn("Unable to setup agent " + host.getId() + " due to " + answer.getDetails());
2978+
}
2979+
} catch (Exception e) {
2980+
s_logger.warn("Failed to cleanup network resources");
2981+
}
2982+
}
2983+
}
2984+
}
2985+
}
2986+
29512987
@Override
29522988
@DB
29532989
public boolean destroyNetwork(final long networkId, final ReservationContext context, final boolean forced) {
@@ -2987,6 +3023,8 @@ public boolean destroyNetwork(final long networkId, final ReservationContext con
29873023
}
29883024
}
29893025

3026+
cleanupPersistentnNetworkResources(network);
3027+
29903028
// Shutdown network first
29913029
shutdownNetwork(networkId, context, false);
29923030

plugins/api/discovery/src/main/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
2222
import java.util.HashSet;
23+
import java.util.LinkedHashSet;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Set;
@@ -67,7 +68,7 @@ public boolean start() {
6768
if (s_apiNameDiscoveryResponseMap == null) {
6869
long startTime = System.nanoTime();
6970
s_apiNameDiscoveryResponseMap = new HashMap<String, ApiDiscoveryResponse>();
70-
Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
71+
Set<Class<?>> cmdClasses = new LinkedHashSet<Class<?>>();
7172
for (PluggableService service : _services) {
7273
s_logger.debug(String.format("getting api commands of service: %s", service.getClass().getName()));
7374
cmdClasses.addAll(service.getCommands());

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,4 +437,20 @@ public boolean isExistingBridge(String bridgeName) {
437437
return false;
438438
}
439439
}
440+
441+
@Override
442+
public void deleteBr(NicTO nic) {
443+
String vlanId = Networks.BroadcastDomainType.getValue(nic.getBroadcastUri());
444+
String trafficLabel = nic.getName();
445+
String pifName = _pifs.get(trafficLabel);
446+
if (pifName == null) {
447+
// if not found in bridge map, maybe traffic label refers to pif already?
448+
File pif = new File("/sys/class/net/" + trafficLabel);
449+
if (pif.isDirectory()) {
450+
pifName = trafficLabel;
451+
}
452+
}
453+
String brName = generateVnetBrName(pifName, vlanId);
454+
deleteVnetBr(brName, true);
455+
}
440456
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/DirectVifDriver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@ public void detach(LibvirtVMDef.InterfaceDef iface) {
8080
public void createControlNetwork(String privBrName) {
8181
}
8282

83+
@Override
84+
public void deleteBr(NicTO nic) {
85+
}
8386
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/IvsVifDriver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public void createControlNetwork(String privBrName) {
287287
}
288288
}
289289

290+
@Override
291+
public void deleteBr(NicTO nic) {
292+
}
293+
290294
private boolean isBridgeExists(String bridgeName) {
291295
File f = new File("/sys/devices/virtual/net/" + bridgeName);
292296
if (f.exists()) {

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/OvsVifDriver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,8 @@ public boolean isExistingBridge(String bridgeName) {
260260
return false;
261261
}
262262
}
263+
264+
@Override
265+
public void deleteBr(NicTO nic) {
266+
}
263267
}

0 commit comments

Comments
 (0)