2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Authors: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Alexandros Kouvakas <alexandros.kouvakas@inria.fr>
20 # Julien Tribino <julien.tribino@inria.fr>
23 from nepi.execution.resource import ResourceManager, clsinit_copy, \
25 from nepi.execution.attribute import Attribute, Flags
26 from nepi.resources.planetlab.node import PlanetlabNode
27 from nepi.resources.linux.application import LinuxApplication
30 reschedule_delay = "0.5s"
33 class OVSSwitch(LinuxApplication):
35 .. class:: Class Args :
37 :param ec: The Experiment controller
38 :type ec: ExperimentController
39 :param guid: guid of the RM
45 _help = "Runs an OpenVSwitch on a PlanetLab host"
46 _backend = "planetlab"
48 _authorized_connections = ["PlanetlabNode", "OVSPort", "LinuxNode"]
51 def _register_attributes(cls):
52 """ Register the attributes of OVSSwitch RM
55 bridge_name = Attribute("bridge_name", "Name of the switch/bridge",
57 virtual_ip_pref = Attribute("virtual_ip_pref", "Virtual IP/PREFIX of the switch",
59 controller_ip = Attribute("controller_ip", "IP of the controller",
61 controller_port = Attribute("controller_port", "Port of the controller",
64 cls._register_attribute(bridge_name)
65 cls._register_attribute(virtual_ip_pref)
66 cls._register_attribute(controller_ip)
67 cls._register_attribute(controller_port)
69 def __init__(self, ec, guid):
71 :param ec: The Experiment controller
72 :type ec: ExperimentController
73 :param guid: guid of the RM
77 super(OVSSwitch, self).__init__(ec, guid)
78 self._home = "ovsswitch-%s" % self.guid
79 self._checks = "ovsChecks-%s" % self.guid
83 """ Node wthat run the switch
85 node = self.get_connected(PlanetlabNode.get_rtype())
86 if node: return node[0]
89 def log_message(self, msg):
90 return " guid %d - OVSSwitch - %s " % (self.guid, msg)
94 return os.path.join(self.node.exp_home, self._home)
98 return os.path.join(self.ovs_home, self._checks)
100 def valid_connection(self, guid):
101 """ Check if the connection with the guid in parameter is possible. Only meaningful connections are allowed.
103 :param guid: Guid of the current RM
108 rm = self.ec.get_resource(guid)
109 if rm.get_rtype() in self._authorized_connections:
110 msg = "Connection between %s %s and %s %s accepted" % \
111 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
114 msg = "Connection between %s %s and %s %s refused" % \
115 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
119 def do_provision(self):
120 """ Create the different OVS folder.
123 # create home dir for ovs
124 self.node.mkdir(self.ovs_home)
125 # create dir for ovs checks
126 self.node.mkdir(self.ovs_checks)
128 super(OVSSwitch, self).do_provision()
131 """ Deploy the OVS Switch : Turn on the server, create the bridges
132 and assign the controller
135 if not self.node or self.node.state < ResourceState.READY:
136 self.ec.schedule(reschedule_delay, self.deploy)
142 self.check_sliver_ovs()
145 self.assign_controller()
148 super(OVSSwitch, self).do_deploy()
150 def check_sliver_ovs(self):
151 """ Check if sliver-ovs exists. If it does not exist, the execution is stopped
154 cmd = "compgen -c | grep sliver-ovs"
157 (out,err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
158 shfile = "check_cmd.sh",
159 pidfile = "check_cmd_pidfile",
160 ecodefile = "check_cmd_exitcode",
162 stdout = "check_cmd_stdout",
163 stderr = "check_cmd_stderr")
165 (out, err), proc = self.node.check_output(self.ovs_checks, 'check_cmd_exitcode')
168 msg = "Command sliver-ovs does not exist on the VM"
170 raise RuntimeError, msg
172 msg = "Command sliver-ovs exists"
175 def servers_on(self):
176 """ Start the openvswitch servers and check it
180 command = "sliver-ovs start"
182 (out, err), proc = self.node.run_and_wait(command, self.ovs_checks,
183 shfile = "start_srv.sh",
184 pidfile = "start_srv_pidfile",
185 ecodefile = "start_srv_exitcode",
187 raise_on_error = True,
188 stdout = "start_srv_stdout",
189 stderr = "start_srv_stderr")
190 (out, err), proc = self.node.check_output(self.ovs_checks, 'start_srv_exitcode')
193 self.error("Servers have not started")
194 raise RuntimeError, msg
196 # Check if the servers are running or not
197 cmd = "ps -A | grep ovsdb-server"
199 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
200 shfile = "status_srv.sh",
201 pidfile = "status_srv_pidfile",
202 ecodefile = "status_srv_exitcode",
204 stdout = "status_srv_stdout",
205 stderr = "status_srv_stderr")
206 (out, err), proc = self.node.check_output(self.ovs_checks, 'status_srv_exitcode')
209 msg = "Servers are not running"
211 raise RuntimeError, msg
213 self.info("Server OVS Started Correctly")
215 def create_bridge(self):
216 """ Create the bridge/switch and check error during SSH connection
218 # TODO: Check if previous bridge exist and delete them. Use ovs-vsctl list-br
219 # TODO: Add check for virtual_ip belonging to vsys_tag
222 if not (self.get("bridge_name") and self.get("virtual_ip_pref")):
223 msg = "No assignment in one or both attributes"
225 raise AttributeError, msg
227 cmd = "sliver-ovs create-bridge '%s' '%s'" %\
228 (self.get("bridge_name"), self.get("virtual_ip_pref"))
230 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
231 shfile = "create_br.sh",
232 pidfile = "create_br_pidfile",
233 ecodefile = "create_br_exitcode",
235 stdout = "create_br_stdout",
236 stderr = "create_br_stderr")
237 (out, err), proc = self.node.check_output(self.ovs_checks, 'create_br_exitcode')
240 msg = "No such pltap netdev\novs-appctl: ovs-vswitchd: server returned an error"
242 raise RuntimeError, msg
244 self.info(" Bridge %s Created and Assigned to %s" %\
245 (self.get("bridge_name"), self.get("virtual_ip_pref")) )
248 def assign_controller(self):
249 """ Set the controller IP
252 if not (self.get("controller_ip") and self.get("controller_port")):
253 msg = "No assignment in one or both attributes"
255 raise AttributeError, msg
257 cmd = "ovs-vsctl set-controller %s tcp:%s:%s" %\
258 (self.get("bridge_name"), self.get("controller_ip"), self.get("controller_port"))
260 (out, err), proc = self.node.run(cmd, self.ovs_checks,
266 msg = "SSH connection in the method assign_controller"
268 raise RuntimeError, msg
270 self.info("Controller assigned to the bridge %s" % self.get("bridge_name"))
272 def ovs_status(self):
273 """ Print the status of the bridge
276 cmd = "sliver-ovs show | tail -n +2"
278 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_home,
280 stdout = "show_stdout",
281 stderr = "show_stderr")
282 (out, err), proc = self.node.check_output(self.ovs_home, 'show_stdout')
285 msg = "Error when checking the status of the OpenVswitch"
287 raise RuntimeError, msg
291 def do_release(self):
292 """ Delete the bridge and close the server.
294 .. note : It need to wait for the others RM (OVSPort and OVSTunnel)
295 to be released before releasing itself
299 from nepi.resources.planetlab.openvswitch.ovsport import OVSPort
300 rms = self.get_connected(OVSPort.get_rtype())
303 if rm.state < ResourceState.RELEASED:
304 self.ec.schedule(reschedule_delay, self.release)
307 cmd = "sliver-ovs del-bridge %s" % self.get('bridge_name')
308 (out, err), proc = self.node.run(cmd, self.ovs_checks,
311 cmd = "sliver-ovs stop"
312 (out, err), proc = self.node.run(cmd, self.ovs_checks,
315 msg = "Deleting the bridge %s" % self.get('bridge_name')
319 self.error(msg, out, err)
320 raise RuntimeError, msg
322 super(OVSSwitch, self).do_release()