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
31 PYTHON_VSYS_VERSION = "1.0"
34 class PlanetlabTap(LinuxApplication):
35 _rtype = "PlanetlabTap"
36 _help = "Creates a TAP device on a PlanetLab host"
37 _backend = "planetlab"
40 def _register_attributes(cls):
41 ip4 = Attribute("ip4", "IPv4 Address",
44 mac = Attribute("mac", "MAC Address",
47 prefix4 = Attribute("prefix4", "IPv4 network prefix",
51 mtu = Attribute("mtu", "Maximum transmition unit for device",
54 devname = Attribute("deviceName",
55 "Name of the network interface (e.g. eth0, wlan0, etc)",
56 flags = Flags.NoWrite)
58 up = Attribute("up", "Link up",
61 snat = Attribute("snat", "Set SNAT=1",
65 pointopoint = Attribute("pointopoint", "Peer IP address",
68 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
71 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
74 gre_key = Attribute("greKey",
75 "GRE key to be used to configure GRE tunnel",
79 gre_remote = Attribute("greRemote",
80 "Public IP of remote endpoint for GRE tunnel",
83 tear_down = Attribute("tearDown",
84 "Bash script to be executed before releasing the resource",
87 cls._register_attribute(ip4)
88 cls._register_attribute(mac)
89 cls._register_attribute(prefix4)
90 cls._register_attribute(mtu)
91 cls._register_attribute(devname)
92 cls._register_attribute(up)
93 cls._register_attribute(snat)
94 cls._register_attribute(pointopoint)
95 cls._register_attribute(txqueuelen)
96 cls._register_attribute(gre_key)
97 cls._register_attribute(gre_remote)
98 cls._register_attribute(tear_down)
100 def __init__(self, ec, guid):
101 super(PlanetlabTap, self).__init__(ec, guid)
102 self._home = "tap-%s" % self.guid
103 self._gre_enabled = False
107 node = self.get_connected(PlanetlabNode.get_rtype())
108 if node: return node[0]
112 def gre_enabled(self):
113 if not self._gre_enabled:
114 from nepi.resources.linux.gretunnel import LinuxGRETunnel
115 gre = self.get_connected(LinuxGRETunnel.get_rtype())
116 if gre: self._gre_enabled = True
118 return self._gre_enabled
120 def upload_sources(self):
123 # vif-creation python script
124 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
127 scripts.append(pl_vif_create)
129 # vif-up python script
130 pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
133 scripts.append(pl_vif_up)
135 # vif-down python script
136 pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
139 scripts.append(pl_vif_down)
141 # udp-connect python script
142 pl_vif_connect = os.path.join(os.path.dirname(__file__), "scripts",
143 "pl-vif-udp-connect.py")
145 scripts.append(pl_vif_connect)
147 # tunnel creation python script
148 tunchannel = os.path.join(os.path.dirname(__file__), "..", "linux",
149 "scripts", "tunchannel.py")
151 scripts.append(tunchannel)
154 scripts = ";".join(scripts)
156 self.node.upload(scripts,
157 os.path.join(self.node.src_dir),
160 # upload stop.sh script
161 stop_command = self.replace_paths(self._stop_command)
163 self.node.upload(stop_command,
164 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):
270 """ Waits until the vif_name file for the command is generated,
271 and returns the vif_name for the device """
276 (out, err), proc = self.node.check_output(self.run_home, "vif_name")
279 (out, err), proc = self.node.check_errors(self.run_home)
282 raise RuntimeError, err
285 vif_name = out.strip()
291 msg = "Couldn't retrieve vif_name"
292 self.error(msg, out, err)
293 raise RuntimeError, msg
297 def udp_connect_command(self, remote_endpoint, connection_run_home,
298 cipher, cipher_key, bwlimit, txqueuelen):
300 # Set the remote endpoint
301 self.set("pointopoint", remote_endpoint.get("ip4"))
303 remote_ip = socket.gethostbyname(
304 remote_endpoint.node.get("hostname"))
306 local_port_file = os.path.join(connection_run_home,
309 remote_port_file = os.path.join(connection_run_home,
312 ret_file = os.path.join(connection_run_home,
315 # Generate UDP connect command
316 # Use pl-vif-up.py script to configure TAP with peer info
317 vif_up_command = self._vif_up_command
320 command.append(vif_up_command)
322 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
323 command.append(") & (")
324 command.append("sudo -S")
325 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
326 command.append("python ${SRC}/pl-vif-udp-connect.py")
327 command.append("-t %s" % self.vif_type)
328 command.append("-S %s " % self.sock_name)
329 command.append("-l %s " % local_port_file)
330 command.append("-r %s " % remote_port_file)
331 command.append("-H %s " % remote_ip)
332 command.append("-R %s " % ret_file)
334 command.append("-c %s " % cipher)
336 command.append("-k %s " % cipher_key)
338 command.append("-q %s " % txqueuelen)
340 command.append("-b %s " % bwlimit)
344 command = " ".join(command)
345 command = self.replace_paths(command)
349 def gre_connect_command(self, remote_endpoint, connection_run_home):
350 # Set the remote endpoint
351 self.set("pointopoint", remote_endpoint.get("ip4"))
352 self.set("greRemote", socket.gethostbyname(
353 remote_endpoint.node.get("hostname")))
355 # Generate GRE connect command
357 # Use vif_down command to first kill existing TAP in GRE mode
358 vif_down_command = self._vif_down_command
360 # Use pl-vif-up.py script to configure TAP with peer info
361 vif_up_command = self._vif_up_command
364 command.append(vif_down_command)
365 command.append(") ; (")
366 command.append(vif_up_command)
369 command = " ".join(command)
370 command = self.replace_paths(command)
375 def _start_command(self):
379 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
381 command.append("-t %s" % self.vif_type)
382 command.append("-a %s" % self.get("ip4"))
383 command.append("-n %d" % self.get("prefix4"))
384 command.append("-f %s " % self.vif_name_file)
385 command.append("-S %s " % self.sock_name)
387 if self.get("snat") == True:
390 if self.get("pointopoint"):
391 command.append("-p %s" % self.get("pointopoint"))
393 if self.get("txqueuelen"):
394 command.append("-q %s" % self.get("txqueuelen"))
396 return " ".join(command)
399 def _stop_command(self):
401 command = self._vif_down_command
403 command = ["sudo -S "]
404 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
405 command.append("python ${SRC}/pl-vif-down.py")
406 command.append("-S %s " % self.sock_name)
407 command = " ".join(command)
412 def _vif_up_command(self):
414 device_name = "%s" % self.guid
416 device_name = self.get("deviceName")
418 # Use pl-vif-up.py script to configure TAP
419 command = ["sudo -S "]
420 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
421 command.append("python ${SRC}/pl-vif-up.py")
422 command.append("-u %s" % self.node.get("username"))
423 command.append("-N %s" % device_name)
424 command.append("-t %s" % self.vif_type)
425 command.append("-a %s" % self.get("ip4"))
426 command.append("-n %d" % self.get("prefix4"))
428 if self.get("snat") == True:
431 if self.get("pointopoint"):
432 command.append("-p %s" % self.get("pointopoint"))
434 if self.get("txqueuelen"):
435 command.append("-q %s" % self.get("txqueuelen"))
438 command.append("-g %s" % self.get("greKey"))
439 command.append("-G %s" % self.get("greRemote"))
441 command.append("-f %s " % self.vif_name_file)
443 return " ".join(command)
446 def _vif_down_command(self):
448 device_name = "%s" % self.guid
450 device_name = self.get("deviceName")
452 command = ["sudo -S "]
453 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
454 command.append("python ${SRC}/pl-vif-down.py")
455 command.append("-N %s " % device_name)
458 command.append("-u %s" % self.node.get("username"))
459 command.append("-t %s" % self.vif_type)
462 return " ".join(command)
469 def vif_name_file(self):
470 return os.path.join(self.run_home, "vif_name")
474 return os.path.join(self.run_home, "tap.sock")
477 def _dependencies(self):
478 return "mercurial make gcc"
482 # Install python-vsys and python-passfd
483 install_vsys = ( " ( "
484 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
489 " hg clone http://nepi.inria.fr/code/python-vsys ; "
492 " sudo -S make install "
494 "version": PYTHON_VSYS_VERSION
497 install_passfd = ( " ( python -c 'import passfd' ) "
501 " hg clone http://nepi.inria.fr/code/python-passfd ; "
502 " cd python-passfd ; "
504 " sudo -S make install "
507 return "%s ; %s" % ( install_vsys, install_passfd )
509 def valid_connection(self, guid):