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
22 from nepi.resources.linux.tap import LinuxTap
23 from nepi.resources.planetlab.node import PlanetlabNode
24 from nepi.util.timefuncs import tnow, tdiffsec
29 PYTHON_VSYS_VERSION = "1.0"
32 class PlanetlabTap(LinuxTap):
33 _rtype = "planetlab::Tap"
34 _help = "Creates a TAP device on a PlanetLab host"
35 _platform = "planetlab"
38 def _register_attributes(cls):
39 snat = Attribute("snat", "Set SNAT=1",
43 cls._register_attribute(snat)
45 def __init__(self, ec, guid):
46 super(PlanetlabTap, self).__init__(ec, guid)
47 self._home = "tap-%s" % self.guid
48 self._gre_enabled = False
52 node = self.get_connected(PlanetlabNode.get_rtype())
53 if node: return node[0]
54 raise RuntimeError, "TAP/TUN devices must be connected to Node"
56 def upload_sources(self):
59 # vif-creation python script
60 pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
63 scripts.append(pl_vif_create)
65 # vif-up python script
66 pl_vif_up = os.path.join(os.path.dirname(__file__), "scripts",
69 scripts.append(pl_vif_up)
71 # vif-down python script
72 pl_vif_down = os.path.join(os.path.dirname(__file__), "scripts",
75 scripts.append(pl_vif_down)
77 # udp-connect python script
78 udp_connect = os.path.join(os.path.dirname(__file__),
82 "linux-udp-connect.py")
84 scripts.append(udp_connect)
86 # tunnel creation python script
87 tunchannel = os.path.join(os.path.dirname(__file__),
93 scripts.append(tunchannel)
96 scripts = ";".join(scripts)
98 self.node.upload(scripts,
99 os.path.join(self.node.src_dir),
102 # upload stop.sh script
103 stop_command = self.replace_paths(self._stop_command)
105 self.node.upload_command(stop_command,
106 shfile = os.path.join(self.app_home, "stop.sh"),
107 # Overwrite file every time.
108 # The stop.sh has the path to the socket, which should change
109 # on every experiment run.
112 def upload_start_command(self):
113 super(PlanetlabTap, self).upload_start_command()
115 # Planetlab TAPs always add a PI header
118 if not self.gre_enabled:
119 # After creating the TAP, the pl-vif-create.py script
120 # will write the name of the TAP to a file. We wait until
121 # we can read the interface name from the file.
122 vif_name = self.wait_vif_name()
123 self.set("deviceName", vif_name)
125 def wait_vif_name(self, exec_run_home = None):
126 """ Waits until the vif_name file for the command is generated,
127 and returns the vif_name for the device """
131 # The vif_name file will be created in the tap-home, while the
132 # current execution home might be elsewhere to check for errors
133 # (e.g. could be a tunnel-home)
134 if not exec_run_home:
135 exec_run_home = self.run_home
138 (out, err), proc = self.node.check_output(self.run_home, "vif_name")
141 (out, err), proc = self.node.check_errors(exec_run_home)
144 raise RuntimeError, err
147 vif_name = out.strip()
153 msg = "Couldn't retrieve vif_name"
154 self.error(msg, out, err)
155 raise RuntimeError, msg
159 def gre_connect(self, remote_endpoint, connection_app_home,
160 connection_run_home):
161 super(PlanetlabTap, self).gre_connect(remote_endpoint,
162 connection_app_home, connection_run_home)
163 # After creating the TAP, the pl-vif-create.py script
164 # will write the name of the TAP to a file. We wait until
165 # we can read the interface name from the file.
166 vif_name = self.wait_vif_name(exec_run_home = connection_run_home)
167 self.set("deviceName", vif_name)
171 def _gre_connect_command(self, remote_endpoint, connection_app_home,
172 connection_run_home):
173 # Set the remote endpoint, (private) IP of the device
174 self.set("pointopoint", remote_endpoint.get("ip"))
175 # Public IP of the node
176 self.set("greRemote", remote_endpoint.node.get("ip"))
178 # Generate GRE connect command
180 # Use vif_down command to first kill existing TAP in GRE mode
181 vif_down_command = self._vif_down_command
183 # Use pl-vif-up.py script to configure TAP with peer info
184 vif_up_command = self._vif_up_command
187 command.append(vif_down_command)
188 command.append(") ; (")
189 command.append(vif_up_command)
192 command = " ".join(command)
193 command = self.replace_paths(command)
198 def _start_command(self):
202 command = ["sudo -S python ${SRC}/pl-vif-create.py"]
204 command.append("-t %s" % self.vif_type)
205 command.append("-a %s" % self.get("ip"))
206 command.append("-n %s" % self.get("prefix"))
207 command.append("-f %s " % self.vif_name_file)
208 command.append("-S %s " % self.sock_name)
210 if self.get("snat") == True:
213 if self.get("pointopoint"):
214 command.append("-p %s" % self.get("pointopoint"))
216 if self.get("txqueuelen"):
217 command.append("-q %s" % self.get("txqueuelen"))
219 return " ".join(command)
222 def _stop_command(self):
224 command = self._vif_down_command
226 command = ["sudo -S "]
227 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
228 command.append("python ${SRC}/pl-vif-down.py")
229 command.append("-S %s " % self.sock_name)
230 command = " ".join(command)
235 def _vif_up_command(self):
237 device_name = "%s" % self.guid
239 device_name = self.get("deviceName")
241 # Use pl-vif-up.py script to configure TAP
242 command = ["sudo -S "]
243 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
244 command.append("python ${SRC}/pl-vif-up.py")
245 command.append("-u %s" % self.node.get("username"))
246 command.append("-N %s" % device_name)
247 command.append("-t %s" % self.vif_type)
248 command.append("-a %s" % self.get("ip"))
249 command.append("-n %s" % self.get("prefix"))
251 if self.get("snat") == True:
254 if self.get("pointopoint"):
255 command.append("-p %s" % self.get("pointopoint"))
257 if self.get("txqueuelen"):
258 command.append("-q %s" % self.get("txqueuelen"))
261 command.append("-g %s" % self.get("greKey"))
262 command.append("-G %s" % self.get("greRemote"))
264 command.append("-f %s " % self.vif_name_file)
266 return " ".join(command)
269 def _vif_down_command(self):
271 device_name = "%s" % self.guid
273 device_name = self.get("deviceName")
275 command = ["sudo -S "]
276 command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
277 command.append("python ${SRC}/pl-vif-down.py")
278 command.append("-N %s " % device_name)
281 command.append("-u %s" % self.node.get("username"))
282 command.append("-t %s" % self.vif_type)
285 return " ".join(command)
288 def vif_name_file(self):
289 return os.path.join(self.run_home, "vif_name")
293 # Install python-vsys and python-passfd
294 install_vsys = ( " ( "
295 " python -c 'import vsys, os; vsys.__version__ == \"%(version)s\" or os._exit(1)' "
300 " hg clone http://nepi.inria.fr/code/python-vsys ; "
303 " sudo -S make install "
305 "version": PYTHON_VSYS_VERSION
308 install_passfd = ( " ( python -c 'import passfd' ) "
312 " hg clone http://nepi.inria.fr/code/python-passfd ; "
313 " cd python-passfd ; "
315 " sudo -S make install "
318 return "%s ; %s" % ( install_vsys, install_passfd )
320 def valid_connection(self, guid):