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.linux.node import LinuxNode
25 from nepi.util.timefuncs import tnow, tdiffsec
30 PYTHON_VSYS_VERSION = "1.0"
33 class LinuxTap(LinuxApplication):
35 _help = "Creates a TAP device on a Linux host"
39 def _register_attributes(cls):
40 ip4 = Attribute("ip4", "IPv4 Address",
43 mac = Attribute("mac", "MAC Address",
46 prefix4 = Attribute("prefix4", "IPv4 network prefix",
50 mtu = Attribute("mtu", "Maximum transmition unit for device",
53 devname = Attribute("deviceName",
54 "Name of the network interface (e.g. eth0, wlan0, etc)",
55 flags = Flags.NoWrite)
57 up = Attribute("up", "Link up",
60 pointopoint = Attribute("pointopoint", "Peer IP address",
63 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
66 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
69 gre_key = Attribute("greKey",
70 "GRE key to be used to configure GRE tunnel",
74 gre_remote = Attribute("greRemote",
75 "Public IP of remote endpoint for GRE tunnel",
78 pi = Attribute("pi", "Add PI (protocol information) header",
82 tear_down = Attribute("tearDown",
83 "Bash script to be executed before releasing the resource",
86 cls._register_attribute(ip4)
87 cls._register_attribute(mac)
88 cls._register_attribute(prefix4)
89 cls._register_attribute(mtu)
90 cls._register_attribute(devname)
91 cls._register_attribute(up)
92 cls._register_attribute(pointopoint)
93 cls._register_attribute(txqueuelen)
94 cls._register_attribute(gre_key)
95 cls._register_attribute(gre_remote)
96 cls._register_attribute(pi)
97 cls._register_attribute(tear_down)
99 def __init__(self, ec, guid):
100 super(LinuxTap, self).__init__(ec, guid)
101 self._home = "tap-%s" % self.guid
102 self._gre_enabled = False
103 self._tunnel_mode = False
107 node = self.get_connected(LinuxNode.get_rtype())
108 if node: return node[0]
109 raise RuntimeError, "TAP/TUN devices must be connected to Node"
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
121 def tunnel_mode(self):
122 if not self._tunnel_mode:
123 from nepi.resources.linux.tunnel import LinuxTunnel
124 tunnel = self.get_connected(LinuxTunnel.get_rtype())
125 if tunnel: self._tunnel_mode = True
127 return self._tunnel_mode
129 def upload_sources(self):
132 # udp-connect python script
133 udp_connect = os.path.join(os.path.dirname(__file__), "scripts",
134 "linux-udp-connect.py")
136 scripts.append(udp_connect)
138 # tunnel creation python script
139 tunchannel = os.path.join(os.path.dirname(__file__), "scripts",
142 scripts.append(tunchannel)
145 scripts = ";".join(scripts)
147 self.node.upload(scripts,
148 os.path.join(self.node.src_dir),
151 # upload stop.sh script
152 stop_command = self.replace_paths(self._stop_command)
154 self.node.upload(stop_command,
155 os.path.join(self.app_home, "stop.sh"),
157 # Overwrite file every time.
158 # The stop.sh has the path to the socket, which should change
159 # on every experiment run.
162 def upload_start_command(self):
163 # If GRE mode is enabled, TAP creation is delayed until the
164 # tunnel is established
165 if not self.tunnel_mode:
166 # We want to make sure the device is up and running
167 # before the deploy is over, so we execute the
168 # start script now and wait until it finishes.
169 command = self.get("command")
170 command = self.replace_paths(command)
172 shfile = os.path.join(self.app_home, "start.sh")
173 self.node.run_and_wait(command, self.run_home,
178 if not self.node or self.node.state < ResourceState.PROVISIONED:
179 self.ec.schedule(reschedule_delay, self.deploy)
181 if not self.get("deviceName"):
182 self.set("deviceName", "%s%d" % (self.vif_prefix, self.guid))
184 if not self.get("command"):
185 self.set("command", self._start_command)
193 if self.state == ResourceState.READY:
194 command = self.get("command")
195 self.info("Starting command '%s'" % command)
199 msg = " Failed to execute command '%s'" % command
200 self.error(msg, out, err)
201 raise RuntimeError, msg
204 command = self.get('command') or ''
206 if self.state == ResourceState.STARTED:
207 self.info("Stopping command '%s'" % command)
209 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
210 (out, err), proc = self.execute_command(command,
214 msg = " Failed to stop command '%s' " % command
215 self.error(msg, out, err)
221 state_check_delay = 0.5
222 if self._state == ResourceState.STARTED and \
223 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
225 if self.get("deviceName"):
226 (out, err), proc = self.node.execute("ifconfig")
228 if out.strip().find(self.get("deviceName")) == -1:
229 # tap is not running is not running (socket not found)
232 self._last_state_check = tnow()
236 def do_release(self):
237 # Node needs to wait until all associated RMs are released
239 from nepi.resources.linux.tunnel import LinuxTunnel
240 rms = self.get_connected(LinuxTunnel.get_rtype())
243 if rm.state < ResourceState.STOPPED:
244 self.ec.schedule(reschedule_delay, self.release)
247 super(LinuxTap, self).do_release()
249 def gre_connect(self, remote_endpoint, connection_app_home,
250 connection_run_home):
251 gre_connect_command = self._gre_connect_command(
252 remote_endpoint, connection_run_home)
254 # upload command to connect.sh script
255 shfile = os.path.join(connection_app_home, "gre-connect.sh")
256 self.node.upload_command(gre_connect_command,
260 # invoke connect script
261 cmd = "bash %s" % shfile
262 (out, err), proc = self.node.run(cmd, connection_run_home)
264 # check if execution errors occurred
265 msg = " Failed to connect endpoints "
267 if proc.poll() or err:
268 self.error(msg, out, err)
269 raise RuntimeError, msg
271 # Wait for pid file to be generated
272 pid, ppid = self.node.wait_pid(connection_run_home)
274 # If the process is not running, check for error information
275 # on the remote machine
276 if not pid or not ppid:
277 (out, err), proc = self.node.check_errors(connection_run_home)
278 # Out is what was written in the stderr file
280 msg = " Failed to start command '%s' " % command
281 self.error(msg, out, err)
282 raise RuntimeError, msg
286 ## XXX: NOT REALLY WORKING YET!
287 def udp_connect(self, remote_endpoint, connection_app_home,
288 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
289 udp_connect_command = self._udp_connect_command(
290 remote_endpoint, connection_run_home,
291 cipher, cipher_key, bwlimit, txqueuelen)
293 # upload command to connect.sh script
294 shfile = os.path.join(connection_app_home, "udp-connect.sh")
295 self.node.upload_command(udp_connect_command,
299 # invoke connect script
300 cmd = "bash %s" % shfile
301 (out, err), proc = self.node.run(cmd, connection_run_home)
303 # check if execution errors occurred
304 msg = "Failed to connect endpoints "
307 self.error(msg, out, err)
308 raise RuntimeError, msg
310 # Wait for pid file to be generated
311 pid, ppid = self.node.wait_pid(connection_run_home)
313 # If the process is not running, check for error information
314 # on the remote machine
315 if not pid or not ppid:
316 (out, err), proc = self.node.check_errors(connection_run_home)
317 # Out is what was written in the stderr file
319 msg = " Failed to start command '%s' " % command
320 self.error(msg, out, err)
321 raise RuntimeError, msg
325 def _udp_connect_command(self, remote_endpoint, connection_run_home,
326 cipher, cipher_key, bwlimit, txqueuelen):
328 # Set the remote endpoint
329 self.set("pointopoint", remote_endpoint.get("ip4"))
331 # Planetlab TAPs always use PI headers
332 from nepi.resources.planetlab.tap import PlanetlabTap
333 if self.is_rm_instance(PlanetlabTap.get_rtype()):
336 remote_ip = remote_endpoint.node.get("ip")
338 local_port_file = os.path.join(connection_run_home,
341 remote_port_file = os.path.join(connection_run_home,
344 ret_file = os.path.join(connection_run_home,
347 # Generate UDP connect command
348 # Use the start command to configure TAP with peer info
349 start_command = self._start_command
352 command.append(start_command)
354 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
355 command.append(") & (")
356 command.append("sudo -S")
357 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
358 command.append("python ${SRC}/linux-udp-connect.py")
359 command.append("-N %s" % self.get("deviceName"))
360 command.append("-t %s" % self.vif_type)
363 command.append("-l %s " % local_port_file)
364 command.append("-r %s " % remote_port_file)
365 command.append("-H %s " % remote_ip)
366 command.append("-R %s " % ret_file)
368 command.append("-c %s " % cipher)
370 command.append("-k %s " % cipher_key)
372 command.append("-q %s " % txqueuelen)
374 command.append("-b %s " % bwlimit)
378 command = " ".join(command)
379 command = self.replace_paths(command)
383 def _gre_connect_command(self, remote_endpoint, connection_run_home):
384 # Set the remote endpoint
385 self.set("pointopoint", remote_endpoint.get("ip4"))
386 self.set("greRemote", remote_endpoint.node.get("ip"))
388 # Generate GRE connect command
390 command.append(self._stop_command)
391 command.append(") ; (")
392 command.append(self._start_gre_command)
395 command = " ".join(command)
396 command = self.replace_paths(command)
401 def _start_command(self):
403 if not self.gre_enabled:
404 # Make sure to clean TAP if it existed
405 stop_command = self._stop_command
408 start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
409 self.get("deviceName"),
411 "pi" if self.get("pi") else ""))
412 start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
413 start_command.append("sudo -S ip addr add %s/%d dev %s" % (
416 self.get("deviceName"),
419 start_command = ";".join(start_command)
422 command.append(stop_command)
423 command.append(") ; (")
424 command.append(start_command)
427 return " ".join(command)
430 def _stop_command(self):
432 command.append("sudo -S ip link set %s down" % self.get("deviceName"))
433 command.append("sudo -S ip link del %s" % self.get("deviceName"))
435 return ";".join(command)
438 def _start_gre_command(self):
440 command.append("sudo -S modprobe ip_gre")
441 command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
442 self.get("deviceName"),
443 self.get("greRemote"),
447 command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
450 self.get("pointopoint"),
452 self.get("deviceName"),
454 command.append("sudo -S ip link set %s up " % self.get("deviceName"))
456 return ";".join(command)
463 def vif_prefix(self):
467 return os.path.join(self.run_home, "tap.sock")
469 def valid_connection(self, guid):