Skip to content

Commit b64039b

Browse files
author
Sheng Yang
committed
Implement PVLAN on Xen
Start/stop vm/dhcp server are done. Not done with VM migration. A new command(PvlanSetupCommand) is sent for setting up PVLAN for vms. Currently it's focus on OVS implementation. Need to be more abstruct and add vSwitch part.
1 parent 9c9e2ec commit b64039b

13 files changed

Lines changed: 804 additions & 5 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.agent.api;
18+
19+
import java.net.URI;
20+
21+
import com.cloud.utils.net.NetUtils;
22+
23+
public class PvlanSetupCommand extends Command {
24+
public enum Type {
25+
DHCP,
26+
VM,
27+
VM_IN_DHCP_HOST
28+
}
29+
private String op;
30+
private String bridge;
31+
private String primary;
32+
private String isolated;
33+
private String vmMac;
34+
private String dhcpMac;
35+
private String dhcpIp;
36+
private boolean strict;
37+
private Type type;
38+
39+
protected PvlanSetupCommand() {}
40+
41+
protected PvlanSetupCommand(Type type, String op, String bridge, URI uri)
42+
{
43+
this.type = type;
44+
this.op = op;
45+
this.bridge = bridge;
46+
this.primary = NetUtils.getPrimaryPvlanFromUri(uri);
47+
this.isolated = NetUtils.getIsolatedPvlanFromUri(uri);
48+
this.strict = true;
49+
}
50+
51+
static public PvlanSetupCommand createDhcpSetup(String op, String bridge, URI uri, String dhcpMac, String dhcpIp)
52+
{
53+
PvlanSetupCommand cmd = new PvlanSetupCommand(Type.DHCP, op, bridge, uri);
54+
cmd.setDhcpMac(dhcpMac);
55+
cmd.setDhcpIp(dhcpIp);
56+
return cmd;
57+
}
58+
59+
static public PvlanSetupCommand createVmSetup(String op, String bridge, URI uri, String vmMac)
60+
{
61+
PvlanSetupCommand cmd = new PvlanSetupCommand(Type.VM, op, bridge, uri);
62+
cmd.setVmMac(vmMac);
63+
return cmd;
64+
}
65+
66+
static public PvlanSetupCommand createVmInDhcpHostSetup(String op, String bridge, URI uri, String dhcpMac, String vmMac)
67+
{
68+
PvlanSetupCommand cmd = new PvlanSetupCommand(Type.VM_IN_DHCP_HOST, op, bridge, uri);
69+
cmd.setDhcpMac(dhcpMac);
70+
cmd.setVmMac(vmMac);
71+
return cmd;
72+
}
73+
74+
@Override
75+
public boolean executeInSequence() {
76+
return true;
77+
}
78+
79+
public String getOp() {
80+
return op;
81+
}
82+
83+
public String getBridge() {
84+
return bridge;
85+
}
86+
87+
public String getPrimary() {
88+
return primary;
89+
}
90+
91+
public String getIsolated() {
92+
return isolated;
93+
}
94+
95+
public String getVmMac() {
96+
return vmMac;
97+
}
98+
99+
protected void setVmMac(String vmMac) {
100+
this.vmMac = vmMac;
101+
}
102+
103+
public String getDhcpMac() {
104+
return dhcpMac;
105+
}
106+
107+
protected void setDhcpMac(String dhcpMac) {
108+
this.dhcpMac = dhcpMac;
109+
}
110+
111+
public String getDhcpIp() {
112+
return dhcpIp;
113+
}
114+
115+
protected void setDhcpIp(String dhcpIp) {
116+
this.dhcpIp = dhcpIp;
117+
}
118+
119+
public Type getType() {
120+
return type;
121+
}
122+
123+
public boolean isStrict() {
124+
return strict;
125+
}
126+
127+
public void setStrict(boolean strict) {
128+
this.strict = strict;
129+
}
130+
}

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ public Answer executeRequest(Command cmd) {
604604
return execute((NetworkRulesVmSecondaryIpCommand)cmd);
605605
} else if (clazz == ScaleVmCommand.class) {
606606
return execute((ScaleVmCommand) cmd);
607+
} else if (clazz == PvlanSetupCommand.class) {
608+
return execute((PvlanSetupCommand) cmd);
607609
} else {
608610
return Answer.createUnsupportedCommandAnswer(cmd);
609611
}
@@ -1054,7 +1056,7 @@ protected VIF createVif(Connection conn, String vmName, VM vm, NicTO nic) throws
10541056
vifr = vif.getRecord(conn);
10551057
s_logger.debug("Created a vif " + vifr.uuid + " on " + nic.getDeviceId());
10561058
}
1057-
1059+
10581060
return vif;
10591061
}
10601062

@@ -1465,6 +1467,48 @@ private void cleanUpTmpDomVif(Connection conn) {
14651467
}
14661468
}
14671469
}
1470+
1471+
private Answer execute(PvlanSetupCommand cmd) {
1472+
Connection conn = getConnection();
1473+
1474+
String primaryPvlan = cmd.getPrimary();
1475+
String isolatedPvlan = cmd.getIsolated();
1476+
String op = cmd.getOp();
1477+
String bridge = cmd.getBridge();
1478+
String result = null;
1479+
String dhcpMac = cmd.getDhcpMac();
1480+
String dhcpIp = cmd.getDhcpIp();
1481+
String vmMac = cmd.getVmMac();
1482+
if (cmd.getType() == PvlanSetupCommand.Type.DHCP) {
1483+
result = callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "bridge", bridge,
1484+
"primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac);
1485+
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
1486+
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
1487+
return new Answer(cmd, false, result);
1488+
} else {
1489+
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
1490+
}
1491+
} else if (cmd.getType() == PvlanSetupCommand.Type.VM) {
1492+
result = callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm-alone", "op", op, "bridge", bridge,
1493+
"primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac);
1494+
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
1495+
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
1496+
return new Answer(cmd, false, result);
1497+
} else {
1498+
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
1499+
}
1500+
} else if (cmd.getType() == PvlanSetupCommand.Type.VM_IN_DHCP_HOST) {
1501+
result = callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm-dhcp", "op", op, "bridge", bridge,
1502+
"primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac, "dhcp-mac", dhcpMac);
1503+
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
1504+
s_logger.warn("Failed to program pvlan for vm in dhcp host with mac " + vmMac);
1505+
return new Answer(cmd, false, result);
1506+
} else {
1507+
s_logger.info("Programmed pvlan for vm in dhcp host with mac " + vmMac);
1508+
}
1509+
}
1510+
return new Answer(cmd, true, result);
1511+
}
14681512

14691513
@Override
14701514
public StartAnswer execute(StartCommand cmd) {
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/python
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
20+
import cloudstack_pluginlib as lib
21+
import logging
22+
import os
23+
import sys
24+
import subprocess
25+
import time
26+
import XenAPIPlugin
27+
28+
sys.path.append("/opt/xensource/sm/")
29+
import util
30+
31+
from time import localtime as _localtime, asctime as _asctime
32+
33+
xePath = "/opt/xensource/bin/xe"
34+
lib.setup_logging("/var/log/ovs-pvlan.log")
35+
dhcpSetupPath = "/opt/xensource/bin/ovs-pvlan-dhcp-host.sh"
36+
vmAloneSetupPath = "/opt/xensource/bin/ovs-pvlan-vm.sh"
37+
vmDhcpSetupPath = "/opt/xensource/bin/ovs-pvlan-vm-in-dhcp-host.sh"
38+
pvlanCleanupPath = "/opt/xensource/bin/ovs-pvlan-cleanup.sh"
39+
40+
def echo(fn):
41+
def wrapped(*v, **k):
42+
name = fn.__name__
43+
util.SMlog("#### VMOPS enter %s ####" % name)
44+
res = fn(*v, **k)
45+
util.SMlog("#### VMOPS exit %s ####" % name)
46+
return res
47+
return wrapped
48+
49+
50+
@echo
51+
def setup_pvlan_dhcp(session, args):
52+
op = args.pop("op")
53+
bridge = args.pop("bridge")
54+
primary = args.pop("primary-pvlan")
55+
isolated = args.pop("isolated-pvlan")
56+
dhcp_ip = args.pop("dhcp-ip");
57+
dhcp_mac = args.pop("dhcp-mac");
58+
59+
res = lib.check_switch()
60+
if res != "SUCCESS":
61+
return "FAILURE:%s" % res
62+
63+
if op == "add":
64+
logging.debug("About to setup dhcp vm on the switch:%s" % bridge)
65+
res = lib.do_cmd([dhcpSetupPath, "-A", "-b", bridge, "-p", primary,
66+
"-i", isolated, "-d", dhcp_ip, "-m", dhcp_mac])
67+
if res:
68+
result = "FAILURE:%s" % res
69+
return result;
70+
logging.debug("Setup dhcp vm on switch program done")
71+
elif op == "delete":
72+
logging.debug("About to remove dhcp the switch:%s" % bridge)
73+
res = lib.do_cmd([dhcpSetupPath, "-D", "-b", bridge, "-p", primary,
74+
"-i", isolated, "-d", dhcp_ip, "-m", dhcp_mac])
75+
if res:
76+
result = "FAILURE:%s" % res
77+
return result;
78+
logging.debug("Remove DHCP on switch program done")
79+
80+
result = "true"
81+
logging.debug("Setup_pvlan_dhcp completed with result:%s" % result)
82+
return result
83+
84+
@echo
85+
def setup_pvlan_vm_alone(session, args):
86+
op = args.pop("op")
87+
bridge = args.pop("bridge")
88+
isolated = args.pop("isolated-pvlan")
89+
vm_mac = args.pop("vm-mac")
90+
trunk_port = 1
91+
92+
res = lib.check_switch()
93+
if res != "SUCCESS":
94+
return "FAILURE:%s" % res
95+
96+
if op == "add":
97+
logging.debug("About to setup vm alone on the switch:%s" % bridge)
98+
res = lib.do_cmd([vmAloneSetupPath, "-A", "-b", bridge, "-i", isolated, "-v", vm_mac])
99+
if res:
100+
result = "FAILURE:%s" % res
101+
return result;
102+
logging.debug("Setup vm alone on switch program done")
103+
elif op == "delete":
104+
logging.debug("About to remove vm alone on the switch:%s" % bridge)
105+
res = lib.do_cmd([vmAloneSetupPath, "-D", "-b", bridge, "-i", isolated, "-v", vm_mac])
106+
if res:
107+
result = "FAILURE:%s" % res
108+
return result;
109+
logging.debug("Remove vm alone on switch program done")
110+
111+
result = "true"
112+
logging.debug("Setup_pvlan_vm_alone completed with result:%s" % result)
113+
return result
114+
115+
@echo
116+
def setup_pvlan_vm_dhcp(session, args):
117+
op = args.pop("op")
118+
bridge = args.pop("bridge")
119+
isolated = args.pop("isolated-pvlan")
120+
vm_mac = args.pop("vm-mac")
121+
dhcp_mac = args.pop("dhcp-mac");
122+
trunk_port = 1
123+
124+
res = lib.check_switch()
125+
if res != "SUCCESS":
126+
return "FAILURE:%s" % res
127+
128+
if op == "add":
129+
logging.debug("About to setup vm dhcp on the switch:%s" % bridge)
130+
res = lib.do_cmd([vmDhcpSetupPath, "-A", "-b", bridge, "-i", isolated,
131+
"-v", vm_mac, "-m", dhcp_mac])
132+
if res:
133+
result = "FAILURE:%s" % res
134+
return result;
135+
logging.debug("Setup vm dhcp on switch program done")
136+
elif op == "delete":
137+
logging.debug("About to remove vm dhcp on the switch:%s" % bridge)
138+
res = lib.do_cmd([vmDhcpSetupPath, "-D", "-b", bridge, "-i", isolated,
139+
"-v", vm_mac, "-m", dhcp_mac])
140+
if res:
141+
result = "FAILURE:%s" % res
142+
return result;
143+
logging.debug("Remove vm dhcp on switch program done")
144+
145+
result = "true"
146+
logging.debug("Setup_pvlan_vm_dhcp completed with result:%s" % result)
147+
return result
148+
149+
@echo
150+
def cleanup(session, args):
151+
res = lib.check_switch()
152+
if res != "SUCCESS":
153+
return "FAILURE:%s" % res
154+
155+
res = lib.do_cmd([pvlanCleanUpPath])
156+
if res:
157+
result = "FAILURE:%s" % res
158+
return result;
159+
160+
result = "true"
161+
logging.debug("Setup_pvlan_vm_dhcp completed with result:%s" % result)
162+
return result
163+
164+
if __name__ == "__main__":
165+
XenAPIPlugin.dispatch({"setup-pvlan-dhcp": setup_pvlan_dhcp,
166+
"setup-pvlan-vm-alone": setup_pvlan_vm_alone,
167+
"setup-pvlan-vm-dhcp": setup_pvlan_vm_dhcp,
168+
"cleanup":cleanup})

‎scripts/vm/hypervisor/xenserver/xenserver60/patch‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ bumpUpPriority.sh=../../../../network/domr/,0755,/opt/xensource/bin
6767
swift=..,0755,/opt/xensource/bin
6868
swiftxen=..,0755,/etc/xapi.d/plugins
6969
s3xen=..,0755,/etc/xapi.d/plugins
70-
70+
ovs-pvlan=..,0755,/etc/xapi.d/plugins
71+
ovs-pvlan-dhcp-host.sh=../../../network,0755,/opt/xensource/bin
72+
ovs-pvlan-vm-in-dhcp-host.sh=../../../network,0755,/opt/xensource/bin
73+
ovs-pvlan-vm.sh=../../../network,0755,/opt/xensource/bin
74+
ovs-pvlan-cleanup.sh=../../../network,0755,/opt/xensource/bin

0 commit comments

Comments
 (0)