Skip to content

Commit 4c01d81

Browse files
committed
CLOUDSTACK-6668: OVS distributed routing: ensure bridge is deleted when
last VM from the VPC is deleted on a host OVS distributed routing: ensure bridge is deleted when last VM from the VPC is deleted on a host. This fix ensures that bridge is destroyed.
1 parent e7e1ae2 commit 4c01d81

7 files changed

Lines changed: 111 additions & 80 deletions

File tree

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ private synchronized Network findOrCreateTunnelNetwork(Connection conn, String n
990990
Network.Record rec = new Network.Record();
991991
Set<Network> networks = Network.getByNameLabel(conn, nwName);
992992

993+
993994
if (networks.size() == 0) {
994995
rec.nameDescription = "tunnel network id# " + nwName;
995996
rec.nameLabel = nwName;
@@ -1064,11 +1065,11 @@ private synchronized Network configureTunnelNetwork(Connection conn, Long networ
10641065
}
10651066
}
10661067

1067-
private synchronized void destroyTunnelNetwork(Connection conn, String bridgeName) {
1068+
private synchronized void destroyTunnelNetwork(Connection conn, Network nw, long hostId) {
10681069
try {
1069-
Network nw = findOrCreateTunnelNetwork(conn, bridgeName);
10701070
String bridge = nw.getBridge(conn);
1071-
String result = callHostPlugin(conn, "ovstunnel", "destroy_ovs_bridge", "bridge", bridge);
1071+
String result = callHostPlugin(conn, "ovstunnel", "destroy_ovs_bridge", "bridge", bridge,
1072+
"cs_host_id", ((Long)hostId).toString());
10721073
String[] res = result.split(":");
10731074
if (res.length != 2 || !res[0].equalsIgnoreCase("SUCCESS")) {
10741075
//TODO: Should make this error not fatal?
@@ -1677,14 +1678,12 @@ private HashMap<String, String> parseDefaultOvsRuleComamnd(String str) {
16771678
return cmd;
16781679
}
16791680

1680-
private void cleanUpTmpDomVif(Connection conn, Network nw) {
1681-
List<VIF> vifs;
1682-
synchronized (_tmpDom0Vif) {
1683-
vifs = _tmpDom0Vif;
1684-
_tmpDom0Vif = new ArrayList<VIF>();
1685-
}
1681+
private void cleanUpTmpDomVif(Connection conn, Network nw) throws XenAPIException, XmlRpcException {
16861682

1687-
for (VIF v : vifs) {
1683+
Pair<VM, VM.Record> vm = getControlDomain(conn);
1684+
VM dom0 = vm.first();
1685+
Set<VIF> dom0Vifs = dom0.getVIFs(conn);
1686+
for (VIF v : dom0Vifs) {
16881687
String vifName = "unknown";
16891688
try {
16901689
VIF.Record vifr = v.getRecord(conn);
@@ -5273,12 +5272,17 @@ private Answer execute(OvsSetupBridgeCommand cmd) {
52735272
}
52745273

52755274
private Answer execute(OvsDestroyBridgeCommand cmd) {
5276-
Connection conn = getConnection();
5277-
Network nw = findOrCreateTunnelNetwork(conn, cmd.getBridgeName());
5278-
cleanUpTmpDomVif(conn, nw);
5279-
destroyTunnelNetwork(conn, cmd.getBridgeName());
5280-
s_logger.debug("OVS Bridge destroyed");
5281-
return new Answer(cmd, true, null);
5275+
try {
5276+
Connection conn = getConnection();
5277+
Network nw = findOrCreateTunnelNetwork(conn, cmd.getBridgeName());
5278+
cleanUpTmpDomVif(conn, nw);
5279+
destroyTunnelNetwork(conn, nw, cmd.getHostId());
5280+
s_logger.debug("OVS Bridge destroyed");
5281+
return new Answer(cmd, true, null);
5282+
} catch (Exception e) {
5283+
s_logger.warn("caught execption when destroying ovs bridge", e);
5284+
return new Answer(cmd, false, e.getMessage());
5285+
}
52825286
}
52835287

52845288
private Answer execute(OvsDestroyTunnelCommand cmd) {

‎plugins/network-elements/ovs/src/com/cloud/agent/api/OvsDestroyBridgeCommand.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public class OvsDestroyBridgeCommand extends Command {
2222

2323
Long networkId;
2424
String name;
25+
Long hostId;
2526

26-
public OvsDestroyBridgeCommand(Long networkId, String name) {
27+
public OvsDestroyBridgeCommand(Long networkId, String name, Long hostId) {
2728
this.networkId = networkId;
2829
this.name = name;
30+
this.hostId = hostId;
2931
}
3032

3133
public Long getNetworkId() {
@@ -36,6 +38,10 @@ public String getBridgeName() {
3638
return name;
3739
}
3840

41+
public Long getHostId() {
42+
return hostId;
43+
}
44+
3945
@Override
4046
public boolean executeInSequence() {
4147
return true;

‎plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsNetworkTopologyGuru.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface OvsNetworkTopologyGuru extends Manager {
5555
/**
5656
* get the list of all Vm id's in the network that are running on the host
5757
*/
58-
public List<Long> getActiveVmsInNetworkOnHost(long vpcId, long hostId);
58+
public List<Long> getActiveVmsInNetworkOnHost(long vpcId, long hostId, boolean includeVr);
5959

6060
/**
6161
* get the list of all Vpc id's in which, a VM has a nic in the network that is part of VPC

‎plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsNetworkTopologyGuruImpl.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public List<Long> getActiveVmsInVpcOnHost(long vpcId, long hostId) {
181181
Set<Long> vmIdsSet = new HashSet<>();
182182
List<? extends Network> vpcNetworks = _vpcMgr.getVpcNetworks(vpcId);
183183
for (Network network : vpcNetworks) {
184-
List<Long> networkVmIds = getActiveVmsInNetworkOnHost(network.getId(), hostId);
184+
List<Long> networkVmIds = getActiveVmsInNetworkOnHost(network.getId(), hostId, false);
185185
if (networkVmIds != null && !networkVmIds.isEmpty()) {
186186
vmIdsSet.addAll(networkVmIds);
187187
}
@@ -195,21 +195,19 @@ public List<Long> getActiveVmsInVpcOnHost(long vpcId, long hostId) {
195195
* get the list of all Vm id's in the network that are running on the host
196196
*/
197197
@Override
198-
public List<Long> getActiveVmsInNetworkOnHost(long networkId, long hostId) {
198+
public List<Long> getActiveVmsInNetworkOnHost(long networkId, long hostId, boolean includeVr) {
199199
List <Long> vmIds = new ArrayList<>();
200200
List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(networkId,
201-
VirtualMachine.State.Running, VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Unknown,
202-
VirtualMachine.State.Migrating);
201+
VirtualMachine.State.Running, VirtualMachine.State.Migrating);
203202
// Find routers for the network
204203
List<DomainRouterVO> routers = _routerDao.findByNetwork(networkId);
205-
206204
if (vms != null) {
207205
for (UserVmVO vm : vms) {
208206
if (vm.getHostId() == hostId)
209207
vmIds.add(vm.getId());
210208
}
211209
}
212-
if (routers.size() != 0) {
210+
if (routers.size() != 0 && includeVr) {
213211
for (DomainRouterVO router: routers) {
214212
if (router.getHostId() == hostId)
215213
vmIds.add(router.getId());

‎plugins/network-elements/ovs/src/com/cloud/network/ovs/OvsTunnelManagerImpl.java‎

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ protected OvsTunnelNetworkVO createTunnelRecord(long from, long to, long network
209209
private void handleCreateTunnelAnswer(Answer[] answers) {
210210
OvsCreateTunnelAnswer r = (OvsCreateTunnelAnswer)answers[0];
211211
String s =
212-
String.format("(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," + "greKey:%4$s, portName:%5$s)", r.getFromIp(), r.getToIp(), r.getBridge(), r.getKey(),
213-
r.getInPortName());
212+
String.format("(hostIP:%1$s, remoteIP:%2$s, bridge:%3$s," + "greKey:%4$s, portName:%5$s)",
213+
r.getFromIp(), r.getToIp(), r.getBridge(), r.getKey(), r.getInPortName());
214214
Long from = r.getFrom();
215215
Long to = r.getTo();
216216
long networkId = r.getNetworkId();
@@ -476,9 +476,13 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
476476

477477
if (nw.getVpcId() != null && isVpcEnabledForDistributedRouter(nw.getVpcId())) {
478478
List<Long> vmIds = _ovsNetworkToplogyGuru.getActiveVmsInVpcOnHost(nw.getVpcId(), host.getId());
479+
479480
if (vmIds != null && !vmIds.isEmpty()) {
480481
return;
481482
}
483+
484+
// there are not active VM's on this host belonging to any of the tiers in the VPC, so remove
485+
// the host from the tunnel mesh network and destroy the bridge
482486
List<? extends Network> vpcNetworks = _vpcMgr.getVpcNetworks(nw.getVpcId());
483487
try {
484488
for (Network network: vpcNetworks) {
@@ -499,15 +503,16 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
499503
}
500504
}
501505

502-
Command cmd = new OvsDestroyBridgeCommand(nw.getId(), generateBridgeNameForVpc(nw.getVpcId()));
506+
Command cmd = new OvsDestroyBridgeCommand(nw.getId(), generateBridgeNameForVpc(nw.getVpcId()),
507+
host.getId());
503508
s_logger.debug("Destroying bridge for network " + nw.getId() + " on host:" + host.getId());
504509
Answer ans = _agentMgr.send(host.getId(), cmd);
505510
handleDestroyBridgeAnswer(ans, host.getId(), nw.getId());
506511
} catch (Exception e) {
507512

508513
}
509514
} else {
510-
List<Long> vmIds = _ovsNetworkToplogyGuru.getActiveVmsInNetworkOnHost(nw.getId(), host.getId());
515+
List<Long> vmIds = _ovsNetworkToplogyGuru.getActiveVmsInNetworkOnHost(nw.getId(), host.getId(), true);
511516
if (vmIds != null && !vmIds.isEmpty()) {
512517
return;
513518
}
@@ -516,7 +521,7 @@ public void checkAndRemoveHostFromTunnelNetwork(Network nw, Host host) {
516521
* the tunnels for this network */
517522
int key = getGreKey(nw);
518523
String bridgeName = generateBridgeName(nw, key);
519-
Command cmd = new OvsDestroyBridgeCommand(nw.getId(), bridgeName);
524+
Command cmd = new OvsDestroyBridgeCommand(nw.getId(), bridgeName, host.getId());
520525
s_logger.debug("Destroying bridge for network " + nw.getId() + " on host:" + host.getId());
521526
Answer ans = _agentMgr.send(host.getId(), cmd);
522527
handleDestroyBridgeAnswer(ans, host.getId(), nw.getId());
@@ -558,44 +563,66 @@ private String generateBridgeNameForVpc(long vpcId) {
558563
protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
559564

560565
long hostId = host.getId();
561-
List<? extends Network> vpcNetworks = _vpcMgr.getVpcNetworks(vpcId);
562-
List<Long> vpcSpannedHostIds = _ovsNetworkToplogyGuru.getVpcSpannedHosts(vpcId);
563566
String bridgeName=generateBridgeNameForVpc(vpcId);
564-
boolean bridgeNotSetup = true;
565567

568+
List<Long> vmIds = _ovsNetworkToplogyGuru.getActiveVmsInVpcOnHost(vpcId, hostId);
569+
570+
if (vmIds == null || vmIds.isEmpty()) {
571+
572+
// since this is the first VM from the VPC being launched on the host, first setup the bridge
573+
try {
574+
Commands cmds = new Commands(new OvsSetupBridgeCommand(bridgeName, hostId, null));
575+
s_logger.debug("Ask host " + hostId + " to create bridge for vpc " + vpcId + " and configure the "
576+
+ " bridge for distributed routing.");
577+
Answer[] answers = _agentMgr.send(hostId, cmds);
578+
handleSetupBridgeAnswer(answers);
579+
} catch (OperationTimedoutException | AgentUnavailableException e) {
580+
s_logger.warn("Ovs Tunnel network created tunnel failed", e);
581+
}
582+
583+
// now that bridge is setup, populate network acl's before the VM gets created
584+
OvsVpcRoutingPolicyConfigCommand cmd = prepareVpcRoutingPolicyUpdate(vpcId);
585+
cmd.setSequenceNumber(getNextRoutingPolicyUpdateSequenceNumber(vpcId));
586+
587+
if (!sendVpcRoutingPolicyChangeUpdate(cmd, hostId, bridgeName)) {
588+
s_logger.debug("Failed to send VPC routing policy change update to host : " + hostId +
589+
". But moving on with sending the updates to the rest of the hosts.");
590+
}
591+
}
592+
593+
List<? extends Network> vpcNetworks = _vpcMgr.getVpcNetworks(vpcId);
594+
List<Long> vpcSpannedHostIds = _ovsNetworkToplogyGuru.getVpcSpannedHosts(vpcId);
566595
for (Network vpcNetwork: vpcNetworks) {
567596
if (vpcNetwork.getState() != Network.State.Implemented &&
568597
vpcNetwork.getState() != Network.State.Implementing && vpcNetwork.getState() != Network.State.Setup)
569598
continue;
599+
570600
int key = getGreKey(vpcNetwork);
571601
List<Long> toHostIds = new ArrayList<Long>();
572602
List<Long> fromHostIds = new ArrayList<Long>();
603+
OvsTunnelNetworkVO tunnelRecord = null;
573604

574605
for (Long rh : vpcSpannedHostIds) {
575606
if (rh == hostId) {
576607
continue;
577608
}
578-
OvsTunnelNetworkVO ta = _tunnelNetworkDao.getByFromToNetwork(hostId, rh.longValue(), vpcNetwork.getId());
579-
// Try and create the tunnel even if a previous attempt failed
580-
if (ta == null || ta.getState().equals(OvsTunnel.State.Failed.name())) {
609+
tunnelRecord = _tunnelNetworkDao.getByFromToNetwork(hostId, rh.longValue(), vpcNetwork.getId());
610+
// Try and create the tunnel if does not exit or previous attempt failed
611+
if (tunnelRecord == null || tunnelRecord.getState().equals(OvsTunnel.State.Failed.name())) {
581612
s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + rh.longValue());
582-
if (ta == null) {
613+
if (tunnelRecord == null) {
583614
createTunnelRecord(hostId, rh.longValue(), vpcNetwork.getId(), key);
584615
}
585616
if (!toHostIds.contains(rh)) {
586617
toHostIds.add(rh);
587618
}
588619
}
589-
590-
ta = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(),
591-
hostId, vpcNetwork.getId());
592-
// Try and create the tunnel even if a previous attempt failed
593-
if (ta == null || ta.getState().equals(OvsTunnel.State.Failed.name())) {
594-
s_logger.debug("Attempting to create tunnel from:" +
595-
rh.longValue() + " to:" + hostId);
596-
if (ta == null) {
597-
createTunnelRecord(rh.longValue(), hostId,
598-
vpcNetwork.getId(), key);
620+
tunnelRecord = _tunnelNetworkDao.getByFromToNetwork(rh.longValue(), hostId, vpcNetwork.getId());
621+
// Try and create the tunnel if does not exit or previous attempt failed
622+
if (tunnelRecord == null || tunnelRecord.getState().equals(OvsTunnel.State.Failed.name())) {
623+
s_logger.debug("Attempting to create tunnel from:" + rh.longValue() + " to:" + hostId);
624+
if (tunnelRecord == null) {
625+
createTunnelRecord(rh.longValue(), hostId, vpcNetwork.getId(), key);
599626
}
600627
if (!fromHostIds.contains(rh)) {
601628
fromHostIds.add(rh);
@@ -615,20 +642,16 @@ protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
615642
String otherIp = getGreEndpointIP(rHost, vpcNetwork);
616643
if (otherIp == null)
617644
throw new GreTunnelException(
618-
"Unable to retrieve the remote "
619-
+ "endpoint for the GRE tunnel."
645+
"Unable to retrieve the remote endpoint for the GRE tunnel."
620646
+ "Failure is on host:" + rHost.getId());
621-
Commands cmds = new Commands(
622-
new OvsCreateTunnelCommand(otherIp, key,
623-
Long.valueOf(hostId), i, vpcNetwork.getId(), myIp, bridgeName,
624-
vpcNetwork.getUuid()));
647+
Commands cmds = new Commands( new OvsCreateTunnelCommand(otherIp, key, Long.valueOf(hostId),
648+
i, vpcNetwork.getId(), myIp, bridgeName, vpcNetwork.getUuid()));
625649
s_logger.debug("Attempting to create tunnel from:" + hostId + " to:" + i + " for the network "
626650
+ vpcNetwork.getId());
627651
s_logger.debug("Ask host " + hostId
628652
+ " to create gre tunnel to " + i);
629653
Answer[] answers = _agentMgr.send(hostId, cmds);
630654
handleCreateTunnelAnswer(answers);
631-
bridgeNotSetup = false;
632655
}
633656

634657
for (Long i : fromHostIds) {
@@ -641,27 +664,12 @@ protected void checkAndCreateVpcTunnelNetworks(Host host, long vpcId) {
641664
+ hostId);
642665
Answer[] answers = _agentMgr.send(i, cmds);
643666
handleCreateTunnelAnswer(answers);
644-
bridgeNotSetup = false;
645667
}
646668
} catch (GreTunnelException | OperationTimedoutException | AgentUnavailableException e) {
647669
// I really thing we should do a better handling of these exceptions
648670
s_logger.warn("Ovs Tunnel network created tunnel failed", e);
649671
}
650672
}
651-
652-
// If no tunnels have been configured, perform the bridge setup
653-
// anyway. This will ensure VIF rules will be triggered
654-
if (bridgeNotSetup) {
655-
try {
656-
Commands cmds = new Commands(new OvsSetupBridgeCommand(bridgeName, hostId, null));
657-
s_logger.debug("Ask host " + hostId + " to configure bridge for vpc");
658-
Answer[] answers = _agentMgr.send(hostId, cmds);
659-
handleSetupBridgeAnswer(answers);
660-
} catch (OperationTimedoutException | AgentUnavailableException e) {
661-
// I really thing we should do a better handling of these exceptions
662-
s_logger.warn("Ovs Tunnel network created tunnel failed", e);
663-
}
664-
}
665673
}
666674

667675
@Override

‎scripts/vm/hypervisor/xenserver/cloudstack_pluginlib.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,11 @@ def get_acl(vpcconfig, required_acl_id):
345345
return None
346346

347347
def check_tunnel_exists(bridge, tunnel_name):
348-
res = do_cmd([VSCTL_PATH, "port-to-br", tunnel_name])
349-
return res == bridge
348+
try:
349+
res = do_cmd([VSCTL_PATH, "port-to-br", tunnel_name])
350+
return res == bridge
351+
except:
352+
return False
350353

351354
def create_tunnel(bridge, remote_ip, gre_key, src_host, dst_host, network_uuid):
352355

@@ -460,7 +463,7 @@ def create_tunnel(bridge, remote_ip, gre_key, src_host, dst_host, network_uuid):
460463

461464
logging.debug("Successfully created tunnel from host %s" %src_host + " to host %s" %dst_host +
462465
" with GRE key %s" %gre_key)
463-
return "SUCCESS:%s creation succeeded" % name
466+
return "SUCCESS:%s" % name
464467
except:
465468
logging.debug("An unexpected error occured. Rolling back")
466469
if tunnel_setup:
@@ -548,7 +551,8 @@ def configure_vpc_bridge_for_network_topology(bridge, this_host_id, json_config,
548551

549552
# check if tunnel exists already, if not create a tunnel from this host to remote host
550553
if not check_tunnel_exists(bridge, tunnel_name):
551-
create_tunnel(bridge, host.ipaddress, gre_key, this_host_id, host.hostid, network.networkuuid)
554+
create_tunnel(bridge, str(host.ipaddress), str(gre_key), this_host_id,
555+
host.hostid, network.networkuuid)
552556

553557
of_port = get_ofport_for_vif(tunnel_name)
554558

0 commit comments

Comments
 (0)