CLOUDSTACK-6183: unplug the nic when all the ips of public vlan range is removed

(cherry picked from commit 7700a1b716)

Signed-off-by: Animesh Chaturvedi <animesh@apache.org>
This commit is contained in:
Jayapal 2014-02-27 18:39:37 +05:30 committed by Animesh Chaturvedi
parent 9e1525c032
commit 86643d6e3f
2 changed files with 44 additions and 2 deletions

View File

@ -1789,6 +1789,23 @@ ServerResource {
return new Answer(cmd, true, result);
}
private void vifHotUnPlug (Connect conn, String vmName, String macAddr) throws InternalErrorException, LibvirtException {
Domain vm = null;
vm = getDomain(conn, vmName);
List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
for (InterfaceDef pluggedNic : pluggedNics) {
if (pluggedNic.getMacAddress().equalsIgnoreCase(macAddr)) {
vm.detachDevice(pluggedNic.toString());
// We don't know which "traffic type" is associated with
// each interface at this point, so inform all vif drivers
for (VifDriver vifDriver : getAllVifDrivers()) {
vifDriver.unplug(pluggedNic);
}
}
}
}
private void VifHotPlug(Connect conn, String vmName, String broadcastUri,
String macAddr) throws InternalErrorException, LibvirtException {
NicTO nicTO = new NicTO();
@ -2103,6 +2120,12 @@ ServerResource {
String result = null;
int nicNum = 0;
boolean newNic = false;
int numOfIps = 0;
if (ips != null) {
numOfIps = ips.length;
}
for (IpAddressTO ip : ips) {
if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
/* plug a vif into router */
@ -2121,6 +2144,14 @@ ServerResource {
if (result == null) {
results[i++] = ip.getPublicIp() + " - success";
}
//there is only only ip in public subnet and it is deleted so unplug the vif
if (numOfIps == 1 && !ip.isAdd()) {
// There are no ips on the vm so delete the vif
networkUsage(routerIp, "deleteVif", "eth" + nicNum);
vifHotUnPlug(conn, routerName, ip.getVifMacAddress());
}
}
return new IpAssocAnswer(cmd, results);
} catch (LibvirtException e) {

View File

@ -2383,7 +2383,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
protected void assignPublicIpAddress(Connection conn, String vmName, String privateIpAddress, String publicIpAddress, boolean add, boolean firstIP,
boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, Integer networkRate, TrafficType trafficType, String name) throws InternalErrorException {
boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, Integer networkRate, TrafficType trafficType, String name,
int numOfips) throws InternalErrorException {
try {
VM router = getVM(conn, vmName);
@ -2474,6 +2475,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
throw new InternalErrorException("Xen plugin \"ipassoc\" failed.");
}
//there is only only ip in public subnet and it is deleted so unplug the vif
if (numOfips == 1 && !add) {
removeVif = true;
}
if (removeVif) {
network = correctVif.getNetwork(conn);
@ -2575,14 +2581,19 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
Connection conn = getConnection();
String[] results = new String[cmd.getIpAddresses().length];
int i = 0;
int numOfIps = 0;
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
try {
IpAddressTO[] ips = cmd.getIpAddresses();
if (ips != null) {
numOfIps = ips.length;
}
for (IpAddressTO ip : ips) {
assignPublicIpAddress(conn, routerName, routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getBroadcastUri(),
ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getNetworkRate(), ip.getTrafficType(), ip.getNetworkName());
ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getNetworkRate(), ip.getTrafficType(), ip.getNetworkName(), numOfIps);
results[i++] = ip.getPublicIp() + " - success";
}
} catch (InternalErrorException e) {