Skip to content

Commit 076d2d7

Browse files
committed
Fixing shared network pvlan
1 parent c14dbf6 commit 076d2d7

7 files changed

Lines changed: 240 additions & 189 deletions

File tree

engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ public List<NetworkVO> listByAccountIdNetworkName(final long accountId, final St
740740
* - The requested exact PVLAN pair exists
741741
* - The requested secondary VLAN ID is secondary VLAN ID of an existing PVLAN pair
742742
* - The requested secondary VLAN ID is primary VLAN ID of an existing PVLAN pair
743+
* - The requested primary VLAN ID is secondary VLAN ID of an existing PVLAN pair
743744
*/
744745
protected boolean isNetworkOverlappingRequestedPvlan(Integer existingPrimaryVlan, Integer existingSecondaryVlan, Network.PVlanType existingPvlanType,
745746
Integer requestedPrimaryVlan, Integer requestedSecondaryVlan, Network.PVlanType requestedPvlanType) {
@@ -749,14 +750,15 @@ protected boolean isNetworkOverlappingRequestedPvlan(Integer existingPrimaryVlan
749750
}
750751
boolean exactMatch = existingPrimaryVlan.equals(requestedPrimaryVlan) && existingSecondaryVlan.equals(requestedSecondaryVlan);
751752
boolean secondaryVlanUsed = requestedPvlanType != Network.PVlanType.Promiscuous && requestedSecondaryVlan.equals(existingPrimaryVlan) || requestedSecondaryVlan.equals(existingSecondaryVlan);
753+
boolean primaryVlanUsed = existingPvlanType != Network.PVlanType.Promiscuous && requestedPrimaryVlan.equals(existingSecondaryVlan);
752754
boolean isolatedMax = false;
753755
boolean promiscuousMax = false;
754756
if (requestedPvlanType == Network.PVlanType.Isolated && existingPrimaryVlan.equals(requestedPrimaryVlan) && existingPvlanType.equals(Network.PVlanType.Isolated)) {
755757
isolatedMax = true;
756758
} else if (requestedPvlanType == Network.PVlanType.Promiscuous && existingPrimaryVlan.equals(requestedPrimaryVlan) && existingPvlanType == Network.PVlanType.Promiscuous) {
757759
promiscuousMax = true;
758760
}
759-
return exactMatch || secondaryVlanUsed || isolatedMax || promiscuousMax;
761+
return exactMatch || secondaryVlanUsed || primaryVlanUsed || isolatedMax || promiscuousMax;
760762
}
761763

762764
protected Network.PVlanType getNetworkPvlanType(long networkId, List<Integer> existingPvlan) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,14 +798,14 @@ public boolean configure(final String name, final Map<String, Object> params) th
798798
throw new ConfigurationException("Unable to find the router_proxy.sh");
799799
}
800800

801-
_ovsPvlanDhcpHostPath = Script.findScript(networkScriptsDir, "ovs-pvlan-dhcp-host.sh");
801+
_ovsPvlanDhcpHostPath = Script.findScript(networkScriptsDir, "ovs-pvlan-kvm-dhcp-host.sh");
802802
if (_ovsPvlanDhcpHostPath == null) {
803-
throw new ConfigurationException("Unable to find the ovs-pvlan-dhcp-host.sh");
803+
throw new ConfigurationException("Unable to find the ovs-pvlan-kvm-dhcp-host.sh");
804804
}
805805

806806
_ovsPvlanVmPath = Script.findScript(networkScriptsDir, "ovs-pvlan-kvm-vm.sh");
807807
if (_ovsPvlanVmPath == null) {
808-
throw new ConfigurationException("Unable to find the ovs-pvlan-vm.sh");
808+
throw new ConfigurationException("Unable to find the ovs-pvlan-kvm-vm.sh");
809809
}
810810

811811
String value = (String)params.get("developer");

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

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@
1919

2020
package com.cloud.hypervisor.kvm.resource.wrapper;
2121

22-
import java.util.List;
23-
2422
import org.apache.log4j.Logger;
2523
import org.joda.time.Duration;
26-
import org.libvirt.Connect;
27-
import org.libvirt.LibvirtException;
2824

2925
import com.cloud.agent.api.Answer;
3026
import com.cloud.agent.api.PvlanSetupCommand;
3127
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
32-
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
3328
import com.cloud.resource.CommandWrapper;
3429
import com.cloud.resource.ResourceWrapper;
3530
import com.cloud.utils.script.Script;
3631

37-
@ResourceWrapper(handles = PvlanSetupCommand.class)
32+
@ResourceWrapper(handles = PvlanSetupCommand.class)
3833
public final class LibvirtPvlanSetupCommandWrapper extends CommandWrapper<PvlanSetupCommand, Answer, LibvirtComputingResource> {
3934

4035
private static final Logger s_logger = Logger.getLogger(LibvirtPvlanSetupCommandWrapper.class);
@@ -45,65 +40,48 @@ public Answer execute(final PvlanSetupCommand command, final LibvirtComputingRes
4540
final String isolatedPvlan = command.getIsolated();
4641
final String pvlanType = "-" + command.getPvlanType();
4742
final String op = command.getOp();
48-
final String dhcpName = command.getDhcpName();
4943
final String dhcpMac = command.getDhcpMac();
50-
final String vmMac = command.getVmMac();
44+
final String vmMac = command.getVmMac() == null ? dhcpMac : command.getVmMac();
5145
final String dhcpIp = command.getDhcpIp();
52-
boolean add = true;
5346

5447
String opr = "-A";
5548
if (op.equals("delete")) {
5649
opr = "-D";
57-
add = false;
5850
}
5951

6052
String result = null;
61-
try {
62-
final String guestBridgeName = libvirtComputingResource.getGuestBridgeName();
63-
final Duration timeout = libvirtComputingResource.getTimeout();
64-
65-
if (command.getType() == PvlanSetupCommand.Type.DHCP) {
66-
final String ovsPvlanDhcpHostPath = libvirtComputingResource.getOvsPvlanDhcpHostPath();
67-
final Script script = new Script(ovsPvlanDhcpHostPath, timeout, s_logger);
68-
69-
if (add) {
70-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
71-
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(dhcpName);
72-
73-
final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, dhcpName);
74-
final InterfaceDef guestNic = ifaces.get(0);
75-
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac, "-I",
76-
guestNic.getDevName());
77-
} else {
78-
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac);
79-
}
8053

81-
result = script.execute();
54+
final String guestBridgeName = libvirtComputingResource.getGuestBridgeName();
55+
final Duration timeout = libvirtComputingResource.getTimeout();
8256

83-
if (result != null) {
84-
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
85-
return new Answer(command, false, result);
86-
} else {
87-
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
88-
}
89-
} else if (command.getType() == PvlanSetupCommand.Type.VM) {
90-
final String ovsPvlanVmPath = libvirtComputingResource.getOvsPvlanVmPath();
57+
if (command.getType() == PvlanSetupCommand.Type.DHCP) {
58+
final String ovsPvlanDhcpHostPath = libvirtComputingResource.getOvsPvlanDhcpHostPath();
59+
final Script script = new Script(ovsPvlanDhcpHostPath, timeout, s_logger);
9160

92-
final Script script = new Script(ovsPvlanVmPath, timeout, s_logger);
93-
script.add(opr, pvlanType, "-b", guestBridgeName, "-p", primaryPvlan, "-s", isolatedPvlan, "-v", vmMac);
94-
result = script.execute();
61+
script.add(opr, pvlanType, "-b", guestBridgeName, "-p", primaryPvlan, "-s", isolatedPvlan, "-m", dhcpMac,
62+
"-d", dhcpIp);
63+
result = script.execute();
9564

96-
if (result != null) {
97-
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
98-
return new Answer(command, false, result);
99-
} else {
100-
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
101-
}
65+
if (result != null) {
66+
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
67+
} else {
68+
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
10269
}
103-
} catch (final LibvirtException e) {
104-
s_logger.error("Error whislt executing OVS Setup command! ==> " + e.getMessage());
105-
return new Answer(command, false, e.getMessage());
10670
}
71+
72+
// We run this even for DHCP servers since they're all vms after all
73+
final String ovsPvlanVmPath = libvirtComputingResource.getOvsPvlanVmPath();
74+
final Script script = new Script(ovsPvlanVmPath, timeout, s_logger);
75+
script.add(opr, pvlanType, "-b", guestBridgeName, "-p", primaryPvlan, "-s", isolatedPvlan, "-m", vmMac);
76+
result = script.execute();
77+
78+
if (result != null) {
79+
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
80+
return new Answer(command, false, result);
81+
} else {
82+
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
83+
}
84+
10785
return new Answer(command, true, result);
10886
}
10987
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4336,48 +4336,26 @@ public void testCopyVolumeCommandPrimaryNotFound() {
43364336
@Test
43374337
public void testPvlanSetupCommandDhcpAdd() {
43384338
final String op = "add";
4339-
final URI uri = URI.create("http://localhost");
4339+
final URI uri = URI.create("pvlan://200-p200");
43404340
final String networkTag = "/105";
43414341
final String dhcpName = "dhcp";
43424342
final String dhcpMac = "00:00:00:00";
43434343
final String dhcpIp = "127.0.0.1";
43444344

43454345
final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
43464346

4347-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
4348-
final Connect conn = Mockito.mock(Connect.class);
4349-
43504347
final String guestBridgeName = "br0";
43514348
when(libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
4352-
43534349
when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
4350+
43544351
final String ovsPvlanDhcpHostPath = "/pvlan";
43554352
when(libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
4356-
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4357-
4358-
final List<InterfaceDef> ifaces = new ArrayList<InterfaceDef>();
4359-
final InterfaceDef nic = Mockito.mock(InterfaceDef.class);
4360-
ifaces.add(nic);
4361-
4362-
try {
4363-
when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenReturn(conn);
4364-
when(libvirtComputingResource.getInterfaces(conn, dhcpName)).thenReturn(ifaces);
4365-
} catch (final LibvirtException e) {
4366-
fail(e.getMessage());
4367-
}
43684353

43694354
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
43704355
assertNotNull(wrapper);
43714356

43724357
final Answer answer = wrapper.execute(command, libvirtComputingResource);
43734358
assertFalse(answer.getResult());
4374-
4375-
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
4376-
try {
4377-
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(dhcpName);
4378-
} catch (final LibvirtException e) {
4379-
fail(e.getMessage());
4380-
}
43814359
}
43824360

43834361
@Test
@@ -4403,68 +4381,23 @@ public void testPvlanSetupCommandVm() {
44034381
assertFalse(answer.getResult());
44044382
}
44054383

4406-
@SuppressWarnings("unchecked")
4407-
@Test
4408-
public void testPvlanSetupCommandDhcpException() {
4409-
final String op = "add";
4410-
final URI uri = URI.create("http://localhost");
4411-
final String networkTag = "/105";
4412-
final String dhcpName = "dhcp";
4413-
final String dhcpMac = "00:00:00:00";
4414-
final String dhcpIp = "127.0.0.1";
4415-
4416-
final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
4417-
4418-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
4419-
4420-
final String guestBridgeName = "br0";
4421-
when(libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
4422-
4423-
when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
4424-
final String ovsPvlanDhcpHostPath = "/pvlan";
4425-
when(libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
4426-
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
4427-
4428-
try {
4429-
when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenThrow(LibvirtException.class);
4430-
} catch (final LibvirtException e) {
4431-
fail(e.getMessage());
4432-
}
4433-
4434-
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
4435-
assertNotNull(wrapper);
4436-
4437-
final Answer answer = wrapper.execute(command, libvirtComputingResource);
4438-
assertFalse(answer.getResult());
4439-
4440-
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
4441-
try {
4442-
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(dhcpName);
4443-
} catch (final LibvirtException e) {
4444-
fail(e.getMessage());
4445-
}
4446-
}
4447-
44484384
@Test
44494385
public void testPvlanSetupCommandDhcpDelete() {
44504386
final String op = "delete";
4451-
final URI uri = URI.create("http://localhost");
4387+
final URI uri = URI.create("pvlan://200-p200");
44524388
final String networkTag = "/105";
44534389
final String dhcpName = "dhcp";
44544390
final String dhcpMac = "00:00:00:00";
44554391
final String dhcpIp = "127.0.0.1";
44564392

44574393
final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
44584394

4459-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
4460-
44614395
final String guestBridgeName = "br0";
44624396
when(libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
4463-
44644397
when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
4398+
44654399
final String ovsPvlanDhcpHostPath = "/pvlan";
44664400
when(libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
4467-
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
44684401

44694402
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
44704403
assertNotNull(wrapper);

0 commit comments

Comments
 (0)