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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState
21 from nepi.resources.linux.tap import LinuxTap
22 from nepi.resources.planetlab.node import PlanetlabNode
23 from nepi.util.timefuncs import tnow, tdiffsec
28 PYTHON_VSYS_VERSION = "1.0"
31 class PlanetlabTap(LinuxTap):
32 _rtype = "planetlab::Tap"
33 _help = "Creates a TAP device on a PlanetLab host"
34 _platform = "planetlab"
37 def _register_attributes(cls):
38 snat = Attribute("snat", "Set SNAT=1",
42 cls._register_attribute(snat)
44 def __init__(self, ec, guid):
45 super(PlanetlabTap, self).__init__(ec, guid)
46 self._home = "tap-%s" % self.guid
47 self._gre_enabled = False
51 node = self.get_connected(PlanetlabNode.get_rtype())
52 if node: return node[0]
53 raise RuntimeError("TAP/TUN devices must be connected to Node")
55 def upload_sources(self):
58 # vif-creation python script
59 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
62 scripts.append(pl_vif_create)
64 # vif-up python script
65 pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
68 scripts.append(pl_vif_up)
70 # vif-down python script
71 pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
74 scripts.append(pl_vif_down)
76 # udp-connect python script
77 udp_connect = os.path.join(os.path.dirname(__file__),
81 "linux-udp-connect.py")
83 scripts.append(udp_connect)
85 # tunnel creation python script
86 tunchannel = os.path.join(os.path.dirname(__file__),
92 scripts.append(tunchannel)
95 scripts = ";".join(scripts)
97 self.node.upload(scripts,
98 os.path.join(self.node.src_dir),
101 # upload stop.sh script
102 stop_command = self.replace_paths(self._stop_command)
104 self.node.upload_command(stop_command,
105 shfile = os.path.join(self.app_home, "stop.sh"),
106 # Overwrite file every time.
107 # The stop.sh has the path to the socket, which should change
108 # on every experiment run.
111 def upload_start_command(self):
112 super(PlanetlabTap, self).upload_start_command()
114 # Planetlab TAPs always add a PI header
117 if not self.gre_enabled:
118 # After creating the TAP, the pl-vif-create.py script
119 # will write the name of the TAP to a file. We wait until
120 # we can read the interface name from the file.
121 vif_name = self.wait_vif_name()
122 self.set("deviceName", vif_name)
124 def wait_vif_name(self, exec_run_home = None):
125 """ Waits until the vif_name file for the command is generated,
126 and returns the vif_name for the device """
130 # The vif_name file will be created in the tap-home, while the
131 # current execution home might be elsewhere to check for errors
132 # (e.g. could be a tunnel-home)
133 if not exec_run_home:
134 exec_run_home = self.run_home
137 (out, err), proc = self.node.check_output(self.run_home, "vif_name")
140 (out, err), proc = self.node.check_errors(exec_run_home)
143 raise RuntimeError(err)
146 vif_name = out.strip()
152 msg = "Couldn't retrieve vif_name"
153 self.error(msg, out, err)
154 raise RuntimeError(msg)
158 def gre_connect(self, remote_endpoint, connection_app_home,
159 connection_run_home):
160 super(PlanetlabTap, self).gre_connect(remote_endpoint,
161 connection_app_home, connection_run_home)
162 # After creating the TAP, the pl-vif-create.py script
163 # will write the name of the TAP to a file. We wait until
164 # we can read the interface name from the file.
165 vif_name = self.wait_vif_name(exec_run_home = connection_run_home)
166 self.set("deviceName", vif_name)
170 def _gre_connect_command(self, remote_endpoint, connection_app_home,
171 connection_run_home):
172 # Set the remote endpoint, (private) IP of the device
173 self.set("pointopoint", remote_endpoint.get("ip"))
174 # Public IP of the node
175 self.set("greRemote", remote_endpoint.node.get("ip"))
177 # Generate GRE connect command
179 # Use vif_down command to first kill existing TAP in GRE mode
180 vif_down_command = self._vif_down_command
182 # Use pl-vif-up.py script to configure TAP with peer info
183 vif_up_command = self._vif_up_command
186 command.append(vif_down_command)
187 command.append(") ; (")
188 command.append(vif_up_command)
191 command = " ".join(command)
192 command = self.replace_paths(command)
197 def _start_command(self):
201 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
203 command.append("-t %s" % self.vif_type)
204 command.append("-a %s" % self.get("ip"))
205 command.append("-n %s" % self.get("prefix"))
206 command.append("-f %s " % self.vif_name_file)
207 command.append("-S %s " % self.sock_name)
209 if self.get("snat") == True:
212 if self.get("pointopoint"):
213 command.append("-p %s" % self.get("pointopoint"))
215 if self.get("txqueuelen"):
216 command.append("-q %s" % self.get("txqueuelen"))
218 return " ".join(command)
221 def _stop_command(self):
223 command = self._vif_down_command
225 command = ["sudo -S "]
226 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
227 command.append("python ${SRC}/pl-vif-down.py")
228 command.append("-S %s " % self.sock_name)
229 command = " ".join(command)
234 def _vif_up_command(self):
236 device_name = "%s" % self.guid
238 device_name = self.get("deviceName")
240 # Use pl-vif-up.py script to configure TAP
241 command = ["sudo -S "]
242 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
243 command.append("python ${SRC}/pl-vif-up.py")
244 command.append("-u %s" % self.node.get("username"))
245 command.append("-N %s" % device_name)
246 command.append("-t %s" % self.vif_type)
247 command.append("-a %s" % self.get("ip"))
248 command.append("-n %s" % self.get("prefix"))
250 if self.get("snat") == True:
253 if self.get("pointopoint"):
254 command.append("-p %s" % self.get("pointopoint"))
256 if self.get("txqueuelen"):
257 command.append("-q %s" % self.get("txqueuelen"))
260 command.append("-g %s" % self.get("greKey"))
261 command.append("-G %s" % self.get("greRemote"))
263 command.append("-f %s " % self.vif_name_file)
265 return " ".join(command)
268 def _vif_down_command(self):
270 device_name = "%s" % self.guid
272 device_name = self.get("deviceName")
274 command = ["sudo -S "]
275 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
276 command.append("python ${SRC}/pl-vif-down.py")
277 command.append("-N %s " % device_name)
280 command.append("-u %s" % self.node.get("username"))
281 command.append("-t %s" % self.vif_type)
284 return " ".join(command)
287 def vif_name_file(self):
288 return os.path.join(self.run_home, "vif_name")
292 # Install python-vsys and python-passfd
293 install_vsys = ( " ( "
294 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
299 " hg clone http://nepi.inria.fr/code/python-vsys ; "
302 " sudo -S make install "
304 "version": PYTHON_VSYS_VERSION
307 install_passfd = ( " ( python -c 'import passfd' ) "
311 " hg clone http://nepi.inria.fr/code/python-passfd ; "
312 " cd python-passfd ; "
314 " sudo -S make install "
317 return "%s ; %s" % ( install_vsys, install_passfd )
319 def valid_connection(self, guid):