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
31 class OVSSwitch(LinuxApplication):
33 .. class:: Class Args :
35 :param ec: The Experiment controller
36 :type ec: ExperimentController
37 :param guid: guid of the RM
43 _help = "Runs an OpenVSwitch on a PlanetLab host"
44 _backend = "planetlab"
46 _authorized_connections = ["PlanetlabNode", "OVSPort", "LinuxNode"]
49 def _register_attributes(cls):
50 """ Register the attributes of OVSSwitch RM
53 bridge_name = Attribute("bridge_name", "Name of the switch/bridge",
55 virtual_ip_pref = Attribute("virtual_ip_pref", "Virtual IP/PREFIX of the switch",
57 controller_ip = Attribute("controller_ip", "IP of the controller",
59 controller_port = Attribute("controller_port", "Port of the controller",
62 cls._register_attribute(bridge_name)
63 cls._register_attribute(virtual_ip_pref)
64 cls._register_attribute(controller_ip)
65 cls._register_attribute(controller_port)
67 def __init__(self, ec, guid):
69 :param ec: The Experiment controller
70 :type ec: ExperimentController
71 :param guid: guid of the RM
75 super(OVSSwitch, self).__init__(ec, guid)
76 self._home = "ovsswitch-%s" % self.guid
77 self._checks = "ovsChecks-%s" % self.guid
81 """ Node wthat run the switch
83 node = self.get_connected(PlanetlabNode.get_rtype())
84 if node: return node[0]
87 def log_message(self, msg):
88 return " guid %d - OVSSwitch - %s " % (self.guid, msg)
92 return os.path.join(self.node.exp_home, self._home)
96 return os.path.join(self.ovs_home, self._checks)
98 def valid_connection(self, guid):
99 """ Check if the connection with the guid in parameter is possible. Only meaningful connections are allowed.
101 :param guid: Guid of the current RM
106 rm = self.ec.get_resource(guid)
107 if rm.get_rtype() in self._authorized_connections:
108 msg = "Connection between %s %s and %s %s accepted" % \
109 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
112 msg = "Connection between %s %s and %s %s refused" % \
113 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
117 def do_provision(self):
118 """ Create the different OVS folder.
121 # create home dir for ovs
122 self.node.mkdir(self.ovs_home)
123 # create dir for ovs checks
124 self.node.mkdir(self.ovs_checks)
126 super(OVSSwitch, self).do_provision()
129 """ Deploy the OVS Switch : Turn on the server, create the bridges
130 and assign the controller
133 if not self.node or self.node.state < ResourceState.READY:
134 self.ec.schedule(self.reschedule_delay, self.deploy)
140 self.check_sliver_ovs()
143 self.assign_controller()
146 super(OVSSwitch, self).do_deploy()
148 def check_sliver_ovs(self):
149 """ Check if sliver-ovs exists. If it does not exist, the execution is stopped
152 cmd = "compgen -c | grep sliver-ovs"
155 (out,err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
156 shfile = "check_cmd.sh",
157 pidfile = "check_cmd_pidfile",
158 ecodefile = "check_cmd_exitcode",
160 stdout = "check_cmd_stdout",
161 stderr = "check_cmd_stderr")
163 (out, err), proc = self.node.check_output(self.ovs_checks, 'check_cmd_exitcode')
166 msg = "Command sliver-ovs does not exist on the VM"
168 raise RuntimeError, msg
170 msg = "Command sliver-ovs exists"
173 def servers_on(self):
174 """ Start the openvswitch servers and check it
178 command = "sliver-ovs start"
180 (out, err), proc = self.node.run_and_wait(command, self.ovs_checks,
181 shfile = "start_srv.sh",
182 pidfile = "start_srv_pidfile",
183 ecodefile = "start_srv_exitcode",
185 raise_on_error = True,
186 stdout = "start_srv_stdout",
187 stderr = "start_srv_stderr")
188 (out, err), proc = self.node.check_output(self.ovs_checks, 'start_srv_exitcode')
191 self.error("Servers have not started")
192 raise RuntimeError, msg
194 # Check if the servers are running or not
195 cmd = "ps -A | grep ovsdb-server"
197 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
198 shfile = "status_srv.sh",
199 pidfile = "status_srv_pidfile",
200 ecodefile = "status_srv_exitcode",
202 stdout = "status_srv_stdout",
203 stderr = "status_srv_stderr")
204 (out, err), proc = self.node.check_output(self.ovs_checks, 'status_srv_exitcode')
207 msg = "Servers are not running"
209 raise RuntimeError, msg
211 self.info("Server OVS Started Correctly")
213 def create_bridge(self):
214 """ Create the bridge/switch and check error during SSH connection
216 # TODO: Check if previous bridge exist and delete them. Use ovs-vsctl list-br
217 # TODO: Add check for virtual_ip belonging to vsys_tag
220 if not (self.get("bridge_name") and self.get("virtual_ip_pref")):
221 msg = "No assignment in one or both attributes"
223 raise AttributeError, msg
225 cmd = "sliver-ovs create-bridge '%s' '%s'" %\
226 (self.get("bridge_name"), self.get("virtual_ip_pref"))
228 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
229 shfile = "create_br.sh",
230 pidfile = "create_br_pidfile",
231 ecodefile = "create_br_exitcode",
233 stdout = "create_br_stdout",
234 stderr = "create_br_stderr")
235 (out, err), proc = self.node.check_output(self.ovs_checks, 'create_br_exitcode')
238 msg = "No such pltap netdev\novs-appctl: ovs-vswitchd: server returned an error"
240 raise RuntimeError, msg
242 self.info(" Bridge %s Created and Assigned to %s" %\
243 (self.get("bridge_name"), self.get("virtual_ip_pref")) )
246 def assign_controller(self):
247 """ Set the controller IP
250 if not (self.get("controller_ip") and self.get("controller_port")):
251 msg = "No assignment in one or both attributes"
253 raise AttributeError, msg
255 cmd = "ovs-vsctl set-controller %s tcp:%s:%s" %\
256 (self.get("bridge_name"), self.get("controller_ip"), self.get("controller_port"))
258 (out, err), proc = self.node.run(cmd, self.ovs_checks,
264 msg = "SSH connection in the method assign_controller"
266 raise RuntimeError, msg
268 self.info("Controller assigned to the bridge %s" % self.get("bridge_name"))
270 def ovs_status(self):
271 """ Print the status of the bridge
274 cmd = "sliver-ovs show | tail -n +2"
276 (out, err), proc = self.node.run_and_wait(cmd, self.ovs_home,
278 stdout = "show_stdout",
279 stderr = "show_stderr")
280 (out, err), proc = self.node.check_output(self.ovs_home, 'show_stdout')
283 msg = "Error when checking the status of the OpenVswitch"
285 raise RuntimeError, msg
289 def do_release(self):
290 """ Delete the bridge and close the server.
292 .. note : It need to wait for the others RM (OVSPort and OVSTunnel)
293 to be released before releasing itself
297 from nepi.resources.planetlab.openvswitch.ovsport import OVSPort
298 rms = self.get_connected(OVSPort.get_rtype())
301 if rm.state < ResourceState.RELEASED:
302 self.ec.schedule(self.reschedule_delay, self.release)
305 cmd = "sliver-ovs del-bridge %s" % self.get('bridge_name')
306 (out, err), proc = self.node.run(cmd, self.ovs_checks,
309 cmd = "sliver-ovs stop"
310 (out, err), proc = self.node.run(cmd, self.ovs_checks,
313 msg = "Deleting the bridge %s" % self.get('bridge_name')
317 self.error(msg, out, err)
318 raise RuntimeError, msg
320 super(OVSSwitch, self).do_release()