Skip to content

Commit 37cb0ae

Browse files
committed
add security group for direct tagged vlan
1 parent cc77245 commit 37cb0ae

5 files changed

Lines changed: 229 additions & 157 deletions

File tree

‎agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java‎

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ private Answer execute(AttachIsoCommand cmd) {
15001500
private AttachVolumeAnswer execute(AttachVolumeCommand cmd) {
15011501
try {
15021502
Connect conn = LibvirtConnection.getConnection();
1503-
attachOrDetachDisk(conn, cmd.getAttach(), cmd.getVmName(), cmd.getVolumePath());
1503+
attachOrDetachDisk(conn, cmd.getAttach(), cmd.getVmName(), cmd.getVolumePath(), cmd.getDeviceId().intValue());
15041504
} catch (LibvirtException e) {
15051505
return new AttachVolumeAnswer(cmd, e.toString());
15061506
} catch (InternalErrorException e) {
@@ -1766,12 +1766,12 @@ private Answer execute(NetworkUsageCommand cmd) {
17661766
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
17671767
String xml = dm.getXMLDesc(0);
17681768
parser.parseDomainXML(xml);
1769-
List<String> nics = parser.getInterfaces();
1769+
List<InterfaceDef> nics = parser.getInterfaces();
17701770
if (nics.size() != 3) {
17711771
return new Answer(cmd, false, vmName + " doesn't have public nic");
17721772
}
1773-
String pubNic = nics.get(2);
1774-
Pair<Double, Double> nicStats = getNicStats(pubNic);
1773+
InterfaceDef pubNic = nics.get(2);
1774+
Pair<Double, Double> nicStats = getNicStats(pubNic.getBrName());
17751775
/*Note: received means bytes received by all the vms, but from host kernel's pov, it's tx*/
17761776
return new NetworkUsageAnswer(cmd, "", nicStats.first().longValue(), nicStats.second().longValue());
17771777
} catch (LibvirtException e) {
@@ -2045,20 +2045,29 @@ protected synchronized StartAnswer execute(StartCommand cmd) {
20452045
startDomain(conn, vmName, vm.toString());
20462046

20472047
if (vmSpec.getType() != VirtualMachine.Type.User) {
2048-
default_network_rules_for_systemvm(vmName);
2048+
default_network_rules_for_systemvm(vmName);
20492049
} else {
20502050
NicTO[] nics = vmSpec.getNics();
2051+
List<InterfaceDef> vifs = getInterfaces(conn, vmName);
2052+
StringBuilder rules = new StringBuilder();
2053+
20512054
for (NicTO nic : nics) {
2052-
if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
2053-
default_network_rules(vmName, vmSpec.getNics()[0].getIp(), vmSpec.getId(), vmSpec.getNics()[0].getMac());
2055+
if (nic.getIsolationUri() != null ) {
2056+
if (nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString()) ||
2057+
nic.getType() == TrafficType.Public) {
2058+
rules.append(nic.getIp() + "," + nic.getMac() + "," + vifs.get(nic.getDeviceId()).getDevName() + ";") ;
2059+
}
20542060
}
20552061
}
2062+
2063+
if (rules.toString() != null)
2064+
default_network_rules(vmName, vmSpec.getId(), rules.toString());
20562065
}
20572066

20582067
// Attach each data volume to the VM, if there is a deferred attached disk
20592068
for (DiskDef disk : vm.getDevices().getDisks()) {
20602069
if (disk.isAttachDeferred()) {
2061-
attachOrDetachDisk(conn, true, vmName, disk.getDiskPath());
2070+
attachOrDetachDisk(conn, true, vmName, disk.getDiskPath(), disk.getDiskSeq());
20622071
}
20632072
}
20642073
state = State.Running;
@@ -2247,16 +2256,15 @@ protected synchronized String attachOrDetachISO(Connect conn, String vmName, Str
22472256
return attachOrDetachDevice(conn, true, vmName, isoXml);
22482257
}
22492258

2250-
protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName, String sourceFile) throws LibvirtException, InternalErrorException {
2251-
String diskDev = null;
2252-
SortedMap<String, String> diskMaps = null;
2259+
protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName, String sourceFile, int devId) throws LibvirtException, InternalErrorException {
2260+
List<DiskDef> disks = null;
22532261
Domain dm = null;
22542262
try {
22552263
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
22562264
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
22572265
String xml = dm.getXMLDesc(0);
22582266
parser.parseDomainXML(xml);
2259-
diskMaps = parser.getDiskMaps();
2267+
disks = parser.getDisks();
22602268
} catch (LibvirtException e) {
22612269
throw e;
22622270
} finally {
@@ -2265,32 +2273,26 @@ protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, S
22652273
}
22662274
}
22672275

2268-
if (attach) {
2269-
diskDev = diskMaps.lastKey();
2270-
/*Find the latest disk dev, and add 1 on it: e.g. if we already attach sdc to a vm, the next disk dev is sdd*/
2271-
diskDev = diskDev.substring(0, diskDev.length() - 1) + (char)(diskDev.charAt(diskDev.length() -1) + 1);
2272-
} else {
2273-
Set<Map.Entry<String, String>> entrySet = diskMaps.entrySet();
2274-
Iterator<Map.Entry<String, String>> itr = entrySet.iterator();
2275-
while (itr.hasNext()) {
2276-
Map.Entry<String, String> entry = itr.next();
2277-
if ((entry.getValue() != null) && (entry.getValue().equalsIgnoreCase(sourceFile))) {
2278-
diskDev = entry.getKey();
2279-
break;
2280-
}
2281-
}
2282-
}
2276+
if (!attach) {
2277+
boolean diskAttached = false;
22832278

2284-
if (diskDev == null) {
2285-
s_logger.warn("Can't get disk dev");
2286-
return "Can't get disk dev";
2279+
for (DiskDef disk : disks) {
2280+
if (disk.getDiskPath().equalsIgnoreCase(sourceFile)) {
2281+
devId = disk.getDiskSeq();
2282+
diskAttached = true;
2283+
}
2284+
}
2285+
if (!diskAttached) {
2286+
throw new InternalErrorException("disk: " + sourceFile + " is not attached before");
2287+
}
22872288
}
2289+
22882290
DiskDef disk = new DiskDef();
22892291
String guestOSType = getGuestType(conn, vmName);
22902292
if (isGuestPVEnabled(guestOSType)) {
2291-
disk.defFileBasedDisk(sourceFile, diskDev, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
2293+
disk.defFileBasedDisk(sourceFile, devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
22922294
} else {
2293-
disk.defFileBasedDisk(sourceFile, diskDev, DiskDef.diskBus.SCSI, DiskDef.diskFmtType.QCOW2);
2295+
disk.defFileBasedDisk(sourceFile, devId, DiskDef.diskBus.SCSI, DiskDef.diskFmtType.QCOW2);
22942296
}
22952297
String xml = disk.toString();
22962298
return attachOrDetachDevice(conn, attach, vmName, xml);
@@ -2993,18 +2995,20 @@ private Domain getDomain(Connect conn, String vmName) throws LibvirtException {
29932995
return conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
29942996
}
29952997

2996-
private List<String> getInterfaces(Connect conn, String vmName) {
2998+
private List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
29972999
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
29983000
Domain dm = null;
29993001
try {
30003002
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
30013003
parser.parseDomainXML(dm.getXMLDesc(0));
3004+
return parser.getInterfaces();
3005+
30023006
} catch (LibvirtException e) {
30033007
s_logger.debug("Failed to get dom xml: " + e.toString());
3004-
return new ArrayList<String>();
3008+
return new ArrayList<InterfaceDef>();
30053009
} catch (Exception e) {
30063010
s_logger.debug("Failed to get dom xml: " + e.toString());
3007-
return new ArrayList<String>();
3011+
return new ArrayList<InterfaceDef>();
30083012
} finally {
30093013
try {
30103014
if (dm != null) {
@@ -3014,7 +3018,6 @@ private List<String> getInterfaces(Connect conn, String vmName) {
30143018

30153019
}
30163020
}
3017-
return parser.getInterfaces();
30183021
}
30193022

30203023
private String executeBashScript(String script) {
@@ -3091,11 +3094,11 @@ private VmStatsEntry getVmStat(Connect conn, String vmName) throws LibvirtExcept
30913094

30923095
/*get network stats*/
30933096

3094-
List<String> vifs = getInterfaces(conn, vmName);
3097+
List<InterfaceDef> vifs = getInterfaces(conn, vmName);
30953098
long rx = 0;
30963099
long tx = 0;
3097-
for (String vif : vifs) {
3098-
DomainInterfaceStats ifStats = dm.interfaceStats(vif);
3100+
for (InterfaceDef vif : vifs) {
3101+
DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
30993102
rx += ifStats.rx_bytes;
31003103
tx += ifStats.tx_bytes;
31013104
}
@@ -3145,16 +3148,15 @@ private boolean destroy_network_rules_for_vm(String vmName) {
31453148
return true;
31463149
}
31473150

3148-
private boolean default_network_rules(String vmName, String pubIP, long vmId, String mac) {
3151+
private boolean default_network_rules(String vmName, long vmId, String rules) {
31493152
if (!_can_bridge_firewall) {
31503153
return false;
31513154
}
31523155
Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
31533156
cmd.add("default_network_rules");
31543157
cmd.add("--vmname", vmName);
3155-
cmd.add("--vmip", pubIP);
3158+
cmd.add("--rules", rules);
31563159
cmd.add("--vmid", Long.toString(vmId));
3157-
cmd.add("--vmmac", mac);
31583160

31593161
String result = cmd.execute();
31603162
if (result != null) {

0 commit comments

Comments
 (0)