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
31 PYTHON_VSYS_VERSION = "1.0"
34 class LinuxTap(LinuxApplication):
36 _help = "Creates a TAP device on a Linux host"
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 pointopoint = Attribute("pointopoint", "Peer IP address",
64 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
67 txqueuelen = Attribute("txqueuelen", "Length of transmission queue",
70 gre_key = Attribute("greKey",
71 "GRE key to be used to configure GRE tunnel",
75 gre_remote = Attribute("greRemote",
76 "Public IP of remote endpoint for GRE tunnel",
79 pi = Attribute("pi", "Add PI (protocol information) header",
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(pointopoint)
94 cls._register_attribute(txqueuelen)
95 cls._register_attribute(gre_key)
96 cls._register_attribute(gre_remote)
97 cls._register_attribute(pi)
98 cls._register_attribute(tear_down)
100 def __init__(self, ec, guid):
101 super(LinuxTap, self).__init__(ec, guid)
102 self._home = "tap-%s" % self.guid
103 self._gre_enabled = False
104 self._tunnel_mode = False
108 node = self.get_connected(LinuxNode.get_rtype())
109 if node: return node[0]
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
122 def tunnel_mode(self):
123 if not self._tunnel_mode:
124 from nepi.resources.linux.tunnel import LinuxTunnel
125 tunnel = self.get_connected(LinuxTunnel.get_rtype())
126 if tunnel: self._tunnel_mode = True
128 return self._tunnel_mode
130 def upload_sources(self):
133 # udp-connect python script
134 udp_connect = os.path.join(os.path.dirname(__file__), "scripts",
135 "linux-udp-connect.py")
137 scripts.append(udp_connect)
139 # tunnel creation python script
140 tunchannel = os.path.join(os.path.dirname(__file__), "scripts",
143 scripts.append(tunchannel)
146 scripts = ";".join(scripts)
148 self.node.upload(scripts,
149 os.path.join(self.node.src_dir),
152 # upload stop.sh script
153 stop_command = self.replace_paths(self._stop_command)
155 self.node.upload(stop_command,
156 os.path.join(self.app_home, "stop.sh"),
158 # Overwrite file every time.
159 # The stop.sh has the path to the socket, which should change
160 # on every experiment run.
163 def upload_start_command(self):
164 # If GRE mode is enabled, TAP creation is delayed until the
165 # tunnel is established
166 if not self.tunnel_mode:
167 # We want to make sure the device is up and running
168 # before the deploy is over, so we execute the
169 # start script now and wait until it finishes.
170 command = self.get("command")
171 command = self.replace_paths(command)
173 shfile = os.path.join(self.app_home, "start.sh")
174 self.node.run_and_wait(command, self.run_home,
179 if not self.node or self.node.state < ResourceState.PROVISIONED:
180 self.ec.schedule(reschedule_delay, self.deploy)
182 if not self.get("deviceName"):
183 self.set("deviceName", "%s%d" % (self.vif_prefix, self.guid))
185 if not self.get("command"):
186 self.set("command", self._start_command)
194 if self.state == ResourceState.READY:
195 command = self.get("command")
196 self.info("Starting command '%s'" % command)
200 msg = " Failed to execute command '%s'" % command
201 self.error(msg, out, err)
202 raise RuntimeError, msg
205 command = self.get('command') or ''
207 if self.state == ResourceState.STARTED:
208 self.info("Stopping command '%s'" % command)
210 command = "bash %s" % os.path.join(self.app_home, "stop.sh")
211 (out, err), proc = self.execute_command(command,
215 msg = " Failed to stop command '%s' " % command
216 self.error(msg, out, err)
222 state_check_delay = 0.5
223 if self._state == ResourceState.STARTED and \
224 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
226 if self.get("deviceName"):
227 (out, err), proc = self.node.execute("ifconfig")
229 if out.strip().find(self.get("deviceName")) == -1:
230 # tap is not running is not running (socket not found)
233 self._last_state_check = tnow()
237 def do_release(self):
238 # Node needs to wait until all associated RMs are released
240 from nepi.resources.linux.tunnel import LinuxTunnel
241 rms = self.get_connected(LinuxTunnel.get_rtype())
244 if rm.state < ResourceState.STOPPED:
245 self.ec.schedule(reschedule_delay, self.release)
248 super(LinuxTap, self).do_release()
250 def gre_connect(self, remote_endpoint, connection_app_home,
251 connection_run_home):
252 gre_connect_command = self._gre_connect_command(
253 remote_endpoint, connection_run_home)
255 # upload command to connect.sh script
256 shfile = os.path.join(connection_app_home, "gre-connect.sh")
257 self.node.upload(gre_connect_command,
262 # invoke connect script
263 cmd = "bash %s" % shfile
264 (out, err), proc = self.node.run(cmd, connection_run_home)
266 # check if execution errors occurred
267 msg = " Failed to connect endpoints "
269 if proc.poll() or err:
270 self.error(msg, out, err)
271 raise RuntimeError, msg
273 # Wait for pid file to be generated
274 pid, ppid = self.node.wait_pid(connection_run_home)
276 # If the process is not running, check for error information
277 # on the remote machine
278 if not pid or not ppid:
279 (out, err), proc = self.node.check_errors(connection_run_home)
280 # Out is what was written in the stderr file
282 msg = " Failed to start command '%s' " % command
283 self.error(msg, out, err)
284 raise RuntimeError, msg
288 ## XXX: NOT REALLY WORKING YET!
289 def udp_connect(self, remote_endpoint, connection_app_home,
290 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
291 udp_connect_command = self._udp_connect_command(
292 remote_endpoint, connection_run_home,
293 cipher, cipher_key, bwlimit, txqueuelen)
295 # upload command to connect.sh script
296 shfile = os.path.join(connection_app_home, "udp-connect.sh")
297 self.node.upload(udp_connect_command,
302 # invoke connect script
303 cmd = "bash %s" % shfile
304 (out, err), proc = self.node.run(cmd, connection_run_home)
306 # check if execution errors occurred
307 msg = "Failed to connect endpoints "
310 self.error(msg, out, err)
311 raise RuntimeError, msg
313 # Wait for pid file to be generated
314 pid, ppid = self.node.wait_pid(connection_run_home)
316 # If the process is not running, check for error information
317 # on the remote machine
318 if not pid or not ppid:
319 (out, err), proc = self.node.check_errors(connection_run_home)
320 # Out is what was written in the stderr file
322 msg = " Failed to start command '%s' " % command
323 self.error(msg, out, err)
324 raise RuntimeError, msg
328 def _udp_connect_command(self, remote_endpoint, connection_run_home,
329 cipher, cipher_key, bwlimit, txqueuelen):
331 # Set the remote endpoint
332 self.set("pointopoint", remote_endpoint.get("ip4"))
334 # Planetlab TAPs always use PI headers
335 from nepi.resources.planetlab.tap import PlanetlabTap
336 if self.is_rm_instance(PlanetlabTap.get_rtype()):
339 remote_ip = socket.gethostbyname(
340 remote_endpoint.node.get("hostname"))
342 local_port_file = os.path.join(connection_run_home,
345 remote_port_file = os.path.join(connection_run_home,
348 ret_file = os.path.join(connection_run_home,
351 # Generate UDP connect command
352 # Use the start command to configure TAP with peer info
353 start_command = self._start_command
356 command.append(start_command)
358 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
359 command.append(") & (")
360 command.append("sudo -S")
361 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
362 command.append("python ${SRC}/linux-udp-connect.py")
363 command.append("-N %s" % self.get("deviceName"))
364 command.append("-t %s" % self.vif_type)
367 command.append("-l %s " % local_port_file)
368 command.append("-r %s " % remote_port_file)
369 command.append("-H %s " % remote_ip)
370 command.append("-R %s " % ret_file)
372 command.append("-c %s " % cipher)
374 command.append("-k %s " % cipher_key)
376 command.append("-q %s " % txqueuelen)
378 command.append("-b %s " % bwlimit)
382 command = " ".join(command)
383 command = self.replace_paths(command)
387 def _gre_connect_command(self, remote_endpoint, connection_run_home):
388 # Set the remote endpoint
389 self.set("pointopoint", remote_endpoint.get("ip4"))
390 self.set("greRemote", socket.gethostbyname(
391 remote_endpoint.node.get("hostname")))
393 # Generate GRE connect command
395 command.append(self._stop_command)
396 command.append(") ; (")
397 command.append(self._start_gre_command)
400 command = " ".join(command)
401 command = self.replace_paths(command)
406 def _start_command(self):
408 if not self.gre_enabled:
409 # Make sure to clean TAP if it existed
410 stop_command = self._stop_command
413 start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
414 self.get("deviceName"),
416 "pi" if self.get("pi") else ""))
417 start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
418 start_command.append("sudo -S ip addr add %s/%d dev %s" % (
421 self.get("deviceName"),
424 start_command = ";".join(start_command)
427 command.append(stop_command)
428 command.append(") ; (")
429 command.append(start_command)
432 return " ".join(command)
435 def _stop_command(self):
437 command.append("sudo -S ip link set %s down" % self.get("deviceName"))
438 command.append("sudo -S ip link del %s" % self.get("deviceName"))
440 return ";".join(command)
443 def _start_gre_command(self):
445 command.append("sudo -S modprobe ip_gre")
446 command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
447 self.get("deviceName"),
448 self.get("greRemote"),
449 socket.gethostbyname(self.node.get("hostname")),
452 command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
455 self.get("pointopoint"),
457 self.get("deviceName"),
459 command.append("sudo -S ip link set %s up " % self.get("deviceName"))
461 return ";".join(command)
468 def vif_prefix(self):
472 return os.path.join(self.run_home, "tap.sock")
474 def valid_connection(self, guid):