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 endpoint_ip = Attribute("endpoint_ip", "IPv4 Address",
43 mac = Attribute("mac", "MAC Address",
46 endpoint_prefix = Attribute("endpoint_prefix", "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(endpoint_ip)
87 cls._register_attribute(mac)
88 cls._register_attribute(endpoint_prefix)
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 def initiate_udp_connection(self, remote_endpoint, connection_app_home,
287 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
288 port = self.udp_connect(remote_endpoint, connection_app_home,
289 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen)
292 def udp_connect(self, remote_endpoint, connection_app_home,
293 connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
294 udp_connect_command = self._udp_connect_command(
295 remote_endpoint, connection_run_home,
296 cipher, cipher_key, bwlimit, txqueuelen)
298 # upload command to connect.sh script
299 shfile = os.path.join(self.app_home, "udp-connect.sh")
300 self.node.upload_command(udp_connect_command,
304 # invoke connect script
305 cmd = "bash %s" % shfile
306 (out, err), proc = self.node.run(cmd, self.run_home)
308 # check if execution errors occurred
309 msg = "Failed to connect endpoints "
312 self.error(msg, out, err)
313 raise RuntimeError, msg
315 # Wait for pid file to be generated
316 self._pid, self._ppid = self.node.wait_pid(self.run_home)
318 # If the process is not running, check for error information
319 # on the remote machine
320 if not self._pid or not self._ppid:
321 (out, err), proc = self.node.check_errors(self.run_home)
322 # Out is what was written in the stderr file
324 msg = " Failed to start command '%s' " % command
325 self.error(msg, out, err)
326 raise RuntimeError, msg
328 port = self.wait_local_port()
332 def _udp_connect_command(self, remote_endpoint, connection_run_home,
333 cipher, cipher_key, bwlimit, txqueuelen):
335 # Set the remote endpoint
336 self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
338 # Planetlab TAPs always use PI headers
339 from nepi.resources.planetlab.tap import PlanetlabTap
340 if self.is_rm_instance(PlanetlabTap.get_rtype()):
343 remote_ip = remote_endpoint.node.get("ip")
345 local_port_file = os.path.join(self.run_home,
348 remote_port_file = os.path.join(self.run_home,
351 ret_file = os.path.join(self.run_home,
354 # Generate UDP connect command
355 # Use the start command to configure TAP with peer info
356 start_command = self._start_command
359 command.append(start_command)
361 # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
362 command.append(") & (")
363 command.append("sudo -S")
364 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
365 command.append("python ${SRC}/linux-udp-connect.py")
366 command.append("-N %s" % self.get("deviceName"))
367 command.append("-t %s" % self.vif_type)
370 command.append("-l %s " % local_port_file)
371 command.append("-r %s " % remote_port_file)
372 command.append("-H %s " % remote_ip)
373 command.append("-R %s " % ret_file)
375 command.append("-c %s " % cipher)
377 command.append("-k %s " % cipher_key)
379 command.append("-q %s " % txqueuelen)
381 command.append("-b %s " % bwlimit)
385 command = " ".join(command)
386 command = self.replace_paths(command)
390 def _gre_connect_command(self, remote_endpoint, connection_run_home):
391 # Set the remote endpoint
392 self.set("pointopoint", remote_endpoint.get("endpoint_ip"))
393 self.set("greRemote", remote_endpoint.node.get("ip"))
395 # Generate GRE connect command
397 command.append(self._stop_command)
398 command.append(") ; (")
399 command.append(self._start_gre_command)
402 command = " ".join(command)
403 command = self.replace_paths(command)
407 def establish_udp_connection(self, remote_endpoint, port):
408 # upload remote port number to file
409 rem_port = "%s\n" % port
410 self.node.upload(rem_port,
411 os.path.join(self.run_home, "remote_port"),
415 def verify_connection(self):
418 def terminate_connection(self):
419 if self._pid and self._ppid:
420 (out, err), proc = self.node.kill(self._pid, self._ppid,
423 # check if execution errors occurred
424 if proc.poll() and err:
425 msg = " Failed to Kill the Tap"
426 self.error(msg, out, err)
427 raise RuntimeError, msg
429 def check_status(self):
430 return self.node.status(self._pid, self._ppid)
432 def wait_local_port(self):
433 """ Waits until the local_port file for the endpoint is generated,
434 and returns the port number
437 return self.wait_file("local_port")
439 def wait_result(self):
440 """ Waits until the return code file for the endpoint is generated
443 return self.wait_file("ret_file")
445 def wait_file(self, filename):
446 """ Waits until file on endpoint is generated """
451 (out, err), proc = self.node.check_output(
452 self.run_home, filename)
460 msg = "Couldn't retrieve %s" % filename
461 self.error(msg, out, err)
462 raise RuntimeError, msg
467 def _start_command(self):
469 if not self.gre_enabled:
470 # Make sure to clean TAP if it existed
471 stop_command = self._stop_command
474 start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
475 self.get("deviceName"),
477 "pi" if self.get("pi") else ""))
478 start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
479 start_command.append("sudo -S ip addr add %s/%d dev %s" % (
480 self.get("endpoint_ip"),
481 self.get("endpoint_prefix"),
482 self.get("deviceName"),
485 start_command = ";".join(start_command)
488 command.append(stop_command)
489 command.append(") ; (")
490 command.append(start_command)
493 return " ".join(command)
496 def _stop_command(self):
498 command.append("sudo -S ip link set %s down" % self.get("deviceName"))
499 command.append("sudo -S ip link del %s" % self.get("deviceName"))
501 return ";".join(command)
504 def _start_gre_command(self):
506 command.append("sudo -S modprobe ip_gre")
507 command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
508 self.get("deviceName"),
509 self.get("greRemote"),
513 command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
514 self.get("endpoint_ip"),
515 self.get("endpoint_prefix"),
516 self.get("pointopoint"),
517 self.get("endpoint_prefix"),
518 self.get("deviceName"),
520 command.append("sudo -S ip link set %s up " % self.get("deviceName"))
522 return ";".join(command)
529 def vif_prefix(self):
533 return os.path.join(self.run_home, "tap.sock")
535 def valid_connection(self, guid):