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, \
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.planetlab.node import PlanetlabNode
25 from nepi.util.timefuncs import tnow, tdiffsec
32 # - CREATE GRE - PlanetlabGRE - it only needs to set the gre and remote
33 # properties when configuring the vif_up
35 PYTHON_VSYS_VERSION = "1.0"
38 class PlanetlabTap(LinuxApplication):
39 _rtype = "PlanetlabTap"
40 _help = "Creates a TAP device on a PlanetLab host"
41 _backend = "planetlab"
44 def _register_attributes(cls):
45 ip4 = Attribute("ip4", "IPv4 Address",
48 mac = Attribute("mac", "MAC Address",
51 prefix4 = Attribute("prefix4", "IPv4 network prefix",
55 mtu = Attribute("mtu", "Maximum transmition unit for device",
58 devname = Attribute("deviceName",
59 "Name of the network interface (e.g. eth0, wlan0, etc)",
60 flags = Flags.NoWrite)
62 up = Attribute("up", "Link up",
65 snat = Attribute("snat", "Set SNAT=1",
69 pointopoint = Attribute("pointopoint", "Peer IP address",
72 tear_down = Attribute("tearDown", "Bash script to be executed before " + \
73 "releasing the resource",
76 cls._register_attribute(ip4)
77 cls._register_attribute(mac)
78 cls._register_attribute(prefix4)
79 cls._register_attribute(mtu)
80 cls._register_attribute(devname)
81 cls._register_attribute(up)
82 cls._register_attribute(snat)
83 cls._register_attribute(pointopoint)
84 cls._register_attribute(tear_down)
86 def __init__(self, ec, guid):
87 super(PlanetlabTap, self).__init__(ec, guid)
88 self._home = "tap-%s" % self.guid
92 node = self.get_connected(PlanetlabNode.get_rtype())
93 if node: return node[0]
96 def upload_sources(self):
99 # vif-creation python script
100 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
103 scripts.append(pl_vif_create)
105 # vif-up python script
106 pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
109 scripts.append(pl_vif_up)
111 # vif-down python script
112 pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
115 scripts.append(pl_vif_down)
117 # udp-connect python script
118 pl_vif_connect = os.path.join(os.path.dirname(__file__), "scripts",
119 "pl-vif-udp-connect.py")
121 scripts.append(pl_vif_connect)
123 # tunnel creation python script
124 tunchannel = os.path.join(os.path.dirname(__file__), "..", "linux",
125 "scripts", "tunchannel.py")
127 scripts.append(tunchannel)
130 scripts = ";".join(scripts)
132 self.node.upload(scripts,
133 os.path.join(self.node.src_dir),
136 # upload stop.sh script
137 stop_command = self.replace_paths(self._stop_command)
139 self.node.upload(stop_command,
140 os.path.join(self.app_home, "stop.sh"),
142 # Overwrite file every time.
143 # The stop.sh has the path to the socket, wich should change
144 # on every experiment run.
147 def upload_start_command(self):
148 # Overwrite file every time.
149 # The start.sh has the path to the socket, wich should change
150 # on every experiment run.
151 super(PlanetlabTap, self).upload_start_command(overwrite = True)
153 # We want to make sure the device is up and running
154 # before the deploy finishes, so we execute now the
155 # start script. We run it in background, because the
156 # TAP will live for as long as the process that
157 # created it is running, and wait until the TAP
159 self._run_in_background()
161 # After creating the TAP, the pl-vif-create.py script
162 # will write the name of the TAP to a file. We wait until
163 # we can read the interface name from the file.
164 vif_name = self.wait_vif_name()
165 self.set("deviceName", vif_name)
168 if not self.node or self.node.state < ResourceState.PROVISIONED:
169 self.ec.schedule(reschedule_delay, self.deploy)
171 if not self.get("command"):
172 self.set("command", self._start_command)
174 if not self.get("depends"):
175 self.set("depends", self._dependencies)
177 if not self.get("install"):
178 self.set("install", self._install)
186 if self.state == ResourceState.READY:
187 command = self.get("command")
188 self.info("Starting command '%s'" % command)
192 msg = " Failed to execute command '%s'" % command
193 self.error(msg, out, err)
194 raise RuntimeError, msg
197 command = self.get('command') or ''
199 if self.state == ResourceState.STARTED:
200 self.info("Stopping command '%s'" % command)
202 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
203 (out, err), proc = self.execute_command(command,
207 msg = " Failed to stop command '%s' " % command
208 self.error(msg, out, err)
214 # First check if the ccnd has failed
215 state_check_delay = 0.5
216 if self._state == ResourceState.STARTED and \
217 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
219 if self.get("deviceName"):
220 (out, err), proc = self.node.execute("ifconfig")
222 if out.strip().find(self.get("deviceName")) == -1:
223 # tap is not running is not running (socket not found)
226 self._last_state_check = tnow()
230 def do_release(self):
231 # Node needs to wait until all associated RMs are released
233 from nepi.resources.linux.tunnel import LinuxTunnel
234 rms = self.get_connected(LinuxTunnel.get_rtype())
237 if rm.state < ResourceState.STOPPED:
238 self.ec.schedule(reschedule_delay, self.release)
241 super(PlanetlabTap, self).do_release()
243 def wait_vif_name(self):
244 """ Waits until the vif_name file for the command is generated,
245 and returns the vif_name for the device """
250 (out, err), proc = self.node.check_output(self.run_home, "vif_name")
253 (out, err), proc = self.node.check_errors(self.run_home)
256 raise RuntimeError, err
259 vif_name = out.strip()
265 msg = "Couldn't retrieve vif_name"
266 self.error(msg, out, err)
267 raise RuntimeError, msg
271 def udp_connect_command(self, remote_endpoint, connection_run_home,
272 cipher, cipher_key, bwlimit, txqueuelen):
274 # Generate UDP connect command
275 remote_ip = socket.gethostbyname(
276 remote_endpoint.node.get("hostname"))
278 local_port_file = os.path.join(connection_run_home,
281 remote_port_file = os.path.join(connection_run_home,
284 ret_file = os.path.join(connection_run_home,
287 # Use pl-vif-up.py script to configure TAP with peer info
288 command = ["( sudo -S "]
289 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
290 command.append("python ${SRC}/pl-vif-up.py")
291 command.append("-N %s" % self.get("deviceName"))
292 command.append("-t %s" % self.vif_type)
293 command.append("-a %s" % self.get("ip4"))
294 command.append("-n %d" % self.get("prefix4"))
295 if self.get("snat") == True:
297 command.append("-p %s" % remote_endpoint.get("ip4"))
299 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
300 command.append(") & (")
301 command.append("sudo -S")
302 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
303 command.append("python ${SRC}/pl-vif-udp-connect.py")
304 command.append("-t %s" % self.vif_type)
305 command.append("-S %s " % self.sock_name)
306 command.append("-l %s " % local_port_file)
307 command.append("-r %s " % remote_port_file)
308 command.append("-H %s " % remote_ip)
309 command.append("-R %s " % ret_file)
311 command.append("-c %s " % cipher)
313 command.append("-k %s " % cipher_key)
315 command.append("-q %s " % txqueuelen)
317 command.append("-b %s " % bwlimit)
321 command = " ".join(command)
322 command = self.replace_paths(command)
327 def _start_command(self):
328 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
330 command.append("-t %s" % self.vif_type)
331 command.append("-a %s" % self.get("ip4"))
332 command.append("-n %d" % self.get("prefix4"))
333 command.append("-f %s " % self.vif_name_file)
334 command.append("-S %s " % self.sock_name)
336 if self.get("snat") == True:
339 if self.get("pointopoint"):
340 command.append("-p %s" % self.get("pointopoint"))
342 return " ".join(command)
345 def _stop_command(self):
346 command = ["sudo -S python ${SRC}/pl-vif-down.py"]
348 command.append("-S %s " % self.sock_name)
349 return " ".join(command)
356 def vif_name_file(self):
357 return os.path.join(self.run_home, "vif_name")
361 return os.path.join(self.run_home, "tap.sock")
364 def _dependencies(self):
365 return "mercurial make gcc"
369 # Install python-vsys and python-passfd
370 install_vsys = ( " ( "
371 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
376 " hg clone http://nepi.inria.fr/code/python-vsys ; "
379 " sudo -S make install "
381 "version": PYTHON_VSYS_VERSION
384 install_passfd = ( " ( python -c 'import passfd' ) "
388 " hg clone http://nepi.inria.fr/code/python-passfd ; "
389 " cd python-passfd ; "
391 " sudo -S make install "
394 return "%s ; %s" % ( install_vsys, install_passfd )
396 def valid_connection(self, guid):