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
30 PYTHON_VSYS_VERSION = "1.0"
33 class PlanetlabTap(LinuxApplication):
34 _rtype = "PlanetlabTap"
35 _help = "Creates a TAP device on a PlanetLab host"
36 _backend = "planetlab"
39 def _register_attributes(cls):
40 endpoint_ip = Attribute("endpoint_ip", "IP of the endpoint. This is the attribute "
41 "you should use to establish a tunnel or a remote "
42 "connection between endpoint",
45 mac = Attribute("mac", "MAC Address",
48 endpoint_prefix = Attribute("endpoint_prefix", "IPv4 network prefix of the endpoint",
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.NoWrite)
59 up = Attribute("up", "Link up",
62 snat = Attribute("snat", "Set SNAT=1",
66 pointopoint = Attribute("pointopoint", "Peer IP address",
69 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
72 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
75 gre_key = Attribute("greKey",
76 "GRE key to be used to configure GRE tunnel",
80 gre_remote = Attribute("greRemote",
81 "Public IP of remote endpoint for GRE tunnel",
84 tear_down = Attribute("tearDown",
85 "Bash script to be executed before releasing the resource",
88 cls._register_attribute(endpoint_ip)
89 cls._register_attribute(mac)
90 cls._register_attribute(endpoint_prefix)
91 cls._register_attribute(mtu)
92 cls._register_attribute(devname)
93 cls._register_attribute(up)
94 cls._register_attribute(snat)
95 cls._register_attribute(pointopoint)
96 cls._register_attribute(txqueuelen)
97 cls._register_attribute(gre_key)
98 cls._register_attribute(gre_remote)
99 cls._register_attribute(tear_down)
101 def __init__(self, ec, guid):
102 super(PlanetlabTap, self).__init__(ec, guid)
103 self._home = "tap-%s" % self.guid
104 self._gre_enabled = False
108 node = self.get_connected(PlanetlabNode.get_rtype())
109 if node: return node[0]
110 raise RuntimeError, "TAP/TUN devices must be connected to Node"
113 def gre_enabled(self):
114 if not self._gre_enabled:
115 from nepi.resources.linux.gretunnel import LinuxGRETunnel
116 gre = self.get_connected(LinuxGRETunnel.get_rtype())
117 if gre: self._gre_enabled = True
119 return self._gre_enabled
121 def upload_sources(self):
124 # vif-creation python script
125 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
128 scripts.append(pl_vif_create)
130 # vif-up python script
131 pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
134 scripts.append(pl_vif_up)
136 # vif-down python script
137 pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
140 scripts.append(pl_vif_down)
142 # udp-connect python script
143 pl_vif_connect = os.path.join(os.path.dirname(__file__), "scripts",
144 "pl-vif-udp-connect.py")
146 scripts.append(pl_vif_connect)
148 # tunnel creation python script
149 tunchannel = os.path.join(os.path.dirname(__file__), "..", "linux",
150 "scripts", "tunchannel.py")
152 scripts.append(tunchannel)
155 scripts = ";".join(scripts)
157 self.node.upload(scripts,
158 os.path.join(self.node.src_dir),
161 # upload stop.sh script
162 stop_command = self.replace_paths(self._stop_command)
164 self.node.upload_command(stop_command,
165 shfile = os.path.join(self.app_home, "stop.sh"),
166 # Overwrite file every time.
167 # The stop.sh has the path to the socket, which should change
168 # on every experiment run.
171 def upload_start_command(self):
172 # If GRE mode is enabled, TAP creation is delayed until the
173 # tunnel is established
174 if not self.gre_enabled:
175 # Overwrite file every time.
176 # The start.sh has the path to the socket, wich should change
177 # on every experiment run.
178 super(PlanetlabTap, self).upload_start_command(overwrite = True)
180 # We want to make sure the device is up and running
181 # before the deploy finishes, so we execute now the
182 # start script. We run it in background, because the
183 # TAP will live for as long as the process that
184 # created it is running, and wait until the TAP
186 self._run_in_background()
188 # After creating the TAP, the pl-vif-create.py script
189 # will write the name of the TAP to a file. We wait until
190 # we can read the interface name from the file.
191 vif_name = self.wait_vif_name()
192 self.set("deviceName", vif_name)
195 if not self.node or self.node.state < ResourceState.PROVISIONED:
196 self.ec.schedule(reschedule_delay, self.deploy)
198 if not self.get("command"):
199 self.set("command", self._start_command)
201 if not self.get("depends"):
202 self.set("depends", self._dependencies)
204 if not self.get("install"):
205 self.set("install", self._install)
213 if self.state == ResourceState.READY:
214 command = self.get("command")
215 self.info("Starting command '%s'" % command)
219 msg = " Failed to execute command '%s'" % command
220 self.error(msg, out, err)
221 raise RuntimeError, msg
224 command = self.get('command') or ''
226 if self.state == ResourceState.STARTED:
227 self.info("Stopping command '%s'" % command)
229 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
230 (out, err), proc = self.execute_command(command,
234 msg = " Failed to stop command '%s' " % command
235 self.error(msg, out, err)
241 state_check_delay = 0.5
242 if self._state == ResourceState.STARTED and \
243 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
245 if self.get("deviceName"):
246 (out, err), proc = self.node.execute("ifconfig")
248 if out.strip().find(self.get("deviceName")) == -1:
249 # tap is not running is not running (socket not found)
252 self._last_state_check = tnow()
256 def do_release(self):
257 # Node needs to wait until all associated RMs are released
259 from nepi.resources.linux.tunnel import LinuxTunnel
260 rms = self.get_connected(LinuxTunnel.get_rtype())
263 if rm.state < ResourceState.STOPPED:
264 self.ec.schedule(reschedule_delay, self.release)
267 super(PlanetlabTap, self).do_release()
269 def wait_vif_name(self, exec_run_home = None):
270 """ Waits until the vif_name file for the command is generated,
271 and returns the vif_name for the device """
275 # The vif_name file will be created in the tap-home, while the
276 # current execution home might be elsewhere to check for errors
277 # (e.g. could be a tunnel-home)
278 if not exec_run_home:
279 exec_run_home = self.run_home
282 (out, err), proc = self.node.check_output(self.run_home, "vif_name")
285 (out, err), proc = self.node.check_errors(exec_run_home)
288 raise RuntimeError, err
291 vif_name = out.strip()
297 msg = "Couldn't retrieve vif_name"
298 self.error(msg, out, err)
299 raise RuntimeError, msg
303 def gre_connect(self, remote_endpoint, connection_app_home,
304 connection_run_home):
305 gre_connect_command = self._gre_connect_command(
306 remote_endpoint, connection_run_home)
308 # upload command to connect.sh script
309 shfile = os.path.join(connection_app_home, "gre-connect.sh")
310 self.node.upload_command(gre_connect_command,
314 # invoke connect script
315 cmd = "bash %s" % shfile
316 (out, err), proc = self.node.run(cmd, connection_run_home)
318 # check if execution errors occurred
319 msg = " Failed to connect endpoints "
321 if proc.poll() or err:
322 self.error(msg, out, err)
323 raise RuntimeError, msg
325 # Wait for pid file to be generated
326 pid, ppid = self.node.wait_pid(connection_run_home)
328 # If the process is not running, check for error information
329 # on the remote machine
330 if not pid or not ppid:
331 (out, err), proc = self.node.check_errors(connection_run_home)
332 # Out is what was written in the stderr file
334 msg = " Failed to start command '%s' " % command
335 self.error(msg, out, err)
336 raise RuntimeError, msg
338 # After creating the TAP, the pl-vif-create.py script
339 # will write the name of the TAP to a file. We wait until
340 # we can read the interface name from the file.
341 vif_name = self.wait_vif_name(exec_run_home = connection_run_home)
342 self.set("deviceName", vif_name)
346 def udp_connect(self, remote_endpoint, connection_app_home,
347 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
348 udp_connect_command = self._udp_connect_command(
349 remote_endpoint, connection_run_home,
350 cipher, cipher_key, bwlimit, txqueuelen)
352 # upload command to connect.sh script
353 shfile = os.path.join(connection_app_home, "udp-connect.sh")
354 self.node.upload_command(udp_connect_command,
358 # invoke connect script
359 cmd = "bash %s" % shfile
360 (out, err), proc = self.node.run(cmd, connection_run_home)
362 # check if execution errors occurred
363 msg = "Failed to connect endpoints "
366 self.error(msg, out, err)
367 raise RuntimeError, msg
369 # Wait for pid file to be generated
370 pid, ppid = self.node.wait_pid(connection_run_home)
372 # If the process is not running, check for error information
373 # on the remote machine
374 if not pid or not ppid:
375 (out, err), proc = self.node.check_errors(connection_run_home)
376 # Out is what was written in the stderr file
378 msg = " Failed to start command '%s' " % command
379 self.error(msg, out, err)
380 raise RuntimeError, msg
384 def _udp_connect_command(self, remote_endpoint, connection_run_home,
385 cipher, cipher_key, bwlimit, txqueuelen):
387 # Set the remote endpoint
388 self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
390 remote_ip = remote_endpoint.node.get("ip")
392 local_port_file = os.path.join(connection_run_home,
395 remote_port_file = os.path.join(connection_run_home,
398 ret_file = os.path.join(connection_run_home,
401 # Generate UDP connect command
402 # Use pl-vif-up.py script to configure TAP with peer info
403 vif_up_command = self._vif_up_command
406 command.append(vif_up_command)
408 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
409 command.append(") & (")
410 command.append("sudo -S")
411 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
412 command.append("python ${SRC}/pl-vif-udp-connect.py")
413 command.append("-t %s" % self.vif_type)
414 command.append("-S %s " % self.sock_name)
415 command.append("-l %s " % local_port_file)
416 command.append("-r %s " % remote_port_file)
417 command.append("-H %s " % remote_ip)
418 command.append("-R %s " % ret_file)
420 command.append("-c %s " % cipher)
422 command.append("-k %s " % cipher_key)
424 command.append("-q %s " % txqueuelen)
426 command.append("-b %s " % bwlimit)
430 command = " ".join(command)
431 command = self.replace_paths(command)
435 def _gre_connect_command(self, remote_endpoint, connection_run_home):
436 # Set the remote endpoint
437 self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
438 self.set("greRemote", remote_endpoint.node.get("ip"))
440 # Generate GRE connect command
442 # Use vif_down command to first kill existing TAP in GRE mode
443 vif_down_command = self._vif_down_command
445 # Use pl-vif-up.py script to configure TAP with peer info
446 vif_up_command = self._vif_up_command
449 command.append(vif_down_command)
450 command.append(") ; (")
451 command.append(vif_up_command)
454 command = " ".join(command)
455 command = self.replace_paths(command)
460 def _start_command(self):
464 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
466 command.append("-t %s" % self.vif_type)
467 command.append("-a %s" % self.get("endpoint_ip"))
468 command.append("-n %d" % self.get("endpoint_prefix"))
469 command.append("-f %s " % self.vif_name_file)
470 command.append("-S %s " % self.sock_name)
472 if self.get("snat") == True:
475 if self.get("pointopoint"):
476 command.append("-p %s" % self.get("pointopoint"))
478 if self.get("txqueuelen"):
479 command.append("-q %s" % self.get("txqueuelen"))
481 return " ".join(command)
484 def _stop_command(self):
486 command = self._vif_down_command
488 command = ["sudo -S "]
489 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
490 command.append("python ${SRC}/pl-vif-down.py")
491 command.append("-S %s " % self.sock_name)
492 command = " ".join(command)
497 def _vif_up_command(self):
499 device_name = "%s" % self.guid
501 device_name = self.get("deviceName")
503 # Use pl-vif-up.py script to configure TAP
504 command = ["sudo -S "]
505 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
506 command.append("python ${SRC}/pl-vif-up.py")
507 command.append("-u %s" % self.node.get("username"))
508 command.append("-N %s" % device_name)
509 command.append("-t %s" % self.vif_type)
510 command.append("-a %s" % self.get("endpoint_ip"))
511 command.append("-n %d" % self.get("endpoint_prefix"))
513 if self.get("snat") == True:
516 if self.get("pointopoint"):
517 command.append("-p %s" % self.get("pointopoint"))
519 if self.get("txqueuelen"):
520 command.append("-q %s" % self.get("txqueuelen"))
523 command.append("-g %s" % self.get("greKey"))
524 command.append("-G %s" % self.get("greRemote"))
526 command.append("-f %s " % self.vif_name_file)
528 return " ".join(command)
531 def _vif_down_command(self):
533 device_name = "%s" % self.guid
535 device_name = self.get("deviceName")
537 command = ["sudo -S "]
538 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
539 command.append("python ${SRC}/pl-vif-down.py")
540 command.append("-N %s " % device_name)
543 command.append("-u %s" % self.node.get("username"))
544 command.append("-t %s" % self.vif_type)
547 return " ".join(command)
554 def vif_name_file(self):
555 return os.path.join(self.run_home, "vif_name")
559 return os.path.join(self.run_home, "tap.sock")
562 def _dependencies(self):
563 return "mercurial make gcc"
567 # Install python-vsys and python-passfd
568 install_vsys = ( " ( "
569 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
574 " hg clone http://nepi.inria.fr/code/python-vsys ; "
577 " sudo -S make install "
579 "version": PYTHON_VSYS_VERSION
582 install_passfd = ( " ( python -c 'import passfd' ) "
586 " hg clone http://nepi.inria.fr/code/python-passfd ; "
587 " cd python-passfd ; "
589 " sudo -S make install "
592 return "%s ; %s" % ( install_vsys, install_passfd )
594 def valid_connection(self, guid):