PVLAN: PvlanSetupCommand for KVM support

This commit is contained in:
Sheng Yang 2013-05-01 13:23:09 -07:00
parent 40386fc4cb
commit 3c3d67769b
6 changed files with 89 additions and 15 deletions

View File

@ -125,6 +125,7 @@ import com.cloud.agent.api.PlugNicAnswer;
import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PlugNicCommand;
import com.cloud.agent.api.PrepareForMigrationAnswer; import com.cloud.agent.api.PrepareForMigrationAnswer;
import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.PrepareForMigrationCommand;
import com.cloud.agent.api.PvlanSetupCommand;
import com.cloud.agent.api.ReadyAnswer; import com.cloud.agent.api.ReadyAnswer;
import com.cloud.agent.api.ReadyCommand; import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.RebootAnswer; import com.cloud.agent.api.RebootAnswer;
@ -267,6 +268,8 @@ ServerResource {
private String _createTmplPath; private String _createTmplPath;
private String _heartBeatPath; private String _heartBeatPath;
private String _securityGroupPath; private String _securityGroupPath;
private String _ovsPvlanDhcpHostPath;
private String _ovsPvlanVmPath;
private String _routerProxyPath; private String _routerProxyPath;
private String _host; private String _host;
private String _dcId; private String _dcId;
@ -587,6 +590,18 @@ ServerResource {
"Unable to find the router_proxy.sh"); "Unable to find the router_proxy.sh");
} }
_ovsPvlanDhcpHostPath = Script.findScript(networkScriptsDir, "ovs-pvlan-dhcp-host.sh");
if ( _ovsPvlanDhcpHostPath == null) {
throw new ConfigurationException(
"Unable to find the ovs-pvlan-dhcp-host.sh");
}
_ovsPvlanVmPath = Script.findScript(networkScriptsDir, "ovs-pvlan-vm.sh");
if ( _ovsPvlanVmPath == null) {
throw new ConfigurationException(
"Unable to find the ovs-pvlan-vm.sh");
}
String value = (String) params.get("developer"); String value = (String) params.get("developer");
boolean isDeveloper = Boolean.parseBoolean(value); boolean isDeveloper = Boolean.parseBoolean(value);
@ -1202,6 +1217,8 @@ ServerResource {
return execute((CheckNetworkCommand) cmd); return execute((CheckNetworkCommand) cmd);
} else if (cmd instanceof NetworkRulesVmSecondaryIpCommand) { } else if (cmd instanceof NetworkRulesVmSecondaryIpCommand) {
return execute((NetworkRulesVmSecondaryIpCommand) cmd); return execute((NetworkRulesVmSecondaryIpCommand) cmd);
} else if (cmd instanceof PvlanSetupCommand) {
return execute((PvlanSetupCommand) cmd);
} else { } else {
s_logger.warn("Unsupported command "); s_logger.warn("Unsupported command ");
return Answer.createUnsupportedCommandAnswer(cmd); return Answer.createUnsupportedCommandAnswer(cmd);
@ -1517,6 +1534,65 @@ ServerResource {
} }
} }
private Answer execute(PvlanSetupCommand cmd) {
String primaryPvlan = cmd.getPrimary();
String isolatedPvlan = cmd.getIsolated();
String op = cmd.getOp();
String dhcpName = cmd.getDhcpName();
String dhcpMac = cmd.getDhcpMac();
String dhcpIp = cmd.getDhcpIp();
String vmMac = cmd.getVmMac();
boolean add = true;
String opr = "-A";
if (op.equals("delete")) {
opr = "-D";
add = false;
}
String result = null;
Connect conn;
try {
if (cmd.getType() == PvlanSetupCommand.Type.DHCP) {
Script script = new Script(_ovsPvlanDhcpHostPath, _timeout, s_logger);
if (add) {
conn = LibvirtConnection.getConnectionByVmName(dhcpName);
List<InterfaceDef> ifaces = getInterfaces(conn, dhcpName);
InterfaceDef guestNic = ifaces.get(0);
script.add(opr, "-b", _guestBridgeName,
"-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName,
"-d", dhcpIp, "-m", dhcpMac, "-I", guestNic.getDevName());
} else {
script.add(opr, "-b", _guestBridgeName,
"-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName,
"-d", dhcpIp, "-m", dhcpMac);
}
result = script.execute();
if (result != null) {
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
return new Answer(cmd, false, result);
} else {
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
}
} else if (cmd.getType() == PvlanSetupCommand.Type.VM) {
Script script = new Script(_ovsPvlanVmPath, _timeout, s_logger);
script.add(opr, "-b", _guestBridgeName,
"-p", primaryPvlan, "-i", isolatedPvlan, "-v", vmMac);
result = script.execute();
if (result != null) {
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
return new Answer(cmd, false, result);
} else {
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
}
}
} catch (LibvirtException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new Answer(cmd, true, result);
}
private void VifHotPlug(Connect conn, String vmName, String vlanId, private void VifHotPlug(Connect conn, String vmName, String vlanId,
String macAddr) throws InternalErrorException, LibvirtException { String macAddr) throws InternalErrorException, LibvirtException {
NicTO nicTO = new NicTO(); NicTO nicTO = new NicTO();

View File

@ -79,7 +79,7 @@ public class OvsVifDriver extends VifDriverBase {
} }
String trafficLabel = nic.getName(); String trafficLabel = nic.getName();
if (nic.getType() == Networks.TrafficType.Guest) { if (nic.getType() == Networks.TrafficType.Guest) {
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan if ((nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan || nic.getBroadcastType() == Networks.BroadcastDomainType.Pvlan)
&& !vlanId.equalsIgnoreCase("untagged")) { && !vlanId.equalsIgnoreCase("untagged")) {
if(trafficLabel != null && !trafficLabel.isEmpty()) { if(trafficLabel != null && !trafficLabel.isEmpty()) {
s_logger.debug("creating a vlan dev and bridge for guest traffic per traffic label " + trafficLabel); s_logger.debug("creating a vlan dev and bridge for guest traffic per traffic label " + trafficLabel);

View File

@ -22,5 +22,4 @@ bridge=$1
dhcp_name=$2 dhcp_name=$2
dom_id=`xe vm-list is-control-domain=false power-state=running params=dom-id name-label=$dhcp_name|cut -d ':' -f 2 |tr -d ' ' ` dom_id=`xe vm-list is-control-domain=false power-state=running params=dom-id name-label=$dhcp_name|cut -d ':' -f 2 |tr -d ' ' `
iface="vif${dom_id}.0" iface="vif${dom_id}.0"
port=`ovs-ofctl show $bridge|grep $iface|cut -d '(' -f 1|tr -d ' '` echo $iface
echo $port

View File

@ -34,7 +34,7 @@ xePath = "/opt/xensource/bin/xe"
lib.setup_logging("/var/log/ovs-pvlan.log") lib.setup_logging("/var/log/ovs-pvlan.log")
dhcpSetupPath = "/opt/xensource/bin/ovs-pvlan-dhcp-host.sh" dhcpSetupPath = "/opt/xensource/bin/ovs-pvlan-dhcp-host.sh"
vmSetupPath = "/opt/xensource/bin/ovs-pvlan-vm.sh" vmSetupPath = "/opt/xensource/bin/ovs-pvlan-vm.sh"
getDhcpPortPath = "/opt/xensource/bin/ovs-get-dhcp-port.sh" getDhcpIfacePath = "/opt/xensource/bin/ovs-get-dhcp-iface.sh"
pvlanCleanupPath = "/opt/xensource/bin/ovs-pvlan-cleanup.sh" pvlanCleanupPath = "/opt/xensource/bin/ovs-pvlan-cleanup.sh"
getBridgePath = "/opt/xensource/bin/ovs-get-bridge.sh" getBridgePath = "/opt/xensource/bin/ovs-get-bridge.sh"
@ -67,11 +67,11 @@ def setup_pvlan_dhcp(session, args):
if op == "add": if op == "add":
logging.debug("Try to get dhcp vm %s port on the switch:%s" % (dhcp_name, bridge)) logging.debug("Try to get dhcp vm %s port on the switch:%s" % (dhcp_name, bridge))
dhcp_port = lib.do_cmd([getDhcpPortPath, bridge, dhcp_name]) dhcp_iface = lib.do_cmd([getDhcpIfacePath, bridge, dhcp_name])
logging.debug("About to setup dhcp vm on the switch:%s" % bridge) logging.debug("About to setup dhcp vm on the switch:%s" % bridge)
res = lib.do_cmd([dhcpSetupPath, "-A", "-b", bridge, "-p", primary, res = lib.do_cmd([dhcpSetupPath, "-A", "-b", bridge, "-p", primary,
"-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac, "-i", isolated, "-n", dhcp_name, "-d", dhcp_ip, "-m", dhcp_mac,
"-P", dhcp_port]) "-I", dhcp_iface])
if res: if res:
result = "FAILURE:%s" % res result = "FAILURE:%s" % res
return result; return result;

View File

@ -71,5 +71,5 @@ ovs-pvlan=..,0755,/etc/xapi.d/plugins
ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/xensource/bin ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/xensource/bin
ovs-pvlan-vm.sh=../../../network,0755,/opt/xensource/bin ovs-pvlan-vm.sh=../../../network,0755,/opt/xensource/bin
ovs-pvlan-cleanup.sh=../../../network,0755,/opt/xensource/bin ovs-pvlan-cleanup.sh=../../../network,0755,/opt/xensource/bin
ovs-get-dhcp-port.sh=..,0755,/opt/xensource/bin ovs-get-dhcp-iface.sh=..,0755,/opt/xensource/bin
ovs-get-bridge.sh=..,0755,/opt/xensource/bin ovs-get-bridge.sh=..,0755,/opt/xensource/bin

View File

@ -18,10 +18,8 @@
#!/bin/bash #!/bin/bash
source ovs-func.sh
usage() { usage() {
printf "Usage: %s: (-A|-D) -b <bridge/switch> -p <primary vlan> -i <secondary isolated vlan> -n <DHCP server name> -d <DHCP server IP> -m <DHCP server MAC> -P <DHCP on OVS port> -v <VM MAC> -h \n" $(basename $0) >&2 printf "Usage: %s: (-A|-D) -b <bridge/switch> -p <primary vlan> -i <secondary isolated vlan> -n <DHCP server name> -d <DHCP server IP> -m <DHCP server MAC> -I <interface> -v <VM MAC> -h \n" $(basename $0) >&2
exit 2 exit 2
} }
@ -31,11 +29,11 @@ sec_iso_vlan=
dhcp_name= dhcp_name=
dhcp_ip= dhcp_ip=
dhcp_mac= dhcp_mac=
dhcp_port=
vm_mac= vm_mac=
iface=
op= op=
while getopts 'ADb:p:i:d:m:v:n:P:h' OPTION while getopts 'ADb:p:i:d:m:v:n:I:h' OPTION
do do
case $OPTION in case $OPTION in
A) op="add" A) op="add"
@ -54,7 +52,7 @@ do
;; ;;
m) dhcp_mac="$OPTARG" m) dhcp_mac="$OPTARG"
;; ;;
P) dhcp_port="$OPTARG" I) iface="$OPTARG"
;; ;;
v) vm_mac="$OPTARG" v) vm_mac="$OPTARG"
;; ;;
@ -106,14 +104,15 @@ then
exit 1 exit 1
fi fi
if [ "$op" == "add" -a -z "$dhcp_port" ] if [ "$op" == "add" -a -z "$iface" ]
then then
echo Missing parameter DHCP PORT! echo Missing parameter DHCP VM interface!
exit 1 exit 1
fi fi
if [ "$op" == "add" ] if [ "$op" == "add" ]
then then
dhcp_port=`ovs-ofctl show $br | grep $iface | cut -d '(' -f 1|tr -d ' '`
ovs-ofctl add-flow $br priority=200,arp,dl_vlan=$sec_iso_vlan,nw_dst=$dhcp_ip,actions=strip_vlan,output:$dhcp_port ovs-ofctl add-flow $br priority=200,arp,dl_vlan=$sec_iso_vlan,nw_dst=$dhcp_ip,actions=strip_vlan,output:$dhcp_port
ovs-ofctl add-flow $br priority=180,arp,nw_dst=$dhcp_ip,actions=strip_vlan,output:$dhcp_port ovs-ofctl add-flow $br priority=180,arp,nw_dst=$dhcp_ip,actions=strip_vlan,output:$dhcp_port
ovs-ofctl add-flow $br priority=150,dl_vlan=$sec_iso_vlan,dl_dst=$dhcp_mac,actions=strip_vlan,output:$dhcp_port ovs-ofctl add-flow $br priority=150,dl_vlan=$sec_iso_vlan,dl_dst=$dhcp_mac,actions=strip_vlan,output:$dhcp_port