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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState, \
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.planetlab.node import PlanetlabNode
25 from nepi.util.timefuncs import tnow, tdiffsec
31 # - Make base clase 'virtual device' and redefine vif_type
32 # - Instead of doing an infinite loop, open a port for communication allowing
33 # to pass the fd to another process
35 PYTHON_VSYS_VERSION = "1.0"
38 class PlanetlabTap(LinuxApplication):
39 _rtype = "PlanetlabTap"
42 def _register_attributes(cls):
43 ip4 = Attribute("ip4", "IPv4 Address",
44 flags = Flags.ExecReadOnly)
46 mac = Attribute("mac", "MAC Address",
47 flags = Flags.ExecReadOnly)
49 prefix4 = Attribute("prefix4", "IPv4 network prefix",
50 flags = Flags.ExecReadOnly)
52 mtu = Attribute("mtu", "Maximum transmition unit for device",
55 devname = Attribute("deviceName",
56 "Name of the network interface (e.g. eth0, wlan0, etc)",
57 flags = Flags.ReadOnly)
59 up = Attribute("up", "Link up", type = Types.Bool)
61 snat = Attribute("snat", "Set SNAT=1", type = Types.Bool,
62 flags = Flags.ReadOnly)
64 pointopoint = Attribute("pointopoint", "Peer IP address",
65 flags = Flags.ReadOnly)
67 tear_down = Attribute("tearDown", "Bash script to be executed before " + \
68 "releasing the resource",
69 flags = Flags.ExecReadOnly)
71 cls._register_attribute(ip4)
72 cls._register_attribute(mac)
73 cls._register_attribute(prefix4)
74 cls._register_attribute(mtu)
75 cls._register_attribute(devname)
76 cls._register_attribute(up)
77 cls._register_attribute(snat)
78 cls._register_attribute(pointopoint)
79 cls._register_attribute(tear_down)
81 def __init__(self, ec, guid):
82 super(PlanetlabTap, self).__init__(ec, guid)
83 self._home = "tap-%s" % self.guid
87 node = self.get_connected(PlanetlabNode.rtype())
88 if node: return node[0]
91 def upload_sources(self):
92 depends = "mercurial make gcc"
93 self.set("depends", depends)
96 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
101 " hg clone http://nepi.inria.fr/code/python-vsys ; "
104 " sudo -S make install "
106 "version": PYTHON_VSYS_VERSION
109 self.set("install", install)
111 def upload_start_command(self):
112 # upload tap-creation python script
113 start_script = self.replace_paths(self._start_script)
114 self.node.upload(start_script,
115 os.path.join(self.app_home, "tap_create.py"),
120 start_command = self.replace_paths(self._start_command)
122 self.info("Uploading command '%s'" % start_command)
124 self.set("command", start_command)
125 self.node.upload(start_command,
126 os.path.join(self.app_home, "start.sh"),
130 # We want to make sure the device is up and running
131 # before the experiment starts.
132 # Run the command as a bash script in background,
133 # in the host ( but wait until the command has
134 # finished to continue )
135 self._run_in_background()
138 if_name = self.wait_if_name()
139 self.set("deviceName", if_name)
142 if not self.node or self.node.state < ResourceState.PROVISIONED:
143 self.ec.schedule(reschedule_delay, self.deploy)
153 self.debug("----- READY ---- ")
154 self._ready_time = tnow()
155 self._state = ResourceState.READY
158 if self._state == ResourceState.READY:
159 command = self.get("command")
160 self.info("Starting command '%s'" % command)
162 self._start_time = tnow()
163 self._state = ResourceState.STARTED
165 msg = " Failed to execute command '%s'" % command
166 self.error(msg, out, err)
167 self._state = ResourceState.FAILED
168 raise RuntimeError, msg
171 command = self.get('command') or ''
174 if state == ResourceState.STARTED:
175 self.info("Stopping command '%s'" % command)
177 command = "rm %s" % os.path.join(self.run_home, "if_stop")
178 (out, err), proc = self.execute_command(command)
180 self._stop_time = tnow()
181 self._state = ResourceState.STOPPED
185 # First check if the ccnd has failed
186 state_check_delay = 0.5
187 if self._state == ResourceState.STARTED and \
188 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
190 if self.get("deviceName"):
191 (out, err), proc = self.node.execute("ifconfig")
193 if out.strip().find(self.get("deviceName")) == -1:
194 # tap is not running is not running (socket not found)
195 self._state = ResourceState.FINISHED
197 self._last_state_check = tnow()
201 def wait_if_name(self):
202 """ Waits until the if_name file for the command is generated,
203 and returns the if_name for the devide """
208 (out, err), proc = self.node.check_output(self.run_home, "if_name")
211 if_name = out.strip()
217 msg = "Couldn't retrieve if_name"
218 self.error(msg, out, err)
220 raise RuntimeError, msg
225 def _start_command(self):
226 return "sudo -S python ${APP_HOME}/tap_create.py"
229 def _start_script(self):
230 return ( "import vsys, time, os \n"
231 "(fd, if_name) = vsys.fd_tuntap(vsys.%(devtype)s)\n"
232 "vsys.vif_up(if_name, '%(ip)s', %(prefix)s%(snat)s%(pointopoint)s)\n"
233 "f = open('%(if_name_file)s', 'w')\n"
236 "f = open('%(if_stop_file)s', 'w')\n"
238 "while os.path.exists('%(if_stop_file)s'):\n"
241 "devtype": self._vif_type,
242 "ip": self.get("ip4"),
243 "prefix": self.get("prefix4"),
244 "snat": ", snat=True" if self.get("snat") else "",
245 "pointopoint": ", pointopoint=%s" % self.get("pointopoint") \
246 if self.get("pointopoint") else "",
247 "if_name_file": os.path.join(self.run_home, "if_name"),
248 "if_stop_file": os.path.join(self.run_home, "if_stop"),
255 def valid_connection(self, guid):