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 clsinit_copy, ResourceState, \
22 reschedule_delay, failtrap
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 # - CREATE GRE - PlanetlabGRE - it only needs to set the gre and remote
32 # properties when configuring the vif_up
34 PYTHON_VSYS_VERSION = "1.0"
37 class PlanetlabTap(LinuxApplication):
38 _rtype = "PlanetlabTap"
39 _help = "Creates a TAP device on a PlanetLab host"
40 _backend = "planetlab"
43 def _register_attributes(cls):
44 ip4 = Attribute("ip4", "IPv4 Address",
45 flags = Flags.ExecReadOnly)
47 mac = Attribute("mac", "MAC Address",
48 flags = Flags.ExecReadOnly)
50 prefix4 = Attribute("prefix4", "IPv4 network prefix",
52 flags = Flags.ExecReadOnly)
54 mtu = Attribute("mtu", "Maximum transmition unit for device",
57 devname = Attribute("deviceName",
58 "Name of the network interface (e.g. eth0, wlan0, etc)",
59 flags = Flags.ReadOnly)
61 up = Attribute("up", "Link up",
64 snat = Attribute("snat", "Set SNAT=1",
66 flags = Flags.ExecReadOnly)
68 pointopoint = Attribute("pointopoint", "Peer IP address",
69 flags = Flags.ExecReadOnly)
71 tear_down = Attribute("tearDown", "Bash script to be executed before " + \
72 "releasing the resource",
73 flags = Flags.ExecReadOnly)
75 cls._register_attribute(ip4)
76 cls._register_attribute(mac)
77 cls._register_attribute(prefix4)
78 cls._register_attribute(mtu)
79 cls._register_attribute(devname)
80 cls._register_attribute(up)
81 cls._register_attribute(snat)
82 cls._register_attribute(pointopoint)
83 cls._register_attribute(tear_down)
85 def __init__(self, ec, guid):
86 super(PlanetlabTap, self).__init__(ec, guid)
87 self._home = "tap-%s" % self.guid
91 node = self.get_connected(PlanetlabNode.rtype())
92 if node: return node[0]
95 def upload_sources(self):
96 # upload vif-creation python script
97 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
100 self.node.upload(pl_vif_create,
101 os.path.join(self.node.src_dir, "pl-vif-create.py"),
104 # upload vif-stop python script
105 pl_vif_stop = os.path.join(os.path.dirname(__file__), "scripts",
108 self.node.upload(pl_vif_stop,
109 os.path.join(self.node.src_dir, "pl-vif-stop.py"),
112 # upload vif-connect python script
113 pl_vif_connect = os.path.join(os.path.dirname(__file__), "scripts",
114 "pl-vif-udp-connect.py")
116 self.node.upload(pl_vif_connect,
117 os.path.join(self.node.src_dir, "pl-vif-udp-connect.py"),
120 # upload tun-connect python script
121 tunchannel = os.path.join(os.path.dirname(__file__), "..", "linux",
122 "scripts", "tunchannel.py")
124 self.node.upload(tunchannel,
125 os.path.join(self.node.src_dir, "tunchannel.py"),
128 # upload stop.sh script
129 stop_command = self.replace_paths(self._stop_command)
130 self.node.upload(stop_command,
131 os.path.join(self.app_home, "stop.sh"),
135 def upload_start_command(self):
136 super(PlanetlabTap, self).upload_start_command()
138 # We want to make sure the device is up and running
139 # before the deploy finishes (so things will be ready
140 # before other stuff starts running).
141 # Run the command as a bash script in background,
142 # in the host ( but wait until the command has
143 # finished to continue )
144 self._run_in_background()
147 if_name = self.wait_if_name()
148 self.set("deviceName", if_name)
152 if not self.node or self.node.state < ResourceState.PROVISIONED:
153 self.ec.schedule(reschedule_delay, self.deploy)
155 if not self.get("command"):
156 self.set("command", self._start_command)
158 if not self.get("depends"):
159 self.set("depends", self._dependencies)
161 if not self.get("install"):
162 self.set("install", self._install)
167 self.debug("----- READY ---- ")
172 if self.state == ResourceState.READY:
173 command = self.get("command")
174 self.info("Starting command '%s'" % command)
178 msg = " Failed to execute command '%s'" % command
179 self.error(msg, out, err)
180 raise RuntimeError, msg
184 command = self.get('command') or ''
186 if self.state == ResourceState.STARTED:
187 self.info("Stopping command '%s'" % command)
189 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
190 (out, err), proc = self.execute_command(command,
197 # First check if the ccnd has failed
198 state_check_delay = 0.5
199 if self._state == ResourceState.STARTED and \
200 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
202 if self.get("deviceName"):
203 (out, err), proc = self.node.execute("ifconfig")
205 if out.strip().find(self.get("deviceName")) == -1:
206 # tap is not running is not running (socket not found)
209 self._last_state_check = tnow()
214 # Node needs to wait until all associated RMs are released
217 from nepi.resources.linux.udptunnel import UdpTunnel
218 rms = self.get_connected(UdpTunnel.rtype())
220 if rm.state < ResourceState.STOPPED:
221 self.ec.schedule(reschedule_delay, self.release)
225 err = traceback.format_exc()
228 super(PlanetlabTap, self).release()
230 def wait_if_name(self):
231 """ Waits until the if_name file for the command is generated,
232 and returns the if_name for the device """
237 (out, err), proc = self.node.check_output(self.run_home, "if_name")
240 if_name = out.strip()
246 msg = "Couldn't retrieve if_name"
247 self.error(msg, out, err)
248 raise RuntimeError, msg
252 def udp_connect_command(self, remote_ip, local_port_file,
253 remote_port_file, ret_file, cipher, cipher_key,
254 bwlimit, txqueuelen):
255 command = ["sudo -S "]
256 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
257 command.append("python ${SRC}/pl-vif-udp-connect.py")
258 command.append("-t %s" % self.vif_type)
259 command.append("-S %s " % self.sock_name)
260 command.append("-l %s " % local_port_file)
261 command.append("-r %s " % remote_port_file)
262 command.append("-H %s " % remote_ip)
263 command.append("-R %s " % ret_file)
265 command.append("-c %s " % cipher)
267 command.append("-k %s " % cipher_key)
269 command.append("-q %s " % txqueuelen)
271 command.append("-b %s " % bwlimit)
273 command = " ".join(command)
274 command = self.replace_paths(command)
278 def _start_command(self):
279 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
281 command.append("-t %s" % self.vif_type)
282 command.append("-a %s" % self.get("ip4"))
283 command.append("-n %d" % self.get("prefix4"))
284 command.append("-f %s " % self.if_name_file)
285 command.append("-S %s " % self.sock_name)
286 if self.get("snat") == True:
288 if self.get("pointopoint"):
289 command.append("-p %s" % self.get("pointopoint"))
291 return " ".join(command)
294 def _stop_command(self):
295 command = ["sudo -S python ${SRC}/pl-vif-stop.py"]
297 command.append("-S %s " % self.sock_name)
298 return " ".join(command)
305 def if_name_file(self):
306 return os.path.join(self.run_home, "if_name")
310 return os.path.join(self.run_home, "tap.sock")
313 def _dependencies(self):
314 return "mercurial make gcc"
318 # Install python-vsys and python-passfd
319 install_vsys = ( " ( "
320 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
325 " hg clone http://nepi.inria.fr/code/python-vsys ; "
328 " sudo -S make install "
330 "version": PYTHON_VSYS_VERSION
333 install_passfd = ( " ( python -c 'import passfd' ) "
337 " hg clone http://nepi.inria.fr/code/python-passfd ; "
338 " cd python-passfd ; "
340 " sudo -S make install "
343 return "%s ; %s" % ( install_vsys, install_passfd )
345 def valid_connection(self, guid):