Skip to content

Commit a65b584

Browse files
author
Marcus Sorensen
committed
Summary: Fix bridge parsing when bridge names are subsets of others
Detail: There are several places in the code that do a "brctl show | grep bridgeName" or similar, which causes all sorts of problems when you have for example a cloudVirBr50 and a cloudVirBr5000. This patch attempts to stop relying on the output of brctl, instead favoring sysfs and /sys/devices/virtual/net. It cuts a lot of bash out altogether by using java File. It was tested in my devcloud-kvm against current 4.0, as well as by the customer reporting initial bug. BUG-ID: CLOUDSTACK-938 Fix-For: 4.0.1 Signed-off-by: Marcus Sorensen <marcus@betterservers.com>
1 parent 98a2890 commit a65b584

2 files changed

Lines changed: 62 additions & 33 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.naming.ConfigurationException;
3434
import java.net.URI;
3535
import java.util.Map;
36+
import java.io.File;
3637

3738
public class BridgeVifDriver extends VifDriverBase {
3839

@@ -206,15 +207,11 @@ private void createControlNetwork(String privBrName) {
206207
}
207208

208209
private boolean isBridgeExists(String bridgeName) {
209-
Script command = new Script("/bin/sh", _timeout);
210-
command.add("-c");
211-
command.add("brctl show|grep " + bridgeName);
212-
final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
213-
String result = command.execute(parser);
214-
if (result != null || parser.getLine() == null) {
215-
return false;
216-
} else {
210+
File f = new File("/sys/devices/virtual/net/" + bridgeName);
211+
if (f.exists()) {
217212
return true;
213+
} else {
214+
return false;
218215
}
219216
}
220217
}

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java‎

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,19 @@ public boolean configure(String name, Map<String, Object> params)
788788
}
789789

790790
private void getPifs() {
791-
/* gather all available bridges and find their pifs, so that we can match them against traffic labels later */
792-
String cmdout = Script.runSimpleBashScript("brctl show | tail -n +2 | grep -v \"^\\s\"|awk '{print $1}'|sed '{:q;N;s/\\n/%/g;t q}'");
793-
s_logger.debug("cmdout was " + cmdout);
794-
List<String> bridges = Arrays.asList(cmdout.split("%"));
791+
File dir = new File("/sys/devices/virtual/net");
792+
File[] netdevs = dir.listFiles();
793+
List<String> bridges = new ArrayList<String>();
794+
for (int i = 0; i < netdevs.length; i++) {
795+
File isbridge = new File(netdevs[i].getAbsolutePath() + "/bridge");
796+
String netdevName = netdevs[i].getName();
797+
s_logger.debug("looking in file " + netdevs[i].getAbsolutePath() + "/bridge");
798+
if (isbridge.exists()) {
799+
s_logger.debug("Found bridge " + netdevName);
800+
bridges.add(netdevName);
801+
}
802+
}
803+
795804
for (String bridge : bridges) {
796805
s_logger.debug("looking for pif for bridge " + bridge);
797806
String pif = getPif(bridge);
@@ -807,24 +816,53 @@ private void getPifs() {
807816
}
808817

809818
private String getPif(String bridge) {
810-
String pif = Script.runSimpleBashScript("brctl show | grep " + bridge + " | awk '{print $4}'");
811-
String vlan = Script.runSimpleBashScript("ls /proc/net/vlan/" + pif);
819+
String pif = matchPifFileInDirectory(bridge);
820+
File vlanfile = new File("/proc/net/vlan" + pif);
812821

813-
if (vlan != null && !vlan.isEmpty()) {
814-
pif = Script.runSimpleBashScript("grep ^Device\\: /proc/net/vlan/" + pif + " | awk {'print $2'}");
822+
if (vlanfile.isFile()) {
823+
pif = Script.runSimpleBashScript("grep ^Device\\: /proc/net/vlan/"
824+
+ pif + " | awk {'print $2'}");
815825
}
816826

817827
return pif;
818828
}
819829

830+
private String matchPifFileInDirectory(String bridgeName){
831+
File f = new File("/sys/devices/virtual/net/" + bridgeName + "/brif");
832+
833+
if (! f.isDirectory()){
834+
s_logger.debug("failing to get physical interface from bridge"
835+
+ bridgeName + ", does " + f.getAbsolutePath()
836+
+ "exist?");
837+
return "";
838+
}
839+
840+
File[] interfaces = f.listFiles();
841+
842+
for (int i = 0; i < interfaces.length; i++) {
843+
String fname = interfaces[i].getName();
844+
s_logger.debug("matchPifFileInDirectory: file name '"+fname+"'");
845+
if (fname.startsWith("eth") || fname.startsWith("bond")
846+
|| fname.startsWith("vlan")) {
847+
return fname;
848+
}
849+
}
850+
851+
s_logger.debug("failing to get physical interface from bridge"
852+
+ bridgeName + ", did not find an eth*, bond*, or vlan* in "
853+
+ f.getAbsolutePath());
854+
return "";
855+
}
856+
857+
820858
private boolean checkNetwork(String networkName) {
821859
if (networkName == null) {
822860
return true;
823861
}
824862

825-
String name = Script.runSimpleBashScript("brctl show | grep "
826-
+ networkName + " | awk '{print $4}'");
827-
if (name == null) {
863+
String name = matchPifFileInDirectory(networkName);
864+
865+
if (name == null || name.isEmpty()) {
828866
return false;
829867
} else {
830868
return true;
@@ -1381,20 +1419,15 @@ public Answer execute(DestroyCommand cmd) {
13811419
}
13821420

13831421
private String getVlanIdFromBridge(String brName) {
1384-
OutputInterpreter.OneLineParser vlanIdParser = new OutputInterpreter.OneLineParser();
1385-
final Script cmd = new Script("/bin/bash", s_logger);
1386-
cmd.add("-c");
1387-
cmd.add("vlanid=$(brctl show |grep " + brName
1388-
+ " |awk '{print $4}' | cut -s -d. -f 2);echo $vlanid");
1389-
String result = cmd.execute(vlanIdParser);
1390-
if (result != null) {
1391-
return null;
1392-
}
1393-
String vlanId = vlanIdParser.getLine();
1394-
if (vlanId.equalsIgnoreCase("")) {
1395-
return null;
1422+
String pif= matchPifFileInDirectory(brName);
1423+
String[] pifparts = pif.split("\\.");
1424+
1425+
if(pifparts.length == 2) {
1426+
return pifparts[1];
13961427
} else {
1397-
return vlanId;
1428+
s_logger.debug("failed to get vlan id from bridge " + brName
1429+
+ "attached to physical interface" + pif);
1430+
return "";
13981431
}
13991432
}
14001433

@@ -1632,7 +1665,6 @@ else if (pluggedVlan.equalsIgnoreCase(_publicBridgeName)
16321665
}
16331666

16341667
for (IpAddressTO ip : ips) {
1635-
String ipVlan = ip.getVlanId();
16361668
String nicName = "eth" + vlanToNicNum.get(ip.getVlanId());
16371669
String netmask = Long.toString(NetUtils.getCidrSize(ip.getVlanNetmask()));
16381670
String subnet = NetUtils.getSubNet(ip.getPublicIp(), ip.getVlanNetmask());

0 commit comments

Comments
 (0)